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_irq(&ring->syncp);
423 		packets = ring->stats.packets;
424 		bytes   = ring->stats.bytes;
425 	} while (u64_stats_fetch_retry_irq(&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_irq(&ring->syncp);
476 			packets = ring->stats.packets;
477 			bytes   = ring->stats.bytes;
478 		} while (u64_stats_fetch_retry_irq(&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_irq(&p->syncp);
901 			packets = p->stats.packets;
902 			bytes = p->stats.bytes;
903 		} while (u64_stats_fetch_retry_irq(&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_irq(&p->syncp);
919 			packets = p->stats.packets;
920 			bytes = p->stats.bytes;
921 		} while (u64_stats_fetch_retry_irq(&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_irq(&p->syncp);
939 				packets = p->stats.packets;
940 				bytes = p->stats.bytes;
941 			} while (u64_stats_fetch_retry_irq(&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 	kfree(ring->rx_bi);
3570 	ring->xsk_pool = i40e_xsk_pool(ring);
3571 	if (ring->xsk_pool) {
3572 		ret = i40e_alloc_rx_bi_zc(ring);
3573 		if (ret)
3574 			return ret;
3575 		ring->rx_buf_len =
3576 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3577 		/* For AF_XDP ZC, we disallow packets to span on
3578 		 * multiple buffers, thus letting us skip that
3579 		 * handling in the fast-path.
3580 		 */
3581 		chain_len = 1;
3582 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3583 						 MEM_TYPE_XSK_BUFF_POOL,
3584 						 NULL);
3585 		if (ret)
3586 			return ret;
3587 		dev_info(&vsi->back->pdev->dev,
3588 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3589 			 ring->queue_index);
3590 
3591 	} else {
3592 		ret = i40e_alloc_rx_bi(ring);
3593 		if (ret)
3594 			return ret;
3595 		ring->rx_buf_len = vsi->rx_buf_len;
3596 		if (ring->vsi->type == I40E_VSI_MAIN) {
3597 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3598 							 MEM_TYPE_PAGE_SHARED,
3599 							 NULL);
3600 			if (ret)
3601 				return ret;
3602 		}
3603 	}
3604 
3605 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3606 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3607 
3608 	rx_ctx.base = (ring->dma / 128);
3609 	rx_ctx.qlen = ring->count;
3610 
3611 	/* use 16 byte descriptors */
3612 	rx_ctx.dsize = 0;
3613 
3614 	/* descriptor type is always zero
3615 	 * rx_ctx.dtype = 0;
3616 	 */
3617 	rx_ctx.hsplit_0 = 0;
3618 
3619 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3620 	if (hw->revision_id == 0)
3621 		rx_ctx.lrxqthresh = 0;
3622 	else
3623 		rx_ctx.lrxqthresh = 1;
3624 	rx_ctx.crcstrip = 1;
3625 	rx_ctx.l2tsel = 1;
3626 	/* this controls whether VLAN is stripped from inner headers */
3627 	rx_ctx.showiv = 0;
3628 	/* set the prefena field to 1 because the manual says to */
3629 	rx_ctx.prefena = 1;
3630 
3631 	/* clear the context in the HMC */
3632 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3633 	if (err) {
3634 		dev_info(&vsi->back->pdev->dev,
3635 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3636 			 ring->queue_index, pf_q, err);
3637 		return -ENOMEM;
3638 	}
3639 
3640 	/* set the context in the HMC */
3641 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3642 	if (err) {
3643 		dev_info(&vsi->back->pdev->dev,
3644 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3645 			 ring->queue_index, pf_q, err);
3646 		return -ENOMEM;
3647 	}
3648 
3649 	/* configure Rx buffer alignment */
3650 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3651 		clear_ring_build_skb_enabled(ring);
3652 	else
3653 		set_ring_build_skb_enabled(ring);
3654 
3655 	ring->rx_offset = i40e_rx_offset(ring);
3656 
3657 	/* cache tail for quicker writes, and clear the reg before use */
3658 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3659 	writel(0, ring->tail);
3660 
3661 	if (ring->xsk_pool) {
3662 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3663 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3664 	} else {
3665 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3666 	}
3667 	if (!ok) {
3668 		/* Log this in case the user has forgotten to give the kernel
3669 		 * any buffers, even later in the application.
3670 		 */
3671 		dev_info(&vsi->back->pdev->dev,
3672 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3673 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3674 			 ring->queue_index, pf_q);
3675 	}
3676 
3677 	return 0;
3678 }
3679 
3680 /**
3681  * i40e_vsi_configure_tx - Configure the VSI for Tx
3682  * @vsi: VSI structure describing this set of rings and resources
3683  *
3684  * Configure the Tx VSI for operation.
3685  **/
3686 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3687 {
3688 	int err = 0;
3689 	u16 i;
3690 
3691 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3692 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3693 
3694 	if (err || !i40e_enabled_xdp_vsi(vsi))
3695 		return err;
3696 
3697 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3698 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3699 
3700 	return err;
3701 }
3702 
3703 /**
3704  * i40e_vsi_configure_rx - Configure the VSI for Rx
3705  * @vsi: the VSI being configured
3706  *
3707  * Configure the Rx VSI for operation.
3708  **/
3709 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3710 {
3711 	int err = 0;
3712 	u16 i;
3713 
3714 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3715 		vsi->max_frame = I40E_MAX_RXBUFFER;
3716 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3717 #if (PAGE_SIZE < 8192)
3718 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3719 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3720 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3721 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3722 #endif
3723 	} else {
3724 		vsi->max_frame = I40E_MAX_RXBUFFER;
3725 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3726 						       I40E_RXBUFFER_2048;
3727 	}
3728 
3729 	/* set up individual rings */
3730 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3731 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3732 
3733 	return err;
3734 }
3735 
3736 /**
3737  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3738  * @vsi: ptr to the VSI
3739  **/
3740 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3741 {
3742 	struct i40e_ring *tx_ring, *rx_ring;
3743 	u16 qoffset, qcount;
3744 	int i, n;
3745 
3746 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3747 		/* Reset the TC information */
3748 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3749 			rx_ring = vsi->rx_rings[i];
3750 			tx_ring = vsi->tx_rings[i];
3751 			rx_ring->dcb_tc = 0;
3752 			tx_ring->dcb_tc = 0;
3753 		}
3754 		return;
3755 	}
3756 
3757 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3758 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3759 			continue;
3760 
3761 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3762 		qcount = vsi->tc_config.tc_info[n].qcount;
3763 		for (i = qoffset; i < (qoffset + qcount); i++) {
3764 			rx_ring = vsi->rx_rings[i];
3765 			tx_ring = vsi->tx_rings[i];
3766 			rx_ring->dcb_tc = n;
3767 			tx_ring->dcb_tc = n;
3768 		}
3769 	}
3770 }
3771 
3772 /**
3773  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3774  * @vsi: ptr to the VSI
3775  **/
3776 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3777 {
3778 	if (vsi->netdev)
3779 		i40e_set_rx_mode(vsi->netdev);
3780 }
3781 
3782 /**
3783  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3784  * @pf: Pointer to the targeted PF
3785  *
3786  * Set all flow director counters to 0.
3787  */
3788 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3789 {
3790 	pf->fd_tcp4_filter_cnt = 0;
3791 	pf->fd_udp4_filter_cnt = 0;
3792 	pf->fd_sctp4_filter_cnt = 0;
3793 	pf->fd_ip4_filter_cnt = 0;
3794 	pf->fd_tcp6_filter_cnt = 0;
3795 	pf->fd_udp6_filter_cnt = 0;
3796 	pf->fd_sctp6_filter_cnt = 0;
3797 	pf->fd_ip6_filter_cnt = 0;
3798 }
3799 
3800 /**
3801  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3802  * @vsi: Pointer to the targeted VSI
3803  *
3804  * This function replays the hlist on the hw where all the SB Flow Director
3805  * filters were saved.
3806  **/
3807 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3808 {
3809 	struct i40e_fdir_filter *filter;
3810 	struct i40e_pf *pf = vsi->back;
3811 	struct hlist_node *node;
3812 
3813 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3814 		return;
3815 
3816 	/* Reset FDir counters as we're replaying all existing filters */
3817 	i40e_reset_fdir_filter_cnt(pf);
3818 
3819 	hlist_for_each_entry_safe(filter, node,
3820 				  &pf->fdir_filter_list, fdir_node) {
3821 		i40e_add_del_fdir(vsi, filter, true);
3822 	}
3823 }
3824 
3825 /**
3826  * i40e_vsi_configure - Set up the VSI for action
3827  * @vsi: the VSI being configured
3828  **/
3829 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3830 {
3831 	int err;
3832 
3833 	i40e_set_vsi_rx_mode(vsi);
3834 	i40e_restore_vlan(vsi);
3835 	i40e_vsi_config_dcb_rings(vsi);
3836 	err = i40e_vsi_configure_tx(vsi);
3837 	if (!err)
3838 		err = i40e_vsi_configure_rx(vsi);
3839 
3840 	return err;
3841 }
3842 
3843 /**
3844  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3845  * @vsi: the VSI being configured
3846  **/
3847 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3848 {
3849 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3850 	struct i40e_pf *pf = vsi->back;
3851 	struct i40e_hw *hw = &pf->hw;
3852 	u16 vector;
3853 	int i, q;
3854 	u32 qp;
3855 
3856 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3857 	 * and PFINT_LNKLSTn registers, e.g.:
3858 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3859 	 */
3860 	qp = vsi->base_queue;
3861 	vector = vsi->base_vector;
3862 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3863 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3864 
3865 		q_vector->rx.next_update = jiffies + 1;
3866 		q_vector->rx.target_itr =
3867 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3868 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3869 		     q_vector->rx.target_itr >> 1);
3870 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3871 
3872 		q_vector->tx.next_update = jiffies + 1;
3873 		q_vector->tx.target_itr =
3874 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3875 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3876 		     q_vector->tx.target_itr >> 1);
3877 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3878 
3879 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3880 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3881 
3882 		/* begin of linked list for RX queue assigned to this vector */
3883 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3884 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3885 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3886 			u32 val;
3887 
3888 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3889 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3890 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3891 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3892 			      (I40E_QUEUE_TYPE_TX <<
3893 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3894 
3895 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3896 
3897 			if (has_xdp) {
3898 				/* TX queue with next queue set to TX */
3899 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3900 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3901 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3902 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3903 				      (I40E_QUEUE_TYPE_TX <<
3904 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3905 
3906 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3907 			}
3908 			/* TX queue with next RX or end of linked list */
3909 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3910 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3911 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3912 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3913 			      (I40E_QUEUE_TYPE_RX <<
3914 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3915 
3916 			/* Terminate the linked list */
3917 			if (q == (q_vector->num_ringpairs - 1))
3918 				val |= (I40E_QUEUE_END_OF_LIST <<
3919 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3920 
3921 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3922 			qp++;
3923 		}
3924 	}
3925 
3926 	i40e_flush(hw);
3927 }
3928 
3929 /**
3930  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3931  * @pf: pointer to private device data structure
3932  **/
3933 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3934 {
3935 	struct i40e_hw *hw = &pf->hw;
3936 	u32 val;
3937 
3938 	/* clear things first */
3939 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3940 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3941 
3942 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3943 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3944 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3945 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3946 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3947 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3948 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3949 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3950 
3951 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3952 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3953 
3954 	if (pf->flags & I40E_FLAG_PTP)
3955 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3956 
3957 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3958 
3959 	/* SW_ITR_IDX = 0, but don't change INTENA */
3960 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3961 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3962 
3963 	/* OTHER_ITR_IDX = 0 */
3964 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3965 }
3966 
3967 /**
3968  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3969  * @vsi: the VSI being configured
3970  **/
3971 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3972 {
3973 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3974 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3975 	struct i40e_pf *pf = vsi->back;
3976 	struct i40e_hw *hw = &pf->hw;
3977 
3978 	/* set the ITR configuration */
3979 	q_vector->rx.next_update = jiffies + 1;
3980 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3981 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3982 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3983 	q_vector->tx.next_update = jiffies + 1;
3984 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3985 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3986 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3987 
3988 	i40e_enable_misc_int_causes(pf);
3989 
3990 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3991 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3992 
3993 	/* Associate the queue pair to the vector and enable the queue
3994 	 * interrupt RX queue in linked list with next queue set to TX
3995 	 */
3996 	wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
3997 
3998 	if (i40e_enabled_xdp_vsi(vsi)) {
3999 		/* TX queue in linked list with next queue set to TX */
4000 		wr32(hw, I40E_QINT_TQCTL(nextqp),
4001 		     I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
4002 	}
4003 
4004 	/* last TX queue so the next RX queue doesn't matter */
4005 	wr32(hw, I40E_QINT_TQCTL(0),
4006 	     I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
4007 	i40e_flush(hw);
4008 }
4009 
4010 /**
4011  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4012  * @pf: board private structure
4013  **/
4014 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4015 {
4016 	struct i40e_hw *hw = &pf->hw;
4017 
4018 	wr32(hw, I40E_PFINT_DYN_CTL0,
4019 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4020 	i40e_flush(hw);
4021 }
4022 
4023 /**
4024  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4025  * @pf: board private structure
4026  **/
4027 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4028 {
4029 	struct i40e_hw *hw = &pf->hw;
4030 	u32 val;
4031 
4032 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4033 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4034 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4035 
4036 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4037 	i40e_flush(hw);
4038 }
4039 
4040 /**
4041  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4042  * @irq: interrupt number
4043  * @data: pointer to a q_vector
4044  **/
4045 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4046 {
4047 	struct i40e_q_vector *q_vector = data;
4048 
4049 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4050 		return IRQ_HANDLED;
4051 
4052 	napi_schedule_irqoff(&q_vector->napi);
4053 
4054 	return IRQ_HANDLED;
4055 }
4056 
4057 /**
4058  * i40e_irq_affinity_notify - Callback for affinity changes
4059  * @notify: context as to what irq was changed
4060  * @mask: the new affinity mask
4061  *
4062  * This is a callback function used by the irq_set_affinity_notifier function
4063  * so that we may register to receive changes to the irq affinity masks.
4064  **/
4065 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4066 				     const cpumask_t *mask)
4067 {
4068 	struct i40e_q_vector *q_vector =
4069 		container_of(notify, struct i40e_q_vector, affinity_notify);
4070 
4071 	cpumask_copy(&q_vector->affinity_mask, mask);
4072 }
4073 
4074 /**
4075  * i40e_irq_affinity_release - Callback for affinity notifier release
4076  * @ref: internal core kernel usage
4077  *
4078  * This is a callback function used by the irq_set_affinity_notifier function
4079  * to inform the current notification subscriber that they will no longer
4080  * receive notifications.
4081  **/
4082 static void i40e_irq_affinity_release(struct kref *ref) {}
4083 
4084 /**
4085  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4086  * @vsi: the VSI being configured
4087  * @basename: name for the vector
4088  *
4089  * Allocates MSI-X vectors and requests interrupts from the kernel.
4090  **/
4091 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4092 {
4093 	int q_vectors = vsi->num_q_vectors;
4094 	struct i40e_pf *pf = vsi->back;
4095 	int base = vsi->base_vector;
4096 	int rx_int_idx = 0;
4097 	int tx_int_idx = 0;
4098 	int vector, err;
4099 	int irq_num;
4100 	int cpu;
4101 
4102 	for (vector = 0; vector < q_vectors; vector++) {
4103 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4104 
4105 		irq_num = pf->msix_entries[base + vector].vector;
4106 
4107 		if (q_vector->tx.ring && q_vector->rx.ring) {
4108 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4109 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4110 			tx_int_idx++;
4111 		} else if (q_vector->rx.ring) {
4112 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4113 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4114 		} else if (q_vector->tx.ring) {
4115 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4116 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4117 		} else {
4118 			/* skip this unused q_vector */
4119 			continue;
4120 		}
4121 		err = request_irq(irq_num,
4122 				  vsi->irq_handler,
4123 				  0,
4124 				  q_vector->name,
4125 				  q_vector);
4126 		if (err) {
4127 			dev_info(&pf->pdev->dev,
4128 				 "MSIX request_irq failed, error: %d\n", err);
4129 			goto free_queue_irqs;
4130 		}
4131 
4132 		/* register for affinity change notifications */
4133 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4134 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4135 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4136 		/* Spread affinity hints out across online CPUs.
4137 		 *
4138 		 * get_cpu_mask returns a static constant mask with
4139 		 * a permanent lifetime so it's ok to pass to
4140 		 * irq_update_affinity_hint without making a copy.
4141 		 */
4142 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4143 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4144 	}
4145 
4146 	vsi->irqs_ready = true;
4147 	return 0;
4148 
4149 free_queue_irqs:
4150 	while (vector) {
4151 		vector--;
4152 		irq_num = pf->msix_entries[base + vector].vector;
4153 		irq_set_affinity_notifier(irq_num, NULL);
4154 		irq_update_affinity_hint(irq_num, NULL);
4155 		free_irq(irq_num, &vsi->q_vectors[vector]);
4156 	}
4157 	return err;
4158 }
4159 
4160 /**
4161  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4162  * @vsi: the VSI being un-configured
4163  **/
4164 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4165 {
4166 	struct i40e_pf *pf = vsi->back;
4167 	struct i40e_hw *hw = &pf->hw;
4168 	int base = vsi->base_vector;
4169 	int i;
4170 
4171 	/* disable interrupt causation from each queue */
4172 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4173 		u32 val;
4174 
4175 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4176 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4177 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4178 
4179 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4180 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4181 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4182 
4183 		if (!i40e_enabled_xdp_vsi(vsi))
4184 			continue;
4185 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4186 	}
4187 
4188 	/* disable each interrupt */
4189 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4190 		for (i = vsi->base_vector;
4191 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4192 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4193 
4194 		i40e_flush(hw);
4195 		for (i = 0; i < vsi->num_q_vectors; i++)
4196 			synchronize_irq(pf->msix_entries[i + base].vector);
4197 	} else {
4198 		/* Legacy and MSI mode - this stops all interrupt handling */
4199 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4200 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4201 		i40e_flush(hw);
4202 		synchronize_irq(pf->pdev->irq);
4203 	}
4204 }
4205 
4206 /**
4207  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4208  * @vsi: the VSI being configured
4209  **/
4210 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4211 {
4212 	struct i40e_pf *pf = vsi->back;
4213 	int i;
4214 
4215 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4216 		for (i = 0; i < vsi->num_q_vectors; i++)
4217 			i40e_irq_dynamic_enable(vsi, i);
4218 	} else {
4219 		i40e_irq_dynamic_enable_icr0(pf);
4220 	}
4221 
4222 	i40e_flush(&pf->hw);
4223 	return 0;
4224 }
4225 
4226 /**
4227  * i40e_free_misc_vector - Free the vector that handles non-queue events
4228  * @pf: board private structure
4229  **/
4230 static void i40e_free_misc_vector(struct i40e_pf *pf)
4231 {
4232 	/* Disable ICR 0 */
4233 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4234 	i40e_flush(&pf->hw);
4235 
4236 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4237 		free_irq(pf->msix_entries[0].vector, pf);
4238 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4239 	}
4240 }
4241 
4242 /**
4243  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4244  * @irq: interrupt number
4245  * @data: pointer to a q_vector
4246  *
4247  * This is the handler used for all MSI/Legacy interrupts, and deals
4248  * with both queue and non-queue interrupts.  This is also used in
4249  * MSIX mode to handle the non-queue interrupts.
4250  **/
4251 static irqreturn_t i40e_intr(int irq, void *data)
4252 {
4253 	struct i40e_pf *pf = (struct i40e_pf *)data;
4254 	struct i40e_hw *hw = &pf->hw;
4255 	irqreturn_t ret = IRQ_NONE;
4256 	u32 icr0, icr0_remaining;
4257 	u32 val, ena_mask;
4258 
4259 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4260 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4261 
4262 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4263 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4264 		goto enable_intr;
4265 
4266 	/* if interrupt but no bits showing, must be SWINT */
4267 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4268 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4269 		pf->sw_int_count++;
4270 
4271 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4272 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4273 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4274 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4275 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4276 	}
4277 
4278 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4279 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4280 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4281 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4282 
4283 		/* We do not have a way to disarm Queue causes while leaving
4284 		 * interrupt enabled for all other causes, ideally
4285 		 * interrupt should be disabled while we are in NAPI but
4286 		 * this is not a performance path and napi_schedule()
4287 		 * can deal with rescheduling.
4288 		 */
4289 		if (!test_bit(__I40E_DOWN, pf->state))
4290 			napi_schedule_irqoff(&q_vector->napi);
4291 	}
4292 
4293 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4294 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4295 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4296 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4297 	}
4298 
4299 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4300 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4301 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4302 	}
4303 
4304 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4305 		/* disable any further VFLR event notifications */
4306 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4307 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4308 
4309 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4310 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4311 		} else {
4312 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4313 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4314 		}
4315 	}
4316 
4317 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4318 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4319 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4320 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4321 		val = rd32(hw, I40E_GLGEN_RSTAT);
4322 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4323 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4324 		if (val == I40E_RESET_CORER) {
4325 			pf->corer_count++;
4326 		} else if (val == I40E_RESET_GLOBR) {
4327 			pf->globr_count++;
4328 		} else if (val == I40E_RESET_EMPR) {
4329 			pf->empr_count++;
4330 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4331 		}
4332 	}
4333 
4334 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4335 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4336 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4337 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4338 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4339 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4340 	}
4341 
4342 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4343 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4344 
4345 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4346 			schedule_work(&pf->ptp_extts0_work);
4347 
4348 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4349 			i40e_ptp_tx_hwtstamp(pf);
4350 
4351 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4352 	}
4353 
4354 	/* If a critical error is pending we have no choice but to reset the
4355 	 * device.
4356 	 * Report and mask out any remaining unexpected interrupts.
4357 	 */
4358 	icr0_remaining = icr0 & ena_mask;
4359 	if (icr0_remaining) {
4360 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4361 			 icr0_remaining);
4362 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4363 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4364 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4365 			dev_info(&pf->pdev->dev, "device will be reset\n");
4366 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4367 			i40e_service_event_schedule(pf);
4368 		}
4369 		ena_mask &= ~icr0_remaining;
4370 	}
4371 	ret = IRQ_HANDLED;
4372 
4373 enable_intr:
4374 	/* re-enable interrupt causes */
4375 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4376 	if (!test_bit(__I40E_DOWN, pf->state) ||
4377 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4378 		i40e_service_event_schedule(pf);
4379 		i40e_irq_dynamic_enable_icr0(pf);
4380 	}
4381 
4382 	return ret;
4383 }
4384 
4385 /**
4386  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4387  * @tx_ring:  tx ring to clean
4388  * @budget:   how many cleans we're allowed
4389  *
4390  * Returns true if there's any budget left (e.g. the clean is finished)
4391  **/
4392 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4393 {
4394 	struct i40e_vsi *vsi = tx_ring->vsi;
4395 	u16 i = tx_ring->next_to_clean;
4396 	struct i40e_tx_buffer *tx_buf;
4397 	struct i40e_tx_desc *tx_desc;
4398 
4399 	tx_buf = &tx_ring->tx_bi[i];
4400 	tx_desc = I40E_TX_DESC(tx_ring, i);
4401 	i -= tx_ring->count;
4402 
4403 	do {
4404 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4405 
4406 		/* if next_to_watch is not set then there is no work pending */
4407 		if (!eop_desc)
4408 			break;
4409 
4410 		/* prevent any other reads prior to eop_desc */
4411 		smp_rmb();
4412 
4413 		/* if the descriptor isn't done, no work yet to do */
4414 		if (!(eop_desc->cmd_type_offset_bsz &
4415 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4416 			break;
4417 
4418 		/* clear next_to_watch to prevent false hangs */
4419 		tx_buf->next_to_watch = NULL;
4420 
4421 		tx_desc->buffer_addr = 0;
4422 		tx_desc->cmd_type_offset_bsz = 0;
4423 		/* move past filter desc */
4424 		tx_buf++;
4425 		tx_desc++;
4426 		i++;
4427 		if (unlikely(!i)) {
4428 			i -= tx_ring->count;
4429 			tx_buf = tx_ring->tx_bi;
4430 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4431 		}
4432 		/* unmap skb header data */
4433 		dma_unmap_single(tx_ring->dev,
4434 				 dma_unmap_addr(tx_buf, dma),
4435 				 dma_unmap_len(tx_buf, len),
4436 				 DMA_TO_DEVICE);
4437 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4438 			kfree(tx_buf->raw_buf);
4439 
4440 		tx_buf->raw_buf = NULL;
4441 		tx_buf->tx_flags = 0;
4442 		tx_buf->next_to_watch = NULL;
4443 		dma_unmap_len_set(tx_buf, len, 0);
4444 		tx_desc->buffer_addr = 0;
4445 		tx_desc->cmd_type_offset_bsz = 0;
4446 
4447 		/* move us past the eop_desc for start of next FD desc */
4448 		tx_buf++;
4449 		tx_desc++;
4450 		i++;
4451 		if (unlikely(!i)) {
4452 			i -= tx_ring->count;
4453 			tx_buf = tx_ring->tx_bi;
4454 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4455 		}
4456 
4457 		/* update budget accounting */
4458 		budget--;
4459 	} while (likely(budget));
4460 
4461 	i += tx_ring->count;
4462 	tx_ring->next_to_clean = i;
4463 
4464 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4465 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4466 
4467 	return budget > 0;
4468 }
4469 
4470 /**
4471  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4472  * @irq: interrupt number
4473  * @data: pointer to a q_vector
4474  **/
4475 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4476 {
4477 	struct i40e_q_vector *q_vector = data;
4478 	struct i40e_vsi *vsi;
4479 
4480 	if (!q_vector->tx.ring)
4481 		return IRQ_HANDLED;
4482 
4483 	vsi = q_vector->tx.ring->vsi;
4484 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4485 
4486 	return IRQ_HANDLED;
4487 }
4488 
4489 /**
4490  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4491  * @vsi: the VSI being configured
4492  * @v_idx: vector index
4493  * @qp_idx: queue pair index
4494  **/
4495 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4496 {
4497 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4498 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4499 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4500 
4501 	tx_ring->q_vector = q_vector;
4502 	tx_ring->next = q_vector->tx.ring;
4503 	q_vector->tx.ring = tx_ring;
4504 	q_vector->tx.count++;
4505 
4506 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4507 	if (i40e_enabled_xdp_vsi(vsi)) {
4508 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4509 
4510 		xdp_ring->q_vector = q_vector;
4511 		xdp_ring->next = q_vector->tx.ring;
4512 		q_vector->tx.ring = xdp_ring;
4513 		q_vector->tx.count++;
4514 	}
4515 
4516 	rx_ring->q_vector = q_vector;
4517 	rx_ring->next = q_vector->rx.ring;
4518 	q_vector->rx.ring = rx_ring;
4519 	q_vector->rx.count++;
4520 }
4521 
4522 /**
4523  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4524  * @vsi: the VSI being configured
4525  *
4526  * This function maps descriptor rings to the queue-specific vectors
4527  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4528  * one vector per queue pair, but on a constrained vector budget, we
4529  * group the queue pairs as "efficiently" as possible.
4530  **/
4531 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4532 {
4533 	int qp_remaining = vsi->num_queue_pairs;
4534 	int q_vectors = vsi->num_q_vectors;
4535 	int num_ringpairs;
4536 	int v_start = 0;
4537 	int qp_idx = 0;
4538 
4539 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4540 	 * group them so there are multiple queues per vector.
4541 	 * It is also important to go through all the vectors available to be
4542 	 * sure that if we don't use all the vectors, that the remaining vectors
4543 	 * are cleared. This is especially important when decreasing the
4544 	 * number of queues in use.
4545 	 */
4546 	for (; v_start < q_vectors; v_start++) {
4547 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4548 
4549 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4550 
4551 		q_vector->num_ringpairs = num_ringpairs;
4552 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4553 
4554 		q_vector->rx.count = 0;
4555 		q_vector->tx.count = 0;
4556 		q_vector->rx.ring = NULL;
4557 		q_vector->tx.ring = NULL;
4558 
4559 		while (num_ringpairs--) {
4560 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4561 			qp_idx++;
4562 			qp_remaining--;
4563 		}
4564 	}
4565 }
4566 
4567 /**
4568  * i40e_vsi_request_irq - Request IRQ from the OS
4569  * @vsi: the VSI being configured
4570  * @basename: name for the vector
4571  **/
4572 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4573 {
4574 	struct i40e_pf *pf = vsi->back;
4575 	int err;
4576 
4577 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4578 		err = i40e_vsi_request_irq_msix(vsi, basename);
4579 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4580 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4581 				  pf->int_name, pf);
4582 	else
4583 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4584 				  pf->int_name, pf);
4585 
4586 	if (err)
4587 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4588 
4589 	return err;
4590 }
4591 
4592 #ifdef CONFIG_NET_POLL_CONTROLLER
4593 /**
4594  * i40e_netpoll - A Polling 'interrupt' handler
4595  * @netdev: network interface device structure
4596  *
4597  * This is used by netconsole to send skbs without having to re-enable
4598  * interrupts.  It's not called while the normal interrupt routine is executing.
4599  **/
4600 static void i40e_netpoll(struct net_device *netdev)
4601 {
4602 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4603 	struct i40e_vsi *vsi = np->vsi;
4604 	struct i40e_pf *pf = vsi->back;
4605 	int i;
4606 
4607 	/* if interface is down do nothing */
4608 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4609 		return;
4610 
4611 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4612 		for (i = 0; i < vsi->num_q_vectors; i++)
4613 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4614 	} else {
4615 		i40e_intr(pf->pdev->irq, netdev);
4616 	}
4617 }
4618 #endif
4619 
4620 #define I40E_QTX_ENA_WAIT_COUNT 50
4621 
4622 /**
4623  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4624  * @pf: the PF being configured
4625  * @pf_q: the PF queue
4626  * @enable: enable or disable state of the queue
4627  *
4628  * This routine will wait for the given Tx queue of the PF to reach the
4629  * enabled or disabled state.
4630  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4631  * multiple retries; else will return 0 in case of success.
4632  **/
4633 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4634 {
4635 	int i;
4636 	u32 tx_reg;
4637 
4638 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4639 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4640 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4641 			break;
4642 
4643 		usleep_range(10, 20);
4644 	}
4645 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4646 		return -ETIMEDOUT;
4647 
4648 	return 0;
4649 }
4650 
4651 /**
4652  * i40e_control_tx_q - Start or stop a particular Tx queue
4653  * @pf: the PF structure
4654  * @pf_q: the PF queue to configure
4655  * @enable: start or stop the queue
4656  *
4657  * This function enables or disables a single queue. Note that any delay
4658  * required after the operation is expected to be handled by the caller of
4659  * this function.
4660  **/
4661 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4662 {
4663 	struct i40e_hw *hw = &pf->hw;
4664 	u32 tx_reg;
4665 	int i;
4666 
4667 	/* warn the TX unit of coming changes */
4668 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4669 	if (!enable)
4670 		usleep_range(10, 20);
4671 
4672 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4673 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4674 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4675 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4676 			break;
4677 		usleep_range(1000, 2000);
4678 	}
4679 
4680 	/* Skip if the queue is already in the requested state */
4681 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4682 		return;
4683 
4684 	/* turn on/off the queue */
4685 	if (enable) {
4686 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4687 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4688 	} else {
4689 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4690 	}
4691 
4692 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4693 }
4694 
4695 /**
4696  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4697  * @seid: VSI SEID
4698  * @pf: the PF structure
4699  * @pf_q: the PF queue to configure
4700  * @is_xdp: true if the queue is used for XDP
4701  * @enable: start or stop the queue
4702  **/
4703 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4704 			   bool is_xdp, bool enable)
4705 {
4706 	int ret;
4707 
4708 	i40e_control_tx_q(pf, pf_q, enable);
4709 
4710 	/* wait for the change to finish */
4711 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4712 	if (ret) {
4713 		dev_info(&pf->pdev->dev,
4714 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4715 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4716 			 (enable ? "en" : "dis"));
4717 	}
4718 
4719 	return ret;
4720 }
4721 
4722 /**
4723  * i40e_vsi_enable_tx - Start a VSI's rings
4724  * @vsi: the VSI being configured
4725  **/
4726 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4727 {
4728 	struct i40e_pf *pf = vsi->back;
4729 	int i, pf_q, ret = 0;
4730 
4731 	pf_q = vsi->base_queue;
4732 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4733 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4734 					     pf_q,
4735 					     false /*is xdp*/, true);
4736 		if (ret)
4737 			break;
4738 
4739 		if (!i40e_enabled_xdp_vsi(vsi))
4740 			continue;
4741 
4742 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4743 					     pf_q + vsi->alloc_queue_pairs,
4744 					     true /*is xdp*/, true);
4745 		if (ret)
4746 			break;
4747 	}
4748 	return ret;
4749 }
4750 
4751 /**
4752  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4753  * @pf: the PF being configured
4754  * @pf_q: the PF queue
4755  * @enable: enable or disable state of the queue
4756  *
4757  * This routine will wait for the given Rx queue of the PF to reach the
4758  * enabled or disabled state.
4759  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4760  * multiple retries; else will return 0 in case of success.
4761  **/
4762 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4763 {
4764 	int i;
4765 	u32 rx_reg;
4766 
4767 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4768 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4769 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4770 			break;
4771 
4772 		usleep_range(10, 20);
4773 	}
4774 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4775 		return -ETIMEDOUT;
4776 
4777 	return 0;
4778 }
4779 
4780 /**
4781  * i40e_control_rx_q - Start or stop a particular Rx queue
4782  * @pf: the PF structure
4783  * @pf_q: the PF queue to configure
4784  * @enable: start or stop the queue
4785  *
4786  * This function enables or disables a single queue. Note that
4787  * any delay required after the operation is expected to be
4788  * handled by the caller of this function.
4789  **/
4790 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4791 {
4792 	struct i40e_hw *hw = &pf->hw;
4793 	u32 rx_reg;
4794 	int i;
4795 
4796 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4797 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4798 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4799 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4800 			break;
4801 		usleep_range(1000, 2000);
4802 	}
4803 
4804 	/* Skip if the queue is already in the requested state */
4805 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4806 		return;
4807 
4808 	/* turn on/off the queue */
4809 	if (enable)
4810 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4811 	else
4812 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4813 
4814 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4815 }
4816 
4817 /**
4818  * i40e_control_wait_rx_q
4819  * @pf: the PF structure
4820  * @pf_q: queue being configured
4821  * @enable: start or stop the rings
4822  *
4823  * This function enables or disables a single queue along with waiting
4824  * for the change to finish. The caller of this function should handle
4825  * the delays needed in the case of disabling queues.
4826  **/
4827 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4828 {
4829 	int ret = 0;
4830 
4831 	i40e_control_rx_q(pf, pf_q, enable);
4832 
4833 	/* wait for the change to finish */
4834 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4835 	if (ret)
4836 		return ret;
4837 
4838 	return ret;
4839 }
4840 
4841 /**
4842  * i40e_vsi_enable_rx - Start a VSI's rings
4843  * @vsi: the VSI being configured
4844  **/
4845 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4846 {
4847 	struct i40e_pf *pf = vsi->back;
4848 	int i, pf_q, ret = 0;
4849 
4850 	pf_q = vsi->base_queue;
4851 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4852 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4853 		if (ret) {
4854 			dev_info(&pf->pdev->dev,
4855 				 "VSI seid %d Rx ring %d enable timeout\n",
4856 				 vsi->seid, pf_q);
4857 			break;
4858 		}
4859 	}
4860 
4861 	return ret;
4862 }
4863 
4864 /**
4865  * i40e_vsi_start_rings - Start a VSI's rings
4866  * @vsi: the VSI being configured
4867  **/
4868 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4869 {
4870 	int ret = 0;
4871 
4872 	/* do rx first for enable and last for disable */
4873 	ret = i40e_vsi_enable_rx(vsi);
4874 	if (ret)
4875 		return ret;
4876 	ret = i40e_vsi_enable_tx(vsi);
4877 
4878 	return ret;
4879 }
4880 
4881 #define I40E_DISABLE_TX_GAP_MSEC	50
4882 
4883 /**
4884  * i40e_vsi_stop_rings - Stop a VSI's rings
4885  * @vsi: the VSI being configured
4886  **/
4887 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4888 {
4889 	struct i40e_pf *pf = vsi->back;
4890 	int pf_q, err, q_end;
4891 
4892 	/* When port TX is suspended, don't wait */
4893 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4894 		return i40e_vsi_stop_rings_no_wait(vsi);
4895 
4896 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4897 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4898 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4899 
4900 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4901 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4902 		if (err)
4903 			dev_info(&pf->pdev->dev,
4904 				 "VSI seid %d Rx ring %d disable timeout\n",
4905 				 vsi->seid, pf_q);
4906 	}
4907 
4908 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4909 	pf_q = vsi->base_queue;
4910 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4911 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4912 
4913 	i40e_vsi_wait_queues_disabled(vsi);
4914 }
4915 
4916 /**
4917  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4918  * @vsi: the VSI being shutdown
4919  *
4920  * This function stops all the rings for a VSI but does not delay to verify
4921  * that rings have been disabled. It is expected that the caller is shutting
4922  * down multiple VSIs at once and will delay together for all the VSIs after
4923  * initiating the shutdown. This is particularly useful for shutting down lots
4924  * of VFs together. Otherwise, a large delay can be incurred while configuring
4925  * each VSI in serial.
4926  **/
4927 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4928 {
4929 	struct i40e_pf *pf = vsi->back;
4930 	int i, pf_q;
4931 
4932 	pf_q = vsi->base_queue;
4933 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4934 		i40e_control_tx_q(pf, pf_q, false);
4935 		i40e_control_rx_q(pf, pf_q, false);
4936 	}
4937 }
4938 
4939 /**
4940  * i40e_vsi_free_irq - Free the irq association with the OS
4941  * @vsi: the VSI being configured
4942  **/
4943 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4944 {
4945 	struct i40e_pf *pf = vsi->back;
4946 	struct i40e_hw *hw = &pf->hw;
4947 	int base = vsi->base_vector;
4948 	u32 val, qp;
4949 	int i;
4950 
4951 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4952 		if (!vsi->q_vectors)
4953 			return;
4954 
4955 		if (!vsi->irqs_ready)
4956 			return;
4957 
4958 		vsi->irqs_ready = false;
4959 		for (i = 0; i < vsi->num_q_vectors; i++) {
4960 			int irq_num;
4961 			u16 vector;
4962 
4963 			vector = i + base;
4964 			irq_num = pf->msix_entries[vector].vector;
4965 
4966 			/* free only the irqs that were actually requested */
4967 			if (!vsi->q_vectors[i] ||
4968 			    !vsi->q_vectors[i]->num_ringpairs)
4969 				continue;
4970 
4971 			/* clear the affinity notifier in the IRQ descriptor */
4972 			irq_set_affinity_notifier(irq_num, NULL);
4973 			/* remove our suggested affinity mask for this IRQ */
4974 			irq_update_affinity_hint(irq_num, NULL);
4975 			free_irq(irq_num, vsi->q_vectors[i]);
4976 
4977 			/* Tear down the interrupt queue link list
4978 			 *
4979 			 * We know that they come in pairs and always
4980 			 * the Rx first, then the Tx.  To clear the
4981 			 * link list, stick the EOL value into the
4982 			 * next_q field of the registers.
4983 			 */
4984 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4985 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4986 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4987 			val |= I40E_QUEUE_END_OF_LIST
4988 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4989 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4990 
4991 			while (qp != I40E_QUEUE_END_OF_LIST) {
4992 				u32 next;
4993 
4994 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4995 
4996 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4997 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4998 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4999 					 I40E_QINT_RQCTL_INTEVENT_MASK);
5000 
5001 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5002 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5003 
5004 				wr32(hw, I40E_QINT_RQCTL(qp), val);
5005 
5006 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5007 
5008 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
5009 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
5010 
5011 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5012 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5013 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5014 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5015 
5016 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5017 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5018 
5019 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5020 				qp = next;
5021 			}
5022 		}
5023 	} else {
5024 		free_irq(pf->pdev->irq, pf);
5025 
5026 		val = rd32(hw, I40E_PFINT_LNKLST0);
5027 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5028 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5029 		val |= I40E_QUEUE_END_OF_LIST
5030 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5031 		wr32(hw, I40E_PFINT_LNKLST0, val);
5032 
5033 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5034 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5035 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5036 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5037 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5038 
5039 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5040 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5041 
5042 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5043 
5044 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5045 
5046 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5047 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5048 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5049 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5050 
5051 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5052 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5053 
5054 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5055 	}
5056 }
5057 
5058 /**
5059  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5060  * @vsi: the VSI being configured
5061  * @v_idx: Index of vector to be freed
5062  *
5063  * This function frees the memory allocated to the q_vector.  In addition if
5064  * NAPI is enabled it will delete any references to the NAPI struct prior
5065  * to freeing the q_vector.
5066  **/
5067 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5068 {
5069 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5070 	struct i40e_ring *ring;
5071 
5072 	if (!q_vector)
5073 		return;
5074 
5075 	/* disassociate q_vector from rings */
5076 	i40e_for_each_ring(ring, q_vector->tx)
5077 		ring->q_vector = NULL;
5078 
5079 	i40e_for_each_ring(ring, q_vector->rx)
5080 		ring->q_vector = NULL;
5081 
5082 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5083 	if (vsi->netdev)
5084 		netif_napi_del(&q_vector->napi);
5085 
5086 	vsi->q_vectors[v_idx] = NULL;
5087 
5088 	kfree_rcu(q_vector, rcu);
5089 }
5090 
5091 /**
5092  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5093  * @vsi: the VSI being un-configured
5094  *
5095  * This frees the memory allocated to the q_vectors and
5096  * deletes references to the NAPI struct.
5097  **/
5098 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5099 {
5100 	int v_idx;
5101 
5102 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5103 		i40e_free_q_vector(vsi, v_idx);
5104 }
5105 
5106 /**
5107  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5108  * @pf: board private structure
5109  **/
5110 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5111 {
5112 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5113 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5114 		pci_disable_msix(pf->pdev);
5115 		kfree(pf->msix_entries);
5116 		pf->msix_entries = NULL;
5117 		kfree(pf->irq_pile);
5118 		pf->irq_pile = NULL;
5119 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
5120 		pci_disable_msi(pf->pdev);
5121 	}
5122 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
5123 }
5124 
5125 /**
5126  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5127  * @pf: board private structure
5128  *
5129  * We go through and clear interrupt specific resources and reset the structure
5130  * to pre-load conditions
5131  **/
5132 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5133 {
5134 	int i;
5135 
5136 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5137 		i40e_free_misc_vector(pf);
5138 
5139 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5140 		      I40E_IWARP_IRQ_PILE_ID);
5141 
5142 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5143 	for (i = 0; i < pf->num_alloc_vsi; i++)
5144 		if (pf->vsi[i])
5145 			i40e_vsi_free_q_vectors(pf->vsi[i]);
5146 	i40e_reset_interrupt_capability(pf);
5147 }
5148 
5149 /**
5150  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5151  * @vsi: the VSI being configured
5152  **/
5153 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5154 {
5155 	int q_idx;
5156 
5157 	if (!vsi->netdev)
5158 		return;
5159 
5160 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5161 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5162 
5163 		if (q_vector->rx.ring || q_vector->tx.ring)
5164 			napi_enable(&q_vector->napi);
5165 	}
5166 }
5167 
5168 /**
5169  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5170  * @vsi: the VSI being configured
5171  **/
5172 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5173 {
5174 	int q_idx;
5175 
5176 	if (!vsi->netdev)
5177 		return;
5178 
5179 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5180 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5181 
5182 		if (q_vector->rx.ring || q_vector->tx.ring)
5183 			napi_disable(&q_vector->napi);
5184 	}
5185 }
5186 
5187 /**
5188  * i40e_vsi_close - Shut down a VSI
5189  * @vsi: the vsi to be quelled
5190  **/
5191 static void i40e_vsi_close(struct i40e_vsi *vsi)
5192 {
5193 	struct i40e_pf *pf = vsi->back;
5194 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5195 		i40e_down(vsi);
5196 	i40e_vsi_free_irq(vsi);
5197 	i40e_vsi_free_tx_resources(vsi);
5198 	i40e_vsi_free_rx_resources(vsi);
5199 	vsi->current_netdev_flags = 0;
5200 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5201 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5202 		set_bit(__I40E_CLIENT_RESET, pf->state);
5203 }
5204 
5205 /**
5206  * i40e_quiesce_vsi - Pause a given VSI
5207  * @vsi: the VSI being paused
5208  **/
5209 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5210 {
5211 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5212 		return;
5213 
5214 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5215 	if (vsi->netdev && netif_running(vsi->netdev))
5216 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5217 	else
5218 		i40e_vsi_close(vsi);
5219 }
5220 
5221 /**
5222  * i40e_unquiesce_vsi - Resume a given VSI
5223  * @vsi: the VSI being resumed
5224  **/
5225 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5226 {
5227 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5228 		return;
5229 
5230 	if (vsi->netdev && netif_running(vsi->netdev))
5231 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5232 	else
5233 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5234 }
5235 
5236 /**
5237  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5238  * @pf: the PF
5239  **/
5240 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5241 {
5242 	int v;
5243 
5244 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5245 		if (pf->vsi[v])
5246 			i40e_quiesce_vsi(pf->vsi[v]);
5247 	}
5248 }
5249 
5250 /**
5251  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5252  * @pf: the PF
5253  **/
5254 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5255 {
5256 	int v;
5257 
5258 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5259 		if (pf->vsi[v])
5260 			i40e_unquiesce_vsi(pf->vsi[v]);
5261 	}
5262 }
5263 
5264 /**
5265  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5266  * @vsi: the VSI being configured
5267  *
5268  * Wait until all queues on a given VSI have been disabled.
5269  **/
5270 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5271 {
5272 	struct i40e_pf *pf = vsi->back;
5273 	int i, pf_q, ret;
5274 
5275 	pf_q = vsi->base_queue;
5276 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5277 		/* Check and wait for the Tx queue */
5278 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5279 		if (ret) {
5280 			dev_info(&pf->pdev->dev,
5281 				 "VSI seid %d Tx ring %d disable timeout\n",
5282 				 vsi->seid, pf_q);
5283 			return ret;
5284 		}
5285 
5286 		if (!i40e_enabled_xdp_vsi(vsi))
5287 			goto wait_rx;
5288 
5289 		/* Check and wait for the XDP Tx queue */
5290 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5291 				       false);
5292 		if (ret) {
5293 			dev_info(&pf->pdev->dev,
5294 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5295 				 vsi->seid, pf_q);
5296 			return ret;
5297 		}
5298 wait_rx:
5299 		/* Check and wait for the Rx queue */
5300 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5301 		if (ret) {
5302 			dev_info(&pf->pdev->dev,
5303 				 "VSI seid %d Rx ring %d disable timeout\n",
5304 				 vsi->seid, pf_q);
5305 			return ret;
5306 		}
5307 	}
5308 
5309 	return 0;
5310 }
5311 
5312 #ifdef CONFIG_I40E_DCB
5313 /**
5314  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5315  * @pf: the PF
5316  *
5317  * This function waits for the queues to be in disabled state for all the
5318  * VSIs that are managed by this PF.
5319  **/
5320 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5321 {
5322 	int v, ret = 0;
5323 
5324 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5325 		if (pf->vsi[v]) {
5326 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5327 			if (ret)
5328 				break;
5329 		}
5330 	}
5331 
5332 	return ret;
5333 }
5334 
5335 #endif
5336 
5337 /**
5338  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5339  * @pf: pointer to PF
5340  *
5341  * Get TC map for ISCSI PF type that will include iSCSI TC
5342  * and LAN TC.
5343  **/
5344 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5345 {
5346 	struct i40e_dcb_app_priority_table app;
5347 	struct i40e_hw *hw = &pf->hw;
5348 	u8 enabled_tc = 1; /* TC0 is always enabled */
5349 	u8 tc, i;
5350 	/* Get the iSCSI APP TLV */
5351 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5352 
5353 	for (i = 0; i < dcbcfg->numapps; i++) {
5354 		app = dcbcfg->app[i];
5355 		if (app.selector == I40E_APP_SEL_TCPIP &&
5356 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5357 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5358 			enabled_tc |= BIT(tc);
5359 			break;
5360 		}
5361 	}
5362 
5363 	return enabled_tc;
5364 }
5365 
5366 /**
5367  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5368  * @dcbcfg: the corresponding DCBx configuration structure
5369  *
5370  * Return the number of TCs from given DCBx configuration
5371  **/
5372 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5373 {
5374 	int i, tc_unused = 0;
5375 	u8 num_tc = 0;
5376 	u8 ret = 0;
5377 
5378 	/* Scan the ETS Config Priority Table to find
5379 	 * traffic class enabled for a given priority
5380 	 * and create a bitmask of enabled TCs
5381 	 */
5382 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5383 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5384 
5385 	/* Now scan the bitmask to check for
5386 	 * contiguous TCs starting with TC0
5387 	 */
5388 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5389 		if (num_tc & BIT(i)) {
5390 			if (!tc_unused) {
5391 				ret++;
5392 			} else {
5393 				pr_err("Non-contiguous TC - Disabling DCB\n");
5394 				return 1;
5395 			}
5396 		} else {
5397 			tc_unused = 1;
5398 		}
5399 	}
5400 
5401 	/* There is always at least TC0 */
5402 	if (!ret)
5403 		ret = 1;
5404 
5405 	return ret;
5406 }
5407 
5408 /**
5409  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5410  * @dcbcfg: the corresponding DCBx configuration structure
5411  *
5412  * Query the current DCB configuration and return the number of
5413  * traffic classes enabled from the given DCBX config
5414  **/
5415 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5416 {
5417 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5418 	u8 enabled_tc = 1;
5419 	u8 i;
5420 
5421 	for (i = 0; i < num_tc; i++)
5422 		enabled_tc |= BIT(i);
5423 
5424 	return enabled_tc;
5425 }
5426 
5427 /**
5428  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5429  * @pf: PF being queried
5430  *
5431  * Query the current MQPRIO configuration and return the number of
5432  * traffic classes enabled.
5433  **/
5434 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5435 {
5436 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5437 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5438 	u8 enabled_tc = 1, i;
5439 
5440 	for (i = 1; i < num_tc; i++)
5441 		enabled_tc |= BIT(i);
5442 	return enabled_tc;
5443 }
5444 
5445 /**
5446  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5447  * @pf: PF being queried
5448  *
5449  * Return number of traffic classes enabled for the given PF
5450  **/
5451 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5452 {
5453 	struct i40e_hw *hw = &pf->hw;
5454 	u8 i, enabled_tc = 1;
5455 	u8 num_tc = 0;
5456 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5457 
5458 	if (i40e_is_tc_mqprio_enabled(pf))
5459 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5460 
5461 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5462 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5463 		return 1;
5464 
5465 	/* SFP mode will be enabled for all TCs on port */
5466 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5467 		return i40e_dcb_get_num_tc(dcbcfg);
5468 
5469 	/* MFP mode return count of enabled TCs for this PF */
5470 	if (pf->hw.func_caps.iscsi)
5471 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5472 	else
5473 		return 1; /* Only TC0 */
5474 
5475 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5476 		if (enabled_tc & BIT(i))
5477 			num_tc++;
5478 	}
5479 	return num_tc;
5480 }
5481 
5482 /**
5483  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5484  * @pf: PF being queried
5485  *
5486  * Return a bitmap for enabled traffic classes for this PF.
5487  **/
5488 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5489 {
5490 	if (i40e_is_tc_mqprio_enabled(pf))
5491 		return i40e_mqprio_get_enabled_tc(pf);
5492 
5493 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5494 	 * default TC
5495 	 */
5496 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5497 		return I40E_DEFAULT_TRAFFIC_CLASS;
5498 
5499 	/* SFP mode we want PF to be enabled for all TCs */
5500 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5501 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5502 
5503 	/* MFP enabled and iSCSI PF type */
5504 	if (pf->hw.func_caps.iscsi)
5505 		return i40e_get_iscsi_tc_map(pf);
5506 	else
5507 		return I40E_DEFAULT_TRAFFIC_CLASS;
5508 }
5509 
5510 /**
5511  * i40e_vsi_get_bw_info - Query VSI BW Information
5512  * @vsi: the VSI being queried
5513  *
5514  * Returns 0 on success, negative value on failure
5515  **/
5516 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5517 {
5518 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5519 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5520 	struct i40e_pf *pf = vsi->back;
5521 	struct i40e_hw *hw = &pf->hw;
5522 	i40e_status ret;
5523 	u32 tc_bw_max;
5524 	int i;
5525 
5526 	/* Get the VSI level BW configuration */
5527 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5528 	if (ret) {
5529 		dev_info(&pf->pdev->dev,
5530 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5531 			 i40e_stat_str(&pf->hw, ret),
5532 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5533 		return -EINVAL;
5534 	}
5535 
5536 	/* Get the VSI level BW configuration per TC */
5537 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5538 					       NULL);
5539 	if (ret) {
5540 		dev_info(&pf->pdev->dev,
5541 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5542 			 i40e_stat_str(&pf->hw, ret),
5543 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5544 		return -EINVAL;
5545 	}
5546 
5547 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5548 		dev_info(&pf->pdev->dev,
5549 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5550 			 bw_config.tc_valid_bits,
5551 			 bw_ets_config.tc_valid_bits);
5552 		/* Still continuing */
5553 	}
5554 
5555 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5556 	vsi->bw_max_quanta = bw_config.max_bw;
5557 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5558 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5559 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5560 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5561 		vsi->bw_ets_limit_credits[i] =
5562 					le16_to_cpu(bw_ets_config.credits[i]);
5563 		/* 3 bits out of 4 for each TC */
5564 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5565 	}
5566 
5567 	return 0;
5568 }
5569 
5570 /**
5571  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5572  * @vsi: the VSI being configured
5573  * @enabled_tc: TC bitmap
5574  * @bw_share: BW shared credits per TC
5575  *
5576  * Returns 0 on success, negative value on failure
5577  **/
5578 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5579 				       u8 *bw_share)
5580 {
5581 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5582 	struct i40e_pf *pf = vsi->back;
5583 	i40e_status ret;
5584 	int i;
5585 
5586 	/* There is no need to reset BW when mqprio mode is on.  */
5587 	if (i40e_is_tc_mqprio_enabled(pf))
5588 		return 0;
5589 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5590 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5591 		if (ret)
5592 			dev_info(&pf->pdev->dev,
5593 				 "Failed to reset tx rate for vsi->seid %u\n",
5594 				 vsi->seid);
5595 		return ret;
5596 	}
5597 	memset(&bw_data, 0, sizeof(bw_data));
5598 	bw_data.tc_valid_bits = enabled_tc;
5599 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5600 		bw_data.tc_bw_credits[i] = bw_share[i];
5601 
5602 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5603 	if (ret) {
5604 		dev_info(&pf->pdev->dev,
5605 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5606 			 pf->hw.aq.asq_last_status);
5607 		return -EINVAL;
5608 	}
5609 
5610 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5611 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5612 
5613 	return 0;
5614 }
5615 
5616 /**
5617  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5618  * @vsi: the VSI being configured
5619  * @enabled_tc: TC map to be enabled
5620  *
5621  **/
5622 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5623 {
5624 	struct net_device *netdev = vsi->netdev;
5625 	struct i40e_pf *pf = vsi->back;
5626 	struct i40e_hw *hw = &pf->hw;
5627 	u8 netdev_tc = 0;
5628 	int i;
5629 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5630 
5631 	if (!netdev)
5632 		return;
5633 
5634 	if (!enabled_tc) {
5635 		netdev_reset_tc(netdev);
5636 		return;
5637 	}
5638 
5639 	/* Set up actual enabled TCs on the VSI */
5640 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5641 		return;
5642 
5643 	/* set per TC queues for the VSI */
5644 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5645 		/* Only set TC queues for enabled tcs
5646 		 *
5647 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5648 		 * enabled_tc bitmap would be 0x00001001; the driver
5649 		 * will set the numtc for netdev as 2 that will be
5650 		 * referenced by the netdev layer as TC 0 and 1.
5651 		 */
5652 		if (vsi->tc_config.enabled_tc & BIT(i))
5653 			netdev_set_tc_queue(netdev,
5654 					vsi->tc_config.tc_info[i].netdev_tc,
5655 					vsi->tc_config.tc_info[i].qcount,
5656 					vsi->tc_config.tc_info[i].qoffset);
5657 	}
5658 
5659 	if (i40e_is_tc_mqprio_enabled(pf))
5660 		return;
5661 
5662 	/* Assign UP2TC map for the VSI */
5663 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5664 		/* Get the actual TC# for the UP */
5665 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5666 		/* Get the mapped netdev TC# for the UP */
5667 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5668 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5669 	}
5670 }
5671 
5672 /**
5673  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5674  * @vsi: the VSI being configured
5675  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5676  **/
5677 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5678 				      struct i40e_vsi_context *ctxt)
5679 {
5680 	/* copy just the sections touched not the entire info
5681 	 * since not all sections are valid as returned by
5682 	 * update vsi params
5683 	 */
5684 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5685 	memcpy(&vsi->info.queue_mapping,
5686 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5687 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5688 	       sizeof(vsi->info.tc_mapping));
5689 }
5690 
5691 /**
5692  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5693  * @vsi: the VSI being reconfigured
5694  * @vsi_offset: offset from main VF VSI
5695  */
5696 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5697 {
5698 	struct i40e_vsi_context ctxt = {};
5699 	struct i40e_pf *pf;
5700 	struct i40e_hw *hw;
5701 	int ret;
5702 
5703 	if (!vsi)
5704 		return I40E_ERR_PARAM;
5705 	pf = vsi->back;
5706 	hw = &pf->hw;
5707 
5708 	ctxt.seid = vsi->seid;
5709 	ctxt.pf_num = hw->pf_id;
5710 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5711 	ctxt.uplink_seid = vsi->uplink_seid;
5712 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5713 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5714 	ctxt.info = vsi->info;
5715 
5716 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5717 				 false);
5718 	if (vsi->reconfig_rss) {
5719 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5720 				      vsi->num_queue_pairs);
5721 		ret = i40e_vsi_config_rss(vsi);
5722 		if (ret) {
5723 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5724 			return ret;
5725 		}
5726 		vsi->reconfig_rss = false;
5727 	}
5728 
5729 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5730 	if (ret) {
5731 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %s aq_err %s\n",
5732 			 i40e_stat_str(hw, ret),
5733 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5734 		return ret;
5735 	}
5736 	/* update the local VSI info with updated queue map */
5737 	i40e_vsi_update_queue_map(vsi, &ctxt);
5738 	vsi->info.valid_sections = 0;
5739 
5740 	return ret;
5741 }
5742 
5743 /**
5744  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5745  * @vsi: VSI to be configured
5746  * @enabled_tc: TC bitmap
5747  *
5748  * This configures a particular VSI for TCs that are mapped to the
5749  * given TC bitmap. It uses default bandwidth share for TCs across
5750  * VSIs to configure TC for a particular VSI.
5751  *
5752  * NOTE:
5753  * It is expected that the VSI queues have been quisced before calling
5754  * this function.
5755  **/
5756 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5757 {
5758 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5759 	struct i40e_pf *pf = vsi->back;
5760 	struct i40e_hw *hw = &pf->hw;
5761 	struct i40e_vsi_context ctxt;
5762 	int ret = 0;
5763 	int i;
5764 
5765 	/* Check if enabled_tc is same as existing or new TCs */
5766 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5767 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5768 		return ret;
5769 
5770 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5771 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5772 		if (enabled_tc & BIT(i))
5773 			bw_share[i] = 1;
5774 	}
5775 
5776 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5777 	if (ret) {
5778 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5779 
5780 		dev_info(&pf->pdev->dev,
5781 			 "Failed configuring TC map %d for VSI %d\n",
5782 			 enabled_tc, vsi->seid);
5783 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5784 						  &bw_config, NULL);
5785 		if (ret) {
5786 			dev_info(&pf->pdev->dev,
5787 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5788 				 i40e_stat_str(hw, ret),
5789 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5790 			goto out;
5791 		}
5792 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5793 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5794 
5795 			if (!valid_tc)
5796 				valid_tc = bw_config.tc_valid_bits;
5797 			/* Always enable TC0, no matter what */
5798 			valid_tc |= 1;
5799 			dev_info(&pf->pdev->dev,
5800 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5801 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5802 			enabled_tc = valid_tc;
5803 		}
5804 
5805 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5806 		if (ret) {
5807 			dev_err(&pf->pdev->dev,
5808 				"Unable to  configure TC map %d for VSI %d\n",
5809 				enabled_tc, vsi->seid);
5810 			goto out;
5811 		}
5812 	}
5813 
5814 	/* Update Queue Pairs Mapping for currently enabled UPs */
5815 	ctxt.seid = vsi->seid;
5816 	ctxt.pf_num = vsi->back->hw.pf_id;
5817 	ctxt.vf_num = 0;
5818 	ctxt.uplink_seid = vsi->uplink_seid;
5819 	ctxt.info = vsi->info;
5820 	if (i40e_is_tc_mqprio_enabled(pf)) {
5821 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5822 		if (ret)
5823 			goto out;
5824 	} else {
5825 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5826 	}
5827 
5828 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5829 	 * queues changed.
5830 	 */
5831 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5832 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5833 				      vsi->num_queue_pairs);
5834 		ret = i40e_vsi_config_rss(vsi);
5835 		if (ret) {
5836 			dev_info(&vsi->back->pdev->dev,
5837 				 "Failed to reconfig rss for num_queues\n");
5838 			return ret;
5839 		}
5840 		vsi->reconfig_rss = false;
5841 	}
5842 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5843 		ctxt.info.valid_sections |=
5844 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5845 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5846 	}
5847 
5848 	/* Update the VSI after updating the VSI queue-mapping
5849 	 * information
5850 	 */
5851 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5852 	if (ret) {
5853 		dev_info(&pf->pdev->dev,
5854 			 "Update vsi tc config failed, err %s aq_err %s\n",
5855 			 i40e_stat_str(hw, ret),
5856 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5857 		goto out;
5858 	}
5859 	/* update the local VSI info with updated queue map */
5860 	i40e_vsi_update_queue_map(vsi, &ctxt);
5861 	vsi->info.valid_sections = 0;
5862 
5863 	/* Update current VSI BW information */
5864 	ret = i40e_vsi_get_bw_info(vsi);
5865 	if (ret) {
5866 		dev_info(&pf->pdev->dev,
5867 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5868 			 i40e_stat_str(hw, ret),
5869 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5870 		goto out;
5871 	}
5872 
5873 	/* Update the netdev TC setup */
5874 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5875 out:
5876 	return ret;
5877 }
5878 
5879 /**
5880  * i40e_get_link_speed - Returns link speed for the interface
5881  * @vsi: VSI to be configured
5882  *
5883  **/
5884 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5885 {
5886 	struct i40e_pf *pf = vsi->back;
5887 
5888 	switch (pf->hw.phy.link_info.link_speed) {
5889 	case I40E_LINK_SPEED_40GB:
5890 		return 40000;
5891 	case I40E_LINK_SPEED_25GB:
5892 		return 25000;
5893 	case I40E_LINK_SPEED_20GB:
5894 		return 20000;
5895 	case I40E_LINK_SPEED_10GB:
5896 		return 10000;
5897 	case I40E_LINK_SPEED_1GB:
5898 		return 1000;
5899 	default:
5900 		return -EINVAL;
5901 	}
5902 }
5903 
5904 /**
5905  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5906  * @vsi: Pointer to vsi structure
5907  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5908  *
5909  * Helper function to convert units before send to set BW limit
5910  **/
5911 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5912 {
5913 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5914 		dev_warn(&vsi->back->pdev->dev,
5915 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5916 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5917 	} else {
5918 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5919 	}
5920 
5921 	return max_tx_rate;
5922 }
5923 
5924 /**
5925  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5926  * @vsi: VSI to be configured
5927  * @seid: seid of the channel/VSI
5928  * @max_tx_rate: max TX rate to be configured as BW limit
5929  *
5930  * Helper function to set BW limit for a given VSI
5931  **/
5932 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5933 {
5934 	struct i40e_pf *pf = vsi->back;
5935 	u64 credits = 0;
5936 	int speed = 0;
5937 	int ret = 0;
5938 
5939 	speed = i40e_get_link_speed(vsi);
5940 	if (max_tx_rate > speed) {
5941 		dev_err(&pf->pdev->dev,
5942 			"Invalid max tx rate %llu specified for VSI seid %d.",
5943 			max_tx_rate, seid);
5944 		return -EINVAL;
5945 	}
5946 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
5947 		dev_warn(&pf->pdev->dev,
5948 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5949 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5950 	}
5951 
5952 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5953 	credits = max_tx_rate;
5954 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5955 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5956 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5957 	if (ret)
5958 		dev_err(&pf->pdev->dev,
5959 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5960 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5961 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5962 	return ret;
5963 }
5964 
5965 /**
5966  * i40e_remove_queue_channels - Remove queue channels for the TCs
5967  * @vsi: VSI to be configured
5968  *
5969  * Remove queue channels for the TCs
5970  **/
5971 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5972 {
5973 	enum i40e_admin_queue_err last_aq_status;
5974 	struct i40e_cloud_filter *cfilter;
5975 	struct i40e_channel *ch, *ch_tmp;
5976 	struct i40e_pf *pf = vsi->back;
5977 	struct hlist_node *node;
5978 	int ret, i;
5979 
5980 	/* Reset rss size that was stored when reconfiguring rss for
5981 	 * channel VSIs with non-power-of-2 queue count.
5982 	 */
5983 	vsi->current_rss_size = 0;
5984 
5985 	/* perform cleanup for channels if they exist */
5986 	if (list_empty(&vsi->ch_list))
5987 		return;
5988 
5989 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5990 		struct i40e_vsi *p_vsi;
5991 
5992 		list_del(&ch->list);
5993 		p_vsi = ch->parent_vsi;
5994 		if (!p_vsi || !ch->initialized) {
5995 			kfree(ch);
5996 			continue;
5997 		}
5998 		/* Reset queue contexts */
5999 		for (i = 0; i < ch->num_queue_pairs; i++) {
6000 			struct i40e_ring *tx_ring, *rx_ring;
6001 			u16 pf_q;
6002 
6003 			pf_q = ch->base_queue + i;
6004 			tx_ring = vsi->tx_rings[pf_q];
6005 			tx_ring->ch = NULL;
6006 
6007 			rx_ring = vsi->rx_rings[pf_q];
6008 			rx_ring->ch = NULL;
6009 		}
6010 
6011 		/* Reset BW configured for this VSI via mqprio */
6012 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6013 		if (ret)
6014 			dev_info(&vsi->back->pdev->dev,
6015 				 "Failed to reset tx rate for ch->seid %u\n",
6016 				 ch->seid);
6017 
6018 		/* delete cloud filters associated with this channel */
6019 		hlist_for_each_entry_safe(cfilter, node,
6020 					  &pf->cloud_filter_list, cloud_node) {
6021 			if (cfilter->seid != ch->seid)
6022 				continue;
6023 
6024 			hash_del(&cfilter->cloud_node);
6025 			if (cfilter->dst_port)
6026 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6027 									cfilter,
6028 									false);
6029 			else
6030 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6031 								false);
6032 			last_aq_status = pf->hw.aq.asq_last_status;
6033 			if (ret)
6034 				dev_info(&pf->pdev->dev,
6035 					 "Failed to delete cloud filter, err %s aq_err %s\n",
6036 					 i40e_stat_str(&pf->hw, ret),
6037 					 i40e_aq_str(&pf->hw, last_aq_status));
6038 			kfree(cfilter);
6039 		}
6040 
6041 		/* delete VSI from FW */
6042 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6043 					     NULL);
6044 		if (ret)
6045 			dev_err(&vsi->back->pdev->dev,
6046 				"unable to remove channel (%d) for parent VSI(%d)\n",
6047 				ch->seid, p_vsi->seid);
6048 		kfree(ch);
6049 	}
6050 	INIT_LIST_HEAD(&vsi->ch_list);
6051 }
6052 
6053 /**
6054  * i40e_get_max_queues_for_channel
6055  * @vsi: ptr to VSI to which channels are associated with
6056  *
6057  * Helper function which returns max value among the queue counts set on the
6058  * channels/TCs created.
6059  **/
6060 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6061 {
6062 	struct i40e_channel *ch, *ch_tmp;
6063 	int max = 0;
6064 
6065 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6066 		if (!ch->initialized)
6067 			continue;
6068 		if (ch->num_queue_pairs > max)
6069 			max = ch->num_queue_pairs;
6070 	}
6071 
6072 	return max;
6073 }
6074 
6075 /**
6076  * i40e_validate_num_queues - validate num_queues w.r.t channel
6077  * @pf: ptr to PF device
6078  * @num_queues: number of queues
6079  * @vsi: the parent VSI
6080  * @reconfig_rss: indicates should the RSS be reconfigured or not
6081  *
6082  * This function validates number of queues in the context of new channel
6083  * which is being established and determines if RSS should be reconfigured
6084  * or not for parent VSI.
6085  **/
6086 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6087 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6088 {
6089 	int max_ch_queues;
6090 
6091 	if (!reconfig_rss)
6092 		return -EINVAL;
6093 
6094 	*reconfig_rss = false;
6095 	if (vsi->current_rss_size) {
6096 		if (num_queues > vsi->current_rss_size) {
6097 			dev_dbg(&pf->pdev->dev,
6098 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6099 				num_queues, vsi->current_rss_size);
6100 			return -EINVAL;
6101 		} else if ((num_queues < vsi->current_rss_size) &&
6102 			   (!is_power_of_2(num_queues))) {
6103 			dev_dbg(&pf->pdev->dev,
6104 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6105 				num_queues, vsi->current_rss_size);
6106 			return -EINVAL;
6107 		}
6108 	}
6109 
6110 	if (!is_power_of_2(num_queues)) {
6111 		/* Find the max num_queues configured for channel if channel
6112 		 * exist.
6113 		 * if channel exist, then enforce 'num_queues' to be more than
6114 		 * max ever queues configured for channel.
6115 		 */
6116 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6117 		if (num_queues < max_ch_queues) {
6118 			dev_dbg(&pf->pdev->dev,
6119 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6120 				num_queues, max_ch_queues);
6121 			return -EINVAL;
6122 		}
6123 		*reconfig_rss = true;
6124 	}
6125 
6126 	return 0;
6127 }
6128 
6129 /**
6130  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6131  * @vsi: the VSI being setup
6132  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6133  *
6134  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6135  **/
6136 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6137 {
6138 	struct i40e_pf *pf = vsi->back;
6139 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6140 	struct i40e_hw *hw = &pf->hw;
6141 	int local_rss_size;
6142 	u8 *lut;
6143 	int ret;
6144 
6145 	if (!vsi->rss_size)
6146 		return -EINVAL;
6147 
6148 	if (rss_size > vsi->rss_size)
6149 		return -EINVAL;
6150 
6151 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6152 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6153 	if (!lut)
6154 		return -ENOMEM;
6155 
6156 	/* Ignoring user configured lut if there is one */
6157 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6158 
6159 	/* Use user configured hash key if there is one, otherwise
6160 	 * use default.
6161 	 */
6162 	if (vsi->rss_hkey_user)
6163 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6164 	else
6165 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6166 
6167 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6168 	if (ret) {
6169 		dev_info(&pf->pdev->dev,
6170 			 "Cannot set RSS lut, err %s aq_err %s\n",
6171 			 i40e_stat_str(hw, ret),
6172 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6173 		kfree(lut);
6174 		return ret;
6175 	}
6176 	kfree(lut);
6177 
6178 	/* Do the update w.r.t. storing rss_size */
6179 	if (!vsi->orig_rss_size)
6180 		vsi->orig_rss_size = vsi->rss_size;
6181 	vsi->current_rss_size = local_rss_size;
6182 
6183 	return ret;
6184 }
6185 
6186 /**
6187  * i40e_channel_setup_queue_map - Setup a channel queue map
6188  * @pf: ptr to PF device
6189  * @ctxt: VSI context structure
6190  * @ch: ptr to channel structure
6191  *
6192  * Setup queue map for a specific channel
6193  **/
6194 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6195 					 struct i40e_vsi_context *ctxt,
6196 					 struct i40e_channel *ch)
6197 {
6198 	u16 qcount, qmap, sections = 0;
6199 	u8 offset = 0;
6200 	int pow;
6201 
6202 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6203 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6204 
6205 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6206 	ch->num_queue_pairs = qcount;
6207 
6208 	/* find the next higher power-of-2 of num queue pairs */
6209 	pow = ilog2(qcount);
6210 	if (!is_power_of_2(qcount))
6211 		pow++;
6212 
6213 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6214 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6215 
6216 	/* Setup queue TC[0].qmap for given VSI context */
6217 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6218 
6219 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6220 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6221 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6222 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6223 }
6224 
6225 /**
6226  * i40e_add_channel - add a channel by adding VSI
6227  * @pf: ptr to PF device
6228  * @uplink_seid: underlying HW switching element (VEB) ID
6229  * @ch: ptr to channel structure
6230  *
6231  * Add a channel (VSI) using add_vsi and queue_map
6232  **/
6233 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6234 			    struct i40e_channel *ch)
6235 {
6236 	struct i40e_hw *hw = &pf->hw;
6237 	struct i40e_vsi_context ctxt;
6238 	u8 enabled_tc = 0x1; /* TC0 enabled */
6239 	int ret;
6240 
6241 	if (ch->type != I40E_VSI_VMDQ2) {
6242 		dev_info(&pf->pdev->dev,
6243 			 "add new vsi failed, ch->type %d\n", ch->type);
6244 		return -EINVAL;
6245 	}
6246 
6247 	memset(&ctxt, 0, sizeof(ctxt));
6248 	ctxt.pf_num = hw->pf_id;
6249 	ctxt.vf_num = 0;
6250 	ctxt.uplink_seid = uplink_seid;
6251 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6252 	if (ch->type == I40E_VSI_VMDQ2)
6253 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6254 
6255 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6256 		ctxt.info.valid_sections |=
6257 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6258 		ctxt.info.switch_id =
6259 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6260 	}
6261 
6262 	/* Set queue map for a given VSI context */
6263 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6264 
6265 	/* Now time to create VSI */
6266 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6267 	if (ret) {
6268 		dev_info(&pf->pdev->dev,
6269 			 "add new vsi failed, err %s aq_err %s\n",
6270 			 i40e_stat_str(&pf->hw, ret),
6271 			 i40e_aq_str(&pf->hw,
6272 				     pf->hw.aq.asq_last_status));
6273 		return -ENOENT;
6274 	}
6275 
6276 	/* Success, update channel, set enabled_tc only if the channel
6277 	 * is not a macvlan
6278 	 */
6279 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6280 	ch->seid = ctxt.seid;
6281 	ch->vsi_number = ctxt.vsi_number;
6282 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6283 
6284 	/* copy just the sections touched not the entire info
6285 	 * since not all sections are valid as returned by
6286 	 * update vsi params
6287 	 */
6288 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6289 	memcpy(&ch->info.queue_mapping,
6290 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6291 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6292 	       sizeof(ctxt.info.tc_mapping));
6293 
6294 	return 0;
6295 }
6296 
6297 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6298 				  u8 *bw_share)
6299 {
6300 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6301 	i40e_status ret;
6302 	int i;
6303 
6304 	memset(&bw_data, 0, sizeof(bw_data));
6305 	bw_data.tc_valid_bits = ch->enabled_tc;
6306 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6307 		bw_data.tc_bw_credits[i] = bw_share[i];
6308 
6309 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6310 				       &bw_data, NULL);
6311 	if (ret) {
6312 		dev_info(&vsi->back->pdev->dev,
6313 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6314 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6315 		return -EINVAL;
6316 	}
6317 
6318 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6319 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6320 
6321 	return 0;
6322 }
6323 
6324 /**
6325  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6326  * @pf: ptr to PF device
6327  * @vsi: the VSI being setup
6328  * @ch: ptr to channel structure
6329  *
6330  * Configure TX rings associated with channel (VSI) since queues are being
6331  * from parent VSI.
6332  **/
6333 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6334 				       struct i40e_vsi *vsi,
6335 				       struct i40e_channel *ch)
6336 {
6337 	i40e_status ret;
6338 	int i;
6339 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6340 
6341 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6342 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6343 		if (ch->enabled_tc & BIT(i))
6344 			bw_share[i] = 1;
6345 	}
6346 
6347 	/* configure BW for new VSI */
6348 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6349 	if (ret) {
6350 		dev_info(&vsi->back->pdev->dev,
6351 			 "Failed configuring TC map %d for channel (seid %u)\n",
6352 			 ch->enabled_tc, ch->seid);
6353 		return ret;
6354 	}
6355 
6356 	for (i = 0; i < ch->num_queue_pairs; i++) {
6357 		struct i40e_ring *tx_ring, *rx_ring;
6358 		u16 pf_q;
6359 
6360 		pf_q = ch->base_queue + i;
6361 
6362 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6363 		 * context
6364 		 */
6365 		tx_ring = vsi->tx_rings[pf_q];
6366 		tx_ring->ch = ch;
6367 
6368 		/* Get the RX ring ptr */
6369 		rx_ring = vsi->rx_rings[pf_q];
6370 		rx_ring->ch = ch;
6371 	}
6372 
6373 	return 0;
6374 }
6375 
6376 /**
6377  * i40e_setup_hw_channel - setup new channel
6378  * @pf: ptr to PF device
6379  * @vsi: the VSI being setup
6380  * @ch: ptr to channel structure
6381  * @uplink_seid: underlying HW switching element (VEB) ID
6382  * @type: type of channel to be created (VMDq2/VF)
6383  *
6384  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6385  * and configures TX rings accordingly
6386  **/
6387 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6388 					struct i40e_vsi *vsi,
6389 					struct i40e_channel *ch,
6390 					u16 uplink_seid, u8 type)
6391 {
6392 	int ret;
6393 
6394 	ch->initialized = false;
6395 	ch->base_queue = vsi->next_base_queue;
6396 	ch->type = type;
6397 
6398 	/* Proceed with creation of channel (VMDq2) VSI */
6399 	ret = i40e_add_channel(pf, uplink_seid, ch);
6400 	if (ret) {
6401 		dev_info(&pf->pdev->dev,
6402 			 "failed to add_channel using uplink_seid %u\n",
6403 			 uplink_seid);
6404 		return ret;
6405 	}
6406 
6407 	/* Mark the successful creation of channel */
6408 	ch->initialized = true;
6409 
6410 	/* Reconfigure TX queues using QTX_CTL register */
6411 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6412 	if (ret) {
6413 		dev_info(&pf->pdev->dev,
6414 			 "failed to configure TX rings for channel %u\n",
6415 			 ch->seid);
6416 		return ret;
6417 	}
6418 
6419 	/* update 'next_base_queue' */
6420 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6421 	dev_dbg(&pf->pdev->dev,
6422 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6423 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6424 		ch->num_queue_pairs,
6425 		vsi->next_base_queue);
6426 	return ret;
6427 }
6428 
6429 /**
6430  * i40e_setup_channel - setup new channel using uplink element
6431  * @pf: ptr to PF device
6432  * @vsi: pointer to the VSI to set up the channel within
6433  * @ch: ptr to channel structure
6434  *
6435  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6436  * and uplink switching element (uplink_seid)
6437  **/
6438 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6439 			       struct i40e_channel *ch)
6440 {
6441 	u8 vsi_type;
6442 	u16 seid;
6443 	int ret;
6444 
6445 	if (vsi->type == I40E_VSI_MAIN) {
6446 		vsi_type = I40E_VSI_VMDQ2;
6447 	} else {
6448 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6449 			vsi->type);
6450 		return false;
6451 	}
6452 
6453 	/* underlying switching element */
6454 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6455 
6456 	/* create channel (VSI), configure TX rings */
6457 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6458 	if (ret) {
6459 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6460 		return false;
6461 	}
6462 
6463 	return ch->initialized ? true : false;
6464 }
6465 
6466 /**
6467  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6468  * @vsi: ptr to VSI which has PF backing
6469  *
6470  * Sets up switch mode correctly if it needs to be changed and perform
6471  * what are allowed modes.
6472  **/
6473 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6474 {
6475 	u8 mode;
6476 	struct i40e_pf *pf = vsi->back;
6477 	struct i40e_hw *hw = &pf->hw;
6478 	int ret;
6479 
6480 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6481 	if (ret)
6482 		return -EINVAL;
6483 
6484 	if (hw->dev_caps.switch_mode) {
6485 		/* if switch mode is set, support mode2 (non-tunneled for
6486 		 * cloud filter) for now
6487 		 */
6488 		u32 switch_mode = hw->dev_caps.switch_mode &
6489 				  I40E_SWITCH_MODE_MASK;
6490 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6491 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6492 				return 0;
6493 			dev_err(&pf->pdev->dev,
6494 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6495 				hw->dev_caps.switch_mode);
6496 			return -EINVAL;
6497 		}
6498 	}
6499 
6500 	/* Set Bit 7 to be valid */
6501 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6502 
6503 	/* Set L4type for TCP support */
6504 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6505 
6506 	/* Set cloud filter mode */
6507 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6508 
6509 	/* Prep mode field for set_switch_config */
6510 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6511 					pf->last_sw_conf_valid_flags,
6512 					mode, NULL);
6513 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6514 		dev_err(&pf->pdev->dev,
6515 			"couldn't set switch config bits, err %s aq_err %s\n",
6516 			i40e_stat_str(hw, ret),
6517 			i40e_aq_str(hw,
6518 				    hw->aq.asq_last_status));
6519 
6520 	return ret;
6521 }
6522 
6523 /**
6524  * i40e_create_queue_channel - function to create channel
6525  * @vsi: VSI to be configured
6526  * @ch: ptr to channel (it contains channel specific params)
6527  *
6528  * This function creates channel (VSI) using num_queues specified by user,
6529  * reconfigs RSS if needed.
6530  **/
6531 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6532 			      struct i40e_channel *ch)
6533 {
6534 	struct i40e_pf *pf = vsi->back;
6535 	bool reconfig_rss;
6536 	int err;
6537 
6538 	if (!ch)
6539 		return -EINVAL;
6540 
6541 	if (!ch->num_queue_pairs) {
6542 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6543 			ch->num_queue_pairs);
6544 		return -EINVAL;
6545 	}
6546 
6547 	/* validate user requested num_queues for channel */
6548 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6549 				       &reconfig_rss);
6550 	if (err) {
6551 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6552 			 ch->num_queue_pairs);
6553 		return -EINVAL;
6554 	}
6555 
6556 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6557 	 * VSI to be added switch to VEB mode.
6558 	 */
6559 
6560 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6561 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6562 
6563 		if (vsi->type == I40E_VSI_MAIN) {
6564 			if (i40e_is_tc_mqprio_enabled(pf))
6565 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6566 			else
6567 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6568 		}
6569 		/* now onwards for main VSI, number of queues will be value
6570 		 * of TC0's queue count
6571 		 */
6572 	}
6573 
6574 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6575 	 * it should be more than num_queues
6576 	 */
6577 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6578 		dev_dbg(&pf->pdev->dev,
6579 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6580 			vsi->cnt_q_avail, ch->num_queue_pairs);
6581 		return -EINVAL;
6582 	}
6583 
6584 	/* reconfig_rss only if vsi type is MAIN_VSI */
6585 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6586 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6587 		if (err) {
6588 			dev_info(&pf->pdev->dev,
6589 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6590 				 ch->num_queue_pairs);
6591 			return -EINVAL;
6592 		}
6593 	}
6594 
6595 	if (!i40e_setup_channel(pf, vsi, ch)) {
6596 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6597 		return -EINVAL;
6598 	}
6599 
6600 	dev_info(&pf->pdev->dev,
6601 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6602 		 ch->seid, ch->num_queue_pairs);
6603 
6604 	/* configure VSI for BW limit */
6605 	if (ch->max_tx_rate) {
6606 		u64 credits = ch->max_tx_rate;
6607 
6608 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6609 			return -EINVAL;
6610 
6611 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6612 		dev_dbg(&pf->pdev->dev,
6613 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6614 			ch->max_tx_rate,
6615 			credits,
6616 			ch->seid);
6617 	}
6618 
6619 	/* in case of VF, this will be main SRIOV VSI */
6620 	ch->parent_vsi = vsi;
6621 
6622 	/* and update main_vsi's count for queue_available to use */
6623 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6624 
6625 	return 0;
6626 }
6627 
6628 /**
6629  * i40e_configure_queue_channels - Add queue channel for the given TCs
6630  * @vsi: VSI to be configured
6631  *
6632  * Configures queue channel mapping to the given TCs
6633  **/
6634 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6635 {
6636 	struct i40e_channel *ch;
6637 	u64 max_rate = 0;
6638 	int ret = 0, i;
6639 
6640 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6641 	vsi->tc_seid_map[0] = vsi->seid;
6642 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6643 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6644 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6645 			if (!ch) {
6646 				ret = -ENOMEM;
6647 				goto err_free;
6648 			}
6649 
6650 			INIT_LIST_HEAD(&ch->list);
6651 			ch->num_queue_pairs =
6652 				vsi->tc_config.tc_info[i].qcount;
6653 			ch->base_queue =
6654 				vsi->tc_config.tc_info[i].qoffset;
6655 
6656 			/* Bandwidth limit through tc interface is in bytes/s,
6657 			 * change to Mbit/s
6658 			 */
6659 			max_rate = vsi->mqprio_qopt.max_rate[i];
6660 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6661 			ch->max_tx_rate = max_rate;
6662 
6663 			list_add_tail(&ch->list, &vsi->ch_list);
6664 
6665 			ret = i40e_create_queue_channel(vsi, ch);
6666 			if (ret) {
6667 				dev_err(&vsi->back->pdev->dev,
6668 					"Failed creating queue channel with TC%d: queues %d\n",
6669 					i, ch->num_queue_pairs);
6670 				goto err_free;
6671 			}
6672 			vsi->tc_seid_map[i] = ch->seid;
6673 		}
6674 	}
6675 
6676 	/* reset to reconfigure TX queue contexts */
6677 	i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6678 	return ret;
6679 
6680 err_free:
6681 	i40e_remove_queue_channels(vsi);
6682 	return ret;
6683 }
6684 
6685 /**
6686  * i40e_veb_config_tc - Configure TCs for given VEB
6687  * @veb: given VEB
6688  * @enabled_tc: TC bitmap
6689  *
6690  * Configures given TC bitmap for VEB (switching) element
6691  **/
6692 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6693 {
6694 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6695 	struct i40e_pf *pf = veb->pf;
6696 	int ret = 0;
6697 	int i;
6698 
6699 	/* No TCs or already enabled TCs just return */
6700 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6701 		return ret;
6702 
6703 	bw_data.tc_valid_bits = enabled_tc;
6704 	/* bw_data.absolute_credits is not set (relative) */
6705 
6706 	/* Enable ETS TCs with equal BW Share for now */
6707 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6708 		if (enabled_tc & BIT(i))
6709 			bw_data.tc_bw_share_credits[i] = 1;
6710 	}
6711 
6712 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6713 						   &bw_data, NULL);
6714 	if (ret) {
6715 		dev_info(&pf->pdev->dev,
6716 			 "VEB bw config failed, err %s aq_err %s\n",
6717 			 i40e_stat_str(&pf->hw, ret),
6718 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6719 		goto out;
6720 	}
6721 
6722 	/* Update the BW information */
6723 	ret = i40e_veb_get_bw_info(veb);
6724 	if (ret) {
6725 		dev_info(&pf->pdev->dev,
6726 			 "Failed getting veb bw config, err %s aq_err %s\n",
6727 			 i40e_stat_str(&pf->hw, ret),
6728 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6729 	}
6730 
6731 out:
6732 	return ret;
6733 }
6734 
6735 #ifdef CONFIG_I40E_DCB
6736 /**
6737  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6738  * @pf: PF struct
6739  *
6740  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6741  * the caller would've quiesce all the VSIs before calling
6742  * this function
6743  **/
6744 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6745 {
6746 	u8 tc_map = 0;
6747 	int ret;
6748 	u8 v;
6749 
6750 	/* Enable the TCs available on PF to all VEBs */
6751 	tc_map = i40e_pf_get_tc_map(pf);
6752 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6753 		return;
6754 
6755 	for (v = 0; v < I40E_MAX_VEB; v++) {
6756 		if (!pf->veb[v])
6757 			continue;
6758 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6759 		if (ret) {
6760 			dev_info(&pf->pdev->dev,
6761 				 "Failed configuring TC for VEB seid=%d\n",
6762 				 pf->veb[v]->seid);
6763 			/* Will try to configure as many components */
6764 		}
6765 	}
6766 
6767 	/* Update each VSI */
6768 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6769 		if (!pf->vsi[v])
6770 			continue;
6771 
6772 		/* - Enable all TCs for the LAN VSI
6773 		 * - For all others keep them at TC0 for now
6774 		 */
6775 		if (v == pf->lan_vsi)
6776 			tc_map = i40e_pf_get_tc_map(pf);
6777 		else
6778 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6779 
6780 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6781 		if (ret) {
6782 			dev_info(&pf->pdev->dev,
6783 				 "Failed configuring TC for VSI seid=%d\n",
6784 				 pf->vsi[v]->seid);
6785 			/* Will try to configure as many components */
6786 		} else {
6787 			/* Re-configure VSI vectors based on updated TC map */
6788 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6789 			if (pf->vsi[v]->netdev)
6790 				i40e_dcbnl_set_all(pf->vsi[v]);
6791 		}
6792 	}
6793 }
6794 
6795 /**
6796  * i40e_resume_port_tx - Resume port Tx
6797  * @pf: PF struct
6798  *
6799  * Resume a port's Tx and issue a PF reset in case of failure to
6800  * resume.
6801  **/
6802 static int i40e_resume_port_tx(struct i40e_pf *pf)
6803 {
6804 	struct i40e_hw *hw = &pf->hw;
6805 	int ret;
6806 
6807 	ret = i40e_aq_resume_port_tx(hw, NULL);
6808 	if (ret) {
6809 		dev_info(&pf->pdev->dev,
6810 			 "Resume Port Tx failed, err %s aq_err %s\n",
6811 			  i40e_stat_str(&pf->hw, ret),
6812 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6813 		/* Schedule PF reset to recover */
6814 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6815 		i40e_service_event_schedule(pf);
6816 	}
6817 
6818 	return ret;
6819 }
6820 
6821 /**
6822  * i40e_suspend_port_tx - Suspend port Tx
6823  * @pf: PF struct
6824  *
6825  * Suspend a port's Tx and issue a PF reset in case of failure.
6826  **/
6827 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6828 {
6829 	struct i40e_hw *hw = &pf->hw;
6830 	int ret;
6831 
6832 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6833 	if (ret) {
6834 		dev_info(&pf->pdev->dev,
6835 			 "Suspend Port Tx failed, err %s aq_err %s\n",
6836 			 i40e_stat_str(&pf->hw, ret),
6837 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6838 		/* Schedule PF reset to recover */
6839 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6840 		i40e_service_event_schedule(pf);
6841 	}
6842 
6843 	return ret;
6844 }
6845 
6846 /**
6847  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6848  * @pf: PF being configured
6849  * @new_cfg: New DCBX configuration
6850  *
6851  * Program DCB settings into HW and reconfigure VEB/VSIs on
6852  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6853  **/
6854 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6855 				  struct i40e_dcbx_config *new_cfg)
6856 {
6857 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6858 	int ret;
6859 
6860 	/* Check if need reconfiguration */
6861 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6862 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6863 		return 0;
6864 	}
6865 
6866 	/* Config change disable all VSIs */
6867 	i40e_pf_quiesce_all_vsi(pf);
6868 
6869 	/* Copy the new config to the current config */
6870 	*old_cfg = *new_cfg;
6871 	old_cfg->etsrec = old_cfg->etscfg;
6872 	ret = i40e_set_dcb_config(&pf->hw);
6873 	if (ret) {
6874 		dev_info(&pf->pdev->dev,
6875 			 "Set DCB Config failed, err %s aq_err %s\n",
6876 			 i40e_stat_str(&pf->hw, ret),
6877 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6878 		goto out;
6879 	}
6880 
6881 	/* Changes in configuration update VEB/VSI */
6882 	i40e_dcb_reconfigure(pf);
6883 out:
6884 	/* In case of reset do not try to resume anything */
6885 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6886 		/* Re-start the VSIs if disabled */
6887 		ret = i40e_resume_port_tx(pf);
6888 		/* In case of error no point in resuming VSIs */
6889 		if (ret)
6890 			goto err;
6891 		i40e_pf_unquiesce_all_vsi(pf);
6892 	}
6893 err:
6894 	return ret;
6895 }
6896 
6897 /**
6898  * i40e_hw_dcb_config - Program new DCBX settings into HW
6899  * @pf: PF being configured
6900  * @new_cfg: New DCBX configuration
6901  *
6902  * Program DCB settings into HW and reconfigure VEB/VSIs on
6903  * given PF
6904  **/
6905 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6906 {
6907 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6908 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6909 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6910 	struct i40e_dcbx_config *old_cfg;
6911 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6912 	struct i40e_rx_pb_config pb_cfg;
6913 	struct i40e_hw *hw = &pf->hw;
6914 	u8 num_ports = hw->num_ports;
6915 	bool need_reconfig;
6916 	int ret = -EINVAL;
6917 	u8 lltc_map = 0;
6918 	u8 tc_map = 0;
6919 	u8 new_numtc;
6920 	u8 i;
6921 
6922 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6923 	/* Un-pack information to Program ETS HW via shared API
6924 	 * numtc, tcmap
6925 	 * LLTC map
6926 	 * ETS/NON-ETS arbiter mode
6927 	 * max exponent (credit refills)
6928 	 * Total number of ports
6929 	 * PFC priority bit-map
6930 	 * Priority Table
6931 	 * BW % per TC
6932 	 * Arbiter mode between UPs sharing same TC
6933 	 * TSA table (ETS or non-ETS)
6934 	 * EEE enabled or not
6935 	 * MFS TC table
6936 	 */
6937 
6938 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6939 
6940 	memset(&ets_data, 0, sizeof(ets_data));
6941 	for (i = 0; i < new_numtc; i++) {
6942 		tc_map |= BIT(i);
6943 		switch (new_cfg->etscfg.tsatable[i]) {
6944 		case I40E_IEEE_TSA_ETS:
6945 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6946 			ets_data.tc_bw_share_credits[i] =
6947 					new_cfg->etscfg.tcbwtable[i];
6948 			break;
6949 		case I40E_IEEE_TSA_STRICT:
6950 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6951 			lltc_map |= BIT(i);
6952 			ets_data.tc_bw_share_credits[i] =
6953 					I40E_DCB_STRICT_PRIO_CREDITS;
6954 			break;
6955 		default:
6956 			/* Invalid TSA type */
6957 			need_reconfig = false;
6958 			goto out;
6959 		}
6960 	}
6961 
6962 	old_cfg = &hw->local_dcbx_config;
6963 	/* Check if need reconfiguration */
6964 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6965 
6966 	/* If needed, enable/disable frame tagging, disable all VSIs
6967 	 * and suspend port tx
6968 	 */
6969 	if (need_reconfig) {
6970 		/* Enable DCB tagging only when more than one TC */
6971 		if (new_numtc > 1)
6972 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6973 		else
6974 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6975 
6976 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6977 		/* Reconfiguration needed quiesce all VSIs */
6978 		i40e_pf_quiesce_all_vsi(pf);
6979 		ret = i40e_suspend_port_tx(pf);
6980 		if (ret)
6981 			goto err;
6982 	}
6983 
6984 	/* Configure Port ETS Tx Scheduler */
6985 	ets_data.tc_valid_bits = tc_map;
6986 	ets_data.tc_strict_priority_flags = lltc_map;
6987 	ret = i40e_aq_config_switch_comp_ets
6988 		(hw, pf->mac_seid, &ets_data,
6989 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
6990 	if (ret) {
6991 		dev_info(&pf->pdev->dev,
6992 			 "Modify Port ETS failed, err %s aq_err %s\n",
6993 			 i40e_stat_str(&pf->hw, ret),
6994 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6995 		goto out;
6996 	}
6997 
6998 	/* Configure Rx ETS HW */
6999 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
7000 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
7001 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
7002 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
7003 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
7004 				   lltc_map);
7005 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
7006 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7007 				     prio_type);
7008 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7009 			       new_cfg->etscfg.prioritytable);
7010 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7011 
7012 	/* Configure Rx Packet Buffers in HW */
7013 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7014 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
7015 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7016 	}
7017 
7018 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7019 					 false, new_cfg->pfc.pfcenable,
7020 					 mfs_tc, &pb_cfg);
7021 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7022 
7023 	/* Update the local Rx Packet buffer config */
7024 	pf->pb_cfg = pb_cfg;
7025 
7026 	/* Inform the FW about changes to DCB configuration */
7027 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7028 	if (ret) {
7029 		dev_info(&pf->pdev->dev,
7030 			 "DCB Updated failed, err %s aq_err %s\n",
7031 			 i40e_stat_str(&pf->hw, ret),
7032 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7033 		goto out;
7034 	}
7035 
7036 	/* Update the port DCBx configuration */
7037 	*old_cfg = *new_cfg;
7038 
7039 	/* Changes in configuration update VEB/VSI */
7040 	i40e_dcb_reconfigure(pf);
7041 out:
7042 	/* Re-start the VSIs if disabled */
7043 	if (need_reconfig) {
7044 		ret = i40e_resume_port_tx(pf);
7045 
7046 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7047 		/* In case of error no point in resuming VSIs */
7048 		if (ret)
7049 			goto err;
7050 
7051 		/* Wait for the PF's queues to be disabled */
7052 		ret = i40e_pf_wait_queues_disabled(pf);
7053 		if (ret) {
7054 			/* Schedule PF reset to recover */
7055 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7056 			i40e_service_event_schedule(pf);
7057 			goto err;
7058 		} else {
7059 			i40e_pf_unquiesce_all_vsi(pf);
7060 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7061 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7062 		}
7063 		/* registers are set, lets apply */
7064 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
7065 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7066 	}
7067 
7068 err:
7069 	return ret;
7070 }
7071 
7072 /**
7073  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7074  * @pf: PF being queried
7075  *
7076  * Set default DCB configuration in case DCB is to be done in SW.
7077  **/
7078 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7079 {
7080 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7081 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7082 	struct i40e_hw *hw = &pf->hw;
7083 	int err;
7084 
7085 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
7086 		/* Update the local cached instance with TC0 ETS */
7087 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7088 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7089 		pf->tmp_cfg.etscfg.maxtcs = 0;
7090 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7091 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7092 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7093 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7094 		/* FW needs one App to configure HW */
7095 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7096 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7097 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7098 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7099 
7100 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7101 	}
7102 
7103 	memset(&ets_data, 0, sizeof(ets_data));
7104 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7105 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7106 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7107 
7108 	/* Enable ETS on the Physical port */
7109 	err = i40e_aq_config_switch_comp_ets
7110 		(hw, pf->mac_seid, &ets_data,
7111 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7112 	if (err) {
7113 		dev_info(&pf->pdev->dev,
7114 			 "Enable Port ETS failed, err %s aq_err %s\n",
7115 			 i40e_stat_str(&pf->hw, err),
7116 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7117 		err = -ENOENT;
7118 		goto out;
7119 	}
7120 
7121 	/* Update the local cached instance with TC0 ETS */
7122 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7123 	dcb_cfg->etscfg.cbs = 0;
7124 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7125 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7126 
7127 out:
7128 	return err;
7129 }
7130 
7131 /**
7132  * i40e_init_pf_dcb - Initialize DCB configuration
7133  * @pf: PF being configured
7134  *
7135  * Query the current DCB configuration and cache it
7136  * in the hardware structure
7137  **/
7138 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7139 {
7140 	struct i40e_hw *hw = &pf->hw;
7141 	int err;
7142 
7143 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7144 	 * Also do not enable DCBx if FW LLDP agent is disabled
7145 	 */
7146 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
7147 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7148 		err = I40E_NOT_SUPPORTED;
7149 		goto out;
7150 	}
7151 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
7152 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7153 		err = i40e_dcb_sw_default_config(pf);
7154 		if (err) {
7155 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7156 			goto out;
7157 		}
7158 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7159 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7160 			       DCB_CAP_DCBX_VER_IEEE;
7161 		/* at init capable but disabled */
7162 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
7163 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7164 		goto out;
7165 	}
7166 	err = i40e_init_dcb(hw, true);
7167 	if (!err) {
7168 		/* Device/Function is not DCBX capable */
7169 		if ((!hw->func_caps.dcb) ||
7170 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7171 			dev_info(&pf->pdev->dev,
7172 				 "DCBX offload is not supported or is disabled for this PF.\n");
7173 		} else {
7174 			/* When status is not DISABLED then DCBX in FW */
7175 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7176 				       DCB_CAP_DCBX_VER_IEEE;
7177 
7178 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
7179 			/* Enable DCB tagging only when more than one TC
7180 			 * or explicitly disable if only one TC
7181 			 */
7182 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7183 				pf->flags |= I40E_FLAG_DCB_ENABLED;
7184 			else
7185 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7186 			dev_dbg(&pf->pdev->dev,
7187 				"DCBX offload is supported for this PF.\n");
7188 		}
7189 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7190 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7191 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
7192 	} else {
7193 		dev_info(&pf->pdev->dev,
7194 			 "Query for DCB configuration failed, err %s aq_err %s\n",
7195 			 i40e_stat_str(&pf->hw, err),
7196 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7197 	}
7198 
7199 out:
7200 	return err;
7201 }
7202 #endif /* CONFIG_I40E_DCB */
7203 
7204 /**
7205  * i40e_print_link_message - print link up or down
7206  * @vsi: the VSI for which link needs a message
7207  * @isup: true of link is up, false otherwise
7208  */
7209 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7210 {
7211 	enum i40e_aq_link_speed new_speed;
7212 	struct i40e_pf *pf = vsi->back;
7213 	char *speed = "Unknown";
7214 	char *fc = "Unknown";
7215 	char *fec = "";
7216 	char *req_fec = "";
7217 	char *an = "";
7218 
7219 	if (isup)
7220 		new_speed = pf->hw.phy.link_info.link_speed;
7221 	else
7222 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7223 
7224 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7225 		return;
7226 	vsi->current_isup = isup;
7227 	vsi->current_speed = new_speed;
7228 	if (!isup) {
7229 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7230 		return;
7231 	}
7232 
7233 	/* Warn user if link speed on NPAR enabled partition is not at
7234 	 * least 10GB
7235 	 */
7236 	if (pf->hw.func_caps.npar_enable &&
7237 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7238 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7239 		netdev_warn(vsi->netdev,
7240 			    "The partition detected link speed that is less than 10Gbps\n");
7241 
7242 	switch (pf->hw.phy.link_info.link_speed) {
7243 	case I40E_LINK_SPEED_40GB:
7244 		speed = "40 G";
7245 		break;
7246 	case I40E_LINK_SPEED_20GB:
7247 		speed = "20 G";
7248 		break;
7249 	case I40E_LINK_SPEED_25GB:
7250 		speed = "25 G";
7251 		break;
7252 	case I40E_LINK_SPEED_10GB:
7253 		speed = "10 G";
7254 		break;
7255 	case I40E_LINK_SPEED_5GB:
7256 		speed = "5 G";
7257 		break;
7258 	case I40E_LINK_SPEED_2_5GB:
7259 		speed = "2.5 G";
7260 		break;
7261 	case I40E_LINK_SPEED_1GB:
7262 		speed = "1000 M";
7263 		break;
7264 	case I40E_LINK_SPEED_100MB:
7265 		speed = "100 M";
7266 		break;
7267 	default:
7268 		break;
7269 	}
7270 
7271 	switch (pf->hw.fc.current_mode) {
7272 	case I40E_FC_FULL:
7273 		fc = "RX/TX";
7274 		break;
7275 	case I40E_FC_TX_PAUSE:
7276 		fc = "TX";
7277 		break;
7278 	case I40E_FC_RX_PAUSE:
7279 		fc = "RX";
7280 		break;
7281 	default:
7282 		fc = "None";
7283 		break;
7284 	}
7285 
7286 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7287 		req_fec = "None";
7288 		fec = "None";
7289 		an = "False";
7290 
7291 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7292 			an = "True";
7293 
7294 		if (pf->hw.phy.link_info.fec_info &
7295 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7296 			fec = "CL74 FC-FEC/BASE-R";
7297 		else if (pf->hw.phy.link_info.fec_info &
7298 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7299 			fec = "CL108 RS-FEC";
7300 
7301 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7302 		 * both RS and FC are requested
7303 		 */
7304 		if (vsi->back->hw.phy.link_info.req_fec_info &
7305 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7306 			if (vsi->back->hw.phy.link_info.req_fec_info &
7307 			    I40E_AQ_REQUEST_FEC_RS)
7308 				req_fec = "CL108 RS-FEC";
7309 			else
7310 				req_fec = "CL74 FC-FEC/BASE-R";
7311 		}
7312 		netdev_info(vsi->netdev,
7313 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7314 			    speed, req_fec, fec, an, fc);
7315 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7316 		req_fec = "None";
7317 		fec = "None";
7318 		an = "False";
7319 
7320 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7321 			an = "True";
7322 
7323 		if (pf->hw.phy.link_info.fec_info &
7324 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7325 			fec = "CL74 FC-FEC/BASE-R";
7326 
7327 		if (pf->hw.phy.link_info.req_fec_info &
7328 		    I40E_AQ_REQUEST_FEC_KR)
7329 			req_fec = "CL74 FC-FEC/BASE-R";
7330 
7331 		netdev_info(vsi->netdev,
7332 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7333 			    speed, req_fec, fec, an, fc);
7334 	} else {
7335 		netdev_info(vsi->netdev,
7336 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7337 			    speed, fc);
7338 	}
7339 
7340 }
7341 
7342 /**
7343  * i40e_up_complete - Finish the last steps of bringing up a connection
7344  * @vsi: the VSI being configured
7345  **/
7346 static int i40e_up_complete(struct i40e_vsi *vsi)
7347 {
7348 	struct i40e_pf *pf = vsi->back;
7349 	int err;
7350 
7351 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7352 		i40e_vsi_configure_msix(vsi);
7353 	else
7354 		i40e_configure_msi_and_legacy(vsi);
7355 
7356 	/* start rings */
7357 	err = i40e_vsi_start_rings(vsi);
7358 	if (err)
7359 		return err;
7360 
7361 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7362 	i40e_napi_enable_all(vsi);
7363 	i40e_vsi_enable_irq(vsi);
7364 
7365 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7366 	    (vsi->netdev)) {
7367 		i40e_print_link_message(vsi, true);
7368 		netif_tx_start_all_queues(vsi->netdev);
7369 		netif_carrier_on(vsi->netdev);
7370 	}
7371 
7372 	/* replay FDIR SB filters */
7373 	if (vsi->type == I40E_VSI_FDIR) {
7374 		/* reset fd counters */
7375 		pf->fd_add_err = 0;
7376 		pf->fd_atr_cnt = 0;
7377 		i40e_fdir_filter_restore(vsi);
7378 	}
7379 
7380 	/* On the next run of the service_task, notify any clients of the new
7381 	 * opened netdev
7382 	 */
7383 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7384 	i40e_service_event_schedule(pf);
7385 
7386 	return 0;
7387 }
7388 
7389 /**
7390  * i40e_vsi_reinit_locked - Reset the VSI
7391  * @vsi: the VSI being configured
7392  *
7393  * Rebuild the ring structs after some configuration
7394  * has changed, e.g. MTU size.
7395  **/
7396 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7397 {
7398 	struct i40e_pf *pf = vsi->back;
7399 
7400 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7401 		usleep_range(1000, 2000);
7402 	i40e_down(vsi);
7403 
7404 	i40e_up(vsi);
7405 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7406 }
7407 
7408 /**
7409  * i40e_force_link_state - Force the link status
7410  * @pf: board private structure
7411  * @is_up: whether the link state should be forced up or down
7412  **/
7413 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7414 {
7415 	struct i40e_aq_get_phy_abilities_resp abilities;
7416 	struct i40e_aq_set_phy_config config = {0};
7417 	bool non_zero_phy_type = is_up;
7418 	struct i40e_hw *hw = &pf->hw;
7419 	i40e_status err;
7420 	u64 mask;
7421 	u8 speed;
7422 
7423 	/* Card might've been put in an unstable state by other drivers
7424 	 * and applications, which causes incorrect speed values being
7425 	 * set on startup. In order to clear speed registers, we call
7426 	 * get_phy_capabilities twice, once to get initial state of
7427 	 * available speeds, and once to get current PHY config.
7428 	 */
7429 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7430 					   NULL);
7431 	if (err) {
7432 		dev_err(&pf->pdev->dev,
7433 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7434 			i40e_stat_str(hw, err),
7435 			i40e_aq_str(hw, hw->aq.asq_last_status));
7436 		return err;
7437 	}
7438 	speed = abilities.link_speed;
7439 
7440 	/* Get the current phy config */
7441 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7442 					   NULL);
7443 	if (err) {
7444 		dev_err(&pf->pdev->dev,
7445 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7446 			i40e_stat_str(hw, err),
7447 			i40e_aq_str(hw, hw->aq.asq_last_status));
7448 		return err;
7449 	}
7450 
7451 	/* If link needs to go up, but was not forced to go down,
7452 	 * and its speed values are OK, no need for a flap
7453 	 * if non_zero_phy_type was set, still need to force up
7454 	 */
7455 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7456 		non_zero_phy_type = true;
7457 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7458 		return I40E_SUCCESS;
7459 
7460 	/* To force link we need to set bits for all supported PHY types,
7461 	 * but there are now more than 32, so we need to split the bitmap
7462 	 * across two fields.
7463 	 */
7464 	mask = I40E_PHY_TYPES_BITMASK;
7465 	config.phy_type =
7466 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7467 	config.phy_type_ext =
7468 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7469 	/* Copy the old settings, except of phy_type */
7470 	config.abilities = abilities.abilities;
7471 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7472 		if (is_up)
7473 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7474 		else
7475 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7476 	}
7477 	if (abilities.link_speed != 0)
7478 		config.link_speed = abilities.link_speed;
7479 	else
7480 		config.link_speed = speed;
7481 	config.eee_capability = abilities.eee_capability;
7482 	config.eeer = abilities.eeer_val;
7483 	config.low_power_ctrl = abilities.d3_lpan;
7484 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7485 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7486 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7487 
7488 	if (err) {
7489 		dev_err(&pf->pdev->dev,
7490 			"set phy config ret =  %s last_status =  %s\n",
7491 			i40e_stat_str(&pf->hw, err),
7492 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7493 		return err;
7494 	}
7495 
7496 	/* Update the link info */
7497 	err = i40e_update_link_info(hw);
7498 	if (err) {
7499 		/* Wait a little bit (on 40G cards it sometimes takes a really
7500 		 * long time for link to come back from the atomic reset)
7501 		 * and try once more
7502 		 */
7503 		msleep(1000);
7504 		i40e_update_link_info(hw);
7505 	}
7506 
7507 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7508 
7509 	return I40E_SUCCESS;
7510 }
7511 
7512 /**
7513  * i40e_up - Bring the connection back up after being down
7514  * @vsi: the VSI being configured
7515  **/
7516 int i40e_up(struct i40e_vsi *vsi)
7517 {
7518 	int err;
7519 
7520 	if (vsi->type == I40E_VSI_MAIN &&
7521 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7522 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7523 		i40e_force_link_state(vsi->back, true);
7524 
7525 	err = i40e_vsi_configure(vsi);
7526 	if (!err)
7527 		err = i40e_up_complete(vsi);
7528 
7529 	return err;
7530 }
7531 
7532 /**
7533  * i40e_down - Shutdown the connection processing
7534  * @vsi: the VSI being stopped
7535  **/
7536 void i40e_down(struct i40e_vsi *vsi)
7537 {
7538 	int i;
7539 
7540 	/* It is assumed that the caller of this function
7541 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7542 	 */
7543 	if (vsi->netdev) {
7544 		netif_carrier_off(vsi->netdev);
7545 		netif_tx_disable(vsi->netdev);
7546 	}
7547 	i40e_vsi_disable_irq(vsi);
7548 	i40e_vsi_stop_rings(vsi);
7549 	if (vsi->type == I40E_VSI_MAIN &&
7550 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7551 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7552 		i40e_force_link_state(vsi->back, false);
7553 	i40e_napi_disable_all(vsi);
7554 
7555 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7556 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7557 		if (i40e_enabled_xdp_vsi(vsi)) {
7558 			/* Make sure that in-progress ndo_xdp_xmit and
7559 			 * ndo_xsk_wakeup calls are completed.
7560 			 */
7561 			synchronize_rcu();
7562 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7563 		}
7564 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7565 	}
7566 
7567 }
7568 
7569 /**
7570  * i40e_validate_mqprio_qopt- validate queue mapping info
7571  * @vsi: the VSI being configured
7572  * @mqprio_qopt: queue parametrs
7573  **/
7574 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7575 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7576 {
7577 	u64 sum_max_rate = 0;
7578 	u64 max_rate = 0;
7579 	int i;
7580 
7581 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7582 	    mqprio_qopt->qopt.num_tc < 1 ||
7583 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7584 		return -EINVAL;
7585 	for (i = 0; ; i++) {
7586 		if (!mqprio_qopt->qopt.count[i])
7587 			return -EINVAL;
7588 		if (mqprio_qopt->min_rate[i]) {
7589 			dev_err(&vsi->back->pdev->dev,
7590 				"Invalid min tx rate (greater than 0) specified\n");
7591 			return -EINVAL;
7592 		}
7593 		max_rate = mqprio_qopt->max_rate[i];
7594 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7595 		sum_max_rate += max_rate;
7596 
7597 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7598 			break;
7599 		if (mqprio_qopt->qopt.offset[i + 1] !=
7600 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7601 			return -EINVAL;
7602 	}
7603 	if (vsi->num_queue_pairs <
7604 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7605 		dev_err(&vsi->back->pdev->dev,
7606 			"Failed to create traffic channel, insufficient number of queues.\n");
7607 		return -EINVAL;
7608 	}
7609 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7610 		dev_err(&vsi->back->pdev->dev,
7611 			"Invalid max tx rate specified\n");
7612 		return -EINVAL;
7613 	}
7614 	return 0;
7615 }
7616 
7617 /**
7618  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7619  * @vsi: the VSI being configured
7620  **/
7621 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7622 {
7623 	u16 qcount;
7624 	int i;
7625 
7626 	/* Only TC0 is enabled */
7627 	vsi->tc_config.numtc = 1;
7628 	vsi->tc_config.enabled_tc = 1;
7629 	qcount = min_t(int, vsi->alloc_queue_pairs,
7630 		       i40e_pf_get_max_q_per_tc(vsi->back));
7631 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7632 		/* For the TC that is not enabled set the offset to default
7633 		 * queue and allocate one queue for the given TC.
7634 		 */
7635 		vsi->tc_config.tc_info[i].qoffset = 0;
7636 		if (i == 0)
7637 			vsi->tc_config.tc_info[i].qcount = qcount;
7638 		else
7639 			vsi->tc_config.tc_info[i].qcount = 1;
7640 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7641 	}
7642 }
7643 
7644 /**
7645  * i40e_del_macvlan_filter
7646  * @hw: pointer to the HW structure
7647  * @seid: seid of the channel VSI
7648  * @macaddr: the mac address to apply as a filter
7649  * @aq_err: store the admin Q error
7650  *
7651  * This function deletes a mac filter on the channel VSI which serves as the
7652  * macvlan. Returns 0 on success.
7653  **/
7654 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7655 					   const u8 *macaddr, int *aq_err)
7656 {
7657 	struct i40e_aqc_remove_macvlan_element_data element;
7658 	i40e_status status;
7659 
7660 	memset(&element, 0, sizeof(element));
7661 	ether_addr_copy(element.mac_addr, macaddr);
7662 	element.vlan_tag = 0;
7663 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7664 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7665 	*aq_err = hw->aq.asq_last_status;
7666 
7667 	return status;
7668 }
7669 
7670 /**
7671  * i40e_add_macvlan_filter
7672  * @hw: pointer to the HW structure
7673  * @seid: seid of the channel VSI
7674  * @macaddr: the mac address to apply as a filter
7675  * @aq_err: store the admin Q error
7676  *
7677  * This function adds a mac filter on the channel VSI which serves as the
7678  * macvlan. Returns 0 on success.
7679  **/
7680 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7681 					   const u8 *macaddr, int *aq_err)
7682 {
7683 	struct i40e_aqc_add_macvlan_element_data element;
7684 	i40e_status status;
7685 	u16 cmd_flags = 0;
7686 
7687 	ether_addr_copy(element.mac_addr, macaddr);
7688 	element.vlan_tag = 0;
7689 	element.queue_number = 0;
7690 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7691 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7692 	element.flags = cpu_to_le16(cmd_flags);
7693 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7694 	*aq_err = hw->aq.asq_last_status;
7695 
7696 	return status;
7697 }
7698 
7699 /**
7700  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7701  * @vsi: the VSI we want to access
7702  * @ch: the channel we want to access
7703  */
7704 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7705 {
7706 	struct i40e_ring *tx_ring, *rx_ring;
7707 	u16 pf_q;
7708 	int i;
7709 
7710 	for (i = 0; i < ch->num_queue_pairs; i++) {
7711 		pf_q = ch->base_queue + i;
7712 		tx_ring = vsi->tx_rings[pf_q];
7713 		tx_ring->ch = NULL;
7714 		rx_ring = vsi->rx_rings[pf_q];
7715 		rx_ring->ch = NULL;
7716 	}
7717 }
7718 
7719 /**
7720  * i40e_free_macvlan_channels
7721  * @vsi: the VSI we want to access
7722  *
7723  * This function frees the Qs of the channel VSI from
7724  * the stack and also deletes the channel VSIs which
7725  * serve as macvlans.
7726  */
7727 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7728 {
7729 	struct i40e_channel *ch, *ch_tmp;
7730 	int ret;
7731 
7732 	if (list_empty(&vsi->macvlan_list))
7733 		return;
7734 
7735 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7736 		struct i40e_vsi *parent_vsi;
7737 
7738 		if (i40e_is_channel_macvlan(ch)) {
7739 			i40e_reset_ch_rings(vsi, ch);
7740 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7741 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7742 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7743 			kfree(ch->fwd);
7744 			ch->fwd = NULL;
7745 		}
7746 
7747 		list_del(&ch->list);
7748 		parent_vsi = ch->parent_vsi;
7749 		if (!parent_vsi || !ch->initialized) {
7750 			kfree(ch);
7751 			continue;
7752 		}
7753 
7754 		/* remove the VSI */
7755 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7756 					     NULL);
7757 		if (ret)
7758 			dev_err(&vsi->back->pdev->dev,
7759 				"unable to remove channel (%d) for parent VSI(%d)\n",
7760 				ch->seid, parent_vsi->seid);
7761 		kfree(ch);
7762 	}
7763 	vsi->macvlan_cnt = 0;
7764 }
7765 
7766 /**
7767  * i40e_fwd_ring_up - bring the macvlan device up
7768  * @vsi: the VSI we want to access
7769  * @vdev: macvlan netdevice
7770  * @fwd: the private fwd structure
7771  */
7772 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7773 			    struct i40e_fwd_adapter *fwd)
7774 {
7775 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7776 	int ret = 0, num_tc = 1,  i, aq_err;
7777 	struct i40e_pf *pf = vsi->back;
7778 	struct i40e_hw *hw = &pf->hw;
7779 
7780 	/* Go through the list and find an available channel */
7781 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7782 		if (!i40e_is_channel_macvlan(iter)) {
7783 			iter->fwd = fwd;
7784 			/* record configuration for macvlan interface in vdev */
7785 			for (i = 0; i < num_tc; i++)
7786 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7787 							     i,
7788 							     iter->num_queue_pairs,
7789 							     iter->base_queue);
7790 			for (i = 0; i < iter->num_queue_pairs; i++) {
7791 				struct i40e_ring *tx_ring, *rx_ring;
7792 				u16 pf_q;
7793 
7794 				pf_q = iter->base_queue + i;
7795 
7796 				/* Get to TX ring ptr */
7797 				tx_ring = vsi->tx_rings[pf_q];
7798 				tx_ring->ch = iter;
7799 
7800 				/* Get the RX ring ptr */
7801 				rx_ring = vsi->rx_rings[pf_q];
7802 				rx_ring->ch = iter;
7803 			}
7804 			ch = iter;
7805 			break;
7806 		}
7807 	}
7808 
7809 	if (!ch)
7810 		return -EINVAL;
7811 
7812 	/* Guarantee all rings are updated before we update the
7813 	 * MAC address filter.
7814 	 */
7815 	wmb();
7816 
7817 	/* Add a mac filter */
7818 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7819 	if (ret) {
7820 		/* if we cannot add the MAC rule then disable the offload */
7821 		macvlan_release_l2fw_offload(vdev);
7822 		for (i = 0; i < ch->num_queue_pairs; i++) {
7823 			struct i40e_ring *rx_ring;
7824 			u16 pf_q;
7825 
7826 			pf_q = ch->base_queue + i;
7827 			rx_ring = vsi->rx_rings[pf_q];
7828 			rx_ring->netdev = NULL;
7829 		}
7830 		dev_info(&pf->pdev->dev,
7831 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7832 			  i40e_stat_str(hw, ret),
7833 			  i40e_aq_str(hw, aq_err));
7834 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7835 	}
7836 
7837 	return ret;
7838 }
7839 
7840 /**
7841  * i40e_setup_macvlans - create the channels which will be macvlans
7842  * @vsi: the VSI we want to access
7843  * @macvlan_cnt: no. of macvlans to be setup
7844  * @qcnt: no. of Qs per macvlan
7845  * @vdev: macvlan netdevice
7846  */
7847 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7848 			       struct net_device *vdev)
7849 {
7850 	struct i40e_pf *pf = vsi->back;
7851 	struct i40e_hw *hw = &pf->hw;
7852 	struct i40e_vsi_context ctxt;
7853 	u16 sections, qmap, num_qps;
7854 	struct i40e_channel *ch;
7855 	int i, pow, ret = 0;
7856 	u8 offset = 0;
7857 
7858 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7859 		return -EINVAL;
7860 
7861 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7862 
7863 	/* find the next higher power-of-2 of num queue pairs */
7864 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7865 
7866 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7867 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7868 
7869 	/* Setup context bits for the main VSI */
7870 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7871 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7872 	memset(&ctxt, 0, sizeof(ctxt));
7873 	ctxt.seid = vsi->seid;
7874 	ctxt.pf_num = vsi->back->hw.pf_id;
7875 	ctxt.vf_num = 0;
7876 	ctxt.uplink_seid = vsi->uplink_seid;
7877 	ctxt.info = vsi->info;
7878 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7879 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7880 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7881 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7882 
7883 	/* Reconfigure RSS for main VSI with new max queue count */
7884 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7885 	ret = i40e_vsi_config_rss(vsi);
7886 	if (ret) {
7887 		dev_info(&pf->pdev->dev,
7888 			 "Failed to reconfig RSS for num_queues (%u)\n",
7889 			 vsi->rss_size);
7890 		return ret;
7891 	}
7892 	vsi->reconfig_rss = true;
7893 	dev_dbg(&vsi->back->pdev->dev,
7894 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7895 	vsi->next_base_queue = num_qps;
7896 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7897 
7898 	/* Update the VSI after updating the VSI queue-mapping
7899 	 * information
7900 	 */
7901 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7902 	if (ret) {
7903 		dev_info(&pf->pdev->dev,
7904 			 "Update vsi tc config failed, err %s aq_err %s\n",
7905 			 i40e_stat_str(hw, ret),
7906 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7907 		return ret;
7908 	}
7909 	/* update the local VSI info with updated queue map */
7910 	i40e_vsi_update_queue_map(vsi, &ctxt);
7911 	vsi->info.valid_sections = 0;
7912 
7913 	/* Create channels for macvlans */
7914 	INIT_LIST_HEAD(&vsi->macvlan_list);
7915 	for (i = 0; i < macvlan_cnt; i++) {
7916 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7917 		if (!ch) {
7918 			ret = -ENOMEM;
7919 			goto err_free;
7920 		}
7921 		INIT_LIST_HEAD(&ch->list);
7922 		ch->num_queue_pairs = qcnt;
7923 		if (!i40e_setup_channel(pf, vsi, ch)) {
7924 			ret = -EINVAL;
7925 			kfree(ch);
7926 			goto err_free;
7927 		}
7928 		ch->parent_vsi = vsi;
7929 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7930 		vsi->macvlan_cnt++;
7931 		list_add_tail(&ch->list, &vsi->macvlan_list);
7932 	}
7933 
7934 	return ret;
7935 
7936 err_free:
7937 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7938 	i40e_free_macvlan_channels(vsi);
7939 
7940 	return ret;
7941 }
7942 
7943 /**
7944  * i40e_fwd_add - configure macvlans
7945  * @netdev: net device to configure
7946  * @vdev: macvlan netdevice
7947  **/
7948 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7949 {
7950 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7951 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7952 	struct i40e_vsi *vsi = np->vsi;
7953 	struct i40e_pf *pf = vsi->back;
7954 	struct i40e_fwd_adapter *fwd;
7955 	int avail_macvlan, ret;
7956 
7957 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7958 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7959 		return ERR_PTR(-EINVAL);
7960 	}
7961 	if (i40e_is_tc_mqprio_enabled(pf)) {
7962 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7963 		return ERR_PTR(-EINVAL);
7964 	}
7965 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7966 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7967 		return ERR_PTR(-EINVAL);
7968 	}
7969 
7970 	/* The macvlan device has to be a single Q device so that the
7971 	 * tc_to_txq field can be reused to pick the tx queue.
7972 	 */
7973 	if (netif_is_multiqueue(vdev))
7974 		return ERR_PTR(-ERANGE);
7975 
7976 	if (!vsi->macvlan_cnt) {
7977 		/* reserve bit 0 for the pf device */
7978 		set_bit(0, vsi->fwd_bitmask);
7979 
7980 		/* Try to reserve as many queues as possible for macvlans. First
7981 		 * reserve 3/4th of max vectors, then half, then quarter and
7982 		 * calculate Qs per macvlan as you go
7983 		 */
7984 		vectors = pf->num_lan_msix;
7985 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7986 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7987 			q_per_macvlan = 4;
7988 			macvlan_cnt = (vectors - 32) / 4;
7989 		} else if (vectors <= 64 && vectors > 32) {
7990 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7991 			q_per_macvlan = 2;
7992 			macvlan_cnt = (vectors - 16) / 2;
7993 		} else if (vectors <= 32 && vectors > 16) {
7994 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7995 			q_per_macvlan = 1;
7996 			macvlan_cnt = vectors - 16;
7997 		} else if (vectors <= 16 && vectors > 8) {
7998 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7999 			q_per_macvlan = 1;
8000 			macvlan_cnt = vectors - 8;
8001 		} else {
8002 			/* allocate 1 Q per macvlan and 1 Q to the PF */
8003 			q_per_macvlan = 1;
8004 			macvlan_cnt = vectors - 1;
8005 		}
8006 
8007 		if (macvlan_cnt == 0)
8008 			return ERR_PTR(-EBUSY);
8009 
8010 		/* Quiesce VSI queues */
8011 		i40e_quiesce_vsi(vsi);
8012 
8013 		/* sets up the macvlans but does not "enable" them */
8014 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8015 					  vdev);
8016 		if (ret)
8017 			return ERR_PTR(ret);
8018 
8019 		/* Unquiesce VSI */
8020 		i40e_unquiesce_vsi(vsi);
8021 	}
8022 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8023 					    vsi->macvlan_cnt);
8024 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8025 		return ERR_PTR(-EBUSY);
8026 
8027 	/* create the fwd struct */
8028 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8029 	if (!fwd)
8030 		return ERR_PTR(-ENOMEM);
8031 
8032 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8033 	fwd->bit_no = avail_macvlan;
8034 	netdev_set_sb_channel(vdev, avail_macvlan);
8035 	fwd->netdev = vdev;
8036 
8037 	if (!netif_running(netdev))
8038 		return fwd;
8039 
8040 	/* Set fwd ring up */
8041 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8042 	if (ret) {
8043 		/* unbind the queues and drop the subordinate channel config */
8044 		netdev_unbind_sb_channel(netdev, vdev);
8045 		netdev_set_sb_channel(vdev, 0);
8046 
8047 		kfree(fwd);
8048 		return ERR_PTR(-EINVAL);
8049 	}
8050 
8051 	return fwd;
8052 }
8053 
8054 /**
8055  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8056  * @vsi: the VSI we want to access
8057  */
8058 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8059 {
8060 	struct i40e_channel *ch, *ch_tmp;
8061 	struct i40e_pf *pf = vsi->back;
8062 	struct i40e_hw *hw = &pf->hw;
8063 	int aq_err, ret = 0;
8064 
8065 	if (list_empty(&vsi->macvlan_list))
8066 		return;
8067 
8068 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8069 		if (i40e_is_channel_macvlan(ch)) {
8070 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8071 						      i40e_channel_mac(ch),
8072 						      &aq_err);
8073 			if (!ret) {
8074 				/* Reset queue contexts */
8075 				i40e_reset_ch_rings(vsi, ch);
8076 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8077 				netdev_unbind_sb_channel(vsi->netdev,
8078 							 ch->fwd->netdev);
8079 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8080 				kfree(ch->fwd);
8081 				ch->fwd = NULL;
8082 			}
8083 		}
8084 	}
8085 }
8086 
8087 /**
8088  * i40e_fwd_del - delete macvlan interfaces
8089  * @netdev: net device to configure
8090  * @vdev: macvlan netdevice
8091  */
8092 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8093 {
8094 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8095 	struct i40e_fwd_adapter *fwd = vdev;
8096 	struct i40e_channel *ch, *ch_tmp;
8097 	struct i40e_vsi *vsi = np->vsi;
8098 	struct i40e_pf *pf = vsi->back;
8099 	struct i40e_hw *hw = &pf->hw;
8100 	int aq_err, ret = 0;
8101 
8102 	/* Find the channel associated with the macvlan and del mac filter */
8103 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8104 		if (i40e_is_channel_macvlan(ch) &&
8105 		    ether_addr_equal(i40e_channel_mac(ch),
8106 				     fwd->netdev->dev_addr)) {
8107 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8108 						      i40e_channel_mac(ch),
8109 						      &aq_err);
8110 			if (!ret) {
8111 				/* Reset queue contexts */
8112 				i40e_reset_ch_rings(vsi, ch);
8113 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8114 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8115 				netdev_set_sb_channel(fwd->netdev, 0);
8116 				kfree(ch->fwd);
8117 				ch->fwd = NULL;
8118 			} else {
8119 				dev_info(&pf->pdev->dev,
8120 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
8121 					  i40e_stat_str(hw, ret),
8122 					  i40e_aq_str(hw, aq_err));
8123 			}
8124 			break;
8125 		}
8126 	}
8127 }
8128 
8129 /**
8130  * i40e_setup_tc - configure multiple traffic classes
8131  * @netdev: net device to configure
8132  * @type_data: tc offload data
8133  **/
8134 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8135 {
8136 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8137 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8138 	struct i40e_vsi *vsi = np->vsi;
8139 	struct i40e_pf *pf = vsi->back;
8140 	u8 enabled_tc = 0, num_tc, hw;
8141 	bool need_reset = false;
8142 	int old_queue_pairs;
8143 	int ret = -EINVAL;
8144 	u16 mode;
8145 	int i;
8146 
8147 	old_queue_pairs = vsi->num_queue_pairs;
8148 	num_tc = mqprio_qopt->qopt.num_tc;
8149 	hw = mqprio_qopt->qopt.hw;
8150 	mode = mqprio_qopt->mode;
8151 	if (!hw) {
8152 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8153 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8154 		goto config_tc;
8155 	}
8156 
8157 	/* Check if MFP enabled */
8158 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
8159 		netdev_info(netdev,
8160 			    "Configuring TC not supported in MFP mode\n");
8161 		return ret;
8162 	}
8163 	switch (mode) {
8164 	case TC_MQPRIO_MODE_DCB:
8165 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8166 
8167 		/* Check if DCB enabled to continue */
8168 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
8169 			netdev_info(netdev,
8170 				    "DCB is not enabled for adapter\n");
8171 			return ret;
8172 		}
8173 
8174 		/* Check whether tc count is within enabled limit */
8175 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8176 			netdev_info(netdev,
8177 				    "TC count greater than enabled on link for adapter\n");
8178 			return ret;
8179 		}
8180 		break;
8181 	case TC_MQPRIO_MODE_CHANNEL:
8182 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
8183 			netdev_info(netdev,
8184 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8185 			return ret;
8186 		}
8187 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8188 			return ret;
8189 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8190 		if (ret)
8191 			return ret;
8192 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8193 		       sizeof(*mqprio_qopt));
8194 		pf->flags |= I40E_FLAG_TC_MQPRIO;
8195 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8196 		break;
8197 	default:
8198 		return -EINVAL;
8199 	}
8200 
8201 config_tc:
8202 	/* Generate TC map for number of tc requested */
8203 	for (i = 0; i < num_tc; i++)
8204 		enabled_tc |= BIT(i);
8205 
8206 	/* Requesting same TC configuration as already enabled */
8207 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8208 	    mode != TC_MQPRIO_MODE_CHANNEL)
8209 		return 0;
8210 
8211 	/* Quiesce VSI queues */
8212 	i40e_quiesce_vsi(vsi);
8213 
8214 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8215 		i40e_remove_queue_channels(vsi);
8216 
8217 	/* Configure VSI for enabled TCs */
8218 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8219 	if (ret) {
8220 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8221 			    vsi->seid);
8222 		need_reset = true;
8223 		goto exit;
8224 	} else if (enabled_tc &&
8225 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8226 		netdev_info(netdev,
8227 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8228 			    vsi->tc_config.tc_info[0].qcount);
8229 		ret = -EINVAL;
8230 		need_reset = true;
8231 		goto exit;
8232 	}
8233 
8234 	dev_info(&vsi->back->pdev->dev,
8235 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8236 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8237 
8238 	if (i40e_is_tc_mqprio_enabled(pf)) {
8239 		if (vsi->mqprio_qopt.max_rate[0]) {
8240 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8241 						  vsi->mqprio_qopt.max_rate[0]);
8242 
8243 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8244 			if (!ret) {
8245 				u64 credits = max_tx_rate;
8246 
8247 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8248 				dev_dbg(&vsi->back->pdev->dev,
8249 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8250 					max_tx_rate,
8251 					credits,
8252 					vsi->seid);
8253 			} else {
8254 				need_reset = true;
8255 				goto exit;
8256 			}
8257 		}
8258 		ret = i40e_configure_queue_channels(vsi);
8259 		if (ret) {
8260 			vsi->num_queue_pairs = old_queue_pairs;
8261 			netdev_info(netdev,
8262 				    "Failed configuring queue channels\n");
8263 			need_reset = true;
8264 			goto exit;
8265 		}
8266 	}
8267 
8268 exit:
8269 	/* Reset the configuration data to defaults, only TC0 is enabled */
8270 	if (need_reset) {
8271 		i40e_vsi_set_default_tc_config(vsi);
8272 		need_reset = false;
8273 	}
8274 
8275 	/* Unquiesce VSI */
8276 	i40e_unquiesce_vsi(vsi);
8277 	return ret;
8278 }
8279 
8280 /**
8281  * i40e_set_cld_element - sets cloud filter element data
8282  * @filter: cloud filter rule
8283  * @cld: ptr to cloud filter element data
8284  *
8285  * This is helper function to copy data into cloud filter element
8286  **/
8287 static inline void
8288 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8289 		     struct i40e_aqc_cloud_filters_element_data *cld)
8290 {
8291 	u32 ipa;
8292 	int i;
8293 
8294 	memset(cld, 0, sizeof(*cld));
8295 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8296 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8297 
8298 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8299 		return;
8300 
8301 	if (filter->n_proto == ETH_P_IPV6) {
8302 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8303 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8304 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8305 
8306 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8307 		}
8308 	} else {
8309 		ipa = be32_to_cpu(filter->dst_ipv4);
8310 
8311 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8312 	}
8313 
8314 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8315 
8316 	/* tenant_id is not supported by FW now, once the support is enabled
8317 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8318 	 */
8319 	if (filter->tenant_id)
8320 		return;
8321 }
8322 
8323 /**
8324  * i40e_add_del_cloud_filter - Add/del cloud filter
8325  * @vsi: pointer to VSI
8326  * @filter: cloud filter rule
8327  * @add: if true, add, if false, delete
8328  *
8329  * Add or delete a cloud filter for a specific flow spec.
8330  * Returns 0 if the filter were successfully added.
8331  **/
8332 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8333 			      struct i40e_cloud_filter *filter, bool add)
8334 {
8335 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8336 	struct i40e_pf *pf = vsi->back;
8337 	int ret;
8338 	static const u16 flag_table[128] = {
8339 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8340 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8341 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8342 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8343 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8344 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8345 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8346 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8347 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8348 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8349 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8350 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8351 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8352 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8353 	};
8354 
8355 	if (filter->flags >= ARRAY_SIZE(flag_table))
8356 		return I40E_ERR_CONFIG;
8357 
8358 	memset(&cld_filter, 0, sizeof(cld_filter));
8359 
8360 	/* copy element needed to add cloud filter from filter */
8361 	i40e_set_cld_element(filter, &cld_filter);
8362 
8363 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8364 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8365 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8366 
8367 	if (filter->n_proto == ETH_P_IPV6)
8368 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8369 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8370 	else
8371 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8372 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8373 
8374 	if (add)
8375 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8376 						&cld_filter, 1);
8377 	else
8378 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8379 						&cld_filter, 1);
8380 	if (ret)
8381 		dev_dbg(&pf->pdev->dev,
8382 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8383 			add ? "add" : "delete", filter->dst_port, ret,
8384 			pf->hw.aq.asq_last_status);
8385 	else
8386 		dev_info(&pf->pdev->dev,
8387 			 "%s cloud filter for VSI: %d\n",
8388 			 add ? "Added" : "Deleted", filter->seid);
8389 	return ret;
8390 }
8391 
8392 /**
8393  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8394  * @vsi: pointer to VSI
8395  * @filter: cloud filter rule
8396  * @add: if true, add, if false, delete
8397  *
8398  * Add or delete a cloud filter for a specific flow spec using big buffer.
8399  * Returns 0 if the filter were successfully added.
8400  **/
8401 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8402 				      struct i40e_cloud_filter *filter,
8403 				      bool add)
8404 {
8405 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8406 	struct i40e_pf *pf = vsi->back;
8407 	int ret;
8408 
8409 	/* Both (src/dst) valid mac_addr are not supported */
8410 	if ((is_valid_ether_addr(filter->dst_mac) &&
8411 	     is_valid_ether_addr(filter->src_mac)) ||
8412 	    (is_multicast_ether_addr(filter->dst_mac) &&
8413 	     is_multicast_ether_addr(filter->src_mac)))
8414 		return -EOPNOTSUPP;
8415 
8416 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8417 	 * ports are not supported via big buffer now.
8418 	 */
8419 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8420 		return -EOPNOTSUPP;
8421 
8422 	/* adding filter using src_port/src_ip is not supported at this stage */
8423 	if (filter->src_port ||
8424 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8425 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8426 		return -EOPNOTSUPP;
8427 
8428 	memset(&cld_filter, 0, sizeof(cld_filter));
8429 
8430 	/* copy element needed to add cloud filter from filter */
8431 	i40e_set_cld_element(filter, &cld_filter.element);
8432 
8433 	if (is_valid_ether_addr(filter->dst_mac) ||
8434 	    is_valid_ether_addr(filter->src_mac) ||
8435 	    is_multicast_ether_addr(filter->dst_mac) ||
8436 	    is_multicast_ether_addr(filter->src_mac)) {
8437 		/* MAC + IP : unsupported mode */
8438 		if (filter->dst_ipv4)
8439 			return -EOPNOTSUPP;
8440 
8441 		/* since we validated that L4 port must be valid before
8442 		 * we get here, start with respective "flags" value
8443 		 * and update if vlan is present or not
8444 		 */
8445 		cld_filter.element.flags =
8446 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8447 
8448 		if (filter->vlan_id) {
8449 			cld_filter.element.flags =
8450 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8451 		}
8452 
8453 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8454 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8455 		cld_filter.element.flags =
8456 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8457 		if (filter->n_proto == ETH_P_IPV6)
8458 			cld_filter.element.flags |=
8459 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8460 		else
8461 			cld_filter.element.flags |=
8462 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8463 	} else {
8464 		dev_err(&pf->pdev->dev,
8465 			"either mac or ip has to be valid for cloud filter\n");
8466 		return -EINVAL;
8467 	}
8468 
8469 	/* Now copy L4 port in Byte 6..7 in general fields */
8470 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8471 						be16_to_cpu(filter->dst_port);
8472 
8473 	if (add) {
8474 		/* Validate current device switch mode, change if necessary */
8475 		ret = i40e_validate_and_set_switch_mode(vsi);
8476 		if (ret) {
8477 			dev_err(&pf->pdev->dev,
8478 				"failed to set switch mode, ret %d\n",
8479 				ret);
8480 			return ret;
8481 		}
8482 
8483 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8484 						   &cld_filter, 1);
8485 	} else {
8486 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8487 						   &cld_filter, 1);
8488 	}
8489 
8490 	if (ret)
8491 		dev_dbg(&pf->pdev->dev,
8492 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8493 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8494 	else
8495 		dev_info(&pf->pdev->dev,
8496 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8497 			 add ? "add" : "delete", filter->seid,
8498 			 ntohs(filter->dst_port));
8499 	return ret;
8500 }
8501 
8502 /**
8503  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8504  * @vsi: Pointer to VSI
8505  * @f: Pointer to struct flow_cls_offload
8506  * @filter: Pointer to cloud filter structure
8507  *
8508  **/
8509 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8510 				 struct flow_cls_offload *f,
8511 				 struct i40e_cloud_filter *filter)
8512 {
8513 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8514 	struct flow_dissector *dissector = rule->match.dissector;
8515 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8516 	struct i40e_pf *pf = vsi->back;
8517 	u8 field_flags = 0;
8518 
8519 	if (dissector->used_keys &
8520 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8521 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8522 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8523 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8524 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8525 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8526 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8527 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8528 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8529 			dissector->used_keys);
8530 		return -EOPNOTSUPP;
8531 	}
8532 
8533 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8534 		struct flow_match_enc_keyid match;
8535 
8536 		flow_rule_match_enc_keyid(rule, &match);
8537 		if (match.mask->keyid != 0)
8538 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8539 
8540 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8541 	}
8542 
8543 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8544 		struct flow_match_basic match;
8545 
8546 		flow_rule_match_basic(rule, &match);
8547 		n_proto_key = ntohs(match.key->n_proto);
8548 		n_proto_mask = ntohs(match.mask->n_proto);
8549 
8550 		if (n_proto_key == ETH_P_ALL) {
8551 			n_proto_key = 0;
8552 			n_proto_mask = 0;
8553 		}
8554 		filter->n_proto = n_proto_key & n_proto_mask;
8555 		filter->ip_proto = match.key->ip_proto;
8556 	}
8557 
8558 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8559 		struct flow_match_eth_addrs match;
8560 
8561 		flow_rule_match_eth_addrs(rule, &match);
8562 
8563 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8564 		if (!is_zero_ether_addr(match.mask->dst)) {
8565 			if (is_broadcast_ether_addr(match.mask->dst)) {
8566 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8567 			} else {
8568 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8569 					match.mask->dst);
8570 				return I40E_ERR_CONFIG;
8571 			}
8572 		}
8573 
8574 		if (!is_zero_ether_addr(match.mask->src)) {
8575 			if (is_broadcast_ether_addr(match.mask->src)) {
8576 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8577 			} else {
8578 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8579 					match.mask->src);
8580 				return I40E_ERR_CONFIG;
8581 			}
8582 		}
8583 		ether_addr_copy(filter->dst_mac, match.key->dst);
8584 		ether_addr_copy(filter->src_mac, match.key->src);
8585 	}
8586 
8587 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8588 		struct flow_match_vlan match;
8589 
8590 		flow_rule_match_vlan(rule, &match);
8591 		if (match.mask->vlan_id) {
8592 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8593 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8594 
8595 			} else {
8596 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8597 					match.mask->vlan_id);
8598 				return I40E_ERR_CONFIG;
8599 			}
8600 		}
8601 
8602 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8603 	}
8604 
8605 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8606 		struct flow_match_control match;
8607 
8608 		flow_rule_match_control(rule, &match);
8609 		addr_type = match.key->addr_type;
8610 	}
8611 
8612 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8613 		struct flow_match_ipv4_addrs match;
8614 
8615 		flow_rule_match_ipv4_addrs(rule, &match);
8616 		if (match.mask->dst) {
8617 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8618 				field_flags |= I40E_CLOUD_FIELD_IIP;
8619 			} else {
8620 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8621 					&match.mask->dst);
8622 				return I40E_ERR_CONFIG;
8623 			}
8624 		}
8625 
8626 		if (match.mask->src) {
8627 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8628 				field_flags |= I40E_CLOUD_FIELD_IIP;
8629 			} else {
8630 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8631 					&match.mask->src);
8632 				return I40E_ERR_CONFIG;
8633 			}
8634 		}
8635 
8636 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8637 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8638 			return I40E_ERR_CONFIG;
8639 		}
8640 		filter->dst_ipv4 = match.key->dst;
8641 		filter->src_ipv4 = match.key->src;
8642 	}
8643 
8644 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8645 		struct flow_match_ipv6_addrs match;
8646 
8647 		flow_rule_match_ipv6_addrs(rule, &match);
8648 
8649 		/* src and dest IPV6 address should not be LOOPBACK
8650 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8651 		 */
8652 		if (ipv6_addr_loopback(&match.key->dst) ||
8653 		    ipv6_addr_loopback(&match.key->src)) {
8654 			dev_err(&pf->pdev->dev,
8655 				"Bad ipv6, addr is LOOPBACK\n");
8656 			return I40E_ERR_CONFIG;
8657 		}
8658 		if (!ipv6_addr_any(&match.mask->dst) ||
8659 		    !ipv6_addr_any(&match.mask->src))
8660 			field_flags |= I40E_CLOUD_FIELD_IIP;
8661 
8662 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8663 		       sizeof(filter->src_ipv6));
8664 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8665 		       sizeof(filter->dst_ipv6));
8666 	}
8667 
8668 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8669 		struct flow_match_ports match;
8670 
8671 		flow_rule_match_ports(rule, &match);
8672 		if (match.mask->src) {
8673 			if (match.mask->src == cpu_to_be16(0xffff)) {
8674 				field_flags |= I40E_CLOUD_FIELD_IIP;
8675 			} else {
8676 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8677 					be16_to_cpu(match.mask->src));
8678 				return I40E_ERR_CONFIG;
8679 			}
8680 		}
8681 
8682 		if (match.mask->dst) {
8683 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8684 				field_flags |= I40E_CLOUD_FIELD_IIP;
8685 			} else {
8686 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8687 					be16_to_cpu(match.mask->dst));
8688 				return I40E_ERR_CONFIG;
8689 			}
8690 		}
8691 
8692 		filter->dst_port = match.key->dst;
8693 		filter->src_port = match.key->src;
8694 
8695 		switch (filter->ip_proto) {
8696 		case IPPROTO_TCP:
8697 		case IPPROTO_UDP:
8698 			break;
8699 		default:
8700 			dev_err(&pf->pdev->dev,
8701 				"Only UDP and TCP transport are supported\n");
8702 			return -EINVAL;
8703 		}
8704 	}
8705 	filter->flags = field_flags;
8706 	return 0;
8707 }
8708 
8709 /**
8710  * i40e_handle_tclass: Forward to a traffic class on the device
8711  * @vsi: Pointer to VSI
8712  * @tc: traffic class index on the device
8713  * @filter: Pointer to cloud filter structure
8714  *
8715  **/
8716 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8717 			      struct i40e_cloud_filter *filter)
8718 {
8719 	struct i40e_channel *ch, *ch_tmp;
8720 
8721 	/* direct to a traffic class on the same device */
8722 	if (tc == 0) {
8723 		filter->seid = vsi->seid;
8724 		return 0;
8725 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8726 		if (!filter->dst_port) {
8727 			dev_err(&vsi->back->pdev->dev,
8728 				"Specify destination port to direct to traffic class that is not default\n");
8729 			return -EINVAL;
8730 		}
8731 		if (list_empty(&vsi->ch_list))
8732 			return -EINVAL;
8733 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8734 					 list) {
8735 			if (ch->seid == vsi->tc_seid_map[tc])
8736 				filter->seid = ch->seid;
8737 		}
8738 		return 0;
8739 	}
8740 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8741 	return -EINVAL;
8742 }
8743 
8744 /**
8745  * i40e_configure_clsflower - Configure tc flower filters
8746  * @vsi: Pointer to VSI
8747  * @cls_flower: Pointer to struct flow_cls_offload
8748  *
8749  **/
8750 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8751 				    struct flow_cls_offload *cls_flower)
8752 {
8753 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8754 	struct i40e_cloud_filter *filter = NULL;
8755 	struct i40e_pf *pf = vsi->back;
8756 	int err = 0;
8757 
8758 	if (tc < 0) {
8759 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8760 		return -EOPNOTSUPP;
8761 	}
8762 
8763 	if (!tc) {
8764 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8765 		return -EINVAL;
8766 	}
8767 
8768 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8769 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8770 		return -EBUSY;
8771 
8772 	if (pf->fdir_pf_active_filters ||
8773 	    (!hlist_empty(&pf->fdir_filter_list))) {
8774 		dev_err(&vsi->back->pdev->dev,
8775 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8776 		return -EINVAL;
8777 	}
8778 
8779 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8780 		dev_err(&vsi->back->pdev->dev,
8781 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8782 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8783 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8784 	}
8785 
8786 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8787 	if (!filter)
8788 		return -ENOMEM;
8789 
8790 	filter->cookie = cls_flower->cookie;
8791 
8792 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8793 	if (err < 0)
8794 		goto err;
8795 
8796 	err = i40e_handle_tclass(vsi, tc, filter);
8797 	if (err < 0)
8798 		goto err;
8799 
8800 	/* Add cloud filter */
8801 	if (filter->dst_port)
8802 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8803 	else
8804 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8805 
8806 	if (err) {
8807 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8808 			err);
8809 		goto err;
8810 	}
8811 
8812 	/* add filter to the ordered list */
8813 	INIT_HLIST_NODE(&filter->cloud_node);
8814 
8815 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8816 
8817 	pf->num_cloud_filters++;
8818 
8819 	return err;
8820 err:
8821 	kfree(filter);
8822 	return err;
8823 }
8824 
8825 /**
8826  * i40e_find_cloud_filter - Find the could filter in the list
8827  * @vsi: Pointer to VSI
8828  * @cookie: filter specific cookie
8829  *
8830  **/
8831 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8832 							unsigned long *cookie)
8833 {
8834 	struct i40e_cloud_filter *filter = NULL;
8835 	struct hlist_node *node2;
8836 
8837 	hlist_for_each_entry_safe(filter, node2,
8838 				  &vsi->back->cloud_filter_list, cloud_node)
8839 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8840 			return filter;
8841 	return NULL;
8842 }
8843 
8844 /**
8845  * i40e_delete_clsflower - Remove tc flower filters
8846  * @vsi: Pointer to VSI
8847  * @cls_flower: Pointer to struct flow_cls_offload
8848  *
8849  **/
8850 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8851 				 struct flow_cls_offload *cls_flower)
8852 {
8853 	struct i40e_cloud_filter *filter = NULL;
8854 	struct i40e_pf *pf = vsi->back;
8855 	int err = 0;
8856 
8857 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8858 
8859 	if (!filter)
8860 		return -EINVAL;
8861 
8862 	hash_del(&filter->cloud_node);
8863 
8864 	if (filter->dst_port)
8865 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8866 	else
8867 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8868 
8869 	kfree(filter);
8870 	if (err) {
8871 		dev_err(&pf->pdev->dev,
8872 			"Failed to delete cloud filter, err %s\n",
8873 			i40e_stat_str(&pf->hw, err));
8874 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8875 	}
8876 
8877 	pf->num_cloud_filters--;
8878 	if (!pf->num_cloud_filters)
8879 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8880 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8881 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8882 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8883 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8884 		}
8885 	return 0;
8886 }
8887 
8888 /**
8889  * i40e_setup_tc_cls_flower - flower classifier offloads
8890  * @np: net device to configure
8891  * @cls_flower: offload data
8892  **/
8893 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8894 				    struct flow_cls_offload *cls_flower)
8895 {
8896 	struct i40e_vsi *vsi = np->vsi;
8897 
8898 	switch (cls_flower->command) {
8899 	case FLOW_CLS_REPLACE:
8900 		return i40e_configure_clsflower(vsi, cls_flower);
8901 	case FLOW_CLS_DESTROY:
8902 		return i40e_delete_clsflower(vsi, cls_flower);
8903 	case FLOW_CLS_STATS:
8904 		return -EOPNOTSUPP;
8905 	default:
8906 		return -EOPNOTSUPP;
8907 	}
8908 }
8909 
8910 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8911 				  void *cb_priv)
8912 {
8913 	struct i40e_netdev_priv *np = cb_priv;
8914 
8915 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8916 		return -EOPNOTSUPP;
8917 
8918 	switch (type) {
8919 	case TC_SETUP_CLSFLOWER:
8920 		return i40e_setup_tc_cls_flower(np, type_data);
8921 
8922 	default:
8923 		return -EOPNOTSUPP;
8924 	}
8925 }
8926 
8927 static LIST_HEAD(i40e_block_cb_list);
8928 
8929 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8930 			   void *type_data)
8931 {
8932 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8933 
8934 	switch (type) {
8935 	case TC_SETUP_QDISC_MQPRIO:
8936 		return i40e_setup_tc(netdev, type_data);
8937 	case TC_SETUP_BLOCK:
8938 		return flow_block_cb_setup_simple(type_data,
8939 						  &i40e_block_cb_list,
8940 						  i40e_setup_tc_block_cb,
8941 						  np, np, true);
8942 	default:
8943 		return -EOPNOTSUPP;
8944 	}
8945 }
8946 
8947 /**
8948  * i40e_open - Called when a network interface is made active
8949  * @netdev: network interface device structure
8950  *
8951  * The open entry point is called when a network interface is made
8952  * active by the system (IFF_UP).  At this point all resources needed
8953  * for transmit and receive operations are allocated, the interrupt
8954  * handler is registered with the OS, the netdev watchdog subtask is
8955  * enabled, and the stack is notified that the interface is ready.
8956  *
8957  * Returns 0 on success, negative value on failure
8958  **/
8959 int i40e_open(struct net_device *netdev)
8960 {
8961 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8962 	struct i40e_vsi *vsi = np->vsi;
8963 	struct i40e_pf *pf = vsi->back;
8964 	int err;
8965 
8966 	/* disallow open during test or if eeprom is broken */
8967 	if (test_bit(__I40E_TESTING, pf->state) ||
8968 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8969 		return -EBUSY;
8970 
8971 	netif_carrier_off(netdev);
8972 
8973 	if (i40e_force_link_state(pf, true))
8974 		return -EAGAIN;
8975 
8976 	err = i40e_vsi_open(vsi);
8977 	if (err)
8978 		return err;
8979 
8980 	/* configure global TSO hardware offload settings */
8981 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8982 						       TCP_FLAG_FIN) >> 16);
8983 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8984 						       TCP_FLAG_FIN |
8985 						       TCP_FLAG_CWR) >> 16);
8986 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8987 	udp_tunnel_get_rx_info(netdev);
8988 
8989 	return 0;
8990 }
8991 
8992 /**
8993  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
8994  * @vsi: vsi structure
8995  *
8996  * This updates netdev's number of tx/rx queues
8997  *
8998  * Returns status of setting tx/rx queues
8999  **/
9000 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
9001 {
9002 	int ret;
9003 
9004 	ret = netif_set_real_num_rx_queues(vsi->netdev,
9005 					   vsi->num_queue_pairs);
9006 	if (ret)
9007 		return ret;
9008 
9009 	return netif_set_real_num_tx_queues(vsi->netdev,
9010 					    vsi->num_queue_pairs);
9011 }
9012 
9013 /**
9014  * i40e_vsi_open -
9015  * @vsi: the VSI to open
9016  *
9017  * Finish initialization of the VSI.
9018  *
9019  * Returns 0 on success, negative value on failure
9020  *
9021  * Note: expects to be called while under rtnl_lock()
9022  **/
9023 int i40e_vsi_open(struct i40e_vsi *vsi)
9024 {
9025 	struct i40e_pf *pf = vsi->back;
9026 	char int_name[I40E_INT_NAME_STR_LEN];
9027 	int err;
9028 
9029 	/* allocate descriptors */
9030 	err = i40e_vsi_setup_tx_resources(vsi);
9031 	if (err)
9032 		goto err_setup_tx;
9033 	err = i40e_vsi_setup_rx_resources(vsi);
9034 	if (err)
9035 		goto err_setup_rx;
9036 
9037 	err = i40e_vsi_configure(vsi);
9038 	if (err)
9039 		goto err_setup_rx;
9040 
9041 	if (vsi->netdev) {
9042 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9043 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9044 		err = i40e_vsi_request_irq(vsi, int_name);
9045 		if (err)
9046 			goto err_setup_rx;
9047 
9048 		/* Notify the stack of the actual queue counts. */
9049 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9050 		if (err)
9051 			goto err_set_queues;
9052 
9053 	} else if (vsi->type == I40E_VSI_FDIR) {
9054 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9055 			 dev_driver_string(&pf->pdev->dev),
9056 			 dev_name(&pf->pdev->dev));
9057 		err = i40e_vsi_request_irq(vsi, int_name);
9058 		if (err)
9059 			goto err_setup_rx;
9060 
9061 	} else {
9062 		err = -EINVAL;
9063 		goto err_setup_rx;
9064 	}
9065 
9066 	err = i40e_up_complete(vsi);
9067 	if (err)
9068 		goto err_up_complete;
9069 
9070 	return 0;
9071 
9072 err_up_complete:
9073 	i40e_down(vsi);
9074 err_set_queues:
9075 	i40e_vsi_free_irq(vsi);
9076 err_setup_rx:
9077 	i40e_vsi_free_rx_resources(vsi);
9078 err_setup_tx:
9079 	i40e_vsi_free_tx_resources(vsi);
9080 	if (vsi == pf->vsi[pf->lan_vsi])
9081 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9082 
9083 	return err;
9084 }
9085 
9086 /**
9087  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9088  * @pf: Pointer to PF
9089  *
9090  * This function destroys the hlist where all the Flow Director
9091  * filters were saved.
9092  **/
9093 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9094 {
9095 	struct i40e_fdir_filter *filter;
9096 	struct i40e_flex_pit *pit_entry, *tmp;
9097 	struct hlist_node *node2;
9098 
9099 	hlist_for_each_entry_safe(filter, node2,
9100 				  &pf->fdir_filter_list, fdir_node) {
9101 		hlist_del(&filter->fdir_node);
9102 		kfree(filter);
9103 	}
9104 
9105 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9106 		list_del(&pit_entry->list);
9107 		kfree(pit_entry);
9108 	}
9109 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9110 
9111 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9112 		list_del(&pit_entry->list);
9113 		kfree(pit_entry);
9114 	}
9115 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9116 
9117 	pf->fdir_pf_active_filters = 0;
9118 	i40e_reset_fdir_filter_cnt(pf);
9119 
9120 	/* Reprogram the default input set for TCP/IPv4 */
9121 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9122 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9123 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9124 
9125 	/* Reprogram the default input set for TCP/IPv6 */
9126 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9127 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9128 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9129 
9130 	/* Reprogram the default input set for UDP/IPv4 */
9131 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9132 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9133 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9134 
9135 	/* Reprogram the default input set for UDP/IPv6 */
9136 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9137 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9138 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9139 
9140 	/* Reprogram the default input set for SCTP/IPv4 */
9141 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9142 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9143 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9144 
9145 	/* Reprogram the default input set for SCTP/IPv6 */
9146 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9147 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9148 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9149 
9150 	/* Reprogram the default input set for Other/IPv4 */
9151 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9152 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9153 
9154 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9155 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9156 
9157 	/* Reprogram the default input set for Other/IPv6 */
9158 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9159 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9160 
9161 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9162 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9163 }
9164 
9165 /**
9166  * i40e_cloud_filter_exit - Cleans up the cloud filters
9167  * @pf: Pointer to PF
9168  *
9169  * This function destroys the hlist where all the cloud filters
9170  * were saved.
9171  **/
9172 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9173 {
9174 	struct i40e_cloud_filter *cfilter;
9175 	struct hlist_node *node;
9176 
9177 	hlist_for_each_entry_safe(cfilter, node,
9178 				  &pf->cloud_filter_list, cloud_node) {
9179 		hlist_del(&cfilter->cloud_node);
9180 		kfree(cfilter);
9181 	}
9182 	pf->num_cloud_filters = 0;
9183 
9184 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
9185 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
9186 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
9187 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
9188 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
9189 	}
9190 }
9191 
9192 /**
9193  * i40e_close - Disables a network interface
9194  * @netdev: network interface device structure
9195  *
9196  * The close entry point is called when an interface is de-activated
9197  * by the OS.  The hardware is still under the driver's control, but
9198  * this netdev interface is disabled.
9199  *
9200  * Returns 0, this is not allowed to fail
9201  **/
9202 int i40e_close(struct net_device *netdev)
9203 {
9204 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9205 	struct i40e_vsi *vsi = np->vsi;
9206 
9207 	i40e_vsi_close(vsi);
9208 
9209 	return 0;
9210 }
9211 
9212 /**
9213  * i40e_do_reset - Start a PF or Core Reset sequence
9214  * @pf: board private structure
9215  * @reset_flags: which reset is requested
9216  * @lock_acquired: indicates whether or not the lock has been acquired
9217  * before this function was called.
9218  *
9219  * The essential difference in resets is that the PF Reset
9220  * doesn't clear the packet buffers, doesn't reset the PE
9221  * firmware, and doesn't bother the other PFs on the chip.
9222  **/
9223 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9224 {
9225 	u32 val;
9226 
9227 	/* do the biggest reset indicated */
9228 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9229 
9230 		/* Request a Global Reset
9231 		 *
9232 		 * This will start the chip's countdown to the actual full
9233 		 * chip reset event, and a warning interrupt to be sent
9234 		 * to all PFs, including the requestor.  Our handler
9235 		 * for the warning interrupt will deal with the shutdown
9236 		 * and recovery of the switch setup.
9237 		 */
9238 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9239 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9240 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9241 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9242 
9243 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9244 
9245 		/* Request a Core Reset
9246 		 *
9247 		 * Same as Global Reset, except does *not* include the MAC/PHY
9248 		 */
9249 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9250 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9251 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9252 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9253 		i40e_flush(&pf->hw);
9254 
9255 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9256 
9257 		/* Request a PF Reset
9258 		 *
9259 		 * Resets only the PF-specific registers
9260 		 *
9261 		 * This goes directly to the tear-down and rebuild of
9262 		 * the switch, since we need to do all the recovery as
9263 		 * for the Core Reset.
9264 		 */
9265 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9266 		i40e_handle_reset_warning(pf, lock_acquired);
9267 
9268 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9269 		/* Request a PF Reset
9270 		 *
9271 		 * Resets PF and reinitializes PFs VSI.
9272 		 */
9273 		i40e_prep_for_reset(pf);
9274 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9275 		dev_info(&pf->pdev->dev,
9276 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9277 			 "FW LLDP is disabled\n" :
9278 			 "FW LLDP is enabled\n");
9279 
9280 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9281 		int v;
9282 
9283 		/* Find the VSI(s) that requested a re-init */
9284 		dev_info(&pf->pdev->dev,
9285 			 "VSI reinit requested\n");
9286 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9287 			struct i40e_vsi *vsi = pf->vsi[v];
9288 
9289 			if (vsi != NULL &&
9290 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9291 					       vsi->state))
9292 				i40e_vsi_reinit_locked(pf->vsi[v]);
9293 		}
9294 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9295 		int v;
9296 
9297 		/* Find the VSI(s) that needs to be brought down */
9298 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9299 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9300 			struct i40e_vsi *vsi = pf->vsi[v];
9301 
9302 			if (vsi != NULL &&
9303 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9304 					       vsi->state)) {
9305 				set_bit(__I40E_VSI_DOWN, vsi->state);
9306 				i40e_down(vsi);
9307 			}
9308 		}
9309 	} else {
9310 		dev_info(&pf->pdev->dev,
9311 			 "bad reset request 0x%08x\n", reset_flags);
9312 	}
9313 }
9314 
9315 #ifdef CONFIG_I40E_DCB
9316 /**
9317  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9318  * @pf: board private structure
9319  * @old_cfg: current DCB config
9320  * @new_cfg: new DCB config
9321  **/
9322 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9323 			    struct i40e_dcbx_config *old_cfg,
9324 			    struct i40e_dcbx_config *new_cfg)
9325 {
9326 	bool need_reconfig = false;
9327 
9328 	/* Check if ETS configuration has changed */
9329 	if (memcmp(&new_cfg->etscfg,
9330 		   &old_cfg->etscfg,
9331 		   sizeof(new_cfg->etscfg))) {
9332 		/* If Priority Table has changed reconfig is needed */
9333 		if (memcmp(&new_cfg->etscfg.prioritytable,
9334 			   &old_cfg->etscfg.prioritytable,
9335 			   sizeof(new_cfg->etscfg.prioritytable))) {
9336 			need_reconfig = true;
9337 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9338 		}
9339 
9340 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9341 			   &old_cfg->etscfg.tcbwtable,
9342 			   sizeof(new_cfg->etscfg.tcbwtable)))
9343 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9344 
9345 		if (memcmp(&new_cfg->etscfg.tsatable,
9346 			   &old_cfg->etscfg.tsatable,
9347 			   sizeof(new_cfg->etscfg.tsatable)))
9348 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9349 	}
9350 
9351 	/* Check if PFC configuration has changed */
9352 	if (memcmp(&new_cfg->pfc,
9353 		   &old_cfg->pfc,
9354 		   sizeof(new_cfg->pfc))) {
9355 		need_reconfig = true;
9356 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9357 	}
9358 
9359 	/* Check if APP Table has changed */
9360 	if (memcmp(&new_cfg->app,
9361 		   &old_cfg->app,
9362 		   sizeof(new_cfg->app))) {
9363 		need_reconfig = true;
9364 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9365 	}
9366 
9367 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9368 	return need_reconfig;
9369 }
9370 
9371 /**
9372  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9373  * @pf: board private structure
9374  * @e: event info posted on ARQ
9375  **/
9376 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9377 				  struct i40e_arq_event_info *e)
9378 {
9379 	struct i40e_aqc_lldp_get_mib *mib =
9380 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9381 	struct i40e_hw *hw = &pf->hw;
9382 	struct i40e_dcbx_config tmp_dcbx_cfg;
9383 	bool need_reconfig = false;
9384 	int ret = 0;
9385 	u8 type;
9386 
9387 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9388 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9389 	    (hw->phy.link_info.link_speed &
9390 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9391 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9392 		/* let firmware decide if the DCB should be disabled */
9393 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9394 
9395 	/* Not DCB capable or capability disabled */
9396 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9397 		return ret;
9398 
9399 	/* Ignore if event is not for Nearest Bridge */
9400 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9401 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9402 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9403 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9404 		return ret;
9405 
9406 	/* Check MIB Type and return if event for Remote MIB update */
9407 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9408 	dev_dbg(&pf->pdev->dev,
9409 		"LLDP event mib type %s\n", type ? "remote" : "local");
9410 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9411 		/* Update the remote cached instance and return */
9412 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9413 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9414 				&hw->remote_dcbx_config);
9415 		goto exit;
9416 	}
9417 
9418 	/* Store the old configuration */
9419 	tmp_dcbx_cfg = hw->local_dcbx_config;
9420 
9421 	/* Reset the old DCBx configuration data */
9422 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9423 	/* Get updated DCBX data from firmware */
9424 	ret = i40e_get_dcb_config(&pf->hw);
9425 	if (ret) {
9426 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9427 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9428 		    (hw->phy.link_info.link_speed &
9429 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9430 			dev_warn(&pf->pdev->dev,
9431 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9432 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9433 		} else {
9434 			dev_info(&pf->pdev->dev,
9435 				 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
9436 				 i40e_stat_str(&pf->hw, ret),
9437 				 i40e_aq_str(&pf->hw,
9438 					     pf->hw.aq.asq_last_status));
9439 		}
9440 		goto exit;
9441 	}
9442 
9443 	/* No change detected in DCBX configs */
9444 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9445 		    sizeof(tmp_dcbx_cfg))) {
9446 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9447 		goto exit;
9448 	}
9449 
9450 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9451 					       &hw->local_dcbx_config);
9452 
9453 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9454 
9455 	if (!need_reconfig)
9456 		goto exit;
9457 
9458 	/* Enable DCB tagging only when more than one TC */
9459 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9460 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9461 	else
9462 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9463 
9464 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9465 	/* Reconfiguration needed quiesce all VSIs */
9466 	i40e_pf_quiesce_all_vsi(pf);
9467 
9468 	/* Changes in configuration update VEB/VSI */
9469 	i40e_dcb_reconfigure(pf);
9470 
9471 	ret = i40e_resume_port_tx(pf);
9472 
9473 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9474 	/* In case of error no point in resuming VSIs */
9475 	if (ret)
9476 		goto exit;
9477 
9478 	/* Wait for the PF's queues to be disabled */
9479 	ret = i40e_pf_wait_queues_disabled(pf);
9480 	if (ret) {
9481 		/* Schedule PF reset to recover */
9482 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9483 		i40e_service_event_schedule(pf);
9484 	} else {
9485 		i40e_pf_unquiesce_all_vsi(pf);
9486 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9487 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9488 	}
9489 
9490 exit:
9491 	return ret;
9492 }
9493 #endif /* CONFIG_I40E_DCB */
9494 
9495 /**
9496  * i40e_do_reset_safe - Protected reset path for userland calls.
9497  * @pf: board private structure
9498  * @reset_flags: which reset is requested
9499  *
9500  **/
9501 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9502 {
9503 	rtnl_lock();
9504 	i40e_do_reset(pf, reset_flags, true);
9505 	rtnl_unlock();
9506 }
9507 
9508 /**
9509  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9510  * @pf: board private structure
9511  * @e: event info posted on ARQ
9512  *
9513  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9514  * and VF queues
9515  **/
9516 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9517 					   struct i40e_arq_event_info *e)
9518 {
9519 	struct i40e_aqc_lan_overflow *data =
9520 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9521 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9522 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9523 	struct i40e_hw *hw = &pf->hw;
9524 	struct i40e_vf *vf;
9525 	u16 vf_id;
9526 
9527 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9528 		queue, qtx_ctl);
9529 
9530 	/* Queue belongs to VF, find the VF and issue VF reset */
9531 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9532 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9533 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9534 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9535 		vf_id -= hw->func_caps.vf_base_id;
9536 		vf = &pf->vf[vf_id];
9537 		i40e_vc_notify_vf_reset(vf);
9538 		/* Allow VF to process pending reset notification */
9539 		msleep(20);
9540 		i40e_reset_vf(vf, false);
9541 	}
9542 }
9543 
9544 /**
9545  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9546  * @pf: board private structure
9547  **/
9548 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9549 {
9550 	u32 val, fcnt_prog;
9551 
9552 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9553 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9554 	return fcnt_prog;
9555 }
9556 
9557 /**
9558  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9559  * @pf: board private structure
9560  **/
9561 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9562 {
9563 	u32 val, fcnt_prog;
9564 
9565 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9566 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9567 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9568 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9569 	return fcnt_prog;
9570 }
9571 
9572 /**
9573  * i40e_get_global_fd_count - Get total FD filters programmed on device
9574  * @pf: board private structure
9575  **/
9576 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9577 {
9578 	u32 val, fcnt_prog;
9579 
9580 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9581 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9582 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9583 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9584 	return fcnt_prog;
9585 }
9586 
9587 /**
9588  * i40e_reenable_fdir_sb - Restore FDir SB capability
9589  * @pf: board private structure
9590  **/
9591 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9592 {
9593 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9594 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9595 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9596 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9597 }
9598 
9599 /**
9600  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9601  * @pf: board private structure
9602  **/
9603 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9604 {
9605 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9606 		/* ATR uses the same filtering logic as SB rules. It only
9607 		 * functions properly if the input set mask is at the default
9608 		 * settings. It is safe to restore the default input set
9609 		 * because there are no active TCPv4 filter rules.
9610 		 */
9611 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9612 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9613 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9614 
9615 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9616 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9617 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9618 	}
9619 }
9620 
9621 /**
9622  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9623  * @pf: board private structure
9624  * @filter: FDir filter to remove
9625  */
9626 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9627 				       struct i40e_fdir_filter *filter)
9628 {
9629 	/* Update counters */
9630 	pf->fdir_pf_active_filters--;
9631 	pf->fd_inv = 0;
9632 
9633 	switch (filter->flow_type) {
9634 	case TCP_V4_FLOW:
9635 		pf->fd_tcp4_filter_cnt--;
9636 		break;
9637 	case UDP_V4_FLOW:
9638 		pf->fd_udp4_filter_cnt--;
9639 		break;
9640 	case SCTP_V4_FLOW:
9641 		pf->fd_sctp4_filter_cnt--;
9642 		break;
9643 	case TCP_V6_FLOW:
9644 		pf->fd_tcp6_filter_cnt--;
9645 		break;
9646 	case UDP_V6_FLOW:
9647 		pf->fd_udp6_filter_cnt--;
9648 		break;
9649 	case SCTP_V6_FLOW:
9650 		pf->fd_udp6_filter_cnt--;
9651 		break;
9652 	case IP_USER_FLOW:
9653 		switch (filter->ipl4_proto) {
9654 		case IPPROTO_TCP:
9655 			pf->fd_tcp4_filter_cnt--;
9656 			break;
9657 		case IPPROTO_UDP:
9658 			pf->fd_udp4_filter_cnt--;
9659 			break;
9660 		case IPPROTO_SCTP:
9661 			pf->fd_sctp4_filter_cnt--;
9662 			break;
9663 		case IPPROTO_IP:
9664 			pf->fd_ip4_filter_cnt--;
9665 			break;
9666 		}
9667 		break;
9668 	case IPV6_USER_FLOW:
9669 		switch (filter->ipl4_proto) {
9670 		case IPPROTO_TCP:
9671 			pf->fd_tcp6_filter_cnt--;
9672 			break;
9673 		case IPPROTO_UDP:
9674 			pf->fd_udp6_filter_cnt--;
9675 			break;
9676 		case IPPROTO_SCTP:
9677 			pf->fd_sctp6_filter_cnt--;
9678 			break;
9679 		case IPPROTO_IP:
9680 			pf->fd_ip6_filter_cnt--;
9681 			break;
9682 		}
9683 		break;
9684 	}
9685 
9686 	/* Remove the filter from the list and free memory */
9687 	hlist_del(&filter->fdir_node);
9688 	kfree(filter);
9689 }
9690 
9691 /**
9692  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9693  * @pf: board private structure
9694  **/
9695 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9696 {
9697 	struct i40e_fdir_filter *filter;
9698 	u32 fcnt_prog, fcnt_avail;
9699 	struct hlist_node *node;
9700 
9701 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9702 		return;
9703 
9704 	/* Check if we have enough room to re-enable FDir SB capability. */
9705 	fcnt_prog = i40e_get_global_fd_count(pf);
9706 	fcnt_avail = pf->fdir_pf_filter_count;
9707 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9708 	    (pf->fd_add_err == 0) ||
9709 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9710 		i40e_reenable_fdir_sb(pf);
9711 
9712 	/* We should wait for even more space before re-enabling ATR.
9713 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9714 	 * rules active.
9715 	 */
9716 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9717 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9718 		i40e_reenable_fdir_atr(pf);
9719 
9720 	/* if hw had a problem adding a filter, delete it */
9721 	if (pf->fd_inv > 0) {
9722 		hlist_for_each_entry_safe(filter, node,
9723 					  &pf->fdir_filter_list, fdir_node)
9724 			if (filter->fd_id == pf->fd_inv)
9725 				i40e_delete_invalid_filter(pf, filter);
9726 	}
9727 }
9728 
9729 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9730 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9731 /**
9732  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9733  * @pf: board private structure
9734  **/
9735 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9736 {
9737 	unsigned long min_flush_time;
9738 	int flush_wait_retry = 50;
9739 	bool disable_atr = false;
9740 	int fd_room;
9741 	int reg;
9742 
9743 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9744 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9745 		return;
9746 
9747 	/* If the flush is happening too quick and we have mostly SB rules we
9748 	 * should not re-enable ATR for some time.
9749 	 */
9750 	min_flush_time = pf->fd_flush_timestamp +
9751 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9752 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9753 
9754 	if (!(time_after(jiffies, min_flush_time)) &&
9755 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9756 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9757 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9758 		disable_atr = true;
9759 	}
9760 
9761 	pf->fd_flush_timestamp = jiffies;
9762 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9763 	/* flush all filters */
9764 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9765 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9766 	i40e_flush(&pf->hw);
9767 	pf->fd_flush_cnt++;
9768 	pf->fd_add_err = 0;
9769 	do {
9770 		/* Check FD flush status every 5-6msec */
9771 		usleep_range(5000, 6000);
9772 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9773 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9774 			break;
9775 	} while (flush_wait_retry--);
9776 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9777 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9778 	} else {
9779 		/* replay sideband filters */
9780 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9781 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9782 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9783 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9784 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9785 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9786 	}
9787 }
9788 
9789 /**
9790  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9791  * @pf: board private structure
9792  **/
9793 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9794 {
9795 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9796 }
9797 
9798 /**
9799  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9800  * @pf: board private structure
9801  **/
9802 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9803 {
9804 
9805 	/* if interface is down do nothing */
9806 	if (test_bit(__I40E_DOWN, pf->state))
9807 		return;
9808 
9809 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9810 		i40e_fdir_flush_and_replay(pf);
9811 
9812 	i40e_fdir_check_and_reenable(pf);
9813 
9814 }
9815 
9816 /**
9817  * i40e_vsi_link_event - notify VSI of a link event
9818  * @vsi: vsi to be notified
9819  * @link_up: link up or down
9820  **/
9821 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9822 {
9823 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9824 		return;
9825 
9826 	switch (vsi->type) {
9827 	case I40E_VSI_MAIN:
9828 		if (!vsi->netdev || !vsi->netdev_registered)
9829 			break;
9830 
9831 		if (link_up) {
9832 			netif_carrier_on(vsi->netdev);
9833 			netif_tx_wake_all_queues(vsi->netdev);
9834 		} else {
9835 			netif_carrier_off(vsi->netdev);
9836 			netif_tx_stop_all_queues(vsi->netdev);
9837 		}
9838 		break;
9839 
9840 	case I40E_VSI_SRIOV:
9841 	case I40E_VSI_VMDQ2:
9842 	case I40E_VSI_CTRL:
9843 	case I40E_VSI_IWARP:
9844 	case I40E_VSI_MIRROR:
9845 	default:
9846 		/* there is no notification for other VSIs */
9847 		break;
9848 	}
9849 }
9850 
9851 /**
9852  * i40e_veb_link_event - notify elements on the veb of a link event
9853  * @veb: veb to be notified
9854  * @link_up: link up or down
9855  **/
9856 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9857 {
9858 	struct i40e_pf *pf;
9859 	int i;
9860 
9861 	if (!veb || !veb->pf)
9862 		return;
9863 	pf = veb->pf;
9864 
9865 	/* depth first... */
9866 	for (i = 0; i < I40E_MAX_VEB; i++)
9867 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9868 			i40e_veb_link_event(pf->veb[i], link_up);
9869 
9870 	/* ... now the local VSIs */
9871 	for (i = 0; i < pf->num_alloc_vsi; i++)
9872 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9873 			i40e_vsi_link_event(pf->vsi[i], link_up);
9874 }
9875 
9876 /**
9877  * i40e_link_event - Update netif_carrier status
9878  * @pf: board private structure
9879  **/
9880 static void i40e_link_event(struct i40e_pf *pf)
9881 {
9882 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9883 	u8 new_link_speed, old_link_speed;
9884 	i40e_status status;
9885 	bool new_link, old_link;
9886 #ifdef CONFIG_I40E_DCB
9887 	int err;
9888 #endif /* CONFIG_I40E_DCB */
9889 
9890 	/* set this to force the get_link_status call to refresh state */
9891 	pf->hw.phy.get_link_info = true;
9892 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9893 	status = i40e_get_link_status(&pf->hw, &new_link);
9894 
9895 	/* On success, disable temp link polling */
9896 	if (status == I40E_SUCCESS) {
9897 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9898 	} else {
9899 		/* Enable link polling temporarily until i40e_get_link_status
9900 		 * returns I40E_SUCCESS
9901 		 */
9902 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9903 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9904 			status);
9905 		return;
9906 	}
9907 
9908 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9909 	new_link_speed = pf->hw.phy.link_info.link_speed;
9910 
9911 	if (new_link == old_link &&
9912 	    new_link_speed == old_link_speed &&
9913 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9914 	     new_link == netif_carrier_ok(vsi->netdev)))
9915 		return;
9916 
9917 	i40e_print_link_message(vsi, new_link);
9918 
9919 	/* Notify the base of the switch tree connected to
9920 	 * the link.  Floating VEBs are not notified.
9921 	 */
9922 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9923 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9924 	else
9925 		i40e_vsi_link_event(vsi, new_link);
9926 
9927 	if (pf->vf)
9928 		i40e_vc_notify_link_state(pf);
9929 
9930 	if (pf->flags & I40E_FLAG_PTP)
9931 		i40e_ptp_set_increment(pf);
9932 #ifdef CONFIG_I40E_DCB
9933 	if (new_link == old_link)
9934 		return;
9935 	/* Not SW DCB so firmware will take care of default settings */
9936 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9937 		return;
9938 
9939 	/* We cover here only link down, as after link up in case of SW DCB
9940 	 * SW LLDP agent will take care of setting it up
9941 	 */
9942 	if (!new_link) {
9943 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9944 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9945 		err = i40e_dcb_sw_default_config(pf);
9946 		if (err) {
9947 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9948 				       I40E_FLAG_DCB_ENABLED);
9949 		} else {
9950 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9951 				       DCB_CAP_DCBX_VER_IEEE;
9952 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9953 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9954 		}
9955 	}
9956 #endif /* CONFIG_I40E_DCB */
9957 }
9958 
9959 /**
9960  * i40e_watchdog_subtask - periodic checks not using event driven response
9961  * @pf: board private structure
9962  **/
9963 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9964 {
9965 	int i;
9966 
9967 	/* if interface is down do nothing */
9968 	if (test_bit(__I40E_DOWN, pf->state) ||
9969 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9970 		return;
9971 
9972 	/* make sure we don't do these things too often */
9973 	if (time_before(jiffies, (pf->service_timer_previous +
9974 				  pf->service_timer_period)))
9975 		return;
9976 	pf->service_timer_previous = jiffies;
9977 
9978 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9979 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9980 		i40e_link_event(pf);
9981 
9982 	/* Update the stats for active netdevs so the network stack
9983 	 * can look at updated numbers whenever it cares to
9984 	 */
9985 	for (i = 0; i < pf->num_alloc_vsi; i++)
9986 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9987 			i40e_update_stats(pf->vsi[i]);
9988 
9989 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9990 		/* Update the stats for the active switching components */
9991 		for (i = 0; i < I40E_MAX_VEB; i++)
9992 			if (pf->veb[i])
9993 				i40e_update_veb_stats(pf->veb[i]);
9994 	}
9995 
9996 	i40e_ptp_rx_hang(pf);
9997 	i40e_ptp_tx_hang(pf);
9998 }
9999 
10000 /**
10001  * i40e_reset_subtask - Set up for resetting the device and driver
10002  * @pf: board private structure
10003  **/
10004 static void i40e_reset_subtask(struct i40e_pf *pf)
10005 {
10006 	u32 reset_flags = 0;
10007 
10008 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10009 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10010 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10011 	}
10012 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10013 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10014 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10015 	}
10016 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10017 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10018 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10019 	}
10020 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10021 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10022 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10023 	}
10024 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10025 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10026 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10027 	}
10028 
10029 	/* If there's a recovery already waiting, it takes
10030 	 * precedence before starting a new reset sequence.
10031 	 */
10032 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10033 		i40e_prep_for_reset(pf);
10034 		i40e_reset(pf);
10035 		i40e_rebuild(pf, false, false);
10036 	}
10037 
10038 	/* If we're already down or resetting, just bail */
10039 	if (reset_flags &&
10040 	    !test_bit(__I40E_DOWN, pf->state) &&
10041 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10042 		i40e_do_reset(pf, reset_flags, false);
10043 	}
10044 }
10045 
10046 /**
10047  * i40e_handle_link_event - Handle link event
10048  * @pf: board private structure
10049  * @e: event info posted on ARQ
10050  **/
10051 static void i40e_handle_link_event(struct i40e_pf *pf,
10052 				   struct i40e_arq_event_info *e)
10053 {
10054 	struct i40e_aqc_get_link_status *status =
10055 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
10056 
10057 	/* Do a new status request to re-enable LSE reporting
10058 	 * and load new status information into the hw struct
10059 	 * This completely ignores any state information
10060 	 * in the ARQ event info, instead choosing to always
10061 	 * issue the AQ update link status command.
10062 	 */
10063 	i40e_link_event(pf);
10064 
10065 	/* Check if module meets thermal requirements */
10066 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10067 		dev_err(&pf->pdev->dev,
10068 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10069 		dev_err(&pf->pdev->dev,
10070 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10071 	} else {
10072 		/* check for unqualified module, if link is down, suppress
10073 		 * the message if link was forced to be down.
10074 		 */
10075 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10076 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10077 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10078 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
10079 			dev_err(&pf->pdev->dev,
10080 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10081 			dev_err(&pf->pdev->dev,
10082 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10083 		}
10084 	}
10085 }
10086 
10087 /**
10088  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10089  * @pf: board private structure
10090  **/
10091 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10092 {
10093 	struct i40e_arq_event_info event;
10094 	struct i40e_hw *hw = &pf->hw;
10095 	u16 pending, i = 0;
10096 	i40e_status ret;
10097 	u16 opcode;
10098 	u32 oldval;
10099 	u32 val;
10100 
10101 	/* Do not run clean AQ when PF reset fails */
10102 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10103 		return;
10104 
10105 	/* check for error indications */
10106 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
10107 	oldval = val;
10108 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10109 		if (hw->debug_mask & I40E_DEBUG_AQ)
10110 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10111 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10112 	}
10113 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10114 		if (hw->debug_mask & I40E_DEBUG_AQ)
10115 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10116 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10117 		pf->arq_overflows++;
10118 	}
10119 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10120 		if (hw->debug_mask & I40E_DEBUG_AQ)
10121 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10122 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10123 	}
10124 	if (oldval != val)
10125 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
10126 
10127 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
10128 	oldval = val;
10129 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10130 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10131 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10132 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10133 	}
10134 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10135 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10136 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10137 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10138 	}
10139 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10140 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10141 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10142 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10143 	}
10144 	if (oldval != val)
10145 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
10146 
10147 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10148 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10149 	if (!event.msg_buf)
10150 		return;
10151 
10152 	do {
10153 		ret = i40e_clean_arq_element(hw, &event, &pending);
10154 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
10155 			break;
10156 		else if (ret) {
10157 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10158 			break;
10159 		}
10160 
10161 		opcode = le16_to_cpu(event.desc.opcode);
10162 		switch (opcode) {
10163 
10164 		case i40e_aqc_opc_get_link_status:
10165 			rtnl_lock();
10166 			i40e_handle_link_event(pf, &event);
10167 			rtnl_unlock();
10168 			break;
10169 		case i40e_aqc_opc_send_msg_to_pf:
10170 			ret = i40e_vc_process_vf_msg(pf,
10171 					le16_to_cpu(event.desc.retval),
10172 					le32_to_cpu(event.desc.cookie_high),
10173 					le32_to_cpu(event.desc.cookie_low),
10174 					event.msg_buf,
10175 					event.msg_len);
10176 			break;
10177 		case i40e_aqc_opc_lldp_update_mib:
10178 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10179 #ifdef CONFIG_I40E_DCB
10180 			rtnl_lock();
10181 			i40e_handle_lldp_event(pf, &event);
10182 			rtnl_unlock();
10183 #endif /* CONFIG_I40E_DCB */
10184 			break;
10185 		case i40e_aqc_opc_event_lan_overflow:
10186 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10187 			i40e_handle_lan_overflow_event(pf, &event);
10188 			break;
10189 		case i40e_aqc_opc_send_msg_to_peer:
10190 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10191 			break;
10192 		case i40e_aqc_opc_nvm_erase:
10193 		case i40e_aqc_opc_nvm_update:
10194 		case i40e_aqc_opc_oem_post_update:
10195 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10196 				   "ARQ NVM operation 0x%04x completed\n",
10197 				   opcode);
10198 			break;
10199 		default:
10200 			dev_info(&pf->pdev->dev,
10201 				 "ARQ: Unknown event 0x%04x ignored\n",
10202 				 opcode);
10203 			break;
10204 		}
10205 	} while (i++ < pf->adminq_work_limit);
10206 
10207 	if (i < pf->adminq_work_limit)
10208 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10209 
10210 	/* re-enable Admin queue interrupt cause */
10211 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10212 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10213 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10214 	i40e_flush(hw);
10215 
10216 	kfree(event.msg_buf);
10217 }
10218 
10219 /**
10220  * i40e_verify_eeprom - make sure eeprom is good to use
10221  * @pf: board private structure
10222  **/
10223 static void i40e_verify_eeprom(struct i40e_pf *pf)
10224 {
10225 	int err;
10226 
10227 	err = i40e_diag_eeprom_test(&pf->hw);
10228 	if (err) {
10229 		/* retry in case of garbage read */
10230 		err = i40e_diag_eeprom_test(&pf->hw);
10231 		if (err) {
10232 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10233 				 err);
10234 			set_bit(__I40E_BAD_EEPROM, pf->state);
10235 		}
10236 	}
10237 
10238 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10239 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10240 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10241 	}
10242 }
10243 
10244 /**
10245  * i40e_enable_pf_switch_lb
10246  * @pf: pointer to the PF structure
10247  *
10248  * enable switch loop back or die - no point in a return value
10249  **/
10250 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10251 {
10252 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10253 	struct i40e_vsi_context ctxt;
10254 	int ret;
10255 
10256 	ctxt.seid = pf->main_vsi_seid;
10257 	ctxt.pf_num = pf->hw.pf_id;
10258 	ctxt.vf_num = 0;
10259 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10260 	if (ret) {
10261 		dev_info(&pf->pdev->dev,
10262 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10263 			 i40e_stat_str(&pf->hw, ret),
10264 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10265 		return;
10266 	}
10267 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10268 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10269 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10270 
10271 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10272 	if (ret) {
10273 		dev_info(&pf->pdev->dev,
10274 			 "update vsi switch failed, err %s aq_err %s\n",
10275 			 i40e_stat_str(&pf->hw, ret),
10276 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10277 	}
10278 }
10279 
10280 /**
10281  * i40e_disable_pf_switch_lb
10282  * @pf: pointer to the PF structure
10283  *
10284  * disable switch loop back or die - no point in a return value
10285  **/
10286 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10287 {
10288 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10289 	struct i40e_vsi_context ctxt;
10290 	int ret;
10291 
10292 	ctxt.seid = pf->main_vsi_seid;
10293 	ctxt.pf_num = pf->hw.pf_id;
10294 	ctxt.vf_num = 0;
10295 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10296 	if (ret) {
10297 		dev_info(&pf->pdev->dev,
10298 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10299 			 i40e_stat_str(&pf->hw, ret),
10300 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10301 		return;
10302 	}
10303 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10304 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10305 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10306 
10307 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10308 	if (ret) {
10309 		dev_info(&pf->pdev->dev,
10310 			 "update vsi switch failed, err %s aq_err %s\n",
10311 			 i40e_stat_str(&pf->hw, ret),
10312 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10313 	}
10314 }
10315 
10316 /**
10317  * i40e_config_bridge_mode - Configure the HW bridge mode
10318  * @veb: pointer to the bridge instance
10319  *
10320  * Configure the loop back mode for the LAN VSI that is downlink to the
10321  * specified HW bridge instance. It is expected this function is called
10322  * when a new HW bridge is instantiated.
10323  **/
10324 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10325 {
10326 	struct i40e_pf *pf = veb->pf;
10327 
10328 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10329 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10330 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10331 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10332 		i40e_disable_pf_switch_lb(pf);
10333 	else
10334 		i40e_enable_pf_switch_lb(pf);
10335 }
10336 
10337 /**
10338  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10339  * @veb: pointer to the VEB instance
10340  *
10341  * This is a recursive function that first builds the attached VSIs then
10342  * recurses in to build the next layer of VEB.  We track the connections
10343  * through our own index numbers because the seid's from the HW could
10344  * change across the reset.
10345  **/
10346 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10347 {
10348 	struct i40e_vsi *ctl_vsi = NULL;
10349 	struct i40e_pf *pf = veb->pf;
10350 	int v, veb_idx;
10351 	int ret;
10352 
10353 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10354 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10355 		if (pf->vsi[v] &&
10356 		    pf->vsi[v]->veb_idx == veb->idx &&
10357 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10358 			ctl_vsi = pf->vsi[v];
10359 			break;
10360 		}
10361 	}
10362 	if (!ctl_vsi) {
10363 		dev_info(&pf->pdev->dev,
10364 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10365 		ret = -ENOENT;
10366 		goto end_reconstitute;
10367 	}
10368 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10369 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10370 	ret = i40e_add_vsi(ctl_vsi);
10371 	if (ret) {
10372 		dev_info(&pf->pdev->dev,
10373 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10374 			 veb->idx, ret);
10375 		goto end_reconstitute;
10376 	}
10377 	i40e_vsi_reset_stats(ctl_vsi);
10378 
10379 	/* create the VEB in the switch and move the VSI onto the VEB */
10380 	ret = i40e_add_veb(veb, ctl_vsi);
10381 	if (ret)
10382 		goto end_reconstitute;
10383 
10384 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10385 		veb->bridge_mode = BRIDGE_MODE_VEB;
10386 	else
10387 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10388 	i40e_config_bridge_mode(veb);
10389 
10390 	/* create the remaining VSIs attached to this VEB */
10391 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10392 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10393 			continue;
10394 
10395 		if (pf->vsi[v]->veb_idx == veb->idx) {
10396 			struct i40e_vsi *vsi = pf->vsi[v];
10397 
10398 			vsi->uplink_seid = veb->seid;
10399 			ret = i40e_add_vsi(vsi);
10400 			if (ret) {
10401 				dev_info(&pf->pdev->dev,
10402 					 "rebuild of vsi_idx %d failed: %d\n",
10403 					 v, ret);
10404 				goto end_reconstitute;
10405 			}
10406 			i40e_vsi_reset_stats(vsi);
10407 		}
10408 	}
10409 
10410 	/* create any VEBs attached to this VEB - RECURSION */
10411 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10412 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10413 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10414 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10415 			if (ret)
10416 				break;
10417 		}
10418 	}
10419 
10420 end_reconstitute:
10421 	return ret;
10422 }
10423 
10424 /**
10425  * i40e_get_capabilities - get info about the HW
10426  * @pf: the PF struct
10427  * @list_type: AQ capability to be queried
10428  **/
10429 static int i40e_get_capabilities(struct i40e_pf *pf,
10430 				 enum i40e_admin_queue_opc list_type)
10431 {
10432 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10433 	u16 data_size;
10434 	int buf_len;
10435 	int err;
10436 
10437 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10438 	do {
10439 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10440 		if (!cap_buf)
10441 			return -ENOMEM;
10442 
10443 		/* this loads the data into the hw struct for us */
10444 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10445 						    &data_size, list_type,
10446 						    NULL);
10447 		/* data loaded, buffer no longer needed */
10448 		kfree(cap_buf);
10449 
10450 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10451 			/* retry with a larger buffer */
10452 			buf_len = data_size;
10453 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10454 			dev_info(&pf->pdev->dev,
10455 				 "capability discovery failed, err %s aq_err %s\n",
10456 				 i40e_stat_str(&pf->hw, err),
10457 				 i40e_aq_str(&pf->hw,
10458 					     pf->hw.aq.asq_last_status));
10459 			return -ENODEV;
10460 		}
10461 	} while (err);
10462 
10463 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10464 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10465 			dev_info(&pf->pdev->dev,
10466 				 "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",
10467 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10468 				 pf->hw.func_caps.num_msix_vectors,
10469 				 pf->hw.func_caps.num_msix_vectors_vf,
10470 				 pf->hw.func_caps.fd_filters_guaranteed,
10471 				 pf->hw.func_caps.fd_filters_best_effort,
10472 				 pf->hw.func_caps.num_tx_qp,
10473 				 pf->hw.func_caps.num_vsis);
10474 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10475 			dev_info(&pf->pdev->dev,
10476 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10477 				 pf->hw.dev_caps.switch_mode,
10478 				 pf->hw.dev_caps.valid_functions);
10479 			dev_info(&pf->pdev->dev,
10480 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10481 				 pf->hw.dev_caps.sr_iov_1_1,
10482 				 pf->hw.dev_caps.num_vfs);
10483 			dev_info(&pf->pdev->dev,
10484 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10485 				 pf->hw.dev_caps.num_vsis,
10486 				 pf->hw.dev_caps.num_rx_qp,
10487 				 pf->hw.dev_caps.num_tx_qp);
10488 		}
10489 	}
10490 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10491 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10492 		       + pf->hw.func_caps.num_vfs)
10493 		if (pf->hw.revision_id == 0 &&
10494 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10495 			dev_info(&pf->pdev->dev,
10496 				 "got num_vsis %d, setting num_vsis to %d\n",
10497 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10498 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10499 		}
10500 	}
10501 	return 0;
10502 }
10503 
10504 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10505 
10506 /**
10507  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10508  * @pf: board private structure
10509  **/
10510 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10511 {
10512 	struct i40e_vsi *vsi;
10513 
10514 	/* quick workaround for an NVM issue that leaves a critical register
10515 	 * uninitialized
10516 	 */
10517 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10518 		static const u32 hkey[] = {
10519 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10520 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10521 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10522 			0x95b3a76d};
10523 		int i;
10524 
10525 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10526 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10527 	}
10528 
10529 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10530 		return;
10531 
10532 	/* find existing VSI and see if it needs configuring */
10533 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10534 
10535 	/* create a new VSI if none exists */
10536 	if (!vsi) {
10537 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10538 				     pf->vsi[pf->lan_vsi]->seid, 0);
10539 		if (!vsi) {
10540 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10541 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10542 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10543 			return;
10544 		}
10545 	}
10546 
10547 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10548 }
10549 
10550 /**
10551  * i40e_fdir_teardown - release the Flow Director resources
10552  * @pf: board private structure
10553  **/
10554 static void i40e_fdir_teardown(struct i40e_pf *pf)
10555 {
10556 	struct i40e_vsi *vsi;
10557 
10558 	i40e_fdir_filter_exit(pf);
10559 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10560 	if (vsi)
10561 		i40e_vsi_release(vsi);
10562 }
10563 
10564 /**
10565  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10566  * @vsi: PF main vsi
10567  * @seid: seid of main or channel VSIs
10568  *
10569  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10570  * existed before reset
10571  **/
10572 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10573 {
10574 	struct i40e_cloud_filter *cfilter;
10575 	struct i40e_pf *pf = vsi->back;
10576 	struct hlist_node *node;
10577 	i40e_status ret;
10578 
10579 	/* Add cloud filters back if they exist */
10580 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10581 				  cloud_node) {
10582 		if (cfilter->seid != seid)
10583 			continue;
10584 
10585 		if (cfilter->dst_port)
10586 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10587 								true);
10588 		else
10589 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10590 
10591 		if (ret) {
10592 			dev_dbg(&pf->pdev->dev,
10593 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
10594 				i40e_stat_str(&pf->hw, ret),
10595 				i40e_aq_str(&pf->hw,
10596 					    pf->hw.aq.asq_last_status));
10597 			return ret;
10598 		}
10599 	}
10600 	return 0;
10601 }
10602 
10603 /**
10604  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10605  * @vsi: PF main vsi
10606  *
10607  * Rebuilds channel VSIs if they existed before reset
10608  **/
10609 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10610 {
10611 	struct i40e_channel *ch, *ch_tmp;
10612 	i40e_status ret;
10613 
10614 	if (list_empty(&vsi->ch_list))
10615 		return 0;
10616 
10617 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10618 		if (!ch->initialized)
10619 			break;
10620 		/* Proceed with creation of channel (VMDq2) VSI */
10621 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10622 		if (ret) {
10623 			dev_info(&vsi->back->pdev->dev,
10624 				 "failed to rebuild channels using uplink_seid %u\n",
10625 				 vsi->uplink_seid);
10626 			return ret;
10627 		}
10628 		/* Reconfigure TX queues using QTX_CTL register */
10629 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10630 		if (ret) {
10631 			dev_info(&vsi->back->pdev->dev,
10632 				 "failed to configure TX rings for channel %u\n",
10633 				 ch->seid);
10634 			return ret;
10635 		}
10636 		/* update 'next_base_queue' */
10637 		vsi->next_base_queue = vsi->next_base_queue +
10638 							ch->num_queue_pairs;
10639 		if (ch->max_tx_rate) {
10640 			u64 credits = ch->max_tx_rate;
10641 
10642 			if (i40e_set_bw_limit(vsi, ch->seid,
10643 					      ch->max_tx_rate))
10644 				return -EINVAL;
10645 
10646 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10647 			dev_dbg(&vsi->back->pdev->dev,
10648 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10649 				ch->max_tx_rate,
10650 				credits,
10651 				ch->seid);
10652 		}
10653 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10654 		if (ret) {
10655 			dev_dbg(&vsi->back->pdev->dev,
10656 				"Failed to rebuild cloud filters for channel VSI %u\n",
10657 				ch->seid);
10658 			return ret;
10659 		}
10660 	}
10661 	return 0;
10662 }
10663 
10664 /**
10665  * i40e_prep_for_reset - prep for the core to reset
10666  * @pf: board private structure
10667  *
10668  * Close up the VFs and other things in prep for PF Reset.
10669   **/
10670 static void i40e_prep_for_reset(struct i40e_pf *pf)
10671 {
10672 	struct i40e_hw *hw = &pf->hw;
10673 	i40e_status ret = 0;
10674 	u32 v;
10675 
10676 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10677 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10678 		return;
10679 	if (i40e_check_asq_alive(&pf->hw))
10680 		i40e_vc_notify_reset(pf);
10681 
10682 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10683 
10684 	/* quiesce the VSIs and their queues that are not already DOWN */
10685 	i40e_pf_quiesce_all_vsi(pf);
10686 
10687 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10688 		if (pf->vsi[v])
10689 			pf->vsi[v]->seid = 0;
10690 	}
10691 
10692 	i40e_shutdown_adminq(&pf->hw);
10693 
10694 	/* call shutdown HMC */
10695 	if (hw->hmc.hmc_obj) {
10696 		ret = i40e_shutdown_lan_hmc(hw);
10697 		if (ret)
10698 			dev_warn(&pf->pdev->dev,
10699 				 "shutdown_lan_hmc failed: %d\n", ret);
10700 	}
10701 
10702 	/* Save the current PTP time so that we can restore the time after the
10703 	 * reset completes.
10704 	 */
10705 	i40e_ptp_save_hw_time(pf);
10706 }
10707 
10708 /**
10709  * i40e_send_version - update firmware with driver version
10710  * @pf: PF struct
10711  */
10712 static void i40e_send_version(struct i40e_pf *pf)
10713 {
10714 	struct i40e_driver_version dv;
10715 
10716 	dv.major_version = 0xff;
10717 	dv.minor_version = 0xff;
10718 	dv.build_version = 0xff;
10719 	dv.subbuild_version = 0;
10720 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10721 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10722 }
10723 
10724 /**
10725  * i40e_get_oem_version - get OEM specific version information
10726  * @hw: pointer to the hardware structure
10727  **/
10728 static void i40e_get_oem_version(struct i40e_hw *hw)
10729 {
10730 	u16 block_offset = 0xffff;
10731 	u16 block_length = 0;
10732 	u16 capabilities = 0;
10733 	u16 gen_snap = 0;
10734 	u16 release = 0;
10735 
10736 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10737 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10738 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10739 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10740 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10741 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10742 #define I40E_NVM_OEM_LENGTH			3
10743 
10744 	/* Check if pointer to OEM version block is valid. */
10745 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10746 	if (block_offset == 0xffff)
10747 		return;
10748 
10749 	/* Check if OEM version block has correct length. */
10750 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10751 			   &block_length);
10752 	if (block_length < I40E_NVM_OEM_LENGTH)
10753 		return;
10754 
10755 	/* Check if OEM version format is as expected. */
10756 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10757 			   &capabilities);
10758 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10759 		return;
10760 
10761 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10762 			   &gen_snap);
10763 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10764 			   &release);
10765 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10766 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10767 }
10768 
10769 /**
10770  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10771  * @pf: board private structure
10772  **/
10773 static int i40e_reset(struct i40e_pf *pf)
10774 {
10775 	struct i40e_hw *hw = &pf->hw;
10776 	i40e_status ret;
10777 
10778 	ret = i40e_pf_reset(hw);
10779 	if (ret) {
10780 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10781 		set_bit(__I40E_RESET_FAILED, pf->state);
10782 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10783 	} else {
10784 		pf->pfr_count++;
10785 	}
10786 	return ret;
10787 }
10788 
10789 /**
10790  * i40e_rebuild - rebuild using a saved config
10791  * @pf: board private structure
10792  * @reinit: if the Main VSI needs to re-initialized.
10793  * @lock_acquired: indicates whether or not the lock has been acquired
10794  * before this function was called.
10795  **/
10796 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10797 {
10798 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10799 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10800 	struct i40e_hw *hw = &pf->hw;
10801 	i40e_status ret;
10802 	u32 val;
10803 	int v;
10804 
10805 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10806 	    is_recovery_mode_reported)
10807 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10808 
10809 	if (test_bit(__I40E_DOWN, pf->state) &&
10810 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10811 		goto clear_recovery;
10812 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10813 
10814 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10815 	ret = i40e_init_adminq(&pf->hw);
10816 	if (ret) {
10817 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10818 			 i40e_stat_str(&pf->hw, ret),
10819 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10820 		goto clear_recovery;
10821 	}
10822 	i40e_get_oem_version(&pf->hw);
10823 
10824 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10825 		/* The following delay is necessary for firmware update. */
10826 		mdelay(1000);
10827 	}
10828 
10829 	/* re-verify the eeprom if we just had an EMP reset */
10830 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10831 		i40e_verify_eeprom(pf);
10832 
10833 	/* if we are going out of or into recovery mode we have to act
10834 	 * accordingly with regard to resources initialization
10835 	 * and deinitialization
10836 	 */
10837 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10838 		if (i40e_get_capabilities(pf,
10839 					  i40e_aqc_opc_list_func_capabilities))
10840 			goto end_unlock;
10841 
10842 		if (is_recovery_mode_reported) {
10843 			/* we're staying in recovery mode so we'll reinitialize
10844 			 * misc vector here
10845 			 */
10846 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10847 				goto end_unlock;
10848 		} else {
10849 			if (!lock_acquired)
10850 				rtnl_lock();
10851 			/* we're going out of recovery mode so we'll free
10852 			 * the IRQ allocated specifically for recovery mode
10853 			 * and restore the interrupt scheme
10854 			 */
10855 			free_irq(pf->pdev->irq, pf);
10856 			i40e_clear_interrupt_scheme(pf);
10857 			if (i40e_restore_interrupt_scheme(pf))
10858 				goto end_unlock;
10859 		}
10860 
10861 		/* tell the firmware that we're starting */
10862 		i40e_send_version(pf);
10863 
10864 		/* bail out in case recovery mode was detected, as there is
10865 		 * no need for further configuration.
10866 		 */
10867 		goto end_unlock;
10868 	}
10869 
10870 	i40e_clear_pxe_mode(hw);
10871 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10872 	if (ret)
10873 		goto end_core_reset;
10874 
10875 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10876 				hw->func_caps.num_rx_qp, 0, 0);
10877 	if (ret) {
10878 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10879 		goto end_core_reset;
10880 	}
10881 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10882 	if (ret) {
10883 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10884 		goto end_core_reset;
10885 	}
10886 
10887 #ifdef CONFIG_I40E_DCB
10888 	/* Enable FW to write a default DCB config on link-up
10889 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10890 	 * is not supported with new link speed
10891 	 */
10892 	if (i40e_is_tc_mqprio_enabled(pf)) {
10893 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10894 	} else {
10895 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10896 		    (hw->phy.link_info.link_speed &
10897 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10898 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10899 			dev_warn(&pf->pdev->dev,
10900 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10901 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10902 		} else {
10903 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10904 			ret = i40e_init_pf_dcb(pf);
10905 			if (ret) {
10906 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10907 					 ret);
10908 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10909 				/* Continue without DCB enabled */
10910 			}
10911 		}
10912 	}
10913 
10914 #endif /* CONFIG_I40E_DCB */
10915 	if (!lock_acquired)
10916 		rtnl_lock();
10917 	ret = i40e_setup_pf_switch(pf, reinit, true);
10918 	if (ret)
10919 		goto end_unlock;
10920 
10921 	/* The driver only wants link up/down and module qualification
10922 	 * reports from firmware.  Note the negative logic.
10923 	 */
10924 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10925 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10926 					 I40E_AQ_EVENT_MEDIA_NA |
10927 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10928 	if (ret)
10929 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10930 			 i40e_stat_str(&pf->hw, ret),
10931 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10932 
10933 	/* Rebuild the VSIs and VEBs that existed before reset.
10934 	 * They are still in our local switch element arrays, so only
10935 	 * need to rebuild the switch model in the HW.
10936 	 *
10937 	 * If there were VEBs but the reconstitution failed, we'll try
10938 	 * to recover minimal use by getting the basic PF VSI working.
10939 	 */
10940 	if (vsi->uplink_seid != pf->mac_seid) {
10941 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10942 		/* find the one VEB connected to the MAC, and find orphans */
10943 		for (v = 0; v < I40E_MAX_VEB; v++) {
10944 			if (!pf->veb[v])
10945 				continue;
10946 
10947 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10948 			    pf->veb[v]->uplink_seid == 0) {
10949 				ret = i40e_reconstitute_veb(pf->veb[v]);
10950 
10951 				if (!ret)
10952 					continue;
10953 
10954 				/* If Main VEB failed, we're in deep doodoo,
10955 				 * so give up rebuilding the switch and set up
10956 				 * for minimal rebuild of PF VSI.
10957 				 * If orphan failed, we'll report the error
10958 				 * but try to keep going.
10959 				 */
10960 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10961 					dev_info(&pf->pdev->dev,
10962 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10963 						 ret);
10964 					vsi->uplink_seid = pf->mac_seid;
10965 					break;
10966 				} else if (pf->veb[v]->uplink_seid == 0) {
10967 					dev_info(&pf->pdev->dev,
10968 						 "rebuild of orphan VEB failed: %d\n",
10969 						 ret);
10970 				}
10971 			}
10972 		}
10973 	}
10974 
10975 	if (vsi->uplink_seid == pf->mac_seid) {
10976 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10977 		/* no VEB, so rebuild only the Main VSI */
10978 		ret = i40e_add_vsi(vsi);
10979 		if (ret) {
10980 			dev_info(&pf->pdev->dev,
10981 				 "rebuild of Main VSI failed: %d\n", ret);
10982 			goto end_unlock;
10983 		}
10984 	}
10985 
10986 	if (vsi->mqprio_qopt.max_rate[0]) {
10987 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
10988 						  vsi->mqprio_qopt.max_rate[0]);
10989 		u64 credits = 0;
10990 
10991 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10992 		if (ret)
10993 			goto end_unlock;
10994 
10995 		credits = max_tx_rate;
10996 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10997 		dev_dbg(&vsi->back->pdev->dev,
10998 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10999 			max_tx_rate,
11000 			credits,
11001 			vsi->seid);
11002 	}
11003 
11004 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
11005 	if (ret)
11006 		goto end_unlock;
11007 
11008 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11009 	 * for this main VSI if they exist
11010 	 */
11011 	ret = i40e_rebuild_channels(vsi);
11012 	if (ret)
11013 		goto end_unlock;
11014 
11015 	/* Reconfigure hardware for allowing smaller MSS in the case
11016 	 * of TSO, so that we avoid the MDD being fired and causing
11017 	 * a reset in the case of small MSS+TSO.
11018 	 */
11019 #define I40E_REG_MSS          0x000E64DC
11020 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11021 #define I40E_64BYTE_MSS       0x400000
11022 	val = rd32(hw, I40E_REG_MSS);
11023 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11024 		val &= ~I40E_REG_MSS_MIN_MASK;
11025 		val |= I40E_64BYTE_MSS;
11026 		wr32(hw, I40E_REG_MSS, val);
11027 	}
11028 
11029 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
11030 		msleep(75);
11031 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11032 		if (ret)
11033 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11034 				 i40e_stat_str(&pf->hw, ret),
11035 				 i40e_aq_str(&pf->hw,
11036 					     pf->hw.aq.asq_last_status));
11037 	}
11038 	/* reinit the misc interrupt */
11039 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11040 		ret = i40e_setup_misc_vector(pf);
11041 
11042 	/* Add a filter to drop all Flow control frames from any VSI from being
11043 	 * transmitted. By doing so we stop a malicious VF from sending out
11044 	 * PAUSE or PFC frames and potentially controlling traffic for other
11045 	 * PF/VF VSIs.
11046 	 * The FW can still send Flow control frames if enabled.
11047 	 */
11048 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11049 						       pf->main_vsi_seid);
11050 
11051 	/* restart the VSIs that were rebuilt and running before the reset */
11052 	i40e_pf_unquiesce_all_vsi(pf);
11053 
11054 	/* Release the RTNL lock before we start resetting VFs */
11055 	if (!lock_acquired)
11056 		rtnl_unlock();
11057 
11058 	/* Restore promiscuous settings */
11059 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11060 	if (ret)
11061 		dev_warn(&pf->pdev->dev,
11062 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
11063 			 pf->cur_promisc ? "on" : "off",
11064 			 i40e_stat_str(&pf->hw, ret),
11065 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11066 
11067 	i40e_reset_all_vfs(pf, true);
11068 
11069 	/* tell the firmware that we're starting */
11070 	i40e_send_version(pf);
11071 
11072 	/* We've already released the lock, so don't do it again */
11073 	goto end_core_reset;
11074 
11075 end_unlock:
11076 	if (!lock_acquired)
11077 		rtnl_unlock();
11078 end_core_reset:
11079 	clear_bit(__I40E_RESET_FAILED, pf->state);
11080 clear_recovery:
11081 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11082 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11083 }
11084 
11085 /**
11086  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11087  * @pf: board private structure
11088  * @reinit: if the Main VSI needs to re-initialized.
11089  * @lock_acquired: indicates whether or not the lock has been acquired
11090  * before this function was called.
11091  **/
11092 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11093 				   bool lock_acquired)
11094 {
11095 	int ret;
11096 
11097 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11098 		return;
11099 	/* Now we wait for GRST to settle out.
11100 	 * We don't have to delete the VEBs or VSIs from the hw switch
11101 	 * because the reset will make them disappear.
11102 	 */
11103 	ret = i40e_reset(pf);
11104 	if (!ret)
11105 		i40e_rebuild(pf, reinit, lock_acquired);
11106 }
11107 
11108 /**
11109  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11110  * @pf: board private structure
11111  *
11112  * Close up the VFs and other things in prep for a Core Reset,
11113  * then get ready to rebuild the world.
11114  * @lock_acquired: indicates whether or not the lock has been acquired
11115  * before this function was called.
11116  **/
11117 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11118 {
11119 	i40e_prep_for_reset(pf);
11120 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11121 }
11122 
11123 /**
11124  * i40e_handle_mdd_event
11125  * @pf: pointer to the PF structure
11126  *
11127  * Called from the MDD irq handler to identify possibly malicious vfs
11128  **/
11129 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11130 {
11131 	struct i40e_hw *hw = &pf->hw;
11132 	bool mdd_detected = false;
11133 	struct i40e_vf *vf;
11134 	u32 reg;
11135 	int i;
11136 
11137 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11138 		return;
11139 
11140 	/* find what triggered the MDD event */
11141 	reg = rd32(hw, I40E_GL_MDET_TX);
11142 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11143 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
11144 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
11145 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
11146 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
11147 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
11148 				I40E_GL_MDET_TX_EVENT_SHIFT;
11149 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
11150 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
11151 				pf->hw.func_caps.base_queue;
11152 		if (netif_msg_tx_err(pf))
11153 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11154 				 event, queue, pf_num, vf_num);
11155 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11156 		mdd_detected = true;
11157 	}
11158 	reg = rd32(hw, I40E_GL_MDET_RX);
11159 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11160 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
11161 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
11162 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
11163 				I40E_GL_MDET_RX_EVENT_SHIFT;
11164 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
11165 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
11166 				pf->hw.func_caps.base_queue;
11167 		if (netif_msg_rx_err(pf))
11168 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11169 				 event, queue, func);
11170 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11171 		mdd_detected = true;
11172 	}
11173 
11174 	if (mdd_detected) {
11175 		reg = rd32(hw, I40E_PF_MDET_TX);
11176 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11177 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11178 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11179 		}
11180 		reg = rd32(hw, I40E_PF_MDET_RX);
11181 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11182 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11183 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11184 		}
11185 	}
11186 
11187 	/* see if one of the VFs needs its hand slapped */
11188 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11189 		vf = &(pf->vf[i]);
11190 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11191 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11192 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11193 			vf->num_mdd_events++;
11194 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11195 				 i);
11196 			dev_info(&pf->pdev->dev,
11197 				 "Use PF Control I/F to re-enable the VF\n");
11198 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11199 		}
11200 
11201 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11202 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11203 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11204 			vf->num_mdd_events++;
11205 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11206 				 i);
11207 			dev_info(&pf->pdev->dev,
11208 				 "Use PF Control I/F to re-enable the VF\n");
11209 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11210 		}
11211 	}
11212 
11213 	/* re-enable mdd interrupt cause */
11214 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11215 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11216 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11217 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11218 	i40e_flush(hw);
11219 }
11220 
11221 /**
11222  * i40e_service_task - Run the driver's async subtasks
11223  * @work: pointer to work_struct containing our data
11224  **/
11225 static void i40e_service_task(struct work_struct *work)
11226 {
11227 	struct i40e_pf *pf = container_of(work,
11228 					  struct i40e_pf,
11229 					  service_task);
11230 	unsigned long start_time = jiffies;
11231 
11232 	/* don't bother with service tasks if a reset is in progress */
11233 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11234 	    test_bit(__I40E_SUSPENDED, pf->state))
11235 		return;
11236 
11237 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11238 		return;
11239 
11240 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11241 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11242 		i40e_sync_filters_subtask(pf);
11243 		i40e_reset_subtask(pf);
11244 		i40e_handle_mdd_event(pf);
11245 		i40e_vc_process_vflr_event(pf);
11246 		i40e_watchdog_subtask(pf);
11247 		i40e_fdir_reinit_subtask(pf);
11248 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11249 			/* Client subtask will reopen next time through. */
11250 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11251 							   true);
11252 		} else {
11253 			i40e_client_subtask(pf);
11254 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11255 					       pf->state))
11256 				i40e_notify_client_of_l2_param_changes(
11257 								pf->vsi[pf->lan_vsi]);
11258 		}
11259 		i40e_sync_filters_subtask(pf);
11260 	} else {
11261 		i40e_reset_subtask(pf);
11262 	}
11263 
11264 	i40e_clean_adminq_subtask(pf);
11265 
11266 	/* flush memory to make sure state is correct before next watchdog */
11267 	smp_mb__before_atomic();
11268 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11269 
11270 	/* If the tasks have taken longer than one timer cycle or there
11271 	 * is more work to be done, reschedule the service task now
11272 	 * rather than wait for the timer to tick again.
11273 	 */
11274 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11275 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11276 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11277 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11278 		i40e_service_event_schedule(pf);
11279 }
11280 
11281 /**
11282  * i40e_service_timer - timer callback
11283  * @t: timer list pointer
11284  **/
11285 static void i40e_service_timer(struct timer_list *t)
11286 {
11287 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11288 
11289 	mod_timer(&pf->service_timer,
11290 		  round_jiffies(jiffies + pf->service_timer_period));
11291 	i40e_service_event_schedule(pf);
11292 }
11293 
11294 /**
11295  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11296  * @vsi: the VSI being configured
11297  **/
11298 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11299 {
11300 	struct i40e_pf *pf = vsi->back;
11301 
11302 	switch (vsi->type) {
11303 	case I40E_VSI_MAIN:
11304 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11305 		if (!vsi->num_tx_desc)
11306 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11307 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11308 		if (!vsi->num_rx_desc)
11309 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11310 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11311 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11312 			vsi->num_q_vectors = pf->num_lan_msix;
11313 		else
11314 			vsi->num_q_vectors = 1;
11315 
11316 		break;
11317 
11318 	case I40E_VSI_FDIR:
11319 		vsi->alloc_queue_pairs = 1;
11320 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11321 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11322 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11323 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11324 		vsi->num_q_vectors = pf->num_fdsb_msix;
11325 		break;
11326 
11327 	case I40E_VSI_VMDQ2:
11328 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11329 		if (!vsi->num_tx_desc)
11330 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11331 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11332 		if (!vsi->num_rx_desc)
11333 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11334 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11335 		vsi->num_q_vectors = pf->num_vmdq_msix;
11336 		break;
11337 
11338 	case I40E_VSI_SRIOV:
11339 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11340 		if (!vsi->num_tx_desc)
11341 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11342 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11343 		if (!vsi->num_rx_desc)
11344 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11345 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11346 		break;
11347 
11348 	default:
11349 		WARN_ON(1);
11350 		return -ENODATA;
11351 	}
11352 
11353 	if (is_kdump_kernel()) {
11354 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11355 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11356 	}
11357 
11358 	return 0;
11359 }
11360 
11361 /**
11362  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11363  * @vsi: VSI pointer
11364  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11365  *
11366  * On error: returns error code (negative)
11367  * On success: returns 0
11368  **/
11369 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11370 {
11371 	struct i40e_ring **next_rings;
11372 	int size;
11373 	int ret = 0;
11374 
11375 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11376 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11377 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11378 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11379 	if (!vsi->tx_rings)
11380 		return -ENOMEM;
11381 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11382 	if (i40e_enabled_xdp_vsi(vsi)) {
11383 		vsi->xdp_rings = next_rings;
11384 		next_rings += vsi->alloc_queue_pairs;
11385 	}
11386 	vsi->rx_rings = next_rings;
11387 
11388 	if (alloc_qvectors) {
11389 		/* allocate memory for q_vector pointers */
11390 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11391 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11392 		if (!vsi->q_vectors) {
11393 			ret = -ENOMEM;
11394 			goto err_vectors;
11395 		}
11396 	}
11397 	return ret;
11398 
11399 err_vectors:
11400 	kfree(vsi->tx_rings);
11401 	return ret;
11402 }
11403 
11404 /**
11405  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11406  * @pf: board private structure
11407  * @type: type of VSI
11408  *
11409  * On error: returns error code (negative)
11410  * On success: returns vsi index in PF (positive)
11411  **/
11412 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11413 {
11414 	int ret = -ENODEV;
11415 	struct i40e_vsi *vsi;
11416 	int vsi_idx;
11417 	int i;
11418 
11419 	/* Need to protect the allocation of the VSIs at the PF level */
11420 	mutex_lock(&pf->switch_mutex);
11421 
11422 	/* VSI list may be fragmented if VSI creation/destruction has
11423 	 * been happening.  We can afford to do a quick scan to look
11424 	 * for any free VSIs in the list.
11425 	 *
11426 	 * find next empty vsi slot, looping back around if necessary
11427 	 */
11428 	i = pf->next_vsi;
11429 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11430 		i++;
11431 	if (i >= pf->num_alloc_vsi) {
11432 		i = 0;
11433 		while (i < pf->next_vsi && pf->vsi[i])
11434 			i++;
11435 	}
11436 
11437 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11438 		vsi_idx = i;             /* Found one! */
11439 	} else {
11440 		ret = -ENODEV;
11441 		goto unlock_pf;  /* out of VSI slots! */
11442 	}
11443 	pf->next_vsi = ++i;
11444 
11445 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11446 	if (!vsi) {
11447 		ret = -ENOMEM;
11448 		goto unlock_pf;
11449 	}
11450 	vsi->type = type;
11451 	vsi->back = pf;
11452 	set_bit(__I40E_VSI_DOWN, vsi->state);
11453 	vsi->flags = 0;
11454 	vsi->idx = vsi_idx;
11455 	vsi->int_rate_limit = 0;
11456 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11457 				pf->rss_table_size : 64;
11458 	vsi->netdev_registered = false;
11459 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11460 	hash_init(vsi->mac_filter_hash);
11461 	vsi->irqs_ready = false;
11462 
11463 	if (type == I40E_VSI_MAIN) {
11464 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11465 		if (!vsi->af_xdp_zc_qps)
11466 			goto err_rings;
11467 	}
11468 
11469 	ret = i40e_set_num_rings_in_vsi(vsi);
11470 	if (ret)
11471 		goto err_rings;
11472 
11473 	ret = i40e_vsi_alloc_arrays(vsi, true);
11474 	if (ret)
11475 		goto err_rings;
11476 
11477 	/* Setup default MSIX irq handler for VSI */
11478 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11479 
11480 	/* Initialize VSI lock */
11481 	spin_lock_init(&vsi->mac_filter_hash_lock);
11482 	pf->vsi[vsi_idx] = vsi;
11483 	ret = vsi_idx;
11484 	goto unlock_pf;
11485 
11486 err_rings:
11487 	bitmap_free(vsi->af_xdp_zc_qps);
11488 	pf->next_vsi = i - 1;
11489 	kfree(vsi);
11490 unlock_pf:
11491 	mutex_unlock(&pf->switch_mutex);
11492 	return ret;
11493 }
11494 
11495 /**
11496  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11497  * @vsi: VSI pointer
11498  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11499  *
11500  * On error: returns error code (negative)
11501  * On success: returns 0
11502  **/
11503 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11504 {
11505 	/* free the ring and vector containers */
11506 	if (free_qvectors) {
11507 		kfree(vsi->q_vectors);
11508 		vsi->q_vectors = NULL;
11509 	}
11510 	kfree(vsi->tx_rings);
11511 	vsi->tx_rings = NULL;
11512 	vsi->rx_rings = NULL;
11513 	vsi->xdp_rings = NULL;
11514 }
11515 
11516 /**
11517  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11518  * and lookup table
11519  * @vsi: Pointer to VSI structure
11520  */
11521 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11522 {
11523 	if (!vsi)
11524 		return;
11525 
11526 	kfree(vsi->rss_hkey_user);
11527 	vsi->rss_hkey_user = NULL;
11528 
11529 	kfree(vsi->rss_lut_user);
11530 	vsi->rss_lut_user = NULL;
11531 }
11532 
11533 /**
11534  * i40e_vsi_clear - Deallocate the VSI provided
11535  * @vsi: the VSI being un-configured
11536  **/
11537 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11538 {
11539 	struct i40e_pf *pf;
11540 
11541 	if (!vsi)
11542 		return 0;
11543 
11544 	if (!vsi->back)
11545 		goto free_vsi;
11546 	pf = vsi->back;
11547 
11548 	mutex_lock(&pf->switch_mutex);
11549 	if (!pf->vsi[vsi->idx]) {
11550 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11551 			vsi->idx, vsi->idx, vsi->type);
11552 		goto unlock_vsi;
11553 	}
11554 
11555 	if (pf->vsi[vsi->idx] != vsi) {
11556 		dev_err(&pf->pdev->dev,
11557 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11558 			pf->vsi[vsi->idx]->idx,
11559 			pf->vsi[vsi->idx]->type,
11560 			vsi->idx, vsi->type);
11561 		goto unlock_vsi;
11562 	}
11563 
11564 	/* updates the PF for this cleared vsi */
11565 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11566 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11567 
11568 	bitmap_free(vsi->af_xdp_zc_qps);
11569 	i40e_vsi_free_arrays(vsi, true);
11570 	i40e_clear_rss_config_user(vsi);
11571 
11572 	pf->vsi[vsi->idx] = NULL;
11573 	if (vsi->idx < pf->next_vsi)
11574 		pf->next_vsi = vsi->idx;
11575 
11576 unlock_vsi:
11577 	mutex_unlock(&pf->switch_mutex);
11578 free_vsi:
11579 	kfree(vsi);
11580 
11581 	return 0;
11582 }
11583 
11584 /**
11585  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11586  * @vsi: the VSI being cleaned
11587  **/
11588 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11589 {
11590 	int i;
11591 
11592 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11593 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11594 			kfree_rcu(vsi->tx_rings[i], rcu);
11595 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11596 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11597 			if (vsi->xdp_rings)
11598 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11599 		}
11600 	}
11601 }
11602 
11603 /**
11604  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11605  * @vsi: the VSI being configured
11606  **/
11607 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11608 {
11609 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11610 	struct i40e_pf *pf = vsi->back;
11611 	struct i40e_ring *ring;
11612 
11613 	/* Set basic values in the rings to be used later during open() */
11614 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11615 		/* allocate space for both Tx and Rx in one shot */
11616 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11617 		if (!ring)
11618 			goto err_out;
11619 
11620 		ring->queue_index = i;
11621 		ring->reg_idx = vsi->base_queue + i;
11622 		ring->ring_active = false;
11623 		ring->vsi = vsi;
11624 		ring->netdev = vsi->netdev;
11625 		ring->dev = &pf->pdev->dev;
11626 		ring->count = vsi->num_tx_desc;
11627 		ring->size = 0;
11628 		ring->dcb_tc = 0;
11629 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11630 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11631 		ring->itr_setting = pf->tx_itr_default;
11632 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11633 
11634 		if (!i40e_enabled_xdp_vsi(vsi))
11635 			goto setup_rx;
11636 
11637 		ring->queue_index = vsi->alloc_queue_pairs + i;
11638 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11639 		ring->ring_active = false;
11640 		ring->vsi = vsi;
11641 		ring->netdev = NULL;
11642 		ring->dev = &pf->pdev->dev;
11643 		ring->count = vsi->num_tx_desc;
11644 		ring->size = 0;
11645 		ring->dcb_tc = 0;
11646 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11647 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11648 		set_ring_xdp(ring);
11649 		ring->itr_setting = pf->tx_itr_default;
11650 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11651 
11652 setup_rx:
11653 		ring->queue_index = i;
11654 		ring->reg_idx = vsi->base_queue + i;
11655 		ring->ring_active = false;
11656 		ring->vsi = vsi;
11657 		ring->netdev = vsi->netdev;
11658 		ring->dev = &pf->pdev->dev;
11659 		ring->count = vsi->num_rx_desc;
11660 		ring->size = 0;
11661 		ring->dcb_tc = 0;
11662 		ring->itr_setting = pf->rx_itr_default;
11663 		WRITE_ONCE(vsi->rx_rings[i], ring);
11664 	}
11665 
11666 	return 0;
11667 
11668 err_out:
11669 	i40e_vsi_clear_rings(vsi);
11670 	return -ENOMEM;
11671 }
11672 
11673 /**
11674  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11675  * @pf: board private structure
11676  * @vectors: the number of MSI-X vectors to request
11677  *
11678  * Returns the number of vectors reserved, or error
11679  **/
11680 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11681 {
11682 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11683 					I40E_MIN_MSIX, vectors);
11684 	if (vectors < 0) {
11685 		dev_info(&pf->pdev->dev,
11686 			 "MSI-X vector reservation failed: %d\n", vectors);
11687 		vectors = 0;
11688 	}
11689 
11690 	return vectors;
11691 }
11692 
11693 /**
11694  * i40e_init_msix - Setup the MSIX capability
11695  * @pf: board private structure
11696  *
11697  * Work with the OS to set up the MSIX vectors needed.
11698  *
11699  * Returns the number of vectors reserved or negative on failure
11700  **/
11701 static int i40e_init_msix(struct i40e_pf *pf)
11702 {
11703 	struct i40e_hw *hw = &pf->hw;
11704 	int cpus, extra_vectors;
11705 	int vectors_left;
11706 	int v_budget, i;
11707 	int v_actual;
11708 	int iwarp_requested = 0;
11709 
11710 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11711 		return -ENODEV;
11712 
11713 	/* The number of vectors we'll request will be comprised of:
11714 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11715 	 *   - The number of LAN queue pairs
11716 	 *	- Queues being used for RSS.
11717 	 *		We don't need as many as max_rss_size vectors.
11718 	 *		use rss_size instead in the calculation since that
11719 	 *		is governed by number of cpus in the system.
11720 	 *	- assumes symmetric Tx/Rx pairing
11721 	 *   - The number of VMDq pairs
11722 	 *   - The CPU count within the NUMA node if iWARP is enabled
11723 	 * Once we count this up, try the request.
11724 	 *
11725 	 * If we can't get what we want, we'll simplify to nearly nothing
11726 	 * and try again.  If that still fails, we punt.
11727 	 */
11728 	vectors_left = hw->func_caps.num_msix_vectors;
11729 	v_budget = 0;
11730 
11731 	/* reserve one vector for miscellaneous handler */
11732 	if (vectors_left) {
11733 		v_budget++;
11734 		vectors_left--;
11735 	}
11736 
11737 	/* reserve some vectors for the main PF traffic queues. Initially we
11738 	 * only reserve at most 50% of the available vectors, in the case that
11739 	 * the number of online CPUs is large. This ensures that we can enable
11740 	 * extra features as well. Once we've enabled the other features, we
11741 	 * will use any remaining vectors to reach as close as we can to the
11742 	 * number of online CPUs.
11743 	 */
11744 	cpus = num_online_cpus();
11745 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11746 	vectors_left -= pf->num_lan_msix;
11747 
11748 	/* reserve one vector for sideband flow director */
11749 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11750 		if (vectors_left) {
11751 			pf->num_fdsb_msix = 1;
11752 			v_budget++;
11753 			vectors_left--;
11754 		} else {
11755 			pf->num_fdsb_msix = 0;
11756 		}
11757 	}
11758 
11759 	/* can we reserve enough for iWARP? */
11760 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11761 		iwarp_requested = pf->num_iwarp_msix;
11762 
11763 		if (!vectors_left)
11764 			pf->num_iwarp_msix = 0;
11765 		else if (vectors_left < pf->num_iwarp_msix)
11766 			pf->num_iwarp_msix = 1;
11767 		v_budget += pf->num_iwarp_msix;
11768 		vectors_left -= pf->num_iwarp_msix;
11769 	}
11770 
11771 	/* any vectors left over go for VMDq support */
11772 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11773 		if (!vectors_left) {
11774 			pf->num_vmdq_msix = 0;
11775 			pf->num_vmdq_qps = 0;
11776 		} else {
11777 			int vmdq_vecs_wanted =
11778 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11779 			int vmdq_vecs =
11780 				min_t(int, vectors_left, vmdq_vecs_wanted);
11781 
11782 			/* if we're short on vectors for what's desired, we limit
11783 			 * the queues per vmdq.  If this is still more than are
11784 			 * available, the user will need to change the number of
11785 			 * queues/vectors used by the PF later with the ethtool
11786 			 * channels command
11787 			 */
11788 			if (vectors_left < vmdq_vecs_wanted) {
11789 				pf->num_vmdq_qps = 1;
11790 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11791 				vmdq_vecs = min_t(int,
11792 						  vectors_left,
11793 						  vmdq_vecs_wanted);
11794 			}
11795 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11796 
11797 			v_budget += vmdq_vecs;
11798 			vectors_left -= vmdq_vecs;
11799 		}
11800 	}
11801 
11802 	/* On systems with a large number of SMP cores, we previously limited
11803 	 * the number of vectors for num_lan_msix to be at most 50% of the
11804 	 * available vectors, to allow for other features. Now, we add back
11805 	 * the remaining vectors. However, we ensure that the total
11806 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11807 	 * calculate the number of vectors we can add without going over the
11808 	 * cap of CPUs. For systems with a small number of CPUs this will be
11809 	 * zero.
11810 	 */
11811 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11812 	pf->num_lan_msix += extra_vectors;
11813 	vectors_left -= extra_vectors;
11814 
11815 	WARN(vectors_left < 0,
11816 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11817 
11818 	v_budget += pf->num_lan_msix;
11819 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11820 				   GFP_KERNEL);
11821 	if (!pf->msix_entries)
11822 		return -ENOMEM;
11823 
11824 	for (i = 0; i < v_budget; i++)
11825 		pf->msix_entries[i].entry = i;
11826 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11827 
11828 	if (v_actual < I40E_MIN_MSIX) {
11829 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11830 		kfree(pf->msix_entries);
11831 		pf->msix_entries = NULL;
11832 		pci_disable_msix(pf->pdev);
11833 		return -ENODEV;
11834 
11835 	} else if (v_actual == I40E_MIN_MSIX) {
11836 		/* Adjust for minimal MSIX use */
11837 		pf->num_vmdq_vsis = 0;
11838 		pf->num_vmdq_qps = 0;
11839 		pf->num_lan_qps = 1;
11840 		pf->num_lan_msix = 1;
11841 
11842 	} else if (v_actual != v_budget) {
11843 		/* If we have limited resources, we will start with no vectors
11844 		 * for the special features and then allocate vectors to some
11845 		 * of these features based on the policy and at the end disable
11846 		 * the features that did not get any vectors.
11847 		 */
11848 		int vec;
11849 
11850 		dev_info(&pf->pdev->dev,
11851 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11852 			 v_actual, v_budget);
11853 		/* reserve the misc vector */
11854 		vec = v_actual - 1;
11855 
11856 		/* Scale vector usage down */
11857 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11858 		pf->num_vmdq_vsis = 1;
11859 		pf->num_vmdq_qps = 1;
11860 
11861 		/* partition out the remaining vectors */
11862 		switch (vec) {
11863 		case 2:
11864 			pf->num_lan_msix = 1;
11865 			break;
11866 		case 3:
11867 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11868 				pf->num_lan_msix = 1;
11869 				pf->num_iwarp_msix = 1;
11870 			} else {
11871 				pf->num_lan_msix = 2;
11872 			}
11873 			break;
11874 		default:
11875 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11876 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11877 						 iwarp_requested);
11878 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11879 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11880 			} else {
11881 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11882 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11883 			}
11884 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11885 				pf->num_fdsb_msix = 1;
11886 				vec--;
11887 			}
11888 			pf->num_lan_msix = min_t(int,
11889 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11890 							      pf->num_lan_msix);
11891 			pf->num_lan_qps = pf->num_lan_msix;
11892 			break;
11893 		}
11894 	}
11895 
11896 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11897 	    (pf->num_fdsb_msix == 0)) {
11898 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11899 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11900 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11901 	}
11902 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11903 	    (pf->num_vmdq_msix == 0)) {
11904 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11905 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11906 	}
11907 
11908 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11909 	    (pf->num_iwarp_msix == 0)) {
11910 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11911 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11912 	}
11913 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11914 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11915 		   pf->num_lan_msix,
11916 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11917 		   pf->num_fdsb_msix,
11918 		   pf->num_iwarp_msix);
11919 
11920 	return v_actual;
11921 }
11922 
11923 /**
11924  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11925  * @vsi: the VSI being configured
11926  * @v_idx: index of the vector in the vsi struct
11927  *
11928  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11929  **/
11930 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11931 {
11932 	struct i40e_q_vector *q_vector;
11933 
11934 	/* allocate q_vector */
11935 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11936 	if (!q_vector)
11937 		return -ENOMEM;
11938 
11939 	q_vector->vsi = vsi;
11940 	q_vector->v_idx = v_idx;
11941 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11942 
11943 	if (vsi->netdev)
11944 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
11945 
11946 	/* tie q_vector and vsi together */
11947 	vsi->q_vectors[v_idx] = q_vector;
11948 
11949 	return 0;
11950 }
11951 
11952 /**
11953  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11954  * @vsi: the VSI being configured
11955  *
11956  * We allocate one q_vector per queue interrupt.  If allocation fails we
11957  * return -ENOMEM.
11958  **/
11959 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11960 {
11961 	struct i40e_pf *pf = vsi->back;
11962 	int err, v_idx, num_q_vectors;
11963 
11964 	/* if not MSIX, give the one vector only to the LAN VSI */
11965 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11966 		num_q_vectors = vsi->num_q_vectors;
11967 	else if (vsi == pf->vsi[pf->lan_vsi])
11968 		num_q_vectors = 1;
11969 	else
11970 		return -EINVAL;
11971 
11972 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11973 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11974 		if (err)
11975 			goto err_out;
11976 	}
11977 
11978 	return 0;
11979 
11980 err_out:
11981 	while (v_idx--)
11982 		i40e_free_q_vector(vsi, v_idx);
11983 
11984 	return err;
11985 }
11986 
11987 /**
11988  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11989  * @pf: board private structure to initialize
11990  **/
11991 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11992 {
11993 	int vectors = 0;
11994 	ssize_t size;
11995 
11996 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11997 		vectors = i40e_init_msix(pf);
11998 		if (vectors < 0) {
11999 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
12000 				       I40E_FLAG_IWARP_ENABLED	|
12001 				       I40E_FLAG_RSS_ENABLED	|
12002 				       I40E_FLAG_DCB_CAPABLE	|
12003 				       I40E_FLAG_DCB_ENABLED	|
12004 				       I40E_FLAG_SRIOV_ENABLED	|
12005 				       I40E_FLAG_FD_SB_ENABLED	|
12006 				       I40E_FLAG_FD_ATR_ENABLED	|
12007 				       I40E_FLAG_VMDQ_ENABLED);
12008 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12009 
12010 			/* rework the queue expectations without MSIX */
12011 			i40e_determine_queue_usage(pf);
12012 		}
12013 	}
12014 
12015 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
12016 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
12017 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12018 		vectors = pci_enable_msi(pf->pdev);
12019 		if (vectors < 0) {
12020 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12021 				 vectors);
12022 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
12023 		}
12024 		vectors = 1;  /* one MSI or Legacy vector */
12025 	}
12026 
12027 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
12028 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12029 
12030 	/* set up vector assignment tracking */
12031 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12032 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12033 	if (!pf->irq_pile)
12034 		return -ENOMEM;
12035 
12036 	pf->irq_pile->num_entries = vectors;
12037 
12038 	/* track first vector for misc interrupts, ignore return */
12039 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12040 
12041 	return 0;
12042 }
12043 
12044 /**
12045  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12046  * @pf: private board data structure
12047  *
12048  * Restore the interrupt scheme that was cleared when we suspended the
12049  * device. This should be called during resume to re-allocate the q_vectors
12050  * and reacquire IRQs.
12051  */
12052 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12053 {
12054 	int err, i;
12055 
12056 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12057 	 * scheme. We need to re-enabled them here in order to attempt to
12058 	 * re-acquire the MSI or MSI-X vectors
12059 	 */
12060 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
12061 
12062 	err = i40e_init_interrupt_scheme(pf);
12063 	if (err)
12064 		return err;
12065 
12066 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12067 	 * rings together again.
12068 	 */
12069 	for (i = 0; i < pf->num_alloc_vsi; i++) {
12070 		if (pf->vsi[i]) {
12071 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
12072 			if (err)
12073 				goto err_unwind;
12074 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
12075 		}
12076 	}
12077 
12078 	err = i40e_setup_misc_vector(pf);
12079 	if (err)
12080 		goto err_unwind;
12081 
12082 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
12083 		i40e_client_update_msix_info(pf);
12084 
12085 	return 0;
12086 
12087 err_unwind:
12088 	while (i--) {
12089 		if (pf->vsi[i])
12090 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12091 	}
12092 
12093 	return err;
12094 }
12095 
12096 /**
12097  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12098  * non queue events in recovery mode
12099  * @pf: board private structure
12100  *
12101  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12102  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12103  * This is handled differently than in recovery mode since no Tx/Rx resources
12104  * are being allocated.
12105  **/
12106 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12107 {
12108 	int err;
12109 
12110 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12111 		err = i40e_setup_misc_vector(pf);
12112 
12113 		if (err) {
12114 			dev_info(&pf->pdev->dev,
12115 				 "MSI-X misc vector request failed, error %d\n",
12116 				 err);
12117 			return err;
12118 		}
12119 	} else {
12120 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
12121 
12122 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12123 				  pf->int_name, pf);
12124 
12125 		if (err) {
12126 			dev_info(&pf->pdev->dev,
12127 				 "MSI/legacy misc vector request failed, error %d\n",
12128 				 err);
12129 			return err;
12130 		}
12131 		i40e_enable_misc_int_causes(pf);
12132 		i40e_irq_dynamic_enable_icr0(pf);
12133 	}
12134 
12135 	return 0;
12136 }
12137 
12138 /**
12139  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12140  * @pf: board private structure
12141  *
12142  * This sets up the handler for MSIX 0, which is used to manage the
12143  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12144  * when in MSI or Legacy interrupt mode.
12145  **/
12146 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12147 {
12148 	struct i40e_hw *hw = &pf->hw;
12149 	int err = 0;
12150 
12151 	/* Only request the IRQ once, the first time through. */
12152 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12153 		err = request_irq(pf->msix_entries[0].vector,
12154 				  i40e_intr, 0, pf->int_name, pf);
12155 		if (err) {
12156 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12157 			dev_info(&pf->pdev->dev,
12158 				 "request_irq for %s failed: %d\n",
12159 				 pf->int_name, err);
12160 			return -EFAULT;
12161 		}
12162 	}
12163 
12164 	i40e_enable_misc_int_causes(pf);
12165 
12166 	/* associate no queues to the misc vector */
12167 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12168 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12169 
12170 	i40e_flush(hw);
12171 
12172 	i40e_irq_dynamic_enable_icr0(pf);
12173 
12174 	return err;
12175 }
12176 
12177 /**
12178  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12179  * @vsi: Pointer to vsi structure
12180  * @seed: Buffter to store the hash keys
12181  * @lut: Buffer to store the lookup table entries
12182  * @lut_size: Size of buffer to store the lookup table entries
12183  *
12184  * Return 0 on success, negative on failure
12185  */
12186 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12187 			   u8 *lut, u16 lut_size)
12188 {
12189 	struct i40e_pf *pf = vsi->back;
12190 	struct i40e_hw *hw = &pf->hw;
12191 	int ret = 0;
12192 
12193 	if (seed) {
12194 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12195 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12196 		if (ret) {
12197 			dev_info(&pf->pdev->dev,
12198 				 "Cannot get RSS key, err %s aq_err %s\n",
12199 				 i40e_stat_str(&pf->hw, ret),
12200 				 i40e_aq_str(&pf->hw,
12201 					     pf->hw.aq.asq_last_status));
12202 			return ret;
12203 		}
12204 	}
12205 
12206 	if (lut) {
12207 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12208 
12209 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12210 		if (ret) {
12211 			dev_info(&pf->pdev->dev,
12212 				 "Cannot get RSS lut, err %s aq_err %s\n",
12213 				 i40e_stat_str(&pf->hw, ret),
12214 				 i40e_aq_str(&pf->hw,
12215 					     pf->hw.aq.asq_last_status));
12216 			return ret;
12217 		}
12218 	}
12219 
12220 	return ret;
12221 }
12222 
12223 /**
12224  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12225  * @vsi: Pointer to vsi structure
12226  * @seed: RSS hash seed
12227  * @lut: Lookup table
12228  * @lut_size: Lookup table size
12229  *
12230  * Returns 0 on success, negative on failure
12231  **/
12232 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12233 			       const u8 *lut, u16 lut_size)
12234 {
12235 	struct i40e_pf *pf = vsi->back;
12236 	struct i40e_hw *hw = &pf->hw;
12237 	u16 vf_id = vsi->vf_id;
12238 	u8 i;
12239 
12240 	/* Fill out hash function seed */
12241 	if (seed) {
12242 		u32 *seed_dw = (u32 *)seed;
12243 
12244 		if (vsi->type == I40E_VSI_MAIN) {
12245 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12246 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12247 		} else if (vsi->type == I40E_VSI_SRIOV) {
12248 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12249 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12250 		} else {
12251 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12252 		}
12253 	}
12254 
12255 	if (lut) {
12256 		u32 *lut_dw = (u32 *)lut;
12257 
12258 		if (vsi->type == I40E_VSI_MAIN) {
12259 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12260 				return -EINVAL;
12261 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12262 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12263 		} else if (vsi->type == I40E_VSI_SRIOV) {
12264 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12265 				return -EINVAL;
12266 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12267 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12268 		} else {
12269 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12270 		}
12271 	}
12272 	i40e_flush(hw);
12273 
12274 	return 0;
12275 }
12276 
12277 /**
12278  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12279  * @vsi: Pointer to VSI structure
12280  * @seed: Buffer to store the keys
12281  * @lut: Buffer to store the lookup table entries
12282  * @lut_size: Size of buffer to store the lookup table entries
12283  *
12284  * Returns 0 on success, negative on failure
12285  */
12286 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12287 			    u8 *lut, u16 lut_size)
12288 {
12289 	struct i40e_pf *pf = vsi->back;
12290 	struct i40e_hw *hw = &pf->hw;
12291 	u16 i;
12292 
12293 	if (seed) {
12294 		u32 *seed_dw = (u32 *)seed;
12295 
12296 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12297 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12298 	}
12299 	if (lut) {
12300 		u32 *lut_dw = (u32 *)lut;
12301 
12302 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12303 			return -EINVAL;
12304 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12305 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12306 	}
12307 
12308 	return 0;
12309 }
12310 
12311 /**
12312  * i40e_config_rss - Configure RSS keys and lut
12313  * @vsi: Pointer to VSI structure
12314  * @seed: RSS hash seed
12315  * @lut: Lookup table
12316  * @lut_size: Lookup table size
12317  *
12318  * Returns 0 on success, negative on failure
12319  */
12320 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12321 {
12322 	struct i40e_pf *pf = vsi->back;
12323 
12324 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12325 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12326 	else
12327 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12328 }
12329 
12330 /**
12331  * i40e_get_rss - Get RSS keys and lut
12332  * @vsi: Pointer to VSI structure
12333  * @seed: Buffer to store the keys
12334  * @lut: Buffer to store the lookup table entries
12335  * @lut_size: Size of buffer to store the lookup table entries
12336  *
12337  * Returns 0 on success, negative on failure
12338  */
12339 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12340 {
12341 	struct i40e_pf *pf = vsi->back;
12342 
12343 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12344 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12345 	else
12346 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12347 }
12348 
12349 /**
12350  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12351  * @pf: Pointer to board private structure
12352  * @lut: Lookup table
12353  * @rss_table_size: Lookup table size
12354  * @rss_size: Range of queue number for hashing
12355  */
12356 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12357 		       u16 rss_table_size, u16 rss_size)
12358 {
12359 	u16 i;
12360 
12361 	for (i = 0; i < rss_table_size; i++)
12362 		lut[i] = i % rss_size;
12363 }
12364 
12365 /**
12366  * i40e_pf_config_rss - Prepare for RSS if used
12367  * @pf: board private structure
12368  **/
12369 static int i40e_pf_config_rss(struct i40e_pf *pf)
12370 {
12371 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12372 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12373 	u8 *lut;
12374 	struct i40e_hw *hw = &pf->hw;
12375 	u32 reg_val;
12376 	u64 hena;
12377 	int ret;
12378 
12379 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12380 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12381 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12382 	hena |= i40e_pf_get_default_rss_hena(pf);
12383 
12384 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12385 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12386 
12387 	/* Determine the RSS table size based on the hardware capabilities */
12388 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12389 	reg_val = (pf->rss_table_size == 512) ?
12390 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12391 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12392 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12393 
12394 	/* Determine the RSS size of the VSI */
12395 	if (!vsi->rss_size) {
12396 		u16 qcount;
12397 		/* If the firmware does something weird during VSI init, we
12398 		 * could end up with zero TCs. Check for that to avoid
12399 		 * divide-by-zero. It probably won't pass traffic, but it also
12400 		 * won't panic.
12401 		 */
12402 		qcount = vsi->num_queue_pairs /
12403 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12404 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12405 	}
12406 	if (!vsi->rss_size)
12407 		return -EINVAL;
12408 
12409 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12410 	if (!lut)
12411 		return -ENOMEM;
12412 
12413 	/* Use user configured lut if there is one, otherwise use default */
12414 	if (vsi->rss_lut_user)
12415 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12416 	else
12417 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12418 
12419 	/* Use user configured hash key if there is one, otherwise
12420 	 * use default.
12421 	 */
12422 	if (vsi->rss_hkey_user)
12423 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12424 	else
12425 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12426 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12427 	kfree(lut);
12428 
12429 	return ret;
12430 }
12431 
12432 /**
12433  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12434  * @pf: board private structure
12435  * @queue_count: the requested queue count for rss.
12436  *
12437  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12438  * count which may be different from the requested queue count.
12439  * Note: expects to be called while under rtnl_lock()
12440  **/
12441 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12442 {
12443 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12444 	int new_rss_size;
12445 
12446 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12447 		return 0;
12448 
12449 	queue_count = min_t(int, queue_count, num_online_cpus());
12450 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12451 
12452 	if (queue_count != vsi->num_queue_pairs) {
12453 		u16 qcount;
12454 
12455 		vsi->req_queue_pairs = queue_count;
12456 		i40e_prep_for_reset(pf);
12457 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12458 			return pf->alloc_rss_size;
12459 
12460 		pf->alloc_rss_size = new_rss_size;
12461 
12462 		i40e_reset_and_rebuild(pf, true, true);
12463 
12464 		/* Discard the user configured hash keys and lut, if less
12465 		 * queues are enabled.
12466 		 */
12467 		if (queue_count < vsi->rss_size) {
12468 			i40e_clear_rss_config_user(vsi);
12469 			dev_dbg(&pf->pdev->dev,
12470 				"discard user configured hash keys and lut\n");
12471 		}
12472 
12473 		/* Reset vsi->rss_size, as number of enabled queues changed */
12474 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12475 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12476 
12477 		i40e_pf_config_rss(pf);
12478 	}
12479 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12480 		 vsi->req_queue_pairs, pf->rss_size_max);
12481 	return pf->alloc_rss_size;
12482 }
12483 
12484 /**
12485  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12486  * @pf: board private structure
12487  **/
12488 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
12489 {
12490 	i40e_status status;
12491 	bool min_valid, max_valid;
12492 	u32 max_bw, min_bw;
12493 
12494 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12495 					   &min_valid, &max_valid);
12496 
12497 	if (!status) {
12498 		if (min_valid)
12499 			pf->min_bw = min_bw;
12500 		if (max_valid)
12501 			pf->max_bw = max_bw;
12502 	}
12503 
12504 	return status;
12505 }
12506 
12507 /**
12508  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12509  * @pf: board private structure
12510  **/
12511 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
12512 {
12513 	struct i40e_aqc_configure_partition_bw_data bw_data;
12514 	i40e_status status;
12515 
12516 	memset(&bw_data, 0, sizeof(bw_data));
12517 
12518 	/* Set the valid bit for this PF */
12519 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12520 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12521 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12522 
12523 	/* Set the new bandwidths */
12524 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12525 
12526 	return status;
12527 }
12528 
12529 /**
12530  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12531  * @pf: board private structure
12532  **/
12533 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12534 {
12535 	/* Commit temporary BW setting to permanent NVM image */
12536 	enum i40e_admin_queue_err last_aq_status;
12537 	i40e_status ret;
12538 	u16 nvm_word;
12539 
12540 	if (pf->hw.partition_id != 1) {
12541 		dev_info(&pf->pdev->dev,
12542 			 "Commit BW only works on partition 1! This is partition %d",
12543 			 pf->hw.partition_id);
12544 		ret = I40E_NOT_SUPPORTED;
12545 		goto bw_commit_out;
12546 	}
12547 
12548 	/* Acquire NVM for read access */
12549 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12550 	last_aq_status = pf->hw.aq.asq_last_status;
12551 	if (ret) {
12552 		dev_info(&pf->pdev->dev,
12553 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
12554 			 i40e_stat_str(&pf->hw, ret),
12555 			 i40e_aq_str(&pf->hw, last_aq_status));
12556 		goto bw_commit_out;
12557 	}
12558 
12559 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12560 	ret = i40e_aq_read_nvm(&pf->hw,
12561 			       I40E_SR_NVM_CONTROL_WORD,
12562 			       0x10, sizeof(nvm_word), &nvm_word,
12563 			       false, NULL);
12564 	/* Save off last admin queue command status before releasing
12565 	 * the NVM
12566 	 */
12567 	last_aq_status = pf->hw.aq.asq_last_status;
12568 	i40e_release_nvm(&pf->hw);
12569 	if (ret) {
12570 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
12571 			 i40e_stat_str(&pf->hw, ret),
12572 			 i40e_aq_str(&pf->hw, last_aq_status));
12573 		goto bw_commit_out;
12574 	}
12575 
12576 	/* Wait a bit for NVM release to complete */
12577 	msleep(50);
12578 
12579 	/* Acquire NVM for write access */
12580 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12581 	last_aq_status = pf->hw.aq.asq_last_status;
12582 	if (ret) {
12583 		dev_info(&pf->pdev->dev,
12584 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
12585 			 i40e_stat_str(&pf->hw, ret),
12586 			 i40e_aq_str(&pf->hw, last_aq_status));
12587 		goto bw_commit_out;
12588 	}
12589 	/* Write it back out unchanged to initiate update NVM,
12590 	 * which will force a write of the shadow (alt) RAM to
12591 	 * the NVM - thus storing the bandwidth values permanently.
12592 	 */
12593 	ret = i40e_aq_update_nvm(&pf->hw,
12594 				 I40E_SR_NVM_CONTROL_WORD,
12595 				 0x10, sizeof(nvm_word),
12596 				 &nvm_word, true, 0, NULL);
12597 	/* Save off last admin queue command status before releasing
12598 	 * the NVM
12599 	 */
12600 	last_aq_status = pf->hw.aq.asq_last_status;
12601 	i40e_release_nvm(&pf->hw);
12602 	if (ret)
12603 		dev_info(&pf->pdev->dev,
12604 			 "BW settings NOT SAVED, err %s aq_err %s\n",
12605 			 i40e_stat_str(&pf->hw, ret),
12606 			 i40e_aq_str(&pf->hw, last_aq_status));
12607 bw_commit_out:
12608 
12609 	return ret;
12610 }
12611 
12612 /**
12613  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12614  * if total port shutdown feature is enabled for this PF
12615  * @pf: board private structure
12616  **/
12617 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12618 {
12619 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12620 #define I40E_FEATURES_ENABLE_PTR		0x2A
12621 #define I40E_CURRENT_SETTING_PTR		0x2B
12622 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12623 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12624 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12625 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12626 	i40e_status read_status = I40E_SUCCESS;
12627 	u16 sr_emp_sr_settings_ptr = 0;
12628 	u16 features_enable = 0;
12629 	u16 link_behavior = 0;
12630 	bool ret = false;
12631 
12632 	read_status = i40e_read_nvm_word(&pf->hw,
12633 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12634 					 &sr_emp_sr_settings_ptr);
12635 	if (read_status)
12636 		goto err_nvm;
12637 	read_status = i40e_read_nvm_word(&pf->hw,
12638 					 sr_emp_sr_settings_ptr +
12639 					 I40E_FEATURES_ENABLE_PTR,
12640 					 &features_enable);
12641 	if (read_status)
12642 		goto err_nvm;
12643 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12644 		read_status = i40e_read_nvm_module_data(&pf->hw,
12645 							I40E_SR_EMP_SR_SETTINGS_PTR,
12646 							I40E_CURRENT_SETTING_PTR,
12647 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12648 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12649 							&link_behavior);
12650 		if (read_status)
12651 			goto err_nvm;
12652 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12653 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12654 	}
12655 	return ret;
12656 
12657 err_nvm:
12658 	dev_warn(&pf->pdev->dev,
12659 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
12660 		 i40e_stat_str(&pf->hw, read_status));
12661 	return ret;
12662 }
12663 
12664 /**
12665  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12666  * @pf: board private structure to initialize
12667  *
12668  * i40e_sw_init initializes the Adapter private data structure.
12669  * Fields are initialized based on PCI device information and
12670  * OS network device settings (MTU size).
12671  **/
12672 static int i40e_sw_init(struct i40e_pf *pf)
12673 {
12674 	int err = 0;
12675 	int size;
12676 	u16 pow;
12677 
12678 	/* Set default capability flags */
12679 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12680 		    I40E_FLAG_MSI_ENABLED     |
12681 		    I40E_FLAG_MSIX_ENABLED;
12682 
12683 	/* Set default ITR */
12684 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12685 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12686 
12687 	/* Depending on PF configurations, it is possible that the RSS
12688 	 * maximum might end up larger than the available queues
12689 	 */
12690 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12691 	pf->alloc_rss_size = 1;
12692 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12693 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12694 				 pf->hw.func_caps.num_tx_qp);
12695 
12696 	/* find the next higher power-of-2 of num cpus */
12697 	pow = roundup_pow_of_two(num_online_cpus());
12698 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12699 
12700 	if (pf->hw.func_caps.rss) {
12701 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12702 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12703 					   num_online_cpus());
12704 	}
12705 
12706 	/* MFP mode enabled */
12707 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12708 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12709 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12710 		if (i40e_get_partition_bw_setting(pf)) {
12711 			dev_warn(&pf->pdev->dev,
12712 				 "Could not get partition bw settings\n");
12713 		} else {
12714 			dev_info(&pf->pdev->dev,
12715 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12716 				 pf->min_bw, pf->max_bw);
12717 
12718 			/* nudge the Tx scheduler */
12719 			i40e_set_partition_bw_setting(pf);
12720 		}
12721 	}
12722 
12723 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12724 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12725 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12726 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12727 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12728 		    pf->hw.num_partitions > 1)
12729 			dev_info(&pf->pdev->dev,
12730 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12731 		else
12732 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12733 		pf->fdir_pf_filter_count =
12734 				 pf->hw.func_caps.fd_filters_guaranteed;
12735 		pf->hw.fdir_shared_filter_count =
12736 				 pf->hw.func_caps.fd_filters_best_effort;
12737 	}
12738 
12739 	if (pf->hw.mac.type == I40E_MAC_X722) {
12740 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12741 				    I40E_HW_128_QP_RSS_CAPABLE |
12742 				    I40E_HW_ATR_EVICT_CAPABLE |
12743 				    I40E_HW_WB_ON_ITR_CAPABLE |
12744 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12745 				    I40E_HW_NO_PCI_LINK_CHECK |
12746 				    I40E_HW_USE_SET_LLDP_MIB |
12747 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12748 				    I40E_HW_PTP_L4_CAPABLE |
12749 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12750 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12751 
12752 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12753 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12754 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12755 			dev_warn(&pf->pdev->dev,
12756 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12757 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12758 		}
12759 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12760 		   ((pf->hw.aq.api_maj_ver == 1) &&
12761 		    (pf->hw.aq.api_min_ver > 4))) {
12762 		/* Supported in FW API version higher than 1.4 */
12763 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12764 	}
12765 
12766 	/* Enable HW ATR eviction if possible */
12767 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12768 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12769 
12770 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12771 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12772 	    (pf->hw.aq.fw_maj_ver < 4))) {
12773 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12774 		/* No DCB support  for FW < v4.33 */
12775 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12776 	}
12777 
12778 	/* Disable FW LLDP if FW < v4.3 */
12779 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12780 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12781 	    (pf->hw.aq.fw_maj_ver < 4)))
12782 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12783 
12784 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12785 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12786 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12787 	    (pf->hw.aq.fw_maj_ver >= 5)))
12788 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12789 
12790 	/* Enable PTP L4 if FW > v6.0 */
12791 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12792 	    pf->hw.aq.fw_maj_ver >= 6)
12793 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12794 
12795 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12796 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12797 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12798 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12799 	}
12800 
12801 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12802 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12803 		/* IWARP needs one extra vector for CQP just like MISC.*/
12804 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12805 	}
12806 	/* Stopping FW LLDP engine is supported on XL710 and X722
12807 	 * starting from FW versions determined in i40e_init_adminq.
12808 	 * Stopping the FW LLDP engine is not supported on XL710
12809 	 * if NPAR is functioning so unset this hw flag in this case.
12810 	 */
12811 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12812 	    pf->hw.func_caps.npar_enable &&
12813 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12814 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12815 
12816 #ifdef CONFIG_PCI_IOV
12817 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12818 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12819 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12820 		pf->num_req_vfs = min_t(int,
12821 					pf->hw.func_caps.num_vfs,
12822 					I40E_MAX_VF_COUNT);
12823 	}
12824 #endif /* CONFIG_PCI_IOV */
12825 	pf->eeprom_version = 0xDEAD;
12826 	pf->lan_veb = I40E_NO_VEB;
12827 	pf->lan_vsi = I40E_NO_VSI;
12828 
12829 	/* By default FW has this off for performance reasons */
12830 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12831 
12832 	/* set up queue assignment tracking */
12833 	size = sizeof(struct i40e_lump_tracking)
12834 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12835 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12836 	if (!pf->qp_pile) {
12837 		err = -ENOMEM;
12838 		goto sw_init_done;
12839 	}
12840 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12841 
12842 	pf->tx_timeout_recovery_level = 1;
12843 
12844 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12845 	    i40e_is_total_port_shutdown_enabled(pf)) {
12846 		/* Link down on close must be on when total port shutdown
12847 		 * is enabled for a given port
12848 		 */
12849 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12850 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12851 		dev_info(&pf->pdev->dev,
12852 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12853 	}
12854 	mutex_init(&pf->switch_mutex);
12855 
12856 sw_init_done:
12857 	return err;
12858 }
12859 
12860 /**
12861  * i40e_set_ntuple - set the ntuple feature flag and take action
12862  * @pf: board private structure to initialize
12863  * @features: the feature set that the stack is suggesting
12864  *
12865  * returns a bool to indicate if reset needs to happen
12866  **/
12867 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12868 {
12869 	bool need_reset = false;
12870 
12871 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12872 	 * the state changed, we need to reset.
12873 	 */
12874 	if (features & NETIF_F_NTUPLE) {
12875 		/* Enable filters and mark for reset */
12876 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12877 			need_reset = true;
12878 		/* enable FD_SB only if there is MSI-X vector and no cloud
12879 		 * filters exist
12880 		 */
12881 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12882 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12883 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12884 		}
12885 	} else {
12886 		/* turn off filters, mark for reset and clear SW filter list */
12887 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12888 			need_reset = true;
12889 			i40e_fdir_filter_exit(pf);
12890 		}
12891 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12892 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12893 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12894 
12895 		/* reset fd counters */
12896 		pf->fd_add_err = 0;
12897 		pf->fd_atr_cnt = 0;
12898 		/* if ATR was auto disabled it can be re-enabled. */
12899 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12900 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12901 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12902 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12903 	}
12904 	return need_reset;
12905 }
12906 
12907 /**
12908  * i40e_clear_rss_lut - clear the rx hash lookup table
12909  * @vsi: the VSI being configured
12910  **/
12911 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12912 {
12913 	struct i40e_pf *pf = vsi->back;
12914 	struct i40e_hw *hw = &pf->hw;
12915 	u16 vf_id = vsi->vf_id;
12916 	u8 i;
12917 
12918 	if (vsi->type == I40E_VSI_MAIN) {
12919 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12920 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12921 	} else if (vsi->type == I40E_VSI_SRIOV) {
12922 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12923 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12924 	} else {
12925 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12926 	}
12927 }
12928 
12929 /**
12930  * i40e_set_features - set the netdev feature flags
12931  * @netdev: ptr to the netdev being adjusted
12932  * @features: the feature set that the stack is suggesting
12933  * Note: expects to be called while under rtnl_lock()
12934  **/
12935 static int i40e_set_features(struct net_device *netdev,
12936 			     netdev_features_t features)
12937 {
12938 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12939 	struct i40e_vsi *vsi = np->vsi;
12940 	struct i40e_pf *pf = vsi->back;
12941 	bool need_reset;
12942 
12943 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12944 		i40e_pf_config_rss(pf);
12945 	else if (!(features & NETIF_F_RXHASH) &&
12946 		 netdev->features & NETIF_F_RXHASH)
12947 		i40e_clear_rss_lut(vsi);
12948 
12949 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12950 		i40e_vlan_stripping_enable(vsi);
12951 	else
12952 		i40e_vlan_stripping_disable(vsi);
12953 
12954 	if (!(features & NETIF_F_HW_TC) &&
12955 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12956 		dev_err(&pf->pdev->dev,
12957 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12958 		return -EINVAL;
12959 	}
12960 
12961 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12962 		i40e_del_all_macvlans(vsi);
12963 
12964 	need_reset = i40e_set_ntuple(pf, features);
12965 
12966 	if (need_reset)
12967 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12968 
12969 	return 0;
12970 }
12971 
12972 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12973 				    unsigned int table, unsigned int idx,
12974 				    struct udp_tunnel_info *ti)
12975 {
12976 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12977 	struct i40e_hw *hw = &np->vsi->back->hw;
12978 	u8 type, filter_index;
12979 	i40e_status ret;
12980 
12981 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12982 						   I40E_AQC_TUNNEL_TYPE_NGE;
12983 
12984 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12985 				     NULL);
12986 	if (ret) {
12987 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12988 			    i40e_stat_str(hw, ret),
12989 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12990 		return -EIO;
12991 	}
12992 
12993 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12994 	return 0;
12995 }
12996 
12997 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12998 				      unsigned int table, unsigned int idx,
12999 				      struct udp_tunnel_info *ti)
13000 {
13001 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13002 	struct i40e_hw *hw = &np->vsi->back->hw;
13003 	i40e_status ret;
13004 
13005 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
13006 	if (ret) {
13007 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
13008 			    i40e_stat_str(hw, ret),
13009 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13010 		return -EIO;
13011 	}
13012 
13013 	return 0;
13014 }
13015 
13016 static int i40e_get_phys_port_id(struct net_device *netdev,
13017 				 struct netdev_phys_item_id *ppid)
13018 {
13019 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13020 	struct i40e_pf *pf = np->vsi->back;
13021 	struct i40e_hw *hw = &pf->hw;
13022 
13023 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
13024 		return -EOPNOTSUPP;
13025 
13026 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13027 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13028 
13029 	return 0;
13030 }
13031 
13032 /**
13033  * i40e_ndo_fdb_add - add an entry to the hardware database
13034  * @ndm: the input from the stack
13035  * @tb: pointer to array of nladdr (unused)
13036  * @dev: the net device pointer
13037  * @addr: the MAC address entry being added
13038  * @vid: VLAN ID
13039  * @flags: instructions from stack about fdb operation
13040  * @extack: netlink extended ack, unused currently
13041  */
13042 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13043 			    struct net_device *dev,
13044 			    const unsigned char *addr, u16 vid,
13045 			    u16 flags,
13046 			    struct netlink_ext_ack *extack)
13047 {
13048 	struct i40e_netdev_priv *np = netdev_priv(dev);
13049 	struct i40e_pf *pf = np->vsi->back;
13050 	int err = 0;
13051 
13052 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
13053 		return -EOPNOTSUPP;
13054 
13055 	if (vid) {
13056 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13057 		return -EINVAL;
13058 	}
13059 
13060 	/* Hardware does not support aging addresses so if a
13061 	 * ndm_state is given only allow permanent addresses
13062 	 */
13063 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13064 		netdev_info(dev, "FDB only supports static addresses\n");
13065 		return -EINVAL;
13066 	}
13067 
13068 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13069 		err = dev_uc_add_excl(dev, addr);
13070 	else if (is_multicast_ether_addr(addr))
13071 		err = dev_mc_add_excl(dev, addr);
13072 	else
13073 		err = -EINVAL;
13074 
13075 	/* Only return duplicate errors if NLM_F_EXCL is set */
13076 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13077 		err = 0;
13078 
13079 	return err;
13080 }
13081 
13082 /**
13083  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13084  * @dev: the netdev being configured
13085  * @nlh: RTNL message
13086  * @flags: bridge flags
13087  * @extack: netlink extended ack
13088  *
13089  * Inserts a new hardware bridge if not already created and
13090  * enables the bridging mode requested (VEB or VEPA). If the
13091  * hardware bridge has already been inserted and the request
13092  * is to change the mode then that requires a PF reset to
13093  * allow rebuild of the components with required hardware
13094  * bridge mode enabled.
13095  *
13096  * Note: expects to be called while under rtnl_lock()
13097  **/
13098 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13099 				   struct nlmsghdr *nlh,
13100 				   u16 flags,
13101 				   struct netlink_ext_ack *extack)
13102 {
13103 	struct i40e_netdev_priv *np = netdev_priv(dev);
13104 	struct i40e_vsi *vsi = np->vsi;
13105 	struct i40e_pf *pf = vsi->back;
13106 	struct i40e_veb *veb = NULL;
13107 	struct nlattr *attr, *br_spec;
13108 	int i, rem;
13109 
13110 	/* Only for PF VSI for now */
13111 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13112 		return -EOPNOTSUPP;
13113 
13114 	/* Find the HW bridge for PF VSI */
13115 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13116 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13117 			veb = pf->veb[i];
13118 	}
13119 
13120 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13121 
13122 	nla_for_each_nested(attr, br_spec, rem) {
13123 		__u16 mode;
13124 
13125 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
13126 			continue;
13127 
13128 		mode = nla_get_u16(attr);
13129 		if ((mode != BRIDGE_MODE_VEPA) &&
13130 		    (mode != BRIDGE_MODE_VEB))
13131 			return -EINVAL;
13132 
13133 		/* Insert a new HW bridge */
13134 		if (!veb) {
13135 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13136 					     vsi->tc_config.enabled_tc);
13137 			if (veb) {
13138 				veb->bridge_mode = mode;
13139 				i40e_config_bridge_mode(veb);
13140 			} else {
13141 				/* No Bridge HW offload available */
13142 				return -ENOENT;
13143 			}
13144 			break;
13145 		} else if (mode != veb->bridge_mode) {
13146 			/* Existing HW bridge but different mode needs reset */
13147 			veb->bridge_mode = mode;
13148 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13149 			if (mode == BRIDGE_MODE_VEB)
13150 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13151 			else
13152 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13153 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13154 			break;
13155 		}
13156 	}
13157 
13158 	return 0;
13159 }
13160 
13161 /**
13162  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13163  * @skb: skb buff
13164  * @pid: process id
13165  * @seq: RTNL message seq #
13166  * @dev: the netdev being configured
13167  * @filter_mask: unused
13168  * @nlflags: netlink flags passed in
13169  *
13170  * Return the mode in which the hardware bridge is operating in
13171  * i.e VEB or VEPA.
13172  **/
13173 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13174 				   struct net_device *dev,
13175 				   u32 __always_unused filter_mask,
13176 				   int nlflags)
13177 {
13178 	struct i40e_netdev_priv *np = netdev_priv(dev);
13179 	struct i40e_vsi *vsi = np->vsi;
13180 	struct i40e_pf *pf = vsi->back;
13181 	struct i40e_veb *veb = NULL;
13182 	int i;
13183 
13184 	/* Only for PF VSI for now */
13185 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13186 		return -EOPNOTSUPP;
13187 
13188 	/* Find the HW bridge for the PF VSI */
13189 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13190 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13191 			veb = pf->veb[i];
13192 	}
13193 
13194 	if (!veb)
13195 		return 0;
13196 
13197 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13198 				       0, 0, nlflags, filter_mask, NULL);
13199 }
13200 
13201 /**
13202  * i40e_features_check - Validate encapsulated packet conforms to limits
13203  * @skb: skb buff
13204  * @dev: This physical port's netdev
13205  * @features: Offload features that the stack believes apply
13206  **/
13207 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13208 					     struct net_device *dev,
13209 					     netdev_features_t features)
13210 {
13211 	size_t len;
13212 
13213 	/* No point in doing any of this if neither checksum nor GSO are
13214 	 * being requested for this frame.  We can rule out both by just
13215 	 * checking for CHECKSUM_PARTIAL
13216 	 */
13217 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13218 		return features;
13219 
13220 	/* We cannot support GSO if the MSS is going to be less than
13221 	 * 64 bytes.  If it is then we need to drop support for GSO.
13222 	 */
13223 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13224 		features &= ~NETIF_F_GSO_MASK;
13225 
13226 	/* MACLEN can support at most 63 words */
13227 	len = skb_network_header(skb) - skb->data;
13228 	if (len & ~(63 * 2))
13229 		goto out_err;
13230 
13231 	/* IPLEN and EIPLEN can support at most 127 dwords */
13232 	len = skb_transport_header(skb) - skb_network_header(skb);
13233 	if (len & ~(127 * 4))
13234 		goto out_err;
13235 
13236 	if (skb->encapsulation) {
13237 		/* L4TUNLEN can support 127 words */
13238 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13239 		if (len & ~(127 * 2))
13240 			goto out_err;
13241 
13242 		/* IPLEN can support at most 127 dwords */
13243 		len = skb_inner_transport_header(skb) -
13244 		      skb_inner_network_header(skb);
13245 		if (len & ~(127 * 4))
13246 			goto out_err;
13247 	}
13248 
13249 	/* No need to validate L4LEN as TCP is the only protocol with a
13250 	 * flexible value and we support all possible values supported
13251 	 * by TCP, which is at most 15 dwords
13252 	 */
13253 
13254 	return features;
13255 out_err:
13256 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13257 }
13258 
13259 /**
13260  * i40e_xdp_setup - add/remove an XDP program
13261  * @vsi: VSI to changed
13262  * @prog: XDP program
13263  * @extack: netlink extended ack
13264  **/
13265 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13266 			  struct netlink_ext_ack *extack)
13267 {
13268 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
13269 	struct i40e_pf *pf = vsi->back;
13270 	struct bpf_prog *old_prog;
13271 	bool need_reset;
13272 	int i;
13273 
13274 	/* Don't allow frames that span over multiple buffers */
13275 	if (frame_size > vsi->rx_buf_len) {
13276 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
13277 		return -EINVAL;
13278 	}
13279 
13280 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13281 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13282 
13283 	if (need_reset)
13284 		i40e_prep_for_reset(pf);
13285 
13286 	/* VSI shall be deleted in a moment, just return EINVAL */
13287 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13288 		return -EINVAL;
13289 
13290 	old_prog = xchg(&vsi->xdp_prog, prog);
13291 
13292 	if (need_reset) {
13293 		if (!prog)
13294 			/* Wait until ndo_xsk_wakeup completes. */
13295 			synchronize_rcu();
13296 		i40e_reset_and_rebuild(pf, true, true);
13297 	}
13298 
13299 	for (i = 0; i < vsi->num_queue_pairs; i++)
13300 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13301 
13302 	if (old_prog)
13303 		bpf_prog_put(old_prog);
13304 
13305 	/* Kick start the NAPI context if there is an AF_XDP socket open
13306 	 * on that queue id. This so that receiving will start.
13307 	 */
13308 	if (need_reset && prog)
13309 		for (i = 0; i < vsi->num_queue_pairs; i++)
13310 			if (vsi->xdp_rings[i]->xsk_pool)
13311 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13312 						      XDP_WAKEUP_RX);
13313 
13314 	return 0;
13315 }
13316 
13317 /**
13318  * i40e_enter_busy_conf - Enters busy config state
13319  * @vsi: vsi
13320  *
13321  * Returns 0 on success, <0 for failure.
13322  **/
13323 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13324 {
13325 	struct i40e_pf *pf = vsi->back;
13326 	int timeout = 50;
13327 
13328 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13329 		timeout--;
13330 		if (!timeout)
13331 			return -EBUSY;
13332 		usleep_range(1000, 2000);
13333 	}
13334 
13335 	return 0;
13336 }
13337 
13338 /**
13339  * i40e_exit_busy_conf - Exits busy config state
13340  * @vsi: vsi
13341  **/
13342 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13343 {
13344 	struct i40e_pf *pf = vsi->back;
13345 
13346 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13347 }
13348 
13349 /**
13350  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13351  * @vsi: vsi
13352  * @queue_pair: queue pair
13353  **/
13354 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13355 {
13356 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13357 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13358 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13359 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13360 	if (i40e_enabled_xdp_vsi(vsi)) {
13361 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13362 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13363 	}
13364 }
13365 
13366 /**
13367  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13368  * @vsi: vsi
13369  * @queue_pair: queue pair
13370  **/
13371 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13372 {
13373 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13374 	if (i40e_enabled_xdp_vsi(vsi)) {
13375 		/* Make sure that in-progress ndo_xdp_xmit calls are
13376 		 * completed.
13377 		 */
13378 		synchronize_rcu();
13379 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13380 	}
13381 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13382 }
13383 
13384 /**
13385  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13386  * @vsi: vsi
13387  * @queue_pair: queue pair
13388  * @enable: true for enable, false for disable
13389  **/
13390 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13391 					bool enable)
13392 {
13393 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13394 	struct i40e_q_vector *q_vector = rxr->q_vector;
13395 
13396 	if (!vsi->netdev)
13397 		return;
13398 
13399 	/* All rings in a qp belong to the same qvector. */
13400 	if (q_vector->rx.ring || q_vector->tx.ring) {
13401 		if (enable)
13402 			napi_enable(&q_vector->napi);
13403 		else
13404 			napi_disable(&q_vector->napi);
13405 	}
13406 }
13407 
13408 /**
13409  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13410  * @vsi: vsi
13411  * @queue_pair: queue pair
13412  * @enable: true for enable, false for disable
13413  *
13414  * Returns 0 on success, <0 on failure.
13415  **/
13416 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13417 					bool enable)
13418 {
13419 	struct i40e_pf *pf = vsi->back;
13420 	int pf_q, ret = 0;
13421 
13422 	pf_q = vsi->base_queue + queue_pair;
13423 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13424 				     false /*is xdp*/, enable);
13425 	if (ret) {
13426 		dev_info(&pf->pdev->dev,
13427 			 "VSI seid %d Tx ring %d %sable timeout\n",
13428 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13429 		return ret;
13430 	}
13431 
13432 	i40e_control_rx_q(pf, pf_q, enable);
13433 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13434 	if (ret) {
13435 		dev_info(&pf->pdev->dev,
13436 			 "VSI seid %d Rx ring %d %sable timeout\n",
13437 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13438 		return ret;
13439 	}
13440 
13441 	/* Due to HW errata, on Rx disable only, the register can
13442 	 * indicate done before it really is. Needs 50ms to be sure
13443 	 */
13444 	if (!enable)
13445 		mdelay(50);
13446 
13447 	if (!i40e_enabled_xdp_vsi(vsi))
13448 		return ret;
13449 
13450 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13451 				     pf_q + vsi->alloc_queue_pairs,
13452 				     true /*is xdp*/, enable);
13453 	if (ret) {
13454 		dev_info(&pf->pdev->dev,
13455 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13456 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13457 	}
13458 
13459 	return ret;
13460 }
13461 
13462 /**
13463  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13464  * @vsi: vsi
13465  * @queue_pair: queue_pair
13466  **/
13467 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13468 {
13469 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13470 	struct i40e_pf *pf = vsi->back;
13471 	struct i40e_hw *hw = &pf->hw;
13472 
13473 	/* All rings in a qp belong to the same qvector. */
13474 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13475 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13476 	else
13477 		i40e_irq_dynamic_enable_icr0(pf);
13478 
13479 	i40e_flush(hw);
13480 }
13481 
13482 /**
13483  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13484  * @vsi: vsi
13485  * @queue_pair: queue_pair
13486  **/
13487 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13488 {
13489 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13490 	struct i40e_pf *pf = vsi->back;
13491 	struct i40e_hw *hw = &pf->hw;
13492 
13493 	/* For simplicity, instead of removing the qp interrupt causes
13494 	 * from the interrupt linked list, we simply disable the interrupt, and
13495 	 * leave the list intact.
13496 	 *
13497 	 * All rings in a qp belong to the same qvector.
13498 	 */
13499 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13500 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13501 
13502 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13503 		i40e_flush(hw);
13504 		synchronize_irq(pf->msix_entries[intpf].vector);
13505 	} else {
13506 		/* Legacy and MSI mode - this stops all interrupt handling */
13507 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13508 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13509 		i40e_flush(hw);
13510 		synchronize_irq(pf->pdev->irq);
13511 	}
13512 }
13513 
13514 /**
13515  * i40e_queue_pair_disable - Disables a queue pair
13516  * @vsi: vsi
13517  * @queue_pair: queue pair
13518  *
13519  * Returns 0 on success, <0 on failure.
13520  **/
13521 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13522 {
13523 	int err;
13524 
13525 	err = i40e_enter_busy_conf(vsi);
13526 	if (err)
13527 		return err;
13528 
13529 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13530 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13531 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13532 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13533 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13534 
13535 	return err;
13536 }
13537 
13538 /**
13539  * i40e_queue_pair_enable - Enables a queue pair
13540  * @vsi: vsi
13541  * @queue_pair: queue pair
13542  *
13543  * Returns 0 on success, <0 on failure.
13544  **/
13545 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13546 {
13547 	int err;
13548 
13549 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13550 	if (err)
13551 		return err;
13552 
13553 	if (i40e_enabled_xdp_vsi(vsi)) {
13554 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13555 		if (err)
13556 			return err;
13557 	}
13558 
13559 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13560 	if (err)
13561 		return err;
13562 
13563 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13564 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13565 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13566 
13567 	i40e_exit_busy_conf(vsi);
13568 
13569 	return err;
13570 }
13571 
13572 /**
13573  * i40e_xdp - implements ndo_bpf for i40e
13574  * @dev: netdevice
13575  * @xdp: XDP command
13576  **/
13577 static int i40e_xdp(struct net_device *dev,
13578 		    struct netdev_bpf *xdp)
13579 {
13580 	struct i40e_netdev_priv *np = netdev_priv(dev);
13581 	struct i40e_vsi *vsi = np->vsi;
13582 
13583 	if (vsi->type != I40E_VSI_MAIN)
13584 		return -EINVAL;
13585 
13586 	switch (xdp->command) {
13587 	case XDP_SETUP_PROG:
13588 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13589 	case XDP_SETUP_XSK_POOL:
13590 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13591 					   xdp->xsk.queue_id);
13592 	default:
13593 		return -EINVAL;
13594 	}
13595 }
13596 
13597 static const struct net_device_ops i40e_netdev_ops = {
13598 	.ndo_open		= i40e_open,
13599 	.ndo_stop		= i40e_close,
13600 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13601 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13602 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13603 	.ndo_validate_addr	= eth_validate_addr,
13604 	.ndo_set_mac_address	= i40e_set_mac,
13605 	.ndo_change_mtu		= i40e_change_mtu,
13606 	.ndo_eth_ioctl		= i40e_ioctl,
13607 	.ndo_tx_timeout		= i40e_tx_timeout,
13608 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13609 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13610 #ifdef CONFIG_NET_POLL_CONTROLLER
13611 	.ndo_poll_controller	= i40e_netpoll,
13612 #endif
13613 	.ndo_setup_tc		= __i40e_setup_tc,
13614 	.ndo_select_queue	= i40e_lan_select_queue,
13615 	.ndo_set_features	= i40e_set_features,
13616 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13617 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13618 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13619 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13620 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13621 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13622 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13623 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13624 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13625 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13626 	.ndo_features_check	= i40e_features_check,
13627 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13628 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13629 	.ndo_bpf		= i40e_xdp,
13630 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13631 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13632 	.ndo_dfwd_add_station	= i40e_fwd_add,
13633 	.ndo_dfwd_del_station	= i40e_fwd_del,
13634 };
13635 
13636 /**
13637  * i40e_config_netdev - Setup the netdev flags
13638  * @vsi: the VSI being configured
13639  *
13640  * Returns 0 on success, negative value on failure
13641  **/
13642 static int i40e_config_netdev(struct i40e_vsi *vsi)
13643 {
13644 	struct i40e_pf *pf = vsi->back;
13645 	struct i40e_hw *hw = &pf->hw;
13646 	struct i40e_netdev_priv *np;
13647 	struct net_device *netdev;
13648 	u8 broadcast[ETH_ALEN];
13649 	u8 mac_addr[ETH_ALEN];
13650 	int etherdev_size;
13651 	netdev_features_t hw_enc_features;
13652 	netdev_features_t hw_features;
13653 
13654 	etherdev_size = sizeof(struct i40e_netdev_priv);
13655 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13656 	if (!netdev)
13657 		return -ENOMEM;
13658 
13659 	vsi->netdev = netdev;
13660 	np = netdev_priv(netdev);
13661 	np->vsi = vsi;
13662 
13663 	hw_enc_features = NETIF_F_SG			|
13664 			  NETIF_F_HW_CSUM		|
13665 			  NETIF_F_HIGHDMA		|
13666 			  NETIF_F_SOFT_FEATURES		|
13667 			  NETIF_F_TSO			|
13668 			  NETIF_F_TSO_ECN		|
13669 			  NETIF_F_TSO6			|
13670 			  NETIF_F_GSO_GRE		|
13671 			  NETIF_F_GSO_GRE_CSUM		|
13672 			  NETIF_F_GSO_PARTIAL		|
13673 			  NETIF_F_GSO_IPXIP4		|
13674 			  NETIF_F_GSO_IPXIP6		|
13675 			  NETIF_F_GSO_UDP_TUNNEL	|
13676 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13677 			  NETIF_F_GSO_UDP_L4		|
13678 			  NETIF_F_SCTP_CRC		|
13679 			  NETIF_F_RXHASH		|
13680 			  NETIF_F_RXCSUM		|
13681 			  0;
13682 
13683 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13684 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13685 
13686 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13687 
13688 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13689 
13690 	netdev->hw_enc_features |= hw_enc_features;
13691 
13692 	/* record features VLANs can make use of */
13693 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13694 
13695 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13696 				   NETIF_F_GSO_GRE_CSUM |	\
13697 				   NETIF_F_GSO_IPXIP4 |		\
13698 				   NETIF_F_GSO_IPXIP6 |		\
13699 				   NETIF_F_GSO_UDP_TUNNEL |	\
13700 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13701 
13702 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13703 	netdev->features |= NETIF_F_GSO_PARTIAL |
13704 			    I40E_GSO_PARTIAL_FEATURES;
13705 
13706 	netdev->mpls_features |= NETIF_F_SG;
13707 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13708 	netdev->mpls_features |= NETIF_F_TSO;
13709 	netdev->mpls_features |= NETIF_F_TSO6;
13710 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13711 
13712 	/* enable macvlan offloads */
13713 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13714 
13715 	hw_features = hw_enc_features		|
13716 		      NETIF_F_HW_VLAN_CTAG_TX	|
13717 		      NETIF_F_HW_VLAN_CTAG_RX;
13718 
13719 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13720 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13721 
13722 	netdev->hw_features |= hw_features;
13723 
13724 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13725 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13726 
13727 	netdev->features &= ~NETIF_F_HW_TC;
13728 
13729 	if (vsi->type == I40E_VSI_MAIN) {
13730 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13731 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13732 		/* The following steps are necessary for two reasons. First,
13733 		 * some older NVM configurations load a default MAC-VLAN
13734 		 * filter that will accept any tagged packet, and we want to
13735 		 * replace this with a normal filter. Additionally, it is
13736 		 * possible our MAC address was provided by the platform using
13737 		 * Open Firmware or similar.
13738 		 *
13739 		 * Thus, we need to remove the default filter and install one
13740 		 * specific to the MAC address.
13741 		 */
13742 		i40e_rm_default_mac_filter(vsi, mac_addr);
13743 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13744 		i40e_add_mac_filter(vsi, mac_addr);
13745 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13746 	} else {
13747 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13748 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13749 		 * the end, which is 4 bytes long, so force truncation of the
13750 		 * original name by IFNAMSIZ - 4
13751 		 */
13752 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13753 			 IFNAMSIZ - 4,
13754 			 pf->vsi[pf->lan_vsi]->netdev->name);
13755 		eth_random_addr(mac_addr);
13756 
13757 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13758 		i40e_add_mac_filter(vsi, mac_addr);
13759 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13760 	}
13761 
13762 	/* Add the broadcast filter so that we initially will receive
13763 	 * broadcast packets. Note that when a new VLAN is first added the
13764 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13765 	 * specific filters as part of transitioning into "vlan" operation.
13766 	 * When more VLANs are added, the driver will copy each existing MAC
13767 	 * filter and add it for the new VLAN.
13768 	 *
13769 	 * Broadcast filters are handled specially by
13770 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13771 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13772 	 * filter. The subtask will update the correct broadcast promiscuous
13773 	 * bits as VLANs become active or inactive.
13774 	 */
13775 	eth_broadcast_addr(broadcast);
13776 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13777 	i40e_add_mac_filter(vsi, broadcast);
13778 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13779 
13780 	eth_hw_addr_set(netdev, mac_addr);
13781 	ether_addr_copy(netdev->perm_addr, mac_addr);
13782 
13783 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13784 	netdev->neigh_priv_len = sizeof(u32) * 4;
13785 
13786 	netdev->priv_flags |= IFF_UNICAST_FLT;
13787 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13788 	/* Setup netdev TC information */
13789 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13790 
13791 	netdev->netdev_ops = &i40e_netdev_ops;
13792 	netdev->watchdog_timeo = 5 * HZ;
13793 	i40e_set_ethtool_ops(netdev);
13794 
13795 	/* MTU range: 68 - 9706 */
13796 	netdev->min_mtu = ETH_MIN_MTU;
13797 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13798 
13799 	return 0;
13800 }
13801 
13802 /**
13803  * i40e_vsi_delete - Delete a VSI from the switch
13804  * @vsi: the VSI being removed
13805  *
13806  * Returns 0 on success, negative value on failure
13807  **/
13808 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13809 {
13810 	/* remove default VSI is not allowed */
13811 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13812 		return;
13813 
13814 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13815 }
13816 
13817 /**
13818  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13819  * @vsi: the VSI being queried
13820  *
13821  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13822  **/
13823 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13824 {
13825 	struct i40e_veb *veb;
13826 	struct i40e_pf *pf = vsi->back;
13827 
13828 	/* Uplink is not a bridge so default to VEB */
13829 	if (vsi->veb_idx >= I40E_MAX_VEB)
13830 		return 1;
13831 
13832 	veb = pf->veb[vsi->veb_idx];
13833 	if (!veb) {
13834 		dev_info(&pf->pdev->dev,
13835 			 "There is no veb associated with the bridge\n");
13836 		return -ENOENT;
13837 	}
13838 
13839 	/* Uplink is a bridge in VEPA mode */
13840 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13841 		return 0;
13842 	} else {
13843 		/* Uplink is a bridge in VEB mode */
13844 		return 1;
13845 	}
13846 
13847 	/* VEPA is now default bridge, so return 0 */
13848 	return 0;
13849 }
13850 
13851 /**
13852  * i40e_add_vsi - Add a VSI to the switch
13853  * @vsi: the VSI being configured
13854  *
13855  * This initializes a VSI context depending on the VSI type to be added and
13856  * passes it down to the add_vsi aq command.
13857  **/
13858 static int i40e_add_vsi(struct i40e_vsi *vsi)
13859 {
13860 	int ret = -ENODEV;
13861 	struct i40e_pf *pf = vsi->back;
13862 	struct i40e_hw *hw = &pf->hw;
13863 	struct i40e_vsi_context ctxt;
13864 	struct i40e_mac_filter *f;
13865 	struct hlist_node *h;
13866 	int bkt;
13867 
13868 	u8 enabled_tc = 0x1; /* TC0 enabled */
13869 	int f_count = 0;
13870 
13871 	memset(&ctxt, 0, sizeof(ctxt));
13872 	switch (vsi->type) {
13873 	case I40E_VSI_MAIN:
13874 		/* The PF's main VSI is already setup as part of the
13875 		 * device initialization, so we'll not bother with
13876 		 * the add_vsi call, but we will retrieve the current
13877 		 * VSI context.
13878 		 */
13879 		ctxt.seid = pf->main_vsi_seid;
13880 		ctxt.pf_num = pf->hw.pf_id;
13881 		ctxt.vf_num = 0;
13882 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13883 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13884 		if (ret) {
13885 			dev_info(&pf->pdev->dev,
13886 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13887 				 i40e_stat_str(&pf->hw, ret),
13888 				 i40e_aq_str(&pf->hw,
13889 					     pf->hw.aq.asq_last_status));
13890 			return -ENOENT;
13891 		}
13892 		vsi->info = ctxt.info;
13893 		vsi->info.valid_sections = 0;
13894 
13895 		vsi->seid = ctxt.seid;
13896 		vsi->id = ctxt.vsi_number;
13897 
13898 		enabled_tc = i40e_pf_get_tc_map(pf);
13899 
13900 		/* Source pruning is enabled by default, so the flag is
13901 		 * negative logic - if it's set, we need to fiddle with
13902 		 * the VSI to disable source pruning.
13903 		 */
13904 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13905 			memset(&ctxt, 0, sizeof(ctxt));
13906 			ctxt.seid = pf->main_vsi_seid;
13907 			ctxt.pf_num = pf->hw.pf_id;
13908 			ctxt.vf_num = 0;
13909 			ctxt.info.valid_sections |=
13910 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13911 			ctxt.info.switch_id =
13912 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13913 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13914 			if (ret) {
13915 				dev_info(&pf->pdev->dev,
13916 					 "update vsi failed, err %s aq_err %s\n",
13917 					 i40e_stat_str(&pf->hw, ret),
13918 					 i40e_aq_str(&pf->hw,
13919 						     pf->hw.aq.asq_last_status));
13920 				ret = -ENOENT;
13921 				goto err;
13922 			}
13923 		}
13924 
13925 		/* MFP mode setup queue map and update VSI */
13926 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13927 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13928 			memset(&ctxt, 0, sizeof(ctxt));
13929 			ctxt.seid = pf->main_vsi_seid;
13930 			ctxt.pf_num = pf->hw.pf_id;
13931 			ctxt.vf_num = 0;
13932 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13933 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13934 			if (ret) {
13935 				dev_info(&pf->pdev->dev,
13936 					 "update vsi failed, err %s aq_err %s\n",
13937 					 i40e_stat_str(&pf->hw, ret),
13938 					 i40e_aq_str(&pf->hw,
13939 						    pf->hw.aq.asq_last_status));
13940 				ret = -ENOENT;
13941 				goto err;
13942 			}
13943 			/* update the local VSI info queue map */
13944 			i40e_vsi_update_queue_map(vsi, &ctxt);
13945 			vsi->info.valid_sections = 0;
13946 		} else {
13947 			/* Default/Main VSI is only enabled for TC0
13948 			 * reconfigure it to enable all TCs that are
13949 			 * available on the port in SFP mode.
13950 			 * For MFP case the iSCSI PF would use this
13951 			 * flow to enable LAN+iSCSI TC.
13952 			 */
13953 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13954 			if (ret) {
13955 				/* Single TC condition is not fatal,
13956 				 * message and continue
13957 				 */
13958 				dev_info(&pf->pdev->dev,
13959 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13960 					 enabled_tc,
13961 					 i40e_stat_str(&pf->hw, ret),
13962 					 i40e_aq_str(&pf->hw,
13963 						    pf->hw.aq.asq_last_status));
13964 			}
13965 		}
13966 		break;
13967 
13968 	case I40E_VSI_FDIR:
13969 		ctxt.pf_num = hw->pf_id;
13970 		ctxt.vf_num = 0;
13971 		ctxt.uplink_seid = vsi->uplink_seid;
13972 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13973 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13974 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13975 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13976 			ctxt.info.valid_sections |=
13977 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13978 			ctxt.info.switch_id =
13979 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13980 		}
13981 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13982 		break;
13983 
13984 	case I40E_VSI_VMDQ2:
13985 		ctxt.pf_num = hw->pf_id;
13986 		ctxt.vf_num = 0;
13987 		ctxt.uplink_seid = vsi->uplink_seid;
13988 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13989 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13990 
13991 		/* This VSI is connected to VEB so the switch_id
13992 		 * should be set to zero by default.
13993 		 */
13994 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13995 			ctxt.info.valid_sections |=
13996 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13997 			ctxt.info.switch_id =
13998 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13999 		}
14000 
14001 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14002 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14003 		break;
14004 
14005 	case I40E_VSI_SRIOV:
14006 		ctxt.pf_num = hw->pf_id;
14007 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
14008 		ctxt.uplink_seid = vsi->uplink_seid;
14009 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14010 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
14011 
14012 		/* This VSI is connected to VEB so the switch_id
14013 		 * should be set to zero by default.
14014 		 */
14015 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14016 			ctxt.info.valid_sections |=
14017 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14018 			ctxt.info.switch_id =
14019 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14020 		}
14021 
14022 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
14023 			ctxt.info.valid_sections |=
14024 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14025 			ctxt.info.queueing_opt_flags |=
14026 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14027 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14028 		}
14029 
14030 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14031 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14032 		if (pf->vf[vsi->vf_id].spoofchk) {
14033 			ctxt.info.valid_sections |=
14034 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14035 			ctxt.info.sec_flags |=
14036 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14037 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14038 		}
14039 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14040 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14041 		break;
14042 
14043 	case I40E_VSI_IWARP:
14044 		/* send down message to iWARP */
14045 		break;
14046 
14047 	default:
14048 		return -ENODEV;
14049 	}
14050 
14051 	if (vsi->type != I40E_VSI_MAIN) {
14052 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14053 		if (ret) {
14054 			dev_info(&vsi->back->pdev->dev,
14055 				 "add vsi failed, err %s aq_err %s\n",
14056 				 i40e_stat_str(&pf->hw, ret),
14057 				 i40e_aq_str(&pf->hw,
14058 					     pf->hw.aq.asq_last_status));
14059 			ret = -ENOENT;
14060 			goto err;
14061 		}
14062 		vsi->info = ctxt.info;
14063 		vsi->info.valid_sections = 0;
14064 		vsi->seid = ctxt.seid;
14065 		vsi->id = ctxt.vsi_number;
14066 	}
14067 
14068 	vsi->active_filters = 0;
14069 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14070 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14071 	/* If macvlan filters already exist, force them to get loaded */
14072 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14073 		f->state = I40E_FILTER_NEW;
14074 		f_count++;
14075 	}
14076 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14077 
14078 	if (f_count) {
14079 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14080 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14081 	}
14082 
14083 	/* Update VSI BW information */
14084 	ret = i40e_vsi_get_bw_info(vsi);
14085 	if (ret) {
14086 		dev_info(&pf->pdev->dev,
14087 			 "couldn't get vsi bw info, err %s aq_err %s\n",
14088 			 i40e_stat_str(&pf->hw, ret),
14089 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14090 		/* VSI is already added so not tearing that up */
14091 		ret = 0;
14092 	}
14093 
14094 err:
14095 	return ret;
14096 }
14097 
14098 /**
14099  * i40e_vsi_release - Delete a VSI and free its resources
14100  * @vsi: the VSI being removed
14101  *
14102  * Returns 0 on success or < 0 on error
14103  **/
14104 int i40e_vsi_release(struct i40e_vsi *vsi)
14105 {
14106 	struct i40e_mac_filter *f;
14107 	struct hlist_node *h;
14108 	struct i40e_veb *veb = NULL;
14109 	struct i40e_pf *pf;
14110 	u16 uplink_seid;
14111 	int i, n, bkt;
14112 
14113 	pf = vsi->back;
14114 
14115 	/* release of a VEB-owner or last VSI is not allowed */
14116 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14117 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14118 			 vsi->seid, vsi->uplink_seid);
14119 		return -ENODEV;
14120 	}
14121 	if (vsi == pf->vsi[pf->lan_vsi] &&
14122 	    !test_bit(__I40E_DOWN, pf->state)) {
14123 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14124 		return -ENODEV;
14125 	}
14126 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14127 	uplink_seid = vsi->uplink_seid;
14128 	if (vsi->type != I40E_VSI_SRIOV) {
14129 		if (vsi->netdev_registered) {
14130 			vsi->netdev_registered = false;
14131 			if (vsi->netdev) {
14132 				/* results in a call to i40e_close() */
14133 				unregister_netdev(vsi->netdev);
14134 			}
14135 		} else {
14136 			i40e_vsi_close(vsi);
14137 		}
14138 		i40e_vsi_disable_irq(vsi);
14139 	}
14140 
14141 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14142 
14143 	/* clear the sync flag on all filters */
14144 	if (vsi->netdev) {
14145 		__dev_uc_unsync(vsi->netdev, NULL);
14146 		__dev_mc_unsync(vsi->netdev, NULL);
14147 	}
14148 
14149 	/* make sure any remaining filters are marked for deletion */
14150 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14151 		__i40e_del_filter(vsi, f);
14152 
14153 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14154 
14155 	i40e_sync_vsi_filters(vsi);
14156 
14157 	i40e_vsi_delete(vsi);
14158 	i40e_vsi_free_q_vectors(vsi);
14159 	if (vsi->netdev) {
14160 		free_netdev(vsi->netdev);
14161 		vsi->netdev = NULL;
14162 	}
14163 	i40e_vsi_clear_rings(vsi);
14164 	i40e_vsi_clear(vsi);
14165 
14166 	/* If this was the last thing on the VEB, except for the
14167 	 * controlling VSI, remove the VEB, which puts the controlling
14168 	 * VSI onto the next level down in the switch.
14169 	 *
14170 	 * Well, okay, there's one more exception here: don't remove
14171 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
14172 	 * from up the network stack.
14173 	 */
14174 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
14175 		if (pf->vsi[i] &&
14176 		    pf->vsi[i]->uplink_seid == uplink_seid &&
14177 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14178 			n++;      /* count the VSIs */
14179 		}
14180 	}
14181 	for (i = 0; i < I40E_MAX_VEB; i++) {
14182 		if (!pf->veb[i])
14183 			continue;
14184 		if (pf->veb[i]->uplink_seid == uplink_seid)
14185 			n++;     /* count the VEBs */
14186 		if (pf->veb[i]->seid == uplink_seid)
14187 			veb = pf->veb[i];
14188 	}
14189 	if (n == 0 && veb && veb->uplink_seid != 0)
14190 		i40e_veb_release(veb);
14191 
14192 	return 0;
14193 }
14194 
14195 /**
14196  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14197  * @vsi: ptr to the VSI
14198  *
14199  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14200  * corresponding SW VSI structure and initializes num_queue_pairs for the
14201  * newly allocated VSI.
14202  *
14203  * Returns 0 on success or negative on failure
14204  **/
14205 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14206 {
14207 	int ret = -ENOENT;
14208 	struct i40e_pf *pf = vsi->back;
14209 
14210 	if (vsi->q_vectors[0]) {
14211 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14212 			 vsi->seid);
14213 		return -EEXIST;
14214 	}
14215 
14216 	if (vsi->base_vector) {
14217 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14218 			 vsi->seid, vsi->base_vector);
14219 		return -EEXIST;
14220 	}
14221 
14222 	ret = i40e_vsi_alloc_q_vectors(vsi);
14223 	if (ret) {
14224 		dev_info(&pf->pdev->dev,
14225 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14226 			 vsi->num_q_vectors, vsi->seid, ret);
14227 		vsi->num_q_vectors = 0;
14228 		goto vector_setup_out;
14229 	}
14230 
14231 	/* In Legacy mode, we do not have to get any other vector since we
14232 	 * piggyback on the misc/ICR0 for queue interrupts.
14233 	*/
14234 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
14235 		return ret;
14236 	if (vsi->num_q_vectors)
14237 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14238 						 vsi->num_q_vectors, vsi->idx);
14239 	if (vsi->base_vector < 0) {
14240 		dev_info(&pf->pdev->dev,
14241 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14242 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14243 		i40e_vsi_free_q_vectors(vsi);
14244 		ret = -ENOENT;
14245 		goto vector_setup_out;
14246 	}
14247 
14248 vector_setup_out:
14249 	return ret;
14250 }
14251 
14252 /**
14253  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14254  * @vsi: pointer to the vsi.
14255  *
14256  * This re-allocates a vsi's queue resources.
14257  *
14258  * Returns pointer to the successfully allocated and configured VSI sw struct
14259  * on success, otherwise returns NULL on failure.
14260  **/
14261 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14262 {
14263 	u16 alloc_queue_pairs;
14264 	struct i40e_pf *pf;
14265 	u8 enabled_tc;
14266 	int ret;
14267 
14268 	if (!vsi)
14269 		return NULL;
14270 
14271 	pf = vsi->back;
14272 
14273 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14274 	i40e_vsi_clear_rings(vsi);
14275 
14276 	i40e_vsi_free_arrays(vsi, false);
14277 	i40e_set_num_rings_in_vsi(vsi);
14278 	ret = i40e_vsi_alloc_arrays(vsi, false);
14279 	if (ret)
14280 		goto err_vsi;
14281 
14282 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14283 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14284 
14285 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14286 	if (ret < 0) {
14287 		dev_info(&pf->pdev->dev,
14288 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14289 			 alloc_queue_pairs, vsi->seid, ret);
14290 		goto err_vsi;
14291 	}
14292 	vsi->base_queue = ret;
14293 
14294 	/* Update the FW view of the VSI. Force a reset of TC and queue
14295 	 * layout configurations.
14296 	 */
14297 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14298 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14299 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14300 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14301 	if (vsi->type == I40E_VSI_MAIN)
14302 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14303 
14304 	/* assign it some queues */
14305 	ret = i40e_alloc_rings(vsi);
14306 	if (ret)
14307 		goto err_rings;
14308 
14309 	/* map all of the rings to the q_vectors */
14310 	i40e_vsi_map_rings_to_vectors(vsi);
14311 	return vsi;
14312 
14313 err_rings:
14314 	i40e_vsi_free_q_vectors(vsi);
14315 	if (vsi->netdev_registered) {
14316 		vsi->netdev_registered = false;
14317 		unregister_netdev(vsi->netdev);
14318 		free_netdev(vsi->netdev);
14319 		vsi->netdev = NULL;
14320 	}
14321 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14322 err_vsi:
14323 	i40e_vsi_clear(vsi);
14324 	return NULL;
14325 }
14326 
14327 /**
14328  * i40e_vsi_setup - Set up a VSI by a given type
14329  * @pf: board private structure
14330  * @type: VSI type
14331  * @uplink_seid: the switch element to link to
14332  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14333  *
14334  * This allocates the sw VSI structure and its queue resources, then add a VSI
14335  * to the identified VEB.
14336  *
14337  * Returns pointer to the successfully allocated and configure VSI sw struct on
14338  * success, otherwise returns NULL on failure.
14339  **/
14340 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14341 				u16 uplink_seid, u32 param1)
14342 {
14343 	struct i40e_vsi *vsi = NULL;
14344 	struct i40e_veb *veb = NULL;
14345 	u16 alloc_queue_pairs;
14346 	int ret, i;
14347 	int v_idx;
14348 
14349 	/* The requested uplink_seid must be either
14350 	 *     - the PF's port seid
14351 	 *              no VEB is needed because this is the PF
14352 	 *              or this is a Flow Director special case VSI
14353 	 *     - seid of an existing VEB
14354 	 *     - seid of a VSI that owns an existing VEB
14355 	 *     - seid of a VSI that doesn't own a VEB
14356 	 *              a new VEB is created and the VSI becomes the owner
14357 	 *     - seid of the PF VSI, which is what creates the first VEB
14358 	 *              this is a special case of the previous
14359 	 *
14360 	 * Find which uplink_seid we were given and create a new VEB if needed
14361 	 */
14362 	for (i = 0; i < I40E_MAX_VEB; i++) {
14363 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14364 			veb = pf->veb[i];
14365 			break;
14366 		}
14367 	}
14368 
14369 	if (!veb && uplink_seid != pf->mac_seid) {
14370 
14371 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14372 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14373 				vsi = pf->vsi[i];
14374 				break;
14375 			}
14376 		}
14377 		if (!vsi) {
14378 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14379 				 uplink_seid);
14380 			return NULL;
14381 		}
14382 
14383 		if (vsi->uplink_seid == pf->mac_seid)
14384 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14385 					     vsi->tc_config.enabled_tc);
14386 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14387 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14388 					     vsi->tc_config.enabled_tc);
14389 		if (veb) {
14390 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14391 				dev_info(&vsi->back->pdev->dev,
14392 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14393 				return NULL;
14394 			}
14395 			/* We come up by default in VEPA mode if SRIOV is not
14396 			 * already enabled, in which case we can't force VEPA
14397 			 * mode.
14398 			 */
14399 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14400 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14401 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14402 			}
14403 			i40e_config_bridge_mode(veb);
14404 		}
14405 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14406 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14407 				veb = pf->veb[i];
14408 		}
14409 		if (!veb) {
14410 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14411 			return NULL;
14412 		}
14413 
14414 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14415 		uplink_seid = veb->seid;
14416 	}
14417 
14418 	/* get vsi sw struct */
14419 	v_idx = i40e_vsi_mem_alloc(pf, type);
14420 	if (v_idx < 0)
14421 		goto err_alloc;
14422 	vsi = pf->vsi[v_idx];
14423 	if (!vsi)
14424 		goto err_alloc;
14425 	vsi->type = type;
14426 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14427 
14428 	if (type == I40E_VSI_MAIN)
14429 		pf->lan_vsi = v_idx;
14430 	else if (type == I40E_VSI_SRIOV)
14431 		vsi->vf_id = param1;
14432 	/* assign it some queues */
14433 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14434 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14435 
14436 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14437 	if (ret < 0) {
14438 		dev_info(&pf->pdev->dev,
14439 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14440 			 alloc_queue_pairs, vsi->seid, ret);
14441 		goto err_vsi;
14442 	}
14443 	vsi->base_queue = ret;
14444 
14445 	/* get a VSI from the hardware */
14446 	vsi->uplink_seid = uplink_seid;
14447 	ret = i40e_add_vsi(vsi);
14448 	if (ret)
14449 		goto err_vsi;
14450 
14451 	switch (vsi->type) {
14452 	/* setup the netdev if needed */
14453 	case I40E_VSI_MAIN:
14454 	case I40E_VSI_VMDQ2:
14455 		ret = i40e_config_netdev(vsi);
14456 		if (ret)
14457 			goto err_netdev;
14458 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14459 		if (ret)
14460 			goto err_netdev;
14461 		ret = register_netdev(vsi->netdev);
14462 		if (ret)
14463 			goto err_netdev;
14464 		vsi->netdev_registered = true;
14465 		netif_carrier_off(vsi->netdev);
14466 #ifdef CONFIG_I40E_DCB
14467 		/* Setup DCB netlink interface */
14468 		i40e_dcbnl_setup(vsi);
14469 #endif /* CONFIG_I40E_DCB */
14470 		fallthrough;
14471 	case I40E_VSI_FDIR:
14472 		/* set up vectors and rings if needed */
14473 		ret = i40e_vsi_setup_vectors(vsi);
14474 		if (ret)
14475 			goto err_msix;
14476 
14477 		ret = i40e_alloc_rings(vsi);
14478 		if (ret)
14479 			goto err_rings;
14480 
14481 		/* map all of the rings to the q_vectors */
14482 		i40e_vsi_map_rings_to_vectors(vsi);
14483 
14484 		i40e_vsi_reset_stats(vsi);
14485 		break;
14486 	default:
14487 		/* no netdev or rings for the other VSI types */
14488 		break;
14489 	}
14490 
14491 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14492 	    (vsi->type == I40E_VSI_VMDQ2)) {
14493 		ret = i40e_vsi_config_rss(vsi);
14494 	}
14495 	return vsi;
14496 
14497 err_rings:
14498 	i40e_vsi_free_q_vectors(vsi);
14499 err_msix:
14500 	if (vsi->netdev_registered) {
14501 		vsi->netdev_registered = false;
14502 		unregister_netdev(vsi->netdev);
14503 		free_netdev(vsi->netdev);
14504 		vsi->netdev = NULL;
14505 	}
14506 err_netdev:
14507 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14508 err_vsi:
14509 	i40e_vsi_clear(vsi);
14510 err_alloc:
14511 	return NULL;
14512 }
14513 
14514 /**
14515  * i40e_veb_get_bw_info - Query VEB BW information
14516  * @veb: the veb to query
14517  *
14518  * Query the Tx scheduler BW configuration data for given VEB
14519  **/
14520 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14521 {
14522 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14523 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14524 	struct i40e_pf *pf = veb->pf;
14525 	struct i40e_hw *hw = &pf->hw;
14526 	u32 tc_bw_max;
14527 	int ret = 0;
14528 	int i;
14529 
14530 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14531 						  &bw_data, NULL);
14532 	if (ret) {
14533 		dev_info(&pf->pdev->dev,
14534 			 "query veb bw config failed, err %s aq_err %s\n",
14535 			 i40e_stat_str(&pf->hw, ret),
14536 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14537 		goto out;
14538 	}
14539 
14540 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14541 						   &ets_data, NULL);
14542 	if (ret) {
14543 		dev_info(&pf->pdev->dev,
14544 			 "query veb bw ets config failed, err %s aq_err %s\n",
14545 			 i40e_stat_str(&pf->hw, ret),
14546 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14547 		goto out;
14548 	}
14549 
14550 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14551 	veb->bw_max_quanta = ets_data.tc_bw_max;
14552 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14553 	veb->enabled_tc = ets_data.tc_valid_bits;
14554 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14555 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14556 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14557 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14558 		veb->bw_tc_limit_credits[i] =
14559 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14560 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14561 	}
14562 
14563 out:
14564 	return ret;
14565 }
14566 
14567 /**
14568  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14569  * @pf: board private structure
14570  *
14571  * On error: returns error code (negative)
14572  * On success: returns vsi index in PF (positive)
14573  **/
14574 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14575 {
14576 	int ret = -ENOENT;
14577 	struct i40e_veb *veb;
14578 	int i;
14579 
14580 	/* Need to protect the allocation of switch elements at the PF level */
14581 	mutex_lock(&pf->switch_mutex);
14582 
14583 	/* VEB list may be fragmented if VEB creation/destruction has
14584 	 * been happening.  We can afford to do a quick scan to look
14585 	 * for any free slots in the list.
14586 	 *
14587 	 * find next empty veb slot, looping back around if necessary
14588 	 */
14589 	i = 0;
14590 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14591 		i++;
14592 	if (i >= I40E_MAX_VEB) {
14593 		ret = -ENOMEM;
14594 		goto err_alloc_veb;  /* out of VEB slots! */
14595 	}
14596 
14597 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14598 	if (!veb) {
14599 		ret = -ENOMEM;
14600 		goto err_alloc_veb;
14601 	}
14602 	veb->pf = pf;
14603 	veb->idx = i;
14604 	veb->enabled_tc = 1;
14605 
14606 	pf->veb[i] = veb;
14607 	ret = i;
14608 err_alloc_veb:
14609 	mutex_unlock(&pf->switch_mutex);
14610 	return ret;
14611 }
14612 
14613 /**
14614  * i40e_switch_branch_release - Delete a branch of the switch tree
14615  * @branch: where to start deleting
14616  *
14617  * This uses recursion to find the tips of the branch to be
14618  * removed, deleting until we get back to and can delete this VEB.
14619  **/
14620 static void i40e_switch_branch_release(struct i40e_veb *branch)
14621 {
14622 	struct i40e_pf *pf = branch->pf;
14623 	u16 branch_seid = branch->seid;
14624 	u16 veb_idx = branch->idx;
14625 	int i;
14626 
14627 	/* release any VEBs on this VEB - RECURSION */
14628 	for (i = 0; i < I40E_MAX_VEB; i++) {
14629 		if (!pf->veb[i])
14630 			continue;
14631 		if (pf->veb[i]->uplink_seid == branch->seid)
14632 			i40e_switch_branch_release(pf->veb[i]);
14633 	}
14634 
14635 	/* Release the VSIs on this VEB, but not the owner VSI.
14636 	 *
14637 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14638 	 *       the VEB itself, so don't use (*branch) after this loop.
14639 	 */
14640 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14641 		if (!pf->vsi[i])
14642 			continue;
14643 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14644 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14645 			i40e_vsi_release(pf->vsi[i]);
14646 		}
14647 	}
14648 
14649 	/* There's one corner case where the VEB might not have been
14650 	 * removed, so double check it here and remove it if needed.
14651 	 * This case happens if the veb was created from the debugfs
14652 	 * commands and no VSIs were added to it.
14653 	 */
14654 	if (pf->veb[veb_idx])
14655 		i40e_veb_release(pf->veb[veb_idx]);
14656 }
14657 
14658 /**
14659  * i40e_veb_clear - remove veb struct
14660  * @veb: the veb to remove
14661  **/
14662 static void i40e_veb_clear(struct i40e_veb *veb)
14663 {
14664 	if (!veb)
14665 		return;
14666 
14667 	if (veb->pf) {
14668 		struct i40e_pf *pf = veb->pf;
14669 
14670 		mutex_lock(&pf->switch_mutex);
14671 		if (pf->veb[veb->idx] == veb)
14672 			pf->veb[veb->idx] = NULL;
14673 		mutex_unlock(&pf->switch_mutex);
14674 	}
14675 
14676 	kfree(veb);
14677 }
14678 
14679 /**
14680  * i40e_veb_release - Delete a VEB and free its resources
14681  * @veb: the VEB being removed
14682  **/
14683 void i40e_veb_release(struct i40e_veb *veb)
14684 {
14685 	struct i40e_vsi *vsi = NULL;
14686 	struct i40e_pf *pf;
14687 	int i, n = 0;
14688 
14689 	pf = veb->pf;
14690 
14691 	/* find the remaining VSI and check for extras */
14692 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14693 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14694 			n++;
14695 			vsi = pf->vsi[i];
14696 		}
14697 	}
14698 	if (n != 1) {
14699 		dev_info(&pf->pdev->dev,
14700 			 "can't remove VEB %d with %d VSIs left\n",
14701 			 veb->seid, n);
14702 		return;
14703 	}
14704 
14705 	/* move the remaining VSI to uplink veb */
14706 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14707 	if (veb->uplink_seid) {
14708 		vsi->uplink_seid = veb->uplink_seid;
14709 		if (veb->uplink_seid == pf->mac_seid)
14710 			vsi->veb_idx = I40E_NO_VEB;
14711 		else
14712 			vsi->veb_idx = veb->veb_idx;
14713 	} else {
14714 		/* floating VEB */
14715 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14716 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14717 	}
14718 
14719 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14720 	i40e_veb_clear(veb);
14721 }
14722 
14723 /**
14724  * i40e_add_veb - create the VEB in the switch
14725  * @veb: the VEB to be instantiated
14726  * @vsi: the controlling VSI
14727  **/
14728 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14729 {
14730 	struct i40e_pf *pf = veb->pf;
14731 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14732 	int ret;
14733 
14734 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14735 			      veb->enabled_tc, false,
14736 			      &veb->seid, enable_stats, NULL);
14737 
14738 	/* get a VEB from the hardware */
14739 	if (ret) {
14740 		dev_info(&pf->pdev->dev,
14741 			 "couldn't add VEB, err %s aq_err %s\n",
14742 			 i40e_stat_str(&pf->hw, ret),
14743 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14744 		return -EPERM;
14745 	}
14746 
14747 	/* get statistics counter */
14748 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14749 					 &veb->stats_idx, NULL, NULL, NULL);
14750 	if (ret) {
14751 		dev_info(&pf->pdev->dev,
14752 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
14753 			 i40e_stat_str(&pf->hw, ret),
14754 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14755 		return -EPERM;
14756 	}
14757 	ret = i40e_veb_get_bw_info(veb);
14758 	if (ret) {
14759 		dev_info(&pf->pdev->dev,
14760 			 "couldn't get VEB bw info, err %s aq_err %s\n",
14761 			 i40e_stat_str(&pf->hw, ret),
14762 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14763 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14764 		return -ENOENT;
14765 	}
14766 
14767 	vsi->uplink_seid = veb->seid;
14768 	vsi->veb_idx = veb->idx;
14769 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14770 
14771 	return 0;
14772 }
14773 
14774 /**
14775  * i40e_veb_setup - Set up a VEB
14776  * @pf: board private structure
14777  * @flags: VEB setup flags
14778  * @uplink_seid: the switch element to link to
14779  * @vsi_seid: the initial VSI seid
14780  * @enabled_tc: Enabled TC bit-map
14781  *
14782  * This allocates the sw VEB structure and links it into the switch
14783  * It is possible and legal for this to be a duplicate of an already
14784  * existing VEB.  It is also possible for both uplink and vsi seids
14785  * to be zero, in order to create a floating VEB.
14786  *
14787  * Returns pointer to the successfully allocated VEB sw struct on
14788  * success, otherwise returns NULL on failure.
14789  **/
14790 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14791 				u16 uplink_seid, u16 vsi_seid,
14792 				u8 enabled_tc)
14793 {
14794 	struct i40e_veb *veb, *uplink_veb = NULL;
14795 	int vsi_idx, veb_idx;
14796 	int ret;
14797 
14798 	/* if one seid is 0, the other must be 0 to create a floating relay */
14799 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14800 	    (uplink_seid + vsi_seid != 0)) {
14801 		dev_info(&pf->pdev->dev,
14802 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14803 			 uplink_seid, vsi_seid);
14804 		return NULL;
14805 	}
14806 
14807 	/* make sure there is such a vsi and uplink */
14808 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14809 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14810 			break;
14811 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14812 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14813 			 vsi_seid);
14814 		return NULL;
14815 	}
14816 
14817 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14818 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14819 			if (pf->veb[veb_idx] &&
14820 			    pf->veb[veb_idx]->seid == uplink_seid) {
14821 				uplink_veb = pf->veb[veb_idx];
14822 				break;
14823 			}
14824 		}
14825 		if (!uplink_veb) {
14826 			dev_info(&pf->pdev->dev,
14827 				 "uplink seid %d not found\n", uplink_seid);
14828 			return NULL;
14829 		}
14830 	}
14831 
14832 	/* get veb sw struct */
14833 	veb_idx = i40e_veb_mem_alloc(pf);
14834 	if (veb_idx < 0)
14835 		goto err_alloc;
14836 	veb = pf->veb[veb_idx];
14837 	veb->flags = flags;
14838 	veb->uplink_seid = uplink_seid;
14839 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14840 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14841 
14842 	/* create the VEB in the switch */
14843 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14844 	if (ret)
14845 		goto err_veb;
14846 	if (vsi_idx == pf->lan_vsi)
14847 		pf->lan_veb = veb->idx;
14848 
14849 	return veb;
14850 
14851 err_veb:
14852 	i40e_veb_clear(veb);
14853 err_alloc:
14854 	return NULL;
14855 }
14856 
14857 /**
14858  * i40e_setup_pf_switch_element - set PF vars based on switch type
14859  * @pf: board private structure
14860  * @ele: element we are building info from
14861  * @num_reported: total number of elements
14862  * @printconfig: should we print the contents
14863  *
14864  * helper function to assist in extracting a few useful SEID values.
14865  **/
14866 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14867 				struct i40e_aqc_switch_config_element_resp *ele,
14868 				u16 num_reported, bool printconfig)
14869 {
14870 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14871 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14872 	u8 element_type = ele->element_type;
14873 	u16 seid = le16_to_cpu(ele->seid);
14874 
14875 	if (printconfig)
14876 		dev_info(&pf->pdev->dev,
14877 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14878 			 element_type, seid, uplink_seid, downlink_seid);
14879 
14880 	switch (element_type) {
14881 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14882 		pf->mac_seid = seid;
14883 		break;
14884 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14885 		/* Main VEB? */
14886 		if (uplink_seid != pf->mac_seid)
14887 			break;
14888 		if (pf->lan_veb >= I40E_MAX_VEB) {
14889 			int v;
14890 
14891 			/* find existing or else empty VEB */
14892 			for (v = 0; v < I40E_MAX_VEB; v++) {
14893 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14894 					pf->lan_veb = v;
14895 					break;
14896 				}
14897 			}
14898 			if (pf->lan_veb >= I40E_MAX_VEB) {
14899 				v = i40e_veb_mem_alloc(pf);
14900 				if (v < 0)
14901 					break;
14902 				pf->lan_veb = v;
14903 			}
14904 		}
14905 		if (pf->lan_veb >= I40E_MAX_VEB)
14906 			break;
14907 
14908 		pf->veb[pf->lan_veb]->seid = seid;
14909 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14910 		pf->veb[pf->lan_veb]->pf = pf;
14911 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14912 		break;
14913 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14914 		if (num_reported != 1)
14915 			break;
14916 		/* This is immediately after a reset so we can assume this is
14917 		 * the PF's VSI
14918 		 */
14919 		pf->mac_seid = uplink_seid;
14920 		pf->pf_seid = downlink_seid;
14921 		pf->main_vsi_seid = seid;
14922 		if (printconfig)
14923 			dev_info(&pf->pdev->dev,
14924 				 "pf_seid=%d main_vsi_seid=%d\n",
14925 				 pf->pf_seid, pf->main_vsi_seid);
14926 		break;
14927 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14928 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14929 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14930 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14931 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14932 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14933 		/* ignore these for now */
14934 		break;
14935 	default:
14936 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14937 			 element_type, seid);
14938 		break;
14939 	}
14940 }
14941 
14942 /**
14943  * i40e_fetch_switch_configuration - Get switch config from firmware
14944  * @pf: board private structure
14945  * @printconfig: should we print the contents
14946  *
14947  * Get the current switch configuration from the device and
14948  * extract a few useful SEID values.
14949  **/
14950 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14951 {
14952 	struct i40e_aqc_get_switch_config_resp *sw_config;
14953 	u16 next_seid = 0;
14954 	int ret = 0;
14955 	u8 *aq_buf;
14956 	int i;
14957 
14958 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14959 	if (!aq_buf)
14960 		return -ENOMEM;
14961 
14962 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14963 	do {
14964 		u16 num_reported, num_total;
14965 
14966 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14967 						I40E_AQ_LARGE_BUF,
14968 						&next_seid, NULL);
14969 		if (ret) {
14970 			dev_info(&pf->pdev->dev,
14971 				 "get switch config failed err %s aq_err %s\n",
14972 				 i40e_stat_str(&pf->hw, ret),
14973 				 i40e_aq_str(&pf->hw,
14974 					     pf->hw.aq.asq_last_status));
14975 			kfree(aq_buf);
14976 			return -ENOENT;
14977 		}
14978 
14979 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14980 		num_total = le16_to_cpu(sw_config->header.num_total);
14981 
14982 		if (printconfig)
14983 			dev_info(&pf->pdev->dev,
14984 				 "header: %d reported %d total\n",
14985 				 num_reported, num_total);
14986 
14987 		for (i = 0; i < num_reported; i++) {
14988 			struct i40e_aqc_switch_config_element_resp *ele =
14989 				&sw_config->element[i];
14990 
14991 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14992 						     printconfig);
14993 		}
14994 	} while (next_seid != 0);
14995 
14996 	kfree(aq_buf);
14997 	return ret;
14998 }
14999 
15000 /**
15001  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
15002  * @pf: board private structure
15003  * @reinit: if the Main VSI needs to re-initialized.
15004  * @lock_acquired: indicates whether or not the lock has been acquired
15005  *
15006  * Returns 0 on success, negative value on failure
15007  **/
15008 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
15009 {
15010 	u16 flags = 0;
15011 	int ret;
15012 
15013 	/* find out what's out there already */
15014 	ret = i40e_fetch_switch_configuration(pf, false);
15015 	if (ret) {
15016 		dev_info(&pf->pdev->dev,
15017 			 "couldn't fetch switch config, err %s aq_err %s\n",
15018 			 i40e_stat_str(&pf->hw, ret),
15019 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15020 		return ret;
15021 	}
15022 	i40e_pf_reset_stats(pf);
15023 
15024 	/* set the switch config bit for the whole device to
15025 	 * support limited promisc or true promisc
15026 	 * when user requests promisc. The default is limited
15027 	 * promisc.
15028 	*/
15029 
15030 	if ((pf->hw.pf_id == 0) &&
15031 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
15032 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15033 		pf->last_sw_conf_flags = flags;
15034 	}
15035 
15036 	if (pf->hw.pf_id == 0) {
15037 		u16 valid_flags;
15038 
15039 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15040 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15041 						NULL);
15042 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
15043 			dev_info(&pf->pdev->dev,
15044 				 "couldn't set switch config bits, err %s aq_err %s\n",
15045 				 i40e_stat_str(&pf->hw, ret),
15046 				 i40e_aq_str(&pf->hw,
15047 					     pf->hw.aq.asq_last_status));
15048 			/* not a fatal problem, just keep going */
15049 		}
15050 		pf->last_sw_conf_valid_flags = valid_flags;
15051 	}
15052 
15053 	/* first time setup */
15054 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
15055 		struct i40e_vsi *vsi = NULL;
15056 		u16 uplink_seid;
15057 
15058 		/* Set up the PF VSI associated with the PF's main VSI
15059 		 * that is already in the HW switch
15060 		 */
15061 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
15062 			uplink_seid = pf->veb[pf->lan_veb]->seid;
15063 		else
15064 			uplink_seid = pf->mac_seid;
15065 		if (pf->lan_vsi == I40E_NO_VSI)
15066 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
15067 		else if (reinit)
15068 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
15069 		if (!vsi) {
15070 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15071 			i40e_cloud_filter_exit(pf);
15072 			i40e_fdir_teardown(pf);
15073 			return -EAGAIN;
15074 		}
15075 	} else {
15076 		/* force a reset of TC and queue layout configurations */
15077 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
15078 
15079 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
15080 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
15081 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
15082 	}
15083 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
15084 
15085 	i40e_fdir_sb_setup(pf);
15086 
15087 	/* Setup static PF queue filter control settings */
15088 	ret = i40e_setup_pf_filter_control(pf);
15089 	if (ret) {
15090 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15091 			 ret);
15092 		/* Failure here should not stop continuing other steps */
15093 	}
15094 
15095 	/* enable RSS in the HW, even for only one queue, as the stack can use
15096 	 * the hash
15097 	 */
15098 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
15099 		i40e_pf_config_rss(pf);
15100 
15101 	/* fill in link information and enable LSE reporting */
15102 	i40e_link_event(pf);
15103 
15104 	/* Initialize user-specific link properties */
15105 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
15106 				  I40E_AQ_AN_COMPLETED) ? true : false);
15107 
15108 	i40e_ptp_init(pf);
15109 
15110 	if (!lock_acquired)
15111 		rtnl_lock();
15112 
15113 	/* repopulate tunnel port filters */
15114 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
15115 
15116 	if (!lock_acquired)
15117 		rtnl_unlock();
15118 
15119 	return ret;
15120 }
15121 
15122 /**
15123  * i40e_determine_queue_usage - Work out queue distribution
15124  * @pf: board private structure
15125  **/
15126 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15127 {
15128 	int queues_left;
15129 	int q_max;
15130 
15131 	pf->num_lan_qps = 0;
15132 
15133 	/* Find the max queues to be put into basic use.  We'll always be
15134 	 * using TC0, whether or not DCB is running, and TC0 will get the
15135 	 * big RSS set.
15136 	 */
15137 	queues_left = pf->hw.func_caps.num_tx_qp;
15138 
15139 	if ((queues_left == 1) ||
15140 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
15141 		/* one qp for PF, no queues for anything else */
15142 		queues_left = 0;
15143 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15144 
15145 		/* make sure all the fancies are disabled */
15146 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15147 			       I40E_FLAG_IWARP_ENABLED	|
15148 			       I40E_FLAG_FD_SB_ENABLED	|
15149 			       I40E_FLAG_FD_ATR_ENABLED	|
15150 			       I40E_FLAG_DCB_CAPABLE	|
15151 			       I40E_FLAG_DCB_ENABLED	|
15152 			       I40E_FLAG_SRIOV_ENABLED	|
15153 			       I40E_FLAG_VMDQ_ENABLED);
15154 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15155 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
15156 				  I40E_FLAG_FD_SB_ENABLED |
15157 				  I40E_FLAG_FD_ATR_ENABLED |
15158 				  I40E_FLAG_DCB_CAPABLE))) {
15159 		/* one qp for PF */
15160 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15161 		queues_left -= pf->num_lan_qps;
15162 
15163 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15164 			       I40E_FLAG_IWARP_ENABLED	|
15165 			       I40E_FLAG_FD_SB_ENABLED	|
15166 			       I40E_FLAG_FD_ATR_ENABLED	|
15167 			       I40E_FLAG_DCB_ENABLED	|
15168 			       I40E_FLAG_VMDQ_ENABLED);
15169 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15170 	} else {
15171 		/* Not enough queues for all TCs */
15172 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
15173 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
15174 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
15175 					I40E_FLAG_DCB_ENABLED);
15176 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15177 		}
15178 
15179 		/* limit lan qps to the smaller of qps, cpus or msix */
15180 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15181 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15182 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15183 		pf->num_lan_qps = q_max;
15184 
15185 		queues_left -= pf->num_lan_qps;
15186 	}
15187 
15188 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15189 		if (queues_left > 1) {
15190 			queues_left -= 1; /* save 1 queue for FD */
15191 		} else {
15192 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
15193 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15194 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15195 		}
15196 	}
15197 
15198 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15199 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15200 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15201 					(queues_left / pf->num_vf_qps));
15202 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15203 	}
15204 
15205 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
15206 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15207 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15208 					  (queues_left / pf->num_vmdq_qps));
15209 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15210 	}
15211 
15212 	pf->queues_left = queues_left;
15213 	dev_dbg(&pf->pdev->dev,
15214 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15215 		pf->hw.func_caps.num_tx_qp,
15216 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
15217 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15218 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15219 		queues_left);
15220 }
15221 
15222 /**
15223  * i40e_setup_pf_filter_control - Setup PF static filter control
15224  * @pf: PF to be setup
15225  *
15226  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15227  * settings. If PE/FCoE are enabled then it will also set the per PF
15228  * based filter sizes required for them. It also enables Flow director,
15229  * ethertype and macvlan type filter settings for the pf.
15230  *
15231  * Returns 0 on success, negative on failure
15232  **/
15233 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15234 {
15235 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15236 
15237 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15238 
15239 	/* Flow Director is enabled */
15240 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
15241 		settings->enable_fdir = true;
15242 
15243 	/* Ethtype and MACVLAN filters enabled for PF */
15244 	settings->enable_ethtype = true;
15245 	settings->enable_macvlan = true;
15246 
15247 	if (i40e_set_filter_control(&pf->hw, settings))
15248 		return -ENOENT;
15249 
15250 	return 0;
15251 }
15252 
15253 #define INFO_STRING_LEN 255
15254 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15255 static void i40e_print_features(struct i40e_pf *pf)
15256 {
15257 	struct i40e_hw *hw = &pf->hw;
15258 	char *buf;
15259 	int i;
15260 
15261 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15262 	if (!buf)
15263 		return;
15264 
15265 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15266 #ifdef CONFIG_PCI_IOV
15267 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15268 #endif
15269 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15270 		      pf->hw.func_caps.num_vsis,
15271 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15272 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15273 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15274 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15275 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15276 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15277 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15278 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15279 	}
15280 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15281 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15282 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15283 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15284 	if (pf->flags & I40E_FLAG_PTP)
15285 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15286 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15287 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15288 	else
15289 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15290 
15291 	dev_info(&pf->pdev->dev, "%s\n", buf);
15292 	kfree(buf);
15293 	WARN_ON(i > INFO_STRING_LEN);
15294 }
15295 
15296 /**
15297  * i40e_get_platform_mac_addr - get platform-specific MAC address
15298  * @pdev: PCI device information struct
15299  * @pf: board private structure
15300  *
15301  * Look up the MAC address for the device. First we'll try
15302  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15303  * specific fallback. Otherwise, we'll default to the stored value in
15304  * firmware.
15305  **/
15306 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15307 {
15308 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15309 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15310 }
15311 
15312 /**
15313  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15314  * @fec_cfg: FEC option to set in flags
15315  * @flags: ptr to flags in which we set FEC option
15316  **/
15317 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15318 {
15319 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15320 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15321 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15322 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15323 		*flags |= I40E_FLAG_RS_FEC;
15324 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15325 	}
15326 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15327 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15328 		*flags |= I40E_FLAG_BASE_R_FEC;
15329 		*flags &= ~I40E_FLAG_RS_FEC;
15330 	}
15331 	if (fec_cfg == 0)
15332 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15333 }
15334 
15335 /**
15336  * i40e_check_recovery_mode - check if we are running transition firmware
15337  * @pf: board private structure
15338  *
15339  * Check registers indicating the firmware runs in recovery mode. Sets the
15340  * appropriate driver state.
15341  *
15342  * Returns true if the recovery mode was detected, false otherwise
15343  **/
15344 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15345 {
15346 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15347 
15348 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15349 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15350 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15351 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15352 
15353 		return true;
15354 	}
15355 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15356 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15357 
15358 	return false;
15359 }
15360 
15361 /**
15362  * i40e_pf_loop_reset - perform reset in a loop.
15363  * @pf: board private structure
15364  *
15365  * This function is useful when a NIC is about to enter recovery mode.
15366  * When a NIC's internal data structures are corrupted the NIC's
15367  * firmware is going to enter recovery mode.
15368  * Right after a POR it takes about 7 minutes for firmware to enter
15369  * recovery mode. Until that time a NIC is in some kind of intermediate
15370  * state. After that time period the NIC almost surely enters
15371  * recovery mode. The only way for a driver to detect intermediate
15372  * state is to issue a series of pf-resets and check a return value.
15373  * If a PF reset returns success then the firmware could be in recovery
15374  * mode so the caller of this code needs to check for recovery mode
15375  * if this function returns success. There is a little chance that
15376  * firmware will hang in intermediate state forever.
15377  * Since waiting 7 minutes is quite a lot of time this function waits
15378  * 10 seconds and then gives up by returning an error.
15379  *
15380  * Return 0 on success, negative on failure.
15381  **/
15382 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
15383 {
15384 	/* wait max 10 seconds for PF reset to succeed */
15385 	const unsigned long time_end = jiffies + 10 * HZ;
15386 
15387 	struct i40e_hw *hw = &pf->hw;
15388 	i40e_status ret;
15389 
15390 	ret = i40e_pf_reset(hw);
15391 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15392 		usleep_range(10000, 20000);
15393 		ret = i40e_pf_reset(hw);
15394 	}
15395 
15396 	if (ret == I40E_SUCCESS)
15397 		pf->pfr_count++;
15398 	else
15399 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15400 
15401 	return ret;
15402 }
15403 
15404 /**
15405  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15406  * @pf: board private structure
15407  *
15408  * Check FW registers to determine if FW issued unexpected EMP Reset.
15409  * Every time when unexpected EMP Reset occurs the FW increments
15410  * a counter of unexpected EMP Resets. When the counter reaches 10
15411  * the FW should enter the Recovery mode
15412  *
15413  * Returns true if FW issued unexpected EMP Reset
15414  **/
15415 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15416 {
15417 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15418 			   I40E_GL_FWSTS_FWS1B_MASK;
15419 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15420 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15421 }
15422 
15423 /**
15424  * i40e_handle_resets - handle EMP resets and PF resets
15425  * @pf: board private structure
15426  *
15427  * Handle both EMP resets and PF resets and conclude whether there are
15428  * any issues regarding these resets. If there are any issues then
15429  * generate log entry.
15430  *
15431  * Return 0 if NIC is healthy or negative value when there are issues
15432  * with resets
15433  **/
15434 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
15435 {
15436 	const i40e_status pfr = i40e_pf_loop_reset(pf);
15437 	const bool is_empr = i40e_check_fw_empr(pf);
15438 
15439 	if (is_empr || pfr != I40E_SUCCESS)
15440 		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");
15441 
15442 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15443 }
15444 
15445 /**
15446  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15447  * @pf: board private structure
15448  * @hw: ptr to the hardware info
15449  *
15450  * This function does a minimal setup of all subsystems needed for running
15451  * recovery mode.
15452  *
15453  * Returns 0 on success, negative on failure
15454  **/
15455 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15456 {
15457 	struct i40e_vsi *vsi;
15458 	int err;
15459 	int v_idx;
15460 
15461 	pci_save_state(pf->pdev);
15462 
15463 	/* set up periodic task facility */
15464 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15465 	pf->service_timer_period = HZ;
15466 
15467 	INIT_WORK(&pf->service_task, i40e_service_task);
15468 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15469 
15470 	err = i40e_init_interrupt_scheme(pf);
15471 	if (err)
15472 		goto err_switch_setup;
15473 
15474 	/* The number of VSIs reported by the FW is the minimum guaranteed
15475 	 * to us; HW supports far more and we share the remaining pool with
15476 	 * the other PFs. We allocate space for more than the guarantee with
15477 	 * the understanding that we might not get them all later.
15478 	 */
15479 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15480 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15481 	else
15482 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15483 
15484 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15485 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15486 			  GFP_KERNEL);
15487 	if (!pf->vsi) {
15488 		err = -ENOMEM;
15489 		goto err_switch_setup;
15490 	}
15491 
15492 	/* We allocate one VSI which is needed as absolute minimum
15493 	 * in order to register the netdev
15494 	 */
15495 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15496 	if (v_idx < 0) {
15497 		err = v_idx;
15498 		goto err_switch_setup;
15499 	}
15500 	pf->lan_vsi = v_idx;
15501 	vsi = pf->vsi[v_idx];
15502 	if (!vsi) {
15503 		err = -EFAULT;
15504 		goto err_switch_setup;
15505 	}
15506 	vsi->alloc_queue_pairs = 1;
15507 	err = i40e_config_netdev(vsi);
15508 	if (err)
15509 		goto err_switch_setup;
15510 	err = register_netdev(vsi->netdev);
15511 	if (err)
15512 		goto err_switch_setup;
15513 	vsi->netdev_registered = true;
15514 	i40e_dbg_pf_init(pf);
15515 
15516 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15517 	if (err)
15518 		goto err_switch_setup;
15519 
15520 	/* tell the firmware that we're starting */
15521 	i40e_send_version(pf);
15522 
15523 	/* since everything's happy, start the service_task timer */
15524 	mod_timer(&pf->service_timer,
15525 		  round_jiffies(jiffies + pf->service_timer_period));
15526 
15527 	return 0;
15528 
15529 err_switch_setup:
15530 	i40e_reset_interrupt_capability(pf);
15531 	del_timer_sync(&pf->service_timer);
15532 	i40e_shutdown_adminq(hw);
15533 	iounmap(hw->hw_addr);
15534 	pci_disable_pcie_error_reporting(pf->pdev);
15535 	pci_release_mem_regions(pf->pdev);
15536 	pci_disable_device(pf->pdev);
15537 	kfree(pf);
15538 
15539 	return err;
15540 }
15541 
15542 /**
15543  * i40e_set_subsystem_device_id - set subsystem device id
15544  * @hw: pointer to the hardware info
15545  *
15546  * Set PCI subsystem device id either from a pci_dev structure or
15547  * a specific FW register.
15548  **/
15549 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15550 {
15551 	struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15552 
15553 	hw->subsystem_device_id = pdev->subsystem_device ?
15554 		pdev->subsystem_device :
15555 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15556 }
15557 
15558 /**
15559  * i40e_probe - Device initialization routine
15560  * @pdev: PCI device information struct
15561  * @ent: entry in i40e_pci_tbl
15562  *
15563  * i40e_probe initializes a PF identified by a pci_dev structure.
15564  * The OS initialization, configuring of the PF private structure,
15565  * and a hardware reset occur.
15566  *
15567  * Returns 0 on success, negative on failure
15568  **/
15569 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15570 {
15571 	struct i40e_aq_get_phy_abilities_resp abilities;
15572 #ifdef CONFIG_I40E_DCB
15573 	enum i40e_get_fw_lldp_status_resp lldp_status;
15574 	i40e_status status;
15575 #endif /* CONFIG_I40E_DCB */
15576 	struct i40e_pf *pf;
15577 	struct i40e_hw *hw;
15578 	static u16 pfs_found;
15579 	u16 wol_nvm_bits;
15580 	u16 link_status;
15581 	int err;
15582 	u32 val;
15583 	u32 i;
15584 
15585 	err = pci_enable_device_mem(pdev);
15586 	if (err)
15587 		return err;
15588 
15589 	/* set up for high or low dma */
15590 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15591 	if (err) {
15592 		dev_err(&pdev->dev,
15593 			"DMA configuration failed: 0x%x\n", err);
15594 		goto err_dma;
15595 	}
15596 
15597 	/* set up pci connections */
15598 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15599 	if (err) {
15600 		dev_info(&pdev->dev,
15601 			 "pci_request_selected_regions failed %d\n", err);
15602 		goto err_pci_reg;
15603 	}
15604 
15605 	pci_enable_pcie_error_reporting(pdev);
15606 	pci_set_master(pdev);
15607 
15608 	/* Now that we have a PCI connection, we need to do the
15609 	 * low level device setup.  This is primarily setting up
15610 	 * the Admin Queue structures and then querying for the
15611 	 * device's current profile information.
15612 	 */
15613 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15614 	if (!pf) {
15615 		err = -ENOMEM;
15616 		goto err_pf_alloc;
15617 	}
15618 	pf->next_vsi = 0;
15619 	pf->pdev = pdev;
15620 	set_bit(__I40E_DOWN, pf->state);
15621 
15622 	hw = &pf->hw;
15623 	hw->back = pf;
15624 
15625 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15626 				I40E_MAX_CSR_SPACE);
15627 	/* We believe that the highest register to read is
15628 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15629 	 * is not less than that before mapping to prevent a
15630 	 * kernel panic.
15631 	 */
15632 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15633 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15634 			pf->ioremap_len);
15635 		err = -ENOMEM;
15636 		goto err_ioremap;
15637 	}
15638 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15639 	if (!hw->hw_addr) {
15640 		err = -EIO;
15641 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15642 			 (unsigned int)pci_resource_start(pdev, 0),
15643 			 pf->ioremap_len, err);
15644 		goto err_ioremap;
15645 	}
15646 	hw->vendor_id = pdev->vendor;
15647 	hw->device_id = pdev->device;
15648 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15649 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15650 	i40e_set_subsystem_device_id(hw);
15651 	hw->bus.device = PCI_SLOT(pdev->devfn);
15652 	hw->bus.func = PCI_FUNC(pdev->devfn);
15653 	hw->bus.bus_id = pdev->bus->number;
15654 	pf->instance = pfs_found;
15655 
15656 	/* Select something other than the 802.1ad ethertype for the
15657 	 * switch to use internally and drop on ingress.
15658 	 */
15659 	hw->switch_tag = 0xffff;
15660 	hw->first_tag = ETH_P_8021AD;
15661 	hw->second_tag = ETH_P_8021Q;
15662 
15663 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15664 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15665 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15666 
15667 	/* set up the locks for the AQ, do this only once in probe
15668 	 * and destroy them only once in remove
15669 	 */
15670 	mutex_init(&hw->aq.asq_mutex);
15671 	mutex_init(&hw->aq.arq_mutex);
15672 
15673 	pf->msg_enable = netif_msg_init(debug,
15674 					NETIF_MSG_DRV |
15675 					NETIF_MSG_PROBE |
15676 					NETIF_MSG_LINK);
15677 	if (debug < -1)
15678 		pf->hw.debug_mask = debug;
15679 
15680 	/* do a special CORER for clearing PXE mode once at init */
15681 	if (hw->revision_id == 0 &&
15682 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15683 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15684 		i40e_flush(hw);
15685 		msleep(200);
15686 		pf->corer_count++;
15687 
15688 		i40e_clear_pxe_mode(hw);
15689 	}
15690 
15691 	/* Reset here to make sure all is clean and to define PF 'n' */
15692 	i40e_clear_hw(hw);
15693 
15694 	err = i40e_set_mac_type(hw);
15695 	if (err) {
15696 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15697 			 err);
15698 		goto err_pf_reset;
15699 	}
15700 
15701 	err = i40e_handle_resets(pf);
15702 	if (err)
15703 		goto err_pf_reset;
15704 
15705 	i40e_check_recovery_mode(pf);
15706 
15707 	if (is_kdump_kernel()) {
15708 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15709 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15710 	} else {
15711 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15712 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15713 	}
15714 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15715 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15716 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15717 
15718 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15719 		 "%s-%s:misc",
15720 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15721 
15722 	err = i40e_init_shared_code(hw);
15723 	if (err) {
15724 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15725 			 err);
15726 		goto err_pf_reset;
15727 	}
15728 
15729 	/* set up a default setting for link flow control */
15730 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15731 
15732 	err = i40e_init_adminq(hw);
15733 	if (err) {
15734 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15735 			dev_info(&pdev->dev,
15736 				 "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",
15737 				 hw->aq.api_maj_ver,
15738 				 hw->aq.api_min_ver,
15739 				 I40E_FW_API_VERSION_MAJOR,
15740 				 I40E_FW_MINOR_VERSION(hw));
15741 		else
15742 			dev_info(&pdev->dev,
15743 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15744 
15745 		goto err_pf_reset;
15746 	}
15747 	i40e_get_oem_version(hw);
15748 
15749 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15750 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15751 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15752 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15753 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15754 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15755 
15756 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15757 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15758 		dev_dbg(&pdev->dev,
15759 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15760 			 hw->aq.api_maj_ver,
15761 			 hw->aq.api_min_ver,
15762 			 I40E_FW_API_VERSION_MAJOR,
15763 			 I40E_FW_MINOR_VERSION(hw));
15764 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15765 		dev_info(&pdev->dev,
15766 			 "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",
15767 			 hw->aq.api_maj_ver,
15768 			 hw->aq.api_min_ver,
15769 			 I40E_FW_API_VERSION_MAJOR,
15770 			 I40E_FW_MINOR_VERSION(hw));
15771 
15772 	i40e_verify_eeprom(pf);
15773 
15774 	/* Rev 0 hardware was never productized */
15775 	if (hw->revision_id < 1)
15776 		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");
15777 
15778 	i40e_clear_pxe_mode(hw);
15779 
15780 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15781 	if (err)
15782 		goto err_adminq_setup;
15783 
15784 	err = i40e_sw_init(pf);
15785 	if (err) {
15786 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15787 		goto err_sw_init;
15788 	}
15789 
15790 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15791 		return i40e_init_recovery_mode(pf, hw);
15792 
15793 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15794 				hw->func_caps.num_rx_qp, 0, 0);
15795 	if (err) {
15796 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15797 		goto err_init_lan_hmc;
15798 	}
15799 
15800 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15801 	if (err) {
15802 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15803 		err = -ENOENT;
15804 		goto err_configure_lan_hmc;
15805 	}
15806 
15807 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15808 	 * Ignore error return codes because if it was already disabled via
15809 	 * hardware settings this will fail
15810 	 */
15811 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15812 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15813 		i40e_aq_stop_lldp(hw, true, false, NULL);
15814 	}
15815 
15816 	/* allow a platform config to override the HW addr */
15817 	i40e_get_platform_mac_addr(pdev, pf);
15818 
15819 	if (!is_valid_ether_addr(hw->mac.addr)) {
15820 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15821 		err = -EIO;
15822 		goto err_mac_addr;
15823 	}
15824 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15825 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15826 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15827 	if (is_valid_ether_addr(hw->mac.port_addr))
15828 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15829 
15830 	i40e_ptp_alloc_pins(pf);
15831 	pci_set_drvdata(pdev, pf);
15832 	pci_save_state(pdev);
15833 
15834 #ifdef CONFIG_I40E_DCB
15835 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15836 	(!status &&
15837 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15838 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15839 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15840 	dev_info(&pdev->dev,
15841 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15842 			"FW LLDP is disabled\n" :
15843 			"FW LLDP is enabled\n");
15844 
15845 	/* Enable FW to write default DCB config on link-up */
15846 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15847 
15848 	err = i40e_init_pf_dcb(pf);
15849 	if (err) {
15850 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15851 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15852 		/* Continue without DCB enabled */
15853 	}
15854 #endif /* CONFIG_I40E_DCB */
15855 
15856 	/* set up periodic task facility */
15857 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15858 	pf->service_timer_period = HZ;
15859 
15860 	INIT_WORK(&pf->service_task, i40e_service_task);
15861 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15862 
15863 	/* NVM bit on means WoL disabled for the port */
15864 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15865 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15866 		pf->wol_en = false;
15867 	else
15868 		pf->wol_en = true;
15869 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15870 
15871 	/* set up the main switch operations */
15872 	i40e_determine_queue_usage(pf);
15873 	err = i40e_init_interrupt_scheme(pf);
15874 	if (err)
15875 		goto err_switch_setup;
15876 
15877 	/* Reduce Tx and Rx pairs for kdump
15878 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15879 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15880 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15881 	 */
15882 	if (is_kdump_kernel())
15883 		pf->num_lan_msix = 1;
15884 
15885 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15886 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15887 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15888 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15889 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15890 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15891 						    UDP_TUNNEL_TYPE_GENEVE;
15892 
15893 	/* The number of VSIs reported by the FW is the minimum guaranteed
15894 	 * to us; HW supports far more and we share the remaining pool with
15895 	 * the other PFs. We allocate space for more than the guarantee with
15896 	 * the understanding that we might not get them all later.
15897 	 */
15898 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15899 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15900 	else
15901 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15902 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15903 		dev_warn(&pf->pdev->dev,
15904 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15905 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15906 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15907 	}
15908 
15909 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15910 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15911 			  GFP_KERNEL);
15912 	if (!pf->vsi) {
15913 		err = -ENOMEM;
15914 		goto err_switch_setup;
15915 	}
15916 
15917 #ifdef CONFIG_PCI_IOV
15918 	/* prep for VF support */
15919 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15920 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15921 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15922 		if (pci_num_vf(pdev))
15923 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15924 	}
15925 #endif
15926 	err = i40e_setup_pf_switch(pf, false, false);
15927 	if (err) {
15928 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15929 		goto err_vsis;
15930 	}
15931 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15932 
15933 	/* if FDIR VSI was set up, start it now */
15934 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15935 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15936 			i40e_vsi_open(pf->vsi[i]);
15937 			break;
15938 		}
15939 	}
15940 
15941 	/* The driver only wants link up/down and module qualification
15942 	 * reports from firmware.  Note the negative logic.
15943 	 */
15944 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15945 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15946 					 I40E_AQ_EVENT_MEDIA_NA |
15947 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15948 	if (err)
15949 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15950 			 i40e_stat_str(&pf->hw, err),
15951 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15952 
15953 	/* Reconfigure hardware for allowing smaller MSS in the case
15954 	 * of TSO, so that we avoid the MDD being fired and causing
15955 	 * a reset in the case of small MSS+TSO.
15956 	 */
15957 	val = rd32(hw, I40E_REG_MSS);
15958 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15959 		val &= ~I40E_REG_MSS_MIN_MASK;
15960 		val |= I40E_64BYTE_MSS;
15961 		wr32(hw, I40E_REG_MSS, val);
15962 	}
15963 
15964 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15965 		msleep(75);
15966 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15967 		if (err)
15968 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15969 				 i40e_stat_str(&pf->hw, err),
15970 				 i40e_aq_str(&pf->hw,
15971 					     pf->hw.aq.asq_last_status));
15972 	}
15973 	/* The main driver is (mostly) up and happy. We need to set this state
15974 	 * before setting up the misc vector or we get a race and the vector
15975 	 * ends up disabled forever.
15976 	 */
15977 	clear_bit(__I40E_DOWN, pf->state);
15978 
15979 	/* In case of MSIX we are going to setup the misc vector right here
15980 	 * to handle admin queue events etc. In case of legacy and MSI
15981 	 * the misc functionality and queue processing is combined in
15982 	 * the same vector and that gets setup at open.
15983 	 */
15984 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15985 		err = i40e_setup_misc_vector(pf);
15986 		if (err) {
15987 			dev_info(&pdev->dev,
15988 				 "setup of misc vector failed: %d\n", err);
15989 			i40e_cloud_filter_exit(pf);
15990 			i40e_fdir_teardown(pf);
15991 			goto err_vsis;
15992 		}
15993 	}
15994 
15995 #ifdef CONFIG_PCI_IOV
15996 	/* prep for VF support */
15997 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15998 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15999 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16000 		/* disable link interrupts for VFs */
16001 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
16002 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
16003 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
16004 		i40e_flush(hw);
16005 
16006 		if (pci_num_vf(pdev)) {
16007 			dev_info(&pdev->dev,
16008 				 "Active VFs found, allocating resources.\n");
16009 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
16010 			if (err)
16011 				dev_info(&pdev->dev,
16012 					 "Error %d allocating resources for existing VFs\n",
16013 					 err);
16014 		}
16015 	}
16016 #endif /* CONFIG_PCI_IOV */
16017 
16018 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16019 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16020 						      pf->num_iwarp_msix,
16021 						      I40E_IWARP_IRQ_PILE_ID);
16022 		if (pf->iwarp_base_vector < 0) {
16023 			dev_info(&pdev->dev,
16024 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
16025 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
16026 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
16027 		}
16028 	}
16029 
16030 	i40e_dbg_pf_init(pf);
16031 
16032 	/* tell the firmware that we're starting */
16033 	i40e_send_version(pf);
16034 
16035 	/* since everything's happy, start the service_task timer */
16036 	mod_timer(&pf->service_timer,
16037 		  round_jiffies(jiffies + pf->service_timer_period));
16038 
16039 	/* add this PF to client device list and launch a client service task */
16040 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16041 		err = i40e_lan_add_device(pf);
16042 		if (err)
16043 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16044 				 err);
16045 	}
16046 
16047 #define PCI_SPEED_SIZE 8
16048 #define PCI_WIDTH_SIZE 8
16049 	/* Devices on the IOSF bus do not have this information
16050 	 * and will report PCI Gen 1 x 1 by default so don't bother
16051 	 * checking them.
16052 	 */
16053 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
16054 		char speed[PCI_SPEED_SIZE] = "Unknown";
16055 		char width[PCI_WIDTH_SIZE] = "Unknown";
16056 
16057 		/* Get the negotiated link width and speed from PCI config
16058 		 * space
16059 		 */
16060 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16061 					  &link_status);
16062 
16063 		i40e_set_pci_config_data(hw, link_status);
16064 
16065 		switch (hw->bus.speed) {
16066 		case i40e_bus_speed_8000:
16067 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16068 		case i40e_bus_speed_5000:
16069 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16070 		case i40e_bus_speed_2500:
16071 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16072 		default:
16073 			break;
16074 		}
16075 		switch (hw->bus.width) {
16076 		case i40e_bus_width_pcie_x8:
16077 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16078 		case i40e_bus_width_pcie_x4:
16079 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16080 		case i40e_bus_width_pcie_x2:
16081 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16082 		case i40e_bus_width_pcie_x1:
16083 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16084 		default:
16085 			break;
16086 		}
16087 
16088 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16089 			 speed, width);
16090 
16091 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16092 		    hw->bus.speed < i40e_bus_speed_8000) {
16093 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16094 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16095 		}
16096 	}
16097 
16098 	/* get the requested speeds from the fw */
16099 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16100 	if (err)
16101 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
16102 			i40e_stat_str(&pf->hw, err),
16103 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16104 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16105 
16106 	/* set the FEC config due to the board capabilities */
16107 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
16108 
16109 	/* get the supported phy types from the fw */
16110 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16111 	if (err)
16112 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
16113 			i40e_stat_str(&pf->hw, err),
16114 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16115 
16116 	/* make sure the MFS hasn't been set lower than the default */
16117 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16118 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
16119 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
16120 	if (val < MAX_FRAME_SIZE_DEFAULT)
16121 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
16122 			 i, val);
16123 
16124 	/* Add a filter to drop all Flow control frames from any VSI from being
16125 	 * transmitted. By doing so we stop a malicious VF from sending out
16126 	 * PAUSE or PFC frames and potentially controlling traffic for other
16127 	 * PF/VF VSIs.
16128 	 * The FW can still send Flow control frames if enabled.
16129 	 */
16130 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16131 						       pf->main_vsi_seid);
16132 
16133 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16134 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16135 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
16136 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16137 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
16138 	/* print a string summarizing features */
16139 	i40e_print_features(pf);
16140 
16141 	return 0;
16142 
16143 	/* Unwind what we've done if something failed in the setup */
16144 err_vsis:
16145 	set_bit(__I40E_DOWN, pf->state);
16146 	i40e_clear_interrupt_scheme(pf);
16147 	kfree(pf->vsi);
16148 err_switch_setup:
16149 	i40e_reset_interrupt_capability(pf);
16150 	del_timer_sync(&pf->service_timer);
16151 err_mac_addr:
16152 err_configure_lan_hmc:
16153 	(void)i40e_shutdown_lan_hmc(hw);
16154 err_init_lan_hmc:
16155 	kfree(pf->qp_pile);
16156 err_sw_init:
16157 err_adminq_setup:
16158 err_pf_reset:
16159 	iounmap(hw->hw_addr);
16160 err_ioremap:
16161 	kfree(pf);
16162 err_pf_alloc:
16163 	pci_disable_pcie_error_reporting(pdev);
16164 	pci_release_mem_regions(pdev);
16165 err_pci_reg:
16166 err_dma:
16167 	pci_disable_device(pdev);
16168 	return err;
16169 }
16170 
16171 /**
16172  * i40e_remove - Device removal routine
16173  * @pdev: PCI device information struct
16174  *
16175  * i40e_remove is called by the PCI subsystem to alert the driver
16176  * that is should release a PCI device.  This could be caused by a
16177  * Hot-Plug event, or because the driver is going to be removed from
16178  * memory.
16179  **/
16180 static void i40e_remove(struct pci_dev *pdev)
16181 {
16182 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16183 	struct i40e_hw *hw = &pf->hw;
16184 	i40e_status ret_code;
16185 	int i;
16186 
16187 	i40e_dbg_pf_exit(pf);
16188 
16189 	i40e_ptp_stop(pf);
16190 
16191 	/* Disable RSS in hw */
16192 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16193 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16194 
16195 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16196 	 * flags, once they are set, i40e_rebuild should not be called as
16197 	 * i40e_prep_for_reset always returns early.
16198 	 */
16199 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16200 		usleep_range(1000, 2000);
16201 	set_bit(__I40E_IN_REMOVE, pf->state);
16202 
16203 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
16204 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16205 		i40e_free_vfs(pf);
16206 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
16207 	}
16208 	/* no more scheduling of any task */
16209 	set_bit(__I40E_SUSPENDED, pf->state);
16210 	set_bit(__I40E_DOWN, pf->state);
16211 	if (pf->service_timer.function)
16212 		del_timer_sync(&pf->service_timer);
16213 	if (pf->service_task.func)
16214 		cancel_work_sync(&pf->service_task);
16215 
16216 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16217 		struct i40e_vsi *vsi = pf->vsi[0];
16218 
16219 		/* We know that we have allocated only one vsi for this PF,
16220 		 * it was just for registering netdevice, so the interface
16221 		 * could be visible in the 'ifconfig' output
16222 		 */
16223 		unregister_netdev(vsi->netdev);
16224 		free_netdev(vsi->netdev);
16225 
16226 		goto unmap;
16227 	}
16228 
16229 	/* Client close must be called explicitly here because the timer
16230 	 * has been stopped.
16231 	 */
16232 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16233 
16234 	i40e_fdir_teardown(pf);
16235 
16236 	/* If there is a switch structure or any orphans, remove them.
16237 	 * This will leave only the PF's VSI remaining.
16238 	 */
16239 	for (i = 0; i < I40E_MAX_VEB; i++) {
16240 		if (!pf->veb[i])
16241 			continue;
16242 
16243 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16244 		    pf->veb[i]->uplink_seid == 0)
16245 			i40e_switch_branch_release(pf->veb[i]);
16246 	}
16247 
16248 	/* Now we can shutdown the PF's VSI, just before we kill
16249 	 * adminq and hmc.
16250 	 */
16251 	if (pf->vsi[pf->lan_vsi])
16252 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
16253 
16254 	i40e_cloud_filter_exit(pf);
16255 
16256 	/* remove attached clients */
16257 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16258 		ret_code = i40e_lan_del_device(pf);
16259 		if (ret_code)
16260 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16261 				 ret_code);
16262 	}
16263 
16264 	/* shutdown and destroy the HMC */
16265 	if (hw->hmc.hmc_obj) {
16266 		ret_code = i40e_shutdown_lan_hmc(hw);
16267 		if (ret_code)
16268 			dev_warn(&pdev->dev,
16269 				 "Failed to destroy the HMC resources: %d\n",
16270 				 ret_code);
16271 	}
16272 
16273 unmap:
16274 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16275 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16276 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16277 		free_irq(pf->pdev->irq, pf);
16278 
16279 	/* shutdown the adminq */
16280 	i40e_shutdown_adminq(hw);
16281 
16282 	/* destroy the locks only once, here */
16283 	mutex_destroy(&hw->aq.arq_mutex);
16284 	mutex_destroy(&hw->aq.asq_mutex);
16285 
16286 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16287 	rtnl_lock();
16288 	i40e_clear_interrupt_scheme(pf);
16289 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16290 		if (pf->vsi[i]) {
16291 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16292 				i40e_vsi_clear_rings(pf->vsi[i]);
16293 			i40e_vsi_clear(pf->vsi[i]);
16294 			pf->vsi[i] = NULL;
16295 		}
16296 	}
16297 	rtnl_unlock();
16298 
16299 	for (i = 0; i < I40E_MAX_VEB; i++) {
16300 		kfree(pf->veb[i]);
16301 		pf->veb[i] = NULL;
16302 	}
16303 
16304 	kfree(pf->qp_pile);
16305 	kfree(pf->vsi);
16306 
16307 	iounmap(hw->hw_addr);
16308 	kfree(pf);
16309 	pci_release_mem_regions(pdev);
16310 
16311 	pci_disable_pcie_error_reporting(pdev);
16312 	pci_disable_device(pdev);
16313 }
16314 
16315 /**
16316  * i40e_pci_error_detected - warning that something funky happened in PCI land
16317  * @pdev: PCI device information struct
16318  * @error: the type of PCI error
16319  *
16320  * Called to warn that something happened and the error handling steps
16321  * are in progress.  Allows the driver to quiesce things, be ready for
16322  * remediation.
16323  **/
16324 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16325 						pci_channel_state_t error)
16326 {
16327 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16328 
16329 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16330 
16331 	if (!pf) {
16332 		dev_info(&pdev->dev,
16333 			 "Cannot recover - error happened during device probe\n");
16334 		return PCI_ERS_RESULT_DISCONNECT;
16335 	}
16336 
16337 	/* shutdown all operations */
16338 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16339 		i40e_prep_for_reset(pf);
16340 
16341 	/* Request a slot reset */
16342 	return PCI_ERS_RESULT_NEED_RESET;
16343 }
16344 
16345 /**
16346  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16347  * @pdev: PCI device information struct
16348  *
16349  * Called to find if the driver can work with the device now that
16350  * the pci slot has been reset.  If a basic connection seems good
16351  * (registers are readable and have sane content) then return a
16352  * happy little PCI_ERS_RESULT_xxx.
16353  **/
16354 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16355 {
16356 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16357 	pci_ers_result_t result;
16358 	u32 reg;
16359 
16360 	dev_dbg(&pdev->dev, "%s\n", __func__);
16361 	if (pci_enable_device_mem(pdev)) {
16362 		dev_info(&pdev->dev,
16363 			 "Cannot re-enable PCI device after reset.\n");
16364 		result = PCI_ERS_RESULT_DISCONNECT;
16365 	} else {
16366 		pci_set_master(pdev);
16367 		pci_restore_state(pdev);
16368 		pci_save_state(pdev);
16369 		pci_wake_from_d3(pdev, false);
16370 
16371 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16372 		if (reg == 0)
16373 			result = PCI_ERS_RESULT_RECOVERED;
16374 		else
16375 			result = PCI_ERS_RESULT_DISCONNECT;
16376 	}
16377 
16378 	return result;
16379 }
16380 
16381 /**
16382  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16383  * @pdev: PCI device information struct
16384  */
16385 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16386 {
16387 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16388 
16389 	i40e_prep_for_reset(pf);
16390 }
16391 
16392 /**
16393  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16394  * @pdev: PCI device information struct
16395  */
16396 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16397 {
16398 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16399 
16400 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16401 		return;
16402 
16403 	i40e_reset_and_rebuild(pf, false, false);
16404 }
16405 
16406 /**
16407  * i40e_pci_error_resume - restart operations after PCI error recovery
16408  * @pdev: PCI device information struct
16409  *
16410  * Called to allow the driver to bring things back up after PCI error
16411  * and/or reset recovery has finished.
16412  **/
16413 static void i40e_pci_error_resume(struct pci_dev *pdev)
16414 {
16415 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16416 
16417 	dev_dbg(&pdev->dev, "%s\n", __func__);
16418 	if (test_bit(__I40E_SUSPENDED, pf->state))
16419 		return;
16420 
16421 	i40e_handle_reset_warning(pf, false);
16422 }
16423 
16424 /**
16425  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16426  * using the mac_address_write admin q function
16427  * @pf: pointer to i40e_pf struct
16428  **/
16429 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16430 {
16431 	struct i40e_hw *hw = &pf->hw;
16432 	i40e_status ret;
16433 	u8 mac_addr[6];
16434 	u16 flags = 0;
16435 
16436 	/* Get current MAC address in case it's an LAA */
16437 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16438 		ether_addr_copy(mac_addr,
16439 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16440 	} else {
16441 		dev_err(&pf->pdev->dev,
16442 			"Failed to retrieve MAC address; using default\n");
16443 		ether_addr_copy(mac_addr, hw->mac.addr);
16444 	}
16445 
16446 	/* The FW expects the mac address write cmd to first be called with
16447 	 * one of these flags before calling it again with the multicast
16448 	 * enable flags.
16449 	 */
16450 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16451 
16452 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16453 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16454 
16455 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16456 	if (ret) {
16457 		dev_err(&pf->pdev->dev,
16458 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16459 		return;
16460 	}
16461 
16462 	flags = I40E_AQC_MC_MAG_EN
16463 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16464 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16465 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16466 	if (ret)
16467 		dev_err(&pf->pdev->dev,
16468 			"Failed to enable Multicast Magic Packet wake up\n");
16469 }
16470 
16471 /**
16472  * i40e_shutdown - PCI callback for shutting down
16473  * @pdev: PCI device information struct
16474  **/
16475 static void i40e_shutdown(struct pci_dev *pdev)
16476 {
16477 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16478 	struct i40e_hw *hw = &pf->hw;
16479 
16480 	set_bit(__I40E_SUSPENDED, pf->state);
16481 	set_bit(__I40E_DOWN, pf->state);
16482 
16483 	del_timer_sync(&pf->service_timer);
16484 	cancel_work_sync(&pf->service_task);
16485 	i40e_cloud_filter_exit(pf);
16486 	i40e_fdir_teardown(pf);
16487 
16488 	/* Client close must be called explicitly here because the timer
16489 	 * has been stopped.
16490 	 */
16491 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16492 
16493 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16494 		i40e_enable_mc_magic_wake(pf);
16495 
16496 	i40e_prep_for_reset(pf);
16497 
16498 	wr32(hw, I40E_PFPM_APM,
16499 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16500 	wr32(hw, I40E_PFPM_WUFC,
16501 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16502 
16503 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16504 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16505 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16506 		free_irq(pf->pdev->irq, pf);
16507 
16508 	/* Since we're going to destroy queues during the
16509 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16510 	 * whole section
16511 	 */
16512 	rtnl_lock();
16513 	i40e_clear_interrupt_scheme(pf);
16514 	rtnl_unlock();
16515 
16516 	if (system_state == SYSTEM_POWER_OFF) {
16517 		pci_wake_from_d3(pdev, pf->wol_en);
16518 		pci_set_power_state(pdev, PCI_D3hot);
16519 	}
16520 }
16521 
16522 /**
16523  * i40e_suspend - PM callback for moving to D3
16524  * @dev: generic device information structure
16525  **/
16526 static int __maybe_unused i40e_suspend(struct device *dev)
16527 {
16528 	struct i40e_pf *pf = dev_get_drvdata(dev);
16529 	struct i40e_hw *hw = &pf->hw;
16530 
16531 	/* If we're already suspended, then there is nothing to do */
16532 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16533 		return 0;
16534 
16535 	set_bit(__I40E_DOWN, pf->state);
16536 
16537 	/* Ensure service task will not be running */
16538 	del_timer_sync(&pf->service_timer);
16539 	cancel_work_sync(&pf->service_task);
16540 
16541 	/* Client close must be called explicitly here because the timer
16542 	 * has been stopped.
16543 	 */
16544 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16545 
16546 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16547 		i40e_enable_mc_magic_wake(pf);
16548 
16549 	/* Since we're going to destroy queues during the
16550 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16551 	 * whole section
16552 	 */
16553 	rtnl_lock();
16554 
16555 	i40e_prep_for_reset(pf);
16556 
16557 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16558 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16559 
16560 	/* Clear the interrupt scheme and release our IRQs so that the system
16561 	 * can safely hibernate even when there are a large number of CPUs.
16562 	 * Otherwise hibernation might fail when mapping all the vectors back
16563 	 * to CPU0.
16564 	 */
16565 	i40e_clear_interrupt_scheme(pf);
16566 
16567 	rtnl_unlock();
16568 
16569 	return 0;
16570 }
16571 
16572 /**
16573  * i40e_resume - PM callback for waking up from D3
16574  * @dev: generic device information structure
16575  **/
16576 static int __maybe_unused i40e_resume(struct device *dev)
16577 {
16578 	struct i40e_pf *pf = dev_get_drvdata(dev);
16579 	int err;
16580 
16581 	/* If we're not suspended, then there is nothing to do */
16582 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16583 		return 0;
16584 
16585 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16586 	 * since we're going to be restoring queues
16587 	 */
16588 	rtnl_lock();
16589 
16590 	/* We cleared the interrupt scheme when we suspended, so we need to
16591 	 * restore it now to resume device functionality.
16592 	 */
16593 	err = i40e_restore_interrupt_scheme(pf);
16594 	if (err) {
16595 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16596 			err);
16597 	}
16598 
16599 	clear_bit(__I40E_DOWN, pf->state);
16600 	i40e_reset_and_rebuild(pf, false, true);
16601 
16602 	rtnl_unlock();
16603 
16604 	/* Clear suspended state last after everything is recovered */
16605 	clear_bit(__I40E_SUSPENDED, pf->state);
16606 
16607 	/* Restart the service task */
16608 	mod_timer(&pf->service_timer,
16609 		  round_jiffies(jiffies + pf->service_timer_period));
16610 
16611 	return 0;
16612 }
16613 
16614 static const struct pci_error_handlers i40e_err_handler = {
16615 	.error_detected = i40e_pci_error_detected,
16616 	.slot_reset = i40e_pci_error_slot_reset,
16617 	.reset_prepare = i40e_pci_error_reset_prepare,
16618 	.reset_done = i40e_pci_error_reset_done,
16619 	.resume = i40e_pci_error_resume,
16620 };
16621 
16622 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16623 
16624 static struct pci_driver i40e_driver = {
16625 	.name     = i40e_driver_name,
16626 	.id_table = i40e_pci_tbl,
16627 	.probe    = i40e_probe,
16628 	.remove   = i40e_remove,
16629 	.driver   = {
16630 		.pm = &i40e_pm_ops,
16631 	},
16632 	.shutdown = i40e_shutdown,
16633 	.err_handler = &i40e_err_handler,
16634 	.sriov_configure = i40e_pci_sriov_configure,
16635 };
16636 
16637 /**
16638  * i40e_init_module - Driver registration routine
16639  *
16640  * i40e_init_module is the first routine called when the driver is
16641  * loaded. All it does is register with the PCI subsystem.
16642  **/
16643 static int __init i40e_init_module(void)
16644 {
16645 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16646 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16647 
16648 	/* There is no need to throttle the number of active tasks because
16649 	 * each device limits its own task using a state bit for scheduling
16650 	 * the service task, and the device tasks do not interfere with each
16651 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16652 	 * since we need to be able to guarantee forward progress even under
16653 	 * memory pressure.
16654 	 */
16655 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16656 	if (!i40e_wq) {
16657 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16658 		return -ENOMEM;
16659 	}
16660 
16661 	i40e_dbg_init();
16662 	return pci_register_driver(&i40e_driver);
16663 }
16664 module_init(i40e_init_module);
16665 
16666 /**
16667  * i40e_exit_module - Driver exit cleanup routine
16668  *
16669  * i40e_exit_module is called just before the driver is removed
16670  * from memory.
16671  **/
16672 static void __exit i40e_exit_module(void)
16673 {
16674 	pci_unregister_driver(&i40e_driver);
16675 	destroy_workqueue(i40e_wq);
16676 	ida_destroy(&i40e_client_ida);
16677 	i40e_dbg_exit();
16678 }
16679 module_exit(i40e_exit_module);
16680