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_10G_BASE_T), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
86 	/* required last entry */
87 	{0, }
88 };
89 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
90 
91 #define I40E_MAX_VF_COUNT 128
92 static int debug = -1;
93 module_param(debug, uint, 0);
94 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
95 
96 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
97 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
98 MODULE_LICENSE("GPL v2");
99 
100 static struct workqueue_struct *i40e_wq;
101 
102 /**
103  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
104  * @hw:   pointer to the HW structure
105  * @mem:  ptr to mem struct to fill out
106  * @size: size of memory requested
107  * @alignment: what to align the allocation to
108  **/
109 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
110 			    u64 size, u32 alignment)
111 {
112 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
113 
114 	mem->size = ALIGN(size, alignment);
115 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
116 				     GFP_KERNEL);
117 	if (!mem->va)
118 		return -ENOMEM;
119 
120 	return 0;
121 }
122 
123 /**
124  * i40e_free_dma_mem_d - OS specific memory free for shared code
125  * @hw:   pointer to the HW structure
126  * @mem:  ptr to mem struct to free
127  **/
128 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
129 {
130 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
131 
132 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
133 	mem->va = NULL;
134 	mem->pa = 0;
135 	mem->size = 0;
136 
137 	return 0;
138 }
139 
140 /**
141  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
142  * @hw:   pointer to the HW structure
143  * @mem:  ptr to mem struct to fill out
144  * @size: size of memory requested
145  **/
146 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
147 			     u32 size)
148 {
149 	mem->size = size;
150 	mem->va = kzalloc(size, GFP_KERNEL);
151 
152 	if (!mem->va)
153 		return -ENOMEM;
154 
155 	return 0;
156 }
157 
158 /**
159  * i40e_free_virt_mem_d - OS specific memory free for shared code
160  * @hw:   pointer to the HW structure
161  * @mem:  ptr to mem struct to free
162  **/
163 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
164 {
165 	/* it's ok to kfree a NULL pointer */
166 	kfree(mem->va);
167 	mem->va = NULL;
168 	mem->size = 0;
169 
170 	return 0;
171 }
172 
173 /**
174  * i40e_get_lump - find a lump of free generic resource
175  * @pf: board private structure
176  * @pile: the pile of resource to search
177  * @needed: the number of items needed
178  * @id: an owner id to stick on the items assigned
179  *
180  * Returns the base item index of the lump, or negative for error
181  *
182  * The search_hint trick and lack of advanced fit-finding only work
183  * because we're highly likely to have all the same size lump requests.
184  * Linear search time and any fragmentation should be minimal.
185  **/
186 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
187 			 u16 needed, u16 id)
188 {
189 	int ret = -ENOMEM;
190 	int i, j;
191 
192 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
193 		dev_info(&pf->pdev->dev,
194 			 "param err: pile=%s needed=%d id=0x%04x\n",
195 			 pile ? "<valid>" : "<null>", needed, id);
196 		return -EINVAL;
197 	}
198 
199 	/* start the linear search with an imperfect hint */
200 	i = pile->search_hint;
201 	while (i < pile->num_entries) {
202 		/* skip already allocated entries */
203 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
204 			i++;
205 			continue;
206 		}
207 
208 		/* do we have enough in this lump? */
209 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
210 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
211 				break;
212 		}
213 
214 		if (j == needed) {
215 			/* there was enough, so assign it to the requestor */
216 			for (j = 0; j < needed; j++)
217 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
218 			ret = i;
219 			pile->search_hint = i + j;
220 			break;
221 		}
222 
223 		/* not enough, so skip over it and continue looking */
224 		i += j;
225 	}
226 
227 	return ret;
228 }
229 
230 /**
231  * i40e_put_lump - return a lump of generic resource
232  * @pile: the pile of resource to search
233  * @index: the base item index
234  * @id: the owner id of the items assigned
235  *
236  * Returns the count of items in the lump
237  **/
238 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
239 {
240 	int valid_id = (id | I40E_PILE_VALID_BIT);
241 	int count = 0;
242 	int i;
243 
244 	if (!pile || index >= pile->num_entries)
245 		return -EINVAL;
246 
247 	for (i = index;
248 	     i < pile->num_entries && pile->list[i] == valid_id;
249 	     i++) {
250 		pile->list[i] = 0;
251 		count++;
252 	}
253 
254 	if (count && index < pile->search_hint)
255 		pile->search_hint = index;
256 
257 	return count;
258 }
259 
260 /**
261  * i40e_find_vsi_from_id - searches for the vsi with the given id
262  * @pf: the pf structure to search for the vsi
263  * @id: id of the vsi it is searching for
264  **/
265 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
266 {
267 	int i;
268 
269 	for (i = 0; i < pf->num_alloc_vsi; i++)
270 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
271 			return pf->vsi[i];
272 
273 	return NULL;
274 }
275 
276 /**
277  * i40e_service_event_schedule - Schedule the service task to wake up
278  * @pf: board private structure
279  *
280  * If not already scheduled, this puts the task into the work queue
281  **/
282 void i40e_service_event_schedule(struct i40e_pf *pf)
283 {
284 	if ((!test_bit(__I40E_DOWN, pf->state) &&
285 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
286 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
287 		queue_work(i40e_wq, &pf->service_task);
288 }
289 
290 /**
291  * i40e_tx_timeout - Respond to a Tx Hang
292  * @netdev: network interface device structure
293  * @txqueue: queue number timing out
294  *
295  * If any port has noticed a Tx timeout, it is likely that the whole
296  * device is munged, not just the one netdev port, so go for the full
297  * reset.
298  **/
299 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
300 {
301 	struct i40e_netdev_priv *np = netdev_priv(netdev);
302 	struct i40e_vsi *vsi = np->vsi;
303 	struct i40e_pf *pf = vsi->back;
304 	struct i40e_ring *tx_ring = NULL;
305 	unsigned int i;
306 	u32 head, val;
307 
308 	pf->tx_timeout_count++;
309 
310 	/* with txqueue index, find the tx_ring struct */
311 	for (i = 0; i < vsi->num_queue_pairs; i++) {
312 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
313 			if (txqueue ==
314 			    vsi->tx_rings[i]->queue_index) {
315 				tx_ring = vsi->tx_rings[i];
316 				break;
317 			}
318 		}
319 	}
320 
321 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
322 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
323 	else if (time_before(jiffies,
324 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
325 		return;   /* don't do any new action before the next timeout */
326 
327 	/* don't kick off another recovery if one is already pending */
328 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
329 		return;
330 
331 	if (tx_ring) {
332 		head = i40e_get_head(tx_ring);
333 		/* Read interrupt register */
334 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
335 			val = rd32(&pf->hw,
336 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
337 						tx_ring->vsi->base_vector - 1));
338 		else
339 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
340 
341 		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",
342 			    vsi->seid, txqueue, tx_ring->next_to_clean,
343 			    head, tx_ring->next_to_use,
344 			    readl(tx_ring->tail), val);
345 	}
346 
347 	pf->tx_timeout_last_recovery = jiffies;
348 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
349 		    pf->tx_timeout_recovery_level, txqueue);
350 
351 	switch (pf->tx_timeout_recovery_level) {
352 	case 1:
353 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
354 		break;
355 	case 2:
356 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
357 		break;
358 	case 3:
359 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
360 		break;
361 	default:
362 		netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
363 		break;
364 	}
365 
366 	i40e_service_event_schedule(pf);
367 	pf->tx_timeout_recovery_level++;
368 }
369 
370 /**
371  * i40e_get_vsi_stats_struct - Get System Network Statistics
372  * @vsi: the VSI we care about
373  *
374  * Returns the address of the device statistics structure.
375  * The statistics are actually updated from the service task.
376  **/
377 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
378 {
379 	return &vsi->net_stats;
380 }
381 
382 /**
383  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
384  * @ring: Tx ring to get statistics from
385  * @stats: statistics entry to be updated
386  **/
387 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
388 					    struct rtnl_link_stats64 *stats)
389 {
390 	u64 bytes, packets;
391 	unsigned int start;
392 
393 	do {
394 		start = u64_stats_fetch_begin_irq(&ring->syncp);
395 		packets = ring->stats.packets;
396 		bytes   = ring->stats.bytes;
397 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
398 
399 	stats->tx_packets += packets;
400 	stats->tx_bytes   += bytes;
401 }
402 
403 /**
404  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
405  * @netdev: network interface device structure
406  * @stats: data structure to store statistics
407  *
408  * Returns the address of the device statistics structure.
409  * The statistics are actually updated from the service task.
410  **/
411 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
412 				  struct rtnl_link_stats64 *stats)
413 {
414 	struct i40e_netdev_priv *np = netdev_priv(netdev);
415 	struct i40e_vsi *vsi = np->vsi;
416 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
417 	struct i40e_ring *ring;
418 	int i;
419 
420 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
421 		return;
422 
423 	if (!vsi->tx_rings)
424 		return;
425 
426 	rcu_read_lock();
427 	for (i = 0; i < vsi->num_queue_pairs; i++) {
428 		u64 bytes, packets;
429 		unsigned int start;
430 
431 		ring = READ_ONCE(vsi->tx_rings[i]);
432 		if (!ring)
433 			continue;
434 		i40e_get_netdev_stats_struct_tx(ring, stats);
435 
436 		if (i40e_enabled_xdp_vsi(vsi)) {
437 			ring = READ_ONCE(vsi->xdp_rings[i]);
438 			if (!ring)
439 				continue;
440 			i40e_get_netdev_stats_struct_tx(ring, stats);
441 		}
442 
443 		ring = READ_ONCE(vsi->rx_rings[i]);
444 		if (!ring)
445 			continue;
446 		do {
447 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
448 			packets = ring->stats.packets;
449 			bytes   = ring->stats.bytes;
450 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
451 
452 		stats->rx_packets += packets;
453 		stats->rx_bytes   += bytes;
454 
455 	}
456 	rcu_read_unlock();
457 
458 	/* following stats updated by i40e_watchdog_subtask() */
459 	stats->multicast	= vsi_stats->multicast;
460 	stats->tx_errors	= vsi_stats->tx_errors;
461 	stats->tx_dropped	= vsi_stats->tx_dropped;
462 	stats->rx_errors	= vsi_stats->rx_errors;
463 	stats->rx_dropped	= vsi_stats->rx_dropped;
464 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
465 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
466 }
467 
468 /**
469  * i40e_vsi_reset_stats - Resets all stats of the given vsi
470  * @vsi: the VSI to have its stats reset
471  **/
472 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
473 {
474 	struct rtnl_link_stats64 *ns;
475 	int i;
476 
477 	if (!vsi)
478 		return;
479 
480 	ns = i40e_get_vsi_stats_struct(vsi);
481 	memset(ns, 0, sizeof(*ns));
482 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
483 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
484 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
485 	if (vsi->rx_rings && vsi->rx_rings[0]) {
486 		for (i = 0; i < vsi->num_queue_pairs; i++) {
487 			memset(&vsi->rx_rings[i]->stats, 0,
488 			       sizeof(vsi->rx_rings[i]->stats));
489 			memset(&vsi->rx_rings[i]->rx_stats, 0,
490 			       sizeof(vsi->rx_rings[i]->rx_stats));
491 			memset(&vsi->tx_rings[i]->stats, 0,
492 			       sizeof(vsi->tx_rings[i]->stats));
493 			memset(&vsi->tx_rings[i]->tx_stats, 0,
494 			       sizeof(vsi->tx_rings[i]->tx_stats));
495 		}
496 	}
497 	vsi->stat_offsets_loaded = false;
498 }
499 
500 /**
501  * i40e_pf_reset_stats - Reset all of the stats for the given PF
502  * @pf: the PF to be reset
503  **/
504 void i40e_pf_reset_stats(struct i40e_pf *pf)
505 {
506 	int i;
507 
508 	memset(&pf->stats, 0, sizeof(pf->stats));
509 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
510 	pf->stat_offsets_loaded = false;
511 
512 	for (i = 0; i < I40E_MAX_VEB; i++) {
513 		if (pf->veb[i]) {
514 			memset(&pf->veb[i]->stats, 0,
515 			       sizeof(pf->veb[i]->stats));
516 			memset(&pf->veb[i]->stats_offsets, 0,
517 			       sizeof(pf->veb[i]->stats_offsets));
518 			memset(&pf->veb[i]->tc_stats, 0,
519 			       sizeof(pf->veb[i]->tc_stats));
520 			memset(&pf->veb[i]->tc_stats_offsets, 0,
521 			       sizeof(pf->veb[i]->tc_stats_offsets));
522 			pf->veb[i]->stat_offsets_loaded = false;
523 		}
524 	}
525 	pf->hw_csum_rx_error = 0;
526 }
527 
528 /**
529  * i40e_stat_update48 - read and update a 48 bit stat from the chip
530  * @hw: ptr to the hardware info
531  * @hireg: the high 32 bit reg to read
532  * @loreg: the low 32 bit reg to read
533  * @offset_loaded: has the initial offset been loaded yet
534  * @offset: ptr to current offset value
535  * @stat: ptr to the stat
536  *
537  * Since the device stats are not reset at PFReset, they likely will not
538  * be zeroed when the driver starts.  We'll save the first values read
539  * and use them as offsets to be subtracted from the raw values in order
540  * to report stats that count from zero.  In the process, we also manage
541  * the potential roll-over.
542  **/
543 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
544 			       bool offset_loaded, u64 *offset, u64 *stat)
545 {
546 	u64 new_data;
547 
548 	if (hw->device_id == I40E_DEV_ID_QEMU) {
549 		new_data = rd32(hw, loreg);
550 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
551 	} else {
552 		new_data = rd64(hw, loreg);
553 	}
554 	if (!offset_loaded)
555 		*offset = new_data;
556 	if (likely(new_data >= *offset))
557 		*stat = new_data - *offset;
558 	else
559 		*stat = (new_data + BIT_ULL(48)) - *offset;
560 	*stat &= 0xFFFFFFFFFFFFULL;
561 }
562 
563 /**
564  * i40e_stat_update32 - read and update a 32 bit stat from the chip
565  * @hw: ptr to the hardware info
566  * @reg: the hw reg to read
567  * @offset_loaded: has the initial offset been loaded yet
568  * @offset: ptr to current offset value
569  * @stat: ptr to the stat
570  **/
571 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
572 			       bool offset_loaded, u64 *offset, u64 *stat)
573 {
574 	u32 new_data;
575 
576 	new_data = rd32(hw, reg);
577 	if (!offset_loaded)
578 		*offset = new_data;
579 	if (likely(new_data >= *offset))
580 		*stat = (u32)(new_data - *offset);
581 	else
582 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
583 }
584 
585 /**
586  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
587  * @hw: ptr to the hardware info
588  * @reg: the hw reg to read and clear
589  * @stat: ptr to the stat
590  **/
591 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
592 {
593 	u32 new_data = rd32(hw, reg);
594 
595 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
596 	*stat += new_data;
597 }
598 
599 /**
600  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
601  * @vsi: the VSI to be updated
602  **/
603 void i40e_update_eth_stats(struct i40e_vsi *vsi)
604 {
605 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
606 	struct i40e_pf *pf = vsi->back;
607 	struct i40e_hw *hw = &pf->hw;
608 	struct i40e_eth_stats *oes;
609 	struct i40e_eth_stats *es;     /* device's eth stats */
610 
611 	es = &vsi->eth_stats;
612 	oes = &vsi->eth_stats_offsets;
613 
614 	/* Gather up the stats that the hw collects */
615 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
616 			   vsi->stat_offsets_loaded,
617 			   &oes->tx_errors, &es->tx_errors);
618 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
619 			   vsi->stat_offsets_loaded,
620 			   &oes->rx_discards, &es->rx_discards);
621 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
622 			   vsi->stat_offsets_loaded,
623 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
624 
625 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
626 			   I40E_GLV_GORCL(stat_idx),
627 			   vsi->stat_offsets_loaded,
628 			   &oes->rx_bytes, &es->rx_bytes);
629 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
630 			   I40E_GLV_UPRCL(stat_idx),
631 			   vsi->stat_offsets_loaded,
632 			   &oes->rx_unicast, &es->rx_unicast);
633 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
634 			   I40E_GLV_MPRCL(stat_idx),
635 			   vsi->stat_offsets_loaded,
636 			   &oes->rx_multicast, &es->rx_multicast);
637 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
638 			   I40E_GLV_BPRCL(stat_idx),
639 			   vsi->stat_offsets_loaded,
640 			   &oes->rx_broadcast, &es->rx_broadcast);
641 
642 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
643 			   I40E_GLV_GOTCL(stat_idx),
644 			   vsi->stat_offsets_loaded,
645 			   &oes->tx_bytes, &es->tx_bytes);
646 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
647 			   I40E_GLV_UPTCL(stat_idx),
648 			   vsi->stat_offsets_loaded,
649 			   &oes->tx_unicast, &es->tx_unicast);
650 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
651 			   I40E_GLV_MPTCL(stat_idx),
652 			   vsi->stat_offsets_loaded,
653 			   &oes->tx_multicast, &es->tx_multicast);
654 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
655 			   I40E_GLV_BPTCL(stat_idx),
656 			   vsi->stat_offsets_loaded,
657 			   &oes->tx_broadcast, &es->tx_broadcast);
658 	vsi->stat_offsets_loaded = true;
659 }
660 
661 /**
662  * i40e_update_veb_stats - Update Switch component statistics
663  * @veb: the VEB being updated
664  **/
665 void i40e_update_veb_stats(struct i40e_veb *veb)
666 {
667 	struct i40e_pf *pf = veb->pf;
668 	struct i40e_hw *hw = &pf->hw;
669 	struct i40e_eth_stats *oes;
670 	struct i40e_eth_stats *es;     /* device's eth stats */
671 	struct i40e_veb_tc_stats *veb_oes;
672 	struct i40e_veb_tc_stats *veb_es;
673 	int i, idx = 0;
674 
675 	idx = veb->stats_idx;
676 	es = &veb->stats;
677 	oes = &veb->stats_offsets;
678 	veb_es = &veb->tc_stats;
679 	veb_oes = &veb->tc_stats_offsets;
680 
681 	/* Gather up the stats that the hw collects */
682 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
683 			   veb->stat_offsets_loaded,
684 			   &oes->tx_discards, &es->tx_discards);
685 	if (hw->revision_id > 0)
686 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
687 				   veb->stat_offsets_loaded,
688 				   &oes->rx_unknown_protocol,
689 				   &es->rx_unknown_protocol);
690 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
691 			   veb->stat_offsets_loaded,
692 			   &oes->rx_bytes, &es->rx_bytes);
693 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
694 			   veb->stat_offsets_loaded,
695 			   &oes->rx_unicast, &es->rx_unicast);
696 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
697 			   veb->stat_offsets_loaded,
698 			   &oes->rx_multicast, &es->rx_multicast);
699 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
700 			   veb->stat_offsets_loaded,
701 			   &oes->rx_broadcast, &es->rx_broadcast);
702 
703 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
704 			   veb->stat_offsets_loaded,
705 			   &oes->tx_bytes, &es->tx_bytes);
706 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
707 			   veb->stat_offsets_loaded,
708 			   &oes->tx_unicast, &es->tx_unicast);
709 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
710 			   veb->stat_offsets_loaded,
711 			   &oes->tx_multicast, &es->tx_multicast);
712 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
713 			   veb->stat_offsets_loaded,
714 			   &oes->tx_broadcast, &es->tx_broadcast);
715 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
716 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
717 				   I40E_GLVEBTC_RPCL(i, idx),
718 				   veb->stat_offsets_loaded,
719 				   &veb_oes->tc_rx_packets[i],
720 				   &veb_es->tc_rx_packets[i]);
721 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
722 				   I40E_GLVEBTC_RBCL(i, idx),
723 				   veb->stat_offsets_loaded,
724 				   &veb_oes->tc_rx_bytes[i],
725 				   &veb_es->tc_rx_bytes[i]);
726 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
727 				   I40E_GLVEBTC_TPCL(i, idx),
728 				   veb->stat_offsets_loaded,
729 				   &veb_oes->tc_tx_packets[i],
730 				   &veb_es->tc_tx_packets[i]);
731 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
732 				   I40E_GLVEBTC_TBCL(i, idx),
733 				   veb->stat_offsets_loaded,
734 				   &veb_oes->tc_tx_bytes[i],
735 				   &veb_es->tc_tx_bytes[i]);
736 	}
737 	veb->stat_offsets_loaded = true;
738 }
739 
740 /**
741  * i40e_update_vsi_stats - Update the vsi statistics counters.
742  * @vsi: the VSI to be updated
743  *
744  * There are a few instances where we store the same stat in a
745  * couple of different structs.  This is partly because we have
746  * the netdev stats that need to be filled out, which is slightly
747  * different from the "eth_stats" defined by the chip and used in
748  * VF communications.  We sort it out here.
749  **/
750 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
751 {
752 	struct i40e_pf *pf = vsi->back;
753 	struct rtnl_link_stats64 *ons;
754 	struct rtnl_link_stats64 *ns;   /* netdev stats */
755 	struct i40e_eth_stats *oes;
756 	struct i40e_eth_stats *es;     /* device's eth stats */
757 	u32 tx_restart, tx_busy;
758 	struct i40e_ring *p;
759 	u32 rx_page, rx_buf;
760 	u64 bytes, packets;
761 	unsigned int start;
762 	u64 tx_linearize;
763 	u64 tx_force_wb;
764 	u64 rx_p, rx_b;
765 	u64 tx_p, tx_b;
766 	u16 q;
767 
768 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
769 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
770 		return;
771 
772 	ns = i40e_get_vsi_stats_struct(vsi);
773 	ons = &vsi->net_stats_offsets;
774 	es = &vsi->eth_stats;
775 	oes = &vsi->eth_stats_offsets;
776 
777 	/* Gather up the netdev and vsi stats that the driver collects
778 	 * on the fly during packet processing
779 	 */
780 	rx_b = rx_p = 0;
781 	tx_b = tx_p = 0;
782 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
783 	rx_page = 0;
784 	rx_buf = 0;
785 	rcu_read_lock();
786 	for (q = 0; q < vsi->num_queue_pairs; q++) {
787 		/* locate Tx ring */
788 		p = READ_ONCE(vsi->tx_rings[q]);
789 		if (!p)
790 			continue;
791 
792 		do {
793 			start = u64_stats_fetch_begin_irq(&p->syncp);
794 			packets = p->stats.packets;
795 			bytes = p->stats.bytes;
796 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
797 		tx_b += bytes;
798 		tx_p += packets;
799 		tx_restart += p->tx_stats.restart_queue;
800 		tx_busy += p->tx_stats.tx_busy;
801 		tx_linearize += p->tx_stats.tx_linearize;
802 		tx_force_wb += p->tx_stats.tx_force_wb;
803 
804 		/* locate Rx ring */
805 		p = READ_ONCE(vsi->rx_rings[q]);
806 		if (!p)
807 			continue;
808 
809 		do {
810 			start = u64_stats_fetch_begin_irq(&p->syncp);
811 			packets = p->stats.packets;
812 			bytes = p->stats.bytes;
813 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
814 		rx_b += bytes;
815 		rx_p += packets;
816 		rx_buf += p->rx_stats.alloc_buff_failed;
817 		rx_page += p->rx_stats.alloc_page_failed;
818 
819 		if (i40e_enabled_xdp_vsi(vsi)) {
820 			/* locate XDP ring */
821 			p = READ_ONCE(vsi->xdp_rings[q]);
822 			if (!p)
823 				continue;
824 
825 			do {
826 				start = u64_stats_fetch_begin_irq(&p->syncp);
827 				packets = p->stats.packets;
828 				bytes = p->stats.bytes;
829 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
830 			tx_b += bytes;
831 			tx_p += packets;
832 			tx_restart += p->tx_stats.restart_queue;
833 			tx_busy += p->tx_stats.tx_busy;
834 			tx_linearize += p->tx_stats.tx_linearize;
835 			tx_force_wb += p->tx_stats.tx_force_wb;
836 		}
837 	}
838 	rcu_read_unlock();
839 	vsi->tx_restart = tx_restart;
840 	vsi->tx_busy = tx_busy;
841 	vsi->tx_linearize = tx_linearize;
842 	vsi->tx_force_wb = tx_force_wb;
843 	vsi->rx_page_failed = rx_page;
844 	vsi->rx_buf_failed = rx_buf;
845 
846 	ns->rx_packets = rx_p;
847 	ns->rx_bytes = rx_b;
848 	ns->tx_packets = tx_p;
849 	ns->tx_bytes = tx_b;
850 
851 	/* update netdev stats from eth stats */
852 	i40e_update_eth_stats(vsi);
853 	ons->tx_errors = oes->tx_errors;
854 	ns->tx_errors = es->tx_errors;
855 	ons->multicast = oes->rx_multicast;
856 	ns->multicast = es->rx_multicast;
857 	ons->rx_dropped = oes->rx_discards;
858 	ns->rx_dropped = es->rx_discards;
859 	ons->tx_dropped = oes->tx_discards;
860 	ns->tx_dropped = es->tx_discards;
861 
862 	/* pull in a couple PF stats if this is the main vsi */
863 	if (vsi == pf->vsi[pf->lan_vsi]) {
864 		ns->rx_crc_errors = pf->stats.crc_errors;
865 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
866 		ns->rx_length_errors = pf->stats.rx_length_errors;
867 	}
868 }
869 
870 /**
871  * i40e_update_pf_stats - Update the PF statistics counters.
872  * @pf: the PF to be updated
873  **/
874 static void i40e_update_pf_stats(struct i40e_pf *pf)
875 {
876 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
877 	struct i40e_hw_port_stats *nsd = &pf->stats;
878 	struct i40e_hw *hw = &pf->hw;
879 	u32 val;
880 	int i;
881 
882 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
883 			   I40E_GLPRT_GORCL(hw->port),
884 			   pf->stat_offsets_loaded,
885 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
886 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
887 			   I40E_GLPRT_GOTCL(hw->port),
888 			   pf->stat_offsets_loaded,
889 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
890 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
891 			   pf->stat_offsets_loaded,
892 			   &osd->eth.rx_discards,
893 			   &nsd->eth.rx_discards);
894 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
895 			   I40E_GLPRT_UPRCL(hw->port),
896 			   pf->stat_offsets_loaded,
897 			   &osd->eth.rx_unicast,
898 			   &nsd->eth.rx_unicast);
899 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
900 			   I40E_GLPRT_MPRCL(hw->port),
901 			   pf->stat_offsets_loaded,
902 			   &osd->eth.rx_multicast,
903 			   &nsd->eth.rx_multicast);
904 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
905 			   I40E_GLPRT_BPRCL(hw->port),
906 			   pf->stat_offsets_loaded,
907 			   &osd->eth.rx_broadcast,
908 			   &nsd->eth.rx_broadcast);
909 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
910 			   I40E_GLPRT_UPTCL(hw->port),
911 			   pf->stat_offsets_loaded,
912 			   &osd->eth.tx_unicast,
913 			   &nsd->eth.tx_unicast);
914 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
915 			   I40E_GLPRT_MPTCL(hw->port),
916 			   pf->stat_offsets_loaded,
917 			   &osd->eth.tx_multicast,
918 			   &nsd->eth.tx_multicast);
919 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
920 			   I40E_GLPRT_BPTCL(hw->port),
921 			   pf->stat_offsets_loaded,
922 			   &osd->eth.tx_broadcast,
923 			   &nsd->eth.tx_broadcast);
924 
925 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
926 			   pf->stat_offsets_loaded,
927 			   &osd->tx_dropped_link_down,
928 			   &nsd->tx_dropped_link_down);
929 
930 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
931 			   pf->stat_offsets_loaded,
932 			   &osd->crc_errors, &nsd->crc_errors);
933 
934 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
935 			   pf->stat_offsets_loaded,
936 			   &osd->illegal_bytes, &nsd->illegal_bytes);
937 
938 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
939 			   pf->stat_offsets_loaded,
940 			   &osd->mac_local_faults,
941 			   &nsd->mac_local_faults);
942 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
943 			   pf->stat_offsets_loaded,
944 			   &osd->mac_remote_faults,
945 			   &nsd->mac_remote_faults);
946 
947 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
948 			   pf->stat_offsets_loaded,
949 			   &osd->rx_length_errors,
950 			   &nsd->rx_length_errors);
951 
952 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
953 			   pf->stat_offsets_loaded,
954 			   &osd->link_xon_rx, &nsd->link_xon_rx);
955 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
956 			   pf->stat_offsets_loaded,
957 			   &osd->link_xon_tx, &nsd->link_xon_tx);
958 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
959 			   pf->stat_offsets_loaded,
960 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
961 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
962 			   pf->stat_offsets_loaded,
963 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
964 
965 	for (i = 0; i < 8; i++) {
966 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
967 				   pf->stat_offsets_loaded,
968 				   &osd->priority_xoff_rx[i],
969 				   &nsd->priority_xoff_rx[i]);
970 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
971 				   pf->stat_offsets_loaded,
972 				   &osd->priority_xon_rx[i],
973 				   &nsd->priority_xon_rx[i]);
974 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
975 				   pf->stat_offsets_loaded,
976 				   &osd->priority_xon_tx[i],
977 				   &nsd->priority_xon_tx[i]);
978 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
979 				   pf->stat_offsets_loaded,
980 				   &osd->priority_xoff_tx[i],
981 				   &nsd->priority_xoff_tx[i]);
982 		i40e_stat_update32(hw,
983 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
984 				   pf->stat_offsets_loaded,
985 				   &osd->priority_xon_2_xoff[i],
986 				   &nsd->priority_xon_2_xoff[i]);
987 	}
988 
989 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
990 			   I40E_GLPRT_PRC64L(hw->port),
991 			   pf->stat_offsets_loaded,
992 			   &osd->rx_size_64, &nsd->rx_size_64);
993 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
994 			   I40E_GLPRT_PRC127L(hw->port),
995 			   pf->stat_offsets_loaded,
996 			   &osd->rx_size_127, &nsd->rx_size_127);
997 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
998 			   I40E_GLPRT_PRC255L(hw->port),
999 			   pf->stat_offsets_loaded,
1000 			   &osd->rx_size_255, &nsd->rx_size_255);
1001 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1002 			   I40E_GLPRT_PRC511L(hw->port),
1003 			   pf->stat_offsets_loaded,
1004 			   &osd->rx_size_511, &nsd->rx_size_511);
1005 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1006 			   I40E_GLPRT_PRC1023L(hw->port),
1007 			   pf->stat_offsets_loaded,
1008 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1009 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1010 			   I40E_GLPRT_PRC1522L(hw->port),
1011 			   pf->stat_offsets_loaded,
1012 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1013 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1014 			   I40E_GLPRT_PRC9522L(hw->port),
1015 			   pf->stat_offsets_loaded,
1016 			   &osd->rx_size_big, &nsd->rx_size_big);
1017 
1018 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1019 			   I40E_GLPRT_PTC64L(hw->port),
1020 			   pf->stat_offsets_loaded,
1021 			   &osd->tx_size_64, &nsd->tx_size_64);
1022 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1023 			   I40E_GLPRT_PTC127L(hw->port),
1024 			   pf->stat_offsets_loaded,
1025 			   &osd->tx_size_127, &nsd->tx_size_127);
1026 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1027 			   I40E_GLPRT_PTC255L(hw->port),
1028 			   pf->stat_offsets_loaded,
1029 			   &osd->tx_size_255, &nsd->tx_size_255);
1030 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1031 			   I40E_GLPRT_PTC511L(hw->port),
1032 			   pf->stat_offsets_loaded,
1033 			   &osd->tx_size_511, &nsd->tx_size_511);
1034 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1035 			   I40E_GLPRT_PTC1023L(hw->port),
1036 			   pf->stat_offsets_loaded,
1037 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1038 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1039 			   I40E_GLPRT_PTC1522L(hw->port),
1040 			   pf->stat_offsets_loaded,
1041 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1042 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1043 			   I40E_GLPRT_PTC9522L(hw->port),
1044 			   pf->stat_offsets_loaded,
1045 			   &osd->tx_size_big, &nsd->tx_size_big);
1046 
1047 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1048 			   pf->stat_offsets_loaded,
1049 			   &osd->rx_undersize, &nsd->rx_undersize);
1050 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1051 			   pf->stat_offsets_loaded,
1052 			   &osd->rx_fragments, &nsd->rx_fragments);
1053 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1054 			   pf->stat_offsets_loaded,
1055 			   &osd->rx_oversize, &nsd->rx_oversize);
1056 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1057 			   pf->stat_offsets_loaded,
1058 			   &osd->rx_jabber, &nsd->rx_jabber);
1059 
1060 	/* FDIR stats */
1061 	i40e_stat_update_and_clear32(hw,
1062 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1063 			&nsd->fd_atr_match);
1064 	i40e_stat_update_and_clear32(hw,
1065 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1066 			&nsd->fd_sb_match);
1067 	i40e_stat_update_and_clear32(hw,
1068 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1069 			&nsd->fd_atr_tunnel_match);
1070 
1071 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1072 	nsd->tx_lpi_status =
1073 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1074 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1075 	nsd->rx_lpi_status =
1076 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1077 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1078 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1079 			   pf->stat_offsets_loaded,
1080 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1081 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1082 			   pf->stat_offsets_loaded,
1083 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1084 
1085 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1086 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1087 		nsd->fd_sb_status = true;
1088 	else
1089 		nsd->fd_sb_status = false;
1090 
1091 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1092 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1093 		nsd->fd_atr_status = true;
1094 	else
1095 		nsd->fd_atr_status = false;
1096 
1097 	pf->stat_offsets_loaded = true;
1098 }
1099 
1100 /**
1101  * i40e_update_stats - Update the various statistics counters.
1102  * @vsi: the VSI to be updated
1103  *
1104  * Update the various stats for this VSI and its related entities.
1105  **/
1106 void i40e_update_stats(struct i40e_vsi *vsi)
1107 {
1108 	struct i40e_pf *pf = vsi->back;
1109 
1110 	if (vsi == pf->vsi[pf->lan_vsi])
1111 		i40e_update_pf_stats(pf);
1112 
1113 	i40e_update_vsi_stats(vsi);
1114 }
1115 
1116 /**
1117  * i40e_count_filters - counts VSI mac filters
1118  * @vsi: the VSI to be searched
1119  *
1120  * Returns count of mac filters
1121  **/
1122 int i40e_count_filters(struct i40e_vsi *vsi)
1123 {
1124 	struct i40e_mac_filter *f;
1125 	struct hlist_node *h;
1126 	int bkt;
1127 	int cnt = 0;
1128 
1129 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1130 		++cnt;
1131 
1132 	return cnt;
1133 }
1134 
1135 /**
1136  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1137  * @vsi: the VSI to be searched
1138  * @macaddr: the MAC address
1139  * @vlan: the vlan
1140  *
1141  * Returns ptr to the filter object or NULL
1142  **/
1143 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1144 						const u8 *macaddr, s16 vlan)
1145 {
1146 	struct i40e_mac_filter *f;
1147 	u64 key;
1148 
1149 	if (!vsi || !macaddr)
1150 		return NULL;
1151 
1152 	key = i40e_addr_to_hkey(macaddr);
1153 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1154 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1155 		    (vlan == f->vlan))
1156 			return f;
1157 	}
1158 	return NULL;
1159 }
1160 
1161 /**
1162  * i40e_find_mac - Find a mac addr in the macvlan filters list
1163  * @vsi: the VSI to be searched
1164  * @macaddr: the MAC address we are searching for
1165  *
1166  * Returns the first filter with the provided MAC address or NULL if
1167  * MAC address was not found
1168  **/
1169 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1170 {
1171 	struct i40e_mac_filter *f;
1172 	u64 key;
1173 
1174 	if (!vsi || !macaddr)
1175 		return NULL;
1176 
1177 	key = i40e_addr_to_hkey(macaddr);
1178 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1179 		if ((ether_addr_equal(macaddr, f->macaddr)))
1180 			return f;
1181 	}
1182 	return NULL;
1183 }
1184 
1185 /**
1186  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1187  * @vsi: the VSI to be searched
1188  *
1189  * Returns true if VSI is in vlan mode or false otherwise
1190  **/
1191 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1192 {
1193 	/* If we have a PVID, always operate in VLAN mode */
1194 	if (vsi->info.pvid)
1195 		return true;
1196 
1197 	/* We need to operate in VLAN mode whenever we have any filters with
1198 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1199 	 * time, incurring search cost repeatedly. However, we can notice two
1200 	 * things:
1201 	 *
1202 	 * 1) the only place where we can gain a VLAN filter is in
1203 	 *    i40e_add_filter.
1204 	 *
1205 	 * 2) the only place where filters are actually removed is in
1206 	 *    i40e_sync_filters_subtask.
1207 	 *
1208 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1209 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1210 	 * we have to perform the full search after deleting filters in
1211 	 * i40e_sync_filters_subtask, but we already have to search
1212 	 * filters here and can perform the check at the same time. This
1213 	 * results in avoiding embedding a loop for VLAN mode inside another
1214 	 * loop over all the filters, and should maintain correctness as noted
1215 	 * above.
1216 	 */
1217 	return vsi->has_vlan_filter;
1218 }
1219 
1220 /**
1221  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1222  * @vsi: the VSI to configure
1223  * @tmp_add_list: list of filters ready to be added
1224  * @tmp_del_list: list of filters ready to be deleted
1225  * @vlan_filters: the number of active VLAN filters
1226  *
1227  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1228  * behave as expected. If we have any active VLAN filters remaining or about
1229  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1230  * so that they only match against untagged traffic. If we no longer have any
1231  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1232  * so that they match against both tagged and untagged traffic. In this way,
1233  * we ensure that we correctly receive the desired traffic. This ensures that
1234  * when we have an active VLAN we will receive only untagged traffic and
1235  * traffic matching active VLANs. If we have no active VLANs then we will
1236  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1237  *
1238  * Finally, in a similar fashion, this function also corrects filters when
1239  * there is an active PVID assigned to this VSI.
1240  *
1241  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1242  *
1243  * This function is only expected to be called from within
1244  * i40e_sync_vsi_filters.
1245  *
1246  * NOTE: This function expects to be called while under the
1247  * mac_filter_hash_lock
1248  */
1249 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1250 					 struct hlist_head *tmp_add_list,
1251 					 struct hlist_head *tmp_del_list,
1252 					 int vlan_filters)
1253 {
1254 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1255 	struct i40e_mac_filter *f, *add_head;
1256 	struct i40e_new_mac_filter *new;
1257 	struct hlist_node *h;
1258 	int bkt, new_vlan;
1259 
1260 	/* To determine if a particular filter needs to be replaced we
1261 	 * have the three following conditions:
1262 	 *
1263 	 * a) if we have a PVID assigned, then all filters which are
1264 	 *    not marked as VLAN=PVID must be replaced with filters that
1265 	 *    are.
1266 	 * b) otherwise, if we have any active VLANS, all filters
1267 	 *    which are marked as VLAN=-1 must be replaced with
1268 	 *    filters marked as VLAN=0
1269 	 * c) finally, if we do not have any active VLANS, all filters
1270 	 *    which are marked as VLAN=0 must be replaced with filters
1271 	 *    marked as VLAN=-1
1272 	 */
1273 
1274 	/* Update the filters about to be added in place */
1275 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1276 		if (pvid && new->f->vlan != pvid)
1277 			new->f->vlan = pvid;
1278 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1279 			new->f->vlan = 0;
1280 		else if (!vlan_filters && new->f->vlan == 0)
1281 			new->f->vlan = I40E_VLAN_ANY;
1282 	}
1283 
1284 	/* Update the remaining active filters */
1285 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1286 		/* Combine the checks for whether a filter needs to be changed
1287 		 * and then determine the new VLAN inside the if block, in
1288 		 * order to avoid duplicating code for adding the new filter
1289 		 * then deleting the old filter.
1290 		 */
1291 		if ((pvid && f->vlan != pvid) ||
1292 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1293 		    (!vlan_filters && f->vlan == 0)) {
1294 			/* Determine the new vlan we will be adding */
1295 			if (pvid)
1296 				new_vlan = pvid;
1297 			else if (vlan_filters)
1298 				new_vlan = 0;
1299 			else
1300 				new_vlan = I40E_VLAN_ANY;
1301 
1302 			/* Create the new filter */
1303 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1304 			if (!add_head)
1305 				return -ENOMEM;
1306 
1307 			/* Create a temporary i40e_new_mac_filter */
1308 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1309 			if (!new)
1310 				return -ENOMEM;
1311 
1312 			new->f = add_head;
1313 			new->state = add_head->state;
1314 
1315 			/* Add the new filter to the tmp list */
1316 			hlist_add_head(&new->hlist, tmp_add_list);
1317 
1318 			/* Put the original filter into the delete list */
1319 			f->state = I40E_FILTER_REMOVE;
1320 			hash_del(&f->hlist);
1321 			hlist_add_head(&f->hlist, tmp_del_list);
1322 		}
1323 	}
1324 
1325 	vsi->has_vlan_filter = !!vlan_filters;
1326 
1327 	return 0;
1328 }
1329 
1330 /**
1331  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1332  * @vsi: the PF Main VSI - inappropriate for any other VSI
1333  * @macaddr: the MAC address
1334  *
1335  * Remove whatever filter the firmware set up so the driver can manage
1336  * its own filtering intelligently.
1337  **/
1338 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1339 {
1340 	struct i40e_aqc_remove_macvlan_element_data element;
1341 	struct i40e_pf *pf = vsi->back;
1342 
1343 	/* Only appropriate for the PF main VSI */
1344 	if (vsi->type != I40E_VSI_MAIN)
1345 		return;
1346 
1347 	memset(&element, 0, sizeof(element));
1348 	ether_addr_copy(element.mac_addr, macaddr);
1349 	element.vlan_tag = 0;
1350 	/* Ignore error returns, some firmware does it this way... */
1351 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1352 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1353 
1354 	memset(&element, 0, sizeof(element));
1355 	ether_addr_copy(element.mac_addr, macaddr);
1356 	element.vlan_tag = 0;
1357 	/* ...and some firmware does it this way. */
1358 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1359 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1360 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1361 }
1362 
1363 /**
1364  * i40e_add_filter - Add a mac/vlan filter to the VSI
1365  * @vsi: the VSI to be searched
1366  * @macaddr: the MAC address
1367  * @vlan: the vlan
1368  *
1369  * Returns ptr to the filter object or NULL when no memory available.
1370  *
1371  * NOTE: This function is expected to be called with mac_filter_hash_lock
1372  * being held.
1373  **/
1374 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1375 					const u8 *macaddr, s16 vlan)
1376 {
1377 	struct i40e_mac_filter *f;
1378 	u64 key;
1379 
1380 	if (!vsi || !macaddr)
1381 		return NULL;
1382 
1383 	f = i40e_find_filter(vsi, macaddr, vlan);
1384 	if (!f) {
1385 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1386 		if (!f)
1387 			return NULL;
1388 
1389 		/* Update the boolean indicating if we need to function in
1390 		 * VLAN mode.
1391 		 */
1392 		if (vlan >= 0)
1393 			vsi->has_vlan_filter = true;
1394 
1395 		ether_addr_copy(f->macaddr, macaddr);
1396 		f->vlan = vlan;
1397 		f->state = I40E_FILTER_NEW;
1398 		INIT_HLIST_NODE(&f->hlist);
1399 
1400 		key = i40e_addr_to_hkey(macaddr);
1401 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1402 
1403 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1404 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1405 	}
1406 
1407 	/* If we're asked to add a filter that has been marked for removal, it
1408 	 * is safe to simply restore it to active state. __i40e_del_filter
1409 	 * will have simply deleted any filters which were previously marked
1410 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1411 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1412 	 * task, just restore this filter to the ACTIVE state so that the
1413 	 * sync task leaves it in place
1414 	 */
1415 	if (f->state == I40E_FILTER_REMOVE)
1416 		f->state = I40E_FILTER_ACTIVE;
1417 
1418 	return f;
1419 }
1420 
1421 /**
1422  * __i40e_del_filter - Remove a specific filter from the VSI
1423  * @vsi: VSI to remove from
1424  * @f: the filter to remove from the list
1425  *
1426  * This function should be called instead of i40e_del_filter only if you know
1427  * the exact filter you will remove already, such as via i40e_find_filter or
1428  * i40e_find_mac.
1429  *
1430  * NOTE: This function is expected to be called with mac_filter_hash_lock
1431  * being held.
1432  * ANOTHER NOTE: This function MUST be called from within the context of
1433  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1434  * instead of list_for_each_entry().
1435  **/
1436 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1437 {
1438 	if (!f)
1439 		return;
1440 
1441 	/* If the filter was never added to firmware then we can just delete it
1442 	 * directly and we don't want to set the status to remove or else an
1443 	 * admin queue command will unnecessarily fire.
1444 	 */
1445 	if ((f->state == I40E_FILTER_FAILED) ||
1446 	    (f->state == I40E_FILTER_NEW)) {
1447 		hash_del(&f->hlist);
1448 		kfree(f);
1449 	} else {
1450 		f->state = I40E_FILTER_REMOVE;
1451 	}
1452 
1453 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1454 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1455 }
1456 
1457 /**
1458  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1459  * @vsi: the VSI to be searched
1460  * @macaddr: the MAC address
1461  * @vlan: the VLAN
1462  *
1463  * NOTE: This function is expected to be called with mac_filter_hash_lock
1464  * being held.
1465  * ANOTHER NOTE: This function MUST be called from within the context of
1466  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1467  * instead of list_for_each_entry().
1468  **/
1469 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1470 {
1471 	struct i40e_mac_filter *f;
1472 
1473 	if (!vsi || !macaddr)
1474 		return;
1475 
1476 	f = i40e_find_filter(vsi, macaddr, vlan);
1477 	__i40e_del_filter(vsi, f);
1478 }
1479 
1480 /**
1481  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1482  * @vsi: the VSI to be searched
1483  * @macaddr: the mac address to be filtered
1484  *
1485  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1486  * go through all the macvlan filters and add a macvlan filter for each
1487  * unique vlan that already exists. If a PVID has been assigned, instead only
1488  * add the macaddr to that VLAN.
1489  *
1490  * Returns last filter added on success, else NULL
1491  **/
1492 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1493 					    const u8 *macaddr)
1494 {
1495 	struct i40e_mac_filter *f, *add = NULL;
1496 	struct hlist_node *h;
1497 	int bkt;
1498 
1499 	if (vsi->info.pvid)
1500 		return i40e_add_filter(vsi, macaddr,
1501 				       le16_to_cpu(vsi->info.pvid));
1502 
1503 	if (!i40e_is_vsi_in_vlan(vsi))
1504 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1505 
1506 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1507 		if (f->state == I40E_FILTER_REMOVE)
1508 			continue;
1509 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1510 		if (!add)
1511 			return NULL;
1512 	}
1513 
1514 	return add;
1515 }
1516 
1517 /**
1518  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1519  * @vsi: the VSI to be searched
1520  * @macaddr: the mac address to be removed
1521  *
1522  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1523  * associated with.
1524  *
1525  * Returns 0 for success, or error
1526  **/
1527 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1528 {
1529 	struct i40e_mac_filter *f;
1530 	struct hlist_node *h;
1531 	bool found = false;
1532 	int bkt;
1533 
1534 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1535 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1536 		if (ether_addr_equal(macaddr, f->macaddr)) {
1537 			__i40e_del_filter(vsi, f);
1538 			found = true;
1539 		}
1540 	}
1541 
1542 	if (found)
1543 		return 0;
1544 	else
1545 		return -ENOENT;
1546 }
1547 
1548 /**
1549  * i40e_set_mac - NDO callback to set mac address
1550  * @netdev: network interface device structure
1551  * @p: pointer to an address structure
1552  *
1553  * Returns 0 on success, negative on failure
1554  **/
1555 static int i40e_set_mac(struct net_device *netdev, void *p)
1556 {
1557 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1558 	struct i40e_vsi *vsi = np->vsi;
1559 	struct i40e_pf *pf = vsi->back;
1560 	struct i40e_hw *hw = &pf->hw;
1561 	struct sockaddr *addr = p;
1562 
1563 	if (!is_valid_ether_addr(addr->sa_data))
1564 		return -EADDRNOTAVAIL;
1565 
1566 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1567 		netdev_info(netdev, "already using mac address %pM\n",
1568 			    addr->sa_data);
1569 		return 0;
1570 	}
1571 
1572 	if (test_bit(__I40E_DOWN, pf->state) ||
1573 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1574 		return -EADDRNOTAVAIL;
1575 
1576 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1577 		netdev_info(netdev, "returning to hw mac address %pM\n",
1578 			    hw->mac.addr);
1579 	else
1580 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1581 
1582 	/* Copy the address first, so that we avoid a possible race with
1583 	 * .set_rx_mode().
1584 	 * - Remove old address from MAC filter
1585 	 * - Copy new address
1586 	 * - Add new address to MAC filter
1587 	 */
1588 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1589 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1590 	ether_addr_copy(netdev->dev_addr, addr->sa_data);
1591 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1592 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1593 
1594 	if (vsi->type == I40E_VSI_MAIN) {
1595 		i40e_status ret;
1596 
1597 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1598 						addr->sa_data, NULL);
1599 		if (ret)
1600 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1601 				    i40e_stat_str(hw, ret),
1602 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1603 	}
1604 
1605 	/* schedule our worker thread which will take care of
1606 	 * applying the new filter changes
1607 	 */
1608 	i40e_service_event_schedule(pf);
1609 	return 0;
1610 }
1611 
1612 /**
1613  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1614  * @vsi: vsi structure
1615  * @seed: RSS hash seed
1616  * @lut: pointer to lookup table of lut_size
1617  * @lut_size: size of the lookup table
1618  **/
1619 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1620 			      u8 *lut, u16 lut_size)
1621 {
1622 	struct i40e_pf *pf = vsi->back;
1623 	struct i40e_hw *hw = &pf->hw;
1624 	int ret = 0;
1625 
1626 	if (seed) {
1627 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1628 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1629 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1630 		if (ret) {
1631 			dev_info(&pf->pdev->dev,
1632 				 "Cannot set RSS key, err %s aq_err %s\n",
1633 				 i40e_stat_str(hw, ret),
1634 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1635 			return ret;
1636 		}
1637 	}
1638 	if (lut) {
1639 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1640 
1641 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1642 		if (ret) {
1643 			dev_info(&pf->pdev->dev,
1644 				 "Cannot set RSS lut, err %s aq_err %s\n",
1645 				 i40e_stat_str(hw, ret),
1646 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1647 			return ret;
1648 		}
1649 	}
1650 	return ret;
1651 }
1652 
1653 /**
1654  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1655  * @vsi: VSI structure
1656  **/
1657 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1658 {
1659 	struct i40e_pf *pf = vsi->back;
1660 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1661 	u8 *lut;
1662 	int ret;
1663 
1664 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1665 		return 0;
1666 	if (!vsi->rss_size)
1667 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1668 				      vsi->num_queue_pairs);
1669 	if (!vsi->rss_size)
1670 		return -EINVAL;
1671 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1672 	if (!lut)
1673 		return -ENOMEM;
1674 
1675 	/* Use the user configured hash keys and lookup table if there is one,
1676 	 * otherwise use default
1677 	 */
1678 	if (vsi->rss_lut_user)
1679 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1680 	else
1681 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1682 	if (vsi->rss_hkey_user)
1683 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1684 	else
1685 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1686 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1687 	kfree(lut);
1688 	return ret;
1689 }
1690 
1691 /**
1692  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1693  * @vsi: the VSI being configured,
1694  * @ctxt: VSI context structure
1695  * @enabled_tc: number of traffic classes to enable
1696  *
1697  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1698  **/
1699 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1700 					   struct i40e_vsi_context *ctxt,
1701 					   u8 enabled_tc)
1702 {
1703 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1704 	int i, override_q, pow, num_qps, ret;
1705 	u8 netdev_tc = 0, offset = 0;
1706 
1707 	if (vsi->type != I40E_VSI_MAIN)
1708 		return -EINVAL;
1709 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1710 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1711 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1712 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1713 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1714 
1715 	/* find the next higher power-of-2 of num queue pairs */
1716 	pow = ilog2(num_qps);
1717 	if (!is_power_of_2(num_qps))
1718 		pow++;
1719 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1720 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1721 
1722 	/* Setup queue offset/count for all TCs for given VSI */
1723 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1724 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1725 		/* See if the given TC is enabled for the given VSI */
1726 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1727 			offset = vsi->mqprio_qopt.qopt.offset[i];
1728 			qcount = vsi->mqprio_qopt.qopt.count[i];
1729 			if (qcount > max_qcount)
1730 				max_qcount = qcount;
1731 			vsi->tc_config.tc_info[i].qoffset = offset;
1732 			vsi->tc_config.tc_info[i].qcount = qcount;
1733 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1734 		} else {
1735 			/* TC is not enabled so set the offset to
1736 			 * default queue and allocate one queue
1737 			 * for the given TC.
1738 			 */
1739 			vsi->tc_config.tc_info[i].qoffset = 0;
1740 			vsi->tc_config.tc_info[i].qcount = 1;
1741 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1742 		}
1743 	}
1744 
1745 	/* Set actual Tx/Rx queue pairs */
1746 	vsi->num_queue_pairs = offset + qcount;
1747 
1748 	/* Setup queue TC[0].qmap for given VSI context */
1749 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1750 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1751 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1752 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1753 
1754 	/* Reconfigure RSS for main VSI with max queue count */
1755 	vsi->rss_size = max_qcount;
1756 	ret = i40e_vsi_config_rss(vsi);
1757 	if (ret) {
1758 		dev_info(&vsi->back->pdev->dev,
1759 			 "Failed to reconfig rss for num_queues (%u)\n",
1760 			 max_qcount);
1761 		return ret;
1762 	}
1763 	vsi->reconfig_rss = true;
1764 	dev_dbg(&vsi->back->pdev->dev,
1765 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1766 
1767 	/* Find queue count available for channel VSIs and starting offset
1768 	 * for channel VSIs
1769 	 */
1770 	override_q = vsi->mqprio_qopt.qopt.count[0];
1771 	if (override_q && override_q < vsi->num_queue_pairs) {
1772 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1773 		vsi->next_base_queue = override_q;
1774 	}
1775 	return 0;
1776 }
1777 
1778 /**
1779  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1780  * @vsi: the VSI being setup
1781  * @ctxt: VSI context structure
1782  * @enabled_tc: Enabled TCs bitmap
1783  * @is_add: True if called before Add VSI
1784  *
1785  * Setup VSI queue mapping for enabled traffic classes.
1786  **/
1787 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1788 				     struct i40e_vsi_context *ctxt,
1789 				     u8 enabled_tc,
1790 				     bool is_add)
1791 {
1792 	struct i40e_pf *pf = vsi->back;
1793 	u16 sections = 0;
1794 	u8 netdev_tc = 0;
1795 	u16 numtc = 1;
1796 	u16 qcount;
1797 	u8 offset;
1798 	u16 qmap;
1799 	int i;
1800 	u16 num_tc_qps = 0;
1801 
1802 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1803 	offset = 0;
1804 
1805 	/* Number of queues per enabled TC */
1806 	num_tc_qps = vsi->alloc_queue_pairs;
1807 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1808 		/* Find numtc from enabled TC bitmap */
1809 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1810 			if (enabled_tc & BIT(i)) /* TC is enabled */
1811 				numtc++;
1812 		}
1813 		if (!numtc) {
1814 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1815 			numtc = 1;
1816 		}
1817 		num_tc_qps = num_tc_qps / numtc;
1818 		num_tc_qps = min_t(int, num_tc_qps,
1819 				   i40e_pf_get_max_q_per_tc(pf));
1820 	}
1821 
1822 	vsi->tc_config.numtc = numtc;
1823 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1824 
1825 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
1826 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1827 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
1828 
1829 	/* Setup queue offset/count for all TCs for given VSI */
1830 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1831 		/* See if the given TC is enabled for the given VSI */
1832 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1833 			/* TC is enabled */
1834 			int pow, num_qps;
1835 
1836 			switch (vsi->type) {
1837 			case I40E_VSI_MAIN:
1838 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
1839 				    I40E_FLAG_FD_ATR_ENABLED)) ||
1840 				    vsi->tc_config.enabled_tc != 1) {
1841 					qcount = min_t(int, pf->alloc_rss_size,
1842 						       num_tc_qps);
1843 					break;
1844 				}
1845 				fallthrough;
1846 			case I40E_VSI_FDIR:
1847 			case I40E_VSI_SRIOV:
1848 			case I40E_VSI_VMDQ2:
1849 			default:
1850 				qcount = num_tc_qps;
1851 				WARN_ON(i != 0);
1852 				break;
1853 			}
1854 			vsi->tc_config.tc_info[i].qoffset = offset;
1855 			vsi->tc_config.tc_info[i].qcount = qcount;
1856 
1857 			/* find the next higher power-of-2 of num queue pairs */
1858 			num_qps = qcount;
1859 			pow = 0;
1860 			while (num_qps && (BIT_ULL(pow) < qcount)) {
1861 				pow++;
1862 				num_qps >>= 1;
1863 			}
1864 
1865 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1866 			qmap =
1867 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1868 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1869 
1870 			offset += qcount;
1871 		} else {
1872 			/* TC is not enabled so set the offset to
1873 			 * default queue and allocate one queue
1874 			 * for the given TC.
1875 			 */
1876 			vsi->tc_config.tc_info[i].qoffset = 0;
1877 			vsi->tc_config.tc_info[i].qcount = 1;
1878 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1879 
1880 			qmap = 0;
1881 		}
1882 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1883 	}
1884 
1885 	/* Set actual Tx/Rx queue pairs */
1886 	vsi->num_queue_pairs = offset;
1887 	if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1888 		if (vsi->req_queue_pairs > 0)
1889 			vsi->num_queue_pairs = vsi->req_queue_pairs;
1890 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1891 			vsi->num_queue_pairs = pf->num_lan_msix;
1892 	}
1893 
1894 	/* Scheduler section valid can only be set for ADD VSI */
1895 	if (is_add) {
1896 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1897 
1898 		ctxt->info.up_enable_bits = enabled_tc;
1899 	}
1900 	if (vsi->type == I40E_VSI_SRIOV) {
1901 		ctxt->info.mapping_flags |=
1902 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1903 		for (i = 0; i < vsi->num_queue_pairs; i++)
1904 			ctxt->info.queue_mapping[i] =
1905 					       cpu_to_le16(vsi->base_queue + i);
1906 	} else {
1907 		ctxt->info.mapping_flags |=
1908 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1909 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1910 	}
1911 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1912 }
1913 
1914 /**
1915  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1916  * @netdev: the netdevice
1917  * @addr: address to add
1918  *
1919  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1920  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1921  */
1922 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1923 {
1924 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1925 	struct i40e_vsi *vsi = np->vsi;
1926 
1927 	if (i40e_add_mac_filter(vsi, addr))
1928 		return 0;
1929 	else
1930 		return -ENOMEM;
1931 }
1932 
1933 /**
1934  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1935  * @netdev: the netdevice
1936  * @addr: address to add
1937  *
1938  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1939  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1940  */
1941 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1942 {
1943 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1944 	struct i40e_vsi *vsi = np->vsi;
1945 
1946 	/* Under some circumstances, we might receive a request to delete
1947 	 * our own device address from our uc list. Because we store the
1948 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
1949 	 * such requests and not delete our device address from this list.
1950 	 */
1951 	if (ether_addr_equal(addr, netdev->dev_addr))
1952 		return 0;
1953 
1954 	i40e_del_mac_filter(vsi, addr);
1955 
1956 	return 0;
1957 }
1958 
1959 /**
1960  * i40e_set_rx_mode - NDO callback to set the netdev filters
1961  * @netdev: network interface device structure
1962  **/
1963 static void i40e_set_rx_mode(struct net_device *netdev)
1964 {
1965 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1966 	struct i40e_vsi *vsi = np->vsi;
1967 
1968 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1969 
1970 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1971 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1972 
1973 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1974 
1975 	/* check for other flag changes */
1976 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
1977 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1978 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1979 	}
1980 }
1981 
1982 /**
1983  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
1984  * @vsi: Pointer to VSI struct
1985  * @from: Pointer to list which contains MAC filter entries - changes to
1986  *        those entries needs to be undone.
1987  *
1988  * MAC filter entries from this list were slated for deletion.
1989  **/
1990 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1991 					 struct hlist_head *from)
1992 {
1993 	struct i40e_mac_filter *f;
1994 	struct hlist_node *h;
1995 
1996 	hlist_for_each_entry_safe(f, h, from, hlist) {
1997 		u64 key = i40e_addr_to_hkey(f->macaddr);
1998 
1999 		/* Move the element back into MAC filter list*/
2000 		hlist_del(&f->hlist);
2001 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2002 	}
2003 }
2004 
2005 /**
2006  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2007  * @vsi: Pointer to vsi struct
2008  * @from: Pointer to list which contains MAC filter entries - changes to
2009  *        those entries needs to be undone.
2010  *
2011  * MAC filter entries from this list were slated for addition.
2012  **/
2013 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2014 					 struct hlist_head *from)
2015 {
2016 	struct i40e_new_mac_filter *new;
2017 	struct hlist_node *h;
2018 
2019 	hlist_for_each_entry_safe(new, h, from, hlist) {
2020 		/* We can simply free the wrapper structure */
2021 		hlist_del(&new->hlist);
2022 		kfree(new);
2023 	}
2024 }
2025 
2026 /**
2027  * i40e_next_filter - Get the next non-broadcast filter from a list
2028  * @next: pointer to filter in list
2029  *
2030  * Returns the next non-broadcast filter in the list. Required so that we
2031  * ignore broadcast filters within the list, since these are not handled via
2032  * the normal firmware update path.
2033  */
2034 static
2035 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2036 {
2037 	hlist_for_each_entry_continue(next, hlist) {
2038 		if (!is_broadcast_ether_addr(next->f->macaddr))
2039 			return next;
2040 	}
2041 
2042 	return NULL;
2043 }
2044 
2045 /**
2046  * i40e_update_filter_state - Update filter state based on return data
2047  * from firmware
2048  * @count: Number of filters added
2049  * @add_list: return data from fw
2050  * @add_head: pointer to first filter in current batch
2051  *
2052  * MAC filter entries from list were slated to be added to device. Returns
2053  * number of successful filters. Note that 0 does NOT mean success!
2054  **/
2055 static int
2056 i40e_update_filter_state(int count,
2057 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2058 			 struct i40e_new_mac_filter *add_head)
2059 {
2060 	int retval = 0;
2061 	int i;
2062 
2063 	for (i = 0; i < count; i++) {
2064 		/* Always check status of each filter. We don't need to check
2065 		 * the firmware return status because we pre-set the filter
2066 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2067 		 * request to the adminq. Thus, if it no longer matches then
2068 		 * we know the filter is active.
2069 		 */
2070 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2071 			add_head->state = I40E_FILTER_FAILED;
2072 		} else {
2073 			add_head->state = I40E_FILTER_ACTIVE;
2074 			retval++;
2075 		}
2076 
2077 		add_head = i40e_next_filter(add_head);
2078 		if (!add_head)
2079 			break;
2080 	}
2081 
2082 	return retval;
2083 }
2084 
2085 /**
2086  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2087  * @vsi: ptr to the VSI
2088  * @vsi_name: name to display in messages
2089  * @list: the list of filters to send to firmware
2090  * @num_del: the number of filters to delete
2091  * @retval: Set to -EIO on failure to delete
2092  *
2093  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2094  * *retval instead of a return value so that success does not force ret_val to
2095  * be set to 0. This ensures that a sequence of calls to this function
2096  * preserve the previous value of *retval on successful delete.
2097  */
2098 static
2099 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2100 			  struct i40e_aqc_remove_macvlan_element_data *list,
2101 			  int num_del, int *retval)
2102 {
2103 	struct i40e_hw *hw = &vsi->back->hw;
2104 	i40e_status aq_ret;
2105 	int aq_err;
2106 
2107 	aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
2108 	aq_err = hw->aq.asq_last_status;
2109 
2110 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2111 	if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
2112 		*retval = -EIO;
2113 		dev_info(&vsi->back->pdev->dev,
2114 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2115 			 vsi_name, i40e_stat_str(hw, aq_ret),
2116 			 i40e_aq_str(hw, aq_err));
2117 	}
2118 }
2119 
2120 /**
2121  * i40e_aqc_add_filters - Request firmware to add a set of filters
2122  * @vsi: ptr to the VSI
2123  * @vsi_name: name to display in messages
2124  * @list: the list of filters to send to firmware
2125  * @add_head: Position in the add hlist
2126  * @num_add: the number of filters to add
2127  *
2128  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2129  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2130  * space for more filters.
2131  */
2132 static
2133 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2134 			  struct i40e_aqc_add_macvlan_element_data *list,
2135 			  struct i40e_new_mac_filter *add_head,
2136 			  int num_add)
2137 {
2138 	struct i40e_hw *hw = &vsi->back->hw;
2139 	int aq_err, fcnt;
2140 
2141 	i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
2142 	aq_err = hw->aq.asq_last_status;
2143 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2144 
2145 	if (fcnt != num_add) {
2146 		if (vsi->type == I40E_VSI_MAIN) {
2147 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2148 			dev_warn(&vsi->back->pdev->dev,
2149 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2150 				 i40e_aq_str(hw, aq_err), vsi_name);
2151 		} else if (vsi->type == I40E_VSI_SRIOV ||
2152 			   vsi->type == I40E_VSI_VMDQ1 ||
2153 			   vsi->type == I40E_VSI_VMDQ2) {
2154 			dev_warn(&vsi->back->pdev->dev,
2155 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2156 				 i40e_aq_str(hw, aq_err), vsi_name, vsi_name);
2157 		} else {
2158 			dev_warn(&vsi->back->pdev->dev,
2159 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2160 				 i40e_aq_str(hw, aq_err), vsi_name, vsi->type);
2161 		}
2162 	}
2163 }
2164 
2165 /**
2166  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2167  * @vsi: pointer to the VSI
2168  * @vsi_name: the VSI name
2169  * @f: filter data
2170  *
2171  * This function sets or clears the promiscuous broadcast flags for VLAN
2172  * filters in order to properly receive broadcast frames. Assumes that only
2173  * broadcast filters are passed.
2174  *
2175  * Returns status indicating success or failure;
2176  **/
2177 static i40e_status
2178 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2179 			  struct i40e_mac_filter *f)
2180 {
2181 	bool enable = f->state == I40E_FILTER_NEW;
2182 	struct i40e_hw *hw = &vsi->back->hw;
2183 	i40e_status aq_ret;
2184 
2185 	if (f->vlan == I40E_VLAN_ANY) {
2186 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2187 						   vsi->seid,
2188 						   enable,
2189 						   NULL);
2190 	} else {
2191 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2192 							    vsi->seid,
2193 							    enable,
2194 							    f->vlan,
2195 							    NULL);
2196 	}
2197 
2198 	if (aq_ret) {
2199 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2200 		dev_warn(&vsi->back->pdev->dev,
2201 			 "Error %s, forcing overflow promiscuous on %s\n",
2202 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2203 			 vsi_name);
2204 	}
2205 
2206 	return aq_ret;
2207 }
2208 
2209 /**
2210  * i40e_set_promiscuous - set promiscuous mode
2211  * @pf: board private structure
2212  * @promisc: promisc on or off
2213  *
2214  * There are different ways of setting promiscuous mode on a PF depending on
2215  * what state/environment we're in.  This identifies and sets it appropriately.
2216  * Returns 0 on success.
2217  **/
2218 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2219 {
2220 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2221 	struct i40e_hw *hw = &pf->hw;
2222 	i40e_status aq_ret;
2223 
2224 	if (vsi->type == I40E_VSI_MAIN &&
2225 	    pf->lan_veb != I40E_NO_VEB &&
2226 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2227 		/* set defport ON for Main VSI instead of true promisc
2228 		 * this way we will get all unicast/multicast and VLAN
2229 		 * promisc behavior but will not get VF or VMDq traffic
2230 		 * replicated on the Main VSI.
2231 		 */
2232 		if (promisc)
2233 			aq_ret = i40e_aq_set_default_vsi(hw,
2234 							 vsi->seid,
2235 							 NULL);
2236 		else
2237 			aq_ret = i40e_aq_clear_default_vsi(hw,
2238 							   vsi->seid,
2239 							   NULL);
2240 		if (aq_ret) {
2241 			dev_info(&pf->pdev->dev,
2242 				 "Set default VSI failed, err %s, aq_err %s\n",
2243 				 i40e_stat_str(hw, aq_ret),
2244 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2245 		}
2246 	} else {
2247 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2248 						  hw,
2249 						  vsi->seid,
2250 						  promisc, NULL,
2251 						  true);
2252 		if (aq_ret) {
2253 			dev_info(&pf->pdev->dev,
2254 				 "set unicast promisc failed, err %s, aq_err %s\n",
2255 				 i40e_stat_str(hw, aq_ret),
2256 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2257 		}
2258 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2259 						  hw,
2260 						  vsi->seid,
2261 						  promisc, NULL);
2262 		if (aq_ret) {
2263 			dev_info(&pf->pdev->dev,
2264 				 "set multicast promisc failed, err %s, aq_err %s\n",
2265 				 i40e_stat_str(hw, aq_ret),
2266 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2267 		}
2268 	}
2269 
2270 	if (!aq_ret)
2271 		pf->cur_promisc = promisc;
2272 
2273 	return aq_ret;
2274 }
2275 
2276 /**
2277  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2278  * @vsi: ptr to the VSI
2279  *
2280  * Push any outstanding VSI filter changes through the AdminQ.
2281  *
2282  * Returns 0 or error value
2283  **/
2284 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2285 {
2286 	struct hlist_head tmp_add_list, tmp_del_list;
2287 	struct i40e_mac_filter *f;
2288 	struct i40e_new_mac_filter *new, *add_head = NULL;
2289 	struct i40e_hw *hw = &vsi->back->hw;
2290 	bool old_overflow, new_overflow;
2291 	unsigned int failed_filters = 0;
2292 	unsigned int vlan_filters = 0;
2293 	char vsi_name[16] = "PF";
2294 	int filter_list_len = 0;
2295 	i40e_status aq_ret = 0;
2296 	u32 changed_flags = 0;
2297 	struct hlist_node *h;
2298 	struct i40e_pf *pf;
2299 	int num_add = 0;
2300 	int num_del = 0;
2301 	int retval = 0;
2302 	u16 cmd_flags;
2303 	int list_size;
2304 	int bkt;
2305 
2306 	/* empty array typed pointers, kcalloc later */
2307 	struct i40e_aqc_add_macvlan_element_data *add_list;
2308 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2309 
2310 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2311 		usleep_range(1000, 2000);
2312 	pf = vsi->back;
2313 
2314 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2315 
2316 	if (vsi->netdev) {
2317 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2318 		vsi->current_netdev_flags = vsi->netdev->flags;
2319 	}
2320 
2321 	INIT_HLIST_HEAD(&tmp_add_list);
2322 	INIT_HLIST_HEAD(&tmp_del_list);
2323 
2324 	if (vsi->type == I40E_VSI_SRIOV)
2325 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2326 	else if (vsi->type != I40E_VSI_MAIN)
2327 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2328 
2329 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2330 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2331 
2332 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2333 		/* Create a list of filters to delete. */
2334 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2335 			if (f->state == I40E_FILTER_REMOVE) {
2336 				/* Move the element into temporary del_list */
2337 				hash_del(&f->hlist);
2338 				hlist_add_head(&f->hlist, &tmp_del_list);
2339 
2340 				/* Avoid counting removed filters */
2341 				continue;
2342 			}
2343 			if (f->state == I40E_FILTER_NEW) {
2344 				/* Create a temporary i40e_new_mac_filter */
2345 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2346 				if (!new)
2347 					goto err_no_memory_locked;
2348 
2349 				/* Store pointer to the real filter */
2350 				new->f = f;
2351 				new->state = f->state;
2352 
2353 				/* Add it to the hash list */
2354 				hlist_add_head(&new->hlist, &tmp_add_list);
2355 			}
2356 
2357 			/* Count the number of active (current and new) VLAN
2358 			 * filters we have now. Does not count filters which
2359 			 * are marked for deletion.
2360 			 */
2361 			if (f->vlan > 0)
2362 				vlan_filters++;
2363 		}
2364 
2365 		retval = i40e_correct_mac_vlan_filters(vsi,
2366 						       &tmp_add_list,
2367 						       &tmp_del_list,
2368 						       vlan_filters);
2369 		if (retval)
2370 			goto err_no_memory_locked;
2371 
2372 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2373 	}
2374 
2375 	/* Now process 'del_list' outside the lock */
2376 	if (!hlist_empty(&tmp_del_list)) {
2377 		filter_list_len = hw->aq.asq_buf_size /
2378 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2379 		list_size = filter_list_len *
2380 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2381 		del_list = kzalloc(list_size, GFP_ATOMIC);
2382 		if (!del_list)
2383 			goto err_no_memory;
2384 
2385 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2386 			cmd_flags = 0;
2387 
2388 			/* handle broadcast filters by updating the broadcast
2389 			 * promiscuous flag and release filter list.
2390 			 */
2391 			if (is_broadcast_ether_addr(f->macaddr)) {
2392 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2393 
2394 				hlist_del(&f->hlist);
2395 				kfree(f);
2396 				continue;
2397 			}
2398 
2399 			/* add to delete list */
2400 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2401 			if (f->vlan == I40E_VLAN_ANY) {
2402 				del_list[num_del].vlan_tag = 0;
2403 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2404 			} else {
2405 				del_list[num_del].vlan_tag =
2406 					cpu_to_le16((u16)(f->vlan));
2407 			}
2408 
2409 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2410 			del_list[num_del].flags = cmd_flags;
2411 			num_del++;
2412 
2413 			/* flush a full buffer */
2414 			if (num_del == filter_list_len) {
2415 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2416 						     num_del, &retval);
2417 				memset(del_list, 0, list_size);
2418 				num_del = 0;
2419 			}
2420 			/* Release memory for MAC filter entries which were
2421 			 * synced up with HW.
2422 			 */
2423 			hlist_del(&f->hlist);
2424 			kfree(f);
2425 		}
2426 
2427 		if (num_del) {
2428 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2429 					     num_del, &retval);
2430 		}
2431 
2432 		kfree(del_list);
2433 		del_list = NULL;
2434 	}
2435 
2436 	if (!hlist_empty(&tmp_add_list)) {
2437 		/* Do all the adds now. */
2438 		filter_list_len = hw->aq.asq_buf_size /
2439 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2440 		list_size = filter_list_len *
2441 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2442 		add_list = kzalloc(list_size, GFP_ATOMIC);
2443 		if (!add_list)
2444 			goto err_no_memory;
2445 
2446 		num_add = 0;
2447 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2448 			/* handle broadcast filters by updating the broadcast
2449 			 * promiscuous flag instead of adding a MAC filter.
2450 			 */
2451 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2452 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2453 							      new->f))
2454 					new->state = I40E_FILTER_FAILED;
2455 				else
2456 					new->state = I40E_FILTER_ACTIVE;
2457 				continue;
2458 			}
2459 
2460 			/* add to add array */
2461 			if (num_add == 0)
2462 				add_head = new;
2463 			cmd_flags = 0;
2464 			ether_addr_copy(add_list[num_add].mac_addr,
2465 					new->f->macaddr);
2466 			if (new->f->vlan == I40E_VLAN_ANY) {
2467 				add_list[num_add].vlan_tag = 0;
2468 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2469 			} else {
2470 				add_list[num_add].vlan_tag =
2471 					cpu_to_le16((u16)(new->f->vlan));
2472 			}
2473 			add_list[num_add].queue_number = 0;
2474 			/* set invalid match method for later detection */
2475 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2476 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2477 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2478 			num_add++;
2479 
2480 			/* flush a full buffer */
2481 			if (num_add == filter_list_len) {
2482 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2483 						     add_head, num_add);
2484 				memset(add_list, 0, list_size);
2485 				num_add = 0;
2486 			}
2487 		}
2488 		if (num_add) {
2489 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2490 					     num_add);
2491 		}
2492 		/* Now move all of the filters from the temp add list back to
2493 		 * the VSI's list.
2494 		 */
2495 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2496 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2497 			/* Only update the state if we're still NEW */
2498 			if (new->f->state == I40E_FILTER_NEW)
2499 				new->f->state = new->state;
2500 			hlist_del(&new->hlist);
2501 			kfree(new);
2502 		}
2503 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2504 		kfree(add_list);
2505 		add_list = NULL;
2506 	}
2507 
2508 	/* Determine the number of active and failed filters. */
2509 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2510 	vsi->active_filters = 0;
2511 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2512 		if (f->state == I40E_FILTER_ACTIVE)
2513 			vsi->active_filters++;
2514 		else if (f->state == I40E_FILTER_FAILED)
2515 			failed_filters++;
2516 	}
2517 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2518 
2519 	/* Check if we are able to exit overflow promiscuous mode. We can
2520 	 * safely exit if we didn't just enter, we no longer have any failed
2521 	 * filters, and we have reduced filters below the threshold value.
2522 	 */
2523 	if (old_overflow && !failed_filters &&
2524 	    vsi->active_filters < vsi->promisc_threshold) {
2525 		dev_info(&pf->pdev->dev,
2526 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2527 			 vsi_name);
2528 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2529 		vsi->promisc_threshold = 0;
2530 	}
2531 
2532 	/* if the VF is not trusted do not do promisc */
2533 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2534 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2535 		goto out;
2536 	}
2537 
2538 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2539 
2540 	/* If we are entering overflow promiscuous, we need to calculate a new
2541 	 * threshold for when we are safe to exit
2542 	 */
2543 	if (!old_overflow && new_overflow)
2544 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2545 
2546 	/* check for changes in promiscuous modes */
2547 	if (changed_flags & IFF_ALLMULTI) {
2548 		bool cur_multipromisc;
2549 
2550 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2551 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2552 							       vsi->seid,
2553 							       cur_multipromisc,
2554 							       NULL);
2555 		if (aq_ret) {
2556 			retval = i40e_aq_rc_to_posix(aq_ret,
2557 						     hw->aq.asq_last_status);
2558 			dev_info(&pf->pdev->dev,
2559 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2560 				 vsi_name,
2561 				 i40e_stat_str(hw, aq_ret),
2562 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2563 		} else {
2564 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2565 				 cur_multipromisc ? "entering" : "leaving");
2566 		}
2567 	}
2568 
2569 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2570 		bool cur_promisc;
2571 
2572 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2573 			       new_overflow);
2574 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2575 		if (aq_ret) {
2576 			retval = i40e_aq_rc_to_posix(aq_ret,
2577 						     hw->aq.asq_last_status);
2578 			dev_info(&pf->pdev->dev,
2579 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2580 				 cur_promisc ? "on" : "off",
2581 				 vsi_name,
2582 				 i40e_stat_str(hw, aq_ret),
2583 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2584 		}
2585 	}
2586 out:
2587 	/* if something went wrong then set the changed flag so we try again */
2588 	if (retval)
2589 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2590 
2591 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2592 	return retval;
2593 
2594 err_no_memory:
2595 	/* Restore elements on the temporary add and delete lists */
2596 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2597 err_no_memory_locked:
2598 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2599 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2600 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2601 
2602 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2603 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2604 	return -ENOMEM;
2605 }
2606 
2607 /**
2608  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2609  * @pf: board private structure
2610  **/
2611 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2612 {
2613 	int v;
2614 
2615 	if (!pf)
2616 		return;
2617 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2618 		return;
2619 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2620 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2621 		return;
2622 	}
2623 
2624 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2625 		if (pf->vsi[v] &&
2626 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2627 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2628 
2629 			if (ret) {
2630 				/* come back and try again later */
2631 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2632 					pf->state);
2633 				break;
2634 			}
2635 		}
2636 	}
2637 }
2638 
2639 /**
2640  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2641  * @vsi: the vsi
2642  **/
2643 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2644 {
2645 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2646 		return I40E_RXBUFFER_2048;
2647 	else
2648 		return I40E_RXBUFFER_3072;
2649 }
2650 
2651 /**
2652  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2653  * @netdev: network interface device structure
2654  * @new_mtu: new value for maximum frame size
2655  *
2656  * Returns 0 on success, negative on failure
2657  **/
2658 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2659 {
2660 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2661 	struct i40e_vsi *vsi = np->vsi;
2662 	struct i40e_pf *pf = vsi->back;
2663 
2664 	if (i40e_enabled_xdp_vsi(vsi)) {
2665 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2666 
2667 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2668 			return -EINVAL;
2669 	}
2670 
2671 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2672 		   netdev->mtu, new_mtu);
2673 	netdev->mtu = new_mtu;
2674 	if (netif_running(netdev))
2675 		i40e_vsi_reinit_locked(vsi);
2676 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2677 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2678 	return 0;
2679 }
2680 
2681 /**
2682  * i40e_ioctl - Access the hwtstamp interface
2683  * @netdev: network interface device structure
2684  * @ifr: interface request data
2685  * @cmd: ioctl command
2686  **/
2687 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2688 {
2689 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2690 	struct i40e_pf *pf = np->vsi->back;
2691 
2692 	switch (cmd) {
2693 	case SIOCGHWTSTAMP:
2694 		return i40e_ptp_get_ts_config(pf, ifr);
2695 	case SIOCSHWTSTAMP:
2696 		return i40e_ptp_set_ts_config(pf, ifr);
2697 	default:
2698 		return -EOPNOTSUPP;
2699 	}
2700 }
2701 
2702 /**
2703  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2704  * @vsi: the vsi being adjusted
2705  **/
2706 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2707 {
2708 	struct i40e_vsi_context ctxt;
2709 	i40e_status ret;
2710 
2711 	/* Don't modify stripping options if a port VLAN is active */
2712 	if (vsi->info.pvid)
2713 		return;
2714 
2715 	if ((vsi->info.valid_sections &
2716 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2717 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2718 		return;  /* already enabled */
2719 
2720 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2721 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2722 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2723 
2724 	ctxt.seid = vsi->seid;
2725 	ctxt.info = vsi->info;
2726 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2727 	if (ret) {
2728 		dev_info(&vsi->back->pdev->dev,
2729 			 "update vlan stripping failed, err %s aq_err %s\n",
2730 			 i40e_stat_str(&vsi->back->hw, ret),
2731 			 i40e_aq_str(&vsi->back->hw,
2732 				     vsi->back->hw.aq.asq_last_status));
2733 	}
2734 }
2735 
2736 /**
2737  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2738  * @vsi: the vsi being adjusted
2739  **/
2740 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2741 {
2742 	struct i40e_vsi_context ctxt;
2743 	i40e_status ret;
2744 
2745 	/* Don't modify stripping options if a port VLAN is active */
2746 	if (vsi->info.pvid)
2747 		return;
2748 
2749 	if ((vsi->info.valid_sections &
2750 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2751 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2752 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
2753 		return;  /* already disabled */
2754 
2755 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2756 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2757 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2758 
2759 	ctxt.seid = vsi->seid;
2760 	ctxt.info = vsi->info;
2761 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2762 	if (ret) {
2763 		dev_info(&vsi->back->pdev->dev,
2764 			 "update vlan stripping failed, err %s aq_err %s\n",
2765 			 i40e_stat_str(&vsi->back->hw, ret),
2766 			 i40e_aq_str(&vsi->back->hw,
2767 				     vsi->back->hw.aq.asq_last_status));
2768 	}
2769 }
2770 
2771 /**
2772  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2773  * @vsi: the vsi being configured
2774  * @vid: vlan id to be added (0 = untagged only , -1 = any)
2775  *
2776  * This is a helper function for adding a new MAC/VLAN filter with the
2777  * specified VLAN for each existing MAC address already in the hash table.
2778  * This function does *not* perform any accounting to update filters based on
2779  * VLAN mode.
2780  *
2781  * NOTE: this function expects to be called while under the
2782  * mac_filter_hash_lock
2783  **/
2784 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2785 {
2786 	struct i40e_mac_filter *f, *add_f;
2787 	struct hlist_node *h;
2788 	int bkt;
2789 
2790 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2791 		if (f->state == I40E_FILTER_REMOVE)
2792 			continue;
2793 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
2794 		if (!add_f) {
2795 			dev_info(&vsi->back->pdev->dev,
2796 				 "Could not add vlan filter %d for %pM\n",
2797 				 vid, f->macaddr);
2798 			return -ENOMEM;
2799 		}
2800 	}
2801 
2802 	return 0;
2803 }
2804 
2805 /**
2806  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2807  * @vsi: the VSI being configured
2808  * @vid: VLAN id to be added
2809  **/
2810 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
2811 {
2812 	int err;
2813 
2814 	if (vsi->info.pvid)
2815 		return -EINVAL;
2816 
2817 	/* The network stack will attempt to add VID=0, with the intention to
2818 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
2819 	 * these packets by default when configured to receive untagged
2820 	 * packets, so we don't need to add a filter for this case.
2821 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
2822 	 * receive *only* tagged traffic and stops receiving untagged traffic.
2823 	 * Thus, we do not want to actually add a filter for VID=0
2824 	 */
2825 	if (!vid)
2826 		return 0;
2827 
2828 	/* Locked once because all functions invoked below iterates list*/
2829 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2830 	err = i40e_add_vlan_all_mac(vsi, vid);
2831 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2832 	if (err)
2833 		return err;
2834 
2835 	/* schedule our worker thread which will take care of
2836 	 * applying the new filter changes
2837 	 */
2838 	i40e_service_event_schedule(vsi->back);
2839 	return 0;
2840 }
2841 
2842 /**
2843  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
2844  * @vsi: the vsi being configured
2845  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2846  *
2847  * This function should be used to remove all VLAN filters which match the
2848  * given VID. It does not schedule the service event and does not take the
2849  * mac_filter_hash_lock so it may be combined with other operations under
2850  * a single invocation of the mac_filter_hash_lock.
2851  *
2852  * NOTE: this function expects to be called while under the
2853  * mac_filter_hash_lock
2854  */
2855 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2856 {
2857 	struct i40e_mac_filter *f;
2858 	struct hlist_node *h;
2859 	int bkt;
2860 
2861 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2862 		if (f->vlan == vid)
2863 			__i40e_del_filter(vsi, f);
2864 	}
2865 }
2866 
2867 /**
2868  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2869  * @vsi: the VSI being configured
2870  * @vid: VLAN id to be removed
2871  **/
2872 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
2873 {
2874 	if (!vid || vsi->info.pvid)
2875 		return;
2876 
2877 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2878 	i40e_rm_vlan_all_mac(vsi, vid);
2879 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2880 
2881 	/* schedule our worker thread which will take care of
2882 	 * applying the new filter changes
2883 	 */
2884 	i40e_service_event_schedule(vsi->back);
2885 }
2886 
2887 /**
2888  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2889  * @netdev: network interface to be adjusted
2890  * @proto: unused protocol value
2891  * @vid: vlan id to be added
2892  *
2893  * net_device_ops implementation for adding vlan ids
2894  **/
2895 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2896 				__always_unused __be16 proto, u16 vid)
2897 {
2898 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2899 	struct i40e_vsi *vsi = np->vsi;
2900 	int ret = 0;
2901 
2902 	if (vid >= VLAN_N_VID)
2903 		return -EINVAL;
2904 
2905 	ret = i40e_vsi_add_vlan(vsi, vid);
2906 	if (!ret)
2907 		set_bit(vid, vsi->active_vlans);
2908 
2909 	return ret;
2910 }
2911 
2912 /**
2913  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
2914  * @netdev: network interface to be adjusted
2915  * @proto: unused protocol value
2916  * @vid: vlan id to be added
2917  **/
2918 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
2919 				    __always_unused __be16 proto, u16 vid)
2920 {
2921 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2922 	struct i40e_vsi *vsi = np->vsi;
2923 
2924 	if (vid >= VLAN_N_VID)
2925 		return;
2926 	set_bit(vid, vsi->active_vlans);
2927 }
2928 
2929 /**
2930  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2931  * @netdev: network interface to be adjusted
2932  * @proto: unused protocol value
2933  * @vid: vlan id to be removed
2934  *
2935  * net_device_ops implementation for removing vlan ids
2936  **/
2937 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2938 				 __always_unused __be16 proto, u16 vid)
2939 {
2940 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2941 	struct i40e_vsi *vsi = np->vsi;
2942 
2943 	/* return code is ignored as there is nothing a user
2944 	 * can do about failure to remove and a log message was
2945 	 * already printed from the other function
2946 	 */
2947 	i40e_vsi_kill_vlan(vsi, vid);
2948 
2949 	clear_bit(vid, vsi->active_vlans);
2950 
2951 	return 0;
2952 }
2953 
2954 /**
2955  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2956  * @vsi: the vsi being brought back up
2957  **/
2958 static void i40e_restore_vlan(struct i40e_vsi *vsi)
2959 {
2960 	u16 vid;
2961 
2962 	if (!vsi->netdev)
2963 		return;
2964 
2965 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2966 		i40e_vlan_stripping_enable(vsi);
2967 	else
2968 		i40e_vlan_stripping_disable(vsi);
2969 
2970 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2971 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
2972 					vid);
2973 }
2974 
2975 /**
2976  * i40e_vsi_add_pvid - Add pvid for the VSI
2977  * @vsi: the vsi being adjusted
2978  * @vid: the vlan id to set as a PVID
2979  **/
2980 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
2981 {
2982 	struct i40e_vsi_context ctxt;
2983 	i40e_status ret;
2984 
2985 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2986 	vsi->info.pvid = cpu_to_le16(vid);
2987 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2988 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
2989 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
2990 
2991 	ctxt.seid = vsi->seid;
2992 	ctxt.info = vsi->info;
2993 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2994 	if (ret) {
2995 		dev_info(&vsi->back->pdev->dev,
2996 			 "add pvid failed, err %s aq_err %s\n",
2997 			 i40e_stat_str(&vsi->back->hw, ret),
2998 			 i40e_aq_str(&vsi->back->hw,
2999 				     vsi->back->hw.aq.asq_last_status));
3000 		return -ENOENT;
3001 	}
3002 
3003 	return 0;
3004 }
3005 
3006 /**
3007  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3008  * @vsi: the vsi being adjusted
3009  *
3010  * Just use the vlan_rx_register() service to put it back to normal
3011  **/
3012 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3013 {
3014 	vsi->info.pvid = 0;
3015 
3016 	i40e_vlan_stripping_disable(vsi);
3017 }
3018 
3019 /**
3020  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3021  * @vsi: ptr to the VSI
3022  *
3023  * If this function returns with an error, then it's possible one or
3024  * more of the rings is populated (while the rest are not).  It is the
3025  * callers duty to clean those orphaned rings.
3026  *
3027  * Return 0 on success, negative on failure
3028  **/
3029 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3030 {
3031 	int i, err = 0;
3032 
3033 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3034 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3035 
3036 	if (!i40e_enabled_xdp_vsi(vsi))
3037 		return err;
3038 
3039 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3040 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3041 
3042 	return err;
3043 }
3044 
3045 /**
3046  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3047  * @vsi: ptr to the VSI
3048  *
3049  * Free VSI's transmit software resources
3050  **/
3051 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3052 {
3053 	int i;
3054 
3055 	if (vsi->tx_rings) {
3056 		for (i = 0; i < vsi->num_queue_pairs; i++)
3057 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3058 				i40e_free_tx_resources(vsi->tx_rings[i]);
3059 	}
3060 
3061 	if (vsi->xdp_rings) {
3062 		for (i = 0; i < vsi->num_queue_pairs; i++)
3063 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3064 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3065 	}
3066 }
3067 
3068 /**
3069  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3070  * @vsi: ptr to the VSI
3071  *
3072  * If this function returns with an error, then it's possible one or
3073  * more of the rings is populated (while the rest are not).  It is the
3074  * callers duty to clean those orphaned rings.
3075  *
3076  * Return 0 on success, negative on failure
3077  **/
3078 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3079 {
3080 	int i, err = 0;
3081 
3082 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3083 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3084 	return err;
3085 }
3086 
3087 /**
3088  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3089  * @vsi: ptr to the VSI
3090  *
3091  * Free all receive software resources
3092  **/
3093 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3094 {
3095 	int i;
3096 
3097 	if (!vsi->rx_rings)
3098 		return;
3099 
3100 	for (i = 0; i < vsi->num_queue_pairs; i++)
3101 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3102 			i40e_free_rx_resources(vsi->rx_rings[i]);
3103 }
3104 
3105 /**
3106  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3107  * @ring: The Tx ring to configure
3108  *
3109  * This enables/disables XPS for a given Tx descriptor ring
3110  * based on the TCs enabled for the VSI that ring belongs to.
3111  **/
3112 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3113 {
3114 	int cpu;
3115 
3116 	if (!ring->q_vector || !ring->netdev || ring->ch)
3117 		return;
3118 
3119 	/* We only initialize XPS once, so as not to overwrite user settings */
3120 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3121 		return;
3122 
3123 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3124 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3125 			    ring->queue_index);
3126 }
3127 
3128 /**
3129  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3130  * @ring: The Tx or Rx ring
3131  *
3132  * Returns the AF_XDP buffer pool or NULL.
3133  **/
3134 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3135 {
3136 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3137 	int qid = ring->queue_index;
3138 
3139 	if (ring_is_xdp(ring))
3140 		qid -= ring->vsi->alloc_queue_pairs;
3141 
3142 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3143 		return NULL;
3144 
3145 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3146 }
3147 
3148 /**
3149  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3150  * @ring: The Tx ring to configure
3151  *
3152  * Configure the Tx descriptor ring in the HMC context.
3153  **/
3154 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3155 {
3156 	struct i40e_vsi *vsi = ring->vsi;
3157 	u16 pf_q = vsi->base_queue + ring->queue_index;
3158 	struct i40e_hw *hw = &vsi->back->hw;
3159 	struct i40e_hmc_obj_txq tx_ctx;
3160 	i40e_status err = 0;
3161 	u32 qtx_ctl = 0;
3162 
3163 	if (ring_is_xdp(ring))
3164 		ring->xsk_pool = i40e_xsk_pool(ring);
3165 
3166 	/* some ATR related tx ring init */
3167 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3168 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3169 		ring->atr_count = 0;
3170 	} else {
3171 		ring->atr_sample_rate = 0;
3172 	}
3173 
3174 	/* configure XPS */
3175 	i40e_config_xps_tx_ring(ring);
3176 
3177 	/* clear the context structure first */
3178 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3179 
3180 	tx_ctx.new_context = 1;
3181 	tx_ctx.base = (ring->dma / 128);
3182 	tx_ctx.qlen = ring->count;
3183 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3184 					       I40E_FLAG_FD_ATR_ENABLED));
3185 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3186 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3187 	if (vsi->type != I40E_VSI_FDIR)
3188 		tx_ctx.head_wb_ena = 1;
3189 	tx_ctx.head_wb_addr = ring->dma +
3190 			      (ring->count * sizeof(struct i40e_tx_desc));
3191 
3192 	/* As part of VSI creation/update, FW allocates certain
3193 	 * Tx arbitration queue sets for each TC enabled for
3194 	 * the VSI. The FW returns the handles to these queue
3195 	 * sets as part of the response buffer to Add VSI,
3196 	 * Update VSI, etc. AQ commands. It is expected that
3197 	 * these queue set handles be associated with the Tx
3198 	 * queues by the driver as part of the TX queue context
3199 	 * initialization. This has to be done regardless of
3200 	 * DCB as by default everything is mapped to TC0.
3201 	 */
3202 
3203 	if (ring->ch)
3204 		tx_ctx.rdylist =
3205 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3206 
3207 	else
3208 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3209 
3210 	tx_ctx.rdylist_act = 0;
3211 
3212 	/* clear the context in the HMC */
3213 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3214 	if (err) {
3215 		dev_info(&vsi->back->pdev->dev,
3216 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3217 			 ring->queue_index, pf_q, err);
3218 		return -ENOMEM;
3219 	}
3220 
3221 	/* set the context in the HMC */
3222 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3223 	if (err) {
3224 		dev_info(&vsi->back->pdev->dev,
3225 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3226 			 ring->queue_index, pf_q, err);
3227 		return -ENOMEM;
3228 	}
3229 
3230 	/* Now associate this queue with this PCI function */
3231 	if (ring->ch) {
3232 		if (ring->ch->type == I40E_VSI_VMDQ2)
3233 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3234 		else
3235 			return -EINVAL;
3236 
3237 		qtx_ctl |= (ring->ch->vsi_number <<
3238 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3239 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3240 	} else {
3241 		if (vsi->type == I40E_VSI_VMDQ2) {
3242 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3243 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3244 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3245 		} else {
3246 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3247 		}
3248 	}
3249 
3250 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3251 		    I40E_QTX_CTL_PF_INDX_MASK);
3252 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3253 	i40e_flush(hw);
3254 
3255 	/* cache tail off for easier writes later */
3256 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3257 
3258 	return 0;
3259 }
3260 
3261 /**
3262  * i40e_rx_offset - Return expected offset into page to access data
3263  * @rx_ring: Ring we are requesting offset of
3264  *
3265  * Returns the offset value for ring into the data buffer.
3266  */
3267 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3268 {
3269 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3270 }
3271 
3272 /**
3273  * i40e_configure_rx_ring - Configure a receive ring context
3274  * @ring: The Rx ring to configure
3275  *
3276  * Configure the Rx descriptor ring in the HMC context.
3277  **/
3278 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3279 {
3280 	struct i40e_vsi *vsi = ring->vsi;
3281 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3282 	u16 pf_q = vsi->base_queue + ring->queue_index;
3283 	struct i40e_hw *hw = &vsi->back->hw;
3284 	struct i40e_hmc_obj_rxq rx_ctx;
3285 	i40e_status err = 0;
3286 	bool ok;
3287 	int ret;
3288 
3289 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3290 
3291 	/* clear the context structure first */
3292 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3293 
3294 	if (ring->vsi->type == I40E_VSI_MAIN)
3295 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3296 
3297 	kfree(ring->rx_bi);
3298 	ring->xsk_pool = i40e_xsk_pool(ring);
3299 	if (ring->xsk_pool) {
3300 		ret = i40e_alloc_rx_bi_zc(ring);
3301 		if (ret)
3302 			return ret;
3303 		ring->rx_buf_len =
3304 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3305 		/* For AF_XDP ZC, we disallow packets to span on
3306 		 * multiple buffers, thus letting us skip that
3307 		 * handling in the fast-path.
3308 		 */
3309 		chain_len = 1;
3310 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3311 						 MEM_TYPE_XSK_BUFF_POOL,
3312 						 NULL);
3313 		if (ret)
3314 			return ret;
3315 		dev_info(&vsi->back->pdev->dev,
3316 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3317 			 ring->queue_index);
3318 
3319 	} else {
3320 		ret = i40e_alloc_rx_bi(ring);
3321 		if (ret)
3322 			return ret;
3323 		ring->rx_buf_len = vsi->rx_buf_len;
3324 		if (ring->vsi->type == I40E_VSI_MAIN) {
3325 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3326 							 MEM_TYPE_PAGE_SHARED,
3327 							 NULL);
3328 			if (ret)
3329 				return ret;
3330 		}
3331 	}
3332 
3333 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3334 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3335 
3336 	rx_ctx.base = (ring->dma / 128);
3337 	rx_ctx.qlen = ring->count;
3338 
3339 	/* use 16 byte descriptors */
3340 	rx_ctx.dsize = 0;
3341 
3342 	/* descriptor type is always zero
3343 	 * rx_ctx.dtype = 0;
3344 	 */
3345 	rx_ctx.hsplit_0 = 0;
3346 
3347 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3348 	if (hw->revision_id == 0)
3349 		rx_ctx.lrxqthresh = 0;
3350 	else
3351 		rx_ctx.lrxqthresh = 1;
3352 	rx_ctx.crcstrip = 1;
3353 	rx_ctx.l2tsel = 1;
3354 	/* this controls whether VLAN is stripped from inner headers */
3355 	rx_ctx.showiv = 0;
3356 	/* set the prefena field to 1 because the manual says to */
3357 	rx_ctx.prefena = 1;
3358 
3359 	/* clear the context in the HMC */
3360 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3361 	if (err) {
3362 		dev_info(&vsi->back->pdev->dev,
3363 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3364 			 ring->queue_index, pf_q, err);
3365 		return -ENOMEM;
3366 	}
3367 
3368 	/* set the context in the HMC */
3369 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3370 	if (err) {
3371 		dev_info(&vsi->back->pdev->dev,
3372 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3373 			 ring->queue_index, pf_q, err);
3374 		return -ENOMEM;
3375 	}
3376 
3377 	/* configure Rx buffer alignment */
3378 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3379 		clear_ring_build_skb_enabled(ring);
3380 	else
3381 		set_ring_build_skb_enabled(ring);
3382 
3383 	ring->rx_offset = i40e_rx_offset(ring);
3384 
3385 	/* cache tail for quicker writes, and clear the reg before use */
3386 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3387 	writel(0, ring->tail);
3388 
3389 	if (ring->xsk_pool) {
3390 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3391 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3392 	} else {
3393 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3394 	}
3395 	if (!ok) {
3396 		/* Log this in case the user has forgotten to give the kernel
3397 		 * any buffers, even later in the application.
3398 		 */
3399 		dev_info(&vsi->back->pdev->dev,
3400 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3401 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3402 			 ring->queue_index, pf_q);
3403 	}
3404 
3405 	return 0;
3406 }
3407 
3408 /**
3409  * i40e_vsi_configure_tx - Configure the VSI for Tx
3410  * @vsi: VSI structure describing this set of rings and resources
3411  *
3412  * Configure the Tx VSI for operation.
3413  **/
3414 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3415 {
3416 	int err = 0;
3417 	u16 i;
3418 
3419 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3420 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3421 
3422 	if (err || !i40e_enabled_xdp_vsi(vsi))
3423 		return err;
3424 
3425 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3426 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3427 
3428 	return err;
3429 }
3430 
3431 /**
3432  * i40e_vsi_configure_rx - Configure the VSI for Rx
3433  * @vsi: the VSI being configured
3434  *
3435  * Configure the Rx VSI for operation.
3436  **/
3437 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3438 {
3439 	int err = 0;
3440 	u16 i;
3441 
3442 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3443 		vsi->max_frame = I40E_MAX_RXBUFFER;
3444 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3445 #if (PAGE_SIZE < 8192)
3446 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3447 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3448 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3449 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3450 #endif
3451 	} else {
3452 		vsi->max_frame = I40E_MAX_RXBUFFER;
3453 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3454 						       I40E_RXBUFFER_2048;
3455 	}
3456 
3457 	/* set up individual rings */
3458 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3459 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3460 
3461 	return err;
3462 }
3463 
3464 /**
3465  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3466  * @vsi: ptr to the VSI
3467  **/
3468 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3469 {
3470 	struct i40e_ring *tx_ring, *rx_ring;
3471 	u16 qoffset, qcount;
3472 	int i, n;
3473 
3474 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3475 		/* Reset the TC information */
3476 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3477 			rx_ring = vsi->rx_rings[i];
3478 			tx_ring = vsi->tx_rings[i];
3479 			rx_ring->dcb_tc = 0;
3480 			tx_ring->dcb_tc = 0;
3481 		}
3482 		return;
3483 	}
3484 
3485 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3486 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3487 			continue;
3488 
3489 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3490 		qcount = vsi->tc_config.tc_info[n].qcount;
3491 		for (i = qoffset; i < (qoffset + qcount); i++) {
3492 			rx_ring = vsi->rx_rings[i];
3493 			tx_ring = vsi->tx_rings[i];
3494 			rx_ring->dcb_tc = n;
3495 			tx_ring->dcb_tc = n;
3496 		}
3497 	}
3498 }
3499 
3500 /**
3501  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3502  * @vsi: ptr to the VSI
3503  **/
3504 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3505 {
3506 	if (vsi->netdev)
3507 		i40e_set_rx_mode(vsi->netdev);
3508 }
3509 
3510 /**
3511  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3512  * @pf: Pointer to the targeted PF
3513  *
3514  * Set all flow director counters to 0.
3515  */
3516 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3517 {
3518 	pf->fd_tcp4_filter_cnt = 0;
3519 	pf->fd_udp4_filter_cnt = 0;
3520 	pf->fd_sctp4_filter_cnt = 0;
3521 	pf->fd_ip4_filter_cnt = 0;
3522 	pf->fd_tcp6_filter_cnt = 0;
3523 	pf->fd_udp6_filter_cnt = 0;
3524 	pf->fd_sctp6_filter_cnt = 0;
3525 	pf->fd_ip6_filter_cnt = 0;
3526 }
3527 
3528 /**
3529  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3530  * @vsi: Pointer to the targeted VSI
3531  *
3532  * This function replays the hlist on the hw where all the SB Flow Director
3533  * filters were saved.
3534  **/
3535 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3536 {
3537 	struct i40e_fdir_filter *filter;
3538 	struct i40e_pf *pf = vsi->back;
3539 	struct hlist_node *node;
3540 
3541 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3542 		return;
3543 
3544 	/* Reset FDir counters as we're replaying all existing filters */
3545 	i40e_reset_fdir_filter_cnt(pf);
3546 
3547 	hlist_for_each_entry_safe(filter, node,
3548 				  &pf->fdir_filter_list, fdir_node) {
3549 		i40e_add_del_fdir(vsi, filter, true);
3550 	}
3551 }
3552 
3553 /**
3554  * i40e_vsi_configure - Set up the VSI for action
3555  * @vsi: the VSI being configured
3556  **/
3557 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3558 {
3559 	int err;
3560 
3561 	i40e_set_vsi_rx_mode(vsi);
3562 	i40e_restore_vlan(vsi);
3563 	i40e_vsi_config_dcb_rings(vsi);
3564 	err = i40e_vsi_configure_tx(vsi);
3565 	if (!err)
3566 		err = i40e_vsi_configure_rx(vsi);
3567 
3568 	return err;
3569 }
3570 
3571 /**
3572  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3573  * @vsi: the VSI being configured
3574  **/
3575 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3576 {
3577 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3578 	struct i40e_pf *pf = vsi->back;
3579 	struct i40e_hw *hw = &pf->hw;
3580 	u16 vector;
3581 	int i, q;
3582 	u32 qp;
3583 
3584 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3585 	 * and PFINT_LNKLSTn registers, e.g.:
3586 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3587 	 */
3588 	qp = vsi->base_queue;
3589 	vector = vsi->base_vector;
3590 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3591 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3592 
3593 		q_vector->rx.next_update = jiffies + 1;
3594 		q_vector->rx.target_itr =
3595 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3596 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3597 		     q_vector->rx.target_itr >> 1);
3598 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3599 
3600 		q_vector->tx.next_update = jiffies + 1;
3601 		q_vector->tx.target_itr =
3602 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3603 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3604 		     q_vector->tx.target_itr >> 1);
3605 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3606 
3607 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3608 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3609 
3610 		/* Linked list for the queuepairs assigned to this vector */
3611 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3612 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3613 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3614 			u32 val;
3615 
3616 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3617 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3618 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3619 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3620 			      (I40E_QUEUE_TYPE_TX <<
3621 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3622 
3623 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3624 
3625 			if (has_xdp) {
3626 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3627 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3628 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3629 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3630 				      (I40E_QUEUE_TYPE_TX <<
3631 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3632 
3633 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3634 			}
3635 
3636 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3637 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3638 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3639 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3640 			      (I40E_QUEUE_TYPE_RX <<
3641 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3642 
3643 			/* Terminate the linked list */
3644 			if (q == (q_vector->num_ringpairs - 1))
3645 				val |= (I40E_QUEUE_END_OF_LIST <<
3646 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3647 
3648 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3649 			qp++;
3650 		}
3651 	}
3652 
3653 	i40e_flush(hw);
3654 }
3655 
3656 /**
3657  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3658  * @pf: pointer to private device data structure
3659  **/
3660 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3661 {
3662 	struct i40e_hw *hw = &pf->hw;
3663 	u32 val;
3664 
3665 	/* clear things first */
3666 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3667 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3668 
3669 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3670 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3671 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3672 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3673 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3674 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3675 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3676 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3677 
3678 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3679 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3680 
3681 	if (pf->flags & I40E_FLAG_PTP)
3682 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3683 
3684 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3685 
3686 	/* SW_ITR_IDX = 0, but don't change INTENA */
3687 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3688 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3689 
3690 	/* OTHER_ITR_IDX = 0 */
3691 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3692 }
3693 
3694 /**
3695  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3696  * @vsi: the VSI being configured
3697  **/
3698 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3699 {
3700 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3701 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3702 	struct i40e_pf *pf = vsi->back;
3703 	struct i40e_hw *hw = &pf->hw;
3704 	u32 val;
3705 
3706 	/* set the ITR configuration */
3707 	q_vector->rx.next_update = jiffies + 1;
3708 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3709 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3710 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3711 	q_vector->tx.next_update = jiffies + 1;
3712 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3713 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3714 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3715 
3716 	i40e_enable_misc_int_causes(pf);
3717 
3718 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3719 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3720 
3721 	/* Associate the queue pair to the vector and enable the queue int */
3722 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3723 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3724 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3725 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3726 
3727 	wr32(hw, I40E_QINT_RQCTL(0), val);
3728 
3729 	if (i40e_enabled_xdp_vsi(vsi)) {
3730 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
3731 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3732 		      (I40E_QUEUE_TYPE_TX
3733 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3734 
3735 		wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3736 	}
3737 
3738 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
3739 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3740 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3741 
3742 	wr32(hw, I40E_QINT_TQCTL(0), val);
3743 	i40e_flush(hw);
3744 }
3745 
3746 /**
3747  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3748  * @pf: board private structure
3749  **/
3750 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3751 {
3752 	struct i40e_hw *hw = &pf->hw;
3753 
3754 	wr32(hw, I40E_PFINT_DYN_CTL0,
3755 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3756 	i40e_flush(hw);
3757 }
3758 
3759 /**
3760  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3761  * @pf: board private structure
3762  **/
3763 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3764 {
3765 	struct i40e_hw *hw = &pf->hw;
3766 	u32 val;
3767 
3768 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
3769 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3770 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3771 
3772 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
3773 	i40e_flush(hw);
3774 }
3775 
3776 /**
3777  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3778  * @irq: interrupt number
3779  * @data: pointer to a q_vector
3780  **/
3781 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3782 {
3783 	struct i40e_q_vector *q_vector = data;
3784 
3785 	if (!q_vector->tx.ring && !q_vector->rx.ring)
3786 		return IRQ_HANDLED;
3787 
3788 	napi_schedule_irqoff(&q_vector->napi);
3789 
3790 	return IRQ_HANDLED;
3791 }
3792 
3793 /**
3794  * i40e_irq_affinity_notify - Callback for affinity changes
3795  * @notify: context as to what irq was changed
3796  * @mask: the new affinity mask
3797  *
3798  * This is a callback function used by the irq_set_affinity_notifier function
3799  * so that we may register to receive changes to the irq affinity masks.
3800  **/
3801 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3802 				     const cpumask_t *mask)
3803 {
3804 	struct i40e_q_vector *q_vector =
3805 		container_of(notify, struct i40e_q_vector, affinity_notify);
3806 
3807 	cpumask_copy(&q_vector->affinity_mask, mask);
3808 }
3809 
3810 /**
3811  * i40e_irq_affinity_release - Callback for affinity notifier release
3812  * @ref: internal core kernel usage
3813  *
3814  * This is a callback function used by the irq_set_affinity_notifier function
3815  * to inform the current notification subscriber that they will no longer
3816  * receive notifications.
3817  **/
3818 static void i40e_irq_affinity_release(struct kref *ref) {}
3819 
3820 /**
3821  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3822  * @vsi: the VSI being configured
3823  * @basename: name for the vector
3824  *
3825  * Allocates MSI-X vectors and requests interrupts from the kernel.
3826  **/
3827 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3828 {
3829 	int q_vectors = vsi->num_q_vectors;
3830 	struct i40e_pf *pf = vsi->back;
3831 	int base = vsi->base_vector;
3832 	int rx_int_idx = 0;
3833 	int tx_int_idx = 0;
3834 	int vector, err;
3835 	int irq_num;
3836 	int cpu;
3837 
3838 	for (vector = 0; vector < q_vectors; vector++) {
3839 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
3840 
3841 		irq_num = pf->msix_entries[base + vector].vector;
3842 
3843 		if (q_vector->tx.ring && q_vector->rx.ring) {
3844 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3845 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3846 			tx_int_idx++;
3847 		} else if (q_vector->rx.ring) {
3848 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3849 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
3850 		} else if (q_vector->tx.ring) {
3851 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3852 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
3853 		} else {
3854 			/* skip this unused q_vector */
3855 			continue;
3856 		}
3857 		err = request_irq(irq_num,
3858 				  vsi->irq_handler,
3859 				  0,
3860 				  q_vector->name,
3861 				  q_vector);
3862 		if (err) {
3863 			dev_info(&pf->pdev->dev,
3864 				 "MSIX request_irq failed, error: %d\n", err);
3865 			goto free_queue_irqs;
3866 		}
3867 
3868 		/* register for affinity change notifications */
3869 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3870 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
3871 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
3872 		/* Spread affinity hints out across online CPUs.
3873 		 *
3874 		 * get_cpu_mask returns a static constant mask with
3875 		 * a permanent lifetime so it's ok to pass to
3876 		 * irq_set_affinity_hint without making a copy.
3877 		 */
3878 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
3879 		irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
3880 	}
3881 
3882 	vsi->irqs_ready = true;
3883 	return 0;
3884 
3885 free_queue_irqs:
3886 	while (vector) {
3887 		vector--;
3888 		irq_num = pf->msix_entries[base + vector].vector;
3889 		irq_set_affinity_notifier(irq_num, NULL);
3890 		irq_set_affinity_hint(irq_num, NULL);
3891 		free_irq(irq_num, &vsi->q_vectors[vector]);
3892 	}
3893 	return err;
3894 }
3895 
3896 /**
3897  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3898  * @vsi: the VSI being un-configured
3899  **/
3900 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3901 {
3902 	struct i40e_pf *pf = vsi->back;
3903 	struct i40e_hw *hw = &pf->hw;
3904 	int base = vsi->base_vector;
3905 	int i;
3906 
3907 	/* disable interrupt causation from each queue */
3908 	for (i = 0; i < vsi->num_queue_pairs; i++) {
3909 		u32 val;
3910 
3911 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3912 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3913 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3914 
3915 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3916 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3917 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3918 
3919 		if (!i40e_enabled_xdp_vsi(vsi))
3920 			continue;
3921 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
3922 	}
3923 
3924 	/* disable each interrupt */
3925 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3926 		for (i = vsi->base_vector;
3927 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
3928 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3929 
3930 		i40e_flush(hw);
3931 		for (i = 0; i < vsi->num_q_vectors; i++)
3932 			synchronize_irq(pf->msix_entries[i + base].vector);
3933 	} else {
3934 		/* Legacy and MSI mode - this stops all interrupt handling */
3935 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3936 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3937 		i40e_flush(hw);
3938 		synchronize_irq(pf->pdev->irq);
3939 	}
3940 }
3941 
3942 /**
3943  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3944  * @vsi: the VSI being configured
3945  **/
3946 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3947 {
3948 	struct i40e_pf *pf = vsi->back;
3949 	int i;
3950 
3951 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3952 		for (i = 0; i < vsi->num_q_vectors; i++)
3953 			i40e_irq_dynamic_enable(vsi, i);
3954 	} else {
3955 		i40e_irq_dynamic_enable_icr0(pf);
3956 	}
3957 
3958 	i40e_flush(&pf->hw);
3959 	return 0;
3960 }
3961 
3962 /**
3963  * i40e_free_misc_vector - Free the vector that handles non-queue events
3964  * @pf: board private structure
3965  **/
3966 static void i40e_free_misc_vector(struct i40e_pf *pf)
3967 {
3968 	/* Disable ICR 0 */
3969 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3970 	i40e_flush(&pf->hw);
3971 
3972 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
3973 		synchronize_irq(pf->msix_entries[0].vector);
3974 		free_irq(pf->msix_entries[0].vector, pf);
3975 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
3976 	}
3977 }
3978 
3979 /**
3980  * i40e_intr - MSI/Legacy and non-queue interrupt handler
3981  * @irq: interrupt number
3982  * @data: pointer to a q_vector
3983  *
3984  * This is the handler used for all MSI/Legacy interrupts, and deals
3985  * with both queue and non-queue interrupts.  This is also used in
3986  * MSIX mode to handle the non-queue interrupts.
3987  **/
3988 static irqreturn_t i40e_intr(int irq, void *data)
3989 {
3990 	struct i40e_pf *pf = (struct i40e_pf *)data;
3991 	struct i40e_hw *hw = &pf->hw;
3992 	irqreturn_t ret = IRQ_NONE;
3993 	u32 icr0, icr0_remaining;
3994 	u32 val, ena_mask;
3995 
3996 	icr0 = rd32(hw, I40E_PFINT_ICR0);
3997 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
3998 
3999 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4000 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4001 		goto enable_intr;
4002 
4003 	/* if interrupt but no bits showing, must be SWINT */
4004 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4005 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4006 		pf->sw_int_count++;
4007 
4008 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4009 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4010 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4011 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4012 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4013 	}
4014 
4015 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4016 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4017 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4018 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4019 
4020 		/* We do not have a way to disarm Queue causes while leaving
4021 		 * interrupt enabled for all other causes, ideally
4022 		 * interrupt should be disabled while we are in NAPI but
4023 		 * this is not a performance path and napi_schedule()
4024 		 * can deal with rescheduling.
4025 		 */
4026 		if (!test_bit(__I40E_DOWN, pf->state))
4027 			napi_schedule_irqoff(&q_vector->napi);
4028 	}
4029 
4030 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4031 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4032 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4033 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4034 	}
4035 
4036 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4037 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4038 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4039 	}
4040 
4041 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4042 		/* disable any further VFLR event notifications */
4043 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4044 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4045 
4046 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4047 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4048 		} else {
4049 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4050 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4051 		}
4052 	}
4053 
4054 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4055 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4056 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4057 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4058 		val = rd32(hw, I40E_GLGEN_RSTAT);
4059 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4060 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4061 		if (val == I40E_RESET_CORER) {
4062 			pf->corer_count++;
4063 		} else if (val == I40E_RESET_GLOBR) {
4064 			pf->globr_count++;
4065 		} else if (val == I40E_RESET_EMPR) {
4066 			pf->empr_count++;
4067 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4068 		}
4069 	}
4070 
4071 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4072 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4073 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4074 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4075 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4076 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4077 	}
4078 
4079 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4080 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4081 
4082 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
4083 			icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4084 			i40e_ptp_tx_hwtstamp(pf);
4085 		}
4086 	}
4087 
4088 	/* If a critical error is pending we have no choice but to reset the
4089 	 * device.
4090 	 * Report and mask out any remaining unexpected interrupts.
4091 	 */
4092 	icr0_remaining = icr0 & ena_mask;
4093 	if (icr0_remaining) {
4094 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4095 			 icr0_remaining);
4096 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4097 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4098 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4099 			dev_info(&pf->pdev->dev, "device will be reset\n");
4100 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4101 			i40e_service_event_schedule(pf);
4102 		}
4103 		ena_mask &= ~icr0_remaining;
4104 	}
4105 	ret = IRQ_HANDLED;
4106 
4107 enable_intr:
4108 	/* re-enable interrupt causes */
4109 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4110 	if (!test_bit(__I40E_DOWN, pf->state) ||
4111 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4112 		i40e_service_event_schedule(pf);
4113 		i40e_irq_dynamic_enable_icr0(pf);
4114 	}
4115 
4116 	return ret;
4117 }
4118 
4119 /**
4120  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4121  * @tx_ring:  tx ring to clean
4122  * @budget:   how many cleans we're allowed
4123  *
4124  * Returns true if there's any budget left (e.g. the clean is finished)
4125  **/
4126 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4127 {
4128 	struct i40e_vsi *vsi = tx_ring->vsi;
4129 	u16 i = tx_ring->next_to_clean;
4130 	struct i40e_tx_buffer *tx_buf;
4131 	struct i40e_tx_desc *tx_desc;
4132 
4133 	tx_buf = &tx_ring->tx_bi[i];
4134 	tx_desc = I40E_TX_DESC(tx_ring, i);
4135 	i -= tx_ring->count;
4136 
4137 	do {
4138 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4139 
4140 		/* if next_to_watch is not set then there is no work pending */
4141 		if (!eop_desc)
4142 			break;
4143 
4144 		/* prevent any other reads prior to eop_desc */
4145 		smp_rmb();
4146 
4147 		/* if the descriptor isn't done, no work yet to do */
4148 		if (!(eop_desc->cmd_type_offset_bsz &
4149 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4150 			break;
4151 
4152 		/* clear next_to_watch to prevent false hangs */
4153 		tx_buf->next_to_watch = NULL;
4154 
4155 		tx_desc->buffer_addr = 0;
4156 		tx_desc->cmd_type_offset_bsz = 0;
4157 		/* move past filter desc */
4158 		tx_buf++;
4159 		tx_desc++;
4160 		i++;
4161 		if (unlikely(!i)) {
4162 			i -= tx_ring->count;
4163 			tx_buf = tx_ring->tx_bi;
4164 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4165 		}
4166 		/* unmap skb header data */
4167 		dma_unmap_single(tx_ring->dev,
4168 				 dma_unmap_addr(tx_buf, dma),
4169 				 dma_unmap_len(tx_buf, len),
4170 				 DMA_TO_DEVICE);
4171 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4172 			kfree(tx_buf->raw_buf);
4173 
4174 		tx_buf->raw_buf = NULL;
4175 		tx_buf->tx_flags = 0;
4176 		tx_buf->next_to_watch = NULL;
4177 		dma_unmap_len_set(tx_buf, len, 0);
4178 		tx_desc->buffer_addr = 0;
4179 		tx_desc->cmd_type_offset_bsz = 0;
4180 
4181 		/* move us past the eop_desc for start of next FD desc */
4182 		tx_buf++;
4183 		tx_desc++;
4184 		i++;
4185 		if (unlikely(!i)) {
4186 			i -= tx_ring->count;
4187 			tx_buf = tx_ring->tx_bi;
4188 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4189 		}
4190 
4191 		/* update budget accounting */
4192 		budget--;
4193 	} while (likely(budget));
4194 
4195 	i += tx_ring->count;
4196 	tx_ring->next_to_clean = i;
4197 
4198 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4199 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4200 
4201 	return budget > 0;
4202 }
4203 
4204 /**
4205  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4206  * @irq: interrupt number
4207  * @data: pointer to a q_vector
4208  **/
4209 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4210 {
4211 	struct i40e_q_vector *q_vector = data;
4212 	struct i40e_vsi *vsi;
4213 
4214 	if (!q_vector->tx.ring)
4215 		return IRQ_HANDLED;
4216 
4217 	vsi = q_vector->tx.ring->vsi;
4218 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4219 
4220 	return IRQ_HANDLED;
4221 }
4222 
4223 /**
4224  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4225  * @vsi: the VSI being configured
4226  * @v_idx: vector index
4227  * @qp_idx: queue pair index
4228  **/
4229 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4230 {
4231 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4232 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4233 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4234 
4235 	tx_ring->q_vector = q_vector;
4236 	tx_ring->next = q_vector->tx.ring;
4237 	q_vector->tx.ring = tx_ring;
4238 	q_vector->tx.count++;
4239 
4240 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4241 	if (i40e_enabled_xdp_vsi(vsi)) {
4242 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4243 
4244 		xdp_ring->q_vector = q_vector;
4245 		xdp_ring->next = q_vector->tx.ring;
4246 		q_vector->tx.ring = xdp_ring;
4247 		q_vector->tx.count++;
4248 	}
4249 
4250 	rx_ring->q_vector = q_vector;
4251 	rx_ring->next = q_vector->rx.ring;
4252 	q_vector->rx.ring = rx_ring;
4253 	q_vector->rx.count++;
4254 }
4255 
4256 /**
4257  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4258  * @vsi: the VSI being configured
4259  *
4260  * This function maps descriptor rings to the queue-specific vectors
4261  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4262  * one vector per queue pair, but on a constrained vector budget, we
4263  * group the queue pairs as "efficiently" as possible.
4264  **/
4265 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4266 {
4267 	int qp_remaining = vsi->num_queue_pairs;
4268 	int q_vectors = vsi->num_q_vectors;
4269 	int num_ringpairs;
4270 	int v_start = 0;
4271 	int qp_idx = 0;
4272 
4273 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4274 	 * group them so there are multiple queues per vector.
4275 	 * It is also important to go through all the vectors available to be
4276 	 * sure that if we don't use all the vectors, that the remaining vectors
4277 	 * are cleared. This is especially important when decreasing the
4278 	 * number of queues in use.
4279 	 */
4280 	for (; v_start < q_vectors; v_start++) {
4281 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4282 
4283 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4284 
4285 		q_vector->num_ringpairs = num_ringpairs;
4286 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4287 
4288 		q_vector->rx.count = 0;
4289 		q_vector->tx.count = 0;
4290 		q_vector->rx.ring = NULL;
4291 		q_vector->tx.ring = NULL;
4292 
4293 		while (num_ringpairs--) {
4294 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4295 			qp_idx++;
4296 			qp_remaining--;
4297 		}
4298 	}
4299 }
4300 
4301 /**
4302  * i40e_vsi_request_irq - Request IRQ from the OS
4303  * @vsi: the VSI being configured
4304  * @basename: name for the vector
4305  **/
4306 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4307 {
4308 	struct i40e_pf *pf = vsi->back;
4309 	int err;
4310 
4311 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4312 		err = i40e_vsi_request_irq_msix(vsi, basename);
4313 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4314 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4315 				  pf->int_name, pf);
4316 	else
4317 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4318 				  pf->int_name, pf);
4319 
4320 	if (err)
4321 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4322 
4323 	return err;
4324 }
4325 
4326 #ifdef CONFIG_NET_POLL_CONTROLLER
4327 /**
4328  * i40e_netpoll - A Polling 'interrupt' handler
4329  * @netdev: network interface device structure
4330  *
4331  * This is used by netconsole to send skbs without having to re-enable
4332  * interrupts.  It's not called while the normal interrupt routine is executing.
4333  **/
4334 static void i40e_netpoll(struct net_device *netdev)
4335 {
4336 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4337 	struct i40e_vsi *vsi = np->vsi;
4338 	struct i40e_pf *pf = vsi->back;
4339 	int i;
4340 
4341 	/* if interface is down do nothing */
4342 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4343 		return;
4344 
4345 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4346 		for (i = 0; i < vsi->num_q_vectors; i++)
4347 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4348 	} else {
4349 		i40e_intr(pf->pdev->irq, netdev);
4350 	}
4351 }
4352 #endif
4353 
4354 #define I40E_QTX_ENA_WAIT_COUNT 50
4355 
4356 /**
4357  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4358  * @pf: the PF being configured
4359  * @pf_q: the PF queue
4360  * @enable: enable or disable state of the queue
4361  *
4362  * This routine will wait for the given Tx queue of the PF to reach the
4363  * enabled or disabled state.
4364  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4365  * multiple retries; else will return 0 in case of success.
4366  **/
4367 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4368 {
4369 	int i;
4370 	u32 tx_reg;
4371 
4372 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4373 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4374 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4375 			break;
4376 
4377 		usleep_range(10, 20);
4378 	}
4379 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4380 		return -ETIMEDOUT;
4381 
4382 	return 0;
4383 }
4384 
4385 /**
4386  * i40e_control_tx_q - Start or stop a particular Tx queue
4387  * @pf: the PF structure
4388  * @pf_q: the PF queue to configure
4389  * @enable: start or stop the queue
4390  *
4391  * This function enables or disables a single queue. Note that any delay
4392  * required after the operation is expected to be handled by the caller of
4393  * this function.
4394  **/
4395 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4396 {
4397 	struct i40e_hw *hw = &pf->hw;
4398 	u32 tx_reg;
4399 	int i;
4400 
4401 	/* warn the TX unit of coming changes */
4402 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4403 	if (!enable)
4404 		usleep_range(10, 20);
4405 
4406 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4407 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4408 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4409 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4410 			break;
4411 		usleep_range(1000, 2000);
4412 	}
4413 
4414 	/* Skip if the queue is already in the requested state */
4415 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4416 		return;
4417 
4418 	/* turn on/off the queue */
4419 	if (enable) {
4420 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4421 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4422 	} else {
4423 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4424 	}
4425 
4426 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4427 }
4428 
4429 /**
4430  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4431  * @seid: VSI SEID
4432  * @pf: the PF structure
4433  * @pf_q: the PF queue to configure
4434  * @is_xdp: true if the queue is used for XDP
4435  * @enable: start or stop the queue
4436  **/
4437 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4438 			   bool is_xdp, bool enable)
4439 {
4440 	int ret;
4441 
4442 	i40e_control_tx_q(pf, pf_q, enable);
4443 
4444 	/* wait for the change to finish */
4445 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4446 	if (ret) {
4447 		dev_info(&pf->pdev->dev,
4448 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4449 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4450 			 (enable ? "en" : "dis"));
4451 	}
4452 
4453 	return ret;
4454 }
4455 
4456 /**
4457  * i40e_vsi_control_tx - Start or stop a VSI's rings
4458  * @vsi: the VSI being configured
4459  * @enable: start or stop the rings
4460  **/
4461 static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4462 {
4463 	struct i40e_pf *pf = vsi->back;
4464 	int i, pf_q, ret = 0;
4465 
4466 	pf_q = vsi->base_queue;
4467 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4468 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4469 					     pf_q,
4470 					     false /*is xdp*/, enable);
4471 		if (ret)
4472 			break;
4473 
4474 		if (!i40e_enabled_xdp_vsi(vsi))
4475 			continue;
4476 
4477 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4478 					     pf_q + vsi->alloc_queue_pairs,
4479 					     true /*is xdp*/, enable);
4480 		if (ret)
4481 			break;
4482 	}
4483 	return ret;
4484 }
4485 
4486 /**
4487  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4488  * @pf: the PF being configured
4489  * @pf_q: the PF queue
4490  * @enable: enable or disable state of the queue
4491  *
4492  * This routine will wait for the given Rx queue of the PF to reach the
4493  * enabled or disabled state.
4494  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4495  * multiple retries; else will return 0 in case of success.
4496  **/
4497 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4498 {
4499 	int i;
4500 	u32 rx_reg;
4501 
4502 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4503 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4504 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4505 			break;
4506 
4507 		usleep_range(10, 20);
4508 	}
4509 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4510 		return -ETIMEDOUT;
4511 
4512 	return 0;
4513 }
4514 
4515 /**
4516  * i40e_control_rx_q - Start or stop a particular Rx queue
4517  * @pf: the PF structure
4518  * @pf_q: the PF queue to configure
4519  * @enable: start or stop the queue
4520  *
4521  * This function enables or disables a single queue. Note that
4522  * any delay required after the operation is expected to be
4523  * handled by the caller of this function.
4524  **/
4525 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4526 {
4527 	struct i40e_hw *hw = &pf->hw;
4528 	u32 rx_reg;
4529 	int i;
4530 
4531 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4532 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4533 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4534 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4535 			break;
4536 		usleep_range(1000, 2000);
4537 	}
4538 
4539 	/* Skip if the queue is already in the requested state */
4540 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4541 		return;
4542 
4543 	/* turn on/off the queue */
4544 	if (enable)
4545 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4546 	else
4547 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4548 
4549 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4550 }
4551 
4552 /**
4553  * i40e_control_wait_rx_q
4554  * @pf: the PF structure
4555  * @pf_q: queue being configured
4556  * @enable: start or stop the rings
4557  *
4558  * This function enables or disables a single queue along with waiting
4559  * for the change to finish. The caller of this function should handle
4560  * the delays needed in the case of disabling queues.
4561  **/
4562 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4563 {
4564 	int ret = 0;
4565 
4566 	i40e_control_rx_q(pf, pf_q, enable);
4567 
4568 	/* wait for the change to finish */
4569 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4570 	if (ret)
4571 		return ret;
4572 
4573 	return ret;
4574 }
4575 
4576 /**
4577  * i40e_vsi_control_rx - Start or stop a VSI's rings
4578  * @vsi: the VSI being configured
4579  * @enable: start or stop the rings
4580  **/
4581 static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4582 {
4583 	struct i40e_pf *pf = vsi->back;
4584 	int i, pf_q, ret = 0;
4585 
4586 	pf_q = vsi->base_queue;
4587 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4588 		ret = i40e_control_wait_rx_q(pf, pf_q, enable);
4589 		if (ret) {
4590 			dev_info(&pf->pdev->dev,
4591 				 "VSI seid %d Rx ring %d %sable timeout\n",
4592 				 vsi->seid, pf_q, (enable ? "en" : "dis"));
4593 			break;
4594 		}
4595 	}
4596 
4597 	/* Due to HW errata, on Rx disable only, the register can indicate done
4598 	 * before it really is. Needs 50ms to be sure
4599 	 */
4600 	if (!enable)
4601 		mdelay(50);
4602 
4603 	return ret;
4604 }
4605 
4606 /**
4607  * i40e_vsi_start_rings - Start a VSI's rings
4608  * @vsi: the VSI being configured
4609  **/
4610 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4611 {
4612 	int ret = 0;
4613 
4614 	/* do rx first for enable and last for disable */
4615 	ret = i40e_vsi_control_rx(vsi, true);
4616 	if (ret)
4617 		return ret;
4618 	ret = i40e_vsi_control_tx(vsi, true);
4619 
4620 	return ret;
4621 }
4622 
4623 /**
4624  * i40e_vsi_stop_rings - Stop a VSI's rings
4625  * @vsi: the VSI being configured
4626  **/
4627 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4628 {
4629 	/* When port TX is suspended, don't wait */
4630 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4631 		return i40e_vsi_stop_rings_no_wait(vsi);
4632 
4633 	/* do rx first for enable and last for disable
4634 	 * Ignore return value, we need to shutdown whatever we can
4635 	 */
4636 	i40e_vsi_control_tx(vsi, false);
4637 	i40e_vsi_control_rx(vsi, false);
4638 }
4639 
4640 /**
4641  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4642  * @vsi: the VSI being shutdown
4643  *
4644  * This function stops all the rings for a VSI but does not delay to verify
4645  * that rings have been disabled. It is expected that the caller is shutting
4646  * down multiple VSIs at once and will delay together for all the VSIs after
4647  * initiating the shutdown. This is particularly useful for shutting down lots
4648  * of VFs together. Otherwise, a large delay can be incurred while configuring
4649  * each VSI in serial.
4650  **/
4651 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4652 {
4653 	struct i40e_pf *pf = vsi->back;
4654 	int i, pf_q;
4655 
4656 	pf_q = vsi->base_queue;
4657 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4658 		i40e_control_tx_q(pf, pf_q, false);
4659 		i40e_control_rx_q(pf, pf_q, false);
4660 	}
4661 }
4662 
4663 /**
4664  * i40e_vsi_free_irq - Free the irq association with the OS
4665  * @vsi: the VSI being configured
4666  **/
4667 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4668 {
4669 	struct i40e_pf *pf = vsi->back;
4670 	struct i40e_hw *hw = &pf->hw;
4671 	int base = vsi->base_vector;
4672 	u32 val, qp;
4673 	int i;
4674 
4675 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4676 		if (!vsi->q_vectors)
4677 			return;
4678 
4679 		if (!vsi->irqs_ready)
4680 			return;
4681 
4682 		vsi->irqs_ready = false;
4683 		for (i = 0; i < vsi->num_q_vectors; i++) {
4684 			int irq_num;
4685 			u16 vector;
4686 
4687 			vector = i + base;
4688 			irq_num = pf->msix_entries[vector].vector;
4689 
4690 			/* free only the irqs that were actually requested */
4691 			if (!vsi->q_vectors[i] ||
4692 			    !vsi->q_vectors[i]->num_ringpairs)
4693 				continue;
4694 
4695 			/* clear the affinity notifier in the IRQ descriptor */
4696 			irq_set_affinity_notifier(irq_num, NULL);
4697 			/* remove our suggested affinity mask for this IRQ */
4698 			irq_set_affinity_hint(irq_num, NULL);
4699 			synchronize_irq(irq_num);
4700 			free_irq(irq_num, vsi->q_vectors[i]);
4701 
4702 			/* Tear down the interrupt queue link list
4703 			 *
4704 			 * We know that they come in pairs and always
4705 			 * the Rx first, then the Tx.  To clear the
4706 			 * link list, stick the EOL value into the
4707 			 * next_q field of the registers.
4708 			 */
4709 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4710 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4711 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4712 			val |= I40E_QUEUE_END_OF_LIST
4713 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4714 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4715 
4716 			while (qp != I40E_QUEUE_END_OF_LIST) {
4717 				u32 next;
4718 
4719 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4720 
4721 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4722 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4723 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4724 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4725 
4726 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4727 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4728 
4729 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4730 
4731 				val = rd32(hw, I40E_QINT_TQCTL(qp));
4732 
4733 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4734 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4735 
4736 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4737 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4738 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4739 					 I40E_QINT_TQCTL_INTEVENT_MASK);
4740 
4741 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4742 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4743 
4744 				wr32(hw, I40E_QINT_TQCTL(qp), val);
4745 				qp = next;
4746 			}
4747 		}
4748 	} else {
4749 		free_irq(pf->pdev->irq, pf);
4750 
4751 		val = rd32(hw, I40E_PFINT_LNKLST0);
4752 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4753 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4754 		val |= I40E_QUEUE_END_OF_LIST
4755 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4756 		wr32(hw, I40E_PFINT_LNKLST0, val);
4757 
4758 		val = rd32(hw, I40E_QINT_RQCTL(qp));
4759 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4760 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4761 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4762 			 I40E_QINT_RQCTL_INTEVENT_MASK);
4763 
4764 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4765 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4766 
4767 		wr32(hw, I40E_QINT_RQCTL(qp), val);
4768 
4769 		val = rd32(hw, I40E_QINT_TQCTL(qp));
4770 
4771 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4772 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4773 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4774 			 I40E_QINT_TQCTL_INTEVENT_MASK);
4775 
4776 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4777 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4778 
4779 		wr32(hw, I40E_QINT_TQCTL(qp), val);
4780 	}
4781 }
4782 
4783 /**
4784  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4785  * @vsi: the VSI being configured
4786  * @v_idx: Index of vector to be freed
4787  *
4788  * This function frees the memory allocated to the q_vector.  In addition if
4789  * NAPI is enabled it will delete any references to the NAPI struct prior
4790  * to freeing the q_vector.
4791  **/
4792 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4793 {
4794 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4795 	struct i40e_ring *ring;
4796 
4797 	if (!q_vector)
4798 		return;
4799 
4800 	/* disassociate q_vector from rings */
4801 	i40e_for_each_ring(ring, q_vector->tx)
4802 		ring->q_vector = NULL;
4803 
4804 	i40e_for_each_ring(ring, q_vector->rx)
4805 		ring->q_vector = NULL;
4806 
4807 	/* only VSI w/ an associated netdev is set up w/ NAPI */
4808 	if (vsi->netdev)
4809 		netif_napi_del(&q_vector->napi);
4810 
4811 	vsi->q_vectors[v_idx] = NULL;
4812 
4813 	kfree_rcu(q_vector, rcu);
4814 }
4815 
4816 /**
4817  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4818  * @vsi: the VSI being un-configured
4819  *
4820  * This frees the memory allocated to the q_vectors and
4821  * deletes references to the NAPI struct.
4822  **/
4823 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4824 {
4825 	int v_idx;
4826 
4827 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4828 		i40e_free_q_vector(vsi, v_idx);
4829 }
4830 
4831 /**
4832  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4833  * @pf: board private structure
4834  **/
4835 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4836 {
4837 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4838 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4839 		pci_disable_msix(pf->pdev);
4840 		kfree(pf->msix_entries);
4841 		pf->msix_entries = NULL;
4842 		kfree(pf->irq_pile);
4843 		pf->irq_pile = NULL;
4844 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4845 		pci_disable_msi(pf->pdev);
4846 	}
4847 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4848 }
4849 
4850 /**
4851  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4852  * @pf: board private structure
4853  *
4854  * We go through and clear interrupt specific resources and reset the structure
4855  * to pre-load conditions
4856  **/
4857 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4858 {
4859 	int i;
4860 
4861 	i40e_free_misc_vector(pf);
4862 
4863 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4864 		      I40E_IWARP_IRQ_PILE_ID);
4865 
4866 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
4867 	for (i = 0; i < pf->num_alloc_vsi; i++)
4868 		if (pf->vsi[i])
4869 			i40e_vsi_free_q_vectors(pf->vsi[i]);
4870 	i40e_reset_interrupt_capability(pf);
4871 }
4872 
4873 /**
4874  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4875  * @vsi: the VSI being configured
4876  **/
4877 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4878 {
4879 	int q_idx;
4880 
4881 	if (!vsi->netdev)
4882 		return;
4883 
4884 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4885 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4886 
4887 		if (q_vector->rx.ring || q_vector->tx.ring)
4888 			napi_enable(&q_vector->napi);
4889 	}
4890 }
4891 
4892 /**
4893  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4894  * @vsi: the VSI being configured
4895  **/
4896 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4897 {
4898 	int q_idx;
4899 
4900 	if (!vsi->netdev)
4901 		return;
4902 
4903 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4904 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4905 
4906 		if (q_vector->rx.ring || q_vector->tx.ring)
4907 			napi_disable(&q_vector->napi);
4908 	}
4909 }
4910 
4911 /**
4912  * i40e_vsi_close - Shut down a VSI
4913  * @vsi: the vsi to be quelled
4914  **/
4915 static void i40e_vsi_close(struct i40e_vsi *vsi)
4916 {
4917 	struct i40e_pf *pf = vsi->back;
4918 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
4919 		i40e_down(vsi);
4920 	i40e_vsi_free_irq(vsi);
4921 	i40e_vsi_free_tx_resources(vsi);
4922 	i40e_vsi_free_rx_resources(vsi);
4923 	vsi->current_netdev_flags = 0;
4924 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
4925 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4926 		set_bit(__I40E_CLIENT_RESET, pf->state);
4927 }
4928 
4929 /**
4930  * i40e_quiesce_vsi - Pause a given VSI
4931  * @vsi: the VSI being paused
4932  **/
4933 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4934 {
4935 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4936 		return;
4937 
4938 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
4939 	if (vsi->netdev && netif_running(vsi->netdev))
4940 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
4941 	else
4942 		i40e_vsi_close(vsi);
4943 }
4944 
4945 /**
4946  * i40e_unquiesce_vsi - Resume a given VSI
4947  * @vsi: the VSI being resumed
4948  **/
4949 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4950 {
4951 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
4952 		return;
4953 
4954 	if (vsi->netdev && netif_running(vsi->netdev))
4955 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4956 	else
4957 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
4958 }
4959 
4960 /**
4961  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4962  * @pf: the PF
4963  **/
4964 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4965 {
4966 	int v;
4967 
4968 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4969 		if (pf->vsi[v])
4970 			i40e_quiesce_vsi(pf->vsi[v]);
4971 	}
4972 }
4973 
4974 /**
4975  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4976  * @pf: the PF
4977  **/
4978 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4979 {
4980 	int v;
4981 
4982 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4983 		if (pf->vsi[v])
4984 			i40e_unquiesce_vsi(pf->vsi[v]);
4985 	}
4986 }
4987 
4988 /**
4989  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
4990  * @vsi: the VSI being configured
4991  *
4992  * Wait until all queues on a given VSI have been disabled.
4993  **/
4994 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
4995 {
4996 	struct i40e_pf *pf = vsi->back;
4997 	int i, pf_q, ret;
4998 
4999 	pf_q = vsi->base_queue;
5000 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5001 		/* Check and wait for the Tx queue */
5002 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5003 		if (ret) {
5004 			dev_info(&pf->pdev->dev,
5005 				 "VSI seid %d Tx ring %d disable timeout\n",
5006 				 vsi->seid, pf_q);
5007 			return ret;
5008 		}
5009 
5010 		if (!i40e_enabled_xdp_vsi(vsi))
5011 			goto wait_rx;
5012 
5013 		/* Check and wait for the XDP Tx queue */
5014 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5015 				       false);
5016 		if (ret) {
5017 			dev_info(&pf->pdev->dev,
5018 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5019 				 vsi->seid, pf_q);
5020 			return ret;
5021 		}
5022 wait_rx:
5023 		/* Check and wait for the Rx queue */
5024 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5025 		if (ret) {
5026 			dev_info(&pf->pdev->dev,
5027 				 "VSI seid %d Rx ring %d disable timeout\n",
5028 				 vsi->seid, pf_q);
5029 			return ret;
5030 		}
5031 	}
5032 
5033 	return 0;
5034 }
5035 
5036 #ifdef CONFIG_I40E_DCB
5037 /**
5038  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5039  * @pf: the PF
5040  *
5041  * This function waits for the queues to be in disabled state for all the
5042  * VSIs that are managed by this PF.
5043  **/
5044 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5045 {
5046 	int v, ret = 0;
5047 
5048 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5049 		if (pf->vsi[v]) {
5050 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5051 			if (ret)
5052 				break;
5053 		}
5054 	}
5055 
5056 	return ret;
5057 }
5058 
5059 #endif
5060 
5061 /**
5062  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5063  * @pf: pointer to PF
5064  *
5065  * Get TC map for ISCSI PF type that will include iSCSI TC
5066  * and LAN TC.
5067  **/
5068 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5069 {
5070 	struct i40e_dcb_app_priority_table app;
5071 	struct i40e_hw *hw = &pf->hw;
5072 	u8 enabled_tc = 1; /* TC0 is always enabled */
5073 	u8 tc, i;
5074 	/* Get the iSCSI APP TLV */
5075 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5076 
5077 	for (i = 0; i < dcbcfg->numapps; i++) {
5078 		app = dcbcfg->app[i];
5079 		if (app.selector == I40E_APP_SEL_TCPIP &&
5080 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5081 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5082 			enabled_tc |= BIT(tc);
5083 			break;
5084 		}
5085 	}
5086 
5087 	return enabled_tc;
5088 }
5089 
5090 /**
5091  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5092  * @dcbcfg: the corresponding DCBx configuration structure
5093  *
5094  * Return the number of TCs from given DCBx configuration
5095  **/
5096 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5097 {
5098 	int i, tc_unused = 0;
5099 	u8 num_tc = 0;
5100 	u8 ret = 0;
5101 
5102 	/* Scan the ETS Config Priority Table to find
5103 	 * traffic class enabled for a given priority
5104 	 * and create a bitmask of enabled TCs
5105 	 */
5106 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5107 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5108 
5109 	/* Now scan the bitmask to check for
5110 	 * contiguous TCs starting with TC0
5111 	 */
5112 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5113 		if (num_tc & BIT(i)) {
5114 			if (!tc_unused) {
5115 				ret++;
5116 			} else {
5117 				pr_err("Non-contiguous TC - Disabling DCB\n");
5118 				return 1;
5119 			}
5120 		} else {
5121 			tc_unused = 1;
5122 		}
5123 	}
5124 
5125 	/* There is always at least TC0 */
5126 	if (!ret)
5127 		ret = 1;
5128 
5129 	return ret;
5130 }
5131 
5132 /**
5133  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5134  * @dcbcfg: the corresponding DCBx configuration structure
5135  *
5136  * Query the current DCB configuration and return the number of
5137  * traffic classes enabled from the given DCBX config
5138  **/
5139 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5140 {
5141 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5142 	u8 enabled_tc = 1;
5143 	u8 i;
5144 
5145 	for (i = 0; i < num_tc; i++)
5146 		enabled_tc |= BIT(i);
5147 
5148 	return enabled_tc;
5149 }
5150 
5151 /**
5152  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5153  * @pf: PF being queried
5154  *
5155  * Query the current MQPRIO configuration and return the number of
5156  * traffic classes enabled.
5157  **/
5158 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5159 {
5160 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5161 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5162 	u8 enabled_tc = 1, i;
5163 
5164 	for (i = 1; i < num_tc; i++)
5165 		enabled_tc |= BIT(i);
5166 	return enabled_tc;
5167 }
5168 
5169 /**
5170  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5171  * @pf: PF being queried
5172  *
5173  * Return number of traffic classes enabled for the given PF
5174  **/
5175 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5176 {
5177 	struct i40e_hw *hw = &pf->hw;
5178 	u8 i, enabled_tc = 1;
5179 	u8 num_tc = 0;
5180 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5181 
5182 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5183 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5184 
5185 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5186 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5187 		return 1;
5188 
5189 	/* SFP mode will be enabled for all TCs on port */
5190 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5191 		return i40e_dcb_get_num_tc(dcbcfg);
5192 
5193 	/* MFP mode return count of enabled TCs for this PF */
5194 	if (pf->hw.func_caps.iscsi)
5195 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5196 	else
5197 		return 1; /* Only TC0 */
5198 
5199 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5200 		if (enabled_tc & BIT(i))
5201 			num_tc++;
5202 	}
5203 	return num_tc;
5204 }
5205 
5206 /**
5207  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5208  * @pf: PF being queried
5209  *
5210  * Return a bitmap for enabled traffic classes for this PF.
5211  **/
5212 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5213 {
5214 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5215 		return i40e_mqprio_get_enabled_tc(pf);
5216 
5217 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5218 	 * default TC
5219 	 */
5220 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5221 		return I40E_DEFAULT_TRAFFIC_CLASS;
5222 
5223 	/* SFP mode we want PF to be enabled for all TCs */
5224 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5225 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5226 
5227 	/* MFP enabled and iSCSI PF type */
5228 	if (pf->hw.func_caps.iscsi)
5229 		return i40e_get_iscsi_tc_map(pf);
5230 	else
5231 		return I40E_DEFAULT_TRAFFIC_CLASS;
5232 }
5233 
5234 /**
5235  * i40e_vsi_get_bw_info - Query VSI BW Information
5236  * @vsi: the VSI being queried
5237  *
5238  * Returns 0 on success, negative value on failure
5239  **/
5240 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5241 {
5242 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5243 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5244 	struct i40e_pf *pf = vsi->back;
5245 	struct i40e_hw *hw = &pf->hw;
5246 	i40e_status ret;
5247 	u32 tc_bw_max;
5248 	int i;
5249 
5250 	/* Get the VSI level BW configuration */
5251 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5252 	if (ret) {
5253 		dev_info(&pf->pdev->dev,
5254 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5255 			 i40e_stat_str(&pf->hw, ret),
5256 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5257 		return -EINVAL;
5258 	}
5259 
5260 	/* Get the VSI level BW configuration per TC */
5261 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5262 					       NULL);
5263 	if (ret) {
5264 		dev_info(&pf->pdev->dev,
5265 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5266 			 i40e_stat_str(&pf->hw, ret),
5267 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5268 		return -EINVAL;
5269 	}
5270 
5271 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5272 		dev_info(&pf->pdev->dev,
5273 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5274 			 bw_config.tc_valid_bits,
5275 			 bw_ets_config.tc_valid_bits);
5276 		/* Still continuing */
5277 	}
5278 
5279 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5280 	vsi->bw_max_quanta = bw_config.max_bw;
5281 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5282 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5283 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5284 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5285 		vsi->bw_ets_limit_credits[i] =
5286 					le16_to_cpu(bw_ets_config.credits[i]);
5287 		/* 3 bits out of 4 for each TC */
5288 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5289 	}
5290 
5291 	return 0;
5292 }
5293 
5294 /**
5295  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5296  * @vsi: the VSI being configured
5297  * @enabled_tc: TC bitmap
5298  * @bw_share: BW shared credits per TC
5299  *
5300  * Returns 0 on success, negative value on failure
5301  **/
5302 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5303 				       u8 *bw_share)
5304 {
5305 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5306 	struct i40e_pf *pf = vsi->back;
5307 	i40e_status ret;
5308 	int i;
5309 
5310 	/* There is no need to reset BW when mqprio mode is on.  */
5311 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5312 		return 0;
5313 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5314 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5315 		if (ret)
5316 			dev_info(&pf->pdev->dev,
5317 				 "Failed to reset tx rate for vsi->seid %u\n",
5318 				 vsi->seid);
5319 		return ret;
5320 	}
5321 	memset(&bw_data, 0, sizeof(bw_data));
5322 	bw_data.tc_valid_bits = enabled_tc;
5323 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5324 		bw_data.tc_bw_credits[i] = bw_share[i];
5325 
5326 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5327 	if (ret) {
5328 		dev_info(&pf->pdev->dev,
5329 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5330 			 pf->hw.aq.asq_last_status);
5331 		return -EINVAL;
5332 	}
5333 
5334 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5335 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5336 
5337 	return 0;
5338 }
5339 
5340 /**
5341  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5342  * @vsi: the VSI being configured
5343  * @enabled_tc: TC map to be enabled
5344  *
5345  **/
5346 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5347 {
5348 	struct net_device *netdev = vsi->netdev;
5349 	struct i40e_pf *pf = vsi->back;
5350 	struct i40e_hw *hw = &pf->hw;
5351 	u8 netdev_tc = 0;
5352 	int i;
5353 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5354 
5355 	if (!netdev)
5356 		return;
5357 
5358 	if (!enabled_tc) {
5359 		netdev_reset_tc(netdev);
5360 		return;
5361 	}
5362 
5363 	/* Set up actual enabled TCs on the VSI */
5364 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5365 		return;
5366 
5367 	/* set per TC queues for the VSI */
5368 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5369 		/* Only set TC queues for enabled tcs
5370 		 *
5371 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5372 		 * enabled_tc bitmap would be 0x00001001; the driver
5373 		 * will set the numtc for netdev as 2 that will be
5374 		 * referenced by the netdev layer as TC 0 and 1.
5375 		 */
5376 		if (vsi->tc_config.enabled_tc & BIT(i))
5377 			netdev_set_tc_queue(netdev,
5378 					vsi->tc_config.tc_info[i].netdev_tc,
5379 					vsi->tc_config.tc_info[i].qcount,
5380 					vsi->tc_config.tc_info[i].qoffset);
5381 	}
5382 
5383 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5384 		return;
5385 
5386 	/* Assign UP2TC map for the VSI */
5387 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5388 		/* Get the actual TC# for the UP */
5389 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5390 		/* Get the mapped netdev TC# for the UP */
5391 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5392 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5393 	}
5394 }
5395 
5396 /**
5397  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5398  * @vsi: the VSI being configured
5399  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5400  **/
5401 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5402 				      struct i40e_vsi_context *ctxt)
5403 {
5404 	/* copy just the sections touched not the entire info
5405 	 * since not all sections are valid as returned by
5406 	 * update vsi params
5407 	 */
5408 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5409 	memcpy(&vsi->info.queue_mapping,
5410 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5411 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5412 	       sizeof(vsi->info.tc_mapping));
5413 }
5414 
5415 /**
5416  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5417  * @vsi: VSI to be configured
5418  * @enabled_tc: TC bitmap
5419  *
5420  * This configures a particular VSI for TCs that are mapped to the
5421  * given TC bitmap. It uses default bandwidth share for TCs across
5422  * VSIs to configure TC for a particular VSI.
5423  *
5424  * NOTE:
5425  * It is expected that the VSI queues have been quisced before calling
5426  * this function.
5427  **/
5428 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5429 {
5430 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5431 	struct i40e_pf *pf = vsi->back;
5432 	struct i40e_hw *hw = &pf->hw;
5433 	struct i40e_vsi_context ctxt;
5434 	int ret = 0;
5435 	int i;
5436 
5437 	/* Check if enabled_tc is same as existing or new TCs */
5438 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5439 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5440 		return ret;
5441 
5442 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5443 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5444 		if (enabled_tc & BIT(i))
5445 			bw_share[i] = 1;
5446 	}
5447 
5448 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5449 	if (ret) {
5450 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5451 
5452 		dev_info(&pf->pdev->dev,
5453 			 "Failed configuring TC map %d for VSI %d\n",
5454 			 enabled_tc, vsi->seid);
5455 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5456 						  &bw_config, NULL);
5457 		if (ret) {
5458 			dev_info(&pf->pdev->dev,
5459 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5460 				 i40e_stat_str(hw, ret),
5461 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5462 			goto out;
5463 		}
5464 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5465 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5466 
5467 			if (!valid_tc)
5468 				valid_tc = bw_config.tc_valid_bits;
5469 			/* Always enable TC0, no matter what */
5470 			valid_tc |= 1;
5471 			dev_info(&pf->pdev->dev,
5472 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5473 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5474 			enabled_tc = valid_tc;
5475 		}
5476 
5477 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5478 		if (ret) {
5479 			dev_err(&pf->pdev->dev,
5480 				"Unable to  configure TC map %d for VSI %d\n",
5481 				enabled_tc, vsi->seid);
5482 			goto out;
5483 		}
5484 	}
5485 
5486 	/* Update Queue Pairs Mapping for currently enabled UPs */
5487 	ctxt.seid = vsi->seid;
5488 	ctxt.pf_num = vsi->back->hw.pf_id;
5489 	ctxt.vf_num = 0;
5490 	ctxt.uplink_seid = vsi->uplink_seid;
5491 	ctxt.info = vsi->info;
5492 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5493 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5494 		if (ret)
5495 			goto out;
5496 	} else {
5497 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5498 	}
5499 
5500 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5501 	 * queues changed.
5502 	 */
5503 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5504 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5505 				      vsi->num_queue_pairs);
5506 		ret = i40e_vsi_config_rss(vsi);
5507 		if (ret) {
5508 			dev_info(&vsi->back->pdev->dev,
5509 				 "Failed to reconfig rss for num_queues\n");
5510 			return ret;
5511 		}
5512 		vsi->reconfig_rss = false;
5513 	}
5514 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5515 		ctxt.info.valid_sections |=
5516 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5517 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5518 	}
5519 
5520 	/* Update the VSI after updating the VSI queue-mapping
5521 	 * information
5522 	 */
5523 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5524 	if (ret) {
5525 		dev_info(&pf->pdev->dev,
5526 			 "Update vsi tc config failed, err %s aq_err %s\n",
5527 			 i40e_stat_str(hw, ret),
5528 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5529 		goto out;
5530 	}
5531 	/* update the local VSI info with updated queue map */
5532 	i40e_vsi_update_queue_map(vsi, &ctxt);
5533 	vsi->info.valid_sections = 0;
5534 
5535 	/* Update current VSI BW information */
5536 	ret = i40e_vsi_get_bw_info(vsi);
5537 	if (ret) {
5538 		dev_info(&pf->pdev->dev,
5539 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5540 			 i40e_stat_str(hw, ret),
5541 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5542 		goto out;
5543 	}
5544 
5545 	/* Update the netdev TC setup */
5546 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5547 out:
5548 	return ret;
5549 }
5550 
5551 /**
5552  * i40e_get_link_speed - Returns link speed for the interface
5553  * @vsi: VSI to be configured
5554  *
5555  **/
5556 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5557 {
5558 	struct i40e_pf *pf = vsi->back;
5559 
5560 	switch (pf->hw.phy.link_info.link_speed) {
5561 	case I40E_LINK_SPEED_40GB:
5562 		return 40000;
5563 	case I40E_LINK_SPEED_25GB:
5564 		return 25000;
5565 	case I40E_LINK_SPEED_20GB:
5566 		return 20000;
5567 	case I40E_LINK_SPEED_10GB:
5568 		return 10000;
5569 	case I40E_LINK_SPEED_1GB:
5570 		return 1000;
5571 	default:
5572 		return -EINVAL;
5573 	}
5574 }
5575 
5576 /**
5577  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5578  * @vsi: VSI to be configured
5579  * @seid: seid of the channel/VSI
5580  * @max_tx_rate: max TX rate to be configured as BW limit
5581  *
5582  * Helper function to set BW limit for a given VSI
5583  **/
5584 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5585 {
5586 	struct i40e_pf *pf = vsi->back;
5587 	u64 credits = 0;
5588 	int speed = 0;
5589 	int ret = 0;
5590 
5591 	speed = i40e_get_link_speed(vsi);
5592 	if (max_tx_rate > speed) {
5593 		dev_err(&pf->pdev->dev,
5594 			"Invalid max tx rate %llu specified for VSI seid %d.",
5595 			max_tx_rate, seid);
5596 		return -EINVAL;
5597 	}
5598 	if (max_tx_rate && max_tx_rate < 50) {
5599 		dev_warn(&pf->pdev->dev,
5600 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5601 		max_tx_rate = 50;
5602 	}
5603 
5604 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5605 	credits = max_tx_rate;
5606 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5607 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5608 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5609 	if (ret)
5610 		dev_err(&pf->pdev->dev,
5611 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5612 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5613 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5614 	return ret;
5615 }
5616 
5617 /**
5618  * i40e_remove_queue_channels - Remove queue channels for the TCs
5619  * @vsi: VSI to be configured
5620  *
5621  * Remove queue channels for the TCs
5622  **/
5623 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5624 {
5625 	enum i40e_admin_queue_err last_aq_status;
5626 	struct i40e_cloud_filter *cfilter;
5627 	struct i40e_channel *ch, *ch_tmp;
5628 	struct i40e_pf *pf = vsi->back;
5629 	struct hlist_node *node;
5630 	int ret, i;
5631 
5632 	/* Reset rss size that was stored when reconfiguring rss for
5633 	 * channel VSIs with non-power-of-2 queue count.
5634 	 */
5635 	vsi->current_rss_size = 0;
5636 
5637 	/* perform cleanup for channels if they exist */
5638 	if (list_empty(&vsi->ch_list))
5639 		return;
5640 
5641 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5642 		struct i40e_vsi *p_vsi;
5643 
5644 		list_del(&ch->list);
5645 		p_vsi = ch->parent_vsi;
5646 		if (!p_vsi || !ch->initialized) {
5647 			kfree(ch);
5648 			continue;
5649 		}
5650 		/* Reset queue contexts */
5651 		for (i = 0; i < ch->num_queue_pairs; i++) {
5652 			struct i40e_ring *tx_ring, *rx_ring;
5653 			u16 pf_q;
5654 
5655 			pf_q = ch->base_queue + i;
5656 			tx_ring = vsi->tx_rings[pf_q];
5657 			tx_ring->ch = NULL;
5658 
5659 			rx_ring = vsi->rx_rings[pf_q];
5660 			rx_ring->ch = NULL;
5661 		}
5662 
5663 		/* Reset BW configured for this VSI via mqprio */
5664 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5665 		if (ret)
5666 			dev_info(&vsi->back->pdev->dev,
5667 				 "Failed to reset tx rate for ch->seid %u\n",
5668 				 ch->seid);
5669 
5670 		/* delete cloud filters associated with this channel */
5671 		hlist_for_each_entry_safe(cfilter, node,
5672 					  &pf->cloud_filter_list, cloud_node) {
5673 			if (cfilter->seid != ch->seid)
5674 				continue;
5675 
5676 			hash_del(&cfilter->cloud_node);
5677 			if (cfilter->dst_port)
5678 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
5679 									cfilter,
5680 									false);
5681 			else
5682 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
5683 								false);
5684 			last_aq_status = pf->hw.aq.asq_last_status;
5685 			if (ret)
5686 				dev_info(&pf->pdev->dev,
5687 					 "Failed to delete cloud filter, err %s aq_err %s\n",
5688 					 i40e_stat_str(&pf->hw, ret),
5689 					 i40e_aq_str(&pf->hw, last_aq_status));
5690 			kfree(cfilter);
5691 		}
5692 
5693 		/* delete VSI from FW */
5694 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5695 					     NULL);
5696 		if (ret)
5697 			dev_err(&vsi->back->pdev->dev,
5698 				"unable to remove channel (%d) for parent VSI(%d)\n",
5699 				ch->seid, p_vsi->seid);
5700 		kfree(ch);
5701 	}
5702 	INIT_LIST_HEAD(&vsi->ch_list);
5703 }
5704 
5705 /**
5706  * i40e_is_any_channel - channel exist or not
5707  * @vsi: ptr to VSI to which channels are associated with
5708  *
5709  * Returns true or false if channel(s) exist for associated VSI or not
5710  **/
5711 static bool i40e_is_any_channel(struct i40e_vsi *vsi)
5712 {
5713 	struct i40e_channel *ch, *ch_tmp;
5714 
5715 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5716 		if (ch->initialized)
5717 			return true;
5718 	}
5719 
5720 	return false;
5721 }
5722 
5723 /**
5724  * i40e_get_max_queues_for_channel
5725  * @vsi: ptr to VSI to which channels are associated with
5726  *
5727  * Helper function which returns max value among the queue counts set on the
5728  * channels/TCs created.
5729  **/
5730 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5731 {
5732 	struct i40e_channel *ch, *ch_tmp;
5733 	int max = 0;
5734 
5735 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5736 		if (!ch->initialized)
5737 			continue;
5738 		if (ch->num_queue_pairs > max)
5739 			max = ch->num_queue_pairs;
5740 	}
5741 
5742 	return max;
5743 }
5744 
5745 /**
5746  * i40e_validate_num_queues - validate num_queues w.r.t channel
5747  * @pf: ptr to PF device
5748  * @num_queues: number of queues
5749  * @vsi: the parent VSI
5750  * @reconfig_rss: indicates should the RSS be reconfigured or not
5751  *
5752  * This function validates number of queues in the context of new channel
5753  * which is being established and determines if RSS should be reconfigured
5754  * or not for parent VSI.
5755  **/
5756 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5757 				    struct i40e_vsi *vsi, bool *reconfig_rss)
5758 {
5759 	int max_ch_queues;
5760 
5761 	if (!reconfig_rss)
5762 		return -EINVAL;
5763 
5764 	*reconfig_rss = false;
5765 	if (vsi->current_rss_size) {
5766 		if (num_queues > vsi->current_rss_size) {
5767 			dev_dbg(&pf->pdev->dev,
5768 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
5769 				num_queues, vsi->current_rss_size);
5770 			return -EINVAL;
5771 		} else if ((num_queues < vsi->current_rss_size) &&
5772 			   (!is_power_of_2(num_queues))) {
5773 			dev_dbg(&pf->pdev->dev,
5774 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5775 				num_queues, vsi->current_rss_size);
5776 			return -EINVAL;
5777 		}
5778 	}
5779 
5780 	if (!is_power_of_2(num_queues)) {
5781 		/* Find the max num_queues configured for channel if channel
5782 		 * exist.
5783 		 * if channel exist, then enforce 'num_queues' to be more than
5784 		 * max ever queues configured for channel.
5785 		 */
5786 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5787 		if (num_queues < max_ch_queues) {
5788 			dev_dbg(&pf->pdev->dev,
5789 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
5790 				num_queues, max_ch_queues);
5791 			return -EINVAL;
5792 		}
5793 		*reconfig_rss = true;
5794 	}
5795 
5796 	return 0;
5797 }
5798 
5799 /**
5800  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5801  * @vsi: the VSI being setup
5802  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5803  *
5804  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5805  **/
5806 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5807 {
5808 	struct i40e_pf *pf = vsi->back;
5809 	u8 seed[I40E_HKEY_ARRAY_SIZE];
5810 	struct i40e_hw *hw = &pf->hw;
5811 	int local_rss_size;
5812 	u8 *lut;
5813 	int ret;
5814 
5815 	if (!vsi->rss_size)
5816 		return -EINVAL;
5817 
5818 	if (rss_size > vsi->rss_size)
5819 		return -EINVAL;
5820 
5821 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
5822 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5823 	if (!lut)
5824 		return -ENOMEM;
5825 
5826 	/* Ignoring user configured lut if there is one */
5827 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5828 
5829 	/* Use user configured hash key if there is one, otherwise
5830 	 * use default.
5831 	 */
5832 	if (vsi->rss_hkey_user)
5833 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5834 	else
5835 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5836 
5837 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5838 	if (ret) {
5839 		dev_info(&pf->pdev->dev,
5840 			 "Cannot set RSS lut, err %s aq_err %s\n",
5841 			 i40e_stat_str(hw, ret),
5842 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5843 		kfree(lut);
5844 		return ret;
5845 	}
5846 	kfree(lut);
5847 
5848 	/* Do the update w.r.t. storing rss_size */
5849 	if (!vsi->orig_rss_size)
5850 		vsi->orig_rss_size = vsi->rss_size;
5851 	vsi->current_rss_size = local_rss_size;
5852 
5853 	return ret;
5854 }
5855 
5856 /**
5857  * i40e_channel_setup_queue_map - Setup a channel queue map
5858  * @pf: ptr to PF device
5859  * @ctxt: VSI context structure
5860  * @ch: ptr to channel structure
5861  *
5862  * Setup queue map for a specific channel
5863  **/
5864 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5865 					 struct i40e_vsi_context *ctxt,
5866 					 struct i40e_channel *ch)
5867 {
5868 	u16 qcount, qmap, sections = 0;
5869 	u8 offset = 0;
5870 	int pow;
5871 
5872 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5873 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5874 
5875 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5876 	ch->num_queue_pairs = qcount;
5877 
5878 	/* find the next higher power-of-2 of num queue pairs */
5879 	pow = ilog2(qcount);
5880 	if (!is_power_of_2(qcount))
5881 		pow++;
5882 
5883 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5884 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
5885 
5886 	/* Setup queue TC[0].qmap for given VSI context */
5887 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
5888 
5889 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
5890 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
5891 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
5892 	ctxt->info.valid_sections |= cpu_to_le16(sections);
5893 }
5894 
5895 /**
5896  * i40e_add_channel - add a channel by adding VSI
5897  * @pf: ptr to PF device
5898  * @uplink_seid: underlying HW switching element (VEB) ID
5899  * @ch: ptr to channel structure
5900  *
5901  * Add a channel (VSI) using add_vsi and queue_map
5902  **/
5903 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
5904 			    struct i40e_channel *ch)
5905 {
5906 	struct i40e_hw *hw = &pf->hw;
5907 	struct i40e_vsi_context ctxt;
5908 	u8 enabled_tc = 0x1; /* TC0 enabled */
5909 	int ret;
5910 
5911 	if (ch->type != I40E_VSI_VMDQ2) {
5912 		dev_info(&pf->pdev->dev,
5913 			 "add new vsi failed, ch->type %d\n", ch->type);
5914 		return -EINVAL;
5915 	}
5916 
5917 	memset(&ctxt, 0, sizeof(ctxt));
5918 	ctxt.pf_num = hw->pf_id;
5919 	ctxt.vf_num = 0;
5920 	ctxt.uplink_seid = uplink_seid;
5921 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5922 	if (ch->type == I40E_VSI_VMDQ2)
5923 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
5924 
5925 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
5926 		ctxt.info.valid_sections |=
5927 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5928 		ctxt.info.switch_id =
5929 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5930 	}
5931 
5932 	/* Set queue map for a given VSI context */
5933 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
5934 
5935 	/* Now time to create VSI */
5936 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
5937 	if (ret) {
5938 		dev_info(&pf->pdev->dev,
5939 			 "add new vsi failed, err %s aq_err %s\n",
5940 			 i40e_stat_str(&pf->hw, ret),
5941 			 i40e_aq_str(&pf->hw,
5942 				     pf->hw.aq.asq_last_status));
5943 		return -ENOENT;
5944 	}
5945 
5946 	/* Success, update channel, set enabled_tc only if the channel
5947 	 * is not a macvlan
5948 	 */
5949 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
5950 	ch->seid = ctxt.seid;
5951 	ch->vsi_number = ctxt.vsi_number;
5952 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
5953 
5954 	/* copy just the sections touched not the entire info
5955 	 * since not all sections are valid as returned by
5956 	 * update vsi params
5957 	 */
5958 	ch->info.mapping_flags = ctxt.info.mapping_flags;
5959 	memcpy(&ch->info.queue_mapping,
5960 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
5961 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
5962 	       sizeof(ctxt.info.tc_mapping));
5963 
5964 	return 0;
5965 }
5966 
5967 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
5968 				  u8 *bw_share)
5969 {
5970 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5971 	i40e_status ret;
5972 	int i;
5973 
5974 	memset(&bw_data, 0, sizeof(bw_data));
5975 	bw_data.tc_valid_bits = ch->enabled_tc;
5976 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5977 		bw_data.tc_bw_credits[i] = bw_share[i];
5978 
5979 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
5980 				       &bw_data, NULL);
5981 	if (ret) {
5982 		dev_info(&vsi->back->pdev->dev,
5983 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
5984 			 vsi->back->hw.aq.asq_last_status, ch->seid);
5985 		return -EINVAL;
5986 	}
5987 
5988 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5989 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
5990 
5991 	return 0;
5992 }
5993 
5994 /**
5995  * i40e_channel_config_tx_ring - config TX ring associated with new channel
5996  * @pf: ptr to PF device
5997  * @vsi: the VSI being setup
5998  * @ch: ptr to channel structure
5999  *
6000  * Configure TX rings associated with channel (VSI) since queues are being
6001  * from parent VSI.
6002  **/
6003 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6004 				       struct i40e_vsi *vsi,
6005 				       struct i40e_channel *ch)
6006 {
6007 	i40e_status ret;
6008 	int i;
6009 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6010 
6011 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6012 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6013 		if (ch->enabled_tc & BIT(i))
6014 			bw_share[i] = 1;
6015 	}
6016 
6017 	/* configure BW for new VSI */
6018 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6019 	if (ret) {
6020 		dev_info(&vsi->back->pdev->dev,
6021 			 "Failed configuring TC map %d for channel (seid %u)\n",
6022 			 ch->enabled_tc, ch->seid);
6023 		return ret;
6024 	}
6025 
6026 	for (i = 0; i < ch->num_queue_pairs; i++) {
6027 		struct i40e_ring *tx_ring, *rx_ring;
6028 		u16 pf_q;
6029 
6030 		pf_q = ch->base_queue + i;
6031 
6032 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6033 		 * context
6034 		 */
6035 		tx_ring = vsi->tx_rings[pf_q];
6036 		tx_ring->ch = ch;
6037 
6038 		/* Get the RX ring ptr */
6039 		rx_ring = vsi->rx_rings[pf_q];
6040 		rx_ring->ch = ch;
6041 	}
6042 
6043 	return 0;
6044 }
6045 
6046 /**
6047  * i40e_setup_hw_channel - setup new channel
6048  * @pf: ptr to PF device
6049  * @vsi: the VSI being setup
6050  * @ch: ptr to channel structure
6051  * @uplink_seid: underlying HW switching element (VEB) ID
6052  * @type: type of channel to be created (VMDq2/VF)
6053  *
6054  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6055  * and configures TX rings accordingly
6056  **/
6057 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6058 					struct i40e_vsi *vsi,
6059 					struct i40e_channel *ch,
6060 					u16 uplink_seid, u8 type)
6061 {
6062 	int ret;
6063 
6064 	ch->initialized = false;
6065 	ch->base_queue = vsi->next_base_queue;
6066 	ch->type = type;
6067 
6068 	/* Proceed with creation of channel (VMDq2) VSI */
6069 	ret = i40e_add_channel(pf, uplink_seid, ch);
6070 	if (ret) {
6071 		dev_info(&pf->pdev->dev,
6072 			 "failed to add_channel using uplink_seid %u\n",
6073 			 uplink_seid);
6074 		return ret;
6075 	}
6076 
6077 	/* Mark the successful creation of channel */
6078 	ch->initialized = true;
6079 
6080 	/* Reconfigure TX queues using QTX_CTL register */
6081 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6082 	if (ret) {
6083 		dev_info(&pf->pdev->dev,
6084 			 "failed to configure TX rings for channel %u\n",
6085 			 ch->seid);
6086 		return ret;
6087 	}
6088 
6089 	/* update 'next_base_queue' */
6090 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6091 	dev_dbg(&pf->pdev->dev,
6092 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6093 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6094 		ch->num_queue_pairs,
6095 		vsi->next_base_queue);
6096 	return ret;
6097 }
6098 
6099 /**
6100  * i40e_setup_channel - setup new channel using uplink element
6101  * @pf: ptr to PF device
6102  * @vsi: pointer to the VSI to set up the channel within
6103  * @ch: ptr to channel structure
6104  *
6105  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6106  * and uplink switching element (uplink_seid)
6107  **/
6108 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6109 			       struct i40e_channel *ch)
6110 {
6111 	u8 vsi_type;
6112 	u16 seid;
6113 	int ret;
6114 
6115 	if (vsi->type == I40E_VSI_MAIN) {
6116 		vsi_type = I40E_VSI_VMDQ2;
6117 	} else {
6118 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6119 			vsi->type);
6120 		return false;
6121 	}
6122 
6123 	/* underlying switching element */
6124 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6125 
6126 	/* create channel (VSI), configure TX rings */
6127 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6128 	if (ret) {
6129 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6130 		return false;
6131 	}
6132 
6133 	return ch->initialized ? true : false;
6134 }
6135 
6136 /**
6137  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6138  * @vsi: ptr to VSI which has PF backing
6139  *
6140  * Sets up switch mode correctly if it needs to be changed and perform
6141  * what are allowed modes.
6142  **/
6143 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6144 {
6145 	u8 mode;
6146 	struct i40e_pf *pf = vsi->back;
6147 	struct i40e_hw *hw = &pf->hw;
6148 	int ret;
6149 
6150 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6151 	if (ret)
6152 		return -EINVAL;
6153 
6154 	if (hw->dev_caps.switch_mode) {
6155 		/* if switch mode is set, support mode2 (non-tunneled for
6156 		 * cloud filter) for now
6157 		 */
6158 		u32 switch_mode = hw->dev_caps.switch_mode &
6159 				  I40E_SWITCH_MODE_MASK;
6160 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6161 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6162 				return 0;
6163 			dev_err(&pf->pdev->dev,
6164 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6165 				hw->dev_caps.switch_mode);
6166 			return -EINVAL;
6167 		}
6168 	}
6169 
6170 	/* Set Bit 7 to be valid */
6171 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6172 
6173 	/* Set L4type for TCP support */
6174 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6175 
6176 	/* Set cloud filter mode */
6177 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6178 
6179 	/* Prep mode field for set_switch_config */
6180 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6181 					pf->last_sw_conf_valid_flags,
6182 					mode, NULL);
6183 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6184 		dev_err(&pf->pdev->dev,
6185 			"couldn't set switch config bits, err %s aq_err %s\n",
6186 			i40e_stat_str(hw, ret),
6187 			i40e_aq_str(hw,
6188 				    hw->aq.asq_last_status));
6189 
6190 	return ret;
6191 }
6192 
6193 /**
6194  * i40e_create_queue_channel - function to create channel
6195  * @vsi: VSI to be configured
6196  * @ch: ptr to channel (it contains channel specific params)
6197  *
6198  * This function creates channel (VSI) using num_queues specified by user,
6199  * reconfigs RSS if needed.
6200  **/
6201 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6202 			      struct i40e_channel *ch)
6203 {
6204 	struct i40e_pf *pf = vsi->back;
6205 	bool reconfig_rss;
6206 	int err;
6207 
6208 	if (!ch)
6209 		return -EINVAL;
6210 
6211 	if (!ch->num_queue_pairs) {
6212 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6213 			ch->num_queue_pairs);
6214 		return -EINVAL;
6215 	}
6216 
6217 	/* validate user requested num_queues for channel */
6218 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6219 				       &reconfig_rss);
6220 	if (err) {
6221 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6222 			 ch->num_queue_pairs);
6223 		return -EINVAL;
6224 	}
6225 
6226 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6227 	 * VSI to be added switch to VEB mode.
6228 	 */
6229 	if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
6230 	    (!i40e_is_any_channel(vsi))) {
6231 		if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
6232 			dev_dbg(&pf->pdev->dev,
6233 				"Failed to create channel. Override queues (%u) not power of 2\n",
6234 				vsi->tc_config.tc_info[0].qcount);
6235 			return -EINVAL;
6236 		}
6237 
6238 		if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6239 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6240 
6241 			if (vsi->type == I40E_VSI_MAIN) {
6242 				if (pf->flags & I40E_FLAG_TC_MQPRIO)
6243 					i40e_do_reset(pf, I40E_PF_RESET_FLAG,
6244 						      true);
6245 				else
6246 					i40e_do_reset_safe(pf,
6247 							   I40E_PF_RESET_FLAG);
6248 			}
6249 		}
6250 		/* now onwards for main VSI, number of queues will be value
6251 		 * of TC0's queue count
6252 		 */
6253 	}
6254 
6255 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6256 	 * it should be more than num_queues
6257 	 */
6258 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6259 		dev_dbg(&pf->pdev->dev,
6260 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6261 			vsi->cnt_q_avail, ch->num_queue_pairs);
6262 		return -EINVAL;
6263 	}
6264 
6265 	/* reconfig_rss only if vsi type is MAIN_VSI */
6266 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6267 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6268 		if (err) {
6269 			dev_info(&pf->pdev->dev,
6270 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6271 				 ch->num_queue_pairs);
6272 			return -EINVAL;
6273 		}
6274 	}
6275 
6276 	if (!i40e_setup_channel(pf, vsi, ch)) {
6277 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6278 		return -EINVAL;
6279 	}
6280 
6281 	dev_info(&pf->pdev->dev,
6282 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6283 		 ch->seid, ch->num_queue_pairs);
6284 
6285 	/* configure VSI for BW limit */
6286 	if (ch->max_tx_rate) {
6287 		u64 credits = ch->max_tx_rate;
6288 
6289 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6290 			return -EINVAL;
6291 
6292 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6293 		dev_dbg(&pf->pdev->dev,
6294 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6295 			ch->max_tx_rate,
6296 			credits,
6297 			ch->seid);
6298 	}
6299 
6300 	/* in case of VF, this will be main SRIOV VSI */
6301 	ch->parent_vsi = vsi;
6302 
6303 	/* and update main_vsi's count for queue_available to use */
6304 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6305 
6306 	return 0;
6307 }
6308 
6309 /**
6310  * i40e_configure_queue_channels - Add queue channel for the given TCs
6311  * @vsi: VSI to be configured
6312  *
6313  * Configures queue channel mapping to the given TCs
6314  **/
6315 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6316 {
6317 	struct i40e_channel *ch;
6318 	u64 max_rate = 0;
6319 	int ret = 0, i;
6320 
6321 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6322 	vsi->tc_seid_map[0] = vsi->seid;
6323 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6324 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6325 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6326 			if (!ch) {
6327 				ret = -ENOMEM;
6328 				goto err_free;
6329 			}
6330 
6331 			INIT_LIST_HEAD(&ch->list);
6332 			ch->num_queue_pairs =
6333 				vsi->tc_config.tc_info[i].qcount;
6334 			ch->base_queue =
6335 				vsi->tc_config.tc_info[i].qoffset;
6336 
6337 			/* Bandwidth limit through tc interface is in bytes/s,
6338 			 * change to Mbit/s
6339 			 */
6340 			max_rate = vsi->mqprio_qopt.max_rate[i];
6341 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6342 			ch->max_tx_rate = max_rate;
6343 
6344 			list_add_tail(&ch->list, &vsi->ch_list);
6345 
6346 			ret = i40e_create_queue_channel(vsi, ch);
6347 			if (ret) {
6348 				dev_err(&vsi->back->pdev->dev,
6349 					"Failed creating queue channel with TC%d: queues %d\n",
6350 					i, ch->num_queue_pairs);
6351 				goto err_free;
6352 			}
6353 			vsi->tc_seid_map[i] = ch->seid;
6354 		}
6355 	}
6356 	return ret;
6357 
6358 err_free:
6359 	i40e_remove_queue_channels(vsi);
6360 	return ret;
6361 }
6362 
6363 /**
6364  * i40e_veb_config_tc - Configure TCs for given VEB
6365  * @veb: given VEB
6366  * @enabled_tc: TC bitmap
6367  *
6368  * Configures given TC bitmap for VEB (switching) element
6369  **/
6370 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6371 {
6372 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6373 	struct i40e_pf *pf = veb->pf;
6374 	int ret = 0;
6375 	int i;
6376 
6377 	/* No TCs or already enabled TCs just return */
6378 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6379 		return ret;
6380 
6381 	bw_data.tc_valid_bits = enabled_tc;
6382 	/* bw_data.absolute_credits is not set (relative) */
6383 
6384 	/* Enable ETS TCs with equal BW Share for now */
6385 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6386 		if (enabled_tc & BIT(i))
6387 			bw_data.tc_bw_share_credits[i] = 1;
6388 	}
6389 
6390 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6391 						   &bw_data, NULL);
6392 	if (ret) {
6393 		dev_info(&pf->pdev->dev,
6394 			 "VEB bw config failed, err %s aq_err %s\n",
6395 			 i40e_stat_str(&pf->hw, ret),
6396 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6397 		goto out;
6398 	}
6399 
6400 	/* Update the BW information */
6401 	ret = i40e_veb_get_bw_info(veb);
6402 	if (ret) {
6403 		dev_info(&pf->pdev->dev,
6404 			 "Failed getting veb bw config, err %s aq_err %s\n",
6405 			 i40e_stat_str(&pf->hw, ret),
6406 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6407 	}
6408 
6409 out:
6410 	return ret;
6411 }
6412 
6413 #ifdef CONFIG_I40E_DCB
6414 /**
6415  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6416  * @pf: PF struct
6417  *
6418  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6419  * the caller would've quiesce all the VSIs before calling
6420  * this function
6421  **/
6422 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6423 {
6424 	u8 tc_map = 0;
6425 	int ret;
6426 	u8 v;
6427 
6428 	/* Enable the TCs available on PF to all VEBs */
6429 	tc_map = i40e_pf_get_tc_map(pf);
6430 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6431 		return;
6432 
6433 	for (v = 0; v < I40E_MAX_VEB; v++) {
6434 		if (!pf->veb[v])
6435 			continue;
6436 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6437 		if (ret) {
6438 			dev_info(&pf->pdev->dev,
6439 				 "Failed configuring TC for VEB seid=%d\n",
6440 				 pf->veb[v]->seid);
6441 			/* Will try to configure as many components */
6442 		}
6443 	}
6444 
6445 	/* Update each VSI */
6446 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6447 		if (!pf->vsi[v])
6448 			continue;
6449 
6450 		/* - Enable all TCs for the LAN VSI
6451 		 * - For all others keep them at TC0 for now
6452 		 */
6453 		if (v == pf->lan_vsi)
6454 			tc_map = i40e_pf_get_tc_map(pf);
6455 		else
6456 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6457 
6458 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6459 		if (ret) {
6460 			dev_info(&pf->pdev->dev,
6461 				 "Failed configuring TC for VSI seid=%d\n",
6462 				 pf->vsi[v]->seid);
6463 			/* Will try to configure as many components */
6464 		} else {
6465 			/* Re-configure VSI vectors based on updated TC map */
6466 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6467 			if (pf->vsi[v]->netdev)
6468 				i40e_dcbnl_set_all(pf->vsi[v]);
6469 		}
6470 	}
6471 }
6472 
6473 /**
6474  * i40e_resume_port_tx - Resume port Tx
6475  * @pf: PF struct
6476  *
6477  * Resume a port's Tx and issue a PF reset in case of failure to
6478  * resume.
6479  **/
6480 static int i40e_resume_port_tx(struct i40e_pf *pf)
6481 {
6482 	struct i40e_hw *hw = &pf->hw;
6483 	int ret;
6484 
6485 	ret = i40e_aq_resume_port_tx(hw, NULL);
6486 	if (ret) {
6487 		dev_info(&pf->pdev->dev,
6488 			 "Resume Port Tx failed, err %s aq_err %s\n",
6489 			  i40e_stat_str(&pf->hw, ret),
6490 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6491 		/* Schedule PF reset to recover */
6492 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6493 		i40e_service_event_schedule(pf);
6494 	}
6495 
6496 	return ret;
6497 }
6498 
6499 /**
6500  * i40e_suspend_port_tx - Suspend port Tx
6501  * @pf: PF struct
6502  *
6503  * Suspend a port's Tx and issue a PF reset in case of failure.
6504  **/
6505 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6506 {
6507 	struct i40e_hw *hw = &pf->hw;
6508 	int ret;
6509 
6510 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6511 	if (ret) {
6512 		dev_info(&pf->pdev->dev,
6513 			 "Suspend Port Tx failed, err %s aq_err %s\n",
6514 			 i40e_stat_str(&pf->hw, ret),
6515 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6516 		/* Schedule PF reset to recover */
6517 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6518 		i40e_service_event_schedule(pf);
6519 	}
6520 
6521 	return ret;
6522 }
6523 
6524 /**
6525  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6526  * @pf: PF being configured
6527  * @new_cfg: New DCBX configuration
6528  *
6529  * Program DCB settings into HW and reconfigure VEB/VSIs on
6530  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6531  **/
6532 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6533 				  struct i40e_dcbx_config *new_cfg)
6534 {
6535 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6536 	int ret;
6537 
6538 	/* Check if need reconfiguration */
6539 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6540 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6541 		return 0;
6542 	}
6543 
6544 	/* Config change disable all VSIs */
6545 	i40e_pf_quiesce_all_vsi(pf);
6546 
6547 	/* Copy the new config to the current config */
6548 	*old_cfg = *new_cfg;
6549 	old_cfg->etsrec = old_cfg->etscfg;
6550 	ret = i40e_set_dcb_config(&pf->hw);
6551 	if (ret) {
6552 		dev_info(&pf->pdev->dev,
6553 			 "Set DCB Config failed, err %s aq_err %s\n",
6554 			 i40e_stat_str(&pf->hw, ret),
6555 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6556 		goto out;
6557 	}
6558 
6559 	/* Changes in configuration update VEB/VSI */
6560 	i40e_dcb_reconfigure(pf);
6561 out:
6562 	/* In case of reset do not try to resume anything */
6563 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6564 		/* Re-start the VSIs if disabled */
6565 		ret = i40e_resume_port_tx(pf);
6566 		/* In case of error no point in resuming VSIs */
6567 		if (ret)
6568 			goto err;
6569 		i40e_pf_unquiesce_all_vsi(pf);
6570 	}
6571 err:
6572 	return ret;
6573 }
6574 
6575 /**
6576  * i40e_hw_dcb_config - Program new DCBX settings into HW
6577  * @pf: PF being configured
6578  * @new_cfg: New DCBX configuration
6579  *
6580  * Program DCB settings into HW and reconfigure VEB/VSIs on
6581  * given PF
6582  **/
6583 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6584 {
6585 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6586 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6587 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6588 	struct i40e_dcbx_config *old_cfg;
6589 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6590 	struct i40e_rx_pb_config pb_cfg;
6591 	struct i40e_hw *hw = &pf->hw;
6592 	u8 num_ports = hw->num_ports;
6593 	bool need_reconfig;
6594 	int ret = -EINVAL;
6595 	u8 lltc_map = 0;
6596 	u8 tc_map = 0;
6597 	u8 new_numtc;
6598 	u8 i;
6599 
6600 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6601 	/* Un-pack information to Program ETS HW via shared API
6602 	 * numtc, tcmap
6603 	 * LLTC map
6604 	 * ETS/NON-ETS arbiter mode
6605 	 * max exponent (credit refills)
6606 	 * Total number of ports
6607 	 * PFC priority bit-map
6608 	 * Priority Table
6609 	 * BW % per TC
6610 	 * Arbiter mode between UPs sharing same TC
6611 	 * TSA table (ETS or non-ETS)
6612 	 * EEE enabled or not
6613 	 * MFS TC table
6614 	 */
6615 
6616 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6617 
6618 	memset(&ets_data, 0, sizeof(ets_data));
6619 	for (i = 0; i < new_numtc; i++) {
6620 		tc_map |= BIT(i);
6621 		switch (new_cfg->etscfg.tsatable[i]) {
6622 		case I40E_IEEE_TSA_ETS:
6623 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6624 			ets_data.tc_bw_share_credits[i] =
6625 					new_cfg->etscfg.tcbwtable[i];
6626 			break;
6627 		case I40E_IEEE_TSA_STRICT:
6628 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6629 			lltc_map |= BIT(i);
6630 			ets_data.tc_bw_share_credits[i] =
6631 					I40E_DCB_STRICT_PRIO_CREDITS;
6632 			break;
6633 		default:
6634 			/* Invalid TSA type */
6635 			need_reconfig = false;
6636 			goto out;
6637 		}
6638 	}
6639 
6640 	old_cfg = &hw->local_dcbx_config;
6641 	/* Check if need reconfiguration */
6642 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6643 
6644 	/* If needed, enable/disable frame tagging, disable all VSIs
6645 	 * and suspend port tx
6646 	 */
6647 	if (need_reconfig) {
6648 		/* Enable DCB tagging only when more than one TC */
6649 		if (new_numtc > 1)
6650 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6651 		else
6652 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6653 
6654 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6655 		/* Reconfiguration needed quiesce all VSIs */
6656 		i40e_pf_quiesce_all_vsi(pf);
6657 		ret = i40e_suspend_port_tx(pf);
6658 		if (ret)
6659 			goto err;
6660 	}
6661 
6662 	/* Configure Port ETS Tx Scheduler */
6663 	ets_data.tc_valid_bits = tc_map;
6664 	ets_data.tc_strict_priority_flags = lltc_map;
6665 	ret = i40e_aq_config_switch_comp_ets
6666 		(hw, pf->mac_seid, &ets_data,
6667 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
6668 	if (ret) {
6669 		dev_info(&pf->pdev->dev,
6670 			 "Modify Port ETS failed, err %s aq_err %s\n",
6671 			 i40e_stat_str(&pf->hw, ret),
6672 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6673 		goto out;
6674 	}
6675 
6676 	/* Configure Rx ETS HW */
6677 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
6678 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
6679 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
6680 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
6681 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
6682 				   lltc_map);
6683 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
6684 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
6685 				     prio_type);
6686 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
6687 			       new_cfg->etscfg.prioritytable);
6688 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
6689 
6690 	/* Configure Rx Packet Buffers in HW */
6691 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6692 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
6693 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
6694 	}
6695 
6696 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
6697 					 false, new_cfg->pfc.pfcenable,
6698 					 mfs_tc, &pb_cfg);
6699 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
6700 
6701 	/* Update the local Rx Packet buffer config */
6702 	pf->pb_cfg = pb_cfg;
6703 
6704 	/* Inform the FW about changes to DCB configuration */
6705 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
6706 	if (ret) {
6707 		dev_info(&pf->pdev->dev,
6708 			 "DCB Updated failed, err %s aq_err %s\n",
6709 			 i40e_stat_str(&pf->hw, ret),
6710 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6711 		goto out;
6712 	}
6713 
6714 	/* Update the port DCBx configuration */
6715 	*old_cfg = *new_cfg;
6716 
6717 	/* Changes in configuration update VEB/VSI */
6718 	i40e_dcb_reconfigure(pf);
6719 out:
6720 	/* Re-start the VSIs if disabled */
6721 	if (need_reconfig) {
6722 		ret = i40e_resume_port_tx(pf);
6723 
6724 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
6725 		/* In case of error no point in resuming VSIs */
6726 		if (ret)
6727 			goto err;
6728 
6729 		/* Wait for the PF's queues to be disabled */
6730 		ret = i40e_pf_wait_queues_disabled(pf);
6731 		if (ret) {
6732 			/* Schedule PF reset to recover */
6733 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6734 			i40e_service_event_schedule(pf);
6735 			goto err;
6736 		} else {
6737 			i40e_pf_unquiesce_all_vsi(pf);
6738 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
6739 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
6740 		}
6741 		/* registers are set, lets apply */
6742 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
6743 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
6744 	}
6745 
6746 err:
6747 	return ret;
6748 }
6749 
6750 /**
6751  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
6752  * @pf: PF being queried
6753  *
6754  * Set default DCB configuration in case DCB is to be done in SW.
6755  **/
6756 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
6757 {
6758 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
6759 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6760 	struct i40e_hw *hw = &pf->hw;
6761 	int err;
6762 
6763 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
6764 		/* Update the local cached instance with TC0 ETS */
6765 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
6766 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
6767 		pf->tmp_cfg.etscfg.maxtcs = 0;
6768 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
6769 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
6770 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
6771 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
6772 		/* FW needs one App to configure HW */
6773 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
6774 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
6775 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
6776 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
6777 
6778 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
6779 	}
6780 
6781 	memset(&ets_data, 0, sizeof(ets_data));
6782 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
6783 	ets_data.tc_strict_priority_flags = 0; /* ETS */
6784 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
6785 
6786 	/* Enable ETS on the Physical port */
6787 	err = i40e_aq_config_switch_comp_ets
6788 		(hw, pf->mac_seid, &ets_data,
6789 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
6790 	if (err) {
6791 		dev_info(&pf->pdev->dev,
6792 			 "Enable Port ETS failed, err %s aq_err %s\n",
6793 			 i40e_stat_str(&pf->hw, err),
6794 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6795 		err = -ENOENT;
6796 		goto out;
6797 	}
6798 
6799 	/* Update the local cached instance with TC0 ETS */
6800 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
6801 	dcb_cfg->etscfg.cbs = 0;
6802 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
6803 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
6804 
6805 out:
6806 	return err;
6807 }
6808 
6809 /**
6810  * i40e_init_pf_dcb - Initialize DCB configuration
6811  * @pf: PF being configured
6812  *
6813  * Query the current DCB configuration and cache it
6814  * in the hardware structure
6815  **/
6816 static int i40e_init_pf_dcb(struct i40e_pf *pf)
6817 {
6818 	struct i40e_hw *hw = &pf->hw;
6819 	int err;
6820 
6821 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
6822 	 * Also do not enable DCBx if FW LLDP agent is disabled
6823 	 */
6824 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
6825 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
6826 		err = I40E_NOT_SUPPORTED;
6827 		goto out;
6828 	}
6829 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
6830 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
6831 		err = i40e_dcb_sw_default_config(pf);
6832 		if (err) {
6833 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
6834 			goto out;
6835 		}
6836 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
6837 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
6838 			       DCB_CAP_DCBX_VER_IEEE;
6839 		/* at init capable but disabled */
6840 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
6841 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6842 		goto out;
6843 	}
6844 	err = i40e_init_dcb(hw, true);
6845 	if (!err) {
6846 		/* Device/Function is not DCBX capable */
6847 		if ((!hw->func_caps.dcb) ||
6848 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6849 			dev_info(&pf->pdev->dev,
6850 				 "DCBX offload is not supported or is disabled for this PF.\n");
6851 		} else {
6852 			/* When status is not DISABLED then DCBX in FW */
6853 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6854 				       DCB_CAP_DCBX_VER_IEEE;
6855 
6856 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
6857 			/* Enable DCB tagging only when more than one TC
6858 			 * or explicitly disable if only one TC
6859 			 */
6860 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6861 				pf->flags |= I40E_FLAG_DCB_ENABLED;
6862 			else
6863 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6864 			dev_dbg(&pf->pdev->dev,
6865 				"DCBX offload is supported for this PF.\n");
6866 		}
6867 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
6868 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
6869 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
6870 	} else {
6871 		dev_info(&pf->pdev->dev,
6872 			 "Query for DCB configuration failed, err %s aq_err %s\n",
6873 			 i40e_stat_str(&pf->hw, err),
6874 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6875 	}
6876 
6877 out:
6878 	return err;
6879 }
6880 #endif /* CONFIG_I40E_DCB */
6881 
6882 /**
6883  * i40e_print_link_message - print link up or down
6884  * @vsi: the VSI for which link needs a message
6885  * @isup: true of link is up, false otherwise
6886  */
6887 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
6888 {
6889 	enum i40e_aq_link_speed new_speed;
6890 	struct i40e_pf *pf = vsi->back;
6891 	char *speed = "Unknown";
6892 	char *fc = "Unknown";
6893 	char *fec = "";
6894 	char *req_fec = "";
6895 	char *an = "";
6896 
6897 	if (isup)
6898 		new_speed = pf->hw.phy.link_info.link_speed;
6899 	else
6900 		new_speed = I40E_LINK_SPEED_UNKNOWN;
6901 
6902 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
6903 		return;
6904 	vsi->current_isup = isup;
6905 	vsi->current_speed = new_speed;
6906 	if (!isup) {
6907 		netdev_info(vsi->netdev, "NIC Link is Down\n");
6908 		return;
6909 	}
6910 
6911 	/* Warn user if link speed on NPAR enabled partition is not at
6912 	 * least 10GB
6913 	 */
6914 	if (pf->hw.func_caps.npar_enable &&
6915 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
6916 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
6917 		netdev_warn(vsi->netdev,
6918 			    "The partition detected link speed that is less than 10Gbps\n");
6919 
6920 	switch (pf->hw.phy.link_info.link_speed) {
6921 	case I40E_LINK_SPEED_40GB:
6922 		speed = "40 G";
6923 		break;
6924 	case I40E_LINK_SPEED_20GB:
6925 		speed = "20 G";
6926 		break;
6927 	case I40E_LINK_SPEED_25GB:
6928 		speed = "25 G";
6929 		break;
6930 	case I40E_LINK_SPEED_10GB:
6931 		speed = "10 G";
6932 		break;
6933 	case I40E_LINK_SPEED_5GB:
6934 		speed = "5 G";
6935 		break;
6936 	case I40E_LINK_SPEED_2_5GB:
6937 		speed = "2.5 G";
6938 		break;
6939 	case I40E_LINK_SPEED_1GB:
6940 		speed = "1000 M";
6941 		break;
6942 	case I40E_LINK_SPEED_100MB:
6943 		speed = "100 M";
6944 		break;
6945 	default:
6946 		break;
6947 	}
6948 
6949 	switch (pf->hw.fc.current_mode) {
6950 	case I40E_FC_FULL:
6951 		fc = "RX/TX";
6952 		break;
6953 	case I40E_FC_TX_PAUSE:
6954 		fc = "TX";
6955 		break;
6956 	case I40E_FC_RX_PAUSE:
6957 		fc = "RX";
6958 		break;
6959 	default:
6960 		fc = "None";
6961 		break;
6962 	}
6963 
6964 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
6965 		req_fec = "None";
6966 		fec = "None";
6967 		an = "False";
6968 
6969 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
6970 			an = "True";
6971 
6972 		if (pf->hw.phy.link_info.fec_info &
6973 		    I40E_AQ_CONFIG_FEC_KR_ENA)
6974 			fec = "CL74 FC-FEC/BASE-R";
6975 		else if (pf->hw.phy.link_info.fec_info &
6976 			 I40E_AQ_CONFIG_FEC_RS_ENA)
6977 			fec = "CL108 RS-FEC";
6978 
6979 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
6980 		 * both RS and FC are requested
6981 		 */
6982 		if (vsi->back->hw.phy.link_info.req_fec_info &
6983 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
6984 			if (vsi->back->hw.phy.link_info.req_fec_info &
6985 			    I40E_AQ_REQUEST_FEC_RS)
6986 				req_fec = "CL108 RS-FEC";
6987 			else
6988 				req_fec = "CL74 FC-FEC/BASE-R";
6989 		}
6990 		netdev_info(vsi->netdev,
6991 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
6992 			    speed, req_fec, fec, an, fc);
6993 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
6994 		req_fec = "None";
6995 		fec = "None";
6996 		an = "False";
6997 
6998 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
6999 			an = "True";
7000 
7001 		if (pf->hw.phy.link_info.fec_info &
7002 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7003 			fec = "CL74 FC-FEC/BASE-R";
7004 
7005 		if (pf->hw.phy.link_info.req_fec_info &
7006 		    I40E_AQ_REQUEST_FEC_KR)
7007 			req_fec = "CL74 FC-FEC/BASE-R";
7008 
7009 		netdev_info(vsi->netdev,
7010 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7011 			    speed, req_fec, fec, an, fc);
7012 	} else {
7013 		netdev_info(vsi->netdev,
7014 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7015 			    speed, fc);
7016 	}
7017 
7018 }
7019 
7020 /**
7021  * i40e_up_complete - Finish the last steps of bringing up a connection
7022  * @vsi: the VSI being configured
7023  **/
7024 static int i40e_up_complete(struct i40e_vsi *vsi)
7025 {
7026 	struct i40e_pf *pf = vsi->back;
7027 	int err;
7028 
7029 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7030 		i40e_vsi_configure_msix(vsi);
7031 	else
7032 		i40e_configure_msi_and_legacy(vsi);
7033 
7034 	/* start rings */
7035 	err = i40e_vsi_start_rings(vsi);
7036 	if (err)
7037 		return err;
7038 
7039 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7040 	i40e_napi_enable_all(vsi);
7041 	i40e_vsi_enable_irq(vsi);
7042 
7043 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7044 	    (vsi->netdev)) {
7045 		i40e_print_link_message(vsi, true);
7046 		netif_tx_start_all_queues(vsi->netdev);
7047 		netif_carrier_on(vsi->netdev);
7048 	}
7049 
7050 	/* replay FDIR SB filters */
7051 	if (vsi->type == I40E_VSI_FDIR) {
7052 		/* reset fd counters */
7053 		pf->fd_add_err = 0;
7054 		pf->fd_atr_cnt = 0;
7055 		i40e_fdir_filter_restore(vsi);
7056 	}
7057 
7058 	/* On the next run of the service_task, notify any clients of the new
7059 	 * opened netdev
7060 	 */
7061 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7062 	i40e_service_event_schedule(pf);
7063 
7064 	return 0;
7065 }
7066 
7067 /**
7068  * i40e_vsi_reinit_locked - Reset the VSI
7069  * @vsi: the VSI being configured
7070  *
7071  * Rebuild the ring structs after some configuration
7072  * has changed, e.g. MTU size.
7073  **/
7074 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7075 {
7076 	struct i40e_pf *pf = vsi->back;
7077 
7078 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7079 		usleep_range(1000, 2000);
7080 	i40e_down(vsi);
7081 
7082 	i40e_up(vsi);
7083 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7084 }
7085 
7086 /**
7087  * i40e_force_link_state - Force the link status
7088  * @pf: board private structure
7089  * @is_up: whether the link state should be forced up or down
7090  **/
7091 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7092 {
7093 	struct i40e_aq_get_phy_abilities_resp abilities;
7094 	struct i40e_aq_set_phy_config config = {0};
7095 	bool non_zero_phy_type = is_up;
7096 	struct i40e_hw *hw = &pf->hw;
7097 	i40e_status err;
7098 	u64 mask;
7099 	u8 speed;
7100 
7101 	/* Card might've been put in an unstable state by other drivers
7102 	 * and applications, which causes incorrect speed values being
7103 	 * set on startup. In order to clear speed registers, we call
7104 	 * get_phy_capabilities twice, once to get initial state of
7105 	 * available speeds, and once to get current PHY config.
7106 	 */
7107 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7108 					   NULL);
7109 	if (err) {
7110 		dev_err(&pf->pdev->dev,
7111 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7112 			i40e_stat_str(hw, err),
7113 			i40e_aq_str(hw, hw->aq.asq_last_status));
7114 		return err;
7115 	}
7116 	speed = abilities.link_speed;
7117 
7118 	/* Get the current phy config */
7119 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7120 					   NULL);
7121 	if (err) {
7122 		dev_err(&pf->pdev->dev,
7123 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7124 			i40e_stat_str(hw, err),
7125 			i40e_aq_str(hw, hw->aq.asq_last_status));
7126 		return err;
7127 	}
7128 
7129 	/* If link needs to go up, but was not forced to go down,
7130 	 * and its speed values are OK, no need for a flap
7131 	 * if non_zero_phy_type was set, still need to force up
7132 	 */
7133 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7134 		non_zero_phy_type = true;
7135 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7136 		return I40E_SUCCESS;
7137 
7138 	/* To force link we need to set bits for all supported PHY types,
7139 	 * but there are now more than 32, so we need to split the bitmap
7140 	 * across two fields.
7141 	 */
7142 	mask = I40E_PHY_TYPES_BITMASK;
7143 	config.phy_type =
7144 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7145 	config.phy_type_ext =
7146 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7147 	/* Copy the old settings, except of phy_type */
7148 	config.abilities = abilities.abilities;
7149 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7150 		if (is_up)
7151 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7152 		else
7153 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7154 	}
7155 	if (abilities.link_speed != 0)
7156 		config.link_speed = abilities.link_speed;
7157 	else
7158 		config.link_speed = speed;
7159 	config.eee_capability = abilities.eee_capability;
7160 	config.eeer = abilities.eeer_val;
7161 	config.low_power_ctrl = abilities.d3_lpan;
7162 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7163 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7164 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7165 
7166 	if (err) {
7167 		dev_err(&pf->pdev->dev,
7168 			"set phy config ret =  %s last_status =  %s\n",
7169 			i40e_stat_str(&pf->hw, err),
7170 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7171 		return err;
7172 	}
7173 
7174 	/* Update the link info */
7175 	err = i40e_update_link_info(hw);
7176 	if (err) {
7177 		/* Wait a little bit (on 40G cards it sometimes takes a really
7178 		 * long time for link to come back from the atomic reset)
7179 		 * and try once more
7180 		 */
7181 		msleep(1000);
7182 		i40e_update_link_info(hw);
7183 	}
7184 
7185 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7186 
7187 	return I40E_SUCCESS;
7188 }
7189 
7190 /**
7191  * i40e_up - Bring the connection back up after being down
7192  * @vsi: the VSI being configured
7193  **/
7194 int i40e_up(struct i40e_vsi *vsi)
7195 {
7196 	int err;
7197 
7198 	if (vsi->type == I40E_VSI_MAIN &&
7199 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7200 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7201 		i40e_force_link_state(vsi->back, true);
7202 
7203 	err = i40e_vsi_configure(vsi);
7204 	if (!err)
7205 		err = i40e_up_complete(vsi);
7206 
7207 	return err;
7208 }
7209 
7210 /**
7211  * i40e_down - Shutdown the connection processing
7212  * @vsi: the VSI being stopped
7213  **/
7214 void i40e_down(struct i40e_vsi *vsi)
7215 {
7216 	int i;
7217 
7218 	/* It is assumed that the caller of this function
7219 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7220 	 */
7221 	if (vsi->netdev) {
7222 		netif_carrier_off(vsi->netdev);
7223 		netif_tx_disable(vsi->netdev);
7224 	}
7225 	i40e_vsi_disable_irq(vsi);
7226 	i40e_vsi_stop_rings(vsi);
7227 	if (vsi->type == I40E_VSI_MAIN &&
7228 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7229 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7230 		i40e_force_link_state(vsi->back, false);
7231 	i40e_napi_disable_all(vsi);
7232 
7233 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7234 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7235 		if (i40e_enabled_xdp_vsi(vsi)) {
7236 			/* Make sure that in-progress ndo_xdp_xmit and
7237 			 * ndo_xsk_wakeup calls are completed.
7238 			 */
7239 			synchronize_rcu();
7240 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7241 		}
7242 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7243 	}
7244 
7245 }
7246 
7247 /**
7248  * i40e_validate_mqprio_qopt- validate queue mapping info
7249  * @vsi: the VSI being configured
7250  * @mqprio_qopt: queue parametrs
7251  **/
7252 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7253 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7254 {
7255 	u64 sum_max_rate = 0;
7256 	u64 max_rate = 0;
7257 	int i;
7258 
7259 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7260 	    mqprio_qopt->qopt.num_tc < 1 ||
7261 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7262 		return -EINVAL;
7263 	for (i = 0; ; i++) {
7264 		if (!mqprio_qopt->qopt.count[i])
7265 			return -EINVAL;
7266 		if (mqprio_qopt->min_rate[i]) {
7267 			dev_err(&vsi->back->pdev->dev,
7268 				"Invalid min tx rate (greater than 0) specified\n");
7269 			return -EINVAL;
7270 		}
7271 		max_rate = mqprio_qopt->max_rate[i];
7272 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7273 		sum_max_rate += max_rate;
7274 
7275 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7276 			break;
7277 		if (mqprio_qopt->qopt.offset[i + 1] !=
7278 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7279 			return -EINVAL;
7280 	}
7281 	if (vsi->num_queue_pairs <
7282 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7283 		return -EINVAL;
7284 	}
7285 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7286 		dev_err(&vsi->back->pdev->dev,
7287 			"Invalid max tx rate specified\n");
7288 		return -EINVAL;
7289 	}
7290 	return 0;
7291 }
7292 
7293 /**
7294  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7295  * @vsi: the VSI being configured
7296  **/
7297 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7298 {
7299 	u16 qcount;
7300 	int i;
7301 
7302 	/* Only TC0 is enabled */
7303 	vsi->tc_config.numtc = 1;
7304 	vsi->tc_config.enabled_tc = 1;
7305 	qcount = min_t(int, vsi->alloc_queue_pairs,
7306 		       i40e_pf_get_max_q_per_tc(vsi->back));
7307 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7308 		/* For the TC that is not enabled set the offset to default
7309 		 * queue and allocate one queue for the given TC.
7310 		 */
7311 		vsi->tc_config.tc_info[i].qoffset = 0;
7312 		if (i == 0)
7313 			vsi->tc_config.tc_info[i].qcount = qcount;
7314 		else
7315 			vsi->tc_config.tc_info[i].qcount = 1;
7316 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7317 	}
7318 }
7319 
7320 /**
7321  * i40e_del_macvlan_filter
7322  * @hw: pointer to the HW structure
7323  * @seid: seid of the channel VSI
7324  * @macaddr: the mac address to apply as a filter
7325  * @aq_err: store the admin Q error
7326  *
7327  * This function deletes a mac filter on the channel VSI which serves as the
7328  * macvlan. Returns 0 on success.
7329  **/
7330 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7331 					   const u8 *macaddr, int *aq_err)
7332 {
7333 	struct i40e_aqc_remove_macvlan_element_data element;
7334 	i40e_status status;
7335 
7336 	memset(&element, 0, sizeof(element));
7337 	ether_addr_copy(element.mac_addr, macaddr);
7338 	element.vlan_tag = 0;
7339 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7340 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7341 	*aq_err = hw->aq.asq_last_status;
7342 
7343 	return status;
7344 }
7345 
7346 /**
7347  * i40e_add_macvlan_filter
7348  * @hw: pointer to the HW structure
7349  * @seid: seid of the channel VSI
7350  * @macaddr: the mac address to apply as a filter
7351  * @aq_err: store the admin Q error
7352  *
7353  * This function adds a mac filter on the channel VSI which serves as the
7354  * macvlan. Returns 0 on success.
7355  **/
7356 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7357 					   const u8 *macaddr, int *aq_err)
7358 {
7359 	struct i40e_aqc_add_macvlan_element_data element;
7360 	i40e_status status;
7361 	u16 cmd_flags = 0;
7362 
7363 	ether_addr_copy(element.mac_addr, macaddr);
7364 	element.vlan_tag = 0;
7365 	element.queue_number = 0;
7366 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7367 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7368 	element.flags = cpu_to_le16(cmd_flags);
7369 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7370 	*aq_err = hw->aq.asq_last_status;
7371 
7372 	return status;
7373 }
7374 
7375 /**
7376  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7377  * @vsi: the VSI we want to access
7378  * @ch: the channel we want to access
7379  */
7380 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7381 {
7382 	struct i40e_ring *tx_ring, *rx_ring;
7383 	u16 pf_q;
7384 	int i;
7385 
7386 	for (i = 0; i < ch->num_queue_pairs; i++) {
7387 		pf_q = ch->base_queue + i;
7388 		tx_ring = vsi->tx_rings[pf_q];
7389 		tx_ring->ch = NULL;
7390 		rx_ring = vsi->rx_rings[pf_q];
7391 		rx_ring->ch = NULL;
7392 	}
7393 }
7394 
7395 /**
7396  * i40e_free_macvlan_channels
7397  * @vsi: the VSI we want to access
7398  *
7399  * This function frees the Qs of the channel VSI from
7400  * the stack and also deletes the channel VSIs which
7401  * serve as macvlans.
7402  */
7403 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7404 {
7405 	struct i40e_channel *ch, *ch_tmp;
7406 	int ret;
7407 
7408 	if (list_empty(&vsi->macvlan_list))
7409 		return;
7410 
7411 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7412 		struct i40e_vsi *parent_vsi;
7413 
7414 		if (i40e_is_channel_macvlan(ch)) {
7415 			i40e_reset_ch_rings(vsi, ch);
7416 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7417 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7418 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7419 			kfree(ch->fwd);
7420 			ch->fwd = NULL;
7421 		}
7422 
7423 		list_del(&ch->list);
7424 		parent_vsi = ch->parent_vsi;
7425 		if (!parent_vsi || !ch->initialized) {
7426 			kfree(ch);
7427 			continue;
7428 		}
7429 
7430 		/* remove the VSI */
7431 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7432 					     NULL);
7433 		if (ret)
7434 			dev_err(&vsi->back->pdev->dev,
7435 				"unable to remove channel (%d) for parent VSI(%d)\n",
7436 				ch->seid, parent_vsi->seid);
7437 		kfree(ch);
7438 	}
7439 	vsi->macvlan_cnt = 0;
7440 }
7441 
7442 /**
7443  * i40e_fwd_ring_up - bring the macvlan device up
7444  * @vsi: the VSI we want to access
7445  * @vdev: macvlan netdevice
7446  * @fwd: the private fwd structure
7447  */
7448 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7449 			    struct i40e_fwd_adapter *fwd)
7450 {
7451 	int ret = 0, num_tc = 1,  i, aq_err;
7452 	struct i40e_channel *ch, *ch_tmp;
7453 	struct i40e_pf *pf = vsi->back;
7454 	struct i40e_hw *hw = &pf->hw;
7455 
7456 	if (list_empty(&vsi->macvlan_list))
7457 		return -EINVAL;
7458 
7459 	/* Go through the list and find an available channel */
7460 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7461 		if (!i40e_is_channel_macvlan(ch)) {
7462 			ch->fwd = fwd;
7463 			/* record configuration for macvlan interface in vdev */
7464 			for (i = 0; i < num_tc; i++)
7465 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7466 							     i,
7467 							     ch->num_queue_pairs,
7468 							     ch->base_queue);
7469 			for (i = 0; i < ch->num_queue_pairs; i++) {
7470 				struct i40e_ring *tx_ring, *rx_ring;
7471 				u16 pf_q;
7472 
7473 				pf_q = ch->base_queue + i;
7474 
7475 				/* Get to TX ring ptr */
7476 				tx_ring = vsi->tx_rings[pf_q];
7477 				tx_ring->ch = ch;
7478 
7479 				/* Get the RX ring ptr */
7480 				rx_ring = vsi->rx_rings[pf_q];
7481 				rx_ring->ch = ch;
7482 			}
7483 			break;
7484 		}
7485 	}
7486 
7487 	/* Guarantee all rings are updated before we update the
7488 	 * MAC address filter.
7489 	 */
7490 	wmb();
7491 
7492 	/* Add a mac filter */
7493 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7494 	if (ret) {
7495 		/* if we cannot add the MAC rule then disable the offload */
7496 		macvlan_release_l2fw_offload(vdev);
7497 		for (i = 0; i < ch->num_queue_pairs; i++) {
7498 			struct i40e_ring *rx_ring;
7499 			u16 pf_q;
7500 
7501 			pf_q = ch->base_queue + i;
7502 			rx_ring = vsi->rx_rings[pf_q];
7503 			rx_ring->netdev = NULL;
7504 		}
7505 		dev_info(&pf->pdev->dev,
7506 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7507 			  i40e_stat_str(hw, ret),
7508 			  i40e_aq_str(hw, aq_err));
7509 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7510 	}
7511 
7512 	return ret;
7513 }
7514 
7515 /**
7516  * i40e_setup_macvlans - create the channels which will be macvlans
7517  * @vsi: the VSI we want to access
7518  * @macvlan_cnt: no. of macvlans to be setup
7519  * @qcnt: no. of Qs per macvlan
7520  * @vdev: macvlan netdevice
7521  */
7522 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7523 			       struct net_device *vdev)
7524 {
7525 	struct i40e_pf *pf = vsi->back;
7526 	struct i40e_hw *hw = &pf->hw;
7527 	struct i40e_vsi_context ctxt;
7528 	u16 sections, qmap, num_qps;
7529 	struct i40e_channel *ch;
7530 	int i, pow, ret = 0;
7531 	u8 offset = 0;
7532 
7533 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7534 		return -EINVAL;
7535 
7536 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7537 
7538 	/* find the next higher power-of-2 of num queue pairs */
7539 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7540 
7541 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7542 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7543 
7544 	/* Setup context bits for the main VSI */
7545 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7546 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7547 	memset(&ctxt, 0, sizeof(ctxt));
7548 	ctxt.seid = vsi->seid;
7549 	ctxt.pf_num = vsi->back->hw.pf_id;
7550 	ctxt.vf_num = 0;
7551 	ctxt.uplink_seid = vsi->uplink_seid;
7552 	ctxt.info = vsi->info;
7553 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7554 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7555 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7556 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7557 
7558 	/* Reconfigure RSS for main VSI with new max queue count */
7559 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7560 	ret = i40e_vsi_config_rss(vsi);
7561 	if (ret) {
7562 		dev_info(&pf->pdev->dev,
7563 			 "Failed to reconfig RSS for num_queues (%u)\n",
7564 			 vsi->rss_size);
7565 		return ret;
7566 	}
7567 	vsi->reconfig_rss = true;
7568 	dev_dbg(&vsi->back->pdev->dev,
7569 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7570 	vsi->next_base_queue = num_qps;
7571 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7572 
7573 	/* Update the VSI after updating the VSI queue-mapping
7574 	 * information
7575 	 */
7576 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7577 	if (ret) {
7578 		dev_info(&pf->pdev->dev,
7579 			 "Update vsi tc config failed, err %s aq_err %s\n",
7580 			 i40e_stat_str(hw, ret),
7581 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7582 		return ret;
7583 	}
7584 	/* update the local VSI info with updated queue map */
7585 	i40e_vsi_update_queue_map(vsi, &ctxt);
7586 	vsi->info.valid_sections = 0;
7587 
7588 	/* Create channels for macvlans */
7589 	INIT_LIST_HEAD(&vsi->macvlan_list);
7590 	for (i = 0; i < macvlan_cnt; i++) {
7591 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7592 		if (!ch) {
7593 			ret = -ENOMEM;
7594 			goto err_free;
7595 		}
7596 		INIT_LIST_HEAD(&ch->list);
7597 		ch->num_queue_pairs = qcnt;
7598 		if (!i40e_setup_channel(pf, vsi, ch)) {
7599 			ret = -EINVAL;
7600 			kfree(ch);
7601 			goto err_free;
7602 		}
7603 		ch->parent_vsi = vsi;
7604 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7605 		vsi->macvlan_cnt++;
7606 		list_add_tail(&ch->list, &vsi->macvlan_list);
7607 	}
7608 
7609 	return ret;
7610 
7611 err_free:
7612 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7613 	i40e_free_macvlan_channels(vsi);
7614 
7615 	return ret;
7616 }
7617 
7618 /**
7619  * i40e_fwd_add - configure macvlans
7620  * @netdev: net device to configure
7621  * @vdev: macvlan netdevice
7622  **/
7623 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7624 {
7625 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7626 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7627 	struct i40e_vsi *vsi = np->vsi;
7628 	struct i40e_pf *pf = vsi->back;
7629 	struct i40e_fwd_adapter *fwd;
7630 	int avail_macvlan, ret;
7631 
7632 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7633 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7634 		return ERR_PTR(-EINVAL);
7635 	}
7636 	if ((pf->flags & I40E_FLAG_TC_MQPRIO)) {
7637 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7638 		return ERR_PTR(-EINVAL);
7639 	}
7640 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7641 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7642 		return ERR_PTR(-EINVAL);
7643 	}
7644 
7645 	/* The macvlan device has to be a single Q device so that the
7646 	 * tc_to_txq field can be reused to pick the tx queue.
7647 	 */
7648 	if (netif_is_multiqueue(vdev))
7649 		return ERR_PTR(-ERANGE);
7650 
7651 	if (!vsi->macvlan_cnt) {
7652 		/* reserve bit 0 for the pf device */
7653 		set_bit(0, vsi->fwd_bitmask);
7654 
7655 		/* Try to reserve as many queues as possible for macvlans. First
7656 		 * reserve 3/4th of max vectors, then half, then quarter and
7657 		 * calculate Qs per macvlan as you go
7658 		 */
7659 		vectors = pf->num_lan_msix;
7660 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7661 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7662 			q_per_macvlan = 4;
7663 			macvlan_cnt = (vectors - 32) / 4;
7664 		} else if (vectors <= 64 && vectors > 32) {
7665 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7666 			q_per_macvlan = 2;
7667 			macvlan_cnt = (vectors - 16) / 2;
7668 		} else if (vectors <= 32 && vectors > 16) {
7669 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7670 			q_per_macvlan = 1;
7671 			macvlan_cnt = vectors - 16;
7672 		} else if (vectors <= 16 && vectors > 8) {
7673 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7674 			q_per_macvlan = 1;
7675 			macvlan_cnt = vectors - 8;
7676 		} else {
7677 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7678 			q_per_macvlan = 1;
7679 			macvlan_cnt = vectors - 1;
7680 		}
7681 
7682 		if (macvlan_cnt == 0)
7683 			return ERR_PTR(-EBUSY);
7684 
7685 		/* Quiesce VSI queues */
7686 		i40e_quiesce_vsi(vsi);
7687 
7688 		/* sets up the macvlans but does not "enable" them */
7689 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
7690 					  vdev);
7691 		if (ret)
7692 			return ERR_PTR(ret);
7693 
7694 		/* Unquiesce VSI */
7695 		i40e_unquiesce_vsi(vsi);
7696 	}
7697 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
7698 					    vsi->macvlan_cnt);
7699 	if (avail_macvlan >= I40E_MAX_MACVLANS)
7700 		return ERR_PTR(-EBUSY);
7701 
7702 	/* create the fwd struct */
7703 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
7704 	if (!fwd)
7705 		return ERR_PTR(-ENOMEM);
7706 
7707 	set_bit(avail_macvlan, vsi->fwd_bitmask);
7708 	fwd->bit_no = avail_macvlan;
7709 	netdev_set_sb_channel(vdev, avail_macvlan);
7710 	fwd->netdev = vdev;
7711 
7712 	if (!netif_running(netdev))
7713 		return fwd;
7714 
7715 	/* Set fwd ring up */
7716 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
7717 	if (ret) {
7718 		/* unbind the queues and drop the subordinate channel config */
7719 		netdev_unbind_sb_channel(netdev, vdev);
7720 		netdev_set_sb_channel(vdev, 0);
7721 
7722 		kfree(fwd);
7723 		return ERR_PTR(-EINVAL);
7724 	}
7725 
7726 	return fwd;
7727 }
7728 
7729 /**
7730  * i40e_del_all_macvlans - Delete all the mac filters on the channels
7731  * @vsi: the VSI we want to access
7732  */
7733 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
7734 {
7735 	struct i40e_channel *ch, *ch_tmp;
7736 	struct i40e_pf *pf = vsi->back;
7737 	struct i40e_hw *hw = &pf->hw;
7738 	int aq_err, ret = 0;
7739 
7740 	if (list_empty(&vsi->macvlan_list))
7741 		return;
7742 
7743 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7744 		if (i40e_is_channel_macvlan(ch)) {
7745 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7746 						      i40e_channel_mac(ch),
7747 						      &aq_err);
7748 			if (!ret) {
7749 				/* Reset queue contexts */
7750 				i40e_reset_ch_rings(vsi, ch);
7751 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7752 				netdev_unbind_sb_channel(vsi->netdev,
7753 							 ch->fwd->netdev);
7754 				netdev_set_sb_channel(ch->fwd->netdev, 0);
7755 				kfree(ch->fwd);
7756 				ch->fwd = NULL;
7757 			}
7758 		}
7759 	}
7760 }
7761 
7762 /**
7763  * i40e_fwd_del - delete macvlan interfaces
7764  * @netdev: net device to configure
7765  * @vdev: macvlan netdevice
7766  */
7767 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
7768 {
7769 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7770 	struct i40e_fwd_adapter *fwd = vdev;
7771 	struct i40e_channel *ch, *ch_tmp;
7772 	struct i40e_vsi *vsi = np->vsi;
7773 	struct i40e_pf *pf = vsi->back;
7774 	struct i40e_hw *hw = &pf->hw;
7775 	int aq_err, ret = 0;
7776 
7777 	/* Find the channel associated with the macvlan and del mac filter */
7778 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7779 		if (i40e_is_channel_macvlan(ch) &&
7780 		    ether_addr_equal(i40e_channel_mac(ch),
7781 				     fwd->netdev->dev_addr)) {
7782 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7783 						      i40e_channel_mac(ch),
7784 						      &aq_err);
7785 			if (!ret) {
7786 				/* Reset queue contexts */
7787 				i40e_reset_ch_rings(vsi, ch);
7788 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7789 				netdev_unbind_sb_channel(netdev, fwd->netdev);
7790 				netdev_set_sb_channel(fwd->netdev, 0);
7791 				kfree(ch->fwd);
7792 				ch->fwd = NULL;
7793 			} else {
7794 				dev_info(&pf->pdev->dev,
7795 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
7796 					  i40e_stat_str(hw, ret),
7797 					  i40e_aq_str(hw, aq_err));
7798 			}
7799 			break;
7800 		}
7801 	}
7802 }
7803 
7804 /**
7805  * i40e_setup_tc - configure multiple traffic classes
7806  * @netdev: net device to configure
7807  * @type_data: tc offload data
7808  **/
7809 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
7810 {
7811 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
7812 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7813 	struct i40e_vsi *vsi = np->vsi;
7814 	struct i40e_pf *pf = vsi->back;
7815 	u8 enabled_tc = 0, num_tc, hw;
7816 	bool need_reset = false;
7817 	int old_queue_pairs;
7818 	int ret = -EINVAL;
7819 	u16 mode;
7820 	int i;
7821 
7822 	old_queue_pairs = vsi->num_queue_pairs;
7823 	num_tc = mqprio_qopt->qopt.num_tc;
7824 	hw = mqprio_qopt->qopt.hw;
7825 	mode = mqprio_qopt->mode;
7826 	if (!hw) {
7827 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7828 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
7829 		goto config_tc;
7830 	}
7831 
7832 	/* Check if MFP enabled */
7833 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
7834 		netdev_info(netdev,
7835 			    "Configuring TC not supported in MFP mode\n");
7836 		return ret;
7837 	}
7838 	switch (mode) {
7839 	case TC_MQPRIO_MODE_DCB:
7840 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7841 
7842 		/* Check if DCB enabled to continue */
7843 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7844 			netdev_info(netdev,
7845 				    "DCB is not enabled for adapter\n");
7846 			return ret;
7847 		}
7848 
7849 		/* Check whether tc count is within enabled limit */
7850 		if (num_tc > i40e_pf_get_num_tc(pf)) {
7851 			netdev_info(netdev,
7852 				    "TC count greater than enabled on link for adapter\n");
7853 			return ret;
7854 		}
7855 		break;
7856 	case TC_MQPRIO_MODE_CHANNEL:
7857 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
7858 			netdev_info(netdev,
7859 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
7860 			return ret;
7861 		}
7862 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7863 			return ret;
7864 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
7865 		if (ret)
7866 			return ret;
7867 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
7868 		       sizeof(*mqprio_qopt));
7869 		pf->flags |= I40E_FLAG_TC_MQPRIO;
7870 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7871 		break;
7872 	default:
7873 		return -EINVAL;
7874 	}
7875 
7876 config_tc:
7877 	/* Generate TC map for number of tc requested */
7878 	for (i = 0; i < num_tc; i++)
7879 		enabled_tc |= BIT(i);
7880 
7881 	/* Requesting same TC configuration as already enabled */
7882 	if (enabled_tc == vsi->tc_config.enabled_tc &&
7883 	    mode != TC_MQPRIO_MODE_CHANNEL)
7884 		return 0;
7885 
7886 	/* Quiesce VSI queues */
7887 	i40e_quiesce_vsi(vsi);
7888 
7889 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
7890 		i40e_remove_queue_channels(vsi);
7891 
7892 	/* Configure VSI for enabled TCs */
7893 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
7894 	if (ret) {
7895 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
7896 			    vsi->seid);
7897 		need_reset = true;
7898 		goto exit;
7899 	} else {
7900 		dev_info(&vsi->back->pdev->dev,
7901 			 "Setup channel (id:%u) utilizing num_queues %d\n",
7902 			 vsi->seid, vsi->tc_config.tc_info[0].qcount);
7903 	}
7904 
7905 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
7906 		if (vsi->mqprio_qopt.max_rate[0]) {
7907 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
7908 
7909 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
7910 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
7911 			if (!ret) {
7912 				u64 credits = max_tx_rate;
7913 
7914 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
7915 				dev_dbg(&vsi->back->pdev->dev,
7916 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
7917 					max_tx_rate,
7918 					credits,
7919 					vsi->seid);
7920 			} else {
7921 				need_reset = true;
7922 				goto exit;
7923 			}
7924 		}
7925 		ret = i40e_configure_queue_channels(vsi);
7926 		if (ret) {
7927 			vsi->num_queue_pairs = old_queue_pairs;
7928 			netdev_info(netdev,
7929 				    "Failed configuring queue channels\n");
7930 			need_reset = true;
7931 			goto exit;
7932 		}
7933 	}
7934 
7935 exit:
7936 	/* Reset the configuration data to defaults, only TC0 is enabled */
7937 	if (need_reset) {
7938 		i40e_vsi_set_default_tc_config(vsi);
7939 		need_reset = false;
7940 	}
7941 
7942 	/* Unquiesce VSI */
7943 	i40e_unquiesce_vsi(vsi);
7944 	return ret;
7945 }
7946 
7947 /**
7948  * i40e_set_cld_element - sets cloud filter element data
7949  * @filter: cloud filter rule
7950  * @cld: ptr to cloud filter element data
7951  *
7952  * This is helper function to copy data into cloud filter element
7953  **/
7954 static inline void
7955 i40e_set_cld_element(struct i40e_cloud_filter *filter,
7956 		     struct i40e_aqc_cloud_filters_element_data *cld)
7957 {
7958 	u32 ipa;
7959 	int i;
7960 
7961 	memset(cld, 0, sizeof(*cld));
7962 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
7963 	ether_addr_copy(cld->inner_mac, filter->src_mac);
7964 
7965 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
7966 		return;
7967 
7968 	if (filter->n_proto == ETH_P_IPV6) {
7969 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
7970 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
7971 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
7972 
7973 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
7974 		}
7975 	} else {
7976 		ipa = be32_to_cpu(filter->dst_ipv4);
7977 
7978 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
7979 	}
7980 
7981 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
7982 
7983 	/* tenant_id is not supported by FW now, once the support is enabled
7984 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
7985 	 */
7986 	if (filter->tenant_id)
7987 		return;
7988 }
7989 
7990 /**
7991  * i40e_add_del_cloud_filter - Add/del cloud filter
7992  * @vsi: pointer to VSI
7993  * @filter: cloud filter rule
7994  * @add: if true, add, if false, delete
7995  *
7996  * Add or delete a cloud filter for a specific flow spec.
7997  * Returns 0 if the filter were successfully added.
7998  **/
7999 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8000 			      struct i40e_cloud_filter *filter, bool add)
8001 {
8002 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8003 	struct i40e_pf *pf = vsi->back;
8004 	int ret;
8005 	static const u16 flag_table[128] = {
8006 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8007 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8008 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8009 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8010 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8011 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8012 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8013 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8014 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8015 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8016 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8017 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8018 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8019 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8020 	};
8021 
8022 	if (filter->flags >= ARRAY_SIZE(flag_table))
8023 		return I40E_ERR_CONFIG;
8024 
8025 	memset(&cld_filter, 0, sizeof(cld_filter));
8026 
8027 	/* copy element needed to add cloud filter from filter */
8028 	i40e_set_cld_element(filter, &cld_filter);
8029 
8030 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8031 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8032 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8033 
8034 	if (filter->n_proto == ETH_P_IPV6)
8035 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8036 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8037 	else
8038 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8039 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8040 
8041 	if (add)
8042 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8043 						&cld_filter, 1);
8044 	else
8045 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8046 						&cld_filter, 1);
8047 	if (ret)
8048 		dev_dbg(&pf->pdev->dev,
8049 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8050 			add ? "add" : "delete", filter->dst_port, ret,
8051 			pf->hw.aq.asq_last_status);
8052 	else
8053 		dev_info(&pf->pdev->dev,
8054 			 "%s cloud filter for VSI: %d\n",
8055 			 add ? "Added" : "Deleted", filter->seid);
8056 	return ret;
8057 }
8058 
8059 /**
8060  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8061  * @vsi: pointer to VSI
8062  * @filter: cloud filter rule
8063  * @add: if true, add, if false, delete
8064  *
8065  * Add or delete a cloud filter for a specific flow spec using big buffer.
8066  * Returns 0 if the filter were successfully added.
8067  **/
8068 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8069 				      struct i40e_cloud_filter *filter,
8070 				      bool add)
8071 {
8072 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8073 	struct i40e_pf *pf = vsi->back;
8074 	int ret;
8075 
8076 	/* Both (src/dst) valid mac_addr are not supported */
8077 	if ((is_valid_ether_addr(filter->dst_mac) &&
8078 	     is_valid_ether_addr(filter->src_mac)) ||
8079 	    (is_multicast_ether_addr(filter->dst_mac) &&
8080 	     is_multicast_ether_addr(filter->src_mac)))
8081 		return -EOPNOTSUPP;
8082 
8083 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8084 	 * ports are not supported via big buffer now.
8085 	 */
8086 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8087 		return -EOPNOTSUPP;
8088 
8089 	/* adding filter using src_port/src_ip is not supported at this stage */
8090 	if (filter->src_port ||
8091 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8092 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8093 		return -EOPNOTSUPP;
8094 
8095 	memset(&cld_filter, 0, sizeof(cld_filter));
8096 
8097 	/* copy element needed to add cloud filter from filter */
8098 	i40e_set_cld_element(filter, &cld_filter.element);
8099 
8100 	if (is_valid_ether_addr(filter->dst_mac) ||
8101 	    is_valid_ether_addr(filter->src_mac) ||
8102 	    is_multicast_ether_addr(filter->dst_mac) ||
8103 	    is_multicast_ether_addr(filter->src_mac)) {
8104 		/* MAC + IP : unsupported mode */
8105 		if (filter->dst_ipv4)
8106 			return -EOPNOTSUPP;
8107 
8108 		/* since we validated that L4 port must be valid before
8109 		 * we get here, start with respective "flags" value
8110 		 * and update if vlan is present or not
8111 		 */
8112 		cld_filter.element.flags =
8113 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8114 
8115 		if (filter->vlan_id) {
8116 			cld_filter.element.flags =
8117 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8118 		}
8119 
8120 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8121 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8122 		cld_filter.element.flags =
8123 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8124 		if (filter->n_proto == ETH_P_IPV6)
8125 			cld_filter.element.flags |=
8126 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8127 		else
8128 			cld_filter.element.flags |=
8129 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8130 	} else {
8131 		dev_err(&pf->pdev->dev,
8132 			"either mac or ip has to be valid for cloud filter\n");
8133 		return -EINVAL;
8134 	}
8135 
8136 	/* Now copy L4 port in Byte 6..7 in general fields */
8137 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8138 						be16_to_cpu(filter->dst_port);
8139 
8140 	if (add) {
8141 		/* Validate current device switch mode, change if necessary */
8142 		ret = i40e_validate_and_set_switch_mode(vsi);
8143 		if (ret) {
8144 			dev_err(&pf->pdev->dev,
8145 				"failed to set switch mode, ret %d\n",
8146 				ret);
8147 			return ret;
8148 		}
8149 
8150 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8151 						   &cld_filter, 1);
8152 	} else {
8153 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8154 						   &cld_filter, 1);
8155 	}
8156 
8157 	if (ret)
8158 		dev_dbg(&pf->pdev->dev,
8159 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8160 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8161 	else
8162 		dev_info(&pf->pdev->dev,
8163 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8164 			 add ? "add" : "delete", filter->seid,
8165 			 ntohs(filter->dst_port));
8166 	return ret;
8167 }
8168 
8169 /**
8170  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8171  * @vsi: Pointer to VSI
8172  * @f: Pointer to struct flow_cls_offload
8173  * @filter: Pointer to cloud filter structure
8174  *
8175  **/
8176 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8177 				 struct flow_cls_offload *f,
8178 				 struct i40e_cloud_filter *filter)
8179 {
8180 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8181 	struct flow_dissector *dissector = rule->match.dissector;
8182 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8183 	struct i40e_pf *pf = vsi->back;
8184 	u8 field_flags = 0;
8185 
8186 	if (dissector->used_keys &
8187 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8188 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8189 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8190 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8191 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8192 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8193 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8194 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8195 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8196 			dissector->used_keys);
8197 		return -EOPNOTSUPP;
8198 	}
8199 
8200 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8201 		struct flow_match_enc_keyid match;
8202 
8203 		flow_rule_match_enc_keyid(rule, &match);
8204 		if (match.mask->keyid != 0)
8205 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8206 
8207 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8208 	}
8209 
8210 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8211 		struct flow_match_basic match;
8212 
8213 		flow_rule_match_basic(rule, &match);
8214 		n_proto_key = ntohs(match.key->n_proto);
8215 		n_proto_mask = ntohs(match.mask->n_proto);
8216 
8217 		if (n_proto_key == ETH_P_ALL) {
8218 			n_proto_key = 0;
8219 			n_proto_mask = 0;
8220 		}
8221 		filter->n_proto = n_proto_key & n_proto_mask;
8222 		filter->ip_proto = match.key->ip_proto;
8223 	}
8224 
8225 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8226 		struct flow_match_eth_addrs match;
8227 
8228 		flow_rule_match_eth_addrs(rule, &match);
8229 
8230 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8231 		if (!is_zero_ether_addr(match.mask->dst)) {
8232 			if (is_broadcast_ether_addr(match.mask->dst)) {
8233 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8234 			} else {
8235 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8236 					match.mask->dst);
8237 				return I40E_ERR_CONFIG;
8238 			}
8239 		}
8240 
8241 		if (!is_zero_ether_addr(match.mask->src)) {
8242 			if (is_broadcast_ether_addr(match.mask->src)) {
8243 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8244 			} else {
8245 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8246 					match.mask->src);
8247 				return I40E_ERR_CONFIG;
8248 			}
8249 		}
8250 		ether_addr_copy(filter->dst_mac, match.key->dst);
8251 		ether_addr_copy(filter->src_mac, match.key->src);
8252 	}
8253 
8254 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8255 		struct flow_match_vlan match;
8256 
8257 		flow_rule_match_vlan(rule, &match);
8258 		if (match.mask->vlan_id) {
8259 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8260 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8261 
8262 			} else {
8263 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8264 					match.mask->vlan_id);
8265 				return I40E_ERR_CONFIG;
8266 			}
8267 		}
8268 
8269 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8270 	}
8271 
8272 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8273 		struct flow_match_control match;
8274 
8275 		flow_rule_match_control(rule, &match);
8276 		addr_type = match.key->addr_type;
8277 	}
8278 
8279 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8280 		struct flow_match_ipv4_addrs match;
8281 
8282 		flow_rule_match_ipv4_addrs(rule, &match);
8283 		if (match.mask->dst) {
8284 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8285 				field_flags |= I40E_CLOUD_FIELD_IIP;
8286 			} else {
8287 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8288 					&match.mask->dst);
8289 				return I40E_ERR_CONFIG;
8290 			}
8291 		}
8292 
8293 		if (match.mask->src) {
8294 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8295 				field_flags |= I40E_CLOUD_FIELD_IIP;
8296 			} else {
8297 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8298 					&match.mask->src);
8299 				return I40E_ERR_CONFIG;
8300 			}
8301 		}
8302 
8303 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8304 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8305 			return I40E_ERR_CONFIG;
8306 		}
8307 		filter->dst_ipv4 = match.key->dst;
8308 		filter->src_ipv4 = match.key->src;
8309 	}
8310 
8311 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8312 		struct flow_match_ipv6_addrs match;
8313 
8314 		flow_rule_match_ipv6_addrs(rule, &match);
8315 
8316 		/* src and dest IPV6 address should not be LOOPBACK
8317 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8318 		 */
8319 		if (ipv6_addr_loopback(&match.key->dst) ||
8320 		    ipv6_addr_loopback(&match.key->src)) {
8321 			dev_err(&pf->pdev->dev,
8322 				"Bad ipv6, addr is LOOPBACK\n");
8323 			return I40E_ERR_CONFIG;
8324 		}
8325 		if (!ipv6_addr_any(&match.mask->dst) ||
8326 		    !ipv6_addr_any(&match.mask->src))
8327 			field_flags |= I40E_CLOUD_FIELD_IIP;
8328 
8329 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8330 		       sizeof(filter->src_ipv6));
8331 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8332 		       sizeof(filter->dst_ipv6));
8333 	}
8334 
8335 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8336 		struct flow_match_ports match;
8337 
8338 		flow_rule_match_ports(rule, &match);
8339 		if (match.mask->src) {
8340 			if (match.mask->src == cpu_to_be16(0xffff)) {
8341 				field_flags |= I40E_CLOUD_FIELD_IIP;
8342 			} else {
8343 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8344 					be16_to_cpu(match.mask->src));
8345 				return I40E_ERR_CONFIG;
8346 			}
8347 		}
8348 
8349 		if (match.mask->dst) {
8350 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8351 				field_flags |= I40E_CLOUD_FIELD_IIP;
8352 			} else {
8353 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8354 					be16_to_cpu(match.mask->dst));
8355 				return I40E_ERR_CONFIG;
8356 			}
8357 		}
8358 
8359 		filter->dst_port = match.key->dst;
8360 		filter->src_port = match.key->src;
8361 
8362 		switch (filter->ip_proto) {
8363 		case IPPROTO_TCP:
8364 		case IPPROTO_UDP:
8365 			break;
8366 		default:
8367 			dev_err(&pf->pdev->dev,
8368 				"Only UDP and TCP transport are supported\n");
8369 			return -EINVAL;
8370 		}
8371 	}
8372 	filter->flags = field_flags;
8373 	return 0;
8374 }
8375 
8376 /**
8377  * i40e_handle_tclass: Forward to a traffic class on the device
8378  * @vsi: Pointer to VSI
8379  * @tc: traffic class index on the device
8380  * @filter: Pointer to cloud filter structure
8381  *
8382  **/
8383 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8384 			      struct i40e_cloud_filter *filter)
8385 {
8386 	struct i40e_channel *ch, *ch_tmp;
8387 
8388 	/* direct to a traffic class on the same device */
8389 	if (tc == 0) {
8390 		filter->seid = vsi->seid;
8391 		return 0;
8392 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8393 		if (!filter->dst_port) {
8394 			dev_err(&vsi->back->pdev->dev,
8395 				"Specify destination port to direct to traffic class that is not default\n");
8396 			return -EINVAL;
8397 		}
8398 		if (list_empty(&vsi->ch_list))
8399 			return -EINVAL;
8400 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8401 					 list) {
8402 			if (ch->seid == vsi->tc_seid_map[tc])
8403 				filter->seid = ch->seid;
8404 		}
8405 		return 0;
8406 	}
8407 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8408 	return -EINVAL;
8409 }
8410 
8411 /**
8412  * i40e_configure_clsflower - Configure tc flower filters
8413  * @vsi: Pointer to VSI
8414  * @cls_flower: Pointer to struct flow_cls_offload
8415  *
8416  **/
8417 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8418 				    struct flow_cls_offload *cls_flower)
8419 {
8420 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8421 	struct i40e_cloud_filter *filter = NULL;
8422 	struct i40e_pf *pf = vsi->back;
8423 	int err = 0;
8424 
8425 	if (tc < 0) {
8426 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8427 		return -EOPNOTSUPP;
8428 	}
8429 
8430 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8431 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8432 		return -EBUSY;
8433 
8434 	if (pf->fdir_pf_active_filters ||
8435 	    (!hlist_empty(&pf->fdir_filter_list))) {
8436 		dev_err(&vsi->back->pdev->dev,
8437 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8438 		return -EINVAL;
8439 	}
8440 
8441 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8442 		dev_err(&vsi->back->pdev->dev,
8443 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8444 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8445 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8446 	}
8447 
8448 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8449 	if (!filter)
8450 		return -ENOMEM;
8451 
8452 	filter->cookie = cls_flower->cookie;
8453 
8454 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8455 	if (err < 0)
8456 		goto err;
8457 
8458 	err = i40e_handle_tclass(vsi, tc, filter);
8459 	if (err < 0)
8460 		goto err;
8461 
8462 	/* Add cloud filter */
8463 	if (filter->dst_port)
8464 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8465 	else
8466 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8467 
8468 	if (err) {
8469 		dev_err(&pf->pdev->dev,
8470 			"Failed to add cloud filter, err %s\n",
8471 			i40e_stat_str(&pf->hw, err));
8472 		goto err;
8473 	}
8474 
8475 	/* add filter to the ordered list */
8476 	INIT_HLIST_NODE(&filter->cloud_node);
8477 
8478 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8479 
8480 	pf->num_cloud_filters++;
8481 
8482 	return err;
8483 err:
8484 	kfree(filter);
8485 	return err;
8486 }
8487 
8488 /**
8489  * i40e_find_cloud_filter - Find the could filter in the list
8490  * @vsi: Pointer to VSI
8491  * @cookie: filter specific cookie
8492  *
8493  **/
8494 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8495 							unsigned long *cookie)
8496 {
8497 	struct i40e_cloud_filter *filter = NULL;
8498 	struct hlist_node *node2;
8499 
8500 	hlist_for_each_entry_safe(filter, node2,
8501 				  &vsi->back->cloud_filter_list, cloud_node)
8502 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8503 			return filter;
8504 	return NULL;
8505 }
8506 
8507 /**
8508  * i40e_delete_clsflower - Remove tc flower filters
8509  * @vsi: Pointer to VSI
8510  * @cls_flower: Pointer to struct flow_cls_offload
8511  *
8512  **/
8513 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8514 				 struct flow_cls_offload *cls_flower)
8515 {
8516 	struct i40e_cloud_filter *filter = NULL;
8517 	struct i40e_pf *pf = vsi->back;
8518 	int err = 0;
8519 
8520 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8521 
8522 	if (!filter)
8523 		return -EINVAL;
8524 
8525 	hash_del(&filter->cloud_node);
8526 
8527 	if (filter->dst_port)
8528 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8529 	else
8530 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8531 
8532 	kfree(filter);
8533 	if (err) {
8534 		dev_err(&pf->pdev->dev,
8535 			"Failed to delete cloud filter, err %s\n",
8536 			i40e_stat_str(&pf->hw, err));
8537 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8538 	}
8539 
8540 	pf->num_cloud_filters--;
8541 	if (!pf->num_cloud_filters)
8542 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8543 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8544 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8545 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8546 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8547 		}
8548 	return 0;
8549 }
8550 
8551 /**
8552  * i40e_setup_tc_cls_flower - flower classifier offloads
8553  * @np: net device to configure
8554  * @cls_flower: offload data
8555  **/
8556 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8557 				    struct flow_cls_offload *cls_flower)
8558 {
8559 	struct i40e_vsi *vsi = np->vsi;
8560 
8561 	switch (cls_flower->command) {
8562 	case FLOW_CLS_REPLACE:
8563 		return i40e_configure_clsflower(vsi, cls_flower);
8564 	case FLOW_CLS_DESTROY:
8565 		return i40e_delete_clsflower(vsi, cls_flower);
8566 	case FLOW_CLS_STATS:
8567 		return -EOPNOTSUPP;
8568 	default:
8569 		return -EOPNOTSUPP;
8570 	}
8571 }
8572 
8573 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8574 				  void *cb_priv)
8575 {
8576 	struct i40e_netdev_priv *np = cb_priv;
8577 
8578 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8579 		return -EOPNOTSUPP;
8580 
8581 	switch (type) {
8582 	case TC_SETUP_CLSFLOWER:
8583 		return i40e_setup_tc_cls_flower(np, type_data);
8584 
8585 	default:
8586 		return -EOPNOTSUPP;
8587 	}
8588 }
8589 
8590 static LIST_HEAD(i40e_block_cb_list);
8591 
8592 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8593 			   void *type_data)
8594 {
8595 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8596 
8597 	switch (type) {
8598 	case TC_SETUP_QDISC_MQPRIO:
8599 		return i40e_setup_tc(netdev, type_data);
8600 	case TC_SETUP_BLOCK:
8601 		return flow_block_cb_setup_simple(type_data,
8602 						  &i40e_block_cb_list,
8603 						  i40e_setup_tc_block_cb,
8604 						  np, np, true);
8605 	default:
8606 		return -EOPNOTSUPP;
8607 	}
8608 }
8609 
8610 /**
8611  * i40e_open - Called when a network interface is made active
8612  * @netdev: network interface device structure
8613  *
8614  * The open entry point is called when a network interface is made
8615  * active by the system (IFF_UP).  At this point all resources needed
8616  * for transmit and receive operations are allocated, the interrupt
8617  * handler is registered with the OS, the netdev watchdog subtask is
8618  * enabled, and the stack is notified that the interface is ready.
8619  *
8620  * Returns 0 on success, negative value on failure
8621  **/
8622 int i40e_open(struct net_device *netdev)
8623 {
8624 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8625 	struct i40e_vsi *vsi = np->vsi;
8626 	struct i40e_pf *pf = vsi->back;
8627 	int err;
8628 
8629 	/* disallow open during test or if eeprom is broken */
8630 	if (test_bit(__I40E_TESTING, pf->state) ||
8631 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8632 		return -EBUSY;
8633 
8634 	netif_carrier_off(netdev);
8635 
8636 	if (i40e_force_link_state(pf, true))
8637 		return -EAGAIN;
8638 
8639 	err = i40e_vsi_open(vsi);
8640 	if (err)
8641 		return err;
8642 
8643 	/* configure global TSO hardware offload settings */
8644 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8645 						       TCP_FLAG_FIN) >> 16);
8646 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8647 						       TCP_FLAG_FIN |
8648 						       TCP_FLAG_CWR) >> 16);
8649 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8650 	udp_tunnel_get_rx_info(netdev);
8651 
8652 	return 0;
8653 }
8654 
8655 /**
8656  * i40e_vsi_open -
8657  * @vsi: the VSI to open
8658  *
8659  * Finish initialization of the VSI.
8660  *
8661  * Returns 0 on success, negative value on failure
8662  *
8663  * Note: expects to be called while under rtnl_lock()
8664  **/
8665 int i40e_vsi_open(struct i40e_vsi *vsi)
8666 {
8667 	struct i40e_pf *pf = vsi->back;
8668 	char int_name[I40E_INT_NAME_STR_LEN];
8669 	int err;
8670 
8671 	/* allocate descriptors */
8672 	err = i40e_vsi_setup_tx_resources(vsi);
8673 	if (err)
8674 		goto err_setup_tx;
8675 	err = i40e_vsi_setup_rx_resources(vsi);
8676 	if (err)
8677 		goto err_setup_rx;
8678 
8679 	err = i40e_vsi_configure(vsi);
8680 	if (err)
8681 		goto err_setup_rx;
8682 
8683 	if (vsi->netdev) {
8684 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
8685 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
8686 		err = i40e_vsi_request_irq(vsi, int_name);
8687 		if (err)
8688 			goto err_setup_rx;
8689 
8690 		/* Notify the stack of the actual queue counts. */
8691 		err = netif_set_real_num_tx_queues(vsi->netdev,
8692 						   vsi->num_queue_pairs);
8693 		if (err)
8694 			goto err_set_queues;
8695 
8696 		err = netif_set_real_num_rx_queues(vsi->netdev,
8697 						   vsi->num_queue_pairs);
8698 		if (err)
8699 			goto err_set_queues;
8700 
8701 	} else if (vsi->type == I40E_VSI_FDIR) {
8702 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
8703 			 dev_driver_string(&pf->pdev->dev),
8704 			 dev_name(&pf->pdev->dev));
8705 		err = i40e_vsi_request_irq(vsi, int_name);
8706 		if (err)
8707 			goto err_setup_rx;
8708 
8709 	} else {
8710 		err = -EINVAL;
8711 		goto err_setup_rx;
8712 	}
8713 
8714 	err = i40e_up_complete(vsi);
8715 	if (err)
8716 		goto err_up_complete;
8717 
8718 	return 0;
8719 
8720 err_up_complete:
8721 	i40e_down(vsi);
8722 err_set_queues:
8723 	i40e_vsi_free_irq(vsi);
8724 err_setup_rx:
8725 	i40e_vsi_free_rx_resources(vsi);
8726 err_setup_tx:
8727 	i40e_vsi_free_tx_resources(vsi);
8728 	if (vsi == pf->vsi[pf->lan_vsi])
8729 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
8730 
8731 	return err;
8732 }
8733 
8734 /**
8735  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
8736  * @pf: Pointer to PF
8737  *
8738  * This function destroys the hlist where all the Flow Director
8739  * filters were saved.
8740  **/
8741 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
8742 {
8743 	struct i40e_fdir_filter *filter;
8744 	struct i40e_flex_pit *pit_entry, *tmp;
8745 	struct hlist_node *node2;
8746 
8747 	hlist_for_each_entry_safe(filter, node2,
8748 				  &pf->fdir_filter_list, fdir_node) {
8749 		hlist_del(&filter->fdir_node);
8750 		kfree(filter);
8751 	}
8752 
8753 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
8754 		list_del(&pit_entry->list);
8755 		kfree(pit_entry);
8756 	}
8757 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
8758 
8759 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
8760 		list_del(&pit_entry->list);
8761 		kfree(pit_entry);
8762 	}
8763 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
8764 
8765 	pf->fdir_pf_active_filters = 0;
8766 	i40e_reset_fdir_filter_cnt(pf);
8767 
8768 	/* Reprogram the default input set for TCP/IPv4 */
8769 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8770 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8771 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8772 
8773 	/* Reprogram the default input set for TCP/IPv6 */
8774 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
8775 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8776 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8777 
8778 	/* Reprogram the default input set for UDP/IPv4 */
8779 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
8780 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8781 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8782 
8783 	/* Reprogram the default input set for UDP/IPv6 */
8784 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
8785 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8786 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8787 
8788 	/* Reprogram the default input set for SCTP/IPv4 */
8789 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
8790 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8791 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8792 
8793 	/* Reprogram the default input set for SCTP/IPv6 */
8794 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
8795 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8796 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8797 
8798 	/* Reprogram the default input set for Other/IPv4 */
8799 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
8800 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8801 
8802 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
8803 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8804 
8805 	/* Reprogram the default input set for Other/IPv6 */
8806 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
8807 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8808 
8809 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
8810 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8811 }
8812 
8813 /**
8814  * i40e_cloud_filter_exit - Cleans up the cloud filters
8815  * @pf: Pointer to PF
8816  *
8817  * This function destroys the hlist where all the cloud filters
8818  * were saved.
8819  **/
8820 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
8821 {
8822 	struct i40e_cloud_filter *cfilter;
8823 	struct hlist_node *node;
8824 
8825 	hlist_for_each_entry_safe(cfilter, node,
8826 				  &pf->cloud_filter_list, cloud_node) {
8827 		hlist_del(&cfilter->cloud_node);
8828 		kfree(cfilter);
8829 	}
8830 	pf->num_cloud_filters = 0;
8831 
8832 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8833 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8834 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8835 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8836 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8837 	}
8838 }
8839 
8840 /**
8841  * i40e_close - Disables a network interface
8842  * @netdev: network interface device structure
8843  *
8844  * The close entry point is called when an interface is de-activated
8845  * by the OS.  The hardware is still under the driver's control, but
8846  * this netdev interface is disabled.
8847  *
8848  * Returns 0, this is not allowed to fail
8849  **/
8850 int i40e_close(struct net_device *netdev)
8851 {
8852 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8853 	struct i40e_vsi *vsi = np->vsi;
8854 
8855 	i40e_vsi_close(vsi);
8856 
8857 	return 0;
8858 }
8859 
8860 /**
8861  * i40e_do_reset - Start a PF or Core Reset sequence
8862  * @pf: board private structure
8863  * @reset_flags: which reset is requested
8864  * @lock_acquired: indicates whether or not the lock has been acquired
8865  * before this function was called.
8866  *
8867  * The essential difference in resets is that the PF Reset
8868  * doesn't clear the packet buffers, doesn't reset the PE
8869  * firmware, and doesn't bother the other PFs on the chip.
8870  **/
8871 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
8872 {
8873 	u32 val;
8874 
8875 	/* do the biggest reset indicated */
8876 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
8877 
8878 		/* Request a Global Reset
8879 		 *
8880 		 * This will start the chip's countdown to the actual full
8881 		 * chip reset event, and a warning interrupt to be sent
8882 		 * to all PFs, including the requestor.  Our handler
8883 		 * for the warning interrupt will deal with the shutdown
8884 		 * and recovery of the switch setup.
8885 		 */
8886 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
8887 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8888 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
8889 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
8890 
8891 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
8892 
8893 		/* Request a Core Reset
8894 		 *
8895 		 * Same as Global Reset, except does *not* include the MAC/PHY
8896 		 */
8897 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
8898 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8899 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
8900 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
8901 		i40e_flush(&pf->hw);
8902 
8903 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
8904 
8905 		/* Request a PF Reset
8906 		 *
8907 		 * Resets only the PF-specific registers
8908 		 *
8909 		 * This goes directly to the tear-down and rebuild of
8910 		 * the switch, since we need to do all the recovery as
8911 		 * for the Core Reset.
8912 		 */
8913 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
8914 		i40e_handle_reset_warning(pf, lock_acquired);
8915 
8916 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
8917 		/* Request a PF Reset
8918 		 *
8919 		 * Resets PF and reinitializes PFs VSI.
8920 		 */
8921 		i40e_prep_for_reset(pf);
8922 		i40e_reset_and_rebuild(pf, true, lock_acquired);
8923 		dev_info(&pf->pdev->dev,
8924 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
8925 			 "FW LLDP is disabled\n" :
8926 			 "FW LLDP is enabled\n");
8927 
8928 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
8929 		int v;
8930 
8931 		/* Find the VSI(s) that requested a re-init */
8932 		dev_info(&pf->pdev->dev,
8933 			 "VSI reinit requested\n");
8934 		for (v = 0; v < pf->num_alloc_vsi; v++) {
8935 			struct i40e_vsi *vsi = pf->vsi[v];
8936 
8937 			if (vsi != NULL &&
8938 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
8939 					       vsi->state))
8940 				i40e_vsi_reinit_locked(pf->vsi[v]);
8941 		}
8942 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
8943 		int v;
8944 
8945 		/* Find the VSI(s) that needs to be brought down */
8946 		dev_info(&pf->pdev->dev, "VSI down requested\n");
8947 		for (v = 0; v < pf->num_alloc_vsi; v++) {
8948 			struct i40e_vsi *vsi = pf->vsi[v];
8949 
8950 			if (vsi != NULL &&
8951 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
8952 					       vsi->state)) {
8953 				set_bit(__I40E_VSI_DOWN, vsi->state);
8954 				i40e_down(vsi);
8955 			}
8956 		}
8957 	} else {
8958 		dev_info(&pf->pdev->dev,
8959 			 "bad reset request 0x%08x\n", reset_flags);
8960 	}
8961 }
8962 
8963 #ifdef CONFIG_I40E_DCB
8964 /**
8965  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
8966  * @pf: board private structure
8967  * @old_cfg: current DCB config
8968  * @new_cfg: new DCB config
8969  **/
8970 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
8971 			    struct i40e_dcbx_config *old_cfg,
8972 			    struct i40e_dcbx_config *new_cfg)
8973 {
8974 	bool need_reconfig = false;
8975 
8976 	/* Check if ETS configuration has changed */
8977 	if (memcmp(&new_cfg->etscfg,
8978 		   &old_cfg->etscfg,
8979 		   sizeof(new_cfg->etscfg))) {
8980 		/* If Priority Table has changed reconfig is needed */
8981 		if (memcmp(&new_cfg->etscfg.prioritytable,
8982 			   &old_cfg->etscfg.prioritytable,
8983 			   sizeof(new_cfg->etscfg.prioritytable))) {
8984 			need_reconfig = true;
8985 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
8986 		}
8987 
8988 		if (memcmp(&new_cfg->etscfg.tcbwtable,
8989 			   &old_cfg->etscfg.tcbwtable,
8990 			   sizeof(new_cfg->etscfg.tcbwtable)))
8991 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
8992 
8993 		if (memcmp(&new_cfg->etscfg.tsatable,
8994 			   &old_cfg->etscfg.tsatable,
8995 			   sizeof(new_cfg->etscfg.tsatable)))
8996 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
8997 	}
8998 
8999 	/* Check if PFC configuration has changed */
9000 	if (memcmp(&new_cfg->pfc,
9001 		   &old_cfg->pfc,
9002 		   sizeof(new_cfg->pfc))) {
9003 		need_reconfig = true;
9004 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9005 	}
9006 
9007 	/* Check if APP Table has changed */
9008 	if (memcmp(&new_cfg->app,
9009 		   &old_cfg->app,
9010 		   sizeof(new_cfg->app))) {
9011 		need_reconfig = true;
9012 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9013 	}
9014 
9015 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9016 	return need_reconfig;
9017 }
9018 
9019 /**
9020  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9021  * @pf: board private structure
9022  * @e: event info posted on ARQ
9023  **/
9024 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9025 				  struct i40e_arq_event_info *e)
9026 {
9027 	struct i40e_aqc_lldp_get_mib *mib =
9028 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9029 	struct i40e_hw *hw = &pf->hw;
9030 	struct i40e_dcbx_config tmp_dcbx_cfg;
9031 	bool need_reconfig = false;
9032 	int ret = 0;
9033 	u8 type;
9034 
9035 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9036 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9037 	    (hw->phy.link_info.link_speed &
9038 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9039 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9040 		/* let firmware decide if the DCB should be disabled */
9041 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9042 
9043 	/* Not DCB capable or capability disabled */
9044 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9045 		return ret;
9046 
9047 	/* Ignore if event is not for Nearest Bridge */
9048 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9049 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9050 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9051 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9052 		return ret;
9053 
9054 	/* Check MIB Type and return if event for Remote MIB update */
9055 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9056 	dev_dbg(&pf->pdev->dev,
9057 		"LLDP event mib type %s\n", type ? "remote" : "local");
9058 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9059 		/* Update the remote cached instance and return */
9060 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9061 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9062 				&hw->remote_dcbx_config);
9063 		goto exit;
9064 	}
9065 
9066 	/* Store the old configuration */
9067 	tmp_dcbx_cfg = hw->local_dcbx_config;
9068 
9069 	/* Reset the old DCBx configuration data */
9070 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9071 	/* Get updated DCBX data from firmware */
9072 	ret = i40e_get_dcb_config(&pf->hw);
9073 	if (ret) {
9074 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9075 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9076 		    (hw->phy.link_info.link_speed &
9077 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9078 			dev_warn(&pf->pdev->dev,
9079 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9080 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9081 		} else {
9082 			dev_info(&pf->pdev->dev,
9083 				 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
9084 				 i40e_stat_str(&pf->hw, ret),
9085 				 i40e_aq_str(&pf->hw,
9086 					     pf->hw.aq.asq_last_status));
9087 		}
9088 		goto exit;
9089 	}
9090 
9091 	/* No change detected in DCBX configs */
9092 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9093 		    sizeof(tmp_dcbx_cfg))) {
9094 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9095 		goto exit;
9096 	}
9097 
9098 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9099 					       &hw->local_dcbx_config);
9100 
9101 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9102 
9103 	if (!need_reconfig)
9104 		goto exit;
9105 
9106 	/* Enable DCB tagging only when more than one TC */
9107 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9108 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9109 	else
9110 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9111 
9112 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9113 	/* Reconfiguration needed quiesce all VSIs */
9114 	i40e_pf_quiesce_all_vsi(pf);
9115 
9116 	/* Changes in configuration update VEB/VSI */
9117 	i40e_dcb_reconfigure(pf);
9118 
9119 	ret = i40e_resume_port_tx(pf);
9120 
9121 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9122 	/* In case of error no point in resuming VSIs */
9123 	if (ret)
9124 		goto exit;
9125 
9126 	/* Wait for the PF's queues to be disabled */
9127 	ret = i40e_pf_wait_queues_disabled(pf);
9128 	if (ret) {
9129 		/* Schedule PF reset to recover */
9130 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9131 		i40e_service_event_schedule(pf);
9132 	} else {
9133 		i40e_pf_unquiesce_all_vsi(pf);
9134 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9135 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9136 	}
9137 
9138 exit:
9139 	return ret;
9140 }
9141 #endif /* CONFIG_I40E_DCB */
9142 
9143 /**
9144  * i40e_do_reset_safe - Protected reset path for userland calls.
9145  * @pf: board private structure
9146  * @reset_flags: which reset is requested
9147  *
9148  **/
9149 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9150 {
9151 	rtnl_lock();
9152 	i40e_do_reset(pf, reset_flags, true);
9153 	rtnl_unlock();
9154 }
9155 
9156 /**
9157  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9158  * @pf: board private structure
9159  * @e: event info posted on ARQ
9160  *
9161  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9162  * and VF queues
9163  **/
9164 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9165 					   struct i40e_arq_event_info *e)
9166 {
9167 	struct i40e_aqc_lan_overflow *data =
9168 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9169 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9170 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9171 	struct i40e_hw *hw = &pf->hw;
9172 	struct i40e_vf *vf;
9173 	u16 vf_id;
9174 
9175 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9176 		queue, qtx_ctl);
9177 
9178 	/* Queue belongs to VF, find the VF and issue VF reset */
9179 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9180 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9181 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9182 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9183 		vf_id -= hw->func_caps.vf_base_id;
9184 		vf = &pf->vf[vf_id];
9185 		i40e_vc_notify_vf_reset(vf);
9186 		/* Allow VF to process pending reset notification */
9187 		msleep(20);
9188 		i40e_reset_vf(vf, false);
9189 	}
9190 }
9191 
9192 /**
9193  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9194  * @pf: board private structure
9195  **/
9196 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9197 {
9198 	u32 val, fcnt_prog;
9199 
9200 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9201 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9202 	return fcnt_prog;
9203 }
9204 
9205 /**
9206  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9207  * @pf: board private structure
9208  **/
9209 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9210 {
9211 	u32 val, fcnt_prog;
9212 
9213 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9214 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9215 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9216 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9217 	return fcnt_prog;
9218 }
9219 
9220 /**
9221  * i40e_get_global_fd_count - Get total FD filters programmed on device
9222  * @pf: board private structure
9223  **/
9224 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9225 {
9226 	u32 val, fcnt_prog;
9227 
9228 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9229 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9230 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9231 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9232 	return fcnt_prog;
9233 }
9234 
9235 /**
9236  * i40e_reenable_fdir_sb - Restore FDir SB capability
9237  * @pf: board private structure
9238  **/
9239 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9240 {
9241 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9242 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9243 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9244 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9245 }
9246 
9247 /**
9248  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9249  * @pf: board private structure
9250  **/
9251 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9252 {
9253 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9254 		/* ATR uses the same filtering logic as SB rules. It only
9255 		 * functions properly if the input set mask is at the default
9256 		 * settings. It is safe to restore the default input set
9257 		 * because there are no active TCPv4 filter rules.
9258 		 */
9259 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9260 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9261 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9262 
9263 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9264 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9265 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9266 	}
9267 }
9268 
9269 /**
9270  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9271  * @pf: board private structure
9272  * @filter: FDir filter to remove
9273  */
9274 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9275 				       struct i40e_fdir_filter *filter)
9276 {
9277 	/* Update counters */
9278 	pf->fdir_pf_active_filters--;
9279 	pf->fd_inv = 0;
9280 
9281 	switch (filter->flow_type) {
9282 	case TCP_V4_FLOW:
9283 		pf->fd_tcp4_filter_cnt--;
9284 		break;
9285 	case UDP_V4_FLOW:
9286 		pf->fd_udp4_filter_cnt--;
9287 		break;
9288 	case SCTP_V4_FLOW:
9289 		pf->fd_sctp4_filter_cnt--;
9290 		break;
9291 	case TCP_V6_FLOW:
9292 		pf->fd_tcp6_filter_cnt--;
9293 		break;
9294 	case UDP_V6_FLOW:
9295 		pf->fd_udp6_filter_cnt--;
9296 		break;
9297 	case SCTP_V6_FLOW:
9298 		pf->fd_udp6_filter_cnt--;
9299 		break;
9300 	case IP_USER_FLOW:
9301 		switch (filter->ipl4_proto) {
9302 		case IPPROTO_TCP:
9303 			pf->fd_tcp4_filter_cnt--;
9304 			break;
9305 		case IPPROTO_UDP:
9306 			pf->fd_udp4_filter_cnt--;
9307 			break;
9308 		case IPPROTO_SCTP:
9309 			pf->fd_sctp4_filter_cnt--;
9310 			break;
9311 		case IPPROTO_IP:
9312 			pf->fd_ip4_filter_cnt--;
9313 			break;
9314 		}
9315 		break;
9316 	case IPV6_USER_FLOW:
9317 		switch (filter->ipl4_proto) {
9318 		case IPPROTO_TCP:
9319 			pf->fd_tcp6_filter_cnt--;
9320 			break;
9321 		case IPPROTO_UDP:
9322 			pf->fd_udp6_filter_cnt--;
9323 			break;
9324 		case IPPROTO_SCTP:
9325 			pf->fd_sctp6_filter_cnt--;
9326 			break;
9327 		case IPPROTO_IP:
9328 			pf->fd_ip6_filter_cnt--;
9329 			break;
9330 		}
9331 		break;
9332 	}
9333 
9334 	/* Remove the filter from the list and free memory */
9335 	hlist_del(&filter->fdir_node);
9336 	kfree(filter);
9337 }
9338 
9339 /**
9340  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9341  * @pf: board private structure
9342  **/
9343 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9344 {
9345 	struct i40e_fdir_filter *filter;
9346 	u32 fcnt_prog, fcnt_avail;
9347 	struct hlist_node *node;
9348 
9349 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9350 		return;
9351 
9352 	/* Check if we have enough room to re-enable FDir SB capability. */
9353 	fcnt_prog = i40e_get_global_fd_count(pf);
9354 	fcnt_avail = pf->fdir_pf_filter_count;
9355 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9356 	    (pf->fd_add_err == 0) ||
9357 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9358 		i40e_reenable_fdir_sb(pf);
9359 
9360 	/* We should wait for even more space before re-enabling ATR.
9361 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9362 	 * rules active.
9363 	 */
9364 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9365 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9366 		i40e_reenable_fdir_atr(pf);
9367 
9368 	/* if hw had a problem adding a filter, delete it */
9369 	if (pf->fd_inv > 0) {
9370 		hlist_for_each_entry_safe(filter, node,
9371 					  &pf->fdir_filter_list, fdir_node)
9372 			if (filter->fd_id == pf->fd_inv)
9373 				i40e_delete_invalid_filter(pf, filter);
9374 	}
9375 }
9376 
9377 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9378 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9379 /**
9380  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9381  * @pf: board private structure
9382  **/
9383 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9384 {
9385 	unsigned long min_flush_time;
9386 	int flush_wait_retry = 50;
9387 	bool disable_atr = false;
9388 	int fd_room;
9389 	int reg;
9390 
9391 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9392 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9393 		return;
9394 
9395 	/* If the flush is happening too quick and we have mostly SB rules we
9396 	 * should not re-enable ATR for some time.
9397 	 */
9398 	min_flush_time = pf->fd_flush_timestamp +
9399 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9400 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9401 
9402 	if (!(time_after(jiffies, min_flush_time)) &&
9403 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9404 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9405 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9406 		disable_atr = true;
9407 	}
9408 
9409 	pf->fd_flush_timestamp = jiffies;
9410 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9411 	/* flush all filters */
9412 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9413 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9414 	i40e_flush(&pf->hw);
9415 	pf->fd_flush_cnt++;
9416 	pf->fd_add_err = 0;
9417 	do {
9418 		/* Check FD flush status every 5-6msec */
9419 		usleep_range(5000, 6000);
9420 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9421 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9422 			break;
9423 	} while (flush_wait_retry--);
9424 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9425 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9426 	} else {
9427 		/* replay sideband filters */
9428 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9429 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9430 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9431 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9432 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9433 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9434 	}
9435 }
9436 
9437 /**
9438  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9439  * @pf: board private structure
9440  **/
9441 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9442 {
9443 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9444 }
9445 
9446 /**
9447  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9448  * @pf: board private structure
9449  **/
9450 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9451 {
9452 
9453 	/* if interface is down do nothing */
9454 	if (test_bit(__I40E_DOWN, pf->state))
9455 		return;
9456 
9457 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9458 		i40e_fdir_flush_and_replay(pf);
9459 
9460 	i40e_fdir_check_and_reenable(pf);
9461 
9462 }
9463 
9464 /**
9465  * i40e_vsi_link_event - notify VSI of a link event
9466  * @vsi: vsi to be notified
9467  * @link_up: link up or down
9468  **/
9469 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9470 {
9471 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9472 		return;
9473 
9474 	switch (vsi->type) {
9475 	case I40E_VSI_MAIN:
9476 		if (!vsi->netdev || !vsi->netdev_registered)
9477 			break;
9478 
9479 		if (link_up) {
9480 			netif_carrier_on(vsi->netdev);
9481 			netif_tx_wake_all_queues(vsi->netdev);
9482 		} else {
9483 			netif_carrier_off(vsi->netdev);
9484 			netif_tx_stop_all_queues(vsi->netdev);
9485 		}
9486 		break;
9487 
9488 	case I40E_VSI_SRIOV:
9489 	case I40E_VSI_VMDQ2:
9490 	case I40E_VSI_CTRL:
9491 	case I40E_VSI_IWARP:
9492 	case I40E_VSI_MIRROR:
9493 	default:
9494 		/* there is no notification for other VSIs */
9495 		break;
9496 	}
9497 }
9498 
9499 /**
9500  * i40e_veb_link_event - notify elements on the veb of a link event
9501  * @veb: veb to be notified
9502  * @link_up: link up or down
9503  **/
9504 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9505 {
9506 	struct i40e_pf *pf;
9507 	int i;
9508 
9509 	if (!veb || !veb->pf)
9510 		return;
9511 	pf = veb->pf;
9512 
9513 	/* depth first... */
9514 	for (i = 0; i < I40E_MAX_VEB; i++)
9515 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9516 			i40e_veb_link_event(pf->veb[i], link_up);
9517 
9518 	/* ... now the local VSIs */
9519 	for (i = 0; i < pf->num_alloc_vsi; i++)
9520 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9521 			i40e_vsi_link_event(pf->vsi[i], link_up);
9522 }
9523 
9524 /**
9525  * i40e_link_event - Update netif_carrier status
9526  * @pf: board private structure
9527  **/
9528 static void i40e_link_event(struct i40e_pf *pf)
9529 {
9530 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9531 	u8 new_link_speed, old_link_speed;
9532 	i40e_status status;
9533 	bool new_link, old_link;
9534 #ifdef CONFIG_I40E_DCB
9535 	int err;
9536 #endif /* CONFIG_I40E_DCB */
9537 
9538 	/* set this to force the get_link_status call to refresh state */
9539 	pf->hw.phy.get_link_info = true;
9540 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9541 	status = i40e_get_link_status(&pf->hw, &new_link);
9542 
9543 	/* On success, disable temp link polling */
9544 	if (status == I40E_SUCCESS) {
9545 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9546 	} else {
9547 		/* Enable link polling temporarily until i40e_get_link_status
9548 		 * returns I40E_SUCCESS
9549 		 */
9550 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9551 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9552 			status);
9553 		return;
9554 	}
9555 
9556 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9557 	new_link_speed = pf->hw.phy.link_info.link_speed;
9558 
9559 	if (new_link == old_link &&
9560 	    new_link_speed == old_link_speed &&
9561 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9562 	     new_link == netif_carrier_ok(vsi->netdev)))
9563 		return;
9564 
9565 	i40e_print_link_message(vsi, new_link);
9566 
9567 	/* Notify the base of the switch tree connected to
9568 	 * the link.  Floating VEBs are not notified.
9569 	 */
9570 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9571 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9572 	else
9573 		i40e_vsi_link_event(vsi, new_link);
9574 
9575 	if (pf->vf)
9576 		i40e_vc_notify_link_state(pf);
9577 
9578 	if (pf->flags & I40E_FLAG_PTP)
9579 		i40e_ptp_set_increment(pf);
9580 #ifdef CONFIG_I40E_DCB
9581 	if (new_link == old_link)
9582 		return;
9583 	/* Not SW DCB so firmware will take care of default settings */
9584 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9585 		return;
9586 
9587 	/* We cover here only link down, as after link up in case of SW DCB
9588 	 * SW LLDP agent will take care of setting it up
9589 	 */
9590 	if (!new_link) {
9591 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9592 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9593 		err = i40e_dcb_sw_default_config(pf);
9594 		if (err) {
9595 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9596 				       I40E_FLAG_DCB_ENABLED);
9597 		} else {
9598 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9599 				       DCB_CAP_DCBX_VER_IEEE;
9600 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9601 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9602 		}
9603 	}
9604 #endif /* CONFIG_I40E_DCB */
9605 }
9606 
9607 /**
9608  * i40e_watchdog_subtask - periodic checks not using event driven response
9609  * @pf: board private structure
9610  **/
9611 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9612 {
9613 	int i;
9614 
9615 	/* if interface is down do nothing */
9616 	if (test_bit(__I40E_DOWN, pf->state) ||
9617 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9618 		return;
9619 
9620 	/* make sure we don't do these things too often */
9621 	if (time_before(jiffies, (pf->service_timer_previous +
9622 				  pf->service_timer_period)))
9623 		return;
9624 	pf->service_timer_previous = jiffies;
9625 
9626 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9627 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9628 		i40e_link_event(pf);
9629 
9630 	/* Update the stats for active netdevs so the network stack
9631 	 * can look at updated numbers whenever it cares to
9632 	 */
9633 	for (i = 0; i < pf->num_alloc_vsi; i++)
9634 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9635 			i40e_update_stats(pf->vsi[i]);
9636 
9637 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9638 		/* Update the stats for the active switching components */
9639 		for (i = 0; i < I40E_MAX_VEB; i++)
9640 			if (pf->veb[i])
9641 				i40e_update_veb_stats(pf->veb[i]);
9642 	}
9643 
9644 	i40e_ptp_rx_hang(pf);
9645 	i40e_ptp_tx_hang(pf);
9646 }
9647 
9648 /**
9649  * i40e_reset_subtask - Set up for resetting the device and driver
9650  * @pf: board private structure
9651  **/
9652 static void i40e_reset_subtask(struct i40e_pf *pf)
9653 {
9654 	u32 reset_flags = 0;
9655 
9656 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
9657 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
9658 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
9659 	}
9660 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
9661 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
9662 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9663 	}
9664 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
9665 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
9666 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
9667 	}
9668 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
9669 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
9670 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
9671 	}
9672 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
9673 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
9674 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
9675 	}
9676 
9677 	/* If there's a recovery already waiting, it takes
9678 	 * precedence before starting a new reset sequence.
9679 	 */
9680 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
9681 		i40e_prep_for_reset(pf);
9682 		i40e_reset(pf);
9683 		i40e_rebuild(pf, false, false);
9684 	}
9685 
9686 	/* If we're already down or resetting, just bail */
9687 	if (reset_flags &&
9688 	    !test_bit(__I40E_DOWN, pf->state) &&
9689 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
9690 		i40e_do_reset(pf, reset_flags, false);
9691 	}
9692 }
9693 
9694 /**
9695  * i40e_handle_link_event - Handle link event
9696  * @pf: board private structure
9697  * @e: event info posted on ARQ
9698  **/
9699 static void i40e_handle_link_event(struct i40e_pf *pf,
9700 				   struct i40e_arq_event_info *e)
9701 {
9702 	struct i40e_aqc_get_link_status *status =
9703 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
9704 
9705 	/* Do a new status request to re-enable LSE reporting
9706 	 * and load new status information into the hw struct
9707 	 * This completely ignores any state information
9708 	 * in the ARQ event info, instead choosing to always
9709 	 * issue the AQ update link status command.
9710 	 */
9711 	i40e_link_event(pf);
9712 
9713 	/* Check if module meets thermal requirements */
9714 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
9715 		dev_err(&pf->pdev->dev,
9716 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
9717 		dev_err(&pf->pdev->dev,
9718 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9719 	} else {
9720 		/* check for unqualified module, if link is down, suppress
9721 		 * the message if link was forced to be down.
9722 		 */
9723 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
9724 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
9725 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
9726 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
9727 			dev_err(&pf->pdev->dev,
9728 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
9729 			dev_err(&pf->pdev->dev,
9730 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9731 		}
9732 	}
9733 }
9734 
9735 /**
9736  * i40e_clean_adminq_subtask - Clean the AdminQ rings
9737  * @pf: board private structure
9738  **/
9739 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
9740 {
9741 	struct i40e_arq_event_info event;
9742 	struct i40e_hw *hw = &pf->hw;
9743 	u16 pending, i = 0;
9744 	i40e_status ret;
9745 	u16 opcode;
9746 	u32 oldval;
9747 	u32 val;
9748 
9749 	/* Do not run clean AQ when PF reset fails */
9750 	if (test_bit(__I40E_RESET_FAILED, pf->state))
9751 		return;
9752 
9753 	/* check for error indications */
9754 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
9755 	oldval = val;
9756 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
9757 		if (hw->debug_mask & I40E_DEBUG_AQ)
9758 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
9759 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
9760 	}
9761 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
9762 		if (hw->debug_mask & I40E_DEBUG_AQ)
9763 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
9764 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
9765 		pf->arq_overflows++;
9766 	}
9767 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
9768 		if (hw->debug_mask & I40E_DEBUG_AQ)
9769 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
9770 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
9771 	}
9772 	if (oldval != val)
9773 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
9774 
9775 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
9776 	oldval = val;
9777 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
9778 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9779 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
9780 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
9781 	}
9782 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
9783 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9784 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
9785 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
9786 	}
9787 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
9788 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9789 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
9790 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
9791 	}
9792 	if (oldval != val)
9793 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
9794 
9795 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
9796 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
9797 	if (!event.msg_buf)
9798 		return;
9799 
9800 	do {
9801 		ret = i40e_clean_arq_element(hw, &event, &pending);
9802 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
9803 			break;
9804 		else if (ret) {
9805 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
9806 			break;
9807 		}
9808 
9809 		opcode = le16_to_cpu(event.desc.opcode);
9810 		switch (opcode) {
9811 
9812 		case i40e_aqc_opc_get_link_status:
9813 			rtnl_lock();
9814 			i40e_handle_link_event(pf, &event);
9815 			rtnl_unlock();
9816 			break;
9817 		case i40e_aqc_opc_send_msg_to_pf:
9818 			ret = i40e_vc_process_vf_msg(pf,
9819 					le16_to_cpu(event.desc.retval),
9820 					le32_to_cpu(event.desc.cookie_high),
9821 					le32_to_cpu(event.desc.cookie_low),
9822 					event.msg_buf,
9823 					event.msg_len);
9824 			break;
9825 		case i40e_aqc_opc_lldp_update_mib:
9826 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
9827 #ifdef CONFIG_I40E_DCB
9828 			rtnl_lock();
9829 			i40e_handle_lldp_event(pf, &event);
9830 			rtnl_unlock();
9831 #endif /* CONFIG_I40E_DCB */
9832 			break;
9833 		case i40e_aqc_opc_event_lan_overflow:
9834 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
9835 			i40e_handle_lan_overflow_event(pf, &event);
9836 			break;
9837 		case i40e_aqc_opc_send_msg_to_peer:
9838 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
9839 			break;
9840 		case i40e_aqc_opc_nvm_erase:
9841 		case i40e_aqc_opc_nvm_update:
9842 		case i40e_aqc_opc_oem_post_update:
9843 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
9844 				   "ARQ NVM operation 0x%04x completed\n",
9845 				   opcode);
9846 			break;
9847 		default:
9848 			dev_info(&pf->pdev->dev,
9849 				 "ARQ: Unknown event 0x%04x ignored\n",
9850 				 opcode);
9851 			break;
9852 		}
9853 	} while (i++ < pf->adminq_work_limit);
9854 
9855 	if (i < pf->adminq_work_limit)
9856 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
9857 
9858 	/* re-enable Admin queue interrupt cause */
9859 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
9860 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
9861 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
9862 	i40e_flush(hw);
9863 
9864 	kfree(event.msg_buf);
9865 }
9866 
9867 /**
9868  * i40e_verify_eeprom - make sure eeprom is good to use
9869  * @pf: board private structure
9870  **/
9871 static void i40e_verify_eeprom(struct i40e_pf *pf)
9872 {
9873 	int err;
9874 
9875 	err = i40e_diag_eeprom_test(&pf->hw);
9876 	if (err) {
9877 		/* retry in case of garbage read */
9878 		err = i40e_diag_eeprom_test(&pf->hw);
9879 		if (err) {
9880 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
9881 				 err);
9882 			set_bit(__I40E_BAD_EEPROM, pf->state);
9883 		}
9884 	}
9885 
9886 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
9887 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
9888 		clear_bit(__I40E_BAD_EEPROM, pf->state);
9889 	}
9890 }
9891 
9892 /**
9893  * i40e_enable_pf_switch_lb
9894  * @pf: pointer to the PF structure
9895  *
9896  * enable switch loop back or die - no point in a return value
9897  **/
9898 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
9899 {
9900 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9901 	struct i40e_vsi_context ctxt;
9902 	int ret;
9903 
9904 	ctxt.seid = pf->main_vsi_seid;
9905 	ctxt.pf_num = pf->hw.pf_id;
9906 	ctxt.vf_num = 0;
9907 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9908 	if (ret) {
9909 		dev_info(&pf->pdev->dev,
9910 			 "couldn't get PF vsi config, err %s aq_err %s\n",
9911 			 i40e_stat_str(&pf->hw, ret),
9912 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9913 		return;
9914 	}
9915 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9916 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9917 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9918 
9919 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
9920 	if (ret) {
9921 		dev_info(&pf->pdev->dev,
9922 			 "update vsi switch failed, err %s aq_err %s\n",
9923 			 i40e_stat_str(&pf->hw, ret),
9924 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9925 	}
9926 }
9927 
9928 /**
9929  * i40e_disable_pf_switch_lb
9930  * @pf: pointer to the PF structure
9931  *
9932  * disable switch loop back or die - no point in a return value
9933  **/
9934 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
9935 {
9936 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9937 	struct i40e_vsi_context ctxt;
9938 	int ret;
9939 
9940 	ctxt.seid = pf->main_vsi_seid;
9941 	ctxt.pf_num = pf->hw.pf_id;
9942 	ctxt.vf_num = 0;
9943 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9944 	if (ret) {
9945 		dev_info(&pf->pdev->dev,
9946 			 "couldn't get PF vsi config, err %s aq_err %s\n",
9947 			 i40e_stat_str(&pf->hw, ret),
9948 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9949 		return;
9950 	}
9951 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9952 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9953 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9954 
9955 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
9956 	if (ret) {
9957 		dev_info(&pf->pdev->dev,
9958 			 "update vsi switch failed, err %s aq_err %s\n",
9959 			 i40e_stat_str(&pf->hw, ret),
9960 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9961 	}
9962 }
9963 
9964 /**
9965  * i40e_config_bridge_mode - Configure the HW bridge mode
9966  * @veb: pointer to the bridge instance
9967  *
9968  * Configure the loop back mode for the LAN VSI that is downlink to the
9969  * specified HW bridge instance. It is expected this function is called
9970  * when a new HW bridge is instantiated.
9971  **/
9972 static void i40e_config_bridge_mode(struct i40e_veb *veb)
9973 {
9974 	struct i40e_pf *pf = veb->pf;
9975 
9976 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
9977 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
9978 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
9979 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
9980 		i40e_disable_pf_switch_lb(pf);
9981 	else
9982 		i40e_enable_pf_switch_lb(pf);
9983 }
9984 
9985 /**
9986  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
9987  * @veb: pointer to the VEB instance
9988  *
9989  * This is a recursive function that first builds the attached VSIs then
9990  * recurses in to build the next layer of VEB.  We track the connections
9991  * through our own index numbers because the seid's from the HW could
9992  * change across the reset.
9993  **/
9994 static int i40e_reconstitute_veb(struct i40e_veb *veb)
9995 {
9996 	struct i40e_vsi *ctl_vsi = NULL;
9997 	struct i40e_pf *pf = veb->pf;
9998 	int v, veb_idx;
9999 	int ret;
10000 
10001 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10002 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10003 		if (pf->vsi[v] &&
10004 		    pf->vsi[v]->veb_idx == veb->idx &&
10005 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10006 			ctl_vsi = pf->vsi[v];
10007 			break;
10008 		}
10009 	}
10010 	if (!ctl_vsi) {
10011 		dev_info(&pf->pdev->dev,
10012 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10013 		ret = -ENOENT;
10014 		goto end_reconstitute;
10015 	}
10016 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10017 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10018 	ret = i40e_add_vsi(ctl_vsi);
10019 	if (ret) {
10020 		dev_info(&pf->pdev->dev,
10021 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10022 			 veb->idx, ret);
10023 		goto end_reconstitute;
10024 	}
10025 	i40e_vsi_reset_stats(ctl_vsi);
10026 
10027 	/* create the VEB in the switch and move the VSI onto the VEB */
10028 	ret = i40e_add_veb(veb, ctl_vsi);
10029 	if (ret)
10030 		goto end_reconstitute;
10031 
10032 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10033 		veb->bridge_mode = BRIDGE_MODE_VEB;
10034 	else
10035 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10036 	i40e_config_bridge_mode(veb);
10037 
10038 	/* create the remaining VSIs attached to this VEB */
10039 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10040 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10041 			continue;
10042 
10043 		if (pf->vsi[v]->veb_idx == veb->idx) {
10044 			struct i40e_vsi *vsi = pf->vsi[v];
10045 
10046 			vsi->uplink_seid = veb->seid;
10047 			ret = i40e_add_vsi(vsi);
10048 			if (ret) {
10049 				dev_info(&pf->pdev->dev,
10050 					 "rebuild of vsi_idx %d failed: %d\n",
10051 					 v, ret);
10052 				goto end_reconstitute;
10053 			}
10054 			i40e_vsi_reset_stats(vsi);
10055 		}
10056 	}
10057 
10058 	/* create any VEBs attached to this VEB - RECURSION */
10059 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10060 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10061 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10062 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10063 			if (ret)
10064 				break;
10065 		}
10066 	}
10067 
10068 end_reconstitute:
10069 	return ret;
10070 }
10071 
10072 /**
10073  * i40e_get_capabilities - get info about the HW
10074  * @pf: the PF struct
10075  * @list_type: AQ capability to be queried
10076  **/
10077 static int i40e_get_capabilities(struct i40e_pf *pf,
10078 				 enum i40e_admin_queue_opc list_type)
10079 {
10080 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10081 	u16 data_size;
10082 	int buf_len;
10083 	int err;
10084 
10085 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10086 	do {
10087 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10088 		if (!cap_buf)
10089 			return -ENOMEM;
10090 
10091 		/* this loads the data into the hw struct for us */
10092 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10093 						    &data_size, list_type,
10094 						    NULL);
10095 		/* data loaded, buffer no longer needed */
10096 		kfree(cap_buf);
10097 
10098 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10099 			/* retry with a larger buffer */
10100 			buf_len = data_size;
10101 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
10102 			dev_info(&pf->pdev->dev,
10103 				 "capability discovery failed, err %s aq_err %s\n",
10104 				 i40e_stat_str(&pf->hw, err),
10105 				 i40e_aq_str(&pf->hw,
10106 					     pf->hw.aq.asq_last_status));
10107 			return -ENODEV;
10108 		}
10109 	} while (err);
10110 
10111 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10112 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10113 			dev_info(&pf->pdev->dev,
10114 				 "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",
10115 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10116 				 pf->hw.func_caps.num_msix_vectors,
10117 				 pf->hw.func_caps.num_msix_vectors_vf,
10118 				 pf->hw.func_caps.fd_filters_guaranteed,
10119 				 pf->hw.func_caps.fd_filters_best_effort,
10120 				 pf->hw.func_caps.num_tx_qp,
10121 				 pf->hw.func_caps.num_vsis);
10122 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10123 			dev_info(&pf->pdev->dev,
10124 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10125 				 pf->hw.dev_caps.switch_mode,
10126 				 pf->hw.dev_caps.valid_functions);
10127 			dev_info(&pf->pdev->dev,
10128 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10129 				 pf->hw.dev_caps.sr_iov_1_1,
10130 				 pf->hw.dev_caps.num_vfs);
10131 			dev_info(&pf->pdev->dev,
10132 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10133 				 pf->hw.dev_caps.num_vsis,
10134 				 pf->hw.dev_caps.num_rx_qp,
10135 				 pf->hw.dev_caps.num_tx_qp);
10136 		}
10137 	}
10138 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10139 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10140 		       + pf->hw.func_caps.num_vfs)
10141 		if (pf->hw.revision_id == 0 &&
10142 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10143 			dev_info(&pf->pdev->dev,
10144 				 "got num_vsis %d, setting num_vsis to %d\n",
10145 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10146 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10147 		}
10148 	}
10149 	return 0;
10150 }
10151 
10152 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10153 
10154 /**
10155  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10156  * @pf: board private structure
10157  **/
10158 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10159 {
10160 	struct i40e_vsi *vsi;
10161 
10162 	/* quick workaround for an NVM issue that leaves a critical register
10163 	 * uninitialized
10164 	 */
10165 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10166 		static const u32 hkey[] = {
10167 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10168 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10169 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10170 			0x95b3a76d};
10171 		int i;
10172 
10173 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10174 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10175 	}
10176 
10177 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10178 		return;
10179 
10180 	/* find existing VSI and see if it needs configuring */
10181 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10182 
10183 	/* create a new VSI if none exists */
10184 	if (!vsi) {
10185 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10186 				     pf->vsi[pf->lan_vsi]->seid, 0);
10187 		if (!vsi) {
10188 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10189 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10190 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10191 			return;
10192 		}
10193 	}
10194 
10195 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10196 }
10197 
10198 /**
10199  * i40e_fdir_teardown - release the Flow Director resources
10200  * @pf: board private structure
10201  **/
10202 static void i40e_fdir_teardown(struct i40e_pf *pf)
10203 {
10204 	struct i40e_vsi *vsi;
10205 
10206 	i40e_fdir_filter_exit(pf);
10207 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10208 	if (vsi)
10209 		i40e_vsi_release(vsi);
10210 }
10211 
10212 /**
10213  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10214  * @vsi: PF main vsi
10215  * @seid: seid of main or channel VSIs
10216  *
10217  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10218  * existed before reset
10219  **/
10220 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10221 {
10222 	struct i40e_cloud_filter *cfilter;
10223 	struct i40e_pf *pf = vsi->back;
10224 	struct hlist_node *node;
10225 	i40e_status ret;
10226 
10227 	/* Add cloud filters back if they exist */
10228 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10229 				  cloud_node) {
10230 		if (cfilter->seid != seid)
10231 			continue;
10232 
10233 		if (cfilter->dst_port)
10234 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10235 								true);
10236 		else
10237 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10238 
10239 		if (ret) {
10240 			dev_dbg(&pf->pdev->dev,
10241 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
10242 				i40e_stat_str(&pf->hw, ret),
10243 				i40e_aq_str(&pf->hw,
10244 					    pf->hw.aq.asq_last_status));
10245 			return ret;
10246 		}
10247 	}
10248 	return 0;
10249 }
10250 
10251 /**
10252  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10253  * @vsi: PF main vsi
10254  *
10255  * Rebuilds channel VSIs if they existed before reset
10256  **/
10257 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10258 {
10259 	struct i40e_channel *ch, *ch_tmp;
10260 	i40e_status ret;
10261 
10262 	if (list_empty(&vsi->ch_list))
10263 		return 0;
10264 
10265 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10266 		if (!ch->initialized)
10267 			break;
10268 		/* Proceed with creation of channel (VMDq2) VSI */
10269 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10270 		if (ret) {
10271 			dev_info(&vsi->back->pdev->dev,
10272 				 "failed to rebuild channels using uplink_seid %u\n",
10273 				 vsi->uplink_seid);
10274 			return ret;
10275 		}
10276 		/* Reconfigure TX queues using QTX_CTL register */
10277 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10278 		if (ret) {
10279 			dev_info(&vsi->back->pdev->dev,
10280 				 "failed to configure TX rings for channel %u\n",
10281 				 ch->seid);
10282 			return ret;
10283 		}
10284 		/* update 'next_base_queue' */
10285 		vsi->next_base_queue = vsi->next_base_queue +
10286 							ch->num_queue_pairs;
10287 		if (ch->max_tx_rate) {
10288 			u64 credits = ch->max_tx_rate;
10289 
10290 			if (i40e_set_bw_limit(vsi, ch->seid,
10291 					      ch->max_tx_rate))
10292 				return -EINVAL;
10293 
10294 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10295 			dev_dbg(&vsi->back->pdev->dev,
10296 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10297 				ch->max_tx_rate,
10298 				credits,
10299 				ch->seid);
10300 		}
10301 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10302 		if (ret) {
10303 			dev_dbg(&vsi->back->pdev->dev,
10304 				"Failed to rebuild cloud filters for channel VSI %u\n",
10305 				ch->seid);
10306 			return ret;
10307 		}
10308 	}
10309 	return 0;
10310 }
10311 
10312 /**
10313  * i40e_prep_for_reset - prep for the core to reset
10314  * @pf: board private structure
10315  *
10316  * Close up the VFs and other things in prep for PF Reset.
10317   **/
10318 static void i40e_prep_for_reset(struct i40e_pf *pf)
10319 {
10320 	struct i40e_hw *hw = &pf->hw;
10321 	i40e_status ret = 0;
10322 	u32 v;
10323 
10324 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10325 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10326 		return;
10327 	if (i40e_check_asq_alive(&pf->hw))
10328 		i40e_vc_notify_reset(pf);
10329 
10330 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10331 
10332 	/* quiesce the VSIs and their queues that are not already DOWN */
10333 	i40e_pf_quiesce_all_vsi(pf);
10334 
10335 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10336 		if (pf->vsi[v])
10337 			pf->vsi[v]->seid = 0;
10338 	}
10339 
10340 	i40e_shutdown_adminq(&pf->hw);
10341 
10342 	/* call shutdown HMC */
10343 	if (hw->hmc.hmc_obj) {
10344 		ret = i40e_shutdown_lan_hmc(hw);
10345 		if (ret)
10346 			dev_warn(&pf->pdev->dev,
10347 				 "shutdown_lan_hmc failed: %d\n", ret);
10348 	}
10349 
10350 	/* Save the current PTP time so that we can restore the time after the
10351 	 * reset completes.
10352 	 */
10353 	i40e_ptp_save_hw_time(pf);
10354 }
10355 
10356 /**
10357  * i40e_send_version - update firmware with driver version
10358  * @pf: PF struct
10359  */
10360 static void i40e_send_version(struct i40e_pf *pf)
10361 {
10362 	struct i40e_driver_version dv;
10363 
10364 	dv.major_version = 0xff;
10365 	dv.minor_version = 0xff;
10366 	dv.build_version = 0xff;
10367 	dv.subbuild_version = 0;
10368 	strlcpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10369 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10370 }
10371 
10372 /**
10373  * i40e_get_oem_version - get OEM specific version information
10374  * @hw: pointer to the hardware structure
10375  **/
10376 static void i40e_get_oem_version(struct i40e_hw *hw)
10377 {
10378 	u16 block_offset = 0xffff;
10379 	u16 block_length = 0;
10380 	u16 capabilities = 0;
10381 	u16 gen_snap = 0;
10382 	u16 release = 0;
10383 
10384 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10385 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10386 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10387 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10388 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10389 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10390 #define I40E_NVM_OEM_LENGTH			3
10391 
10392 	/* Check if pointer to OEM version block is valid. */
10393 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10394 	if (block_offset == 0xffff)
10395 		return;
10396 
10397 	/* Check if OEM version block has correct length. */
10398 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10399 			   &block_length);
10400 	if (block_length < I40E_NVM_OEM_LENGTH)
10401 		return;
10402 
10403 	/* Check if OEM version format is as expected. */
10404 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10405 			   &capabilities);
10406 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10407 		return;
10408 
10409 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10410 			   &gen_snap);
10411 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10412 			   &release);
10413 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10414 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10415 }
10416 
10417 /**
10418  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10419  * @pf: board private structure
10420  **/
10421 static int i40e_reset(struct i40e_pf *pf)
10422 {
10423 	struct i40e_hw *hw = &pf->hw;
10424 	i40e_status ret;
10425 
10426 	ret = i40e_pf_reset(hw);
10427 	if (ret) {
10428 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10429 		set_bit(__I40E_RESET_FAILED, pf->state);
10430 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10431 	} else {
10432 		pf->pfr_count++;
10433 	}
10434 	return ret;
10435 }
10436 
10437 /**
10438  * i40e_rebuild - rebuild using a saved config
10439  * @pf: board private structure
10440  * @reinit: if the Main VSI needs to re-initialized.
10441  * @lock_acquired: indicates whether or not the lock has been acquired
10442  * before this function was called.
10443  **/
10444 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10445 {
10446 	int old_recovery_mode_bit = test_bit(__I40E_RECOVERY_MODE, pf->state);
10447 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10448 	struct i40e_hw *hw = &pf->hw;
10449 	i40e_status ret;
10450 	u32 val;
10451 	int v;
10452 
10453 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10454 	    i40e_check_recovery_mode(pf)) {
10455 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10456 	}
10457 
10458 	if (test_bit(__I40E_DOWN, pf->state) &&
10459 	    !test_bit(__I40E_RECOVERY_MODE, pf->state) &&
10460 	    !old_recovery_mode_bit)
10461 		goto clear_recovery;
10462 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10463 
10464 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10465 	ret = i40e_init_adminq(&pf->hw);
10466 	if (ret) {
10467 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10468 			 i40e_stat_str(&pf->hw, ret),
10469 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10470 		goto clear_recovery;
10471 	}
10472 	i40e_get_oem_version(&pf->hw);
10473 
10474 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10475 	    ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) ||
10476 	     hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) {
10477 		/* The following delay is necessary for 4.33 firmware and older
10478 		 * to recover after EMP reset. 200 ms should suffice but we
10479 		 * put here 300 ms to be sure that FW is ready to operate
10480 		 * after reset.
10481 		 */
10482 		mdelay(300);
10483 	}
10484 
10485 	/* re-verify the eeprom if we just had an EMP reset */
10486 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10487 		i40e_verify_eeprom(pf);
10488 
10489 	/* if we are going out of or into recovery mode we have to act
10490 	 * accordingly with regard to resources initialization
10491 	 * and deinitialization
10492 	 */
10493 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) ||
10494 	    old_recovery_mode_bit) {
10495 		if (i40e_get_capabilities(pf,
10496 					  i40e_aqc_opc_list_func_capabilities))
10497 			goto end_unlock;
10498 
10499 		if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10500 			/* we're staying in recovery mode so we'll reinitialize
10501 			 * misc vector here
10502 			 */
10503 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10504 				goto end_unlock;
10505 		} else {
10506 			if (!lock_acquired)
10507 				rtnl_lock();
10508 			/* we're going out of recovery mode so we'll free
10509 			 * the IRQ allocated specifically for recovery mode
10510 			 * and restore the interrupt scheme
10511 			 */
10512 			free_irq(pf->pdev->irq, pf);
10513 			i40e_clear_interrupt_scheme(pf);
10514 			if (i40e_restore_interrupt_scheme(pf))
10515 				goto end_unlock;
10516 		}
10517 
10518 		/* tell the firmware that we're starting */
10519 		i40e_send_version(pf);
10520 
10521 		/* bail out in case recovery mode was detected, as there is
10522 		 * no need for further configuration.
10523 		 */
10524 		goto end_unlock;
10525 	}
10526 
10527 	i40e_clear_pxe_mode(hw);
10528 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10529 	if (ret)
10530 		goto end_core_reset;
10531 
10532 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10533 				hw->func_caps.num_rx_qp, 0, 0);
10534 	if (ret) {
10535 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10536 		goto end_core_reset;
10537 	}
10538 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10539 	if (ret) {
10540 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10541 		goto end_core_reset;
10542 	}
10543 
10544 #ifdef CONFIG_I40E_DCB
10545 	/* Enable FW to write a default DCB config on link-up
10546 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10547 	 * is not supported with new link speed
10548 	 */
10549 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
10550 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10551 	} else {
10552 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10553 		    (hw->phy.link_info.link_speed &
10554 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10555 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10556 			dev_warn(&pf->pdev->dev,
10557 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10558 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10559 		} else {
10560 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10561 			ret = i40e_init_pf_dcb(pf);
10562 			if (ret) {
10563 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10564 					 ret);
10565 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10566 				/* Continue without DCB enabled */
10567 			}
10568 		}
10569 	}
10570 
10571 #endif /* CONFIG_I40E_DCB */
10572 	if (!lock_acquired)
10573 		rtnl_lock();
10574 	ret = i40e_setup_pf_switch(pf, reinit, true);
10575 	if (ret)
10576 		goto end_unlock;
10577 
10578 	/* The driver only wants link up/down and module qualification
10579 	 * reports from firmware.  Note the negative logic.
10580 	 */
10581 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10582 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10583 					 I40E_AQ_EVENT_MEDIA_NA |
10584 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10585 	if (ret)
10586 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10587 			 i40e_stat_str(&pf->hw, ret),
10588 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10589 
10590 	/* Rebuild the VSIs and VEBs that existed before reset.
10591 	 * They are still in our local switch element arrays, so only
10592 	 * need to rebuild the switch model in the HW.
10593 	 *
10594 	 * If there were VEBs but the reconstitution failed, we'll try
10595 	 * to recover minimal use by getting the basic PF VSI working.
10596 	 */
10597 	if (vsi->uplink_seid != pf->mac_seid) {
10598 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10599 		/* find the one VEB connected to the MAC, and find orphans */
10600 		for (v = 0; v < I40E_MAX_VEB; v++) {
10601 			if (!pf->veb[v])
10602 				continue;
10603 
10604 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10605 			    pf->veb[v]->uplink_seid == 0) {
10606 				ret = i40e_reconstitute_veb(pf->veb[v]);
10607 
10608 				if (!ret)
10609 					continue;
10610 
10611 				/* If Main VEB failed, we're in deep doodoo,
10612 				 * so give up rebuilding the switch and set up
10613 				 * for minimal rebuild of PF VSI.
10614 				 * If orphan failed, we'll report the error
10615 				 * but try to keep going.
10616 				 */
10617 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10618 					dev_info(&pf->pdev->dev,
10619 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10620 						 ret);
10621 					vsi->uplink_seid = pf->mac_seid;
10622 					break;
10623 				} else if (pf->veb[v]->uplink_seid == 0) {
10624 					dev_info(&pf->pdev->dev,
10625 						 "rebuild of orphan VEB failed: %d\n",
10626 						 ret);
10627 				}
10628 			}
10629 		}
10630 	}
10631 
10632 	if (vsi->uplink_seid == pf->mac_seid) {
10633 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10634 		/* no VEB, so rebuild only the Main VSI */
10635 		ret = i40e_add_vsi(vsi);
10636 		if (ret) {
10637 			dev_info(&pf->pdev->dev,
10638 				 "rebuild of Main VSI failed: %d\n", ret);
10639 			goto end_unlock;
10640 		}
10641 	}
10642 
10643 	if (vsi->mqprio_qopt.max_rate[0]) {
10644 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
10645 		u64 credits = 0;
10646 
10647 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
10648 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10649 		if (ret)
10650 			goto end_unlock;
10651 
10652 		credits = max_tx_rate;
10653 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10654 		dev_dbg(&vsi->back->pdev->dev,
10655 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10656 			max_tx_rate,
10657 			credits,
10658 			vsi->seid);
10659 	}
10660 
10661 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10662 	if (ret)
10663 		goto end_unlock;
10664 
10665 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
10666 	 * for this main VSI if they exist
10667 	 */
10668 	ret = i40e_rebuild_channels(vsi);
10669 	if (ret)
10670 		goto end_unlock;
10671 
10672 	/* Reconfigure hardware for allowing smaller MSS in the case
10673 	 * of TSO, so that we avoid the MDD being fired and causing
10674 	 * a reset in the case of small MSS+TSO.
10675 	 */
10676 #define I40E_REG_MSS          0x000E64DC
10677 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
10678 #define I40E_64BYTE_MSS       0x400000
10679 	val = rd32(hw, I40E_REG_MSS);
10680 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10681 		val &= ~I40E_REG_MSS_MIN_MASK;
10682 		val |= I40E_64BYTE_MSS;
10683 		wr32(hw, I40E_REG_MSS, val);
10684 	}
10685 
10686 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
10687 		msleep(75);
10688 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10689 		if (ret)
10690 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10691 				 i40e_stat_str(&pf->hw, ret),
10692 				 i40e_aq_str(&pf->hw,
10693 					     pf->hw.aq.asq_last_status));
10694 	}
10695 	/* reinit the misc interrupt */
10696 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10697 		ret = i40e_setup_misc_vector(pf);
10698 
10699 	/* Add a filter to drop all Flow control frames from any VSI from being
10700 	 * transmitted. By doing so we stop a malicious VF from sending out
10701 	 * PAUSE or PFC frames and potentially controlling traffic for other
10702 	 * PF/VF VSIs.
10703 	 * The FW can still send Flow control frames if enabled.
10704 	 */
10705 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
10706 						       pf->main_vsi_seid);
10707 
10708 	/* restart the VSIs that were rebuilt and running before the reset */
10709 	i40e_pf_unquiesce_all_vsi(pf);
10710 
10711 	/* Release the RTNL lock before we start resetting VFs */
10712 	if (!lock_acquired)
10713 		rtnl_unlock();
10714 
10715 	/* Restore promiscuous settings */
10716 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
10717 	if (ret)
10718 		dev_warn(&pf->pdev->dev,
10719 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
10720 			 pf->cur_promisc ? "on" : "off",
10721 			 i40e_stat_str(&pf->hw, ret),
10722 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10723 
10724 	i40e_reset_all_vfs(pf, true);
10725 
10726 	/* tell the firmware that we're starting */
10727 	i40e_send_version(pf);
10728 
10729 	/* We've already released the lock, so don't do it again */
10730 	goto end_core_reset;
10731 
10732 end_unlock:
10733 	if (!lock_acquired)
10734 		rtnl_unlock();
10735 end_core_reset:
10736 	clear_bit(__I40E_RESET_FAILED, pf->state);
10737 clear_recovery:
10738 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10739 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
10740 }
10741 
10742 /**
10743  * i40e_reset_and_rebuild - reset and rebuild using a saved config
10744  * @pf: board private structure
10745  * @reinit: if the Main VSI needs to re-initialized.
10746  * @lock_acquired: indicates whether or not the lock has been acquired
10747  * before this function was called.
10748  **/
10749 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
10750 				   bool lock_acquired)
10751 {
10752 	int ret;
10753 	/* Now we wait for GRST to settle out.
10754 	 * We don't have to delete the VEBs or VSIs from the hw switch
10755 	 * because the reset will make them disappear.
10756 	 */
10757 	ret = i40e_reset(pf);
10758 	if (!ret)
10759 		i40e_rebuild(pf, reinit, lock_acquired);
10760 }
10761 
10762 /**
10763  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
10764  * @pf: board private structure
10765  *
10766  * Close up the VFs and other things in prep for a Core Reset,
10767  * then get ready to rebuild the world.
10768  * @lock_acquired: indicates whether or not the lock has been acquired
10769  * before this function was called.
10770  **/
10771 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
10772 {
10773 	i40e_prep_for_reset(pf);
10774 	i40e_reset_and_rebuild(pf, false, lock_acquired);
10775 }
10776 
10777 /**
10778  * i40e_handle_mdd_event
10779  * @pf: pointer to the PF structure
10780  *
10781  * Called from the MDD irq handler to identify possibly malicious vfs
10782  **/
10783 static void i40e_handle_mdd_event(struct i40e_pf *pf)
10784 {
10785 	struct i40e_hw *hw = &pf->hw;
10786 	bool mdd_detected = false;
10787 	struct i40e_vf *vf;
10788 	u32 reg;
10789 	int i;
10790 
10791 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
10792 		return;
10793 
10794 	/* find what triggered the MDD event */
10795 	reg = rd32(hw, I40E_GL_MDET_TX);
10796 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
10797 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
10798 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
10799 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
10800 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
10801 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
10802 				I40E_GL_MDET_TX_EVENT_SHIFT;
10803 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
10804 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
10805 				pf->hw.func_caps.base_queue;
10806 		if (netif_msg_tx_err(pf))
10807 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
10808 				 event, queue, pf_num, vf_num);
10809 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
10810 		mdd_detected = true;
10811 	}
10812 	reg = rd32(hw, I40E_GL_MDET_RX);
10813 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
10814 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
10815 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
10816 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
10817 				I40E_GL_MDET_RX_EVENT_SHIFT;
10818 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
10819 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
10820 				pf->hw.func_caps.base_queue;
10821 		if (netif_msg_rx_err(pf))
10822 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
10823 				 event, queue, func);
10824 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
10825 		mdd_detected = true;
10826 	}
10827 
10828 	if (mdd_detected) {
10829 		reg = rd32(hw, I40E_PF_MDET_TX);
10830 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
10831 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
10832 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
10833 		}
10834 		reg = rd32(hw, I40E_PF_MDET_RX);
10835 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
10836 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
10837 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
10838 		}
10839 	}
10840 
10841 	/* see if one of the VFs needs its hand slapped */
10842 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
10843 		vf = &(pf->vf[i]);
10844 		reg = rd32(hw, I40E_VP_MDET_TX(i));
10845 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
10846 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
10847 			vf->num_mdd_events++;
10848 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
10849 				 i);
10850 			dev_info(&pf->pdev->dev,
10851 				 "Use PF Control I/F to re-enable the VF\n");
10852 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10853 		}
10854 
10855 		reg = rd32(hw, I40E_VP_MDET_RX(i));
10856 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
10857 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
10858 			vf->num_mdd_events++;
10859 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
10860 				 i);
10861 			dev_info(&pf->pdev->dev,
10862 				 "Use PF Control I/F to re-enable the VF\n");
10863 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10864 		}
10865 	}
10866 
10867 	/* re-enable mdd interrupt cause */
10868 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
10869 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
10870 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
10871 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
10872 	i40e_flush(hw);
10873 }
10874 
10875 /**
10876  * i40e_service_task - Run the driver's async subtasks
10877  * @work: pointer to work_struct containing our data
10878  **/
10879 static void i40e_service_task(struct work_struct *work)
10880 {
10881 	struct i40e_pf *pf = container_of(work,
10882 					  struct i40e_pf,
10883 					  service_task);
10884 	unsigned long start_time = jiffies;
10885 
10886 	/* don't bother with service tasks if a reset is in progress */
10887 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
10888 	    test_bit(__I40E_SUSPENDED, pf->state))
10889 		return;
10890 
10891 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
10892 		return;
10893 
10894 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10895 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
10896 		i40e_sync_filters_subtask(pf);
10897 		i40e_reset_subtask(pf);
10898 		i40e_handle_mdd_event(pf);
10899 		i40e_vc_process_vflr_event(pf);
10900 		i40e_watchdog_subtask(pf);
10901 		i40e_fdir_reinit_subtask(pf);
10902 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
10903 			/* Client subtask will reopen next time through. */
10904 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
10905 							   true);
10906 		} else {
10907 			i40e_client_subtask(pf);
10908 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
10909 					       pf->state))
10910 				i40e_notify_client_of_l2_param_changes(
10911 								pf->vsi[pf->lan_vsi]);
10912 		}
10913 		i40e_sync_filters_subtask(pf);
10914 	} else {
10915 		i40e_reset_subtask(pf);
10916 	}
10917 
10918 	i40e_clean_adminq_subtask(pf);
10919 
10920 	/* flush memory to make sure state is correct before next watchdog */
10921 	smp_mb__before_atomic();
10922 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
10923 
10924 	/* If the tasks have taken longer than one timer cycle or there
10925 	 * is more work to be done, reschedule the service task now
10926 	 * rather than wait for the timer to tick again.
10927 	 */
10928 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
10929 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
10930 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
10931 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
10932 		i40e_service_event_schedule(pf);
10933 }
10934 
10935 /**
10936  * i40e_service_timer - timer callback
10937  * @t: timer list pointer
10938  **/
10939 static void i40e_service_timer(struct timer_list *t)
10940 {
10941 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
10942 
10943 	mod_timer(&pf->service_timer,
10944 		  round_jiffies(jiffies + pf->service_timer_period));
10945 	i40e_service_event_schedule(pf);
10946 }
10947 
10948 /**
10949  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
10950  * @vsi: the VSI being configured
10951  **/
10952 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
10953 {
10954 	struct i40e_pf *pf = vsi->back;
10955 
10956 	switch (vsi->type) {
10957 	case I40E_VSI_MAIN:
10958 		vsi->alloc_queue_pairs = pf->num_lan_qps;
10959 		if (!vsi->num_tx_desc)
10960 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10961 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10962 		if (!vsi->num_rx_desc)
10963 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10964 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10965 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10966 			vsi->num_q_vectors = pf->num_lan_msix;
10967 		else
10968 			vsi->num_q_vectors = 1;
10969 
10970 		break;
10971 
10972 	case I40E_VSI_FDIR:
10973 		vsi->alloc_queue_pairs = 1;
10974 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
10975 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
10976 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
10977 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
10978 		vsi->num_q_vectors = pf->num_fdsb_msix;
10979 		break;
10980 
10981 	case I40E_VSI_VMDQ2:
10982 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
10983 		if (!vsi->num_tx_desc)
10984 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10985 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10986 		if (!vsi->num_rx_desc)
10987 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10988 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10989 		vsi->num_q_vectors = pf->num_vmdq_msix;
10990 		break;
10991 
10992 	case I40E_VSI_SRIOV:
10993 		vsi->alloc_queue_pairs = pf->num_vf_qps;
10994 		if (!vsi->num_tx_desc)
10995 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10996 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10997 		if (!vsi->num_rx_desc)
10998 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10999 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11000 		break;
11001 
11002 	default:
11003 		WARN_ON(1);
11004 		return -ENODATA;
11005 	}
11006 
11007 	if (is_kdump_kernel()) {
11008 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11009 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11010 	}
11011 
11012 	return 0;
11013 }
11014 
11015 /**
11016  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11017  * @vsi: VSI pointer
11018  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11019  *
11020  * On error: returns error code (negative)
11021  * On success: returns 0
11022  **/
11023 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11024 {
11025 	struct i40e_ring **next_rings;
11026 	int size;
11027 	int ret = 0;
11028 
11029 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11030 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11031 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11032 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11033 	if (!vsi->tx_rings)
11034 		return -ENOMEM;
11035 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11036 	if (i40e_enabled_xdp_vsi(vsi)) {
11037 		vsi->xdp_rings = next_rings;
11038 		next_rings += vsi->alloc_queue_pairs;
11039 	}
11040 	vsi->rx_rings = next_rings;
11041 
11042 	if (alloc_qvectors) {
11043 		/* allocate memory for q_vector pointers */
11044 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11045 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11046 		if (!vsi->q_vectors) {
11047 			ret = -ENOMEM;
11048 			goto err_vectors;
11049 		}
11050 	}
11051 	return ret;
11052 
11053 err_vectors:
11054 	kfree(vsi->tx_rings);
11055 	return ret;
11056 }
11057 
11058 /**
11059  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11060  * @pf: board private structure
11061  * @type: type of VSI
11062  *
11063  * On error: returns error code (negative)
11064  * On success: returns vsi index in PF (positive)
11065  **/
11066 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11067 {
11068 	int ret = -ENODEV;
11069 	struct i40e_vsi *vsi;
11070 	int vsi_idx;
11071 	int i;
11072 
11073 	/* Need to protect the allocation of the VSIs at the PF level */
11074 	mutex_lock(&pf->switch_mutex);
11075 
11076 	/* VSI list may be fragmented if VSI creation/destruction has
11077 	 * been happening.  We can afford to do a quick scan to look
11078 	 * for any free VSIs in the list.
11079 	 *
11080 	 * find next empty vsi slot, looping back around if necessary
11081 	 */
11082 	i = pf->next_vsi;
11083 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11084 		i++;
11085 	if (i >= pf->num_alloc_vsi) {
11086 		i = 0;
11087 		while (i < pf->next_vsi && pf->vsi[i])
11088 			i++;
11089 	}
11090 
11091 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11092 		vsi_idx = i;             /* Found one! */
11093 	} else {
11094 		ret = -ENODEV;
11095 		goto unlock_pf;  /* out of VSI slots! */
11096 	}
11097 	pf->next_vsi = ++i;
11098 
11099 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11100 	if (!vsi) {
11101 		ret = -ENOMEM;
11102 		goto unlock_pf;
11103 	}
11104 	vsi->type = type;
11105 	vsi->back = pf;
11106 	set_bit(__I40E_VSI_DOWN, vsi->state);
11107 	vsi->flags = 0;
11108 	vsi->idx = vsi_idx;
11109 	vsi->int_rate_limit = 0;
11110 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11111 				pf->rss_table_size : 64;
11112 	vsi->netdev_registered = false;
11113 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11114 	hash_init(vsi->mac_filter_hash);
11115 	vsi->irqs_ready = false;
11116 
11117 	if (type == I40E_VSI_MAIN) {
11118 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11119 		if (!vsi->af_xdp_zc_qps)
11120 			goto err_rings;
11121 	}
11122 
11123 	ret = i40e_set_num_rings_in_vsi(vsi);
11124 	if (ret)
11125 		goto err_rings;
11126 
11127 	ret = i40e_vsi_alloc_arrays(vsi, true);
11128 	if (ret)
11129 		goto err_rings;
11130 
11131 	/* Setup default MSIX irq handler for VSI */
11132 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11133 
11134 	/* Initialize VSI lock */
11135 	spin_lock_init(&vsi->mac_filter_hash_lock);
11136 	pf->vsi[vsi_idx] = vsi;
11137 	ret = vsi_idx;
11138 	goto unlock_pf;
11139 
11140 err_rings:
11141 	bitmap_free(vsi->af_xdp_zc_qps);
11142 	pf->next_vsi = i - 1;
11143 	kfree(vsi);
11144 unlock_pf:
11145 	mutex_unlock(&pf->switch_mutex);
11146 	return ret;
11147 }
11148 
11149 /**
11150  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11151  * @vsi: VSI pointer
11152  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11153  *
11154  * On error: returns error code (negative)
11155  * On success: returns 0
11156  **/
11157 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11158 {
11159 	/* free the ring and vector containers */
11160 	if (free_qvectors) {
11161 		kfree(vsi->q_vectors);
11162 		vsi->q_vectors = NULL;
11163 	}
11164 	kfree(vsi->tx_rings);
11165 	vsi->tx_rings = NULL;
11166 	vsi->rx_rings = NULL;
11167 	vsi->xdp_rings = NULL;
11168 }
11169 
11170 /**
11171  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11172  * and lookup table
11173  * @vsi: Pointer to VSI structure
11174  */
11175 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11176 {
11177 	if (!vsi)
11178 		return;
11179 
11180 	kfree(vsi->rss_hkey_user);
11181 	vsi->rss_hkey_user = NULL;
11182 
11183 	kfree(vsi->rss_lut_user);
11184 	vsi->rss_lut_user = NULL;
11185 }
11186 
11187 /**
11188  * i40e_vsi_clear - Deallocate the VSI provided
11189  * @vsi: the VSI being un-configured
11190  **/
11191 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11192 {
11193 	struct i40e_pf *pf;
11194 
11195 	if (!vsi)
11196 		return 0;
11197 
11198 	if (!vsi->back)
11199 		goto free_vsi;
11200 	pf = vsi->back;
11201 
11202 	mutex_lock(&pf->switch_mutex);
11203 	if (!pf->vsi[vsi->idx]) {
11204 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11205 			vsi->idx, vsi->idx, vsi->type);
11206 		goto unlock_vsi;
11207 	}
11208 
11209 	if (pf->vsi[vsi->idx] != vsi) {
11210 		dev_err(&pf->pdev->dev,
11211 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11212 			pf->vsi[vsi->idx]->idx,
11213 			pf->vsi[vsi->idx]->type,
11214 			vsi->idx, vsi->type);
11215 		goto unlock_vsi;
11216 	}
11217 
11218 	/* updates the PF for this cleared vsi */
11219 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11220 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11221 
11222 	bitmap_free(vsi->af_xdp_zc_qps);
11223 	i40e_vsi_free_arrays(vsi, true);
11224 	i40e_clear_rss_config_user(vsi);
11225 
11226 	pf->vsi[vsi->idx] = NULL;
11227 	if (vsi->idx < pf->next_vsi)
11228 		pf->next_vsi = vsi->idx;
11229 
11230 unlock_vsi:
11231 	mutex_unlock(&pf->switch_mutex);
11232 free_vsi:
11233 	kfree(vsi);
11234 
11235 	return 0;
11236 }
11237 
11238 /**
11239  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11240  * @vsi: the VSI being cleaned
11241  **/
11242 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11243 {
11244 	int i;
11245 
11246 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11247 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11248 			kfree_rcu(vsi->tx_rings[i], rcu);
11249 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11250 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11251 			if (vsi->xdp_rings)
11252 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11253 		}
11254 	}
11255 }
11256 
11257 /**
11258  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11259  * @vsi: the VSI being configured
11260  **/
11261 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11262 {
11263 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11264 	struct i40e_pf *pf = vsi->back;
11265 	struct i40e_ring *ring;
11266 
11267 	/* Set basic values in the rings to be used later during open() */
11268 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11269 		/* allocate space for both Tx and Rx in one shot */
11270 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11271 		if (!ring)
11272 			goto err_out;
11273 
11274 		ring->queue_index = i;
11275 		ring->reg_idx = vsi->base_queue + i;
11276 		ring->ring_active = false;
11277 		ring->vsi = vsi;
11278 		ring->netdev = vsi->netdev;
11279 		ring->dev = &pf->pdev->dev;
11280 		ring->count = vsi->num_tx_desc;
11281 		ring->size = 0;
11282 		ring->dcb_tc = 0;
11283 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11284 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11285 		ring->itr_setting = pf->tx_itr_default;
11286 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11287 
11288 		if (!i40e_enabled_xdp_vsi(vsi))
11289 			goto setup_rx;
11290 
11291 		ring->queue_index = vsi->alloc_queue_pairs + i;
11292 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11293 		ring->ring_active = false;
11294 		ring->vsi = vsi;
11295 		ring->netdev = NULL;
11296 		ring->dev = &pf->pdev->dev;
11297 		ring->count = vsi->num_tx_desc;
11298 		ring->size = 0;
11299 		ring->dcb_tc = 0;
11300 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11301 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11302 		set_ring_xdp(ring);
11303 		ring->itr_setting = pf->tx_itr_default;
11304 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11305 
11306 setup_rx:
11307 		ring->queue_index = i;
11308 		ring->reg_idx = vsi->base_queue + i;
11309 		ring->ring_active = false;
11310 		ring->vsi = vsi;
11311 		ring->netdev = vsi->netdev;
11312 		ring->dev = &pf->pdev->dev;
11313 		ring->count = vsi->num_rx_desc;
11314 		ring->size = 0;
11315 		ring->dcb_tc = 0;
11316 		ring->itr_setting = pf->rx_itr_default;
11317 		WRITE_ONCE(vsi->rx_rings[i], ring);
11318 	}
11319 
11320 	return 0;
11321 
11322 err_out:
11323 	i40e_vsi_clear_rings(vsi);
11324 	return -ENOMEM;
11325 }
11326 
11327 /**
11328  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11329  * @pf: board private structure
11330  * @vectors: the number of MSI-X vectors to request
11331  *
11332  * Returns the number of vectors reserved, or error
11333  **/
11334 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11335 {
11336 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11337 					I40E_MIN_MSIX, vectors);
11338 	if (vectors < 0) {
11339 		dev_info(&pf->pdev->dev,
11340 			 "MSI-X vector reservation failed: %d\n", vectors);
11341 		vectors = 0;
11342 	}
11343 
11344 	return vectors;
11345 }
11346 
11347 /**
11348  * i40e_init_msix - Setup the MSIX capability
11349  * @pf: board private structure
11350  *
11351  * Work with the OS to set up the MSIX vectors needed.
11352  *
11353  * Returns the number of vectors reserved or negative on failure
11354  **/
11355 static int i40e_init_msix(struct i40e_pf *pf)
11356 {
11357 	struct i40e_hw *hw = &pf->hw;
11358 	int cpus, extra_vectors;
11359 	int vectors_left;
11360 	int v_budget, i;
11361 	int v_actual;
11362 	int iwarp_requested = 0;
11363 
11364 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11365 		return -ENODEV;
11366 
11367 	/* The number of vectors we'll request will be comprised of:
11368 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11369 	 *   - The number of LAN queue pairs
11370 	 *	- Queues being used for RSS.
11371 	 *		We don't need as many as max_rss_size vectors.
11372 	 *		use rss_size instead in the calculation since that
11373 	 *		is governed by number of cpus in the system.
11374 	 *	- assumes symmetric Tx/Rx pairing
11375 	 *   - The number of VMDq pairs
11376 	 *   - The CPU count within the NUMA node if iWARP is enabled
11377 	 * Once we count this up, try the request.
11378 	 *
11379 	 * If we can't get what we want, we'll simplify to nearly nothing
11380 	 * and try again.  If that still fails, we punt.
11381 	 */
11382 	vectors_left = hw->func_caps.num_msix_vectors;
11383 	v_budget = 0;
11384 
11385 	/* reserve one vector for miscellaneous handler */
11386 	if (vectors_left) {
11387 		v_budget++;
11388 		vectors_left--;
11389 	}
11390 
11391 	/* reserve some vectors for the main PF traffic queues. Initially we
11392 	 * only reserve at most 50% of the available vectors, in the case that
11393 	 * the number of online CPUs is large. This ensures that we can enable
11394 	 * extra features as well. Once we've enabled the other features, we
11395 	 * will use any remaining vectors to reach as close as we can to the
11396 	 * number of online CPUs.
11397 	 */
11398 	cpus = num_online_cpus();
11399 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11400 	vectors_left -= pf->num_lan_msix;
11401 
11402 	/* reserve one vector for sideband flow director */
11403 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11404 		if (vectors_left) {
11405 			pf->num_fdsb_msix = 1;
11406 			v_budget++;
11407 			vectors_left--;
11408 		} else {
11409 			pf->num_fdsb_msix = 0;
11410 		}
11411 	}
11412 
11413 	/* can we reserve enough for iWARP? */
11414 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11415 		iwarp_requested = pf->num_iwarp_msix;
11416 
11417 		if (!vectors_left)
11418 			pf->num_iwarp_msix = 0;
11419 		else if (vectors_left < pf->num_iwarp_msix)
11420 			pf->num_iwarp_msix = 1;
11421 		v_budget += pf->num_iwarp_msix;
11422 		vectors_left -= pf->num_iwarp_msix;
11423 	}
11424 
11425 	/* any vectors left over go for VMDq support */
11426 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11427 		if (!vectors_left) {
11428 			pf->num_vmdq_msix = 0;
11429 			pf->num_vmdq_qps = 0;
11430 		} else {
11431 			int vmdq_vecs_wanted =
11432 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11433 			int vmdq_vecs =
11434 				min_t(int, vectors_left, vmdq_vecs_wanted);
11435 
11436 			/* if we're short on vectors for what's desired, we limit
11437 			 * the queues per vmdq.  If this is still more than are
11438 			 * available, the user will need to change the number of
11439 			 * queues/vectors used by the PF later with the ethtool
11440 			 * channels command
11441 			 */
11442 			if (vectors_left < vmdq_vecs_wanted) {
11443 				pf->num_vmdq_qps = 1;
11444 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11445 				vmdq_vecs = min_t(int,
11446 						  vectors_left,
11447 						  vmdq_vecs_wanted);
11448 			}
11449 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11450 
11451 			v_budget += vmdq_vecs;
11452 			vectors_left -= vmdq_vecs;
11453 		}
11454 	}
11455 
11456 	/* On systems with a large number of SMP cores, we previously limited
11457 	 * the number of vectors for num_lan_msix to be at most 50% of the
11458 	 * available vectors, to allow for other features. Now, we add back
11459 	 * the remaining vectors. However, we ensure that the total
11460 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11461 	 * calculate the number of vectors we can add without going over the
11462 	 * cap of CPUs. For systems with a small number of CPUs this will be
11463 	 * zero.
11464 	 */
11465 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11466 	pf->num_lan_msix += extra_vectors;
11467 	vectors_left -= extra_vectors;
11468 
11469 	WARN(vectors_left < 0,
11470 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11471 
11472 	v_budget += pf->num_lan_msix;
11473 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11474 				   GFP_KERNEL);
11475 	if (!pf->msix_entries)
11476 		return -ENOMEM;
11477 
11478 	for (i = 0; i < v_budget; i++)
11479 		pf->msix_entries[i].entry = i;
11480 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11481 
11482 	if (v_actual < I40E_MIN_MSIX) {
11483 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11484 		kfree(pf->msix_entries);
11485 		pf->msix_entries = NULL;
11486 		pci_disable_msix(pf->pdev);
11487 		return -ENODEV;
11488 
11489 	} else if (v_actual == I40E_MIN_MSIX) {
11490 		/* Adjust for minimal MSIX use */
11491 		pf->num_vmdq_vsis = 0;
11492 		pf->num_vmdq_qps = 0;
11493 		pf->num_lan_qps = 1;
11494 		pf->num_lan_msix = 1;
11495 
11496 	} else if (v_actual != v_budget) {
11497 		/* If we have limited resources, we will start with no vectors
11498 		 * for the special features and then allocate vectors to some
11499 		 * of these features based on the policy and at the end disable
11500 		 * the features that did not get any vectors.
11501 		 */
11502 		int vec;
11503 
11504 		dev_info(&pf->pdev->dev,
11505 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11506 			 v_actual, v_budget);
11507 		/* reserve the misc vector */
11508 		vec = v_actual - 1;
11509 
11510 		/* Scale vector usage down */
11511 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11512 		pf->num_vmdq_vsis = 1;
11513 		pf->num_vmdq_qps = 1;
11514 
11515 		/* partition out the remaining vectors */
11516 		switch (vec) {
11517 		case 2:
11518 			pf->num_lan_msix = 1;
11519 			break;
11520 		case 3:
11521 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11522 				pf->num_lan_msix = 1;
11523 				pf->num_iwarp_msix = 1;
11524 			} else {
11525 				pf->num_lan_msix = 2;
11526 			}
11527 			break;
11528 		default:
11529 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11530 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11531 						 iwarp_requested);
11532 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11533 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11534 			} else {
11535 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11536 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11537 			}
11538 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11539 				pf->num_fdsb_msix = 1;
11540 				vec--;
11541 			}
11542 			pf->num_lan_msix = min_t(int,
11543 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11544 							      pf->num_lan_msix);
11545 			pf->num_lan_qps = pf->num_lan_msix;
11546 			break;
11547 		}
11548 	}
11549 
11550 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11551 	    (pf->num_fdsb_msix == 0)) {
11552 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11553 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11554 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11555 	}
11556 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11557 	    (pf->num_vmdq_msix == 0)) {
11558 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11559 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11560 	}
11561 
11562 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11563 	    (pf->num_iwarp_msix == 0)) {
11564 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11565 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11566 	}
11567 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11568 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11569 		   pf->num_lan_msix,
11570 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11571 		   pf->num_fdsb_msix,
11572 		   pf->num_iwarp_msix);
11573 
11574 	return v_actual;
11575 }
11576 
11577 /**
11578  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11579  * @vsi: the VSI being configured
11580  * @v_idx: index of the vector in the vsi struct
11581  *
11582  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11583  **/
11584 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11585 {
11586 	struct i40e_q_vector *q_vector;
11587 
11588 	/* allocate q_vector */
11589 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11590 	if (!q_vector)
11591 		return -ENOMEM;
11592 
11593 	q_vector->vsi = vsi;
11594 	q_vector->v_idx = v_idx;
11595 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11596 
11597 	if (vsi->netdev)
11598 		netif_napi_add(vsi->netdev, &q_vector->napi,
11599 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
11600 
11601 	/* tie q_vector and vsi together */
11602 	vsi->q_vectors[v_idx] = q_vector;
11603 
11604 	return 0;
11605 }
11606 
11607 /**
11608  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11609  * @vsi: the VSI being configured
11610  *
11611  * We allocate one q_vector per queue interrupt.  If allocation fails we
11612  * return -ENOMEM.
11613  **/
11614 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11615 {
11616 	struct i40e_pf *pf = vsi->back;
11617 	int err, v_idx, num_q_vectors;
11618 
11619 	/* if not MSIX, give the one vector only to the LAN VSI */
11620 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11621 		num_q_vectors = vsi->num_q_vectors;
11622 	else if (vsi == pf->vsi[pf->lan_vsi])
11623 		num_q_vectors = 1;
11624 	else
11625 		return -EINVAL;
11626 
11627 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11628 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11629 		if (err)
11630 			goto err_out;
11631 	}
11632 
11633 	return 0;
11634 
11635 err_out:
11636 	while (v_idx--)
11637 		i40e_free_q_vector(vsi, v_idx);
11638 
11639 	return err;
11640 }
11641 
11642 /**
11643  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11644  * @pf: board private structure to initialize
11645  **/
11646 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11647 {
11648 	int vectors = 0;
11649 	ssize_t size;
11650 
11651 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11652 		vectors = i40e_init_msix(pf);
11653 		if (vectors < 0) {
11654 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11655 				       I40E_FLAG_IWARP_ENABLED	|
11656 				       I40E_FLAG_RSS_ENABLED	|
11657 				       I40E_FLAG_DCB_CAPABLE	|
11658 				       I40E_FLAG_DCB_ENABLED	|
11659 				       I40E_FLAG_SRIOV_ENABLED	|
11660 				       I40E_FLAG_FD_SB_ENABLED	|
11661 				       I40E_FLAG_FD_ATR_ENABLED	|
11662 				       I40E_FLAG_VMDQ_ENABLED);
11663 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11664 
11665 			/* rework the queue expectations without MSIX */
11666 			i40e_determine_queue_usage(pf);
11667 		}
11668 	}
11669 
11670 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11671 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
11672 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
11673 		vectors = pci_enable_msi(pf->pdev);
11674 		if (vectors < 0) {
11675 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
11676 				 vectors);
11677 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
11678 		}
11679 		vectors = 1;  /* one MSI or Legacy vector */
11680 	}
11681 
11682 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
11683 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
11684 
11685 	/* set up vector assignment tracking */
11686 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
11687 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
11688 	if (!pf->irq_pile)
11689 		return -ENOMEM;
11690 
11691 	pf->irq_pile->num_entries = vectors;
11692 	pf->irq_pile->search_hint = 0;
11693 
11694 	/* track first vector for misc interrupts, ignore return */
11695 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
11696 
11697 	return 0;
11698 }
11699 
11700 /**
11701  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
11702  * @pf: private board data structure
11703  *
11704  * Restore the interrupt scheme that was cleared when we suspended the
11705  * device. This should be called during resume to re-allocate the q_vectors
11706  * and reacquire IRQs.
11707  */
11708 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
11709 {
11710 	int err, i;
11711 
11712 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
11713 	 * scheme. We need to re-enabled them here in order to attempt to
11714 	 * re-acquire the MSI or MSI-X vectors
11715 	 */
11716 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
11717 
11718 	err = i40e_init_interrupt_scheme(pf);
11719 	if (err)
11720 		return err;
11721 
11722 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
11723 	 * rings together again.
11724 	 */
11725 	for (i = 0; i < pf->num_alloc_vsi; i++) {
11726 		if (pf->vsi[i]) {
11727 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
11728 			if (err)
11729 				goto err_unwind;
11730 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
11731 		}
11732 	}
11733 
11734 	err = i40e_setup_misc_vector(pf);
11735 	if (err)
11736 		goto err_unwind;
11737 
11738 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
11739 		i40e_client_update_msix_info(pf);
11740 
11741 	return 0;
11742 
11743 err_unwind:
11744 	while (i--) {
11745 		if (pf->vsi[i])
11746 			i40e_vsi_free_q_vectors(pf->vsi[i]);
11747 	}
11748 
11749 	return err;
11750 }
11751 
11752 /**
11753  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
11754  * non queue events in recovery mode
11755  * @pf: board private structure
11756  *
11757  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
11758  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
11759  * This is handled differently than in recovery mode since no Tx/Rx resources
11760  * are being allocated.
11761  **/
11762 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
11763 {
11764 	int err;
11765 
11766 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11767 		err = i40e_setup_misc_vector(pf);
11768 
11769 		if (err) {
11770 			dev_info(&pf->pdev->dev,
11771 				 "MSI-X misc vector request failed, error %d\n",
11772 				 err);
11773 			return err;
11774 		}
11775 	} else {
11776 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
11777 
11778 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
11779 				  pf->int_name, pf);
11780 
11781 		if (err) {
11782 			dev_info(&pf->pdev->dev,
11783 				 "MSI/legacy misc vector request failed, error %d\n",
11784 				 err);
11785 			return err;
11786 		}
11787 		i40e_enable_misc_int_causes(pf);
11788 		i40e_irq_dynamic_enable_icr0(pf);
11789 	}
11790 
11791 	return 0;
11792 }
11793 
11794 /**
11795  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
11796  * @pf: board private structure
11797  *
11798  * This sets up the handler for MSIX 0, which is used to manage the
11799  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
11800  * when in MSI or Legacy interrupt mode.
11801  **/
11802 static int i40e_setup_misc_vector(struct i40e_pf *pf)
11803 {
11804 	struct i40e_hw *hw = &pf->hw;
11805 	int err = 0;
11806 
11807 	/* Only request the IRQ once, the first time through. */
11808 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
11809 		err = request_irq(pf->msix_entries[0].vector,
11810 				  i40e_intr, 0, pf->int_name, pf);
11811 		if (err) {
11812 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
11813 			dev_info(&pf->pdev->dev,
11814 				 "request_irq for %s failed: %d\n",
11815 				 pf->int_name, err);
11816 			return -EFAULT;
11817 		}
11818 	}
11819 
11820 	i40e_enable_misc_int_causes(pf);
11821 
11822 	/* associate no queues to the misc vector */
11823 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
11824 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
11825 
11826 	i40e_flush(hw);
11827 
11828 	i40e_irq_dynamic_enable_icr0(pf);
11829 
11830 	return err;
11831 }
11832 
11833 /**
11834  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
11835  * @vsi: Pointer to vsi structure
11836  * @seed: Buffter to store the hash keys
11837  * @lut: Buffer to store the lookup table entries
11838  * @lut_size: Size of buffer to store the lookup table entries
11839  *
11840  * Return 0 on success, negative on failure
11841  */
11842 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
11843 			   u8 *lut, u16 lut_size)
11844 {
11845 	struct i40e_pf *pf = vsi->back;
11846 	struct i40e_hw *hw = &pf->hw;
11847 	int ret = 0;
11848 
11849 	if (seed) {
11850 		ret = i40e_aq_get_rss_key(hw, vsi->id,
11851 			(struct i40e_aqc_get_set_rss_key_data *)seed);
11852 		if (ret) {
11853 			dev_info(&pf->pdev->dev,
11854 				 "Cannot get RSS key, err %s aq_err %s\n",
11855 				 i40e_stat_str(&pf->hw, ret),
11856 				 i40e_aq_str(&pf->hw,
11857 					     pf->hw.aq.asq_last_status));
11858 			return ret;
11859 		}
11860 	}
11861 
11862 	if (lut) {
11863 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
11864 
11865 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
11866 		if (ret) {
11867 			dev_info(&pf->pdev->dev,
11868 				 "Cannot get RSS lut, err %s aq_err %s\n",
11869 				 i40e_stat_str(&pf->hw, ret),
11870 				 i40e_aq_str(&pf->hw,
11871 					     pf->hw.aq.asq_last_status));
11872 			return ret;
11873 		}
11874 	}
11875 
11876 	return ret;
11877 }
11878 
11879 /**
11880  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
11881  * @vsi: Pointer to vsi structure
11882  * @seed: RSS hash seed
11883  * @lut: Lookup table
11884  * @lut_size: Lookup table size
11885  *
11886  * Returns 0 on success, negative on failure
11887  **/
11888 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
11889 			       const u8 *lut, u16 lut_size)
11890 {
11891 	struct i40e_pf *pf = vsi->back;
11892 	struct i40e_hw *hw = &pf->hw;
11893 	u16 vf_id = vsi->vf_id;
11894 	u8 i;
11895 
11896 	/* Fill out hash function seed */
11897 	if (seed) {
11898 		u32 *seed_dw = (u32 *)seed;
11899 
11900 		if (vsi->type == I40E_VSI_MAIN) {
11901 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
11902 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
11903 		} else if (vsi->type == I40E_VSI_SRIOV) {
11904 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
11905 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
11906 		} else {
11907 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
11908 		}
11909 	}
11910 
11911 	if (lut) {
11912 		u32 *lut_dw = (u32 *)lut;
11913 
11914 		if (vsi->type == I40E_VSI_MAIN) {
11915 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
11916 				return -EINVAL;
11917 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11918 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
11919 		} else if (vsi->type == I40E_VSI_SRIOV) {
11920 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
11921 				return -EINVAL;
11922 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
11923 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
11924 		} else {
11925 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
11926 		}
11927 	}
11928 	i40e_flush(hw);
11929 
11930 	return 0;
11931 }
11932 
11933 /**
11934  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
11935  * @vsi: Pointer to VSI structure
11936  * @seed: Buffer to store the keys
11937  * @lut: Buffer to store the lookup table entries
11938  * @lut_size: Size of buffer to store the lookup table entries
11939  *
11940  * Returns 0 on success, negative on failure
11941  */
11942 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
11943 			    u8 *lut, u16 lut_size)
11944 {
11945 	struct i40e_pf *pf = vsi->back;
11946 	struct i40e_hw *hw = &pf->hw;
11947 	u16 i;
11948 
11949 	if (seed) {
11950 		u32 *seed_dw = (u32 *)seed;
11951 
11952 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
11953 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
11954 	}
11955 	if (lut) {
11956 		u32 *lut_dw = (u32 *)lut;
11957 
11958 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
11959 			return -EINVAL;
11960 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11961 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
11962 	}
11963 
11964 	return 0;
11965 }
11966 
11967 /**
11968  * i40e_config_rss - Configure RSS keys and lut
11969  * @vsi: Pointer to VSI structure
11970  * @seed: RSS hash seed
11971  * @lut: Lookup table
11972  * @lut_size: Lookup table size
11973  *
11974  * Returns 0 on success, negative on failure
11975  */
11976 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
11977 {
11978 	struct i40e_pf *pf = vsi->back;
11979 
11980 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
11981 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
11982 	else
11983 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
11984 }
11985 
11986 /**
11987  * i40e_get_rss - Get RSS keys and lut
11988  * @vsi: Pointer to VSI structure
11989  * @seed: Buffer to store the keys
11990  * @lut: Buffer to store the lookup table entries
11991  * @lut_size: Size of buffer to store the lookup table entries
11992  *
11993  * Returns 0 on success, negative on failure
11994  */
11995 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
11996 {
11997 	struct i40e_pf *pf = vsi->back;
11998 
11999 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12000 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12001 	else
12002 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12003 }
12004 
12005 /**
12006  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12007  * @pf: Pointer to board private structure
12008  * @lut: Lookup table
12009  * @rss_table_size: Lookup table size
12010  * @rss_size: Range of queue number for hashing
12011  */
12012 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12013 		       u16 rss_table_size, u16 rss_size)
12014 {
12015 	u16 i;
12016 
12017 	for (i = 0; i < rss_table_size; i++)
12018 		lut[i] = i % rss_size;
12019 }
12020 
12021 /**
12022  * i40e_pf_config_rss - Prepare for RSS if used
12023  * @pf: board private structure
12024  **/
12025 static int i40e_pf_config_rss(struct i40e_pf *pf)
12026 {
12027 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12028 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12029 	u8 *lut;
12030 	struct i40e_hw *hw = &pf->hw;
12031 	u32 reg_val;
12032 	u64 hena;
12033 	int ret;
12034 
12035 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12036 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12037 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12038 	hena |= i40e_pf_get_default_rss_hena(pf);
12039 
12040 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12041 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12042 
12043 	/* Determine the RSS table size based on the hardware capabilities */
12044 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12045 	reg_val = (pf->rss_table_size == 512) ?
12046 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12047 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12048 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12049 
12050 	/* Determine the RSS size of the VSI */
12051 	if (!vsi->rss_size) {
12052 		u16 qcount;
12053 		/* If the firmware does something weird during VSI init, we
12054 		 * could end up with zero TCs. Check for that to avoid
12055 		 * divide-by-zero. It probably won't pass traffic, but it also
12056 		 * won't panic.
12057 		 */
12058 		qcount = vsi->num_queue_pairs /
12059 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12060 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12061 	}
12062 	if (!vsi->rss_size)
12063 		return -EINVAL;
12064 
12065 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12066 	if (!lut)
12067 		return -ENOMEM;
12068 
12069 	/* Use user configured lut if there is one, otherwise use default */
12070 	if (vsi->rss_lut_user)
12071 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12072 	else
12073 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12074 
12075 	/* Use user configured hash key if there is one, otherwise
12076 	 * use default.
12077 	 */
12078 	if (vsi->rss_hkey_user)
12079 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12080 	else
12081 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12082 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12083 	kfree(lut);
12084 
12085 	return ret;
12086 }
12087 
12088 /**
12089  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12090  * @pf: board private structure
12091  * @queue_count: the requested queue count for rss.
12092  *
12093  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12094  * count which may be different from the requested queue count.
12095  * Note: expects to be called while under rtnl_lock()
12096  **/
12097 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12098 {
12099 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12100 	int new_rss_size;
12101 
12102 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12103 		return 0;
12104 
12105 	queue_count = min_t(int, queue_count, num_online_cpus());
12106 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12107 
12108 	if (queue_count != vsi->num_queue_pairs) {
12109 		u16 qcount;
12110 
12111 		vsi->req_queue_pairs = queue_count;
12112 		i40e_prep_for_reset(pf);
12113 
12114 		pf->alloc_rss_size = new_rss_size;
12115 
12116 		i40e_reset_and_rebuild(pf, true, true);
12117 
12118 		/* Discard the user configured hash keys and lut, if less
12119 		 * queues are enabled.
12120 		 */
12121 		if (queue_count < vsi->rss_size) {
12122 			i40e_clear_rss_config_user(vsi);
12123 			dev_dbg(&pf->pdev->dev,
12124 				"discard user configured hash keys and lut\n");
12125 		}
12126 
12127 		/* Reset vsi->rss_size, as number of enabled queues changed */
12128 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12129 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12130 
12131 		i40e_pf_config_rss(pf);
12132 	}
12133 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12134 		 vsi->req_queue_pairs, pf->rss_size_max);
12135 	return pf->alloc_rss_size;
12136 }
12137 
12138 /**
12139  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12140  * @pf: board private structure
12141  **/
12142 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
12143 {
12144 	i40e_status status;
12145 	bool min_valid, max_valid;
12146 	u32 max_bw, min_bw;
12147 
12148 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12149 					   &min_valid, &max_valid);
12150 
12151 	if (!status) {
12152 		if (min_valid)
12153 			pf->min_bw = min_bw;
12154 		if (max_valid)
12155 			pf->max_bw = max_bw;
12156 	}
12157 
12158 	return status;
12159 }
12160 
12161 /**
12162  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12163  * @pf: board private structure
12164  **/
12165 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
12166 {
12167 	struct i40e_aqc_configure_partition_bw_data bw_data;
12168 	i40e_status status;
12169 
12170 	memset(&bw_data, 0, sizeof(bw_data));
12171 
12172 	/* Set the valid bit for this PF */
12173 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12174 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12175 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12176 
12177 	/* Set the new bandwidths */
12178 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12179 
12180 	return status;
12181 }
12182 
12183 /**
12184  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12185  * @pf: board private structure
12186  **/
12187 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12188 {
12189 	/* Commit temporary BW setting to permanent NVM image */
12190 	enum i40e_admin_queue_err last_aq_status;
12191 	i40e_status ret;
12192 	u16 nvm_word;
12193 
12194 	if (pf->hw.partition_id != 1) {
12195 		dev_info(&pf->pdev->dev,
12196 			 "Commit BW only works on partition 1! This is partition %d",
12197 			 pf->hw.partition_id);
12198 		ret = I40E_NOT_SUPPORTED;
12199 		goto bw_commit_out;
12200 	}
12201 
12202 	/* Acquire NVM for read access */
12203 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12204 	last_aq_status = pf->hw.aq.asq_last_status;
12205 	if (ret) {
12206 		dev_info(&pf->pdev->dev,
12207 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
12208 			 i40e_stat_str(&pf->hw, ret),
12209 			 i40e_aq_str(&pf->hw, last_aq_status));
12210 		goto bw_commit_out;
12211 	}
12212 
12213 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12214 	ret = i40e_aq_read_nvm(&pf->hw,
12215 			       I40E_SR_NVM_CONTROL_WORD,
12216 			       0x10, sizeof(nvm_word), &nvm_word,
12217 			       false, NULL);
12218 	/* Save off last admin queue command status before releasing
12219 	 * the NVM
12220 	 */
12221 	last_aq_status = pf->hw.aq.asq_last_status;
12222 	i40e_release_nvm(&pf->hw);
12223 	if (ret) {
12224 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
12225 			 i40e_stat_str(&pf->hw, ret),
12226 			 i40e_aq_str(&pf->hw, last_aq_status));
12227 		goto bw_commit_out;
12228 	}
12229 
12230 	/* Wait a bit for NVM release to complete */
12231 	msleep(50);
12232 
12233 	/* Acquire NVM for write access */
12234 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12235 	last_aq_status = pf->hw.aq.asq_last_status;
12236 	if (ret) {
12237 		dev_info(&pf->pdev->dev,
12238 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
12239 			 i40e_stat_str(&pf->hw, ret),
12240 			 i40e_aq_str(&pf->hw, last_aq_status));
12241 		goto bw_commit_out;
12242 	}
12243 	/* Write it back out unchanged to initiate update NVM,
12244 	 * which will force a write of the shadow (alt) RAM to
12245 	 * the NVM - thus storing the bandwidth values permanently.
12246 	 */
12247 	ret = i40e_aq_update_nvm(&pf->hw,
12248 				 I40E_SR_NVM_CONTROL_WORD,
12249 				 0x10, sizeof(nvm_word),
12250 				 &nvm_word, true, 0, NULL);
12251 	/* Save off last admin queue command status before releasing
12252 	 * the NVM
12253 	 */
12254 	last_aq_status = pf->hw.aq.asq_last_status;
12255 	i40e_release_nvm(&pf->hw);
12256 	if (ret)
12257 		dev_info(&pf->pdev->dev,
12258 			 "BW settings NOT SAVED, err %s aq_err %s\n",
12259 			 i40e_stat_str(&pf->hw, ret),
12260 			 i40e_aq_str(&pf->hw, last_aq_status));
12261 bw_commit_out:
12262 
12263 	return ret;
12264 }
12265 
12266 /**
12267  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12268  * if total port shutdown feature is enabled for this PF
12269  * @pf: board private structure
12270  **/
12271 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12272 {
12273 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12274 #define I40E_FEATURES_ENABLE_PTR		0x2A
12275 #define I40E_CURRENT_SETTING_PTR		0x2B
12276 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12277 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12278 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12279 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12280 	i40e_status read_status = I40E_SUCCESS;
12281 	u16 sr_emp_sr_settings_ptr = 0;
12282 	u16 features_enable = 0;
12283 	u16 link_behavior = 0;
12284 	bool ret = false;
12285 
12286 	read_status = i40e_read_nvm_word(&pf->hw,
12287 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12288 					 &sr_emp_sr_settings_ptr);
12289 	if (read_status)
12290 		goto err_nvm;
12291 	read_status = i40e_read_nvm_word(&pf->hw,
12292 					 sr_emp_sr_settings_ptr +
12293 					 I40E_FEATURES_ENABLE_PTR,
12294 					 &features_enable);
12295 	if (read_status)
12296 		goto err_nvm;
12297 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12298 		read_status = i40e_read_nvm_module_data(&pf->hw,
12299 							I40E_SR_EMP_SR_SETTINGS_PTR,
12300 							I40E_CURRENT_SETTING_PTR,
12301 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12302 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12303 							&link_behavior);
12304 		if (read_status)
12305 			goto err_nvm;
12306 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12307 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12308 	}
12309 	return ret;
12310 
12311 err_nvm:
12312 	dev_warn(&pf->pdev->dev,
12313 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
12314 		 i40e_stat_str(&pf->hw, read_status));
12315 	return ret;
12316 }
12317 
12318 /**
12319  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12320  * @pf: board private structure to initialize
12321  *
12322  * i40e_sw_init initializes the Adapter private data structure.
12323  * Fields are initialized based on PCI device information and
12324  * OS network device settings (MTU size).
12325  **/
12326 static int i40e_sw_init(struct i40e_pf *pf)
12327 {
12328 	int err = 0;
12329 	int size;
12330 	u16 pow;
12331 
12332 	/* Set default capability flags */
12333 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12334 		    I40E_FLAG_MSI_ENABLED     |
12335 		    I40E_FLAG_MSIX_ENABLED;
12336 
12337 	/* Set default ITR */
12338 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12339 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12340 
12341 	/* Depending on PF configurations, it is possible that the RSS
12342 	 * maximum might end up larger than the available queues
12343 	 */
12344 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12345 	pf->alloc_rss_size = 1;
12346 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12347 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12348 				 pf->hw.func_caps.num_tx_qp);
12349 
12350 	/* find the next higher power-of-2 of num cpus */
12351 	pow = roundup_pow_of_two(num_online_cpus());
12352 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12353 
12354 	if (pf->hw.func_caps.rss) {
12355 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12356 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12357 					   num_online_cpus());
12358 	}
12359 
12360 	/* MFP mode enabled */
12361 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12362 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12363 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12364 		if (i40e_get_partition_bw_setting(pf)) {
12365 			dev_warn(&pf->pdev->dev,
12366 				 "Could not get partition bw settings\n");
12367 		} else {
12368 			dev_info(&pf->pdev->dev,
12369 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12370 				 pf->min_bw, pf->max_bw);
12371 
12372 			/* nudge the Tx scheduler */
12373 			i40e_set_partition_bw_setting(pf);
12374 		}
12375 	}
12376 
12377 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12378 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12379 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12380 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12381 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12382 		    pf->hw.num_partitions > 1)
12383 			dev_info(&pf->pdev->dev,
12384 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12385 		else
12386 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12387 		pf->fdir_pf_filter_count =
12388 				 pf->hw.func_caps.fd_filters_guaranteed;
12389 		pf->hw.fdir_shared_filter_count =
12390 				 pf->hw.func_caps.fd_filters_best_effort;
12391 	}
12392 
12393 	if (pf->hw.mac.type == I40E_MAC_X722) {
12394 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12395 				    I40E_HW_128_QP_RSS_CAPABLE |
12396 				    I40E_HW_ATR_EVICT_CAPABLE |
12397 				    I40E_HW_WB_ON_ITR_CAPABLE |
12398 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12399 				    I40E_HW_NO_PCI_LINK_CHECK |
12400 				    I40E_HW_USE_SET_LLDP_MIB |
12401 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12402 				    I40E_HW_PTP_L4_CAPABLE |
12403 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12404 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12405 
12406 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12407 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12408 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12409 			dev_warn(&pf->pdev->dev,
12410 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12411 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12412 		}
12413 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12414 		   ((pf->hw.aq.api_maj_ver == 1) &&
12415 		    (pf->hw.aq.api_min_ver > 4))) {
12416 		/* Supported in FW API version higher than 1.4 */
12417 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12418 	}
12419 
12420 	/* Enable HW ATR eviction if possible */
12421 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12422 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12423 
12424 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12425 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12426 	    (pf->hw.aq.fw_maj_ver < 4))) {
12427 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12428 		/* No DCB support  for FW < v4.33 */
12429 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12430 	}
12431 
12432 	/* Disable FW LLDP if FW < v4.3 */
12433 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12434 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12435 	    (pf->hw.aq.fw_maj_ver < 4)))
12436 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12437 
12438 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12439 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12440 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12441 	    (pf->hw.aq.fw_maj_ver >= 5)))
12442 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12443 
12444 	/* Enable PTP L4 if FW > v6.0 */
12445 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12446 	    pf->hw.aq.fw_maj_ver >= 6)
12447 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12448 
12449 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12450 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12451 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12452 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12453 	}
12454 
12455 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12456 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12457 		/* IWARP needs one extra vector for CQP just like MISC.*/
12458 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12459 	}
12460 	/* Stopping FW LLDP engine is supported on XL710 and X722
12461 	 * starting from FW versions determined in i40e_init_adminq.
12462 	 * Stopping the FW LLDP engine is not supported on XL710
12463 	 * if NPAR is functioning so unset this hw flag in this case.
12464 	 */
12465 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12466 	    pf->hw.func_caps.npar_enable &&
12467 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12468 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12469 
12470 #ifdef CONFIG_PCI_IOV
12471 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12472 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12473 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12474 		pf->num_req_vfs = min_t(int,
12475 					pf->hw.func_caps.num_vfs,
12476 					I40E_MAX_VF_COUNT);
12477 	}
12478 #endif /* CONFIG_PCI_IOV */
12479 	pf->eeprom_version = 0xDEAD;
12480 	pf->lan_veb = I40E_NO_VEB;
12481 	pf->lan_vsi = I40E_NO_VSI;
12482 
12483 	/* By default FW has this off for performance reasons */
12484 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12485 
12486 	/* set up queue assignment tracking */
12487 	size = sizeof(struct i40e_lump_tracking)
12488 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12489 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12490 	if (!pf->qp_pile) {
12491 		err = -ENOMEM;
12492 		goto sw_init_done;
12493 	}
12494 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12495 	pf->qp_pile->search_hint = 0;
12496 
12497 	pf->tx_timeout_recovery_level = 1;
12498 
12499 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12500 	    i40e_is_total_port_shutdown_enabled(pf)) {
12501 		/* Link down on close must be on when total port shutdown
12502 		 * is enabled for a given port
12503 		 */
12504 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12505 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12506 		dev_info(&pf->pdev->dev,
12507 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12508 	}
12509 	mutex_init(&pf->switch_mutex);
12510 
12511 sw_init_done:
12512 	return err;
12513 }
12514 
12515 /**
12516  * i40e_set_ntuple - set the ntuple feature flag and take action
12517  * @pf: board private structure to initialize
12518  * @features: the feature set that the stack is suggesting
12519  *
12520  * returns a bool to indicate if reset needs to happen
12521  **/
12522 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12523 {
12524 	bool need_reset = false;
12525 
12526 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12527 	 * the state changed, we need to reset.
12528 	 */
12529 	if (features & NETIF_F_NTUPLE) {
12530 		/* Enable filters and mark for reset */
12531 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12532 			need_reset = true;
12533 		/* enable FD_SB only if there is MSI-X vector and no cloud
12534 		 * filters exist
12535 		 */
12536 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12537 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12538 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12539 		}
12540 	} else {
12541 		/* turn off filters, mark for reset and clear SW filter list */
12542 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12543 			need_reset = true;
12544 			i40e_fdir_filter_exit(pf);
12545 		}
12546 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12547 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12548 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12549 
12550 		/* reset fd counters */
12551 		pf->fd_add_err = 0;
12552 		pf->fd_atr_cnt = 0;
12553 		/* if ATR was auto disabled it can be re-enabled. */
12554 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12555 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12556 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12557 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12558 	}
12559 	return need_reset;
12560 }
12561 
12562 /**
12563  * i40e_clear_rss_lut - clear the rx hash lookup table
12564  * @vsi: the VSI being configured
12565  **/
12566 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12567 {
12568 	struct i40e_pf *pf = vsi->back;
12569 	struct i40e_hw *hw = &pf->hw;
12570 	u16 vf_id = vsi->vf_id;
12571 	u8 i;
12572 
12573 	if (vsi->type == I40E_VSI_MAIN) {
12574 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12575 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12576 	} else if (vsi->type == I40E_VSI_SRIOV) {
12577 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12578 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12579 	} else {
12580 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12581 	}
12582 }
12583 
12584 /**
12585  * i40e_set_features - set the netdev feature flags
12586  * @netdev: ptr to the netdev being adjusted
12587  * @features: the feature set that the stack is suggesting
12588  * Note: expects to be called while under rtnl_lock()
12589  **/
12590 static int i40e_set_features(struct net_device *netdev,
12591 			     netdev_features_t features)
12592 {
12593 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12594 	struct i40e_vsi *vsi = np->vsi;
12595 	struct i40e_pf *pf = vsi->back;
12596 	bool need_reset;
12597 
12598 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12599 		i40e_pf_config_rss(pf);
12600 	else if (!(features & NETIF_F_RXHASH) &&
12601 		 netdev->features & NETIF_F_RXHASH)
12602 		i40e_clear_rss_lut(vsi);
12603 
12604 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12605 		i40e_vlan_stripping_enable(vsi);
12606 	else
12607 		i40e_vlan_stripping_disable(vsi);
12608 
12609 	if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12610 		dev_err(&pf->pdev->dev,
12611 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12612 		return -EINVAL;
12613 	}
12614 
12615 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12616 		i40e_del_all_macvlans(vsi);
12617 
12618 	need_reset = i40e_set_ntuple(pf, features);
12619 
12620 	if (need_reset)
12621 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12622 
12623 	return 0;
12624 }
12625 
12626 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12627 				    unsigned int table, unsigned int idx,
12628 				    struct udp_tunnel_info *ti)
12629 {
12630 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12631 	struct i40e_hw *hw = &np->vsi->back->hw;
12632 	u8 type, filter_index;
12633 	i40e_status ret;
12634 
12635 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12636 						   I40E_AQC_TUNNEL_TYPE_NGE;
12637 
12638 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12639 				     NULL);
12640 	if (ret) {
12641 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12642 			    i40e_stat_str(hw, ret),
12643 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12644 		return -EIO;
12645 	}
12646 
12647 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12648 	return 0;
12649 }
12650 
12651 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12652 				      unsigned int table, unsigned int idx,
12653 				      struct udp_tunnel_info *ti)
12654 {
12655 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12656 	struct i40e_hw *hw = &np->vsi->back->hw;
12657 	i40e_status ret;
12658 
12659 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12660 	if (ret) {
12661 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
12662 			    i40e_stat_str(hw, ret),
12663 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12664 		return -EIO;
12665 	}
12666 
12667 	return 0;
12668 }
12669 
12670 static int i40e_get_phys_port_id(struct net_device *netdev,
12671 				 struct netdev_phys_item_id *ppid)
12672 {
12673 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12674 	struct i40e_pf *pf = np->vsi->back;
12675 	struct i40e_hw *hw = &pf->hw;
12676 
12677 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
12678 		return -EOPNOTSUPP;
12679 
12680 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
12681 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
12682 
12683 	return 0;
12684 }
12685 
12686 /**
12687  * i40e_ndo_fdb_add - add an entry to the hardware database
12688  * @ndm: the input from the stack
12689  * @tb: pointer to array of nladdr (unused)
12690  * @dev: the net device pointer
12691  * @addr: the MAC address entry being added
12692  * @vid: VLAN ID
12693  * @flags: instructions from stack about fdb operation
12694  * @extack: netlink extended ack, unused currently
12695  */
12696 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
12697 			    struct net_device *dev,
12698 			    const unsigned char *addr, u16 vid,
12699 			    u16 flags,
12700 			    struct netlink_ext_ack *extack)
12701 {
12702 	struct i40e_netdev_priv *np = netdev_priv(dev);
12703 	struct i40e_pf *pf = np->vsi->back;
12704 	int err = 0;
12705 
12706 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
12707 		return -EOPNOTSUPP;
12708 
12709 	if (vid) {
12710 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
12711 		return -EINVAL;
12712 	}
12713 
12714 	/* Hardware does not support aging addresses so if a
12715 	 * ndm_state is given only allow permanent addresses
12716 	 */
12717 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
12718 		netdev_info(dev, "FDB only supports static addresses\n");
12719 		return -EINVAL;
12720 	}
12721 
12722 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
12723 		err = dev_uc_add_excl(dev, addr);
12724 	else if (is_multicast_ether_addr(addr))
12725 		err = dev_mc_add_excl(dev, addr);
12726 	else
12727 		err = -EINVAL;
12728 
12729 	/* Only return duplicate errors if NLM_F_EXCL is set */
12730 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
12731 		err = 0;
12732 
12733 	return err;
12734 }
12735 
12736 /**
12737  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
12738  * @dev: the netdev being configured
12739  * @nlh: RTNL message
12740  * @flags: bridge flags
12741  * @extack: netlink extended ack
12742  *
12743  * Inserts a new hardware bridge if not already created and
12744  * enables the bridging mode requested (VEB or VEPA). If the
12745  * hardware bridge has already been inserted and the request
12746  * is to change the mode then that requires a PF reset to
12747  * allow rebuild of the components with required hardware
12748  * bridge mode enabled.
12749  *
12750  * Note: expects to be called while under rtnl_lock()
12751  **/
12752 static int i40e_ndo_bridge_setlink(struct net_device *dev,
12753 				   struct nlmsghdr *nlh,
12754 				   u16 flags,
12755 				   struct netlink_ext_ack *extack)
12756 {
12757 	struct i40e_netdev_priv *np = netdev_priv(dev);
12758 	struct i40e_vsi *vsi = np->vsi;
12759 	struct i40e_pf *pf = vsi->back;
12760 	struct i40e_veb *veb = NULL;
12761 	struct nlattr *attr, *br_spec;
12762 	int i, rem;
12763 
12764 	/* Only for PF VSI for now */
12765 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12766 		return -EOPNOTSUPP;
12767 
12768 	/* Find the HW bridge for PF VSI */
12769 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12770 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12771 			veb = pf->veb[i];
12772 	}
12773 
12774 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
12775 
12776 	nla_for_each_nested(attr, br_spec, rem) {
12777 		__u16 mode;
12778 
12779 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
12780 			continue;
12781 
12782 		mode = nla_get_u16(attr);
12783 		if ((mode != BRIDGE_MODE_VEPA) &&
12784 		    (mode != BRIDGE_MODE_VEB))
12785 			return -EINVAL;
12786 
12787 		/* Insert a new HW bridge */
12788 		if (!veb) {
12789 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12790 					     vsi->tc_config.enabled_tc);
12791 			if (veb) {
12792 				veb->bridge_mode = mode;
12793 				i40e_config_bridge_mode(veb);
12794 			} else {
12795 				/* No Bridge HW offload available */
12796 				return -ENOENT;
12797 			}
12798 			break;
12799 		} else if (mode != veb->bridge_mode) {
12800 			/* Existing HW bridge but different mode needs reset */
12801 			veb->bridge_mode = mode;
12802 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
12803 			if (mode == BRIDGE_MODE_VEB)
12804 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
12805 			else
12806 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12807 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12808 			break;
12809 		}
12810 	}
12811 
12812 	return 0;
12813 }
12814 
12815 /**
12816  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
12817  * @skb: skb buff
12818  * @pid: process id
12819  * @seq: RTNL message seq #
12820  * @dev: the netdev being configured
12821  * @filter_mask: unused
12822  * @nlflags: netlink flags passed in
12823  *
12824  * Return the mode in which the hardware bridge is operating in
12825  * i.e VEB or VEPA.
12826  **/
12827 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
12828 				   struct net_device *dev,
12829 				   u32 __always_unused filter_mask,
12830 				   int nlflags)
12831 {
12832 	struct i40e_netdev_priv *np = netdev_priv(dev);
12833 	struct i40e_vsi *vsi = np->vsi;
12834 	struct i40e_pf *pf = vsi->back;
12835 	struct i40e_veb *veb = NULL;
12836 	int i;
12837 
12838 	/* Only for PF VSI for now */
12839 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12840 		return -EOPNOTSUPP;
12841 
12842 	/* Find the HW bridge for the PF VSI */
12843 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12844 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12845 			veb = pf->veb[i];
12846 	}
12847 
12848 	if (!veb)
12849 		return 0;
12850 
12851 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
12852 				       0, 0, nlflags, filter_mask, NULL);
12853 }
12854 
12855 /**
12856  * i40e_features_check - Validate encapsulated packet conforms to limits
12857  * @skb: skb buff
12858  * @dev: This physical port's netdev
12859  * @features: Offload features that the stack believes apply
12860  **/
12861 static netdev_features_t i40e_features_check(struct sk_buff *skb,
12862 					     struct net_device *dev,
12863 					     netdev_features_t features)
12864 {
12865 	size_t len;
12866 
12867 	/* No point in doing any of this if neither checksum nor GSO are
12868 	 * being requested for this frame.  We can rule out both by just
12869 	 * checking for CHECKSUM_PARTIAL
12870 	 */
12871 	if (skb->ip_summed != CHECKSUM_PARTIAL)
12872 		return features;
12873 
12874 	/* We cannot support GSO if the MSS is going to be less than
12875 	 * 64 bytes.  If it is then we need to drop support for GSO.
12876 	 */
12877 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
12878 		features &= ~NETIF_F_GSO_MASK;
12879 
12880 	/* MACLEN can support at most 63 words */
12881 	len = skb_network_header(skb) - skb->data;
12882 	if (len & ~(63 * 2))
12883 		goto out_err;
12884 
12885 	/* IPLEN and EIPLEN can support at most 127 dwords */
12886 	len = skb_transport_header(skb) - skb_network_header(skb);
12887 	if (len & ~(127 * 4))
12888 		goto out_err;
12889 
12890 	if (skb->encapsulation) {
12891 		/* L4TUNLEN can support 127 words */
12892 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
12893 		if (len & ~(127 * 2))
12894 			goto out_err;
12895 
12896 		/* IPLEN can support at most 127 dwords */
12897 		len = skb_inner_transport_header(skb) -
12898 		      skb_inner_network_header(skb);
12899 		if (len & ~(127 * 4))
12900 			goto out_err;
12901 	}
12902 
12903 	/* No need to validate L4LEN as TCP is the only protocol with a
12904 	 * a flexible value and we support all possible values supported
12905 	 * by TCP, which is at most 15 dwords
12906 	 */
12907 
12908 	return features;
12909 out_err:
12910 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
12911 }
12912 
12913 /**
12914  * i40e_xdp_setup - add/remove an XDP program
12915  * @vsi: VSI to changed
12916  * @prog: XDP program
12917  * @extack: netlink extended ack
12918  **/
12919 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
12920 			  struct netlink_ext_ack *extack)
12921 {
12922 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
12923 	struct i40e_pf *pf = vsi->back;
12924 	struct bpf_prog *old_prog;
12925 	bool need_reset;
12926 	int i;
12927 
12928 	/* Don't allow frames that span over multiple buffers */
12929 	if (frame_size > vsi->rx_buf_len) {
12930 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
12931 		return -EINVAL;
12932 	}
12933 
12934 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
12935 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
12936 
12937 	if (need_reset)
12938 		i40e_prep_for_reset(pf);
12939 
12940 	old_prog = xchg(&vsi->xdp_prog, prog);
12941 
12942 	if (need_reset) {
12943 		if (!prog)
12944 			/* Wait until ndo_xsk_wakeup completes. */
12945 			synchronize_rcu();
12946 		i40e_reset_and_rebuild(pf, true, true);
12947 	}
12948 
12949 	for (i = 0; i < vsi->num_queue_pairs; i++)
12950 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
12951 
12952 	if (old_prog)
12953 		bpf_prog_put(old_prog);
12954 
12955 	/* Kick start the NAPI context if there is an AF_XDP socket open
12956 	 * on that queue id. This so that receiving will start.
12957 	 */
12958 	if (need_reset && prog)
12959 		for (i = 0; i < vsi->num_queue_pairs; i++)
12960 			if (vsi->xdp_rings[i]->xsk_pool)
12961 				(void)i40e_xsk_wakeup(vsi->netdev, i,
12962 						      XDP_WAKEUP_RX);
12963 
12964 	return 0;
12965 }
12966 
12967 /**
12968  * i40e_enter_busy_conf - Enters busy config state
12969  * @vsi: vsi
12970  *
12971  * Returns 0 on success, <0 for failure.
12972  **/
12973 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
12974 {
12975 	struct i40e_pf *pf = vsi->back;
12976 	int timeout = 50;
12977 
12978 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
12979 		timeout--;
12980 		if (!timeout)
12981 			return -EBUSY;
12982 		usleep_range(1000, 2000);
12983 	}
12984 
12985 	return 0;
12986 }
12987 
12988 /**
12989  * i40e_exit_busy_conf - Exits busy config state
12990  * @vsi: vsi
12991  **/
12992 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
12993 {
12994 	struct i40e_pf *pf = vsi->back;
12995 
12996 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
12997 }
12998 
12999 /**
13000  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13001  * @vsi: vsi
13002  * @queue_pair: queue pair
13003  **/
13004 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13005 {
13006 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13007 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13008 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13009 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13010 	if (i40e_enabled_xdp_vsi(vsi)) {
13011 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13012 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13013 	}
13014 }
13015 
13016 /**
13017  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13018  * @vsi: vsi
13019  * @queue_pair: queue pair
13020  **/
13021 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13022 {
13023 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13024 	if (i40e_enabled_xdp_vsi(vsi)) {
13025 		/* Make sure that in-progress ndo_xdp_xmit calls are
13026 		 * completed.
13027 		 */
13028 		synchronize_rcu();
13029 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13030 	}
13031 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13032 }
13033 
13034 /**
13035  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13036  * @vsi: vsi
13037  * @queue_pair: queue pair
13038  * @enable: true for enable, false for disable
13039  **/
13040 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13041 					bool enable)
13042 {
13043 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13044 	struct i40e_q_vector *q_vector = rxr->q_vector;
13045 
13046 	if (!vsi->netdev)
13047 		return;
13048 
13049 	/* All rings in a qp belong to the same qvector. */
13050 	if (q_vector->rx.ring || q_vector->tx.ring) {
13051 		if (enable)
13052 			napi_enable(&q_vector->napi);
13053 		else
13054 			napi_disable(&q_vector->napi);
13055 	}
13056 }
13057 
13058 /**
13059  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13060  * @vsi: vsi
13061  * @queue_pair: queue pair
13062  * @enable: true for enable, false for disable
13063  *
13064  * Returns 0 on success, <0 on failure.
13065  **/
13066 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13067 					bool enable)
13068 {
13069 	struct i40e_pf *pf = vsi->back;
13070 	int pf_q, ret = 0;
13071 
13072 	pf_q = vsi->base_queue + queue_pair;
13073 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13074 				     false /*is xdp*/, enable);
13075 	if (ret) {
13076 		dev_info(&pf->pdev->dev,
13077 			 "VSI seid %d Tx ring %d %sable timeout\n",
13078 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13079 		return ret;
13080 	}
13081 
13082 	i40e_control_rx_q(pf, pf_q, enable);
13083 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13084 	if (ret) {
13085 		dev_info(&pf->pdev->dev,
13086 			 "VSI seid %d Rx ring %d %sable timeout\n",
13087 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13088 		return ret;
13089 	}
13090 
13091 	/* Due to HW errata, on Rx disable only, the register can
13092 	 * indicate done before it really is. Needs 50ms to be sure
13093 	 */
13094 	if (!enable)
13095 		mdelay(50);
13096 
13097 	if (!i40e_enabled_xdp_vsi(vsi))
13098 		return ret;
13099 
13100 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13101 				     pf_q + vsi->alloc_queue_pairs,
13102 				     true /*is xdp*/, enable);
13103 	if (ret) {
13104 		dev_info(&pf->pdev->dev,
13105 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13106 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13107 	}
13108 
13109 	return ret;
13110 }
13111 
13112 /**
13113  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13114  * @vsi: vsi
13115  * @queue_pair: queue_pair
13116  **/
13117 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13118 {
13119 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13120 	struct i40e_pf *pf = vsi->back;
13121 	struct i40e_hw *hw = &pf->hw;
13122 
13123 	/* All rings in a qp belong to the same qvector. */
13124 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13125 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13126 	else
13127 		i40e_irq_dynamic_enable_icr0(pf);
13128 
13129 	i40e_flush(hw);
13130 }
13131 
13132 /**
13133  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13134  * @vsi: vsi
13135  * @queue_pair: queue_pair
13136  **/
13137 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13138 {
13139 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13140 	struct i40e_pf *pf = vsi->back;
13141 	struct i40e_hw *hw = &pf->hw;
13142 
13143 	/* For simplicity, instead of removing the qp interrupt causes
13144 	 * from the interrupt linked list, we simply disable the interrupt, and
13145 	 * leave the list intact.
13146 	 *
13147 	 * All rings in a qp belong to the same qvector.
13148 	 */
13149 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13150 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13151 
13152 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13153 		i40e_flush(hw);
13154 		synchronize_irq(pf->msix_entries[intpf].vector);
13155 	} else {
13156 		/* Legacy and MSI mode - this stops all interrupt handling */
13157 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13158 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13159 		i40e_flush(hw);
13160 		synchronize_irq(pf->pdev->irq);
13161 	}
13162 }
13163 
13164 /**
13165  * i40e_queue_pair_disable - Disables a queue pair
13166  * @vsi: vsi
13167  * @queue_pair: queue pair
13168  *
13169  * Returns 0 on success, <0 on failure.
13170  **/
13171 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13172 {
13173 	int err;
13174 
13175 	err = i40e_enter_busy_conf(vsi);
13176 	if (err)
13177 		return err;
13178 
13179 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13180 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13181 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13182 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13183 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13184 
13185 	return err;
13186 }
13187 
13188 /**
13189  * i40e_queue_pair_enable - Enables a queue pair
13190  * @vsi: vsi
13191  * @queue_pair: queue pair
13192  *
13193  * Returns 0 on success, <0 on failure.
13194  **/
13195 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13196 {
13197 	int err;
13198 
13199 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13200 	if (err)
13201 		return err;
13202 
13203 	if (i40e_enabled_xdp_vsi(vsi)) {
13204 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13205 		if (err)
13206 			return err;
13207 	}
13208 
13209 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13210 	if (err)
13211 		return err;
13212 
13213 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13214 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13215 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13216 
13217 	i40e_exit_busy_conf(vsi);
13218 
13219 	return err;
13220 }
13221 
13222 /**
13223  * i40e_xdp - implements ndo_bpf for i40e
13224  * @dev: netdevice
13225  * @xdp: XDP command
13226  **/
13227 static int i40e_xdp(struct net_device *dev,
13228 		    struct netdev_bpf *xdp)
13229 {
13230 	struct i40e_netdev_priv *np = netdev_priv(dev);
13231 	struct i40e_vsi *vsi = np->vsi;
13232 
13233 	if (vsi->type != I40E_VSI_MAIN)
13234 		return -EINVAL;
13235 
13236 	switch (xdp->command) {
13237 	case XDP_SETUP_PROG:
13238 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13239 	case XDP_SETUP_XSK_POOL:
13240 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13241 					   xdp->xsk.queue_id);
13242 	default:
13243 		return -EINVAL;
13244 	}
13245 }
13246 
13247 static const struct net_device_ops i40e_netdev_ops = {
13248 	.ndo_open		= i40e_open,
13249 	.ndo_stop		= i40e_close,
13250 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13251 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13252 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13253 	.ndo_validate_addr	= eth_validate_addr,
13254 	.ndo_set_mac_address	= i40e_set_mac,
13255 	.ndo_change_mtu		= i40e_change_mtu,
13256 	.ndo_do_ioctl		= i40e_ioctl,
13257 	.ndo_tx_timeout		= i40e_tx_timeout,
13258 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13259 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13260 #ifdef CONFIG_NET_POLL_CONTROLLER
13261 	.ndo_poll_controller	= i40e_netpoll,
13262 #endif
13263 	.ndo_setup_tc		= __i40e_setup_tc,
13264 	.ndo_set_features	= i40e_set_features,
13265 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13266 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13267 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13268 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13269 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13270 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13271 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13272 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13273 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13274 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13275 	.ndo_features_check	= i40e_features_check,
13276 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13277 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13278 	.ndo_bpf		= i40e_xdp,
13279 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13280 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13281 	.ndo_dfwd_add_station	= i40e_fwd_add,
13282 	.ndo_dfwd_del_station	= i40e_fwd_del,
13283 };
13284 
13285 /**
13286  * i40e_config_netdev - Setup the netdev flags
13287  * @vsi: the VSI being configured
13288  *
13289  * Returns 0 on success, negative value on failure
13290  **/
13291 static int i40e_config_netdev(struct i40e_vsi *vsi)
13292 {
13293 	struct i40e_pf *pf = vsi->back;
13294 	struct i40e_hw *hw = &pf->hw;
13295 	struct i40e_netdev_priv *np;
13296 	struct net_device *netdev;
13297 	u8 broadcast[ETH_ALEN];
13298 	u8 mac_addr[ETH_ALEN];
13299 	int etherdev_size;
13300 	netdev_features_t hw_enc_features;
13301 	netdev_features_t hw_features;
13302 
13303 	etherdev_size = sizeof(struct i40e_netdev_priv);
13304 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13305 	if (!netdev)
13306 		return -ENOMEM;
13307 
13308 	vsi->netdev = netdev;
13309 	np = netdev_priv(netdev);
13310 	np->vsi = vsi;
13311 
13312 	hw_enc_features = NETIF_F_SG			|
13313 			  NETIF_F_IP_CSUM		|
13314 			  NETIF_F_IPV6_CSUM		|
13315 			  NETIF_F_HIGHDMA		|
13316 			  NETIF_F_SOFT_FEATURES		|
13317 			  NETIF_F_TSO			|
13318 			  NETIF_F_TSO_ECN		|
13319 			  NETIF_F_TSO6			|
13320 			  NETIF_F_GSO_GRE		|
13321 			  NETIF_F_GSO_GRE_CSUM		|
13322 			  NETIF_F_GSO_PARTIAL		|
13323 			  NETIF_F_GSO_IPXIP4		|
13324 			  NETIF_F_GSO_IPXIP6		|
13325 			  NETIF_F_GSO_UDP_TUNNEL	|
13326 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13327 			  NETIF_F_GSO_UDP_L4		|
13328 			  NETIF_F_SCTP_CRC		|
13329 			  NETIF_F_RXHASH		|
13330 			  NETIF_F_RXCSUM		|
13331 			  0;
13332 
13333 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13334 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13335 
13336 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13337 
13338 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13339 
13340 	netdev->hw_enc_features |= hw_enc_features;
13341 
13342 	/* record features VLANs can make use of */
13343 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13344 
13345 	/* enable macvlan offloads */
13346 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13347 
13348 	hw_features = hw_enc_features		|
13349 		      NETIF_F_HW_VLAN_CTAG_TX	|
13350 		      NETIF_F_HW_VLAN_CTAG_RX;
13351 
13352 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13353 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13354 
13355 	netdev->hw_features |= hw_features;
13356 
13357 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13358 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13359 
13360 	if (vsi->type == I40E_VSI_MAIN) {
13361 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13362 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13363 		/* The following steps are necessary for two reasons. First,
13364 		 * some older NVM configurations load a default MAC-VLAN
13365 		 * filter that will accept any tagged packet, and we want to
13366 		 * replace this with a normal filter. Additionally, it is
13367 		 * possible our MAC address was provided by the platform using
13368 		 * Open Firmware or similar.
13369 		 *
13370 		 * Thus, we need to remove the default filter and install one
13371 		 * specific to the MAC address.
13372 		 */
13373 		i40e_rm_default_mac_filter(vsi, mac_addr);
13374 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13375 		i40e_add_mac_filter(vsi, mac_addr);
13376 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13377 	} else {
13378 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13379 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13380 		 * the end, which is 4 bytes long, so force truncation of the
13381 		 * original name by IFNAMSIZ - 4
13382 		 */
13383 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13384 			 IFNAMSIZ - 4,
13385 			 pf->vsi[pf->lan_vsi]->netdev->name);
13386 		eth_random_addr(mac_addr);
13387 
13388 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13389 		i40e_add_mac_filter(vsi, mac_addr);
13390 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13391 	}
13392 
13393 	/* Add the broadcast filter so that we initially will receive
13394 	 * broadcast packets. Note that when a new VLAN is first added the
13395 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13396 	 * specific filters as part of transitioning into "vlan" operation.
13397 	 * When more VLANs are added, the driver will copy each existing MAC
13398 	 * filter and add it for the new VLAN.
13399 	 *
13400 	 * Broadcast filters are handled specially by
13401 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13402 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13403 	 * filter. The subtask will update the correct broadcast promiscuous
13404 	 * bits as VLANs become active or inactive.
13405 	 */
13406 	eth_broadcast_addr(broadcast);
13407 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13408 	i40e_add_mac_filter(vsi, broadcast);
13409 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13410 
13411 	ether_addr_copy(netdev->dev_addr, mac_addr);
13412 	ether_addr_copy(netdev->perm_addr, mac_addr);
13413 
13414 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13415 	netdev->neigh_priv_len = sizeof(u32) * 4;
13416 
13417 	netdev->priv_flags |= IFF_UNICAST_FLT;
13418 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13419 	/* Setup netdev TC information */
13420 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13421 
13422 	netdev->netdev_ops = &i40e_netdev_ops;
13423 	netdev->watchdog_timeo = 5 * HZ;
13424 	i40e_set_ethtool_ops(netdev);
13425 
13426 	/* MTU range: 68 - 9706 */
13427 	netdev->min_mtu = ETH_MIN_MTU;
13428 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13429 
13430 	return 0;
13431 }
13432 
13433 /**
13434  * i40e_vsi_delete - Delete a VSI from the switch
13435  * @vsi: the VSI being removed
13436  *
13437  * Returns 0 on success, negative value on failure
13438  **/
13439 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13440 {
13441 	/* remove default VSI is not allowed */
13442 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13443 		return;
13444 
13445 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13446 }
13447 
13448 /**
13449  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13450  * @vsi: the VSI being queried
13451  *
13452  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13453  **/
13454 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13455 {
13456 	struct i40e_veb *veb;
13457 	struct i40e_pf *pf = vsi->back;
13458 
13459 	/* Uplink is not a bridge so default to VEB */
13460 	if (vsi->veb_idx >= I40E_MAX_VEB)
13461 		return 1;
13462 
13463 	veb = pf->veb[vsi->veb_idx];
13464 	if (!veb) {
13465 		dev_info(&pf->pdev->dev,
13466 			 "There is no veb associated with the bridge\n");
13467 		return -ENOENT;
13468 	}
13469 
13470 	/* Uplink is a bridge in VEPA mode */
13471 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13472 		return 0;
13473 	} else {
13474 		/* Uplink is a bridge in VEB mode */
13475 		return 1;
13476 	}
13477 
13478 	/* VEPA is now default bridge, so return 0 */
13479 	return 0;
13480 }
13481 
13482 /**
13483  * i40e_add_vsi - Add a VSI to the switch
13484  * @vsi: the VSI being configured
13485  *
13486  * This initializes a VSI context depending on the VSI type to be added and
13487  * passes it down to the add_vsi aq command.
13488  **/
13489 static int i40e_add_vsi(struct i40e_vsi *vsi)
13490 {
13491 	int ret = -ENODEV;
13492 	struct i40e_pf *pf = vsi->back;
13493 	struct i40e_hw *hw = &pf->hw;
13494 	struct i40e_vsi_context ctxt;
13495 	struct i40e_mac_filter *f;
13496 	struct hlist_node *h;
13497 	int bkt;
13498 
13499 	u8 enabled_tc = 0x1; /* TC0 enabled */
13500 	int f_count = 0;
13501 
13502 	memset(&ctxt, 0, sizeof(ctxt));
13503 	switch (vsi->type) {
13504 	case I40E_VSI_MAIN:
13505 		/* The PF's main VSI is already setup as part of the
13506 		 * device initialization, so we'll not bother with
13507 		 * the add_vsi call, but we will retrieve the current
13508 		 * VSI context.
13509 		 */
13510 		ctxt.seid = pf->main_vsi_seid;
13511 		ctxt.pf_num = pf->hw.pf_id;
13512 		ctxt.vf_num = 0;
13513 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13514 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13515 		if (ret) {
13516 			dev_info(&pf->pdev->dev,
13517 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13518 				 i40e_stat_str(&pf->hw, ret),
13519 				 i40e_aq_str(&pf->hw,
13520 					     pf->hw.aq.asq_last_status));
13521 			return -ENOENT;
13522 		}
13523 		vsi->info = ctxt.info;
13524 		vsi->info.valid_sections = 0;
13525 
13526 		vsi->seid = ctxt.seid;
13527 		vsi->id = ctxt.vsi_number;
13528 
13529 		enabled_tc = i40e_pf_get_tc_map(pf);
13530 
13531 		/* Source pruning is enabled by default, so the flag is
13532 		 * negative logic - if it's set, we need to fiddle with
13533 		 * the VSI to disable source pruning.
13534 		 */
13535 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13536 			memset(&ctxt, 0, sizeof(ctxt));
13537 			ctxt.seid = pf->main_vsi_seid;
13538 			ctxt.pf_num = pf->hw.pf_id;
13539 			ctxt.vf_num = 0;
13540 			ctxt.info.valid_sections |=
13541 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13542 			ctxt.info.switch_id =
13543 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13544 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13545 			if (ret) {
13546 				dev_info(&pf->pdev->dev,
13547 					 "update vsi failed, err %s aq_err %s\n",
13548 					 i40e_stat_str(&pf->hw, ret),
13549 					 i40e_aq_str(&pf->hw,
13550 						     pf->hw.aq.asq_last_status));
13551 				ret = -ENOENT;
13552 				goto err;
13553 			}
13554 		}
13555 
13556 		/* MFP mode setup queue map and update VSI */
13557 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13558 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13559 			memset(&ctxt, 0, sizeof(ctxt));
13560 			ctxt.seid = pf->main_vsi_seid;
13561 			ctxt.pf_num = pf->hw.pf_id;
13562 			ctxt.vf_num = 0;
13563 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13564 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13565 			if (ret) {
13566 				dev_info(&pf->pdev->dev,
13567 					 "update vsi failed, err %s aq_err %s\n",
13568 					 i40e_stat_str(&pf->hw, ret),
13569 					 i40e_aq_str(&pf->hw,
13570 						    pf->hw.aq.asq_last_status));
13571 				ret = -ENOENT;
13572 				goto err;
13573 			}
13574 			/* update the local VSI info queue map */
13575 			i40e_vsi_update_queue_map(vsi, &ctxt);
13576 			vsi->info.valid_sections = 0;
13577 		} else {
13578 			/* Default/Main VSI is only enabled for TC0
13579 			 * reconfigure it to enable all TCs that are
13580 			 * available on the port in SFP mode.
13581 			 * For MFP case the iSCSI PF would use this
13582 			 * flow to enable LAN+iSCSI TC.
13583 			 */
13584 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13585 			if (ret) {
13586 				/* Single TC condition is not fatal,
13587 				 * message and continue
13588 				 */
13589 				dev_info(&pf->pdev->dev,
13590 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13591 					 enabled_tc,
13592 					 i40e_stat_str(&pf->hw, ret),
13593 					 i40e_aq_str(&pf->hw,
13594 						    pf->hw.aq.asq_last_status));
13595 			}
13596 		}
13597 		break;
13598 
13599 	case I40E_VSI_FDIR:
13600 		ctxt.pf_num = hw->pf_id;
13601 		ctxt.vf_num = 0;
13602 		ctxt.uplink_seid = vsi->uplink_seid;
13603 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13604 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13605 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13606 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13607 			ctxt.info.valid_sections |=
13608 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13609 			ctxt.info.switch_id =
13610 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13611 		}
13612 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13613 		break;
13614 
13615 	case I40E_VSI_VMDQ2:
13616 		ctxt.pf_num = hw->pf_id;
13617 		ctxt.vf_num = 0;
13618 		ctxt.uplink_seid = vsi->uplink_seid;
13619 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13620 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13621 
13622 		/* This VSI is connected to VEB so the switch_id
13623 		 * should be set to zero by default.
13624 		 */
13625 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13626 			ctxt.info.valid_sections |=
13627 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13628 			ctxt.info.switch_id =
13629 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13630 		}
13631 
13632 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13633 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13634 		break;
13635 
13636 	case I40E_VSI_SRIOV:
13637 		ctxt.pf_num = hw->pf_id;
13638 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13639 		ctxt.uplink_seid = vsi->uplink_seid;
13640 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13641 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13642 
13643 		/* This VSI is connected to VEB so the switch_id
13644 		 * should be set to zero by default.
13645 		 */
13646 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13647 			ctxt.info.valid_sections |=
13648 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13649 			ctxt.info.switch_id =
13650 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13651 		}
13652 
13653 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
13654 			ctxt.info.valid_sections |=
13655 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
13656 			ctxt.info.queueing_opt_flags |=
13657 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
13658 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
13659 		}
13660 
13661 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
13662 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
13663 		if (pf->vf[vsi->vf_id].spoofchk) {
13664 			ctxt.info.valid_sections |=
13665 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
13666 			ctxt.info.sec_flags |=
13667 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
13668 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
13669 		}
13670 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13671 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13672 		break;
13673 
13674 	case I40E_VSI_IWARP:
13675 		/* send down message to iWARP */
13676 		break;
13677 
13678 	default:
13679 		return -ENODEV;
13680 	}
13681 
13682 	if (vsi->type != I40E_VSI_MAIN) {
13683 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
13684 		if (ret) {
13685 			dev_info(&vsi->back->pdev->dev,
13686 				 "add vsi failed, err %s aq_err %s\n",
13687 				 i40e_stat_str(&pf->hw, ret),
13688 				 i40e_aq_str(&pf->hw,
13689 					     pf->hw.aq.asq_last_status));
13690 			ret = -ENOENT;
13691 			goto err;
13692 		}
13693 		vsi->info = ctxt.info;
13694 		vsi->info.valid_sections = 0;
13695 		vsi->seid = ctxt.seid;
13696 		vsi->id = ctxt.vsi_number;
13697 	}
13698 
13699 	vsi->active_filters = 0;
13700 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
13701 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13702 	/* If macvlan filters already exist, force them to get loaded */
13703 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
13704 		f->state = I40E_FILTER_NEW;
13705 		f_count++;
13706 	}
13707 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13708 
13709 	if (f_count) {
13710 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
13711 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
13712 	}
13713 
13714 	/* Update VSI BW information */
13715 	ret = i40e_vsi_get_bw_info(vsi);
13716 	if (ret) {
13717 		dev_info(&pf->pdev->dev,
13718 			 "couldn't get vsi bw info, err %s aq_err %s\n",
13719 			 i40e_stat_str(&pf->hw, ret),
13720 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13721 		/* VSI is already added so not tearing that up */
13722 		ret = 0;
13723 	}
13724 
13725 err:
13726 	return ret;
13727 }
13728 
13729 /**
13730  * i40e_vsi_release - Delete a VSI and free its resources
13731  * @vsi: the VSI being removed
13732  *
13733  * Returns 0 on success or < 0 on error
13734  **/
13735 int i40e_vsi_release(struct i40e_vsi *vsi)
13736 {
13737 	struct i40e_mac_filter *f;
13738 	struct hlist_node *h;
13739 	struct i40e_veb *veb = NULL;
13740 	struct i40e_pf *pf;
13741 	u16 uplink_seid;
13742 	int i, n, bkt;
13743 
13744 	pf = vsi->back;
13745 
13746 	/* release of a VEB-owner or last VSI is not allowed */
13747 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
13748 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
13749 			 vsi->seid, vsi->uplink_seid);
13750 		return -ENODEV;
13751 	}
13752 	if (vsi == pf->vsi[pf->lan_vsi] &&
13753 	    !test_bit(__I40E_DOWN, pf->state)) {
13754 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
13755 		return -ENODEV;
13756 	}
13757 
13758 	uplink_seid = vsi->uplink_seid;
13759 	if (vsi->type != I40E_VSI_SRIOV) {
13760 		if (vsi->netdev_registered) {
13761 			vsi->netdev_registered = false;
13762 			if (vsi->netdev) {
13763 				/* results in a call to i40e_close() */
13764 				unregister_netdev(vsi->netdev);
13765 			}
13766 		} else {
13767 			i40e_vsi_close(vsi);
13768 		}
13769 		i40e_vsi_disable_irq(vsi);
13770 	}
13771 
13772 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13773 
13774 	/* clear the sync flag on all filters */
13775 	if (vsi->netdev) {
13776 		__dev_uc_unsync(vsi->netdev, NULL);
13777 		__dev_mc_unsync(vsi->netdev, NULL);
13778 	}
13779 
13780 	/* make sure any remaining filters are marked for deletion */
13781 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
13782 		__i40e_del_filter(vsi, f);
13783 
13784 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13785 
13786 	i40e_sync_vsi_filters(vsi);
13787 
13788 	i40e_vsi_delete(vsi);
13789 	i40e_vsi_free_q_vectors(vsi);
13790 	if (vsi->netdev) {
13791 		free_netdev(vsi->netdev);
13792 		vsi->netdev = NULL;
13793 	}
13794 	i40e_vsi_clear_rings(vsi);
13795 	i40e_vsi_clear(vsi);
13796 
13797 	/* If this was the last thing on the VEB, except for the
13798 	 * controlling VSI, remove the VEB, which puts the controlling
13799 	 * VSI onto the next level down in the switch.
13800 	 *
13801 	 * Well, okay, there's one more exception here: don't remove
13802 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
13803 	 * from up the network stack.
13804 	 */
13805 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
13806 		if (pf->vsi[i] &&
13807 		    pf->vsi[i]->uplink_seid == uplink_seid &&
13808 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13809 			n++;      /* count the VSIs */
13810 		}
13811 	}
13812 	for (i = 0; i < I40E_MAX_VEB; i++) {
13813 		if (!pf->veb[i])
13814 			continue;
13815 		if (pf->veb[i]->uplink_seid == uplink_seid)
13816 			n++;     /* count the VEBs */
13817 		if (pf->veb[i]->seid == uplink_seid)
13818 			veb = pf->veb[i];
13819 	}
13820 	if (n == 0 && veb && veb->uplink_seid != 0)
13821 		i40e_veb_release(veb);
13822 
13823 	return 0;
13824 }
13825 
13826 /**
13827  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
13828  * @vsi: ptr to the VSI
13829  *
13830  * This should only be called after i40e_vsi_mem_alloc() which allocates the
13831  * corresponding SW VSI structure and initializes num_queue_pairs for the
13832  * newly allocated VSI.
13833  *
13834  * Returns 0 on success or negative on failure
13835  **/
13836 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
13837 {
13838 	int ret = -ENOENT;
13839 	struct i40e_pf *pf = vsi->back;
13840 
13841 	if (vsi->q_vectors[0]) {
13842 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
13843 			 vsi->seid);
13844 		return -EEXIST;
13845 	}
13846 
13847 	if (vsi->base_vector) {
13848 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
13849 			 vsi->seid, vsi->base_vector);
13850 		return -EEXIST;
13851 	}
13852 
13853 	ret = i40e_vsi_alloc_q_vectors(vsi);
13854 	if (ret) {
13855 		dev_info(&pf->pdev->dev,
13856 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
13857 			 vsi->num_q_vectors, vsi->seid, ret);
13858 		vsi->num_q_vectors = 0;
13859 		goto vector_setup_out;
13860 	}
13861 
13862 	/* In Legacy mode, we do not have to get any other vector since we
13863 	 * piggyback on the misc/ICR0 for queue interrupts.
13864 	*/
13865 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
13866 		return ret;
13867 	if (vsi->num_q_vectors)
13868 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
13869 						 vsi->num_q_vectors, vsi->idx);
13870 	if (vsi->base_vector < 0) {
13871 		dev_info(&pf->pdev->dev,
13872 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
13873 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
13874 		i40e_vsi_free_q_vectors(vsi);
13875 		ret = -ENOENT;
13876 		goto vector_setup_out;
13877 	}
13878 
13879 vector_setup_out:
13880 	return ret;
13881 }
13882 
13883 /**
13884  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
13885  * @vsi: pointer to the vsi.
13886  *
13887  * This re-allocates a vsi's queue resources.
13888  *
13889  * Returns pointer to the successfully allocated and configured VSI sw struct
13890  * on success, otherwise returns NULL on failure.
13891  **/
13892 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
13893 {
13894 	u16 alloc_queue_pairs;
13895 	struct i40e_pf *pf;
13896 	u8 enabled_tc;
13897 	int ret;
13898 
13899 	if (!vsi)
13900 		return NULL;
13901 
13902 	pf = vsi->back;
13903 
13904 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
13905 	i40e_vsi_clear_rings(vsi);
13906 
13907 	i40e_vsi_free_arrays(vsi, false);
13908 	i40e_set_num_rings_in_vsi(vsi);
13909 	ret = i40e_vsi_alloc_arrays(vsi, false);
13910 	if (ret)
13911 		goto err_vsi;
13912 
13913 	alloc_queue_pairs = vsi->alloc_queue_pairs *
13914 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
13915 
13916 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
13917 	if (ret < 0) {
13918 		dev_info(&pf->pdev->dev,
13919 			 "failed to get tracking for %d queues for VSI %d err %d\n",
13920 			 alloc_queue_pairs, vsi->seid, ret);
13921 		goto err_vsi;
13922 	}
13923 	vsi->base_queue = ret;
13924 
13925 	/* Update the FW view of the VSI. Force a reset of TC and queue
13926 	 * layout configurations.
13927 	 */
13928 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
13929 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
13930 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
13931 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
13932 	if (vsi->type == I40E_VSI_MAIN)
13933 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
13934 
13935 	/* assign it some queues */
13936 	ret = i40e_alloc_rings(vsi);
13937 	if (ret)
13938 		goto err_rings;
13939 
13940 	/* map all of the rings to the q_vectors */
13941 	i40e_vsi_map_rings_to_vectors(vsi);
13942 	return vsi;
13943 
13944 err_rings:
13945 	i40e_vsi_free_q_vectors(vsi);
13946 	if (vsi->netdev_registered) {
13947 		vsi->netdev_registered = false;
13948 		unregister_netdev(vsi->netdev);
13949 		free_netdev(vsi->netdev);
13950 		vsi->netdev = NULL;
13951 	}
13952 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
13953 err_vsi:
13954 	i40e_vsi_clear(vsi);
13955 	return NULL;
13956 }
13957 
13958 /**
13959  * i40e_vsi_setup - Set up a VSI by a given type
13960  * @pf: board private structure
13961  * @type: VSI type
13962  * @uplink_seid: the switch element to link to
13963  * @param1: usage depends upon VSI type. For VF types, indicates VF id
13964  *
13965  * This allocates the sw VSI structure and its queue resources, then add a VSI
13966  * to the identified VEB.
13967  *
13968  * Returns pointer to the successfully allocated and configure VSI sw struct on
13969  * success, otherwise returns NULL on failure.
13970  **/
13971 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
13972 				u16 uplink_seid, u32 param1)
13973 {
13974 	struct i40e_vsi *vsi = NULL;
13975 	struct i40e_veb *veb = NULL;
13976 	u16 alloc_queue_pairs;
13977 	int ret, i;
13978 	int v_idx;
13979 
13980 	/* The requested uplink_seid must be either
13981 	 *     - the PF's port seid
13982 	 *              no VEB is needed because this is the PF
13983 	 *              or this is a Flow Director special case VSI
13984 	 *     - seid of an existing VEB
13985 	 *     - seid of a VSI that owns an existing VEB
13986 	 *     - seid of a VSI that doesn't own a VEB
13987 	 *              a new VEB is created and the VSI becomes the owner
13988 	 *     - seid of the PF VSI, which is what creates the first VEB
13989 	 *              this is a special case of the previous
13990 	 *
13991 	 * Find which uplink_seid we were given and create a new VEB if needed
13992 	 */
13993 	for (i = 0; i < I40E_MAX_VEB; i++) {
13994 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
13995 			veb = pf->veb[i];
13996 			break;
13997 		}
13998 	}
13999 
14000 	if (!veb && uplink_seid != pf->mac_seid) {
14001 
14002 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14003 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14004 				vsi = pf->vsi[i];
14005 				break;
14006 			}
14007 		}
14008 		if (!vsi) {
14009 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14010 				 uplink_seid);
14011 			return NULL;
14012 		}
14013 
14014 		if (vsi->uplink_seid == pf->mac_seid)
14015 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14016 					     vsi->tc_config.enabled_tc);
14017 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14018 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14019 					     vsi->tc_config.enabled_tc);
14020 		if (veb) {
14021 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14022 				dev_info(&vsi->back->pdev->dev,
14023 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14024 				return NULL;
14025 			}
14026 			/* We come up by default in VEPA mode if SRIOV is not
14027 			 * already enabled, in which case we can't force VEPA
14028 			 * mode.
14029 			 */
14030 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14031 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14032 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14033 			}
14034 			i40e_config_bridge_mode(veb);
14035 		}
14036 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14037 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14038 				veb = pf->veb[i];
14039 		}
14040 		if (!veb) {
14041 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14042 			return NULL;
14043 		}
14044 
14045 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14046 		uplink_seid = veb->seid;
14047 	}
14048 
14049 	/* get vsi sw struct */
14050 	v_idx = i40e_vsi_mem_alloc(pf, type);
14051 	if (v_idx < 0)
14052 		goto err_alloc;
14053 	vsi = pf->vsi[v_idx];
14054 	if (!vsi)
14055 		goto err_alloc;
14056 	vsi->type = type;
14057 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14058 
14059 	if (type == I40E_VSI_MAIN)
14060 		pf->lan_vsi = v_idx;
14061 	else if (type == I40E_VSI_SRIOV)
14062 		vsi->vf_id = param1;
14063 	/* assign it some queues */
14064 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14065 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14066 
14067 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14068 	if (ret < 0) {
14069 		dev_info(&pf->pdev->dev,
14070 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14071 			 alloc_queue_pairs, vsi->seid, ret);
14072 		goto err_vsi;
14073 	}
14074 	vsi->base_queue = ret;
14075 
14076 	/* get a VSI from the hardware */
14077 	vsi->uplink_seid = uplink_seid;
14078 	ret = i40e_add_vsi(vsi);
14079 	if (ret)
14080 		goto err_vsi;
14081 
14082 	switch (vsi->type) {
14083 	/* setup the netdev if needed */
14084 	case I40E_VSI_MAIN:
14085 	case I40E_VSI_VMDQ2:
14086 		ret = i40e_config_netdev(vsi);
14087 		if (ret)
14088 			goto err_netdev;
14089 		ret = register_netdev(vsi->netdev);
14090 		if (ret)
14091 			goto err_netdev;
14092 		vsi->netdev_registered = true;
14093 		netif_carrier_off(vsi->netdev);
14094 #ifdef CONFIG_I40E_DCB
14095 		/* Setup DCB netlink interface */
14096 		i40e_dcbnl_setup(vsi);
14097 #endif /* CONFIG_I40E_DCB */
14098 		fallthrough;
14099 	case I40E_VSI_FDIR:
14100 		/* set up vectors and rings if needed */
14101 		ret = i40e_vsi_setup_vectors(vsi);
14102 		if (ret)
14103 			goto err_msix;
14104 
14105 		ret = i40e_alloc_rings(vsi);
14106 		if (ret)
14107 			goto err_rings;
14108 
14109 		/* map all of the rings to the q_vectors */
14110 		i40e_vsi_map_rings_to_vectors(vsi);
14111 
14112 		i40e_vsi_reset_stats(vsi);
14113 		break;
14114 	default:
14115 		/* no netdev or rings for the other VSI types */
14116 		break;
14117 	}
14118 
14119 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14120 	    (vsi->type == I40E_VSI_VMDQ2)) {
14121 		ret = i40e_vsi_config_rss(vsi);
14122 	}
14123 	return vsi;
14124 
14125 err_rings:
14126 	i40e_vsi_free_q_vectors(vsi);
14127 err_msix:
14128 	if (vsi->netdev_registered) {
14129 		vsi->netdev_registered = false;
14130 		unregister_netdev(vsi->netdev);
14131 		free_netdev(vsi->netdev);
14132 		vsi->netdev = NULL;
14133 	}
14134 err_netdev:
14135 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14136 err_vsi:
14137 	i40e_vsi_clear(vsi);
14138 err_alloc:
14139 	return NULL;
14140 }
14141 
14142 /**
14143  * i40e_veb_get_bw_info - Query VEB BW information
14144  * @veb: the veb to query
14145  *
14146  * Query the Tx scheduler BW configuration data for given VEB
14147  **/
14148 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14149 {
14150 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14151 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14152 	struct i40e_pf *pf = veb->pf;
14153 	struct i40e_hw *hw = &pf->hw;
14154 	u32 tc_bw_max;
14155 	int ret = 0;
14156 	int i;
14157 
14158 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14159 						  &bw_data, NULL);
14160 	if (ret) {
14161 		dev_info(&pf->pdev->dev,
14162 			 "query veb bw config failed, err %s aq_err %s\n",
14163 			 i40e_stat_str(&pf->hw, ret),
14164 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14165 		goto out;
14166 	}
14167 
14168 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14169 						   &ets_data, NULL);
14170 	if (ret) {
14171 		dev_info(&pf->pdev->dev,
14172 			 "query veb bw ets config failed, err %s aq_err %s\n",
14173 			 i40e_stat_str(&pf->hw, ret),
14174 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14175 		goto out;
14176 	}
14177 
14178 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14179 	veb->bw_max_quanta = ets_data.tc_bw_max;
14180 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14181 	veb->enabled_tc = ets_data.tc_valid_bits;
14182 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14183 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14184 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14185 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14186 		veb->bw_tc_limit_credits[i] =
14187 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14188 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14189 	}
14190 
14191 out:
14192 	return ret;
14193 }
14194 
14195 /**
14196  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14197  * @pf: board private structure
14198  *
14199  * On error: returns error code (negative)
14200  * On success: returns vsi index in PF (positive)
14201  **/
14202 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14203 {
14204 	int ret = -ENOENT;
14205 	struct i40e_veb *veb;
14206 	int i;
14207 
14208 	/* Need to protect the allocation of switch elements at the PF level */
14209 	mutex_lock(&pf->switch_mutex);
14210 
14211 	/* VEB list may be fragmented if VEB creation/destruction has
14212 	 * been happening.  We can afford to do a quick scan to look
14213 	 * for any free slots in the list.
14214 	 *
14215 	 * find next empty veb slot, looping back around if necessary
14216 	 */
14217 	i = 0;
14218 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14219 		i++;
14220 	if (i >= I40E_MAX_VEB) {
14221 		ret = -ENOMEM;
14222 		goto err_alloc_veb;  /* out of VEB slots! */
14223 	}
14224 
14225 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14226 	if (!veb) {
14227 		ret = -ENOMEM;
14228 		goto err_alloc_veb;
14229 	}
14230 	veb->pf = pf;
14231 	veb->idx = i;
14232 	veb->enabled_tc = 1;
14233 
14234 	pf->veb[i] = veb;
14235 	ret = i;
14236 err_alloc_veb:
14237 	mutex_unlock(&pf->switch_mutex);
14238 	return ret;
14239 }
14240 
14241 /**
14242  * i40e_switch_branch_release - Delete a branch of the switch tree
14243  * @branch: where to start deleting
14244  *
14245  * This uses recursion to find the tips of the branch to be
14246  * removed, deleting until we get back to and can delete this VEB.
14247  **/
14248 static void i40e_switch_branch_release(struct i40e_veb *branch)
14249 {
14250 	struct i40e_pf *pf = branch->pf;
14251 	u16 branch_seid = branch->seid;
14252 	u16 veb_idx = branch->idx;
14253 	int i;
14254 
14255 	/* release any VEBs on this VEB - RECURSION */
14256 	for (i = 0; i < I40E_MAX_VEB; i++) {
14257 		if (!pf->veb[i])
14258 			continue;
14259 		if (pf->veb[i]->uplink_seid == branch->seid)
14260 			i40e_switch_branch_release(pf->veb[i]);
14261 	}
14262 
14263 	/* Release the VSIs on this VEB, but not the owner VSI.
14264 	 *
14265 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14266 	 *       the VEB itself, so don't use (*branch) after this loop.
14267 	 */
14268 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14269 		if (!pf->vsi[i])
14270 			continue;
14271 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14272 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14273 			i40e_vsi_release(pf->vsi[i]);
14274 		}
14275 	}
14276 
14277 	/* There's one corner case where the VEB might not have been
14278 	 * removed, so double check it here and remove it if needed.
14279 	 * This case happens if the veb was created from the debugfs
14280 	 * commands and no VSIs were added to it.
14281 	 */
14282 	if (pf->veb[veb_idx])
14283 		i40e_veb_release(pf->veb[veb_idx]);
14284 }
14285 
14286 /**
14287  * i40e_veb_clear - remove veb struct
14288  * @veb: the veb to remove
14289  **/
14290 static void i40e_veb_clear(struct i40e_veb *veb)
14291 {
14292 	if (!veb)
14293 		return;
14294 
14295 	if (veb->pf) {
14296 		struct i40e_pf *pf = veb->pf;
14297 
14298 		mutex_lock(&pf->switch_mutex);
14299 		if (pf->veb[veb->idx] == veb)
14300 			pf->veb[veb->idx] = NULL;
14301 		mutex_unlock(&pf->switch_mutex);
14302 	}
14303 
14304 	kfree(veb);
14305 }
14306 
14307 /**
14308  * i40e_veb_release - Delete a VEB and free its resources
14309  * @veb: the VEB being removed
14310  **/
14311 void i40e_veb_release(struct i40e_veb *veb)
14312 {
14313 	struct i40e_vsi *vsi = NULL;
14314 	struct i40e_pf *pf;
14315 	int i, n = 0;
14316 
14317 	pf = veb->pf;
14318 
14319 	/* find the remaining VSI and check for extras */
14320 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14321 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14322 			n++;
14323 			vsi = pf->vsi[i];
14324 		}
14325 	}
14326 	if (n != 1) {
14327 		dev_info(&pf->pdev->dev,
14328 			 "can't remove VEB %d with %d VSIs left\n",
14329 			 veb->seid, n);
14330 		return;
14331 	}
14332 
14333 	/* move the remaining VSI to uplink veb */
14334 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14335 	if (veb->uplink_seid) {
14336 		vsi->uplink_seid = veb->uplink_seid;
14337 		if (veb->uplink_seid == pf->mac_seid)
14338 			vsi->veb_idx = I40E_NO_VEB;
14339 		else
14340 			vsi->veb_idx = veb->veb_idx;
14341 	} else {
14342 		/* floating VEB */
14343 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14344 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14345 	}
14346 
14347 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14348 	i40e_veb_clear(veb);
14349 }
14350 
14351 /**
14352  * i40e_add_veb - create the VEB in the switch
14353  * @veb: the VEB to be instantiated
14354  * @vsi: the controlling VSI
14355  **/
14356 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14357 {
14358 	struct i40e_pf *pf = veb->pf;
14359 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14360 	int ret;
14361 
14362 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14363 			      veb->enabled_tc, false,
14364 			      &veb->seid, enable_stats, NULL);
14365 
14366 	/* get a VEB from the hardware */
14367 	if (ret) {
14368 		dev_info(&pf->pdev->dev,
14369 			 "couldn't add VEB, err %s aq_err %s\n",
14370 			 i40e_stat_str(&pf->hw, ret),
14371 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14372 		return -EPERM;
14373 	}
14374 
14375 	/* get statistics counter */
14376 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14377 					 &veb->stats_idx, NULL, NULL, NULL);
14378 	if (ret) {
14379 		dev_info(&pf->pdev->dev,
14380 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
14381 			 i40e_stat_str(&pf->hw, ret),
14382 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14383 		return -EPERM;
14384 	}
14385 	ret = i40e_veb_get_bw_info(veb);
14386 	if (ret) {
14387 		dev_info(&pf->pdev->dev,
14388 			 "couldn't get VEB bw info, err %s aq_err %s\n",
14389 			 i40e_stat_str(&pf->hw, ret),
14390 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14391 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14392 		return -ENOENT;
14393 	}
14394 
14395 	vsi->uplink_seid = veb->seid;
14396 	vsi->veb_idx = veb->idx;
14397 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14398 
14399 	return 0;
14400 }
14401 
14402 /**
14403  * i40e_veb_setup - Set up a VEB
14404  * @pf: board private structure
14405  * @flags: VEB setup flags
14406  * @uplink_seid: the switch element to link to
14407  * @vsi_seid: the initial VSI seid
14408  * @enabled_tc: Enabled TC bit-map
14409  *
14410  * This allocates the sw VEB structure and links it into the switch
14411  * It is possible and legal for this to be a duplicate of an already
14412  * existing VEB.  It is also possible for both uplink and vsi seids
14413  * to be zero, in order to create a floating VEB.
14414  *
14415  * Returns pointer to the successfully allocated VEB sw struct on
14416  * success, otherwise returns NULL on failure.
14417  **/
14418 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14419 				u16 uplink_seid, u16 vsi_seid,
14420 				u8 enabled_tc)
14421 {
14422 	struct i40e_veb *veb, *uplink_veb = NULL;
14423 	int vsi_idx, veb_idx;
14424 	int ret;
14425 
14426 	/* if one seid is 0, the other must be 0 to create a floating relay */
14427 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14428 	    (uplink_seid + vsi_seid != 0)) {
14429 		dev_info(&pf->pdev->dev,
14430 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14431 			 uplink_seid, vsi_seid);
14432 		return NULL;
14433 	}
14434 
14435 	/* make sure there is such a vsi and uplink */
14436 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14437 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14438 			break;
14439 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14440 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14441 			 vsi_seid);
14442 		return NULL;
14443 	}
14444 
14445 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14446 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14447 			if (pf->veb[veb_idx] &&
14448 			    pf->veb[veb_idx]->seid == uplink_seid) {
14449 				uplink_veb = pf->veb[veb_idx];
14450 				break;
14451 			}
14452 		}
14453 		if (!uplink_veb) {
14454 			dev_info(&pf->pdev->dev,
14455 				 "uplink seid %d not found\n", uplink_seid);
14456 			return NULL;
14457 		}
14458 	}
14459 
14460 	/* get veb sw struct */
14461 	veb_idx = i40e_veb_mem_alloc(pf);
14462 	if (veb_idx < 0)
14463 		goto err_alloc;
14464 	veb = pf->veb[veb_idx];
14465 	veb->flags = flags;
14466 	veb->uplink_seid = uplink_seid;
14467 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14468 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14469 
14470 	/* create the VEB in the switch */
14471 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14472 	if (ret)
14473 		goto err_veb;
14474 	if (vsi_idx == pf->lan_vsi)
14475 		pf->lan_veb = veb->idx;
14476 
14477 	return veb;
14478 
14479 err_veb:
14480 	i40e_veb_clear(veb);
14481 err_alloc:
14482 	return NULL;
14483 }
14484 
14485 /**
14486  * i40e_setup_pf_switch_element - set PF vars based on switch type
14487  * @pf: board private structure
14488  * @ele: element we are building info from
14489  * @num_reported: total number of elements
14490  * @printconfig: should we print the contents
14491  *
14492  * helper function to assist in extracting a few useful SEID values.
14493  **/
14494 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14495 				struct i40e_aqc_switch_config_element_resp *ele,
14496 				u16 num_reported, bool printconfig)
14497 {
14498 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14499 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14500 	u8 element_type = ele->element_type;
14501 	u16 seid = le16_to_cpu(ele->seid);
14502 
14503 	if (printconfig)
14504 		dev_info(&pf->pdev->dev,
14505 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14506 			 element_type, seid, uplink_seid, downlink_seid);
14507 
14508 	switch (element_type) {
14509 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14510 		pf->mac_seid = seid;
14511 		break;
14512 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14513 		/* Main VEB? */
14514 		if (uplink_seid != pf->mac_seid)
14515 			break;
14516 		if (pf->lan_veb >= I40E_MAX_VEB) {
14517 			int v;
14518 
14519 			/* find existing or else empty VEB */
14520 			for (v = 0; v < I40E_MAX_VEB; v++) {
14521 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14522 					pf->lan_veb = v;
14523 					break;
14524 				}
14525 			}
14526 			if (pf->lan_veb >= I40E_MAX_VEB) {
14527 				v = i40e_veb_mem_alloc(pf);
14528 				if (v < 0)
14529 					break;
14530 				pf->lan_veb = v;
14531 			}
14532 		}
14533 		if (pf->lan_veb >= I40E_MAX_VEB)
14534 			break;
14535 
14536 		pf->veb[pf->lan_veb]->seid = seid;
14537 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14538 		pf->veb[pf->lan_veb]->pf = pf;
14539 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14540 		break;
14541 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14542 		if (num_reported != 1)
14543 			break;
14544 		/* This is immediately after a reset so we can assume this is
14545 		 * the PF's VSI
14546 		 */
14547 		pf->mac_seid = uplink_seid;
14548 		pf->pf_seid = downlink_seid;
14549 		pf->main_vsi_seid = seid;
14550 		if (printconfig)
14551 			dev_info(&pf->pdev->dev,
14552 				 "pf_seid=%d main_vsi_seid=%d\n",
14553 				 pf->pf_seid, pf->main_vsi_seid);
14554 		break;
14555 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14556 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14557 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14558 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14559 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14560 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14561 		/* ignore these for now */
14562 		break;
14563 	default:
14564 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14565 			 element_type, seid);
14566 		break;
14567 	}
14568 }
14569 
14570 /**
14571  * i40e_fetch_switch_configuration - Get switch config from firmware
14572  * @pf: board private structure
14573  * @printconfig: should we print the contents
14574  *
14575  * Get the current switch configuration from the device and
14576  * extract a few useful SEID values.
14577  **/
14578 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14579 {
14580 	struct i40e_aqc_get_switch_config_resp *sw_config;
14581 	u16 next_seid = 0;
14582 	int ret = 0;
14583 	u8 *aq_buf;
14584 	int i;
14585 
14586 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14587 	if (!aq_buf)
14588 		return -ENOMEM;
14589 
14590 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14591 	do {
14592 		u16 num_reported, num_total;
14593 
14594 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14595 						I40E_AQ_LARGE_BUF,
14596 						&next_seid, NULL);
14597 		if (ret) {
14598 			dev_info(&pf->pdev->dev,
14599 				 "get switch config failed err %s aq_err %s\n",
14600 				 i40e_stat_str(&pf->hw, ret),
14601 				 i40e_aq_str(&pf->hw,
14602 					     pf->hw.aq.asq_last_status));
14603 			kfree(aq_buf);
14604 			return -ENOENT;
14605 		}
14606 
14607 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14608 		num_total = le16_to_cpu(sw_config->header.num_total);
14609 
14610 		if (printconfig)
14611 			dev_info(&pf->pdev->dev,
14612 				 "header: %d reported %d total\n",
14613 				 num_reported, num_total);
14614 
14615 		for (i = 0; i < num_reported; i++) {
14616 			struct i40e_aqc_switch_config_element_resp *ele =
14617 				&sw_config->element[i];
14618 
14619 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14620 						     printconfig);
14621 		}
14622 	} while (next_seid != 0);
14623 
14624 	kfree(aq_buf);
14625 	return ret;
14626 }
14627 
14628 /**
14629  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14630  * @pf: board private structure
14631  * @reinit: if the Main VSI needs to re-initialized.
14632  * @lock_acquired: indicates whether or not the lock has been acquired
14633  *
14634  * Returns 0 on success, negative value on failure
14635  **/
14636 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14637 {
14638 	u16 flags = 0;
14639 	int ret;
14640 
14641 	/* find out what's out there already */
14642 	ret = i40e_fetch_switch_configuration(pf, false);
14643 	if (ret) {
14644 		dev_info(&pf->pdev->dev,
14645 			 "couldn't fetch switch config, err %s aq_err %s\n",
14646 			 i40e_stat_str(&pf->hw, ret),
14647 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14648 		return ret;
14649 	}
14650 	i40e_pf_reset_stats(pf);
14651 
14652 	/* set the switch config bit for the whole device to
14653 	 * support limited promisc or true promisc
14654 	 * when user requests promisc. The default is limited
14655 	 * promisc.
14656 	*/
14657 
14658 	if ((pf->hw.pf_id == 0) &&
14659 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
14660 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14661 		pf->last_sw_conf_flags = flags;
14662 	}
14663 
14664 	if (pf->hw.pf_id == 0) {
14665 		u16 valid_flags;
14666 
14667 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14668 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
14669 						NULL);
14670 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
14671 			dev_info(&pf->pdev->dev,
14672 				 "couldn't set switch config bits, err %s aq_err %s\n",
14673 				 i40e_stat_str(&pf->hw, ret),
14674 				 i40e_aq_str(&pf->hw,
14675 					     pf->hw.aq.asq_last_status));
14676 			/* not a fatal problem, just keep going */
14677 		}
14678 		pf->last_sw_conf_valid_flags = valid_flags;
14679 	}
14680 
14681 	/* first time setup */
14682 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
14683 		struct i40e_vsi *vsi = NULL;
14684 		u16 uplink_seid;
14685 
14686 		/* Set up the PF VSI associated with the PF's main VSI
14687 		 * that is already in the HW switch
14688 		 */
14689 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
14690 			uplink_seid = pf->veb[pf->lan_veb]->seid;
14691 		else
14692 			uplink_seid = pf->mac_seid;
14693 		if (pf->lan_vsi == I40E_NO_VSI)
14694 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
14695 		else if (reinit)
14696 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
14697 		if (!vsi) {
14698 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
14699 			i40e_cloud_filter_exit(pf);
14700 			i40e_fdir_teardown(pf);
14701 			return -EAGAIN;
14702 		}
14703 	} else {
14704 		/* force a reset of TC and queue layout configurations */
14705 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14706 
14707 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14708 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14709 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14710 	}
14711 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
14712 
14713 	i40e_fdir_sb_setup(pf);
14714 
14715 	/* Setup static PF queue filter control settings */
14716 	ret = i40e_setup_pf_filter_control(pf);
14717 	if (ret) {
14718 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
14719 			 ret);
14720 		/* Failure here should not stop continuing other steps */
14721 	}
14722 
14723 	/* enable RSS in the HW, even for only one queue, as the stack can use
14724 	 * the hash
14725 	 */
14726 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
14727 		i40e_pf_config_rss(pf);
14728 
14729 	/* fill in link information and enable LSE reporting */
14730 	i40e_link_event(pf);
14731 
14732 	/* Initialize user-specific link properties */
14733 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
14734 				  I40E_AQ_AN_COMPLETED) ? true : false);
14735 
14736 	i40e_ptp_init(pf);
14737 
14738 	if (!lock_acquired)
14739 		rtnl_lock();
14740 
14741 	/* repopulate tunnel port filters */
14742 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
14743 
14744 	if (!lock_acquired)
14745 		rtnl_unlock();
14746 
14747 	return ret;
14748 }
14749 
14750 /**
14751  * i40e_determine_queue_usage - Work out queue distribution
14752  * @pf: board private structure
14753  **/
14754 static void i40e_determine_queue_usage(struct i40e_pf *pf)
14755 {
14756 	int queues_left;
14757 	int q_max;
14758 
14759 	pf->num_lan_qps = 0;
14760 
14761 	/* Find the max queues to be put into basic use.  We'll always be
14762 	 * using TC0, whether or not DCB is running, and TC0 will get the
14763 	 * big RSS set.
14764 	 */
14765 	queues_left = pf->hw.func_caps.num_tx_qp;
14766 
14767 	if ((queues_left == 1) ||
14768 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
14769 		/* one qp for PF, no queues for anything else */
14770 		queues_left = 0;
14771 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14772 
14773 		/* make sure all the fancies are disabled */
14774 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14775 			       I40E_FLAG_IWARP_ENABLED	|
14776 			       I40E_FLAG_FD_SB_ENABLED	|
14777 			       I40E_FLAG_FD_ATR_ENABLED	|
14778 			       I40E_FLAG_DCB_CAPABLE	|
14779 			       I40E_FLAG_DCB_ENABLED	|
14780 			       I40E_FLAG_SRIOV_ENABLED	|
14781 			       I40E_FLAG_VMDQ_ENABLED);
14782 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14783 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
14784 				  I40E_FLAG_FD_SB_ENABLED |
14785 				  I40E_FLAG_FD_ATR_ENABLED |
14786 				  I40E_FLAG_DCB_CAPABLE))) {
14787 		/* one qp for PF */
14788 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14789 		queues_left -= pf->num_lan_qps;
14790 
14791 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14792 			       I40E_FLAG_IWARP_ENABLED	|
14793 			       I40E_FLAG_FD_SB_ENABLED	|
14794 			       I40E_FLAG_FD_ATR_ENABLED	|
14795 			       I40E_FLAG_DCB_ENABLED	|
14796 			       I40E_FLAG_VMDQ_ENABLED);
14797 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14798 	} else {
14799 		/* Not enough queues for all TCs */
14800 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
14801 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
14802 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
14803 					I40E_FLAG_DCB_ENABLED);
14804 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
14805 		}
14806 
14807 		/* limit lan qps to the smaller of qps, cpus or msix */
14808 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
14809 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
14810 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
14811 		pf->num_lan_qps = q_max;
14812 
14813 		queues_left -= pf->num_lan_qps;
14814 	}
14815 
14816 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14817 		if (queues_left > 1) {
14818 			queues_left -= 1; /* save 1 queue for FD */
14819 		} else {
14820 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
14821 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14822 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
14823 		}
14824 	}
14825 
14826 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
14827 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
14828 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
14829 					(queues_left / pf->num_vf_qps));
14830 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
14831 	}
14832 
14833 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
14834 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
14835 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
14836 					  (queues_left / pf->num_vmdq_qps));
14837 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
14838 	}
14839 
14840 	pf->queues_left = queues_left;
14841 	dev_dbg(&pf->pdev->dev,
14842 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
14843 		pf->hw.func_caps.num_tx_qp,
14844 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
14845 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
14846 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
14847 		queues_left);
14848 }
14849 
14850 /**
14851  * i40e_setup_pf_filter_control - Setup PF static filter control
14852  * @pf: PF to be setup
14853  *
14854  * i40e_setup_pf_filter_control sets up a PF's initial filter control
14855  * settings. If PE/FCoE are enabled then it will also set the per PF
14856  * based filter sizes required for them. It also enables Flow director,
14857  * ethertype and macvlan type filter settings for the pf.
14858  *
14859  * Returns 0 on success, negative on failure
14860  **/
14861 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
14862 {
14863 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
14864 
14865 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
14866 
14867 	/* Flow Director is enabled */
14868 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
14869 		settings->enable_fdir = true;
14870 
14871 	/* Ethtype and MACVLAN filters enabled for PF */
14872 	settings->enable_ethtype = true;
14873 	settings->enable_macvlan = true;
14874 
14875 	if (i40e_set_filter_control(&pf->hw, settings))
14876 		return -ENOENT;
14877 
14878 	return 0;
14879 }
14880 
14881 #define INFO_STRING_LEN 255
14882 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
14883 static void i40e_print_features(struct i40e_pf *pf)
14884 {
14885 	struct i40e_hw *hw = &pf->hw;
14886 	char *buf;
14887 	int i;
14888 
14889 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
14890 	if (!buf)
14891 		return;
14892 
14893 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
14894 #ifdef CONFIG_PCI_IOV
14895 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
14896 #endif
14897 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
14898 		      pf->hw.func_caps.num_vsis,
14899 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
14900 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
14901 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
14902 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
14903 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
14904 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14905 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
14906 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
14907 	}
14908 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
14909 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
14910 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
14911 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
14912 	if (pf->flags & I40E_FLAG_PTP)
14913 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
14914 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
14915 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
14916 	else
14917 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
14918 
14919 	dev_info(&pf->pdev->dev, "%s\n", buf);
14920 	kfree(buf);
14921 	WARN_ON(i > INFO_STRING_LEN);
14922 }
14923 
14924 /**
14925  * i40e_get_platform_mac_addr - get platform-specific MAC address
14926  * @pdev: PCI device information struct
14927  * @pf: board private structure
14928  *
14929  * Look up the MAC address for the device. First we'll try
14930  * eth_platform_get_mac_address, which will check Open Firmware, or arch
14931  * specific fallback. Otherwise, we'll default to the stored value in
14932  * firmware.
14933  **/
14934 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
14935 {
14936 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
14937 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
14938 }
14939 
14940 /**
14941  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
14942  * @fec_cfg: FEC option to set in flags
14943  * @flags: ptr to flags in which we set FEC option
14944  **/
14945 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
14946 {
14947 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
14948 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
14949 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
14950 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
14951 		*flags |= I40E_FLAG_RS_FEC;
14952 		*flags &= ~I40E_FLAG_BASE_R_FEC;
14953 	}
14954 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
14955 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
14956 		*flags |= I40E_FLAG_BASE_R_FEC;
14957 		*flags &= ~I40E_FLAG_RS_FEC;
14958 	}
14959 	if (fec_cfg == 0)
14960 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
14961 }
14962 
14963 /**
14964  * i40e_check_recovery_mode - check if we are running transition firmware
14965  * @pf: board private structure
14966  *
14967  * Check registers indicating the firmware runs in recovery mode. Sets the
14968  * appropriate driver state.
14969  *
14970  * Returns true if the recovery mode was detected, false otherwise
14971  **/
14972 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
14973 {
14974 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
14975 
14976 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
14977 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
14978 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
14979 		set_bit(__I40E_RECOVERY_MODE, pf->state);
14980 
14981 		return true;
14982 	}
14983 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
14984 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
14985 
14986 	return false;
14987 }
14988 
14989 /**
14990  * i40e_pf_loop_reset - perform reset in a loop.
14991  * @pf: board private structure
14992  *
14993  * This function is useful when a NIC is about to enter recovery mode.
14994  * When a NIC's internal data structures are corrupted the NIC's
14995  * firmware is going to enter recovery mode.
14996  * Right after a POR it takes about 7 minutes for firmware to enter
14997  * recovery mode. Until that time a NIC is in some kind of intermediate
14998  * state. After that time period the NIC almost surely enters
14999  * recovery mode. The only way for a driver to detect intermediate
15000  * state is to issue a series of pf-resets and check a return value.
15001  * If a PF reset returns success then the firmware could be in recovery
15002  * mode so the caller of this code needs to check for recovery mode
15003  * if this function returns success. There is a little chance that
15004  * firmware will hang in intermediate state forever.
15005  * Since waiting 7 minutes is quite a lot of time this function waits
15006  * 10 seconds and then gives up by returning an error.
15007  *
15008  * Return 0 on success, negative on failure.
15009  **/
15010 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
15011 {
15012 	/* wait max 10 seconds for PF reset to succeed */
15013 	const unsigned long time_end = jiffies + 10 * HZ;
15014 
15015 	struct i40e_hw *hw = &pf->hw;
15016 	i40e_status ret;
15017 
15018 	ret = i40e_pf_reset(hw);
15019 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15020 		usleep_range(10000, 20000);
15021 		ret = i40e_pf_reset(hw);
15022 	}
15023 
15024 	if (ret == I40E_SUCCESS)
15025 		pf->pfr_count++;
15026 	else
15027 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15028 
15029 	return ret;
15030 }
15031 
15032 /**
15033  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15034  * @pf: board private structure
15035  *
15036  * Check FW registers to determine if FW issued unexpected EMP Reset.
15037  * Every time when unexpected EMP Reset occurs the FW increments
15038  * a counter of unexpected EMP Resets. When the counter reaches 10
15039  * the FW should enter the Recovery mode
15040  *
15041  * Returns true if FW issued unexpected EMP Reset
15042  **/
15043 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15044 {
15045 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15046 			   I40E_GL_FWSTS_FWS1B_MASK;
15047 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15048 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15049 }
15050 
15051 /**
15052  * i40e_handle_resets - handle EMP resets and PF resets
15053  * @pf: board private structure
15054  *
15055  * Handle both EMP resets and PF resets and conclude whether there are
15056  * any issues regarding these resets. If there are any issues then
15057  * generate log entry.
15058  *
15059  * Return 0 if NIC is healthy or negative value when there are issues
15060  * with resets
15061  **/
15062 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
15063 {
15064 	const i40e_status pfr = i40e_pf_loop_reset(pf);
15065 	const bool is_empr = i40e_check_fw_empr(pf);
15066 
15067 	if (is_empr || pfr != I40E_SUCCESS)
15068 		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");
15069 
15070 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15071 }
15072 
15073 /**
15074  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15075  * @pf: board private structure
15076  * @hw: ptr to the hardware info
15077  *
15078  * This function does a minimal setup of all subsystems needed for running
15079  * recovery mode.
15080  *
15081  * Returns 0 on success, negative on failure
15082  **/
15083 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15084 {
15085 	struct i40e_vsi *vsi;
15086 	int err;
15087 	int v_idx;
15088 
15089 	pci_save_state(pf->pdev);
15090 
15091 	/* set up periodic task facility */
15092 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15093 	pf->service_timer_period = HZ;
15094 
15095 	INIT_WORK(&pf->service_task, i40e_service_task);
15096 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15097 
15098 	err = i40e_init_interrupt_scheme(pf);
15099 	if (err)
15100 		goto err_switch_setup;
15101 
15102 	/* The number of VSIs reported by the FW is the minimum guaranteed
15103 	 * to us; HW supports far more and we share the remaining pool with
15104 	 * the other PFs. We allocate space for more than the guarantee with
15105 	 * the understanding that we might not get them all later.
15106 	 */
15107 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15108 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15109 	else
15110 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15111 
15112 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15113 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15114 			  GFP_KERNEL);
15115 	if (!pf->vsi) {
15116 		err = -ENOMEM;
15117 		goto err_switch_setup;
15118 	}
15119 
15120 	/* We allocate one VSI which is needed as absolute minimum
15121 	 * in order to register the netdev
15122 	 */
15123 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15124 	if (v_idx < 0) {
15125 		err = v_idx;
15126 		goto err_switch_setup;
15127 	}
15128 	pf->lan_vsi = v_idx;
15129 	vsi = pf->vsi[v_idx];
15130 	if (!vsi) {
15131 		err = -EFAULT;
15132 		goto err_switch_setup;
15133 	}
15134 	vsi->alloc_queue_pairs = 1;
15135 	err = i40e_config_netdev(vsi);
15136 	if (err)
15137 		goto err_switch_setup;
15138 	err = register_netdev(vsi->netdev);
15139 	if (err)
15140 		goto err_switch_setup;
15141 	vsi->netdev_registered = true;
15142 	i40e_dbg_pf_init(pf);
15143 
15144 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15145 	if (err)
15146 		goto err_switch_setup;
15147 
15148 	/* tell the firmware that we're starting */
15149 	i40e_send_version(pf);
15150 
15151 	/* since everything's happy, start the service_task timer */
15152 	mod_timer(&pf->service_timer,
15153 		  round_jiffies(jiffies + pf->service_timer_period));
15154 
15155 	return 0;
15156 
15157 err_switch_setup:
15158 	i40e_reset_interrupt_capability(pf);
15159 	del_timer_sync(&pf->service_timer);
15160 	i40e_shutdown_adminq(hw);
15161 	iounmap(hw->hw_addr);
15162 	pci_disable_pcie_error_reporting(pf->pdev);
15163 	pci_release_mem_regions(pf->pdev);
15164 	pci_disable_device(pf->pdev);
15165 	kfree(pf);
15166 
15167 	return err;
15168 }
15169 
15170 /**
15171  * i40e_probe - Device initialization routine
15172  * @pdev: PCI device information struct
15173  * @ent: entry in i40e_pci_tbl
15174  *
15175  * i40e_probe initializes a PF identified by a pci_dev structure.
15176  * The OS initialization, configuring of the PF private structure,
15177  * and a hardware reset occur.
15178  *
15179  * Returns 0 on success, negative on failure
15180  **/
15181 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15182 {
15183 	struct i40e_aq_get_phy_abilities_resp abilities;
15184 #ifdef CONFIG_I40E_DCB
15185 	enum i40e_get_fw_lldp_status_resp lldp_status;
15186 	i40e_status status;
15187 #endif /* CONFIG_I40E_DCB */
15188 	struct i40e_pf *pf;
15189 	struct i40e_hw *hw;
15190 	static u16 pfs_found;
15191 	u16 wol_nvm_bits;
15192 	u16 link_status;
15193 	int err;
15194 	u32 val;
15195 	u32 i;
15196 
15197 	err = pci_enable_device_mem(pdev);
15198 	if (err)
15199 		return err;
15200 
15201 	/* set up for high or low dma */
15202 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15203 	if (err) {
15204 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
15205 		if (err) {
15206 			dev_err(&pdev->dev,
15207 				"DMA configuration failed: 0x%x\n", err);
15208 			goto err_dma;
15209 		}
15210 	}
15211 
15212 	/* set up pci connections */
15213 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15214 	if (err) {
15215 		dev_info(&pdev->dev,
15216 			 "pci_request_selected_regions failed %d\n", err);
15217 		goto err_pci_reg;
15218 	}
15219 
15220 	pci_enable_pcie_error_reporting(pdev);
15221 	pci_set_master(pdev);
15222 
15223 	/* Now that we have a PCI connection, we need to do the
15224 	 * low level device setup.  This is primarily setting up
15225 	 * the Admin Queue structures and then querying for the
15226 	 * device's current profile information.
15227 	 */
15228 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15229 	if (!pf) {
15230 		err = -ENOMEM;
15231 		goto err_pf_alloc;
15232 	}
15233 	pf->next_vsi = 0;
15234 	pf->pdev = pdev;
15235 	set_bit(__I40E_DOWN, pf->state);
15236 
15237 	hw = &pf->hw;
15238 	hw->back = pf;
15239 
15240 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15241 				I40E_MAX_CSR_SPACE);
15242 	/* We believe that the highest register to read is
15243 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15244 	 * is not less than that before mapping to prevent a
15245 	 * kernel panic.
15246 	 */
15247 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15248 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15249 			pf->ioremap_len);
15250 		err = -ENOMEM;
15251 		goto err_ioremap;
15252 	}
15253 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15254 	if (!hw->hw_addr) {
15255 		err = -EIO;
15256 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15257 			 (unsigned int)pci_resource_start(pdev, 0),
15258 			 pf->ioremap_len, err);
15259 		goto err_ioremap;
15260 	}
15261 	hw->vendor_id = pdev->vendor;
15262 	hw->device_id = pdev->device;
15263 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15264 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15265 	hw->subsystem_device_id = pdev->subsystem_device;
15266 	hw->bus.device = PCI_SLOT(pdev->devfn);
15267 	hw->bus.func = PCI_FUNC(pdev->devfn);
15268 	hw->bus.bus_id = pdev->bus->number;
15269 	pf->instance = pfs_found;
15270 
15271 	/* Select something other than the 802.1ad ethertype for the
15272 	 * switch to use internally and drop on ingress.
15273 	 */
15274 	hw->switch_tag = 0xffff;
15275 	hw->first_tag = ETH_P_8021AD;
15276 	hw->second_tag = ETH_P_8021Q;
15277 
15278 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15279 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15280 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15281 
15282 	/* set up the locks for the AQ, do this only once in probe
15283 	 * and destroy them only once in remove
15284 	 */
15285 	mutex_init(&hw->aq.asq_mutex);
15286 	mutex_init(&hw->aq.arq_mutex);
15287 
15288 	pf->msg_enable = netif_msg_init(debug,
15289 					NETIF_MSG_DRV |
15290 					NETIF_MSG_PROBE |
15291 					NETIF_MSG_LINK);
15292 	if (debug < -1)
15293 		pf->hw.debug_mask = debug;
15294 
15295 	/* do a special CORER for clearing PXE mode once at init */
15296 	if (hw->revision_id == 0 &&
15297 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15298 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15299 		i40e_flush(hw);
15300 		msleep(200);
15301 		pf->corer_count++;
15302 
15303 		i40e_clear_pxe_mode(hw);
15304 	}
15305 
15306 	/* Reset here to make sure all is clean and to define PF 'n' */
15307 	i40e_clear_hw(hw);
15308 
15309 	err = i40e_set_mac_type(hw);
15310 	if (err) {
15311 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15312 			 err);
15313 		goto err_pf_reset;
15314 	}
15315 
15316 	err = i40e_handle_resets(pf);
15317 	if (err)
15318 		goto err_pf_reset;
15319 
15320 	i40e_check_recovery_mode(pf);
15321 
15322 	if (is_kdump_kernel()) {
15323 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15324 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15325 	} else {
15326 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15327 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15328 	}
15329 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15330 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15331 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15332 
15333 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15334 		 "%s-%s:misc",
15335 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15336 
15337 	err = i40e_init_shared_code(hw);
15338 	if (err) {
15339 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15340 			 err);
15341 		goto err_pf_reset;
15342 	}
15343 
15344 	/* set up a default setting for link flow control */
15345 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15346 
15347 	err = i40e_init_adminq(hw);
15348 	if (err) {
15349 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15350 			dev_info(&pdev->dev,
15351 				 "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",
15352 				 hw->aq.api_maj_ver,
15353 				 hw->aq.api_min_ver,
15354 				 I40E_FW_API_VERSION_MAJOR,
15355 				 I40E_FW_MINOR_VERSION(hw));
15356 		else
15357 			dev_info(&pdev->dev,
15358 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15359 
15360 		goto err_pf_reset;
15361 	}
15362 	i40e_get_oem_version(hw);
15363 
15364 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15365 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15366 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15367 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15368 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15369 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15370 
15371 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15372 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15373 		dev_info(&pdev->dev,
15374 			 "The driver for the device detected a newer version of the NVM image v%u.%u than expected v%u.%u. Please install the most recent version of the network driver.\n",
15375 			 hw->aq.api_maj_ver,
15376 			 hw->aq.api_min_ver,
15377 			 I40E_FW_API_VERSION_MAJOR,
15378 			 I40E_FW_MINOR_VERSION(hw));
15379 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15380 		dev_info(&pdev->dev,
15381 			 "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",
15382 			 hw->aq.api_maj_ver,
15383 			 hw->aq.api_min_ver,
15384 			 I40E_FW_API_VERSION_MAJOR,
15385 			 I40E_FW_MINOR_VERSION(hw));
15386 
15387 	i40e_verify_eeprom(pf);
15388 
15389 	/* Rev 0 hardware was never productized */
15390 	if (hw->revision_id < 1)
15391 		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");
15392 
15393 	i40e_clear_pxe_mode(hw);
15394 
15395 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15396 	if (err)
15397 		goto err_adminq_setup;
15398 
15399 	err = i40e_sw_init(pf);
15400 	if (err) {
15401 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15402 		goto err_sw_init;
15403 	}
15404 
15405 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15406 		return i40e_init_recovery_mode(pf, hw);
15407 
15408 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15409 				hw->func_caps.num_rx_qp, 0, 0);
15410 	if (err) {
15411 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15412 		goto err_init_lan_hmc;
15413 	}
15414 
15415 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15416 	if (err) {
15417 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15418 		err = -ENOENT;
15419 		goto err_configure_lan_hmc;
15420 	}
15421 
15422 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15423 	 * Ignore error return codes because if it was already disabled via
15424 	 * hardware settings this will fail
15425 	 */
15426 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15427 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15428 		i40e_aq_stop_lldp(hw, true, false, NULL);
15429 	}
15430 
15431 	/* allow a platform config to override the HW addr */
15432 	i40e_get_platform_mac_addr(pdev, pf);
15433 
15434 	if (!is_valid_ether_addr(hw->mac.addr)) {
15435 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15436 		err = -EIO;
15437 		goto err_mac_addr;
15438 	}
15439 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15440 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15441 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15442 	if (is_valid_ether_addr(hw->mac.port_addr))
15443 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15444 
15445 	pci_set_drvdata(pdev, pf);
15446 	pci_save_state(pdev);
15447 
15448 #ifdef CONFIG_I40E_DCB
15449 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15450 	(!status &&
15451 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15452 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15453 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15454 	dev_info(&pdev->dev,
15455 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15456 			"FW LLDP is disabled\n" :
15457 			"FW LLDP is enabled\n");
15458 
15459 	/* Enable FW to write default DCB config on link-up */
15460 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15461 
15462 	err = i40e_init_pf_dcb(pf);
15463 	if (err) {
15464 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15465 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15466 		/* Continue without DCB enabled */
15467 	}
15468 #endif /* CONFIG_I40E_DCB */
15469 
15470 	/* set up periodic task facility */
15471 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15472 	pf->service_timer_period = HZ;
15473 
15474 	INIT_WORK(&pf->service_task, i40e_service_task);
15475 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15476 
15477 	/* NVM bit on means WoL disabled for the port */
15478 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15479 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15480 		pf->wol_en = false;
15481 	else
15482 		pf->wol_en = true;
15483 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15484 
15485 	/* set up the main switch operations */
15486 	i40e_determine_queue_usage(pf);
15487 	err = i40e_init_interrupt_scheme(pf);
15488 	if (err)
15489 		goto err_switch_setup;
15490 
15491 	/* Reduce Tx and Rx pairs for kdump
15492 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15493 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15494 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15495 	 */
15496 	if (is_kdump_kernel())
15497 		pf->num_lan_msix = 1;
15498 
15499 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15500 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15501 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15502 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15503 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15504 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15505 						    UDP_TUNNEL_TYPE_GENEVE;
15506 
15507 	/* The number of VSIs reported by the FW is the minimum guaranteed
15508 	 * to us; HW supports far more and we share the remaining pool with
15509 	 * the other PFs. We allocate space for more than the guarantee with
15510 	 * the understanding that we might not get them all later.
15511 	 */
15512 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15513 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15514 	else
15515 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15516 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15517 		dev_warn(&pf->pdev->dev,
15518 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15519 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15520 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15521 	}
15522 
15523 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15524 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15525 			  GFP_KERNEL);
15526 	if (!pf->vsi) {
15527 		err = -ENOMEM;
15528 		goto err_switch_setup;
15529 	}
15530 
15531 #ifdef CONFIG_PCI_IOV
15532 	/* prep for VF support */
15533 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15534 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15535 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15536 		if (pci_num_vf(pdev))
15537 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15538 	}
15539 #endif
15540 	err = i40e_setup_pf_switch(pf, false, false);
15541 	if (err) {
15542 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15543 		goto err_vsis;
15544 	}
15545 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15546 
15547 	/* if FDIR VSI was set up, start it now */
15548 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15549 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15550 			i40e_vsi_open(pf->vsi[i]);
15551 			break;
15552 		}
15553 	}
15554 
15555 	/* The driver only wants link up/down and module qualification
15556 	 * reports from firmware.  Note the negative logic.
15557 	 */
15558 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15559 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15560 					 I40E_AQ_EVENT_MEDIA_NA |
15561 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15562 	if (err)
15563 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15564 			 i40e_stat_str(&pf->hw, err),
15565 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15566 
15567 	/* Reconfigure hardware for allowing smaller MSS in the case
15568 	 * of TSO, so that we avoid the MDD being fired and causing
15569 	 * a reset in the case of small MSS+TSO.
15570 	 */
15571 	val = rd32(hw, I40E_REG_MSS);
15572 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15573 		val &= ~I40E_REG_MSS_MIN_MASK;
15574 		val |= I40E_64BYTE_MSS;
15575 		wr32(hw, I40E_REG_MSS, val);
15576 	}
15577 
15578 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15579 		msleep(75);
15580 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15581 		if (err)
15582 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15583 				 i40e_stat_str(&pf->hw, err),
15584 				 i40e_aq_str(&pf->hw,
15585 					     pf->hw.aq.asq_last_status));
15586 	}
15587 	/* The main driver is (mostly) up and happy. We need to set this state
15588 	 * before setting up the misc vector or we get a race and the vector
15589 	 * ends up disabled forever.
15590 	 */
15591 	clear_bit(__I40E_DOWN, pf->state);
15592 
15593 	/* In case of MSIX we are going to setup the misc vector right here
15594 	 * to handle admin queue events etc. In case of legacy and MSI
15595 	 * the misc functionality and queue processing is combined in
15596 	 * the same vector and that gets setup at open.
15597 	 */
15598 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15599 		err = i40e_setup_misc_vector(pf);
15600 		if (err) {
15601 			dev_info(&pdev->dev,
15602 				 "setup of misc vector failed: %d\n", err);
15603 			i40e_cloud_filter_exit(pf);
15604 			i40e_fdir_teardown(pf);
15605 			goto err_vsis;
15606 		}
15607 	}
15608 
15609 #ifdef CONFIG_PCI_IOV
15610 	/* prep for VF support */
15611 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15612 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15613 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15614 		/* disable link interrupts for VFs */
15615 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15616 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15617 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15618 		i40e_flush(hw);
15619 
15620 		if (pci_num_vf(pdev)) {
15621 			dev_info(&pdev->dev,
15622 				 "Active VFs found, allocating resources.\n");
15623 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15624 			if (err)
15625 				dev_info(&pdev->dev,
15626 					 "Error %d allocating resources for existing VFs\n",
15627 					 err);
15628 		}
15629 	}
15630 #endif /* CONFIG_PCI_IOV */
15631 
15632 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15633 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
15634 						      pf->num_iwarp_msix,
15635 						      I40E_IWARP_IRQ_PILE_ID);
15636 		if (pf->iwarp_base_vector < 0) {
15637 			dev_info(&pdev->dev,
15638 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
15639 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
15640 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
15641 		}
15642 	}
15643 
15644 	i40e_dbg_pf_init(pf);
15645 
15646 	/* tell the firmware that we're starting */
15647 	i40e_send_version(pf);
15648 
15649 	/* since everything's happy, start the service_task timer */
15650 	mod_timer(&pf->service_timer,
15651 		  round_jiffies(jiffies + pf->service_timer_period));
15652 
15653 	/* add this PF to client device list and launch a client service task */
15654 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15655 		err = i40e_lan_add_device(pf);
15656 		if (err)
15657 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
15658 				 err);
15659 	}
15660 
15661 #define PCI_SPEED_SIZE 8
15662 #define PCI_WIDTH_SIZE 8
15663 	/* Devices on the IOSF bus do not have this information
15664 	 * and will report PCI Gen 1 x 1 by default so don't bother
15665 	 * checking them.
15666 	 */
15667 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
15668 		char speed[PCI_SPEED_SIZE] = "Unknown";
15669 		char width[PCI_WIDTH_SIZE] = "Unknown";
15670 
15671 		/* Get the negotiated link width and speed from PCI config
15672 		 * space
15673 		 */
15674 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
15675 					  &link_status);
15676 
15677 		i40e_set_pci_config_data(hw, link_status);
15678 
15679 		switch (hw->bus.speed) {
15680 		case i40e_bus_speed_8000:
15681 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
15682 		case i40e_bus_speed_5000:
15683 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
15684 		case i40e_bus_speed_2500:
15685 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
15686 		default:
15687 			break;
15688 		}
15689 		switch (hw->bus.width) {
15690 		case i40e_bus_width_pcie_x8:
15691 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
15692 		case i40e_bus_width_pcie_x4:
15693 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
15694 		case i40e_bus_width_pcie_x2:
15695 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
15696 		case i40e_bus_width_pcie_x1:
15697 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
15698 		default:
15699 			break;
15700 		}
15701 
15702 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
15703 			 speed, width);
15704 
15705 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
15706 		    hw->bus.speed < i40e_bus_speed_8000) {
15707 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
15708 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
15709 		}
15710 	}
15711 
15712 	/* get the requested speeds from the fw */
15713 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
15714 	if (err)
15715 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
15716 			i40e_stat_str(&pf->hw, err),
15717 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15718 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
15719 
15720 	/* set the FEC config due to the board capabilities */
15721 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
15722 
15723 	/* get the supported phy types from the fw */
15724 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
15725 	if (err)
15726 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
15727 			i40e_stat_str(&pf->hw, err),
15728 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15729 
15730 	/* make sure the MFS hasn't been set lower than the default */
15731 #define MAX_FRAME_SIZE_DEFAULT 0x2600
15732 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
15733 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
15734 	if (val < MAX_FRAME_SIZE_DEFAULT)
15735 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
15736 			 i, val);
15737 
15738 	/* Add a filter to drop all Flow control frames from any VSI from being
15739 	 * transmitted. By doing so we stop a malicious VF from sending out
15740 	 * PAUSE or PFC frames and potentially controlling traffic for other
15741 	 * PF/VF VSIs.
15742 	 * The FW can still send Flow control frames if enabled.
15743 	 */
15744 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
15745 						       pf->main_vsi_seid);
15746 
15747 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
15748 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
15749 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
15750 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
15751 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
15752 	/* print a string summarizing features */
15753 	i40e_print_features(pf);
15754 
15755 	return 0;
15756 
15757 	/* Unwind what we've done if something failed in the setup */
15758 err_vsis:
15759 	set_bit(__I40E_DOWN, pf->state);
15760 	i40e_clear_interrupt_scheme(pf);
15761 	kfree(pf->vsi);
15762 err_switch_setup:
15763 	i40e_reset_interrupt_capability(pf);
15764 	del_timer_sync(&pf->service_timer);
15765 err_mac_addr:
15766 err_configure_lan_hmc:
15767 	(void)i40e_shutdown_lan_hmc(hw);
15768 err_init_lan_hmc:
15769 	kfree(pf->qp_pile);
15770 err_sw_init:
15771 err_adminq_setup:
15772 err_pf_reset:
15773 	iounmap(hw->hw_addr);
15774 err_ioremap:
15775 	kfree(pf);
15776 err_pf_alloc:
15777 	pci_disable_pcie_error_reporting(pdev);
15778 	pci_release_mem_regions(pdev);
15779 err_pci_reg:
15780 err_dma:
15781 	pci_disable_device(pdev);
15782 	return err;
15783 }
15784 
15785 /**
15786  * i40e_remove - Device removal routine
15787  * @pdev: PCI device information struct
15788  *
15789  * i40e_remove is called by the PCI subsystem to alert the driver
15790  * that is should release a PCI device.  This could be caused by a
15791  * Hot-Plug event, or because the driver is going to be removed from
15792  * memory.
15793  **/
15794 static void i40e_remove(struct pci_dev *pdev)
15795 {
15796 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15797 	struct i40e_hw *hw = &pf->hw;
15798 	i40e_status ret_code;
15799 	int i;
15800 
15801 	i40e_dbg_pf_exit(pf);
15802 
15803 	i40e_ptp_stop(pf);
15804 
15805 	/* Disable RSS in hw */
15806 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
15807 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
15808 
15809 	while (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
15810 		usleep_range(1000, 2000);
15811 
15812 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
15813 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
15814 		i40e_free_vfs(pf);
15815 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
15816 	}
15817 	/* no more scheduling of any task */
15818 	set_bit(__I40E_SUSPENDED, pf->state);
15819 	set_bit(__I40E_DOWN, pf->state);
15820 	if (pf->service_timer.function)
15821 		del_timer_sync(&pf->service_timer);
15822 	if (pf->service_task.func)
15823 		cancel_work_sync(&pf->service_task);
15824 
15825 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
15826 		struct i40e_vsi *vsi = pf->vsi[0];
15827 
15828 		/* We know that we have allocated only one vsi for this PF,
15829 		 * it was just for registering netdevice, so the interface
15830 		 * could be visible in the 'ifconfig' output
15831 		 */
15832 		unregister_netdev(vsi->netdev);
15833 		free_netdev(vsi->netdev);
15834 
15835 		goto unmap;
15836 	}
15837 
15838 	/* Client close must be called explicitly here because the timer
15839 	 * has been stopped.
15840 	 */
15841 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15842 
15843 	i40e_fdir_teardown(pf);
15844 
15845 	/* If there is a switch structure or any orphans, remove them.
15846 	 * This will leave only the PF's VSI remaining.
15847 	 */
15848 	for (i = 0; i < I40E_MAX_VEB; i++) {
15849 		if (!pf->veb[i])
15850 			continue;
15851 
15852 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
15853 		    pf->veb[i]->uplink_seid == 0)
15854 			i40e_switch_branch_release(pf->veb[i]);
15855 	}
15856 
15857 	/* Now we can shutdown the PF's VSI, just before we kill
15858 	 * adminq and hmc.
15859 	 */
15860 	if (pf->vsi[pf->lan_vsi])
15861 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
15862 
15863 	i40e_cloud_filter_exit(pf);
15864 
15865 	/* remove attached clients */
15866 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15867 		ret_code = i40e_lan_del_device(pf);
15868 		if (ret_code)
15869 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
15870 				 ret_code);
15871 	}
15872 
15873 	/* shutdown and destroy the HMC */
15874 	if (hw->hmc.hmc_obj) {
15875 		ret_code = i40e_shutdown_lan_hmc(hw);
15876 		if (ret_code)
15877 			dev_warn(&pdev->dev,
15878 				 "Failed to destroy the HMC resources: %d\n",
15879 				 ret_code);
15880 	}
15881 
15882 unmap:
15883 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
15884 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
15885 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
15886 		free_irq(pf->pdev->irq, pf);
15887 
15888 	/* shutdown the adminq */
15889 	i40e_shutdown_adminq(hw);
15890 
15891 	/* destroy the locks only once, here */
15892 	mutex_destroy(&hw->aq.arq_mutex);
15893 	mutex_destroy(&hw->aq.asq_mutex);
15894 
15895 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
15896 	rtnl_lock();
15897 	i40e_clear_interrupt_scheme(pf);
15898 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15899 		if (pf->vsi[i]) {
15900 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
15901 				i40e_vsi_clear_rings(pf->vsi[i]);
15902 			i40e_vsi_clear(pf->vsi[i]);
15903 			pf->vsi[i] = NULL;
15904 		}
15905 	}
15906 	rtnl_unlock();
15907 
15908 	for (i = 0; i < I40E_MAX_VEB; i++) {
15909 		kfree(pf->veb[i]);
15910 		pf->veb[i] = NULL;
15911 	}
15912 
15913 	kfree(pf->qp_pile);
15914 	kfree(pf->vsi);
15915 
15916 	iounmap(hw->hw_addr);
15917 	kfree(pf);
15918 	pci_release_mem_regions(pdev);
15919 
15920 	pci_disable_pcie_error_reporting(pdev);
15921 	pci_disable_device(pdev);
15922 }
15923 
15924 /**
15925  * i40e_pci_error_detected - warning that something funky happened in PCI land
15926  * @pdev: PCI device information struct
15927  * @error: the type of PCI error
15928  *
15929  * Called to warn that something happened and the error handling steps
15930  * are in progress.  Allows the driver to quiesce things, be ready for
15931  * remediation.
15932  **/
15933 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
15934 						pci_channel_state_t error)
15935 {
15936 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15937 
15938 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
15939 
15940 	if (!pf) {
15941 		dev_info(&pdev->dev,
15942 			 "Cannot recover - error happened during device probe\n");
15943 		return PCI_ERS_RESULT_DISCONNECT;
15944 	}
15945 
15946 	/* shutdown all operations */
15947 	if (!test_bit(__I40E_SUSPENDED, pf->state))
15948 		i40e_prep_for_reset(pf);
15949 
15950 	/* Request a slot reset */
15951 	return PCI_ERS_RESULT_NEED_RESET;
15952 }
15953 
15954 /**
15955  * i40e_pci_error_slot_reset - a PCI slot reset just happened
15956  * @pdev: PCI device information struct
15957  *
15958  * Called to find if the driver can work with the device now that
15959  * the pci slot has been reset.  If a basic connection seems good
15960  * (registers are readable and have sane content) then return a
15961  * happy little PCI_ERS_RESULT_xxx.
15962  **/
15963 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
15964 {
15965 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15966 	pci_ers_result_t result;
15967 	u32 reg;
15968 
15969 	dev_dbg(&pdev->dev, "%s\n", __func__);
15970 	if (pci_enable_device_mem(pdev)) {
15971 		dev_info(&pdev->dev,
15972 			 "Cannot re-enable PCI device after reset.\n");
15973 		result = PCI_ERS_RESULT_DISCONNECT;
15974 	} else {
15975 		pci_set_master(pdev);
15976 		pci_restore_state(pdev);
15977 		pci_save_state(pdev);
15978 		pci_wake_from_d3(pdev, false);
15979 
15980 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
15981 		if (reg == 0)
15982 			result = PCI_ERS_RESULT_RECOVERED;
15983 		else
15984 			result = PCI_ERS_RESULT_DISCONNECT;
15985 	}
15986 
15987 	return result;
15988 }
15989 
15990 /**
15991  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
15992  * @pdev: PCI device information struct
15993  */
15994 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
15995 {
15996 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15997 
15998 	i40e_prep_for_reset(pf);
15999 }
16000 
16001 /**
16002  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16003  * @pdev: PCI device information struct
16004  */
16005 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16006 {
16007 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16008 
16009 	i40e_reset_and_rebuild(pf, false, false);
16010 }
16011 
16012 /**
16013  * i40e_pci_error_resume - restart operations after PCI error recovery
16014  * @pdev: PCI device information struct
16015  *
16016  * Called to allow the driver to bring things back up after PCI error
16017  * and/or reset recovery has finished.
16018  **/
16019 static void i40e_pci_error_resume(struct pci_dev *pdev)
16020 {
16021 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16022 
16023 	dev_dbg(&pdev->dev, "%s\n", __func__);
16024 	if (test_bit(__I40E_SUSPENDED, pf->state))
16025 		return;
16026 
16027 	i40e_handle_reset_warning(pf, false);
16028 }
16029 
16030 /**
16031  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16032  * using the mac_address_write admin q function
16033  * @pf: pointer to i40e_pf struct
16034  **/
16035 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16036 {
16037 	struct i40e_hw *hw = &pf->hw;
16038 	i40e_status ret;
16039 	u8 mac_addr[6];
16040 	u16 flags = 0;
16041 
16042 	/* Get current MAC address in case it's an LAA */
16043 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16044 		ether_addr_copy(mac_addr,
16045 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16046 	} else {
16047 		dev_err(&pf->pdev->dev,
16048 			"Failed to retrieve MAC address; using default\n");
16049 		ether_addr_copy(mac_addr, hw->mac.addr);
16050 	}
16051 
16052 	/* The FW expects the mac address write cmd to first be called with
16053 	 * one of these flags before calling it again with the multicast
16054 	 * enable flags.
16055 	 */
16056 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16057 
16058 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16059 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16060 
16061 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16062 	if (ret) {
16063 		dev_err(&pf->pdev->dev,
16064 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16065 		return;
16066 	}
16067 
16068 	flags = I40E_AQC_MC_MAG_EN
16069 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16070 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16071 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16072 	if (ret)
16073 		dev_err(&pf->pdev->dev,
16074 			"Failed to enable Multicast Magic Packet wake up\n");
16075 }
16076 
16077 /**
16078  * i40e_shutdown - PCI callback for shutting down
16079  * @pdev: PCI device information struct
16080  **/
16081 static void i40e_shutdown(struct pci_dev *pdev)
16082 {
16083 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16084 	struct i40e_hw *hw = &pf->hw;
16085 
16086 	set_bit(__I40E_SUSPENDED, pf->state);
16087 	set_bit(__I40E_DOWN, pf->state);
16088 
16089 	del_timer_sync(&pf->service_timer);
16090 	cancel_work_sync(&pf->service_task);
16091 	i40e_cloud_filter_exit(pf);
16092 	i40e_fdir_teardown(pf);
16093 
16094 	/* Client close must be called explicitly here because the timer
16095 	 * has been stopped.
16096 	 */
16097 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16098 
16099 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16100 		i40e_enable_mc_magic_wake(pf);
16101 
16102 	i40e_prep_for_reset(pf);
16103 
16104 	wr32(hw, I40E_PFPM_APM,
16105 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16106 	wr32(hw, I40E_PFPM_WUFC,
16107 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16108 
16109 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16110 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16111 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16112 		free_irq(pf->pdev->irq, pf);
16113 
16114 	/* Since we're going to destroy queues during the
16115 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16116 	 * whole section
16117 	 */
16118 	rtnl_lock();
16119 	i40e_clear_interrupt_scheme(pf);
16120 	rtnl_unlock();
16121 
16122 	if (system_state == SYSTEM_POWER_OFF) {
16123 		pci_wake_from_d3(pdev, pf->wol_en);
16124 		pci_set_power_state(pdev, PCI_D3hot);
16125 	}
16126 }
16127 
16128 /**
16129  * i40e_suspend - PM callback for moving to D3
16130  * @dev: generic device information structure
16131  **/
16132 static int __maybe_unused i40e_suspend(struct device *dev)
16133 {
16134 	struct i40e_pf *pf = dev_get_drvdata(dev);
16135 	struct i40e_hw *hw = &pf->hw;
16136 
16137 	/* If we're already suspended, then there is nothing to do */
16138 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16139 		return 0;
16140 
16141 	set_bit(__I40E_DOWN, pf->state);
16142 
16143 	/* Ensure service task will not be running */
16144 	del_timer_sync(&pf->service_timer);
16145 	cancel_work_sync(&pf->service_task);
16146 
16147 	/* Client close must be called explicitly here because the timer
16148 	 * has been stopped.
16149 	 */
16150 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16151 
16152 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16153 		i40e_enable_mc_magic_wake(pf);
16154 
16155 	/* Since we're going to destroy queues during the
16156 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16157 	 * whole section
16158 	 */
16159 	rtnl_lock();
16160 
16161 	i40e_prep_for_reset(pf);
16162 
16163 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16164 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16165 
16166 	/* Clear the interrupt scheme and release our IRQs so that the system
16167 	 * can safely hibernate even when there are a large number of CPUs.
16168 	 * Otherwise hibernation might fail when mapping all the vectors back
16169 	 * to CPU0.
16170 	 */
16171 	i40e_clear_interrupt_scheme(pf);
16172 
16173 	rtnl_unlock();
16174 
16175 	return 0;
16176 }
16177 
16178 /**
16179  * i40e_resume - PM callback for waking up from D3
16180  * @dev: generic device information structure
16181  **/
16182 static int __maybe_unused i40e_resume(struct device *dev)
16183 {
16184 	struct i40e_pf *pf = dev_get_drvdata(dev);
16185 	int err;
16186 
16187 	/* If we're not suspended, then there is nothing to do */
16188 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16189 		return 0;
16190 
16191 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16192 	 * since we're going to be restoring queues
16193 	 */
16194 	rtnl_lock();
16195 
16196 	/* We cleared the interrupt scheme when we suspended, so we need to
16197 	 * restore it now to resume device functionality.
16198 	 */
16199 	err = i40e_restore_interrupt_scheme(pf);
16200 	if (err) {
16201 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16202 			err);
16203 	}
16204 
16205 	clear_bit(__I40E_DOWN, pf->state);
16206 	i40e_reset_and_rebuild(pf, false, true);
16207 
16208 	rtnl_unlock();
16209 
16210 	/* Clear suspended state last after everything is recovered */
16211 	clear_bit(__I40E_SUSPENDED, pf->state);
16212 
16213 	/* Restart the service task */
16214 	mod_timer(&pf->service_timer,
16215 		  round_jiffies(jiffies + pf->service_timer_period));
16216 
16217 	return 0;
16218 }
16219 
16220 static const struct pci_error_handlers i40e_err_handler = {
16221 	.error_detected = i40e_pci_error_detected,
16222 	.slot_reset = i40e_pci_error_slot_reset,
16223 	.reset_prepare = i40e_pci_error_reset_prepare,
16224 	.reset_done = i40e_pci_error_reset_done,
16225 	.resume = i40e_pci_error_resume,
16226 };
16227 
16228 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16229 
16230 static struct pci_driver i40e_driver = {
16231 	.name     = i40e_driver_name,
16232 	.id_table = i40e_pci_tbl,
16233 	.probe    = i40e_probe,
16234 	.remove   = i40e_remove,
16235 	.driver   = {
16236 		.pm = &i40e_pm_ops,
16237 	},
16238 	.shutdown = i40e_shutdown,
16239 	.err_handler = &i40e_err_handler,
16240 	.sriov_configure = i40e_pci_sriov_configure,
16241 };
16242 
16243 /**
16244  * i40e_init_module - Driver registration routine
16245  *
16246  * i40e_init_module is the first routine called when the driver is
16247  * loaded. All it does is register with the PCI subsystem.
16248  **/
16249 static int __init i40e_init_module(void)
16250 {
16251 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16252 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16253 
16254 	/* There is no need to throttle the number of active tasks because
16255 	 * each device limits its own task using a state bit for scheduling
16256 	 * the service task, and the device tasks do not interfere with each
16257 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16258 	 * since we need to be able to guarantee forward progress even under
16259 	 * memory pressure.
16260 	 */
16261 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16262 	if (!i40e_wq) {
16263 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16264 		return -ENOMEM;
16265 	}
16266 
16267 	i40e_dbg_init();
16268 	return pci_register_driver(&i40e_driver);
16269 }
16270 module_init(i40e_init_module);
16271 
16272 /**
16273  * i40e_exit_module - Driver exit cleanup routine
16274  *
16275  * i40e_exit_module is called just before the driver is removed
16276  * from memory.
16277  **/
16278 static void __exit i40e_exit_module(void)
16279 {
16280 	pci_unregister_driver(&i40e_driver);
16281 	destroy_workqueue(i40e_wq);
16282 	ida_destroy(&i40e_client_ida);
16283 	i40e_dbg_exit();
16284 }
16285 module_exit(i40e_exit_module);
16286