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_list *ha_list;
108 	struct netdev_hw_addr *ha;
109 
110 	if (!f || !netdev)
111 		return;
112 
113 	if (is_unicast_ether_addr(f->macaddr) || is_link_local_ether_addr(f->macaddr))
114 		ha_list = &netdev->uc;
115 	else
116 		ha_list = &netdev->mc;
117 
118 	netdev_hw_addr_list_for_each(ha, ha_list) {
119 		if (ether_addr_equal(ha->addr, f->macaddr)) {
120 			ha->refcount += delta;
121 			if (ha->refcount <= 0)
122 				ha->refcount = 1;
123 			break;
124 		}
125 	}
126 }
127 
128 /**
129  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
130  * @hw:   pointer to the HW structure
131  * @mem:  ptr to mem struct to fill out
132  * @size: size of memory requested
133  * @alignment: what to align the allocation to
134  **/
135 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
136 			    u64 size, u32 alignment)
137 {
138 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
139 
140 	mem->size = ALIGN(size, alignment);
141 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
142 				     GFP_KERNEL);
143 	if (!mem->va)
144 		return -ENOMEM;
145 
146 	return 0;
147 }
148 
149 /**
150  * i40e_free_dma_mem_d - OS specific memory free for shared code
151  * @hw:   pointer to the HW structure
152  * @mem:  ptr to mem struct to free
153  **/
154 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
155 {
156 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
157 
158 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
159 	mem->va = NULL;
160 	mem->pa = 0;
161 	mem->size = 0;
162 
163 	return 0;
164 }
165 
166 /**
167  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
168  * @hw:   pointer to the HW structure
169  * @mem:  ptr to mem struct to fill out
170  * @size: size of memory requested
171  **/
172 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
173 			     u32 size)
174 {
175 	mem->size = size;
176 	mem->va = kzalloc(size, GFP_KERNEL);
177 
178 	if (!mem->va)
179 		return -ENOMEM;
180 
181 	return 0;
182 }
183 
184 /**
185  * i40e_free_virt_mem_d - OS specific memory free for shared code
186  * @hw:   pointer to the HW structure
187  * @mem:  ptr to mem struct to free
188  **/
189 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
190 {
191 	/* it's ok to kfree a NULL pointer */
192 	kfree(mem->va);
193 	mem->va = NULL;
194 	mem->size = 0;
195 
196 	return 0;
197 }
198 
199 /**
200  * i40e_get_lump - find a lump of free generic resource
201  * @pf: board private structure
202  * @pile: the pile of resource to search
203  * @needed: the number of items needed
204  * @id: an owner id to stick on the items assigned
205  *
206  * Returns the base item index of the lump, or negative for error
207  **/
208 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
209 			 u16 needed, u16 id)
210 {
211 	int ret = -ENOMEM;
212 	int i, j;
213 
214 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
215 		dev_info(&pf->pdev->dev,
216 			 "param err: pile=%s needed=%d id=0x%04x\n",
217 			 pile ? "<valid>" : "<null>", needed, id);
218 		return -EINVAL;
219 	}
220 
221 	/* Allocate last queue in the pile for FDIR VSI queue
222 	 * so it doesn't fragment the qp_pile
223 	 */
224 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
225 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
226 			dev_err(&pf->pdev->dev,
227 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
228 				pile->num_entries - 1);
229 			return -ENOMEM;
230 		}
231 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
232 		return pile->num_entries - 1;
233 	}
234 
235 	i = 0;
236 	while (i < pile->num_entries) {
237 		/* skip already allocated entries */
238 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
239 			i++;
240 			continue;
241 		}
242 
243 		/* do we have enough in this lump? */
244 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
245 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
246 				break;
247 		}
248 
249 		if (j == needed) {
250 			/* there was enough, so assign it to the requestor */
251 			for (j = 0; j < needed; j++)
252 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
253 			ret = i;
254 			break;
255 		}
256 
257 		/* not enough, so skip over it and continue looking */
258 		i += j;
259 	}
260 
261 	return ret;
262 }
263 
264 /**
265  * i40e_put_lump - return a lump of generic resource
266  * @pile: the pile of resource to search
267  * @index: the base item index
268  * @id: the owner id of the items assigned
269  *
270  * Returns the count of items in the lump
271  **/
272 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
273 {
274 	int valid_id = (id | I40E_PILE_VALID_BIT);
275 	int count = 0;
276 	u16 i;
277 
278 	if (!pile || index >= pile->num_entries)
279 		return -EINVAL;
280 
281 	for (i = index;
282 	     i < pile->num_entries && pile->list[i] == valid_id;
283 	     i++) {
284 		pile->list[i] = 0;
285 		count++;
286 	}
287 
288 
289 	return count;
290 }
291 
292 /**
293  * i40e_find_vsi_from_id - searches for the vsi with the given id
294  * @pf: the pf structure to search for the vsi
295  * @id: id of the vsi it is searching for
296  **/
297 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
298 {
299 	int i;
300 
301 	for (i = 0; i < pf->num_alloc_vsi; i++)
302 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
303 			return pf->vsi[i];
304 
305 	return NULL;
306 }
307 
308 /**
309  * i40e_service_event_schedule - Schedule the service task to wake up
310  * @pf: board private structure
311  *
312  * If not already scheduled, this puts the task into the work queue
313  **/
314 void i40e_service_event_schedule(struct i40e_pf *pf)
315 {
316 	if ((!test_bit(__I40E_DOWN, pf->state) &&
317 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
318 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
319 		queue_work(i40e_wq, &pf->service_task);
320 }
321 
322 /**
323  * i40e_tx_timeout - Respond to a Tx Hang
324  * @netdev: network interface device structure
325  * @txqueue: queue number timing out
326  *
327  * If any port has noticed a Tx timeout, it is likely that the whole
328  * device is munged, not just the one netdev port, so go for the full
329  * reset.
330  **/
331 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
332 {
333 	struct i40e_netdev_priv *np = netdev_priv(netdev);
334 	struct i40e_vsi *vsi = np->vsi;
335 	struct i40e_pf *pf = vsi->back;
336 	struct i40e_ring *tx_ring = NULL;
337 	unsigned int i;
338 	u32 head, val;
339 
340 	pf->tx_timeout_count++;
341 
342 	/* with txqueue index, find the tx_ring struct */
343 	for (i = 0; i < vsi->num_queue_pairs; i++) {
344 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
345 			if (txqueue ==
346 			    vsi->tx_rings[i]->queue_index) {
347 				tx_ring = vsi->tx_rings[i];
348 				break;
349 			}
350 		}
351 	}
352 
353 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
354 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
355 	else if (time_before(jiffies,
356 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
357 		return;   /* don't do any new action before the next timeout */
358 
359 	/* don't kick off another recovery if one is already pending */
360 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
361 		return;
362 
363 	if (tx_ring) {
364 		head = i40e_get_head(tx_ring);
365 		/* Read interrupt register */
366 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
367 			val = rd32(&pf->hw,
368 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
369 						tx_ring->vsi->base_vector - 1));
370 		else
371 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
372 
373 		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",
374 			    vsi->seid, txqueue, tx_ring->next_to_clean,
375 			    head, tx_ring->next_to_use,
376 			    readl(tx_ring->tail), val);
377 	}
378 
379 	pf->tx_timeout_last_recovery = jiffies;
380 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
381 		    pf->tx_timeout_recovery_level, txqueue);
382 
383 	switch (pf->tx_timeout_recovery_level) {
384 	case 1:
385 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
386 		break;
387 	case 2:
388 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
389 		break;
390 	case 3:
391 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
392 		break;
393 	default:
394 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in non-recoverable state.\n");
395 		set_bit(__I40E_DOWN_REQUESTED, pf->state);
396 		set_bit(__I40E_VSI_DOWN_REQUESTED, vsi->state);
397 		break;
398 	}
399 
400 	i40e_service_event_schedule(pf);
401 	pf->tx_timeout_recovery_level++;
402 }
403 
404 /**
405  * i40e_get_vsi_stats_struct - Get System Network Statistics
406  * @vsi: the VSI we care about
407  *
408  * Returns the address of the device statistics structure.
409  * The statistics are actually updated from the service task.
410  **/
411 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
412 {
413 	return &vsi->net_stats;
414 }
415 
416 /**
417  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
418  * @ring: Tx ring to get statistics from
419  * @stats: statistics entry to be updated
420  **/
421 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
422 					    struct rtnl_link_stats64 *stats)
423 {
424 	u64 bytes, packets;
425 	unsigned int start;
426 
427 	do {
428 		start = u64_stats_fetch_begin(&ring->syncp);
429 		packets = ring->stats.packets;
430 		bytes   = ring->stats.bytes;
431 	} while (u64_stats_fetch_retry(&ring->syncp, start));
432 
433 	stats->tx_packets += packets;
434 	stats->tx_bytes   += bytes;
435 }
436 
437 /**
438  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
439  * @netdev: network interface device structure
440  * @stats: data structure to store statistics
441  *
442  * Returns the address of the device statistics structure.
443  * The statistics are actually updated from the service task.
444  **/
445 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
446 				  struct rtnl_link_stats64 *stats)
447 {
448 	struct i40e_netdev_priv *np = netdev_priv(netdev);
449 	struct i40e_vsi *vsi = np->vsi;
450 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
451 	struct i40e_ring *ring;
452 	int i;
453 
454 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
455 		return;
456 
457 	if (!vsi->tx_rings)
458 		return;
459 
460 	rcu_read_lock();
461 	for (i = 0; i < vsi->num_queue_pairs; i++) {
462 		u64 bytes, packets;
463 		unsigned int start;
464 
465 		ring = READ_ONCE(vsi->tx_rings[i]);
466 		if (!ring)
467 			continue;
468 		i40e_get_netdev_stats_struct_tx(ring, stats);
469 
470 		if (i40e_enabled_xdp_vsi(vsi)) {
471 			ring = READ_ONCE(vsi->xdp_rings[i]);
472 			if (!ring)
473 				continue;
474 			i40e_get_netdev_stats_struct_tx(ring, stats);
475 		}
476 
477 		ring = READ_ONCE(vsi->rx_rings[i]);
478 		if (!ring)
479 			continue;
480 		do {
481 			start   = u64_stats_fetch_begin(&ring->syncp);
482 			packets = ring->stats.packets;
483 			bytes   = ring->stats.bytes;
484 		} while (u64_stats_fetch_retry(&ring->syncp, start));
485 
486 		stats->rx_packets += packets;
487 		stats->rx_bytes   += bytes;
488 
489 	}
490 	rcu_read_unlock();
491 
492 	/* following stats updated by i40e_watchdog_subtask() */
493 	stats->multicast	= vsi_stats->multicast;
494 	stats->tx_errors	= vsi_stats->tx_errors;
495 	stats->tx_dropped	= vsi_stats->tx_dropped;
496 	stats->rx_errors	= vsi_stats->rx_errors;
497 	stats->rx_dropped	= vsi_stats->rx_dropped;
498 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
499 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
500 }
501 
502 /**
503  * i40e_vsi_reset_stats - Resets all stats of the given vsi
504  * @vsi: the VSI to have its stats reset
505  **/
506 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
507 {
508 	struct rtnl_link_stats64 *ns;
509 	int i;
510 
511 	if (!vsi)
512 		return;
513 
514 	ns = i40e_get_vsi_stats_struct(vsi);
515 	memset(ns, 0, sizeof(*ns));
516 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
517 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
518 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
519 	if (vsi->rx_rings && vsi->rx_rings[0]) {
520 		for (i = 0; i < vsi->num_queue_pairs; i++) {
521 			memset(&vsi->rx_rings[i]->stats, 0,
522 			       sizeof(vsi->rx_rings[i]->stats));
523 			memset(&vsi->rx_rings[i]->rx_stats, 0,
524 			       sizeof(vsi->rx_rings[i]->rx_stats));
525 			memset(&vsi->tx_rings[i]->stats, 0,
526 			       sizeof(vsi->tx_rings[i]->stats));
527 			memset(&vsi->tx_rings[i]->tx_stats, 0,
528 			       sizeof(vsi->tx_rings[i]->tx_stats));
529 		}
530 	}
531 	vsi->stat_offsets_loaded = false;
532 }
533 
534 /**
535  * i40e_pf_reset_stats - Reset all of the stats for the given PF
536  * @pf: the PF to be reset
537  **/
538 void i40e_pf_reset_stats(struct i40e_pf *pf)
539 {
540 	int i;
541 
542 	memset(&pf->stats, 0, sizeof(pf->stats));
543 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
544 	pf->stat_offsets_loaded = false;
545 
546 	for (i = 0; i < I40E_MAX_VEB; i++) {
547 		if (pf->veb[i]) {
548 			memset(&pf->veb[i]->stats, 0,
549 			       sizeof(pf->veb[i]->stats));
550 			memset(&pf->veb[i]->stats_offsets, 0,
551 			       sizeof(pf->veb[i]->stats_offsets));
552 			memset(&pf->veb[i]->tc_stats, 0,
553 			       sizeof(pf->veb[i]->tc_stats));
554 			memset(&pf->veb[i]->tc_stats_offsets, 0,
555 			       sizeof(pf->veb[i]->tc_stats_offsets));
556 			pf->veb[i]->stat_offsets_loaded = false;
557 		}
558 	}
559 	pf->hw_csum_rx_error = 0;
560 }
561 
562 /**
563  * i40e_compute_pci_to_hw_id - compute index form PCI function.
564  * @vsi: ptr to the VSI to read from.
565  * @hw: ptr to the hardware info.
566  **/
567 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
568 {
569 	int pf_count = i40e_get_pf_count(hw);
570 
571 	if (vsi->type == I40E_VSI_SRIOV)
572 		return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
573 
574 	return hw->port + BIT(7);
575 }
576 
577 /**
578  * i40e_stat_update64 - read and update a 64 bit stat from the chip.
579  * @hw: ptr to the hardware info.
580  * @hireg: the high 32 bit reg to read.
581  * @loreg: the low 32 bit reg to read.
582  * @offset_loaded: has the initial offset been loaded yet.
583  * @offset: ptr to current offset value.
584  * @stat: ptr to the stat.
585  *
586  * Since the device stats are not reset at PFReset, they will not
587  * be zeroed when the driver starts.  We'll save the first values read
588  * and use them as offsets to be subtracted from the raw values in order
589  * to report stats that count from zero.
590  **/
591 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
592 			       bool offset_loaded, u64 *offset, u64 *stat)
593 {
594 	u64 new_data;
595 
596 	new_data = rd64(hw, loreg);
597 
598 	if (!offset_loaded || new_data < *offset)
599 		*offset = new_data;
600 	*stat = new_data - *offset;
601 }
602 
603 /**
604  * i40e_stat_update48 - read and update a 48 bit stat from the chip
605  * @hw: ptr to the hardware info
606  * @hireg: the high 32 bit reg to read
607  * @loreg: the low 32 bit reg to read
608  * @offset_loaded: has the initial offset been loaded yet
609  * @offset: ptr to current offset value
610  * @stat: ptr to the stat
611  *
612  * Since the device stats are not reset at PFReset, they likely will not
613  * be zeroed when the driver starts.  We'll save the first values read
614  * and use them as offsets to be subtracted from the raw values in order
615  * to report stats that count from zero.  In the process, we also manage
616  * the potential roll-over.
617  **/
618 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
619 			       bool offset_loaded, u64 *offset, u64 *stat)
620 {
621 	u64 new_data;
622 
623 	if (hw->device_id == I40E_DEV_ID_QEMU) {
624 		new_data = rd32(hw, loreg);
625 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
626 	} else {
627 		new_data = rd64(hw, loreg);
628 	}
629 	if (!offset_loaded)
630 		*offset = new_data;
631 	if (likely(new_data >= *offset))
632 		*stat = new_data - *offset;
633 	else
634 		*stat = (new_data + BIT_ULL(48)) - *offset;
635 	*stat &= 0xFFFFFFFFFFFFULL;
636 }
637 
638 /**
639  * i40e_stat_update32 - read and update a 32 bit stat from the chip
640  * @hw: ptr to the hardware info
641  * @reg: the hw reg to read
642  * @offset_loaded: has the initial offset been loaded yet
643  * @offset: ptr to current offset value
644  * @stat: ptr to the stat
645  **/
646 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
647 			       bool offset_loaded, u64 *offset, u64 *stat)
648 {
649 	u32 new_data;
650 
651 	new_data = rd32(hw, reg);
652 	if (!offset_loaded)
653 		*offset = new_data;
654 	if (likely(new_data >= *offset))
655 		*stat = (u32)(new_data - *offset);
656 	else
657 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
658 }
659 
660 /**
661  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
662  * @hw: ptr to the hardware info
663  * @reg: the hw reg to read and clear
664  * @stat: ptr to the stat
665  **/
666 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
667 {
668 	u32 new_data = rd32(hw, reg);
669 
670 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
671 	*stat += new_data;
672 }
673 
674 /**
675  * i40e_stats_update_rx_discards - update rx_discards.
676  * @vsi: ptr to the VSI to be updated.
677  * @hw: ptr to the hardware info.
678  * @stat_idx: VSI's stat_counter_idx.
679  * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
680  * @stat_offset: ptr to stat_offset to store first read of specific register.
681  * @stat: ptr to VSI's stat to be updated.
682  **/
683 static void
684 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
685 			      int stat_idx, bool offset_loaded,
686 			      struct i40e_eth_stats *stat_offset,
687 			      struct i40e_eth_stats *stat)
688 {
689 	u64 rx_rdpc, rx_rxerr;
690 
691 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
692 			   &stat_offset->rx_discards, &rx_rdpc);
693 	i40e_stat_update64(hw,
694 			   I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
695 			   I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
696 			   offset_loaded, &stat_offset->rx_discards_other,
697 			   &rx_rxerr);
698 
699 	stat->rx_discards = rx_rdpc + rx_rxerr;
700 }
701 
702 /**
703  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
704  * @vsi: the VSI to be updated
705  **/
706 void i40e_update_eth_stats(struct i40e_vsi *vsi)
707 {
708 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
709 	struct i40e_pf *pf = vsi->back;
710 	struct i40e_hw *hw = &pf->hw;
711 	struct i40e_eth_stats *oes;
712 	struct i40e_eth_stats *es;     /* device's eth stats */
713 
714 	es = &vsi->eth_stats;
715 	oes = &vsi->eth_stats_offsets;
716 
717 	/* Gather up the stats that the hw collects */
718 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
719 			   vsi->stat_offsets_loaded,
720 			   &oes->tx_errors, &es->tx_errors);
721 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
722 			   vsi->stat_offsets_loaded,
723 			   &oes->rx_discards, &es->rx_discards);
724 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
725 			   vsi->stat_offsets_loaded,
726 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
727 
728 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
729 			   I40E_GLV_GORCL(stat_idx),
730 			   vsi->stat_offsets_loaded,
731 			   &oes->rx_bytes, &es->rx_bytes);
732 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
733 			   I40E_GLV_UPRCL(stat_idx),
734 			   vsi->stat_offsets_loaded,
735 			   &oes->rx_unicast, &es->rx_unicast);
736 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
737 			   I40E_GLV_MPRCL(stat_idx),
738 			   vsi->stat_offsets_loaded,
739 			   &oes->rx_multicast, &es->rx_multicast);
740 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
741 			   I40E_GLV_BPRCL(stat_idx),
742 			   vsi->stat_offsets_loaded,
743 			   &oes->rx_broadcast, &es->rx_broadcast);
744 
745 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
746 			   I40E_GLV_GOTCL(stat_idx),
747 			   vsi->stat_offsets_loaded,
748 			   &oes->tx_bytes, &es->tx_bytes);
749 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
750 			   I40E_GLV_UPTCL(stat_idx),
751 			   vsi->stat_offsets_loaded,
752 			   &oes->tx_unicast, &es->tx_unicast);
753 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
754 			   I40E_GLV_MPTCL(stat_idx),
755 			   vsi->stat_offsets_loaded,
756 			   &oes->tx_multicast, &es->tx_multicast);
757 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
758 			   I40E_GLV_BPTCL(stat_idx),
759 			   vsi->stat_offsets_loaded,
760 			   &oes->tx_broadcast, &es->tx_broadcast);
761 
762 	i40e_stats_update_rx_discards(vsi, hw, stat_idx,
763 				      vsi->stat_offsets_loaded, oes, es);
764 
765 	vsi->stat_offsets_loaded = true;
766 }
767 
768 /**
769  * i40e_update_veb_stats - Update Switch component statistics
770  * @veb: the VEB being updated
771  **/
772 void i40e_update_veb_stats(struct i40e_veb *veb)
773 {
774 	struct i40e_pf *pf = veb->pf;
775 	struct i40e_hw *hw = &pf->hw;
776 	struct i40e_eth_stats *oes;
777 	struct i40e_eth_stats *es;     /* device's eth stats */
778 	struct i40e_veb_tc_stats *veb_oes;
779 	struct i40e_veb_tc_stats *veb_es;
780 	int i, idx = 0;
781 
782 	idx = veb->stats_idx;
783 	es = &veb->stats;
784 	oes = &veb->stats_offsets;
785 	veb_es = &veb->tc_stats;
786 	veb_oes = &veb->tc_stats_offsets;
787 
788 	/* Gather up the stats that the hw collects */
789 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
790 			   veb->stat_offsets_loaded,
791 			   &oes->tx_discards, &es->tx_discards);
792 	if (hw->revision_id > 0)
793 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
794 				   veb->stat_offsets_loaded,
795 				   &oes->rx_unknown_protocol,
796 				   &es->rx_unknown_protocol);
797 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
798 			   veb->stat_offsets_loaded,
799 			   &oes->rx_bytes, &es->rx_bytes);
800 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
801 			   veb->stat_offsets_loaded,
802 			   &oes->rx_unicast, &es->rx_unicast);
803 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
804 			   veb->stat_offsets_loaded,
805 			   &oes->rx_multicast, &es->rx_multicast);
806 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
807 			   veb->stat_offsets_loaded,
808 			   &oes->rx_broadcast, &es->rx_broadcast);
809 
810 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
811 			   veb->stat_offsets_loaded,
812 			   &oes->tx_bytes, &es->tx_bytes);
813 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
814 			   veb->stat_offsets_loaded,
815 			   &oes->tx_unicast, &es->tx_unicast);
816 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
817 			   veb->stat_offsets_loaded,
818 			   &oes->tx_multicast, &es->tx_multicast);
819 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
820 			   veb->stat_offsets_loaded,
821 			   &oes->tx_broadcast, &es->tx_broadcast);
822 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
823 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
824 				   I40E_GLVEBTC_RPCL(i, idx),
825 				   veb->stat_offsets_loaded,
826 				   &veb_oes->tc_rx_packets[i],
827 				   &veb_es->tc_rx_packets[i]);
828 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
829 				   I40E_GLVEBTC_RBCL(i, idx),
830 				   veb->stat_offsets_loaded,
831 				   &veb_oes->tc_rx_bytes[i],
832 				   &veb_es->tc_rx_bytes[i]);
833 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
834 				   I40E_GLVEBTC_TPCL(i, idx),
835 				   veb->stat_offsets_loaded,
836 				   &veb_oes->tc_tx_packets[i],
837 				   &veb_es->tc_tx_packets[i]);
838 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
839 				   I40E_GLVEBTC_TBCL(i, idx),
840 				   veb->stat_offsets_loaded,
841 				   &veb_oes->tc_tx_bytes[i],
842 				   &veb_es->tc_tx_bytes[i]);
843 	}
844 	veb->stat_offsets_loaded = true;
845 }
846 
847 /**
848  * i40e_update_vsi_stats - Update the vsi statistics counters.
849  * @vsi: the VSI to be updated
850  *
851  * There are a few instances where we store the same stat in a
852  * couple of different structs.  This is partly because we have
853  * the netdev stats that need to be filled out, which is slightly
854  * different from the "eth_stats" defined by the chip and used in
855  * VF communications.  We sort it out here.
856  **/
857 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
858 {
859 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
860 	struct i40e_pf *pf = vsi->back;
861 	struct rtnl_link_stats64 *ons;
862 	struct rtnl_link_stats64 *ns;   /* netdev stats */
863 	struct i40e_eth_stats *oes;
864 	struct i40e_eth_stats *es;     /* device's eth stats */
865 	u64 tx_restart, tx_busy;
866 	struct i40e_ring *p;
867 	u64 bytes, packets;
868 	unsigned int start;
869 	u64 tx_linearize;
870 	u64 tx_force_wb;
871 	u64 tx_stopped;
872 	u64 rx_p, rx_b;
873 	u64 tx_p, tx_b;
874 	u16 q;
875 
876 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
877 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
878 		return;
879 
880 	ns = i40e_get_vsi_stats_struct(vsi);
881 	ons = &vsi->net_stats_offsets;
882 	es = &vsi->eth_stats;
883 	oes = &vsi->eth_stats_offsets;
884 
885 	/* Gather up the netdev and vsi stats that the driver collects
886 	 * on the fly during packet processing
887 	 */
888 	rx_b = rx_p = 0;
889 	tx_b = tx_p = 0;
890 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
891 	tx_stopped = 0;
892 	rx_page = 0;
893 	rx_buf = 0;
894 	rx_reuse = 0;
895 	rx_alloc = 0;
896 	rx_waive = 0;
897 	rx_busy = 0;
898 	rcu_read_lock();
899 	for (q = 0; q < vsi->num_queue_pairs; q++) {
900 		/* locate Tx ring */
901 		p = READ_ONCE(vsi->tx_rings[q]);
902 		if (!p)
903 			continue;
904 
905 		do {
906 			start = u64_stats_fetch_begin(&p->syncp);
907 			packets = p->stats.packets;
908 			bytes = p->stats.bytes;
909 		} while (u64_stats_fetch_retry(&p->syncp, start));
910 		tx_b += bytes;
911 		tx_p += packets;
912 		tx_restart += p->tx_stats.restart_queue;
913 		tx_busy += p->tx_stats.tx_busy;
914 		tx_linearize += p->tx_stats.tx_linearize;
915 		tx_force_wb += p->tx_stats.tx_force_wb;
916 		tx_stopped += p->tx_stats.tx_stopped;
917 
918 		/* locate Rx ring */
919 		p = READ_ONCE(vsi->rx_rings[q]);
920 		if (!p)
921 			continue;
922 
923 		do {
924 			start = u64_stats_fetch_begin(&p->syncp);
925 			packets = p->stats.packets;
926 			bytes = p->stats.bytes;
927 		} while (u64_stats_fetch_retry(&p->syncp, start));
928 		rx_b += bytes;
929 		rx_p += packets;
930 		rx_buf += p->rx_stats.alloc_buff_failed;
931 		rx_page += p->rx_stats.alloc_page_failed;
932 		rx_reuse += p->rx_stats.page_reuse_count;
933 		rx_alloc += p->rx_stats.page_alloc_count;
934 		rx_waive += p->rx_stats.page_waive_count;
935 		rx_busy += p->rx_stats.page_busy_count;
936 
937 		if (i40e_enabled_xdp_vsi(vsi)) {
938 			/* locate XDP ring */
939 			p = READ_ONCE(vsi->xdp_rings[q]);
940 			if (!p)
941 				continue;
942 
943 			do {
944 				start = u64_stats_fetch_begin(&p->syncp);
945 				packets = p->stats.packets;
946 				bytes = p->stats.bytes;
947 			} while (u64_stats_fetch_retry(&p->syncp, start));
948 			tx_b += bytes;
949 			tx_p += packets;
950 			tx_restart += p->tx_stats.restart_queue;
951 			tx_busy += p->tx_stats.tx_busy;
952 			tx_linearize += p->tx_stats.tx_linearize;
953 			tx_force_wb += p->tx_stats.tx_force_wb;
954 		}
955 	}
956 	rcu_read_unlock();
957 	vsi->tx_restart = tx_restart;
958 	vsi->tx_busy = tx_busy;
959 	vsi->tx_linearize = tx_linearize;
960 	vsi->tx_force_wb = tx_force_wb;
961 	vsi->tx_stopped = tx_stopped;
962 	vsi->rx_page_failed = rx_page;
963 	vsi->rx_buf_failed = rx_buf;
964 	vsi->rx_page_reuse = rx_reuse;
965 	vsi->rx_page_alloc = rx_alloc;
966 	vsi->rx_page_waive = rx_waive;
967 	vsi->rx_page_busy = rx_busy;
968 
969 	ns->rx_packets = rx_p;
970 	ns->rx_bytes = rx_b;
971 	ns->tx_packets = tx_p;
972 	ns->tx_bytes = tx_b;
973 
974 	/* update netdev stats from eth stats */
975 	i40e_update_eth_stats(vsi);
976 	ons->tx_errors = oes->tx_errors;
977 	ns->tx_errors = es->tx_errors;
978 	ons->multicast = oes->rx_multicast;
979 	ns->multicast = es->rx_multicast;
980 	ons->rx_dropped = oes->rx_discards;
981 	ns->rx_dropped = es->rx_discards;
982 	ons->tx_dropped = oes->tx_discards;
983 	ns->tx_dropped = es->tx_discards;
984 
985 	/* pull in a couple PF stats if this is the main vsi */
986 	if (vsi == pf->vsi[pf->lan_vsi]) {
987 		ns->rx_crc_errors = pf->stats.crc_errors;
988 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
989 		ns->rx_length_errors = pf->stats.rx_length_errors;
990 	}
991 }
992 
993 /**
994  * i40e_update_pf_stats - Update the PF statistics counters.
995  * @pf: the PF to be updated
996  **/
997 static void i40e_update_pf_stats(struct i40e_pf *pf)
998 {
999 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
1000 	struct i40e_hw_port_stats *nsd = &pf->stats;
1001 	struct i40e_hw *hw = &pf->hw;
1002 	u32 val;
1003 	int i;
1004 
1005 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
1006 			   I40E_GLPRT_GORCL(hw->port),
1007 			   pf->stat_offsets_loaded,
1008 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
1009 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
1010 			   I40E_GLPRT_GOTCL(hw->port),
1011 			   pf->stat_offsets_loaded,
1012 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
1013 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
1014 			   pf->stat_offsets_loaded,
1015 			   &osd->eth.rx_discards,
1016 			   &nsd->eth.rx_discards);
1017 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
1018 			   I40E_GLPRT_UPRCL(hw->port),
1019 			   pf->stat_offsets_loaded,
1020 			   &osd->eth.rx_unicast,
1021 			   &nsd->eth.rx_unicast);
1022 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
1023 			   I40E_GLPRT_MPRCL(hw->port),
1024 			   pf->stat_offsets_loaded,
1025 			   &osd->eth.rx_multicast,
1026 			   &nsd->eth.rx_multicast);
1027 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1028 			   I40E_GLPRT_BPRCL(hw->port),
1029 			   pf->stat_offsets_loaded,
1030 			   &osd->eth.rx_broadcast,
1031 			   &nsd->eth.rx_broadcast);
1032 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1033 			   I40E_GLPRT_UPTCL(hw->port),
1034 			   pf->stat_offsets_loaded,
1035 			   &osd->eth.tx_unicast,
1036 			   &nsd->eth.tx_unicast);
1037 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1038 			   I40E_GLPRT_MPTCL(hw->port),
1039 			   pf->stat_offsets_loaded,
1040 			   &osd->eth.tx_multicast,
1041 			   &nsd->eth.tx_multicast);
1042 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1043 			   I40E_GLPRT_BPTCL(hw->port),
1044 			   pf->stat_offsets_loaded,
1045 			   &osd->eth.tx_broadcast,
1046 			   &nsd->eth.tx_broadcast);
1047 
1048 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1049 			   pf->stat_offsets_loaded,
1050 			   &osd->tx_dropped_link_down,
1051 			   &nsd->tx_dropped_link_down);
1052 
1053 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1054 			   pf->stat_offsets_loaded,
1055 			   &osd->crc_errors, &nsd->crc_errors);
1056 
1057 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1058 			   pf->stat_offsets_loaded,
1059 			   &osd->illegal_bytes, &nsd->illegal_bytes);
1060 
1061 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1062 			   pf->stat_offsets_loaded,
1063 			   &osd->mac_local_faults,
1064 			   &nsd->mac_local_faults);
1065 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1066 			   pf->stat_offsets_loaded,
1067 			   &osd->mac_remote_faults,
1068 			   &nsd->mac_remote_faults);
1069 
1070 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1071 			   pf->stat_offsets_loaded,
1072 			   &osd->rx_length_errors,
1073 			   &nsd->rx_length_errors);
1074 
1075 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1076 			   pf->stat_offsets_loaded,
1077 			   &osd->link_xon_rx, &nsd->link_xon_rx);
1078 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1079 			   pf->stat_offsets_loaded,
1080 			   &osd->link_xon_tx, &nsd->link_xon_tx);
1081 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1082 			   pf->stat_offsets_loaded,
1083 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1084 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1085 			   pf->stat_offsets_loaded,
1086 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1087 
1088 	for (i = 0; i < 8; i++) {
1089 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1090 				   pf->stat_offsets_loaded,
1091 				   &osd->priority_xoff_rx[i],
1092 				   &nsd->priority_xoff_rx[i]);
1093 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1094 				   pf->stat_offsets_loaded,
1095 				   &osd->priority_xon_rx[i],
1096 				   &nsd->priority_xon_rx[i]);
1097 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1098 				   pf->stat_offsets_loaded,
1099 				   &osd->priority_xon_tx[i],
1100 				   &nsd->priority_xon_tx[i]);
1101 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1102 				   pf->stat_offsets_loaded,
1103 				   &osd->priority_xoff_tx[i],
1104 				   &nsd->priority_xoff_tx[i]);
1105 		i40e_stat_update32(hw,
1106 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1107 				   pf->stat_offsets_loaded,
1108 				   &osd->priority_xon_2_xoff[i],
1109 				   &nsd->priority_xon_2_xoff[i]);
1110 	}
1111 
1112 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1113 			   I40E_GLPRT_PRC64L(hw->port),
1114 			   pf->stat_offsets_loaded,
1115 			   &osd->rx_size_64, &nsd->rx_size_64);
1116 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1117 			   I40E_GLPRT_PRC127L(hw->port),
1118 			   pf->stat_offsets_loaded,
1119 			   &osd->rx_size_127, &nsd->rx_size_127);
1120 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1121 			   I40E_GLPRT_PRC255L(hw->port),
1122 			   pf->stat_offsets_loaded,
1123 			   &osd->rx_size_255, &nsd->rx_size_255);
1124 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1125 			   I40E_GLPRT_PRC511L(hw->port),
1126 			   pf->stat_offsets_loaded,
1127 			   &osd->rx_size_511, &nsd->rx_size_511);
1128 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1129 			   I40E_GLPRT_PRC1023L(hw->port),
1130 			   pf->stat_offsets_loaded,
1131 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1132 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1133 			   I40E_GLPRT_PRC1522L(hw->port),
1134 			   pf->stat_offsets_loaded,
1135 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1136 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1137 			   I40E_GLPRT_PRC9522L(hw->port),
1138 			   pf->stat_offsets_loaded,
1139 			   &osd->rx_size_big, &nsd->rx_size_big);
1140 
1141 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1142 			   I40E_GLPRT_PTC64L(hw->port),
1143 			   pf->stat_offsets_loaded,
1144 			   &osd->tx_size_64, &nsd->tx_size_64);
1145 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1146 			   I40E_GLPRT_PTC127L(hw->port),
1147 			   pf->stat_offsets_loaded,
1148 			   &osd->tx_size_127, &nsd->tx_size_127);
1149 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1150 			   I40E_GLPRT_PTC255L(hw->port),
1151 			   pf->stat_offsets_loaded,
1152 			   &osd->tx_size_255, &nsd->tx_size_255);
1153 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1154 			   I40E_GLPRT_PTC511L(hw->port),
1155 			   pf->stat_offsets_loaded,
1156 			   &osd->tx_size_511, &nsd->tx_size_511);
1157 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1158 			   I40E_GLPRT_PTC1023L(hw->port),
1159 			   pf->stat_offsets_loaded,
1160 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1161 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1162 			   I40E_GLPRT_PTC1522L(hw->port),
1163 			   pf->stat_offsets_loaded,
1164 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1165 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1166 			   I40E_GLPRT_PTC9522L(hw->port),
1167 			   pf->stat_offsets_loaded,
1168 			   &osd->tx_size_big, &nsd->tx_size_big);
1169 
1170 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1171 			   pf->stat_offsets_loaded,
1172 			   &osd->rx_undersize, &nsd->rx_undersize);
1173 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1174 			   pf->stat_offsets_loaded,
1175 			   &osd->rx_fragments, &nsd->rx_fragments);
1176 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1177 			   pf->stat_offsets_loaded,
1178 			   &osd->rx_oversize, &nsd->rx_oversize);
1179 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1180 			   pf->stat_offsets_loaded,
1181 			   &osd->rx_jabber, &nsd->rx_jabber);
1182 
1183 	/* FDIR stats */
1184 	i40e_stat_update_and_clear32(hw,
1185 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1186 			&nsd->fd_atr_match);
1187 	i40e_stat_update_and_clear32(hw,
1188 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1189 			&nsd->fd_sb_match);
1190 	i40e_stat_update_and_clear32(hw,
1191 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1192 			&nsd->fd_atr_tunnel_match);
1193 
1194 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1195 	nsd->tx_lpi_status =
1196 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1197 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1198 	nsd->rx_lpi_status =
1199 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1200 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1201 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1202 			   pf->stat_offsets_loaded,
1203 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1204 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1205 			   pf->stat_offsets_loaded,
1206 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1207 
1208 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1209 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1210 		nsd->fd_sb_status = true;
1211 	else
1212 		nsd->fd_sb_status = false;
1213 
1214 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1215 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1216 		nsd->fd_atr_status = true;
1217 	else
1218 		nsd->fd_atr_status = false;
1219 
1220 	pf->stat_offsets_loaded = true;
1221 }
1222 
1223 /**
1224  * i40e_update_stats - Update the various statistics counters.
1225  * @vsi: the VSI to be updated
1226  *
1227  * Update the various stats for this VSI and its related entities.
1228  **/
1229 void i40e_update_stats(struct i40e_vsi *vsi)
1230 {
1231 	struct i40e_pf *pf = vsi->back;
1232 
1233 	if (vsi == pf->vsi[pf->lan_vsi])
1234 		i40e_update_pf_stats(pf);
1235 
1236 	i40e_update_vsi_stats(vsi);
1237 }
1238 
1239 /**
1240  * i40e_count_filters - counts VSI mac filters
1241  * @vsi: the VSI to be searched
1242  *
1243  * Returns count of mac filters
1244  **/
1245 int i40e_count_filters(struct i40e_vsi *vsi)
1246 {
1247 	struct i40e_mac_filter *f;
1248 	struct hlist_node *h;
1249 	int bkt;
1250 	int cnt = 0;
1251 
1252 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1253 		++cnt;
1254 
1255 	return cnt;
1256 }
1257 
1258 /**
1259  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1260  * @vsi: the VSI to be searched
1261  * @macaddr: the MAC address
1262  * @vlan: the vlan
1263  *
1264  * Returns ptr to the filter object or NULL
1265  **/
1266 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1267 						const u8 *macaddr, s16 vlan)
1268 {
1269 	struct i40e_mac_filter *f;
1270 	u64 key;
1271 
1272 	if (!vsi || !macaddr)
1273 		return NULL;
1274 
1275 	key = i40e_addr_to_hkey(macaddr);
1276 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1277 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1278 		    (vlan == f->vlan))
1279 			return f;
1280 	}
1281 	return NULL;
1282 }
1283 
1284 /**
1285  * i40e_find_mac - Find a mac addr in the macvlan filters list
1286  * @vsi: the VSI to be searched
1287  * @macaddr: the MAC address we are searching for
1288  *
1289  * Returns the first filter with the provided MAC address or NULL if
1290  * MAC address was not found
1291  **/
1292 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1293 {
1294 	struct i40e_mac_filter *f;
1295 	u64 key;
1296 
1297 	if (!vsi || !macaddr)
1298 		return NULL;
1299 
1300 	key = i40e_addr_to_hkey(macaddr);
1301 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1302 		if ((ether_addr_equal(macaddr, f->macaddr)))
1303 			return f;
1304 	}
1305 	return NULL;
1306 }
1307 
1308 /**
1309  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1310  * @vsi: the VSI to be searched
1311  *
1312  * Returns true if VSI is in vlan mode or false otherwise
1313  **/
1314 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1315 {
1316 	/* If we have a PVID, always operate in VLAN mode */
1317 	if (vsi->info.pvid)
1318 		return true;
1319 
1320 	/* We need to operate in VLAN mode whenever we have any filters with
1321 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1322 	 * time, incurring search cost repeatedly. However, we can notice two
1323 	 * things:
1324 	 *
1325 	 * 1) the only place where we can gain a VLAN filter is in
1326 	 *    i40e_add_filter.
1327 	 *
1328 	 * 2) the only place where filters are actually removed is in
1329 	 *    i40e_sync_filters_subtask.
1330 	 *
1331 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1332 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1333 	 * we have to perform the full search after deleting filters in
1334 	 * i40e_sync_filters_subtask, but we already have to search
1335 	 * filters here and can perform the check at the same time. This
1336 	 * results in avoiding embedding a loop for VLAN mode inside another
1337 	 * loop over all the filters, and should maintain correctness as noted
1338 	 * above.
1339 	 */
1340 	return vsi->has_vlan_filter;
1341 }
1342 
1343 /**
1344  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1345  * @vsi: the VSI to configure
1346  * @tmp_add_list: list of filters ready to be added
1347  * @tmp_del_list: list of filters ready to be deleted
1348  * @vlan_filters: the number of active VLAN filters
1349  *
1350  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1351  * behave as expected. If we have any active VLAN filters remaining or about
1352  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1353  * so that they only match against untagged traffic. If we no longer have any
1354  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1355  * so that they match against both tagged and untagged traffic. In this way,
1356  * we ensure that we correctly receive the desired traffic. This ensures that
1357  * when we have an active VLAN we will receive only untagged traffic and
1358  * traffic matching active VLANs. If we have no active VLANs then we will
1359  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1360  *
1361  * Finally, in a similar fashion, this function also corrects filters when
1362  * there is an active PVID assigned to this VSI.
1363  *
1364  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1365  *
1366  * This function is only expected to be called from within
1367  * i40e_sync_vsi_filters.
1368  *
1369  * NOTE: This function expects to be called while under the
1370  * mac_filter_hash_lock
1371  */
1372 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1373 					 struct hlist_head *tmp_add_list,
1374 					 struct hlist_head *tmp_del_list,
1375 					 int vlan_filters)
1376 {
1377 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1378 	struct i40e_mac_filter *f, *add_head;
1379 	struct i40e_new_mac_filter *new;
1380 	struct hlist_node *h;
1381 	int bkt, new_vlan;
1382 
1383 	/* To determine if a particular filter needs to be replaced we
1384 	 * have the three following conditions:
1385 	 *
1386 	 * a) if we have a PVID assigned, then all filters which are
1387 	 *    not marked as VLAN=PVID must be replaced with filters that
1388 	 *    are.
1389 	 * b) otherwise, if we have any active VLANS, all filters
1390 	 *    which are marked as VLAN=-1 must be replaced with
1391 	 *    filters marked as VLAN=0
1392 	 * c) finally, if we do not have any active VLANS, all filters
1393 	 *    which are marked as VLAN=0 must be replaced with filters
1394 	 *    marked as VLAN=-1
1395 	 */
1396 
1397 	/* Update the filters about to be added in place */
1398 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1399 		if (pvid && new->f->vlan != pvid)
1400 			new->f->vlan = pvid;
1401 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1402 			new->f->vlan = 0;
1403 		else if (!vlan_filters && new->f->vlan == 0)
1404 			new->f->vlan = I40E_VLAN_ANY;
1405 	}
1406 
1407 	/* Update the remaining active filters */
1408 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1409 		/* Combine the checks for whether a filter needs to be changed
1410 		 * and then determine the new VLAN inside the if block, in
1411 		 * order to avoid duplicating code for adding the new filter
1412 		 * then deleting the old filter.
1413 		 */
1414 		if ((pvid && f->vlan != pvid) ||
1415 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1416 		    (!vlan_filters && f->vlan == 0)) {
1417 			/* Determine the new vlan we will be adding */
1418 			if (pvid)
1419 				new_vlan = pvid;
1420 			else if (vlan_filters)
1421 				new_vlan = 0;
1422 			else
1423 				new_vlan = I40E_VLAN_ANY;
1424 
1425 			/* Create the new filter */
1426 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1427 			if (!add_head)
1428 				return -ENOMEM;
1429 
1430 			/* Create a temporary i40e_new_mac_filter */
1431 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1432 			if (!new)
1433 				return -ENOMEM;
1434 
1435 			new->f = add_head;
1436 			new->state = add_head->state;
1437 
1438 			/* Add the new filter to the tmp list */
1439 			hlist_add_head(&new->hlist, tmp_add_list);
1440 
1441 			/* Put the original filter into the delete list */
1442 			f->state = I40E_FILTER_REMOVE;
1443 			hash_del(&f->hlist);
1444 			hlist_add_head(&f->hlist, tmp_del_list);
1445 		}
1446 	}
1447 
1448 	vsi->has_vlan_filter = !!vlan_filters;
1449 
1450 	return 0;
1451 }
1452 
1453 /**
1454  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1455  * @vsi: the vsi to configure
1456  * @new_mac: new mac filter to be added
1457  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1458  * @vlan_filters: the number of active VLAN filters
1459  * @trusted: flag if the VF is trusted
1460  *
1461  * Get new VLAN id based on current VLAN filters, trust, PVID
1462  * and vf-vlan-prune-disable flag.
1463  *
1464  * Returns the value of the new vlan filter or
1465  * the old value if no new filter is needed.
1466  */
1467 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1468 				struct i40e_new_mac_filter *new_mac,
1469 				struct i40e_mac_filter *f,
1470 				int vlan_filters,
1471 				bool trusted)
1472 {
1473 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1474 	struct i40e_pf *pf = vsi->back;
1475 	bool is_any;
1476 
1477 	if (new_mac)
1478 		f = new_mac->f;
1479 
1480 	if (pvid && f->vlan != pvid)
1481 		return pvid;
1482 
1483 	is_any = (trusted ||
1484 		  !(pf->flags & I40E_FLAG_VF_VLAN_PRUNING));
1485 
1486 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1487 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1488 	    (is_any && !vlan_filters && f->vlan == 0)) {
1489 		if (is_any)
1490 			return I40E_VLAN_ANY;
1491 		else
1492 			return 0;
1493 	}
1494 
1495 	return f->vlan;
1496 }
1497 
1498 /**
1499  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1500  * @vsi: the vsi to configure
1501  * @tmp_add_list: list of filters ready to be added
1502  * @tmp_del_list: list of filters ready to be deleted
1503  * @vlan_filters: the number of active VLAN filters
1504  * @trusted: flag if the VF is trusted
1505  *
1506  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1507  * and vf-vlan-prune-disable flag.
1508  *
1509  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1510  *
1511  * This function is only expected to be called from within
1512  * i40e_sync_vsi_filters.
1513  *
1514  * NOTE: This function expects to be called while under the
1515  * mac_filter_hash_lock
1516  */
1517 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1518 					    struct hlist_head *tmp_add_list,
1519 					    struct hlist_head *tmp_del_list,
1520 					    int vlan_filters,
1521 					    bool trusted)
1522 {
1523 	struct i40e_mac_filter *f, *add_head;
1524 	struct i40e_new_mac_filter *new_mac;
1525 	struct hlist_node *h;
1526 	int bkt, new_vlan;
1527 
1528 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1529 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1530 							vlan_filters, trusted);
1531 	}
1532 
1533 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1534 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1535 						trusted);
1536 		if (new_vlan != f->vlan) {
1537 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1538 			if (!add_head)
1539 				return -ENOMEM;
1540 			/* Create a temporary i40e_new_mac_filter */
1541 			new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1542 			if (!new_mac)
1543 				return -ENOMEM;
1544 			new_mac->f = add_head;
1545 			new_mac->state = add_head->state;
1546 
1547 			/* Add the new filter to the tmp list */
1548 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1549 
1550 			/* Put the original filter into the delete list */
1551 			f->state = I40E_FILTER_REMOVE;
1552 			hash_del(&f->hlist);
1553 			hlist_add_head(&f->hlist, tmp_del_list);
1554 		}
1555 	}
1556 
1557 	vsi->has_vlan_filter = !!vlan_filters;
1558 	return 0;
1559 }
1560 
1561 /**
1562  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1563  * @vsi: the PF Main VSI - inappropriate for any other VSI
1564  * @macaddr: the MAC address
1565  *
1566  * Remove whatever filter the firmware set up so the driver can manage
1567  * its own filtering intelligently.
1568  **/
1569 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1570 {
1571 	struct i40e_aqc_remove_macvlan_element_data element;
1572 	struct i40e_pf *pf = vsi->back;
1573 
1574 	/* Only appropriate for the PF main VSI */
1575 	if (vsi->type != I40E_VSI_MAIN)
1576 		return;
1577 
1578 	memset(&element, 0, sizeof(element));
1579 	ether_addr_copy(element.mac_addr, macaddr);
1580 	element.vlan_tag = 0;
1581 	/* Ignore error returns, some firmware does it this way... */
1582 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1583 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1584 
1585 	memset(&element, 0, sizeof(element));
1586 	ether_addr_copy(element.mac_addr, macaddr);
1587 	element.vlan_tag = 0;
1588 	/* ...and some firmware does it this way. */
1589 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1590 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1591 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1592 }
1593 
1594 /**
1595  * i40e_add_filter - Add a mac/vlan filter to the VSI
1596  * @vsi: the VSI to be searched
1597  * @macaddr: the MAC address
1598  * @vlan: the vlan
1599  *
1600  * Returns ptr to the filter object or NULL when no memory available.
1601  *
1602  * NOTE: This function is expected to be called with mac_filter_hash_lock
1603  * being held.
1604  **/
1605 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1606 					const u8 *macaddr, s16 vlan)
1607 {
1608 	struct i40e_mac_filter *f;
1609 	u64 key;
1610 
1611 	if (!vsi || !macaddr)
1612 		return NULL;
1613 
1614 	f = i40e_find_filter(vsi, macaddr, vlan);
1615 	if (!f) {
1616 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1617 		if (!f)
1618 			return NULL;
1619 
1620 		/* Update the boolean indicating if we need to function in
1621 		 * VLAN mode.
1622 		 */
1623 		if (vlan >= 0)
1624 			vsi->has_vlan_filter = true;
1625 
1626 		ether_addr_copy(f->macaddr, macaddr);
1627 		f->vlan = vlan;
1628 		f->state = I40E_FILTER_NEW;
1629 		INIT_HLIST_NODE(&f->hlist);
1630 
1631 		key = i40e_addr_to_hkey(macaddr);
1632 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1633 
1634 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1635 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1636 	}
1637 
1638 	/* If we're asked to add a filter that has been marked for removal, it
1639 	 * is safe to simply restore it to active state. __i40e_del_filter
1640 	 * will have simply deleted any filters which were previously marked
1641 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1642 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1643 	 * task, just restore this filter to the ACTIVE state so that the
1644 	 * sync task leaves it in place
1645 	 */
1646 	if (f->state == I40E_FILTER_REMOVE)
1647 		f->state = I40E_FILTER_ACTIVE;
1648 
1649 	return f;
1650 }
1651 
1652 /**
1653  * __i40e_del_filter - Remove a specific filter from the VSI
1654  * @vsi: VSI to remove from
1655  * @f: the filter to remove from the list
1656  *
1657  * This function should be called instead of i40e_del_filter only if you know
1658  * the exact filter you will remove already, such as via i40e_find_filter or
1659  * i40e_find_mac.
1660  *
1661  * NOTE: This function is expected to be called with mac_filter_hash_lock
1662  * being held.
1663  * ANOTHER NOTE: This function MUST be called from within the context of
1664  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1665  * instead of list_for_each_entry().
1666  **/
1667 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1668 {
1669 	if (!f)
1670 		return;
1671 
1672 	/* If the filter was never added to firmware then we can just delete it
1673 	 * directly and we don't want to set the status to remove or else an
1674 	 * admin queue command will unnecessarily fire.
1675 	 */
1676 	if ((f->state == I40E_FILTER_FAILED) ||
1677 	    (f->state == I40E_FILTER_NEW)) {
1678 		hash_del(&f->hlist);
1679 		kfree(f);
1680 	} else {
1681 		f->state = I40E_FILTER_REMOVE;
1682 	}
1683 
1684 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1685 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1686 }
1687 
1688 /**
1689  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1690  * @vsi: the VSI to be searched
1691  * @macaddr: the MAC address
1692  * @vlan: the VLAN
1693  *
1694  * NOTE: This function is expected to be called with mac_filter_hash_lock
1695  * being held.
1696  * ANOTHER NOTE: This function MUST be called from within the context of
1697  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1698  * instead of list_for_each_entry().
1699  **/
1700 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1701 {
1702 	struct i40e_mac_filter *f;
1703 
1704 	if (!vsi || !macaddr)
1705 		return;
1706 
1707 	f = i40e_find_filter(vsi, macaddr, vlan);
1708 	__i40e_del_filter(vsi, f);
1709 }
1710 
1711 /**
1712  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1713  * @vsi: the VSI to be searched
1714  * @macaddr: the mac address to be filtered
1715  *
1716  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1717  * go through all the macvlan filters and add a macvlan filter for each
1718  * unique vlan that already exists. If a PVID has been assigned, instead only
1719  * add the macaddr to that VLAN.
1720  *
1721  * Returns last filter added on success, else NULL
1722  **/
1723 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1724 					    const u8 *macaddr)
1725 {
1726 	struct i40e_mac_filter *f, *add = NULL;
1727 	struct hlist_node *h;
1728 	int bkt;
1729 
1730 	if (vsi->info.pvid)
1731 		return i40e_add_filter(vsi, macaddr,
1732 				       le16_to_cpu(vsi->info.pvid));
1733 
1734 	if (!i40e_is_vsi_in_vlan(vsi))
1735 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1736 
1737 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1738 		if (f->state == I40E_FILTER_REMOVE)
1739 			continue;
1740 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1741 		if (!add)
1742 			return NULL;
1743 	}
1744 
1745 	return add;
1746 }
1747 
1748 /**
1749  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1750  * @vsi: the VSI to be searched
1751  * @macaddr: the mac address to be removed
1752  *
1753  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1754  * associated with.
1755  *
1756  * Returns 0 for success, or error
1757  **/
1758 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1759 {
1760 	struct i40e_mac_filter *f;
1761 	struct hlist_node *h;
1762 	bool found = false;
1763 	int bkt;
1764 
1765 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1766 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1767 		if (ether_addr_equal(macaddr, f->macaddr)) {
1768 			__i40e_del_filter(vsi, f);
1769 			found = true;
1770 		}
1771 	}
1772 
1773 	if (found)
1774 		return 0;
1775 	else
1776 		return -ENOENT;
1777 }
1778 
1779 /**
1780  * i40e_set_mac - NDO callback to set mac address
1781  * @netdev: network interface device structure
1782  * @p: pointer to an address structure
1783  *
1784  * Returns 0 on success, negative on failure
1785  **/
1786 static int i40e_set_mac(struct net_device *netdev, void *p)
1787 {
1788 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1789 	struct i40e_vsi *vsi = np->vsi;
1790 	struct i40e_pf *pf = vsi->back;
1791 	struct i40e_hw *hw = &pf->hw;
1792 	struct sockaddr *addr = p;
1793 
1794 	if (!is_valid_ether_addr(addr->sa_data))
1795 		return -EADDRNOTAVAIL;
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 		int 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 %pe, AQ ret %s\n",
1826 				    ERR_PTR(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 %pe aq_err %s\n",
1858 				 ERR_PTR(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 %pe aq_err %s\n",
1870 				 ERR_PTR(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 	int 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 %pe, aq_err %s\n",
2362 			 vsi_name, ERR_PTR(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 int
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 	int 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 	int 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 %pe, aq_err %s\n",
2492 				 ERR_PTR(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 %pe, aq_err %s\n",
2504 				 ERR_PTR(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 %pe, aq_err %s\n",
2514 				 ERR_PTR(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 	u32 changed_flags = 0;
2545 	struct hlist_node *h;
2546 	struct i40e_pf *pf;
2547 	int num_add = 0;
2548 	int num_del = 0;
2549 	int aq_ret = 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 if (pf->vf)
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 &&
2792 	    !pf->vf[vsi->vf_id].trusted) {
2793 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2794 		goto out;
2795 	}
2796 
2797 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2798 
2799 	/* If we are entering overflow promiscuous, we need to calculate a new
2800 	 * threshold for when we are safe to exit
2801 	 */
2802 	if (!old_overflow && new_overflow)
2803 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2804 
2805 	/* check for changes in promiscuous modes */
2806 	if (changed_flags & IFF_ALLMULTI) {
2807 		bool cur_multipromisc;
2808 
2809 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2810 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2811 							       vsi->seid,
2812 							       cur_multipromisc,
2813 							       NULL);
2814 		if (aq_ret) {
2815 			retval = i40e_aq_rc_to_posix(aq_ret,
2816 						     hw->aq.asq_last_status);
2817 			dev_info(&pf->pdev->dev,
2818 				 "set multi promisc failed on %s, err %pe aq_err %s\n",
2819 				 vsi_name,
2820 				 ERR_PTR(aq_ret),
2821 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2822 		} else {
2823 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2824 				 cur_multipromisc ? "entering" : "leaving");
2825 		}
2826 	}
2827 
2828 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2829 		bool cur_promisc;
2830 
2831 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2832 			       new_overflow);
2833 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2834 		if (aq_ret) {
2835 			retval = i40e_aq_rc_to_posix(aq_ret,
2836 						     hw->aq.asq_last_status);
2837 			dev_info(&pf->pdev->dev,
2838 				 "Setting promiscuous %s failed on %s, err %pe aq_err %s\n",
2839 				 cur_promisc ? "on" : "off",
2840 				 vsi_name,
2841 				 ERR_PTR(aq_ret),
2842 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2843 		}
2844 	}
2845 out:
2846 	/* if something went wrong then set the changed flag so we try again */
2847 	if (retval)
2848 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2849 
2850 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2851 	return retval;
2852 
2853 err_no_memory:
2854 	/* Restore elements on the temporary add and delete lists */
2855 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2856 err_no_memory_locked:
2857 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2858 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2859 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2860 
2861 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2862 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2863 	return -ENOMEM;
2864 }
2865 
2866 /**
2867  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2868  * @pf: board private structure
2869  **/
2870 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2871 {
2872 	int v;
2873 
2874 	if (!pf)
2875 		return;
2876 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2877 		return;
2878 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2879 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2880 		return;
2881 	}
2882 
2883 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2884 		if (pf->vsi[v] &&
2885 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2886 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2887 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2888 
2889 			if (ret) {
2890 				/* come back and try again later */
2891 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2892 					pf->state);
2893 				break;
2894 			}
2895 		}
2896 	}
2897 }
2898 
2899 /**
2900  * i40e_calculate_vsi_rx_buf_len - Calculates buffer length
2901  *
2902  * @vsi: VSI to calculate rx_buf_len from
2903  */
2904 static u16 i40e_calculate_vsi_rx_buf_len(struct i40e_vsi *vsi)
2905 {
2906 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2907 		return SKB_WITH_OVERHEAD(I40E_RXBUFFER_2048);
2908 
2909 	return PAGE_SIZE < 8192 ? I40E_RXBUFFER_3072 : I40E_RXBUFFER_2048;
2910 }
2911 
2912 /**
2913  * i40e_max_vsi_frame_size - returns the maximum allowed frame size for VSI
2914  * @vsi: the vsi
2915  * @xdp_prog: XDP program
2916  **/
2917 static int i40e_max_vsi_frame_size(struct i40e_vsi *vsi,
2918 				   struct bpf_prog *xdp_prog)
2919 {
2920 	u16 rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
2921 	u16 chain_len;
2922 
2923 	if (xdp_prog && !xdp_prog->aux->xdp_has_frags)
2924 		chain_len = 1;
2925 	else
2926 		chain_len = I40E_MAX_CHAINED_RX_BUFFERS;
2927 
2928 	return min_t(u16, rx_buf_len * chain_len, I40E_MAX_RXBUFFER);
2929 }
2930 
2931 /**
2932  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2933  * @netdev: network interface device structure
2934  * @new_mtu: new value for maximum frame size
2935  *
2936  * Returns 0 on success, negative on failure
2937  **/
2938 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2939 {
2940 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2941 	struct i40e_vsi *vsi = np->vsi;
2942 	struct i40e_pf *pf = vsi->back;
2943 	int frame_size;
2944 
2945 	frame_size = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
2946 	if (new_mtu > frame_size - I40E_PACKET_HDR_PAD) {
2947 		netdev_err(netdev, "Error changing mtu to %d, Max is %d\n",
2948 			   new_mtu, frame_size - I40E_PACKET_HDR_PAD);
2949 		return -EINVAL;
2950 	}
2951 
2952 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2953 		   netdev->mtu, new_mtu);
2954 	netdev->mtu = new_mtu;
2955 	if (netif_running(netdev))
2956 		i40e_vsi_reinit_locked(vsi);
2957 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2958 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2959 	return 0;
2960 }
2961 
2962 /**
2963  * i40e_ioctl - Access the hwtstamp interface
2964  * @netdev: network interface device structure
2965  * @ifr: interface request data
2966  * @cmd: ioctl command
2967  **/
2968 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2969 {
2970 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2971 	struct i40e_pf *pf = np->vsi->back;
2972 
2973 	switch (cmd) {
2974 	case SIOCGHWTSTAMP:
2975 		return i40e_ptp_get_ts_config(pf, ifr);
2976 	case SIOCSHWTSTAMP:
2977 		return i40e_ptp_set_ts_config(pf, ifr);
2978 	default:
2979 		return -EOPNOTSUPP;
2980 	}
2981 }
2982 
2983 /**
2984  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2985  * @vsi: the vsi being adjusted
2986  **/
2987 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2988 {
2989 	struct i40e_vsi_context ctxt;
2990 	int ret;
2991 
2992 	/* Don't modify stripping options if a port VLAN is active */
2993 	if (vsi->info.pvid)
2994 		return;
2995 
2996 	if ((vsi->info.valid_sections &
2997 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2998 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2999 		return;  /* already enabled */
3000 
3001 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3002 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3003 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
3004 
3005 	ctxt.seid = vsi->seid;
3006 	ctxt.info = vsi->info;
3007 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3008 	if (ret) {
3009 		dev_info(&vsi->back->pdev->dev,
3010 			 "update vlan stripping failed, err %pe aq_err %s\n",
3011 			 ERR_PTR(ret),
3012 			 i40e_aq_str(&vsi->back->hw,
3013 				     vsi->back->hw.aq.asq_last_status));
3014 	}
3015 }
3016 
3017 /**
3018  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
3019  * @vsi: the vsi being adjusted
3020  **/
3021 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
3022 {
3023 	struct i40e_vsi_context ctxt;
3024 	int ret;
3025 
3026 	/* Don't modify stripping options if a port VLAN is active */
3027 	if (vsi->info.pvid)
3028 		return;
3029 
3030 	if ((vsi->info.valid_sections &
3031 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3032 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3033 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
3034 		return;  /* already disabled */
3035 
3036 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3037 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3038 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3039 
3040 	ctxt.seid = vsi->seid;
3041 	ctxt.info = vsi->info;
3042 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3043 	if (ret) {
3044 		dev_info(&vsi->back->pdev->dev,
3045 			 "update vlan stripping failed, err %pe aq_err %s\n",
3046 			 ERR_PTR(ret),
3047 			 i40e_aq_str(&vsi->back->hw,
3048 				     vsi->back->hw.aq.asq_last_status));
3049 	}
3050 }
3051 
3052 /**
3053  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3054  * @vsi: the vsi being configured
3055  * @vid: vlan id to be added (0 = untagged only , -1 = any)
3056  *
3057  * This is a helper function for adding a new MAC/VLAN filter with the
3058  * specified VLAN for each existing MAC address already in the hash table.
3059  * This function does *not* perform any accounting to update filters based on
3060  * VLAN mode.
3061  *
3062  * NOTE: this function expects to be called while under the
3063  * mac_filter_hash_lock
3064  **/
3065 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3066 {
3067 	struct i40e_mac_filter *f, *add_f;
3068 	struct hlist_node *h;
3069 	int bkt;
3070 
3071 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3072 		/* If we're asked to add a filter that has been marked for
3073 		 * removal, it is safe to simply restore it to active state.
3074 		 * __i40e_del_filter will have simply deleted any filters which
3075 		 * were previously marked NEW or FAILED, so if it is currently
3076 		 * marked REMOVE it must have previously been ACTIVE. Since we
3077 		 * haven't yet run the sync filters task, just restore this
3078 		 * filter to the ACTIVE state so that the sync task leaves it
3079 		 * in place.
3080 		 */
3081 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3082 			f->state = I40E_FILTER_ACTIVE;
3083 			continue;
3084 		} else if (f->state == I40E_FILTER_REMOVE) {
3085 			continue;
3086 		}
3087 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
3088 		if (!add_f) {
3089 			dev_info(&vsi->back->pdev->dev,
3090 				 "Could not add vlan filter %d for %pM\n",
3091 				 vid, f->macaddr);
3092 			return -ENOMEM;
3093 		}
3094 	}
3095 
3096 	return 0;
3097 }
3098 
3099 /**
3100  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3101  * @vsi: the VSI being configured
3102  * @vid: VLAN id to be added
3103  **/
3104 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3105 {
3106 	int err;
3107 
3108 	if (vsi->info.pvid)
3109 		return -EINVAL;
3110 
3111 	/* The network stack will attempt to add VID=0, with the intention to
3112 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3113 	 * these packets by default when configured to receive untagged
3114 	 * packets, so we don't need to add a filter for this case.
3115 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3116 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3117 	 * Thus, we do not want to actually add a filter for VID=0
3118 	 */
3119 	if (!vid)
3120 		return 0;
3121 
3122 	/* Locked once because all functions invoked below iterates list*/
3123 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3124 	err = i40e_add_vlan_all_mac(vsi, vid);
3125 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3126 	if (err)
3127 		return err;
3128 
3129 	/* schedule our worker thread which will take care of
3130 	 * applying the new filter changes
3131 	 */
3132 	i40e_service_event_schedule(vsi->back);
3133 	return 0;
3134 }
3135 
3136 /**
3137  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3138  * @vsi: the vsi being configured
3139  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3140  *
3141  * This function should be used to remove all VLAN filters which match the
3142  * given VID. It does not schedule the service event and does not take the
3143  * mac_filter_hash_lock so it may be combined with other operations under
3144  * a single invocation of the mac_filter_hash_lock.
3145  *
3146  * NOTE: this function expects to be called while under the
3147  * mac_filter_hash_lock
3148  */
3149 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3150 {
3151 	struct i40e_mac_filter *f;
3152 	struct hlist_node *h;
3153 	int bkt;
3154 
3155 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3156 		if (f->vlan == vid)
3157 			__i40e_del_filter(vsi, f);
3158 	}
3159 }
3160 
3161 /**
3162  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3163  * @vsi: the VSI being configured
3164  * @vid: VLAN id to be removed
3165  **/
3166 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3167 {
3168 	if (!vid || vsi->info.pvid)
3169 		return;
3170 
3171 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3172 	i40e_rm_vlan_all_mac(vsi, vid);
3173 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3174 
3175 	/* schedule our worker thread which will take care of
3176 	 * applying the new filter changes
3177 	 */
3178 	i40e_service_event_schedule(vsi->back);
3179 }
3180 
3181 /**
3182  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3183  * @netdev: network interface to be adjusted
3184  * @proto: unused protocol value
3185  * @vid: vlan id to be added
3186  *
3187  * net_device_ops implementation for adding vlan ids
3188  **/
3189 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3190 				__always_unused __be16 proto, u16 vid)
3191 {
3192 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3193 	struct i40e_vsi *vsi = np->vsi;
3194 	int ret = 0;
3195 
3196 	if (vid >= VLAN_N_VID)
3197 		return -EINVAL;
3198 
3199 	ret = i40e_vsi_add_vlan(vsi, vid);
3200 	if (!ret)
3201 		set_bit(vid, vsi->active_vlans);
3202 
3203 	return ret;
3204 }
3205 
3206 /**
3207  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3208  * @netdev: network interface to be adjusted
3209  * @proto: unused protocol value
3210  * @vid: vlan id to be added
3211  **/
3212 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3213 				    __always_unused __be16 proto, u16 vid)
3214 {
3215 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3216 	struct i40e_vsi *vsi = np->vsi;
3217 
3218 	if (vid >= VLAN_N_VID)
3219 		return;
3220 	set_bit(vid, vsi->active_vlans);
3221 }
3222 
3223 /**
3224  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3225  * @netdev: network interface to be adjusted
3226  * @proto: unused protocol value
3227  * @vid: vlan id to be removed
3228  *
3229  * net_device_ops implementation for removing vlan ids
3230  **/
3231 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3232 				 __always_unused __be16 proto, u16 vid)
3233 {
3234 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3235 	struct i40e_vsi *vsi = np->vsi;
3236 
3237 	/* return code is ignored as there is nothing a user
3238 	 * can do about failure to remove and a log message was
3239 	 * already printed from the other function
3240 	 */
3241 	i40e_vsi_kill_vlan(vsi, vid);
3242 
3243 	clear_bit(vid, vsi->active_vlans);
3244 
3245 	return 0;
3246 }
3247 
3248 /**
3249  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3250  * @vsi: the vsi being brought back up
3251  **/
3252 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3253 {
3254 	u16 vid;
3255 
3256 	if (!vsi->netdev)
3257 		return;
3258 
3259 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3260 		i40e_vlan_stripping_enable(vsi);
3261 	else
3262 		i40e_vlan_stripping_disable(vsi);
3263 
3264 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3265 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3266 					vid);
3267 }
3268 
3269 /**
3270  * i40e_vsi_add_pvid - Add pvid for the VSI
3271  * @vsi: the vsi being adjusted
3272  * @vid: the vlan id to set as a PVID
3273  **/
3274 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3275 {
3276 	struct i40e_vsi_context ctxt;
3277 	int ret;
3278 
3279 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3280 	vsi->info.pvid = cpu_to_le16(vid);
3281 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3282 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3283 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3284 
3285 	ctxt.seid = vsi->seid;
3286 	ctxt.info = vsi->info;
3287 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3288 	if (ret) {
3289 		dev_info(&vsi->back->pdev->dev,
3290 			 "add pvid failed, err %pe aq_err %s\n",
3291 			 ERR_PTR(ret),
3292 			 i40e_aq_str(&vsi->back->hw,
3293 				     vsi->back->hw.aq.asq_last_status));
3294 		return -ENOENT;
3295 	}
3296 
3297 	return 0;
3298 }
3299 
3300 /**
3301  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3302  * @vsi: the vsi being adjusted
3303  *
3304  * Just use the vlan_rx_register() service to put it back to normal
3305  **/
3306 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3307 {
3308 	vsi->info.pvid = 0;
3309 
3310 	i40e_vlan_stripping_disable(vsi);
3311 }
3312 
3313 /**
3314  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3315  * @vsi: ptr to the VSI
3316  *
3317  * If this function returns with an error, then it's possible one or
3318  * more of the rings is populated (while the rest are not).  It is the
3319  * callers duty to clean those orphaned rings.
3320  *
3321  * Return 0 on success, negative on failure
3322  **/
3323 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3324 {
3325 	int i, err = 0;
3326 
3327 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3328 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3329 
3330 	if (!i40e_enabled_xdp_vsi(vsi))
3331 		return err;
3332 
3333 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3334 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3335 
3336 	return err;
3337 }
3338 
3339 /**
3340  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3341  * @vsi: ptr to the VSI
3342  *
3343  * Free VSI's transmit software resources
3344  **/
3345 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3346 {
3347 	int i;
3348 
3349 	if (vsi->tx_rings) {
3350 		for (i = 0; i < vsi->num_queue_pairs; i++)
3351 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3352 				i40e_free_tx_resources(vsi->tx_rings[i]);
3353 	}
3354 
3355 	if (vsi->xdp_rings) {
3356 		for (i = 0; i < vsi->num_queue_pairs; i++)
3357 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3358 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3359 	}
3360 }
3361 
3362 /**
3363  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3364  * @vsi: ptr to the VSI
3365  *
3366  * If this function returns with an error, then it's possible one or
3367  * more of the rings is populated (while the rest are not).  It is the
3368  * callers duty to clean those orphaned rings.
3369  *
3370  * Return 0 on success, negative on failure
3371  **/
3372 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3373 {
3374 	int i, err = 0;
3375 
3376 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3377 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3378 	return err;
3379 }
3380 
3381 /**
3382  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3383  * @vsi: ptr to the VSI
3384  *
3385  * Free all receive software resources
3386  **/
3387 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3388 {
3389 	int i;
3390 
3391 	if (!vsi->rx_rings)
3392 		return;
3393 
3394 	for (i = 0; i < vsi->num_queue_pairs; i++)
3395 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3396 			i40e_free_rx_resources(vsi->rx_rings[i]);
3397 }
3398 
3399 /**
3400  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3401  * @ring: The Tx ring to configure
3402  *
3403  * This enables/disables XPS for a given Tx descriptor ring
3404  * based on the TCs enabled for the VSI that ring belongs to.
3405  **/
3406 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3407 {
3408 	int cpu;
3409 
3410 	if (!ring->q_vector || !ring->netdev || ring->ch)
3411 		return;
3412 
3413 	/* We only initialize XPS once, so as not to overwrite user settings */
3414 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3415 		return;
3416 
3417 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3418 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3419 			    ring->queue_index);
3420 }
3421 
3422 /**
3423  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3424  * @ring: The Tx or Rx ring
3425  *
3426  * Returns the AF_XDP buffer pool or NULL.
3427  **/
3428 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3429 {
3430 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3431 	int qid = ring->queue_index;
3432 
3433 	if (ring_is_xdp(ring))
3434 		qid -= ring->vsi->alloc_queue_pairs;
3435 
3436 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3437 		return NULL;
3438 
3439 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3440 }
3441 
3442 /**
3443  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3444  * @ring: The Tx ring to configure
3445  *
3446  * Configure the Tx descriptor ring in the HMC context.
3447  **/
3448 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3449 {
3450 	struct i40e_vsi *vsi = ring->vsi;
3451 	u16 pf_q = vsi->base_queue + ring->queue_index;
3452 	struct i40e_hw *hw = &vsi->back->hw;
3453 	struct i40e_hmc_obj_txq tx_ctx;
3454 	u32 qtx_ctl = 0;
3455 	int err = 0;
3456 
3457 	if (ring_is_xdp(ring))
3458 		ring->xsk_pool = i40e_xsk_pool(ring);
3459 
3460 	/* some ATR related tx ring init */
3461 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3462 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3463 		ring->atr_count = 0;
3464 	} else {
3465 		ring->atr_sample_rate = 0;
3466 	}
3467 
3468 	/* configure XPS */
3469 	i40e_config_xps_tx_ring(ring);
3470 
3471 	/* clear the context structure first */
3472 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3473 
3474 	tx_ctx.new_context = 1;
3475 	tx_ctx.base = (ring->dma / 128);
3476 	tx_ctx.qlen = ring->count;
3477 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3478 					       I40E_FLAG_FD_ATR_ENABLED));
3479 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3480 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3481 	if (vsi->type != I40E_VSI_FDIR)
3482 		tx_ctx.head_wb_ena = 1;
3483 	tx_ctx.head_wb_addr = ring->dma +
3484 			      (ring->count * sizeof(struct i40e_tx_desc));
3485 
3486 	/* As part of VSI creation/update, FW allocates certain
3487 	 * Tx arbitration queue sets for each TC enabled for
3488 	 * the VSI. The FW returns the handles to these queue
3489 	 * sets as part of the response buffer to Add VSI,
3490 	 * Update VSI, etc. AQ commands. It is expected that
3491 	 * these queue set handles be associated with the Tx
3492 	 * queues by the driver as part of the TX queue context
3493 	 * initialization. This has to be done regardless of
3494 	 * DCB as by default everything is mapped to TC0.
3495 	 */
3496 
3497 	if (ring->ch)
3498 		tx_ctx.rdylist =
3499 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3500 
3501 	else
3502 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3503 
3504 	tx_ctx.rdylist_act = 0;
3505 
3506 	/* clear the context in the HMC */
3507 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3508 	if (err) {
3509 		dev_info(&vsi->back->pdev->dev,
3510 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3511 			 ring->queue_index, pf_q, err);
3512 		return -ENOMEM;
3513 	}
3514 
3515 	/* set the context in the HMC */
3516 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3517 	if (err) {
3518 		dev_info(&vsi->back->pdev->dev,
3519 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3520 			 ring->queue_index, pf_q, err);
3521 		return -ENOMEM;
3522 	}
3523 
3524 	/* Now associate this queue with this PCI function */
3525 	if (ring->ch) {
3526 		if (ring->ch->type == I40E_VSI_VMDQ2)
3527 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3528 		else
3529 			return -EINVAL;
3530 
3531 		qtx_ctl |= (ring->ch->vsi_number <<
3532 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3533 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3534 	} else {
3535 		if (vsi->type == I40E_VSI_VMDQ2) {
3536 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3537 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3538 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3539 		} else {
3540 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3541 		}
3542 	}
3543 
3544 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3545 		    I40E_QTX_CTL_PF_INDX_MASK);
3546 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3547 	i40e_flush(hw);
3548 
3549 	/* cache tail off for easier writes later */
3550 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3551 
3552 	return 0;
3553 }
3554 
3555 /**
3556  * i40e_rx_offset - Return expected offset into page to access data
3557  * @rx_ring: Ring we are requesting offset of
3558  *
3559  * Returns the offset value for ring into the data buffer.
3560  */
3561 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3562 {
3563 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3564 }
3565 
3566 /**
3567  * i40e_configure_rx_ring - Configure a receive ring context
3568  * @ring: The Rx ring to configure
3569  *
3570  * Configure the Rx descriptor ring in the HMC context.
3571  **/
3572 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3573 {
3574 	struct i40e_vsi *vsi = ring->vsi;
3575 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3576 	u16 pf_q = vsi->base_queue + ring->queue_index;
3577 	struct i40e_hw *hw = &vsi->back->hw;
3578 	struct i40e_hmc_obj_rxq rx_ctx;
3579 	int err = 0;
3580 	bool ok;
3581 	int ret;
3582 
3583 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3584 
3585 	/* clear the context structure first */
3586 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3587 
3588 	if (ring->vsi->type == I40E_VSI_MAIN)
3589 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3590 
3591 	ring->xsk_pool = i40e_xsk_pool(ring);
3592 	if (ring->xsk_pool) {
3593 		ring->rx_buf_len =
3594 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3595 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3596 						 MEM_TYPE_XSK_BUFF_POOL,
3597 						 NULL);
3598 		if (ret)
3599 			return ret;
3600 		dev_info(&vsi->back->pdev->dev,
3601 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3602 			 ring->queue_index);
3603 
3604 	} else {
3605 		ring->rx_buf_len = vsi->rx_buf_len;
3606 		if (ring->vsi->type == I40E_VSI_MAIN) {
3607 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3608 							 MEM_TYPE_PAGE_SHARED,
3609 							 NULL);
3610 			if (ret)
3611 				return ret;
3612 		}
3613 	}
3614 
3615 	xdp_init_buff(&ring->xdp, i40e_rx_pg_size(ring) / 2, &ring->xdp_rxq);
3616 
3617 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3618 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3619 
3620 	rx_ctx.base = (ring->dma / 128);
3621 	rx_ctx.qlen = ring->count;
3622 
3623 	/* use 16 byte descriptors */
3624 	rx_ctx.dsize = 0;
3625 
3626 	/* descriptor type is always zero
3627 	 * rx_ctx.dtype = 0;
3628 	 */
3629 	rx_ctx.hsplit_0 = 0;
3630 
3631 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3632 	if (hw->revision_id == 0)
3633 		rx_ctx.lrxqthresh = 0;
3634 	else
3635 		rx_ctx.lrxqthresh = 1;
3636 	rx_ctx.crcstrip = 1;
3637 	rx_ctx.l2tsel = 1;
3638 	/* this controls whether VLAN is stripped from inner headers */
3639 	rx_ctx.showiv = 0;
3640 	/* set the prefena field to 1 because the manual says to */
3641 	rx_ctx.prefena = 1;
3642 
3643 	/* clear the context in the HMC */
3644 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3645 	if (err) {
3646 		dev_info(&vsi->back->pdev->dev,
3647 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3648 			 ring->queue_index, pf_q, err);
3649 		return -ENOMEM;
3650 	}
3651 
3652 	/* set the context in the HMC */
3653 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3654 	if (err) {
3655 		dev_info(&vsi->back->pdev->dev,
3656 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3657 			 ring->queue_index, pf_q, err);
3658 		return -ENOMEM;
3659 	}
3660 
3661 	/* configure Rx buffer alignment */
3662 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3663 		if (I40E_2K_TOO_SMALL_WITH_PADDING) {
3664 			dev_info(&vsi->back->pdev->dev,
3665 				 "2k Rx buffer is too small to fit standard MTU and skb_shared_info\n");
3666 			return -EOPNOTSUPP;
3667 		}
3668 		clear_ring_build_skb_enabled(ring);
3669 	} else {
3670 		set_ring_build_skb_enabled(ring);
3671 	}
3672 
3673 	ring->rx_offset = i40e_rx_offset(ring);
3674 
3675 	/* cache tail for quicker writes, and clear the reg before use */
3676 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3677 	writel(0, ring->tail);
3678 
3679 	if (ring->xsk_pool) {
3680 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3681 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3682 	} else {
3683 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3684 	}
3685 	if (!ok) {
3686 		/* Log this in case the user has forgotten to give the kernel
3687 		 * any buffers, even later in the application.
3688 		 */
3689 		dev_info(&vsi->back->pdev->dev,
3690 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3691 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3692 			 ring->queue_index, pf_q);
3693 	}
3694 
3695 	return 0;
3696 }
3697 
3698 /**
3699  * i40e_vsi_configure_tx - Configure the VSI for Tx
3700  * @vsi: VSI structure describing this set of rings and resources
3701  *
3702  * Configure the Tx VSI for operation.
3703  **/
3704 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3705 {
3706 	int err = 0;
3707 	u16 i;
3708 
3709 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3710 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3711 
3712 	if (err || !i40e_enabled_xdp_vsi(vsi))
3713 		return err;
3714 
3715 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3716 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3717 
3718 	return err;
3719 }
3720 
3721 /**
3722  * i40e_vsi_configure_rx - Configure the VSI for Rx
3723  * @vsi: the VSI being configured
3724  *
3725  * Configure the Rx VSI for operation.
3726  **/
3727 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3728 {
3729 	int err = 0;
3730 	u16 i;
3731 
3732 	vsi->max_frame = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
3733 	vsi->rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
3734 
3735 #if (PAGE_SIZE < 8192)
3736 	if (vsi->netdev && !I40E_2K_TOO_SMALL_WITH_PADDING &&
3737 	    vsi->netdev->mtu <= ETH_DATA_LEN) {
3738 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3739 		vsi->max_frame = vsi->rx_buf_len;
3740 	}
3741 #endif
3742 
3743 	/* set up individual rings */
3744 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3745 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3746 
3747 	return err;
3748 }
3749 
3750 /**
3751  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3752  * @vsi: ptr to the VSI
3753  **/
3754 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3755 {
3756 	struct i40e_ring *tx_ring, *rx_ring;
3757 	u16 qoffset, qcount;
3758 	int i, n;
3759 
3760 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3761 		/* Reset the TC information */
3762 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3763 			rx_ring = vsi->rx_rings[i];
3764 			tx_ring = vsi->tx_rings[i];
3765 			rx_ring->dcb_tc = 0;
3766 			tx_ring->dcb_tc = 0;
3767 		}
3768 		return;
3769 	}
3770 
3771 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3772 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3773 			continue;
3774 
3775 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3776 		qcount = vsi->tc_config.tc_info[n].qcount;
3777 		for (i = qoffset; i < (qoffset + qcount); i++) {
3778 			rx_ring = vsi->rx_rings[i];
3779 			tx_ring = vsi->tx_rings[i];
3780 			rx_ring->dcb_tc = n;
3781 			tx_ring->dcb_tc = n;
3782 		}
3783 	}
3784 }
3785 
3786 /**
3787  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3788  * @vsi: ptr to the VSI
3789  **/
3790 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3791 {
3792 	if (vsi->netdev)
3793 		i40e_set_rx_mode(vsi->netdev);
3794 }
3795 
3796 /**
3797  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3798  * @pf: Pointer to the targeted PF
3799  *
3800  * Set all flow director counters to 0.
3801  */
3802 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3803 {
3804 	pf->fd_tcp4_filter_cnt = 0;
3805 	pf->fd_udp4_filter_cnt = 0;
3806 	pf->fd_sctp4_filter_cnt = 0;
3807 	pf->fd_ip4_filter_cnt = 0;
3808 	pf->fd_tcp6_filter_cnt = 0;
3809 	pf->fd_udp6_filter_cnt = 0;
3810 	pf->fd_sctp6_filter_cnt = 0;
3811 	pf->fd_ip6_filter_cnt = 0;
3812 }
3813 
3814 /**
3815  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3816  * @vsi: Pointer to the targeted VSI
3817  *
3818  * This function replays the hlist on the hw where all the SB Flow Director
3819  * filters were saved.
3820  **/
3821 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3822 {
3823 	struct i40e_fdir_filter *filter;
3824 	struct i40e_pf *pf = vsi->back;
3825 	struct hlist_node *node;
3826 
3827 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3828 		return;
3829 
3830 	/* Reset FDir counters as we're replaying all existing filters */
3831 	i40e_reset_fdir_filter_cnt(pf);
3832 
3833 	hlist_for_each_entry_safe(filter, node,
3834 				  &pf->fdir_filter_list, fdir_node) {
3835 		i40e_add_del_fdir(vsi, filter, true);
3836 	}
3837 }
3838 
3839 /**
3840  * i40e_vsi_configure - Set up the VSI for action
3841  * @vsi: the VSI being configured
3842  **/
3843 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3844 {
3845 	int err;
3846 
3847 	i40e_set_vsi_rx_mode(vsi);
3848 	i40e_restore_vlan(vsi);
3849 	i40e_vsi_config_dcb_rings(vsi);
3850 	err = i40e_vsi_configure_tx(vsi);
3851 	if (!err)
3852 		err = i40e_vsi_configure_rx(vsi);
3853 
3854 	return err;
3855 }
3856 
3857 /**
3858  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3859  * @vsi: the VSI being configured
3860  **/
3861 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3862 {
3863 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3864 	struct i40e_pf *pf = vsi->back;
3865 	struct i40e_hw *hw = &pf->hw;
3866 	u16 vector;
3867 	int i, q;
3868 	u32 qp;
3869 
3870 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3871 	 * and PFINT_LNKLSTn registers, e.g.:
3872 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3873 	 */
3874 	qp = vsi->base_queue;
3875 	vector = vsi->base_vector;
3876 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3877 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3878 
3879 		q_vector->rx.next_update = jiffies + 1;
3880 		q_vector->rx.target_itr =
3881 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3882 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3883 		     q_vector->rx.target_itr >> 1);
3884 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3885 
3886 		q_vector->tx.next_update = jiffies + 1;
3887 		q_vector->tx.target_itr =
3888 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3889 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3890 		     q_vector->tx.target_itr >> 1);
3891 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3892 
3893 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3894 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3895 
3896 		/* begin of linked list for RX queue assigned to this vector */
3897 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3898 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3899 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3900 			u32 val;
3901 
3902 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3903 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3904 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3905 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3906 			      (I40E_QUEUE_TYPE_TX <<
3907 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3908 
3909 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3910 
3911 			if (has_xdp) {
3912 				/* TX queue with next queue set to TX */
3913 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3914 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3915 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3916 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3917 				      (I40E_QUEUE_TYPE_TX <<
3918 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3919 
3920 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3921 			}
3922 			/* TX queue with next RX or end of linked list */
3923 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3924 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3925 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3926 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3927 			      (I40E_QUEUE_TYPE_RX <<
3928 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3929 
3930 			/* Terminate the linked list */
3931 			if (q == (q_vector->num_ringpairs - 1))
3932 				val |= (I40E_QUEUE_END_OF_LIST <<
3933 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3934 
3935 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3936 			qp++;
3937 		}
3938 	}
3939 
3940 	i40e_flush(hw);
3941 }
3942 
3943 /**
3944  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3945  * @pf: pointer to private device data structure
3946  **/
3947 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3948 {
3949 	struct i40e_hw *hw = &pf->hw;
3950 	u32 val;
3951 
3952 	/* clear things first */
3953 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3954 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3955 
3956 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3957 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3958 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3959 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3960 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3961 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3962 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3963 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3964 
3965 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3966 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3967 
3968 	if (pf->flags & I40E_FLAG_PTP)
3969 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3970 
3971 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3972 
3973 	/* SW_ITR_IDX = 0, but don't change INTENA */
3974 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3975 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3976 
3977 	/* OTHER_ITR_IDX = 0 */
3978 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3979 }
3980 
3981 /**
3982  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3983  * @vsi: the VSI being configured
3984  **/
3985 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3986 {
3987 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3988 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3989 	struct i40e_pf *pf = vsi->back;
3990 	struct i40e_hw *hw = &pf->hw;
3991 
3992 	/* set the ITR configuration */
3993 	q_vector->rx.next_update = jiffies + 1;
3994 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3995 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3996 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3997 	q_vector->tx.next_update = jiffies + 1;
3998 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3999 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
4000 	q_vector->tx.current_itr = q_vector->tx.target_itr;
4001 
4002 	i40e_enable_misc_int_causes(pf);
4003 
4004 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
4005 	wr32(hw, I40E_PFINT_LNKLST0, 0);
4006 
4007 	/* Associate the queue pair to the vector and enable the queue
4008 	 * interrupt RX queue in linked list with next queue set to TX
4009 	 */
4010 	wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
4011 
4012 	if (i40e_enabled_xdp_vsi(vsi)) {
4013 		/* TX queue in linked list with next queue set to TX */
4014 		wr32(hw, I40E_QINT_TQCTL(nextqp),
4015 		     I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
4016 	}
4017 
4018 	/* last TX queue so the next RX queue doesn't matter */
4019 	wr32(hw, I40E_QINT_TQCTL(0),
4020 	     I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
4021 	i40e_flush(hw);
4022 }
4023 
4024 /**
4025  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4026  * @pf: board private structure
4027  **/
4028 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4029 {
4030 	struct i40e_hw *hw = &pf->hw;
4031 
4032 	wr32(hw, I40E_PFINT_DYN_CTL0,
4033 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4034 	i40e_flush(hw);
4035 }
4036 
4037 /**
4038  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4039  * @pf: board private structure
4040  **/
4041 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4042 {
4043 	struct i40e_hw *hw = &pf->hw;
4044 	u32 val;
4045 
4046 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4047 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4048 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4049 
4050 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4051 	i40e_flush(hw);
4052 }
4053 
4054 /**
4055  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4056  * @irq: interrupt number
4057  * @data: pointer to a q_vector
4058  **/
4059 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4060 {
4061 	struct i40e_q_vector *q_vector = data;
4062 
4063 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4064 		return IRQ_HANDLED;
4065 
4066 	napi_schedule_irqoff(&q_vector->napi);
4067 
4068 	return IRQ_HANDLED;
4069 }
4070 
4071 /**
4072  * i40e_irq_affinity_notify - Callback for affinity changes
4073  * @notify: context as to what irq was changed
4074  * @mask: the new affinity mask
4075  *
4076  * This is a callback function used by the irq_set_affinity_notifier function
4077  * so that we may register to receive changes to the irq affinity masks.
4078  **/
4079 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4080 				     const cpumask_t *mask)
4081 {
4082 	struct i40e_q_vector *q_vector =
4083 		container_of(notify, struct i40e_q_vector, affinity_notify);
4084 
4085 	cpumask_copy(&q_vector->affinity_mask, mask);
4086 }
4087 
4088 /**
4089  * i40e_irq_affinity_release - Callback for affinity notifier release
4090  * @ref: internal core kernel usage
4091  *
4092  * This is a callback function used by the irq_set_affinity_notifier function
4093  * to inform the current notification subscriber that they will no longer
4094  * receive notifications.
4095  **/
4096 static void i40e_irq_affinity_release(struct kref *ref) {}
4097 
4098 /**
4099  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4100  * @vsi: the VSI being configured
4101  * @basename: name for the vector
4102  *
4103  * Allocates MSI-X vectors and requests interrupts from the kernel.
4104  **/
4105 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4106 {
4107 	int q_vectors = vsi->num_q_vectors;
4108 	struct i40e_pf *pf = vsi->back;
4109 	int base = vsi->base_vector;
4110 	int rx_int_idx = 0;
4111 	int tx_int_idx = 0;
4112 	int vector, err;
4113 	int irq_num;
4114 	int cpu;
4115 
4116 	for (vector = 0; vector < q_vectors; vector++) {
4117 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4118 
4119 		irq_num = pf->msix_entries[base + vector].vector;
4120 
4121 		if (q_vector->tx.ring && q_vector->rx.ring) {
4122 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4123 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4124 			tx_int_idx++;
4125 		} else if (q_vector->rx.ring) {
4126 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4127 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4128 		} else if (q_vector->tx.ring) {
4129 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4130 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4131 		} else {
4132 			/* skip this unused q_vector */
4133 			continue;
4134 		}
4135 		err = request_irq(irq_num,
4136 				  vsi->irq_handler,
4137 				  0,
4138 				  q_vector->name,
4139 				  q_vector);
4140 		if (err) {
4141 			dev_info(&pf->pdev->dev,
4142 				 "MSIX request_irq failed, error: %d\n", err);
4143 			goto free_queue_irqs;
4144 		}
4145 
4146 		/* register for affinity change notifications */
4147 		q_vector->irq_num = irq_num;
4148 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4149 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4150 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4151 		/* Spread affinity hints out across online CPUs.
4152 		 *
4153 		 * get_cpu_mask returns a static constant mask with
4154 		 * a permanent lifetime so it's ok to pass to
4155 		 * irq_update_affinity_hint without making a copy.
4156 		 */
4157 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4158 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4159 	}
4160 
4161 	vsi->irqs_ready = true;
4162 	return 0;
4163 
4164 free_queue_irqs:
4165 	while (vector) {
4166 		vector--;
4167 		irq_num = pf->msix_entries[base + vector].vector;
4168 		irq_set_affinity_notifier(irq_num, NULL);
4169 		irq_update_affinity_hint(irq_num, NULL);
4170 		free_irq(irq_num, &vsi->q_vectors[vector]);
4171 	}
4172 	return err;
4173 }
4174 
4175 /**
4176  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4177  * @vsi: the VSI being un-configured
4178  **/
4179 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4180 {
4181 	struct i40e_pf *pf = vsi->back;
4182 	struct i40e_hw *hw = &pf->hw;
4183 	int base = vsi->base_vector;
4184 	int i;
4185 
4186 	/* disable interrupt causation from each queue */
4187 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4188 		u32 val;
4189 
4190 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4191 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4192 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4193 
4194 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4195 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4196 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4197 
4198 		if (!i40e_enabled_xdp_vsi(vsi))
4199 			continue;
4200 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4201 	}
4202 
4203 	/* disable each interrupt */
4204 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4205 		for (i = vsi->base_vector;
4206 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4207 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4208 
4209 		i40e_flush(hw);
4210 		for (i = 0; i < vsi->num_q_vectors; i++)
4211 			synchronize_irq(pf->msix_entries[i + base].vector);
4212 	} else {
4213 		/* Legacy and MSI mode - this stops all interrupt handling */
4214 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4215 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4216 		i40e_flush(hw);
4217 		synchronize_irq(pf->pdev->irq);
4218 	}
4219 }
4220 
4221 /**
4222  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4223  * @vsi: the VSI being configured
4224  **/
4225 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4226 {
4227 	struct i40e_pf *pf = vsi->back;
4228 	int i;
4229 
4230 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4231 		for (i = 0; i < vsi->num_q_vectors; i++)
4232 			i40e_irq_dynamic_enable(vsi, i);
4233 	} else {
4234 		i40e_irq_dynamic_enable_icr0(pf);
4235 	}
4236 
4237 	i40e_flush(&pf->hw);
4238 	return 0;
4239 }
4240 
4241 /**
4242  * i40e_free_misc_vector - Free the vector that handles non-queue events
4243  * @pf: board private structure
4244  **/
4245 static void i40e_free_misc_vector(struct i40e_pf *pf)
4246 {
4247 	/* Disable ICR 0 */
4248 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4249 	i40e_flush(&pf->hw);
4250 
4251 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4252 		free_irq(pf->msix_entries[0].vector, pf);
4253 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4254 	}
4255 }
4256 
4257 /**
4258  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4259  * @irq: interrupt number
4260  * @data: pointer to a q_vector
4261  *
4262  * This is the handler used for all MSI/Legacy interrupts, and deals
4263  * with both queue and non-queue interrupts.  This is also used in
4264  * MSIX mode to handle the non-queue interrupts.
4265  **/
4266 static irqreturn_t i40e_intr(int irq, void *data)
4267 {
4268 	struct i40e_pf *pf = (struct i40e_pf *)data;
4269 	struct i40e_hw *hw = &pf->hw;
4270 	irqreturn_t ret = IRQ_NONE;
4271 	u32 icr0, icr0_remaining;
4272 	u32 val, ena_mask;
4273 
4274 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4275 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4276 
4277 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4278 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4279 		goto enable_intr;
4280 
4281 	/* if interrupt but no bits showing, must be SWINT */
4282 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4283 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4284 		pf->sw_int_count++;
4285 
4286 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4287 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4288 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4289 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4290 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4291 	}
4292 
4293 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4294 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4295 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4296 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4297 
4298 		/* We do not have a way to disarm Queue causes while leaving
4299 		 * interrupt enabled for all other causes, ideally
4300 		 * interrupt should be disabled while we are in NAPI but
4301 		 * this is not a performance path and napi_schedule()
4302 		 * can deal with rescheduling.
4303 		 */
4304 		if (!test_bit(__I40E_DOWN, pf->state))
4305 			napi_schedule_irqoff(&q_vector->napi);
4306 	}
4307 
4308 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4309 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4310 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4311 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4312 	}
4313 
4314 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4315 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4316 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4317 	}
4318 
4319 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4320 		/* disable any further VFLR event notifications */
4321 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4322 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4323 
4324 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4325 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4326 		} else {
4327 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4328 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4329 		}
4330 	}
4331 
4332 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4333 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4334 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4335 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4336 		val = rd32(hw, I40E_GLGEN_RSTAT);
4337 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4338 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4339 		if (val == I40E_RESET_CORER) {
4340 			pf->corer_count++;
4341 		} else if (val == I40E_RESET_GLOBR) {
4342 			pf->globr_count++;
4343 		} else if (val == I40E_RESET_EMPR) {
4344 			pf->empr_count++;
4345 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4346 		}
4347 	}
4348 
4349 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4350 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4351 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4352 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4353 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4354 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4355 	}
4356 
4357 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4358 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4359 
4360 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4361 			schedule_work(&pf->ptp_extts0_work);
4362 
4363 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4364 			i40e_ptp_tx_hwtstamp(pf);
4365 
4366 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4367 	}
4368 
4369 	/* If a critical error is pending we have no choice but to reset the
4370 	 * device.
4371 	 * Report and mask out any remaining unexpected interrupts.
4372 	 */
4373 	icr0_remaining = icr0 & ena_mask;
4374 	if (icr0_remaining) {
4375 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4376 			 icr0_remaining);
4377 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4378 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4379 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4380 			dev_info(&pf->pdev->dev, "device will be reset\n");
4381 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4382 			i40e_service_event_schedule(pf);
4383 		}
4384 		ena_mask &= ~icr0_remaining;
4385 	}
4386 	ret = IRQ_HANDLED;
4387 
4388 enable_intr:
4389 	/* re-enable interrupt causes */
4390 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4391 	if (!test_bit(__I40E_DOWN, pf->state) ||
4392 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4393 		i40e_service_event_schedule(pf);
4394 		i40e_irq_dynamic_enable_icr0(pf);
4395 	}
4396 
4397 	return ret;
4398 }
4399 
4400 /**
4401  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4402  * @tx_ring:  tx ring to clean
4403  * @budget:   how many cleans we're allowed
4404  *
4405  * Returns true if there's any budget left (e.g. the clean is finished)
4406  **/
4407 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4408 {
4409 	struct i40e_vsi *vsi = tx_ring->vsi;
4410 	u16 i = tx_ring->next_to_clean;
4411 	struct i40e_tx_buffer *tx_buf;
4412 	struct i40e_tx_desc *tx_desc;
4413 
4414 	tx_buf = &tx_ring->tx_bi[i];
4415 	tx_desc = I40E_TX_DESC(tx_ring, i);
4416 	i -= tx_ring->count;
4417 
4418 	do {
4419 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4420 
4421 		/* if next_to_watch is not set then there is no work pending */
4422 		if (!eop_desc)
4423 			break;
4424 
4425 		/* prevent any other reads prior to eop_desc */
4426 		smp_rmb();
4427 
4428 		/* if the descriptor isn't done, no work yet to do */
4429 		if (!(eop_desc->cmd_type_offset_bsz &
4430 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4431 			break;
4432 
4433 		/* clear next_to_watch to prevent false hangs */
4434 		tx_buf->next_to_watch = NULL;
4435 
4436 		tx_desc->buffer_addr = 0;
4437 		tx_desc->cmd_type_offset_bsz = 0;
4438 		/* move past filter desc */
4439 		tx_buf++;
4440 		tx_desc++;
4441 		i++;
4442 		if (unlikely(!i)) {
4443 			i -= tx_ring->count;
4444 			tx_buf = tx_ring->tx_bi;
4445 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4446 		}
4447 		/* unmap skb header data */
4448 		dma_unmap_single(tx_ring->dev,
4449 				 dma_unmap_addr(tx_buf, dma),
4450 				 dma_unmap_len(tx_buf, len),
4451 				 DMA_TO_DEVICE);
4452 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4453 			kfree(tx_buf->raw_buf);
4454 
4455 		tx_buf->raw_buf = NULL;
4456 		tx_buf->tx_flags = 0;
4457 		tx_buf->next_to_watch = NULL;
4458 		dma_unmap_len_set(tx_buf, len, 0);
4459 		tx_desc->buffer_addr = 0;
4460 		tx_desc->cmd_type_offset_bsz = 0;
4461 
4462 		/* move us past the eop_desc for start of next FD desc */
4463 		tx_buf++;
4464 		tx_desc++;
4465 		i++;
4466 		if (unlikely(!i)) {
4467 			i -= tx_ring->count;
4468 			tx_buf = tx_ring->tx_bi;
4469 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4470 		}
4471 
4472 		/* update budget accounting */
4473 		budget--;
4474 	} while (likely(budget));
4475 
4476 	i += tx_ring->count;
4477 	tx_ring->next_to_clean = i;
4478 
4479 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4480 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4481 
4482 	return budget > 0;
4483 }
4484 
4485 /**
4486  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4487  * @irq: interrupt number
4488  * @data: pointer to a q_vector
4489  **/
4490 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4491 {
4492 	struct i40e_q_vector *q_vector = data;
4493 	struct i40e_vsi *vsi;
4494 
4495 	if (!q_vector->tx.ring)
4496 		return IRQ_HANDLED;
4497 
4498 	vsi = q_vector->tx.ring->vsi;
4499 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4500 
4501 	return IRQ_HANDLED;
4502 }
4503 
4504 /**
4505  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4506  * @vsi: the VSI being configured
4507  * @v_idx: vector index
4508  * @qp_idx: queue pair index
4509  **/
4510 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4511 {
4512 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4513 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4514 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4515 
4516 	tx_ring->q_vector = q_vector;
4517 	tx_ring->next = q_vector->tx.ring;
4518 	q_vector->tx.ring = tx_ring;
4519 	q_vector->tx.count++;
4520 
4521 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4522 	if (i40e_enabled_xdp_vsi(vsi)) {
4523 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4524 
4525 		xdp_ring->q_vector = q_vector;
4526 		xdp_ring->next = q_vector->tx.ring;
4527 		q_vector->tx.ring = xdp_ring;
4528 		q_vector->tx.count++;
4529 	}
4530 
4531 	rx_ring->q_vector = q_vector;
4532 	rx_ring->next = q_vector->rx.ring;
4533 	q_vector->rx.ring = rx_ring;
4534 	q_vector->rx.count++;
4535 }
4536 
4537 /**
4538  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4539  * @vsi: the VSI being configured
4540  *
4541  * This function maps descriptor rings to the queue-specific vectors
4542  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4543  * one vector per queue pair, but on a constrained vector budget, we
4544  * group the queue pairs as "efficiently" as possible.
4545  **/
4546 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4547 {
4548 	int qp_remaining = vsi->num_queue_pairs;
4549 	int q_vectors = vsi->num_q_vectors;
4550 	int num_ringpairs;
4551 	int v_start = 0;
4552 	int qp_idx = 0;
4553 
4554 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4555 	 * group them so there are multiple queues per vector.
4556 	 * It is also important to go through all the vectors available to be
4557 	 * sure that if we don't use all the vectors, that the remaining vectors
4558 	 * are cleared. This is especially important when decreasing the
4559 	 * number of queues in use.
4560 	 */
4561 	for (; v_start < q_vectors; v_start++) {
4562 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4563 
4564 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4565 
4566 		q_vector->num_ringpairs = num_ringpairs;
4567 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4568 
4569 		q_vector->rx.count = 0;
4570 		q_vector->tx.count = 0;
4571 		q_vector->rx.ring = NULL;
4572 		q_vector->tx.ring = NULL;
4573 
4574 		while (num_ringpairs--) {
4575 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4576 			qp_idx++;
4577 			qp_remaining--;
4578 		}
4579 	}
4580 }
4581 
4582 /**
4583  * i40e_vsi_request_irq - Request IRQ from the OS
4584  * @vsi: the VSI being configured
4585  * @basename: name for the vector
4586  **/
4587 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4588 {
4589 	struct i40e_pf *pf = vsi->back;
4590 	int err;
4591 
4592 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4593 		err = i40e_vsi_request_irq_msix(vsi, basename);
4594 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4595 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4596 				  pf->int_name, pf);
4597 	else
4598 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4599 				  pf->int_name, pf);
4600 
4601 	if (err)
4602 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4603 
4604 	return err;
4605 }
4606 
4607 #ifdef CONFIG_NET_POLL_CONTROLLER
4608 /**
4609  * i40e_netpoll - A Polling 'interrupt' handler
4610  * @netdev: network interface device structure
4611  *
4612  * This is used by netconsole to send skbs without having to re-enable
4613  * interrupts.  It's not called while the normal interrupt routine is executing.
4614  **/
4615 static void i40e_netpoll(struct net_device *netdev)
4616 {
4617 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4618 	struct i40e_vsi *vsi = np->vsi;
4619 	struct i40e_pf *pf = vsi->back;
4620 	int i;
4621 
4622 	/* if interface is down do nothing */
4623 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4624 		return;
4625 
4626 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4627 		for (i = 0; i < vsi->num_q_vectors; i++)
4628 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4629 	} else {
4630 		i40e_intr(pf->pdev->irq, netdev);
4631 	}
4632 }
4633 #endif
4634 
4635 #define I40E_QTX_ENA_WAIT_COUNT 50
4636 
4637 /**
4638  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4639  * @pf: the PF being configured
4640  * @pf_q: the PF queue
4641  * @enable: enable or disable state of the queue
4642  *
4643  * This routine will wait for the given Tx queue of the PF to reach the
4644  * enabled or disabled state.
4645  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4646  * multiple retries; else will return 0 in case of success.
4647  **/
4648 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4649 {
4650 	int i;
4651 	u32 tx_reg;
4652 
4653 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4654 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4655 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4656 			break;
4657 
4658 		usleep_range(10, 20);
4659 	}
4660 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4661 		return -ETIMEDOUT;
4662 
4663 	return 0;
4664 }
4665 
4666 /**
4667  * i40e_control_tx_q - Start or stop a particular Tx queue
4668  * @pf: the PF structure
4669  * @pf_q: the PF queue to configure
4670  * @enable: start or stop the queue
4671  *
4672  * This function enables or disables a single queue. Note that any delay
4673  * required after the operation is expected to be handled by the caller of
4674  * this function.
4675  **/
4676 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4677 {
4678 	struct i40e_hw *hw = &pf->hw;
4679 	u32 tx_reg;
4680 	int i;
4681 
4682 	/* warn the TX unit of coming changes */
4683 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4684 	if (!enable)
4685 		usleep_range(10, 20);
4686 
4687 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4688 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4689 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4690 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4691 			break;
4692 		usleep_range(1000, 2000);
4693 	}
4694 
4695 	/* Skip if the queue is already in the requested state */
4696 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4697 		return;
4698 
4699 	/* turn on/off the queue */
4700 	if (enable) {
4701 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4702 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4703 	} else {
4704 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4705 	}
4706 
4707 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4708 }
4709 
4710 /**
4711  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4712  * @seid: VSI SEID
4713  * @pf: the PF structure
4714  * @pf_q: the PF queue to configure
4715  * @is_xdp: true if the queue is used for XDP
4716  * @enable: start or stop the queue
4717  **/
4718 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4719 			   bool is_xdp, bool enable)
4720 {
4721 	int ret;
4722 
4723 	i40e_control_tx_q(pf, pf_q, enable);
4724 
4725 	/* wait for the change to finish */
4726 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4727 	if (ret) {
4728 		dev_info(&pf->pdev->dev,
4729 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4730 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4731 			 (enable ? "en" : "dis"));
4732 	}
4733 
4734 	return ret;
4735 }
4736 
4737 /**
4738  * i40e_vsi_enable_tx - Start a VSI's rings
4739  * @vsi: the VSI being configured
4740  **/
4741 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4742 {
4743 	struct i40e_pf *pf = vsi->back;
4744 	int i, pf_q, ret = 0;
4745 
4746 	pf_q = vsi->base_queue;
4747 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4748 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4749 					     pf_q,
4750 					     false /*is xdp*/, true);
4751 		if (ret)
4752 			break;
4753 
4754 		if (!i40e_enabled_xdp_vsi(vsi))
4755 			continue;
4756 
4757 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4758 					     pf_q + vsi->alloc_queue_pairs,
4759 					     true /*is xdp*/, true);
4760 		if (ret)
4761 			break;
4762 	}
4763 	return ret;
4764 }
4765 
4766 /**
4767  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4768  * @pf: the PF being configured
4769  * @pf_q: the PF queue
4770  * @enable: enable or disable state of the queue
4771  *
4772  * This routine will wait for the given Rx queue of the PF to reach the
4773  * enabled or disabled state.
4774  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4775  * multiple retries; else will return 0 in case of success.
4776  **/
4777 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4778 {
4779 	int i;
4780 	u32 rx_reg;
4781 
4782 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4783 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4784 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4785 			break;
4786 
4787 		usleep_range(10, 20);
4788 	}
4789 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4790 		return -ETIMEDOUT;
4791 
4792 	return 0;
4793 }
4794 
4795 /**
4796  * i40e_control_rx_q - Start or stop a particular Rx queue
4797  * @pf: the PF structure
4798  * @pf_q: the PF queue to configure
4799  * @enable: start or stop the queue
4800  *
4801  * This function enables or disables a single queue. Note that
4802  * any delay required after the operation is expected to be
4803  * handled by the caller of this function.
4804  **/
4805 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4806 {
4807 	struct i40e_hw *hw = &pf->hw;
4808 	u32 rx_reg;
4809 	int i;
4810 
4811 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4812 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4813 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4814 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4815 			break;
4816 		usleep_range(1000, 2000);
4817 	}
4818 
4819 	/* Skip if the queue is already in the requested state */
4820 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4821 		return;
4822 
4823 	/* turn on/off the queue */
4824 	if (enable)
4825 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4826 	else
4827 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4828 
4829 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4830 }
4831 
4832 /**
4833  * i40e_control_wait_rx_q
4834  * @pf: the PF structure
4835  * @pf_q: queue being configured
4836  * @enable: start or stop the rings
4837  *
4838  * This function enables or disables a single queue along with waiting
4839  * for the change to finish. The caller of this function should handle
4840  * the delays needed in the case of disabling queues.
4841  **/
4842 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4843 {
4844 	int ret = 0;
4845 
4846 	i40e_control_rx_q(pf, pf_q, enable);
4847 
4848 	/* wait for the change to finish */
4849 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4850 	if (ret)
4851 		return ret;
4852 
4853 	return ret;
4854 }
4855 
4856 /**
4857  * i40e_vsi_enable_rx - Start a VSI's rings
4858  * @vsi: the VSI being configured
4859  **/
4860 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4861 {
4862 	struct i40e_pf *pf = vsi->back;
4863 	int i, pf_q, ret = 0;
4864 
4865 	pf_q = vsi->base_queue;
4866 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4867 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4868 		if (ret) {
4869 			dev_info(&pf->pdev->dev,
4870 				 "VSI seid %d Rx ring %d enable timeout\n",
4871 				 vsi->seid, pf_q);
4872 			break;
4873 		}
4874 	}
4875 
4876 	return ret;
4877 }
4878 
4879 /**
4880  * i40e_vsi_start_rings - Start a VSI's rings
4881  * @vsi: the VSI being configured
4882  **/
4883 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4884 {
4885 	int ret = 0;
4886 
4887 	/* do rx first for enable and last for disable */
4888 	ret = i40e_vsi_enable_rx(vsi);
4889 	if (ret)
4890 		return ret;
4891 	ret = i40e_vsi_enable_tx(vsi);
4892 
4893 	return ret;
4894 }
4895 
4896 #define I40E_DISABLE_TX_GAP_MSEC	50
4897 
4898 /**
4899  * i40e_vsi_stop_rings - Stop a VSI's rings
4900  * @vsi: the VSI being configured
4901  **/
4902 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4903 {
4904 	struct i40e_pf *pf = vsi->back;
4905 	int pf_q, err, q_end;
4906 
4907 	/* When port TX is suspended, don't wait */
4908 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4909 		return i40e_vsi_stop_rings_no_wait(vsi);
4910 
4911 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4912 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4913 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4914 
4915 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4916 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4917 		if (err)
4918 			dev_info(&pf->pdev->dev,
4919 				 "VSI seid %d Rx ring %d disable timeout\n",
4920 				 vsi->seid, pf_q);
4921 	}
4922 
4923 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4924 	pf_q = vsi->base_queue;
4925 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4926 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4927 
4928 	i40e_vsi_wait_queues_disabled(vsi);
4929 }
4930 
4931 /**
4932  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4933  * @vsi: the VSI being shutdown
4934  *
4935  * This function stops all the rings for a VSI but does not delay to verify
4936  * that rings have been disabled. It is expected that the caller is shutting
4937  * down multiple VSIs at once and will delay together for all the VSIs after
4938  * initiating the shutdown. This is particularly useful for shutting down lots
4939  * of VFs together. Otherwise, a large delay can be incurred while configuring
4940  * each VSI in serial.
4941  **/
4942 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4943 {
4944 	struct i40e_pf *pf = vsi->back;
4945 	int i, pf_q;
4946 
4947 	pf_q = vsi->base_queue;
4948 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4949 		i40e_control_tx_q(pf, pf_q, false);
4950 		i40e_control_rx_q(pf, pf_q, false);
4951 	}
4952 }
4953 
4954 /**
4955  * i40e_vsi_free_irq - Free the irq association with the OS
4956  * @vsi: the VSI being configured
4957  **/
4958 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4959 {
4960 	struct i40e_pf *pf = vsi->back;
4961 	struct i40e_hw *hw = &pf->hw;
4962 	int base = vsi->base_vector;
4963 	u32 val, qp;
4964 	int i;
4965 
4966 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4967 		if (!vsi->q_vectors)
4968 			return;
4969 
4970 		if (!vsi->irqs_ready)
4971 			return;
4972 
4973 		vsi->irqs_ready = false;
4974 		for (i = 0; i < vsi->num_q_vectors; i++) {
4975 			int irq_num;
4976 			u16 vector;
4977 
4978 			vector = i + base;
4979 			irq_num = pf->msix_entries[vector].vector;
4980 
4981 			/* free only the irqs that were actually requested */
4982 			if (!vsi->q_vectors[i] ||
4983 			    !vsi->q_vectors[i]->num_ringpairs)
4984 				continue;
4985 
4986 			/* clear the affinity notifier in the IRQ descriptor */
4987 			irq_set_affinity_notifier(irq_num, NULL);
4988 			/* remove our suggested affinity mask for this IRQ */
4989 			irq_update_affinity_hint(irq_num, NULL);
4990 			free_irq(irq_num, vsi->q_vectors[i]);
4991 
4992 			/* Tear down the interrupt queue link list
4993 			 *
4994 			 * We know that they come in pairs and always
4995 			 * the Rx first, then the Tx.  To clear the
4996 			 * link list, stick the EOL value into the
4997 			 * next_q field of the registers.
4998 			 */
4999 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
5000 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5001 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5002 			val |= I40E_QUEUE_END_OF_LIST
5003 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5004 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
5005 
5006 			while (qp != I40E_QUEUE_END_OF_LIST) {
5007 				u32 next;
5008 
5009 				val = rd32(hw, I40E_QINT_RQCTL(qp));
5010 
5011 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5012 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5013 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5014 					 I40E_QINT_RQCTL_INTEVENT_MASK);
5015 
5016 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5017 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5018 
5019 				wr32(hw, I40E_QINT_RQCTL(qp), val);
5020 
5021 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5022 
5023 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
5024 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
5025 
5026 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5027 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5028 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5029 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5030 
5031 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5032 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5033 
5034 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5035 				qp = next;
5036 			}
5037 		}
5038 	} else {
5039 		free_irq(pf->pdev->irq, pf);
5040 
5041 		val = rd32(hw, I40E_PFINT_LNKLST0);
5042 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5043 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5044 		val |= I40E_QUEUE_END_OF_LIST
5045 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5046 		wr32(hw, I40E_PFINT_LNKLST0, val);
5047 
5048 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5049 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5050 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5051 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5052 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5053 
5054 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5055 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5056 
5057 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5058 
5059 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5060 
5061 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5062 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5063 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5064 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5065 
5066 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5067 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5068 
5069 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5070 	}
5071 }
5072 
5073 /**
5074  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5075  * @vsi: the VSI being configured
5076  * @v_idx: Index of vector to be freed
5077  *
5078  * This function frees the memory allocated to the q_vector.  In addition if
5079  * NAPI is enabled it will delete any references to the NAPI struct prior
5080  * to freeing the q_vector.
5081  **/
5082 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5083 {
5084 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5085 	struct i40e_ring *ring;
5086 
5087 	if (!q_vector)
5088 		return;
5089 
5090 	/* disassociate q_vector from rings */
5091 	i40e_for_each_ring(ring, q_vector->tx)
5092 		ring->q_vector = NULL;
5093 
5094 	i40e_for_each_ring(ring, q_vector->rx)
5095 		ring->q_vector = NULL;
5096 
5097 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5098 	if (vsi->netdev)
5099 		netif_napi_del(&q_vector->napi);
5100 
5101 	vsi->q_vectors[v_idx] = NULL;
5102 
5103 	kfree_rcu(q_vector, rcu);
5104 }
5105 
5106 /**
5107  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5108  * @vsi: the VSI being un-configured
5109  *
5110  * This frees the memory allocated to the q_vectors and
5111  * deletes references to the NAPI struct.
5112  **/
5113 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5114 {
5115 	int v_idx;
5116 
5117 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5118 		i40e_free_q_vector(vsi, v_idx);
5119 }
5120 
5121 /**
5122  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5123  * @pf: board private structure
5124  **/
5125 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5126 {
5127 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5128 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5129 		pci_disable_msix(pf->pdev);
5130 		kfree(pf->msix_entries);
5131 		pf->msix_entries = NULL;
5132 		kfree(pf->irq_pile);
5133 		pf->irq_pile = NULL;
5134 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
5135 		pci_disable_msi(pf->pdev);
5136 	}
5137 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
5138 }
5139 
5140 /**
5141  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5142  * @pf: board private structure
5143  *
5144  * We go through and clear interrupt specific resources and reset the structure
5145  * to pre-load conditions
5146  **/
5147 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5148 {
5149 	int i;
5150 
5151 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5152 		i40e_free_misc_vector(pf);
5153 
5154 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5155 		      I40E_IWARP_IRQ_PILE_ID);
5156 
5157 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5158 	for (i = 0; i < pf->num_alloc_vsi; i++)
5159 		if (pf->vsi[i])
5160 			i40e_vsi_free_q_vectors(pf->vsi[i]);
5161 	i40e_reset_interrupt_capability(pf);
5162 }
5163 
5164 /**
5165  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5166  * @vsi: the VSI being configured
5167  **/
5168 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5169 {
5170 	int q_idx;
5171 
5172 	if (!vsi->netdev)
5173 		return;
5174 
5175 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5176 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5177 
5178 		if (q_vector->rx.ring || q_vector->tx.ring)
5179 			napi_enable(&q_vector->napi);
5180 	}
5181 }
5182 
5183 /**
5184  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5185  * @vsi: the VSI being configured
5186  **/
5187 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5188 {
5189 	int q_idx;
5190 
5191 	if (!vsi->netdev)
5192 		return;
5193 
5194 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5195 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5196 
5197 		if (q_vector->rx.ring || q_vector->tx.ring)
5198 			napi_disable(&q_vector->napi);
5199 	}
5200 }
5201 
5202 /**
5203  * i40e_vsi_close - Shut down a VSI
5204  * @vsi: the vsi to be quelled
5205  **/
5206 static void i40e_vsi_close(struct i40e_vsi *vsi)
5207 {
5208 	struct i40e_pf *pf = vsi->back;
5209 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5210 		i40e_down(vsi);
5211 	i40e_vsi_free_irq(vsi);
5212 	i40e_vsi_free_tx_resources(vsi);
5213 	i40e_vsi_free_rx_resources(vsi);
5214 	vsi->current_netdev_flags = 0;
5215 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5216 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5217 		set_bit(__I40E_CLIENT_RESET, pf->state);
5218 }
5219 
5220 /**
5221  * i40e_quiesce_vsi - Pause a given VSI
5222  * @vsi: the VSI being paused
5223  **/
5224 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5225 {
5226 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5227 		return;
5228 
5229 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5230 	if (vsi->netdev && netif_running(vsi->netdev))
5231 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5232 	else
5233 		i40e_vsi_close(vsi);
5234 }
5235 
5236 /**
5237  * i40e_unquiesce_vsi - Resume a given VSI
5238  * @vsi: the VSI being resumed
5239  **/
5240 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5241 {
5242 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5243 		return;
5244 
5245 	if (vsi->netdev && netif_running(vsi->netdev))
5246 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5247 	else
5248 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5249 }
5250 
5251 /**
5252  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5253  * @pf: the PF
5254  **/
5255 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5256 {
5257 	int v;
5258 
5259 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5260 		if (pf->vsi[v])
5261 			i40e_quiesce_vsi(pf->vsi[v]);
5262 	}
5263 }
5264 
5265 /**
5266  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5267  * @pf: the PF
5268  **/
5269 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5270 {
5271 	int v;
5272 
5273 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5274 		if (pf->vsi[v])
5275 			i40e_unquiesce_vsi(pf->vsi[v]);
5276 	}
5277 }
5278 
5279 /**
5280  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5281  * @vsi: the VSI being configured
5282  *
5283  * Wait until all queues on a given VSI have been disabled.
5284  **/
5285 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5286 {
5287 	struct i40e_pf *pf = vsi->back;
5288 	int i, pf_q, ret;
5289 
5290 	pf_q = vsi->base_queue;
5291 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5292 		/* Check and wait for the Tx queue */
5293 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5294 		if (ret) {
5295 			dev_info(&pf->pdev->dev,
5296 				 "VSI seid %d Tx ring %d disable timeout\n",
5297 				 vsi->seid, pf_q);
5298 			return ret;
5299 		}
5300 
5301 		if (!i40e_enabled_xdp_vsi(vsi))
5302 			goto wait_rx;
5303 
5304 		/* Check and wait for the XDP Tx queue */
5305 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5306 				       false);
5307 		if (ret) {
5308 			dev_info(&pf->pdev->dev,
5309 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5310 				 vsi->seid, pf_q);
5311 			return ret;
5312 		}
5313 wait_rx:
5314 		/* Check and wait for the Rx queue */
5315 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5316 		if (ret) {
5317 			dev_info(&pf->pdev->dev,
5318 				 "VSI seid %d Rx ring %d disable timeout\n",
5319 				 vsi->seid, pf_q);
5320 			return ret;
5321 		}
5322 	}
5323 
5324 	return 0;
5325 }
5326 
5327 #ifdef CONFIG_I40E_DCB
5328 /**
5329  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5330  * @pf: the PF
5331  *
5332  * This function waits for the queues to be in disabled state for all the
5333  * VSIs that are managed by this PF.
5334  **/
5335 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5336 {
5337 	int v, ret = 0;
5338 
5339 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5340 		if (pf->vsi[v]) {
5341 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5342 			if (ret)
5343 				break;
5344 		}
5345 	}
5346 
5347 	return ret;
5348 }
5349 
5350 #endif
5351 
5352 /**
5353  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5354  * @pf: pointer to PF
5355  *
5356  * Get TC map for ISCSI PF type that will include iSCSI TC
5357  * and LAN TC.
5358  **/
5359 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5360 {
5361 	struct i40e_dcb_app_priority_table app;
5362 	struct i40e_hw *hw = &pf->hw;
5363 	u8 enabled_tc = 1; /* TC0 is always enabled */
5364 	u8 tc, i;
5365 	/* Get the iSCSI APP TLV */
5366 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5367 
5368 	for (i = 0; i < dcbcfg->numapps; i++) {
5369 		app = dcbcfg->app[i];
5370 		if (app.selector == I40E_APP_SEL_TCPIP &&
5371 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5372 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5373 			enabled_tc |= BIT(tc);
5374 			break;
5375 		}
5376 	}
5377 
5378 	return enabled_tc;
5379 }
5380 
5381 /**
5382  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5383  * @dcbcfg: the corresponding DCBx configuration structure
5384  *
5385  * Return the number of TCs from given DCBx configuration
5386  **/
5387 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5388 {
5389 	int i, tc_unused = 0;
5390 	u8 num_tc = 0;
5391 	u8 ret = 0;
5392 
5393 	/* Scan the ETS Config Priority Table to find
5394 	 * traffic class enabled for a given priority
5395 	 * and create a bitmask of enabled TCs
5396 	 */
5397 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5398 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5399 
5400 	/* Now scan the bitmask to check for
5401 	 * contiguous TCs starting with TC0
5402 	 */
5403 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5404 		if (num_tc & BIT(i)) {
5405 			if (!tc_unused) {
5406 				ret++;
5407 			} else {
5408 				pr_err("Non-contiguous TC - Disabling DCB\n");
5409 				return 1;
5410 			}
5411 		} else {
5412 			tc_unused = 1;
5413 		}
5414 	}
5415 
5416 	/* There is always at least TC0 */
5417 	if (!ret)
5418 		ret = 1;
5419 
5420 	return ret;
5421 }
5422 
5423 /**
5424  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5425  * @dcbcfg: the corresponding DCBx configuration structure
5426  *
5427  * Query the current DCB configuration and return the number of
5428  * traffic classes enabled from the given DCBX config
5429  **/
5430 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5431 {
5432 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5433 	u8 enabled_tc = 1;
5434 	u8 i;
5435 
5436 	for (i = 0; i < num_tc; i++)
5437 		enabled_tc |= BIT(i);
5438 
5439 	return enabled_tc;
5440 }
5441 
5442 /**
5443  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5444  * @pf: PF being queried
5445  *
5446  * Query the current MQPRIO configuration and return the number of
5447  * traffic classes enabled.
5448  **/
5449 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5450 {
5451 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5452 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5453 	u8 enabled_tc = 1, i;
5454 
5455 	for (i = 1; i < num_tc; i++)
5456 		enabled_tc |= BIT(i);
5457 	return enabled_tc;
5458 }
5459 
5460 /**
5461  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5462  * @pf: PF being queried
5463  *
5464  * Return number of traffic classes enabled for the given PF
5465  **/
5466 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5467 {
5468 	struct i40e_hw *hw = &pf->hw;
5469 	u8 i, enabled_tc = 1;
5470 	u8 num_tc = 0;
5471 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5472 
5473 	if (i40e_is_tc_mqprio_enabled(pf))
5474 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5475 
5476 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5477 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5478 		return 1;
5479 
5480 	/* SFP mode will be enabled for all TCs on port */
5481 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5482 		return i40e_dcb_get_num_tc(dcbcfg);
5483 
5484 	/* MFP mode return count of enabled TCs for this PF */
5485 	if (pf->hw.func_caps.iscsi)
5486 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5487 	else
5488 		return 1; /* Only TC0 */
5489 
5490 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5491 		if (enabled_tc & BIT(i))
5492 			num_tc++;
5493 	}
5494 	return num_tc;
5495 }
5496 
5497 /**
5498  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5499  * @pf: PF being queried
5500  *
5501  * Return a bitmap for enabled traffic classes for this PF.
5502  **/
5503 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5504 {
5505 	if (i40e_is_tc_mqprio_enabled(pf))
5506 		return i40e_mqprio_get_enabled_tc(pf);
5507 
5508 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5509 	 * default TC
5510 	 */
5511 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5512 		return I40E_DEFAULT_TRAFFIC_CLASS;
5513 
5514 	/* SFP mode we want PF to be enabled for all TCs */
5515 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5516 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5517 
5518 	/* MFP enabled and iSCSI PF type */
5519 	if (pf->hw.func_caps.iscsi)
5520 		return i40e_get_iscsi_tc_map(pf);
5521 	else
5522 		return I40E_DEFAULT_TRAFFIC_CLASS;
5523 }
5524 
5525 /**
5526  * i40e_vsi_get_bw_info - Query VSI BW Information
5527  * @vsi: the VSI being queried
5528  *
5529  * Returns 0 on success, negative value on failure
5530  **/
5531 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5532 {
5533 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5534 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5535 	struct i40e_pf *pf = vsi->back;
5536 	struct i40e_hw *hw = &pf->hw;
5537 	u32 tc_bw_max;
5538 	int ret;
5539 	int i;
5540 
5541 	/* Get the VSI level BW configuration */
5542 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5543 	if (ret) {
5544 		dev_info(&pf->pdev->dev,
5545 			 "couldn't get PF vsi bw config, err %pe aq_err %s\n",
5546 			 ERR_PTR(ret),
5547 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5548 		return -EINVAL;
5549 	}
5550 
5551 	/* Get the VSI level BW configuration per TC */
5552 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5553 					       NULL);
5554 	if (ret) {
5555 		dev_info(&pf->pdev->dev,
5556 			 "couldn't get PF vsi ets bw config, err %pe aq_err %s\n",
5557 			 ERR_PTR(ret),
5558 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5559 		return -EINVAL;
5560 	}
5561 
5562 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5563 		dev_info(&pf->pdev->dev,
5564 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5565 			 bw_config.tc_valid_bits,
5566 			 bw_ets_config.tc_valid_bits);
5567 		/* Still continuing */
5568 	}
5569 
5570 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5571 	vsi->bw_max_quanta = bw_config.max_bw;
5572 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5573 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5574 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5575 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5576 		vsi->bw_ets_limit_credits[i] =
5577 					le16_to_cpu(bw_ets_config.credits[i]);
5578 		/* 3 bits out of 4 for each TC */
5579 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5580 	}
5581 
5582 	return 0;
5583 }
5584 
5585 /**
5586  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5587  * @vsi: the VSI being configured
5588  * @enabled_tc: TC bitmap
5589  * @bw_share: BW shared credits per TC
5590  *
5591  * Returns 0 on success, negative value on failure
5592  **/
5593 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5594 				       u8 *bw_share)
5595 {
5596 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5597 	struct i40e_pf *pf = vsi->back;
5598 	int ret;
5599 	int i;
5600 
5601 	/* There is no need to reset BW when mqprio mode is on.  */
5602 	if (i40e_is_tc_mqprio_enabled(pf))
5603 		return 0;
5604 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5605 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5606 		if (ret)
5607 			dev_info(&pf->pdev->dev,
5608 				 "Failed to reset tx rate for vsi->seid %u\n",
5609 				 vsi->seid);
5610 		return ret;
5611 	}
5612 	memset(&bw_data, 0, sizeof(bw_data));
5613 	bw_data.tc_valid_bits = enabled_tc;
5614 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5615 		bw_data.tc_bw_credits[i] = bw_share[i];
5616 
5617 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5618 	if (ret) {
5619 		dev_info(&pf->pdev->dev,
5620 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5621 			 pf->hw.aq.asq_last_status);
5622 		return -EINVAL;
5623 	}
5624 
5625 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5626 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5627 
5628 	return 0;
5629 }
5630 
5631 /**
5632  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5633  * @vsi: the VSI being configured
5634  * @enabled_tc: TC map to be enabled
5635  *
5636  **/
5637 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5638 {
5639 	struct net_device *netdev = vsi->netdev;
5640 	struct i40e_pf *pf = vsi->back;
5641 	struct i40e_hw *hw = &pf->hw;
5642 	u8 netdev_tc = 0;
5643 	int i;
5644 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5645 
5646 	if (!netdev)
5647 		return;
5648 
5649 	if (!enabled_tc) {
5650 		netdev_reset_tc(netdev);
5651 		return;
5652 	}
5653 
5654 	/* Set up actual enabled TCs on the VSI */
5655 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5656 		return;
5657 
5658 	/* set per TC queues for the VSI */
5659 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5660 		/* Only set TC queues for enabled tcs
5661 		 *
5662 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5663 		 * enabled_tc bitmap would be 0x00001001; the driver
5664 		 * will set the numtc for netdev as 2 that will be
5665 		 * referenced by the netdev layer as TC 0 and 1.
5666 		 */
5667 		if (vsi->tc_config.enabled_tc & BIT(i))
5668 			netdev_set_tc_queue(netdev,
5669 					vsi->tc_config.tc_info[i].netdev_tc,
5670 					vsi->tc_config.tc_info[i].qcount,
5671 					vsi->tc_config.tc_info[i].qoffset);
5672 	}
5673 
5674 	if (i40e_is_tc_mqprio_enabled(pf))
5675 		return;
5676 
5677 	/* Assign UP2TC map for the VSI */
5678 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5679 		/* Get the actual TC# for the UP */
5680 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5681 		/* Get the mapped netdev TC# for the UP */
5682 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5683 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5684 	}
5685 }
5686 
5687 /**
5688  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5689  * @vsi: the VSI being configured
5690  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5691  **/
5692 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5693 				      struct i40e_vsi_context *ctxt)
5694 {
5695 	/* copy just the sections touched not the entire info
5696 	 * since not all sections are valid as returned by
5697 	 * update vsi params
5698 	 */
5699 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5700 	memcpy(&vsi->info.queue_mapping,
5701 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5702 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5703 	       sizeof(vsi->info.tc_mapping));
5704 }
5705 
5706 /**
5707  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5708  * @vsi: the VSI being reconfigured
5709  * @vsi_offset: offset from main VF VSI
5710  */
5711 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5712 {
5713 	struct i40e_vsi_context ctxt = {};
5714 	struct i40e_pf *pf;
5715 	struct i40e_hw *hw;
5716 	int ret;
5717 
5718 	if (!vsi)
5719 		return -EINVAL;
5720 	pf = vsi->back;
5721 	hw = &pf->hw;
5722 
5723 	ctxt.seid = vsi->seid;
5724 	ctxt.pf_num = hw->pf_id;
5725 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5726 	ctxt.uplink_seid = vsi->uplink_seid;
5727 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5728 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5729 	ctxt.info = vsi->info;
5730 
5731 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5732 				 false);
5733 	if (vsi->reconfig_rss) {
5734 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5735 				      vsi->num_queue_pairs);
5736 		ret = i40e_vsi_config_rss(vsi);
5737 		if (ret) {
5738 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5739 			return ret;
5740 		}
5741 		vsi->reconfig_rss = false;
5742 	}
5743 
5744 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5745 	if (ret) {
5746 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %pe aq_err %s\n",
5747 			 ERR_PTR(ret),
5748 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5749 		return ret;
5750 	}
5751 	/* update the local VSI info with updated queue map */
5752 	i40e_vsi_update_queue_map(vsi, &ctxt);
5753 	vsi->info.valid_sections = 0;
5754 
5755 	return ret;
5756 }
5757 
5758 /**
5759  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5760  * @vsi: VSI to be configured
5761  * @enabled_tc: TC bitmap
5762  *
5763  * This configures a particular VSI for TCs that are mapped to the
5764  * given TC bitmap. It uses default bandwidth share for TCs across
5765  * VSIs to configure TC for a particular VSI.
5766  *
5767  * NOTE:
5768  * It is expected that the VSI queues have been quisced before calling
5769  * this function.
5770  **/
5771 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5772 {
5773 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5774 	struct i40e_pf *pf = vsi->back;
5775 	struct i40e_hw *hw = &pf->hw;
5776 	struct i40e_vsi_context ctxt;
5777 	int ret = 0;
5778 	int i;
5779 
5780 	/* Check if enabled_tc is same as existing or new TCs */
5781 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5782 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5783 		return ret;
5784 
5785 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5786 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5787 		if (enabled_tc & BIT(i))
5788 			bw_share[i] = 1;
5789 	}
5790 
5791 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5792 	if (ret) {
5793 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5794 
5795 		dev_info(&pf->pdev->dev,
5796 			 "Failed configuring TC map %d for VSI %d\n",
5797 			 enabled_tc, vsi->seid);
5798 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5799 						  &bw_config, NULL);
5800 		if (ret) {
5801 			dev_info(&pf->pdev->dev,
5802 				 "Failed querying vsi bw info, err %pe aq_err %s\n",
5803 				 ERR_PTR(ret),
5804 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5805 			goto out;
5806 		}
5807 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5808 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5809 
5810 			if (!valid_tc)
5811 				valid_tc = bw_config.tc_valid_bits;
5812 			/* Always enable TC0, no matter what */
5813 			valid_tc |= 1;
5814 			dev_info(&pf->pdev->dev,
5815 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5816 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5817 			enabled_tc = valid_tc;
5818 		}
5819 
5820 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5821 		if (ret) {
5822 			dev_err(&pf->pdev->dev,
5823 				"Unable to  configure TC map %d for VSI %d\n",
5824 				enabled_tc, vsi->seid);
5825 			goto out;
5826 		}
5827 	}
5828 
5829 	/* Update Queue Pairs Mapping for currently enabled UPs */
5830 	ctxt.seid = vsi->seid;
5831 	ctxt.pf_num = vsi->back->hw.pf_id;
5832 	ctxt.vf_num = 0;
5833 	ctxt.uplink_seid = vsi->uplink_seid;
5834 	ctxt.info = vsi->info;
5835 	if (i40e_is_tc_mqprio_enabled(pf)) {
5836 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5837 		if (ret)
5838 			goto out;
5839 	} else {
5840 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5841 	}
5842 
5843 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5844 	 * queues changed.
5845 	 */
5846 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5847 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5848 				      vsi->num_queue_pairs);
5849 		ret = i40e_vsi_config_rss(vsi);
5850 		if (ret) {
5851 			dev_info(&vsi->back->pdev->dev,
5852 				 "Failed to reconfig rss for num_queues\n");
5853 			return ret;
5854 		}
5855 		vsi->reconfig_rss = false;
5856 	}
5857 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5858 		ctxt.info.valid_sections |=
5859 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5860 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5861 	}
5862 
5863 	/* Update the VSI after updating the VSI queue-mapping
5864 	 * information
5865 	 */
5866 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5867 	if (ret) {
5868 		dev_info(&pf->pdev->dev,
5869 			 "Update vsi tc config failed, err %pe aq_err %s\n",
5870 			 ERR_PTR(ret),
5871 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5872 		goto out;
5873 	}
5874 	/* update the local VSI info with updated queue map */
5875 	i40e_vsi_update_queue_map(vsi, &ctxt);
5876 	vsi->info.valid_sections = 0;
5877 
5878 	/* Update current VSI BW information */
5879 	ret = i40e_vsi_get_bw_info(vsi);
5880 	if (ret) {
5881 		dev_info(&pf->pdev->dev,
5882 			 "Failed updating vsi bw info, err %pe aq_err %s\n",
5883 			 ERR_PTR(ret),
5884 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5885 		goto out;
5886 	}
5887 
5888 	/* Update the netdev TC setup */
5889 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5890 out:
5891 	return ret;
5892 }
5893 
5894 /**
5895  * i40e_get_link_speed - Returns link speed for the interface
5896  * @vsi: VSI to be configured
5897  *
5898  **/
5899 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5900 {
5901 	struct i40e_pf *pf = vsi->back;
5902 
5903 	switch (pf->hw.phy.link_info.link_speed) {
5904 	case I40E_LINK_SPEED_40GB:
5905 		return 40000;
5906 	case I40E_LINK_SPEED_25GB:
5907 		return 25000;
5908 	case I40E_LINK_SPEED_20GB:
5909 		return 20000;
5910 	case I40E_LINK_SPEED_10GB:
5911 		return 10000;
5912 	case I40E_LINK_SPEED_1GB:
5913 		return 1000;
5914 	default:
5915 		return -EINVAL;
5916 	}
5917 }
5918 
5919 /**
5920  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5921  * @vsi: Pointer to vsi structure
5922  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5923  *
5924  * Helper function to convert units before send to set BW limit
5925  **/
5926 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5927 {
5928 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5929 		dev_warn(&vsi->back->pdev->dev,
5930 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5931 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5932 	} else {
5933 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5934 	}
5935 
5936 	return max_tx_rate;
5937 }
5938 
5939 /**
5940  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5941  * @vsi: VSI to be configured
5942  * @seid: seid of the channel/VSI
5943  * @max_tx_rate: max TX rate to be configured as BW limit
5944  *
5945  * Helper function to set BW limit for a given VSI
5946  **/
5947 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5948 {
5949 	struct i40e_pf *pf = vsi->back;
5950 	u64 credits = 0;
5951 	int speed = 0;
5952 	int ret = 0;
5953 
5954 	speed = i40e_get_link_speed(vsi);
5955 	if (max_tx_rate > speed) {
5956 		dev_err(&pf->pdev->dev,
5957 			"Invalid max tx rate %llu specified for VSI seid %d.",
5958 			max_tx_rate, seid);
5959 		return -EINVAL;
5960 	}
5961 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
5962 		dev_warn(&pf->pdev->dev,
5963 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5964 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5965 	}
5966 
5967 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5968 	credits = max_tx_rate;
5969 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5970 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5971 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5972 	if (ret)
5973 		dev_err(&pf->pdev->dev,
5974 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %pe aq_err %s\n",
5975 			max_tx_rate, seid, ERR_PTR(ret),
5976 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5977 	return ret;
5978 }
5979 
5980 /**
5981  * i40e_remove_queue_channels - Remove queue channels for the TCs
5982  * @vsi: VSI to be configured
5983  *
5984  * Remove queue channels for the TCs
5985  **/
5986 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5987 {
5988 	enum i40e_admin_queue_err last_aq_status;
5989 	struct i40e_cloud_filter *cfilter;
5990 	struct i40e_channel *ch, *ch_tmp;
5991 	struct i40e_pf *pf = vsi->back;
5992 	struct hlist_node *node;
5993 	int ret, i;
5994 
5995 	/* Reset rss size that was stored when reconfiguring rss for
5996 	 * channel VSIs with non-power-of-2 queue count.
5997 	 */
5998 	vsi->current_rss_size = 0;
5999 
6000 	/* perform cleanup for channels if they exist */
6001 	if (list_empty(&vsi->ch_list))
6002 		return;
6003 
6004 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6005 		struct i40e_vsi *p_vsi;
6006 
6007 		list_del(&ch->list);
6008 		p_vsi = ch->parent_vsi;
6009 		if (!p_vsi || !ch->initialized) {
6010 			kfree(ch);
6011 			continue;
6012 		}
6013 		/* Reset queue contexts */
6014 		for (i = 0; i < ch->num_queue_pairs; i++) {
6015 			struct i40e_ring *tx_ring, *rx_ring;
6016 			u16 pf_q;
6017 
6018 			pf_q = ch->base_queue + i;
6019 			tx_ring = vsi->tx_rings[pf_q];
6020 			tx_ring->ch = NULL;
6021 
6022 			rx_ring = vsi->rx_rings[pf_q];
6023 			rx_ring->ch = NULL;
6024 		}
6025 
6026 		/* Reset BW configured for this VSI via mqprio */
6027 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6028 		if (ret)
6029 			dev_info(&vsi->back->pdev->dev,
6030 				 "Failed to reset tx rate for ch->seid %u\n",
6031 				 ch->seid);
6032 
6033 		/* delete cloud filters associated with this channel */
6034 		hlist_for_each_entry_safe(cfilter, node,
6035 					  &pf->cloud_filter_list, cloud_node) {
6036 			if (cfilter->seid != ch->seid)
6037 				continue;
6038 
6039 			hash_del(&cfilter->cloud_node);
6040 			if (cfilter->dst_port)
6041 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6042 									cfilter,
6043 									false);
6044 			else
6045 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6046 								false);
6047 			last_aq_status = pf->hw.aq.asq_last_status;
6048 			if (ret)
6049 				dev_info(&pf->pdev->dev,
6050 					 "Failed to delete cloud filter, err %pe aq_err %s\n",
6051 					 ERR_PTR(ret),
6052 					 i40e_aq_str(&pf->hw, last_aq_status));
6053 			kfree(cfilter);
6054 		}
6055 
6056 		/* delete VSI from FW */
6057 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6058 					     NULL);
6059 		if (ret)
6060 			dev_err(&vsi->back->pdev->dev,
6061 				"unable to remove channel (%d) for parent VSI(%d)\n",
6062 				ch->seid, p_vsi->seid);
6063 		kfree(ch);
6064 	}
6065 	INIT_LIST_HEAD(&vsi->ch_list);
6066 }
6067 
6068 /**
6069  * i40e_get_max_queues_for_channel
6070  * @vsi: ptr to VSI to which channels are associated with
6071  *
6072  * Helper function which returns max value among the queue counts set on the
6073  * channels/TCs created.
6074  **/
6075 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6076 {
6077 	struct i40e_channel *ch, *ch_tmp;
6078 	int max = 0;
6079 
6080 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6081 		if (!ch->initialized)
6082 			continue;
6083 		if (ch->num_queue_pairs > max)
6084 			max = ch->num_queue_pairs;
6085 	}
6086 
6087 	return max;
6088 }
6089 
6090 /**
6091  * i40e_validate_num_queues - validate num_queues w.r.t channel
6092  * @pf: ptr to PF device
6093  * @num_queues: number of queues
6094  * @vsi: the parent VSI
6095  * @reconfig_rss: indicates should the RSS be reconfigured or not
6096  *
6097  * This function validates number of queues in the context of new channel
6098  * which is being established and determines if RSS should be reconfigured
6099  * or not for parent VSI.
6100  **/
6101 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6102 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6103 {
6104 	int max_ch_queues;
6105 
6106 	if (!reconfig_rss)
6107 		return -EINVAL;
6108 
6109 	*reconfig_rss = false;
6110 	if (vsi->current_rss_size) {
6111 		if (num_queues > vsi->current_rss_size) {
6112 			dev_dbg(&pf->pdev->dev,
6113 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6114 				num_queues, vsi->current_rss_size);
6115 			return -EINVAL;
6116 		} else if ((num_queues < vsi->current_rss_size) &&
6117 			   (!is_power_of_2(num_queues))) {
6118 			dev_dbg(&pf->pdev->dev,
6119 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6120 				num_queues, vsi->current_rss_size);
6121 			return -EINVAL;
6122 		}
6123 	}
6124 
6125 	if (!is_power_of_2(num_queues)) {
6126 		/* Find the max num_queues configured for channel if channel
6127 		 * exist.
6128 		 * if channel exist, then enforce 'num_queues' to be more than
6129 		 * max ever queues configured for channel.
6130 		 */
6131 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6132 		if (num_queues < max_ch_queues) {
6133 			dev_dbg(&pf->pdev->dev,
6134 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6135 				num_queues, max_ch_queues);
6136 			return -EINVAL;
6137 		}
6138 		*reconfig_rss = true;
6139 	}
6140 
6141 	return 0;
6142 }
6143 
6144 /**
6145  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6146  * @vsi: the VSI being setup
6147  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6148  *
6149  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6150  **/
6151 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6152 {
6153 	struct i40e_pf *pf = vsi->back;
6154 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6155 	struct i40e_hw *hw = &pf->hw;
6156 	int local_rss_size;
6157 	u8 *lut;
6158 	int ret;
6159 
6160 	if (!vsi->rss_size)
6161 		return -EINVAL;
6162 
6163 	if (rss_size > vsi->rss_size)
6164 		return -EINVAL;
6165 
6166 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6167 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6168 	if (!lut)
6169 		return -ENOMEM;
6170 
6171 	/* Ignoring user configured lut if there is one */
6172 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6173 
6174 	/* Use user configured hash key if there is one, otherwise
6175 	 * use default.
6176 	 */
6177 	if (vsi->rss_hkey_user)
6178 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6179 	else
6180 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6181 
6182 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6183 	if (ret) {
6184 		dev_info(&pf->pdev->dev,
6185 			 "Cannot set RSS lut, err %pe aq_err %s\n",
6186 			 ERR_PTR(ret),
6187 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6188 		kfree(lut);
6189 		return ret;
6190 	}
6191 	kfree(lut);
6192 
6193 	/* Do the update w.r.t. storing rss_size */
6194 	if (!vsi->orig_rss_size)
6195 		vsi->orig_rss_size = vsi->rss_size;
6196 	vsi->current_rss_size = local_rss_size;
6197 
6198 	return ret;
6199 }
6200 
6201 /**
6202  * i40e_channel_setup_queue_map - Setup a channel queue map
6203  * @pf: ptr to PF device
6204  * @ctxt: VSI context structure
6205  * @ch: ptr to channel structure
6206  *
6207  * Setup queue map for a specific channel
6208  **/
6209 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6210 					 struct i40e_vsi_context *ctxt,
6211 					 struct i40e_channel *ch)
6212 {
6213 	u16 qcount, qmap, sections = 0;
6214 	u8 offset = 0;
6215 	int pow;
6216 
6217 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6218 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6219 
6220 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6221 	ch->num_queue_pairs = qcount;
6222 
6223 	/* find the next higher power-of-2 of num queue pairs */
6224 	pow = ilog2(qcount);
6225 	if (!is_power_of_2(qcount))
6226 		pow++;
6227 
6228 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6229 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6230 
6231 	/* Setup queue TC[0].qmap for given VSI context */
6232 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6233 
6234 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6235 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6236 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6237 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6238 }
6239 
6240 /**
6241  * i40e_add_channel - add a channel by adding VSI
6242  * @pf: ptr to PF device
6243  * @uplink_seid: underlying HW switching element (VEB) ID
6244  * @ch: ptr to channel structure
6245  *
6246  * Add a channel (VSI) using add_vsi and queue_map
6247  **/
6248 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6249 			    struct i40e_channel *ch)
6250 {
6251 	struct i40e_hw *hw = &pf->hw;
6252 	struct i40e_vsi_context ctxt;
6253 	u8 enabled_tc = 0x1; /* TC0 enabled */
6254 	int ret;
6255 
6256 	if (ch->type != I40E_VSI_VMDQ2) {
6257 		dev_info(&pf->pdev->dev,
6258 			 "add new vsi failed, ch->type %d\n", ch->type);
6259 		return -EINVAL;
6260 	}
6261 
6262 	memset(&ctxt, 0, sizeof(ctxt));
6263 	ctxt.pf_num = hw->pf_id;
6264 	ctxt.vf_num = 0;
6265 	ctxt.uplink_seid = uplink_seid;
6266 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6267 	if (ch->type == I40E_VSI_VMDQ2)
6268 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6269 
6270 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6271 		ctxt.info.valid_sections |=
6272 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6273 		ctxt.info.switch_id =
6274 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6275 	}
6276 
6277 	/* Set queue map for a given VSI context */
6278 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6279 
6280 	/* Now time to create VSI */
6281 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6282 	if (ret) {
6283 		dev_info(&pf->pdev->dev,
6284 			 "add new vsi failed, err %pe aq_err %s\n",
6285 			 ERR_PTR(ret),
6286 			 i40e_aq_str(&pf->hw,
6287 				     pf->hw.aq.asq_last_status));
6288 		return -ENOENT;
6289 	}
6290 
6291 	/* Success, update channel, set enabled_tc only if the channel
6292 	 * is not a macvlan
6293 	 */
6294 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6295 	ch->seid = ctxt.seid;
6296 	ch->vsi_number = ctxt.vsi_number;
6297 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6298 
6299 	/* copy just the sections touched not the entire info
6300 	 * since not all sections are valid as returned by
6301 	 * update vsi params
6302 	 */
6303 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6304 	memcpy(&ch->info.queue_mapping,
6305 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6306 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6307 	       sizeof(ctxt.info.tc_mapping));
6308 
6309 	return 0;
6310 }
6311 
6312 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6313 				  u8 *bw_share)
6314 {
6315 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6316 	int ret;
6317 	int i;
6318 
6319 	memset(&bw_data, 0, sizeof(bw_data));
6320 	bw_data.tc_valid_bits = ch->enabled_tc;
6321 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6322 		bw_data.tc_bw_credits[i] = bw_share[i];
6323 
6324 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6325 				       &bw_data, NULL);
6326 	if (ret) {
6327 		dev_info(&vsi->back->pdev->dev,
6328 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6329 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6330 		return -EINVAL;
6331 	}
6332 
6333 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6334 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6335 
6336 	return 0;
6337 }
6338 
6339 /**
6340  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6341  * @pf: ptr to PF device
6342  * @vsi: the VSI being setup
6343  * @ch: ptr to channel structure
6344  *
6345  * Configure TX rings associated with channel (VSI) since queues are being
6346  * from parent VSI.
6347  **/
6348 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6349 				       struct i40e_vsi *vsi,
6350 				       struct i40e_channel *ch)
6351 {
6352 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6353 	int ret;
6354 	int i;
6355 
6356 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6357 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6358 		if (ch->enabled_tc & BIT(i))
6359 			bw_share[i] = 1;
6360 	}
6361 
6362 	/* configure BW for new VSI */
6363 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6364 	if (ret) {
6365 		dev_info(&vsi->back->pdev->dev,
6366 			 "Failed configuring TC map %d for channel (seid %u)\n",
6367 			 ch->enabled_tc, ch->seid);
6368 		return ret;
6369 	}
6370 
6371 	for (i = 0; i < ch->num_queue_pairs; i++) {
6372 		struct i40e_ring *tx_ring, *rx_ring;
6373 		u16 pf_q;
6374 
6375 		pf_q = ch->base_queue + i;
6376 
6377 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6378 		 * context
6379 		 */
6380 		tx_ring = vsi->tx_rings[pf_q];
6381 		tx_ring->ch = ch;
6382 
6383 		/* Get the RX ring ptr */
6384 		rx_ring = vsi->rx_rings[pf_q];
6385 		rx_ring->ch = ch;
6386 	}
6387 
6388 	return 0;
6389 }
6390 
6391 /**
6392  * i40e_setup_hw_channel - setup new channel
6393  * @pf: ptr to PF device
6394  * @vsi: the VSI being setup
6395  * @ch: ptr to channel structure
6396  * @uplink_seid: underlying HW switching element (VEB) ID
6397  * @type: type of channel to be created (VMDq2/VF)
6398  *
6399  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6400  * and configures TX rings accordingly
6401  **/
6402 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6403 					struct i40e_vsi *vsi,
6404 					struct i40e_channel *ch,
6405 					u16 uplink_seid, u8 type)
6406 {
6407 	int ret;
6408 
6409 	ch->initialized = false;
6410 	ch->base_queue = vsi->next_base_queue;
6411 	ch->type = type;
6412 
6413 	/* Proceed with creation of channel (VMDq2) VSI */
6414 	ret = i40e_add_channel(pf, uplink_seid, ch);
6415 	if (ret) {
6416 		dev_info(&pf->pdev->dev,
6417 			 "failed to add_channel using uplink_seid %u\n",
6418 			 uplink_seid);
6419 		return ret;
6420 	}
6421 
6422 	/* Mark the successful creation of channel */
6423 	ch->initialized = true;
6424 
6425 	/* Reconfigure TX queues using QTX_CTL register */
6426 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6427 	if (ret) {
6428 		dev_info(&pf->pdev->dev,
6429 			 "failed to configure TX rings for channel %u\n",
6430 			 ch->seid);
6431 		return ret;
6432 	}
6433 
6434 	/* update 'next_base_queue' */
6435 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6436 	dev_dbg(&pf->pdev->dev,
6437 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6438 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6439 		ch->num_queue_pairs,
6440 		vsi->next_base_queue);
6441 	return ret;
6442 }
6443 
6444 /**
6445  * i40e_setup_channel - setup new channel using uplink element
6446  * @pf: ptr to PF device
6447  * @vsi: pointer to the VSI to set up the channel within
6448  * @ch: ptr to channel structure
6449  *
6450  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6451  * and uplink switching element (uplink_seid)
6452  **/
6453 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6454 			       struct i40e_channel *ch)
6455 {
6456 	u8 vsi_type;
6457 	u16 seid;
6458 	int ret;
6459 
6460 	if (vsi->type == I40E_VSI_MAIN) {
6461 		vsi_type = I40E_VSI_VMDQ2;
6462 	} else {
6463 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6464 			vsi->type);
6465 		return false;
6466 	}
6467 
6468 	/* underlying switching element */
6469 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6470 
6471 	/* create channel (VSI), configure TX rings */
6472 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6473 	if (ret) {
6474 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6475 		return false;
6476 	}
6477 
6478 	return ch->initialized ? true : false;
6479 }
6480 
6481 /**
6482  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6483  * @vsi: ptr to VSI which has PF backing
6484  *
6485  * Sets up switch mode correctly if it needs to be changed and perform
6486  * what are allowed modes.
6487  **/
6488 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6489 {
6490 	u8 mode;
6491 	struct i40e_pf *pf = vsi->back;
6492 	struct i40e_hw *hw = &pf->hw;
6493 	int ret;
6494 
6495 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6496 	if (ret)
6497 		return -EINVAL;
6498 
6499 	if (hw->dev_caps.switch_mode) {
6500 		/* if switch mode is set, support mode2 (non-tunneled for
6501 		 * cloud filter) for now
6502 		 */
6503 		u32 switch_mode = hw->dev_caps.switch_mode &
6504 				  I40E_SWITCH_MODE_MASK;
6505 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6506 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6507 				return 0;
6508 			dev_err(&pf->pdev->dev,
6509 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6510 				hw->dev_caps.switch_mode);
6511 			return -EINVAL;
6512 		}
6513 	}
6514 
6515 	/* Set Bit 7 to be valid */
6516 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6517 
6518 	/* Set L4type for TCP support */
6519 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6520 
6521 	/* Set cloud filter mode */
6522 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6523 
6524 	/* Prep mode field for set_switch_config */
6525 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6526 					pf->last_sw_conf_valid_flags,
6527 					mode, NULL);
6528 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6529 		dev_err(&pf->pdev->dev,
6530 			"couldn't set switch config bits, err %pe aq_err %s\n",
6531 			ERR_PTR(ret),
6532 			i40e_aq_str(hw,
6533 				    hw->aq.asq_last_status));
6534 
6535 	return ret;
6536 }
6537 
6538 /**
6539  * i40e_create_queue_channel - function to create channel
6540  * @vsi: VSI to be configured
6541  * @ch: ptr to channel (it contains channel specific params)
6542  *
6543  * This function creates channel (VSI) using num_queues specified by user,
6544  * reconfigs RSS if needed.
6545  **/
6546 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6547 			      struct i40e_channel *ch)
6548 {
6549 	struct i40e_pf *pf = vsi->back;
6550 	bool reconfig_rss;
6551 	int err;
6552 
6553 	if (!ch)
6554 		return -EINVAL;
6555 
6556 	if (!ch->num_queue_pairs) {
6557 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6558 			ch->num_queue_pairs);
6559 		return -EINVAL;
6560 	}
6561 
6562 	/* validate user requested num_queues for channel */
6563 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6564 				       &reconfig_rss);
6565 	if (err) {
6566 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6567 			 ch->num_queue_pairs);
6568 		return -EINVAL;
6569 	}
6570 
6571 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6572 	 * VSI to be added switch to VEB mode.
6573 	 */
6574 
6575 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6576 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6577 
6578 		if (vsi->type == I40E_VSI_MAIN) {
6579 			if (i40e_is_tc_mqprio_enabled(pf))
6580 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6581 			else
6582 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6583 		}
6584 		/* now onwards for main VSI, number of queues will be value
6585 		 * of TC0's queue count
6586 		 */
6587 	}
6588 
6589 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6590 	 * it should be more than num_queues
6591 	 */
6592 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6593 		dev_dbg(&pf->pdev->dev,
6594 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6595 			vsi->cnt_q_avail, ch->num_queue_pairs);
6596 		return -EINVAL;
6597 	}
6598 
6599 	/* reconfig_rss only if vsi type is MAIN_VSI */
6600 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6601 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6602 		if (err) {
6603 			dev_info(&pf->pdev->dev,
6604 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6605 				 ch->num_queue_pairs);
6606 			return -EINVAL;
6607 		}
6608 	}
6609 
6610 	if (!i40e_setup_channel(pf, vsi, ch)) {
6611 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6612 		return -EINVAL;
6613 	}
6614 
6615 	dev_info(&pf->pdev->dev,
6616 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6617 		 ch->seid, ch->num_queue_pairs);
6618 
6619 	/* configure VSI for BW limit */
6620 	if (ch->max_tx_rate) {
6621 		u64 credits = ch->max_tx_rate;
6622 
6623 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6624 			return -EINVAL;
6625 
6626 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6627 		dev_dbg(&pf->pdev->dev,
6628 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6629 			ch->max_tx_rate,
6630 			credits,
6631 			ch->seid);
6632 	}
6633 
6634 	/* in case of VF, this will be main SRIOV VSI */
6635 	ch->parent_vsi = vsi;
6636 
6637 	/* and update main_vsi's count for queue_available to use */
6638 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6639 
6640 	return 0;
6641 }
6642 
6643 /**
6644  * i40e_configure_queue_channels - Add queue channel for the given TCs
6645  * @vsi: VSI to be configured
6646  *
6647  * Configures queue channel mapping to the given TCs
6648  **/
6649 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6650 {
6651 	struct i40e_channel *ch;
6652 	u64 max_rate = 0;
6653 	int ret = 0, i;
6654 
6655 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6656 	vsi->tc_seid_map[0] = vsi->seid;
6657 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6658 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6659 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6660 			if (!ch) {
6661 				ret = -ENOMEM;
6662 				goto err_free;
6663 			}
6664 
6665 			INIT_LIST_HEAD(&ch->list);
6666 			ch->num_queue_pairs =
6667 				vsi->tc_config.tc_info[i].qcount;
6668 			ch->base_queue =
6669 				vsi->tc_config.tc_info[i].qoffset;
6670 
6671 			/* Bandwidth limit through tc interface is in bytes/s,
6672 			 * change to Mbit/s
6673 			 */
6674 			max_rate = vsi->mqprio_qopt.max_rate[i];
6675 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6676 			ch->max_tx_rate = max_rate;
6677 
6678 			list_add_tail(&ch->list, &vsi->ch_list);
6679 
6680 			ret = i40e_create_queue_channel(vsi, ch);
6681 			if (ret) {
6682 				dev_err(&vsi->back->pdev->dev,
6683 					"Failed creating queue channel with TC%d: queues %d\n",
6684 					i, ch->num_queue_pairs);
6685 				goto err_free;
6686 			}
6687 			vsi->tc_seid_map[i] = ch->seid;
6688 		}
6689 	}
6690 
6691 	/* reset to reconfigure TX queue contexts */
6692 	i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6693 	return ret;
6694 
6695 err_free:
6696 	i40e_remove_queue_channels(vsi);
6697 	return ret;
6698 }
6699 
6700 /**
6701  * i40e_veb_config_tc - Configure TCs for given VEB
6702  * @veb: given VEB
6703  * @enabled_tc: TC bitmap
6704  *
6705  * Configures given TC bitmap for VEB (switching) element
6706  **/
6707 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6708 {
6709 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6710 	struct i40e_pf *pf = veb->pf;
6711 	int ret = 0;
6712 	int i;
6713 
6714 	/* No TCs or already enabled TCs just return */
6715 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6716 		return ret;
6717 
6718 	bw_data.tc_valid_bits = enabled_tc;
6719 	/* bw_data.absolute_credits is not set (relative) */
6720 
6721 	/* Enable ETS TCs with equal BW Share for now */
6722 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6723 		if (enabled_tc & BIT(i))
6724 			bw_data.tc_bw_share_credits[i] = 1;
6725 	}
6726 
6727 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6728 						   &bw_data, NULL);
6729 	if (ret) {
6730 		dev_info(&pf->pdev->dev,
6731 			 "VEB bw config failed, err %pe aq_err %s\n",
6732 			 ERR_PTR(ret),
6733 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6734 		goto out;
6735 	}
6736 
6737 	/* Update the BW information */
6738 	ret = i40e_veb_get_bw_info(veb);
6739 	if (ret) {
6740 		dev_info(&pf->pdev->dev,
6741 			 "Failed getting veb bw config, err %pe aq_err %s\n",
6742 			 ERR_PTR(ret),
6743 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6744 	}
6745 
6746 out:
6747 	return ret;
6748 }
6749 
6750 #ifdef CONFIG_I40E_DCB
6751 /**
6752  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6753  * @pf: PF struct
6754  *
6755  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6756  * the caller would've quiesce all the VSIs before calling
6757  * this function
6758  **/
6759 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6760 {
6761 	u8 tc_map = 0;
6762 	int ret;
6763 	u8 v;
6764 
6765 	/* Enable the TCs available on PF to all VEBs */
6766 	tc_map = i40e_pf_get_tc_map(pf);
6767 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6768 		return;
6769 
6770 	for (v = 0; v < I40E_MAX_VEB; v++) {
6771 		if (!pf->veb[v])
6772 			continue;
6773 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6774 		if (ret) {
6775 			dev_info(&pf->pdev->dev,
6776 				 "Failed configuring TC for VEB seid=%d\n",
6777 				 pf->veb[v]->seid);
6778 			/* Will try to configure as many components */
6779 		}
6780 	}
6781 
6782 	/* Update each VSI */
6783 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6784 		if (!pf->vsi[v])
6785 			continue;
6786 
6787 		/* - Enable all TCs for the LAN VSI
6788 		 * - For all others keep them at TC0 for now
6789 		 */
6790 		if (v == pf->lan_vsi)
6791 			tc_map = i40e_pf_get_tc_map(pf);
6792 		else
6793 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6794 
6795 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6796 		if (ret) {
6797 			dev_info(&pf->pdev->dev,
6798 				 "Failed configuring TC for VSI seid=%d\n",
6799 				 pf->vsi[v]->seid);
6800 			/* Will try to configure as many components */
6801 		} else {
6802 			/* Re-configure VSI vectors based on updated TC map */
6803 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6804 			if (pf->vsi[v]->netdev)
6805 				i40e_dcbnl_set_all(pf->vsi[v]);
6806 		}
6807 	}
6808 }
6809 
6810 /**
6811  * i40e_resume_port_tx - Resume port Tx
6812  * @pf: PF struct
6813  *
6814  * Resume a port's Tx and issue a PF reset in case of failure to
6815  * resume.
6816  **/
6817 static int i40e_resume_port_tx(struct i40e_pf *pf)
6818 {
6819 	struct i40e_hw *hw = &pf->hw;
6820 	int ret;
6821 
6822 	ret = i40e_aq_resume_port_tx(hw, NULL);
6823 	if (ret) {
6824 		dev_info(&pf->pdev->dev,
6825 			 "Resume Port Tx failed, err %pe aq_err %s\n",
6826 			  ERR_PTR(ret),
6827 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6828 		/* Schedule PF reset to recover */
6829 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6830 		i40e_service_event_schedule(pf);
6831 	}
6832 
6833 	return ret;
6834 }
6835 
6836 /**
6837  * i40e_suspend_port_tx - Suspend port Tx
6838  * @pf: PF struct
6839  *
6840  * Suspend a port's Tx and issue a PF reset in case of failure.
6841  **/
6842 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6843 {
6844 	struct i40e_hw *hw = &pf->hw;
6845 	int ret;
6846 
6847 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6848 	if (ret) {
6849 		dev_info(&pf->pdev->dev,
6850 			 "Suspend Port Tx failed, err %pe aq_err %s\n",
6851 			 ERR_PTR(ret),
6852 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6853 		/* Schedule PF reset to recover */
6854 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6855 		i40e_service_event_schedule(pf);
6856 	}
6857 
6858 	return ret;
6859 }
6860 
6861 /**
6862  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6863  * @pf: PF being configured
6864  * @new_cfg: New DCBX configuration
6865  *
6866  * Program DCB settings into HW and reconfigure VEB/VSIs on
6867  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6868  **/
6869 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6870 				  struct i40e_dcbx_config *new_cfg)
6871 {
6872 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6873 	int ret;
6874 
6875 	/* Check if need reconfiguration */
6876 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6877 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6878 		return 0;
6879 	}
6880 
6881 	/* Config change disable all VSIs */
6882 	i40e_pf_quiesce_all_vsi(pf);
6883 
6884 	/* Copy the new config to the current config */
6885 	*old_cfg = *new_cfg;
6886 	old_cfg->etsrec = old_cfg->etscfg;
6887 	ret = i40e_set_dcb_config(&pf->hw);
6888 	if (ret) {
6889 		dev_info(&pf->pdev->dev,
6890 			 "Set DCB Config failed, err %pe aq_err %s\n",
6891 			 ERR_PTR(ret),
6892 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6893 		goto out;
6894 	}
6895 
6896 	/* Changes in configuration update VEB/VSI */
6897 	i40e_dcb_reconfigure(pf);
6898 out:
6899 	/* In case of reset do not try to resume anything */
6900 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6901 		/* Re-start the VSIs if disabled */
6902 		ret = i40e_resume_port_tx(pf);
6903 		/* In case of error no point in resuming VSIs */
6904 		if (ret)
6905 			goto err;
6906 		i40e_pf_unquiesce_all_vsi(pf);
6907 	}
6908 err:
6909 	return ret;
6910 }
6911 
6912 /**
6913  * i40e_hw_dcb_config - Program new DCBX settings into HW
6914  * @pf: PF being configured
6915  * @new_cfg: New DCBX configuration
6916  *
6917  * Program DCB settings into HW and reconfigure VEB/VSIs on
6918  * given PF
6919  **/
6920 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6921 {
6922 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6923 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6924 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6925 	struct i40e_dcbx_config *old_cfg;
6926 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6927 	struct i40e_rx_pb_config pb_cfg;
6928 	struct i40e_hw *hw = &pf->hw;
6929 	u8 num_ports = hw->num_ports;
6930 	bool need_reconfig;
6931 	int ret = -EINVAL;
6932 	u8 lltc_map = 0;
6933 	u8 tc_map = 0;
6934 	u8 new_numtc;
6935 	u8 i;
6936 
6937 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6938 	/* Un-pack information to Program ETS HW via shared API
6939 	 * numtc, tcmap
6940 	 * LLTC map
6941 	 * ETS/NON-ETS arbiter mode
6942 	 * max exponent (credit refills)
6943 	 * Total number of ports
6944 	 * PFC priority bit-map
6945 	 * Priority Table
6946 	 * BW % per TC
6947 	 * Arbiter mode between UPs sharing same TC
6948 	 * TSA table (ETS or non-ETS)
6949 	 * EEE enabled or not
6950 	 * MFS TC table
6951 	 */
6952 
6953 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6954 
6955 	memset(&ets_data, 0, sizeof(ets_data));
6956 	for (i = 0; i < new_numtc; i++) {
6957 		tc_map |= BIT(i);
6958 		switch (new_cfg->etscfg.tsatable[i]) {
6959 		case I40E_IEEE_TSA_ETS:
6960 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6961 			ets_data.tc_bw_share_credits[i] =
6962 					new_cfg->etscfg.tcbwtable[i];
6963 			break;
6964 		case I40E_IEEE_TSA_STRICT:
6965 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6966 			lltc_map |= BIT(i);
6967 			ets_data.tc_bw_share_credits[i] =
6968 					I40E_DCB_STRICT_PRIO_CREDITS;
6969 			break;
6970 		default:
6971 			/* Invalid TSA type */
6972 			need_reconfig = false;
6973 			goto out;
6974 		}
6975 	}
6976 
6977 	old_cfg = &hw->local_dcbx_config;
6978 	/* Check if need reconfiguration */
6979 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6980 
6981 	/* If needed, enable/disable frame tagging, disable all VSIs
6982 	 * and suspend port tx
6983 	 */
6984 	if (need_reconfig) {
6985 		/* Enable DCB tagging only when more than one TC */
6986 		if (new_numtc > 1)
6987 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6988 		else
6989 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6990 
6991 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6992 		/* Reconfiguration needed quiesce all VSIs */
6993 		i40e_pf_quiesce_all_vsi(pf);
6994 		ret = i40e_suspend_port_tx(pf);
6995 		if (ret)
6996 			goto err;
6997 	}
6998 
6999 	/* Configure Port ETS Tx Scheduler */
7000 	ets_data.tc_valid_bits = tc_map;
7001 	ets_data.tc_strict_priority_flags = lltc_map;
7002 	ret = i40e_aq_config_switch_comp_ets
7003 		(hw, pf->mac_seid, &ets_data,
7004 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
7005 	if (ret) {
7006 		dev_info(&pf->pdev->dev,
7007 			 "Modify Port ETS failed, err %pe aq_err %s\n",
7008 			 ERR_PTR(ret),
7009 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7010 		goto out;
7011 	}
7012 
7013 	/* Configure Rx ETS HW */
7014 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
7015 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
7016 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
7017 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
7018 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
7019 				   lltc_map);
7020 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
7021 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7022 				     prio_type);
7023 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7024 			       new_cfg->etscfg.prioritytable);
7025 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7026 
7027 	/* Configure Rx Packet Buffers in HW */
7028 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7029 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
7030 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7031 	}
7032 
7033 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7034 					 false, new_cfg->pfc.pfcenable,
7035 					 mfs_tc, &pb_cfg);
7036 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7037 
7038 	/* Update the local Rx Packet buffer config */
7039 	pf->pb_cfg = pb_cfg;
7040 
7041 	/* Inform the FW about changes to DCB configuration */
7042 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7043 	if (ret) {
7044 		dev_info(&pf->pdev->dev,
7045 			 "DCB Updated failed, err %pe aq_err %s\n",
7046 			 ERR_PTR(ret),
7047 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7048 		goto out;
7049 	}
7050 
7051 	/* Update the port DCBx configuration */
7052 	*old_cfg = *new_cfg;
7053 
7054 	/* Changes in configuration update VEB/VSI */
7055 	i40e_dcb_reconfigure(pf);
7056 out:
7057 	/* Re-start the VSIs if disabled */
7058 	if (need_reconfig) {
7059 		ret = i40e_resume_port_tx(pf);
7060 
7061 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7062 		/* In case of error no point in resuming VSIs */
7063 		if (ret)
7064 			goto err;
7065 
7066 		/* Wait for the PF's queues to be disabled */
7067 		ret = i40e_pf_wait_queues_disabled(pf);
7068 		if (ret) {
7069 			/* Schedule PF reset to recover */
7070 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7071 			i40e_service_event_schedule(pf);
7072 			goto err;
7073 		} else {
7074 			i40e_pf_unquiesce_all_vsi(pf);
7075 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7076 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7077 		}
7078 		/* registers are set, lets apply */
7079 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
7080 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7081 	}
7082 
7083 err:
7084 	return ret;
7085 }
7086 
7087 /**
7088  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7089  * @pf: PF being queried
7090  *
7091  * Set default DCB configuration in case DCB is to be done in SW.
7092  **/
7093 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7094 {
7095 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7096 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7097 	struct i40e_hw *hw = &pf->hw;
7098 	int err;
7099 
7100 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
7101 		/* Update the local cached instance with TC0 ETS */
7102 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7103 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7104 		pf->tmp_cfg.etscfg.maxtcs = 0;
7105 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7106 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7107 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7108 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7109 		/* FW needs one App to configure HW */
7110 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7111 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7112 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7113 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7114 
7115 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7116 	}
7117 
7118 	memset(&ets_data, 0, sizeof(ets_data));
7119 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7120 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7121 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7122 
7123 	/* Enable ETS on the Physical port */
7124 	err = i40e_aq_config_switch_comp_ets
7125 		(hw, pf->mac_seid, &ets_data,
7126 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7127 	if (err) {
7128 		dev_info(&pf->pdev->dev,
7129 			 "Enable Port ETS failed, err %pe aq_err %s\n",
7130 			 ERR_PTR(err),
7131 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7132 		err = -ENOENT;
7133 		goto out;
7134 	}
7135 
7136 	/* Update the local cached instance with TC0 ETS */
7137 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7138 	dcb_cfg->etscfg.cbs = 0;
7139 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7140 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7141 
7142 out:
7143 	return err;
7144 }
7145 
7146 /**
7147  * i40e_init_pf_dcb - Initialize DCB configuration
7148  * @pf: PF being configured
7149  *
7150  * Query the current DCB configuration and cache it
7151  * in the hardware structure
7152  **/
7153 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7154 {
7155 	struct i40e_hw *hw = &pf->hw;
7156 	int err;
7157 
7158 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7159 	 * Also do not enable DCBx if FW LLDP agent is disabled
7160 	 */
7161 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
7162 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7163 		err = -EOPNOTSUPP;
7164 		goto out;
7165 	}
7166 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
7167 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7168 		err = i40e_dcb_sw_default_config(pf);
7169 		if (err) {
7170 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7171 			goto out;
7172 		}
7173 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7174 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7175 			       DCB_CAP_DCBX_VER_IEEE;
7176 		/* at init capable but disabled */
7177 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
7178 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7179 		goto out;
7180 	}
7181 	err = i40e_init_dcb(hw, true);
7182 	if (!err) {
7183 		/* Device/Function is not DCBX capable */
7184 		if ((!hw->func_caps.dcb) ||
7185 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7186 			dev_info(&pf->pdev->dev,
7187 				 "DCBX offload is not supported or is disabled for this PF.\n");
7188 		} else {
7189 			/* When status is not DISABLED then DCBX in FW */
7190 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7191 				       DCB_CAP_DCBX_VER_IEEE;
7192 
7193 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
7194 			/* Enable DCB tagging only when more than one TC
7195 			 * or explicitly disable if only one TC
7196 			 */
7197 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7198 				pf->flags |= I40E_FLAG_DCB_ENABLED;
7199 			else
7200 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7201 			dev_dbg(&pf->pdev->dev,
7202 				"DCBX offload is supported for this PF.\n");
7203 		}
7204 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7205 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7206 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
7207 	} else {
7208 		dev_info(&pf->pdev->dev,
7209 			 "Query for DCB configuration failed, err %pe aq_err %s\n",
7210 			 ERR_PTR(err),
7211 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7212 	}
7213 
7214 out:
7215 	return err;
7216 }
7217 #endif /* CONFIG_I40E_DCB */
7218 
7219 /**
7220  * i40e_print_link_message - print link up or down
7221  * @vsi: the VSI for which link needs a message
7222  * @isup: true of link is up, false otherwise
7223  */
7224 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7225 {
7226 	enum i40e_aq_link_speed new_speed;
7227 	struct i40e_pf *pf = vsi->back;
7228 	char *speed = "Unknown";
7229 	char *fc = "Unknown";
7230 	char *fec = "";
7231 	char *req_fec = "";
7232 	char *an = "";
7233 
7234 	if (isup)
7235 		new_speed = pf->hw.phy.link_info.link_speed;
7236 	else
7237 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7238 
7239 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7240 		return;
7241 	vsi->current_isup = isup;
7242 	vsi->current_speed = new_speed;
7243 	if (!isup) {
7244 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7245 		return;
7246 	}
7247 
7248 	/* Warn user if link speed on NPAR enabled partition is not at
7249 	 * least 10GB
7250 	 */
7251 	if (pf->hw.func_caps.npar_enable &&
7252 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7253 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7254 		netdev_warn(vsi->netdev,
7255 			    "The partition detected link speed that is less than 10Gbps\n");
7256 
7257 	switch (pf->hw.phy.link_info.link_speed) {
7258 	case I40E_LINK_SPEED_40GB:
7259 		speed = "40 G";
7260 		break;
7261 	case I40E_LINK_SPEED_20GB:
7262 		speed = "20 G";
7263 		break;
7264 	case I40E_LINK_SPEED_25GB:
7265 		speed = "25 G";
7266 		break;
7267 	case I40E_LINK_SPEED_10GB:
7268 		speed = "10 G";
7269 		break;
7270 	case I40E_LINK_SPEED_5GB:
7271 		speed = "5 G";
7272 		break;
7273 	case I40E_LINK_SPEED_2_5GB:
7274 		speed = "2.5 G";
7275 		break;
7276 	case I40E_LINK_SPEED_1GB:
7277 		speed = "1000 M";
7278 		break;
7279 	case I40E_LINK_SPEED_100MB:
7280 		speed = "100 M";
7281 		break;
7282 	default:
7283 		break;
7284 	}
7285 
7286 	switch (pf->hw.fc.current_mode) {
7287 	case I40E_FC_FULL:
7288 		fc = "RX/TX";
7289 		break;
7290 	case I40E_FC_TX_PAUSE:
7291 		fc = "TX";
7292 		break;
7293 	case I40E_FC_RX_PAUSE:
7294 		fc = "RX";
7295 		break;
7296 	default:
7297 		fc = "None";
7298 		break;
7299 	}
7300 
7301 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7302 		req_fec = "None";
7303 		fec = "None";
7304 		an = "False";
7305 
7306 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7307 			an = "True";
7308 
7309 		if (pf->hw.phy.link_info.fec_info &
7310 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7311 			fec = "CL74 FC-FEC/BASE-R";
7312 		else if (pf->hw.phy.link_info.fec_info &
7313 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7314 			fec = "CL108 RS-FEC";
7315 
7316 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7317 		 * both RS and FC are requested
7318 		 */
7319 		if (vsi->back->hw.phy.link_info.req_fec_info &
7320 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7321 			if (vsi->back->hw.phy.link_info.req_fec_info &
7322 			    I40E_AQ_REQUEST_FEC_RS)
7323 				req_fec = "CL108 RS-FEC";
7324 			else
7325 				req_fec = "CL74 FC-FEC/BASE-R";
7326 		}
7327 		netdev_info(vsi->netdev,
7328 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7329 			    speed, req_fec, fec, an, fc);
7330 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7331 		req_fec = "None";
7332 		fec = "None";
7333 		an = "False";
7334 
7335 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7336 			an = "True";
7337 
7338 		if (pf->hw.phy.link_info.fec_info &
7339 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7340 			fec = "CL74 FC-FEC/BASE-R";
7341 
7342 		if (pf->hw.phy.link_info.req_fec_info &
7343 		    I40E_AQ_REQUEST_FEC_KR)
7344 			req_fec = "CL74 FC-FEC/BASE-R";
7345 
7346 		netdev_info(vsi->netdev,
7347 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7348 			    speed, req_fec, fec, an, fc);
7349 	} else {
7350 		netdev_info(vsi->netdev,
7351 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7352 			    speed, fc);
7353 	}
7354 
7355 }
7356 
7357 /**
7358  * i40e_up_complete - Finish the last steps of bringing up a connection
7359  * @vsi: the VSI being configured
7360  **/
7361 static int i40e_up_complete(struct i40e_vsi *vsi)
7362 {
7363 	struct i40e_pf *pf = vsi->back;
7364 	int err;
7365 
7366 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7367 		i40e_vsi_configure_msix(vsi);
7368 	else
7369 		i40e_configure_msi_and_legacy(vsi);
7370 
7371 	/* start rings */
7372 	err = i40e_vsi_start_rings(vsi);
7373 	if (err)
7374 		return err;
7375 
7376 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7377 	i40e_napi_enable_all(vsi);
7378 	i40e_vsi_enable_irq(vsi);
7379 
7380 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7381 	    (vsi->netdev)) {
7382 		i40e_print_link_message(vsi, true);
7383 		netif_tx_start_all_queues(vsi->netdev);
7384 		netif_carrier_on(vsi->netdev);
7385 	}
7386 
7387 	/* replay FDIR SB filters */
7388 	if (vsi->type == I40E_VSI_FDIR) {
7389 		/* reset fd counters */
7390 		pf->fd_add_err = 0;
7391 		pf->fd_atr_cnt = 0;
7392 		i40e_fdir_filter_restore(vsi);
7393 	}
7394 
7395 	/* On the next run of the service_task, notify any clients of the new
7396 	 * opened netdev
7397 	 */
7398 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7399 	i40e_service_event_schedule(pf);
7400 
7401 	return 0;
7402 }
7403 
7404 /**
7405  * i40e_vsi_reinit_locked - Reset the VSI
7406  * @vsi: the VSI being configured
7407  *
7408  * Rebuild the ring structs after some configuration
7409  * has changed, e.g. MTU size.
7410  **/
7411 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7412 {
7413 	struct i40e_pf *pf = vsi->back;
7414 
7415 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7416 		usleep_range(1000, 2000);
7417 	i40e_down(vsi);
7418 
7419 	i40e_up(vsi);
7420 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7421 }
7422 
7423 /**
7424  * i40e_force_link_state - Force the link status
7425  * @pf: board private structure
7426  * @is_up: whether the link state should be forced up or down
7427  **/
7428 static int i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7429 {
7430 	struct i40e_aq_get_phy_abilities_resp abilities;
7431 	struct i40e_aq_set_phy_config config = {0};
7432 	bool non_zero_phy_type = is_up;
7433 	struct i40e_hw *hw = &pf->hw;
7434 	u64 mask;
7435 	u8 speed;
7436 	int err;
7437 
7438 	/* Card might've been put in an unstable state by other drivers
7439 	 * and applications, which causes incorrect speed values being
7440 	 * set on startup. In order to clear speed registers, we call
7441 	 * get_phy_capabilities twice, once to get initial state of
7442 	 * available speeds, and once to get current PHY config.
7443 	 */
7444 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7445 					   NULL);
7446 	if (err) {
7447 		dev_err(&pf->pdev->dev,
7448 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7449 			ERR_PTR(err),
7450 			i40e_aq_str(hw, hw->aq.asq_last_status));
7451 		return err;
7452 	}
7453 	speed = abilities.link_speed;
7454 
7455 	/* Get the current phy config */
7456 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7457 					   NULL);
7458 	if (err) {
7459 		dev_err(&pf->pdev->dev,
7460 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7461 			ERR_PTR(err),
7462 			i40e_aq_str(hw, hw->aq.asq_last_status));
7463 		return err;
7464 	}
7465 
7466 	/* If link needs to go up, but was not forced to go down,
7467 	 * and its speed values are OK, no need for a flap
7468 	 * if non_zero_phy_type was set, still need to force up
7469 	 */
7470 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7471 		non_zero_phy_type = true;
7472 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7473 		return 0;
7474 
7475 	/* To force link we need to set bits for all supported PHY types,
7476 	 * but there are now more than 32, so we need to split the bitmap
7477 	 * across two fields.
7478 	 */
7479 	mask = I40E_PHY_TYPES_BITMASK;
7480 	config.phy_type =
7481 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7482 	config.phy_type_ext =
7483 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7484 	/* Copy the old settings, except of phy_type */
7485 	config.abilities = abilities.abilities;
7486 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7487 		if (is_up)
7488 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7489 		else
7490 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7491 	}
7492 	if (abilities.link_speed != 0)
7493 		config.link_speed = abilities.link_speed;
7494 	else
7495 		config.link_speed = speed;
7496 	config.eee_capability = abilities.eee_capability;
7497 	config.eeer = abilities.eeer_val;
7498 	config.low_power_ctrl = abilities.d3_lpan;
7499 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7500 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7501 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7502 
7503 	if (err) {
7504 		dev_err(&pf->pdev->dev,
7505 			"set phy config ret =  %pe last_status =  %s\n",
7506 			ERR_PTR(err),
7507 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7508 		return err;
7509 	}
7510 
7511 	/* Update the link info */
7512 	err = i40e_update_link_info(hw);
7513 	if (err) {
7514 		/* Wait a little bit (on 40G cards it sometimes takes a really
7515 		 * long time for link to come back from the atomic reset)
7516 		 * and try once more
7517 		 */
7518 		msleep(1000);
7519 		i40e_update_link_info(hw);
7520 	}
7521 
7522 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7523 
7524 	return 0;
7525 }
7526 
7527 /**
7528  * i40e_up - Bring the connection back up after being down
7529  * @vsi: the VSI being configured
7530  **/
7531 int i40e_up(struct i40e_vsi *vsi)
7532 {
7533 	int err;
7534 
7535 	if (vsi->type == I40E_VSI_MAIN &&
7536 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7537 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7538 		i40e_force_link_state(vsi->back, true);
7539 
7540 	err = i40e_vsi_configure(vsi);
7541 	if (!err)
7542 		err = i40e_up_complete(vsi);
7543 
7544 	return err;
7545 }
7546 
7547 /**
7548  * i40e_down - Shutdown the connection processing
7549  * @vsi: the VSI being stopped
7550  **/
7551 void i40e_down(struct i40e_vsi *vsi)
7552 {
7553 	int i;
7554 
7555 	/* It is assumed that the caller of this function
7556 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7557 	 */
7558 	if (vsi->netdev) {
7559 		netif_carrier_off(vsi->netdev);
7560 		netif_tx_disable(vsi->netdev);
7561 	}
7562 	i40e_vsi_disable_irq(vsi);
7563 	i40e_vsi_stop_rings(vsi);
7564 	if (vsi->type == I40E_VSI_MAIN &&
7565 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7566 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7567 		i40e_force_link_state(vsi->back, false);
7568 	i40e_napi_disable_all(vsi);
7569 
7570 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7571 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7572 		if (i40e_enabled_xdp_vsi(vsi)) {
7573 			/* Make sure that in-progress ndo_xdp_xmit and
7574 			 * ndo_xsk_wakeup calls are completed.
7575 			 */
7576 			synchronize_rcu();
7577 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7578 		}
7579 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7580 	}
7581 
7582 }
7583 
7584 /**
7585  * i40e_validate_mqprio_qopt- validate queue mapping info
7586  * @vsi: the VSI being configured
7587  * @mqprio_qopt: queue parametrs
7588  **/
7589 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7590 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7591 {
7592 	u64 sum_max_rate = 0;
7593 	u64 max_rate = 0;
7594 	int i;
7595 
7596 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7597 	    mqprio_qopt->qopt.num_tc < 1 ||
7598 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7599 		return -EINVAL;
7600 	for (i = 0; ; i++) {
7601 		if (!mqprio_qopt->qopt.count[i])
7602 			return -EINVAL;
7603 		if (mqprio_qopt->min_rate[i]) {
7604 			dev_err(&vsi->back->pdev->dev,
7605 				"Invalid min tx rate (greater than 0) specified\n");
7606 			return -EINVAL;
7607 		}
7608 		max_rate = mqprio_qopt->max_rate[i];
7609 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7610 		sum_max_rate += max_rate;
7611 
7612 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7613 			break;
7614 		if (mqprio_qopt->qopt.offset[i + 1] !=
7615 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7616 			return -EINVAL;
7617 	}
7618 	if (vsi->num_queue_pairs <
7619 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7620 		dev_err(&vsi->back->pdev->dev,
7621 			"Failed to create traffic channel, insufficient number of queues.\n");
7622 		return -EINVAL;
7623 	}
7624 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7625 		dev_err(&vsi->back->pdev->dev,
7626 			"Invalid max tx rate specified\n");
7627 		return -EINVAL;
7628 	}
7629 	return 0;
7630 }
7631 
7632 /**
7633  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7634  * @vsi: the VSI being configured
7635  **/
7636 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7637 {
7638 	u16 qcount;
7639 	int i;
7640 
7641 	/* Only TC0 is enabled */
7642 	vsi->tc_config.numtc = 1;
7643 	vsi->tc_config.enabled_tc = 1;
7644 	qcount = min_t(int, vsi->alloc_queue_pairs,
7645 		       i40e_pf_get_max_q_per_tc(vsi->back));
7646 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7647 		/* For the TC that is not enabled set the offset to default
7648 		 * queue and allocate one queue for the given TC.
7649 		 */
7650 		vsi->tc_config.tc_info[i].qoffset = 0;
7651 		if (i == 0)
7652 			vsi->tc_config.tc_info[i].qcount = qcount;
7653 		else
7654 			vsi->tc_config.tc_info[i].qcount = 1;
7655 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7656 	}
7657 }
7658 
7659 /**
7660  * i40e_del_macvlan_filter
7661  * @hw: pointer to the HW structure
7662  * @seid: seid of the channel VSI
7663  * @macaddr: the mac address to apply as a filter
7664  * @aq_err: store the admin Q error
7665  *
7666  * This function deletes a mac filter on the channel VSI which serves as the
7667  * macvlan. Returns 0 on success.
7668  **/
7669 static int i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7670 				   const u8 *macaddr, int *aq_err)
7671 {
7672 	struct i40e_aqc_remove_macvlan_element_data element;
7673 	int status;
7674 
7675 	memset(&element, 0, sizeof(element));
7676 	ether_addr_copy(element.mac_addr, macaddr);
7677 	element.vlan_tag = 0;
7678 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7679 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7680 	*aq_err = hw->aq.asq_last_status;
7681 
7682 	return status;
7683 }
7684 
7685 /**
7686  * i40e_add_macvlan_filter
7687  * @hw: pointer to the HW structure
7688  * @seid: seid of the channel VSI
7689  * @macaddr: the mac address to apply as a filter
7690  * @aq_err: store the admin Q error
7691  *
7692  * This function adds a mac filter on the channel VSI which serves as the
7693  * macvlan. Returns 0 on success.
7694  **/
7695 static int i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7696 				   const u8 *macaddr, int *aq_err)
7697 {
7698 	struct i40e_aqc_add_macvlan_element_data element;
7699 	u16 cmd_flags = 0;
7700 	int status;
7701 
7702 	ether_addr_copy(element.mac_addr, macaddr);
7703 	element.vlan_tag = 0;
7704 	element.queue_number = 0;
7705 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7706 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7707 	element.flags = cpu_to_le16(cmd_flags);
7708 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7709 	*aq_err = hw->aq.asq_last_status;
7710 
7711 	return status;
7712 }
7713 
7714 /**
7715  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7716  * @vsi: the VSI we want to access
7717  * @ch: the channel we want to access
7718  */
7719 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7720 {
7721 	struct i40e_ring *tx_ring, *rx_ring;
7722 	u16 pf_q;
7723 	int i;
7724 
7725 	for (i = 0; i < ch->num_queue_pairs; i++) {
7726 		pf_q = ch->base_queue + i;
7727 		tx_ring = vsi->tx_rings[pf_q];
7728 		tx_ring->ch = NULL;
7729 		rx_ring = vsi->rx_rings[pf_q];
7730 		rx_ring->ch = NULL;
7731 	}
7732 }
7733 
7734 /**
7735  * i40e_free_macvlan_channels
7736  * @vsi: the VSI we want to access
7737  *
7738  * This function frees the Qs of the channel VSI from
7739  * the stack and also deletes the channel VSIs which
7740  * serve as macvlans.
7741  */
7742 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7743 {
7744 	struct i40e_channel *ch, *ch_tmp;
7745 	int ret;
7746 
7747 	if (list_empty(&vsi->macvlan_list))
7748 		return;
7749 
7750 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7751 		struct i40e_vsi *parent_vsi;
7752 
7753 		if (i40e_is_channel_macvlan(ch)) {
7754 			i40e_reset_ch_rings(vsi, ch);
7755 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7756 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7757 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7758 			kfree(ch->fwd);
7759 			ch->fwd = NULL;
7760 		}
7761 
7762 		list_del(&ch->list);
7763 		parent_vsi = ch->parent_vsi;
7764 		if (!parent_vsi || !ch->initialized) {
7765 			kfree(ch);
7766 			continue;
7767 		}
7768 
7769 		/* remove the VSI */
7770 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7771 					     NULL);
7772 		if (ret)
7773 			dev_err(&vsi->back->pdev->dev,
7774 				"unable to remove channel (%d) for parent VSI(%d)\n",
7775 				ch->seid, parent_vsi->seid);
7776 		kfree(ch);
7777 	}
7778 	vsi->macvlan_cnt = 0;
7779 }
7780 
7781 /**
7782  * i40e_fwd_ring_up - bring the macvlan device up
7783  * @vsi: the VSI we want to access
7784  * @vdev: macvlan netdevice
7785  * @fwd: the private fwd structure
7786  */
7787 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7788 			    struct i40e_fwd_adapter *fwd)
7789 {
7790 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7791 	int ret = 0, num_tc = 1,  i, aq_err;
7792 	struct i40e_pf *pf = vsi->back;
7793 	struct i40e_hw *hw = &pf->hw;
7794 
7795 	/* Go through the list and find an available channel */
7796 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7797 		if (!i40e_is_channel_macvlan(iter)) {
7798 			iter->fwd = fwd;
7799 			/* record configuration for macvlan interface in vdev */
7800 			for (i = 0; i < num_tc; i++)
7801 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7802 							     i,
7803 							     iter->num_queue_pairs,
7804 							     iter->base_queue);
7805 			for (i = 0; i < iter->num_queue_pairs; i++) {
7806 				struct i40e_ring *tx_ring, *rx_ring;
7807 				u16 pf_q;
7808 
7809 				pf_q = iter->base_queue + i;
7810 
7811 				/* Get to TX ring ptr */
7812 				tx_ring = vsi->tx_rings[pf_q];
7813 				tx_ring->ch = iter;
7814 
7815 				/* Get the RX ring ptr */
7816 				rx_ring = vsi->rx_rings[pf_q];
7817 				rx_ring->ch = iter;
7818 			}
7819 			ch = iter;
7820 			break;
7821 		}
7822 	}
7823 
7824 	if (!ch)
7825 		return -EINVAL;
7826 
7827 	/* Guarantee all rings are updated before we update the
7828 	 * MAC address filter.
7829 	 */
7830 	wmb();
7831 
7832 	/* Add a mac filter */
7833 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7834 	if (ret) {
7835 		/* if we cannot add the MAC rule then disable the offload */
7836 		macvlan_release_l2fw_offload(vdev);
7837 		for (i = 0; i < ch->num_queue_pairs; i++) {
7838 			struct i40e_ring *rx_ring;
7839 			u16 pf_q;
7840 
7841 			pf_q = ch->base_queue + i;
7842 			rx_ring = vsi->rx_rings[pf_q];
7843 			rx_ring->netdev = NULL;
7844 		}
7845 		dev_info(&pf->pdev->dev,
7846 			 "Error adding mac filter on macvlan err %pe, aq_err %s\n",
7847 			  ERR_PTR(ret),
7848 			  i40e_aq_str(hw, aq_err));
7849 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7850 	}
7851 
7852 	return ret;
7853 }
7854 
7855 /**
7856  * i40e_setup_macvlans - create the channels which will be macvlans
7857  * @vsi: the VSI we want to access
7858  * @macvlan_cnt: no. of macvlans to be setup
7859  * @qcnt: no. of Qs per macvlan
7860  * @vdev: macvlan netdevice
7861  */
7862 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7863 			       struct net_device *vdev)
7864 {
7865 	struct i40e_pf *pf = vsi->back;
7866 	struct i40e_hw *hw = &pf->hw;
7867 	struct i40e_vsi_context ctxt;
7868 	u16 sections, qmap, num_qps;
7869 	struct i40e_channel *ch;
7870 	int i, pow, ret = 0;
7871 	u8 offset = 0;
7872 
7873 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7874 		return -EINVAL;
7875 
7876 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7877 
7878 	/* find the next higher power-of-2 of num queue pairs */
7879 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7880 
7881 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7882 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7883 
7884 	/* Setup context bits for the main VSI */
7885 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7886 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7887 	memset(&ctxt, 0, sizeof(ctxt));
7888 	ctxt.seid = vsi->seid;
7889 	ctxt.pf_num = vsi->back->hw.pf_id;
7890 	ctxt.vf_num = 0;
7891 	ctxt.uplink_seid = vsi->uplink_seid;
7892 	ctxt.info = vsi->info;
7893 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7894 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7895 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7896 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7897 
7898 	/* Reconfigure RSS for main VSI with new max queue count */
7899 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7900 	ret = i40e_vsi_config_rss(vsi);
7901 	if (ret) {
7902 		dev_info(&pf->pdev->dev,
7903 			 "Failed to reconfig RSS for num_queues (%u)\n",
7904 			 vsi->rss_size);
7905 		return ret;
7906 	}
7907 	vsi->reconfig_rss = true;
7908 	dev_dbg(&vsi->back->pdev->dev,
7909 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7910 	vsi->next_base_queue = num_qps;
7911 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7912 
7913 	/* Update the VSI after updating the VSI queue-mapping
7914 	 * information
7915 	 */
7916 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7917 	if (ret) {
7918 		dev_info(&pf->pdev->dev,
7919 			 "Update vsi tc config failed, err %pe aq_err %s\n",
7920 			 ERR_PTR(ret),
7921 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7922 		return ret;
7923 	}
7924 	/* update the local VSI info with updated queue map */
7925 	i40e_vsi_update_queue_map(vsi, &ctxt);
7926 	vsi->info.valid_sections = 0;
7927 
7928 	/* Create channels for macvlans */
7929 	INIT_LIST_HEAD(&vsi->macvlan_list);
7930 	for (i = 0; i < macvlan_cnt; i++) {
7931 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7932 		if (!ch) {
7933 			ret = -ENOMEM;
7934 			goto err_free;
7935 		}
7936 		INIT_LIST_HEAD(&ch->list);
7937 		ch->num_queue_pairs = qcnt;
7938 		if (!i40e_setup_channel(pf, vsi, ch)) {
7939 			ret = -EINVAL;
7940 			kfree(ch);
7941 			goto err_free;
7942 		}
7943 		ch->parent_vsi = vsi;
7944 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7945 		vsi->macvlan_cnt++;
7946 		list_add_tail(&ch->list, &vsi->macvlan_list);
7947 	}
7948 
7949 	return ret;
7950 
7951 err_free:
7952 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7953 	i40e_free_macvlan_channels(vsi);
7954 
7955 	return ret;
7956 }
7957 
7958 /**
7959  * i40e_fwd_add - configure macvlans
7960  * @netdev: net device to configure
7961  * @vdev: macvlan netdevice
7962  **/
7963 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7964 {
7965 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7966 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7967 	struct i40e_vsi *vsi = np->vsi;
7968 	struct i40e_pf *pf = vsi->back;
7969 	struct i40e_fwd_adapter *fwd;
7970 	int avail_macvlan, ret;
7971 
7972 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7973 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7974 		return ERR_PTR(-EINVAL);
7975 	}
7976 	if (i40e_is_tc_mqprio_enabled(pf)) {
7977 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7978 		return ERR_PTR(-EINVAL);
7979 	}
7980 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7981 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7982 		return ERR_PTR(-EINVAL);
7983 	}
7984 
7985 	/* The macvlan device has to be a single Q device so that the
7986 	 * tc_to_txq field can be reused to pick the tx queue.
7987 	 */
7988 	if (netif_is_multiqueue(vdev))
7989 		return ERR_PTR(-ERANGE);
7990 
7991 	if (!vsi->macvlan_cnt) {
7992 		/* reserve bit 0 for the pf device */
7993 		set_bit(0, vsi->fwd_bitmask);
7994 
7995 		/* Try to reserve as many queues as possible for macvlans. First
7996 		 * reserve 3/4th of max vectors, then half, then quarter and
7997 		 * calculate Qs per macvlan as you go
7998 		 */
7999 		vectors = pf->num_lan_msix;
8000 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
8001 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
8002 			q_per_macvlan = 4;
8003 			macvlan_cnt = (vectors - 32) / 4;
8004 		} else if (vectors <= 64 && vectors > 32) {
8005 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
8006 			q_per_macvlan = 2;
8007 			macvlan_cnt = (vectors - 16) / 2;
8008 		} else if (vectors <= 32 && vectors > 16) {
8009 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
8010 			q_per_macvlan = 1;
8011 			macvlan_cnt = vectors - 16;
8012 		} else if (vectors <= 16 && vectors > 8) {
8013 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
8014 			q_per_macvlan = 1;
8015 			macvlan_cnt = vectors - 8;
8016 		} else {
8017 			/* allocate 1 Q per macvlan and 1 Q to the PF */
8018 			q_per_macvlan = 1;
8019 			macvlan_cnt = vectors - 1;
8020 		}
8021 
8022 		if (macvlan_cnt == 0)
8023 			return ERR_PTR(-EBUSY);
8024 
8025 		/* Quiesce VSI queues */
8026 		i40e_quiesce_vsi(vsi);
8027 
8028 		/* sets up the macvlans but does not "enable" them */
8029 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8030 					  vdev);
8031 		if (ret)
8032 			return ERR_PTR(ret);
8033 
8034 		/* Unquiesce VSI */
8035 		i40e_unquiesce_vsi(vsi);
8036 	}
8037 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8038 					    vsi->macvlan_cnt);
8039 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8040 		return ERR_PTR(-EBUSY);
8041 
8042 	/* create the fwd struct */
8043 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8044 	if (!fwd)
8045 		return ERR_PTR(-ENOMEM);
8046 
8047 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8048 	fwd->bit_no = avail_macvlan;
8049 	netdev_set_sb_channel(vdev, avail_macvlan);
8050 	fwd->netdev = vdev;
8051 
8052 	if (!netif_running(netdev))
8053 		return fwd;
8054 
8055 	/* Set fwd ring up */
8056 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8057 	if (ret) {
8058 		/* unbind the queues and drop the subordinate channel config */
8059 		netdev_unbind_sb_channel(netdev, vdev);
8060 		netdev_set_sb_channel(vdev, 0);
8061 
8062 		kfree(fwd);
8063 		return ERR_PTR(-EINVAL);
8064 	}
8065 
8066 	return fwd;
8067 }
8068 
8069 /**
8070  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8071  * @vsi: the VSI we want to access
8072  */
8073 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8074 {
8075 	struct i40e_channel *ch, *ch_tmp;
8076 	struct i40e_pf *pf = vsi->back;
8077 	struct i40e_hw *hw = &pf->hw;
8078 	int aq_err, ret = 0;
8079 
8080 	if (list_empty(&vsi->macvlan_list))
8081 		return;
8082 
8083 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8084 		if (i40e_is_channel_macvlan(ch)) {
8085 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8086 						      i40e_channel_mac(ch),
8087 						      &aq_err);
8088 			if (!ret) {
8089 				/* Reset queue contexts */
8090 				i40e_reset_ch_rings(vsi, ch);
8091 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8092 				netdev_unbind_sb_channel(vsi->netdev,
8093 							 ch->fwd->netdev);
8094 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8095 				kfree(ch->fwd);
8096 				ch->fwd = NULL;
8097 			}
8098 		}
8099 	}
8100 }
8101 
8102 /**
8103  * i40e_fwd_del - delete macvlan interfaces
8104  * @netdev: net device to configure
8105  * @vdev: macvlan netdevice
8106  */
8107 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8108 {
8109 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8110 	struct i40e_fwd_adapter *fwd = vdev;
8111 	struct i40e_channel *ch, *ch_tmp;
8112 	struct i40e_vsi *vsi = np->vsi;
8113 	struct i40e_pf *pf = vsi->back;
8114 	struct i40e_hw *hw = &pf->hw;
8115 	int aq_err, ret = 0;
8116 
8117 	/* Find the channel associated with the macvlan and del mac filter */
8118 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8119 		if (i40e_is_channel_macvlan(ch) &&
8120 		    ether_addr_equal(i40e_channel_mac(ch),
8121 				     fwd->netdev->dev_addr)) {
8122 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8123 						      i40e_channel_mac(ch),
8124 						      &aq_err);
8125 			if (!ret) {
8126 				/* Reset queue contexts */
8127 				i40e_reset_ch_rings(vsi, ch);
8128 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8129 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8130 				netdev_set_sb_channel(fwd->netdev, 0);
8131 				kfree(ch->fwd);
8132 				ch->fwd = NULL;
8133 			} else {
8134 				dev_info(&pf->pdev->dev,
8135 					 "Error deleting mac filter on macvlan err %pe, aq_err %s\n",
8136 					  ERR_PTR(ret),
8137 					  i40e_aq_str(hw, aq_err));
8138 			}
8139 			break;
8140 		}
8141 	}
8142 }
8143 
8144 /**
8145  * i40e_setup_tc - configure multiple traffic classes
8146  * @netdev: net device to configure
8147  * @type_data: tc offload data
8148  **/
8149 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8150 {
8151 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8152 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8153 	struct i40e_vsi *vsi = np->vsi;
8154 	struct i40e_pf *pf = vsi->back;
8155 	u8 enabled_tc = 0, num_tc, hw;
8156 	bool need_reset = false;
8157 	int old_queue_pairs;
8158 	int ret = -EINVAL;
8159 	u16 mode;
8160 	int i;
8161 
8162 	old_queue_pairs = vsi->num_queue_pairs;
8163 	num_tc = mqprio_qopt->qopt.num_tc;
8164 	hw = mqprio_qopt->qopt.hw;
8165 	mode = mqprio_qopt->mode;
8166 	if (!hw) {
8167 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8168 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8169 		goto config_tc;
8170 	}
8171 
8172 	/* Check if MFP enabled */
8173 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
8174 		netdev_info(netdev,
8175 			    "Configuring TC not supported in MFP mode\n");
8176 		return ret;
8177 	}
8178 	switch (mode) {
8179 	case TC_MQPRIO_MODE_DCB:
8180 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8181 
8182 		/* Check if DCB enabled to continue */
8183 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
8184 			netdev_info(netdev,
8185 				    "DCB is not enabled for adapter\n");
8186 			return ret;
8187 		}
8188 
8189 		/* Check whether tc count is within enabled limit */
8190 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8191 			netdev_info(netdev,
8192 				    "TC count greater than enabled on link for adapter\n");
8193 			return ret;
8194 		}
8195 		break;
8196 	case TC_MQPRIO_MODE_CHANNEL:
8197 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
8198 			netdev_info(netdev,
8199 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8200 			return ret;
8201 		}
8202 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8203 			return ret;
8204 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8205 		if (ret)
8206 			return ret;
8207 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8208 		       sizeof(*mqprio_qopt));
8209 		pf->flags |= I40E_FLAG_TC_MQPRIO;
8210 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8211 		break;
8212 	default:
8213 		return -EINVAL;
8214 	}
8215 
8216 config_tc:
8217 	/* Generate TC map for number of tc requested */
8218 	for (i = 0; i < num_tc; i++)
8219 		enabled_tc |= BIT(i);
8220 
8221 	/* Requesting same TC configuration as already enabled */
8222 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8223 	    mode != TC_MQPRIO_MODE_CHANNEL)
8224 		return 0;
8225 
8226 	/* Quiesce VSI queues */
8227 	i40e_quiesce_vsi(vsi);
8228 
8229 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8230 		i40e_remove_queue_channels(vsi);
8231 
8232 	/* Configure VSI for enabled TCs */
8233 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8234 	if (ret) {
8235 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8236 			    vsi->seid);
8237 		need_reset = true;
8238 		goto exit;
8239 	} else if (enabled_tc &&
8240 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8241 		netdev_info(netdev,
8242 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8243 			    vsi->tc_config.tc_info[0].qcount);
8244 		ret = -EINVAL;
8245 		need_reset = true;
8246 		goto exit;
8247 	}
8248 
8249 	dev_info(&vsi->back->pdev->dev,
8250 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8251 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8252 
8253 	if (i40e_is_tc_mqprio_enabled(pf)) {
8254 		if (vsi->mqprio_qopt.max_rate[0]) {
8255 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8256 						  vsi->mqprio_qopt.max_rate[0]);
8257 
8258 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8259 			if (!ret) {
8260 				u64 credits = max_tx_rate;
8261 
8262 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8263 				dev_dbg(&vsi->back->pdev->dev,
8264 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8265 					max_tx_rate,
8266 					credits,
8267 					vsi->seid);
8268 			} else {
8269 				need_reset = true;
8270 				goto exit;
8271 			}
8272 		}
8273 		ret = i40e_configure_queue_channels(vsi);
8274 		if (ret) {
8275 			vsi->num_queue_pairs = old_queue_pairs;
8276 			netdev_info(netdev,
8277 				    "Failed configuring queue channels\n");
8278 			need_reset = true;
8279 			goto exit;
8280 		}
8281 	}
8282 
8283 exit:
8284 	/* Reset the configuration data to defaults, only TC0 is enabled */
8285 	if (need_reset) {
8286 		i40e_vsi_set_default_tc_config(vsi);
8287 		need_reset = false;
8288 	}
8289 
8290 	/* Unquiesce VSI */
8291 	i40e_unquiesce_vsi(vsi);
8292 	return ret;
8293 }
8294 
8295 /**
8296  * i40e_set_cld_element - sets cloud filter element data
8297  * @filter: cloud filter rule
8298  * @cld: ptr to cloud filter element data
8299  *
8300  * This is helper function to copy data into cloud filter element
8301  **/
8302 static inline void
8303 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8304 		     struct i40e_aqc_cloud_filters_element_data *cld)
8305 {
8306 	u32 ipa;
8307 	int i;
8308 
8309 	memset(cld, 0, sizeof(*cld));
8310 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8311 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8312 
8313 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8314 		return;
8315 
8316 	if (filter->n_proto == ETH_P_IPV6) {
8317 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8318 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8319 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8320 
8321 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8322 		}
8323 	} else {
8324 		ipa = be32_to_cpu(filter->dst_ipv4);
8325 
8326 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8327 	}
8328 
8329 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8330 
8331 	/* tenant_id is not supported by FW now, once the support is enabled
8332 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8333 	 */
8334 	if (filter->tenant_id)
8335 		return;
8336 }
8337 
8338 /**
8339  * i40e_add_del_cloud_filter - Add/del cloud filter
8340  * @vsi: pointer to VSI
8341  * @filter: cloud filter rule
8342  * @add: if true, add, if false, delete
8343  *
8344  * Add or delete a cloud filter for a specific flow spec.
8345  * Returns 0 if the filter were successfully added.
8346  **/
8347 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8348 			      struct i40e_cloud_filter *filter, bool add)
8349 {
8350 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8351 	struct i40e_pf *pf = vsi->back;
8352 	int ret;
8353 	static const u16 flag_table[128] = {
8354 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8355 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8356 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8357 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8358 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8359 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8360 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8361 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8362 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8363 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8364 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8365 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8366 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8367 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8368 	};
8369 
8370 	if (filter->flags >= ARRAY_SIZE(flag_table))
8371 		return -EIO;
8372 
8373 	memset(&cld_filter, 0, sizeof(cld_filter));
8374 
8375 	/* copy element needed to add cloud filter from filter */
8376 	i40e_set_cld_element(filter, &cld_filter);
8377 
8378 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8379 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8380 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8381 
8382 	if (filter->n_proto == ETH_P_IPV6)
8383 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8384 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8385 	else
8386 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8387 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8388 
8389 	if (add)
8390 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8391 						&cld_filter, 1);
8392 	else
8393 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8394 						&cld_filter, 1);
8395 	if (ret)
8396 		dev_dbg(&pf->pdev->dev,
8397 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8398 			add ? "add" : "delete", filter->dst_port, ret,
8399 			pf->hw.aq.asq_last_status);
8400 	else
8401 		dev_info(&pf->pdev->dev,
8402 			 "%s cloud filter for VSI: %d\n",
8403 			 add ? "Added" : "Deleted", filter->seid);
8404 	return ret;
8405 }
8406 
8407 /**
8408  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8409  * @vsi: pointer to VSI
8410  * @filter: cloud filter rule
8411  * @add: if true, add, if false, delete
8412  *
8413  * Add or delete a cloud filter for a specific flow spec using big buffer.
8414  * Returns 0 if the filter were successfully added.
8415  **/
8416 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8417 				      struct i40e_cloud_filter *filter,
8418 				      bool add)
8419 {
8420 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8421 	struct i40e_pf *pf = vsi->back;
8422 	int ret;
8423 
8424 	/* Both (src/dst) valid mac_addr are not supported */
8425 	if ((is_valid_ether_addr(filter->dst_mac) &&
8426 	     is_valid_ether_addr(filter->src_mac)) ||
8427 	    (is_multicast_ether_addr(filter->dst_mac) &&
8428 	     is_multicast_ether_addr(filter->src_mac)))
8429 		return -EOPNOTSUPP;
8430 
8431 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8432 	 * ports are not supported via big buffer now.
8433 	 */
8434 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8435 		return -EOPNOTSUPP;
8436 
8437 	/* adding filter using src_port/src_ip is not supported at this stage */
8438 	if (filter->src_port ||
8439 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8440 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8441 		return -EOPNOTSUPP;
8442 
8443 	memset(&cld_filter, 0, sizeof(cld_filter));
8444 
8445 	/* copy element needed to add cloud filter from filter */
8446 	i40e_set_cld_element(filter, &cld_filter.element);
8447 
8448 	if (is_valid_ether_addr(filter->dst_mac) ||
8449 	    is_valid_ether_addr(filter->src_mac) ||
8450 	    is_multicast_ether_addr(filter->dst_mac) ||
8451 	    is_multicast_ether_addr(filter->src_mac)) {
8452 		/* MAC + IP : unsupported mode */
8453 		if (filter->dst_ipv4)
8454 			return -EOPNOTSUPP;
8455 
8456 		/* since we validated that L4 port must be valid before
8457 		 * we get here, start with respective "flags" value
8458 		 * and update if vlan is present or not
8459 		 */
8460 		cld_filter.element.flags =
8461 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8462 
8463 		if (filter->vlan_id) {
8464 			cld_filter.element.flags =
8465 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8466 		}
8467 
8468 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8469 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8470 		cld_filter.element.flags =
8471 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8472 		if (filter->n_proto == ETH_P_IPV6)
8473 			cld_filter.element.flags |=
8474 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8475 		else
8476 			cld_filter.element.flags |=
8477 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8478 	} else {
8479 		dev_err(&pf->pdev->dev,
8480 			"either mac or ip has to be valid for cloud filter\n");
8481 		return -EINVAL;
8482 	}
8483 
8484 	/* Now copy L4 port in Byte 6..7 in general fields */
8485 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8486 						be16_to_cpu(filter->dst_port);
8487 
8488 	if (add) {
8489 		/* Validate current device switch mode, change if necessary */
8490 		ret = i40e_validate_and_set_switch_mode(vsi);
8491 		if (ret) {
8492 			dev_err(&pf->pdev->dev,
8493 				"failed to set switch mode, ret %d\n",
8494 				ret);
8495 			return ret;
8496 		}
8497 
8498 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8499 						   &cld_filter, 1);
8500 	} else {
8501 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8502 						   &cld_filter, 1);
8503 	}
8504 
8505 	if (ret)
8506 		dev_dbg(&pf->pdev->dev,
8507 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8508 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8509 	else
8510 		dev_info(&pf->pdev->dev,
8511 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8512 			 add ? "add" : "delete", filter->seid,
8513 			 ntohs(filter->dst_port));
8514 	return ret;
8515 }
8516 
8517 /**
8518  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8519  * @vsi: Pointer to VSI
8520  * @f: Pointer to struct flow_cls_offload
8521  * @filter: Pointer to cloud filter structure
8522  *
8523  **/
8524 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8525 				 struct flow_cls_offload *f,
8526 				 struct i40e_cloud_filter *filter)
8527 {
8528 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8529 	struct flow_dissector *dissector = rule->match.dissector;
8530 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8531 	struct i40e_pf *pf = vsi->back;
8532 	u8 field_flags = 0;
8533 
8534 	if (dissector->used_keys &
8535 	    ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
8536 	      BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
8537 	      BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8538 	      BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
8539 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8540 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8541 	      BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
8542 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8543 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%llx\n",
8544 			dissector->used_keys);
8545 		return -EOPNOTSUPP;
8546 	}
8547 
8548 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8549 		struct flow_match_enc_keyid match;
8550 
8551 		flow_rule_match_enc_keyid(rule, &match);
8552 		if (match.mask->keyid != 0)
8553 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8554 
8555 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8556 	}
8557 
8558 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8559 		struct flow_match_basic match;
8560 
8561 		flow_rule_match_basic(rule, &match);
8562 		n_proto_key = ntohs(match.key->n_proto);
8563 		n_proto_mask = ntohs(match.mask->n_proto);
8564 
8565 		if (n_proto_key == ETH_P_ALL) {
8566 			n_proto_key = 0;
8567 			n_proto_mask = 0;
8568 		}
8569 		filter->n_proto = n_proto_key & n_proto_mask;
8570 		filter->ip_proto = match.key->ip_proto;
8571 	}
8572 
8573 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8574 		struct flow_match_eth_addrs match;
8575 
8576 		flow_rule_match_eth_addrs(rule, &match);
8577 
8578 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8579 		if (!is_zero_ether_addr(match.mask->dst)) {
8580 			if (is_broadcast_ether_addr(match.mask->dst)) {
8581 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8582 			} else {
8583 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8584 					match.mask->dst);
8585 				return -EIO;
8586 			}
8587 		}
8588 
8589 		if (!is_zero_ether_addr(match.mask->src)) {
8590 			if (is_broadcast_ether_addr(match.mask->src)) {
8591 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8592 			} else {
8593 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8594 					match.mask->src);
8595 				return -EIO;
8596 			}
8597 		}
8598 		ether_addr_copy(filter->dst_mac, match.key->dst);
8599 		ether_addr_copy(filter->src_mac, match.key->src);
8600 	}
8601 
8602 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8603 		struct flow_match_vlan match;
8604 
8605 		flow_rule_match_vlan(rule, &match);
8606 		if (match.mask->vlan_id) {
8607 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8608 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8609 
8610 			} else {
8611 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8612 					match.mask->vlan_id);
8613 				return -EIO;
8614 			}
8615 		}
8616 
8617 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8618 	}
8619 
8620 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8621 		struct flow_match_control match;
8622 
8623 		flow_rule_match_control(rule, &match);
8624 		addr_type = match.key->addr_type;
8625 	}
8626 
8627 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8628 		struct flow_match_ipv4_addrs match;
8629 
8630 		flow_rule_match_ipv4_addrs(rule, &match);
8631 		if (match.mask->dst) {
8632 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8633 				field_flags |= I40E_CLOUD_FIELD_IIP;
8634 			} else {
8635 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8636 					&match.mask->dst);
8637 				return -EIO;
8638 			}
8639 		}
8640 
8641 		if (match.mask->src) {
8642 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8643 				field_flags |= I40E_CLOUD_FIELD_IIP;
8644 			} else {
8645 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8646 					&match.mask->src);
8647 				return -EIO;
8648 			}
8649 		}
8650 
8651 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8652 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8653 			return -EIO;
8654 		}
8655 		filter->dst_ipv4 = match.key->dst;
8656 		filter->src_ipv4 = match.key->src;
8657 	}
8658 
8659 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8660 		struct flow_match_ipv6_addrs match;
8661 
8662 		flow_rule_match_ipv6_addrs(rule, &match);
8663 
8664 		/* src and dest IPV6 address should not be LOOPBACK
8665 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8666 		 */
8667 		if (ipv6_addr_loopback(&match.key->dst) ||
8668 		    ipv6_addr_loopback(&match.key->src)) {
8669 			dev_err(&pf->pdev->dev,
8670 				"Bad ipv6, addr is LOOPBACK\n");
8671 			return -EIO;
8672 		}
8673 		if (!ipv6_addr_any(&match.mask->dst) ||
8674 		    !ipv6_addr_any(&match.mask->src))
8675 			field_flags |= I40E_CLOUD_FIELD_IIP;
8676 
8677 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8678 		       sizeof(filter->src_ipv6));
8679 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8680 		       sizeof(filter->dst_ipv6));
8681 	}
8682 
8683 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8684 		struct flow_match_ports match;
8685 
8686 		flow_rule_match_ports(rule, &match);
8687 		if (match.mask->src) {
8688 			if (match.mask->src == cpu_to_be16(0xffff)) {
8689 				field_flags |= I40E_CLOUD_FIELD_IIP;
8690 			} else {
8691 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8692 					be16_to_cpu(match.mask->src));
8693 				return -EIO;
8694 			}
8695 		}
8696 
8697 		if (match.mask->dst) {
8698 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8699 				field_flags |= I40E_CLOUD_FIELD_IIP;
8700 			} else {
8701 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8702 					be16_to_cpu(match.mask->dst));
8703 				return -EIO;
8704 			}
8705 		}
8706 
8707 		filter->dst_port = match.key->dst;
8708 		filter->src_port = match.key->src;
8709 
8710 		switch (filter->ip_proto) {
8711 		case IPPROTO_TCP:
8712 		case IPPROTO_UDP:
8713 			break;
8714 		default:
8715 			dev_err(&pf->pdev->dev,
8716 				"Only UDP and TCP transport are supported\n");
8717 			return -EINVAL;
8718 		}
8719 	}
8720 	filter->flags = field_flags;
8721 	return 0;
8722 }
8723 
8724 /**
8725  * i40e_handle_tclass: Forward to a traffic class on the device
8726  * @vsi: Pointer to VSI
8727  * @tc: traffic class index on the device
8728  * @filter: Pointer to cloud filter structure
8729  *
8730  **/
8731 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8732 			      struct i40e_cloud_filter *filter)
8733 {
8734 	struct i40e_channel *ch, *ch_tmp;
8735 
8736 	/* direct to a traffic class on the same device */
8737 	if (tc == 0) {
8738 		filter->seid = vsi->seid;
8739 		return 0;
8740 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8741 		if (!filter->dst_port) {
8742 			dev_err(&vsi->back->pdev->dev,
8743 				"Specify destination port to direct to traffic class that is not default\n");
8744 			return -EINVAL;
8745 		}
8746 		if (list_empty(&vsi->ch_list))
8747 			return -EINVAL;
8748 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8749 					 list) {
8750 			if (ch->seid == vsi->tc_seid_map[tc])
8751 				filter->seid = ch->seid;
8752 		}
8753 		return 0;
8754 	}
8755 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8756 	return -EINVAL;
8757 }
8758 
8759 /**
8760  * i40e_configure_clsflower - Configure tc flower filters
8761  * @vsi: Pointer to VSI
8762  * @cls_flower: Pointer to struct flow_cls_offload
8763  *
8764  **/
8765 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8766 				    struct flow_cls_offload *cls_flower)
8767 {
8768 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8769 	struct i40e_cloud_filter *filter = NULL;
8770 	struct i40e_pf *pf = vsi->back;
8771 	int err = 0;
8772 
8773 	if (tc < 0) {
8774 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8775 		return -EOPNOTSUPP;
8776 	}
8777 
8778 	if (!tc) {
8779 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8780 		return -EINVAL;
8781 	}
8782 
8783 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8784 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8785 		return -EBUSY;
8786 
8787 	if (pf->fdir_pf_active_filters ||
8788 	    (!hlist_empty(&pf->fdir_filter_list))) {
8789 		dev_err(&vsi->back->pdev->dev,
8790 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8791 		return -EINVAL;
8792 	}
8793 
8794 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8795 		dev_err(&vsi->back->pdev->dev,
8796 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8797 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8798 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8799 	}
8800 
8801 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8802 	if (!filter)
8803 		return -ENOMEM;
8804 
8805 	filter->cookie = cls_flower->cookie;
8806 
8807 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8808 	if (err < 0)
8809 		goto err;
8810 
8811 	err = i40e_handle_tclass(vsi, tc, filter);
8812 	if (err < 0)
8813 		goto err;
8814 
8815 	/* Add cloud filter */
8816 	if (filter->dst_port)
8817 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8818 	else
8819 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8820 
8821 	if (err) {
8822 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8823 			err);
8824 		goto err;
8825 	}
8826 
8827 	/* add filter to the ordered list */
8828 	INIT_HLIST_NODE(&filter->cloud_node);
8829 
8830 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8831 
8832 	pf->num_cloud_filters++;
8833 
8834 	return err;
8835 err:
8836 	kfree(filter);
8837 	return err;
8838 }
8839 
8840 /**
8841  * i40e_find_cloud_filter - Find the could filter in the list
8842  * @vsi: Pointer to VSI
8843  * @cookie: filter specific cookie
8844  *
8845  **/
8846 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8847 							unsigned long *cookie)
8848 {
8849 	struct i40e_cloud_filter *filter = NULL;
8850 	struct hlist_node *node2;
8851 
8852 	hlist_for_each_entry_safe(filter, node2,
8853 				  &vsi->back->cloud_filter_list, cloud_node)
8854 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8855 			return filter;
8856 	return NULL;
8857 }
8858 
8859 /**
8860  * i40e_delete_clsflower - Remove tc flower filters
8861  * @vsi: Pointer to VSI
8862  * @cls_flower: Pointer to struct flow_cls_offload
8863  *
8864  **/
8865 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8866 				 struct flow_cls_offload *cls_flower)
8867 {
8868 	struct i40e_cloud_filter *filter = NULL;
8869 	struct i40e_pf *pf = vsi->back;
8870 	int err = 0;
8871 
8872 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8873 
8874 	if (!filter)
8875 		return -EINVAL;
8876 
8877 	hash_del(&filter->cloud_node);
8878 
8879 	if (filter->dst_port)
8880 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8881 	else
8882 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8883 
8884 	kfree(filter);
8885 	if (err) {
8886 		dev_err(&pf->pdev->dev,
8887 			"Failed to delete cloud filter, err %pe\n",
8888 			ERR_PTR(err));
8889 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8890 	}
8891 
8892 	pf->num_cloud_filters--;
8893 	if (!pf->num_cloud_filters)
8894 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8895 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8896 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8897 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8898 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8899 		}
8900 	return 0;
8901 }
8902 
8903 /**
8904  * i40e_setup_tc_cls_flower - flower classifier offloads
8905  * @np: net device to configure
8906  * @cls_flower: offload data
8907  **/
8908 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8909 				    struct flow_cls_offload *cls_flower)
8910 {
8911 	struct i40e_vsi *vsi = np->vsi;
8912 
8913 	switch (cls_flower->command) {
8914 	case FLOW_CLS_REPLACE:
8915 		return i40e_configure_clsflower(vsi, cls_flower);
8916 	case FLOW_CLS_DESTROY:
8917 		return i40e_delete_clsflower(vsi, cls_flower);
8918 	case FLOW_CLS_STATS:
8919 		return -EOPNOTSUPP;
8920 	default:
8921 		return -EOPNOTSUPP;
8922 	}
8923 }
8924 
8925 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8926 				  void *cb_priv)
8927 {
8928 	struct i40e_netdev_priv *np = cb_priv;
8929 
8930 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8931 		return -EOPNOTSUPP;
8932 
8933 	switch (type) {
8934 	case TC_SETUP_CLSFLOWER:
8935 		return i40e_setup_tc_cls_flower(np, type_data);
8936 
8937 	default:
8938 		return -EOPNOTSUPP;
8939 	}
8940 }
8941 
8942 static LIST_HEAD(i40e_block_cb_list);
8943 
8944 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8945 			   void *type_data)
8946 {
8947 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8948 
8949 	switch (type) {
8950 	case TC_SETUP_QDISC_MQPRIO:
8951 		return i40e_setup_tc(netdev, type_data);
8952 	case TC_SETUP_BLOCK:
8953 		return flow_block_cb_setup_simple(type_data,
8954 						  &i40e_block_cb_list,
8955 						  i40e_setup_tc_block_cb,
8956 						  np, np, true);
8957 	default:
8958 		return -EOPNOTSUPP;
8959 	}
8960 }
8961 
8962 /**
8963  * i40e_open - Called when a network interface is made active
8964  * @netdev: network interface device structure
8965  *
8966  * The open entry point is called when a network interface is made
8967  * active by the system (IFF_UP).  At this point all resources needed
8968  * for transmit and receive operations are allocated, the interrupt
8969  * handler is registered with the OS, the netdev watchdog subtask is
8970  * enabled, and the stack is notified that the interface is ready.
8971  *
8972  * Returns 0 on success, negative value on failure
8973  **/
8974 int i40e_open(struct net_device *netdev)
8975 {
8976 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8977 	struct i40e_vsi *vsi = np->vsi;
8978 	struct i40e_pf *pf = vsi->back;
8979 	int err;
8980 
8981 	/* disallow open during test or if eeprom is broken */
8982 	if (test_bit(__I40E_TESTING, pf->state) ||
8983 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8984 		return -EBUSY;
8985 
8986 	netif_carrier_off(netdev);
8987 
8988 	if (i40e_force_link_state(pf, true))
8989 		return -EAGAIN;
8990 
8991 	err = i40e_vsi_open(vsi);
8992 	if (err)
8993 		return err;
8994 
8995 	/* configure global TSO hardware offload settings */
8996 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8997 						       TCP_FLAG_FIN) >> 16);
8998 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8999 						       TCP_FLAG_FIN |
9000 						       TCP_FLAG_CWR) >> 16);
9001 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
9002 	udp_tunnel_get_rx_info(netdev);
9003 
9004 	return 0;
9005 }
9006 
9007 /**
9008  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
9009  * @vsi: vsi structure
9010  *
9011  * This updates netdev's number of tx/rx queues
9012  *
9013  * Returns status of setting tx/rx queues
9014  **/
9015 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
9016 {
9017 	int ret;
9018 
9019 	ret = netif_set_real_num_rx_queues(vsi->netdev,
9020 					   vsi->num_queue_pairs);
9021 	if (ret)
9022 		return ret;
9023 
9024 	return netif_set_real_num_tx_queues(vsi->netdev,
9025 					    vsi->num_queue_pairs);
9026 }
9027 
9028 /**
9029  * i40e_vsi_open -
9030  * @vsi: the VSI to open
9031  *
9032  * Finish initialization of the VSI.
9033  *
9034  * Returns 0 on success, negative value on failure
9035  *
9036  * Note: expects to be called while under rtnl_lock()
9037  **/
9038 int i40e_vsi_open(struct i40e_vsi *vsi)
9039 {
9040 	struct i40e_pf *pf = vsi->back;
9041 	char int_name[I40E_INT_NAME_STR_LEN];
9042 	int err;
9043 
9044 	/* allocate descriptors */
9045 	err = i40e_vsi_setup_tx_resources(vsi);
9046 	if (err)
9047 		goto err_setup_tx;
9048 	err = i40e_vsi_setup_rx_resources(vsi);
9049 	if (err)
9050 		goto err_setup_rx;
9051 
9052 	err = i40e_vsi_configure(vsi);
9053 	if (err)
9054 		goto err_setup_rx;
9055 
9056 	if (vsi->netdev) {
9057 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9058 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9059 		err = i40e_vsi_request_irq(vsi, int_name);
9060 		if (err)
9061 			goto err_setup_rx;
9062 
9063 		/* Notify the stack of the actual queue counts. */
9064 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9065 		if (err)
9066 			goto err_set_queues;
9067 
9068 	} else if (vsi->type == I40E_VSI_FDIR) {
9069 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9070 			 dev_driver_string(&pf->pdev->dev),
9071 			 dev_name(&pf->pdev->dev));
9072 		err = i40e_vsi_request_irq(vsi, int_name);
9073 		if (err)
9074 			goto err_setup_rx;
9075 
9076 	} else {
9077 		err = -EINVAL;
9078 		goto err_setup_rx;
9079 	}
9080 
9081 	err = i40e_up_complete(vsi);
9082 	if (err)
9083 		goto err_up_complete;
9084 
9085 	return 0;
9086 
9087 err_up_complete:
9088 	i40e_down(vsi);
9089 err_set_queues:
9090 	i40e_vsi_free_irq(vsi);
9091 err_setup_rx:
9092 	i40e_vsi_free_rx_resources(vsi);
9093 err_setup_tx:
9094 	i40e_vsi_free_tx_resources(vsi);
9095 	if (vsi == pf->vsi[pf->lan_vsi])
9096 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9097 
9098 	return err;
9099 }
9100 
9101 /**
9102  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9103  * @pf: Pointer to PF
9104  *
9105  * This function destroys the hlist where all the Flow Director
9106  * filters were saved.
9107  **/
9108 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9109 {
9110 	struct i40e_fdir_filter *filter;
9111 	struct i40e_flex_pit *pit_entry, *tmp;
9112 	struct hlist_node *node2;
9113 
9114 	hlist_for_each_entry_safe(filter, node2,
9115 				  &pf->fdir_filter_list, fdir_node) {
9116 		hlist_del(&filter->fdir_node);
9117 		kfree(filter);
9118 	}
9119 
9120 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9121 		list_del(&pit_entry->list);
9122 		kfree(pit_entry);
9123 	}
9124 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9125 
9126 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9127 		list_del(&pit_entry->list);
9128 		kfree(pit_entry);
9129 	}
9130 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9131 
9132 	pf->fdir_pf_active_filters = 0;
9133 	i40e_reset_fdir_filter_cnt(pf);
9134 
9135 	/* Reprogram the default input set for TCP/IPv4 */
9136 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9137 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9138 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9139 
9140 	/* Reprogram the default input set for TCP/IPv6 */
9141 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9142 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9143 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9144 
9145 	/* Reprogram the default input set for UDP/IPv4 */
9146 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9147 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9148 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9149 
9150 	/* Reprogram the default input set for UDP/IPv6 */
9151 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9152 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9153 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9154 
9155 	/* Reprogram the default input set for SCTP/IPv4 */
9156 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9157 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9158 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9159 
9160 	/* Reprogram the default input set for SCTP/IPv6 */
9161 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9162 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9163 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9164 
9165 	/* Reprogram the default input set for Other/IPv4 */
9166 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9167 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9168 
9169 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9170 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9171 
9172 	/* Reprogram the default input set for Other/IPv6 */
9173 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9174 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9175 
9176 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9177 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9178 }
9179 
9180 /**
9181  * i40e_cloud_filter_exit - Cleans up the cloud filters
9182  * @pf: Pointer to PF
9183  *
9184  * This function destroys the hlist where all the cloud filters
9185  * were saved.
9186  **/
9187 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9188 {
9189 	struct i40e_cloud_filter *cfilter;
9190 	struct hlist_node *node;
9191 
9192 	hlist_for_each_entry_safe(cfilter, node,
9193 				  &pf->cloud_filter_list, cloud_node) {
9194 		hlist_del(&cfilter->cloud_node);
9195 		kfree(cfilter);
9196 	}
9197 	pf->num_cloud_filters = 0;
9198 
9199 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
9200 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
9201 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
9202 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
9203 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
9204 	}
9205 }
9206 
9207 /**
9208  * i40e_close - Disables a network interface
9209  * @netdev: network interface device structure
9210  *
9211  * The close entry point is called when an interface is de-activated
9212  * by the OS.  The hardware is still under the driver's control, but
9213  * this netdev interface is disabled.
9214  *
9215  * Returns 0, this is not allowed to fail
9216  **/
9217 int i40e_close(struct net_device *netdev)
9218 {
9219 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9220 	struct i40e_vsi *vsi = np->vsi;
9221 
9222 	i40e_vsi_close(vsi);
9223 
9224 	return 0;
9225 }
9226 
9227 /**
9228  * i40e_do_reset - Start a PF or Core Reset sequence
9229  * @pf: board private structure
9230  * @reset_flags: which reset is requested
9231  * @lock_acquired: indicates whether or not the lock has been acquired
9232  * before this function was called.
9233  *
9234  * The essential difference in resets is that the PF Reset
9235  * doesn't clear the packet buffers, doesn't reset the PE
9236  * firmware, and doesn't bother the other PFs on the chip.
9237  **/
9238 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9239 {
9240 	u32 val;
9241 
9242 	/* do the biggest reset indicated */
9243 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9244 
9245 		/* Request a Global Reset
9246 		 *
9247 		 * This will start the chip's countdown to the actual full
9248 		 * chip reset event, and a warning interrupt to be sent
9249 		 * to all PFs, including the requestor.  Our handler
9250 		 * for the warning interrupt will deal with the shutdown
9251 		 * and recovery of the switch setup.
9252 		 */
9253 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9254 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9255 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9256 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9257 
9258 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9259 
9260 		/* Request a Core Reset
9261 		 *
9262 		 * Same as Global Reset, except does *not* include the MAC/PHY
9263 		 */
9264 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9265 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9266 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9267 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9268 		i40e_flush(&pf->hw);
9269 
9270 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9271 
9272 		/* Request a PF Reset
9273 		 *
9274 		 * Resets only the PF-specific registers
9275 		 *
9276 		 * This goes directly to the tear-down and rebuild of
9277 		 * the switch, since we need to do all the recovery as
9278 		 * for the Core Reset.
9279 		 */
9280 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9281 		i40e_handle_reset_warning(pf, lock_acquired);
9282 
9283 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9284 		/* Request a PF Reset
9285 		 *
9286 		 * Resets PF and reinitializes PFs VSI.
9287 		 */
9288 		i40e_prep_for_reset(pf);
9289 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9290 		dev_info(&pf->pdev->dev,
9291 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9292 			 "FW LLDP is disabled\n" :
9293 			 "FW LLDP is enabled\n");
9294 
9295 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9296 		int v;
9297 
9298 		/* Find the VSI(s) that requested a re-init */
9299 		dev_info(&pf->pdev->dev,
9300 			 "VSI reinit requested\n");
9301 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9302 			struct i40e_vsi *vsi = pf->vsi[v];
9303 
9304 			if (vsi != NULL &&
9305 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9306 					       vsi->state))
9307 				i40e_vsi_reinit_locked(pf->vsi[v]);
9308 		}
9309 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9310 		int v;
9311 
9312 		/* Find the VSI(s) that needs to be brought down */
9313 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9314 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9315 			struct i40e_vsi *vsi = pf->vsi[v];
9316 
9317 			if (vsi != NULL &&
9318 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9319 					       vsi->state)) {
9320 				set_bit(__I40E_VSI_DOWN, vsi->state);
9321 				i40e_down(vsi);
9322 			}
9323 		}
9324 	} else {
9325 		dev_info(&pf->pdev->dev,
9326 			 "bad reset request 0x%08x\n", reset_flags);
9327 	}
9328 }
9329 
9330 #ifdef CONFIG_I40E_DCB
9331 /**
9332  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9333  * @pf: board private structure
9334  * @old_cfg: current DCB config
9335  * @new_cfg: new DCB config
9336  **/
9337 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9338 			    struct i40e_dcbx_config *old_cfg,
9339 			    struct i40e_dcbx_config *new_cfg)
9340 {
9341 	bool need_reconfig = false;
9342 
9343 	/* Check if ETS configuration has changed */
9344 	if (memcmp(&new_cfg->etscfg,
9345 		   &old_cfg->etscfg,
9346 		   sizeof(new_cfg->etscfg))) {
9347 		/* If Priority Table has changed reconfig is needed */
9348 		if (memcmp(&new_cfg->etscfg.prioritytable,
9349 			   &old_cfg->etscfg.prioritytable,
9350 			   sizeof(new_cfg->etscfg.prioritytable))) {
9351 			need_reconfig = true;
9352 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9353 		}
9354 
9355 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9356 			   &old_cfg->etscfg.tcbwtable,
9357 			   sizeof(new_cfg->etscfg.tcbwtable)))
9358 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9359 
9360 		if (memcmp(&new_cfg->etscfg.tsatable,
9361 			   &old_cfg->etscfg.tsatable,
9362 			   sizeof(new_cfg->etscfg.tsatable)))
9363 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9364 	}
9365 
9366 	/* Check if PFC configuration has changed */
9367 	if (memcmp(&new_cfg->pfc,
9368 		   &old_cfg->pfc,
9369 		   sizeof(new_cfg->pfc))) {
9370 		need_reconfig = true;
9371 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9372 	}
9373 
9374 	/* Check if APP Table has changed */
9375 	if (memcmp(&new_cfg->app,
9376 		   &old_cfg->app,
9377 		   sizeof(new_cfg->app))) {
9378 		need_reconfig = true;
9379 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9380 	}
9381 
9382 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9383 	return need_reconfig;
9384 }
9385 
9386 /**
9387  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9388  * @pf: board private structure
9389  * @e: event info posted on ARQ
9390  **/
9391 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9392 				  struct i40e_arq_event_info *e)
9393 {
9394 	struct i40e_aqc_lldp_get_mib *mib =
9395 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9396 	struct i40e_hw *hw = &pf->hw;
9397 	struct i40e_dcbx_config tmp_dcbx_cfg;
9398 	bool need_reconfig = false;
9399 	int ret = 0;
9400 	u8 type;
9401 
9402 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9403 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9404 	    (hw->phy.link_info.link_speed &
9405 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9406 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9407 		/* let firmware decide if the DCB should be disabled */
9408 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9409 
9410 	/* Not DCB capable or capability disabled */
9411 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9412 		return ret;
9413 
9414 	/* Ignore if event is not for Nearest Bridge */
9415 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9416 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9417 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9418 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9419 		return ret;
9420 
9421 	/* Check MIB Type and return if event for Remote MIB update */
9422 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9423 	dev_dbg(&pf->pdev->dev,
9424 		"LLDP event mib type %s\n", type ? "remote" : "local");
9425 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9426 		/* Update the remote cached instance and return */
9427 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9428 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9429 				&hw->remote_dcbx_config);
9430 		goto exit;
9431 	}
9432 
9433 	/* Store the old configuration */
9434 	tmp_dcbx_cfg = hw->local_dcbx_config;
9435 
9436 	/* Reset the old DCBx configuration data */
9437 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9438 	/* Get updated DCBX data from firmware */
9439 	ret = i40e_get_dcb_config(&pf->hw);
9440 	if (ret) {
9441 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9442 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9443 		    (hw->phy.link_info.link_speed &
9444 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9445 			dev_warn(&pf->pdev->dev,
9446 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9447 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9448 		} else {
9449 			dev_info(&pf->pdev->dev,
9450 				 "Failed querying DCB configuration data from firmware, err %pe aq_err %s\n",
9451 				 ERR_PTR(ret),
9452 				 i40e_aq_str(&pf->hw,
9453 					     pf->hw.aq.asq_last_status));
9454 		}
9455 		goto exit;
9456 	}
9457 
9458 	/* No change detected in DCBX configs */
9459 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9460 		    sizeof(tmp_dcbx_cfg))) {
9461 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9462 		goto exit;
9463 	}
9464 
9465 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9466 					       &hw->local_dcbx_config);
9467 
9468 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9469 
9470 	if (!need_reconfig)
9471 		goto exit;
9472 
9473 	/* Enable DCB tagging only when more than one TC */
9474 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9475 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9476 	else
9477 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9478 
9479 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9480 	/* Reconfiguration needed quiesce all VSIs */
9481 	i40e_pf_quiesce_all_vsi(pf);
9482 
9483 	/* Changes in configuration update VEB/VSI */
9484 	i40e_dcb_reconfigure(pf);
9485 
9486 	ret = i40e_resume_port_tx(pf);
9487 
9488 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9489 	/* In case of error no point in resuming VSIs */
9490 	if (ret)
9491 		goto exit;
9492 
9493 	/* Wait for the PF's queues to be disabled */
9494 	ret = i40e_pf_wait_queues_disabled(pf);
9495 	if (ret) {
9496 		/* Schedule PF reset to recover */
9497 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9498 		i40e_service_event_schedule(pf);
9499 	} else {
9500 		i40e_pf_unquiesce_all_vsi(pf);
9501 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9502 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9503 	}
9504 
9505 exit:
9506 	return ret;
9507 }
9508 #endif /* CONFIG_I40E_DCB */
9509 
9510 /**
9511  * i40e_do_reset_safe - Protected reset path for userland calls.
9512  * @pf: board private structure
9513  * @reset_flags: which reset is requested
9514  *
9515  **/
9516 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9517 {
9518 	rtnl_lock();
9519 	i40e_do_reset(pf, reset_flags, true);
9520 	rtnl_unlock();
9521 }
9522 
9523 /**
9524  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9525  * @pf: board private structure
9526  * @e: event info posted on ARQ
9527  *
9528  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9529  * and VF queues
9530  **/
9531 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9532 					   struct i40e_arq_event_info *e)
9533 {
9534 	struct i40e_aqc_lan_overflow *data =
9535 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9536 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9537 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9538 	struct i40e_hw *hw = &pf->hw;
9539 	struct i40e_vf *vf;
9540 	u16 vf_id;
9541 
9542 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9543 		queue, qtx_ctl);
9544 
9545 	/* Queue belongs to VF, find the VF and issue VF reset */
9546 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9547 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9548 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9549 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9550 		vf_id -= hw->func_caps.vf_base_id;
9551 		vf = &pf->vf[vf_id];
9552 		i40e_vc_notify_vf_reset(vf);
9553 		/* Allow VF to process pending reset notification */
9554 		msleep(20);
9555 		i40e_reset_vf(vf, false);
9556 	}
9557 }
9558 
9559 /**
9560  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9561  * @pf: board private structure
9562  **/
9563 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9564 {
9565 	u32 val, fcnt_prog;
9566 
9567 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9568 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9569 	return fcnt_prog;
9570 }
9571 
9572 /**
9573  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9574  * @pf: board private structure
9575  **/
9576 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9577 {
9578 	u32 val, fcnt_prog;
9579 
9580 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9581 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9582 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9583 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9584 	return fcnt_prog;
9585 }
9586 
9587 /**
9588  * i40e_get_global_fd_count - Get total FD filters programmed on device
9589  * @pf: board private structure
9590  **/
9591 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9592 {
9593 	u32 val, fcnt_prog;
9594 
9595 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9596 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9597 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9598 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9599 	return fcnt_prog;
9600 }
9601 
9602 /**
9603  * i40e_reenable_fdir_sb - Restore FDir SB capability
9604  * @pf: board private structure
9605  **/
9606 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9607 {
9608 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9609 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9610 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9611 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9612 }
9613 
9614 /**
9615  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9616  * @pf: board private structure
9617  **/
9618 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9619 {
9620 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9621 		/* ATR uses the same filtering logic as SB rules. It only
9622 		 * functions properly if the input set mask is at the default
9623 		 * settings. It is safe to restore the default input set
9624 		 * because there are no active TCPv4 filter rules.
9625 		 */
9626 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9627 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9628 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9629 
9630 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9631 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9632 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9633 	}
9634 }
9635 
9636 /**
9637  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9638  * @pf: board private structure
9639  * @filter: FDir filter to remove
9640  */
9641 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9642 				       struct i40e_fdir_filter *filter)
9643 {
9644 	/* Update counters */
9645 	pf->fdir_pf_active_filters--;
9646 	pf->fd_inv = 0;
9647 
9648 	switch (filter->flow_type) {
9649 	case TCP_V4_FLOW:
9650 		pf->fd_tcp4_filter_cnt--;
9651 		break;
9652 	case UDP_V4_FLOW:
9653 		pf->fd_udp4_filter_cnt--;
9654 		break;
9655 	case SCTP_V4_FLOW:
9656 		pf->fd_sctp4_filter_cnt--;
9657 		break;
9658 	case TCP_V6_FLOW:
9659 		pf->fd_tcp6_filter_cnt--;
9660 		break;
9661 	case UDP_V6_FLOW:
9662 		pf->fd_udp6_filter_cnt--;
9663 		break;
9664 	case SCTP_V6_FLOW:
9665 		pf->fd_udp6_filter_cnt--;
9666 		break;
9667 	case IP_USER_FLOW:
9668 		switch (filter->ipl4_proto) {
9669 		case IPPROTO_TCP:
9670 			pf->fd_tcp4_filter_cnt--;
9671 			break;
9672 		case IPPROTO_UDP:
9673 			pf->fd_udp4_filter_cnt--;
9674 			break;
9675 		case IPPROTO_SCTP:
9676 			pf->fd_sctp4_filter_cnt--;
9677 			break;
9678 		case IPPROTO_IP:
9679 			pf->fd_ip4_filter_cnt--;
9680 			break;
9681 		}
9682 		break;
9683 	case IPV6_USER_FLOW:
9684 		switch (filter->ipl4_proto) {
9685 		case IPPROTO_TCP:
9686 			pf->fd_tcp6_filter_cnt--;
9687 			break;
9688 		case IPPROTO_UDP:
9689 			pf->fd_udp6_filter_cnt--;
9690 			break;
9691 		case IPPROTO_SCTP:
9692 			pf->fd_sctp6_filter_cnt--;
9693 			break;
9694 		case IPPROTO_IP:
9695 			pf->fd_ip6_filter_cnt--;
9696 			break;
9697 		}
9698 		break;
9699 	}
9700 
9701 	/* Remove the filter from the list and free memory */
9702 	hlist_del(&filter->fdir_node);
9703 	kfree(filter);
9704 }
9705 
9706 /**
9707  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9708  * @pf: board private structure
9709  **/
9710 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9711 {
9712 	struct i40e_fdir_filter *filter;
9713 	u32 fcnt_prog, fcnt_avail;
9714 	struct hlist_node *node;
9715 
9716 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9717 		return;
9718 
9719 	/* Check if we have enough room to re-enable FDir SB capability. */
9720 	fcnt_prog = i40e_get_global_fd_count(pf);
9721 	fcnt_avail = pf->fdir_pf_filter_count;
9722 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9723 	    (pf->fd_add_err == 0) ||
9724 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9725 		i40e_reenable_fdir_sb(pf);
9726 
9727 	/* We should wait for even more space before re-enabling ATR.
9728 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9729 	 * rules active.
9730 	 */
9731 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9732 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9733 		i40e_reenable_fdir_atr(pf);
9734 
9735 	/* if hw had a problem adding a filter, delete it */
9736 	if (pf->fd_inv > 0) {
9737 		hlist_for_each_entry_safe(filter, node,
9738 					  &pf->fdir_filter_list, fdir_node)
9739 			if (filter->fd_id == pf->fd_inv)
9740 				i40e_delete_invalid_filter(pf, filter);
9741 	}
9742 }
9743 
9744 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9745 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9746 /**
9747  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9748  * @pf: board private structure
9749  **/
9750 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9751 {
9752 	unsigned long min_flush_time;
9753 	int flush_wait_retry = 50;
9754 	bool disable_atr = false;
9755 	int fd_room;
9756 	int reg;
9757 
9758 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9759 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9760 		return;
9761 
9762 	/* If the flush is happening too quick and we have mostly SB rules we
9763 	 * should not re-enable ATR for some time.
9764 	 */
9765 	min_flush_time = pf->fd_flush_timestamp +
9766 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9767 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9768 
9769 	if (!(time_after(jiffies, min_flush_time)) &&
9770 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9771 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9772 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9773 		disable_atr = true;
9774 	}
9775 
9776 	pf->fd_flush_timestamp = jiffies;
9777 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9778 	/* flush all filters */
9779 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9780 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9781 	i40e_flush(&pf->hw);
9782 	pf->fd_flush_cnt++;
9783 	pf->fd_add_err = 0;
9784 	do {
9785 		/* Check FD flush status every 5-6msec */
9786 		usleep_range(5000, 6000);
9787 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9788 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9789 			break;
9790 	} while (flush_wait_retry--);
9791 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9792 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9793 	} else {
9794 		/* replay sideband filters */
9795 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9796 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9797 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9798 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9799 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9800 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9801 	}
9802 }
9803 
9804 /**
9805  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9806  * @pf: board private structure
9807  **/
9808 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9809 {
9810 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9811 }
9812 
9813 /**
9814  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9815  * @pf: board private structure
9816  **/
9817 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9818 {
9819 
9820 	/* if interface is down do nothing */
9821 	if (test_bit(__I40E_DOWN, pf->state))
9822 		return;
9823 
9824 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9825 		i40e_fdir_flush_and_replay(pf);
9826 
9827 	i40e_fdir_check_and_reenable(pf);
9828 
9829 }
9830 
9831 /**
9832  * i40e_vsi_link_event - notify VSI of a link event
9833  * @vsi: vsi to be notified
9834  * @link_up: link up or down
9835  **/
9836 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9837 {
9838 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9839 		return;
9840 
9841 	switch (vsi->type) {
9842 	case I40E_VSI_MAIN:
9843 		if (!vsi->netdev || !vsi->netdev_registered)
9844 			break;
9845 
9846 		if (link_up) {
9847 			netif_carrier_on(vsi->netdev);
9848 			netif_tx_wake_all_queues(vsi->netdev);
9849 		} else {
9850 			netif_carrier_off(vsi->netdev);
9851 			netif_tx_stop_all_queues(vsi->netdev);
9852 		}
9853 		break;
9854 
9855 	case I40E_VSI_SRIOV:
9856 	case I40E_VSI_VMDQ2:
9857 	case I40E_VSI_CTRL:
9858 	case I40E_VSI_IWARP:
9859 	case I40E_VSI_MIRROR:
9860 	default:
9861 		/* there is no notification for other VSIs */
9862 		break;
9863 	}
9864 }
9865 
9866 /**
9867  * i40e_veb_link_event - notify elements on the veb of a link event
9868  * @veb: veb to be notified
9869  * @link_up: link up or down
9870  **/
9871 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9872 {
9873 	struct i40e_pf *pf;
9874 	int i;
9875 
9876 	if (!veb || !veb->pf)
9877 		return;
9878 	pf = veb->pf;
9879 
9880 	/* depth first... */
9881 	for (i = 0; i < I40E_MAX_VEB; i++)
9882 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9883 			i40e_veb_link_event(pf->veb[i], link_up);
9884 
9885 	/* ... now the local VSIs */
9886 	for (i = 0; i < pf->num_alloc_vsi; i++)
9887 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9888 			i40e_vsi_link_event(pf->vsi[i], link_up);
9889 }
9890 
9891 /**
9892  * i40e_link_event - Update netif_carrier status
9893  * @pf: board private structure
9894  **/
9895 static void i40e_link_event(struct i40e_pf *pf)
9896 {
9897 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9898 	u8 new_link_speed, old_link_speed;
9899 	bool new_link, old_link;
9900 	int status;
9901 #ifdef CONFIG_I40E_DCB
9902 	int err;
9903 #endif /* CONFIG_I40E_DCB */
9904 
9905 	/* set this to force the get_link_status call to refresh state */
9906 	pf->hw.phy.get_link_info = true;
9907 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9908 	status = i40e_get_link_status(&pf->hw, &new_link);
9909 
9910 	/* On success, disable temp link polling */
9911 	if (status == 0) {
9912 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9913 	} else {
9914 		/* Enable link polling temporarily until i40e_get_link_status
9915 		 * returns 0
9916 		 */
9917 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9918 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9919 			status);
9920 		return;
9921 	}
9922 
9923 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9924 	new_link_speed = pf->hw.phy.link_info.link_speed;
9925 
9926 	if (new_link == old_link &&
9927 	    new_link_speed == old_link_speed &&
9928 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9929 	     new_link == netif_carrier_ok(vsi->netdev)))
9930 		return;
9931 
9932 	i40e_print_link_message(vsi, new_link);
9933 
9934 	/* Notify the base of the switch tree connected to
9935 	 * the link.  Floating VEBs are not notified.
9936 	 */
9937 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9938 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9939 	else
9940 		i40e_vsi_link_event(vsi, new_link);
9941 
9942 	if (pf->vf)
9943 		i40e_vc_notify_link_state(pf);
9944 
9945 	if (pf->flags & I40E_FLAG_PTP)
9946 		i40e_ptp_set_increment(pf);
9947 #ifdef CONFIG_I40E_DCB
9948 	if (new_link == old_link)
9949 		return;
9950 	/* Not SW DCB so firmware will take care of default settings */
9951 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9952 		return;
9953 
9954 	/* We cover here only link down, as after link up in case of SW DCB
9955 	 * SW LLDP agent will take care of setting it up
9956 	 */
9957 	if (!new_link) {
9958 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9959 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9960 		err = i40e_dcb_sw_default_config(pf);
9961 		if (err) {
9962 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9963 				       I40E_FLAG_DCB_ENABLED);
9964 		} else {
9965 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9966 				       DCB_CAP_DCBX_VER_IEEE;
9967 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9968 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9969 		}
9970 	}
9971 #endif /* CONFIG_I40E_DCB */
9972 }
9973 
9974 /**
9975  * i40e_watchdog_subtask - periodic checks not using event driven response
9976  * @pf: board private structure
9977  **/
9978 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9979 {
9980 	int i;
9981 
9982 	/* if interface is down do nothing */
9983 	if (test_bit(__I40E_DOWN, pf->state) ||
9984 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9985 		return;
9986 
9987 	/* make sure we don't do these things too often */
9988 	if (time_before(jiffies, (pf->service_timer_previous +
9989 				  pf->service_timer_period)))
9990 		return;
9991 	pf->service_timer_previous = jiffies;
9992 
9993 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9994 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9995 		i40e_link_event(pf);
9996 
9997 	/* Update the stats for active netdevs so the network stack
9998 	 * can look at updated numbers whenever it cares to
9999 	 */
10000 	for (i = 0; i < pf->num_alloc_vsi; i++)
10001 		if (pf->vsi[i] && pf->vsi[i]->netdev)
10002 			i40e_update_stats(pf->vsi[i]);
10003 
10004 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
10005 		/* Update the stats for the active switching components */
10006 		for (i = 0; i < I40E_MAX_VEB; i++)
10007 			if (pf->veb[i])
10008 				i40e_update_veb_stats(pf->veb[i]);
10009 	}
10010 
10011 	i40e_ptp_rx_hang(pf);
10012 	i40e_ptp_tx_hang(pf);
10013 }
10014 
10015 /**
10016  * i40e_reset_subtask - Set up for resetting the device and driver
10017  * @pf: board private structure
10018  **/
10019 static void i40e_reset_subtask(struct i40e_pf *pf)
10020 {
10021 	u32 reset_flags = 0;
10022 
10023 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10024 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10025 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10026 	}
10027 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10028 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10029 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10030 	}
10031 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10032 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10033 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10034 	}
10035 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10036 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10037 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10038 	}
10039 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10040 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10041 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10042 	}
10043 
10044 	/* If there's a recovery already waiting, it takes
10045 	 * precedence before starting a new reset sequence.
10046 	 */
10047 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10048 		i40e_prep_for_reset(pf);
10049 		i40e_reset(pf);
10050 		i40e_rebuild(pf, false, false);
10051 	}
10052 
10053 	/* If we're already down or resetting, just bail */
10054 	if (reset_flags &&
10055 	    !test_bit(__I40E_DOWN, pf->state) &&
10056 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10057 		i40e_do_reset(pf, reset_flags, false);
10058 	}
10059 }
10060 
10061 /**
10062  * i40e_handle_link_event - Handle link event
10063  * @pf: board private structure
10064  * @e: event info posted on ARQ
10065  **/
10066 static void i40e_handle_link_event(struct i40e_pf *pf,
10067 				   struct i40e_arq_event_info *e)
10068 {
10069 	struct i40e_aqc_get_link_status *status =
10070 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
10071 
10072 	/* Do a new status request to re-enable LSE reporting
10073 	 * and load new status information into the hw struct
10074 	 * This completely ignores any state information
10075 	 * in the ARQ event info, instead choosing to always
10076 	 * issue the AQ update link status command.
10077 	 */
10078 	i40e_link_event(pf);
10079 
10080 	/* Check if module meets thermal requirements */
10081 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10082 		dev_err(&pf->pdev->dev,
10083 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10084 		dev_err(&pf->pdev->dev,
10085 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10086 	} else {
10087 		/* check for unqualified module, if link is down, suppress
10088 		 * the message if link was forced to be down.
10089 		 */
10090 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10091 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10092 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10093 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
10094 			dev_err(&pf->pdev->dev,
10095 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10096 			dev_err(&pf->pdev->dev,
10097 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10098 		}
10099 	}
10100 }
10101 
10102 /**
10103  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10104  * @pf: board private structure
10105  **/
10106 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10107 {
10108 	struct i40e_arq_event_info event;
10109 	struct i40e_hw *hw = &pf->hw;
10110 	u16 pending, i = 0;
10111 	u16 opcode;
10112 	u32 oldval;
10113 	int ret;
10114 	u32 val;
10115 
10116 	/* Do not run clean AQ when PF reset fails */
10117 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10118 		return;
10119 
10120 	/* check for error indications */
10121 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
10122 	oldval = val;
10123 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10124 		if (hw->debug_mask & I40E_DEBUG_AQ)
10125 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10126 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10127 	}
10128 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10129 		if (hw->debug_mask & I40E_DEBUG_AQ)
10130 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10131 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10132 		pf->arq_overflows++;
10133 	}
10134 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10135 		if (hw->debug_mask & I40E_DEBUG_AQ)
10136 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10137 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10138 	}
10139 	if (oldval != val)
10140 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
10141 
10142 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
10143 	oldval = val;
10144 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10145 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10146 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10147 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10148 	}
10149 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10150 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10151 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10152 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10153 	}
10154 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10155 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10156 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10157 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10158 	}
10159 	if (oldval != val)
10160 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
10161 
10162 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10163 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10164 	if (!event.msg_buf)
10165 		return;
10166 
10167 	do {
10168 		ret = i40e_clean_arq_element(hw, &event, &pending);
10169 		if (ret == -EALREADY)
10170 			break;
10171 		else if (ret) {
10172 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10173 			break;
10174 		}
10175 
10176 		opcode = le16_to_cpu(event.desc.opcode);
10177 		switch (opcode) {
10178 
10179 		case i40e_aqc_opc_get_link_status:
10180 			rtnl_lock();
10181 			i40e_handle_link_event(pf, &event);
10182 			rtnl_unlock();
10183 			break;
10184 		case i40e_aqc_opc_send_msg_to_pf:
10185 			ret = i40e_vc_process_vf_msg(pf,
10186 					le16_to_cpu(event.desc.retval),
10187 					le32_to_cpu(event.desc.cookie_high),
10188 					le32_to_cpu(event.desc.cookie_low),
10189 					event.msg_buf,
10190 					event.msg_len);
10191 			break;
10192 		case i40e_aqc_opc_lldp_update_mib:
10193 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10194 #ifdef CONFIG_I40E_DCB
10195 			rtnl_lock();
10196 			i40e_handle_lldp_event(pf, &event);
10197 			rtnl_unlock();
10198 #endif /* CONFIG_I40E_DCB */
10199 			break;
10200 		case i40e_aqc_opc_event_lan_overflow:
10201 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10202 			i40e_handle_lan_overflow_event(pf, &event);
10203 			break;
10204 		case i40e_aqc_opc_send_msg_to_peer:
10205 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10206 			break;
10207 		case i40e_aqc_opc_nvm_erase:
10208 		case i40e_aqc_opc_nvm_update:
10209 		case i40e_aqc_opc_oem_post_update:
10210 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10211 				   "ARQ NVM operation 0x%04x completed\n",
10212 				   opcode);
10213 			break;
10214 		default:
10215 			dev_info(&pf->pdev->dev,
10216 				 "ARQ: Unknown event 0x%04x ignored\n",
10217 				 opcode);
10218 			break;
10219 		}
10220 	} while (i++ < pf->adminq_work_limit);
10221 
10222 	if (i < pf->adminq_work_limit)
10223 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10224 
10225 	/* re-enable Admin queue interrupt cause */
10226 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10227 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10228 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10229 	i40e_flush(hw);
10230 
10231 	kfree(event.msg_buf);
10232 }
10233 
10234 /**
10235  * i40e_verify_eeprom - make sure eeprom is good to use
10236  * @pf: board private structure
10237  **/
10238 static void i40e_verify_eeprom(struct i40e_pf *pf)
10239 {
10240 	int err;
10241 
10242 	err = i40e_diag_eeprom_test(&pf->hw);
10243 	if (err) {
10244 		/* retry in case of garbage read */
10245 		err = i40e_diag_eeprom_test(&pf->hw);
10246 		if (err) {
10247 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10248 				 err);
10249 			set_bit(__I40E_BAD_EEPROM, pf->state);
10250 		}
10251 	}
10252 
10253 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10254 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10255 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10256 	}
10257 }
10258 
10259 /**
10260  * i40e_enable_pf_switch_lb
10261  * @pf: pointer to the PF structure
10262  *
10263  * enable switch loop back or die - no point in a return value
10264  **/
10265 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10266 {
10267 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10268 	struct i40e_vsi_context ctxt;
10269 	int ret;
10270 
10271 	ctxt.seid = pf->main_vsi_seid;
10272 	ctxt.pf_num = pf->hw.pf_id;
10273 	ctxt.vf_num = 0;
10274 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10275 	if (ret) {
10276 		dev_info(&pf->pdev->dev,
10277 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10278 			 ERR_PTR(ret),
10279 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10280 		return;
10281 	}
10282 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10283 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10284 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10285 
10286 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10287 	if (ret) {
10288 		dev_info(&pf->pdev->dev,
10289 			 "update vsi switch failed, err %pe aq_err %s\n",
10290 			 ERR_PTR(ret),
10291 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10292 	}
10293 }
10294 
10295 /**
10296  * i40e_disable_pf_switch_lb
10297  * @pf: pointer to the PF structure
10298  *
10299  * disable switch loop back or die - no point in a return value
10300  **/
10301 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10302 {
10303 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10304 	struct i40e_vsi_context ctxt;
10305 	int ret;
10306 
10307 	ctxt.seid = pf->main_vsi_seid;
10308 	ctxt.pf_num = pf->hw.pf_id;
10309 	ctxt.vf_num = 0;
10310 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10311 	if (ret) {
10312 		dev_info(&pf->pdev->dev,
10313 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10314 			 ERR_PTR(ret),
10315 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10316 		return;
10317 	}
10318 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10319 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10320 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10321 
10322 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10323 	if (ret) {
10324 		dev_info(&pf->pdev->dev,
10325 			 "update vsi switch failed, err %pe aq_err %s\n",
10326 			 ERR_PTR(ret),
10327 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10328 	}
10329 }
10330 
10331 /**
10332  * i40e_config_bridge_mode - Configure the HW bridge mode
10333  * @veb: pointer to the bridge instance
10334  *
10335  * Configure the loop back mode for the LAN VSI that is downlink to the
10336  * specified HW bridge instance. It is expected this function is called
10337  * when a new HW bridge is instantiated.
10338  **/
10339 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10340 {
10341 	struct i40e_pf *pf = veb->pf;
10342 
10343 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10344 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10345 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10346 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10347 		i40e_disable_pf_switch_lb(pf);
10348 	else
10349 		i40e_enable_pf_switch_lb(pf);
10350 }
10351 
10352 /**
10353  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10354  * @veb: pointer to the VEB instance
10355  *
10356  * This is a recursive function that first builds the attached VSIs then
10357  * recurses in to build the next layer of VEB.  We track the connections
10358  * through our own index numbers because the seid's from the HW could
10359  * change across the reset.
10360  **/
10361 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10362 {
10363 	struct i40e_vsi *ctl_vsi = NULL;
10364 	struct i40e_pf *pf = veb->pf;
10365 	int v, veb_idx;
10366 	int ret;
10367 
10368 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10369 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10370 		if (pf->vsi[v] &&
10371 		    pf->vsi[v]->veb_idx == veb->idx &&
10372 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10373 			ctl_vsi = pf->vsi[v];
10374 			break;
10375 		}
10376 	}
10377 	if (!ctl_vsi) {
10378 		dev_info(&pf->pdev->dev,
10379 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10380 		ret = -ENOENT;
10381 		goto end_reconstitute;
10382 	}
10383 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10384 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10385 	ret = i40e_add_vsi(ctl_vsi);
10386 	if (ret) {
10387 		dev_info(&pf->pdev->dev,
10388 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10389 			 veb->idx, ret);
10390 		goto end_reconstitute;
10391 	}
10392 	i40e_vsi_reset_stats(ctl_vsi);
10393 
10394 	/* create the VEB in the switch and move the VSI onto the VEB */
10395 	ret = i40e_add_veb(veb, ctl_vsi);
10396 	if (ret)
10397 		goto end_reconstitute;
10398 
10399 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10400 		veb->bridge_mode = BRIDGE_MODE_VEB;
10401 	else
10402 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10403 	i40e_config_bridge_mode(veb);
10404 
10405 	/* create the remaining VSIs attached to this VEB */
10406 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10407 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10408 			continue;
10409 
10410 		if (pf->vsi[v]->veb_idx == veb->idx) {
10411 			struct i40e_vsi *vsi = pf->vsi[v];
10412 
10413 			vsi->uplink_seid = veb->seid;
10414 			ret = i40e_add_vsi(vsi);
10415 			if (ret) {
10416 				dev_info(&pf->pdev->dev,
10417 					 "rebuild of vsi_idx %d failed: %d\n",
10418 					 v, ret);
10419 				goto end_reconstitute;
10420 			}
10421 			i40e_vsi_reset_stats(vsi);
10422 		}
10423 	}
10424 
10425 	/* create any VEBs attached to this VEB - RECURSION */
10426 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10427 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10428 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10429 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10430 			if (ret)
10431 				break;
10432 		}
10433 	}
10434 
10435 end_reconstitute:
10436 	return ret;
10437 }
10438 
10439 /**
10440  * i40e_get_capabilities - get info about the HW
10441  * @pf: the PF struct
10442  * @list_type: AQ capability to be queried
10443  **/
10444 static int i40e_get_capabilities(struct i40e_pf *pf,
10445 				 enum i40e_admin_queue_opc list_type)
10446 {
10447 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10448 	u16 data_size;
10449 	int buf_len;
10450 	int err;
10451 
10452 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10453 	do {
10454 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10455 		if (!cap_buf)
10456 			return -ENOMEM;
10457 
10458 		/* this loads the data into the hw struct for us */
10459 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10460 						    &data_size, list_type,
10461 						    NULL);
10462 		/* data loaded, buffer no longer needed */
10463 		kfree(cap_buf);
10464 
10465 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10466 			/* retry with a larger buffer */
10467 			buf_len = data_size;
10468 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10469 			dev_info(&pf->pdev->dev,
10470 				 "capability discovery failed, err %pe aq_err %s\n",
10471 				 ERR_PTR(err),
10472 				 i40e_aq_str(&pf->hw,
10473 					     pf->hw.aq.asq_last_status));
10474 			return -ENODEV;
10475 		}
10476 	} while (err);
10477 
10478 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10479 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10480 			dev_info(&pf->pdev->dev,
10481 				 "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",
10482 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10483 				 pf->hw.func_caps.num_msix_vectors,
10484 				 pf->hw.func_caps.num_msix_vectors_vf,
10485 				 pf->hw.func_caps.fd_filters_guaranteed,
10486 				 pf->hw.func_caps.fd_filters_best_effort,
10487 				 pf->hw.func_caps.num_tx_qp,
10488 				 pf->hw.func_caps.num_vsis);
10489 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10490 			dev_info(&pf->pdev->dev,
10491 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10492 				 pf->hw.dev_caps.switch_mode,
10493 				 pf->hw.dev_caps.valid_functions);
10494 			dev_info(&pf->pdev->dev,
10495 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10496 				 pf->hw.dev_caps.sr_iov_1_1,
10497 				 pf->hw.dev_caps.num_vfs);
10498 			dev_info(&pf->pdev->dev,
10499 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10500 				 pf->hw.dev_caps.num_vsis,
10501 				 pf->hw.dev_caps.num_rx_qp,
10502 				 pf->hw.dev_caps.num_tx_qp);
10503 		}
10504 	}
10505 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10506 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10507 		       + pf->hw.func_caps.num_vfs)
10508 		if (pf->hw.revision_id == 0 &&
10509 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10510 			dev_info(&pf->pdev->dev,
10511 				 "got num_vsis %d, setting num_vsis to %d\n",
10512 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10513 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10514 		}
10515 	}
10516 	return 0;
10517 }
10518 
10519 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10520 
10521 /**
10522  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10523  * @pf: board private structure
10524  **/
10525 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10526 {
10527 	struct i40e_vsi *vsi;
10528 
10529 	/* quick workaround for an NVM issue that leaves a critical register
10530 	 * uninitialized
10531 	 */
10532 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10533 		static const u32 hkey[] = {
10534 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10535 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10536 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10537 			0x95b3a76d};
10538 		int i;
10539 
10540 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10541 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10542 	}
10543 
10544 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10545 		return;
10546 
10547 	/* find existing VSI and see if it needs configuring */
10548 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10549 
10550 	/* create a new VSI if none exists */
10551 	if (!vsi) {
10552 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10553 				     pf->vsi[pf->lan_vsi]->seid, 0);
10554 		if (!vsi) {
10555 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10556 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10557 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10558 			return;
10559 		}
10560 	}
10561 
10562 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10563 }
10564 
10565 /**
10566  * i40e_fdir_teardown - release the Flow Director resources
10567  * @pf: board private structure
10568  **/
10569 static void i40e_fdir_teardown(struct i40e_pf *pf)
10570 {
10571 	struct i40e_vsi *vsi;
10572 
10573 	i40e_fdir_filter_exit(pf);
10574 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10575 	if (vsi)
10576 		i40e_vsi_release(vsi);
10577 }
10578 
10579 /**
10580  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10581  * @vsi: PF main vsi
10582  * @seid: seid of main or channel VSIs
10583  *
10584  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10585  * existed before reset
10586  **/
10587 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10588 {
10589 	struct i40e_cloud_filter *cfilter;
10590 	struct i40e_pf *pf = vsi->back;
10591 	struct hlist_node *node;
10592 	int ret;
10593 
10594 	/* Add cloud filters back if they exist */
10595 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10596 				  cloud_node) {
10597 		if (cfilter->seid != seid)
10598 			continue;
10599 
10600 		if (cfilter->dst_port)
10601 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10602 								true);
10603 		else
10604 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10605 
10606 		if (ret) {
10607 			dev_dbg(&pf->pdev->dev,
10608 				"Failed to rebuild cloud filter, err %pe aq_err %s\n",
10609 				ERR_PTR(ret),
10610 				i40e_aq_str(&pf->hw,
10611 					    pf->hw.aq.asq_last_status));
10612 			return ret;
10613 		}
10614 	}
10615 	return 0;
10616 }
10617 
10618 /**
10619  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10620  * @vsi: PF main vsi
10621  *
10622  * Rebuilds channel VSIs if they existed before reset
10623  **/
10624 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10625 {
10626 	struct i40e_channel *ch, *ch_tmp;
10627 	int ret;
10628 
10629 	if (list_empty(&vsi->ch_list))
10630 		return 0;
10631 
10632 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10633 		if (!ch->initialized)
10634 			break;
10635 		/* Proceed with creation of channel (VMDq2) VSI */
10636 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10637 		if (ret) {
10638 			dev_info(&vsi->back->pdev->dev,
10639 				 "failed to rebuild channels using uplink_seid %u\n",
10640 				 vsi->uplink_seid);
10641 			return ret;
10642 		}
10643 		/* Reconfigure TX queues using QTX_CTL register */
10644 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10645 		if (ret) {
10646 			dev_info(&vsi->back->pdev->dev,
10647 				 "failed to configure TX rings for channel %u\n",
10648 				 ch->seid);
10649 			return ret;
10650 		}
10651 		/* update 'next_base_queue' */
10652 		vsi->next_base_queue = vsi->next_base_queue +
10653 							ch->num_queue_pairs;
10654 		if (ch->max_tx_rate) {
10655 			u64 credits = ch->max_tx_rate;
10656 
10657 			if (i40e_set_bw_limit(vsi, ch->seid,
10658 					      ch->max_tx_rate))
10659 				return -EINVAL;
10660 
10661 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10662 			dev_dbg(&vsi->back->pdev->dev,
10663 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10664 				ch->max_tx_rate,
10665 				credits,
10666 				ch->seid);
10667 		}
10668 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10669 		if (ret) {
10670 			dev_dbg(&vsi->back->pdev->dev,
10671 				"Failed to rebuild cloud filters for channel VSI %u\n",
10672 				ch->seid);
10673 			return ret;
10674 		}
10675 	}
10676 	return 0;
10677 }
10678 
10679 /**
10680  * i40e_clean_xps_state - clean xps state for every tx_ring
10681  * @vsi: ptr to the VSI
10682  **/
10683 static void i40e_clean_xps_state(struct i40e_vsi *vsi)
10684 {
10685 	int i;
10686 
10687 	if (vsi->tx_rings)
10688 		for (i = 0; i < vsi->num_queue_pairs; i++)
10689 			if (vsi->tx_rings[i])
10690 				clear_bit(__I40E_TX_XPS_INIT_DONE,
10691 					  vsi->tx_rings[i]->state);
10692 }
10693 
10694 /**
10695  * i40e_prep_for_reset - prep for the core to reset
10696  * @pf: board private structure
10697  *
10698  * Close up the VFs and other things in prep for PF Reset.
10699   **/
10700 static void i40e_prep_for_reset(struct i40e_pf *pf)
10701 {
10702 	struct i40e_hw *hw = &pf->hw;
10703 	int ret = 0;
10704 	u32 v;
10705 
10706 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10707 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10708 		return;
10709 	if (i40e_check_asq_alive(&pf->hw))
10710 		i40e_vc_notify_reset(pf);
10711 
10712 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10713 
10714 	/* quiesce the VSIs and their queues that are not already DOWN */
10715 	i40e_pf_quiesce_all_vsi(pf);
10716 
10717 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10718 		if (pf->vsi[v]) {
10719 			i40e_clean_xps_state(pf->vsi[v]);
10720 			pf->vsi[v]->seid = 0;
10721 		}
10722 	}
10723 
10724 	i40e_shutdown_adminq(&pf->hw);
10725 
10726 	/* call shutdown HMC */
10727 	if (hw->hmc.hmc_obj) {
10728 		ret = i40e_shutdown_lan_hmc(hw);
10729 		if (ret)
10730 			dev_warn(&pf->pdev->dev,
10731 				 "shutdown_lan_hmc failed: %d\n", ret);
10732 	}
10733 
10734 	/* Save the current PTP time so that we can restore the time after the
10735 	 * reset completes.
10736 	 */
10737 	i40e_ptp_save_hw_time(pf);
10738 }
10739 
10740 /**
10741  * i40e_send_version - update firmware with driver version
10742  * @pf: PF struct
10743  */
10744 static void i40e_send_version(struct i40e_pf *pf)
10745 {
10746 	struct i40e_driver_version dv;
10747 
10748 	dv.major_version = 0xff;
10749 	dv.minor_version = 0xff;
10750 	dv.build_version = 0xff;
10751 	dv.subbuild_version = 0;
10752 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10753 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10754 }
10755 
10756 /**
10757  * i40e_get_oem_version - get OEM specific version information
10758  * @hw: pointer to the hardware structure
10759  **/
10760 static void i40e_get_oem_version(struct i40e_hw *hw)
10761 {
10762 	u16 block_offset = 0xffff;
10763 	u16 block_length = 0;
10764 	u16 capabilities = 0;
10765 	u16 gen_snap = 0;
10766 	u16 release = 0;
10767 
10768 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10769 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10770 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10771 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10772 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10773 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10774 #define I40E_NVM_OEM_LENGTH			3
10775 
10776 	/* Check if pointer to OEM version block is valid. */
10777 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10778 	if (block_offset == 0xffff)
10779 		return;
10780 
10781 	/* Check if OEM version block has correct length. */
10782 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10783 			   &block_length);
10784 	if (block_length < I40E_NVM_OEM_LENGTH)
10785 		return;
10786 
10787 	/* Check if OEM version format is as expected. */
10788 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10789 			   &capabilities);
10790 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10791 		return;
10792 
10793 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10794 			   &gen_snap);
10795 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10796 			   &release);
10797 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10798 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10799 }
10800 
10801 /**
10802  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10803  * @pf: board private structure
10804  **/
10805 static int i40e_reset(struct i40e_pf *pf)
10806 {
10807 	struct i40e_hw *hw = &pf->hw;
10808 	int ret;
10809 
10810 	ret = i40e_pf_reset(hw);
10811 	if (ret) {
10812 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10813 		set_bit(__I40E_RESET_FAILED, pf->state);
10814 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10815 	} else {
10816 		pf->pfr_count++;
10817 	}
10818 	return ret;
10819 }
10820 
10821 /**
10822  * i40e_rebuild - rebuild using a saved config
10823  * @pf: board private structure
10824  * @reinit: if the Main VSI needs to re-initialized.
10825  * @lock_acquired: indicates whether or not the lock has been acquired
10826  * before this function was called.
10827  **/
10828 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10829 {
10830 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10831 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10832 	struct i40e_hw *hw = &pf->hw;
10833 	int ret;
10834 	u32 val;
10835 	int v;
10836 
10837 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10838 	    is_recovery_mode_reported)
10839 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10840 
10841 	if (test_bit(__I40E_DOWN, pf->state) &&
10842 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10843 		goto clear_recovery;
10844 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10845 
10846 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10847 	ret = i40e_init_adminq(&pf->hw);
10848 	if (ret) {
10849 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %pe aq_err %s\n",
10850 			 ERR_PTR(ret),
10851 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10852 		goto clear_recovery;
10853 	}
10854 	i40e_get_oem_version(&pf->hw);
10855 
10856 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10857 		/* The following delay is necessary for firmware update. */
10858 		mdelay(1000);
10859 	}
10860 
10861 	/* re-verify the eeprom if we just had an EMP reset */
10862 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10863 		i40e_verify_eeprom(pf);
10864 
10865 	/* if we are going out of or into recovery mode we have to act
10866 	 * accordingly with regard to resources initialization
10867 	 * and deinitialization
10868 	 */
10869 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10870 		if (i40e_get_capabilities(pf,
10871 					  i40e_aqc_opc_list_func_capabilities))
10872 			goto end_unlock;
10873 
10874 		if (is_recovery_mode_reported) {
10875 			/* we're staying in recovery mode so we'll reinitialize
10876 			 * misc vector here
10877 			 */
10878 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10879 				goto end_unlock;
10880 		} else {
10881 			if (!lock_acquired)
10882 				rtnl_lock();
10883 			/* we're going out of recovery mode so we'll free
10884 			 * the IRQ allocated specifically for recovery mode
10885 			 * and restore the interrupt scheme
10886 			 */
10887 			free_irq(pf->pdev->irq, pf);
10888 			i40e_clear_interrupt_scheme(pf);
10889 			if (i40e_restore_interrupt_scheme(pf))
10890 				goto end_unlock;
10891 		}
10892 
10893 		/* tell the firmware that we're starting */
10894 		i40e_send_version(pf);
10895 
10896 		/* bail out in case recovery mode was detected, as there is
10897 		 * no need for further configuration.
10898 		 */
10899 		goto end_unlock;
10900 	}
10901 
10902 	i40e_clear_pxe_mode(hw);
10903 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10904 	if (ret)
10905 		goto end_core_reset;
10906 
10907 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10908 				hw->func_caps.num_rx_qp, 0, 0);
10909 	if (ret) {
10910 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10911 		goto end_core_reset;
10912 	}
10913 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10914 	if (ret) {
10915 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10916 		goto end_core_reset;
10917 	}
10918 
10919 #ifdef CONFIG_I40E_DCB
10920 	/* Enable FW to write a default DCB config on link-up
10921 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10922 	 * is not supported with new link speed
10923 	 */
10924 	if (i40e_is_tc_mqprio_enabled(pf)) {
10925 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10926 	} else {
10927 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10928 		    (hw->phy.link_info.link_speed &
10929 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10930 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10931 			dev_warn(&pf->pdev->dev,
10932 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10933 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10934 		} else {
10935 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10936 			ret = i40e_init_pf_dcb(pf);
10937 			if (ret) {
10938 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10939 					 ret);
10940 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10941 				/* Continue without DCB enabled */
10942 			}
10943 		}
10944 	}
10945 
10946 #endif /* CONFIG_I40E_DCB */
10947 	if (!lock_acquired)
10948 		rtnl_lock();
10949 	ret = i40e_setup_pf_switch(pf, reinit, true);
10950 	if (ret)
10951 		goto end_unlock;
10952 
10953 	/* The driver only wants link up/down and module qualification
10954 	 * reports from firmware.  Note the negative logic.
10955 	 */
10956 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10957 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10958 					 I40E_AQ_EVENT_MEDIA_NA |
10959 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10960 	if (ret)
10961 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
10962 			 ERR_PTR(ret),
10963 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10964 
10965 	/* Rebuild the VSIs and VEBs that existed before reset.
10966 	 * They are still in our local switch element arrays, so only
10967 	 * need to rebuild the switch model in the HW.
10968 	 *
10969 	 * If there were VEBs but the reconstitution failed, we'll try
10970 	 * to recover minimal use by getting the basic PF VSI working.
10971 	 */
10972 	if (vsi->uplink_seid != pf->mac_seid) {
10973 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10974 		/* find the one VEB connected to the MAC, and find orphans */
10975 		for (v = 0; v < I40E_MAX_VEB; v++) {
10976 			if (!pf->veb[v])
10977 				continue;
10978 
10979 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10980 			    pf->veb[v]->uplink_seid == 0) {
10981 				ret = i40e_reconstitute_veb(pf->veb[v]);
10982 
10983 				if (!ret)
10984 					continue;
10985 
10986 				/* If Main VEB failed, we're in deep doodoo,
10987 				 * so give up rebuilding the switch and set up
10988 				 * for minimal rebuild of PF VSI.
10989 				 * If orphan failed, we'll report the error
10990 				 * but try to keep going.
10991 				 */
10992 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10993 					dev_info(&pf->pdev->dev,
10994 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10995 						 ret);
10996 					vsi->uplink_seid = pf->mac_seid;
10997 					break;
10998 				} else if (pf->veb[v]->uplink_seid == 0) {
10999 					dev_info(&pf->pdev->dev,
11000 						 "rebuild of orphan VEB failed: %d\n",
11001 						 ret);
11002 				}
11003 			}
11004 		}
11005 	}
11006 
11007 	if (vsi->uplink_seid == pf->mac_seid) {
11008 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
11009 		/* no VEB, so rebuild only the Main VSI */
11010 		ret = i40e_add_vsi(vsi);
11011 		if (ret) {
11012 			dev_info(&pf->pdev->dev,
11013 				 "rebuild of Main VSI failed: %d\n", ret);
11014 			goto end_unlock;
11015 		}
11016 	}
11017 
11018 	if (vsi->mqprio_qopt.max_rate[0]) {
11019 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
11020 						  vsi->mqprio_qopt.max_rate[0]);
11021 		u64 credits = 0;
11022 
11023 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
11024 		if (ret)
11025 			goto end_unlock;
11026 
11027 		credits = max_tx_rate;
11028 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
11029 		dev_dbg(&vsi->back->pdev->dev,
11030 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
11031 			max_tx_rate,
11032 			credits,
11033 			vsi->seid);
11034 	}
11035 
11036 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
11037 	if (ret)
11038 		goto end_unlock;
11039 
11040 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11041 	 * for this main VSI if they exist
11042 	 */
11043 	ret = i40e_rebuild_channels(vsi);
11044 	if (ret)
11045 		goto end_unlock;
11046 
11047 	/* Reconfigure hardware for allowing smaller MSS in the case
11048 	 * of TSO, so that we avoid the MDD being fired and causing
11049 	 * a reset in the case of small MSS+TSO.
11050 	 */
11051 #define I40E_REG_MSS          0x000E64DC
11052 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11053 #define I40E_64BYTE_MSS       0x400000
11054 	val = rd32(hw, I40E_REG_MSS);
11055 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11056 		val &= ~I40E_REG_MSS_MIN_MASK;
11057 		val |= I40E_64BYTE_MSS;
11058 		wr32(hw, I40E_REG_MSS, val);
11059 	}
11060 
11061 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
11062 		msleep(75);
11063 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11064 		if (ret)
11065 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
11066 				 ERR_PTR(ret),
11067 				 i40e_aq_str(&pf->hw,
11068 					     pf->hw.aq.asq_last_status));
11069 	}
11070 	/* reinit the misc interrupt */
11071 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11072 		ret = i40e_setup_misc_vector(pf);
11073 		if (ret)
11074 			goto end_unlock;
11075 	}
11076 
11077 	/* Add a filter to drop all Flow control frames from any VSI from being
11078 	 * transmitted. By doing so we stop a malicious VF from sending out
11079 	 * PAUSE or PFC frames and potentially controlling traffic for other
11080 	 * PF/VF VSIs.
11081 	 * The FW can still send Flow control frames if enabled.
11082 	 */
11083 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11084 						       pf->main_vsi_seid);
11085 
11086 	/* restart the VSIs that were rebuilt and running before the reset */
11087 	i40e_pf_unquiesce_all_vsi(pf);
11088 
11089 	/* Release the RTNL lock before we start resetting VFs */
11090 	if (!lock_acquired)
11091 		rtnl_unlock();
11092 
11093 	/* Restore promiscuous settings */
11094 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11095 	if (ret)
11096 		dev_warn(&pf->pdev->dev,
11097 			 "Failed to restore promiscuous setting: %s, err %pe aq_err %s\n",
11098 			 pf->cur_promisc ? "on" : "off",
11099 			 ERR_PTR(ret),
11100 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11101 
11102 	i40e_reset_all_vfs(pf, true);
11103 
11104 	/* tell the firmware that we're starting */
11105 	i40e_send_version(pf);
11106 
11107 	/* We've already released the lock, so don't do it again */
11108 	goto end_core_reset;
11109 
11110 end_unlock:
11111 	if (!lock_acquired)
11112 		rtnl_unlock();
11113 end_core_reset:
11114 	clear_bit(__I40E_RESET_FAILED, pf->state);
11115 clear_recovery:
11116 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11117 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11118 }
11119 
11120 /**
11121  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11122  * @pf: board private structure
11123  * @reinit: if the Main VSI needs to re-initialized.
11124  * @lock_acquired: indicates whether or not the lock has been acquired
11125  * before this function was called.
11126  **/
11127 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11128 				   bool lock_acquired)
11129 {
11130 	int ret;
11131 
11132 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11133 		return;
11134 	/* Now we wait for GRST to settle out.
11135 	 * We don't have to delete the VEBs or VSIs from the hw switch
11136 	 * because the reset will make them disappear.
11137 	 */
11138 	ret = i40e_reset(pf);
11139 	if (!ret)
11140 		i40e_rebuild(pf, reinit, lock_acquired);
11141 }
11142 
11143 /**
11144  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11145  * @pf: board private structure
11146  *
11147  * Close up the VFs and other things in prep for a Core Reset,
11148  * then get ready to rebuild the world.
11149  * @lock_acquired: indicates whether or not the lock has been acquired
11150  * before this function was called.
11151  **/
11152 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11153 {
11154 	i40e_prep_for_reset(pf);
11155 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11156 }
11157 
11158 /**
11159  * i40e_handle_mdd_event
11160  * @pf: pointer to the PF structure
11161  *
11162  * Called from the MDD irq handler to identify possibly malicious vfs
11163  **/
11164 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11165 {
11166 	struct i40e_hw *hw = &pf->hw;
11167 	bool mdd_detected = false;
11168 	struct i40e_vf *vf;
11169 	u32 reg;
11170 	int i;
11171 
11172 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11173 		return;
11174 
11175 	/* find what triggered the MDD event */
11176 	reg = rd32(hw, I40E_GL_MDET_TX);
11177 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11178 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
11179 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
11180 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
11181 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
11182 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
11183 				I40E_GL_MDET_TX_EVENT_SHIFT;
11184 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
11185 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
11186 				pf->hw.func_caps.base_queue;
11187 		if (netif_msg_tx_err(pf))
11188 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11189 				 event, queue, pf_num, vf_num);
11190 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11191 		mdd_detected = true;
11192 	}
11193 	reg = rd32(hw, I40E_GL_MDET_RX);
11194 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11195 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
11196 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
11197 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
11198 				I40E_GL_MDET_RX_EVENT_SHIFT;
11199 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
11200 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
11201 				pf->hw.func_caps.base_queue;
11202 		if (netif_msg_rx_err(pf))
11203 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11204 				 event, queue, func);
11205 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11206 		mdd_detected = true;
11207 	}
11208 
11209 	if (mdd_detected) {
11210 		reg = rd32(hw, I40E_PF_MDET_TX);
11211 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11212 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11213 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11214 		}
11215 		reg = rd32(hw, I40E_PF_MDET_RX);
11216 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11217 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11218 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11219 		}
11220 	}
11221 
11222 	/* see if one of the VFs needs its hand slapped */
11223 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11224 		vf = &(pf->vf[i]);
11225 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11226 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11227 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11228 			vf->num_mdd_events++;
11229 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11230 				 i);
11231 			dev_info(&pf->pdev->dev,
11232 				 "Use PF Control I/F to re-enable the VF\n");
11233 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11234 		}
11235 
11236 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11237 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11238 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11239 			vf->num_mdd_events++;
11240 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11241 				 i);
11242 			dev_info(&pf->pdev->dev,
11243 				 "Use PF Control I/F to re-enable the VF\n");
11244 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11245 		}
11246 	}
11247 
11248 	/* re-enable mdd interrupt cause */
11249 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11250 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11251 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11252 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11253 	i40e_flush(hw);
11254 }
11255 
11256 /**
11257  * i40e_service_task - Run the driver's async subtasks
11258  * @work: pointer to work_struct containing our data
11259  **/
11260 static void i40e_service_task(struct work_struct *work)
11261 {
11262 	struct i40e_pf *pf = container_of(work,
11263 					  struct i40e_pf,
11264 					  service_task);
11265 	unsigned long start_time = jiffies;
11266 
11267 	/* don't bother with service tasks if a reset is in progress */
11268 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11269 	    test_bit(__I40E_SUSPENDED, pf->state))
11270 		return;
11271 
11272 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11273 		return;
11274 
11275 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11276 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11277 		i40e_sync_filters_subtask(pf);
11278 		i40e_reset_subtask(pf);
11279 		i40e_handle_mdd_event(pf);
11280 		i40e_vc_process_vflr_event(pf);
11281 		i40e_watchdog_subtask(pf);
11282 		i40e_fdir_reinit_subtask(pf);
11283 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11284 			/* Client subtask will reopen next time through. */
11285 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11286 							   true);
11287 		} else {
11288 			i40e_client_subtask(pf);
11289 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11290 					       pf->state))
11291 				i40e_notify_client_of_l2_param_changes(
11292 								pf->vsi[pf->lan_vsi]);
11293 		}
11294 		i40e_sync_filters_subtask(pf);
11295 	} else {
11296 		i40e_reset_subtask(pf);
11297 	}
11298 
11299 	i40e_clean_adminq_subtask(pf);
11300 
11301 	/* flush memory to make sure state is correct before next watchdog */
11302 	smp_mb__before_atomic();
11303 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11304 
11305 	/* If the tasks have taken longer than one timer cycle or there
11306 	 * is more work to be done, reschedule the service task now
11307 	 * rather than wait for the timer to tick again.
11308 	 */
11309 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11310 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11311 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11312 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11313 		i40e_service_event_schedule(pf);
11314 }
11315 
11316 /**
11317  * i40e_service_timer - timer callback
11318  * @t: timer list pointer
11319  **/
11320 static void i40e_service_timer(struct timer_list *t)
11321 {
11322 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11323 
11324 	mod_timer(&pf->service_timer,
11325 		  round_jiffies(jiffies + pf->service_timer_period));
11326 	i40e_service_event_schedule(pf);
11327 }
11328 
11329 /**
11330  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11331  * @vsi: the VSI being configured
11332  **/
11333 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11334 {
11335 	struct i40e_pf *pf = vsi->back;
11336 
11337 	switch (vsi->type) {
11338 	case I40E_VSI_MAIN:
11339 		vsi->alloc_queue_pairs = pf->num_lan_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 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11347 			vsi->num_q_vectors = pf->num_lan_msix;
11348 		else
11349 			vsi->num_q_vectors = 1;
11350 
11351 		break;
11352 
11353 	case I40E_VSI_FDIR:
11354 		vsi->alloc_queue_pairs = 1;
11355 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11356 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11357 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11358 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11359 		vsi->num_q_vectors = pf->num_fdsb_msix;
11360 		break;
11361 
11362 	case I40E_VSI_VMDQ2:
11363 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11364 		if (!vsi->num_tx_desc)
11365 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11366 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11367 		if (!vsi->num_rx_desc)
11368 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11369 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11370 		vsi->num_q_vectors = pf->num_vmdq_msix;
11371 		break;
11372 
11373 	case I40E_VSI_SRIOV:
11374 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11375 		if (!vsi->num_tx_desc)
11376 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11377 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11378 		if (!vsi->num_rx_desc)
11379 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11380 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11381 		break;
11382 
11383 	default:
11384 		WARN_ON(1);
11385 		return -ENODATA;
11386 	}
11387 
11388 	if (is_kdump_kernel()) {
11389 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11390 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11391 	}
11392 
11393 	return 0;
11394 }
11395 
11396 /**
11397  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11398  * @vsi: VSI pointer
11399  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11400  *
11401  * On error: returns error code (negative)
11402  * On success: returns 0
11403  **/
11404 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11405 {
11406 	struct i40e_ring **next_rings;
11407 	int size;
11408 	int ret = 0;
11409 
11410 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11411 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11412 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11413 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11414 	if (!vsi->tx_rings)
11415 		return -ENOMEM;
11416 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11417 	if (i40e_enabled_xdp_vsi(vsi)) {
11418 		vsi->xdp_rings = next_rings;
11419 		next_rings += vsi->alloc_queue_pairs;
11420 	}
11421 	vsi->rx_rings = next_rings;
11422 
11423 	if (alloc_qvectors) {
11424 		/* allocate memory for q_vector pointers */
11425 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11426 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11427 		if (!vsi->q_vectors) {
11428 			ret = -ENOMEM;
11429 			goto err_vectors;
11430 		}
11431 	}
11432 	return ret;
11433 
11434 err_vectors:
11435 	kfree(vsi->tx_rings);
11436 	return ret;
11437 }
11438 
11439 /**
11440  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11441  * @pf: board private structure
11442  * @type: type of VSI
11443  *
11444  * On error: returns error code (negative)
11445  * On success: returns vsi index in PF (positive)
11446  **/
11447 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11448 {
11449 	int ret = -ENODEV;
11450 	struct i40e_vsi *vsi;
11451 	int vsi_idx;
11452 	int i;
11453 
11454 	/* Need to protect the allocation of the VSIs at the PF level */
11455 	mutex_lock(&pf->switch_mutex);
11456 
11457 	/* VSI list may be fragmented if VSI creation/destruction has
11458 	 * been happening.  We can afford to do a quick scan to look
11459 	 * for any free VSIs in the list.
11460 	 *
11461 	 * find next empty vsi slot, looping back around if necessary
11462 	 */
11463 	i = pf->next_vsi;
11464 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11465 		i++;
11466 	if (i >= pf->num_alloc_vsi) {
11467 		i = 0;
11468 		while (i < pf->next_vsi && pf->vsi[i])
11469 			i++;
11470 	}
11471 
11472 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11473 		vsi_idx = i;             /* Found one! */
11474 	} else {
11475 		ret = -ENODEV;
11476 		goto unlock_pf;  /* out of VSI slots! */
11477 	}
11478 	pf->next_vsi = ++i;
11479 
11480 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11481 	if (!vsi) {
11482 		ret = -ENOMEM;
11483 		goto unlock_pf;
11484 	}
11485 	vsi->type = type;
11486 	vsi->back = pf;
11487 	set_bit(__I40E_VSI_DOWN, vsi->state);
11488 	vsi->flags = 0;
11489 	vsi->idx = vsi_idx;
11490 	vsi->int_rate_limit = 0;
11491 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11492 				pf->rss_table_size : 64;
11493 	vsi->netdev_registered = false;
11494 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11495 	hash_init(vsi->mac_filter_hash);
11496 	vsi->irqs_ready = false;
11497 
11498 	if (type == I40E_VSI_MAIN) {
11499 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11500 		if (!vsi->af_xdp_zc_qps)
11501 			goto err_rings;
11502 	}
11503 
11504 	ret = i40e_set_num_rings_in_vsi(vsi);
11505 	if (ret)
11506 		goto err_rings;
11507 
11508 	ret = i40e_vsi_alloc_arrays(vsi, true);
11509 	if (ret)
11510 		goto err_rings;
11511 
11512 	/* Setup default MSIX irq handler for VSI */
11513 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11514 
11515 	/* Initialize VSI lock */
11516 	spin_lock_init(&vsi->mac_filter_hash_lock);
11517 	pf->vsi[vsi_idx] = vsi;
11518 	ret = vsi_idx;
11519 	goto unlock_pf;
11520 
11521 err_rings:
11522 	bitmap_free(vsi->af_xdp_zc_qps);
11523 	pf->next_vsi = i - 1;
11524 	kfree(vsi);
11525 unlock_pf:
11526 	mutex_unlock(&pf->switch_mutex);
11527 	return ret;
11528 }
11529 
11530 /**
11531  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11532  * @vsi: VSI pointer
11533  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11534  *
11535  * On error: returns error code (negative)
11536  * On success: returns 0
11537  **/
11538 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11539 {
11540 	/* free the ring and vector containers */
11541 	if (free_qvectors) {
11542 		kfree(vsi->q_vectors);
11543 		vsi->q_vectors = NULL;
11544 	}
11545 	kfree(vsi->tx_rings);
11546 	vsi->tx_rings = NULL;
11547 	vsi->rx_rings = NULL;
11548 	vsi->xdp_rings = NULL;
11549 }
11550 
11551 /**
11552  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11553  * and lookup table
11554  * @vsi: Pointer to VSI structure
11555  */
11556 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11557 {
11558 	if (!vsi)
11559 		return;
11560 
11561 	kfree(vsi->rss_hkey_user);
11562 	vsi->rss_hkey_user = NULL;
11563 
11564 	kfree(vsi->rss_lut_user);
11565 	vsi->rss_lut_user = NULL;
11566 }
11567 
11568 /**
11569  * i40e_vsi_clear - Deallocate the VSI provided
11570  * @vsi: the VSI being un-configured
11571  **/
11572 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11573 {
11574 	struct i40e_pf *pf;
11575 
11576 	if (!vsi)
11577 		return 0;
11578 
11579 	if (!vsi->back)
11580 		goto free_vsi;
11581 	pf = vsi->back;
11582 
11583 	mutex_lock(&pf->switch_mutex);
11584 	if (!pf->vsi[vsi->idx]) {
11585 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11586 			vsi->idx, vsi->idx, vsi->type);
11587 		goto unlock_vsi;
11588 	}
11589 
11590 	if (pf->vsi[vsi->idx] != vsi) {
11591 		dev_err(&pf->pdev->dev,
11592 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11593 			pf->vsi[vsi->idx]->idx,
11594 			pf->vsi[vsi->idx]->type,
11595 			vsi->idx, vsi->type);
11596 		goto unlock_vsi;
11597 	}
11598 
11599 	/* updates the PF for this cleared vsi */
11600 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11601 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11602 
11603 	bitmap_free(vsi->af_xdp_zc_qps);
11604 	i40e_vsi_free_arrays(vsi, true);
11605 	i40e_clear_rss_config_user(vsi);
11606 
11607 	pf->vsi[vsi->idx] = NULL;
11608 	if (vsi->idx < pf->next_vsi)
11609 		pf->next_vsi = vsi->idx;
11610 
11611 unlock_vsi:
11612 	mutex_unlock(&pf->switch_mutex);
11613 free_vsi:
11614 	kfree(vsi);
11615 
11616 	return 0;
11617 }
11618 
11619 /**
11620  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11621  * @vsi: the VSI being cleaned
11622  **/
11623 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11624 {
11625 	int i;
11626 
11627 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11628 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11629 			kfree_rcu(vsi->tx_rings[i], rcu);
11630 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11631 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11632 			if (vsi->xdp_rings)
11633 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11634 		}
11635 	}
11636 }
11637 
11638 /**
11639  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11640  * @vsi: the VSI being configured
11641  **/
11642 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11643 {
11644 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11645 	struct i40e_pf *pf = vsi->back;
11646 	struct i40e_ring *ring;
11647 
11648 	/* Set basic values in the rings to be used later during open() */
11649 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11650 		/* allocate space for both Tx and Rx in one shot */
11651 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11652 		if (!ring)
11653 			goto err_out;
11654 
11655 		ring->queue_index = i;
11656 		ring->reg_idx = vsi->base_queue + i;
11657 		ring->ring_active = false;
11658 		ring->vsi = vsi;
11659 		ring->netdev = vsi->netdev;
11660 		ring->dev = &pf->pdev->dev;
11661 		ring->count = vsi->num_tx_desc;
11662 		ring->size = 0;
11663 		ring->dcb_tc = 0;
11664 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11665 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11666 		ring->itr_setting = pf->tx_itr_default;
11667 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11668 
11669 		if (!i40e_enabled_xdp_vsi(vsi))
11670 			goto setup_rx;
11671 
11672 		ring->queue_index = vsi->alloc_queue_pairs + i;
11673 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11674 		ring->ring_active = false;
11675 		ring->vsi = vsi;
11676 		ring->netdev = NULL;
11677 		ring->dev = &pf->pdev->dev;
11678 		ring->count = vsi->num_tx_desc;
11679 		ring->size = 0;
11680 		ring->dcb_tc = 0;
11681 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11682 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11683 		set_ring_xdp(ring);
11684 		ring->itr_setting = pf->tx_itr_default;
11685 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11686 
11687 setup_rx:
11688 		ring->queue_index = i;
11689 		ring->reg_idx = vsi->base_queue + i;
11690 		ring->ring_active = false;
11691 		ring->vsi = vsi;
11692 		ring->netdev = vsi->netdev;
11693 		ring->dev = &pf->pdev->dev;
11694 		ring->count = vsi->num_rx_desc;
11695 		ring->size = 0;
11696 		ring->dcb_tc = 0;
11697 		ring->itr_setting = pf->rx_itr_default;
11698 		WRITE_ONCE(vsi->rx_rings[i], ring);
11699 	}
11700 
11701 	return 0;
11702 
11703 err_out:
11704 	i40e_vsi_clear_rings(vsi);
11705 	return -ENOMEM;
11706 }
11707 
11708 /**
11709  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11710  * @pf: board private structure
11711  * @vectors: the number of MSI-X vectors to request
11712  *
11713  * Returns the number of vectors reserved, or error
11714  **/
11715 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11716 {
11717 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11718 					I40E_MIN_MSIX, vectors);
11719 	if (vectors < 0) {
11720 		dev_info(&pf->pdev->dev,
11721 			 "MSI-X vector reservation failed: %d\n", vectors);
11722 		vectors = 0;
11723 	}
11724 
11725 	return vectors;
11726 }
11727 
11728 /**
11729  * i40e_init_msix - Setup the MSIX capability
11730  * @pf: board private structure
11731  *
11732  * Work with the OS to set up the MSIX vectors needed.
11733  *
11734  * Returns the number of vectors reserved or negative on failure
11735  **/
11736 static int i40e_init_msix(struct i40e_pf *pf)
11737 {
11738 	struct i40e_hw *hw = &pf->hw;
11739 	int cpus, extra_vectors;
11740 	int vectors_left;
11741 	int v_budget, i;
11742 	int v_actual;
11743 	int iwarp_requested = 0;
11744 
11745 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11746 		return -ENODEV;
11747 
11748 	/* The number of vectors we'll request will be comprised of:
11749 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11750 	 *   - The number of LAN queue pairs
11751 	 *	- Queues being used for RSS.
11752 	 *		We don't need as many as max_rss_size vectors.
11753 	 *		use rss_size instead in the calculation since that
11754 	 *		is governed by number of cpus in the system.
11755 	 *	- assumes symmetric Tx/Rx pairing
11756 	 *   - The number of VMDq pairs
11757 	 *   - The CPU count within the NUMA node if iWARP is enabled
11758 	 * Once we count this up, try the request.
11759 	 *
11760 	 * If we can't get what we want, we'll simplify to nearly nothing
11761 	 * and try again.  If that still fails, we punt.
11762 	 */
11763 	vectors_left = hw->func_caps.num_msix_vectors;
11764 	v_budget = 0;
11765 
11766 	/* reserve one vector for miscellaneous handler */
11767 	if (vectors_left) {
11768 		v_budget++;
11769 		vectors_left--;
11770 	}
11771 
11772 	/* reserve some vectors for the main PF traffic queues. Initially we
11773 	 * only reserve at most 50% of the available vectors, in the case that
11774 	 * the number of online CPUs is large. This ensures that we can enable
11775 	 * extra features as well. Once we've enabled the other features, we
11776 	 * will use any remaining vectors to reach as close as we can to the
11777 	 * number of online CPUs.
11778 	 */
11779 	cpus = num_online_cpus();
11780 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11781 	vectors_left -= pf->num_lan_msix;
11782 
11783 	/* reserve one vector for sideband flow director */
11784 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11785 		if (vectors_left) {
11786 			pf->num_fdsb_msix = 1;
11787 			v_budget++;
11788 			vectors_left--;
11789 		} else {
11790 			pf->num_fdsb_msix = 0;
11791 		}
11792 	}
11793 
11794 	/* can we reserve enough for iWARP? */
11795 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11796 		iwarp_requested = pf->num_iwarp_msix;
11797 
11798 		if (!vectors_left)
11799 			pf->num_iwarp_msix = 0;
11800 		else if (vectors_left < pf->num_iwarp_msix)
11801 			pf->num_iwarp_msix = 1;
11802 		v_budget += pf->num_iwarp_msix;
11803 		vectors_left -= pf->num_iwarp_msix;
11804 	}
11805 
11806 	/* any vectors left over go for VMDq support */
11807 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11808 		if (!vectors_left) {
11809 			pf->num_vmdq_msix = 0;
11810 			pf->num_vmdq_qps = 0;
11811 		} else {
11812 			int vmdq_vecs_wanted =
11813 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11814 			int vmdq_vecs =
11815 				min_t(int, vectors_left, vmdq_vecs_wanted);
11816 
11817 			/* if we're short on vectors for what's desired, we limit
11818 			 * the queues per vmdq.  If this is still more than are
11819 			 * available, the user will need to change the number of
11820 			 * queues/vectors used by the PF later with the ethtool
11821 			 * channels command
11822 			 */
11823 			if (vectors_left < vmdq_vecs_wanted) {
11824 				pf->num_vmdq_qps = 1;
11825 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11826 				vmdq_vecs = min_t(int,
11827 						  vectors_left,
11828 						  vmdq_vecs_wanted);
11829 			}
11830 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11831 
11832 			v_budget += vmdq_vecs;
11833 			vectors_left -= vmdq_vecs;
11834 		}
11835 	}
11836 
11837 	/* On systems with a large number of SMP cores, we previously limited
11838 	 * the number of vectors for num_lan_msix to be at most 50% of the
11839 	 * available vectors, to allow for other features. Now, we add back
11840 	 * the remaining vectors. However, we ensure that the total
11841 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11842 	 * calculate the number of vectors we can add without going over the
11843 	 * cap of CPUs. For systems with a small number of CPUs this will be
11844 	 * zero.
11845 	 */
11846 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11847 	pf->num_lan_msix += extra_vectors;
11848 	vectors_left -= extra_vectors;
11849 
11850 	WARN(vectors_left < 0,
11851 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11852 
11853 	v_budget += pf->num_lan_msix;
11854 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11855 				   GFP_KERNEL);
11856 	if (!pf->msix_entries)
11857 		return -ENOMEM;
11858 
11859 	for (i = 0; i < v_budget; i++)
11860 		pf->msix_entries[i].entry = i;
11861 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11862 
11863 	if (v_actual < I40E_MIN_MSIX) {
11864 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11865 		kfree(pf->msix_entries);
11866 		pf->msix_entries = NULL;
11867 		pci_disable_msix(pf->pdev);
11868 		return -ENODEV;
11869 
11870 	} else if (v_actual == I40E_MIN_MSIX) {
11871 		/* Adjust for minimal MSIX use */
11872 		pf->num_vmdq_vsis = 0;
11873 		pf->num_vmdq_qps = 0;
11874 		pf->num_lan_qps = 1;
11875 		pf->num_lan_msix = 1;
11876 
11877 	} else if (v_actual != v_budget) {
11878 		/* If we have limited resources, we will start with no vectors
11879 		 * for the special features and then allocate vectors to some
11880 		 * of these features based on the policy and at the end disable
11881 		 * the features that did not get any vectors.
11882 		 */
11883 		int vec;
11884 
11885 		dev_info(&pf->pdev->dev,
11886 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11887 			 v_actual, v_budget);
11888 		/* reserve the misc vector */
11889 		vec = v_actual - 1;
11890 
11891 		/* Scale vector usage down */
11892 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11893 		pf->num_vmdq_vsis = 1;
11894 		pf->num_vmdq_qps = 1;
11895 
11896 		/* partition out the remaining vectors */
11897 		switch (vec) {
11898 		case 2:
11899 			pf->num_lan_msix = 1;
11900 			break;
11901 		case 3:
11902 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11903 				pf->num_lan_msix = 1;
11904 				pf->num_iwarp_msix = 1;
11905 			} else {
11906 				pf->num_lan_msix = 2;
11907 			}
11908 			break;
11909 		default:
11910 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11911 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11912 						 iwarp_requested);
11913 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11914 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11915 			} else {
11916 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11917 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11918 			}
11919 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11920 				pf->num_fdsb_msix = 1;
11921 				vec--;
11922 			}
11923 			pf->num_lan_msix = min_t(int,
11924 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11925 							      pf->num_lan_msix);
11926 			pf->num_lan_qps = pf->num_lan_msix;
11927 			break;
11928 		}
11929 	}
11930 
11931 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11932 	    (pf->num_fdsb_msix == 0)) {
11933 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11934 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11935 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11936 	}
11937 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11938 	    (pf->num_vmdq_msix == 0)) {
11939 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11940 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11941 	}
11942 
11943 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11944 	    (pf->num_iwarp_msix == 0)) {
11945 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11946 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11947 	}
11948 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11949 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11950 		   pf->num_lan_msix,
11951 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11952 		   pf->num_fdsb_msix,
11953 		   pf->num_iwarp_msix);
11954 
11955 	return v_actual;
11956 }
11957 
11958 /**
11959  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11960  * @vsi: the VSI being configured
11961  * @v_idx: index of the vector in the vsi struct
11962  *
11963  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11964  **/
11965 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11966 {
11967 	struct i40e_q_vector *q_vector;
11968 
11969 	/* allocate q_vector */
11970 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11971 	if (!q_vector)
11972 		return -ENOMEM;
11973 
11974 	q_vector->vsi = vsi;
11975 	q_vector->v_idx = v_idx;
11976 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11977 
11978 	if (vsi->netdev)
11979 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
11980 
11981 	/* tie q_vector and vsi together */
11982 	vsi->q_vectors[v_idx] = q_vector;
11983 
11984 	return 0;
11985 }
11986 
11987 /**
11988  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11989  * @vsi: the VSI being configured
11990  *
11991  * We allocate one q_vector per queue interrupt.  If allocation fails we
11992  * return -ENOMEM.
11993  **/
11994 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11995 {
11996 	struct i40e_pf *pf = vsi->back;
11997 	int err, v_idx, num_q_vectors;
11998 
11999 	/* if not MSIX, give the one vector only to the LAN VSI */
12000 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
12001 		num_q_vectors = vsi->num_q_vectors;
12002 	else if (vsi == pf->vsi[pf->lan_vsi])
12003 		num_q_vectors = 1;
12004 	else
12005 		return -EINVAL;
12006 
12007 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
12008 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
12009 		if (err)
12010 			goto err_out;
12011 	}
12012 
12013 	return 0;
12014 
12015 err_out:
12016 	while (v_idx--)
12017 		i40e_free_q_vector(vsi, v_idx);
12018 
12019 	return err;
12020 }
12021 
12022 /**
12023  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
12024  * @pf: board private structure to initialize
12025  **/
12026 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
12027 {
12028 	int vectors = 0;
12029 	ssize_t size;
12030 
12031 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12032 		vectors = i40e_init_msix(pf);
12033 		if (vectors < 0) {
12034 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
12035 				       I40E_FLAG_IWARP_ENABLED	|
12036 				       I40E_FLAG_RSS_ENABLED	|
12037 				       I40E_FLAG_DCB_CAPABLE	|
12038 				       I40E_FLAG_DCB_ENABLED	|
12039 				       I40E_FLAG_SRIOV_ENABLED	|
12040 				       I40E_FLAG_FD_SB_ENABLED	|
12041 				       I40E_FLAG_FD_ATR_ENABLED	|
12042 				       I40E_FLAG_VMDQ_ENABLED);
12043 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12044 
12045 			/* rework the queue expectations without MSIX */
12046 			i40e_determine_queue_usage(pf);
12047 		}
12048 	}
12049 
12050 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
12051 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
12052 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12053 		vectors = pci_enable_msi(pf->pdev);
12054 		if (vectors < 0) {
12055 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12056 				 vectors);
12057 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
12058 		}
12059 		vectors = 1;  /* one MSI or Legacy vector */
12060 	}
12061 
12062 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
12063 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12064 
12065 	/* set up vector assignment tracking */
12066 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12067 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12068 	if (!pf->irq_pile)
12069 		return -ENOMEM;
12070 
12071 	pf->irq_pile->num_entries = vectors;
12072 
12073 	/* track first vector for misc interrupts, ignore return */
12074 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12075 
12076 	return 0;
12077 }
12078 
12079 /**
12080  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12081  * @pf: private board data structure
12082  *
12083  * Restore the interrupt scheme that was cleared when we suspended the
12084  * device. This should be called during resume to re-allocate the q_vectors
12085  * and reacquire IRQs.
12086  */
12087 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12088 {
12089 	int err, i;
12090 
12091 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12092 	 * scheme. We need to re-enabled them here in order to attempt to
12093 	 * re-acquire the MSI or MSI-X vectors
12094 	 */
12095 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
12096 
12097 	err = i40e_init_interrupt_scheme(pf);
12098 	if (err)
12099 		return err;
12100 
12101 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12102 	 * rings together again.
12103 	 */
12104 	for (i = 0; i < pf->num_alloc_vsi; i++) {
12105 		if (pf->vsi[i]) {
12106 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
12107 			if (err)
12108 				goto err_unwind;
12109 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
12110 		}
12111 	}
12112 
12113 	err = i40e_setup_misc_vector(pf);
12114 	if (err)
12115 		goto err_unwind;
12116 
12117 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
12118 		i40e_client_update_msix_info(pf);
12119 
12120 	return 0;
12121 
12122 err_unwind:
12123 	while (i--) {
12124 		if (pf->vsi[i])
12125 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12126 	}
12127 
12128 	return err;
12129 }
12130 
12131 /**
12132  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12133  * non queue events in recovery mode
12134  * @pf: board private structure
12135  *
12136  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12137  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12138  * This is handled differently than in recovery mode since no Tx/Rx resources
12139  * are being allocated.
12140  **/
12141 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12142 {
12143 	int err;
12144 
12145 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12146 		err = i40e_setup_misc_vector(pf);
12147 
12148 		if (err) {
12149 			dev_info(&pf->pdev->dev,
12150 				 "MSI-X misc vector request failed, error %d\n",
12151 				 err);
12152 			return err;
12153 		}
12154 	} else {
12155 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
12156 
12157 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12158 				  pf->int_name, pf);
12159 
12160 		if (err) {
12161 			dev_info(&pf->pdev->dev,
12162 				 "MSI/legacy misc vector request failed, error %d\n",
12163 				 err);
12164 			return err;
12165 		}
12166 		i40e_enable_misc_int_causes(pf);
12167 		i40e_irq_dynamic_enable_icr0(pf);
12168 	}
12169 
12170 	return 0;
12171 }
12172 
12173 /**
12174  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12175  * @pf: board private structure
12176  *
12177  * This sets up the handler for MSIX 0, which is used to manage the
12178  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12179  * when in MSI or Legacy interrupt mode.
12180  **/
12181 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12182 {
12183 	struct i40e_hw *hw = &pf->hw;
12184 	int err = 0;
12185 
12186 	/* Only request the IRQ once, the first time through. */
12187 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12188 		err = request_irq(pf->msix_entries[0].vector,
12189 				  i40e_intr, 0, pf->int_name, pf);
12190 		if (err) {
12191 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12192 			dev_info(&pf->pdev->dev,
12193 				 "request_irq for %s failed: %d\n",
12194 				 pf->int_name, err);
12195 			return -EFAULT;
12196 		}
12197 	}
12198 
12199 	i40e_enable_misc_int_causes(pf);
12200 
12201 	/* associate no queues to the misc vector */
12202 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12203 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12204 
12205 	i40e_flush(hw);
12206 
12207 	i40e_irq_dynamic_enable_icr0(pf);
12208 
12209 	return err;
12210 }
12211 
12212 /**
12213  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12214  * @vsi: Pointer to vsi structure
12215  * @seed: Buffter to store the hash keys
12216  * @lut: Buffer to store the lookup table entries
12217  * @lut_size: Size of buffer to store the lookup table entries
12218  *
12219  * Return 0 on success, negative on failure
12220  */
12221 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12222 			   u8 *lut, u16 lut_size)
12223 {
12224 	struct i40e_pf *pf = vsi->back;
12225 	struct i40e_hw *hw = &pf->hw;
12226 	int ret = 0;
12227 
12228 	if (seed) {
12229 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12230 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12231 		if (ret) {
12232 			dev_info(&pf->pdev->dev,
12233 				 "Cannot get RSS key, err %pe aq_err %s\n",
12234 				 ERR_PTR(ret),
12235 				 i40e_aq_str(&pf->hw,
12236 					     pf->hw.aq.asq_last_status));
12237 			return ret;
12238 		}
12239 	}
12240 
12241 	if (lut) {
12242 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12243 
12244 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12245 		if (ret) {
12246 			dev_info(&pf->pdev->dev,
12247 				 "Cannot get RSS lut, err %pe aq_err %s\n",
12248 				 ERR_PTR(ret),
12249 				 i40e_aq_str(&pf->hw,
12250 					     pf->hw.aq.asq_last_status));
12251 			return ret;
12252 		}
12253 	}
12254 
12255 	return ret;
12256 }
12257 
12258 /**
12259  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12260  * @vsi: Pointer to vsi structure
12261  * @seed: RSS hash seed
12262  * @lut: Lookup table
12263  * @lut_size: Lookup table size
12264  *
12265  * Returns 0 on success, negative on failure
12266  **/
12267 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12268 			       const u8 *lut, u16 lut_size)
12269 {
12270 	struct i40e_pf *pf = vsi->back;
12271 	struct i40e_hw *hw = &pf->hw;
12272 	u16 vf_id = vsi->vf_id;
12273 	u8 i;
12274 
12275 	/* Fill out hash function seed */
12276 	if (seed) {
12277 		u32 *seed_dw = (u32 *)seed;
12278 
12279 		if (vsi->type == I40E_VSI_MAIN) {
12280 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12281 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12282 		} else if (vsi->type == I40E_VSI_SRIOV) {
12283 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12284 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12285 		} else {
12286 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12287 		}
12288 	}
12289 
12290 	if (lut) {
12291 		u32 *lut_dw = (u32 *)lut;
12292 
12293 		if (vsi->type == I40E_VSI_MAIN) {
12294 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12295 				return -EINVAL;
12296 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12297 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12298 		} else if (vsi->type == I40E_VSI_SRIOV) {
12299 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12300 				return -EINVAL;
12301 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12302 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12303 		} else {
12304 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12305 		}
12306 	}
12307 	i40e_flush(hw);
12308 
12309 	return 0;
12310 }
12311 
12312 /**
12313  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12314  * @vsi: Pointer to VSI structure
12315  * @seed: Buffer to store the keys
12316  * @lut: Buffer to store the lookup table entries
12317  * @lut_size: Size of buffer to store the lookup table entries
12318  *
12319  * Returns 0 on success, negative on failure
12320  */
12321 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12322 			    u8 *lut, u16 lut_size)
12323 {
12324 	struct i40e_pf *pf = vsi->back;
12325 	struct i40e_hw *hw = &pf->hw;
12326 	u16 i;
12327 
12328 	if (seed) {
12329 		u32 *seed_dw = (u32 *)seed;
12330 
12331 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12332 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12333 	}
12334 	if (lut) {
12335 		u32 *lut_dw = (u32 *)lut;
12336 
12337 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12338 			return -EINVAL;
12339 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12340 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12341 	}
12342 
12343 	return 0;
12344 }
12345 
12346 /**
12347  * i40e_config_rss - Configure RSS keys and lut
12348  * @vsi: Pointer to VSI structure
12349  * @seed: RSS hash seed
12350  * @lut: Lookup table
12351  * @lut_size: Lookup table size
12352  *
12353  * Returns 0 on success, negative on failure
12354  */
12355 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12356 {
12357 	struct i40e_pf *pf = vsi->back;
12358 
12359 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12360 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12361 	else
12362 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12363 }
12364 
12365 /**
12366  * i40e_get_rss - Get RSS keys and lut
12367  * @vsi: Pointer to VSI structure
12368  * @seed: Buffer to store the keys
12369  * @lut: Buffer to store the lookup table entries
12370  * @lut_size: Size of buffer to store the lookup table entries
12371  *
12372  * Returns 0 on success, negative on failure
12373  */
12374 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12375 {
12376 	struct i40e_pf *pf = vsi->back;
12377 
12378 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12379 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12380 	else
12381 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12382 }
12383 
12384 /**
12385  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12386  * @pf: Pointer to board private structure
12387  * @lut: Lookup table
12388  * @rss_table_size: Lookup table size
12389  * @rss_size: Range of queue number for hashing
12390  */
12391 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12392 		       u16 rss_table_size, u16 rss_size)
12393 {
12394 	u16 i;
12395 
12396 	for (i = 0; i < rss_table_size; i++)
12397 		lut[i] = i % rss_size;
12398 }
12399 
12400 /**
12401  * i40e_pf_config_rss - Prepare for RSS if used
12402  * @pf: board private structure
12403  **/
12404 static int i40e_pf_config_rss(struct i40e_pf *pf)
12405 {
12406 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12407 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12408 	u8 *lut;
12409 	struct i40e_hw *hw = &pf->hw;
12410 	u32 reg_val;
12411 	u64 hena;
12412 	int ret;
12413 
12414 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12415 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12416 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12417 	hena |= i40e_pf_get_default_rss_hena(pf);
12418 
12419 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12420 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12421 
12422 	/* Determine the RSS table size based on the hardware capabilities */
12423 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12424 	reg_val = (pf->rss_table_size == 512) ?
12425 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12426 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12427 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12428 
12429 	/* Determine the RSS size of the VSI */
12430 	if (!vsi->rss_size) {
12431 		u16 qcount;
12432 		/* If the firmware does something weird during VSI init, we
12433 		 * could end up with zero TCs. Check for that to avoid
12434 		 * divide-by-zero. It probably won't pass traffic, but it also
12435 		 * won't panic.
12436 		 */
12437 		qcount = vsi->num_queue_pairs /
12438 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12439 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12440 	}
12441 	if (!vsi->rss_size)
12442 		return -EINVAL;
12443 
12444 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12445 	if (!lut)
12446 		return -ENOMEM;
12447 
12448 	/* Use user configured lut if there is one, otherwise use default */
12449 	if (vsi->rss_lut_user)
12450 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12451 	else
12452 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12453 
12454 	/* Use user configured hash key if there is one, otherwise
12455 	 * use default.
12456 	 */
12457 	if (vsi->rss_hkey_user)
12458 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12459 	else
12460 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12461 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12462 	kfree(lut);
12463 
12464 	return ret;
12465 }
12466 
12467 /**
12468  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12469  * @pf: board private structure
12470  * @queue_count: the requested queue count for rss.
12471  *
12472  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12473  * count which may be different from the requested queue count.
12474  * Note: expects to be called while under rtnl_lock()
12475  **/
12476 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12477 {
12478 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12479 	int new_rss_size;
12480 
12481 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12482 		return 0;
12483 
12484 	queue_count = min_t(int, queue_count, num_online_cpus());
12485 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12486 
12487 	if (queue_count != vsi->num_queue_pairs) {
12488 		u16 qcount;
12489 
12490 		vsi->req_queue_pairs = queue_count;
12491 		i40e_prep_for_reset(pf);
12492 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12493 			return pf->alloc_rss_size;
12494 
12495 		pf->alloc_rss_size = new_rss_size;
12496 
12497 		i40e_reset_and_rebuild(pf, true, true);
12498 
12499 		/* Discard the user configured hash keys and lut, if less
12500 		 * queues are enabled.
12501 		 */
12502 		if (queue_count < vsi->rss_size) {
12503 			i40e_clear_rss_config_user(vsi);
12504 			dev_dbg(&pf->pdev->dev,
12505 				"discard user configured hash keys and lut\n");
12506 		}
12507 
12508 		/* Reset vsi->rss_size, as number of enabled queues changed */
12509 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12510 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12511 
12512 		i40e_pf_config_rss(pf);
12513 	}
12514 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12515 		 vsi->req_queue_pairs, pf->rss_size_max);
12516 	return pf->alloc_rss_size;
12517 }
12518 
12519 /**
12520  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12521  * @pf: board private structure
12522  **/
12523 int i40e_get_partition_bw_setting(struct i40e_pf *pf)
12524 {
12525 	bool min_valid, max_valid;
12526 	u32 max_bw, min_bw;
12527 	int status;
12528 
12529 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12530 					   &min_valid, &max_valid);
12531 
12532 	if (!status) {
12533 		if (min_valid)
12534 			pf->min_bw = min_bw;
12535 		if (max_valid)
12536 			pf->max_bw = max_bw;
12537 	}
12538 
12539 	return status;
12540 }
12541 
12542 /**
12543  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12544  * @pf: board private structure
12545  **/
12546 int i40e_set_partition_bw_setting(struct i40e_pf *pf)
12547 {
12548 	struct i40e_aqc_configure_partition_bw_data bw_data;
12549 	int status;
12550 
12551 	memset(&bw_data, 0, sizeof(bw_data));
12552 
12553 	/* Set the valid bit for this PF */
12554 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12555 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12556 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12557 
12558 	/* Set the new bandwidths */
12559 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12560 
12561 	return status;
12562 }
12563 
12564 /**
12565  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12566  * @pf: board private structure
12567  **/
12568 int i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12569 {
12570 	/* Commit temporary BW setting to permanent NVM image */
12571 	enum i40e_admin_queue_err last_aq_status;
12572 	u16 nvm_word;
12573 	int ret;
12574 
12575 	if (pf->hw.partition_id != 1) {
12576 		dev_info(&pf->pdev->dev,
12577 			 "Commit BW only works on partition 1! This is partition %d",
12578 			 pf->hw.partition_id);
12579 		ret = -EOPNOTSUPP;
12580 		goto bw_commit_out;
12581 	}
12582 
12583 	/* Acquire NVM for read access */
12584 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12585 	last_aq_status = pf->hw.aq.asq_last_status;
12586 	if (ret) {
12587 		dev_info(&pf->pdev->dev,
12588 			 "Cannot acquire NVM for read access, err %pe aq_err %s\n",
12589 			 ERR_PTR(ret),
12590 			 i40e_aq_str(&pf->hw, last_aq_status));
12591 		goto bw_commit_out;
12592 	}
12593 
12594 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12595 	ret = i40e_aq_read_nvm(&pf->hw,
12596 			       I40E_SR_NVM_CONTROL_WORD,
12597 			       0x10, sizeof(nvm_word), &nvm_word,
12598 			       false, NULL);
12599 	/* Save off last admin queue command status before releasing
12600 	 * the NVM
12601 	 */
12602 	last_aq_status = pf->hw.aq.asq_last_status;
12603 	i40e_release_nvm(&pf->hw);
12604 	if (ret) {
12605 		dev_info(&pf->pdev->dev, "NVM read error, err %pe aq_err %s\n",
12606 			 ERR_PTR(ret),
12607 			 i40e_aq_str(&pf->hw, last_aq_status));
12608 		goto bw_commit_out;
12609 	}
12610 
12611 	/* Wait a bit for NVM release to complete */
12612 	msleep(50);
12613 
12614 	/* Acquire NVM for write access */
12615 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12616 	last_aq_status = pf->hw.aq.asq_last_status;
12617 	if (ret) {
12618 		dev_info(&pf->pdev->dev,
12619 			 "Cannot acquire NVM for write access, err %pe aq_err %s\n",
12620 			 ERR_PTR(ret),
12621 			 i40e_aq_str(&pf->hw, last_aq_status));
12622 		goto bw_commit_out;
12623 	}
12624 	/* Write it back out unchanged to initiate update NVM,
12625 	 * which will force a write of the shadow (alt) RAM to
12626 	 * the NVM - thus storing the bandwidth values permanently.
12627 	 */
12628 	ret = i40e_aq_update_nvm(&pf->hw,
12629 				 I40E_SR_NVM_CONTROL_WORD,
12630 				 0x10, sizeof(nvm_word),
12631 				 &nvm_word, true, 0, NULL);
12632 	/* Save off last admin queue command status before releasing
12633 	 * the NVM
12634 	 */
12635 	last_aq_status = pf->hw.aq.asq_last_status;
12636 	i40e_release_nvm(&pf->hw);
12637 	if (ret)
12638 		dev_info(&pf->pdev->dev,
12639 			 "BW settings NOT SAVED, err %pe aq_err %s\n",
12640 			 ERR_PTR(ret),
12641 			 i40e_aq_str(&pf->hw, last_aq_status));
12642 bw_commit_out:
12643 
12644 	return ret;
12645 }
12646 
12647 /**
12648  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12649  * if total port shutdown feature is enabled for this PF
12650  * @pf: board private structure
12651  **/
12652 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12653 {
12654 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12655 #define I40E_FEATURES_ENABLE_PTR		0x2A
12656 #define I40E_CURRENT_SETTING_PTR		0x2B
12657 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12658 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12659 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12660 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12661 	u16 sr_emp_sr_settings_ptr = 0;
12662 	u16 features_enable = 0;
12663 	u16 link_behavior = 0;
12664 	int read_status = 0;
12665 	bool ret = false;
12666 
12667 	read_status = i40e_read_nvm_word(&pf->hw,
12668 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12669 					 &sr_emp_sr_settings_ptr);
12670 	if (read_status)
12671 		goto err_nvm;
12672 	read_status = i40e_read_nvm_word(&pf->hw,
12673 					 sr_emp_sr_settings_ptr +
12674 					 I40E_FEATURES_ENABLE_PTR,
12675 					 &features_enable);
12676 	if (read_status)
12677 		goto err_nvm;
12678 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12679 		read_status = i40e_read_nvm_module_data(&pf->hw,
12680 							I40E_SR_EMP_SR_SETTINGS_PTR,
12681 							I40E_CURRENT_SETTING_PTR,
12682 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12683 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12684 							&link_behavior);
12685 		if (read_status)
12686 			goto err_nvm;
12687 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12688 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12689 	}
12690 	return ret;
12691 
12692 err_nvm:
12693 	dev_warn(&pf->pdev->dev,
12694 		 "total-port-shutdown feature is off due to read nvm error: %pe\n",
12695 		 ERR_PTR(read_status));
12696 	return ret;
12697 }
12698 
12699 /**
12700  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12701  * @pf: board private structure to initialize
12702  *
12703  * i40e_sw_init initializes the Adapter private data structure.
12704  * Fields are initialized based on PCI device information and
12705  * OS network device settings (MTU size).
12706  **/
12707 static int i40e_sw_init(struct i40e_pf *pf)
12708 {
12709 	int err = 0;
12710 	int size;
12711 	u16 pow;
12712 
12713 	/* Set default capability flags */
12714 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12715 		    I40E_FLAG_MSI_ENABLED     |
12716 		    I40E_FLAG_MSIX_ENABLED;
12717 
12718 	/* Set default ITR */
12719 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12720 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12721 
12722 	/* Depending on PF configurations, it is possible that the RSS
12723 	 * maximum might end up larger than the available queues
12724 	 */
12725 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12726 	pf->alloc_rss_size = 1;
12727 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12728 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12729 				 pf->hw.func_caps.num_tx_qp);
12730 
12731 	/* find the next higher power-of-2 of num cpus */
12732 	pow = roundup_pow_of_two(num_online_cpus());
12733 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12734 
12735 	if (pf->hw.func_caps.rss) {
12736 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12737 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12738 					   num_online_cpus());
12739 	}
12740 
12741 	/* MFP mode enabled */
12742 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12743 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12744 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12745 		if (i40e_get_partition_bw_setting(pf)) {
12746 			dev_warn(&pf->pdev->dev,
12747 				 "Could not get partition bw settings\n");
12748 		} else {
12749 			dev_info(&pf->pdev->dev,
12750 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12751 				 pf->min_bw, pf->max_bw);
12752 
12753 			/* nudge the Tx scheduler */
12754 			i40e_set_partition_bw_setting(pf);
12755 		}
12756 	}
12757 
12758 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12759 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12760 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12761 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12762 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12763 		    pf->hw.num_partitions > 1)
12764 			dev_info(&pf->pdev->dev,
12765 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12766 		else
12767 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12768 		pf->fdir_pf_filter_count =
12769 				 pf->hw.func_caps.fd_filters_guaranteed;
12770 		pf->hw.fdir_shared_filter_count =
12771 				 pf->hw.func_caps.fd_filters_best_effort;
12772 	}
12773 
12774 	if (pf->hw.mac.type == I40E_MAC_X722) {
12775 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12776 				    I40E_HW_128_QP_RSS_CAPABLE |
12777 				    I40E_HW_ATR_EVICT_CAPABLE |
12778 				    I40E_HW_WB_ON_ITR_CAPABLE |
12779 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12780 				    I40E_HW_NO_PCI_LINK_CHECK |
12781 				    I40E_HW_USE_SET_LLDP_MIB |
12782 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12783 				    I40E_HW_PTP_L4_CAPABLE |
12784 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12785 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12786 
12787 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12788 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12789 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12790 			dev_warn(&pf->pdev->dev,
12791 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12792 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12793 		}
12794 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12795 		   ((pf->hw.aq.api_maj_ver == 1) &&
12796 		    (pf->hw.aq.api_min_ver > 4))) {
12797 		/* Supported in FW API version higher than 1.4 */
12798 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12799 	}
12800 
12801 	/* Enable HW ATR eviction if possible */
12802 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12803 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12804 
12805 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12806 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12807 	    (pf->hw.aq.fw_maj_ver < 4))) {
12808 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12809 		/* No DCB support  for FW < v4.33 */
12810 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12811 	}
12812 
12813 	/* Disable FW LLDP if FW < v4.3 */
12814 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12815 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12816 	    (pf->hw.aq.fw_maj_ver < 4)))
12817 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12818 
12819 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12820 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12821 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12822 	    (pf->hw.aq.fw_maj_ver >= 5)))
12823 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12824 
12825 	/* Enable PTP L4 if FW > v6.0 */
12826 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12827 	    pf->hw.aq.fw_maj_ver >= 6)
12828 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12829 
12830 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12831 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12832 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12833 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12834 	}
12835 
12836 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12837 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12838 		/* IWARP needs one extra vector for CQP just like MISC.*/
12839 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12840 	}
12841 	/* Stopping FW LLDP engine is supported on XL710 and X722
12842 	 * starting from FW versions determined in i40e_init_adminq.
12843 	 * Stopping the FW LLDP engine is not supported on XL710
12844 	 * if NPAR is functioning so unset this hw flag in this case.
12845 	 */
12846 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12847 	    pf->hw.func_caps.npar_enable &&
12848 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12849 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12850 
12851 #ifdef CONFIG_PCI_IOV
12852 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12853 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12854 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12855 		pf->num_req_vfs = min_t(int,
12856 					pf->hw.func_caps.num_vfs,
12857 					I40E_MAX_VF_COUNT);
12858 	}
12859 #endif /* CONFIG_PCI_IOV */
12860 	pf->eeprom_version = 0xDEAD;
12861 	pf->lan_veb = I40E_NO_VEB;
12862 	pf->lan_vsi = I40E_NO_VSI;
12863 
12864 	/* By default FW has this off for performance reasons */
12865 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12866 
12867 	/* set up queue assignment tracking */
12868 	size = sizeof(struct i40e_lump_tracking)
12869 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12870 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12871 	if (!pf->qp_pile) {
12872 		err = -ENOMEM;
12873 		goto sw_init_done;
12874 	}
12875 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12876 
12877 	pf->tx_timeout_recovery_level = 1;
12878 
12879 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12880 	    i40e_is_total_port_shutdown_enabled(pf)) {
12881 		/* Link down on close must be on when total port shutdown
12882 		 * is enabled for a given port
12883 		 */
12884 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12885 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12886 		dev_info(&pf->pdev->dev,
12887 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12888 	}
12889 	mutex_init(&pf->switch_mutex);
12890 
12891 sw_init_done:
12892 	return err;
12893 }
12894 
12895 /**
12896  * i40e_set_ntuple - set the ntuple feature flag and take action
12897  * @pf: board private structure to initialize
12898  * @features: the feature set that the stack is suggesting
12899  *
12900  * returns a bool to indicate if reset needs to happen
12901  **/
12902 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12903 {
12904 	bool need_reset = false;
12905 
12906 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12907 	 * the state changed, we need to reset.
12908 	 */
12909 	if (features & NETIF_F_NTUPLE) {
12910 		/* Enable filters and mark for reset */
12911 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12912 			need_reset = true;
12913 		/* enable FD_SB only if there is MSI-X vector and no cloud
12914 		 * filters exist
12915 		 */
12916 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12917 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12918 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12919 		}
12920 	} else {
12921 		/* turn off filters, mark for reset and clear SW filter list */
12922 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12923 			need_reset = true;
12924 			i40e_fdir_filter_exit(pf);
12925 		}
12926 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12927 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12928 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12929 
12930 		/* reset fd counters */
12931 		pf->fd_add_err = 0;
12932 		pf->fd_atr_cnt = 0;
12933 		/* if ATR was auto disabled it can be re-enabled. */
12934 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12935 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12936 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12937 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12938 	}
12939 	return need_reset;
12940 }
12941 
12942 /**
12943  * i40e_clear_rss_lut - clear the rx hash lookup table
12944  * @vsi: the VSI being configured
12945  **/
12946 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12947 {
12948 	struct i40e_pf *pf = vsi->back;
12949 	struct i40e_hw *hw = &pf->hw;
12950 	u16 vf_id = vsi->vf_id;
12951 	u8 i;
12952 
12953 	if (vsi->type == I40E_VSI_MAIN) {
12954 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12955 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12956 	} else if (vsi->type == I40E_VSI_SRIOV) {
12957 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12958 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12959 	} else {
12960 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12961 	}
12962 }
12963 
12964 /**
12965  * i40e_set_loopback - turn on/off loopback mode on underlying PF
12966  * @vsi: ptr to VSI
12967  * @ena: flag to indicate the on/off setting
12968  */
12969 static int i40e_set_loopback(struct i40e_vsi *vsi, bool ena)
12970 {
12971 	bool if_running = netif_running(vsi->netdev) &&
12972 			  !test_and_set_bit(__I40E_VSI_DOWN, vsi->state);
12973 	int ret;
12974 
12975 	if (if_running)
12976 		i40e_down(vsi);
12977 
12978 	ret = i40e_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
12979 	if (ret)
12980 		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
12981 	if (if_running)
12982 		i40e_up(vsi);
12983 
12984 	return ret;
12985 }
12986 
12987 /**
12988  * i40e_set_features - set the netdev feature flags
12989  * @netdev: ptr to the netdev being adjusted
12990  * @features: the feature set that the stack is suggesting
12991  * Note: expects to be called while under rtnl_lock()
12992  **/
12993 static int i40e_set_features(struct net_device *netdev,
12994 			     netdev_features_t features)
12995 {
12996 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12997 	struct i40e_vsi *vsi = np->vsi;
12998 	struct i40e_pf *pf = vsi->back;
12999 	bool need_reset;
13000 
13001 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
13002 		i40e_pf_config_rss(pf);
13003 	else if (!(features & NETIF_F_RXHASH) &&
13004 		 netdev->features & NETIF_F_RXHASH)
13005 		i40e_clear_rss_lut(vsi);
13006 
13007 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
13008 		i40e_vlan_stripping_enable(vsi);
13009 	else
13010 		i40e_vlan_stripping_disable(vsi);
13011 
13012 	if (!(features & NETIF_F_HW_TC) &&
13013 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
13014 		dev_err(&pf->pdev->dev,
13015 			"Offloaded tc filters active, can't turn hw_tc_offload off");
13016 		return -EINVAL;
13017 	}
13018 
13019 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
13020 		i40e_del_all_macvlans(vsi);
13021 
13022 	need_reset = i40e_set_ntuple(pf, features);
13023 
13024 	if (need_reset)
13025 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13026 
13027 	if ((features ^ netdev->features) & NETIF_F_LOOPBACK)
13028 		return i40e_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
13029 
13030 	return 0;
13031 }
13032 
13033 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
13034 				    unsigned int table, unsigned int idx,
13035 				    struct udp_tunnel_info *ti)
13036 {
13037 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13038 	struct i40e_hw *hw = &np->vsi->back->hw;
13039 	u8 type, filter_index;
13040 	int ret;
13041 
13042 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
13043 						   I40E_AQC_TUNNEL_TYPE_NGE;
13044 
13045 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
13046 				     NULL);
13047 	if (ret) {
13048 		netdev_info(netdev, "add UDP port failed, err %pe aq_err %s\n",
13049 			    ERR_PTR(ret),
13050 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13051 		return -EIO;
13052 	}
13053 
13054 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
13055 	return 0;
13056 }
13057 
13058 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
13059 				      unsigned int table, unsigned int idx,
13060 				      struct udp_tunnel_info *ti)
13061 {
13062 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13063 	struct i40e_hw *hw = &np->vsi->back->hw;
13064 	int ret;
13065 
13066 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
13067 	if (ret) {
13068 		netdev_info(netdev, "delete UDP port failed, err %pe aq_err %s\n",
13069 			    ERR_PTR(ret),
13070 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13071 		return -EIO;
13072 	}
13073 
13074 	return 0;
13075 }
13076 
13077 static int i40e_get_phys_port_id(struct net_device *netdev,
13078 				 struct netdev_phys_item_id *ppid)
13079 {
13080 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13081 	struct i40e_pf *pf = np->vsi->back;
13082 	struct i40e_hw *hw = &pf->hw;
13083 
13084 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
13085 		return -EOPNOTSUPP;
13086 
13087 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13088 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13089 
13090 	return 0;
13091 }
13092 
13093 /**
13094  * i40e_ndo_fdb_add - add an entry to the hardware database
13095  * @ndm: the input from the stack
13096  * @tb: pointer to array of nladdr (unused)
13097  * @dev: the net device pointer
13098  * @addr: the MAC address entry being added
13099  * @vid: VLAN ID
13100  * @flags: instructions from stack about fdb operation
13101  * @extack: netlink extended ack, unused currently
13102  */
13103 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13104 			    struct net_device *dev,
13105 			    const unsigned char *addr, u16 vid,
13106 			    u16 flags,
13107 			    struct netlink_ext_ack *extack)
13108 {
13109 	struct i40e_netdev_priv *np = netdev_priv(dev);
13110 	struct i40e_pf *pf = np->vsi->back;
13111 	int err = 0;
13112 
13113 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
13114 		return -EOPNOTSUPP;
13115 
13116 	if (vid) {
13117 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13118 		return -EINVAL;
13119 	}
13120 
13121 	/* Hardware does not support aging addresses so if a
13122 	 * ndm_state is given only allow permanent addresses
13123 	 */
13124 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13125 		netdev_info(dev, "FDB only supports static addresses\n");
13126 		return -EINVAL;
13127 	}
13128 
13129 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13130 		err = dev_uc_add_excl(dev, addr);
13131 	else if (is_multicast_ether_addr(addr))
13132 		err = dev_mc_add_excl(dev, addr);
13133 	else
13134 		err = -EINVAL;
13135 
13136 	/* Only return duplicate errors if NLM_F_EXCL is set */
13137 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13138 		err = 0;
13139 
13140 	return err;
13141 }
13142 
13143 /**
13144  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13145  * @dev: the netdev being configured
13146  * @nlh: RTNL message
13147  * @flags: bridge flags
13148  * @extack: netlink extended ack
13149  *
13150  * Inserts a new hardware bridge if not already created and
13151  * enables the bridging mode requested (VEB or VEPA). If the
13152  * hardware bridge has already been inserted and the request
13153  * is to change the mode then that requires a PF reset to
13154  * allow rebuild of the components with required hardware
13155  * bridge mode enabled.
13156  *
13157  * Note: expects to be called while under rtnl_lock()
13158  **/
13159 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13160 				   struct nlmsghdr *nlh,
13161 				   u16 flags,
13162 				   struct netlink_ext_ack *extack)
13163 {
13164 	struct i40e_netdev_priv *np = netdev_priv(dev);
13165 	struct i40e_vsi *vsi = np->vsi;
13166 	struct i40e_pf *pf = vsi->back;
13167 	struct i40e_veb *veb = NULL;
13168 	struct nlattr *attr, *br_spec;
13169 	int i, rem;
13170 
13171 	/* Only for PF VSI for now */
13172 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13173 		return -EOPNOTSUPP;
13174 
13175 	/* Find the HW bridge for PF VSI */
13176 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13177 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13178 			veb = pf->veb[i];
13179 	}
13180 
13181 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13182 	if (!br_spec)
13183 		return -EINVAL;
13184 
13185 	nla_for_each_nested(attr, br_spec, rem) {
13186 		__u16 mode;
13187 
13188 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
13189 			continue;
13190 
13191 		mode = nla_get_u16(attr);
13192 		if ((mode != BRIDGE_MODE_VEPA) &&
13193 		    (mode != BRIDGE_MODE_VEB))
13194 			return -EINVAL;
13195 
13196 		/* Insert a new HW bridge */
13197 		if (!veb) {
13198 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13199 					     vsi->tc_config.enabled_tc);
13200 			if (veb) {
13201 				veb->bridge_mode = mode;
13202 				i40e_config_bridge_mode(veb);
13203 			} else {
13204 				/* No Bridge HW offload available */
13205 				return -ENOENT;
13206 			}
13207 			break;
13208 		} else if (mode != veb->bridge_mode) {
13209 			/* Existing HW bridge but different mode needs reset */
13210 			veb->bridge_mode = mode;
13211 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13212 			if (mode == BRIDGE_MODE_VEB)
13213 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13214 			else
13215 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13216 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13217 			break;
13218 		}
13219 	}
13220 
13221 	return 0;
13222 }
13223 
13224 /**
13225  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13226  * @skb: skb buff
13227  * @pid: process id
13228  * @seq: RTNL message seq #
13229  * @dev: the netdev being configured
13230  * @filter_mask: unused
13231  * @nlflags: netlink flags passed in
13232  *
13233  * Return the mode in which the hardware bridge is operating in
13234  * i.e VEB or VEPA.
13235  **/
13236 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13237 				   struct net_device *dev,
13238 				   u32 __always_unused filter_mask,
13239 				   int nlflags)
13240 {
13241 	struct i40e_netdev_priv *np = netdev_priv(dev);
13242 	struct i40e_vsi *vsi = np->vsi;
13243 	struct i40e_pf *pf = vsi->back;
13244 	struct i40e_veb *veb = NULL;
13245 	int i;
13246 
13247 	/* Only for PF VSI for now */
13248 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13249 		return -EOPNOTSUPP;
13250 
13251 	/* Find the HW bridge for the PF VSI */
13252 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13253 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13254 			veb = pf->veb[i];
13255 	}
13256 
13257 	if (!veb)
13258 		return 0;
13259 
13260 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13261 				       0, 0, nlflags, filter_mask, NULL);
13262 }
13263 
13264 /**
13265  * i40e_features_check - Validate encapsulated packet conforms to limits
13266  * @skb: skb buff
13267  * @dev: This physical port's netdev
13268  * @features: Offload features that the stack believes apply
13269  **/
13270 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13271 					     struct net_device *dev,
13272 					     netdev_features_t features)
13273 {
13274 	size_t len;
13275 
13276 	/* No point in doing any of this if neither checksum nor GSO are
13277 	 * being requested for this frame.  We can rule out both by just
13278 	 * checking for CHECKSUM_PARTIAL
13279 	 */
13280 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13281 		return features;
13282 
13283 	/* We cannot support GSO if the MSS is going to be less than
13284 	 * 64 bytes.  If it is then we need to drop support for GSO.
13285 	 */
13286 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13287 		features &= ~NETIF_F_GSO_MASK;
13288 
13289 	/* MACLEN can support at most 63 words */
13290 	len = skb_network_header(skb) - skb->data;
13291 	if (len & ~(63 * 2))
13292 		goto out_err;
13293 
13294 	/* IPLEN and EIPLEN can support at most 127 dwords */
13295 	len = skb_transport_header(skb) - skb_network_header(skb);
13296 	if (len & ~(127 * 4))
13297 		goto out_err;
13298 
13299 	if (skb->encapsulation) {
13300 		/* L4TUNLEN can support 127 words */
13301 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13302 		if (len & ~(127 * 2))
13303 			goto out_err;
13304 
13305 		/* IPLEN can support at most 127 dwords */
13306 		len = skb_inner_transport_header(skb) -
13307 		      skb_inner_network_header(skb);
13308 		if (len & ~(127 * 4))
13309 			goto out_err;
13310 	}
13311 
13312 	/* No need to validate L4LEN as TCP is the only protocol with a
13313 	 * flexible value and we support all possible values supported
13314 	 * by TCP, which is at most 15 dwords
13315 	 */
13316 
13317 	return features;
13318 out_err:
13319 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13320 }
13321 
13322 /**
13323  * i40e_xdp_setup - add/remove an XDP program
13324  * @vsi: VSI to changed
13325  * @prog: XDP program
13326  * @extack: netlink extended ack
13327  **/
13328 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13329 			  struct netlink_ext_ack *extack)
13330 {
13331 	int frame_size = i40e_max_vsi_frame_size(vsi, prog);
13332 	struct i40e_pf *pf = vsi->back;
13333 	struct bpf_prog *old_prog;
13334 	bool need_reset;
13335 	int i;
13336 
13337 	/* Don't allow frames that span over multiple buffers */
13338 	if (vsi->netdev->mtu > frame_size - I40E_PACKET_HDR_PAD) {
13339 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for linear frames and XDP prog does not support frags");
13340 		return -EINVAL;
13341 	}
13342 
13343 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13344 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13345 
13346 	if (need_reset)
13347 		i40e_prep_for_reset(pf);
13348 
13349 	/* VSI shall be deleted in a moment, just return EINVAL */
13350 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13351 		return -EINVAL;
13352 
13353 	old_prog = xchg(&vsi->xdp_prog, prog);
13354 
13355 	if (need_reset) {
13356 		if (!prog) {
13357 			xdp_features_clear_redirect_target(vsi->netdev);
13358 			/* Wait until ndo_xsk_wakeup completes. */
13359 			synchronize_rcu();
13360 		}
13361 		i40e_reset_and_rebuild(pf, true, true);
13362 	}
13363 
13364 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13365 		if (i40e_realloc_rx_bi_zc(vsi, true))
13366 			return -ENOMEM;
13367 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13368 		if (i40e_realloc_rx_bi_zc(vsi, false))
13369 			return -ENOMEM;
13370 	}
13371 
13372 	for (i = 0; i < vsi->num_queue_pairs; i++)
13373 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13374 
13375 	if (old_prog)
13376 		bpf_prog_put(old_prog);
13377 
13378 	/* Kick start the NAPI context if there is an AF_XDP socket open
13379 	 * on that queue id. This so that receiving will start.
13380 	 */
13381 	if (need_reset && prog) {
13382 		for (i = 0; i < vsi->num_queue_pairs; i++)
13383 			if (vsi->xdp_rings[i]->xsk_pool)
13384 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13385 						      XDP_WAKEUP_RX);
13386 		xdp_features_set_redirect_target(vsi->netdev, true);
13387 	}
13388 
13389 	return 0;
13390 }
13391 
13392 /**
13393  * i40e_enter_busy_conf - Enters busy config state
13394  * @vsi: vsi
13395  *
13396  * Returns 0 on success, <0 for failure.
13397  **/
13398 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13399 {
13400 	struct i40e_pf *pf = vsi->back;
13401 	int timeout = 50;
13402 
13403 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13404 		timeout--;
13405 		if (!timeout)
13406 			return -EBUSY;
13407 		usleep_range(1000, 2000);
13408 	}
13409 
13410 	return 0;
13411 }
13412 
13413 /**
13414  * i40e_exit_busy_conf - Exits busy config state
13415  * @vsi: vsi
13416  **/
13417 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13418 {
13419 	struct i40e_pf *pf = vsi->back;
13420 
13421 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13422 }
13423 
13424 /**
13425  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13426  * @vsi: vsi
13427  * @queue_pair: queue pair
13428  **/
13429 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13430 {
13431 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13432 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13433 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13434 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13435 	if (i40e_enabled_xdp_vsi(vsi)) {
13436 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13437 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13438 	}
13439 }
13440 
13441 /**
13442  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13443  * @vsi: vsi
13444  * @queue_pair: queue pair
13445  **/
13446 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13447 {
13448 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13449 	if (i40e_enabled_xdp_vsi(vsi)) {
13450 		/* Make sure that in-progress ndo_xdp_xmit calls are
13451 		 * completed.
13452 		 */
13453 		synchronize_rcu();
13454 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13455 	}
13456 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13457 }
13458 
13459 /**
13460  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13461  * @vsi: vsi
13462  * @queue_pair: queue pair
13463  * @enable: true for enable, false for disable
13464  **/
13465 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13466 					bool enable)
13467 {
13468 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13469 	struct i40e_q_vector *q_vector = rxr->q_vector;
13470 
13471 	if (!vsi->netdev)
13472 		return;
13473 
13474 	/* All rings in a qp belong to the same qvector. */
13475 	if (q_vector->rx.ring || q_vector->tx.ring) {
13476 		if (enable)
13477 			napi_enable(&q_vector->napi);
13478 		else
13479 			napi_disable(&q_vector->napi);
13480 	}
13481 }
13482 
13483 /**
13484  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13485  * @vsi: vsi
13486  * @queue_pair: queue pair
13487  * @enable: true for enable, false for disable
13488  *
13489  * Returns 0 on success, <0 on failure.
13490  **/
13491 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13492 					bool enable)
13493 {
13494 	struct i40e_pf *pf = vsi->back;
13495 	int pf_q, ret = 0;
13496 
13497 	pf_q = vsi->base_queue + queue_pair;
13498 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13499 				     false /*is xdp*/, enable);
13500 	if (ret) {
13501 		dev_info(&pf->pdev->dev,
13502 			 "VSI seid %d Tx ring %d %sable timeout\n",
13503 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13504 		return ret;
13505 	}
13506 
13507 	i40e_control_rx_q(pf, pf_q, enable);
13508 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13509 	if (ret) {
13510 		dev_info(&pf->pdev->dev,
13511 			 "VSI seid %d Rx ring %d %sable timeout\n",
13512 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13513 		return ret;
13514 	}
13515 
13516 	/* Due to HW errata, on Rx disable only, the register can
13517 	 * indicate done before it really is. Needs 50ms to be sure
13518 	 */
13519 	if (!enable)
13520 		mdelay(50);
13521 
13522 	if (!i40e_enabled_xdp_vsi(vsi))
13523 		return ret;
13524 
13525 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13526 				     pf_q + vsi->alloc_queue_pairs,
13527 				     true /*is xdp*/, enable);
13528 	if (ret) {
13529 		dev_info(&pf->pdev->dev,
13530 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13531 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13532 	}
13533 
13534 	return ret;
13535 }
13536 
13537 /**
13538  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13539  * @vsi: vsi
13540  * @queue_pair: queue_pair
13541  **/
13542 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13543 {
13544 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13545 	struct i40e_pf *pf = vsi->back;
13546 	struct i40e_hw *hw = &pf->hw;
13547 
13548 	/* All rings in a qp belong to the same qvector. */
13549 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13550 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13551 	else
13552 		i40e_irq_dynamic_enable_icr0(pf);
13553 
13554 	i40e_flush(hw);
13555 }
13556 
13557 /**
13558  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13559  * @vsi: vsi
13560  * @queue_pair: queue_pair
13561  **/
13562 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13563 {
13564 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13565 	struct i40e_pf *pf = vsi->back;
13566 	struct i40e_hw *hw = &pf->hw;
13567 
13568 	/* For simplicity, instead of removing the qp interrupt causes
13569 	 * from the interrupt linked list, we simply disable the interrupt, and
13570 	 * leave the list intact.
13571 	 *
13572 	 * All rings in a qp belong to the same qvector.
13573 	 */
13574 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13575 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13576 
13577 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13578 		i40e_flush(hw);
13579 		synchronize_irq(pf->msix_entries[intpf].vector);
13580 	} else {
13581 		/* Legacy and MSI mode - this stops all interrupt handling */
13582 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13583 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13584 		i40e_flush(hw);
13585 		synchronize_irq(pf->pdev->irq);
13586 	}
13587 }
13588 
13589 /**
13590  * i40e_queue_pair_disable - Disables a queue pair
13591  * @vsi: vsi
13592  * @queue_pair: queue pair
13593  *
13594  * Returns 0 on success, <0 on failure.
13595  **/
13596 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13597 {
13598 	int err;
13599 
13600 	err = i40e_enter_busy_conf(vsi);
13601 	if (err)
13602 		return err;
13603 
13604 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13605 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13606 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13607 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13608 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13609 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13610 
13611 	return err;
13612 }
13613 
13614 /**
13615  * i40e_queue_pair_enable - Enables a queue pair
13616  * @vsi: vsi
13617  * @queue_pair: queue pair
13618  *
13619  * Returns 0 on success, <0 on failure.
13620  **/
13621 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13622 {
13623 	int err;
13624 
13625 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13626 	if (err)
13627 		return err;
13628 
13629 	if (i40e_enabled_xdp_vsi(vsi)) {
13630 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13631 		if (err)
13632 			return err;
13633 	}
13634 
13635 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13636 	if (err)
13637 		return err;
13638 
13639 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13640 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13641 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13642 
13643 	i40e_exit_busy_conf(vsi);
13644 
13645 	return err;
13646 }
13647 
13648 /**
13649  * i40e_xdp - implements ndo_bpf for i40e
13650  * @dev: netdevice
13651  * @xdp: XDP command
13652  **/
13653 static int i40e_xdp(struct net_device *dev,
13654 		    struct netdev_bpf *xdp)
13655 {
13656 	struct i40e_netdev_priv *np = netdev_priv(dev);
13657 	struct i40e_vsi *vsi = np->vsi;
13658 
13659 	if (vsi->type != I40E_VSI_MAIN)
13660 		return -EINVAL;
13661 
13662 	switch (xdp->command) {
13663 	case XDP_SETUP_PROG:
13664 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13665 	case XDP_SETUP_XSK_POOL:
13666 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13667 					   xdp->xsk.queue_id);
13668 	default:
13669 		return -EINVAL;
13670 	}
13671 }
13672 
13673 static const struct net_device_ops i40e_netdev_ops = {
13674 	.ndo_open		= i40e_open,
13675 	.ndo_stop		= i40e_close,
13676 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13677 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13678 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13679 	.ndo_validate_addr	= eth_validate_addr,
13680 	.ndo_set_mac_address	= i40e_set_mac,
13681 	.ndo_change_mtu		= i40e_change_mtu,
13682 	.ndo_eth_ioctl		= i40e_ioctl,
13683 	.ndo_tx_timeout		= i40e_tx_timeout,
13684 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13685 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13686 #ifdef CONFIG_NET_POLL_CONTROLLER
13687 	.ndo_poll_controller	= i40e_netpoll,
13688 #endif
13689 	.ndo_setup_tc		= __i40e_setup_tc,
13690 	.ndo_select_queue	= i40e_lan_select_queue,
13691 	.ndo_set_features	= i40e_set_features,
13692 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13693 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13694 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13695 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13696 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13697 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13698 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13699 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13700 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13701 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13702 	.ndo_features_check	= i40e_features_check,
13703 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13704 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13705 	.ndo_bpf		= i40e_xdp,
13706 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13707 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13708 	.ndo_dfwd_add_station	= i40e_fwd_add,
13709 	.ndo_dfwd_del_station	= i40e_fwd_del,
13710 };
13711 
13712 /**
13713  * i40e_config_netdev - Setup the netdev flags
13714  * @vsi: the VSI being configured
13715  *
13716  * Returns 0 on success, negative value on failure
13717  **/
13718 static int i40e_config_netdev(struct i40e_vsi *vsi)
13719 {
13720 	struct i40e_pf *pf = vsi->back;
13721 	struct i40e_hw *hw = &pf->hw;
13722 	struct i40e_netdev_priv *np;
13723 	struct net_device *netdev;
13724 	u8 broadcast[ETH_ALEN];
13725 	u8 mac_addr[ETH_ALEN];
13726 	int etherdev_size;
13727 	netdev_features_t hw_enc_features;
13728 	netdev_features_t hw_features;
13729 
13730 	etherdev_size = sizeof(struct i40e_netdev_priv);
13731 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13732 	if (!netdev)
13733 		return -ENOMEM;
13734 
13735 	vsi->netdev = netdev;
13736 	np = netdev_priv(netdev);
13737 	np->vsi = vsi;
13738 
13739 	hw_enc_features = NETIF_F_SG			|
13740 			  NETIF_F_HW_CSUM		|
13741 			  NETIF_F_HIGHDMA		|
13742 			  NETIF_F_SOFT_FEATURES		|
13743 			  NETIF_F_TSO			|
13744 			  NETIF_F_TSO_ECN		|
13745 			  NETIF_F_TSO6			|
13746 			  NETIF_F_GSO_GRE		|
13747 			  NETIF_F_GSO_GRE_CSUM		|
13748 			  NETIF_F_GSO_PARTIAL		|
13749 			  NETIF_F_GSO_IPXIP4		|
13750 			  NETIF_F_GSO_IPXIP6		|
13751 			  NETIF_F_GSO_UDP_TUNNEL	|
13752 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13753 			  NETIF_F_GSO_UDP_L4		|
13754 			  NETIF_F_SCTP_CRC		|
13755 			  NETIF_F_RXHASH		|
13756 			  NETIF_F_RXCSUM		|
13757 			  0;
13758 
13759 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13760 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13761 
13762 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13763 
13764 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13765 
13766 	netdev->hw_enc_features |= hw_enc_features;
13767 
13768 	/* record features VLANs can make use of */
13769 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13770 
13771 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13772 				   NETIF_F_GSO_GRE_CSUM |	\
13773 				   NETIF_F_GSO_IPXIP4 |		\
13774 				   NETIF_F_GSO_IPXIP6 |		\
13775 				   NETIF_F_GSO_UDP_TUNNEL |	\
13776 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13777 
13778 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13779 	netdev->features |= NETIF_F_GSO_PARTIAL |
13780 			    I40E_GSO_PARTIAL_FEATURES;
13781 
13782 	netdev->mpls_features |= NETIF_F_SG;
13783 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13784 	netdev->mpls_features |= NETIF_F_TSO;
13785 	netdev->mpls_features |= NETIF_F_TSO6;
13786 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13787 
13788 	/* enable macvlan offloads */
13789 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13790 
13791 	hw_features = hw_enc_features		|
13792 		      NETIF_F_HW_VLAN_CTAG_TX	|
13793 		      NETIF_F_HW_VLAN_CTAG_RX;
13794 
13795 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13796 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13797 
13798 	netdev->hw_features |= hw_features | NETIF_F_LOOPBACK;
13799 
13800 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13801 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13802 
13803 	netdev->features &= ~NETIF_F_HW_TC;
13804 
13805 	if (vsi->type == I40E_VSI_MAIN) {
13806 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13807 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13808 		/* The following steps are necessary for two reasons. First,
13809 		 * some older NVM configurations load a default MAC-VLAN
13810 		 * filter that will accept any tagged packet, and we want to
13811 		 * replace this with a normal filter. Additionally, it is
13812 		 * possible our MAC address was provided by the platform using
13813 		 * Open Firmware or similar.
13814 		 *
13815 		 * Thus, we need to remove the default filter and install one
13816 		 * specific to the MAC address.
13817 		 */
13818 		i40e_rm_default_mac_filter(vsi, mac_addr);
13819 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13820 		i40e_add_mac_filter(vsi, mac_addr);
13821 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13822 
13823 		netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
13824 				       NETDEV_XDP_ACT_REDIRECT |
13825 				       NETDEV_XDP_ACT_XSK_ZEROCOPY |
13826 				       NETDEV_XDP_ACT_RX_SG;
13827 		netdev->xdp_zc_max_segs = I40E_MAX_BUFFER_TXD;
13828 	} else {
13829 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13830 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13831 		 * the end, which is 4 bytes long, so force truncation of the
13832 		 * original name by IFNAMSIZ - 4
13833 		 */
13834 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13835 			 IFNAMSIZ - 4,
13836 			 pf->vsi[pf->lan_vsi]->netdev->name);
13837 		eth_random_addr(mac_addr);
13838 
13839 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13840 		i40e_add_mac_filter(vsi, mac_addr);
13841 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13842 	}
13843 
13844 	/* Add the broadcast filter so that we initially will receive
13845 	 * broadcast packets. Note that when a new VLAN is first added the
13846 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13847 	 * specific filters as part of transitioning into "vlan" operation.
13848 	 * When more VLANs are added, the driver will copy each existing MAC
13849 	 * filter and add it for the new VLAN.
13850 	 *
13851 	 * Broadcast filters are handled specially by
13852 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13853 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13854 	 * filter. The subtask will update the correct broadcast promiscuous
13855 	 * bits as VLANs become active or inactive.
13856 	 */
13857 	eth_broadcast_addr(broadcast);
13858 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13859 	i40e_add_mac_filter(vsi, broadcast);
13860 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13861 
13862 	eth_hw_addr_set(netdev, mac_addr);
13863 	ether_addr_copy(netdev->perm_addr, mac_addr);
13864 
13865 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13866 	netdev->neigh_priv_len = sizeof(u32) * 4;
13867 
13868 	netdev->priv_flags |= IFF_UNICAST_FLT;
13869 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13870 	/* Setup netdev TC information */
13871 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13872 
13873 	netdev->netdev_ops = &i40e_netdev_ops;
13874 	netdev->watchdog_timeo = 5 * HZ;
13875 	i40e_set_ethtool_ops(netdev);
13876 
13877 	/* MTU range: 68 - 9706 */
13878 	netdev->min_mtu = ETH_MIN_MTU;
13879 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13880 
13881 	return 0;
13882 }
13883 
13884 /**
13885  * i40e_vsi_delete - Delete a VSI from the switch
13886  * @vsi: the VSI being removed
13887  *
13888  * Returns 0 on success, negative value on failure
13889  **/
13890 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13891 {
13892 	/* remove default VSI is not allowed */
13893 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13894 		return;
13895 
13896 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13897 }
13898 
13899 /**
13900  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13901  * @vsi: the VSI being queried
13902  *
13903  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13904  **/
13905 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13906 {
13907 	struct i40e_veb *veb;
13908 	struct i40e_pf *pf = vsi->back;
13909 
13910 	/* Uplink is not a bridge so default to VEB */
13911 	if (vsi->veb_idx >= I40E_MAX_VEB)
13912 		return 1;
13913 
13914 	veb = pf->veb[vsi->veb_idx];
13915 	if (!veb) {
13916 		dev_info(&pf->pdev->dev,
13917 			 "There is no veb associated with the bridge\n");
13918 		return -ENOENT;
13919 	}
13920 
13921 	/* Uplink is a bridge in VEPA mode */
13922 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13923 		return 0;
13924 	} else {
13925 		/* Uplink is a bridge in VEB mode */
13926 		return 1;
13927 	}
13928 
13929 	/* VEPA is now default bridge, so return 0 */
13930 	return 0;
13931 }
13932 
13933 /**
13934  * i40e_add_vsi - Add a VSI to the switch
13935  * @vsi: the VSI being configured
13936  *
13937  * This initializes a VSI context depending on the VSI type to be added and
13938  * passes it down to the add_vsi aq command.
13939  **/
13940 static int i40e_add_vsi(struct i40e_vsi *vsi)
13941 {
13942 	int ret = -ENODEV;
13943 	struct i40e_pf *pf = vsi->back;
13944 	struct i40e_hw *hw = &pf->hw;
13945 	struct i40e_vsi_context ctxt;
13946 	struct i40e_mac_filter *f;
13947 	struct hlist_node *h;
13948 	int bkt;
13949 
13950 	u8 enabled_tc = 0x1; /* TC0 enabled */
13951 	int f_count = 0;
13952 
13953 	memset(&ctxt, 0, sizeof(ctxt));
13954 	switch (vsi->type) {
13955 	case I40E_VSI_MAIN:
13956 		/* The PF's main VSI is already setup as part of the
13957 		 * device initialization, so we'll not bother with
13958 		 * the add_vsi call, but we will retrieve the current
13959 		 * VSI context.
13960 		 */
13961 		ctxt.seid = pf->main_vsi_seid;
13962 		ctxt.pf_num = pf->hw.pf_id;
13963 		ctxt.vf_num = 0;
13964 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13965 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13966 		if (ret) {
13967 			dev_info(&pf->pdev->dev,
13968 				 "couldn't get PF vsi config, err %pe aq_err %s\n",
13969 				 ERR_PTR(ret),
13970 				 i40e_aq_str(&pf->hw,
13971 					     pf->hw.aq.asq_last_status));
13972 			return -ENOENT;
13973 		}
13974 		vsi->info = ctxt.info;
13975 		vsi->info.valid_sections = 0;
13976 
13977 		vsi->seid = ctxt.seid;
13978 		vsi->id = ctxt.vsi_number;
13979 
13980 		enabled_tc = i40e_pf_get_tc_map(pf);
13981 
13982 		/* Source pruning is enabled by default, so the flag is
13983 		 * negative logic - if it's set, we need to fiddle with
13984 		 * the VSI to disable source pruning.
13985 		 */
13986 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13987 			memset(&ctxt, 0, sizeof(ctxt));
13988 			ctxt.seid = pf->main_vsi_seid;
13989 			ctxt.pf_num = pf->hw.pf_id;
13990 			ctxt.vf_num = 0;
13991 			ctxt.info.valid_sections |=
13992 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13993 			ctxt.info.switch_id =
13994 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13995 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13996 			if (ret) {
13997 				dev_info(&pf->pdev->dev,
13998 					 "update vsi failed, err %d aq_err %s\n",
13999 					 ret,
14000 					 i40e_aq_str(&pf->hw,
14001 						     pf->hw.aq.asq_last_status));
14002 				ret = -ENOENT;
14003 				goto err;
14004 			}
14005 		}
14006 
14007 		/* MFP mode setup queue map and update VSI */
14008 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
14009 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
14010 			memset(&ctxt, 0, sizeof(ctxt));
14011 			ctxt.seid = pf->main_vsi_seid;
14012 			ctxt.pf_num = pf->hw.pf_id;
14013 			ctxt.vf_num = 0;
14014 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
14015 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
14016 			if (ret) {
14017 				dev_info(&pf->pdev->dev,
14018 					 "update vsi failed, err %pe aq_err %s\n",
14019 					 ERR_PTR(ret),
14020 					 i40e_aq_str(&pf->hw,
14021 						    pf->hw.aq.asq_last_status));
14022 				ret = -ENOENT;
14023 				goto err;
14024 			}
14025 			/* update the local VSI info queue map */
14026 			i40e_vsi_update_queue_map(vsi, &ctxt);
14027 			vsi->info.valid_sections = 0;
14028 		} else {
14029 			/* Default/Main VSI is only enabled for TC0
14030 			 * reconfigure it to enable all TCs that are
14031 			 * available on the port in SFP mode.
14032 			 * For MFP case the iSCSI PF would use this
14033 			 * flow to enable LAN+iSCSI TC.
14034 			 */
14035 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
14036 			if (ret) {
14037 				/* Single TC condition is not fatal,
14038 				 * message and continue
14039 				 */
14040 				dev_info(&pf->pdev->dev,
14041 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %pe aq_err %s\n",
14042 					 enabled_tc,
14043 					 ERR_PTR(ret),
14044 					 i40e_aq_str(&pf->hw,
14045 						    pf->hw.aq.asq_last_status));
14046 			}
14047 		}
14048 		break;
14049 
14050 	case I40E_VSI_FDIR:
14051 		ctxt.pf_num = hw->pf_id;
14052 		ctxt.vf_num = 0;
14053 		ctxt.uplink_seid = vsi->uplink_seid;
14054 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14055 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
14056 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
14057 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
14058 			ctxt.info.valid_sections |=
14059 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14060 			ctxt.info.switch_id =
14061 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14062 		}
14063 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14064 		break;
14065 
14066 	case I40E_VSI_VMDQ2:
14067 		ctxt.pf_num = hw->pf_id;
14068 		ctxt.vf_num = 0;
14069 		ctxt.uplink_seid = vsi->uplink_seid;
14070 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14071 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
14072 
14073 		/* This VSI is connected to VEB so the switch_id
14074 		 * should be set to zero by default.
14075 		 */
14076 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14077 			ctxt.info.valid_sections |=
14078 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14079 			ctxt.info.switch_id =
14080 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14081 		}
14082 
14083 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14084 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14085 		break;
14086 
14087 	case I40E_VSI_SRIOV:
14088 		ctxt.pf_num = hw->pf_id;
14089 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
14090 		ctxt.uplink_seid = vsi->uplink_seid;
14091 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14092 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
14093 
14094 		/* This VSI is connected to VEB so the switch_id
14095 		 * should be set to zero by default.
14096 		 */
14097 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14098 			ctxt.info.valid_sections |=
14099 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14100 			ctxt.info.switch_id =
14101 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14102 		}
14103 
14104 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
14105 			ctxt.info.valid_sections |=
14106 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14107 			ctxt.info.queueing_opt_flags |=
14108 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14109 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14110 		}
14111 
14112 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14113 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14114 		if (pf->vf[vsi->vf_id].spoofchk) {
14115 			ctxt.info.valid_sections |=
14116 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14117 			ctxt.info.sec_flags |=
14118 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14119 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14120 		}
14121 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14122 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14123 		break;
14124 
14125 	case I40E_VSI_IWARP:
14126 		/* send down message to iWARP */
14127 		break;
14128 
14129 	default:
14130 		return -ENODEV;
14131 	}
14132 
14133 	if (vsi->type != I40E_VSI_MAIN) {
14134 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14135 		if (ret) {
14136 			dev_info(&vsi->back->pdev->dev,
14137 				 "add vsi failed, err %pe aq_err %s\n",
14138 				 ERR_PTR(ret),
14139 				 i40e_aq_str(&pf->hw,
14140 					     pf->hw.aq.asq_last_status));
14141 			ret = -ENOENT;
14142 			goto err;
14143 		}
14144 		vsi->info = ctxt.info;
14145 		vsi->info.valid_sections = 0;
14146 		vsi->seid = ctxt.seid;
14147 		vsi->id = ctxt.vsi_number;
14148 	}
14149 
14150 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14151 	vsi->active_filters = 0;
14152 	/* If macvlan filters already exist, force them to get loaded */
14153 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14154 		f->state = I40E_FILTER_NEW;
14155 		f_count++;
14156 	}
14157 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14158 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14159 
14160 	if (f_count) {
14161 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14162 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14163 	}
14164 
14165 	/* Update VSI BW information */
14166 	ret = i40e_vsi_get_bw_info(vsi);
14167 	if (ret) {
14168 		dev_info(&pf->pdev->dev,
14169 			 "couldn't get vsi bw info, err %pe aq_err %s\n",
14170 			 ERR_PTR(ret),
14171 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14172 		/* VSI is already added so not tearing that up */
14173 		ret = 0;
14174 	}
14175 
14176 err:
14177 	return ret;
14178 }
14179 
14180 /**
14181  * i40e_vsi_release - Delete a VSI and free its resources
14182  * @vsi: the VSI being removed
14183  *
14184  * Returns 0 on success or < 0 on error
14185  **/
14186 int i40e_vsi_release(struct i40e_vsi *vsi)
14187 {
14188 	struct i40e_mac_filter *f;
14189 	struct hlist_node *h;
14190 	struct i40e_veb *veb = NULL;
14191 	struct i40e_pf *pf;
14192 	u16 uplink_seid;
14193 	int i, n, bkt;
14194 
14195 	pf = vsi->back;
14196 
14197 	/* release of a VEB-owner or last VSI is not allowed */
14198 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14199 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14200 			 vsi->seid, vsi->uplink_seid);
14201 		return -ENODEV;
14202 	}
14203 	if (vsi == pf->vsi[pf->lan_vsi] &&
14204 	    !test_bit(__I40E_DOWN, pf->state)) {
14205 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14206 		return -ENODEV;
14207 	}
14208 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14209 	uplink_seid = vsi->uplink_seid;
14210 	if (vsi->type != I40E_VSI_SRIOV) {
14211 		if (vsi->netdev_registered) {
14212 			vsi->netdev_registered = false;
14213 			if (vsi->netdev) {
14214 				/* results in a call to i40e_close() */
14215 				unregister_netdev(vsi->netdev);
14216 			}
14217 		} else {
14218 			i40e_vsi_close(vsi);
14219 		}
14220 		i40e_vsi_disable_irq(vsi);
14221 	}
14222 
14223 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14224 
14225 	/* clear the sync flag on all filters */
14226 	if (vsi->netdev) {
14227 		__dev_uc_unsync(vsi->netdev, NULL);
14228 		__dev_mc_unsync(vsi->netdev, NULL);
14229 	}
14230 
14231 	/* make sure any remaining filters are marked for deletion */
14232 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14233 		__i40e_del_filter(vsi, f);
14234 
14235 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14236 
14237 	i40e_sync_vsi_filters(vsi);
14238 
14239 	i40e_vsi_delete(vsi);
14240 	i40e_vsi_free_q_vectors(vsi);
14241 	if (vsi->netdev) {
14242 		free_netdev(vsi->netdev);
14243 		vsi->netdev = NULL;
14244 	}
14245 	i40e_vsi_clear_rings(vsi);
14246 	i40e_vsi_clear(vsi);
14247 
14248 	/* If this was the last thing on the VEB, except for the
14249 	 * controlling VSI, remove the VEB, which puts the controlling
14250 	 * VSI onto the next level down in the switch.
14251 	 *
14252 	 * Well, okay, there's one more exception here: don't remove
14253 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
14254 	 * from up the network stack.
14255 	 */
14256 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
14257 		if (pf->vsi[i] &&
14258 		    pf->vsi[i]->uplink_seid == uplink_seid &&
14259 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14260 			n++;      /* count the VSIs */
14261 		}
14262 	}
14263 	for (i = 0; i < I40E_MAX_VEB; i++) {
14264 		if (!pf->veb[i])
14265 			continue;
14266 		if (pf->veb[i]->uplink_seid == uplink_seid)
14267 			n++;     /* count the VEBs */
14268 		if (pf->veb[i]->seid == uplink_seid)
14269 			veb = pf->veb[i];
14270 	}
14271 	if (n == 0 && veb && veb->uplink_seid != 0)
14272 		i40e_veb_release(veb);
14273 
14274 	return 0;
14275 }
14276 
14277 /**
14278  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14279  * @vsi: ptr to the VSI
14280  *
14281  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14282  * corresponding SW VSI structure and initializes num_queue_pairs for the
14283  * newly allocated VSI.
14284  *
14285  * Returns 0 on success or negative on failure
14286  **/
14287 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14288 {
14289 	int ret = -ENOENT;
14290 	struct i40e_pf *pf = vsi->back;
14291 
14292 	if (vsi->q_vectors[0]) {
14293 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14294 			 vsi->seid);
14295 		return -EEXIST;
14296 	}
14297 
14298 	if (vsi->base_vector) {
14299 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14300 			 vsi->seid, vsi->base_vector);
14301 		return -EEXIST;
14302 	}
14303 
14304 	ret = i40e_vsi_alloc_q_vectors(vsi);
14305 	if (ret) {
14306 		dev_info(&pf->pdev->dev,
14307 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14308 			 vsi->num_q_vectors, vsi->seid, ret);
14309 		vsi->num_q_vectors = 0;
14310 		goto vector_setup_out;
14311 	}
14312 
14313 	/* In Legacy mode, we do not have to get any other vector since we
14314 	 * piggyback on the misc/ICR0 for queue interrupts.
14315 	*/
14316 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
14317 		return ret;
14318 	if (vsi->num_q_vectors)
14319 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14320 						 vsi->num_q_vectors, vsi->idx);
14321 	if (vsi->base_vector < 0) {
14322 		dev_info(&pf->pdev->dev,
14323 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14324 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14325 		i40e_vsi_free_q_vectors(vsi);
14326 		ret = -ENOENT;
14327 		goto vector_setup_out;
14328 	}
14329 
14330 vector_setup_out:
14331 	return ret;
14332 }
14333 
14334 /**
14335  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14336  * @vsi: pointer to the vsi.
14337  *
14338  * This re-allocates a vsi's queue resources.
14339  *
14340  * Returns pointer to the successfully allocated and configured VSI sw struct
14341  * on success, otherwise returns NULL on failure.
14342  **/
14343 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14344 {
14345 	u16 alloc_queue_pairs;
14346 	struct i40e_pf *pf;
14347 	u8 enabled_tc;
14348 	int ret;
14349 
14350 	if (!vsi)
14351 		return NULL;
14352 
14353 	pf = vsi->back;
14354 
14355 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14356 	i40e_vsi_clear_rings(vsi);
14357 
14358 	i40e_vsi_free_arrays(vsi, false);
14359 	i40e_set_num_rings_in_vsi(vsi);
14360 	ret = i40e_vsi_alloc_arrays(vsi, false);
14361 	if (ret)
14362 		goto err_vsi;
14363 
14364 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14365 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14366 
14367 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14368 	if (ret < 0) {
14369 		dev_info(&pf->pdev->dev,
14370 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14371 			 alloc_queue_pairs, vsi->seid, ret);
14372 		goto err_vsi;
14373 	}
14374 	vsi->base_queue = ret;
14375 
14376 	/* Update the FW view of the VSI. Force a reset of TC and queue
14377 	 * layout configurations.
14378 	 */
14379 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14380 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14381 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14382 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14383 	if (vsi->type == I40E_VSI_MAIN)
14384 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14385 
14386 	/* assign it some queues */
14387 	ret = i40e_alloc_rings(vsi);
14388 	if (ret)
14389 		goto err_rings;
14390 
14391 	/* map all of the rings to the q_vectors */
14392 	i40e_vsi_map_rings_to_vectors(vsi);
14393 	return vsi;
14394 
14395 err_rings:
14396 	i40e_vsi_free_q_vectors(vsi);
14397 	if (vsi->netdev_registered) {
14398 		vsi->netdev_registered = false;
14399 		unregister_netdev(vsi->netdev);
14400 		free_netdev(vsi->netdev);
14401 		vsi->netdev = NULL;
14402 	}
14403 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14404 err_vsi:
14405 	i40e_vsi_clear(vsi);
14406 	return NULL;
14407 }
14408 
14409 /**
14410  * i40e_vsi_setup - Set up a VSI by a given type
14411  * @pf: board private structure
14412  * @type: VSI type
14413  * @uplink_seid: the switch element to link to
14414  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14415  *
14416  * This allocates the sw VSI structure and its queue resources, then add a VSI
14417  * to the identified VEB.
14418  *
14419  * Returns pointer to the successfully allocated and configure VSI sw struct on
14420  * success, otherwise returns NULL on failure.
14421  **/
14422 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14423 				u16 uplink_seid, u32 param1)
14424 {
14425 	struct i40e_vsi *vsi = NULL;
14426 	struct i40e_veb *veb = NULL;
14427 	u16 alloc_queue_pairs;
14428 	int ret, i;
14429 	int v_idx;
14430 
14431 	/* The requested uplink_seid must be either
14432 	 *     - the PF's port seid
14433 	 *              no VEB is needed because this is the PF
14434 	 *              or this is a Flow Director special case VSI
14435 	 *     - seid of an existing VEB
14436 	 *     - seid of a VSI that owns an existing VEB
14437 	 *     - seid of a VSI that doesn't own a VEB
14438 	 *              a new VEB is created and the VSI becomes the owner
14439 	 *     - seid of the PF VSI, which is what creates the first VEB
14440 	 *              this is a special case of the previous
14441 	 *
14442 	 * Find which uplink_seid we were given and create a new VEB if needed
14443 	 */
14444 	for (i = 0; i < I40E_MAX_VEB; i++) {
14445 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14446 			veb = pf->veb[i];
14447 			break;
14448 		}
14449 	}
14450 
14451 	if (!veb && uplink_seid != pf->mac_seid) {
14452 
14453 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14454 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14455 				vsi = pf->vsi[i];
14456 				break;
14457 			}
14458 		}
14459 		if (!vsi) {
14460 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14461 				 uplink_seid);
14462 			return NULL;
14463 		}
14464 
14465 		if (vsi->uplink_seid == pf->mac_seid)
14466 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14467 					     vsi->tc_config.enabled_tc);
14468 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14469 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14470 					     vsi->tc_config.enabled_tc);
14471 		if (veb) {
14472 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14473 				dev_info(&vsi->back->pdev->dev,
14474 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14475 				return NULL;
14476 			}
14477 			/* We come up by default in VEPA mode if SRIOV is not
14478 			 * already enabled, in which case we can't force VEPA
14479 			 * mode.
14480 			 */
14481 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14482 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14483 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14484 			}
14485 			i40e_config_bridge_mode(veb);
14486 		}
14487 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14488 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14489 				veb = pf->veb[i];
14490 		}
14491 		if (!veb) {
14492 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14493 			return NULL;
14494 		}
14495 
14496 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14497 		uplink_seid = veb->seid;
14498 	}
14499 
14500 	/* get vsi sw struct */
14501 	v_idx = i40e_vsi_mem_alloc(pf, type);
14502 	if (v_idx < 0)
14503 		goto err_alloc;
14504 	vsi = pf->vsi[v_idx];
14505 	if (!vsi)
14506 		goto err_alloc;
14507 	vsi->type = type;
14508 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14509 
14510 	if (type == I40E_VSI_MAIN)
14511 		pf->lan_vsi = v_idx;
14512 	else if (type == I40E_VSI_SRIOV)
14513 		vsi->vf_id = param1;
14514 	/* assign it some queues */
14515 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14516 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14517 
14518 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14519 	if (ret < 0) {
14520 		dev_info(&pf->pdev->dev,
14521 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14522 			 alloc_queue_pairs, vsi->seid, ret);
14523 		goto err_vsi;
14524 	}
14525 	vsi->base_queue = ret;
14526 
14527 	/* get a VSI from the hardware */
14528 	vsi->uplink_seid = uplink_seid;
14529 	ret = i40e_add_vsi(vsi);
14530 	if (ret)
14531 		goto err_vsi;
14532 
14533 	switch (vsi->type) {
14534 	/* setup the netdev if needed */
14535 	case I40E_VSI_MAIN:
14536 	case I40E_VSI_VMDQ2:
14537 		ret = i40e_config_netdev(vsi);
14538 		if (ret)
14539 			goto err_netdev;
14540 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14541 		if (ret)
14542 			goto err_netdev;
14543 		ret = register_netdev(vsi->netdev);
14544 		if (ret)
14545 			goto err_netdev;
14546 		vsi->netdev_registered = true;
14547 		netif_carrier_off(vsi->netdev);
14548 #ifdef CONFIG_I40E_DCB
14549 		/* Setup DCB netlink interface */
14550 		i40e_dcbnl_setup(vsi);
14551 #endif /* CONFIG_I40E_DCB */
14552 		fallthrough;
14553 	case I40E_VSI_FDIR:
14554 		/* set up vectors and rings if needed */
14555 		ret = i40e_vsi_setup_vectors(vsi);
14556 		if (ret)
14557 			goto err_msix;
14558 
14559 		ret = i40e_alloc_rings(vsi);
14560 		if (ret)
14561 			goto err_rings;
14562 
14563 		/* map all of the rings to the q_vectors */
14564 		i40e_vsi_map_rings_to_vectors(vsi);
14565 
14566 		i40e_vsi_reset_stats(vsi);
14567 		break;
14568 	default:
14569 		/* no netdev or rings for the other VSI types */
14570 		break;
14571 	}
14572 
14573 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14574 	    (vsi->type == I40E_VSI_VMDQ2)) {
14575 		ret = i40e_vsi_config_rss(vsi);
14576 	}
14577 	return vsi;
14578 
14579 err_rings:
14580 	i40e_vsi_free_q_vectors(vsi);
14581 err_msix:
14582 	if (vsi->netdev_registered) {
14583 		vsi->netdev_registered = false;
14584 		unregister_netdev(vsi->netdev);
14585 		free_netdev(vsi->netdev);
14586 		vsi->netdev = NULL;
14587 	}
14588 err_netdev:
14589 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14590 err_vsi:
14591 	i40e_vsi_clear(vsi);
14592 err_alloc:
14593 	return NULL;
14594 }
14595 
14596 /**
14597  * i40e_veb_get_bw_info - Query VEB BW information
14598  * @veb: the veb to query
14599  *
14600  * Query the Tx scheduler BW configuration data for given VEB
14601  **/
14602 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14603 {
14604 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14605 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14606 	struct i40e_pf *pf = veb->pf;
14607 	struct i40e_hw *hw = &pf->hw;
14608 	u32 tc_bw_max;
14609 	int ret = 0;
14610 	int i;
14611 
14612 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14613 						  &bw_data, NULL);
14614 	if (ret) {
14615 		dev_info(&pf->pdev->dev,
14616 			 "query veb bw config failed, err %pe aq_err %s\n",
14617 			 ERR_PTR(ret),
14618 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14619 		goto out;
14620 	}
14621 
14622 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14623 						   &ets_data, NULL);
14624 	if (ret) {
14625 		dev_info(&pf->pdev->dev,
14626 			 "query veb bw ets config failed, err %pe aq_err %s\n",
14627 			 ERR_PTR(ret),
14628 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14629 		goto out;
14630 	}
14631 
14632 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14633 	veb->bw_max_quanta = ets_data.tc_bw_max;
14634 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14635 	veb->enabled_tc = ets_data.tc_valid_bits;
14636 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14637 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14638 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14639 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14640 		veb->bw_tc_limit_credits[i] =
14641 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14642 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14643 	}
14644 
14645 out:
14646 	return ret;
14647 }
14648 
14649 /**
14650  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14651  * @pf: board private structure
14652  *
14653  * On error: returns error code (negative)
14654  * On success: returns vsi index in PF (positive)
14655  **/
14656 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14657 {
14658 	int ret = -ENOENT;
14659 	struct i40e_veb *veb;
14660 	int i;
14661 
14662 	/* Need to protect the allocation of switch elements at the PF level */
14663 	mutex_lock(&pf->switch_mutex);
14664 
14665 	/* VEB list may be fragmented if VEB creation/destruction has
14666 	 * been happening.  We can afford to do a quick scan to look
14667 	 * for any free slots in the list.
14668 	 *
14669 	 * find next empty veb slot, looping back around if necessary
14670 	 */
14671 	i = 0;
14672 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14673 		i++;
14674 	if (i >= I40E_MAX_VEB) {
14675 		ret = -ENOMEM;
14676 		goto err_alloc_veb;  /* out of VEB slots! */
14677 	}
14678 
14679 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14680 	if (!veb) {
14681 		ret = -ENOMEM;
14682 		goto err_alloc_veb;
14683 	}
14684 	veb->pf = pf;
14685 	veb->idx = i;
14686 	veb->enabled_tc = 1;
14687 
14688 	pf->veb[i] = veb;
14689 	ret = i;
14690 err_alloc_veb:
14691 	mutex_unlock(&pf->switch_mutex);
14692 	return ret;
14693 }
14694 
14695 /**
14696  * i40e_switch_branch_release - Delete a branch of the switch tree
14697  * @branch: where to start deleting
14698  *
14699  * This uses recursion to find the tips of the branch to be
14700  * removed, deleting until we get back to and can delete this VEB.
14701  **/
14702 static void i40e_switch_branch_release(struct i40e_veb *branch)
14703 {
14704 	struct i40e_pf *pf = branch->pf;
14705 	u16 branch_seid = branch->seid;
14706 	u16 veb_idx = branch->idx;
14707 	int i;
14708 
14709 	/* release any VEBs on this VEB - RECURSION */
14710 	for (i = 0; i < I40E_MAX_VEB; i++) {
14711 		if (!pf->veb[i])
14712 			continue;
14713 		if (pf->veb[i]->uplink_seid == branch->seid)
14714 			i40e_switch_branch_release(pf->veb[i]);
14715 	}
14716 
14717 	/* Release the VSIs on this VEB, but not the owner VSI.
14718 	 *
14719 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14720 	 *       the VEB itself, so don't use (*branch) after this loop.
14721 	 */
14722 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14723 		if (!pf->vsi[i])
14724 			continue;
14725 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14726 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14727 			i40e_vsi_release(pf->vsi[i]);
14728 		}
14729 	}
14730 
14731 	/* There's one corner case where the VEB might not have been
14732 	 * removed, so double check it here and remove it if needed.
14733 	 * This case happens if the veb was created from the debugfs
14734 	 * commands and no VSIs were added to it.
14735 	 */
14736 	if (pf->veb[veb_idx])
14737 		i40e_veb_release(pf->veb[veb_idx]);
14738 }
14739 
14740 /**
14741  * i40e_veb_clear - remove veb struct
14742  * @veb: the veb to remove
14743  **/
14744 static void i40e_veb_clear(struct i40e_veb *veb)
14745 {
14746 	if (!veb)
14747 		return;
14748 
14749 	if (veb->pf) {
14750 		struct i40e_pf *pf = veb->pf;
14751 
14752 		mutex_lock(&pf->switch_mutex);
14753 		if (pf->veb[veb->idx] == veb)
14754 			pf->veb[veb->idx] = NULL;
14755 		mutex_unlock(&pf->switch_mutex);
14756 	}
14757 
14758 	kfree(veb);
14759 }
14760 
14761 /**
14762  * i40e_veb_release - Delete a VEB and free its resources
14763  * @veb: the VEB being removed
14764  **/
14765 void i40e_veb_release(struct i40e_veb *veb)
14766 {
14767 	struct i40e_vsi *vsi = NULL;
14768 	struct i40e_pf *pf;
14769 	int i, n = 0;
14770 
14771 	pf = veb->pf;
14772 
14773 	/* find the remaining VSI and check for extras */
14774 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14775 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14776 			n++;
14777 			vsi = pf->vsi[i];
14778 		}
14779 	}
14780 	if (n != 1) {
14781 		dev_info(&pf->pdev->dev,
14782 			 "can't remove VEB %d with %d VSIs left\n",
14783 			 veb->seid, n);
14784 		return;
14785 	}
14786 
14787 	/* move the remaining VSI to uplink veb */
14788 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14789 	if (veb->uplink_seid) {
14790 		vsi->uplink_seid = veb->uplink_seid;
14791 		if (veb->uplink_seid == pf->mac_seid)
14792 			vsi->veb_idx = I40E_NO_VEB;
14793 		else
14794 			vsi->veb_idx = veb->veb_idx;
14795 	} else {
14796 		/* floating VEB */
14797 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14798 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14799 	}
14800 
14801 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14802 	i40e_veb_clear(veb);
14803 }
14804 
14805 /**
14806  * i40e_add_veb - create the VEB in the switch
14807  * @veb: the VEB to be instantiated
14808  * @vsi: the controlling VSI
14809  **/
14810 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14811 {
14812 	struct i40e_pf *pf = veb->pf;
14813 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14814 	int ret;
14815 
14816 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14817 			      veb->enabled_tc, false,
14818 			      &veb->seid, enable_stats, NULL);
14819 
14820 	/* get a VEB from the hardware */
14821 	if (ret) {
14822 		dev_info(&pf->pdev->dev,
14823 			 "couldn't add VEB, err %pe aq_err %s\n",
14824 			 ERR_PTR(ret),
14825 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14826 		return -EPERM;
14827 	}
14828 
14829 	/* get statistics counter */
14830 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14831 					 &veb->stats_idx, NULL, NULL, NULL);
14832 	if (ret) {
14833 		dev_info(&pf->pdev->dev,
14834 			 "couldn't get VEB statistics idx, err %pe aq_err %s\n",
14835 			 ERR_PTR(ret),
14836 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14837 		return -EPERM;
14838 	}
14839 	ret = i40e_veb_get_bw_info(veb);
14840 	if (ret) {
14841 		dev_info(&pf->pdev->dev,
14842 			 "couldn't get VEB bw info, err %pe aq_err %s\n",
14843 			 ERR_PTR(ret),
14844 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14845 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14846 		return -ENOENT;
14847 	}
14848 
14849 	vsi->uplink_seid = veb->seid;
14850 	vsi->veb_idx = veb->idx;
14851 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14852 
14853 	return 0;
14854 }
14855 
14856 /**
14857  * i40e_veb_setup - Set up a VEB
14858  * @pf: board private structure
14859  * @flags: VEB setup flags
14860  * @uplink_seid: the switch element to link to
14861  * @vsi_seid: the initial VSI seid
14862  * @enabled_tc: Enabled TC bit-map
14863  *
14864  * This allocates the sw VEB structure and links it into the switch
14865  * It is possible and legal for this to be a duplicate of an already
14866  * existing VEB.  It is also possible for both uplink and vsi seids
14867  * to be zero, in order to create a floating VEB.
14868  *
14869  * Returns pointer to the successfully allocated VEB sw struct on
14870  * success, otherwise returns NULL on failure.
14871  **/
14872 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14873 				u16 uplink_seid, u16 vsi_seid,
14874 				u8 enabled_tc)
14875 {
14876 	struct i40e_veb *veb, *uplink_veb = NULL;
14877 	int vsi_idx, veb_idx;
14878 	int ret;
14879 
14880 	/* if one seid is 0, the other must be 0 to create a floating relay */
14881 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14882 	    (uplink_seid + vsi_seid != 0)) {
14883 		dev_info(&pf->pdev->dev,
14884 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14885 			 uplink_seid, vsi_seid);
14886 		return NULL;
14887 	}
14888 
14889 	/* make sure there is such a vsi and uplink */
14890 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14891 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14892 			break;
14893 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14894 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14895 			 vsi_seid);
14896 		return NULL;
14897 	}
14898 
14899 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14900 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14901 			if (pf->veb[veb_idx] &&
14902 			    pf->veb[veb_idx]->seid == uplink_seid) {
14903 				uplink_veb = pf->veb[veb_idx];
14904 				break;
14905 			}
14906 		}
14907 		if (!uplink_veb) {
14908 			dev_info(&pf->pdev->dev,
14909 				 "uplink seid %d not found\n", uplink_seid);
14910 			return NULL;
14911 		}
14912 	}
14913 
14914 	/* get veb sw struct */
14915 	veb_idx = i40e_veb_mem_alloc(pf);
14916 	if (veb_idx < 0)
14917 		goto err_alloc;
14918 	veb = pf->veb[veb_idx];
14919 	veb->flags = flags;
14920 	veb->uplink_seid = uplink_seid;
14921 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14922 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14923 
14924 	/* create the VEB in the switch */
14925 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14926 	if (ret)
14927 		goto err_veb;
14928 	if (vsi_idx == pf->lan_vsi)
14929 		pf->lan_veb = veb->idx;
14930 
14931 	return veb;
14932 
14933 err_veb:
14934 	i40e_veb_clear(veb);
14935 err_alloc:
14936 	return NULL;
14937 }
14938 
14939 /**
14940  * i40e_setup_pf_switch_element - set PF vars based on switch type
14941  * @pf: board private structure
14942  * @ele: element we are building info from
14943  * @num_reported: total number of elements
14944  * @printconfig: should we print the contents
14945  *
14946  * helper function to assist in extracting a few useful SEID values.
14947  **/
14948 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14949 				struct i40e_aqc_switch_config_element_resp *ele,
14950 				u16 num_reported, bool printconfig)
14951 {
14952 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14953 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14954 	u8 element_type = ele->element_type;
14955 	u16 seid = le16_to_cpu(ele->seid);
14956 
14957 	if (printconfig)
14958 		dev_info(&pf->pdev->dev,
14959 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14960 			 element_type, seid, uplink_seid, downlink_seid);
14961 
14962 	switch (element_type) {
14963 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14964 		pf->mac_seid = seid;
14965 		break;
14966 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14967 		/* Main VEB? */
14968 		if (uplink_seid != pf->mac_seid)
14969 			break;
14970 		if (pf->lan_veb >= I40E_MAX_VEB) {
14971 			int v;
14972 
14973 			/* find existing or else empty VEB */
14974 			for (v = 0; v < I40E_MAX_VEB; v++) {
14975 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14976 					pf->lan_veb = v;
14977 					break;
14978 				}
14979 			}
14980 			if (pf->lan_veb >= I40E_MAX_VEB) {
14981 				v = i40e_veb_mem_alloc(pf);
14982 				if (v < 0)
14983 					break;
14984 				pf->lan_veb = v;
14985 			}
14986 		}
14987 		if (pf->lan_veb >= I40E_MAX_VEB)
14988 			break;
14989 
14990 		pf->veb[pf->lan_veb]->seid = seid;
14991 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14992 		pf->veb[pf->lan_veb]->pf = pf;
14993 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14994 		break;
14995 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14996 		if (num_reported != 1)
14997 			break;
14998 		/* This is immediately after a reset so we can assume this is
14999 		 * the PF's VSI
15000 		 */
15001 		pf->mac_seid = uplink_seid;
15002 		pf->pf_seid = downlink_seid;
15003 		pf->main_vsi_seid = seid;
15004 		if (printconfig)
15005 			dev_info(&pf->pdev->dev,
15006 				 "pf_seid=%d main_vsi_seid=%d\n",
15007 				 pf->pf_seid, pf->main_vsi_seid);
15008 		break;
15009 	case I40E_SWITCH_ELEMENT_TYPE_PF:
15010 	case I40E_SWITCH_ELEMENT_TYPE_VF:
15011 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
15012 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
15013 	case I40E_SWITCH_ELEMENT_TYPE_PE:
15014 	case I40E_SWITCH_ELEMENT_TYPE_PA:
15015 		/* ignore these for now */
15016 		break;
15017 	default:
15018 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
15019 			 element_type, seid);
15020 		break;
15021 	}
15022 }
15023 
15024 /**
15025  * i40e_fetch_switch_configuration - Get switch config from firmware
15026  * @pf: board private structure
15027  * @printconfig: should we print the contents
15028  *
15029  * Get the current switch configuration from the device and
15030  * extract a few useful SEID values.
15031  **/
15032 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
15033 {
15034 	struct i40e_aqc_get_switch_config_resp *sw_config;
15035 	u16 next_seid = 0;
15036 	int ret = 0;
15037 	u8 *aq_buf;
15038 	int i;
15039 
15040 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
15041 	if (!aq_buf)
15042 		return -ENOMEM;
15043 
15044 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
15045 	do {
15046 		u16 num_reported, num_total;
15047 
15048 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
15049 						I40E_AQ_LARGE_BUF,
15050 						&next_seid, NULL);
15051 		if (ret) {
15052 			dev_info(&pf->pdev->dev,
15053 				 "get switch config failed err %d aq_err %s\n",
15054 				 ret,
15055 				 i40e_aq_str(&pf->hw,
15056 					     pf->hw.aq.asq_last_status));
15057 			kfree(aq_buf);
15058 			return -ENOENT;
15059 		}
15060 
15061 		num_reported = le16_to_cpu(sw_config->header.num_reported);
15062 		num_total = le16_to_cpu(sw_config->header.num_total);
15063 
15064 		if (printconfig)
15065 			dev_info(&pf->pdev->dev,
15066 				 "header: %d reported %d total\n",
15067 				 num_reported, num_total);
15068 
15069 		for (i = 0; i < num_reported; i++) {
15070 			struct i40e_aqc_switch_config_element_resp *ele =
15071 				&sw_config->element[i];
15072 
15073 			i40e_setup_pf_switch_element(pf, ele, num_reported,
15074 						     printconfig);
15075 		}
15076 	} while (next_seid != 0);
15077 
15078 	kfree(aq_buf);
15079 	return ret;
15080 }
15081 
15082 /**
15083  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
15084  * @pf: board private structure
15085  * @reinit: if the Main VSI needs to re-initialized.
15086  * @lock_acquired: indicates whether or not the lock has been acquired
15087  *
15088  * Returns 0 on success, negative value on failure
15089  **/
15090 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
15091 {
15092 	u16 flags = 0;
15093 	int ret;
15094 
15095 	/* find out what's out there already */
15096 	ret = i40e_fetch_switch_configuration(pf, false);
15097 	if (ret) {
15098 		dev_info(&pf->pdev->dev,
15099 			 "couldn't fetch switch config, err %pe aq_err %s\n",
15100 			 ERR_PTR(ret),
15101 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15102 		return ret;
15103 	}
15104 	i40e_pf_reset_stats(pf);
15105 
15106 	/* set the switch config bit for the whole device to
15107 	 * support limited promisc or true promisc
15108 	 * when user requests promisc. The default is limited
15109 	 * promisc.
15110 	*/
15111 
15112 	if ((pf->hw.pf_id == 0) &&
15113 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
15114 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15115 		pf->last_sw_conf_flags = flags;
15116 	}
15117 
15118 	if (pf->hw.pf_id == 0) {
15119 		u16 valid_flags;
15120 
15121 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15122 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15123 						NULL);
15124 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
15125 			dev_info(&pf->pdev->dev,
15126 				 "couldn't set switch config bits, err %pe aq_err %s\n",
15127 				 ERR_PTR(ret),
15128 				 i40e_aq_str(&pf->hw,
15129 					     pf->hw.aq.asq_last_status));
15130 			/* not a fatal problem, just keep going */
15131 		}
15132 		pf->last_sw_conf_valid_flags = valid_flags;
15133 	}
15134 
15135 	/* first time setup */
15136 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
15137 		struct i40e_vsi *vsi = NULL;
15138 		u16 uplink_seid;
15139 
15140 		/* Set up the PF VSI associated with the PF's main VSI
15141 		 * that is already in the HW switch
15142 		 */
15143 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
15144 			uplink_seid = pf->veb[pf->lan_veb]->seid;
15145 		else
15146 			uplink_seid = pf->mac_seid;
15147 		if (pf->lan_vsi == I40E_NO_VSI)
15148 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
15149 		else if (reinit)
15150 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
15151 		if (!vsi) {
15152 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15153 			i40e_cloud_filter_exit(pf);
15154 			i40e_fdir_teardown(pf);
15155 			return -EAGAIN;
15156 		}
15157 	} else {
15158 		/* force a reset of TC and queue layout configurations */
15159 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
15160 
15161 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
15162 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
15163 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
15164 	}
15165 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
15166 
15167 	i40e_fdir_sb_setup(pf);
15168 
15169 	/* Setup static PF queue filter control settings */
15170 	ret = i40e_setup_pf_filter_control(pf);
15171 	if (ret) {
15172 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15173 			 ret);
15174 		/* Failure here should not stop continuing other steps */
15175 	}
15176 
15177 	/* enable RSS in the HW, even for only one queue, as the stack can use
15178 	 * the hash
15179 	 */
15180 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
15181 		i40e_pf_config_rss(pf);
15182 
15183 	/* fill in link information and enable LSE reporting */
15184 	i40e_link_event(pf);
15185 
15186 	/* Initialize user-specific link properties */
15187 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
15188 				  I40E_AQ_AN_COMPLETED) ? true : false);
15189 
15190 	i40e_ptp_init(pf);
15191 
15192 	if (!lock_acquired)
15193 		rtnl_lock();
15194 
15195 	/* repopulate tunnel port filters */
15196 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
15197 
15198 	if (!lock_acquired)
15199 		rtnl_unlock();
15200 
15201 	return ret;
15202 }
15203 
15204 /**
15205  * i40e_determine_queue_usage - Work out queue distribution
15206  * @pf: board private structure
15207  **/
15208 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15209 {
15210 	int queues_left;
15211 	int q_max;
15212 
15213 	pf->num_lan_qps = 0;
15214 
15215 	/* Find the max queues to be put into basic use.  We'll always be
15216 	 * using TC0, whether or not DCB is running, and TC0 will get the
15217 	 * big RSS set.
15218 	 */
15219 	queues_left = pf->hw.func_caps.num_tx_qp;
15220 
15221 	if ((queues_left == 1) ||
15222 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
15223 		/* one qp for PF, no queues for anything else */
15224 		queues_left = 0;
15225 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15226 
15227 		/* make sure all the fancies are disabled */
15228 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15229 			       I40E_FLAG_IWARP_ENABLED	|
15230 			       I40E_FLAG_FD_SB_ENABLED	|
15231 			       I40E_FLAG_FD_ATR_ENABLED	|
15232 			       I40E_FLAG_DCB_CAPABLE	|
15233 			       I40E_FLAG_DCB_ENABLED	|
15234 			       I40E_FLAG_SRIOV_ENABLED	|
15235 			       I40E_FLAG_VMDQ_ENABLED);
15236 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15237 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
15238 				  I40E_FLAG_FD_SB_ENABLED |
15239 				  I40E_FLAG_FD_ATR_ENABLED |
15240 				  I40E_FLAG_DCB_CAPABLE))) {
15241 		/* one qp for PF */
15242 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15243 		queues_left -= pf->num_lan_qps;
15244 
15245 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15246 			       I40E_FLAG_IWARP_ENABLED	|
15247 			       I40E_FLAG_FD_SB_ENABLED	|
15248 			       I40E_FLAG_FD_ATR_ENABLED	|
15249 			       I40E_FLAG_DCB_ENABLED	|
15250 			       I40E_FLAG_VMDQ_ENABLED);
15251 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15252 	} else {
15253 		/* Not enough queues for all TCs */
15254 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
15255 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
15256 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
15257 					I40E_FLAG_DCB_ENABLED);
15258 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15259 		}
15260 
15261 		/* limit lan qps to the smaller of qps, cpus or msix */
15262 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15263 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15264 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15265 		pf->num_lan_qps = q_max;
15266 
15267 		queues_left -= pf->num_lan_qps;
15268 	}
15269 
15270 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15271 		if (queues_left > 1) {
15272 			queues_left -= 1; /* save 1 queue for FD */
15273 		} else {
15274 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
15275 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15276 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15277 		}
15278 	}
15279 
15280 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15281 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15282 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15283 					(queues_left / pf->num_vf_qps));
15284 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15285 	}
15286 
15287 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
15288 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15289 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15290 					  (queues_left / pf->num_vmdq_qps));
15291 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15292 	}
15293 
15294 	pf->queues_left = queues_left;
15295 	dev_dbg(&pf->pdev->dev,
15296 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15297 		pf->hw.func_caps.num_tx_qp,
15298 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
15299 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15300 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15301 		queues_left);
15302 }
15303 
15304 /**
15305  * i40e_setup_pf_filter_control - Setup PF static filter control
15306  * @pf: PF to be setup
15307  *
15308  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15309  * settings. If PE/FCoE are enabled then it will also set the per PF
15310  * based filter sizes required for them. It also enables Flow director,
15311  * ethertype and macvlan type filter settings for the pf.
15312  *
15313  * Returns 0 on success, negative on failure
15314  **/
15315 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15316 {
15317 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15318 
15319 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15320 
15321 	/* Flow Director is enabled */
15322 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
15323 		settings->enable_fdir = true;
15324 
15325 	/* Ethtype and MACVLAN filters enabled for PF */
15326 	settings->enable_ethtype = true;
15327 	settings->enable_macvlan = true;
15328 
15329 	if (i40e_set_filter_control(&pf->hw, settings))
15330 		return -ENOENT;
15331 
15332 	return 0;
15333 }
15334 
15335 #define INFO_STRING_LEN 255
15336 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15337 static void i40e_print_features(struct i40e_pf *pf)
15338 {
15339 	struct i40e_hw *hw = &pf->hw;
15340 	char *buf;
15341 	int i;
15342 
15343 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15344 	if (!buf)
15345 		return;
15346 
15347 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15348 #ifdef CONFIG_PCI_IOV
15349 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15350 #endif
15351 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15352 		      pf->hw.func_caps.num_vsis,
15353 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15354 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15355 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15356 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15357 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15358 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15359 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15360 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15361 	}
15362 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15363 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15364 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15365 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15366 	if (pf->flags & I40E_FLAG_PTP)
15367 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15368 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15369 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15370 	else
15371 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15372 
15373 	dev_info(&pf->pdev->dev, "%s\n", buf);
15374 	kfree(buf);
15375 	WARN_ON(i > INFO_STRING_LEN);
15376 }
15377 
15378 /**
15379  * i40e_get_platform_mac_addr - get platform-specific MAC address
15380  * @pdev: PCI device information struct
15381  * @pf: board private structure
15382  *
15383  * Look up the MAC address for the device. First we'll try
15384  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15385  * specific fallback. Otherwise, we'll default to the stored value in
15386  * firmware.
15387  **/
15388 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15389 {
15390 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15391 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15392 }
15393 
15394 /**
15395  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15396  * @fec_cfg: FEC option to set in flags
15397  * @flags: ptr to flags in which we set FEC option
15398  **/
15399 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15400 {
15401 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15402 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15403 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15404 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15405 		*flags |= I40E_FLAG_RS_FEC;
15406 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15407 	}
15408 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15409 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15410 		*flags |= I40E_FLAG_BASE_R_FEC;
15411 		*flags &= ~I40E_FLAG_RS_FEC;
15412 	}
15413 	if (fec_cfg == 0)
15414 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15415 }
15416 
15417 /**
15418  * i40e_check_recovery_mode - check if we are running transition firmware
15419  * @pf: board private structure
15420  *
15421  * Check registers indicating the firmware runs in recovery mode. Sets the
15422  * appropriate driver state.
15423  *
15424  * Returns true if the recovery mode was detected, false otherwise
15425  **/
15426 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15427 {
15428 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15429 
15430 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15431 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15432 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15433 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15434 
15435 		return true;
15436 	}
15437 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15438 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15439 
15440 	return false;
15441 }
15442 
15443 /**
15444  * i40e_pf_loop_reset - perform reset in a loop.
15445  * @pf: board private structure
15446  *
15447  * This function is useful when a NIC is about to enter recovery mode.
15448  * When a NIC's internal data structures are corrupted the NIC's
15449  * firmware is going to enter recovery mode.
15450  * Right after a POR it takes about 7 minutes for firmware to enter
15451  * recovery mode. Until that time a NIC is in some kind of intermediate
15452  * state. After that time period the NIC almost surely enters
15453  * recovery mode. The only way for a driver to detect intermediate
15454  * state is to issue a series of pf-resets and check a return value.
15455  * If a PF reset returns success then the firmware could be in recovery
15456  * mode so the caller of this code needs to check for recovery mode
15457  * if this function returns success. There is a little chance that
15458  * firmware will hang in intermediate state forever.
15459  * Since waiting 7 minutes is quite a lot of time this function waits
15460  * 10 seconds and then gives up by returning an error.
15461  *
15462  * Return 0 on success, negative on failure.
15463  **/
15464 static int i40e_pf_loop_reset(struct i40e_pf *pf)
15465 {
15466 	/* wait max 10 seconds for PF reset to succeed */
15467 	const unsigned long time_end = jiffies + 10 * HZ;
15468 	struct i40e_hw *hw = &pf->hw;
15469 	int ret;
15470 
15471 	ret = i40e_pf_reset(hw);
15472 	while (ret != 0 && time_before(jiffies, time_end)) {
15473 		usleep_range(10000, 20000);
15474 		ret = i40e_pf_reset(hw);
15475 	}
15476 
15477 	if (ret == 0)
15478 		pf->pfr_count++;
15479 	else
15480 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15481 
15482 	return ret;
15483 }
15484 
15485 /**
15486  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15487  * @pf: board private structure
15488  *
15489  * Check FW registers to determine if FW issued unexpected EMP Reset.
15490  * Every time when unexpected EMP Reset occurs the FW increments
15491  * a counter of unexpected EMP Resets. When the counter reaches 10
15492  * the FW should enter the Recovery mode
15493  *
15494  * Returns true if FW issued unexpected EMP Reset
15495  **/
15496 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15497 {
15498 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15499 			   I40E_GL_FWSTS_FWS1B_MASK;
15500 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15501 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15502 }
15503 
15504 /**
15505  * i40e_handle_resets - handle EMP resets and PF resets
15506  * @pf: board private structure
15507  *
15508  * Handle both EMP resets and PF resets and conclude whether there are
15509  * any issues regarding these resets. If there are any issues then
15510  * generate log entry.
15511  *
15512  * Return 0 if NIC is healthy or negative value when there are issues
15513  * with resets
15514  **/
15515 static int i40e_handle_resets(struct i40e_pf *pf)
15516 {
15517 	const int pfr = i40e_pf_loop_reset(pf);
15518 	const bool is_empr = i40e_check_fw_empr(pf);
15519 
15520 	if (is_empr || pfr != 0)
15521 		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");
15522 
15523 	return is_empr ? -EIO : pfr;
15524 }
15525 
15526 /**
15527  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15528  * @pf: board private structure
15529  * @hw: ptr to the hardware info
15530  *
15531  * This function does a minimal setup of all subsystems needed for running
15532  * recovery mode.
15533  *
15534  * Returns 0 on success, negative on failure
15535  **/
15536 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15537 {
15538 	struct i40e_vsi *vsi;
15539 	int err;
15540 	int v_idx;
15541 
15542 	pci_set_drvdata(pf->pdev, pf);
15543 	pci_save_state(pf->pdev);
15544 
15545 	/* set up periodic task facility */
15546 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15547 	pf->service_timer_period = HZ;
15548 
15549 	INIT_WORK(&pf->service_task, i40e_service_task);
15550 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15551 
15552 	err = i40e_init_interrupt_scheme(pf);
15553 	if (err)
15554 		goto err_switch_setup;
15555 
15556 	/* The number of VSIs reported by the FW is the minimum guaranteed
15557 	 * to us; HW supports far more and we share the remaining pool with
15558 	 * the other PFs. We allocate space for more than the guarantee with
15559 	 * the understanding that we might not get them all later.
15560 	 */
15561 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15562 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15563 	else
15564 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15565 
15566 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15567 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15568 			  GFP_KERNEL);
15569 	if (!pf->vsi) {
15570 		err = -ENOMEM;
15571 		goto err_switch_setup;
15572 	}
15573 
15574 	/* We allocate one VSI which is needed as absolute minimum
15575 	 * in order to register the netdev
15576 	 */
15577 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15578 	if (v_idx < 0) {
15579 		err = v_idx;
15580 		goto err_switch_setup;
15581 	}
15582 	pf->lan_vsi = v_idx;
15583 	vsi = pf->vsi[v_idx];
15584 	if (!vsi) {
15585 		err = -EFAULT;
15586 		goto err_switch_setup;
15587 	}
15588 	vsi->alloc_queue_pairs = 1;
15589 	err = i40e_config_netdev(vsi);
15590 	if (err)
15591 		goto err_switch_setup;
15592 	err = register_netdev(vsi->netdev);
15593 	if (err)
15594 		goto err_switch_setup;
15595 	vsi->netdev_registered = true;
15596 	i40e_dbg_pf_init(pf);
15597 
15598 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15599 	if (err)
15600 		goto err_switch_setup;
15601 
15602 	/* tell the firmware that we're starting */
15603 	i40e_send_version(pf);
15604 
15605 	/* since everything's happy, start the service_task timer */
15606 	mod_timer(&pf->service_timer,
15607 		  round_jiffies(jiffies + pf->service_timer_period));
15608 
15609 	return 0;
15610 
15611 err_switch_setup:
15612 	i40e_reset_interrupt_capability(pf);
15613 	timer_shutdown_sync(&pf->service_timer);
15614 	i40e_shutdown_adminq(hw);
15615 	iounmap(hw->hw_addr);
15616 	pci_release_mem_regions(pf->pdev);
15617 	pci_disable_device(pf->pdev);
15618 	kfree(pf);
15619 
15620 	return err;
15621 }
15622 
15623 /**
15624  * i40e_set_subsystem_device_id - set subsystem device id
15625  * @hw: pointer to the hardware info
15626  *
15627  * Set PCI subsystem device id either from a pci_dev structure or
15628  * a specific FW register.
15629  **/
15630 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15631 {
15632 	struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15633 
15634 	hw->subsystem_device_id = pdev->subsystem_device ?
15635 		pdev->subsystem_device :
15636 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15637 }
15638 
15639 /**
15640  * i40e_probe - Device initialization routine
15641  * @pdev: PCI device information struct
15642  * @ent: entry in i40e_pci_tbl
15643  *
15644  * i40e_probe initializes a PF identified by a pci_dev structure.
15645  * The OS initialization, configuring of the PF private structure,
15646  * and a hardware reset occur.
15647  *
15648  * Returns 0 on success, negative on failure
15649  **/
15650 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15651 {
15652 	struct i40e_aq_get_phy_abilities_resp abilities;
15653 #ifdef CONFIG_I40E_DCB
15654 	enum i40e_get_fw_lldp_status_resp lldp_status;
15655 #endif /* CONFIG_I40E_DCB */
15656 	struct i40e_pf *pf;
15657 	struct i40e_hw *hw;
15658 	static u16 pfs_found;
15659 	u16 wol_nvm_bits;
15660 	u16 link_status;
15661 #ifdef CONFIG_I40E_DCB
15662 	int status;
15663 #endif /* CONFIG_I40E_DCB */
15664 	int err;
15665 	u32 val;
15666 	u32 i;
15667 
15668 	err = pci_enable_device_mem(pdev);
15669 	if (err)
15670 		return err;
15671 
15672 	/* set up for high or low dma */
15673 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15674 	if (err) {
15675 		dev_err(&pdev->dev,
15676 			"DMA configuration failed: 0x%x\n", err);
15677 		goto err_dma;
15678 	}
15679 
15680 	/* set up pci connections */
15681 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15682 	if (err) {
15683 		dev_info(&pdev->dev,
15684 			 "pci_request_selected_regions failed %d\n", err);
15685 		goto err_pci_reg;
15686 	}
15687 
15688 	pci_set_master(pdev);
15689 
15690 	/* Now that we have a PCI connection, we need to do the
15691 	 * low level device setup.  This is primarily setting up
15692 	 * the Admin Queue structures and then querying for the
15693 	 * device's current profile information.
15694 	 */
15695 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15696 	if (!pf) {
15697 		err = -ENOMEM;
15698 		goto err_pf_alloc;
15699 	}
15700 	pf->next_vsi = 0;
15701 	pf->pdev = pdev;
15702 	set_bit(__I40E_DOWN, pf->state);
15703 
15704 	hw = &pf->hw;
15705 	hw->back = pf;
15706 
15707 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15708 				I40E_MAX_CSR_SPACE);
15709 	/* We believe that the highest register to read is
15710 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15711 	 * is not less than that before mapping to prevent a
15712 	 * kernel panic.
15713 	 */
15714 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15715 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15716 			pf->ioremap_len);
15717 		err = -ENOMEM;
15718 		goto err_ioremap;
15719 	}
15720 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15721 	if (!hw->hw_addr) {
15722 		err = -EIO;
15723 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15724 			 (unsigned int)pci_resource_start(pdev, 0),
15725 			 pf->ioremap_len, err);
15726 		goto err_ioremap;
15727 	}
15728 	hw->vendor_id = pdev->vendor;
15729 	hw->device_id = pdev->device;
15730 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15731 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15732 	i40e_set_subsystem_device_id(hw);
15733 	hw->bus.device = PCI_SLOT(pdev->devfn);
15734 	hw->bus.func = PCI_FUNC(pdev->devfn);
15735 	hw->bus.bus_id = pdev->bus->number;
15736 	pf->instance = pfs_found;
15737 
15738 	/* Select something other than the 802.1ad ethertype for the
15739 	 * switch to use internally and drop on ingress.
15740 	 */
15741 	hw->switch_tag = 0xffff;
15742 	hw->first_tag = ETH_P_8021AD;
15743 	hw->second_tag = ETH_P_8021Q;
15744 
15745 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15746 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15747 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15748 
15749 	/* set up the locks for the AQ, do this only once in probe
15750 	 * and destroy them only once in remove
15751 	 */
15752 	mutex_init(&hw->aq.asq_mutex);
15753 	mutex_init(&hw->aq.arq_mutex);
15754 
15755 	pf->msg_enable = netif_msg_init(debug,
15756 					NETIF_MSG_DRV |
15757 					NETIF_MSG_PROBE |
15758 					NETIF_MSG_LINK);
15759 	if (debug < -1)
15760 		pf->hw.debug_mask = debug;
15761 
15762 	/* do a special CORER for clearing PXE mode once at init */
15763 	if (hw->revision_id == 0 &&
15764 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15765 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15766 		i40e_flush(hw);
15767 		msleep(200);
15768 		pf->corer_count++;
15769 
15770 		i40e_clear_pxe_mode(hw);
15771 	}
15772 
15773 	/* Reset here to make sure all is clean and to define PF 'n' */
15774 	i40e_clear_hw(hw);
15775 
15776 	err = i40e_set_mac_type(hw);
15777 	if (err) {
15778 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15779 			 err);
15780 		goto err_pf_reset;
15781 	}
15782 
15783 	err = i40e_handle_resets(pf);
15784 	if (err)
15785 		goto err_pf_reset;
15786 
15787 	i40e_check_recovery_mode(pf);
15788 
15789 	if (is_kdump_kernel()) {
15790 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15791 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15792 	} else {
15793 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15794 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15795 	}
15796 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15797 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15798 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15799 
15800 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15801 		 "%s-%s:misc",
15802 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15803 
15804 	err = i40e_init_shared_code(hw);
15805 	if (err) {
15806 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15807 			 err);
15808 		goto err_pf_reset;
15809 	}
15810 
15811 	/* set up a default setting for link flow control */
15812 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15813 
15814 	err = i40e_init_adminq(hw);
15815 	if (err) {
15816 		if (err == -EIO)
15817 			dev_info(&pdev->dev,
15818 				 "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",
15819 				 hw->aq.api_maj_ver,
15820 				 hw->aq.api_min_ver,
15821 				 I40E_FW_API_VERSION_MAJOR,
15822 				 I40E_FW_MINOR_VERSION(hw));
15823 		else
15824 			dev_info(&pdev->dev,
15825 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15826 
15827 		goto err_pf_reset;
15828 	}
15829 	i40e_get_oem_version(hw);
15830 
15831 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15832 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15833 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15834 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15835 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15836 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15837 
15838 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15839 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15840 		dev_dbg(&pdev->dev,
15841 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15842 			 hw->aq.api_maj_ver,
15843 			 hw->aq.api_min_ver,
15844 			 I40E_FW_API_VERSION_MAJOR,
15845 			 I40E_FW_MINOR_VERSION(hw));
15846 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15847 		dev_info(&pdev->dev,
15848 			 "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",
15849 			 hw->aq.api_maj_ver,
15850 			 hw->aq.api_min_ver,
15851 			 I40E_FW_API_VERSION_MAJOR,
15852 			 I40E_FW_MINOR_VERSION(hw));
15853 
15854 	i40e_verify_eeprom(pf);
15855 
15856 	/* Rev 0 hardware was never productized */
15857 	if (hw->revision_id < 1)
15858 		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");
15859 
15860 	i40e_clear_pxe_mode(hw);
15861 
15862 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15863 	if (err)
15864 		goto err_adminq_setup;
15865 
15866 	err = i40e_sw_init(pf);
15867 	if (err) {
15868 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15869 		goto err_sw_init;
15870 	}
15871 
15872 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15873 		return i40e_init_recovery_mode(pf, hw);
15874 
15875 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15876 				hw->func_caps.num_rx_qp, 0, 0);
15877 	if (err) {
15878 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15879 		goto err_init_lan_hmc;
15880 	}
15881 
15882 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15883 	if (err) {
15884 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15885 		err = -ENOENT;
15886 		goto err_configure_lan_hmc;
15887 	}
15888 
15889 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15890 	 * Ignore error return codes because if it was already disabled via
15891 	 * hardware settings this will fail
15892 	 */
15893 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15894 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15895 		i40e_aq_stop_lldp(hw, true, false, NULL);
15896 	}
15897 
15898 	/* allow a platform config to override the HW addr */
15899 	i40e_get_platform_mac_addr(pdev, pf);
15900 
15901 	if (!is_valid_ether_addr(hw->mac.addr)) {
15902 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15903 		err = -EIO;
15904 		goto err_mac_addr;
15905 	}
15906 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15907 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15908 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15909 	if (is_valid_ether_addr(hw->mac.port_addr))
15910 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15911 
15912 	i40e_ptp_alloc_pins(pf);
15913 	pci_set_drvdata(pdev, pf);
15914 	pci_save_state(pdev);
15915 
15916 #ifdef CONFIG_I40E_DCB
15917 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15918 	(!status &&
15919 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15920 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15921 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15922 	dev_info(&pdev->dev,
15923 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15924 			"FW LLDP is disabled\n" :
15925 			"FW LLDP is enabled\n");
15926 
15927 	/* Enable FW to write default DCB config on link-up */
15928 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15929 
15930 	err = i40e_init_pf_dcb(pf);
15931 	if (err) {
15932 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15933 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15934 		/* Continue without DCB enabled */
15935 	}
15936 #endif /* CONFIG_I40E_DCB */
15937 
15938 	/* set up periodic task facility */
15939 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15940 	pf->service_timer_period = HZ;
15941 
15942 	INIT_WORK(&pf->service_task, i40e_service_task);
15943 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15944 
15945 	/* NVM bit on means WoL disabled for the port */
15946 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15947 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15948 		pf->wol_en = false;
15949 	else
15950 		pf->wol_en = true;
15951 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15952 
15953 	/* set up the main switch operations */
15954 	i40e_determine_queue_usage(pf);
15955 	err = i40e_init_interrupt_scheme(pf);
15956 	if (err)
15957 		goto err_switch_setup;
15958 
15959 	/* Reduce Tx and Rx pairs for kdump
15960 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15961 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15962 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15963 	 */
15964 	if (is_kdump_kernel())
15965 		pf->num_lan_msix = 1;
15966 
15967 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15968 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15969 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15970 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15971 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15972 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15973 						    UDP_TUNNEL_TYPE_GENEVE;
15974 
15975 	/* The number of VSIs reported by the FW is the minimum guaranteed
15976 	 * to us; HW supports far more and we share the remaining pool with
15977 	 * the other PFs. We allocate space for more than the guarantee with
15978 	 * the understanding that we might not get them all later.
15979 	 */
15980 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15981 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15982 	else
15983 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15984 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15985 		dev_warn(&pf->pdev->dev,
15986 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15987 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15988 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15989 	}
15990 
15991 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15992 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15993 			  GFP_KERNEL);
15994 	if (!pf->vsi) {
15995 		err = -ENOMEM;
15996 		goto err_switch_setup;
15997 	}
15998 
15999 #ifdef CONFIG_PCI_IOV
16000 	/* prep for VF support */
16001 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
16002 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
16003 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16004 		if (pci_num_vf(pdev))
16005 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
16006 	}
16007 #endif
16008 	err = i40e_setup_pf_switch(pf, false, false);
16009 	if (err) {
16010 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
16011 		goto err_vsis;
16012 	}
16013 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
16014 
16015 	/* if FDIR VSI was set up, start it now */
16016 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16017 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
16018 			i40e_vsi_open(pf->vsi[i]);
16019 			break;
16020 		}
16021 	}
16022 
16023 	/* The driver only wants link up/down and module qualification
16024 	 * reports from firmware.  Note the negative logic.
16025 	 */
16026 	err = i40e_aq_set_phy_int_mask(&pf->hw,
16027 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
16028 					 I40E_AQ_EVENT_MEDIA_NA |
16029 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
16030 	if (err)
16031 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
16032 			 ERR_PTR(err),
16033 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16034 
16035 	/* Reconfigure hardware for allowing smaller MSS in the case
16036 	 * of TSO, so that we avoid the MDD being fired and causing
16037 	 * a reset in the case of small MSS+TSO.
16038 	 */
16039 	val = rd32(hw, I40E_REG_MSS);
16040 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
16041 		val &= ~I40E_REG_MSS_MIN_MASK;
16042 		val |= I40E_64BYTE_MSS;
16043 		wr32(hw, I40E_REG_MSS, val);
16044 	}
16045 
16046 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
16047 		msleep(75);
16048 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
16049 		if (err)
16050 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
16051 				 ERR_PTR(err),
16052 				 i40e_aq_str(&pf->hw,
16053 					     pf->hw.aq.asq_last_status));
16054 	}
16055 	/* The main driver is (mostly) up and happy. We need to set this state
16056 	 * before setting up the misc vector or we get a race and the vector
16057 	 * ends up disabled forever.
16058 	 */
16059 	clear_bit(__I40E_DOWN, pf->state);
16060 
16061 	/* In case of MSIX we are going to setup the misc vector right here
16062 	 * to handle admin queue events etc. In case of legacy and MSI
16063 	 * the misc functionality and queue processing is combined in
16064 	 * the same vector and that gets setup at open.
16065 	 */
16066 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
16067 		err = i40e_setup_misc_vector(pf);
16068 		if (err) {
16069 			dev_info(&pdev->dev,
16070 				 "setup of misc vector failed: %d\n", err);
16071 			i40e_cloud_filter_exit(pf);
16072 			i40e_fdir_teardown(pf);
16073 			goto err_vsis;
16074 		}
16075 	}
16076 
16077 #ifdef CONFIG_PCI_IOV
16078 	/* prep for VF support */
16079 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
16080 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
16081 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16082 		/* disable link interrupts for VFs */
16083 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
16084 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
16085 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
16086 		i40e_flush(hw);
16087 
16088 		if (pci_num_vf(pdev)) {
16089 			dev_info(&pdev->dev,
16090 				 "Active VFs found, allocating resources.\n");
16091 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
16092 			if (err)
16093 				dev_info(&pdev->dev,
16094 					 "Error %d allocating resources for existing VFs\n",
16095 					 err);
16096 		}
16097 	}
16098 #endif /* CONFIG_PCI_IOV */
16099 
16100 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16101 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16102 						      pf->num_iwarp_msix,
16103 						      I40E_IWARP_IRQ_PILE_ID);
16104 		if (pf->iwarp_base_vector < 0) {
16105 			dev_info(&pdev->dev,
16106 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
16107 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
16108 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
16109 		}
16110 	}
16111 
16112 	i40e_dbg_pf_init(pf);
16113 
16114 	/* tell the firmware that we're starting */
16115 	i40e_send_version(pf);
16116 
16117 	/* since everything's happy, start the service_task timer */
16118 	mod_timer(&pf->service_timer,
16119 		  round_jiffies(jiffies + pf->service_timer_period));
16120 
16121 	/* add this PF to client device list and launch a client service task */
16122 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16123 		err = i40e_lan_add_device(pf);
16124 		if (err)
16125 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16126 				 err);
16127 	}
16128 
16129 #define PCI_SPEED_SIZE 8
16130 #define PCI_WIDTH_SIZE 8
16131 	/* Devices on the IOSF bus do not have this information
16132 	 * and will report PCI Gen 1 x 1 by default so don't bother
16133 	 * checking them.
16134 	 */
16135 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
16136 		char speed[PCI_SPEED_SIZE] = "Unknown";
16137 		char width[PCI_WIDTH_SIZE] = "Unknown";
16138 
16139 		/* Get the negotiated link width and speed from PCI config
16140 		 * space
16141 		 */
16142 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16143 					  &link_status);
16144 
16145 		i40e_set_pci_config_data(hw, link_status);
16146 
16147 		switch (hw->bus.speed) {
16148 		case i40e_bus_speed_8000:
16149 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16150 		case i40e_bus_speed_5000:
16151 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16152 		case i40e_bus_speed_2500:
16153 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16154 		default:
16155 			break;
16156 		}
16157 		switch (hw->bus.width) {
16158 		case i40e_bus_width_pcie_x8:
16159 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16160 		case i40e_bus_width_pcie_x4:
16161 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16162 		case i40e_bus_width_pcie_x2:
16163 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16164 		case i40e_bus_width_pcie_x1:
16165 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16166 		default:
16167 			break;
16168 		}
16169 
16170 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16171 			 speed, width);
16172 
16173 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16174 		    hw->bus.speed < i40e_bus_speed_8000) {
16175 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16176 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16177 		}
16178 	}
16179 
16180 	/* get the requested speeds from the fw */
16181 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16182 	if (err)
16183 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %pe last_status =  %s\n",
16184 			ERR_PTR(err),
16185 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16186 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16187 
16188 	/* set the FEC config due to the board capabilities */
16189 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
16190 
16191 	/* get the supported phy types from the fw */
16192 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16193 	if (err)
16194 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %pe last_status =  %s\n",
16195 			ERR_PTR(err),
16196 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16197 
16198 	/* make sure the MFS hasn't been set lower than the default */
16199 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16200 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
16201 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
16202 	if (val < MAX_FRAME_SIZE_DEFAULT)
16203 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
16204 			 pf->hw.port, val);
16205 
16206 	/* Add a filter to drop all Flow control frames from any VSI from being
16207 	 * transmitted. By doing so we stop a malicious VF from sending out
16208 	 * PAUSE or PFC frames and potentially controlling traffic for other
16209 	 * PF/VF VSIs.
16210 	 * The FW can still send Flow control frames if enabled.
16211 	 */
16212 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16213 						       pf->main_vsi_seid);
16214 
16215 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16216 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16217 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
16218 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16219 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
16220 	/* print a string summarizing features */
16221 	i40e_print_features(pf);
16222 
16223 	return 0;
16224 
16225 	/* Unwind what we've done if something failed in the setup */
16226 err_vsis:
16227 	set_bit(__I40E_DOWN, pf->state);
16228 	i40e_clear_interrupt_scheme(pf);
16229 	kfree(pf->vsi);
16230 err_switch_setup:
16231 	i40e_reset_interrupt_capability(pf);
16232 	timer_shutdown_sync(&pf->service_timer);
16233 err_mac_addr:
16234 err_configure_lan_hmc:
16235 	(void)i40e_shutdown_lan_hmc(hw);
16236 err_init_lan_hmc:
16237 	kfree(pf->qp_pile);
16238 err_sw_init:
16239 err_adminq_setup:
16240 err_pf_reset:
16241 	iounmap(hw->hw_addr);
16242 err_ioremap:
16243 	kfree(pf);
16244 err_pf_alloc:
16245 	pci_release_mem_regions(pdev);
16246 err_pci_reg:
16247 err_dma:
16248 	pci_disable_device(pdev);
16249 	return err;
16250 }
16251 
16252 /**
16253  * i40e_remove - Device removal routine
16254  * @pdev: PCI device information struct
16255  *
16256  * i40e_remove is called by the PCI subsystem to alert the driver
16257  * that is should release a PCI device.  This could be caused by a
16258  * Hot-Plug event, or because the driver is going to be removed from
16259  * memory.
16260  **/
16261 static void i40e_remove(struct pci_dev *pdev)
16262 {
16263 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16264 	struct i40e_hw *hw = &pf->hw;
16265 	int ret_code;
16266 	int i;
16267 
16268 	i40e_dbg_pf_exit(pf);
16269 
16270 	i40e_ptp_stop(pf);
16271 
16272 	/* Disable RSS in hw */
16273 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16274 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16275 
16276 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16277 	 * flags, once they are set, i40e_rebuild should not be called as
16278 	 * i40e_prep_for_reset always returns early.
16279 	 */
16280 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16281 		usleep_range(1000, 2000);
16282 	set_bit(__I40E_IN_REMOVE, pf->state);
16283 
16284 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
16285 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16286 		i40e_free_vfs(pf);
16287 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
16288 	}
16289 	/* no more scheduling of any task */
16290 	set_bit(__I40E_SUSPENDED, pf->state);
16291 	set_bit(__I40E_DOWN, pf->state);
16292 	if (pf->service_timer.function)
16293 		timer_shutdown_sync(&pf->service_timer);
16294 	if (pf->service_task.func)
16295 		cancel_work_sync(&pf->service_task);
16296 
16297 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16298 		struct i40e_vsi *vsi = pf->vsi[0];
16299 
16300 		/* We know that we have allocated only one vsi for this PF,
16301 		 * it was just for registering netdevice, so the interface
16302 		 * could be visible in the 'ifconfig' output
16303 		 */
16304 		unregister_netdev(vsi->netdev);
16305 		free_netdev(vsi->netdev);
16306 
16307 		goto unmap;
16308 	}
16309 
16310 	/* Client close must be called explicitly here because the timer
16311 	 * has been stopped.
16312 	 */
16313 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16314 
16315 	i40e_fdir_teardown(pf);
16316 
16317 	/* If there is a switch structure or any orphans, remove them.
16318 	 * This will leave only the PF's VSI remaining.
16319 	 */
16320 	for (i = 0; i < I40E_MAX_VEB; i++) {
16321 		if (!pf->veb[i])
16322 			continue;
16323 
16324 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16325 		    pf->veb[i]->uplink_seid == 0)
16326 			i40e_switch_branch_release(pf->veb[i]);
16327 	}
16328 
16329 	/* Now we can shutdown the PF's VSIs, just before we kill
16330 	 * adminq and hmc.
16331 	 */
16332 	for (i = pf->num_alloc_vsi; i--;)
16333 		if (pf->vsi[i]) {
16334 			i40e_vsi_close(pf->vsi[i]);
16335 			i40e_vsi_release(pf->vsi[i]);
16336 			pf->vsi[i] = NULL;
16337 		}
16338 
16339 	i40e_cloud_filter_exit(pf);
16340 
16341 	/* remove attached clients */
16342 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16343 		ret_code = i40e_lan_del_device(pf);
16344 		if (ret_code)
16345 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16346 				 ret_code);
16347 	}
16348 
16349 	/* shutdown and destroy the HMC */
16350 	if (hw->hmc.hmc_obj) {
16351 		ret_code = i40e_shutdown_lan_hmc(hw);
16352 		if (ret_code)
16353 			dev_warn(&pdev->dev,
16354 				 "Failed to destroy the HMC resources: %d\n",
16355 				 ret_code);
16356 	}
16357 
16358 unmap:
16359 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16360 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16361 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16362 		free_irq(pf->pdev->irq, pf);
16363 
16364 	/* shutdown the adminq */
16365 	i40e_shutdown_adminq(hw);
16366 
16367 	/* destroy the locks only once, here */
16368 	mutex_destroy(&hw->aq.arq_mutex);
16369 	mutex_destroy(&hw->aq.asq_mutex);
16370 
16371 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16372 	rtnl_lock();
16373 	i40e_clear_interrupt_scheme(pf);
16374 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16375 		if (pf->vsi[i]) {
16376 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16377 				i40e_vsi_clear_rings(pf->vsi[i]);
16378 			i40e_vsi_clear(pf->vsi[i]);
16379 			pf->vsi[i] = NULL;
16380 		}
16381 	}
16382 	rtnl_unlock();
16383 
16384 	for (i = 0; i < I40E_MAX_VEB; i++) {
16385 		kfree(pf->veb[i]);
16386 		pf->veb[i] = NULL;
16387 	}
16388 
16389 	kfree(pf->qp_pile);
16390 	kfree(pf->vsi);
16391 
16392 	iounmap(hw->hw_addr);
16393 	kfree(pf);
16394 	pci_release_mem_regions(pdev);
16395 
16396 	pci_disable_device(pdev);
16397 }
16398 
16399 /**
16400  * i40e_pci_error_detected - warning that something funky happened in PCI land
16401  * @pdev: PCI device information struct
16402  * @error: the type of PCI error
16403  *
16404  * Called to warn that something happened and the error handling steps
16405  * are in progress.  Allows the driver to quiesce things, be ready for
16406  * remediation.
16407  **/
16408 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16409 						pci_channel_state_t error)
16410 {
16411 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16412 
16413 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16414 
16415 	if (!pf) {
16416 		dev_info(&pdev->dev,
16417 			 "Cannot recover - error happened during device probe\n");
16418 		return PCI_ERS_RESULT_DISCONNECT;
16419 	}
16420 
16421 	/* shutdown all operations */
16422 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16423 		i40e_prep_for_reset(pf);
16424 
16425 	/* Request a slot reset */
16426 	return PCI_ERS_RESULT_NEED_RESET;
16427 }
16428 
16429 /**
16430  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16431  * @pdev: PCI device information struct
16432  *
16433  * Called to find if the driver can work with the device now that
16434  * the pci slot has been reset.  If a basic connection seems good
16435  * (registers are readable and have sane content) then return a
16436  * happy little PCI_ERS_RESULT_xxx.
16437  **/
16438 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16439 {
16440 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16441 	pci_ers_result_t result;
16442 	u32 reg;
16443 
16444 	dev_dbg(&pdev->dev, "%s\n", __func__);
16445 	if (pci_enable_device_mem(pdev)) {
16446 		dev_info(&pdev->dev,
16447 			 "Cannot re-enable PCI device after reset.\n");
16448 		result = PCI_ERS_RESULT_DISCONNECT;
16449 	} else {
16450 		pci_set_master(pdev);
16451 		pci_restore_state(pdev);
16452 		pci_save_state(pdev);
16453 		pci_wake_from_d3(pdev, false);
16454 
16455 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16456 		if (reg == 0)
16457 			result = PCI_ERS_RESULT_RECOVERED;
16458 		else
16459 			result = PCI_ERS_RESULT_DISCONNECT;
16460 	}
16461 
16462 	return result;
16463 }
16464 
16465 /**
16466  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16467  * @pdev: PCI device information struct
16468  */
16469 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16470 {
16471 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16472 
16473 	i40e_prep_for_reset(pf);
16474 }
16475 
16476 /**
16477  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16478  * @pdev: PCI device information struct
16479  */
16480 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16481 {
16482 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16483 
16484 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16485 		return;
16486 
16487 	i40e_reset_and_rebuild(pf, false, false);
16488 #ifdef CONFIG_PCI_IOV
16489 	i40e_restore_all_vfs_msi_state(pdev);
16490 #endif /* CONFIG_PCI_IOV */
16491 }
16492 
16493 /**
16494  * i40e_pci_error_resume - restart operations after PCI error recovery
16495  * @pdev: PCI device information struct
16496  *
16497  * Called to allow the driver to bring things back up after PCI error
16498  * and/or reset recovery has finished.
16499  **/
16500 static void i40e_pci_error_resume(struct pci_dev *pdev)
16501 {
16502 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16503 
16504 	dev_dbg(&pdev->dev, "%s\n", __func__);
16505 	if (test_bit(__I40E_SUSPENDED, pf->state))
16506 		return;
16507 
16508 	i40e_handle_reset_warning(pf, false);
16509 }
16510 
16511 /**
16512  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16513  * using the mac_address_write admin q function
16514  * @pf: pointer to i40e_pf struct
16515  **/
16516 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16517 {
16518 	struct i40e_hw *hw = &pf->hw;
16519 	u8 mac_addr[6];
16520 	u16 flags = 0;
16521 	int ret;
16522 
16523 	/* Get current MAC address in case it's an LAA */
16524 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16525 		ether_addr_copy(mac_addr,
16526 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16527 	} else {
16528 		dev_err(&pf->pdev->dev,
16529 			"Failed to retrieve MAC address; using default\n");
16530 		ether_addr_copy(mac_addr, hw->mac.addr);
16531 	}
16532 
16533 	/* The FW expects the mac address write cmd to first be called with
16534 	 * one of these flags before calling it again with the multicast
16535 	 * enable flags.
16536 	 */
16537 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16538 
16539 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16540 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16541 
16542 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16543 	if (ret) {
16544 		dev_err(&pf->pdev->dev,
16545 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16546 		return;
16547 	}
16548 
16549 	flags = I40E_AQC_MC_MAG_EN
16550 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16551 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16552 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16553 	if (ret)
16554 		dev_err(&pf->pdev->dev,
16555 			"Failed to enable Multicast Magic Packet wake up\n");
16556 }
16557 
16558 /**
16559  * i40e_shutdown - PCI callback for shutting down
16560  * @pdev: PCI device information struct
16561  **/
16562 static void i40e_shutdown(struct pci_dev *pdev)
16563 {
16564 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16565 	struct i40e_hw *hw = &pf->hw;
16566 
16567 	set_bit(__I40E_SUSPENDED, pf->state);
16568 	set_bit(__I40E_DOWN, pf->state);
16569 
16570 	del_timer_sync(&pf->service_timer);
16571 	cancel_work_sync(&pf->service_task);
16572 	i40e_cloud_filter_exit(pf);
16573 	i40e_fdir_teardown(pf);
16574 
16575 	/* Client close must be called explicitly here because the timer
16576 	 * has been stopped.
16577 	 */
16578 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16579 
16580 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16581 		i40e_enable_mc_magic_wake(pf);
16582 
16583 	i40e_prep_for_reset(pf);
16584 
16585 	wr32(hw, I40E_PFPM_APM,
16586 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16587 	wr32(hw, I40E_PFPM_WUFC,
16588 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16589 
16590 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16591 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16592 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16593 		free_irq(pf->pdev->irq, pf);
16594 
16595 	/* Since we're going to destroy queues during the
16596 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16597 	 * whole section
16598 	 */
16599 	rtnl_lock();
16600 	i40e_clear_interrupt_scheme(pf);
16601 	rtnl_unlock();
16602 
16603 	if (system_state == SYSTEM_POWER_OFF) {
16604 		pci_wake_from_d3(pdev, pf->wol_en);
16605 		pci_set_power_state(pdev, PCI_D3hot);
16606 	}
16607 }
16608 
16609 /**
16610  * i40e_suspend - PM callback for moving to D3
16611  * @dev: generic device information structure
16612  **/
16613 static int __maybe_unused i40e_suspend(struct device *dev)
16614 {
16615 	struct i40e_pf *pf = dev_get_drvdata(dev);
16616 	struct i40e_hw *hw = &pf->hw;
16617 
16618 	/* If we're already suspended, then there is nothing to do */
16619 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16620 		return 0;
16621 
16622 	set_bit(__I40E_DOWN, pf->state);
16623 
16624 	/* Ensure service task will not be running */
16625 	del_timer_sync(&pf->service_timer);
16626 	cancel_work_sync(&pf->service_task);
16627 
16628 	/* Client close must be called explicitly here because the timer
16629 	 * has been stopped.
16630 	 */
16631 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16632 
16633 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16634 		i40e_enable_mc_magic_wake(pf);
16635 
16636 	/* Since we're going to destroy queues during the
16637 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16638 	 * whole section
16639 	 */
16640 	rtnl_lock();
16641 
16642 	i40e_prep_for_reset(pf);
16643 
16644 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16645 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16646 
16647 	/* Clear the interrupt scheme and release our IRQs so that the system
16648 	 * can safely hibernate even when there are a large number of CPUs.
16649 	 * Otherwise hibernation might fail when mapping all the vectors back
16650 	 * to CPU0.
16651 	 */
16652 	i40e_clear_interrupt_scheme(pf);
16653 
16654 	rtnl_unlock();
16655 
16656 	return 0;
16657 }
16658 
16659 /**
16660  * i40e_resume - PM callback for waking up from D3
16661  * @dev: generic device information structure
16662  **/
16663 static int __maybe_unused i40e_resume(struct device *dev)
16664 {
16665 	struct i40e_pf *pf = dev_get_drvdata(dev);
16666 	int err;
16667 
16668 	/* If we're not suspended, then there is nothing to do */
16669 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16670 		return 0;
16671 
16672 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16673 	 * since we're going to be restoring queues
16674 	 */
16675 	rtnl_lock();
16676 
16677 	/* We cleared the interrupt scheme when we suspended, so we need to
16678 	 * restore it now to resume device functionality.
16679 	 */
16680 	err = i40e_restore_interrupt_scheme(pf);
16681 	if (err) {
16682 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16683 			err);
16684 	}
16685 
16686 	clear_bit(__I40E_DOWN, pf->state);
16687 	i40e_reset_and_rebuild(pf, false, true);
16688 
16689 	rtnl_unlock();
16690 
16691 	/* Clear suspended state last after everything is recovered */
16692 	clear_bit(__I40E_SUSPENDED, pf->state);
16693 
16694 	/* Restart the service task */
16695 	mod_timer(&pf->service_timer,
16696 		  round_jiffies(jiffies + pf->service_timer_period));
16697 
16698 	return 0;
16699 }
16700 
16701 static const struct pci_error_handlers i40e_err_handler = {
16702 	.error_detected = i40e_pci_error_detected,
16703 	.slot_reset = i40e_pci_error_slot_reset,
16704 	.reset_prepare = i40e_pci_error_reset_prepare,
16705 	.reset_done = i40e_pci_error_reset_done,
16706 	.resume = i40e_pci_error_resume,
16707 };
16708 
16709 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16710 
16711 static struct pci_driver i40e_driver = {
16712 	.name     = i40e_driver_name,
16713 	.id_table = i40e_pci_tbl,
16714 	.probe    = i40e_probe,
16715 	.remove   = i40e_remove,
16716 	.driver   = {
16717 		.pm = &i40e_pm_ops,
16718 	},
16719 	.shutdown = i40e_shutdown,
16720 	.err_handler = &i40e_err_handler,
16721 	.sriov_configure = i40e_pci_sriov_configure,
16722 };
16723 
16724 /**
16725  * i40e_init_module - Driver registration routine
16726  *
16727  * i40e_init_module is the first routine called when the driver is
16728  * loaded. All it does is register with the PCI subsystem.
16729  **/
16730 static int __init i40e_init_module(void)
16731 {
16732 	int err;
16733 
16734 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16735 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16736 
16737 	/* There is no need to throttle the number of active tasks because
16738 	 * each device limits its own task using a state bit for scheduling
16739 	 * the service task, and the device tasks do not interfere with each
16740 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16741 	 * since we need to be able to guarantee forward progress even under
16742 	 * memory pressure.
16743 	 */
16744 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16745 	if (!i40e_wq) {
16746 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16747 		return -ENOMEM;
16748 	}
16749 
16750 	i40e_dbg_init();
16751 	err = pci_register_driver(&i40e_driver);
16752 	if (err) {
16753 		destroy_workqueue(i40e_wq);
16754 		i40e_dbg_exit();
16755 		return err;
16756 	}
16757 
16758 	return 0;
16759 }
16760 module_init(i40e_init_module);
16761 
16762 /**
16763  * i40e_exit_module - Driver exit cleanup routine
16764  *
16765  * i40e_exit_module is called just before the driver is removed
16766  * from memory.
16767  **/
16768 static void __exit i40e_exit_module(void)
16769 {
16770 	pci_unregister_driver(&i40e_driver);
16771 	destroy_workqueue(i40e_wq);
16772 	ida_destroy(&i40e_client_ida);
16773 	i40e_dbg_exit();
16774 }
16775 module_exit(i40e_exit_module);
16776