xref: /linux/drivers/net/ethernet/intel/i40e/i40e_main.c (revision 9c39c6ffe0c2945c7cf814814c096bc23b63f53d)
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);
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_set_lldp_forwarding - set forwarding of lldp frames
6884  * @pf: PF being configured
6885  * @enable: if forwarding to OS shall be enabled
6886  *
6887  * Toggle forwarding of lldp frames behavior,
6888  * When passing DCB control from firmware to software
6889  * lldp frames must be forwarded to the software based
6890  * lldp agent.
6891  */
6892 void i40e_set_lldp_forwarding(struct i40e_pf *pf, bool enable)
6893 {
6894 	if (pf->lan_vsi == I40E_NO_VSI)
6895 		return;
6896 
6897 	if (!pf->vsi[pf->lan_vsi])
6898 		return;
6899 
6900 	/* No need to check the outcome, commands may fail
6901 	 * if desired value is already set
6902 	 */
6903 	i40e_aq_add_rem_control_packet_filter(&pf->hw, NULL, ETH_P_LLDP,
6904 					      I40E_AQC_ADD_CONTROL_PACKET_FLAGS_TX |
6905 					      I40E_AQC_ADD_CONTROL_PACKET_FLAGS_IGNORE_MAC,
6906 					      pf->vsi[pf->lan_vsi]->seid, 0,
6907 					      enable, NULL, NULL);
6908 
6909 	i40e_aq_add_rem_control_packet_filter(&pf->hw, NULL, ETH_P_LLDP,
6910 					      I40E_AQC_ADD_CONTROL_PACKET_FLAGS_RX |
6911 					      I40E_AQC_ADD_CONTROL_PACKET_FLAGS_IGNORE_MAC,
6912 					      pf->vsi[pf->lan_vsi]->seid, 0,
6913 					      enable, NULL, NULL);
6914 }
6915 
6916 /**
6917  * i40e_print_link_message - print link up or down
6918  * @vsi: the VSI for which link needs a message
6919  * @isup: true of link is up, false otherwise
6920  */
6921 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
6922 {
6923 	enum i40e_aq_link_speed new_speed;
6924 	struct i40e_pf *pf = vsi->back;
6925 	char *speed = "Unknown";
6926 	char *fc = "Unknown";
6927 	char *fec = "";
6928 	char *req_fec = "";
6929 	char *an = "";
6930 
6931 	if (isup)
6932 		new_speed = pf->hw.phy.link_info.link_speed;
6933 	else
6934 		new_speed = I40E_LINK_SPEED_UNKNOWN;
6935 
6936 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
6937 		return;
6938 	vsi->current_isup = isup;
6939 	vsi->current_speed = new_speed;
6940 	if (!isup) {
6941 		netdev_info(vsi->netdev, "NIC Link is Down\n");
6942 		return;
6943 	}
6944 
6945 	/* Warn user if link speed on NPAR enabled partition is not at
6946 	 * least 10GB
6947 	 */
6948 	if (pf->hw.func_caps.npar_enable &&
6949 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
6950 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
6951 		netdev_warn(vsi->netdev,
6952 			    "The partition detected link speed that is less than 10Gbps\n");
6953 
6954 	switch (pf->hw.phy.link_info.link_speed) {
6955 	case I40E_LINK_SPEED_40GB:
6956 		speed = "40 G";
6957 		break;
6958 	case I40E_LINK_SPEED_20GB:
6959 		speed = "20 G";
6960 		break;
6961 	case I40E_LINK_SPEED_25GB:
6962 		speed = "25 G";
6963 		break;
6964 	case I40E_LINK_SPEED_10GB:
6965 		speed = "10 G";
6966 		break;
6967 	case I40E_LINK_SPEED_5GB:
6968 		speed = "5 G";
6969 		break;
6970 	case I40E_LINK_SPEED_2_5GB:
6971 		speed = "2.5 G";
6972 		break;
6973 	case I40E_LINK_SPEED_1GB:
6974 		speed = "1000 M";
6975 		break;
6976 	case I40E_LINK_SPEED_100MB:
6977 		speed = "100 M";
6978 		break;
6979 	default:
6980 		break;
6981 	}
6982 
6983 	switch (pf->hw.fc.current_mode) {
6984 	case I40E_FC_FULL:
6985 		fc = "RX/TX";
6986 		break;
6987 	case I40E_FC_TX_PAUSE:
6988 		fc = "TX";
6989 		break;
6990 	case I40E_FC_RX_PAUSE:
6991 		fc = "RX";
6992 		break;
6993 	default:
6994 		fc = "None";
6995 		break;
6996 	}
6997 
6998 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
6999 		req_fec = "None";
7000 		fec = "None";
7001 		an = "False";
7002 
7003 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7004 			an = "True";
7005 
7006 		if (pf->hw.phy.link_info.fec_info &
7007 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7008 			fec = "CL74 FC-FEC/BASE-R";
7009 		else if (pf->hw.phy.link_info.fec_info &
7010 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7011 			fec = "CL108 RS-FEC";
7012 
7013 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7014 		 * both RS and FC are requested
7015 		 */
7016 		if (vsi->back->hw.phy.link_info.req_fec_info &
7017 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7018 			if (vsi->back->hw.phy.link_info.req_fec_info &
7019 			    I40E_AQ_REQUEST_FEC_RS)
7020 				req_fec = "CL108 RS-FEC";
7021 			else
7022 				req_fec = "CL74 FC-FEC/BASE-R";
7023 		}
7024 		netdev_info(vsi->netdev,
7025 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7026 			    speed, req_fec, fec, an, fc);
7027 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7028 		req_fec = "None";
7029 		fec = "None";
7030 		an = "False";
7031 
7032 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7033 			an = "True";
7034 
7035 		if (pf->hw.phy.link_info.fec_info &
7036 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7037 			fec = "CL74 FC-FEC/BASE-R";
7038 
7039 		if (pf->hw.phy.link_info.req_fec_info &
7040 		    I40E_AQ_REQUEST_FEC_KR)
7041 			req_fec = "CL74 FC-FEC/BASE-R";
7042 
7043 		netdev_info(vsi->netdev,
7044 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7045 			    speed, req_fec, fec, an, fc);
7046 	} else {
7047 		netdev_info(vsi->netdev,
7048 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7049 			    speed, fc);
7050 	}
7051 
7052 }
7053 
7054 /**
7055  * i40e_up_complete - Finish the last steps of bringing up a connection
7056  * @vsi: the VSI being configured
7057  **/
7058 static int i40e_up_complete(struct i40e_vsi *vsi)
7059 {
7060 	struct i40e_pf *pf = vsi->back;
7061 	int err;
7062 
7063 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7064 		i40e_vsi_configure_msix(vsi);
7065 	else
7066 		i40e_configure_msi_and_legacy(vsi);
7067 
7068 	/* start rings */
7069 	err = i40e_vsi_start_rings(vsi);
7070 	if (err)
7071 		return err;
7072 
7073 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7074 	i40e_napi_enable_all(vsi);
7075 	i40e_vsi_enable_irq(vsi);
7076 
7077 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7078 	    (vsi->netdev)) {
7079 		i40e_print_link_message(vsi, true);
7080 		netif_tx_start_all_queues(vsi->netdev);
7081 		netif_carrier_on(vsi->netdev);
7082 	}
7083 
7084 	/* replay FDIR SB filters */
7085 	if (vsi->type == I40E_VSI_FDIR) {
7086 		/* reset fd counters */
7087 		pf->fd_add_err = 0;
7088 		pf->fd_atr_cnt = 0;
7089 		i40e_fdir_filter_restore(vsi);
7090 	}
7091 
7092 	/* On the next run of the service_task, notify any clients of the new
7093 	 * opened netdev
7094 	 */
7095 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7096 	i40e_service_event_schedule(pf);
7097 
7098 	return 0;
7099 }
7100 
7101 /**
7102  * i40e_vsi_reinit_locked - Reset the VSI
7103  * @vsi: the VSI being configured
7104  *
7105  * Rebuild the ring structs after some configuration
7106  * has changed, e.g. MTU size.
7107  **/
7108 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7109 {
7110 	struct i40e_pf *pf = vsi->back;
7111 
7112 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7113 		usleep_range(1000, 2000);
7114 	i40e_down(vsi);
7115 
7116 	i40e_up(vsi);
7117 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7118 }
7119 
7120 /**
7121  * i40e_force_link_state - Force the link status
7122  * @pf: board private structure
7123  * @is_up: whether the link state should be forced up or down
7124  **/
7125 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7126 {
7127 	struct i40e_aq_get_phy_abilities_resp abilities;
7128 	struct i40e_aq_set_phy_config config = {0};
7129 	bool non_zero_phy_type = is_up;
7130 	struct i40e_hw *hw = &pf->hw;
7131 	i40e_status err;
7132 	u64 mask;
7133 	u8 speed;
7134 
7135 	/* Card might've been put in an unstable state by other drivers
7136 	 * and applications, which causes incorrect speed values being
7137 	 * set on startup. In order to clear speed registers, we call
7138 	 * get_phy_capabilities twice, once to get initial state of
7139 	 * available speeds, and once to get current PHY config.
7140 	 */
7141 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7142 					   NULL);
7143 	if (err) {
7144 		dev_err(&pf->pdev->dev,
7145 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7146 			i40e_stat_str(hw, err),
7147 			i40e_aq_str(hw, hw->aq.asq_last_status));
7148 		return err;
7149 	}
7150 	speed = abilities.link_speed;
7151 
7152 	/* Get the current phy config */
7153 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7154 					   NULL);
7155 	if (err) {
7156 		dev_err(&pf->pdev->dev,
7157 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7158 			i40e_stat_str(hw, err),
7159 			i40e_aq_str(hw, hw->aq.asq_last_status));
7160 		return err;
7161 	}
7162 
7163 	/* If link needs to go up, but was not forced to go down,
7164 	 * and its speed values are OK, no need for a flap
7165 	 * if non_zero_phy_type was set, still need to force up
7166 	 */
7167 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7168 		non_zero_phy_type = true;
7169 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7170 		return I40E_SUCCESS;
7171 
7172 	/* To force link we need to set bits for all supported PHY types,
7173 	 * but there are now more than 32, so we need to split the bitmap
7174 	 * across two fields.
7175 	 */
7176 	mask = I40E_PHY_TYPES_BITMASK;
7177 	config.phy_type =
7178 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7179 	config.phy_type_ext =
7180 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7181 	/* Copy the old settings, except of phy_type */
7182 	config.abilities = abilities.abilities;
7183 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7184 		if (is_up)
7185 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7186 		else
7187 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7188 	}
7189 	if (abilities.link_speed != 0)
7190 		config.link_speed = abilities.link_speed;
7191 	else
7192 		config.link_speed = speed;
7193 	config.eee_capability = abilities.eee_capability;
7194 	config.eeer = abilities.eeer_val;
7195 	config.low_power_ctrl = abilities.d3_lpan;
7196 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7197 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7198 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7199 
7200 	if (err) {
7201 		dev_err(&pf->pdev->dev,
7202 			"set phy config ret =  %s last_status =  %s\n",
7203 			i40e_stat_str(&pf->hw, err),
7204 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7205 		return err;
7206 	}
7207 
7208 	/* Update the link info */
7209 	err = i40e_update_link_info(hw);
7210 	if (err) {
7211 		/* Wait a little bit (on 40G cards it sometimes takes a really
7212 		 * long time for link to come back from the atomic reset)
7213 		 * and try once more
7214 		 */
7215 		msleep(1000);
7216 		i40e_update_link_info(hw);
7217 	}
7218 
7219 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7220 
7221 	return I40E_SUCCESS;
7222 }
7223 
7224 /**
7225  * i40e_up - Bring the connection back up after being down
7226  * @vsi: the VSI being configured
7227  **/
7228 int i40e_up(struct i40e_vsi *vsi)
7229 {
7230 	int err;
7231 
7232 	if (vsi->type == I40E_VSI_MAIN &&
7233 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7234 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7235 		i40e_force_link_state(vsi->back, true);
7236 
7237 	err = i40e_vsi_configure(vsi);
7238 	if (!err)
7239 		err = i40e_up_complete(vsi);
7240 
7241 	return err;
7242 }
7243 
7244 /**
7245  * i40e_down - Shutdown the connection processing
7246  * @vsi: the VSI being stopped
7247  **/
7248 void i40e_down(struct i40e_vsi *vsi)
7249 {
7250 	int i;
7251 
7252 	/* It is assumed that the caller of this function
7253 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7254 	 */
7255 	if (vsi->netdev) {
7256 		netif_carrier_off(vsi->netdev);
7257 		netif_tx_disable(vsi->netdev);
7258 	}
7259 	i40e_vsi_disable_irq(vsi);
7260 	i40e_vsi_stop_rings(vsi);
7261 	if (vsi->type == I40E_VSI_MAIN &&
7262 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7263 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7264 		i40e_force_link_state(vsi->back, false);
7265 	i40e_napi_disable_all(vsi);
7266 
7267 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7268 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7269 		if (i40e_enabled_xdp_vsi(vsi)) {
7270 			/* Make sure that in-progress ndo_xdp_xmit and
7271 			 * ndo_xsk_wakeup calls are completed.
7272 			 */
7273 			synchronize_rcu();
7274 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7275 		}
7276 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7277 	}
7278 
7279 }
7280 
7281 /**
7282  * i40e_validate_mqprio_qopt- validate queue mapping info
7283  * @vsi: the VSI being configured
7284  * @mqprio_qopt: queue parametrs
7285  **/
7286 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7287 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7288 {
7289 	u64 sum_max_rate = 0;
7290 	u64 max_rate = 0;
7291 	int i;
7292 
7293 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7294 	    mqprio_qopt->qopt.num_tc < 1 ||
7295 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7296 		return -EINVAL;
7297 	for (i = 0; ; i++) {
7298 		if (!mqprio_qopt->qopt.count[i])
7299 			return -EINVAL;
7300 		if (mqprio_qopt->min_rate[i]) {
7301 			dev_err(&vsi->back->pdev->dev,
7302 				"Invalid min tx rate (greater than 0) specified\n");
7303 			return -EINVAL;
7304 		}
7305 		max_rate = mqprio_qopt->max_rate[i];
7306 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7307 		sum_max_rate += max_rate;
7308 
7309 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7310 			break;
7311 		if (mqprio_qopt->qopt.offset[i + 1] !=
7312 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7313 			return -EINVAL;
7314 	}
7315 	if (vsi->num_queue_pairs <
7316 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7317 		return -EINVAL;
7318 	}
7319 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7320 		dev_err(&vsi->back->pdev->dev,
7321 			"Invalid max tx rate specified\n");
7322 		return -EINVAL;
7323 	}
7324 	return 0;
7325 }
7326 
7327 /**
7328  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7329  * @vsi: the VSI being configured
7330  **/
7331 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7332 {
7333 	u16 qcount;
7334 	int i;
7335 
7336 	/* Only TC0 is enabled */
7337 	vsi->tc_config.numtc = 1;
7338 	vsi->tc_config.enabled_tc = 1;
7339 	qcount = min_t(int, vsi->alloc_queue_pairs,
7340 		       i40e_pf_get_max_q_per_tc(vsi->back));
7341 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7342 		/* For the TC that is not enabled set the offset to default
7343 		 * queue and allocate one queue for the given TC.
7344 		 */
7345 		vsi->tc_config.tc_info[i].qoffset = 0;
7346 		if (i == 0)
7347 			vsi->tc_config.tc_info[i].qcount = qcount;
7348 		else
7349 			vsi->tc_config.tc_info[i].qcount = 1;
7350 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7351 	}
7352 }
7353 
7354 /**
7355  * i40e_del_macvlan_filter
7356  * @hw: pointer to the HW structure
7357  * @seid: seid of the channel VSI
7358  * @macaddr: the mac address to apply as a filter
7359  * @aq_err: store the admin Q error
7360  *
7361  * This function deletes a mac filter on the channel VSI which serves as the
7362  * macvlan. Returns 0 on success.
7363  **/
7364 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7365 					   const u8 *macaddr, int *aq_err)
7366 {
7367 	struct i40e_aqc_remove_macvlan_element_data element;
7368 	i40e_status status;
7369 
7370 	memset(&element, 0, sizeof(element));
7371 	ether_addr_copy(element.mac_addr, macaddr);
7372 	element.vlan_tag = 0;
7373 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7374 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7375 	*aq_err = hw->aq.asq_last_status;
7376 
7377 	return status;
7378 }
7379 
7380 /**
7381  * i40e_add_macvlan_filter
7382  * @hw: pointer to the HW structure
7383  * @seid: seid of the channel VSI
7384  * @macaddr: the mac address to apply as a filter
7385  * @aq_err: store the admin Q error
7386  *
7387  * This function adds a mac filter on the channel VSI which serves as the
7388  * macvlan. Returns 0 on success.
7389  **/
7390 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7391 					   const u8 *macaddr, int *aq_err)
7392 {
7393 	struct i40e_aqc_add_macvlan_element_data element;
7394 	i40e_status status;
7395 	u16 cmd_flags = 0;
7396 
7397 	ether_addr_copy(element.mac_addr, macaddr);
7398 	element.vlan_tag = 0;
7399 	element.queue_number = 0;
7400 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7401 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7402 	element.flags = cpu_to_le16(cmd_flags);
7403 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7404 	*aq_err = hw->aq.asq_last_status;
7405 
7406 	return status;
7407 }
7408 
7409 /**
7410  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7411  * @vsi: the VSI we want to access
7412  * @ch: the channel we want to access
7413  */
7414 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7415 {
7416 	struct i40e_ring *tx_ring, *rx_ring;
7417 	u16 pf_q;
7418 	int i;
7419 
7420 	for (i = 0; i < ch->num_queue_pairs; i++) {
7421 		pf_q = ch->base_queue + i;
7422 		tx_ring = vsi->tx_rings[pf_q];
7423 		tx_ring->ch = NULL;
7424 		rx_ring = vsi->rx_rings[pf_q];
7425 		rx_ring->ch = NULL;
7426 	}
7427 }
7428 
7429 /**
7430  * i40e_free_macvlan_channels
7431  * @vsi: the VSI we want to access
7432  *
7433  * This function frees the Qs of the channel VSI from
7434  * the stack and also deletes the channel VSIs which
7435  * serve as macvlans.
7436  */
7437 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7438 {
7439 	struct i40e_channel *ch, *ch_tmp;
7440 	int ret;
7441 
7442 	if (list_empty(&vsi->macvlan_list))
7443 		return;
7444 
7445 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7446 		struct i40e_vsi *parent_vsi;
7447 
7448 		if (i40e_is_channel_macvlan(ch)) {
7449 			i40e_reset_ch_rings(vsi, ch);
7450 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7451 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7452 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7453 			kfree(ch->fwd);
7454 			ch->fwd = NULL;
7455 		}
7456 
7457 		list_del(&ch->list);
7458 		parent_vsi = ch->parent_vsi;
7459 		if (!parent_vsi || !ch->initialized) {
7460 			kfree(ch);
7461 			continue;
7462 		}
7463 
7464 		/* remove the VSI */
7465 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7466 					     NULL);
7467 		if (ret)
7468 			dev_err(&vsi->back->pdev->dev,
7469 				"unable to remove channel (%d) for parent VSI(%d)\n",
7470 				ch->seid, parent_vsi->seid);
7471 		kfree(ch);
7472 	}
7473 	vsi->macvlan_cnt = 0;
7474 }
7475 
7476 /**
7477  * i40e_fwd_ring_up - bring the macvlan device up
7478  * @vsi: the VSI we want to access
7479  * @vdev: macvlan netdevice
7480  * @fwd: the private fwd structure
7481  */
7482 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7483 			    struct i40e_fwd_adapter *fwd)
7484 {
7485 	int ret = 0, num_tc = 1,  i, aq_err;
7486 	struct i40e_channel *ch, *ch_tmp;
7487 	struct i40e_pf *pf = vsi->back;
7488 	struct i40e_hw *hw = &pf->hw;
7489 
7490 	if (list_empty(&vsi->macvlan_list))
7491 		return -EINVAL;
7492 
7493 	/* Go through the list and find an available channel */
7494 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7495 		if (!i40e_is_channel_macvlan(ch)) {
7496 			ch->fwd = fwd;
7497 			/* record configuration for macvlan interface in vdev */
7498 			for (i = 0; i < num_tc; i++)
7499 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7500 							     i,
7501 							     ch->num_queue_pairs,
7502 							     ch->base_queue);
7503 			for (i = 0; i < ch->num_queue_pairs; i++) {
7504 				struct i40e_ring *tx_ring, *rx_ring;
7505 				u16 pf_q;
7506 
7507 				pf_q = ch->base_queue + i;
7508 
7509 				/* Get to TX ring ptr */
7510 				tx_ring = vsi->tx_rings[pf_q];
7511 				tx_ring->ch = ch;
7512 
7513 				/* Get the RX ring ptr */
7514 				rx_ring = vsi->rx_rings[pf_q];
7515 				rx_ring->ch = ch;
7516 			}
7517 			break;
7518 		}
7519 	}
7520 
7521 	/* Guarantee all rings are updated before we update the
7522 	 * MAC address filter.
7523 	 */
7524 	wmb();
7525 
7526 	/* Add a mac filter */
7527 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7528 	if (ret) {
7529 		/* if we cannot add the MAC rule then disable the offload */
7530 		macvlan_release_l2fw_offload(vdev);
7531 		for (i = 0; i < ch->num_queue_pairs; i++) {
7532 			struct i40e_ring *rx_ring;
7533 			u16 pf_q;
7534 
7535 			pf_q = ch->base_queue + i;
7536 			rx_ring = vsi->rx_rings[pf_q];
7537 			rx_ring->netdev = NULL;
7538 		}
7539 		dev_info(&pf->pdev->dev,
7540 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7541 			  i40e_stat_str(hw, ret),
7542 			  i40e_aq_str(hw, aq_err));
7543 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7544 	}
7545 
7546 	return ret;
7547 }
7548 
7549 /**
7550  * i40e_setup_macvlans - create the channels which will be macvlans
7551  * @vsi: the VSI we want to access
7552  * @macvlan_cnt: no. of macvlans to be setup
7553  * @qcnt: no. of Qs per macvlan
7554  * @vdev: macvlan netdevice
7555  */
7556 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7557 			       struct net_device *vdev)
7558 {
7559 	struct i40e_pf *pf = vsi->back;
7560 	struct i40e_hw *hw = &pf->hw;
7561 	struct i40e_vsi_context ctxt;
7562 	u16 sections, qmap, num_qps;
7563 	struct i40e_channel *ch;
7564 	int i, pow, ret = 0;
7565 	u8 offset = 0;
7566 
7567 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7568 		return -EINVAL;
7569 
7570 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7571 
7572 	/* find the next higher power-of-2 of num queue pairs */
7573 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7574 
7575 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7576 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7577 
7578 	/* Setup context bits for the main VSI */
7579 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7580 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7581 	memset(&ctxt, 0, sizeof(ctxt));
7582 	ctxt.seid = vsi->seid;
7583 	ctxt.pf_num = vsi->back->hw.pf_id;
7584 	ctxt.vf_num = 0;
7585 	ctxt.uplink_seid = vsi->uplink_seid;
7586 	ctxt.info = vsi->info;
7587 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7588 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7589 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7590 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7591 
7592 	/* Reconfigure RSS for main VSI with new max queue count */
7593 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7594 	ret = i40e_vsi_config_rss(vsi);
7595 	if (ret) {
7596 		dev_info(&pf->pdev->dev,
7597 			 "Failed to reconfig RSS for num_queues (%u)\n",
7598 			 vsi->rss_size);
7599 		return ret;
7600 	}
7601 	vsi->reconfig_rss = true;
7602 	dev_dbg(&vsi->back->pdev->dev,
7603 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7604 	vsi->next_base_queue = num_qps;
7605 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7606 
7607 	/* Update the VSI after updating the VSI queue-mapping
7608 	 * information
7609 	 */
7610 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7611 	if (ret) {
7612 		dev_info(&pf->pdev->dev,
7613 			 "Update vsi tc config failed, err %s aq_err %s\n",
7614 			 i40e_stat_str(hw, ret),
7615 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7616 		return ret;
7617 	}
7618 	/* update the local VSI info with updated queue map */
7619 	i40e_vsi_update_queue_map(vsi, &ctxt);
7620 	vsi->info.valid_sections = 0;
7621 
7622 	/* Create channels for macvlans */
7623 	INIT_LIST_HEAD(&vsi->macvlan_list);
7624 	for (i = 0; i < macvlan_cnt; i++) {
7625 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7626 		if (!ch) {
7627 			ret = -ENOMEM;
7628 			goto err_free;
7629 		}
7630 		INIT_LIST_HEAD(&ch->list);
7631 		ch->num_queue_pairs = qcnt;
7632 		if (!i40e_setup_channel(pf, vsi, ch)) {
7633 			ret = -EINVAL;
7634 			kfree(ch);
7635 			goto err_free;
7636 		}
7637 		ch->parent_vsi = vsi;
7638 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7639 		vsi->macvlan_cnt++;
7640 		list_add_tail(&ch->list, &vsi->macvlan_list);
7641 	}
7642 
7643 	return ret;
7644 
7645 err_free:
7646 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7647 	i40e_free_macvlan_channels(vsi);
7648 
7649 	return ret;
7650 }
7651 
7652 /**
7653  * i40e_fwd_add - configure macvlans
7654  * @netdev: net device to configure
7655  * @vdev: macvlan netdevice
7656  **/
7657 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7658 {
7659 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7660 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7661 	struct i40e_vsi *vsi = np->vsi;
7662 	struct i40e_pf *pf = vsi->back;
7663 	struct i40e_fwd_adapter *fwd;
7664 	int avail_macvlan, ret;
7665 
7666 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7667 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7668 		return ERR_PTR(-EINVAL);
7669 	}
7670 	if ((pf->flags & I40E_FLAG_TC_MQPRIO)) {
7671 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7672 		return ERR_PTR(-EINVAL);
7673 	}
7674 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7675 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7676 		return ERR_PTR(-EINVAL);
7677 	}
7678 
7679 	/* The macvlan device has to be a single Q device so that the
7680 	 * tc_to_txq field can be reused to pick the tx queue.
7681 	 */
7682 	if (netif_is_multiqueue(vdev))
7683 		return ERR_PTR(-ERANGE);
7684 
7685 	if (!vsi->macvlan_cnt) {
7686 		/* reserve bit 0 for the pf device */
7687 		set_bit(0, vsi->fwd_bitmask);
7688 
7689 		/* Try to reserve as many queues as possible for macvlans. First
7690 		 * reserve 3/4th of max vectors, then half, then quarter and
7691 		 * calculate Qs per macvlan as you go
7692 		 */
7693 		vectors = pf->num_lan_msix;
7694 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7695 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7696 			q_per_macvlan = 4;
7697 			macvlan_cnt = (vectors - 32) / 4;
7698 		} else if (vectors <= 64 && vectors > 32) {
7699 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7700 			q_per_macvlan = 2;
7701 			macvlan_cnt = (vectors - 16) / 2;
7702 		} else if (vectors <= 32 && vectors > 16) {
7703 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7704 			q_per_macvlan = 1;
7705 			macvlan_cnt = vectors - 16;
7706 		} else if (vectors <= 16 && vectors > 8) {
7707 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7708 			q_per_macvlan = 1;
7709 			macvlan_cnt = vectors - 8;
7710 		} else {
7711 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7712 			q_per_macvlan = 1;
7713 			macvlan_cnt = vectors - 1;
7714 		}
7715 
7716 		if (macvlan_cnt == 0)
7717 			return ERR_PTR(-EBUSY);
7718 
7719 		/* Quiesce VSI queues */
7720 		i40e_quiesce_vsi(vsi);
7721 
7722 		/* sets up the macvlans but does not "enable" them */
7723 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
7724 					  vdev);
7725 		if (ret)
7726 			return ERR_PTR(ret);
7727 
7728 		/* Unquiesce VSI */
7729 		i40e_unquiesce_vsi(vsi);
7730 	}
7731 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
7732 					    vsi->macvlan_cnt);
7733 	if (avail_macvlan >= I40E_MAX_MACVLANS)
7734 		return ERR_PTR(-EBUSY);
7735 
7736 	/* create the fwd struct */
7737 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
7738 	if (!fwd)
7739 		return ERR_PTR(-ENOMEM);
7740 
7741 	set_bit(avail_macvlan, vsi->fwd_bitmask);
7742 	fwd->bit_no = avail_macvlan;
7743 	netdev_set_sb_channel(vdev, avail_macvlan);
7744 	fwd->netdev = vdev;
7745 
7746 	if (!netif_running(netdev))
7747 		return fwd;
7748 
7749 	/* Set fwd ring up */
7750 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
7751 	if (ret) {
7752 		/* unbind the queues and drop the subordinate channel config */
7753 		netdev_unbind_sb_channel(netdev, vdev);
7754 		netdev_set_sb_channel(vdev, 0);
7755 
7756 		kfree(fwd);
7757 		return ERR_PTR(-EINVAL);
7758 	}
7759 
7760 	return fwd;
7761 }
7762 
7763 /**
7764  * i40e_del_all_macvlans - Delete all the mac filters on the channels
7765  * @vsi: the VSI we want to access
7766  */
7767 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
7768 {
7769 	struct i40e_channel *ch, *ch_tmp;
7770 	struct i40e_pf *pf = vsi->back;
7771 	struct i40e_hw *hw = &pf->hw;
7772 	int aq_err, ret = 0;
7773 
7774 	if (list_empty(&vsi->macvlan_list))
7775 		return;
7776 
7777 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7778 		if (i40e_is_channel_macvlan(ch)) {
7779 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7780 						      i40e_channel_mac(ch),
7781 						      &aq_err);
7782 			if (!ret) {
7783 				/* Reset queue contexts */
7784 				i40e_reset_ch_rings(vsi, ch);
7785 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7786 				netdev_unbind_sb_channel(vsi->netdev,
7787 							 ch->fwd->netdev);
7788 				netdev_set_sb_channel(ch->fwd->netdev, 0);
7789 				kfree(ch->fwd);
7790 				ch->fwd = NULL;
7791 			}
7792 		}
7793 	}
7794 }
7795 
7796 /**
7797  * i40e_fwd_del - delete macvlan interfaces
7798  * @netdev: net device to configure
7799  * @vdev: macvlan netdevice
7800  */
7801 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
7802 {
7803 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7804 	struct i40e_fwd_adapter *fwd = vdev;
7805 	struct i40e_channel *ch, *ch_tmp;
7806 	struct i40e_vsi *vsi = np->vsi;
7807 	struct i40e_pf *pf = vsi->back;
7808 	struct i40e_hw *hw = &pf->hw;
7809 	int aq_err, ret = 0;
7810 
7811 	/* Find the channel associated with the macvlan and del mac filter */
7812 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7813 		if (i40e_is_channel_macvlan(ch) &&
7814 		    ether_addr_equal(i40e_channel_mac(ch),
7815 				     fwd->netdev->dev_addr)) {
7816 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7817 						      i40e_channel_mac(ch),
7818 						      &aq_err);
7819 			if (!ret) {
7820 				/* Reset queue contexts */
7821 				i40e_reset_ch_rings(vsi, ch);
7822 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7823 				netdev_unbind_sb_channel(netdev, fwd->netdev);
7824 				netdev_set_sb_channel(fwd->netdev, 0);
7825 				kfree(ch->fwd);
7826 				ch->fwd = NULL;
7827 			} else {
7828 				dev_info(&pf->pdev->dev,
7829 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
7830 					  i40e_stat_str(hw, ret),
7831 					  i40e_aq_str(hw, aq_err));
7832 			}
7833 			break;
7834 		}
7835 	}
7836 }
7837 
7838 /**
7839  * i40e_setup_tc - configure multiple traffic classes
7840  * @netdev: net device to configure
7841  * @type_data: tc offload data
7842  **/
7843 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
7844 {
7845 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
7846 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7847 	struct i40e_vsi *vsi = np->vsi;
7848 	struct i40e_pf *pf = vsi->back;
7849 	u8 enabled_tc = 0, num_tc, hw;
7850 	bool need_reset = false;
7851 	int old_queue_pairs;
7852 	int ret = -EINVAL;
7853 	u16 mode;
7854 	int i;
7855 
7856 	old_queue_pairs = vsi->num_queue_pairs;
7857 	num_tc = mqprio_qopt->qopt.num_tc;
7858 	hw = mqprio_qopt->qopt.hw;
7859 	mode = mqprio_qopt->mode;
7860 	if (!hw) {
7861 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7862 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
7863 		goto config_tc;
7864 	}
7865 
7866 	/* Check if MFP enabled */
7867 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
7868 		netdev_info(netdev,
7869 			    "Configuring TC not supported in MFP mode\n");
7870 		return ret;
7871 	}
7872 	switch (mode) {
7873 	case TC_MQPRIO_MODE_DCB:
7874 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7875 
7876 		/* Check if DCB enabled to continue */
7877 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7878 			netdev_info(netdev,
7879 				    "DCB is not enabled for adapter\n");
7880 			return ret;
7881 		}
7882 
7883 		/* Check whether tc count is within enabled limit */
7884 		if (num_tc > i40e_pf_get_num_tc(pf)) {
7885 			netdev_info(netdev,
7886 				    "TC count greater than enabled on link for adapter\n");
7887 			return ret;
7888 		}
7889 		break;
7890 	case TC_MQPRIO_MODE_CHANNEL:
7891 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
7892 			netdev_info(netdev,
7893 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
7894 			return ret;
7895 		}
7896 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7897 			return ret;
7898 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
7899 		if (ret)
7900 			return ret;
7901 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
7902 		       sizeof(*mqprio_qopt));
7903 		pf->flags |= I40E_FLAG_TC_MQPRIO;
7904 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7905 		break;
7906 	default:
7907 		return -EINVAL;
7908 	}
7909 
7910 config_tc:
7911 	/* Generate TC map for number of tc requested */
7912 	for (i = 0; i < num_tc; i++)
7913 		enabled_tc |= BIT(i);
7914 
7915 	/* Requesting same TC configuration as already enabled */
7916 	if (enabled_tc == vsi->tc_config.enabled_tc &&
7917 	    mode != TC_MQPRIO_MODE_CHANNEL)
7918 		return 0;
7919 
7920 	/* Quiesce VSI queues */
7921 	i40e_quiesce_vsi(vsi);
7922 
7923 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
7924 		i40e_remove_queue_channels(vsi);
7925 
7926 	/* Configure VSI for enabled TCs */
7927 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
7928 	if (ret) {
7929 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
7930 			    vsi->seid);
7931 		need_reset = true;
7932 		goto exit;
7933 	} else {
7934 		dev_info(&vsi->back->pdev->dev,
7935 			 "Setup channel (id:%u) utilizing num_queues %d\n",
7936 			 vsi->seid, vsi->tc_config.tc_info[0].qcount);
7937 	}
7938 
7939 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
7940 		if (vsi->mqprio_qopt.max_rate[0]) {
7941 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
7942 
7943 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
7944 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
7945 			if (!ret) {
7946 				u64 credits = max_tx_rate;
7947 
7948 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
7949 				dev_dbg(&vsi->back->pdev->dev,
7950 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
7951 					max_tx_rate,
7952 					credits,
7953 					vsi->seid);
7954 			} else {
7955 				need_reset = true;
7956 				goto exit;
7957 			}
7958 		}
7959 		ret = i40e_configure_queue_channels(vsi);
7960 		if (ret) {
7961 			vsi->num_queue_pairs = old_queue_pairs;
7962 			netdev_info(netdev,
7963 				    "Failed configuring queue channels\n");
7964 			need_reset = true;
7965 			goto exit;
7966 		}
7967 	}
7968 
7969 exit:
7970 	/* Reset the configuration data to defaults, only TC0 is enabled */
7971 	if (need_reset) {
7972 		i40e_vsi_set_default_tc_config(vsi);
7973 		need_reset = false;
7974 	}
7975 
7976 	/* Unquiesce VSI */
7977 	i40e_unquiesce_vsi(vsi);
7978 	return ret;
7979 }
7980 
7981 /**
7982  * i40e_set_cld_element - sets cloud filter element data
7983  * @filter: cloud filter rule
7984  * @cld: ptr to cloud filter element data
7985  *
7986  * This is helper function to copy data into cloud filter element
7987  **/
7988 static inline void
7989 i40e_set_cld_element(struct i40e_cloud_filter *filter,
7990 		     struct i40e_aqc_cloud_filters_element_data *cld)
7991 {
7992 	u32 ipa;
7993 	int i;
7994 
7995 	memset(cld, 0, sizeof(*cld));
7996 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
7997 	ether_addr_copy(cld->inner_mac, filter->src_mac);
7998 
7999 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8000 		return;
8001 
8002 	if (filter->n_proto == ETH_P_IPV6) {
8003 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8004 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8005 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8006 
8007 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8008 		}
8009 	} else {
8010 		ipa = be32_to_cpu(filter->dst_ipv4);
8011 
8012 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8013 	}
8014 
8015 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8016 
8017 	/* tenant_id is not supported by FW now, once the support is enabled
8018 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8019 	 */
8020 	if (filter->tenant_id)
8021 		return;
8022 }
8023 
8024 /**
8025  * i40e_add_del_cloud_filter - Add/del cloud filter
8026  * @vsi: pointer to VSI
8027  * @filter: cloud filter rule
8028  * @add: if true, add, if false, delete
8029  *
8030  * Add or delete a cloud filter for a specific flow spec.
8031  * Returns 0 if the filter were successfully added.
8032  **/
8033 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8034 			      struct i40e_cloud_filter *filter, bool add)
8035 {
8036 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8037 	struct i40e_pf *pf = vsi->back;
8038 	int ret;
8039 	static const u16 flag_table[128] = {
8040 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8041 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8042 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8043 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8044 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8045 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8046 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8047 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8048 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8049 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8050 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8051 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8052 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8053 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8054 	};
8055 
8056 	if (filter->flags >= ARRAY_SIZE(flag_table))
8057 		return I40E_ERR_CONFIG;
8058 
8059 	memset(&cld_filter, 0, sizeof(cld_filter));
8060 
8061 	/* copy element needed to add cloud filter from filter */
8062 	i40e_set_cld_element(filter, &cld_filter);
8063 
8064 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8065 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8066 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8067 
8068 	if (filter->n_proto == ETH_P_IPV6)
8069 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8070 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8071 	else
8072 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8073 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8074 
8075 	if (add)
8076 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8077 						&cld_filter, 1);
8078 	else
8079 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8080 						&cld_filter, 1);
8081 	if (ret)
8082 		dev_dbg(&pf->pdev->dev,
8083 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8084 			add ? "add" : "delete", filter->dst_port, ret,
8085 			pf->hw.aq.asq_last_status);
8086 	else
8087 		dev_info(&pf->pdev->dev,
8088 			 "%s cloud filter for VSI: %d\n",
8089 			 add ? "Added" : "Deleted", filter->seid);
8090 	return ret;
8091 }
8092 
8093 /**
8094  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8095  * @vsi: pointer to VSI
8096  * @filter: cloud filter rule
8097  * @add: if true, add, if false, delete
8098  *
8099  * Add or delete a cloud filter for a specific flow spec using big buffer.
8100  * Returns 0 if the filter were successfully added.
8101  **/
8102 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8103 				      struct i40e_cloud_filter *filter,
8104 				      bool add)
8105 {
8106 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8107 	struct i40e_pf *pf = vsi->back;
8108 	int ret;
8109 
8110 	/* Both (src/dst) valid mac_addr are not supported */
8111 	if ((is_valid_ether_addr(filter->dst_mac) &&
8112 	     is_valid_ether_addr(filter->src_mac)) ||
8113 	    (is_multicast_ether_addr(filter->dst_mac) &&
8114 	     is_multicast_ether_addr(filter->src_mac)))
8115 		return -EOPNOTSUPP;
8116 
8117 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8118 	 * ports are not supported via big buffer now.
8119 	 */
8120 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8121 		return -EOPNOTSUPP;
8122 
8123 	/* adding filter using src_port/src_ip is not supported at this stage */
8124 	if (filter->src_port ||
8125 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8126 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8127 		return -EOPNOTSUPP;
8128 
8129 	memset(&cld_filter, 0, sizeof(cld_filter));
8130 
8131 	/* copy element needed to add cloud filter from filter */
8132 	i40e_set_cld_element(filter, &cld_filter.element);
8133 
8134 	if (is_valid_ether_addr(filter->dst_mac) ||
8135 	    is_valid_ether_addr(filter->src_mac) ||
8136 	    is_multicast_ether_addr(filter->dst_mac) ||
8137 	    is_multicast_ether_addr(filter->src_mac)) {
8138 		/* MAC + IP : unsupported mode */
8139 		if (filter->dst_ipv4)
8140 			return -EOPNOTSUPP;
8141 
8142 		/* since we validated that L4 port must be valid before
8143 		 * we get here, start with respective "flags" value
8144 		 * and update if vlan is present or not
8145 		 */
8146 		cld_filter.element.flags =
8147 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8148 
8149 		if (filter->vlan_id) {
8150 			cld_filter.element.flags =
8151 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8152 		}
8153 
8154 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8155 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8156 		cld_filter.element.flags =
8157 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8158 		if (filter->n_proto == ETH_P_IPV6)
8159 			cld_filter.element.flags |=
8160 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8161 		else
8162 			cld_filter.element.flags |=
8163 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8164 	} else {
8165 		dev_err(&pf->pdev->dev,
8166 			"either mac or ip has to be valid for cloud filter\n");
8167 		return -EINVAL;
8168 	}
8169 
8170 	/* Now copy L4 port in Byte 6..7 in general fields */
8171 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8172 						be16_to_cpu(filter->dst_port);
8173 
8174 	if (add) {
8175 		/* Validate current device switch mode, change if necessary */
8176 		ret = i40e_validate_and_set_switch_mode(vsi);
8177 		if (ret) {
8178 			dev_err(&pf->pdev->dev,
8179 				"failed to set switch mode, ret %d\n",
8180 				ret);
8181 			return ret;
8182 		}
8183 
8184 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8185 						   &cld_filter, 1);
8186 	} else {
8187 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8188 						   &cld_filter, 1);
8189 	}
8190 
8191 	if (ret)
8192 		dev_dbg(&pf->pdev->dev,
8193 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8194 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8195 	else
8196 		dev_info(&pf->pdev->dev,
8197 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8198 			 add ? "add" : "delete", filter->seid,
8199 			 ntohs(filter->dst_port));
8200 	return ret;
8201 }
8202 
8203 /**
8204  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8205  * @vsi: Pointer to VSI
8206  * @f: Pointer to struct flow_cls_offload
8207  * @filter: Pointer to cloud filter structure
8208  *
8209  **/
8210 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8211 				 struct flow_cls_offload *f,
8212 				 struct i40e_cloud_filter *filter)
8213 {
8214 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8215 	struct flow_dissector *dissector = rule->match.dissector;
8216 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8217 	struct i40e_pf *pf = vsi->back;
8218 	u8 field_flags = 0;
8219 
8220 	if (dissector->used_keys &
8221 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8222 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8223 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8224 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8225 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8226 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8227 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8228 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8229 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8230 			dissector->used_keys);
8231 		return -EOPNOTSUPP;
8232 	}
8233 
8234 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8235 		struct flow_match_enc_keyid match;
8236 
8237 		flow_rule_match_enc_keyid(rule, &match);
8238 		if (match.mask->keyid != 0)
8239 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8240 
8241 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8242 	}
8243 
8244 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8245 		struct flow_match_basic match;
8246 
8247 		flow_rule_match_basic(rule, &match);
8248 		n_proto_key = ntohs(match.key->n_proto);
8249 		n_proto_mask = ntohs(match.mask->n_proto);
8250 
8251 		if (n_proto_key == ETH_P_ALL) {
8252 			n_proto_key = 0;
8253 			n_proto_mask = 0;
8254 		}
8255 		filter->n_proto = n_proto_key & n_proto_mask;
8256 		filter->ip_proto = match.key->ip_proto;
8257 	}
8258 
8259 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8260 		struct flow_match_eth_addrs match;
8261 
8262 		flow_rule_match_eth_addrs(rule, &match);
8263 
8264 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8265 		if (!is_zero_ether_addr(match.mask->dst)) {
8266 			if (is_broadcast_ether_addr(match.mask->dst)) {
8267 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8268 			} else {
8269 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8270 					match.mask->dst);
8271 				return I40E_ERR_CONFIG;
8272 			}
8273 		}
8274 
8275 		if (!is_zero_ether_addr(match.mask->src)) {
8276 			if (is_broadcast_ether_addr(match.mask->src)) {
8277 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8278 			} else {
8279 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8280 					match.mask->src);
8281 				return I40E_ERR_CONFIG;
8282 			}
8283 		}
8284 		ether_addr_copy(filter->dst_mac, match.key->dst);
8285 		ether_addr_copy(filter->src_mac, match.key->src);
8286 	}
8287 
8288 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8289 		struct flow_match_vlan match;
8290 
8291 		flow_rule_match_vlan(rule, &match);
8292 		if (match.mask->vlan_id) {
8293 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8294 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8295 
8296 			} else {
8297 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8298 					match.mask->vlan_id);
8299 				return I40E_ERR_CONFIG;
8300 			}
8301 		}
8302 
8303 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8304 	}
8305 
8306 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8307 		struct flow_match_control match;
8308 
8309 		flow_rule_match_control(rule, &match);
8310 		addr_type = match.key->addr_type;
8311 	}
8312 
8313 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8314 		struct flow_match_ipv4_addrs match;
8315 
8316 		flow_rule_match_ipv4_addrs(rule, &match);
8317 		if (match.mask->dst) {
8318 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8319 				field_flags |= I40E_CLOUD_FIELD_IIP;
8320 			} else {
8321 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8322 					&match.mask->dst);
8323 				return I40E_ERR_CONFIG;
8324 			}
8325 		}
8326 
8327 		if (match.mask->src) {
8328 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8329 				field_flags |= I40E_CLOUD_FIELD_IIP;
8330 			} else {
8331 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8332 					&match.mask->src);
8333 				return I40E_ERR_CONFIG;
8334 			}
8335 		}
8336 
8337 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8338 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8339 			return I40E_ERR_CONFIG;
8340 		}
8341 		filter->dst_ipv4 = match.key->dst;
8342 		filter->src_ipv4 = match.key->src;
8343 	}
8344 
8345 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8346 		struct flow_match_ipv6_addrs match;
8347 
8348 		flow_rule_match_ipv6_addrs(rule, &match);
8349 
8350 		/* src and dest IPV6 address should not be LOOPBACK
8351 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8352 		 */
8353 		if (ipv6_addr_loopback(&match.key->dst) ||
8354 		    ipv6_addr_loopback(&match.key->src)) {
8355 			dev_err(&pf->pdev->dev,
8356 				"Bad ipv6, addr is LOOPBACK\n");
8357 			return I40E_ERR_CONFIG;
8358 		}
8359 		if (!ipv6_addr_any(&match.mask->dst) ||
8360 		    !ipv6_addr_any(&match.mask->src))
8361 			field_flags |= I40E_CLOUD_FIELD_IIP;
8362 
8363 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8364 		       sizeof(filter->src_ipv6));
8365 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8366 		       sizeof(filter->dst_ipv6));
8367 	}
8368 
8369 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8370 		struct flow_match_ports match;
8371 
8372 		flow_rule_match_ports(rule, &match);
8373 		if (match.mask->src) {
8374 			if (match.mask->src == cpu_to_be16(0xffff)) {
8375 				field_flags |= I40E_CLOUD_FIELD_IIP;
8376 			} else {
8377 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8378 					be16_to_cpu(match.mask->src));
8379 				return I40E_ERR_CONFIG;
8380 			}
8381 		}
8382 
8383 		if (match.mask->dst) {
8384 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8385 				field_flags |= I40E_CLOUD_FIELD_IIP;
8386 			} else {
8387 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8388 					be16_to_cpu(match.mask->dst));
8389 				return I40E_ERR_CONFIG;
8390 			}
8391 		}
8392 
8393 		filter->dst_port = match.key->dst;
8394 		filter->src_port = match.key->src;
8395 
8396 		switch (filter->ip_proto) {
8397 		case IPPROTO_TCP:
8398 		case IPPROTO_UDP:
8399 			break;
8400 		default:
8401 			dev_err(&pf->pdev->dev,
8402 				"Only UDP and TCP transport are supported\n");
8403 			return -EINVAL;
8404 		}
8405 	}
8406 	filter->flags = field_flags;
8407 	return 0;
8408 }
8409 
8410 /**
8411  * i40e_handle_tclass: Forward to a traffic class on the device
8412  * @vsi: Pointer to VSI
8413  * @tc: traffic class index on the device
8414  * @filter: Pointer to cloud filter structure
8415  *
8416  **/
8417 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8418 			      struct i40e_cloud_filter *filter)
8419 {
8420 	struct i40e_channel *ch, *ch_tmp;
8421 
8422 	/* direct to a traffic class on the same device */
8423 	if (tc == 0) {
8424 		filter->seid = vsi->seid;
8425 		return 0;
8426 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8427 		if (!filter->dst_port) {
8428 			dev_err(&vsi->back->pdev->dev,
8429 				"Specify destination port to direct to traffic class that is not default\n");
8430 			return -EINVAL;
8431 		}
8432 		if (list_empty(&vsi->ch_list))
8433 			return -EINVAL;
8434 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8435 					 list) {
8436 			if (ch->seid == vsi->tc_seid_map[tc])
8437 				filter->seid = ch->seid;
8438 		}
8439 		return 0;
8440 	}
8441 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8442 	return -EINVAL;
8443 }
8444 
8445 /**
8446  * i40e_configure_clsflower - Configure tc flower filters
8447  * @vsi: Pointer to VSI
8448  * @cls_flower: Pointer to struct flow_cls_offload
8449  *
8450  **/
8451 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8452 				    struct flow_cls_offload *cls_flower)
8453 {
8454 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8455 	struct i40e_cloud_filter *filter = NULL;
8456 	struct i40e_pf *pf = vsi->back;
8457 	int err = 0;
8458 
8459 	if (tc < 0) {
8460 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8461 		return -EOPNOTSUPP;
8462 	}
8463 
8464 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8465 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8466 		return -EBUSY;
8467 
8468 	if (pf->fdir_pf_active_filters ||
8469 	    (!hlist_empty(&pf->fdir_filter_list))) {
8470 		dev_err(&vsi->back->pdev->dev,
8471 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8472 		return -EINVAL;
8473 	}
8474 
8475 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8476 		dev_err(&vsi->back->pdev->dev,
8477 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8478 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8479 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8480 	}
8481 
8482 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8483 	if (!filter)
8484 		return -ENOMEM;
8485 
8486 	filter->cookie = cls_flower->cookie;
8487 
8488 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8489 	if (err < 0)
8490 		goto err;
8491 
8492 	err = i40e_handle_tclass(vsi, tc, filter);
8493 	if (err < 0)
8494 		goto err;
8495 
8496 	/* Add cloud filter */
8497 	if (filter->dst_port)
8498 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8499 	else
8500 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8501 
8502 	if (err) {
8503 		dev_err(&pf->pdev->dev,
8504 			"Failed to add cloud filter, err %s\n",
8505 			i40e_stat_str(&pf->hw, err));
8506 		goto err;
8507 	}
8508 
8509 	/* add filter to the ordered list */
8510 	INIT_HLIST_NODE(&filter->cloud_node);
8511 
8512 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8513 
8514 	pf->num_cloud_filters++;
8515 
8516 	return err;
8517 err:
8518 	kfree(filter);
8519 	return err;
8520 }
8521 
8522 /**
8523  * i40e_find_cloud_filter - Find the could filter in the list
8524  * @vsi: Pointer to VSI
8525  * @cookie: filter specific cookie
8526  *
8527  **/
8528 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8529 							unsigned long *cookie)
8530 {
8531 	struct i40e_cloud_filter *filter = NULL;
8532 	struct hlist_node *node2;
8533 
8534 	hlist_for_each_entry_safe(filter, node2,
8535 				  &vsi->back->cloud_filter_list, cloud_node)
8536 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8537 			return filter;
8538 	return NULL;
8539 }
8540 
8541 /**
8542  * i40e_delete_clsflower - Remove tc flower filters
8543  * @vsi: Pointer to VSI
8544  * @cls_flower: Pointer to struct flow_cls_offload
8545  *
8546  **/
8547 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8548 				 struct flow_cls_offload *cls_flower)
8549 {
8550 	struct i40e_cloud_filter *filter = NULL;
8551 	struct i40e_pf *pf = vsi->back;
8552 	int err = 0;
8553 
8554 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8555 
8556 	if (!filter)
8557 		return -EINVAL;
8558 
8559 	hash_del(&filter->cloud_node);
8560 
8561 	if (filter->dst_port)
8562 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8563 	else
8564 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8565 
8566 	kfree(filter);
8567 	if (err) {
8568 		dev_err(&pf->pdev->dev,
8569 			"Failed to delete cloud filter, err %s\n",
8570 			i40e_stat_str(&pf->hw, err));
8571 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8572 	}
8573 
8574 	pf->num_cloud_filters--;
8575 	if (!pf->num_cloud_filters)
8576 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8577 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8578 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8579 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8580 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8581 		}
8582 	return 0;
8583 }
8584 
8585 /**
8586  * i40e_setup_tc_cls_flower - flower classifier offloads
8587  * @np: net device to configure
8588  * @cls_flower: offload data
8589  **/
8590 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8591 				    struct flow_cls_offload *cls_flower)
8592 {
8593 	struct i40e_vsi *vsi = np->vsi;
8594 
8595 	switch (cls_flower->command) {
8596 	case FLOW_CLS_REPLACE:
8597 		return i40e_configure_clsflower(vsi, cls_flower);
8598 	case FLOW_CLS_DESTROY:
8599 		return i40e_delete_clsflower(vsi, cls_flower);
8600 	case FLOW_CLS_STATS:
8601 		return -EOPNOTSUPP;
8602 	default:
8603 		return -EOPNOTSUPP;
8604 	}
8605 }
8606 
8607 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8608 				  void *cb_priv)
8609 {
8610 	struct i40e_netdev_priv *np = cb_priv;
8611 
8612 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8613 		return -EOPNOTSUPP;
8614 
8615 	switch (type) {
8616 	case TC_SETUP_CLSFLOWER:
8617 		return i40e_setup_tc_cls_flower(np, type_data);
8618 
8619 	default:
8620 		return -EOPNOTSUPP;
8621 	}
8622 }
8623 
8624 static LIST_HEAD(i40e_block_cb_list);
8625 
8626 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8627 			   void *type_data)
8628 {
8629 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8630 
8631 	switch (type) {
8632 	case TC_SETUP_QDISC_MQPRIO:
8633 		return i40e_setup_tc(netdev, type_data);
8634 	case TC_SETUP_BLOCK:
8635 		return flow_block_cb_setup_simple(type_data,
8636 						  &i40e_block_cb_list,
8637 						  i40e_setup_tc_block_cb,
8638 						  np, np, true);
8639 	default:
8640 		return -EOPNOTSUPP;
8641 	}
8642 }
8643 
8644 /**
8645  * i40e_open - Called when a network interface is made active
8646  * @netdev: network interface device structure
8647  *
8648  * The open entry point is called when a network interface is made
8649  * active by the system (IFF_UP).  At this point all resources needed
8650  * for transmit and receive operations are allocated, the interrupt
8651  * handler is registered with the OS, the netdev watchdog subtask is
8652  * enabled, and the stack is notified that the interface is ready.
8653  *
8654  * Returns 0 on success, negative value on failure
8655  **/
8656 int i40e_open(struct net_device *netdev)
8657 {
8658 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8659 	struct i40e_vsi *vsi = np->vsi;
8660 	struct i40e_pf *pf = vsi->back;
8661 	int err;
8662 
8663 	/* disallow open during test or if eeprom is broken */
8664 	if (test_bit(__I40E_TESTING, pf->state) ||
8665 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8666 		return -EBUSY;
8667 
8668 	netif_carrier_off(netdev);
8669 
8670 	if (i40e_force_link_state(pf, true))
8671 		return -EAGAIN;
8672 
8673 	err = i40e_vsi_open(vsi);
8674 	if (err)
8675 		return err;
8676 
8677 	/* configure global TSO hardware offload settings */
8678 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8679 						       TCP_FLAG_FIN) >> 16);
8680 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8681 						       TCP_FLAG_FIN |
8682 						       TCP_FLAG_CWR) >> 16);
8683 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8684 	udp_tunnel_get_rx_info(netdev);
8685 
8686 	return 0;
8687 }
8688 
8689 /**
8690  * i40e_vsi_open -
8691  * @vsi: the VSI to open
8692  *
8693  * Finish initialization of the VSI.
8694  *
8695  * Returns 0 on success, negative value on failure
8696  *
8697  * Note: expects to be called while under rtnl_lock()
8698  **/
8699 int i40e_vsi_open(struct i40e_vsi *vsi)
8700 {
8701 	struct i40e_pf *pf = vsi->back;
8702 	char int_name[I40E_INT_NAME_STR_LEN];
8703 	int err;
8704 
8705 	/* allocate descriptors */
8706 	err = i40e_vsi_setup_tx_resources(vsi);
8707 	if (err)
8708 		goto err_setup_tx;
8709 	err = i40e_vsi_setup_rx_resources(vsi);
8710 	if (err)
8711 		goto err_setup_rx;
8712 
8713 	err = i40e_vsi_configure(vsi);
8714 	if (err)
8715 		goto err_setup_rx;
8716 
8717 	if (vsi->netdev) {
8718 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
8719 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
8720 		err = i40e_vsi_request_irq(vsi, int_name);
8721 		if (err)
8722 			goto err_setup_rx;
8723 
8724 		/* Notify the stack of the actual queue counts. */
8725 		err = netif_set_real_num_tx_queues(vsi->netdev,
8726 						   vsi->num_queue_pairs);
8727 		if (err)
8728 			goto err_set_queues;
8729 
8730 		err = netif_set_real_num_rx_queues(vsi->netdev,
8731 						   vsi->num_queue_pairs);
8732 		if (err)
8733 			goto err_set_queues;
8734 
8735 	} else if (vsi->type == I40E_VSI_FDIR) {
8736 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
8737 			 dev_driver_string(&pf->pdev->dev),
8738 			 dev_name(&pf->pdev->dev));
8739 		err = i40e_vsi_request_irq(vsi, int_name);
8740 
8741 	} else {
8742 		err = -EINVAL;
8743 		goto err_setup_rx;
8744 	}
8745 
8746 	err = i40e_up_complete(vsi);
8747 	if (err)
8748 		goto err_up_complete;
8749 
8750 	return 0;
8751 
8752 err_up_complete:
8753 	i40e_down(vsi);
8754 err_set_queues:
8755 	i40e_vsi_free_irq(vsi);
8756 err_setup_rx:
8757 	i40e_vsi_free_rx_resources(vsi);
8758 err_setup_tx:
8759 	i40e_vsi_free_tx_resources(vsi);
8760 	if (vsi == pf->vsi[pf->lan_vsi])
8761 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
8762 
8763 	return err;
8764 }
8765 
8766 /**
8767  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
8768  * @pf: Pointer to PF
8769  *
8770  * This function destroys the hlist where all the Flow Director
8771  * filters were saved.
8772  **/
8773 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
8774 {
8775 	struct i40e_fdir_filter *filter;
8776 	struct i40e_flex_pit *pit_entry, *tmp;
8777 	struct hlist_node *node2;
8778 
8779 	hlist_for_each_entry_safe(filter, node2,
8780 				  &pf->fdir_filter_list, fdir_node) {
8781 		hlist_del(&filter->fdir_node);
8782 		kfree(filter);
8783 	}
8784 
8785 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
8786 		list_del(&pit_entry->list);
8787 		kfree(pit_entry);
8788 	}
8789 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
8790 
8791 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
8792 		list_del(&pit_entry->list);
8793 		kfree(pit_entry);
8794 	}
8795 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
8796 
8797 	pf->fdir_pf_active_filters = 0;
8798 	i40e_reset_fdir_filter_cnt(pf);
8799 
8800 	/* Reprogram the default input set for TCP/IPv4 */
8801 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8802 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8803 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8804 
8805 	/* Reprogram the default input set for TCP/IPv6 */
8806 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
8807 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8808 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8809 
8810 	/* Reprogram the default input set for UDP/IPv4 */
8811 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
8812 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8813 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8814 
8815 	/* Reprogram the default input set for UDP/IPv6 */
8816 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
8817 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8818 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8819 
8820 	/* Reprogram the default input set for SCTP/IPv4 */
8821 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
8822 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8823 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8824 
8825 	/* Reprogram the default input set for SCTP/IPv6 */
8826 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
8827 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8828 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8829 
8830 	/* Reprogram the default input set for Other/IPv4 */
8831 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
8832 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8833 
8834 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
8835 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8836 
8837 	/* Reprogram the default input set for Other/IPv6 */
8838 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
8839 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8840 
8841 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
8842 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8843 }
8844 
8845 /**
8846  * i40e_cloud_filter_exit - Cleans up the cloud filters
8847  * @pf: Pointer to PF
8848  *
8849  * This function destroys the hlist where all the cloud filters
8850  * were saved.
8851  **/
8852 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
8853 {
8854 	struct i40e_cloud_filter *cfilter;
8855 	struct hlist_node *node;
8856 
8857 	hlist_for_each_entry_safe(cfilter, node,
8858 				  &pf->cloud_filter_list, cloud_node) {
8859 		hlist_del(&cfilter->cloud_node);
8860 		kfree(cfilter);
8861 	}
8862 	pf->num_cloud_filters = 0;
8863 
8864 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8865 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8866 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8867 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8868 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8869 	}
8870 }
8871 
8872 /**
8873  * i40e_close - Disables a network interface
8874  * @netdev: network interface device structure
8875  *
8876  * The close entry point is called when an interface is de-activated
8877  * by the OS.  The hardware is still under the driver's control, but
8878  * this netdev interface is disabled.
8879  *
8880  * Returns 0, this is not allowed to fail
8881  **/
8882 int i40e_close(struct net_device *netdev)
8883 {
8884 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8885 	struct i40e_vsi *vsi = np->vsi;
8886 
8887 	i40e_vsi_close(vsi);
8888 
8889 	return 0;
8890 }
8891 
8892 /**
8893  * i40e_do_reset - Start a PF or Core Reset sequence
8894  * @pf: board private structure
8895  * @reset_flags: which reset is requested
8896  * @lock_acquired: indicates whether or not the lock has been acquired
8897  * before this function was called.
8898  *
8899  * The essential difference in resets is that the PF Reset
8900  * doesn't clear the packet buffers, doesn't reset the PE
8901  * firmware, and doesn't bother the other PFs on the chip.
8902  **/
8903 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
8904 {
8905 	u32 val;
8906 
8907 	/* do the biggest reset indicated */
8908 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
8909 
8910 		/* Request a Global Reset
8911 		 *
8912 		 * This will start the chip's countdown to the actual full
8913 		 * chip reset event, and a warning interrupt to be sent
8914 		 * to all PFs, including the requestor.  Our handler
8915 		 * for the warning interrupt will deal with the shutdown
8916 		 * and recovery of the switch setup.
8917 		 */
8918 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
8919 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8920 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
8921 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
8922 
8923 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
8924 
8925 		/* Request a Core Reset
8926 		 *
8927 		 * Same as Global Reset, except does *not* include the MAC/PHY
8928 		 */
8929 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
8930 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8931 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
8932 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
8933 		i40e_flush(&pf->hw);
8934 
8935 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
8936 
8937 		/* Request a PF Reset
8938 		 *
8939 		 * Resets only the PF-specific registers
8940 		 *
8941 		 * This goes directly to the tear-down and rebuild of
8942 		 * the switch, since we need to do all the recovery as
8943 		 * for the Core Reset.
8944 		 */
8945 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
8946 		i40e_handle_reset_warning(pf, lock_acquired);
8947 
8948 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
8949 		/* Request a PF Reset
8950 		 *
8951 		 * Resets PF and reinitializes PFs VSI.
8952 		 */
8953 		i40e_prep_for_reset(pf);
8954 		i40e_reset_and_rebuild(pf, true, lock_acquired);
8955 		dev_info(&pf->pdev->dev,
8956 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
8957 			 "FW LLDP is disabled\n" :
8958 			 "FW LLDP is enabled\n");
8959 
8960 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
8961 		int v;
8962 
8963 		/* Find the VSI(s) that requested a re-init */
8964 		dev_info(&pf->pdev->dev,
8965 			 "VSI reinit requested\n");
8966 		for (v = 0; v < pf->num_alloc_vsi; v++) {
8967 			struct i40e_vsi *vsi = pf->vsi[v];
8968 
8969 			if (vsi != NULL &&
8970 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
8971 					       vsi->state))
8972 				i40e_vsi_reinit_locked(pf->vsi[v]);
8973 		}
8974 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
8975 		int v;
8976 
8977 		/* Find the VSI(s) that needs to be brought down */
8978 		dev_info(&pf->pdev->dev, "VSI down requested\n");
8979 		for (v = 0; v < pf->num_alloc_vsi; v++) {
8980 			struct i40e_vsi *vsi = pf->vsi[v];
8981 
8982 			if (vsi != NULL &&
8983 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
8984 					       vsi->state)) {
8985 				set_bit(__I40E_VSI_DOWN, vsi->state);
8986 				i40e_down(vsi);
8987 			}
8988 		}
8989 	} else {
8990 		dev_info(&pf->pdev->dev,
8991 			 "bad reset request 0x%08x\n", reset_flags);
8992 	}
8993 }
8994 
8995 #ifdef CONFIG_I40E_DCB
8996 /**
8997  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
8998  * @pf: board private structure
8999  * @old_cfg: current DCB config
9000  * @new_cfg: new DCB config
9001  **/
9002 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9003 			    struct i40e_dcbx_config *old_cfg,
9004 			    struct i40e_dcbx_config *new_cfg)
9005 {
9006 	bool need_reconfig = false;
9007 
9008 	/* Check if ETS configuration has changed */
9009 	if (memcmp(&new_cfg->etscfg,
9010 		   &old_cfg->etscfg,
9011 		   sizeof(new_cfg->etscfg))) {
9012 		/* If Priority Table has changed reconfig is needed */
9013 		if (memcmp(&new_cfg->etscfg.prioritytable,
9014 			   &old_cfg->etscfg.prioritytable,
9015 			   sizeof(new_cfg->etscfg.prioritytable))) {
9016 			need_reconfig = true;
9017 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9018 		}
9019 
9020 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9021 			   &old_cfg->etscfg.tcbwtable,
9022 			   sizeof(new_cfg->etscfg.tcbwtable)))
9023 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9024 
9025 		if (memcmp(&new_cfg->etscfg.tsatable,
9026 			   &old_cfg->etscfg.tsatable,
9027 			   sizeof(new_cfg->etscfg.tsatable)))
9028 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9029 	}
9030 
9031 	/* Check if PFC configuration has changed */
9032 	if (memcmp(&new_cfg->pfc,
9033 		   &old_cfg->pfc,
9034 		   sizeof(new_cfg->pfc))) {
9035 		need_reconfig = true;
9036 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9037 	}
9038 
9039 	/* Check if APP Table has changed */
9040 	if (memcmp(&new_cfg->app,
9041 		   &old_cfg->app,
9042 		   sizeof(new_cfg->app))) {
9043 		need_reconfig = true;
9044 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9045 	}
9046 
9047 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9048 	return need_reconfig;
9049 }
9050 
9051 /**
9052  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9053  * @pf: board private structure
9054  * @e: event info posted on ARQ
9055  **/
9056 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9057 				  struct i40e_arq_event_info *e)
9058 {
9059 	struct i40e_aqc_lldp_get_mib *mib =
9060 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9061 	struct i40e_hw *hw = &pf->hw;
9062 	struct i40e_dcbx_config tmp_dcbx_cfg;
9063 	bool need_reconfig = false;
9064 	int ret = 0;
9065 	u8 type;
9066 
9067 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9068 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9069 	    (hw->phy.link_info.link_speed &
9070 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9071 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9072 		/* let firmware decide if the DCB should be disabled */
9073 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9074 
9075 	/* Not DCB capable or capability disabled */
9076 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9077 		return ret;
9078 
9079 	/* Ignore if event is not for Nearest Bridge */
9080 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9081 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9082 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9083 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9084 		return ret;
9085 
9086 	/* Check MIB Type and return if event for Remote MIB update */
9087 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9088 	dev_dbg(&pf->pdev->dev,
9089 		"LLDP event mib type %s\n", type ? "remote" : "local");
9090 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9091 		/* Update the remote cached instance and return */
9092 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9093 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9094 				&hw->remote_dcbx_config);
9095 		goto exit;
9096 	}
9097 
9098 	/* Store the old configuration */
9099 	tmp_dcbx_cfg = hw->local_dcbx_config;
9100 
9101 	/* Reset the old DCBx configuration data */
9102 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9103 	/* Get updated DCBX data from firmware */
9104 	ret = i40e_get_dcb_config(&pf->hw);
9105 	if (ret) {
9106 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9107 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9108 		    (hw->phy.link_info.link_speed &
9109 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9110 			dev_warn(&pf->pdev->dev,
9111 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9112 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9113 		} else {
9114 			dev_info(&pf->pdev->dev,
9115 				 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
9116 				 i40e_stat_str(&pf->hw, ret),
9117 				 i40e_aq_str(&pf->hw,
9118 					     pf->hw.aq.asq_last_status));
9119 		}
9120 		goto exit;
9121 	}
9122 
9123 	/* No change detected in DCBX configs */
9124 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9125 		    sizeof(tmp_dcbx_cfg))) {
9126 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9127 		goto exit;
9128 	}
9129 
9130 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9131 					       &hw->local_dcbx_config);
9132 
9133 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9134 
9135 	if (!need_reconfig)
9136 		goto exit;
9137 
9138 	/* Enable DCB tagging only when more than one TC */
9139 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9140 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9141 	else
9142 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9143 
9144 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9145 	/* Reconfiguration needed quiesce all VSIs */
9146 	i40e_pf_quiesce_all_vsi(pf);
9147 
9148 	/* Changes in configuration update VEB/VSI */
9149 	i40e_dcb_reconfigure(pf);
9150 
9151 	ret = i40e_resume_port_tx(pf);
9152 
9153 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9154 	/* In case of error no point in resuming VSIs */
9155 	if (ret)
9156 		goto exit;
9157 
9158 	/* Wait for the PF's queues to be disabled */
9159 	ret = i40e_pf_wait_queues_disabled(pf);
9160 	if (ret) {
9161 		/* Schedule PF reset to recover */
9162 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9163 		i40e_service_event_schedule(pf);
9164 	} else {
9165 		i40e_pf_unquiesce_all_vsi(pf);
9166 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9167 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9168 	}
9169 
9170 exit:
9171 	return ret;
9172 }
9173 #endif /* CONFIG_I40E_DCB */
9174 
9175 /**
9176  * i40e_do_reset_safe - Protected reset path for userland calls.
9177  * @pf: board private structure
9178  * @reset_flags: which reset is requested
9179  *
9180  **/
9181 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9182 {
9183 	rtnl_lock();
9184 	i40e_do_reset(pf, reset_flags, true);
9185 	rtnl_unlock();
9186 }
9187 
9188 /**
9189  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9190  * @pf: board private structure
9191  * @e: event info posted on ARQ
9192  *
9193  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9194  * and VF queues
9195  **/
9196 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9197 					   struct i40e_arq_event_info *e)
9198 {
9199 	struct i40e_aqc_lan_overflow *data =
9200 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9201 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9202 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9203 	struct i40e_hw *hw = &pf->hw;
9204 	struct i40e_vf *vf;
9205 	u16 vf_id;
9206 
9207 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9208 		queue, qtx_ctl);
9209 
9210 	/* Queue belongs to VF, find the VF and issue VF reset */
9211 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9212 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9213 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9214 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9215 		vf_id -= hw->func_caps.vf_base_id;
9216 		vf = &pf->vf[vf_id];
9217 		i40e_vc_notify_vf_reset(vf);
9218 		/* Allow VF to process pending reset notification */
9219 		msleep(20);
9220 		i40e_reset_vf(vf, false);
9221 	}
9222 }
9223 
9224 /**
9225  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9226  * @pf: board private structure
9227  **/
9228 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9229 {
9230 	u32 val, fcnt_prog;
9231 
9232 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9233 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9234 	return fcnt_prog;
9235 }
9236 
9237 /**
9238  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9239  * @pf: board private structure
9240  **/
9241 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9242 {
9243 	u32 val, fcnt_prog;
9244 
9245 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9246 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9247 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9248 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9249 	return fcnt_prog;
9250 }
9251 
9252 /**
9253  * i40e_get_global_fd_count - Get total FD filters programmed on device
9254  * @pf: board private structure
9255  **/
9256 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9257 {
9258 	u32 val, fcnt_prog;
9259 
9260 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9261 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9262 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9263 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9264 	return fcnt_prog;
9265 }
9266 
9267 /**
9268  * i40e_reenable_fdir_sb - Restore FDir SB capability
9269  * @pf: board private structure
9270  **/
9271 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9272 {
9273 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9274 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9275 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9276 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9277 }
9278 
9279 /**
9280  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9281  * @pf: board private structure
9282  **/
9283 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9284 {
9285 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9286 		/* ATR uses the same filtering logic as SB rules. It only
9287 		 * functions properly if the input set mask is at the default
9288 		 * settings. It is safe to restore the default input set
9289 		 * because there are no active TCPv4 filter rules.
9290 		 */
9291 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9292 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9293 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9294 
9295 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9296 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9297 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9298 	}
9299 }
9300 
9301 /**
9302  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9303  * @pf: board private structure
9304  * @filter: FDir filter to remove
9305  */
9306 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9307 				       struct i40e_fdir_filter *filter)
9308 {
9309 	/* Update counters */
9310 	pf->fdir_pf_active_filters--;
9311 	pf->fd_inv = 0;
9312 
9313 	switch (filter->flow_type) {
9314 	case TCP_V4_FLOW:
9315 		pf->fd_tcp4_filter_cnt--;
9316 		break;
9317 	case UDP_V4_FLOW:
9318 		pf->fd_udp4_filter_cnt--;
9319 		break;
9320 	case SCTP_V4_FLOW:
9321 		pf->fd_sctp4_filter_cnt--;
9322 		break;
9323 	case TCP_V6_FLOW:
9324 		pf->fd_tcp6_filter_cnt--;
9325 		break;
9326 	case UDP_V6_FLOW:
9327 		pf->fd_udp6_filter_cnt--;
9328 		break;
9329 	case SCTP_V6_FLOW:
9330 		pf->fd_udp6_filter_cnt--;
9331 		break;
9332 	case IP_USER_FLOW:
9333 		switch (filter->ipl4_proto) {
9334 		case IPPROTO_TCP:
9335 			pf->fd_tcp4_filter_cnt--;
9336 			break;
9337 		case IPPROTO_UDP:
9338 			pf->fd_udp4_filter_cnt--;
9339 			break;
9340 		case IPPROTO_SCTP:
9341 			pf->fd_sctp4_filter_cnt--;
9342 			break;
9343 		case IPPROTO_IP:
9344 			pf->fd_ip4_filter_cnt--;
9345 			break;
9346 		}
9347 		break;
9348 	case IPV6_USER_FLOW:
9349 		switch (filter->ipl4_proto) {
9350 		case IPPROTO_TCP:
9351 			pf->fd_tcp6_filter_cnt--;
9352 			break;
9353 		case IPPROTO_UDP:
9354 			pf->fd_udp6_filter_cnt--;
9355 			break;
9356 		case IPPROTO_SCTP:
9357 			pf->fd_sctp6_filter_cnt--;
9358 			break;
9359 		case IPPROTO_IP:
9360 			pf->fd_ip6_filter_cnt--;
9361 			break;
9362 		}
9363 		break;
9364 	}
9365 
9366 	/* Remove the filter from the list and free memory */
9367 	hlist_del(&filter->fdir_node);
9368 	kfree(filter);
9369 }
9370 
9371 /**
9372  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9373  * @pf: board private structure
9374  **/
9375 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9376 {
9377 	struct i40e_fdir_filter *filter;
9378 	u32 fcnt_prog, fcnt_avail;
9379 	struct hlist_node *node;
9380 
9381 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9382 		return;
9383 
9384 	/* Check if we have enough room to re-enable FDir SB capability. */
9385 	fcnt_prog = i40e_get_global_fd_count(pf);
9386 	fcnt_avail = pf->fdir_pf_filter_count;
9387 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9388 	    (pf->fd_add_err == 0) ||
9389 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9390 		i40e_reenable_fdir_sb(pf);
9391 
9392 	/* We should wait for even more space before re-enabling ATR.
9393 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9394 	 * rules active.
9395 	 */
9396 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9397 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9398 		i40e_reenable_fdir_atr(pf);
9399 
9400 	/* if hw had a problem adding a filter, delete it */
9401 	if (pf->fd_inv > 0) {
9402 		hlist_for_each_entry_safe(filter, node,
9403 					  &pf->fdir_filter_list, fdir_node)
9404 			if (filter->fd_id == pf->fd_inv)
9405 				i40e_delete_invalid_filter(pf, filter);
9406 	}
9407 }
9408 
9409 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9410 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9411 /**
9412  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9413  * @pf: board private structure
9414  **/
9415 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9416 {
9417 	unsigned long min_flush_time;
9418 	int flush_wait_retry = 50;
9419 	bool disable_atr = false;
9420 	int fd_room;
9421 	int reg;
9422 
9423 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9424 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9425 		return;
9426 
9427 	/* If the flush is happening too quick and we have mostly SB rules we
9428 	 * should not re-enable ATR for some time.
9429 	 */
9430 	min_flush_time = pf->fd_flush_timestamp +
9431 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9432 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9433 
9434 	if (!(time_after(jiffies, min_flush_time)) &&
9435 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9436 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9437 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9438 		disable_atr = true;
9439 	}
9440 
9441 	pf->fd_flush_timestamp = jiffies;
9442 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9443 	/* flush all filters */
9444 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9445 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9446 	i40e_flush(&pf->hw);
9447 	pf->fd_flush_cnt++;
9448 	pf->fd_add_err = 0;
9449 	do {
9450 		/* Check FD flush status every 5-6msec */
9451 		usleep_range(5000, 6000);
9452 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9453 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9454 			break;
9455 	} while (flush_wait_retry--);
9456 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9457 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9458 	} else {
9459 		/* replay sideband filters */
9460 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9461 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9462 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9463 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9464 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9465 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9466 	}
9467 }
9468 
9469 /**
9470  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9471  * @pf: board private structure
9472  **/
9473 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9474 {
9475 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9476 }
9477 
9478 /**
9479  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9480  * @pf: board private structure
9481  **/
9482 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9483 {
9484 
9485 	/* if interface is down do nothing */
9486 	if (test_bit(__I40E_DOWN, pf->state))
9487 		return;
9488 
9489 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9490 		i40e_fdir_flush_and_replay(pf);
9491 
9492 	i40e_fdir_check_and_reenable(pf);
9493 
9494 }
9495 
9496 /**
9497  * i40e_vsi_link_event - notify VSI of a link event
9498  * @vsi: vsi to be notified
9499  * @link_up: link up or down
9500  **/
9501 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9502 {
9503 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9504 		return;
9505 
9506 	switch (vsi->type) {
9507 	case I40E_VSI_MAIN:
9508 		if (!vsi->netdev || !vsi->netdev_registered)
9509 			break;
9510 
9511 		if (link_up) {
9512 			netif_carrier_on(vsi->netdev);
9513 			netif_tx_wake_all_queues(vsi->netdev);
9514 		} else {
9515 			netif_carrier_off(vsi->netdev);
9516 			netif_tx_stop_all_queues(vsi->netdev);
9517 		}
9518 		break;
9519 
9520 	case I40E_VSI_SRIOV:
9521 	case I40E_VSI_VMDQ2:
9522 	case I40E_VSI_CTRL:
9523 	case I40E_VSI_IWARP:
9524 	case I40E_VSI_MIRROR:
9525 	default:
9526 		/* there is no notification for other VSIs */
9527 		break;
9528 	}
9529 }
9530 
9531 /**
9532  * i40e_veb_link_event - notify elements on the veb of a link event
9533  * @veb: veb to be notified
9534  * @link_up: link up or down
9535  **/
9536 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9537 {
9538 	struct i40e_pf *pf;
9539 	int i;
9540 
9541 	if (!veb || !veb->pf)
9542 		return;
9543 	pf = veb->pf;
9544 
9545 	/* depth first... */
9546 	for (i = 0; i < I40E_MAX_VEB; i++)
9547 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9548 			i40e_veb_link_event(pf->veb[i], link_up);
9549 
9550 	/* ... now the local VSIs */
9551 	for (i = 0; i < pf->num_alloc_vsi; i++)
9552 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9553 			i40e_vsi_link_event(pf->vsi[i], link_up);
9554 }
9555 
9556 /**
9557  * i40e_link_event - Update netif_carrier status
9558  * @pf: board private structure
9559  **/
9560 static void i40e_link_event(struct i40e_pf *pf)
9561 {
9562 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9563 	u8 new_link_speed, old_link_speed;
9564 	i40e_status status;
9565 	bool new_link, old_link;
9566 #ifdef CONFIG_I40E_DCB
9567 	int err;
9568 #endif /* CONFIG_I40E_DCB */
9569 
9570 	/* set this to force the get_link_status call to refresh state */
9571 	pf->hw.phy.get_link_info = true;
9572 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9573 	status = i40e_get_link_status(&pf->hw, &new_link);
9574 
9575 	/* On success, disable temp link polling */
9576 	if (status == I40E_SUCCESS) {
9577 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9578 	} else {
9579 		/* Enable link polling temporarily until i40e_get_link_status
9580 		 * returns I40E_SUCCESS
9581 		 */
9582 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9583 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9584 			status);
9585 		return;
9586 	}
9587 
9588 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9589 	new_link_speed = pf->hw.phy.link_info.link_speed;
9590 
9591 	if (new_link == old_link &&
9592 	    new_link_speed == old_link_speed &&
9593 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9594 	     new_link == netif_carrier_ok(vsi->netdev)))
9595 		return;
9596 
9597 	i40e_print_link_message(vsi, new_link);
9598 
9599 	/* Notify the base of the switch tree connected to
9600 	 * the link.  Floating VEBs are not notified.
9601 	 */
9602 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9603 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9604 	else
9605 		i40e_vsi_link_event(vsi, new_link);
9606 
9607 	if (pf->vf)
9608 		i40e_vc_notify_link_state(pf);
9609 
9610 	if (pf->flags & I40E_FLAG_PTP)
9611 		i40e_ptp_set_increment(pf);
9612 #ifdef CONFIG_I40E_DCB
9613 	if (new_link == old_link)
9614 		return;
9615 	/* Not SW DCB so firmware will take care of default settings */
9616 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9617 		return;
9618 
9619 	/* We cover here only link down, as after link up in case of SW DCB
9620 	 * SW LLDP agent will take care of setting it up
9621 	 */
9622 	if (!new_link) {
9623 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9624 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9625 		err = i40e_dcb_sw_default_config(pf);
9626 		if (err) {
9627 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9628 				       I40E_FLAG_DCB_ENABLED);
9629 		} else {
9630 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9631 				       DCB_CAP_DCBX_VER_IEEE;
9632 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9633 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9634 		}
9635 	}
9636 #endif /* CONFIG_I40E_DCB */
9637 }
9638 
9639 /**
9640  * i40e_watchdog_subtask - periodic checks not using event driven response
9641  * @pf: board private structure
9642  **/
9643 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9644 {
9645 	int i;
9646 
9647 	/* if interface is down do nothing */
9648 	if (test_bit(__I40E_DOWN, pf->state) ||
9649 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9650 		return;
9651 
9652 	/* make sure we don't do these things too often */
9653 	if (time_before(jiffies, (pf->service_timer_previous +
9654 				  pf->service_timer_period)))
9655 		return;
9656 	pf->service_timer_previous = jiffies;
9657 
9658 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9659 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9660 		i40e_link_event(pf);
9661 
9662 	/* Update the stats for active netdevs so the network stack
9663 	 * can look at updated numbers whenever it cares to
9664 	 */
9665 	for (i = 0; i < pf->num_alloc_vsi; i++)
9666 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9667 			i40e_update_stats(pf->vsi[i]);
9668 
9669 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9670 		/* Update the stats for the active switching components */
9671 		for (i = 0; i < I40E_MAX_VEB; i++)
9672 			if (pf->veb[i])
9673 				i40e_update_veb_stats(pf->veb[i]);
9674 	}
9675 
9676 	i40e_ptp_rx_hang(pf);
9677 	i40e_ptp_tx_hang(pf);
9678 }
9679 
9680 /**
9681  * i40e_reset_subtask - Set up for resetting the device and driver
9682  * @pf: board private structure
9683  **/
9684 static void i40e_reset_subtask(struct i40e_pf *pf)
9685 {
9686 	u32 reset_flags = 0;
9687 
9688 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
9689 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
9690 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
9691 	}
9692 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
9693 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
9694 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9695 	}
9696 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
9697 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
9698 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
9699 	}
9700 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
9701 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
9702 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
9703 	}
9704 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
9705 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
9706 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
9707 	}
9708 
9709 	/* If there's a recovery already waiting, it takes
9710 	 * precedence before starting a new reset sequence.
9711 	 */
9712 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
9713 		i40e_prep_for_reset(pf);
9714 		i40e_reset(pf);
9715 		i40e_rebuild(pf, false, false);
9716 	}
9717 
9718 	/* If we're already down or resetting, just bail */
9719 	if (reset_flags &&
9720 	    !test_bit(__I40E_DOWN, pf->state) &&
9721 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
9722 		i40e_do_reset(pf, reset_flags, false);
9723 	}
9724 }
9725 
9726 /**
9727  * i40e_handle_link_event - Handle link event
9728  * @pf: board private structure
9729  * @e: event info posted on ARQ
9730  **/
9731 static void i40e_handle_link_event(struct i40e_pf *pf,
9732 				   struct i40e_arq_event_info *e)
9733 {
9734 	struct i40e_aqc_get_link_status *status =
9735 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
9736 
9737 	/* Do a new status request to re-enable LSE reporting
9738 	 * and load new status information into the hw struct
9739 	 * This completely ignores any state information
9740 	 * in the ARQ event info, instead choosing to always
9741 	 * issue the AQ update link status command.
9742 	 */
9743 	i40e_link_event(pf);
9744 
9745 	/* Check if module meets thermal requirements */
9746 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
9747 		dev_err(&pf->pdev->dev,
9748 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
9749 		dev_err(&pf->pdev->dev,
9750 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9751 	} else {
9752 		/* check for unqualified module, if link is down, suppress
9753 		 * the message if link was forced to be down.
9754 		 */
9755 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
9756 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
9757 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
9758 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
9759 			dev_err(&pf->pdev->dev,
9760 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
9761 			dev_err(&pf->pdev->dev,
9762 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9763 		}
9764 	}
9765 }
9766 
9767 /**
9768  * i40e_clean_adminq_subtask - Clean the AdminQ rings
9769  * @pf: board private structure
9770  **/
9771 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
9772 {
9773 	struct i40e_arq_event_info event;
9774 	struct i40e_hw *hw = &pf->hw;
9775 	u16 pending, i = 0;
9776 	i40e_status ret;
9777 	u16 opcode;
9778 	u32 oldval;
9779 	u32 val;
9780 
9781 	/* Do not run clean AQ when PF reset fails */
9782 	if (test_bit(__I40E_RESET_FAILED, pf->state))
9783 		return;
9784 
9785 	/* check for error indications */
9786 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
9787 	oldval = val;
9788 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
9789 		if (hw->debug_mask & I40E_DEBUG_AQ)
9790 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
9791 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
9792 	}
9793 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
9794 		if (hw->debug_mask & I40E_DEBUG_AQ)
9795 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
9796 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
9797 		pf->arq_overflows++;
9798 	}
9799 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
9800 		if (hw->debug_mask & I40E_DEBUG_AQ)
9801 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
9802 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
9803 	}
9804 	if (oldval != val)
9805 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
9806 
9807 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
9808 	oldval = val;
9809 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
9810 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9811 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
9812 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
9813 	}
9814 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
9815 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9816 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
9817 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
9818 	}
9819 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
9820 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9821 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
9822 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
9823 	}
9824 	if (oldval != val)
9825 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
9826 
9827 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
9828 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
9829 	if (!event.msg_buf)
9830 		return;
9831 
9832 	do {
9833 		ret = i40e_clean_arq_element(hw, &event, &pending);
9834 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
9835 			break;
9836 		else if (ret) {
9837 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
9838 			break;
9839 		}
9840 
9841 		opcode = le16_to_cpu(event.desc.opcode);
9842 		switch (opcode) {
9843 
9844 		case i40e_aqc_opc_get_link_status:
9845 			rtnl_lock();
9846 			i40e_handle_link_event(pf, &event);
9847 			rtnl_unlock();
9848 			break;
9849 		case i40e_aqc_opc_send_msg_to_pf:
9850 			ret = i40e_vc_process_vf_msg(pf,
9851 					le16_to_cpu(event.desc.retval),
9852 					le32_to_cpu(event.desc.cookie_high),
9853 					le32_to_cpu(event.desc.cookie_low),
9854 					event.msg_buf,
9855 					event.msg_len);
9856 			break;
9857 		case i40e_aqc_opc_lldp_update_mib:
9858 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
9859 #ifdef CONFIG_I40E_DCB
9860 			rtnl_lock();
9861 			i40e_handle_lldp_event(pf, &event);
9862 			rtnl_unlock();
9863 #endif /* CONFIG_I40E_DCB */
9864 			break;
9865 		case i40e_aqc_opc_event_lan_overflow:
9866 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
9867 			i40e_handle_lan_overflow_event(pf, &event);
9868 			break;
9869 		case i40e_aqc_opc_send_msg_to_peer:
9870 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
9871 			break;
9872 		case i40e_aqc_opc_nvm_erase:
9873 		case i40e_aqc_opc_nvm_update:
9874 		case i40e_aqc_opc_oem_post_update:
9875 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
9876 				   "ARQ NVM operation 0x%04x completed\n",
9877 				   opcode);
9878 			break;
9879 		default:
9880 			dev_info(&pf->pdev->dev,
9881 				 "ARQ: Unknown event 0x%04x ignored\n",
9882 				 opcode);
9883 			break;
9884 		}
9885 	} while (i++ < pf->adminq_work_limit);
9886 
9887 	if (i < pf->adminq_work_limit)
9888 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
9889 
9890 	/* re-enable Admin queue interrupt cause */
9891 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
9892 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
9893 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
9894 	i40e_flush(hw);
9895 
9896 	kfree(event.msg_buf);
9897 }
9898 
9899 /**
9900  * i40e_verify_eeprom - make sure eeprom is good to use
9901  * @pf: board private structure
9902  **/
9903 static void i40e_verify_eeprom(struct i40e_pf *pf)
9904 {
9905 	int err;
9906 
9907 	err = i40e_diag_eeprom_test(&pf->hw);
9908 	if (err) {
9909 		/* retry in case of garbage read */
9910 		err = i40e_diag_eeprom_test(&pf->hw);
9911 		if (err) {
9912 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
9913 				 err);
9914 			set_bit(__I40E_BAD_EEPROM, pf->state);
9915 		}
9916 	}
9917 
9918 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
9919 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
9920 		clear_bit(__I40E_BAD_EEPROM, pf->state);
9921 	}
9922 }
9923 
9924 /**
9925  * i40e_enable_pf_switch_lb
9926  * @pf: pointer to the PF structure
9927  *
9928  * enable switch loop back or die - no point in a return value
9929  **/
9930 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
9931 {
9932 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9933 	struct i40e_vsi_context ctxt;
9934 	int ret;
9935 
9936 	ctxt.seid = pf->main_vsi_seid;
9937 	ctxt.pf_num = pf->hw.pf_id;
9938 	ctxt.vf_num = 0;
9939 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9940 	if (ret) {
9941 		dev_info(&pf->pdev->dev,
9942 			 "couldn't get PF vsi config, err %s aq_err %s\n",
9943 			 i40e_stat_str(&pf->hw, ret),
9944 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9945 		return;
9946 	}
9947 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9948 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9949 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9950 
9951 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
9952 	if (ret) {
9953 		dev_info(&pf->pdev->dev,
9954 			 "update vsi switch failed, err %s aq_err %s\n",
9955 			 i40e_stat_str(&pf->hw, ret),
9956 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9957 	}
9958 }
9959 
9960 /**
9961  * i40e_disable_pf_switch_lb
9962  * @pf: pointer to the PF structure
9963  *
9964  * disable switch loop back or die - no point in a return value
9965  **/
9966 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
9967 {
9968 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9969 	struct i40e_vsi_context ctxt;
9970 	int ret;
9971 
9972 	ctxt.seid = pf->main_vsi_seid;
9973 	ctxt.pf_num = pf->hw.pf_id;
9974 	ctxt.vf_num = 0;
9975 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9976 	if (ret) {
9977 		dev_info(&pf->pdev->dev,
9978 			 "couldn't get PF vsi config, err %s aq_err %s\n",
9979 			 i40e_stat_str(&pf->hw, ret),
9980 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9981 		return;
9982 	}
9983 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9984 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9985 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9986 
9987 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
9988 	if (ret) {
9989 		dev_info(&pf->pdev->dev,
9990 			 "update vsi switch failed, err %s aq_err %s\n",
9991 			 i40e_stat_str(&pf->hw, ret),
9992 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9993 	}
9994 }
9995 
9996 /**
9997  * i40e_config_bridge_mode - Configure the HW bridge mode
9998  * @veb: pointer to the bridge instance
9999  *
10000  * Configure the loop back mode for the LAN VSI that is downlink to the
10001  * specified HW bridge instance. It is expected this function is called
10002  * when a new HW bridge is instantiated.
10003  **/
10004 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10005 {
10006 	struct i40e_pf *pf = veb->pf;
10007 
10008 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10009 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10010 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10011 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10012 		i40e_disable_pf_switch_lb(pf);
10013 	else
10014 		i40e_enable_pf_switch_lb(pf);
10015 }
10016 
10017 /**
10018  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10019  * @veb: pointer to the VEB instance
10020  *
10021  * This is a recursive function that first builds the attached VSIs then
10022  * recurses in to build the next layer of VEB.  We track the connections
10023  * through our own index numbers because the seid's from the HW could
10024  * change across the reset.
10025  **/
10026 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10027 {
10028 	struct i40e_vsi *ctl_vsi = NULL;
10029 	struct i40e_pf *pf = veb->pf;
10030 	int v, veb_idx;
10031 	int ret;
10032 
10033 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10034 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10035 		if (pf->vsi[v] &&
10036 		    pf->vsi[v]->veb_idx == veb->idx &&
10037 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10038 			ctl_vsi = pf->vsi[v];
10039 			break;
10040 		}
10041 	}
10042 	if (!ctl_vsi) {
10043 		dev_info(&pf->pdev->dev,
10044 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10045 		ret = -ENOENT;
10046 		goto end_reconstitute;
10047 	}
10048 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10049 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10050 	ret = i40e_add_vsi(ctl_vsi);
10051 	if (ret) {
10052 		dev_info(&pf->pdev->dev,
10053 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10054 			 veb->idx, ret);
10055 		goto end_reconstitute;
10056 	}
10057 	i40e_vsi_reset_stats(ctl_vsi);
10058 
10059 	/* create the VEB in the switch and move the VSI onto the VEB */
10060 	ret = i40e_add_veb(veb, ctl_vsi);
10061 	if (ret)
10062 		goto end_reconstitute;
10063 
10064 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10065 		veb->bridge_mode = BRIDGE_MODE_VEB;
10066 	else
10067 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10068 	i40e_config_bridge_mode(veb);
10069 
10070 	/* create the remaining VSIs attached to this VEB */
10071 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10072 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10073 			continue;
10074 
10075 		if (pf->vsi[v]->veb_idx == veb->idx) {
10076 			struct i40e_vsi *vsi = pf->vsi[v];
10077 
10078 			vsi->uplink_seid = veb->seid;
10079 			ret = i40e_add_vsi(vsi);
10080 			if (ret) {
10081 				dev_info(&pf->pdev->dev,
10082 					 "rebuild of vsi_idx %d failed: %d\n",
10083 					 v, ret);
10084 				goto end_reconstitute;
10085 			}
10086 			i40e_vsi_reset_stats(vsi);
10087 		}
10088 	}
10089 
10090 	/* create any VEBs attached to this VEB - RECURSION */
10091 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10092 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10093 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10094 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10095 			if (ret)
10096 				break;
10097 		}
10098 	}
10099 
10100 end_reconstitute:
10101 	return ret;
10102 }
10103 
10104 /**
10105  * i40e_get_capabilities - get info about the HW
10106  * @pf: the PF struct
10107  * @list_type: AQ capability to be queried
10108  **/
10109 static int i40e_get_capabilities(struct i40e_pf *pf,
10110 				 enum i40e_admin_queue_opc list_type)
10111 {
10112 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10113 	u16 data_size;
10114 	int buf_len;
10115 	int err;
10116 
10117 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10118 	do {
10119 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10120 		if (!cap_buf)
10121 			return -ENOMEM;
10122 
10123 		/* this loads the data into the hw struct for us */
10124 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10125 						    &data_size, list_type,
10126 						    NULL);
10127 		/* data loaded, buffer no longer needed */
10128 		kfree(cap_buf);
10129 
10130 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10131 			/* retry with a larger buffer */
10132 			buf_len = data_size;
10133 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
10134 			dev_info(&pf->pdev->dev,
10135 				 "capability discovery failed, err %s aq_err %s\n",
10136 				 i40e_stat_str(&pf->hw, err),
10137 				 i40e_aq_str(&pf->hw,
10138 					     pf->hw.aq.asq_last_status));
10139 			return -ENODEV;
10140 		}
10141 	} while (err);
10142 
10143 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10144 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10145 			dev_info(&pf->pdev->dev,
10146 				 "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",
10147 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10148 				 pf->hw.func_caps.num_msix_vectors,
10149 				 pf->hw.func_caps.num_msix_vectors_vf,
10150 				 pf->hw.func_caps.fd_filters_guaranteed,
10151 				 pf->hw.func_caps.fd_filters_best_effort,
10152 				 pf->hw.func_caps.num_tx_qp,
10153 				 pf->hw.func_caps.num_vsis);
10154 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10155 			dev_info(&pf->pdev->dev,
10156 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10157 				 pf->hw.dev_caps.switch_mode,
10158 				 pf->hw.dev_caps.valid_functions);
10159 			dev_info(&pf->pdev->dev,
10160 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10161 				 pf->hw.dev_caps.sr_iov_1_1,
10162 				 pf->hw.dev_caps.num_vfs);
10163 			dev_info(&pf->pdev->dev,
10164 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10165 				 pf->hw.dev_caps.num_vsis,
10166 				 pf->hw.dev_caps.num_rx_qp,
10167 				 pf->hw.dev_caps.num_tx_qp);
10168 		}
10169 	}
10170 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10171 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10172 		       + pf->hw.func_caps.num_vfs)
10173 		if (pf->hw.revision_id == 0 &&
10174 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10175 			dev_info(&pf->pdev->dev,
10176 				 "got num_vsis %d, setting num_vsis to %d\n",
10177 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10178 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10179 		}
10180 	}
10181 	return 0;
10182 }
10183 
10184 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10185 
10186 /**
10187  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10188  * @pf: board private structure
10189  **/
10190 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10191 {
10192 	struct i40e_vsi *vsi;
10193 
10194 	/* quick workaround for an NVM issue that leaves a critical register
10195 	 * uninitialized
10196 	 */
10197 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10198 		static const u32 hkey[] = {
10199 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10200 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10201 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10202 			0x95b3a76d};
10203 		int i;
10204 
10205 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10206 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10207 	}
10208 
10209 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10210 		return;
10211 
10212 	/* find existing VSI and see if it needs configuring */
10213 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10214 
10215 	/* create a new VSI if none exists */
10216 	if (!vsi) {
10217 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10218 				     pf->vsi[pf->lan_vsi]->seid, 0);
10219 		if (!vsi) {
10220 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10221 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10222 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10223 			return;
10224 		}
10225 	}
10226 
10227 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10228 }
10229 
10230 /**
10231  * i40e_fdir_teardown - release the Flow Director resources
10232  * @pf: board private structure
10233  **/
10234 static void i40e_fdir_teardown(struct i40e_pf *pf)
10235 {
10236 	struct i40e_vsi *vsi;
10237 
10238 	i40e_fdir_filter_exit(pf);
10239 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10240 	if (vsi)
10241 		i40e_vsi_release(vsi);
10242 }
10243 
10244 /**
10245  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10246  * @vsi: PF main vsi
10247  * @seid: seid of main or channel VSIs
10248  *
10249  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10250  * existed before reset
10251  **/
10252 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10253 {
10254 	struct i40e_cloud_filter *cfilter;
10255 	struct i40e_pf *pf = vsi->back;
10256 	struct hlist_node *node;
10257 	i40e_status ret;
10258 
10259 	/* Add cloud filters back if they exist */
10260 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10261 				  cloud_node) {
10262 		if (cfilter->seid != seid)
10263 			continue;
10264 
10265 		if (cfilter->dst_port)
10266 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10267 								true);
10268 		else
10269 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10270 
10271 		if (ret) {
10272 			dev_dbg(&pf->pdev->dev,
10273 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
10274 				i40e_stat_str(&pf->hw, ret),
10275 				i40e_aq_str(&pf->hw,
10276 					    pf->hw.aq.asq_last_status));
10277 			return ret;
10278 		}
10279 	}
10280 	return 0;
10281 }
10282 
10283 /**
10284  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10285  * @vsi: PF main vsi
10286  *
10287  * Rebuilds channel VSIs if they existed before reset
10288  **/
10289 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10290 {
10291 	struct i40e_channel *ch, *ch_tmp;
10292 	i40e_status ret;
10293 
10294 	if (list_empty(&vsi->ch_list))
10295 		return 0;
10296 
10297 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10298 		if (!ch->initialized)
10299 			break;
10300 		/* Proceed with creation of channel (VMDq2) VSI */
10301 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10302 		if (ret) {
10303 			dev_info(&vsi->back->pdev->dev,
10304 				 "failed to rebuild channels using uplink_seid %u\n",
10305 				 vsi->uplink_seid);
10306 			return ret;
10307 		}
10308 		/* Reconfigure TX queues using QTX_CTL register */
10309 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10310 		if (ret) {
10311 			dev_info(&vsi->back->pdev->dev,
10312 				 "failed to configure TX rings for channel %u\n",
10313 				 ch->seid);
10314 			return ret;
10315 		}
10316 		/* update 'next_base_queue' */
10317 		vsi->next_base_queue = vsi->next_base_queue +
10318 							ch->num_queue_pairs;
10319 		if (ch->max_tx_rate) {
10320 			u64 credits = ch->max_tx_rate;
10321 
10322 			if (i40e_set_bw_limit(vsi, ch->seid,
10323 					      ch->max_tx_rate))
10324 				return -EINVAL;
10325 
10326 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10327 			dev_dbg(&vsi->back->pdev->dev,
10328 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10329 				ch->max_tx_rate,
10330 				credits,
10331 				ch->seid);
10332 		}
10333 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10334 		if (ret) {
10335 			dev_dbg(&vsi->back->pdev->dev,
10336 				"Failed to rebuild cloud filters for channel VSI %u\n",
10337 				ch->seid);
10338 			return ret;
10339 		}
10340 	}
10341 	return 0;
10342 }
10343 
10344 /**
10345  * i40e_prep_for_reset - prep for the core to reset
10346  * @pf: board private structure
10347  *
10348  * Close up the VFs and other things in prep for PF Reset.
10349   **/
10350 static void i40e_prep_for_reset(struct i40e_pf *pf)
10351 {
10352 	struct i40e_hw *hw = &pf->hw;
10353 	i40e_status ret = 0;
10354 	u32 v;
10355 
10356 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10357 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10358 		return;
10359 	if (i40e_check_asq_alive(&pf->hw))
10360 		i40e_vc_notify_reset(pf);
10361 
10362 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10363 
10364 	/* quiesce the VSIs and their queues that are not already DOWN */
10365 	i40e_pf_quiesce_all_vsi(pf);
10366 
10367 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10368 		if (pf->vsi[v])
10369 			pf->vsi[v]->seid = 0;
10370 	}
10371 
10372 	i40e_shutdown_adminq(&pf->hw);
10373 
10374 	/* call shutdown HMC */
10375 	if (hw->hmc.hmc_obj) {
10376 		ret = i40e_shutdown_lan_hmc(hw);
10377 		if (ret)
10378 			dev_warn(&pf->pdev->dev,
10379 				 "shutdown_lan_hmc failed: %d\n", ret);
10380 	}
10381 
10382 	/* Save the current PTP time so that we can restore the time after the
10383 	 * reset completes.
10384 	 */
10385 	i40e_ptp_save_hw_time(pf);
10386 }
10387 
10388 /**
10389  * i40e_send_version - update firmware with driver version
10390  * @pf: PF struct
10391  */
10392 static void i40e_send_version(struct i40e_pf *pf)
10393 {
10394 	struct i40e_driver_version dv;
10395 
10396 	dv.major_version = 0xff;
10397 	dv.minor_version = 0xff;
10398 	dv.build_version = 0xff;
10399 	dv.subbuild_version = 0;
10400 	strlcpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10401 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10402 }
10403 
10404 /**
10405  * i40e_get_oem_version - get OEM specific version information
10406  * @hw: pointer to the hardware structure
10407  **/
10408 static void i40e_get_oem_version(struct i40e_hw *hw)
10409 {
10410 	u16 block_offset = 0xffff;
10411 	u16 block_length = 0;
10412 	u16 capabilities = 0;
10413 	u16 gen_snap = 0;
10414 	u16 release = 0;
10415 
10416 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10417 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10418 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10419 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10420 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10421 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10422 #define I40E_NVM_OEM_LENGTH			3
10423 
10424 	/* Check if pointer to OEM version block is valid. */
10425 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10426 	if (block_offset == 0xffff)
10427 		return;
10428 
10429 	/* Check if OEM version block has correct length. */
10430 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10431 			   &block_length);
10432 	if (block_length < I40E_NVM_OEM_LENGTH)
10433 		return;
10434 
10435 	/* Check if OEM version format is as expected. */
10436 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10437 			   &capabilities);
10438 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10439 		return;
10440 
10441 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10442 			   &gen_snap);
10443 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10444 			   &release);
10445 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10446 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10447 }
10448 
10449 /**
10450  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10451  * @pf: board private structure
10452  **/
10453 static int i40e_reset(struct i40e_pf *pf)
10454 {
10455 	struct i40e_hw *hw = &pf->hw;
10456 	i40e_status ret;
10457 
10458 	ret = i40e_pf_reset(hw);
10459 	if (ret) {
10460 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10461 		set_bit(__I40E_RESET_FAILED, pf->state);
10462 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10463 	} else {
10464 		pf->pfr_count++;
10465 	}
10466 	return ret;
10467 }
10468 
10469 /**
10470  * i40e_rebuild - rebuild using a saved config
10471  * @pf: board private structure
10472  * @reinit: if the Main VSI needs to re-initialized.
10473  * @lock_acquired: indicates whether or not the lock has been acquired
10474  * before this function was called.
10475  **/
10476 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10477 {
10478 	int old_recovery_mode_bit = test_bit(__I40E_RECOVERY_MODE, pf->state);
10479 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10480 	struct i40e_hw *hw = &pf->hw;
10481 	i40e_status ret;
10482 	u32 val;
10483 	int v;
10484 
10485 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10486 	    i40e_check_recovery_mode(pf)) {
10487 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10488 	}
10489 
10490 	if (test_bit(__I40E_DOWN, pf->state) &&
10491 	    !test_bit(__I40E_RECOVERY_MODE, pf->state) &&
10492 	    !old_recovery_mode_bit)
10493 		goto clear_recovery;
10494 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10495 
10496 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10497 	ret = i40e_init_adminq(&pf->hw);
10498 	if (ret) {
10499 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10500 			 i40e_stat_str(&pf->hw, ret),
10501 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10502 		goto clear_recovery;
10503 	}
10504 	i40e_get_oem_version(&pf->hw);
10505 
10506 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10507 	    ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) ||
10508 	     hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) {
10509 		/* The following delay is necessary for 4.33 firmware and older
10510 		 * to recover after EMP reset. 200 ms should suffice but we
10511 		 * put here 300 ms to be sure that FW is ready to operate
10512 		 * after reset.
10513 		 */
10514 		mdelay(300);
10515 	}
10516 
10517 	/* re-verify the eeprom if we just had an EMP reset */
10518 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10519 		i40e_verify_eeprom(pf);
10520 
10521 	/* if we are going out of or into recovery mode we have to act
10522 	 * accordingly with regard to resources initialization
10523 	 * and deinitialization
10524 	 */
10525 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) ||
10526 	    old_recovery_mode_bit) {
10527 		if (i40e_get_capabilities(pf,
10528 					  i40e_aqc_opc_list_func_capabilities))
10529 			goto end_unlock;
10530 
10531 		if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10532 			/* we're staying in recovery mode so we'll reinitialize
10533 			 * misc vector here
10534 			 */
10535 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10536 				goto end_unlock;
10537 		} else {
10538 			if (!lock_acquired)
10539 				rtnl_lock();
10540 			/* we're going out of recovery mode so we'll free
10541 			 * the IRQ allocated specifically for recovery mode
10542 			 * and restore the interrupt scheme
10543 			 */
10544 			free_irq(pf->pdev->irq, pf);
10545 			i40e_clear_interrupt_scheme(pf);
10546 			if (i40e_restore_interrupt_scheme(pf))
10547 				goto end_unlock;
10548 		}
10549 
10550 		/* tell the firmware that we're starting */
10551 		i40e_send_version(pf);
10552 
10553 		/* bail out in case recovery mode was detected, as there is
10554 		 * no need for further configuration.
10555 		 */
10556 		goto end_unlock;
10557 	}
10558 
10559 	i40e_clear_pxe_mode(hw);
10560 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10561 	if (ret)
10562 		goto end_core_reset;
10563 
10564 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10565 				hw->func_caps.num_rx_qp, 0, 0);
10566 	if (ret) {
10567 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10568 		goto end_core_reset;
10569 	}
10570 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10571 	if (ret) {
10572 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10573 		goto end_core_reset;
10574 	}
10575 
10576 #ifdef CONFIG_I40E_DCB
10577 	/* Enable FW to write a default DCB config on link-up
10578 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10579 	 * is not supported with new link speed
10580 	 */
10581 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
10582 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10583 	} else {
10584 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10585 		    (hw->phy.link_info.link_speed &
10586 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10587 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10588 			dev_warn(&pf->pdev->dev,
10589 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10590 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10591 		} else {
10592 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10593 			ret = i40e_init_pf_dcb(pf);
10594 			if (ret) {
10595 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10596 					 ret);
10597 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10598 				/* Continue without DCB enabled */
10599 			}
10600 		}
10601 	}
10602 
10603 #endif /* CONFIG_I40E_DCB */
10604 	if (!lock_acquired)
10605 		rtnl_lock();
10606 	ret = i40e_setup_pf_switch(pf, reinit);
10607 	if (ret)
10608 		goto end_unlock;
10609 
10610 	/* The driver only wants link up/down and module qualification
10611 	 * reports from firmware.  Note the negative logic.
10612 	 */
10613 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10614 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10615 					 I40E_AQ_EVENT_MEDIA_NA |
10616 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10617 	if (ret)
10618 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10619 			 i40e_stat_str(&pf->hw, ret),
10620 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10621 
10622 	/* Rebuild the VSIs and VEBs that existed before reset.
10623 	 * They are still in our local switch element arrays, so only
10624 	 * need to rebuild the switch model in the HW.
10625 	 *
10626 	 * If there were VEBs but the reconstitution failed, we'll try
10627 	 * to recover minimal use by getting the basic PF VSI working.
10628 	 */
10629 	if (vsi->uplink_seid != pf->mac_seid) {
10630 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10631 		/* find the one VEB connected to the MAC, and find orphans */
10632 		for (v = 0; v < I40E_MAX_VEB; v++) {
10633 			if (!pf->veb[v])
10634 				continue;
10635 
10636 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10637 			    pf->veb[v]->uplink_seid == 0) {
10638 				ret = i40e_reconstitute_veb(pf->veb[v]);
10639 
10640 				if (!ret)
10641 					continue;
10642 
10643 				/* If Main VEB failed, we're in deep doodoo,
10644 				 * so give up rebuilding the switch and set up
10645 				 * for minimal rebuild of PF VSI.
10646 				 * If orphan failed, we'll report the error
10647 				 * but try to keep going.
10648 				 */
10649 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10650 					dev_info(&pf->pdev->dev,
10651 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10652 						 ret);
10653 					vsi->uplink_seid = pf->mac_seid;
10654 					break;
10655 				} else if (pf->veb[v]->uplink_seid == 0) {
10656 					dev_info(&pf->pdev->dev,
10657 						 "rebuild of orphan VEB failed: %d\n",
10658 						 ret);
10659 				}
10660 			}
10661 		}
10662 	}
10663 
10664 	if (vsi->uplink_seid == pf->mac_seid) {
10665 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10666 		/* no VEB, so rebuild only the Main VSI */
10667 		ret = i40e_add_vsi(vsi);
10668 		if (ret) {
10669 			dev_info(&pf->pdev->dev,
10670 				 "rebuild of Main VSI failed: %d\n", ret);
10671 			goto end_unlock;
10672 		}
10673 	}
10674 
10675 	if (vsi->mqprio_qopt.max_rate[0]) {
10676 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
10677 		u64 credits = 0;
10678 
10679 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
10680 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10681 		if (ret)
10682 			goto end_unlock;
10683 
10684 		credits = max_tx_rate;
10685 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10686 		dev_dbg(&vsi->back->pdev->dev,
10687 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10688 			max_tx_rate,
10689 			credits,
10690 			vsi->seid);
10691 	}
10692 
10693 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10694 	if (ret)
10695 		goto end_unlock;
10696 
10697 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
10698 	 * for this main VSI if they exist
10699 	 */
10700 	ret = i40e_rebuild_channels(vsi);
10701 	if (ret)
10702 		goto end_unlock;
10703 
10704 	/* Reconfigure hardware for allowing smaller MSS in the case
10705 	 * of TSO, so that we avoid the MDD being fired and causing
10706 	 * a reset in the case of small MSS+TSO.
10707 	 */
10708 #define I40E_REG_MSS          0x000E64DC
10709 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
10710 #define I40E_64BYTE_MSS       0x400000
10711 	val = rd32(hw, I40E_REG_MSS);
10712 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10713 		val &= ~I40E_REG_MSS_MIN_MASK;
10714 		val |= I40E_64BYTE_MSS;
10715 		wr32(hw, I40E_REG_MSS, val);
10716 	}
10717 
10718 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
10719 		msleep(75);
10720 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10721 		if (ret)
10722 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10723 				 i40e_stat_str(&pf->hw, ret),
10724 				 i40e_aq_str(&pf->hw,
10725 					     pf->hw.aq.asq_last_status));
10726 	}
10727 	/* reinit the misc interrupt */
10728 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10729 		ret = i40e_setup_misc_vector(pf);
10730 
10731 	/* Add a filter to drop all Flow control frames from any VSI from being
10732 	 * transmitted. By doing so we stop a malicious VF from sending out
10733 	 * PAUSE or PFC frames and potentially controlling traffic for other
10734 	 * PF/VF VSIs.
10735 	 * The FW can still send Flow control frames if enabled.
10736 	 */
10737 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
10738 						       pf->main_vsi_seid);
10739 #ifdef CONFIG_I40E_DCB
10740 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP)
10741 		i40e_set_lldp_forwarding(pf, true);
10742 #endif /* CONFIG_I40E_DCB */
10743 
10744 	/* restart the VSIs that were rebuilt and running before the reset */
10745 	i40e_pf_unquiesce_all_vsi(pf);
10746 
10747 	/* Release the RTNL lock before we start resetting VFs */
10748 	if (!lock_acquired)
10749 		rtnl_unlock();
10750 
10751 	/* Restore promiscuous settings */
10752 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
10753 	if (ret)
10754 		dev_warn(&pf->pdev->dev,
10755 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
10756 			 pf->cur_promisc ? "on" : "off",
10757 			 i40e_stat_str(&pf->hw, ret),
10758 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10759 
10760 	i40e_reset_all_vfs(pf, true);
10761 
10762 	/* tell the firmware that we're starting */
10763 	i40e_send_version(pf);
10764 
10765 	/* We've already released the lock, so don't do it again */
10766 	goto end_core_reset;
10767 
10768 end_unlock:
10769 	if (!lock_acquired)
10770 		rtnl_unlock();
10771 end_core_reset:
10772 	clear_bit(__I40E_RESET_FAILED, pf->state);
10773 clear_recovery:
10774 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10775 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
10776 }
10777 
10778 /**
10779  * i40e_reset_and_rebuild - reset and rebuild using a saved config
10780  * @pf: board private structure
10781  * @reinit: if the Main VSI needs to re-initialized.
10782  * @lock_acquired: indicates whether or not the lock has been acquired
10783  * before this function was called.
10784  **/
10785 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
10786 				   bool lock_acquired)
10787 {
10788 	int ret;
10789 	/* Now we wait for GRST to settle out.
10790 	 * We don't have to delete the VEBs or VSIs from the hw switch
10791 	 * because the reset will make them disappear.
10792 	 */
10793 	ret = i40e_reset(pf);
10794 	if (!ret)
10795 		i40e_rebuild(pf, reinit, lock_acquired);
10796 }
10797 
10798 /**
10799  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
10800  * @pf: board private structure
10801  *
10802  * Close up the VFs and other things in prep for a Core Reset,
10803  * then get ready to rebuild the world.
10804  * @lock_acquired: indicates whether or not the lock has been acquired
10805  * before this function was called.
10806  **/
10807 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
10808 {
10809 	i40e_prep_for_reset(pf);
10810 	i40e_reset_and_rebuild(pf, false, lock_acquired);
10811 }
10812 
10813 /**
10814  * i40e_handle_mdd_event
10815  * @pf: pointer to the PF structure
10816  *
10817  * Called from the MDD irq handler to identify possibly malicious vfs
10818  **/
10819 static void i40e_handle_mdd_event(struct i40e_pf *pf)
10820 {
10821 	struct i40e_hw *hw = &pf->hw;
10822 	bool mdd_detected = false;
10823 	struct i40e_vf *vf;
10824 	u32 reg;
10825 	int i;
10826 
10827 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
10828 		return;
10829 
10830 	/* find what triggered the MDD event */
10831 	reg = rd32(hw, I40E_GL_MDET_TX);
10832 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
10833 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
10834 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
10835 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
10836 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
10837 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
10838 				I40E_GL_MDET_TX_EVENT_SHIFT;
10839 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
10840 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
10841 				pf->hw.func_caps.base_queue;
10842 		if (netif_msg_tx_err(pf))
10843 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
10844 				 event, queue, pf_num, vf_num);
10845 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
10846 		mdd_detected = true;
10847 	}
10848 	reg = rd32(hw, I40E_GL_MDET_RX);
10849 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
10850 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
10851 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
10852 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
10853 				I40E_GL_MDET_RX_EVENT_SHIFT;
10854 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
10855 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
10856 				pf->hw.func_caps.base_queue;
10857 		if (netif_msg_rx_err(pf))
10858 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
10859 				 event, queue, func);
10860 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
10861 		mdd_detected = true;
10862 	}
10863 
10864 	if (mdd_detected) {
10865 		reg = rd32(hw, I40E_PF_MDET_TX);
10866 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
10867 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
10868 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
10869 		}
10870 		reg = rd32(hw, I40E_PF_MDET_RX);
10871 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
10872 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
10873 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
10874 		}
10875 	}
10876 
10877 	/* see if one of the VFs needs its hand slapped */
10878 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
10879 		vf = &(pf->vf[i]);
10880 		reg = rd32(hw, I40E_VP_MDET_TX(i));
10881 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
10882 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
10883 			vf->num_mdd_events++;
10884 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
10885 				 i);
10886 			dev_info(&pf->pdev->dev,
10887 				 "Use PF Control I/F to re-enable the VF\n");
10888 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10889 		}
10890 
10891 		reg = rd32(hw, I40E_VP_MDET_RX(i));
10892 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
10893 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
10894 			vf->num_mdd_events++;
10895 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
10896 				 i);
10897 			dev_info(&pf->pdev->dev,
10898 				 "Use PF Control I/F to re-enable the VF\n");
10899 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10900 		}
10901 	}
10902 
10903 	/* re-enable mdd interrupt cause */
10904 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
10905 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
10906 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
10907 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
10908 	i40e_flush(hw);
10909 }
10910 
10911 /**
10912  * i40e_service_task - Run the driver's async subtasks
10913  * @work: pointer to work_struct containing our data
10914  **/
10915 static void i40e_service_task(struct work_struct *work)
10916 {
10917 	struct i40e_pf *pf = container_of(work,
10918 					  struct i40e_pf,
10919 					  service_task);
10920 	unsigned long start_time = jiffies;
10921 
10922 	/* don't bother with service tasks if a reset is in progress */
10923 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
10924 	    test_bit(__I40E_SUSPENDED, pf->state))
10925 		return;
10926 
10927 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
10928 		return;
10929 
10930 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10931 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
10932 		i40e_sync_filters_subtask(pf);
10933 		i40e_reset_subtask(pf);
10934 		i40e_handle_mdd_event(pf);
10935 		i40e_vc_process_vflr_event(pf);
10936 		i40e_watchdog_subtask(pf);
10937 		i40e_fdir_reinit_subtask(pf);
10938 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
10939 			/* Client subtask will reopen next time through. */
10940 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
10941 							   true);
10942 		} else {
10943 			i40e_client_subtask(pf);
10944 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
10945 					       pf->state))
10946 				i40e_notify_client_of_l2_param_changes(
10947 								pf->vsi[pf->lan_vsi]);
10948 		}
10949 		i40e_sync_filters_subtask(pf);
10950 	} else {
10951 		i40e_reset_subtask(pf);
10952 	}
10953 
10954 	i40e_clean_adminq_subtask(pf);
10955 
10956 	/* flush memory to make sure state is correct before next watchdog */
10957 	smp_mb__before_atomic();
10958 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
10959 
10960 	/* If the tasks have taken longer than one timer cycle or there
10961 	 * is more work to be done, reschedule the service task now
10962 	 * rather than wait for the timer to tick again.
10963 	 */
10964 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
10965 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
10966 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
10967 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
10968 		i40e_service_event_schedule(pf);
10969 }
10970 
10971 /**
10972  * i40e_service_timer - timer callback
10973  * @t: timer list pointer
10974  **/
10975 static void i40e_service_timer(struct timer_list *t)
10976 {
10977 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
10978 
10979 	mod_timer(&pf->service_timer,
10980 		  round_jiffies(jiffies + pf->service_timer_period));
10981 	i40e_service_event_schedule(pf);
10982 }
10983 
10984 /**
10985  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
10986  * @vsi: the VSI being configured
10987  **/
10988 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
10989 {
10990 	struct i40e_pf *pf = vsi->back;
10991 
10992 	switch (vsi->type) {
10993 	case I40E_VSI_MAIN:
10994 		vsi->alloc_queue_pairs = pf->num_lan_qps;
10995 		if (!vsi->num_tx_desc)
10996 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10997 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10998 		if (!vsi->num_rx_desc)
10999 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11000 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11001 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11002 			vsi->num_q_vectors = pf->num_lan_msix;
11003 		else
11004 			vsi->num_q_vectors = 1;
11005 
11006 		break;
11007 
11008 	case I40E_VSI_FDIR:
11009 		vsi->alloc_queue_pairs = 1;
11010 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11011 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11012 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11013 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11014 		vsi->num_q_vectors = pf->num_fdsb_msix;
11015 		break;
11016 
11017 	case I40E_VSI_VMDQ2:
11018 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11019 		if (!vsi->num_tx_desc)
11020 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11021 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11022 		if (!vsi->num_rx_desc)
11023 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11024 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11025 		vsi->num_q_vectors = pf->num_vmdq_msix;
11026 		break;
11027 
11028 	case I40E_VSI_SRIOV:
11029 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11030 		if (!vsi->num_tx_desc)
11031 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11032 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11033 		if (!vsi->num_rx_desc)
11034 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11035 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11036 		break;
11037 
11038 	default:
11039 		WARN_ON(1);
11040 		return -ENODATA;
11041 	}
11042 
11043 	if (is_kdump_kernel()) {
11044 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11045 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11046 	}
11047 
11048 	return 0;
11049 }
11050 
11051 /**
11052  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11053  * @vsi: VSI pointer
11054  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11055  *
11056  * On error: returns error code (negative)
11057  * On success: returns 0
11058  **/
11059 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11060 {
11061 	struct i40e_ring **next_rings;
11062 	int size;
11063 	int ret = 0;
11064 
11065 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11066 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11067 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11068 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11069 	if (!vsi->tx_rings)
11070 		return -ENOMEM;
11071 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11072 	if (i40e_enabled_xdp_vsi(vsi)) {
11073 		vsi->xdp_rings = next_rings;
11074 		next_rings += vsi->alloc_queue_pairs;
11075 	}
11076 	vsi->rx_rings = next_rings;
11077 
11078 	if (alloc_qvectors) {
11079 		/* allocate memory for q_vector pointers */
11080 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11081 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11082 		if (!vsi->q_vectors) {
11083 			ret = -ENOMEM;
11084 			goto err_vectors;
11085 		}
11086 	}
11087 	return ret;
11088 
11089 err_vectors:
11090 	kfree(vsi->tx_rings);
11091 	return ret;
11092 }
11093 
11094 /**
11095  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11096  * @pf: board private structure
11097  * @type: type of VSI
11098  *
11099  * On error: returns error code (negative)
11100  * On success: returns vsi index in PF (positive)
11101  **/
11102 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11103 {
11104 	int ret = -ENODEV;
11105 	struct i40e_vsi *vsi;
11106 	int vsi_idx;
11107 	int i;
11108 
11109 	/* Need to protect the allocation of the VSIs at the PF level */
11110 	mutex_lock(&pf->switch_mutex);
11111 
11112 	/* VSI list may be fragmented if VSI creation/destruction has
11113 	 * been happening.  We can afford to do a quick scan to look
11114 	 * for any free VSIs in the list.
11115 	 *
11116 	 * find next empty vsi slot, looping back around if necessary
11117 	 */
11118 	i = pf->next_vsi;
11119 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11120 		i++;
11121 	if (i >= pf->num_alloc_vsi) {
11122 		i = 0;
11123 		while (i < pf->next_vsi && pf->vsi[i])
11124 			i++;
11125 	}
11126 
11127 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11128 		vsi_idx = i;             /* Found one! */
11129 	} else {
11130 		ret = -ENODEV;
11131 		goto unlock_pf;  /* out of VSI slots! */
11132 	}
11133 	pf->next_vsi = ++i;
11134 
11135 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11136 	if (!vsi) {
11137 		ret = -ENOMEM;
11138 		goto unlock_pf;
11139 	}
11140 	vsi->type = type;
11141 	vsi->back = pf;
11142 	set_bit(__I40E_VSI_DOWN, vsi->state);
11143 	vsi->flags = 0;
11144 	vsi->idx = vsi_idx;
11145 	vsi->int_rate_limit = 0;
11146 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11147 				pf->rss_table_size : 64;
11148 	vsi->netdev_registered = false;
11149 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11150 	hash_init(vsi->mac_filter_hash);
11151 	vsi->irqs_ready = false;
11152 
11153 	if (type == I40E_VSI_MAIN) {
11154 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11155 		if (!vsi->af_xdp_zc_qps)
11156 			goto err_rings;
11157 	}
11158 
11159 	ret = i40e_set_num_rings_in_vsi(vsi);
11160 	if (ret)
11161 		goto err_rings;
11162 
11163 	ret = i40e_vsi_alloc_arrays(vsi, true);
11164 	if (ret)
11165 		goto err_rings;
11166 
11167 	/* Setup default MSIX irq handler for VSI */
11168 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11169 
11170 	/* Initialize VSI lock */
11171 	spin_lock_init(&vsi->mac_filter_hash_lock);
11172 	pf->vsi[vsi_idx] = vsi;
11173 	ret = vsi_idx;
11174 	goto unlock_pf;
11175 
11176 err_rings:
11177 	bitmap_free(vsi->af_xdp_zc_qps);
11178 	pf->next_vsi = i - 1;
11179 	kfree(vsi);
11180 unlock_pf:
11181 	mutex_unlock(&pf->switch_mutex);
11182 	return ret;
11183 }
11184 
11185 /**
11186  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11187  * @vsi: VSI pointer
11188  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11189  *
11190  * On error: returns error code (negative)
11191  * On success: returns 0
11192  **/
11193 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11194 {
11195 	/* free the ring and vector containers */
11196 	if (free_qvectors) {
11197 		kfree(vsi->q_vectors);
11198 		vsi->q_vectors = NULL;
11199 	}
11200 	kfree(vsi->tx_rings);
11201 	vsi->tx_rings = NULL;
11202 	vsi->rx_rings = NULL;
11203 	vsi->xdp_rings = NULL;
11204 }
11205 
11206 /**
11207  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11208  * and lookup table
11209  * @vsi: Pointer to VSI structure
11210  */
11211 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11212 {
11213 	if (!vsi)
11214 		return;
11215 
11216 	kfree(vsi->rss_hkey_user);
11217 	vsi->rss_hkey_user = NULL;
11218 
11219 	kfree(vsi->rss_lut_user);
11220 	vsi->rss_lut_user = NULL;
11221 }
11222 
11223 /**
11224  * i40e_vsi_clear - Deallocate the VSI provided
11225  * @vsi: the VSI being un-configured
11226  **/
11227 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11228 {
11229 	struct i40e_pf *pf;
11230 
11231 	if (!vsi)
11232 		return 0;
11233 
11234 	if (!vsi->back)
11235 		goto free_vsi;
11236 	pf = vsi->back;
11237 
11238 	mutex_lock(&pf->switch_mutex);
11239 	if (!pf->vsi[vsi->idx]) {
11240 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11241 			vsi->idx, vsi->idx, vsi->type);
11242 		goto unlock_vsi;
11243 	}
11244 
11245 	if (pf->vsi[vsi->idx] != vsi) {
11246 		dev_err(&pf->pdev->dev,
11247 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11248 			pf->vsi[vsi->idx]->idx,
11249 			pf->vsi[vsi->idx]->type,
11250 			vsi->idx, vsi->type);
11251 		goto unlock_vsi;
11252 	}
11253 
11254 	/* updates the PF for this cleared vsi */
11255 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11256 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11257 
11258 	bitmap_free(vsi->af_xdp_zc_qps);
11259 	i40e_vsi_free_arrays(vsi, true);
11260 	i40e_clear_rss_config_user(vsi);
11261 
11262 	pf->vsi[vsi->idx] = NULL;
11263 	if (vsi->idx < pf->next_vsi)
11264 		pf->next_vsi = vsi->idx;
11265 
11266 unlock_vsi:
11267 	mutex_unlock(&pf->switch_mutex);
11268 free_vsi:
11269 	kfree(vsi);
11270 
11271 	return 0;
11272 }
11273 
11274 /**
11275  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11276  * @vsi: the VSI being cleaned
11277  **/
11278 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11279 {
11280 	int i;
11281 
11282 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11283 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11284 			kfree_rcu(vsi->tx_rings[i], rcu);
11285 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11286 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11287 			if (vsi->xdp_rings)
11288 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11289 		}
11290 	}
11291 }
11292 
11293 /**
11294  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11295  * @vsi: the VSI being configured
11296  **/
11297 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11298 {
11299 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11300 	struct i40e_pf *pf = vsi->back;
11301 	struct i40e_ring *ring;
11302 
11303 	/* Set basic values in the rings to be used later during open() */
11304 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11305 		/* allocate space for both Tx and Rx in one shot */
11306 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11307 		if (!ring)
11308 			goto err_out;
11309 
11310 		ring->queue_index = i;
11311 		ring->reg_idx = vsi->base_queue + i;
11312 		ring->ring_active = false;
11313 		ring->vsi = vsi;
11314 		ring->netdev = vsi->netdev;
11315 		ring->dev = &pf->pdev->dev;
11316 		ring->count = vsi->num_tx_desc;
11317 		ring->size = 0;
11318 		ring->dcb_tc = 0;
11319 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11320 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11321 		ring->itr_setting = pf->tx_itr_default;
11322 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11323 
11324 		if (!i40e_enabled_xdp_vsi(vsi))
11325 			goto setup_rx;
11326 
11327 		ring->queue_index = vsi->alloc_queue_pairs + i;
11328 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11329 		ring->ring_active = false;
11330 		ring->vsi = vsi;
11331 		ring->netdev = NULL;
11332 		ring->dev = &pf->pdev->dev;
11333 		ring->count = vsi->num_tx_desc;
11334 		ring->size = 0;
11335 		ring->dcb_tc = 0;
11336 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11337 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11338 		set_ring_xdp(ring);
11339 		ring->itr_setting = pf->tx_itr_default;
11340 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11341 
11342 setup_rx:
11343 		ring->queue_index = i;
11344 		ring->reg_idx = vsi->base_queue + i;
11345 		ring->ring_active = false;
11346 		ring->vsi = vsi;
11347 		ring->netdev = vsi->netdev;
11348 		ring->dev = &pf->pdev->dev;
11349 		ring->count = vsi->num_rx_desc;
11350 		ring->size = 0;
11351 		ring->dcb_tc = 0;
11352 		ring->itr_setting = pf->rx_itr_default;
11353 		WRITE_ONCE(vsi->rx_rings[i], ring);
11354 	}
11355 
11356 	return 0;
11357 
11358 err_out:
11359 	i40e_vsi_clear_rings(vsi);
11360 	return -ENOMEM;
11361 }
11362 
11363 /**
11364  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11365  * @pf: board private structure
11366  * @vectors: the number of MSI-X vectors to request
11367  *
11368  * Returns the number of vectors reserved, or error
11369  **/
11370 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11371 {
11372 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11373 					I40E_MIN_MSIX, vectors);
11374 	if (vectors < 0) {
11375 		dev_info(&pf->pdev->dev,
11376 			 "MSI-X vector reservation failed: %d\n", vectors);
11377 		vectors = 0;
11378 	}
11379 
11380 	return vectors;
11381 }
11382 
11383 /**
11384  * i40e_init_msix - Setup the MSIX capability
11385  * @pf: board private structure
11386  *
11387  * Work with the OS to set up the MSIX vectors needed.
11388  *
11389  * Returns the number of vectors reserved or negative on failure
11390  **/
11391 static int i40e_init_msix(struct i40e_pf *pf)
11392 {
11393 	struct i40e_hw *hw = &pf->hw;
11394 	int cpus, extra_vectors;
11395 	int vectors_left;
11396 	int v_budget, i;
11397 	int v_actual;
11398 	int iwarp_requested = 0;
11399 
11400 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11401 		return -ENODEV;
11402 
11403 	/* The number of vectors we'll request will be comprised of:
11404 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11405 	 *   - The number of LAN queue pairs
11406 	 *	- Queues being used for RSS.
11407 	 *		We don't need as many as max_rss_size vectors.
11408 	 *		use rss_size instead in the calculation since that
11409 	 *		is governed by number of cpus in the system.
11410 	 *	- assumes symmetric Tx/Rx pairing
11411 	 *   - The number of VMDq pairs
11412 	 *   - The CPU count within the NUMA node if iWARP is enabled
11413 	 * Once we count this up, try the request.
11414 	 *
11415 	 * If we can't get what we want, we'll simplify to nearly nothing
11416 	 * and try again.  If that still fails, we punt.
11417 	 */
11418 	vectors_left = hw->func_caps.num_msix_vectors;
11419 	v_budget = 0;
11420 
11421 	/* reserve one vector for miscellaneous handler */
11422 	if (vectors_left) {
11423 		v_budget++;
11424 		vectors_left--;
11425 	}
11426 
11427 	/* reserve some vectors for the main PF traffic queues. Initially we
11428 	 * only reserve at most 50% of the available vectors, in the case that
11429 	 * the number of online CPUs is large. This ensures that we can enable
11430 	 * extra features as well. Once we've enabled the other features, we
11431 	 * will use any remaining vectors to reach as close as we can to the
11432 	 * number of online CPUs.
11433 	 */
11434 	cpus = num_online_cpus();
11435 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11436 	vectors_left -= pf->num_lan_msix;
11437 
11438 	/* reserve one vector for sideband flow director */
11439 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11440 		if (vectors_left) {
11441 			pf->num_fdsb_msix = 1;
11442 			v_budget++;
11443 			vectors_left--;
11444 		} else {
11445 			pf->num_fdsb_msix = 0;
11446 		}
11447 	}
11448 
11449 	/* can we reserve enough for iWARP? */
11450 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11451 		iwarp_requested = pf->num_iwarp_msix;
11452 
11453 		if (!vectors_left)
11454 			pf->num_iwarp_msix = 0;
11455 		else if (vectors_left < pf->num_iwarp_msix)
11456 			pf->num_iwarp_msix = 1;
11457 		v_budget += pf->num_iwarp_msix;
11458 		vectors_left -= pf->num_iwarp_msix;
11459 	}
11460 
11461 	/* any vectors left over go for VMDq support */
11462 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11463 		if (!vectors_left) {
11464 			pf->num_vmdq_msix = 0;
11465 			pf->num_vmdq_qps = 0;
11466 		} else {
11467 			int vmdq_vecs_wanted =
11468 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11469 			int vmdq_vecs =
11470 				min_t(int, vectors_left, vmdq_vecs_wanted);
11471 
11472 			/* if we're short on vectors for what's desired, we limit
11473 			 * the queues per vmdq.  If this is still more than are
11474 			 * available, the user will need to change the number of
11475 			 * queues/vectors used by the PF later with the ethtool
11476 			 * channels command
11477 			 */
11478 			if (vectors_left < vmdq_vecs_wanted) {
11479 				pf->num_vmdq_qps = 1;
11480 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11481 				vmdq_vecs = min_t(int,
11482 						  vectors_left,
11483 						  vmdq_vecs_wanted);
11484 			}
11485 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11486 
11487 			v_budget += vmdq_vecs;
11488 			vectors_left -= vmdq_vecs;
11489 		}
11490 	}
11491 
11492 	/* On systems with a large number of SMP cores, we previously limited
11493 	 * the number of vectors for num_lan_msix to be at most 50% of the
11494 	 * available vectors, to allow for other features. Now, we add back
11495 	 * the remaining vectors. However, we ensure that the total
11496 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11497 	 * calculate the number of vectors we can add without going over the
11498 	 * cap of CPUs. For systems with a small number of CPUs this will be
11499 	 * zero.
11500 	 */
11501 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11502 	pf->num_lan_msix += extra_vectors;
11503 	vectors_left -= extra_vectors;
11504 
11505 	WARN(vectors_left < 0,
11506 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11507 
11508 	v_budget += pf->num_lan_msix;
11509 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11510 				   GFP_KERNEL);
11511 	if (!pf->msix_entries)
11512 		return -ENOMEM;
11513 
11514 	for (i = 0; i < v_budget; i++)
11515 		pf->msix_entries[i].entry = i;
11516 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11517 
11518 	if (v_actual < I40E_MIN_MSIX) {
11519 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11520 		kfree(pf->msix_entries);
11521 		pf->msix_entries = NULL;
11522 		pci_disable_msix(pf->pdev);
11523 		return -ENODEV;
11524 
11525 	} else if (v_actual == I40E_MIN_MSIX) {
11526 		/* Adjust for minimal MSIX use */
11527 		pf->num_vmdq_vsis = 0;
11528 		pf->num_vmdq_qps = 0;
11529 		pf->num_lan_qps = 1;
11530 		pf->num_lan_msix = 1;
11531 
11532 	} else if (v_actual != v_budget) {
11533 		/* If we have limited resources, we will start with no vectors
11534 		 * for the special features and then allocate vectors to some
11535 		 * of these features based on the policy and at the end disable
11536 		 * the features that did not get any vectors.
11537 		 */
11538 		int vec;
11539 
11540 		dev_info(&pf->pdev->dev,
11541 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11542 			 v_actual, v_budget);
11543 		/* reserve the misc vector */
11544 		vec = v_actual - 1;
11545 
11546 		/* Scale vector usage down */
11547 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11548 		pf->num_vmdq_vsis = 1;
11549 		pf->num_vmdq_qps = 1;
11550 
11551 		/* partition out the remaining vectors */
11552 		switch (vec) {
11553 		case 2:
11554 			pf->num_lan_msix = 1;
11555 			break;
11556 		case 3:
11557 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11558 				pf->num_lan_msix = 1;
11559 				pf->num_iwarp_msix = 1;
11560 			} else {
11561 				pf->num_lan_msix = 2;
11562 			}
11563 			break;
11564 		default:
11565 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11566 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11567 						 iwarp_requested);
11568 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11569 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11570 			} else {
11571 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11572 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11573 			}
11574 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11575 				pf->num_fdsb_msix = 1;
11576 				vec--;
11577 			}
11578 			pf->num_lan_msix = min_t(int,
11579 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11580 							      pf->num_lan_msix);
11581 			pf->num_lan_qps = pf->num_lan_msix;
11582 			break;
11583 		}
11584 	}
11585 
11586 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11587 	    (pf->num_fdsb_msix == 0)) {
11588 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11589 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11590 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11591 	}
11592 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11593 	    (pf->num_vmdq_msix == 0)) {
11594 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11595 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11596 	}
11597 
11598 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11599 	    (pf->num_iwarp_msix == 0)) {
11600 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11601 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11602 	}
11603 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11604 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11605 		   pf->num_lan_msix,
11606 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11607 		   pf->num_fdsb_msix,
11608 		   pf->num_iwarp_msix);
11609 
11610 	return v_actual;
11611 }
11612 
11613 /**
11614  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11615  * @vsi: the VSI being configured
11616  * @v_idx: index of the vector in the vsi struct
11617  *
11618  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11619  **/
11620 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11621 {
11622 	struct i40e_q_vector *q_vector;
11623 
11624 	/* allocate q_vector */
11625 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11626 	if (!q_vector)
11627 		return -ENOMEM;
11628 
11629 	q_vector->vsi = vsi;
11630 	q_vector->v_idx = v_idx;
11631 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11632 
11633 	if (vsi->netdev)
11634 		netif_napi_add(vsi->netdev, &q_vector->napi,
11635 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
11636 
11637 	/* tie q_vector and vsi together */
11638 	vsi->q_vectors[v_idx] = q_vector;
11639 
11640 	return 0;
11641 }
11642 
11643 /**
11644  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11645  * @vsi: the VSI being configured
11646  *
11647  * We allocate one q_vector per queue interrupt.  If allocation fails we
11648  * return -ENOMEM.
11649  **/
11650 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11651 {
11652 	struct i40e_pf *pf = vsi->back;
11653 	int err, v_idx, num_q_vectors;
11654 
11655 	/* if not MSIX, give the one vector only to the LAN VSI */
11656 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11657 		num_q_vectors = vsi->num_q_vectors;
11658 	else if (vsi == pf->vsi[pf->lan_vsi])
11659 		num_q_vectors = 1;
11660 	else
11661 		return -EINVAL;
11662 
11663 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11664 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11665 		if (err)
11666 			goto err_out;
11667 	}
11668 
11669 	return 0;
11670 
11671 err_out:
11672 	while (v_idx--)
11673 		i40e_free_q_vector(vsi, v_idx);
11674 
11675 	return err;
11676 }
11677 
11678 /**
11679  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11680  * @pf: board private structure to initialize
11681  **/
11682 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11683 {
11684 	int vectors = 0;
11685 	ssize_t size;
11686 
11687 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11688 		vectors = i40e_init_msix(pf);
11689 		if (vectors < 0) {
11690 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11691 				       I40E_FLAG_IWARP_ENABLED	|
11692 				       I40E_FLAG_RSS_ENABLED	|
11693 				       I40E_FLAG_DCB_CAPABLE	|
11694 				       I40E_FLAG_DCB_ENABLED	|
11695 				       I40E_FLAG_SRIOV_ENABLED	|
11696 				       I40E_FLAG_FD_SB_ENABLED	|
11697 				       I40E_FLAG_FD_ATR_ENABLED	|
11698 				       I40E_FLAG_VMDQ_ENABLED);
11699 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11700 
11701 			/* rework the queue expectations without MSIX */
11702 			i40e_determine_queue_usage(pf);
11703 		}
11704 	}
11705 
11706 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11707 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
11708 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
11709 		vectors = pci_enable_msi(pf->pdev);
11710 		if (vectors < 0) {
11711 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
11712 				 vectors);
11713 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
11714 		}
11715 		vectors = 1;  /* one MSI or Legacy vector */
11716 	}
11717 
11718 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
11719 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
11720 
11721 	/* set up vector assignment tracking */
11722 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
11723 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
11724 	if (!pf->irq_pile)
11725 		return -ENOMEM;
11726 
11727 	pf->irq_pile->num_entries = vectors;
11728 	pf->irq_pile->search_hint = 0;
11729 
11730 	/* track first vector for misc interrupts, ignore return */
11731 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
11732 
11733 	return 0;
11734 }
11735 
11736 /**
11737  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
11738  * @pf: private board data structure
11739  *
11740  * Restore the interrupt scheme that was cleared when we suspended the
11741  * device. This should be called during resume to re-allocate the q_vectors
11742  * and reacquire IRQs.
11743  */
11744 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
11745 {
11746 	int err, i;
11747 
11748 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
11749 	 * scheme. We need to re-enabled them here in order to attempt to
11750 	 * re-acquire the MSI or MSI-X vectors
11751 	 */
11752 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
11753 
11754 	err = i40e_init_interrupt_scheme(pf);
11755 	if (err)
11756 		return err;
11757 
11758 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
11759 	 * rings together again.
11760 	 */
11761 	for (i = 0; i < pf->num_alloc_vsi; i++) {
11762 		if (pf->vsi[i]) {
11763 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
11764 			if (err)
11765 				goto err_unwind;
11766 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
11767 		}
11768 	}
11769 
11770 	err = i40e_setup_misc_vector(pf);
11771 	if (err)
11772 		goto err_unwind;
11773 
11774 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
11775 		i40e_client_update_msix_info(pf);
11776 
11777 	return 0;
11778 
11779 err_unwind:
11780 	while (i--) {
11781 		if (pf->vsi[i])
11782 			i40e_vsi_free_q_vectors(pf->vsi[i]);
11783 	}
11784 
11785 	return err;
11786 }
11787 
11788 /**
11789  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
11790  * non queue events in recovery mode
11791  * @pf: board private structure
11792  *
11793  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
11794  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
11795  * This is handled differently than in recovery mode since no Tx/Rx resources
11796  * are being allocated.
11797  **/
11798 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
11799 {
11800 	int err;
11801 
11802 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11803 		err = i40e_setup_misc_vector(pf);
11804 
11805 		if (err) {
11806 			dev_info(&pf->pdev->dev,
11807 				 "MSI-X misc vector request failed, error %d\n",
11808 				 err);
11809 			return err;
11810 		}
11811 	} else {
11812 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
11813 
11814 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
11815 				  pf->int_name, pf);
11816 
11817 		if (err) {
11818 			dev_info(&pf->pdev->dev,
11819 				 "MSI/legacy misc vector request failed, error %d\n",
11820 				 err);
11821 			return err;
11822 		}
11823 		i40e_enable_misc_int_causes(pf);
11824 		i40e_irq_dynamic_enable_icr0(pf);
11825 	}
11826 
11827 	return 0;
11828 }
11829 
11830 /**
11831  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
11832  * @pf: board private structure
11833  *
11834  * This sets up the handler for MSIX 0, which is used to manage the
11835  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
11836  * when in MSI or Legacy interrupt mode.
11837  **/
11838 static int i40e_setup_misc_vector(struct i40e_pf *pf)
11839 {
11840 	struct i40e_hw *hw = &pf->hw;
11841 	int err = 0;
11842 
11843 	/* Only request the IRQ once, the first time through. */
11844 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
11845 		err = request_irq(pf->msix_entries[0].vector,
11846 				  i40e_intr, 0, pf->int_name, pf);
11847 		if (err) {
11848 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
11849 			dev_info(&pf->pdev->dev,
11850 				 "request_irq for %s failed: %d\n",
11851 				 pf->int_name, err);
11852 			return -EFAULT;
11853 		}
11854 	}
11855 
11856 	i40e_enable_misc_int_causes(pf);
11857 
11858 	/* associate no queues to the misc vector */
11859 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
11860 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
11861 
11862 	i40e_flush(hw);
11863 
11864 	i40e_irq_dynamic_enable_icr0(pf);
11865 
11866 	return err;
11867 }
11868 
11869 /**
11870  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
11871  * @vsi: Pointer to vsi structure
11872  * @seed: Buffter to store the hash keys
11873  * @lut: Buffer to store the lookup table entries
11874  * @lut_size: Size of buffer to store the lookup table entries
11875  *
11876  * Return 0 on success, negative on failure
11877  */
11878 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
11879 			   u8 *lut, u16 lut_size)
11880 {
11881 	struct i40e_pf *pf = vsi->back;
11882 	struct i40e_hw *hw = &pf->hw;
11883 	int ret = 0;
11884 
11885 	if (seed) {
11886 		ret = i40e_aq_get_rss_key(hw, vsi->id,
11887 			(struct i40e_aqc_get_set_rss_key_data *)seed);
11888 		if (ret) {
11889 			dev_info(&pf->pdev->dev,
11890 				 "Cannot get RSS key, err %s aq_err %s\n",
11891 				 i40e_stat_str(&pf->hw, ret),
11892 				 i40e_aq_str(&pf->hw,
11893 					     pf->hw.aq.asq_last_status));
11894 			return ret;
11895 		}
11896 	}
11897 
11898 	if (lut) {
11899 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
11900 
11901 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
11902 		if (ret) {
11903 			dev_info(&pf->pdev->dev,
11904 				 "Cannot get RSS lut, err %s aq_err %s\n",
11905 				 i40e_stat_str(&pf->hw, ret),
11906 				 i40e_aq_str(&pf->hw,
11907 					     pf->hw.aq.asq_last_status));
11908 			return ret;
11909 		}
11910 	}
11911 
11912 	return ret;
11913 }
11914 
11915 /**
11916  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
11917  * @vsi: Pointer to vsi structure
11918  * @seed: RSS hash seed
11919  * @lut: Lookup table
11920  * @lut_size: Lookup table size
11921  *
11922  * Returns 0 on success, negative on failure
11923  **/
11924 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
11925 			       const u8 *lut, u16 lut_size)
11926 {
11927 	struct i40e_pf *pf = vsi->back;
11928 	struct i40e_hw *hw = &pf->hw;
11929 	u16 vf_id = vsi->vf_id;
11930 	u8 i;
11931 
11932 	/* Fill out hash function seed */
11933 	if (seed) {
11934 		u32 *seed_dw = (u32 *)seed;
11935 
11936 		if (vsi->type == I40E_VSI_MAIN) {
11937 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
11938 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
11939 		} else if (vsi->type == I40E_VSI_SRIOV) {
11940 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
11941 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
11942 		} else {
11943 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
11944 		}
11945 	}
11946 
11947 	if (lut) {
11948 		u32 *lut_dw = (u32 *)lut;
11949 
11950 		if (vsi->type == I40E_VSI_MAIN) {
11951 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
11952 				return -EINVAL;
11953 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11954 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
11955 		} else if (vsi->type == I40E_VSI_SRIOV) {
11956 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
11957 				return -EINVAL;
11958 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
11959 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
11960 		} else {
11961 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
11962 		}
11963 	}
11964 	i40e_flush(hw);
11965 
11966 	return 0;
11967 }
11968 
11969 /**
11970  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
11971  * @vsi: Pointer to VSI structure
11972  * @seed: Buffer to store the keys
11973  * @lut: Buffer to store the lookup table entries
11974  * @lut_size: Size of buffer to store the lookup table entries
11975  *
11976  * Returns 0 on success, negative on failure
11977  */
11978 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
11979 			    u8 *lut, u16 lut_size)
11980 {
11981 	struct i40e_pf *pf = vsi->back;
11982 	struct i40e_hw *hw = &pf->hw;
11983 	u16 i;
11984 
11985 	if (seed) {
11986 		u32 *seed_dw = (u32 *)seed;
11987 
11988 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
11989 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
11990 	}
11991 	if (lut) {
11992 		u32 *lut_dw = (u32 *)lut;
11993 
11994 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
11995 			return -EINVAL;
11996 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11997 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
11998 	}
11999 
12000 	return 0;
12001 }
12002 
12003 /**
12004  * i40e_config_rss - Configure RSS keys and lut
12005  * @vsi: Pointer to VSI structure
12006  * @seed: RSS hash seed
12007  * @lut: Lookup table
12008  * @lut_size: Lookup table size
12009  *
12010  * Returns 0 on success, negative on failure
12011  */
12012 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12013 {
12014 	struct i40e_pf *pf = vsi->back;
12015 
12016 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12017 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12018 	else
12019 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12020 }
12021 
12022 /**
12023  * i40e_get_rss - Get RSS keys and lut
12024  * @vsi: Pointer to VSI structure
12025  * @seed: Buffer to store the keys
12026  * @lut: Buffer to store the lookup table entries
12027  * @lut_size: Size of buffer to store the lookup table entries
12028  *
12029  * Returns 0 on success, negative on failure
12030  */
12031 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12032 {
12033 	struct i40e_pf *pf = vsi->back;
12034 
12035 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12036 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12037 	else
12038 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12039 }
12040 
12041 /**
12042  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12043  * @pf: Pointer to board private structure
12044  * @lut: Lookup table
12045  * @rss_table_size: Lookup table size
12046  * @rss_size: Range of queue number for hashing
12047  */
12048 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12049 		       u16 rss_table_size, u16 rss_size)
12050 {
12051 	u16 i;
12052 
12053 	for (i = 0; i < rss_table_size; i++)
12054 		lut[i] = i % rss_size;
12055 }
12056 
12057 /**
12058  * i40e_pf_config_rss - Prepare for RSS if used
12059  * @pf: board private structure
12060  **/
12061 static int i40e_pf_config_rss(struct i40e_pf *pf)
12062 {
12063 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12064 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12065 	u8 *lut;
12066 	struct i40e_hw *hw = &pf->hw;
12067 	u32 reg_val;
12068 	u64 hena;
12069 	int ret;
12070 
12071 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12072 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12073 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12074 	hena |= i40e_pf_get_default_rss_hena(pf);
12075 
12076 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12077 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12078 
12079 	/* Determine the RSS table size based on the hardware capabilities */
12080 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12081 	reg_val = (pf->rss_table_size == 512) ?
12082 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12083 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12084 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12085 
12086 	/* Determine the RSS size of the VSI */
12087 	if (!vsi->rss_size) {
12088 		u16 qcount;
12089 		/* If the firmware does something weird during VSI init, we
12090 		 * could end up with zero TCs. Check for that to avoid
12091 		 * divide-by-zero. It probably won't pass traffic, but it also
12092 		 * won't panic.
12093 		 */
12094 		qcount = vsi->num_queue_pairs /
12095 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12096 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12097 	}
12098 	if (!vsi->rss_size)
12099 		return -EINVAL;
12100 
12101 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12102 	if (!lut)
12103 		return -ENOMEM;
12104 
12105 	/* Use user configured lut if there is one, otherwise use default */
12106 	if (vsi->rss_lut_user)
12107 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12108 	else
12109 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12110 
12111 	/* Use user configured hash key if there is one, otherwise
12112 	 * use default.
12113 	 */
12114 	if (vsi->rss_hkey_user)
12115 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12116 	else
12117 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12118 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12119 	kfree(lut);
12120 
12121 	return ret;
12122 }
12123 
12124 /**
12125  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12126  * @pf: board private structure
12127  * @queue_count: the requested queue count for rss.
12128  *
12129  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12130  * count which may be different from the requested queue count.
12131  * Note: expects to be called while under rtnl_lock()
12132  **/
12133 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12134 {
12135 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12136 	int new_rss_size;
12137 
12138 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12139 		return 0;
12140 
12141 	queue_count = min_t(int, queue_count, num_online_cpus());
12142 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12143 
12144 	if (queue_count != vsi->num_queue_pairs) {
12145 		u16 qcount;
12146 
12147 		vsi->req_queue_pairs = queue_count;
12148 		i40e_prep_for_reset(pf);
12149 
12150 		pf->alloc_rss_size = new_rss_size;
12151 
12152 		i40e_reset_and_rebuild(pf, true, true);
12153 
12154 		/* Discard the user configured hash keys and lut, if less
12155 		 * queues are enabled.
12156 		 */
12157 		if (queue_count < vsi->rss_size) {
12158 			i40e_clear_rss_config_user(vsi);
12159 			dev_dbg(&pf->pdev->dev,
12160 				"discard user configured hash keys and lut\n");
12161 		}
12162 
12163 		/* Reset vsi->rss_size, as number of enabled queues changed */
12164 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12165 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12166 
12167 		i40e_pf_config_rss(pf);
12168 	}
12169 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12170 		 vsi->req_queue_pairs, pf->rss_size_max);
12171 	return pf->alloc_rss_size;
12172 }
12173 
12174 /**
12175  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12176  * @pf: board private structure
12177  **/
12178 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
12179 {
12180 	i40e_status status;
12181 	bool min_valid, max_valid;
12182 	u32 max_bw, min_bw;
12183 
12184 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12185 					   &min_valid, &max_valid);
12186 
12187 	if (!status) {
12188 		if (min_valid)
12189 			pf->min_bw = min_bw;
12190 		if (max_valid)
12191 			pf->max_bw = max_bw;
12192 	}
12193 
12194 	return status;
12195 }
12196 
12197 /**
12198  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12199  * @pf: board private structure
12200  **/
12201 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
12202 {
12203 	struct i40e_aqc_configure_partition_bw_data bw_data;
12204 	i40e_status status;
12205 
12206 	memset(&bw_data, 0, sizeof(bw_data));
12207 
12208 	/* Set the valid bit for this PF */
12209 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12210 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12211 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12212 
12213 	/* Set the new bandwidths */
12214 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12215 
12216 	return status;
12217 }
12218 
12219 /**
12220  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12221  * @pf: board private structure
12222  **/
12223 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12224 {
12225 	/* Commit temporary BW setting to permanent NVM image */
12226 	enum i40e_admin_queue_err last_aq_status;
12227 	i40e_status ret;
12228 	u16 nvm_word;
12229 
12230 	if (pf->hw.partition_id != 1) {
12231 		dev_info(&pf->pdev->dev,
12232 			 "Commit BW only works on partition 1! This is partition %d",
12233 			 pf->hw.partition_id);
12234 		ret = I40E_NOT_SUPPORTED;
12235 		goto bw_commit_out;
12236 	}
12237 
12238 	/* Acquire NVM for read access */
12239 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12240 	last_aq_status = pf->hw.aq.asq_last_status;
12241 	if (ret) {
12242 		dev_info(&pf->pdev->dev,
12243 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
12244 			 i40e_stat_str(&pf->hw, ret),
12245 			 i40e_aq_str(&pf->hw, last_aq_status));
12246 		goto bw_commit_out;
12247 	}
12248 
12249 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12250 	ret = i40e_aq_read_nvm(&pf->hw,
12251 			       I40E_SR_NVM_CONTROL_WORD,
12252 			       0x10, sizeof(nvm_word), &nvm_word,
12253 			       false, NULL);
12254 	/* Save off last admin queue command status before releasing
12255 	 * the NVM
12256 	 */
12257 	last_aq_status = pf->hw.aq.asq_last_status;
12258 	i40e_release_nvm(&pf->hw);
12259 	if (ret) {
12260 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
12261 			 i40e_stat_str(&pf->hw, ret),
12262 			 i40e_aq_str(&pf->hw, last_aq_status));
12263 		goto bw_commit_out;
12264 	}
12265 
12266 	/* Wait a bit for NVM release to complete */
12267 	msleep(50);
12268 
12269 	/* Acquire NVM for write access */
12270 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12271 	last_aq_status = pf->hw.aq.asq_last_status;
12272 	if (ret) {
12273 		dev_info(&pf->pdev->dev,
12274 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
12275 			 i40e_stat_str(&pf->hw, ret),
12276 			 i40e_aq_str(&pf->hw, last_aq_status));
12277 		goto bw_commit_out;
12278 	}
12279 	/* Write it back out unchanged to initiate update NVM,
12280 	 * which will force a write of the shadow (alt) RAM to
12281 	 * the NVM - thus storing the bandwidth values permanently.
12282 	 */
12283 	ret = i40e_aq_update_nvm(&pf->hw,
12284 				 I40E_SR_NVM_CONTROL_WORD,
12285 				 0x10, sizeof(nvm_word),
12286 				 &nvm_word, true, 0, NULL);
12287 	/* Save off last admin queue command status before releasing
12288 	 * the NVM
12289 	 */
12290 	last_aq_status = pf->hw.aq.asq_last_status;
12291 	i40e_release_nvm(&pf->hw);
12292 	if (ret)
12293 		dev_info(&pf->pdev->dev,
12294 			 "BW settings NOT SAVED, err %s aq_err %s\n",
12295 			 i40e_stat_str(&pf->hw, ret),
12296 			 i40e_aq_str(&pf->hw, last_aq_status));
12297 bw_commit_out:
12298 
12299 	return ret;
12300 }
12301 
12302 /**
12303  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12304  * if total port shutdown feature is enabled for this PF
12305  * @pf: board private structure
12306  **/
12307 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12308 {
12309 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12310 #define I40E_FEATURES_ENABLE_PTR		0x2A
12311 #define I40E_CURRENT_SETTING_PTR		0x2B
12312 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12313 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12314 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12315 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12316 	i40e_status read_status = I40E_SUCCESS;
12317 	u16 sr_emp_sr_settings_ptr = 0;
12318 	u16 features_enable = 0;
12319 	u16 link_behavior = 0;
12320 	bool ret = false;
12321 
12322 	read_status = i40e_read_nvm_word(&pf->hw,
12323 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12324 					 &sr_emp_sr_settings_ptr);
12325 	if (read_status)
12326 		goto err_nvm;
12327 	read_status = i40e_read_nvm_word(&pf->hw,
12328 					 sr_emp_sr_settings_ptr +
12329 					 I40E_FEATURES_ENABLE_PTR,
12330 					 &features_enable);
12331 	if (read_status)
12332 		goto err_nvm;
12333 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12334 		read_status = i40e_read_nvm_module_data(&pf->hw,
12335 							I40E_SR_EMP_SR_SETTINGS_PTR,
12336 							I40E_CURRENT_SETTING_PTR,
12337 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12338 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12339 							&link_behavior);
12340 		if (read_status)
12341 			goto err_nvm;
12342 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12343 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12344 	}
12345 	return ret;
12346 
12347 err_nvm:
12348 	dev_warn(&pf->pdev->dev,
12349 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
12350 		 i40e_stat_str(&pf->hw, read_status));
12351 	return ret;
12352 }
12353 
12354 /**
12355  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12356  * @pf: board private structure to initialize
12357  *
12358  * i40e_sw_init initializes the Adapter private data structure.
12359  * Fields are initialized based on PCI device information and
12360  * OS network device settings (MTU size).
12361  **/
12362 static int i40e_sw_init(struct i40e_pf *pf)
12363 {
12364 	int err = 0;
12365 	int size;
12366 	u16 pow;
12367 
12368 	/* Set default capability flags */
12369 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12370 		    I40E_FLAG_MSI_ENABLED     |
12371 		    I40E_FLAG_MSIX_ENABLED;
12372 
12373 	/* Set default ITR */
12374 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12375 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12376 
12377 	/* Depending on PF configurations, it is possible that the RSS
12378 	 * maximum might end up larger than the available queues
12379 	 */
12380 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12381 	pf->alloc_rss_size = 1;
12382 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12383 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12384 				 pf->hw.func_caps.num_tx_qp);
12385 
12386 	/* find the next higher power-of-2 of num cpus */
12387 	pow = roundup_pow_of_two(num_online_cpus());
12388 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12389 
12390 	if (pf->hw.func_caps.rss) {
12391 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12392 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12393 					   num_online_cpus());
12394 	}
12395 
12396 	/* MFP mode enabled */
12397 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12398 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12399 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12400 		if (i40e_get_partition_bw_setting(pf)) {
12401 			dev_warn(&pf->pdev->dev,
12402 				 "Could not get partition bw settings\n");
12403 		} else {
12404 			dev_info(&pf->pdev->dev,
12405 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12406 				 pf->min_bw, pf->max_bw);
12407 
12408 			/* nudge the Tx scheduler */
12409 			i40e_set_partition_bw_setting(pf);
12410 		}
12411 	}
12412 
12413 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12414 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12415 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12416 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12417 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12418 		    pf->hw.num_partitions > 1)
12419 			dev_info(&pf->pdev->dev,
12420 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12421 		else
12422 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12423 		pf->fdir_pf_filter_count =
12424 				 pf->hw.func_caps.fd_filters_guaranteed;
12425 		pf->hw.fdir_shared_filter_count =
12426 				 pf->hw.func_caps.fd_filters_best_effort;
12427 	}
12428 
12429 	if (pf->hw.mac.type == I40E_MAC_X722) {
12430 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12431 				    I40E_HW_128_QP_RSS_CAPABLE |
12432 				    I40E_HW_ATR_EVICT_CAPABLE |
12433 				    I40E_HW_WB_ON_ITR_CAPABLE |
12434 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12435 				    I40E_HW_NO_PCI_LINK_CHECK |
12436 				    I40E_HW_USE_SET_LLDP_MIB |
12437 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12438 				    I40E_HW_PTP_L4_CAPABLE |
12439 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12440 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12441 
12442 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12443 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12444 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12445 			dev_warn(&pf->pdev->dev,
12446 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12447 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12448 		}
12449 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12450 		   ((pf->hw.aq.api_maj_ver == 1) &&
12451 		    (pf->hw.aq.api_min_ver > 4))) {
12452 		/* Supported in FW API version higher than 1.4 */
12453 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12454 	}
12455 
12456 	/* Enable HW ATR eviction if possible */
12457 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12458 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12459 
12460 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12461 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12462 	    (pf->hw.aq.fw_maj_ver < 4))) {
12463 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12464 		/* No DCB support  for FW < v4.33 */
12465 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12466 	}
12467 
12468 	/* Disable FW LLDP if FW < v4.3 */
12469 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12470 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12471 	    (pf->hw.aq.fw_maj_ver < 4)))
12472 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12473 
12474 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12475 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12476 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12477 	    (pf->hw.aq.fw_maj_ver >= 5)))
12478 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12479 
12480 	/* Enable PTP L4 if FW > v6.0 */
12481 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12482 	    pf->hw.aq.fw_maj_ver >= 6)
12483 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12484 
12485 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12486 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12487 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12488 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12489 	}
12490 
12491 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12492 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12493 		/* IWARP needs one extra vector for CQP just like MISC.*/
12494 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12495 	}
12496 	/* Stopping FW LLDP engine is supported on XL710 and X722
12497 	 * starting from FW versions determined in i40e_init_adminq.
12498 	 * Stopping the FW LLDP engine is not supported on XL710
12499 	 * if NPAR is functioning so unset this hw flag in this case.
12500 	 */
12501 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12502 	    pf->hw.func_caps.npar_enable &&
12503 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12504 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12505 
12506 #ifdef CONFIG_PCI_IOV
12507 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12508 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12509 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12510 		pf->num_req_vfs = min_t(int,
12511 					pf->hw.func_caps.num_vfs,
12512 					I40E_MAX_VF_COUNT);
12513 	}
12514 #endif /* CONFIG_PCI_IOV */
12515 	pf->eeprom_version = 0xDEAD;
12516 	pf->lan_veb = I40E_NO_VEB;
12517 	pf->lan_vsi = I40E_NO_VSI;
12518 
12519 	/* By default FW has this off for performance reasons */
12520 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12521 
12522 	/* set up queue assignment tracking */
12523 	size = sizeof(struct i40e_lump_tracking)
12524 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12525 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12526 	if (!pf->qp_pile) {
12527 		err = -ENOMEM;
12528 		goto sw_init_done;
12529 	}
12530 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12531 	pf->qp_pile->search_hint = 0;
12532 
12533 	pf->tx_timeout_recovery_level = 1;
12534 
12535 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12536 	    i40e_is_total_port_shutdown_enabled(pf)) {
12537 		/* Link down on close must be on when total port shutdown
12538 		 * is enabled for a given port
12539 		 */
12540 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12541 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12542 		dev_info(&pf->pdev->dev,
12543 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12544 	}
12545 	mutex_init(&pf->switch_mutex);
12546 
12547 sw_init_done:
12548 	return err;
12549 }
12550 
12551 /**
12552  * i40e_set_ntuple - set the ntuple feature flag and take action
12553  * @pf: board private structure to initialize
12554  * @features: the feature set that the stack is suggesting
12555  *
12556  * returns a bool to indicate if reset needs to happen
12557  **/
12558 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12559 {
12560 	bool need_reset = false;
12561 
12562 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12563 	 * the state changed, we need to reset.
12564 	 */
12565 	if (features & NETIF_F_NTUPLE) {
12566 		/* Enable filters and mark for reset */
12567 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12568 			need_reset = true;
12569 		/* enable FD_SB only if there is MSI-X vector and no cloud
12570 		 * filters exist
12571 		 */
12572 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12573 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12574 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12575 		}
12576 	} else {
12577 		/* turn off filters, mark for reset and clear SW filter list */
12578 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12579 			need_reset = true;
12580 			i40e_fdir_filter_exit(pf);
12581 		}
12582 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12583 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12584 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12585 
12586 		/* reset fd counters */
12587 		pf->fd_add_err = 0;
12588 		pf->fd_atr_cnt = 0;
12589 		/* if ATR was auto disabled it can be re-enabled. */
12590 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12591 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12592 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12593 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12594 	}
12595 	return need_reset;
12596 }
12597 
12598 /**
12599  * i40e_clear_rss_lut - clear the rx hash lookup table
12600  * @vsi: the VSI being configured
12601  **/
12602 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12603 {
12604 	struct i40e_pf *pf = vsi->back;
12605 	struct i40e_hw *hw = &pf->hw;
12606 	u16 vf_id = vsi->vf_id;
12607 	u8 i;
12608 
12609 	if (vsi->type == I40E_VSI_MAIN) {
12610 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12611 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12612 	} else if (vsi->type == I40E_VSI_SRIOV) {
12613 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12614 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12615 	} else {
12616 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12617 	}
12618 }
12619 
12620 /**
12621  * i40e_set_features - set the netdev feature flags
12622  * @netdev: ptr to the netdev being adjusted
12623  * @features: the feature set that the stack is suggesting
12624  * Note: expects to be called while under rtnl_lock()
12625  **/
12626 static int i40e_set_features(struct net_device *netdev,
12627 			     netdev_features_t features)
12628 {
12629 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12630 	struct i40e_vsi *vsi = np->vsi;
12631 	struct i40e_pf *pf = vsi->back;
12632 	bool need_reset;
12633 
12634 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12635 		i40e_pf_config_rss(pf);
12636 	else if (!(features & NETIF_F_RXHASH) &&
12637 		 netdev->features & NETIF_F_RXHASH)
12638 		i40e_clear_rss_lut(vsi);
12639 
12640 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12641 		i40e_vlan_stripping_enable(vsi);
12642 	else
12643 		i40e_vlan_stripping_disable(vsi);
12644 
12645 	if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12646 		dev_err(&pf->pdev->dev,
12647 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12648 		return -EINVAL;
12649 	}
12650 
12651 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12652 		i40e_del_all_macvlans(vsi);
12653 
12654 	need_reset = i40e_set_ntuple(pf, features);
12655 
12656 	if (need_reset)
12657 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12658 
12659 	return 0;
12660 }
12661 
12662 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12663 				    unsigned int table, unsigned int idx,
12664 				    struct udp_tunnel_info *ti)
12665 {
12666 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12667 	struct i40e_hw *hw = &np->vsi->back->hw;
12668 	u8 type, filter_index;
12669 	i40e_status ret;
12670 
12671 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12672 						   I40E_AQC_TUNNEL_TYPE_NGE;
12673 
12674 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12675 				     NULL);
12676 	if (ret) {
12677 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12678 			    i40e_stat_str(hw, ret),
12679 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12680 		return -EIO;
12681 	}
12682 
12683 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12684 	return 0;
12685 }
12686 
12687 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12688 				      unsigned int table, unsigned int idx,
12689 				      struct udp_tunnel_info *ti)
12690 {
12691 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12692 	struct i40e_hw *hw = &np->vsi->back->hw;
12693 	i40e_status ret;
12694 
12695 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12696 	if (ret) {
12697 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
12698 			    i40e_stat_str(hw, ret),
12699 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12700 		return -EIO;
12701 	}
12702 
12703 	return 0;
12704 }
12705 
12706 static int i40e_get_phys_port_id(struct net_device *netdev,
12707 				 struct netdev_phys_item_id *ppid)
12708 {
12709 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12710 	struct i40e_pf *pf = np->vsi->back;
12711 	struct i40e_hw *hw = &pf->hw;
12712 
12713 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
12714 		return -EOPNOTSUPP;
12715 
12716 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
12717 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
12718 
12719 	return 0;
12720 }
12721 
12722 /**
12723  * i40e_ndo_fdb_add - add an entry to the hardware database
12724  * @ndm: the input from the stack
12725  * @tb: pointer to array of nladdr (unused)
12726  * @dev: the net device pointer
12727  * @addr: the MAC address entry being added
12728  * @vid: VLAN ID
12729  * @flags: instructions from stack about fdb operation
12730  * @extack: netlink extended ack, unused currently
12731  */
12732 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
12733 			    struct net_device *dev,
12734 			    const unsigned char *addr, u16 vid,
12735 			    u16 flags,
12736 			    struct netlink_ext_ack *extack)
12737 {
12738 	struct i40e_netdev_priv *np = netdev_priv(dev);
12739 	struct i40e_pf *pf = np->vsi->back;
12740 	int err = 0;
12741 
12742 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
12743 		return -EOPNOTSUPP;
12744 
12745 	if (vid) {
12746 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
12747 		return -EINVAL;
12748 	}
12749 
12750 	/* Hardware does not support aging addresses so if a
12751 	 * ndm_state is given only allow permanent addresses
12752 	 */
12753 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
12754 		netdev_info(dev, "FDB only supports static addresses\n");
12755 		return -EINVAL;
12756 	}
12757 
12758 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
12759 		err = dev_uc_add_excl(dev, addr);
12760 	else if (is_multicast_ether_addr(addr))
12761 		err = dev_mc_add_excl(dev, addr);
12762 	else
12763 		err = -EINVAL;
12764 
12765 	/* Only return duplicate errors if NLM_F_EXCL is set */
12766 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
12767 		err = 0;
12768 
12769 	return err;
12770 }
12771 
12772 /**
12773  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
12774  * @dev: the netdev being configured
12775  * @nlh: RTNL message
12776  * @flags: bridge flags
12777  * @extack: netlink extended ack
12778  *
12779  * Inserts a new hardware bridge if not already created and
12780  * enables the bridging mode requested (VEB or VEPA). If the
12781  * hardware bridge has already been inserted and the request
12782  * is to change the mode then that requires a PF reset to
12783  * allow rebuild of the components with required hardware
12784  * bridge mode enabled.
12785  *
12786  * Note: expects to be called while under rtnl_lock()
12787  **/
12788 static int i40e_ndo_bridge_setlink(struct net_device *dev,
12789 				   struct nlmsghdr *nlh,
12790 				   u16 flags,
12791 				   struct netlink_ext_ack *extack)
12792 {
12793 	struct i40e_netdev_priv *np = netdev_priv(dev);
12794 	struct i40e_vsi *vsi = np->vsi;
12795 	struct i40e_pf *pf = vsi->back;
12796 	struct i40e_veb *veb = NULL;
12797 	struct nlattr *attr, *br_spec;
12798 	int i, rem;
12799 
12800 	/* Only for PF VSI for now */
12801 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12802 		return -EOPNOTSUPP;
12803 
12804 	/* Find the HW bridge for PF VSI */
12805 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12806 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12807 			veb = pf->veb[i];
12808 	}
12809 
12810 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
12811 
12812 	nla_for_each_nested(attr, br_spec, rem) {
12813 		__u16 mode;
12814 
12815 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
12816 			continue;
12817 
12818 		mode = nla_get_u16(attr);
12819 		if ((mode != BRIDGE_MODE_VEPA) &&
12820 		    (mode != BRIDGE_MODE_VEB))
12821 			return -EINVAL;
12822 
12823 		/* Insert a new HW bridge */
12824 		if (!veb) {
12825 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12826 					     vsi->tc_config.enabled_tc);
12827 			if (veb) {
12828 				veb->bridge_mode = mode;
12829 				i40e_config_bridge_mode(veb);
12830 			} else {
12831 				/* No Bridge HW offload available */
12832 				return -ENOENT;
12833 			}
12834 			break;
12835 		} else if (mode != veb->bridge_mode) {
12836 			/* Existing HW bridge but different mode needs reset */
12837 			veb->bridge_mode = mode;
12838 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
12839 			if (mode == BRIDGE_MODE_VEB)
12840 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
12841 			else
12842 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12843 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12844 			break;
12845 		}
12846 	}
12847 
12848 	return 0;
12849 }
12850 
12851 /**
12852  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
12853  * @skb: skb buff
12854  * @pid: process id
12855  * @seq: RTNL message seq #
12856  * @dev: the netdev being configured
12857  * @filter_mask: unused
12858  * @nlflags: netlink flags passed in
12859  *
12860  * Return the mode in which the hardware bridge is operating in
12861  * i.e VEB or VEPA.
12862  **/
12863 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
12864 				   struct net_device *dev,
12865 				   u32 __always_unused filter_mask,
12866 				   int nlflags)
12867 {
12868 	struct i40e_netdev_priv *np = netdev_priv(dev);
12869 	struct i40e_vsi *vsi = np->vsi;
12870 	struct i40e_pf *pf = vsi->back;
12871 	struct i40e_veb *veb = NULL;
12872 	int i;
12873 
12874 	/* Only for PF VSI for now */
12875 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12876 		return -EOPNOTSUPP;
12877 
12878 	/* Find the HW bridge for the PF VSI */
12879 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12880 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12881 			veb = pf->veb[i];
12882 	}
12883 
12884 	if (!veb)
12885 		return 0;
12886 
12887 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
12888 				       0, 0, nlflags, filter_mask, NULL);
12889 }
12890 
12891 /**
12892  * i40e_features_check - Validate encapsulated packet conforms to limits
12893  * @skb: skb buff
12894  * @dev: This physical port's netdev
12895  * @features: Offload features that the stack believes apply
12896  **/
12897 static netdev_features_t i40e_features_check(struct sk_buff *skb,
12898 					     struct net_device *dev,
12899 					     netdev_features_t features)
12900 {
12901 	size_t len;
12902 
12903 	/* No point in doing any of this if neither checksum nor GSO are
12904 	 * being requested for this frame.  We can rule out both by just
12905 	 * checking for CHECKSUM_PARTIAL
12906 	 */
12907 	if (skb->ip_summed != CHECKSUM_PARTIAL)
12908 		return features;
12909 
12910 	/* We cannot support GSO if the MSS is going to be less than
12911 	 * 64 bytes.  If it is then we need to drop support for GSO.
12912 	 */
12913 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
12914 		features &= ~NETIF_F_GSO_MASK;
12915 
12916 	/* MACLEN can support at most 63 words */
12917 	len = skb_network_header(skb) - skb->data;
12918 	if (len & ~(63 * 2))
12919 		goto out_err;
12920 
12921 	/* IPLEN and EIPLEN can support at most 127 dwords */
12922 	len = skb_transport_header(skb) - skb_network_header(skb);
12923 	if (len & ~(127 * 4))
12924 		goto out_err;
12925 
12926 	if (skb->encapsulation) {
12927 		/* L4TUNLEN can support 127 words */
12928 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
12929 		if (len & ~(127 * 2))
12930 			goto out_err;
12931 
12932 		/* IPLEN can support at most 127 dwords */
12933 		len = skb_inner_transport_header(skb) -
12934 		      skb_inner_network_header(skb);
12935 		if (len & ~(127 * 4))
12936 			goto out_err;
12937 	}
12938 
12939 	/* No need to validate L4LEN as TCP is the only protocol with a
12940 	 * a flexible value and we support all possible values supported
12941 	 * by TCP, which is at most 15 dwords
12942 	 */
12943 
12944 	return features;
12945 out_err:
12946 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
12947 }
12948 
12949 /**
12950  * i40e_xdp_setup - add/remove an XDP program
12951  * @vsi: VSI to changed
12952  * @prog: XDP program
12953  * @extack: netlink extended ack
12954  **/
12955 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
12956 			  struct netlink_ext_ack *extack)
12957 {
12958 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
12959 	struct i40e_pf *pf = vsi->back;
12960 	struct bpf_prog *old_prog;
12961 	bool need_reset;
12962 	int i;
12963 
12964 	/* Don't allow frames that span over multiple buffers */
12965 	if (frame_size > vsi->rx_buf_len) {
12966 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
12967 		return -EINVAL;
12968 	}
12969 
12970 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
12971 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
12972 
12973 	if (need_reset)
12974 		i40e_prep_for_reset(pf);
12975 
12976 	old_prog = xchg(&vsi->xdp_prog, prog);
12977 
12978 	if (need_reset) {
12979 		if (!prog)
12980 			/* Wait until ndo_xsk_wakeup completes. */
12981 			synchronize_rcu();
12982 		i40e_reset_and_rebuild(pf, true, true);
12983 	}
12984 
12985 	for (i = 0; i < vsi->num_queue_pairs; i++)
12986 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
12987 
12988 	if (old_prog)
12989 		bpf_prog_put(old_prog);
12990 
12991 	/* Kick start the NAPI context if there is an AF_XDP socket open
12992 	 * on that queue id. This so that receiving will start.
12993 	 */
12994 	if (need_reset && prog)
12995 		for (i = 0; i < vsi->num_queue_pairs; i++)
12996 			if (vsi->xdp_rings[i]->xsk_pool)
12997 				(void)i40e_xsk_wakeup(vsi->netdev, i,
12998 						      XDP_WAKEUP_RX);
12999 
13000 	return 0;
13001 }
13002 
13003 /**
13004  * i40e_enter_busy_conf - Enters busy config state
13005  * @vsi: vsi
13006  *
13007  * Returns 0 on success, <0 for failure.
13008  **/
13009 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13010 {
13011 	struct i40e_pf *pf = vsi->back;
13012 	int timeout = 50;
13013 
13014 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13015 		timeout--;
13016 		if (!timeout)
13017 			return -EBUSY;
13018 		usleep_range(1000, 2000);
13019 	}
13020 
13021 	return 0;
13022 }
13023 
13024 /**
13025  * i40e_exit_busy_conf - Exits busy config state
13026  * @vsi: vsi
13027  **/
13028 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13029 {
13030 	struct i40e_pf *pf = vsi->back;
13031 
13032 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13033 }
13034 
13035 /**
13036  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13037  * @vsi: vsi
13038  * @queue_pair: queue pair
13039  **/
13040 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13041 {
13042 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13043 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13044 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13045 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13046 	if (i40e_enabled_xdp_vsi(vsi)) {
13047 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13048 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13049 	}
13050 }
13051 
13052 /**
13053  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13054  * @vsi: vsi
13055  * @queue_pair: queue pair
13056  **/
13057 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13058 {
13059 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13060 	if (i40e_enabled_xdp_vsi(vsi)) {
13061 		/* Make sure that in-progress ndo_xdp_xmit calls are
13062 		 * completed.
13063 		 */
13064 		synchronize_rcu();
13065 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13066 	}
13067 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13068 }
13069 
13070 /**
13071  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13072  * @vsi: vsi
13073  * @queue_pair: queue pair
13074  * @enable: true for enable, false for disable
13075  **/
13076 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13077 					bool enable)
13078 {
13079 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13080 	struct i40e_q_vector *q_vector = rxr->q_vector;
13081 
13082 	if (!vsi->netdev)
13083 		return;
13084 
13085 	/* All rings in a qp belong to the same qvector. */
13086 	if (q_vector->rx.ring || q_vector->tx.ring) {
13087 		if (enable)
13088 			napi_enable(&q_vector->napi);
13089 		else
13090 			napi_disable(&q_vector->napi);
13091 	}
13092 }
13093 
13094 /**
13095  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13096  * @vsi: vsi
13097  * @queue_pair: queue pair
13098  * @enable: true for enable, false for disable
13099  *
13100  * Returns 0 on success, <0 on failure.
13101  **/
13102 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13103 					bool enable)
13104 {
13105 	struct i40e_pf *pf = vsi->back;
13106 	int pf_q, ret = 0;
13107 
13108 	pf_q = vsi->base_queue + queue_pair;
13109 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13110 				     false /*is xdp*/, enable);
13111 	if (ret) {
13112 		dev_info(&pf->pdev->dev,
13113 			 "VSI seid %d Tx ring %d %sable timeout\n",
13114 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13115 		return ret;
13116 	}
13117 
13118 	i40e_control_rx_q(pf, pf_q, enable);
13119 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13120 	if (ret) {
13121 		dev_info(&pf->pdev->dev,
13122 			 "VSI seid %d Rx ring %d %sable timeout\n",
13123 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13124 		return ret;
13125 	}
13126 
13127 	/* Due to HW errata, on Rx disable only, the register can
13128 	 * indicate done before it really is. Needs 50ms to be sure
13129 	 */
13130 	if (!enable)
13131 		mdelay(50);
13132 
13133 	if (!i40e_enabled_xdp_vsi(vsi))
13134 		return ret;
13135 
13136 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13137 				     pf_q + vsi->alloc_queue_pairs,
13138 				     true /*is xdp*/, enable);
13139 	if (ret) {
13140 		dev_info(&pf->pdev->dev,
13141 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13142 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13143 	}
13144 
13145 	return ret;
13146 }
13147 
13148 /**
13149  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13150  * @vsi: vsi
13151  * @queue_pair: queue_pair
13152  **/
13153 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13154 {
13155 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13156 	struct i40e_pf *pf = vsi->back;
13157 	struct i40e_hw *hw = &pf->hw;
13158 
13159 	/* All rings in a qp belong to the same qvector. */
13160 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13161 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13162 	else
13163 		i40e_irq_dynamic_enable_icr0(pf);
13164 
13165 	i40e_flush(hw);
13166 }
13167 
13168 /**
13169  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13170  * @vsi: vsi
13171  * @queue_pair: queue_pair
13172  **/
13173 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13174 {
13175 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13176 	struct i40e_pf *pf = vsi->back;
13177 	struct i40e_hw *hw = &pf->hw;
13178 
13179 	/* For simplicity, instead of removing the qp interrupt causes
13180 	 * from the interrupt linked list, we simply disable the interrupt, and
13181 	 * leave the list intact.
13182 	 *
13183 	 * All rings in a qp belong to the same qvector.
13184 	 */
13185 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13186 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13187 
13188 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13189 		i40e_flush(hw);
13190 		synchronize_irq(pf->msix_entries[intpf].vector);
13191 	} else {
13192 		/* Legacy and MSI mode - this stops all interrupt handling */
13193 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13194 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13195 		i40e_flush(hw);
13196 		synchronize_irq(pf->pdev->irq);
13197 	}
13198 }
13199 
13200 /**
13201  * i40e_queue_pair_disable - Disables a queue pair
13202  * @vsi: vsi
13203  * @queue_pair: queue pair
13204  *
13205  * Returns 0 on success, <0 on failure.
13206  **/
13207 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13208 {
13209 	int err;
13210 
13211 	err = i40e_enter_busy_conf(vsi);
13212 	if (err)
13213 		return err;
13214 
13215 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13216 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13217 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13218 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13219 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13220 
13221 	return err;
13222 }
13223 
13224 /**
13225  * i40e_queue_pair_enable - Enables a queue pair
13226  * @vsi: vsi
13227  * @queue_pair: queue pair
13228  *
13229  * Returns 0 on success, <0 on failure.
13230  **/
13231 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13232 {
13233 	int err;
13234 
13235 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13236 	if (err)
13237 		return err;
13238 
13239 	if (i40e_enabled_xdp_vsi(vsi)) {
13240 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13241 		if (err)
13242 			return err;
13243 	}
13244 
13245 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13246 	if (err)
13247 		return err;
13248 
13249 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13250 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13251 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13252 
13253 	i40e_exit_busy_conf(vsi);
13254 
13255 	return err;
13256 }
13257 
13258 /**
13259  * i40e_xdp - implements ndo_bpf for i40e
13260  * @dev: netdevice
13261  * @xdp: XDP command
13262  **/
13263 static int i40e_xdp(struct net_device *dev,
13264 		    struct netdev_bpf *xdp)
13265 {
13266 	struct i40e_netdev_priv *np = netdev_priv(dev);
13267 	struct i40e_vsi *vsi = np->vsi;
13268 
13269 	if (vsi->type != I40E_VSI_MAIN)
13270 		return -EINVAL;
13271 
13272 	switch (xdp->command) {
13273 	case XDP_SETUP_PROG:
13274 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13275 	case XDP_SETUP_XSK_POOL:
13276 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13277 					   xdp->xsk.queue_id);
13278 	default:
13279 		return -EINVAL;
13280 	}
13281 }
13282 
13283 static const struct net_device_ops i40e_netdev_ops = {
13284 	.ndo_open		= i40e_open,
13285 	.ndo_stop		= i40e_close,
13286 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13287 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13288 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13289 	.ndo_validate_addr	= eth_validate_addr,
13290 	.ndo_set_mac_address	= i40e_set_mac,
13291 	.ndo_change_mtu		= i40e_change_mtu,
13292 	.ndo_do_ioctl		= i40e_ioctl,
13293 	.ndo_tx_timeout		= i40e_tx_timeout,
13294 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13295 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13296 #ifdef CONFIG_NET_POLL_CONTROLLER
13297 	.ndo_poll_controller	= i40e_netpoll,
13298 #endif
13299 	.ndo_setup_tc		= __i40e_setup_tc,
13300 	.ndo_set_features	= i40e_set_features,
13301 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13302 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13303 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13304 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13305 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13306 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13307 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13308 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13309 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13310 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13311 	.ndo_features_check	= i40e_features_check,
13312 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13313 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13314 	.ndo_bpf		= i40e_xdp,
13315 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13316 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13317 	.ndo_dfwd_add_station	= i40e_fwd_add,
13318 	.ndo_dfwd_del_station	= i40e_fwd_del,
13319 };
13320 
13321 /**
13322  * i40e_config_netdev - Setup the netdev flags
13323  * @vsi: the VSI being configured
13324  *
13325  * Returns 0 on success, negative value on failure
13326  **/
13327 static int i40e_config_netdev(struct i40e_vsi *vsi)
13328 {
13329 	struct i40e_pf *pf = vsi->back;
13330 	struct i40e_hw *hw = &pf->hw;
13331 	struct i40e_netdev_priv *np;
13332 	struct net_device *netdev;
13333 	u8 broadcast[ETH_ALEN];
13334 	u8 mac_addr[ETH_ALEN];
13335 	int etherdev_size;
13336 	netdev_features_t hw_enc_features;
13337 	netdev_features_t hw_features;
13338 
13339 	etherdev_size = sizeof(struct i40e_netdev_priv);
13340 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13341 	if (!netdev)
13342 		return -ENOMEM;
13343 
13344 	vsi->netdev = netdev;
13345 	np = netdev_priv(netdev);
13346 	np->vsi = vsi;
13347 
13348 	hw_enc_features = NETIF_F_SG			|
13349 			  NETIF_F_IP_CSUM		|
13350 			  NETIF_F_IPV6_CSUM		|
13351 			  NETIF_F_HIGHDMA		|
13352 			  NETIF_F_SOFT_FEATURES		|
13353 			  NETIF_F_TSO			|
13354 			  NETIF_F_TSO_ECN		|
13355 			  NETIF_F_TSO6			|
13356 			  NETIF_F_GSO_GRE		|
13357 			  NETIF_F_GSO_GRE_CSUM		|
13358 			  NETIF_F_GSO_PARTIAL		|
13359 			  NETIF_F_GSO_IPXIP4		|
13360 			  NETIF_F_GSO_IPXIP6		|
13361 			  NETIF_F_GSO_UDP_TUNNEL	|
13362 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13363 			  NETIF_F_GSO_UDP_L4		|
13364 			  NETIF_F_SCTP_CRC		|
13365 			  NETIF_F_RXHASH		|
13366 			  NETIF_F_RXCSUM		|
13367 			  0;
13368 
13369 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13370 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13371 
13372 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13373 
13374 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13375 
13376 	netdev->hw_enc_features |= hw_enc_features;
13377 
13378 	/* record features VLANs can make use of */
13379 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13380 
13381 	/* enable macvlan offloads */
13382 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13383 
13384 	hw_features = hw_enc_features		|
13385 		      NETIF_F_HW_VLAN_CTAG_TX	|
13386 		      NETIF_F_HW_VLAN_CTAG_RX;
13387 
13388 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13389 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13390 
13391 	netdev->hw_features |= hw_features;
13392 
13393 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13394 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13395 
13396 	if (vsi->type == I40E_VSI_MAIN) {
13397 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13398 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13399 		/* The following steps are necessary for two reasons. First,
13400 		 * some older NVM configurations load a default MAC-VLAN
13401 		 * filter that will accept any tagged packet, and we want to
13402 		 * replace this with a normal filter. Additionally, it is
13403 		 * possible our MAC address was provided by the platform using
13404 		 * Open Firmware or similar.
13405 		 *
13406 		 * Thus, we need to remove the default filter and install one
13407 		 * specific to the MAC address.
13408 		 */
13409 		i40e_rm_default_mac_filter(vsi, mac_addr);
13410 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13411 		i40e_add_mac_filter(vsi, mac_addr);
13412 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13413 	} else {
13414 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13415 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13416 		 * the end, which is 4 bytes long, so force truncation of the
13417 		 * original name by IFNAMSIZ - 4
13418 		 */
13419 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13420 			 IFNAMSIZ - 4,
13421 			 pf->vsi[pf->lan_vsi]->netdev->name);
13422 		eth_random_addr(mac_addr);
13423 
13424 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13425 		i40e_add_mac_filter(vsi, mac_addr);
13426 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13427 	}
13428 
13429 	/* Add the broadcast filter so that we initially will receive
13430 	 * broadcast packets. Note that when a new VLAN is first added the
13431 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13432 	 * specific filters as part of transitioning into "vlan" operation.
13433 	 * When more VLANs are added, the driver will copy each existing MAC
13434 	 * filter and add it for the new VLAN.
13435 	 *
13436 	 * Broadcast filters are handled specially by
13437 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13438 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13439 	 * filter. The subtask will update the correct broadcast promiscuous
13440 	 * bits as VLANs become active or inactive.
13441 	 */
13442 	eth_broadcast_addr(broadcast);
13443 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13444 	i40e_add_mac_filter(vsi, broadcast);
13445 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13446 
13447 	ether_addr_copy(netdev->dev_addr, mac_addr);
13448 	ether_addr_copy(netdev->perm_addr, mac_addr);
13449 
13450 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13451 	netdev->neigh_priv_len = sizeof(u32) * 4;
13452 
13453 	netdev->priv_flags |= IFF_UNICAST_FLT;
13454 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13455 	/* Setup netdev TC information */
13456 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13457 
13458 	netdev->netdev_ops = &i40e_netdev_ops;
13459 	netdev->watchdog_timeo = 5 * HZ;
13460 	i40e_set_ethtool_ops(netdev);
13461 
13462 	/* MTU range: 68 - 9706 */
13463 	netdev->min_mtu = ETH_MIN_MTU;
13464 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13465 
13466 	return 0;
13467 }
13468 
13469 /**
13470  * i40e_vsi_delete - Delete a VSI from the switch
13471  * @vsi: the VSI being removed
13472  *
13473  * Returns 0 on success, negative value on failure
13474  **/
13475 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13476 {
13477 	/* remove default VSI is not allowed */
13478 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13479 		return;
13480 
13481 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13482 }
13483 
13484 /**
13485  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13486  * @vsi: the VSI being queried
13487  *
13488  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13489  **/
13490 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13491 {
13492 	struct i40e_veb *veb;
13493 	struct i40e_pf *pf = vsi->back;
13494 
13495 	/* Uplink is not a bridge so default to VEB */
13496 	if (vsi->veb_idx >= I40E_MAX_VEB)
13497 		return 1;
13498 
13499 	veb = pf->veb[vsi->veb_idx];
13500 	if (!veb) {
13501 		dev_info(&pf->pdev->dev,
13502 			 "There is no veb associated with the bridge\n");
13503 		return -ENOENT;
13504 	}
13505 
13506 	/* Uplink is a bridge in VEPA mode */
13507 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13508 		return 0;
13509 	} else {
13510 		/* Uplink is a bridge in VEB mode */
13511 		return 1;
13512 	}
13513 
13514 	/* VEPA is now default bridge, so return 0 */
13515 	return 0;
13516 }
13517 
13518 /**
13519  * i40e_add_vsi - Add a VSI to the switch
13520  * @vsi: the VSI being configured
13521  *
13522  * This initializes a VSI context depending on the VSI type to be added and
13523  * passes it down to the add_vsi aq command.
13524  **/
13525 static int i40e_add_vsi(struct i40e_vsi *vsi)
13526 {
13527 	int ret = -ENODEV;
13528 	struct i40e_pf *pf = vsi->back;
13529 	struct i40e_hw *hw = &pf->hw;
13530 	struct i40e_vsi_context ctxt;
13531 	struct i40e_mac_filter *f;
13532 	struct hlist_node *h;
13533 	int bkt;
13534 
13535 	u8 enabled_tc = 0x1; /* TC0 enabled */
13536 	int f_count = 0;
13537 
13538 	memset(&ctxt, 0, sizeof(ctxt));
13539 	switch (vsi->type) {
13540 	case I40E_VSI_MAIN:
13541 		/* The PF's main VSI is already setup as part of the
13542 		 * device initialization, so we'll not bother with
13543 		 * the add_vsi call, but we will retrieve the current
13544 		 * VSI context.
13545 		 */
13546 		ctxt.seid = pf->main_vsi_seid;
13547 		ctxt.pf_num = pf->hw.pf_id;
13548 		ctxt.vf_num = 0;
13549 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13550 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13551 		if (ret) {
13552 			dev_info(&pf->pdev->dev,
13553 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13554 				 i40e_stat_str(&pf->hw, ret),
13555 				 i40e_aq_str(&pf->hw,
13556 					     pf->hw.aq.asq_last_status));
13557 			return -ENOENT;
13558 		}
13559 		vsi->info = ctxt.info;
13560 		vsi->info.valid_sections = 0;
13561 
13562 		vsi->seid = ctxt.seid;
13563 		vsi->id = ctxt.vsi_number;
13564 
13565 		enabled_tc = i40e_pf_get_tc_map(pf);
13566 
13567 		/* Source pruning is enabled by default, so the flag is
13568 		 * negative logic - if it's set, we need to fiddle with
13569 		 * the VSI to disable source pruning.
13570 		 */
13571 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13572 			memset(&ctxt, 0, sizeof(ctxt));
13573 			ctxt.seid = pf->main_vsi_seid;
13574 			ctxt.pf_num = pf->hw.pf_id;
13575 			ctxt.vf_num = 0;
13576 			ctxt.info.valid_sections |=
13577 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13578 			ctxt.info.switch_id =
13579 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13580 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13581 			if (ret) {
13582 				dev_info(&pf->pdev->dev,
13583 					 "update vsi failed, err %s aq_err %s\n",
13584 					 i40e_stat_str(&pf->hw, ret),
13585 					 i40e_aq_str(&pf->hw,
13586 						     pf->hw.aq.asq_last_status));
13587 				ret = -ENOENT;
13588 				goto err;
13589 			}
13590 		}
13591 
13592 		/* MFP mode setup queue map and update VSI */
13593 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13594 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13595 			memset(&ctxt, 0, sizeof(ctxt));
13596 			ctxt.seid = pf->main_vsi_seid;
13597 			ctxt.pf_num = pf->hw.pf_id;
13598 			ctxt.vf_num = 0;
13599 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13600 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13601 			if (ret) {
13602 				dev_info(&pf->pdev->dev,
13603 					 "update vsi failed, err %s aq_err %s\n",
13604 					 i40e_stat_str(&pf->hw, ret),
13605 					 i40e_aq_str(&pf->hw,
13606 						    pf->hw.aq.asq_last_status));
13607 				ret = -ENOENT;
13608 				goto err;
13609 			}
13610 			/* update the local VSI info queue map */
13611 			i40e_vsi_update_queue_map(vsi, &ctxt);
13612 			vsi->info.valid_sections = 0;
13613 		} else {
13614 			/* Default/Main VSI is only enabled for TC0
13615 			 * reconfigure it to enable all TCs that are
13616 			 * available on the port in SFP mode.
13617 			 * For MFP case the iSCSI PF would use this
13618 			 * flow to enable LAN+iSCSI TC.
13619 			 */
13620 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13621 			if (ret) {
13622 				/* Single TC condition is not fatal,
13623 				 * message and continue
13624 				 */
13625 				dev_info(&pf->pdev->dev,
13626 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13627 					 enabled_tc,
13628 					 i40e_stat_str(&pf->hw, ret),
13629 					 i40e_aq_str(&pf->hw,
13630 						    pf->hw.aq.asq_last_status));
13631 			}
13632 		}
13633 		break;
13634 
13635 	case I40E_VSI_FDIR:
13636 		ctxt.pf_num = hw->pf_id;
13637 		ctxt.vf_num = 0;
13638 		ctxt.uplink_seid = vsi->uplink_seid;
13639 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13640 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13641 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13642 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13643 			ctxt.info.valid_sections |=
13644 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13645 			ctxt.info.switch_id =
13646 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13647 		}
13648 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13649 		break;
13650 
13651 	case I40E_VSI_VMDQ2:
13652 		ctxt.pf_num = hw->pf_id;
13653 		ctxt.vf_num = 0;
13654 		ctxt.uplink_seid = vsi->uplink_seid;
13655 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13656 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13657 
13658 		/* This VSI is connected to VEB so the switch_id
13659 		 * should be set to zero by default.
13660 		 */
13661 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13662 			ctxt.info.valid_sections |=
13663 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13664 			ctxt.info.switch_id =
13665 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13666 		}
13667 
13668 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13669 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13670 		break;
13671 
13672 	case I40E_VSI_SRIOV:
13673 		ctxt.pf_num = hw->pf_id;
13674 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13675 		ctxt.uplink_seid = vsi->uplink_seid;
13676 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13677 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13678 
13679 		/* This VSI is connected to VEB so the switch_id
13680 		 * should be set to zero by default.
13681 		 */
13682 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13683 			ctxt.info.valid_sections |=
13684 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13685 			ctxt.info.switch_id =
13686 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13687 		}
13688 
13689 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
13690 			ctxt.info.valid_sections |=
13691 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
13692 			ctxt.info.queueing_opt_flags |=
13693 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
13694 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
13695 		}
13696 
13697 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
13698 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
13699 		if (pf->vf[vsi->vf_id].spoofchk) {
13700 			ctxt.info.valid_sections |=
13701 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
13702 			ctxt.info.sec_flags |=
13703 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
13704 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
13705 		}
13706 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13707 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13708 		break;
13709 
13710 	case I40E_VSI_IWARP:
13711 		/* send down message to iWARP */
13712 		break;
13713 
13714 	default:
13715 		return -ENODEV;
13716 	}
13717 
13718 	if (vsi->type != I40E_VSI_MAIN) {
13719 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
13720 		if (ret) {
13721 			dev_info(&vsi->back->pdev->dev,
13722 				 "add vsi failed, err %s aq_err %s\n",
13723 				 i40e_stat_str(&pf->hw, ret),
13724 				 i40e_aq_str(&pf->hw,
13725 					     pf->hw.aq.asq_last_status));
13726 			ret = -ENOENT;
13727 			goto err;
13728 		}
13729 		vsi->info = ctxt.info;
13730 		vsi->info.valid_sections = 0;
13731 		vsi->seid = ctxt.seid;
13732 		vsi->id = ctxt.vsi_number;
13733 	}
13734 
13735 	vsi->active_filters = 0;
13736 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
13737 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13738 	/* If macvlan filters already exist, force them to get loaded */
13739 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
13740 		f->state = I40E_FILTER_NEW;
13741 		f_count++;
13742 	}
13743 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13744 
13745 	if (f_count) {
13746 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
13747 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
13748 	}
13749 
13750 	/* Update VSI BW information */
13751 	ret = i40e_vsi_get_bw_info(vsi);
13752 	if (ret) {
13753 		dev_info(&pf->pdev->dev,
13754 			 "couldn't get vsi bw info, err %s aq_err %s\n",
13755 			 i40e_stat_str(&pf->hw, ret),
13756 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13757 		/* VSI is already added so not tearing that up */
13758 		ret = 0;
13759 	}
13760 
13761 err:
13762 	return ret;
13763 }
13764 
13765 /**
13766  * i40e_vsi_release - Delete a VSI and free its resources
13767  * @vsi: the VSI being removed
13768  *
13769  * Returns 0 on success or < 0 on error
13770  **/
13771 int i40e_vsi_release(struct i40e_vsi *vsi)
13772 {
13773 	struct i40e_mac_filter *f;
13774 	struct hlist_node *h;
13775 	struct i40e_veb *veb = NULL;
13776 	struct i40e_pf *pf;
13777 	u16 uplink_seid;
13778 	int i, n, bkt;
13779 
13780 	pf = vsi->back;
13781 
13782 	/* release of a VEB-owner or last VSI is not allowed */
13783 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
13784 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
13785 			 vsi->seid, vsi->uplink_seid);
13786 		return -ENODEV;
13787 	}
13788 	if (vsi == pf->vsi[pf->lan_vsi] &&
13789 	    !test_bit(__I40E_DOWN, pf->state)) {
13790 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
13791 		return -ENODEV;
13792 	}
13793 
13794 	uplink_seid = vsi->uplink_seid;
13795 	if (vsi->type != I40E_VSI_SRIOV) {
13796 		if (vsi->netdev_registered) {
13797 			vsi->netdev_registered = false;
13798 			if (vsi->netdev) {
13799 				/* results in a call to i40e_close() */
13800 				unregister_netdev(vsi->netdev);
13801 			}
13802 		} else {
13803 			i40e_vsi_close(vsi);
13804 		}
13805 		i40e_vsi_disable_irq(vsi);
13806 	}
13807 
13808 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13809 
13810 	/* clear the sync flag on all filters */
13811 	if (vsi->netdev) {
13812 		__dev_uc_unsync(vsi->netdev, NULL);
13813 		__dev_mc_unsync(vsi->netdev, NULL);
13814 	}
13815 
13816 	/* make sure any remaining filters are marked for deletion */
13817 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
13818 		__i40e_del_filter(vsi, f);
13819 
13820 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13821 
13822 	i40e_sync_vsi_filters(vsi);
13823 
13824 	i40e_vsi_delete(vsi);
13825 	i40e_vsi_free_q_vectors(vsi);
13826 	if (vsi->netdev) {
13827 		free_netdev(vsi->netdev);
13828 		vsi->netdev = NULL;
13829 	}
13830 	i40e_vsi_clear_rings(vsi);
13831 	i40e_vsi_clear(vsi);
13832 
13833 	/* If this was the last thing on the VEB, except for the
13834 	 * controlling VSI, remove the VEB, which puts the controlling
13835 	 * VSI onto the next level down in the switch.
13836 	 *
13837 	 * Well, okay, there's one more exception here: don't remove
13838 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
13839 	 * from up the network stack.
13840 	 */
13841 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
13842 		if (pf->vsi[i] &&
13843 		    pf->vsi[i]->uplink_seid == uplink_seid &&
13844 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13845 			n++;      /* count the VSIs */
13846 		}
13847 	}
13848 	for (i = 0; i < I40E_MAX_VEB; i++) {
13849 		if (!pf->veb[i])
13850 			continue;
13851 		if (pf->veb[i]->uplink_seid == uplink_seid)
13852 			n++;     /* count the VEBs */
13853 		if (pf->veb[i]->seid == uplink_seid)
13854 			veb = pf->veb[i];
13855 	}
13856 	if (n == 0 && veb && veb->uplink_seid != 0)
13857 		i40e_veb_release(veb);
13858 
13859 	return 0;
13860 }
13861 
13862 /**
13863  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
13864  * @vsi: ptr to the VSI
13865  *
13866  * This should only be called after i40e_vsi_mem_alloc() which allocates the
13867  * corresponding SW VSI structure and initializes num_queue_pairs for the
13868  * newly allocated VSI.
13869  *
13870  * Returns 0 on success or negative on failure
13871  **/
13872 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
13873 {
13874 	int ret = -ENOENT;
13875 	struct i40e_pf *pf = vsi->back;
13876 
13877 	if (vsi->q_vectors[0]) {
13878 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
13879 			 vsi->seid);
13880 		return -EEXIST;
13881 	}
13882 
13883 	if (vsi->base_vector) {
13884 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
13885 			 vsi->seid, vsi->base_vector);
13886 		return -EEXIST;
13887 	}
13888 
13889 	ret = i40e_vsi_alloc_q_vectors(vsi);
13890 	if (ret) {
13891 		dev_info(&pf->pdev->dev,
13892 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
13893 			 vsi->num_q_vectors, vsi->seid, ret);
13894 		vsi->num_q_vectors = 0;
13895 		goto vector_setup_out;
13896 	}
13897 
13898 	/* In Legacy mode, we do not have to get any other vector since we
13899 	 * piggyback on the misc/ICR0 for queue interrupts.
13900 	*/
13901 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
13902 		return ret;
13903 	if (vsi->num_q_vectors)
13904 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
13905 						 vsi->num_q_vectors, vsi->idx);
13906 	if (vsi->base_vector < 0) {
13907 		dev_info(&pf->pdev->dev,
13908 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
13909 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
13910 		i40e_vsi_free_q_vectors(vsi);
13911 		ret = -ENOENT;
13912 		goto vector_setup_out;
13913 	}
13914 
13915 vector_setup_out:
13916 	return ret;
13917 }
13918 
13919 /**
13920  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
13921  * @vsi: pointer to the vsi.
13922  *
13923  * This re-allocates a vsi's queue resources.
13924  *
13925  * Returns pointer to the successfully allocated and configured VSI sw struct
13926  * on success, otherwise returns NULL on failure.
13927  **/
13928 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
13929 {
13930 	u16 alloc_queue_pairs;
13931 	struct i40e_pf *pf;
13932 	u8 enabled_tc;
13933 	int ret;
13934 
13935 	if (!vsi)
13936 		return NULL;
13937 
13938 	pf = vsi->back;
13939 
13940 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
13941 	i40e_vsi_clear_rings(vsi);
13942 
13943 	i40e_vsi_free_arrays(vsi, false);
13944 	i40e_set_num_rings_in_vsi(vsi);
13945 	ret = i40e_vsi_alloc_arrays(vsi, false);
13946 	if (ret)
13947 		goto err_vsi;
13948 
13949 	alloc_queue_pairs = vsi->alloc_queue_pairs *
13950 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
13951 
13952 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
13953 	if (ret < 0) {
13954 		dev_info(&pf->pdev->dev,
13955 			 "failed to get tracking for %d queues for VSI %d err %d\n",
13956 			 alloc_queue_pairs, vsi->seid, ret);
13957 		goto err_vsi;
13958 	}
13959 	vsi->base_queue = ret;
13960 
13961 	/* Update the FW view of the VSI. Force a reset of TC and queue
13962 	 * layout configurations.
13963 	 */
13964 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
13965 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
13966 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
13967 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
13968 	if (vsi->type == I40E_VSI_MAIN)
13969 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
13970 
13971 	/* assign it some queues */
13972 	ret = i40e_alloc_rings(vsi);
13973 	if (ret)
13974 		goto err_rings;
13975 
13976 	/* map all of the rings to the q_vectors */
13977 	i40e_vsi_map_rings_to_vectors(vsi);
13978 	return vsi;
13979 
13980 err_rings:
13981 	i40e_vsi_free_q_vectors(vsi);
13982 	if (vsi->netdev_registered) {
13983 		vsi->netdev_registered = false;
13984 		unregister_netdev(vsi->netdev);
13985 		free_netdev(vsi->netdev);
13986 		vsi->netdev = NULL;
13987 	}
13988 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
13989 err_vsi:
13990 	i40e_vsi_clear(vsi);
13991 	return NULL;
13992 }
13993 
13994 /**
13995  * i40e_vsi_setup - Set up a VSI by a given type
13996  * @pf: board private structure
13997  * @type: VSI type
13998  * @uplink_seid: the switch element to link to
13999  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14000  *
14001  * This allocates the sw VSI structure and its queue resources, then add a VSI
14002  * to the identified VEB.
14003  *
14004  * Returns pointer to the successfully allocated and configure VSI sw struct on
14005  * success, otherwise returns NULL on failure.
14006  **/
14007 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14008 				u16 uplink_seid, u32 param1)
14009 {
14010 	struct i40e_vsi *vsi = NULL;
14011 	struct i40e_veb *veb = NULL;
14012 	u16 alloc_queue_pairs;
14013 	int ret, i;
14014 	int v_idx;
14015 
14016 	/* The requested uplink_seid must be either
14017 	 *     - the PF's port seid
14018 	 *              no VEB is needed because this is the PF
14019 	 *              or this is a Flow Director special case VSI
14020 	 *     - seid of an existing VEB
14021 	 *     - seid of a VSI that owns an existing VEB
14022 	 *     - seid of a VSI that doesn't own a VEB
14023 	 *              a new VEB is created and the VSI becomes the owner
14024 	 *     - seid of the PF VSI, which is what creates the first VEB
14025 	 *              this is a special case of the previous
14026 	 *
14027 	 * Find which uplink_seid we were given and create a new VEB if needed
14028 	 */
14029 	for (i = 0; i < I40E_MAX_VEB; i++) {
14030 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14031 			veb = pf->veb[i];
14032 			break;
14033 		}
14034 	}
14035 
14036 	if (!veb && uplink_seid != pf->mac_seid) {
14037 
14038 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14039 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14040 				vsi = pf->vsi[i];
14041 				break;
14042 			}
14043 		}
14044 		if (!vsi) {
14045 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14046 				 uplink_seid);
14047 			return NULL;
14048 		}
14049 
14050 		if (vsi->uplink_seid == pf->mac_seid)
14051 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14052 					     vsi->tc_config.enabled_tc);
14053 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14054 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14055 					     vsi->tc_config.enabled_tc);
14056 		if (veb) {
14057 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14058 				dev_info(&vsi->back->pdev->dev,
14059 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14060 				return NULL;
14061 			}
14062 			/* We come up by default in VEPA mode if SRIOV is not
14063 			 * already enabled, in which case we can't force VEPA
14064 			 * mode.
14065 			 */
14066 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14067 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14068 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14069 			}
14070 			i40e_config_bridge_mode(veb);
14071 		}
14072 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14073 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14074 				veb = pf->veb[i];
14075 		}
14076 		if (!veb) {
14077 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14078 			return NULL;
14079 		}
14080 
14081 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14082 		uplink_seid = veb->seid;
14083 	}
14084 
14085 	/* get vsi sw struct */
14086 	v_idx = i40e_vsi_mem_alloc(pf, type);
14087 	if (v_idx < 0)
14088 		goto err_alloc;
14089 	vsi = pf->vsi[v_idx];
14090 	if (!vsi)
14091 		goto err_alloc;
14092 	vsi->type = type;
14093 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14094 
14095 	if (type == I40E_VSI_MAIN)
14096 		pf->lan_vsi = v_idx;
14097 	else if (type == I40E_VSI_SRIOV)
14098 		vsi->vf_id = param1;
14099 	/* assign it some queues */
14100 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14101 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14102 
14103 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14104 	if (ret < 0) {
14105 		dev_info(&pf->pdev->dev,
14106 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14107 			 alloc_queue_pairs, vsi->seid, ret);
14108 		goto err_vsi;
14109 	}
14110 	vsi->base_queue = ret;
14111 
14112 	/* get a VSI from the hardware */
14113 	vsi->uplink_seid = uplink_seid;
14114 	ret = i40e_add_vsi(vsi);
14115 	if (ret)
14116 		goto err_vsi;
14117 
14118 	switch (vsi->type) {
14119 	/* setup the netdev if needed */
14120 	case I40E_VSI_MAIN:
14121 	case I40E_VSI_VMDQ2:
14122 		ret = i40e_config_netdev(vsi);
14123 		if (ret)
14124 			goto err_netdev;
14125 		ret = register_netdev(vsi->netdev);
14126 		if (ret)
14127 			goto err_netdev;
14128 		vsi->netdev_registered = true;
14129 		netif_carrier_off(vsi->netdev);
14130 #ifdef CONFIG_I40E_DCB
14131 		/* Setup DCB netlink interface */
14132 		i40e_dcbnl_setup(vsi);
14133 #endif /* CONFIG_I40E_DCB */
14134 		fallthrough;
14135 	case I40E_VSI_FDIR:
14136 		/* set up vectors and rings if needed */
14137 		ret = i40e_vsi_setup_vectors(vsi);
14138 		if (ret)
14139 			goto err_msix;
14140 
14141 		ret = i40e_alloc_rings(vsi);
14142 		if (ret)
14143 			goto err_rings;
14144 
14145 		/* map all of the rings to the q_vectors */
14146 		i40e_vsi_map_rings_to_vectors(vsi);
14147 
14148 		i40e_vsi_reset_stats(vsi);
14149 		break;
14150 	default:
14151 		/* no netdev or rings for the other VSI types */
14152 		break;
14153 	}
14154 
14155 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14156 	    (vsi->type == I40E_VSI_VMDQ2)) {
14157 		ret = i40e_vsi_config_rss(vsi);
14158 	}
14159 	return vsi;
14160 
14161 err_rings:
14162 	i40e_vsi_free_q_vectors(vsi);
14163 err_msix:
14164 	if (vsi->netdev_registered) {
14165 		vsi->netdev_registered = false;
14166 		unregister_netdev(vsi->netdev);
14167 		free_netdev(vsi->netdev);
14168 		vsi->netdev = NULL;
14169 	}
14170 err_netdev:
14171 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14172 err_vsi:
14173 	i40e_vsi_clear(vsi);
14174 err_alloc:
14175 	return NULL;
14176 }
14177 
14178 /**
14179  * i40e_veb_get_bw_info - Query VEB BW information
14180  * @veb: the veb to query
14181  *
14182  * Query the Tx scheduler BW configuration data for given VEB
14183  **/
14184 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14185 {
14186 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14187 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14188 	struct i40e_pf *pf = veb->pf;
14189 	struct i40e_hw *hw = &pf->hw;
14190 	u32 tc_bw_max;
14191 	int ret = 0;
14192 	int i;
14193 
14194 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14195 						  &bw_data, NULL);
14196 	if (ret) {
14197 		dev_info(&pf->pdev->dev,
14198 			 "query veb bw config failed, err %s aq_err %s\n",
14199 			 i40e_stat_str(&pf->hw, ret),
14200 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14201 		goto out;
14202 	}
14203 
14204 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14205 						   &ets_data, NULL);
14206 	if (ret) {
14207 		dev_info(&pf->pdev->dev,
14208 			 "query veb bw ets config failed, err %s aq_err %s\n",
14209 			 i40e_stat_str(&pf->hw, ret),
14210 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14211 		goto out;
14212 	}
14213 
14214 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14215 	veb->bw_max_quanta = ets_data.tc_bw_max;
14216 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14217 	veb->enabled_tc = ets_data.tc_valid_bits;
14218 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14219 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14220 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14221 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14222 		veb->bw_tc_limit_credits[i] =
14223 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14224 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14225 	}
14226 
14227 out:
14228 	return ret;
14229 }
14230 
14231 /**
14232  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14233  * @pf: board private structure
14234  *
14235  * On error: returns error code (negative)
14236  * On success: returns vsi index in PF (positive)
14237  **/
14238 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14239 {
14240 	int ret = -ENOENT;
14241 	struct i40e_veb *veb;
14242 	int i;
14243 
14244 	/* Need to protect the allocation of switch elements at the PF level */
14245 	mutex_lock(&pf->switch_mutex);
14246 
14247 	/* VEB list may be fragmented if VEB creation/destruction has
14248 	 * been happening.  We can afford to do a quick scan to look
14249 	 * for any free slots in the list.
14250 	 *
14251 	 * find next empty veb slot, looping back around if necessary
14252 	 */
14253 	i = 0;
14254 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14255 		i++;
14256 	if (i >= I40E_MAX_VEB) {
14257 		ret = -ENOMEM;
14258 		goto err_alloc_veb;  /* out of VEB slots! */
14259 	}
14260 
14261 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14262 	if (!veb) {
14263 		ret = -ENOMEM;
14264 		goto err_alloc_veb;
14265 	}
14266 	veb->pf = pf;
14267 	veb->idx = i;
14268 	veb->enabled_tc = 1;
14269 
14270 	pf->veb[i] = veb;
14271 	ret = i;
14272 err_alloc_veb:
14273 	mutex_unlock(&pf->switch_mutex);
14274 	return ret;
14275 }
14276 
14277 /**
14278  * i40e_switch_branch_release - Delete a branch of the switch tree
14279  * @branch: where to start deleting
14280  *
14281  * This uses recursion to find the tips of the branch to be
14282  * removed, deleting until we get back to and can delete this VEB.
14283  **/
14284 static void i40e_switch_branch_release(struct i40e_veb *branch)
14285 {
14286 	struct i40e_pf *pf = branch->pf;
14287 	u16 branch_seid = branch->seid;
14288 	u16 veb_idx = branch->idx;
14289 	int i;
14290 
14291 	/* release any VEBs on this VEB - RECURSION */
14292 	for (i = 0; i < I40E_MAX_VEB; i++) {
14293 		if (!pf->veb[i])
14294 			continue;
14295 		if (pf->veb[i]->uplink_seid == branch->seid)
14296 			i40e_switch_branch_release(pf->veb[i]);
14297 	}
14298 
14299 	/* Release the VSIs on this VEB, but not the owner VSI.
14300 	 *
14301 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14302 	 *       the VEB itself, so don't use (*branch) after this loop.
14303 	 */
14304 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14305 		if (!pf->vsi[i])
14306 			continue;
14307 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14308 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14309 			i40e_vsi_release(pf->vsi[i]);
14310 		}
14311 	}
14312 
14313 	/* There's one corner case where the VEB might not have been
14314 	 * removed, so double check it here and remove it if needed.
14315 	 * This case happens if the veb was created from the debugfs
14316 	 * commands and no VSIs were added to it.
14317 	 */
14318 	if (pf->veb[veb_idx])
14319 		i40e_veb_release(pf->veb[veb_idx]);
14320 }
14321 
14322 /**
14323  * i40e_veb_clear - remove veb struct
14324  * @veb: the veb to remove
14325  **/
14326 static void i40e_veb_clear(struct i40e_veb *veb)
14327 {
14328 	if (!veb)
14329 		return;
14330 
14331 	if (veb->pf) {
14332 		struct i40e_pf *pf = veb->pf;
14333 
14334 		mutex_lock(&pf->switch_mutex);
14335 		if (pf->veb[veb->idx] == veb)
14336 			pf->veb[veb->idx] = NULL;
14337 		mutex_unlock(&pf->switch_mutex);
14338 	}
14339 
14340 	kfree(veb);
14341 }
14342 
14343 /**
14344  * i40e_veb_release - Delete a VEB and free its resources
14345  * @veb: the VEB being removed
14346  **/
14347 void i40e_veb_release(struct i40e_veb *veb)
14348 {
14349 	struct i40e_vsi *vsi = NULL;
14350 	struct i40e_pf *pf;
14351 	int i, n = 0;
14352 
14353 	pf = veb->pf;
14354 
14355 	/* find the remaining VSI and check for extras */
14356 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14357 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14358 			n++;
14359 			vsi = pf->vsi[i];
14360 		}
14361 	}
14362 	if (n != 1) {
14363 		dev_info(&pf->pdev->dev,
14364 			 "can't remove VEB %d with %d VSIs left\n",
14365 			 veb->seid, n);
14366 		return;
14367 	}
14368 
14369 	/* move the remaining VSI to uplink veb */
14370 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14371 	if (veb->uplink_seid) {
14372 		vsi->uplink_seid = veb->uplink_seid;
14373 		if (veb->uplink_seid == pf->mac_seid)
14374 			vsi->veb_idx = I40E_NO_VEB;
14375 		else
14376 			vsi->veb_idx = veb->veb_idx;
14377 	} else {
14378 		/* floating VEB */
14379 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14380 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14381 	}
14382 
14383 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14384 	i40e_veb_clear(veb);
14385 }
14386 
14387 /**
14388  * i40e_add_veb - create the VEB in the switch
14389  * @veb: the VEB to be instantiated
14390  * @vsi: the controlling VSI
14391  **/
14392 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14393 {
14394 	struct i40e_pf *pf = veb->pf;
14395 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14396 	int ret;
14397 
14398 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14399 			      veb->enabled_tc, false,
14400 			      &veb->seid, enable_stats, NULL);
14401 
14402 	/* get a VEB from the hardware */
14403 	if (ret) {
14404 		dev_info(&pf->pdev->dev,
14405 			 "couldn't add VEB, err %s aq_err %s\n",
14406 			 i40e_stat_str(&pf->hw, ret),
14407 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14408 		return -EPERM;
14409 	}
14410 
14411 	/* get statistics counter */
14412 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14413 					 &veb->stats_idx, NULL, NULL, NULL);
14414 	if (ret) {
14415 		dev_info(&pf->pdev->dev,
14416 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
14417 			 i40e_stat_str(&pf->hw, ret),
14418 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14419 		return -EPERM;
14420 	}
14421 	ret = i40e_veb_get_bw_info(veb);
14422 	if (ret) {
14423 		dev_info(&pf->pdev->dev,
14424 			 "couldn't get VEB bw info, err %s aq_err %s\n",
14425 			 i40e_stat_str(&pf->hw, ret),
14426 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14427 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14428 		return -ENOENT;
14429 	}
14430 
14431 	vsi->uplink_seid = veb->seid;
14432 	vsi->veb_idx = veb->idx;
14433 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14434 
14435 	return 0;
14436 }
14437 
14438 /**
14439  * i40e_veb_setup - Set up a VEB
14440  * @pf: board private structure
14441  * @flags: VEB setup flags
14442  * @uplink_seid: the switch element to link to
14443  * @vsi_seid: the initial VSI seid
14444  * @enabled_tc: Enabled TC bit-map
14445  *
14446  * This allocates the sw VEB structure and links it into the switch
14447  * It is possible and legal for this to be a duplicate of an already
14448  * existing VEB.  It is also possible for both uplink and vsi seids
14449  * to be zero, in order to create a floating VEB.
14450  *
14451  * Returns pointer to the successfully allocated VEB sw struct on
14452  * success, otherwise returns NULL on failure.
14453  **/
14454 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14455 				u16 uplink_seid, u16 vsi_seid,
14456 				u8 enabled_tc)
14457 {
14458 	struct i40e_veb *veb, *uplink_veb = NULL;
14459 	int vsi_idx, veb_idx;
14460 	int ret;
14461 
14462 	/* if one seid is 0, the other must be 0 to create a floating relay */
14463 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14464 	    (uplink_seid + vsi_seid != 0)) {
14465 		dev_info(&pf->pdev->dev,
14466 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14467 			 uplink_seid, vsi_seid);
14468 		return NULL;
14469 	}
14470 
14471 	/* make sure there is such a vsi and uplink */
14472 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14473 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14474 			break;
14475 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14476 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14477 			 vsi_seid);
14478 		return NULL;
14479 	}
14480 
14481 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14482 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14483 			if (pf->veb[veb_idx] &&
14484 			    pf->veb[veb_idx]->seid == uplink_seid) {
14485 				uplink_veb = pf->veb[veb_idx];
14486 				break;
14487 			}
14488 		}
14489 		if (!uplink_veb) {
14490 			dev_info(&pf->pdev->dev,
14491 				 "uplink seid %d not found\n", uplink_seid);
14492 			return NULL;
14493 		}
14494 	}
14495 
14496 	/* get veb sw struct */
14497 	veb_idx = i40e_veb_mem_alloc(pf);
14498 	if (veb_idx < 0)
14499 		goto err_alloc;
14500 	veb = pf->veb[veb_idx];
14501 	veb->flags = flags;
14502 	veb->uplink_seid = uplink_seid;
14503 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14504 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14505 
14506 	/* create the VEB in the switch */
14507 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14508 	if (ret)
14509 		goto err_veb;
14510 	if (vsi_idx == pf->lan_vsi)
14511 		pf->lan_veb = veb->idx;
14512 
14513 	return veb;
14514 
14515 err_veb:
14516 	i40e_veb_clear(veb);
14517 err_alloc:
14518 	return NULL;
14519 }
14520 
14521 /**
14522  * i40e_setup_pf_switch_element - set PF vars based on switch type
14523  * @pf: board private structure
14524  * @ele: element we are building info from
14525  * @num_reported: total number of elements
14526  * @printconfig: should we print the contents
14527  *
14528  * helper function to assist in extracting a few useful SEID values.
14529  **/
14530 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14531 				struct i40e_aqc_switch_config_element_resp *ele,
14532 				u16 num_reported, bool printconfig)
14533 {
14534 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14535 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14536 	u8 element_type = ele->element_type;
14537 	u16 seid = le16_to_cpu(ele->seid);
14538 
14539 	if (printconfig)
14540 		dev_info(&pf->pdev->dev,
14541 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14542 			 element_type, seid, uplink_seid, downlink_seid);
14543 
14544 	switch (element_type) {
14545 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14546 		pf->mac_seid = seid;
14547 		break;
14548 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14549 		/* Main VEB? */
14550 		if (uplink_seid != pf->mac_seid)
14551 			break;
14552 		if (pf->lan_veb >= I40E_MAX_VEB) {
14553 			int v;
14554 
14555 			/* find existing or else empty VEB */
14556 			for (v = 0; v < I40E_MAX_VEB; v++) {
14557 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14558 					pf->lan_veb = v;
14559 					break;
14560 				}
14561 			}
14562 			if (pf->lan_veb >= I40E_MAX_VEB) {
14563 				v = i40e_veb_mem_alloc(pf);
14564 				if (v < 0)
14565 					break;
14566 				pf->lan_veb = v;
14567 			}
14568 		}
14569 		if (pf->lan_veb >= I40E_MAX_VEB)
14570 			break;
14571 
14572 		pf->veb[pf->lan_veb]->seid = seid;
14573 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14574 		pf->veb[pf->lan_veb]->pf = pf;
14575 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14576 		break;
14577 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14578 		if (num_reported != 1)
14579 			break;
14580 		/* This is immediately after a reset so we can assume this is
14581 		 * the PF's VSI
14582 		 */
14583 		pf->mac_seid = uplink_seid;
14584 		pf->pf_seid = downlink_seid;
14585 		pf->main_vsi_seid = seid;
14586 		if (printconfig)
14587 			dev_info(&pf->pdev->dev,
14588 				 "pf_seid=%d main_vsi_seid=%d\n",
14589 				 pf->pf_seid, pf->main_vsi_seid);
14590 		break;
14591 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14592 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14593 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14594 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14595 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14596 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14597 		/* ignore these for now */
14598 		break;
14599 	default:
14600 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14601 			 element_type, seid);
14602 		break;
14603 	}
14604 }
14605 
14606 /**
14607  * i40e_fetch_switch_configuration - Get switch config from firmware
14608  * @pf: board private structure
14609  * @printconfig: should we print the contents
14610  *
14611  * Get the current switch configuration from the device and
14612  * extract a few useful SEID values.
14613  **/
14614 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14615 {
14616 	struct i40e_aqc_get_switch_config_resp *sw_config;
14617 	u16 next_seid = 0;
14618 	int ret = 0;
14619 	u8 *aq_buf;
14620 	int i;
14621 
14622 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14623 	if (!aq_buf)
14624 		return -ENOMEM;
14625 
14626 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14627 	do {
14628 		u16 num_reported, num_total;
14629 
14630 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14631 						I40E_AQ_LARGE_BUF,
14632 						&next_seid, NULL);
14633 		if (ret) {
14634 			dev_info(&pf->pdev->dev,
14635 				 "get switch config failed err %s aq_err %s\n",
14636 				 i40e_stat_str(&pf->hw, ret),
14637 				 i40e_aq_str(&pf->hw,
14638 					     pf->hw.aq.asq_last_status));
14639 			kfree(aq_buf);
14640 			return -ENOENT;
14641 		}
14642 
14643 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14644 		num_total = le16_to_cpu(sw_config->header.num_total);
14645 
14646 		if (printconfig)
14647 			dev_info(&pf->pdev->dev,
14648 				 "header: %d reported %d total\n",
14649 				 num_reported, num_total);
14650 
14651 		for (i = 0; i < num_reported; i++) {
14652 			struct i40e_aqc_switch_config_element_resp *ele =
14653 				&sw_config->element[i];
14654 
14655 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14656 						     printconfig);
14657 		}
14658 	} while (next_seid != 0);
14659 
14660 	kfree(aq_buf);
14661 	return ret;
14662 }
14663 
14664 /**
14665  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14666  * @pf: board private structure
14667  * @reinit: if the Main VSI needs to re-initialized.
14668  *
14669  * Returns 0 on success, negative value on failure
14670  **/
14671 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
14672 {
14673 	u16 flags = 0;
14674 	int ret;
14675 
14676 	/* find out what's out there already */
14677 	ret = i40e_fetch_switch_configuration(pf, false);
14678 	if (ret) {
14679 		dev_info(&pf->pdev->dev,
14680 			 "couldn't fetch switch config, err %s aq_err %s\n",
14681 			 i40e_stat_str(&pf->hw, ret),
14682 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14683 		return ret;
14684 	}
14685 	i40e_pf_reset_stats(pf);
14686 
14687 	/* set the switch config bit for the whole device to
14688 	 * support limited promisc or true promisc
14689 	 * when user requests promisc. The default is limited
14690 	 * promisc.
14691 	*/
14692 
14693 	if ((pf->hw.pf_id == 0) &&
14694 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
14695 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14696 		pf->last_sw_conf_flags = flags;
14697 	}
14698 
14699 	if (pf->hw.pf_id == 0) {
14700 		u16 valid_flags;
14701 
14702 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14703 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
14704 						NULL);
14705 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
14706 			dev_info(&pf->pdev->dev,
14707 				 "couldn't set switch config bits, err %s aq_err %s\n",
14708 				 i40e_stat_str(&pf->hw, ret),
14709 				 i40e_aq_str(&pf->hw,
14710 					     pf->hw.aq.asq_last_status));
14711 			/* not a fatal problem, just keep going */
14712 		}
14713 		pf->last_sw_conf_valid_flags = valid_flags;
14714 	}
14715 
14716 	/* first time setup */
14717 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
14718 		struct i40e_vsi *vsi = NULL;
14719 		u16 uplink_seid;
14720 
14721 		/* Set up the PF VSI associated with the PF's main VSI
14722 		 * that is already in the HW switch
14723 		 */
14724 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
14725 			uplink_seid = pf->veb[pf->lan_veb]->seid;
14726 		else
14727 			uplink_seid = pf->mac_seid;
14728 		if (pf->lan_vsi == I40E_NO_VSI)
14729 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
14730 		else if (reinit)
14731 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
14732 		if (!vsi) {
14733 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
14734 			i40e_cloud_filter_exit(pf);
14735 			i40e_fdir_teardown(pf);
14736 			return -EAGAIN;
14737 		}
14738 	} else {
14739 		/* force a reset of TC and queue layout configurations */
14740 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14741 
14742 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14743 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14744 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14745 	}
14746 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
14747 
14748 	i40e_fdir_sb_setup(pf);
14749 
14750 	/* Setup static PF queue filter control settings */
14751 	ret = i40e_setup_pf_filter_control(pf);
14752 	if (ret) {
14753 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
14754 			 ret);
14755 		/* Failure here should not stop continuing other steps */
14756 	}
14757 
14758 	/* enable RSS in the HW, even for only one queue, as the stack can use
14759 	 * the hash
14760 	 */
14761 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
14762 		i40e_pf_config_rss(pf);
14763 
14764 	/* fill in link information and enable LSE reporting */
14765 	i40e_link_event(pf);
14766 
14767 	/* Initialize user-specific link properties */
14768 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
14769 				  I40E_AQ_AN_COMPLETED) ? true : false);
14770 
14771 	i40e_ptp_init(pf);
14772 
14773 	/* repopulate tunnel port filters */
14774 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
14775 
14776 	return ret;
14777 }
14778 
14779 /**
14780  * i40e_determine_queue_usage - Work out queue distribution
14781  * @pf: board private structure
14782  **/
14783 static void i40e_determine_queue_usage(struct i40e_pf *pf)
14784 {
14785 	int queues_left;
14786 	int q_max;
14787 
14788 	pf->num_lan_qps = 0;
14789 
14790 	/* Find the max queues to be put into basic use.  We'll always be
14791 	 * using TC0, whether or not DCB is running, and TC0 will get the
14792 	 * big RSS set.
14793 	 */
14794 	queues_left = pf->hw.func_caps.num_tx_qp;
14795 
14796 	if ((queues_left == 1) ||
14797 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
14798 		/* one qp for PF, no queues for anything else */
14799 		queues_left = 0;
14800 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14801 
14802 		/* make sure all the fancies are disabled */
14803 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14804 			       I40E_FLAG_IWARP_ENABLED	|
14805 			       I40E_FLAG_FD_SB_ENABLED	|
14806 			       I40E_FLAG_FD_ATR_ENABLED	|
14807 			       I40E_FLAG_DCB_CAPABLE	|
14808 			       I40E_FLAG_DCB_ENABLED	|
14809 			       I40E_FLAG_SRIOV_ENABLED	|
14810 			       I40E_FLAG_VMDQ_ENABLED);
14811 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14812 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
14813 				  I40E_FLAG_FD_SB_ENABLED |
14814 				  I40E_FLAG_FD_ATR_ENABLED |
14815 				  I40E_FLAG_DCB_CAPABLE))) {
14816 		/* one qp for PF */
14817 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14818 		queues_left -= pf->num_lan_qps;
14819 
14820 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14821 			       I40E_FLAG_IWARP_ENABLED	|
14822 			       I40E_FLAG_FD_SB_ENABLED	|
14823 			       I40E_FLAG_FD_ATR_ENABLED	|
14824 			       I40E_FLAG_DCB_ENABLED	|
14825 			       I40E_FLAG_VMDQ_ENABLED);
14826 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14827 	} else {
14828 		/* Not enough queues for all TCs */
14829 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
14830 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
14831 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
14832 					I40E_FLAG_DCB_ENABLED);
14833 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
14834 		}
14835 
14836 		/* limit lan qps to the smaller of qps, cpus or msix */
14837 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
14838 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
14839 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
14840 		pf->num_lan_qps = q_max;
14841 
14842 		queues_left -= pf->num_lan_qps;
14843 	}
14844 
14845 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14846 		if (queues_left > 1) {
14847 			queues_left -= 1; /* save 1 queue for FD */
14848 		} else {
14849 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
14850 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14851 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
14852 		}
14853 	}
14854 
14855 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
14856 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
14857 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
14858 					(queues_left / pf->num_vf_qps));
14859 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
14860 	}
14861 
14862 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
14863 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
14864 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
14865 					  (queues_left / pf->num_vmdq_qps));
14866 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
14867 	}
14868 
14869 	pf->queues_left = queues_left;
14870 	dev_dbg(&pf->pdev->dev,
14871 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
14872 		pf->hw.func_caps.num_tx_qp,
14873 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
14874 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
14875 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
14876 		queues_left);
14877 }
14878 
14879 /**
14880  * i40e_setup_pf_filter_control - Setup PF static filter control
14881  * @pf: PF to be setup
14882  *
14883  * i40e_setup_pf_filter_control sets up a PF's initial filter control
14884  * settings. If PE/FCoE are enabled then it will also set the per PF
14885  * based filter sizes required for them. It also enables Flow director,
14886  * ethertype and macvlan type filter settings for the pf.
14887  *
14888  * Returns 0 on success, negative on failure
14889  **/
14890 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
14891 {
14892 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
14893 
14894 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
14895 
14896 	/* Flow Director is enabled */
14897 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
14898 		settings->enable_fdir = true;
14899 
14900 	/* Ethtype and MACVLAN filters enabled for PF */
14901 	settings->enable_ethtype = true;
14902 	settings->enable_macvlan = true;
14903 
14904 	if (i40e_set_filter_control(&pf->hw, settings))
14905 		return -ENOENT;
14906 
14907 	return 0;
14908 }
14909 
14910 #define INFO_STRING_LEN 255
14911 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
14912 static void i40e_print_features(struct i40e_pf *pf)
14913 {
14914 	struct i40e_hw *hw = &pf->hw;
14915 	char *buf;
14916 	int i;
14917 
14918 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
14919 	if (!buf)
14920 		return;
14921 
14922 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
14923 #ifdef CONFIG_PCI_IOV
14924 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
14925 #endif
14926 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
14927 		      pf->hw.func_caps.num_vsis,
14928 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
14929 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
14930 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
14931 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
14932 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
14933 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14934 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
14935 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
14936 	}
14937 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
14938 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
14939 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
14940 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
14941 	if (pf->flags & I40E_FLAG_PTP)
14942 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
14943 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
14944 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
14945 	else
14946 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
14947 
14948 	dev_info(&pf->pdev->dev, "%s\n", buf);
14949 	kfree(buf);
14950 	WARN_ON(i > INFO_STRING_LEN);
14951 }
14952 
14953 /**
14954  * i40e_get_platform_mac_addr - get platform-specific MAC address
14955  * @pdev: PCI device information struct
14956  * @pf: board private structure
14957  *
14958  * Look up the MAC address for the device. First we'll try
14959  * eth_platform_get_mac_address, which will check Open Firmware, or arch
14960  * specific fallback. Otherwise, we'll default to the stored value in
14961  * firmware.
14962  **/
14963 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
14964 {
14965 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
14966 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
14967 }
14968 
14969 /**
14970  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
14971  * @fec_cfg: FEC option to set in flags
14972  * @flags: ptr to flags in which we set FEC option
14973  **/
14974 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
14975 {
14976 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
14977 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
14978 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
14979 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
14980 		*flags |= I40E_FLAG_RS_FEC;
14981 		*flags &= ~I40E_FLAG_BASE_R_FEC;
14982 	}
14983 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
14984 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
14985 		*flags |= I40E_FLAG_BASE_R_FEC;
14986 		*flags &= ~I40E_FLAG_RS_FEC;
14987 	}
14988 	if (fec_cfg == 0)
14989 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
14990 }
14991 
14992 /**
14993  * i40e_check_recovery_mode - check if we are running transition firmware
14994  * @pf: board private structure
14995  *
14996  * Check registers indicating the firmware runs in recovery mode. Sets the
14997  * appropriate driver state.
14998  *
14999  * Returns true if the recovery mode was detected, false otherwise
15000  **/
15001 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15002 {
15003 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15004 
15005 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15006 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15007 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15008 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15009 
15010 		return true;
15011 	}
15012 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15013 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15014 
15015 	return false;
15016 }
15017 
15018 /**
15019  * i40e_pf_loop_reset - perform reset in a loop.
15020  * @pf: board private structure
15021  *
15022  * This function is useful when a NIC is about to enter recovery mode.
15023  * When a NIC's internal data structures are corrupted the NIC's
15024  * firmware is going to enter recovery mode.
15025  * Right after a POR it takes about 7 minutes for firmware to enter
15026  * recovery mode. Until that time a NIC is in some kind of intermediate
15027  * state. After that time period the NIC almost surely enters
15028  * recovery mode. The only way for a driver to detect intermediate
15029  * state is to issue a series of pf-resets and check a return value.
15030  * If a PF reset returns success then the firmware could be in recovery
15031  * mode so the caller of this code needs to check for recovery mode
15032  * if this function returns success. There is a little chance that
15033  * firmware will hang in intermediate state forever.
15034  * Since waiting 7 minutes is quite a lot of time this function waits
15035  * 10 seconds and then gives up by returning an error.
15036  *
15037  * Return 0 on success, negative on failure.
15038  **/
15039 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
15040 {
15041 	/* wait max 10 seconds for PF reset to succeed */
15042 	const unsigned long time_end = jiffies + 10 * HZ;
15043 
15044 	struct i40e_hw *hw = &pf->hw;
15045 	i40e_status ret;
15046 
15047 	ret = i40e_pf_reset(hw);
15048 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15049 		usleep_range(10000, 20000);
15050 		ret = i40e_pf_reset(hw);
15051 	}
15052 
15053 	if (ret == I40E_SUCCESS)
15054 		pf->pfr_count++;
15055 	else
15056 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15057 
15058 	return ret;
15059 }
15060 
15061 /**
15062  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15063  * @pf: board private structure
15064  *
15065  * Check FW registers to determine if FW issued unexpected EMP Reset.
15066  * Every time when unexpected EMP Reset occurs the FW increments
15067  * a counter of unexpected EMP Resets. When the counter reaches 10
15068  * the FW should enter the Recovery mode
15069  *
15070  * Returns true if FW issued unexpected EMP Reset
15071  **/
15072 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15073 {
15074 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15075 			   I40E_GL_FWSTS_FWS1B_MASK;
15076 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15077 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15078 }
15079 
15080 /**
15081  * i40e_handle_resets - handle EMP resets and PF resets
15082  * @pf: board private structure
15083  *
15084  * Handle both EMP resets and PF resets and conclude whether there are
15085  * any issues regarding these resets. If there are any issues then
15086  * generate log entry.
15087  *
15088  * Return 0 if NIC is healthy or negative value when there are issues
15089  * with resets
15090  **/
15091 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
15092 {
15093 	const i40e_status pfr = i40e_pf_loop_reset(pf);
15094 	const bool is_empr = i40e_check_fw_empr(pf);
15095 
15096 	if (is_empr || pfr != I40E_SUCCESS)
15097 		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");
15098 
15099 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15100 }
15101 
15102 /**
15103  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15104  * @pf: board private structure
15105  * @hw: ptr to the hardware info
15106  *
15107  * This function does a minimal setup of all subsystems needed for running
15108  * recovery mode.
15109  *
15110  * Returns 0 on success, negative on failure
15111  **/
15112 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15113 {
15114 	struct i40e_vsi *vsi;
15115 	int err;
15116 	int v_idx;
15117 
15118 	pci_save_state(pf->pdev);
15119 
15120 	/* set up periodic task facility */
15121 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15122 	pf->service_timer_period = HZ;
15123 
15124 	INIT_WORK(&pf->service_task, i40e_service_task);
15125 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15126 
15127 	err = i40e_init_interrupt_scheme(pf);
15128 	if (err)
15129 		goto err_switch_setup;
15130 
15131 	/* The number of VSIs reported by the FW is the minimum guaranteed
15132 	 * to us; HW supports far more and we share the remaining pool with
15133 	 * the other PFs. We allocate space for more than the guarantee with
15134 	 * the understanding that we might not get them all later.
15135 	 */
15136 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15137 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15138 	else
15139 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15140 
15141 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15142 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15143 			  GFP_KERNEL);
15144 	if (!pf->vsi) {
15145 		err = -ENOMEM;
15146 		goto err_switch_setup;
15147 	}
15148 
15149 	/* We allocate one VSI which is needed as absolute minimum
15150 	 * in order to register the netdev
15151 	 */
15152 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15153 	if (v_idx < 0) {
15154 		err = v_idx;
15155 		goto err_switch_setup;
15156 	}
15157 	pf->lan_vsi = v_idx;
15158 	vsi = pf->vsi[v_idx];
15159 	if (!vsi) {
15160 		err = -EFAULT;
15161 		goto err_switch_setup;
15162 	}
15163 	vsi->alloc_queue_pairs = 1;
15164 	err = i40e_config_netdev(vsi);
15165 	if (err)
15166 		goto err_switch_setup;
15167 	err = register_netdev(vsi->netdev);
15168 	if (err)
15169 		goto err_switch_setup;
15170 	vsi->netdev_registered = true;
15171 	i40e_dbg_pf_init(pf);
15172 
15173 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15174 	if (err)
15175 		goto err_switch_setup;
15176 
15177 	/* tell the firmware that we're starting */
15178 	i40e_send_version(pf);
15179 
15180 	/* since everything's happy, start the service_task timer */
15181 	mod_timer(&pf->service_timer,
15182 		  round_jiffies(jiffies + pf->service_timer_period));
15183 
15184 	return 0;
15185 
15186 err_switch_setup:
15187 	i40e_reset_interrupt_capability(pf);
15188 	del_timer_sync(&pf->service_timer);
15189 	i40e_shutdown_adminq(hw);
15190 	iounmap(hw->hw_addr);
15191 	pci_disable_pcie_error_reporting(pf->pdev);
15192 	pci_release_mem_regions(pf->pdev);
15193 	pci_disable_device(pf->pdev);
15194 	kfree(pf);
15195 
15196 	return err;
15197 }
15198 
15199 /**
15200  * i40e_probe - Device initialization routine
15201  * @pdev: PCI device information struct
15202  * @ent: entry in i40e_pci_tbl
15203  *
15204  * i40e_probe initializes a PF identified by a pci_dev structure.
15205  * The OS initialization, configuring of the PF private structure,
15206  * and a hardware reset occur.
15207  *
15208  * Returns 0 on success, negative on failure
15209  **/
15210 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15211 {
15212 	struct i40e_aq_get_phy_abilities_resp abilities;
15213 #ifdef CONFIG_I40E_DCB
15214 	enum i40e_get_fw_lldp_status_resp lldp_status;
15215 	i40e_status status;
15216 #endif /* CONFIG_I40E_DCB */
15217 	struct i40e_pf *pf;
15218 	struct i40e_hw *hw;
15219 	static u16 pfs_found;
15220 	u16 wol_nvm_bits;
15221 	u16 link_status;
15222 	int err;
15223 	u32 val;
15224 	u32 i;
15225 
15226 	err = pci_enable_device_mem(pdev);
15227 	if (err)
15228 		return err;
15229 
15230 	/* set up for high or low dma */
15231 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15232 	if (err) {
15233 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
15234 		if (err) {
15235 			dev_err(&pdev->dev,
15236 				"DMA configuration failed: 0x%x\n", err);
15237 			goto err_dma;
15238 		}
15239 	}
15240 
15241 	/* set up pci connections */
15242 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15243 	if (err) {
15244 		dev_info(&pdev->dev,
15245 			 "pci_request_selected_regions failed %d\n", err);
15246 		goto err_pci_reg;
15247 	}
15248 
15249 	pci_enable_pcie_error_reporting(pdev);
15250 	pci_set_master(pdev);
15251 
15252 	/* Now that we have a PCI connection, we need to do the
15253 	 * low level device setup.  This is primarily setting up
15254 	 * the Admin Queue structures and then querying for the
15255 	 * device's current profile information.
15256 	 */
15257 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15258 	if (!pf) {
15259 		err = -ENOMEM;
15260 		goto err_pf_alloc;
15261 	}
15262 	pf->next_vsi = 0;
15263 	pf->pdev = pdev;
15264 	set_bit(__I40E_DOWN, pf->state);
15265 
15266 	hw = &pf->hw;
15267 	hw->back = pf;
15268 
15269 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15270 				I40E_MAX_CSR_SPACE);
15271 	/* We believe that the highest register to read is
15272 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15273 	 * is not less than that before mapping to prevent a
15274 	 * kernel panic.
15275 	 */
15276 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15277 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15278 			pf->ioremap_len);
15279 		err = -ENOMEM;
15280 		goto err_ioremap;
15281 	}
15282 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15283 	if (!hw->hw_addr) {
15284 		err = -EIO;
15285 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15286 			 (unsigned int)pci_resource_start(pdev, 0),
15287 			 pf->ioremap_len, err);
15288 		goto err_ioremap;
15289 	}
15290 	hw->vendor_id = pdev->vendor;
15291 	hw->device_id = pdev->device;
15292 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15293 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15294 	hw->subsystem_device_id = pdev->subsystem_device;
15295 	hw->bus.device = PCI_SLOT(pdev->devfn);
15296 	hw->bus.func = PCI_FUNC(pdev->devfn);
15297 	hw->bus.bus_id = pdev->bus->number;
15298 	pf->instance = pfs_found;
15299 
15300 	/* Select something other than the 802.1ad ethertype for the
15301 	 * switch to use internally and drop on ingress.
15302 	 */
15303 	hw->switch_tag = 0xffff;
15304 	hw->first_tag = ETH_P_8021AD;
15305 	hw->second_tag = ETH_P_8021Q;
15306 
15307 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15308 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15309 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15310 
15311 	/* set up the locks for the AQ, do this only once in probe
15312 	 * and destroy them only once in remove
15313 	 */
15314 	mutex_init(&hw->aq.asq_mutex);
15315 	mutex_init(&hw->aq.arq_mutex);
15316 
15317 	pf->msg_enable = netif_msg_init(debug,
15318 					NETIF_MSG_DRV |
15319 					NETIF_MSG_PROBE |
15320 					NETIF_MSG_LINK);
15321 	if (debug < -1)
15322 		pf->hw.debug_mask = debug;
15323 
15324 	/* do a special CORER for clearing PXE mode once at init */
15325 	if (hw->revision_id == 0 &&
15326 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15327 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15328 		i40e_flush(hw);
15329 		msleep(200);
15330 		pf->corer_count++;
15331 
15332 		i40e_clear_pxe_mode(hw);
15333 	}
15334 
15335 	/* Reset here to make sure all is clean and to define PF 'n' */
15336 	i40e_clear_hw(hw);
15337 
15338 	err = i40e_set_mac_type(hw);
15339 	if (err) {
15340 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15341 			 err);
15342 		goto err_pf_reset;
15343 	}
15344 
15345 	err = i40e_handle_resets(pf);
15346 	if (err)
15347 		goto err_pf_reset;
15348 
15349 	i40e_check_recovery_mode(pf);
15350 
15351 	if (is_kdump_kernel()) {
15352 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15353 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15354 	} else {
15355 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15356 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15357 	}
15358 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15359 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15360 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15361 
15362 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15363 		 "%s-%s:misc",
15364 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15365 
15366 	err = i40e_init_shared_code(hw);
15367 	if (err) {
15368 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15369 			 err);
15370 		goto err_pf_reset;
15371 	}
15372 
15373 	/* set up a default setting for link flow control */
15374 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15375 
15376 	err = i40e_init_adminq(hw);
15377 	if (err) {
15378 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15379 			dev_info(&pdev->dev,
15380 				 "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",
15381 				 hw->aq.api_maj_ver,
15382 				 hw->aq.api_min_ver,
15383 				 I40E_FW_API_VERSION_MAJOR,
15384 				 I40E_FW_MINOR_VERSION(hw));
15385 		else
15386 			dev_info(&pdev->dev,
15387 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15388 
15389 		goto err_pf_reset;
15390 	}
15391 	i40e_get_oem_version(hw);
15392 
15393 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15394 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15395 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15396 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15397 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15398 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15399 
15400 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15401 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15402 		dev_info(&pdev->dev,
15403 			 "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",
15404 			 hw->aq.api_maj_ver,
15405 			 hw->aq.api_min_ver,
15406 			 I40E_FW_API_VERSION_MAJOR,
15407 			 I40E_FW_MINOR_VERSION(hw));
15408 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15409 		dev_info(&pdev->dev,
15410 			 "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",
15411 			 hw->aq.api_maj_ver,
15412 			 hw->aq.api_min_ver,
15413 			 I40E_FW_API_VERSION_MAJOR,
15414 			 I40E_FW_MINOR_VERSION(hw));
15415 
15416 	i40e_verify_eeprom(pf);
15417 
15418 	/* Rev 0 hardware was never productized */
15419 	if (hw->revision_id < 1)
15420 		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");
15421 
15422 	i40e_clear_pxe_mode(hw);
15423 
15424 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15425 	if (err)
15426 		goto err_adminq_setup;
15427 
15428 	err = i40e_sw_init(pf);
15429 	if (err) {
15430 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15431 		goto err_sw_init;
15432 	}
15433 
15434 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15435 		return i40e_init_recovery_mode(pf, hw);
15436 
15437 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15438 				hw->func_caps.num_rx_qp, 0, 0);
15439 	if (err) {
15440 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15441 		goto err_init_lan_hmc;
15442 	}
15443 
15444 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15445 	if (err) {
15446 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15447 		err = -ENOENT;
15448 		goto err_configure_lan_hmc;
15449 	}
15450 
15451 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15452 	 * Ignore error return codes because if it was already disabled via
15453 	 * hardware settings this will fail
15454 	 */
15455 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15456 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15457 		i40e_aq_stop_lldp(hw, true, false, NULL);
15458 	}
15459 
15460 	/* allow a platform config to override the HW addr */
15461 	i40e_get_platform_mac_addr(pdev, pf);
15462 
15463 	if (!is_valid_ether_addr(hw->mac.addr)) {
15464 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15465 		err = -EIO;
15466 		goto err_mac_addr;
15467 	}
15468 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15469 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15470 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15471 	if (is_valid_ether_addr(hw->mac.port_addr))
15472 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15473 
15474 	pci_set_drvdata(pdev, pf);
15475 	pci_save_state(pdev);
15476 
15477 #ifdef CONFIG_I40E_DCB
15478 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15479 	(!status &&
15480 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15481 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15482 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15483 	dev_info(&pdev->dev,
15484 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15485 			"FW LLDP is disabled\n" :
15486 			"FW LLDP is enabled\n");
15487 
15488 	/* Enable FW to write default DCB config on link-up */
15489 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15490 
15491 	err = i40e_init_pf_dcb(pf);
15492 	if (err) {
15493 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15494 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15495 		/* Continue without DCB enabled */
15496 	}
15497 #endif /* CONFIG_I40E_DCB */
15498 
15499 	/* set up periodic task facility */
15500 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15501 	pf->service_timer_period = HZ;
15502 
15503 	INIT_WORK(&pf->service_task, i40e_service_task);
15504 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15505 
15506 	/* NVM bit on means WoL disabled for the port */
15507 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15508 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15509 		pf->wol_en = false;
15510 	else
15511 		pf->wol_en = true;
15512 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15513 
15514 	/* set up the main switch operations */
15515 	i40e_determine_queue_usage(pf);
15516 	err = i40e_init_interrupt_scheme(pf);
15517 	if (err)
15518 		goto err_switch_setup;
15519 
15520 	/* Reduce Tx and Rx pairs for kdump
15521 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15522 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15523 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15524 	 */
15525 	if (is_kdump_kernel())
15526 		pf->num_lan_msix = 1;
15527 
15528 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15529 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15530 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15531 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15532 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15533 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15534 						    UDP_TUNNEL_TYPE_GENEVE;
15535 
15536 	/* The number of VSIs reported by the FW is the minimum guaranteed
15537 	 * to us; HW supports far more and we share the remaining pool with
15538 	 * the other PFs. We allocate space for more than the guarantee with
15539 	 * the understanding that we might not get them all later.
15540 	 */
15541 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15542 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15543 	else
15544 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15545 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15546 		dev_warn(&pf->pdev->dev,
15547 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15548 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15549 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15550 	}
15551 
15552 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15553 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15554 			  GFP_KERNEL);
15555 	if (!pf->vsi) {
15556 		err = -ENOMEM;
15557 		goto err_switch_setup;
15558 	}
15559 
15560 #ifdef CONFIG_PCI_IOV
15561 	/* prep for VF support */
15562 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15563 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15564 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15565 		if (pci_num_vf(pdev))
15566 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15567 	}
15568 #endif
15569 	err = i40e_setup_pf_switch(pf, false);
15570 	if (err) {
15571 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15572 		goto err_vsis;
15573 	}
15574 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15575 
15576 	/* if FDIR VSI was set up, start it now */
15577 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15578 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15579 			i40e_vsi_open(pf->vsi[i]);
15580 			break;
15581 		}
15582 	}
15583 
15584 	/* The driver only wants link up/down and module qualification
15585 	 * reports from firmware.  Note the negative logic.
15586 	 */
15587 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15588 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15589 					 I40E_AQ_EVENT_MEDIA_NA |
15590 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15591 	if (err)
15592 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15593 			 i40e_stat_str(&pf->hw, err),
15594 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15595 
15596 	/* Reconfigure hardware for allowing smaller MSS in the case
15597 	 * of TSO, so that we avoid the MDD being fired and causing
15598 	 * a reset in the case of small MSS+TSO.
15599 	 */
15600 	val = rd32(hw, I40E_REG_MSS);
15601 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15602 		val &= ~I40E_REG_MSS_MIN_MASK;
15603 		val |= I40E_64BYTE_MSS;
15604 		wr32(hw, I40E_REG_MSS, val);
15605 	}
15606 
15607 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15608 		msleep(75);
15609 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15610 		if (err)
15611 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15612 				 i40e_stat_str(&pf->hw, err),
15613 				 i40e_aq_str(&pf->hw,
15614 					     pf->hw.aq.asq_last_status));
15615 	}
15616 	/* The main driver is (mostly) up and happy. We need to set this state
15617 	 * before setting up the misc vector or we get a race and the vector
15618 	 * ends up disabled forever.
15619 	 */
15620 	clear_bit(__I40E_DOWN, pf->state);
15621 
15622 	/* In case of MSIX we are going to setup the misc vector right here
15623 	 * to handle admin queue events etc. In case of legacy and MSI
15624 	 * the misc functionality and queue processing is combined in
15625 	 * the same vector and that gets setup at open.
15626 	 */
15627 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15628 		err = i40e_setup_misc_vector(pf);
15629 		if (err) {
15630 			dev_info(&pdev->dev,
15631 				 "setup of misc vector failed: %d\n", err);
15632 			i40e_cloud_filter_exit(pf);
15633 			i40e_fdir_teardown(pf);
15634 			goto err_vsis;
15635 		}
15636 	}
15637 
15638 #ifdef CONFIG_PCI_IOV
15639 	/* prep for VF support */
15640 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15641 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15642 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15643 		/* disable link interrupts for VFs */
15644 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15645 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15646 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15647 		i40e_flush(hw);
15648 
15649 		if (pci_num_vf(pdev)) {
15650 			dev_info(&pdev->dev,
15651 				 "Active VFs found, allocating resources.\n");
15652 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15653 			if (err)
15654 				dev_info(&pdev->dev,
15655 					 "Error %d allocating resources for existing VFs\n",
15656 					 err);
15657 		}
15658 	}
15659 #endif /* CONFIG_PCI_IOV */
15660 
15661 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15662 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
15663 						      pf->num_iwarp_msix,
15664 						      I40E_IWARP_IRQ_PILE_ID);
15665 		if (pf->iwarp_base_vector < 0) {
15666 			dev_info(&pdev->dev,
15667 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
15668 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
15669 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
15670 		}
15671 	}
15672 
15673 	i40e_dbg_pf_init(pf);
15674 
15675 	/* tell the firmware that we're starting */
15676 	i40e_send_version(pf);
15677 
15678 	/* since everything's happy, start the service_task timer */
15679 	mod_timer(&pf->service_timer,
15680 		  round_jiffies(jiffies + pf->service_timer_period));
15681 
15682 	/* add this PF to client device list and launch a client service task */
15683 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15684 		err = i40e_lan_add_device(pf);
15685 		if (err)
15686 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
15687 				 err);
15688 	}
15689 
15690 #define PCI_SPEED_SIZE 8
15691 #define PCI_WIDTH_SIZE 8
15692 	/* Devices on the IOSF bus do not have this information
15693 	 * and will report PCI Gen 1 x 1 by default so don't bother
15694 	 * checking them.
15695 	 */
15696 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
15697 		char speed[PCI_SPEED_SIZE] = "Unknown";
15698 		char width[PCI_WIDTH_SIZE] = "Unknown";
15699 
15700 		/* Get the negotiated link width and speed from PCI config
15701 		 * space
15702 		 */
15703 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
15704 					  &link_status);
15705 
15706 		i40e_set_pci_config_data(hw, link_status);
15707 
15708 		switch (hw->bus.speed) {
15709 		case i40e_bus_speed_8000:
15710 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
15711 		case i40e_bus_speed_5000:
15712 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
15713 		case i40e_bus_speed_2500:
15714 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
15715 		default:
15716 			break;
15717 		}
15718 		switch (hw->bus.width) {
15719 		case i40e_bus_width_pcie_x8:
15720 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
15721 		case i40e_bus_width_pcie_x4:
15722 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
15723 		case i40e_bus_width_pcie_x2:
15724 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
15725 		case i40e_bus_width_pcie_x1:
15726 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
15727 		default:
15728 			break;
15729 		}
15730 
15731 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
15732 			 speed, width);
15733 
15734 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
15735 		    hw->bus.speed < i40e_bus_speed_8000) {
15736 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
15737 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
15738 		}
15739 	}
15740 
15741 	/* get the requested speeds from the fw */
15742 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
15743 	if (err)
15744 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
15745 			i40e_stat_str(&pf->hw, err),
15746 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15747 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
15748 
15749 	/* set the FEC config due to the board capabilities */
15750 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
15751 
15752 	/* get the supported phy types from the fw */
15753 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
15754 	if (err)
15755 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
15756 			i40e_stat_str(&pf->hw, err),
15757 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15758 
15759 	/* make sure the MFS hasn't been set lower than the default */
15760 #define MAX_FRAME_SIZE_DEFAULT 0x2600
15761 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
15762 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
15763 	if (val < MAX_FRAME_SIZE_DEFAULT)
15764 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
15765 			 i, val);
15766 
15767 	/* Add a filter to drop all Flow control frames from any VSI from being
15768 	 * transmitted. By doing so we stop a malicious VF from sending out
15769 	 * PAUSE or PFC frames and potentially controlling traffic for other
15770 	 * PF/VF VSIs.
15771 	 * The FW can still send Flow control frames if enabled.
15772 	 */
15773 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
15774 						       pf->main_vsi_seid);
15775 #ifdef CONFIG_I40E_DCB
15776 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP)
15777 		i40e_set_lldp_forwarding(pf, true);
15778 #endif /* CONFIG_I40E_DCB */
15779 
15780 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
15781 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
15782 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
15783 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
15784 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
15785 	/* print a string summarizing features */
15786 	i40e_print_features(pf);
15787 
15788 	return 0;
15789 
15790 	/* Unwind what we've done if something failed in the setup */
15791 err_vsis:
15792 	set_bit(__I40E_DOWN, pf->state);
15793 	i40e_clear_interrupt_scheme(pf);
15794 	kfree(pf->vsi);
15795 err_switch_setup:
15796 	i40e_reset_interrupt_capability(pf);
15797 	del_timer_sync(&pf->service_timer);
15798 err_mac_addr:
15799 err_configure_lan_hmc:
15800 	(void)i40e_shutdown_lan_hmc(hw);
15801 err_init_lan_hmc:
15802 	kfree(pf->qp_pile);
15803 err_sw_init:
15804 err_adminq_setup:
15805 err_pf_reset:
15806 	iounmap(hw->hw_addr);
15807 err_ioremap:
15808 	kfree(pf);
15809 err_pf_alloc:
15810 	pci_disable_pcie_error_reporting(pdev);
15811 	pci_release_mem_regions(pdev);
15812 err_pci_reg:
15813 err_dma:
15814 	pci_disable_device(pdev);
15815 	return err;
15816 }
15817 
15818 /**
15819  * i40e_remove - Device removal routine
15820  * @pdev: PCI device information struct
15821  *
15822  * i40e_remove is called by the PCI subsystem to alert the driver
15823  * that is should release a PCI device.  This could be caused by a
15824  * Hot-Plug event, or because the driver is going to be removed from
15825  * memory.
15826  **/
15827 static void i40e_remove(struct pci_dev *pdev)
15828 {
15829 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15830 	struct i40e_hw *hw = &pf->hw;
15831 	i40e_status ret_code;
15832 	int i;
15833 
15834 	i40e_dbg_pf_exit(pf);
15835 
15836 	i40e_ptp_stop(pf);
15837 
15838 	/* Disable RSS in hw */
15839 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
15840 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
15841 
15842 	while (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
15843 		usleep_range(1000, 2000);
15844 
15845 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
15846 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
15847 		i40e_free_vfs(pf);
15848 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
15849 	}
15850 	/* no more scheduling of any task */
15851 	set_bit(__I40E_SUSPENDED, pf->state);
15852 	set_bit(__I40E_DOWN, pf->state);
15853 	if (pf->service_timer.function)
15854 		del_timer_sync(&pf->service_timer);
15855 	if (pf->service_task.func)
15856 		cancel_work_sync(&pf->service_task);
15857 
15858 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
15859 		struct i40e_vsi *vsi = pf->vsi[0];
15860 
15861 		/* We know that we have allocated only one vsi for this PF,
15862 		 * it was just for registering netdevice, so the interface
15863 		 * could be visible in the 'ifconfig' output
15864 		 */
15865 		unregister_netdev(vsi->netdev);
15866 		free_netdev(vsi->netdev);
15867 
15868 		goto unmap;
15869 	}
15870 
15871 	/* Client close must be called explicitly here because the timer
15872 	 * has been stopped.
15873 	 */
15874 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15875 
15876 	i40e_fdir_teardown(pf);
15877 
15878 	/* If there is a switch structure or any orphans, remove them.
15879 	 * This will leave only the PF's VSI remaining.
15880 	 */
15881 	for (i = 0; i < I40E_MAX_VEB; i++) {
15882 		if (!pf->veb[i])
15883 			continue;
15884 
15885 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
15886 		    pf->veb[i]->uplink_seid == 0)
15887 			i40e_switch_branch_release(pf->veb[i]);
15888 	}
15889 
15890 	/* Now we can shutdown the PF's VSI, just before we kill
15891 	 * adminq and hmc.
15892 	 */
15893 	if (pf->vsi[pf->lan_vsi])
15894 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
15895 
15896 	i40e_cloud_filter_exit(pf);
15897 
15898 	/* remove attached clients */
15899 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15900 		ret_code = i40e_lan_del_device(pf);
15901 		if (ret_code)
15902 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
15903 				 ret_code);
15904 	}
15905 
15906 	/* shutdown and destroy the HMC */
15907 	if (hw->hmc.hmc_obj) {
15908 		ret_code = i40e_shutdown_lan_hmc(hw);
15909 		if (ret_code)
15910 			dev_warn(&pdev->dev,
15911 				 "Failed to destroy the HMC resources: %d\n",
15912 				 ret_code);
15913 	}
15914 
15915 unmap:
15916 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
15917 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
15918 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
15919 		free_irq(pf->pdev->irq, pf);
15920 
15921 	/* shutdown the adminq */
15922 	i40e_shutdown_adminq(hw);
15923 
15924 	/* destroy the locks only once, here */
15925 	mutex_destroy(&hw->aq.arq_mutex);
15926 	mutex_destroy(&hw->aq.asq_mutex);
15927 
15928 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
15929 	rtnl_lock();
15930 	i40e_clear_interrupt_scheme(pf);
15931 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15932 		if (pf->vsi[i]) {
15933 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
15934 				i40e_vsi_clear_rings(pf->vsi[i]);
15935 			i40e_vsi_clear(pf->vsi[i]);
15936 			pf->vsi[i] = NULL;
15937 		}
15938 	}
15939 	rtnl_unlock();
15940 
15941 	for (i = 0; i < I40E_MAX_VEB; i++) {
15942 		kfree(pf->veb[i]);
15943 		pf->veb[i] = NULL;
15944 	}
15945 
15946 	kfree(pf->qp_pile);
15947 	kfree(pf->vsi);
15948 
15949 	iounmap(hw->hw_addr);
15950 	kfree(pf);
15951 	pci_release_mem_regions(pdev);
15952 
15953 	pci_disable_pcie_error_reporting(pdev);
15954 	pci_disable_device(pdev);
15955 }
15956 
15957 /**
15958  * i40e_pci_error_detected - warning that something funky happened in PCI land
15959  * @pdev: PCI device information struct
15960  * @error: the type of PCI error
15961  *
15962  * Called to warn that something happened and the error handling steps
15963  * are in progress.  Allows the driver to quiesce things, be ready for
15964  * remediation.
15965  **/
15966 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
15967 						pci_channel_state_t error)
15968 {
15969 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15970 
15971 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
15972 
15973 	if (!pf) {
15974 		dev_info(&pdev->dev,
15975 			 "Cannot recover - error happened during device probe\n");
15976 		return PCI_ERS_RESULT_DISCONNECT;
15977 	}
15978 
15979 	/* shutdown all operations */
15980 	if (!test_bit(__I40E_SUSPENDED, pf->state))
15981 		i40e_prep_for_reset(pf);
15982 
15983 	/* Request a slot reset */
15984 	return PCI_ERS_RESULT_NEED_RESET;
15985 }
15986 
15987 /**
15988  * i40e_pci_error_slot_reset - a PCI slot reset just happened
15989  * @pdev: PCI device information struct
15990  *
15991  * Called to find if the driver can work with the device now that
15992  * the pci slot has been reset.  If a basic connection seems good
15993  * (registers are readable and have sane content) then return a
15994  * happy little PCI_ERS_RESULT_xxx.
15995  **/
15996 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
15997 {
15998 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15999 	pci_ers_result_t result;
16000 	u32 reg;
16001 
16002 	dev_dbg(&pdev->dev, "%s\n", __func__);
16003 	if (pci_enable_device_mem(pdev)) {
16004 		dev_info(&pdev->dev,
16005 			 "Cannot re-enable PCI device after reset.\n");
16006 		result = PCI_ERS_RESULT_DISCONNECT;
16007 	} else {
16008 		pci_set_master(pdev);
16009 		pci_restore_state(pdev);
16010 		pci_save_state(pdev);
16011 		pci_wake_from_d3(pdev, false);
16012 
16013 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16014 		if (reg == 0)
16015 			result = PCI_ERS_RESULT_RECOVERED;
16016 		else
16017 			result = PCI_ERS_RESULT_DISCONNECT;
16018 	}
16019 
16020 	return result;
16021 }
16022 
16023 /**
16024  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16025  * @pdev: PCI device information struct
16026  */
16027 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16028 {
16029 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16030 
16031 	i40e_prep_for_reset(pf);
16032 }
16033 
16034 /**
16035  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16036  * @pdev: PCI device information struct
16037  */
16038 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16039 {
16040 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16041 
16042 	i40e_reset_and_rebuild(pf, false, false);
16043 }
16044 
16045 /**
16046  * i40e_pci_error_resume - restart operations after PCI error recovery
16047  * @pdev: PCI device information struct
16048  *
16049  * Called to allow the driver to bring things back up after PCI error
16050  * and/or reset recovery has finished.
16051  **/
16052 static void i40e_pci_error_resume(struct pci_dev *pdev)
16053 {
16054 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16055 
16056 	dev_dbg(&pdev->dev, "%s\n", __func__);
16057 	if (test_bit(__I40E_SUSPENDED, pf->state))
16058 		return;
16059 
16060 	i40e_handle_reset_warning(pf, false);
16061 }
16062 
16063 /**
16064  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16065  * using the mac_address_write admin q function
16066  * @pf: pointer to i40e_pf struct
16067  **/
16068 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16069 {
16070 	struct i40e_hw *hw = &pf->hw;
16071 	i40e_status ret;
16072 	u8 mac_addr[6];
16073 	u16 flags = 0;
16074 
16075 	/* Get current MAC address in case it's an LAA */
16076 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16077 		ether_addr_copy(mac_addr,
16078 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16079 	} else {
16080 		dev_err(&pf->pdev->dev,
16081 			"Failed to retrieve MAC address; using default\n");
16082 		ether_addr_copy(mac_addr, hw->mac.addr);
16083 	}
16084 
16085 	/* The FW expects the mac address write cmd to first be called with
16086 	 * one of these flags before calling it again with the multicast
16087 	 * enable flags.
16088 	 */
16089 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16090 
16091 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16092 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16093 
16094 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16095 	if (ret) {
16096 		dev_err(&pf->pdev->dev,
16097 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16098 		return;
16099 	}
16100 
16101 	flags = I40E_AQC_MC_MAG_EN
16102 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16103 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16104 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16105 	if (ret)
16106 		dev_err(&pf->pdev->dev,
16107 			"Failed to enable Multicast Magic Packet wake up\n");
16108 }
16109 
16110 /**
16111  * i40e_shutdown - PCI callback for shutting down
16112  * @pdev: PCI device information struct
16113  **/
16114 static void i40e_shutdown(struct pci_dev *pdev)
16115 {
16116 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16117 	struct i40e_hw *hw = &pf->hw;
16118 
16119 	set_bit(__I40E_SUSPENDED, pf->state);
16120 	set_bit(__I40E_DOWN, pf->state);
16121 
16122 	del_timer_sync(&pf->service_timer);
16123 	cancel_work_sync(&pf->service_task);
16124 	i40e_cloud_filter_exit(pf);
16125 	i40e_fdir_teardown(pf);
16126 
16127 	/* Client close must be called explicitly here because the timer
16128 	 * has been stopped.
16129 	 */
16130 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16131 
16132 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16133 		i40e_enable_mc_magic_wake(pf);
16134 
16135 	i40e_prep_for_reset(pf);
16136 
16137 	wr32(hw, I40E_PFPM_APM,
16138 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16139 	wr32(hw, I40E_PFPM_WUFC,
16140 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16141 
16142 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16143 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16144 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16145 		free_irq(pf->pdev->irq, pf);
16146 
16147 	/* Since we're going to destroy queues during the
16148 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16149 	 * whole section
16150 	 */
16151 	rtnl_lock();
16152 	i40e_clear_interrupt_scheme(pf);
16153 	rtnl_unlock();
16154 
16155 	if (system_state == SYSTEM_POWER_OFF) {
16156 		pci_wake_from_d3(pdev, pf->wol_en);
16157 		pci_set_power_state(pdev, PCI_D3hot);
16158 	}
16159 }
16160 
16161 /**
16162  * i40e_suspend - PM callback for moving to D3
16163  * @dev: generic device information structure
16164  **/
16165 static int __maybe_unused i40e_suspend(struct device *dev)
16166 {
16167 	struct i40e_pf *pf = dev_get_drvdata(dev);
16168 	struct i40e_hw *hw = &pf->hw;
16169 
16170 	/* If we're already suspended, then there is nothing to do */
16171 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16172 		return 0;
16173 
16174 	set_bit(__I40E_DOWN, pf->state);
16175 
16176 	/* Ensure service task will not be running */
16177 	del_timer_sync(&pf->service_timer);
16178 	cancel_work_sync(&pf->service_task);
16179 
16180 	/* Client close must be called explicitly here because the timer
16181 	 * has been stopped.
16182 	 */
16183 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16184 
16185 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16186 		i40e_enable_mc_magic_wake(pf);
16187 
16188 	/* Since we're going to destroy queues during the
16189 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16190 	 * whole section
16191 	 */
16192 	rtnl_lock();
16193 
16194 	i40e_prep_for_reset(pf);
16195 
16196 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16197 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16198 
16199 	/* Clear the interrupt scheme and release our IRQs so that the system
16200 	 * can safely hibernate even when there are a large number of CPUs.
16201 	 * Otherwise hibernation might fail when mapping all the vectors back
16202 	 * to CPU0.
16203 	 */
16204 	i40e_clear_interrupt_scheme(pf);
16205 
16206 	rtnl_unlock();
16207 
16208 	return 0;
16209 }
16210 
16211 /**
16212  * i40e_resume - PM callback for waking up from D3
16213  * @dev: generic device information structure
16214  **/
16215 static int __maybe_unused i40e_resume(struct device *dev)
16216 {
16217 	struct i40e_pf *pf = dev_get_drvdata(dev);
16218 	int err;
16219 
16220 	/* If we're not suspended, then there is nothing to do */
16221 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16222 		return 0;
16223 
16224 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16225 	 * since we're going to be restoring queues
16226 	 */
16227 	rtnl_lock();
16228 
16229 	/* We cleared the interrupt scheme when we suspended, so we need to
16230 	 * restore it now to resume device functionality.
16231 	 */
16232 	err = i40e_restore_interrupt_scheme(pf);
16233 	if (err) {
16234 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16235 			err);
16236 	}
16237 
16238 	clear_bit(__I40E_DOWN, pf->state);
16239 	i40e_reset_and_rebuild(pf, false, true);
16240 
16241 	rtnl_unlock();
16242 
16243 	/* Clear suspended state last after everything is recovered */
16244 	clear_bit(__I40E_SUSPENDED, pf->state);
16245 
16246 	/* Restart the service task */
16247 	mod_timer(&pf->service_timer,
16248 		  round_jiffies(jiffies + pf->service_timer_period));
16249 
16250 	return 0;
16251 }
16252 
16253 static const struct pci_error_handlers i40e_err_handler = {
16254 	.error_detected = i40e_pci_error_detected,
16255 	.slot_reset = i40e_pci_error_slot_reset,
16256 	.reset_prepare = i40e_pci_error_reset_prepare,
16257 	.reset_done = i40e_pci_error_reset_done,
16258 	.resume = i40e_pci_error_resume,
16259 };
16260 
16261 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16262 
16263 static struct pci_driver i40e_driver = {
16264 	.name     = i40e_driver_name,
16265 	.id_table = i40e_pci_tbl,
16266 	.probe    = i40e_probe,
16267 	.remove   = i40e_remove,
16268 	.driver   = {
16269 		.pm = &i40e_pm_ops,
16270 	},
16271 	.shutdown = i40e_shutdown,
16272 	.err_handler = &i40e_err_handler,
16273 	.sriov_configure = i40e_pci_sriov_configure,
16274 };
16275 
16276 /**
16277  * i40e_init_module - Driver registration routine
16278  *
16279  * i40e_init_module is the first routine called when the driver is
16280  * loaded. All it does is register with the PCI subsystem.
16281  **/
16282 static int __init i40e_init_module(void)
16283 {
16284 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16285 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16286 
16287 	/* There is no need to throttle the number of active tasks because
16288 	 * each device limits its own task using a state bit for scheduling
16289 	 * the service task, and the device tasks do not interfere with each
16290 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16291 	 * since we need to be able to guarantee forward progress even under
16292 	 * memory pressure.
16293 	 */
16294 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16295 	if (!i40e_wq) {
16296 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16297 		return -ENOMEM;
16298 	}
16299 
16300 	i40e_dbg_init();
16301 	return pci_register_driver(&i40e_driver);
16302 }
16303 module_init(i40e_init_module);
16304 
16305 /**
16306  * i40e_exit_module - Driver exit cleanup routine
16307  *
16308  * i40e_exit_module is called just before the driver is removed
16309  * from memory.
16310  **/
16311 static void __exit i40e_exit_module(void)
16312 {
16313 	pci_unregister_driver(&i40e_driver);
16314 	destroy_workqueue(i40e_wq);
16315 	i40e_dbg_exit();
16316 }
16317 module_exit(i40e_exit_module);
16318