xref: /linux/drivers/net/ethernet/intel/i40e/i40e_main.c (revision 4d374ba0bf30a2a372167ee4b7cdd527e7b47b3b)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 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 
9 /* Local includes */
10 #include "i40e.h"
11 #include "i40e_diag.h"
12 #include "i40e_xsk.h"
13 #include <net/udp_tunnel.h>
14 #include <net/xdp_sock.h>
15 /* All i40e tracepoints are defined by the include below, which
16  * must be included exactly once across the whole kernel with
17  * CREATE_TRACE_POINTS defined
18  */
19 #define CREATE_TRACE_POINTS
20 #include "i40e_trace.h"
21 
22 const char i40e_driver_name[] = "i40e";
23 static const char i40e_driver_string[] =
24 			"Intel(R) Ethernet Connection XL710 Network Driver";
25 
26 #define DRV_KERN "-k"
27 
28 #define DRV_VERSION_MAJOR 2
29 #define DRV_VERSION_MINOR 8
30 #define DRV_VERSION_BUILD 20
31 #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
32 	     __stringify(DRV_VERSION_MINOR) "." \
33 	     __stringify(DRV_VERSION_BUILD)    DRV_KERN
34 const char i40e_driver_version_str[] = DRV_VERSION;
35 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
36 
37 /* a bit of forward declarations */
38 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
39 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
40 static int i40e_add_vsi(struct i40e_vsi *vsi);
41 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
42 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
43 static int i40e_setup_misc_vector(struct i40e_pf *pf);
44 static void i40e_determine_queue_usage(struct i40e_pf *pf);
45 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
46 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
47 static int i40e_reset(struct i40e_pf *pf);
48 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
49 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
50 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
51 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
52 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
53 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
54 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
55 static int i40e_get_capabilities(struct i40e_pf *pf,
56 				 enum i40e_admin_queue_opc list_type);
57 
58 
59 /* i40e_pci_tbl - PCI Device ID Table
60  *
61  * Last entry must be all 0s
62  *
63  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
64  *   Class, Class Mask, private data (not used) }
65  */
66 static const struct pci_device_id i40e_pci_tbl[] = {
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
87 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
88 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
89 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
90 	/* required last entry */
91 	{0, }
92 };
93 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
94 
95 #define I40E_MAX_VF_COUNT 128
96 static int debug = -1;
97 module_param(debug, uint, 0);
98 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
99 
100 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
101 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
102 MODULE_LICENSE("GPL v2");
103 MODULE_VERSION(DRV_VERSION);
104 
105 static struct workqueue_struct *i40e_wq;
106 
107 /**
108  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
109  * @hw:   pointer to the HW structure
110  * @mem:  ptr to mem struct to fill out
111  * @size: size of memory requested
112  * @alignment: what to align the allocation to
113  **/
114 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
115 			    u64 size, u32 alignment)
116 {
117 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
118 
119 	mem->size = ALIGN(size, alignment);
120 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
121 				     GFP_KERNEL);
122 	if (!mem->va)
123 		return -ENOMEM;
124 
125 	return 0;
126 }
127 
128 /**
129  * i40e_free_dma_mem_d - OS specific memory free for shared code
130  * @hw:   pointer to the HW structure
131  * @mem:  ptr to mem struct to free
132  **/
133 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
134 {
135 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
136 
137 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
138 	mem->va = NULL;
139 	mem->pa = 0;
140 	mem->size = 0;
141 
142 	return 0;
143 }
144 
145 /**
146  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
147  * @hw:   pointer to the HW structure
148  * @mem:  ptr to mem struct to fill out
149  * @size: size of memory requested
150  **/
151 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
152 			     u32 size)
153 {
154 	mem->size = size;
155 	mem->va = kzalloc(size, GFP_KERNEL);
156 
157 	if (!mem->va)
158 		return -ENOMEM;
159 
160 	return 0;
161 }
162 
163 /**
164  * i40e_free_virt_mem_d - OS specific memory free for shared code
165  * @hw:   pointer to the HW structure
166  * @mem:  ptr to mem struct to free
167  **/
168 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
169 {
170 	/* it's ok to kfree a NULL pointer */
171 	kfree(mem->va);
172 	mem->va = NULL;
173 	mem->size = 0;
174 
175 	return 0;
176 }
177 
178 /**
179  * i40e_get_lump - find a lump of free generic resource
180  * @pf: board private structure
181  * @pile: the pile of resource to search
182  * @needed: the number of items needed
183  * @id: an owner id to stick on the items assigned
184  *
185  * Returns the base item index of the lump, or negative for error
186  *
187  * The search_hint trick and lack of advanced fit-finding only work
188  * because we're highly likely to have all the same size lump requests.
189  * Linear search time and any fragmentation should be minimal.
190  **/
191 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
192 			 u16 needed, u16 id)
193 {
194 	int ret = -ENOMEM;
195 	int i, j;
196 
197 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
198 		dev_info(&pf->pdev->dev,
199 			 "param err: pile=%s needed=%d id=0x%04x\n",
200 			 pile ? "<valid>" : "<null>", needed, id);
201 		return -EINVAL;
202 	}
203 
204 	/* start the linear search with an imperfect hint */
205 	i = pile->search_hint;
206 	while (i < pile->num_entries) {
207 		/* skip already allocated entries */
208 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
209 			i++;
210 			continue;
211 		}
212 
213 		/* do we have enough in this lump? */
214 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
215 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
216 				break;
217 		}
218 
219 		if (j == needed) {
220 			/* there was enough, so assign it to the requestor */
221 			for (j = 0; j < needed; j++)
222 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
223 			ret = i;
224 			pile->search_hint = i + j;
225 			break;
226 		}
227 
228 		/* not enough, so skip over it and continue looking */
229 		i += j;
230 	}
231 
232 	return ret;
233 }
234 
235 /**
236  * i40e_put_lump - return a lump of generic resource
237  * @pile: the pile of resource to search
238  * @index: the base item index
239  * @id: the owner id of the items assigned
240  *
241  * Returns the count of items in the lump
242  **/
243 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
244 {
245 	int valid_id = (id | I40E_PILE_VALID_BIT);
246 	int count = 0;
247 	int i;
248 
249 	if (!pile || index >= pile->num_entries)
250 		return -EINVAL;
251 
252 	for (i = index;
253 	     i < pile->num_entries && pile->list[i] == valid_id;
254 	     i++) {
255 		pile->list[i] = 0;
256 		count++;
257 	}
258 
259 	if (count && index < pile->search_hint)
260 		pile->search_hint = index;
261 
262 	return count;
263 }
264 
265 /**
266  * i40e_find_vsi_from_id - searches for the vsi with the given id
267  * @pf: the pf structure to search for the vsi
268  * @id: id of the vsi it is searching for
269  **/
270 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
271 {
272 	int i;
273 
274 	for (i = 0; i < pf->num_alloc_vsi; i++)
275 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
276 			return pf->vsi[i];
277 
278 	return NULL;
279 }
280 
281 /**
282  * i40e_service_event_schedule - Schedule the service task to wake up
283  * @pf: board private structure
284  *
285  * If not already scheduled, this puts the task into the work queue
286  **/
287 void i40e_service_event_schedule(struct i40e_pf *pf)
288 {
289 	if ((!test_bit(__I40E_DOWN, pf->state) &&
290 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
291 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
292 		queue_work(i40e_wq, &pf->service_task);
293 }
294 
295 /**
296  * i40e_tx_timeout - Respond to a Tx Hang
297  * @netdev: network interface device structure
298  *
299  * If any port has noticed a Tx timeout, it is likely that the whole
300  * device is munged, not just the one netdev port, so go for the full
301  * reset.
302  **/
303 static void i40e_tx_timeout(struct net_device *netdev)
304 {
305 	struct i40e_netdev_priv *np = netdev_priv(netdev);
306 	struct i40e_vsi *vsi = np->vsi;
307 	struct i40e_pf *pf = vsi->back;
308 	struct i40e_ring *tx_ring = NULL;
309 	unsigned int i, hung_queue = 0;
310 	u32 head, val;
311 
312 	pf->tx_timeout_count++;
313 
314 	/* find the stopped queue the same way the stack does */
315 	for (i = 0; i < netdev->num_tx_queues; i++) {
316 		struct netdev_queue *q;
317 		unsigned long trans_start;
318 
319 		q = netdev_get_tx_queue(netdev, i);
320 		trans_start = q->trans_start;
321 		if (netif_xmit_stopped(q) &&
322 		    time_after(jiffies,
323 			       (trans_start + netdev->watchdog_timeo))) {
324 			hung_queue = i;
325 			break;
326 		}
327 	}
328 
329 	if (i == netdev->num_tx_queues) {
330 		netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
331 	} else {
332 		/* now that we have an index, find the tx_ring struct */
333 		for (i = 0; i < vsi->num_queue_pairs; i++) {
334 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
335 				if (hung_queue ==
336 				    vsi->tx_rings[i]->queue_index) {
337 					tx_ring = vsi->tx_rings[i];
338 					break;
339 				}
340 			}
341 		}
342 	}
343 
344 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
345 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
346 	else if (time_before(jiffies,
347 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
348 		return;   /* don't do any new action before the next timeout */
349 
350 	/* don't kick off another recovery if one is already pending */
351 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
352 		return;
353 
354 	if (tx_ring) {
355 		head = i40e_get_head(tx_ring);
356 		/* Read interrupt register */
357 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
358 			val = rd32(&pf->hw,
359 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
360 						tx_ring->vsi->base_vector - 1));
361 		else
362 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
363 
364 		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",
365 			    vsi->seid, hung_queue, tx_ring->next_to_clean,
366 			    head, tx_ring->next_to_use,
367 			    readl(tx_ring->tail), val);
368 	}
369 
370 	pf->tx_timeout_last_recovery = jiffies;
371 	netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
372 		    pf->tx_timeout_recovery_level, hung_queue);
373 
374 	switch (pf->tx_timeout_recovery_level) {
375 	case 1:
376 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
377 		break;
378 	case 2:
379 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
380 		break;
381 	case 3:
382 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
383 		break;
384 	default:
385 		netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
386 		break;
387 	}
388 
389 	i40e_service_event_schedule(pf);
390 	pf->tx_timeout_recovery_level++;
391 }
392 
393 /**
394  * i40e_get_vsi_stats_struct - Get System Network Statistics
395  * @vsi: the VSI we care about
396  *
397  * Returns the address of the device statistics structure.
398  * The statistics are actually updated from the service task.
399  **/
400 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
401 {
402 	return &vsi->net_stats;
403 }
404 
405 /**
406  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
407  * @ring: Tx ring to get statistics from
408  * @stats: statistics entry to be updated
409  **/
410 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
411 					    struct rtnl_link_stats64 *stats)
412 {
413 	u64 bytes, packets;
414 	unsigned int start;
415 
416 	do {
417 		start = u64_stats_fetch_begin_irq(&ring->syncp);
418 		packets = ring->stats.packets;
419 		bytes   = ring->stats.bytes;
420 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
421 
422 	stats->tx_packets += packets;
423 	stats->tx_bytes   += bytes;
424 }
425 
426 /**
427  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
428  * @netdev: network interface device structure
429  * @stats: data structure to store statistics
430  *
431  * Returns the address of the device statistics structure.
432  * The statistics are actually updated from the service task.
433  **/
434 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
435 				  struct rtnl_link_stats64 *stats)
436 {
437 	struct i40e_netdev_priv *np = netdev_priv(netdev);
438 	struct i40e_vsi *vsi = np->vsi;
439 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
440 	struct i40e_ring *ring;
441 	int i;
442 
443 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
444 		return;
445 
446 	if (!vsi->tx_rings)
447 		return;
448 
449 	rcu_read_lock();
450 	for (i = 0; i < vsi->num_queue_pairs; i++) {
451 		u64 bytes, packets;
452 		unsigned int start;
453 
454 		ring = READ_ONCE(vsi->tx_rings[i]);
455 		if (!ring)
456 			continue;
457 		i40e_get_netdev_stats_struct_tx(ring, stats);
458 
459 		if (i40e_enabled_xdp_vsi(vsi)) {
460 			ring++;
461 			i40e_get_netdev_stats_struct_tx(ring, stats);
462 		}
463 
464 		ring++;
465 		do {
466 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
467 			packets = ring->stats.packets;
468 			bytes   = ring->stats.bytes;
469 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
470 
471 		stats->rx_packets += packets;
472 		stats->rx_bytes   += bytes;
473 
474 	}
475 	rcu_read_unlock();
476 
477 	/* following stats updated by i40e_watchdog_subtask() */
478 	stats->multicast	= vsi_stats->multicast;
479 	stats->tx_errors	= vsi_stats->tx_errors;
480 	stats->tx_dropped	= vsi_stats->tx_dropped;
481 	stats->rx_errors	= vsi_stats->rx_errors;
482 	stats->rx_dropped	= vsi_stats->rx_dropped;
483 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
484 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
485 }
486 
487 /**
488  * i40e_vsi_reset_stats - Resets all stats of the given vsi
489  * @vsi: the VSI to have its stats reset
490  **/
491 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
492 {
493 	struct rtnl_link_stats64 *ns;
494 	int i;
495 
496 	if (!vsi)
497 		return;
498 
499 	ns = i40e_get_vsi_stats_struct(vsi);
500 	memset(ns, 0, sizeof(*ns));
501 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
502 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
503 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
504 	if (vsi->rx_rings && vsi->rx_rings[0]) {
505 		for (i = 0; i < vsi->num_queue_pairs; i++) {
506 			memset(&vsi->rx_rings[i]->stats, 0,
507 			       sizeof(vsi->rx_rings[i]->stats));
508 			memset(&vsi->rx_rings[i]->rx_stats, 0,
509 			       sizeof(vsi->rx_rings[i]->rx_stats));
510 			memset(&vsi->tx_rings[i]->stats, 0,
511 			       sizeof(vsi->tx_rings[i]->stats));
512 			memset(&vsi->tx_rings[i]->tx_stats, 0,
513 			       sizeof(vsi->tx_rings[i]->tx_stats));
514 		}
515 	}
516 	vsi->stat_offsets_loaded = false;
517 }
518 
519 /**
520  * i40e_pf_reset_stats - Reset all of the stats for the given PF
521  * @pf: the PF to be reset
522  **/
523 void i40e_pf_reset_stats(struct i40e_pf *pf)
524 {
525 	int i;
526 
527 	memset(&pf->stats, 0, sizeof(pf->stats));
528 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
529 	pf->stat_offsets_loaded = false;
530 
531 	for (i = 0; i < I40E_MAX_VEB; i++) {
532 		if (pf->veb[i]) {
533 			memset(&pf->veb[i]->stats, 0,
534 			       sizeof(pf->veb[i]->stats));
535 			memset(&pf->veb[i]->stats_offsets, 0,
536 			       sizeof(pf->veb[i]->stats_offsets));
537 			pf->veb[i]->stat_offsets_loaded = false;
538 		}
539 	}
540 	pf->hw_csum_rx_error = 0;
541 }
542 
543 /**
544  * i40e_stat_update48 - read and update a 48 bit stat from the chip
545  * @hw: ptr to the hardware info
546  * @hireg: the high 32 bit reg to read
547  * @loreg: the low 32 bit reg to read
548  * @offset_loaded: has the initial offset been loaded yet
549  * @offset: ptr to current offset value
550  * @stat: ptr to the stat
551  *
552  * Since the device stats are not reset at PFReset, they likely will not
553  * be zeroed when the driver starts.  We'll save the first values read
554  * and use them as offsets to be subtracted from the raw values in order
555  * to report stats that count from zero.  In the process, we also manage
556  * the potential roll-over.
557  **/
558 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
559 			       bool offset_loaded, u64 *offset, u64 *stat)
560 {
561 	u64 new_data;
562 
563 	if (hw->device_id == I40E_DEV_ID_QEMU) {
564 		new_data = rd32(hw, loreg);
565 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
566 	} else {
567 		new_data = rd64(hw, loreg);
568 	}
569 	if (!offset_loaded)
570 		*offset = new_data;
571 	if (likely(new_data >= *offset))
572 		*stat = new_data - *offset;
573 	else
574 		*stat = (new_data + BIT_ULL(48)) - *offset;
575 	*stat &= 0xFFFFFFFFFFFFULL;
576 }
577 
578 /**
579  * i40e_stat_update32 - read and update a 32 bit stat from the chip
580  * @hw: ptr to the hardware info
581  * @reg: the hw reg to read
582  * @offset_loaded: has the initial offset been loaded yet
583  * @offset: ptr to current offset value
584  * @stat: ptr to the stat
585  **/
586 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
587 			       bool offset_loaded, u64 *offset, u64 *stat)
588 {
589 	u32 new_data;
590 
591 	new_data = rd32(hw, reg);
592 	if (!offset_loaded)
593 		*offset = new_data;
594 	if (likely(new_data >= *offset))
595 		*stat = (u32)(new_data - *offset);
596 	else
597 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
598 }
599 
600 /**
601  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
602  * @hw: ptr to the hardware info
603  * @reg: the hw reg to read and clear
604  * @stat: ptr to the stat
605  **/
606 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
607 {
608 	u32 new_data = rd32(hw, reg);
609 
610 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
611 	*stat += new_data;
612 }
613 
614 /**
615  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
616  * @vsi: the VSI to be updated
617  **/
618 void i40e_update_eth_stats(struct i40e_vsi *vsi)
619 {
620 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
621 	struct i40e_pf *pf = vsi->back;
622 	struct i40e_hw *hw = &pf->hw;
623 	struct i40e_eth_stats *oes;
624 	struct i40e_eth_stats *es;     /* device's eth stats */
625 
626 	es = &vsi->eth_stats;
627 	oes = &vsi->eth_stats_offsets;
628 
629 	/* Gather up the stats that the hw collects */
630 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
631 			   vsi->stat_offsets_loaded,
632 			   &oes->tx_errors, &es->tx_errors);
633 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
634 			   vsi->stat_offsets_loaded,
635 			   &oes->rx_discards, &es->rx_discards);
636 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
637 			   vsi->stat_offsets_loaded,
638 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
639 
640 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
641 			   I40E_GLV_GORCL(stat_idx),
642 			   vsi->stat_offsets_loaded,
643 			   &oes->rx_bytes, &es->rx_bytes);
644 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
645 			   I40E_GLV_UPRCL(stat_idx),
646 			   vsi->stat_offsets_loaded,
647 			   &oes->rx_unicast, &es->rx_unicast);
648 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
649 			   I40E_GLV_MPRCL(stat_idx),
650 			   vsi->stat_offsets_loaded,
651 			   &oes->rx_multicast, &es->rx_multicast);
652 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
653 			   I40E_GLV_BPRCL(stat_idx),
654 			   vsi->stat_offsets_loaded,
655 			   &oes->rx_broadcast, &es->rx_broadcast);
656 
657 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
658 			   I40E_GLV_GOTCL(stat_idx),
659 			   vsi->stat_offsets_loaded,
660 			   &oes->tx_bytes, &es->tx_bytes);
661 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
662 			   I40E_GLV_UPTCL(stat_idx),
663 			   vsi->stat_offsets_loaded,
664 			   &oes->tx_unicast, &es->tx_unicast);
665 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
666 			   I40E_GLV_MPTCL(stat_idx),
667 			   vsi->stat_offsets_loaded,
668 			   &oes->tx_multicast, &es->tx_multicast);
669 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
670 			   I40E_GLV_BPTCL(stat_idx),
671 			   vsi->stat_offsets_loaded,
672 			   &oes->tx_broadcast, &es->tx_broadcast);
673 	vsi->stat_offsets_loaded = true;
674 }
675 
676 /**
677  * i40e_update_veb_stats - Update Switch component statistics
678  * @veb: the VEB being updated
679  **/
680 void i40e_update_veb_stats(struct i40e_veb *veb)
681 {
682 	struct i40e_pf *pf = veb->pf;
683 	struct i40e_hw *hw = &pf->hw;
684 	struct i40e_eth_stats *oes;
685 	struct i40e_eth_stats *es;     /* device's eth stats */
686 	struct i40e_veb_tc_stats *veb_oes;
687 	struct i40e_veb_tc_stats *veb_es;
688 	int i, idx = 0;
689 
690 	idx = veb->stats_idx;
691 	es = &veb->stats;
692 	oes = &veb->stats_offsets;
693 	veb_es = &veb->tc_stats;
694 	veb_oes = &veb->tc_stats_offsets;
695 
696 	/* Gather up the stats that the hw collects */
697 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
698 			   veb->stat_offsets_loaded,
699 			   &oes->tx_discards, &es->tx_discards);
700 	if (hw->revision_id > 0)
701 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
702 				   veb->stat_offsets_loaded,
703 				   &oes->rx_unknown_protocol,
704 				   &es->rx_unknown_protocol);
705 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
706 			   veb->stat_offsets_loaded,
707 			   &oes->rx_bytes, &es->rx_bytes);
708 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
709 			   veb->stat_offsets_loaded,
710 			   &oes->rx_unicast, &es->rx_unicast);
711 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
712 			   veb->stat_offsets_loaded,
713 			   &oes->rx_multicast, &es->rx_multicast);
714 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
715 			   veb->stat_offsets_loaded,
716 			   &oes->rx_broadcast, &es->rx_broadcast);
717 
718 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
719 			   veb->stat_offsets_loaded,
720 			   &oes->tx_bytes, &es->tx_bytes);
721 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
722 			   veb->stat_offsets_loaded,
723 			   &oes->tx_unicast, &es->tx_unicast);
724 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
725 			   veb->stat_offsets_loaded,
726 			   &oes->tx_multicast, &es->tx_multicast);
727 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
728 			   veb->stat_offsets_loaded,
729 			   &oes->tx_broadcast, &es->tx_broadcast);
730 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
731 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
732 				   I40E_GLVEBTC_RPCL(i, idx),
733 				   veb->stat_offsets_loaded,
734 				   &veb_oes->tc_rx_packets[i],
735 				   &veb_es->tc_rx_packets[i]);
736 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
737 				   I40E_GLVEBTC_RBCL(i, idx),
738 				   veb->stat_offsets_loaded,
739 				   &veb_oes->tc_rx_bytes[i],
740 				   &veb_es->tc_rx_bytes[i]);
741 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
742 				   I40E_GLVEBTC_TPCL(i, idx),
743 				   veb->stat_offsets_loaded,
744 				   &veb_oes->tc_tx_packets[i],
745 				   &veb_es->tc_tx_packets[i]);
746 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
747 				   I40E_GLVEBTC_TBCL(i, idx),
748 				   veb->stat_offsets_loaded,
749 				   &veb_oes->tc_tx_bytes[i],
750 				   &veb_es->tc_tx_bytes[i]);
751 	}
752 	veb->stat_offsets_loaded = true;
753 }
754 
755 /**
756  * i40e_update_vsi_stats - Update the vsi statistics counters.
757  * @vsi: the VSI to be updated
758  *
759  * There are a few instances where we store the same stat in a
760  * couple of different structs.  This is partly because we have
761  * the netdev stats that need to be filled out, which is slightly
762  * different from the "eth_stats" defined by the chip and used in
763  * VF communications.  We sort it out here.
764  **/
765 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
766 {
767 	struct i40e_pf *pf = vsi->back;
768 	struct rtnl_link_stats64 *ons;
769 	struct rtnl_link_stats64 *ns;   /* netdev stats */
770 	struct i40e_eth_stats *oes;
771 	struct i40e_eth_stats *es;     /* device's eth stats */
772 	u32 tx_restart, tx_busy;
773 	struct i40e_ring *p;
774 	u32 rx_page, rx_buf;
775 	u64 bytes, packets;
776 	unsigned int start;
777 	u64 tx_linearize;
778 	u64 tx_force_wb;
779 	u64 rx_p, rx_b;
780 	u64 tx_p, tx_b;
781 	u16 q;
782 
783 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
784 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
785 		return;
786 
787 	ns = i40e_get_vsi_stats_struct(vsi);
788 	ons = &vsi->net_stats_offsets;
789 	es = &vsi->eth_stats;
790 	oes = &vsi->eth_stats_offsets;
791 
792 	/* Gather up the netdev and vsi stats that the driver collects
793 	 * on the fly during packet processing
794 	 */
795 	rx_b = rx_p = 0;
796 	tx_b = tx_p = 0;
797 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
798 	rx_page = 0;
799 	rx_buf = 0;
800 	rcu_read_lock();
801 	for (q = 0; q < vsi->num_queue_pairs; q++) {
802 		/* locate Tx ring */
803 		p = READ_ONCE(vsi->tx_rings[q]);
804 
805 		do {
806 			start = u64_stats_fetch_begin_irq(&p->syncp);
807 			packets = p->stats.packets;
808 			bytes = p->stats.bytes;
809 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
810 		tx_b += bytes;
811 		tx_p += packets;
812 		tx_restart += p->tx_stats.restart_queue;
813 		tx_busy += p->tx_stats.tx_busy;
814 		tx_linearize += p->tx_stats.tx_linearize;
815 		tx_force_wb += p->tx_stats.tx_force_wb;
816 
817 		/* Rx queue is part of the same block as Tx queue */
818 		p = &p[1];
819 		do {
820 			start = u64_stats_fetch_begin_irq(&p->syncp);
821 			packets = p->stats.packets;
822 			bytes = p->stats.bytes;
823 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
824 		rx_b += bytes;
825 		rx_p += packets;
826 		rx_buf += p->rx_stats.alloc_buff_failed;
827 		rx_page += p->rx_stats.alloc_page_failed;
828 	}
829 	rcu_read_unlock();
830 	vsi->tx_restart = tx_restart;
831 	vsi->tx_busy = tx_busy;
832 	vsi->tx_linearize = tx_linearize;
833 	vsi->tx_force_wb = tx_force_wb;
834 	vsi->rx_page_failed = rx_page;
835 	vsi->rx_buf_failed = rx_buf;
836 
837 	ns->rx_packets = rx_p;
838 	ns->rx_bytes = rx_b;
839 	ns->tx_packets = tx_p;
840 	ns->tx_bytes = tx_b;
841 
842 	/* update netdev stats from eth stats */
843 	i40e_update_eth_stats(vsi);
844 	ons->tx_errors = oes->tx_errors;
845 	ns->tx_errors = es->tx_errors;
846 	ons->multicast = oes->rx_multicast;
847 	ns->multicast = es->rx_multicast;
848 	ons->rx_dropped = oes->rx_discards;
849 	ns->rx_dropped = es->rx_discards;
850 	ons->tx_dropped = oes->tx_discards;
851 	ns->tx_dropped = es->tx_discards;
852 
853 	/* pull in a couple PF stats if this is the main vsi */
854 	if (vsi == pf->vsi[pf->lan_vsi]) {
855 		ns->rx_crc_errors = pf->stats.crc_errors;
856 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
857 		ns->rx_length_errors = pf->stats.rx_length_errors;
858 	}
859 }
860 
861 /**
862  * i40e_update_pf_stats - Update the PF statistics counters.
863  * @pf: the PF to be updated
864  **/
865 static void i40e_update_pf_stats(struct i40e_pf *pf)
866 {
867 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
868 	struct i40e_hw_port_stats *nsd = &pf->stats;
869 	struct i40e_hw *hw = &pf->hw;
870 	u32 val;
871 	int i;
872 
873 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
874 			   I40E_GLPRT_GORCL(hw->port),
875 			   pf->stat_offsets_loaded,
876 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
877 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
878 			   I40E_GLPRT_GOTCL(hw->port),
879 			   pf->stat_offsets_loaded,
880 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
881 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
882 			   pf->stat_offsets_loaded,
883 			   &osd->eth.rx_discards,
884 			   &nsd->eth.rx_discards);
885 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
886 			   I40E_GLPRT_UPRCL(hw->port),
887 			   pf->stat_offsets_loaded,
888 			   &osd->eth.rx_unicast,
889 			   &nsd->eth.rx_unicast);
890 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
891 			   I40E_GLPRT_MPRCL(hw->port),
892 			   pf->stat_offsets_loaded,
893 			   &osd->eth.rx_multicast,
894 			   &nsd->eth.rx_multicast);
895 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
896 			   I40E_GLPRT_BPRCL(hw->port),
897 			   pf->stat_offsets_loaded,
898 			   &osd->eth.rx_broadcast,
899 			   &nsd->eth.rx_broadcast);
900 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
901 			   I40E_GLPRT_UPTCL(hw->port),
902 			   pf->stat_offsets_loaded,
903 			   &osd->eth.tx_unicast,
904 			   &nsd->eth.tx_unicast);
905 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
906 			   I40E_GLPRT_MPTCL(hw->port),
907 			   pf->stat_offsets_loaded,
908 			   &osd->eth.tx_multicast,
909 			   &nsd->eth.tx_multicast);
910 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
911 			   I40E_GLPRT_BPTCL(hw->port),
912 			   pf->stat_offsets_loaded,
913 			   &osd->eth.tx_broadcast,
914 			   &nsd->eth.tx_broadcast);
915 
916 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
917 			   pf->stat_offsets_loaded,
918 			   &osd->tx_dropped_link_down,
919 			   &nsd->tx_dropped_link_down);
920 
921 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
922 			   pf->stat_offsets_loaded,
923 			   &osd->crc_errors, &nsd->crc_errors);
924 
925 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
926 			   pf->stat_offsets_loaded,
927 			   &osd->illegal_bytes, &nsd->illegal_bytes);
928 
929 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
930 			   pf->stat_offsets_loaded,
931 			   &osd->mac_local_faults,
932 			   &nsd->mac_local_faults);
933 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
934 			   pf->stat_offsets_loaded,
935 			   &osd->mac_remote_faults,
936 			   &nsd->mac_remote_faults);
937 
938 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
939 			   pf->stat_offsets_loaded,
940 			   &osd->rx_length_errors,
941 			   &nsd->rx_length_errors);
942 
943 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
944 			   pf->stat_offsets_loaded,
945 			   &osd->link_xon_rx, &nsd->link_xon_rx);
946 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
947 			   pf->stat_offsets_loaded,
948 			   &osd->link_xon_tx, &nsd->link_xon_tx);
949 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
950 			   pf->stat_offsets_loaded,
951 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
952 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
953 			   pf->stat_offsets_loaded,
954 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
955 
956 	for (i = 0; i < 8; i++) {
957 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
958 				   pf->stat_offsets_loaded,
959 				   &osd->priority_xoff_rx[i],
960 				   &nsd->priority_xoff_rx[i]);
961 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
962 				   pf->stat_offsets_loaded,
963 				   &osd->priority_xon_rx[i],
964 				   &nsd->priority_xon_rx[i]);
965 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
966 				   pf->stat_offsets_loaded,
967 				   &osd->priority_xon_tx[i],
968 				   &nsd->priority_xon_tx[i]);
969 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
970 				   pf->stat_offsets_loaded,
971 				   &osd->priority_xoff_tx[i],
972 				   &nsd->priority_xoff_tx[i]);
973 		i40e_stat_update32(hw,
974 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
975 				   pf->stat_offsets_loaded,
976 				   &osd->priority_xon_2_xoff[i],
977 				   &nsd->priority_xon_2_xoff[i]);
978 	}
979 
980 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
981 			   I40E_GLPRT_PRC64L(hw->port),
982 			   pf->stat_offsets_loaded,
983 			   &osd->rx_size_64, &nsd->rx_size_64);
984 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
985 			   I40E_GLPRT_PRC127L(hw->port),
986 			   pf->stat_offsets_loaded,
987 			   &osd->rx_size_127, &nsd->rx_size_127);
988 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
989 			   I40E_GLPRT_PRC255L(hw->port),
990 			   pf->stat_offsets_loaded,
991 			   &osd->rx_size_255, &nsd->rx_size_255);
992 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
993 			   I40E_GLPRT_PRC511L(hw->port),
994 			   pf->stat_offsets_loaded,
995 			   &osd->rx_size_511, &nsd->rx_size_511);
996 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
997 			   I40E_GLPRT_PRC1023L(hw->port),
998 			   pf->stat_offsets_loaded,
999 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1000 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1001 			   I40E_GLPRT_PRC1522L(hw->port),
1002 			   pf->stat_offsets_loaded,
1003 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1004 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1005 			   I40E_GLPRT_PRC9522L(hw->port),
1006 			   pf->stat_offsets_loaded,
1007 			   &osd->rx_size_big, &nsd->rx_size_big);
1008 
1009 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1010 			   I40E_GLPRT_PTC64L(hw->port),
1011 			   pf->stat_offsets_loaded,
1012 			   &osd->tx_size_64, &nsd->tx_size_64);
1013 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1014 			   I40E_GLPRT_PTC127L(hw->port),
1015 			   pf->stat_offsets_loaded,
1016 			   &osd->tx_size_127, &nsd->tx_size_127);
1017 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1018 			   I40E_GLPRT_PTC255L(hw->port),
1019 			   pf->stat_offsets_loaded,
1020 			   &osd->tx_size_255, &nsd->tx_size_255);
1021 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1022 			   I40E_GLPRT_PTC511L(hw->port),
1023 			   pf->stat_offsets_loaded,
1024 			   &osd->tx_size_511, &nsd->tx_size_511);
1025 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1026 			   I40E_GLPRT_PTC1023L(hw->port),
1027 			   pf->stat_offsets_loaded,
1028 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1029 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1030 			   I40E_GLPRT_PTC1522L(hw->port),
1031 			   pf->stat_offsets_loaded,
1032 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1033 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1034 			   I40E_GLPRT_PTC9522L(hw->port),
1035 			   pf->stat_offsets_loaded,
1036 			   &osd->tx_size_big, &nsd->tx_size_big);
1037 
1038 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1039 			   pf->stat_offsets_loaded,
1040 			   &osd->rx_undersize, &nsd->rx_undersize);
1041 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1042 			   pf->stat_offsets_loaded,
1043 			   &osd->rx_fragments, &nsd->rx_fragments);
1044 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1045 			   pf->stat_offsets_loaded,
1046 			   &osd->rx_oversize, &nsd->rx_oversize);
1047 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1048 			   pf->stat_offsets_loaded,
1049 			   &osd->rx_jabber, &nsd->rx_jabber);
1050 
1051 	/* FDIR stats */
1052 	i40e_stat_update_and_clear32(hw,
1053 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1054 			&nsd->fd_atr_match);
1055 	i40e_stat_update_and_clear32(hw,
1056 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1057 			&nsd->fd_sb_match);
1058 	i40e_stat_update_and_clear32(hw,
1059 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1060 			&nsd->fd_atr_tunnel_match);
1061 
1062 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1063 	nsd->tx_lpi_status =
1064 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1065 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1066 	nsd->rx_lpi_status =
1067 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1068 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1069 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1070 			   pf->stat_offsets_loaded,
1071 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1072 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1073 			   pf->stat_offsets_loaded,
1074 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1075 
1076 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1077 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1078 		nsd->fd_sb_status = true;
1079 	else
1080 		nsd->fd_sb_status = false;
1081 
1082 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1083 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1084 		nsd->fd_atr_status = true;
1085 	else
1086 		nsd->fd_atr_status = false;
1087 
1088 	pf->stat_offsets_loaded = true;
1089 }
1090 
1091 /**
1092  * i40e_update_stats - Update the various statistics counters.
1093  * @vsi: the VSI to be updated
1094  *
1095  * Update the various stats for this VSI and its related entities.
1096  **/
1097 void i40e_update_stats(struct i40e_vsi *vsi)
1098 {
1099 	struct i40e_pf *pf = vsi->back;
1100 
1101 	if (vsi == pf->vsi[pf->lan_vsi])
1102 		i40e_update_pf_stats(pf);
1103 
1104 	i40e_update_vsi_stats(vsi);
1105 }
1106 
1107 /**
1108  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1109  * @vsi: the VSI to be searched
1110  * @macaddr: the MAC address
1111  * @vlan: the vlan
1112  *
1113  * Returns ptr to the filter object or NULL
1114  **/
1115 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1116 						const u8 *macaddr, s16 vlan)
1117 {
1118 	struct i40e_mac_filter *f;
1119 	u64 key;
1120 
1121 	if (!vsi || !macaddr)
1122 		return NULL;
1123 
1124 	key = i40e_addr_to_hkey(macaddr);
1125 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1126 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1127 		    (vlan == f->vlan))
1128 			return f;
1129 	}
1130 	return NULL;
1131 }
1132 
1133 /**
1134  * i40e_find_mac - Find a mac addr in the macvlan filters list
1135  * @vsi: the VSI to be searched
1136  * @macaddr: the MAC address we are searching for
1137  *
1138  * Returns the first filter with the provided MAC address or NULL if
1139  * MAC address was not found
1140  **/
1141 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1142 {
1143 	struct i40e_mac_filter *f;
1144 	u64 key;
1145 
1146 	if (!vsi || !macaddr)
1147 		return NULL;
1148 
1149 	key = i40e_addr_to_hkey(macaddr);
1150 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1151 		if ((ether_addr_equal(macaddr, f->macaddr)))
1152 			return f;
1153 	}
1154 	return NULL;
1155 }
1156 
1157 /**
1158  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1159  * @vsi: the VSI to be searched
1160  *
1161  * Returns true if VSI is in vlan mode or false otherwise
1162  **/
1163 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1164 {
1165 	/* If we have a PVID, always operate in VLAN mode */
1166 	if (vsi->info.pvid)
1167 		return true;
1168 
1169 	/* We need to operate in VLAN mode whenever we have any filters with
1170 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1171 	 * time, incurring search cost repeatedly. However, we can notice two
1172 	 * things:
1173 	 *
1174 	 * 1) the only place where we can gain a VLAN filter is in
1175 	 *    i40e_add_filter.
1176 	 *
1177 	 * 2) the only place where filters are actually removed is in
1178 	 *    i40e_sync_filters_subtask.
1179 	 *
1180 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1181 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1182 	 * we have to perform the full search after deleting filters in
1183 	 * i40e_sync_filters_subtask, but we already have to search
1184 	 * filters here and can perform the check at the same time. This
1185 	 * results in avoiding embedding a loop for VLAN mode inside another
1186 	 * loop over all the filters, and should maintain correctness as noted
1187 	 * above.
1188 	 */
1189 	return vsi->has_vlan_filter;
1190 }
1191 
1192 /**
1193  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1194  * @vsi: the VSI to configure
1195  * @tmp_add_list: list of filters ready to be added
1196  * @tmp_del_list: list of filters ready to be deleted
1197  * @vlan_filters: the number of active VLAN filters
1198  *
1199  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1200  * behave as expected. If we have any active VLAN filters remaining or about
1201  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1202  * so that they only match against untagged traffic. If we no longer have any
1203  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1204  * so that they match against both tagged and untagged traffic. In this way,
1205  * we ensure that we correctly receive the desired traffic. This ensures that
1206  * when we have an active VLAN we will receive only untagged traffic and
1207  * traffic matching active VLANs. If we have no active VLANs then we will
1208  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1209  *
1210  * Finally, in a similar fashion, this function also corrects filters when
1211  * there is an active PVID assigned to this VSI.
1212  *
1213  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1214  *
1215  * This function is only expected to be called from within
1216  * i40e_sync_vsi_filters.
1217  *
1218  * NOTE: This function expects to be called while under the
1219  * mac_filter_hash_lock
1220  */
1221 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1222 					 struct hlist_head *tmp_add_list,
1223 					 struct hlist_head *tmp_del_list,
1224 					 int vlan_filters)
1225 {
1226 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1227 	struct i40e_mac_filter *f, *add_head;
1228 	struct i40e_new_mac_filter *new;
1229 	struct hlist_node *h;
1230 	int bkt, new_vlan;
1231 
1232 	/* To determine if a particular filter needs to be replaced we
1233 	 * have the three following conditions:
1234 	 *
1235 	 * a) if we have a PVID assigned, then all filters which are
1236 	 *    not marked as VLAN=PVID must be replaced with filters that
1237 	 *    are.
1238 	 * b) otherwise, if we have any active VLANS, all filters
1239 	 *    which are marked as VLAN=-1 must be replaced with
1240 	 *    filters marked as VLAN=0
1241 	 * c) finally, if we do not have any active VLANS, all filters
1242 	 *    which are marked as VLAN=0 must be replaced with filters
1243 	 *    marked as VLAN=-1
1244 	 */
1245 
1246 	/* Update the filters about to be added in place */
1247 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1248 		if (pvid && new->f->vlan != pvid)
1249 			new->f->vlan = pvid;
1250 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1251 			new->f->vlan = 0;
1252 		else if (!vlan_filters && new->f->vlan == 0)
1253 			new->f->vlan = I40E_VLAN_ANY;
1254 	}
1255 
1256 	/* Update the remaining active filters */
1257 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1258 		/* Combine the checks for whether a filter needs to be changed
1259 		 * and then determine the new VLAN inside the if block, in
1260 		 * order to avoid duplicating code for adding the new filter
1261 		 * then deleting the old filter.
1262 		 */
1263 		if ((pvid && f->vlan != pvid) ||
1264 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1265 		    (!vlan_filters && f->vlan == 0)) {
1266 			/* Determine the new vlan we will be adding */
1267 			if (pvid)
1268 				new_vlan = pvid;
1269 			else if (vlan_filters)
1270 				new_vlan = 0;
1271 			else
1272 				new_vlan = I40E_VLAN_ANY;
1273 
1274 			/* Create the new filter */
1275 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1276 			if (!add_head)
1277 				return -ENOMEM;
1278 
1279 			/* Create a temporary i40e_new_mac_filter */
1280 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1281 			if (!new)
1282 				return -ENOMEM;
1283 
1284 			new->f = add_head;
1285 			new->state = add_head->state;
1286 
1287 			/* Add the new filter to the tmp list */
1288 			hlist_add_head(&new->hlist, tmp_add_list);
1289 
1290 			/* Put the original filter into the delete list */
1291 			f->state = I40E_FILTER_REMOVE;
1292 			hash_del(&f->hlist);
1293 			hlist_add_head(&f->hlist, tmp_del_list);
1294 		}
1295 	}
1296 
1297 	vsi->has_vlan_filter = !!vlan_filters;
1298 
1299 	return 0;
1300 }
1301 
1302 /**
1303  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1304  * @vsi: the PF Main VSI - inappropriate for any other VSI
1305  * @macaddr: the MAC address
1306  *
1307  * Remove whatever filter the firmware set up so the driver can manage
1308  * its own filtering intelligently.
1309  **/
1310 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1311 {
1312 	struct i40e_aqc_remove_macvlan_element_data element;
1313 	struct i40e_pf *pf = vsi->back;
1314 
1315 	/* Only appropriate for the PF main VSI */
1316 	if (vsi->type != I40E_VSI_MAIN)
1317 		return;
1318 
1319 	memset(&element, 0, sizeof(element));
1320 	ether_addr_copy(element.mac_addr, macaddr);
1321 	element.vlan_tag = 0;
1322 	/* Ignore error returns, some firmware does it this way... */
1323 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1324 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1325 
1326 	memset(&element, 0, sizeof(element));
1327 	ether_addr_copy(element.mac_addr, macaddr);
1328 	element.vlan_tag = 0;
1329 	/* ...and some firmware does it this way. */
1330 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1331 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1332 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1333 }
1334 
1335 /**
1336  * i40e_add_filter - Add a mac/vlan filter to the VSI
1337  * @vsi: the VSI to be searched
1338  * @macaddr: the MAC address
1339  * @vlan: the vlan
1340  *
1341  * Returns ptr to the filter object or NULL when no memory available.
1342  *
1343  * NOTE: This function is expected to be called with mac_filter_hash_lock
1344  * being held.
1345  **/
1346 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1347 					const u8 *macaddr, s16 vlan)
1348 {
1349 	struct i40e_mac_filter *f;
1350 	u64 key;
1351 
1352 	if (!vsi || !macaddr)
1353 		return NULL;
1354 
1355 	f = i40e_find_filter(vsi, macaddr, vlan);
1356 	if (!f) {
1357 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1358 		if (!f)
1359 			return NULL;
1360 
1361 		/* Update the boolean indicating if we need to function in
1362 		 * VLAN mode.
1363 		 */
1364 		if (vlan >= 0)
1365 			vsi->has_vlan_filter = true;
1366 
1367 		ether_addr_copy(f->macaddr, macaddr);
1368 		f->vlan = vlan;
1369 		f->state = I40E_FILTER_NEW;
1370 		INIT_HLIST_NODE(&f->hlist);
1371 
1372 		key = i40e_addr_to_hkey(macaddr);
1373 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1374 
1375 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1376 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1377 	}
1378 
1379 	/* If we're asked to add a filter that has been marked for removal, it
1380 	 * is safe to simply restore it to active state. __i40e_del_filter
1381 	 * will have simply deleted any filters which were previously marked
1382 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1383 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1384 	 * task, just restore this filter to the ACTIVE state so that the
1385 	 * sync task leaves it in place
1386 	 */
1387 	if (f->state == I40E_FILTER_REMOVE)
1388 		f->state = I40E_FILTER_ACTIVE;
1389 
1390 	return f;
1391 }
1392 
1393 /**
1394  * __i40e_del_filter - Remove a specific filter from the VSI
1395  * @vsi: VSI to remove from
1396  * @f: the filter to remove from the list
1397  *
1398  * This function should be called instead of i40e_del_filter only if you know
1399  * the exact filter you will remove already, such as via i40e_find_filter or
1400  * i40e_find_mac.
1401  *
1402  * NOTE: This function is expected to be called with mac_filter_hash_lock
1403  * being held.
1404  * ANOTHER NOTE: This function MUST be called from within the context of
1405  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1406  * instead of list_for_each_entry().
1407  **/
1408 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1409 {
1410 	if (!f)
1411 		return;
1412 
1413 	/* If the filter was never added to firmware then we can just delete it
1414 	 * directly and we don't want to set the status to remove or else an
1415 	 * admin queue command will unnecessarily fire.
1416 	 */
1417 	if ((f->state == I40E_FILTER_FAILED) ||
1418 	    (f->state == I40E_FILTER_NEW)) {
1419 		hash_del(&f->hlist);
1420 		kfree(f);
1421 	} else {
1422 		f->state = I40E_FILTER_REMOVE;
1423 	}
1424 
1425 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1426 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1427 }
1428 
1429 /**
1430  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1431  * @vsi: the VSI to be searched
1432  * @macaddr: the MAC address
1433  * @vlan: the VLAN
1434  *
1435  * NOTE: This function is expected to be called with mac_filter_hash_lock
1436  * being held.
1437  * ANOTHER NOTE: This function MUST be called from within the context of
1438  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1439  * instead of list_for_each_entry().
1440  **/
1441 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1442 {
1443 	struct i40e_mac_filter *f;
1444 
1445 	if (!vsi || !macaddr)
1446 		return;
1447 
1448 	f = i40e_find_filter(vsi, macaddr, vlan);
1449 	__i40e_del_filter(vsi, f);
1450 }
1451 
1452 /**
1453  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1454  * @vsi: the VSI to be searched
1455  * @macaddr: the mac address to be filtered
1456  *
1457  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1458  * go through all the macvlan filters and add a macvlan filter for each
1459  * unique vlan that already exists. If a PVID has been assigned, instead only
1460  * add the macaddr to that VLAN.
1461  *
1462  * Returns last filter added on success, else NULL
1463  **/
1464 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1465 					    const u8 *macaddr)
1466 {
1467 	struct i40e_mac_filter *f, *add = NULL;
1468 	struct hlist_node *h;
1469 	int bkt;
1470 
1471 	if (vsi->info.pvid)
1472 		return i40e_add_filter(vsi, macaddr,
1473 				       le16_to_cpu(vsi->info.pvid));
1474 
1475 	if (!i40e_is_vsi_in_vlan(vsi))
1476 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1477 
1478 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1479 		if (f->state == I40E_FILTER_REMOVE)
1480 			continue;
1481 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1482 		if (!add)
1483 			return NULL;
1484 	}
1485 
1486 	return add;
1487 }
1488 
1489 /**
1490  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1491  * @vsi: the VSI to be searched
1492  * @macaddr: the mac address to be removed
1493  *
1494  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1495  * associated with.
1496  *
1497  * Returns 0 for success, or error
1498  **/
1499 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1500 {
1501 	struct i40e_mac_filter *f;
1502 	struct hlist_node *h;
1503 	bool found = false;
1504 	int bkt;
1505 
1506 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1507 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1508 		if (ether_addr_equal(macaddr, f->macaddr)) {
1509 			__i40e_del_filter(vsi, f);
1510 			found = true;
1511 		}
1512 	}
1513 
1514 	if (found)
1515 		return 0;
1516 	else
1517 		return -ENOENT;
1518 }
1519 
1520 /**
1521  * i40e_set_mac - NDO callback to set mac address
1522  * @netdev: network interface device structure
1523  * @p: pointer to an address structure
1524  *
1525  * Returns 0 on success, negative on failure
1526  **/
1527 static int i40e_set_mac(struct net_device *netdev, void *p)
1528 {
1529 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1530 	struct i40e_vsi *vsi = np->vsi;
1531 	struct i40e_pf *pf = vsi->back;
1532 	struct i40e_hw *hw = &pf->hw;
1533 	struct sockaddr *addr = p;
1534 
1535 	if (!is_valid_ether_addr(addr->sa_data))
1536 		return -EADDRNOTAVAIL;
1537 
1538 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1539 		netdev_info(netdev, "already using mac address %pM\n",
1540 			    addr->sa_data);
1541 		return 0;
1542 	}
1543 
1544 	if (test_bit(__I40E_DOWN, pf->state) ||
1545 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1546 		return -EADDRNOTAVAIL;
1547 
1548 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1549 		netdev_info(netdev, "returning to hw mac address %pM\n",
1550 			    hw->mac.addr);
1551 	else
1552 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1553 
1554 	/* Copy the address first, so that we avoid a possible race with
1555 	 * .set_rx_mode().
1556 	 * - Remove old address from MAC filter
1557 	 * - Copy new address
1558 	 * - Add new address to MAC filter
1559 	 */
1560 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1561 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1562 	ether_addr_copy(netdev->dev_addr, addr->sa_data);
1563 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1564 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1565 
1566 	if (vsi->type == I40E_VSI_MAIN) {
1567 		i40e_status ret;
1568 
1569 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1570 						addr->sa_data, NULL);
1571 		if (ret)
1572 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1573 				    i40e_stat_str(hw, ret),
1574 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1575 	}
1576 
1577 	/* schedule our worker thread which will take care of
1578 	 * applying the new filter changes
1579 	 */
1580 	i40e_service_event_schedule(pf);
1581 	return 0;
1582 }
1583 
1584 /**
1585  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1586  * @vsi: vsi structure
1587  * @seed: RSS hash seed
1588  **/
1589 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1590 			      u8 *lut, u16 lut_size)
1591 {
1592 	struct i40e_pf *pf = vsi->back;
1593 	struct i40e_hw *hw = &pf->hw;
1594 	int ret = 0;
1595 
1596 	if (seed) {
1597 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1598 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1599 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1600 		if (ret) {
1601 			dev_info(&pf->pdev->dev,
1602 				 "Cannot set RSS key, err %s aq_err %s\n",
1603 				 i40e_stat_str(hw, ret),
1604 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1605 			return ret;
1606 		}
1607 	}
1608 	if (lut) {
1609 		bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
1610 
1611 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1612 		if (ret) {
1613 			dev_info(&pf->pdev->dev,
1614 				 "Cannot set RSS lut, err %s aq_err %s\n",
1615 				 i40e_stat_str(hw, ret),
1616 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1617 			return ret;
1618 		}
1619 	}
1620 	return ret;
1621 }
1622 
1623 /**
1624  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1625  * @vsi: VSI structure
1626  **/
1627 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1628 {
1629 	struct i40e_pf *pf = vsi->back;
1630 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1631 	u8 *lut;
1632 	int ret;
1633 
1634 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1635 		return 0;
1636 	if (!vsi->rss_size)
1637 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1638 				      vsi->num_queue_pairs);
1639 	if (!vsi->rss_size)
1640 		return -EINVAL;
1641 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1642 	if (!lut)
1643 		return -ENOMEM;
1644 
1645 	/* Use the user configured hash keys and lookup table if there is one,
1646 	 * otherwise use default
1647 	 */
1648 	if (vsi->rss_lut_user)
1649 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1650 	else
1651 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1652 	if (vsi->rss_hkey_user)
1653 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1654 	else
1655 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1656 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1657 	kfree(lut);
1658 	return ret;
1659 }
1660 
1661 /**
1662  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1663  * @vsi: the VSI being configured,
1664  * @ctxt: VSI context structure
1665  * @enabled_tc: number of traffic classes to enable
1666  *
1667  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1668  **/
1669 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1670 					   struct i40e_vsi_context *ctxt,
1671 					   u8 enabled_tc)
1672 {
1673 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1674 	int i, override_q, pow, num_qps, ret;
1675 	u8 netdev_tc = 0, offset = 0;
1676 
1677 	if (vsi->type != I40E_VSI_MAIN)
1678 		return -EINVAL;
1679 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1680 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1681 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1682 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1683 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1684 
1685 	/* find the next higher power-of-2 of num queue pairs */
1686 	pow = ilog2(num_qps);
1687 	if (!is_power_of_2(num_qps))
1688 		pow++;
1689 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1690 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1691 
1692 	/* Setup queue offset/count for all TCs for given VSI */
1693 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1694 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1695 		/* See if the given TC is enabled for the given VSI */
1696 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1697 			offset = vsi->mqprio_qopt.qopt.offset[i];
1698 			qcount = vsi->mqprio_qopt.qopt.count[i];
1699 			if (qcount > max_qcount)
1700 				max_qcount = qcount;
1701 			vsi->tc_config.tc_info[i].qoffset = offset;
1702 			vsi->tc_config.tc_info[i].qcount = qcount;
1703 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1704 		} else {
1705 			/* TC is not enabled so set the offset to
1706 			 * default queue and allocate one queue
1707 			 * for the given TC.
1708 			 */
1709 			vsi->tc_config.tc_info[i].qoffset = 0;
1710 			vsi->tc_config.tc_info[i].qcount = 1;
1711 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1712 		}
1713 	}
1714 
1715 	/* Set actual Tx/Rx queue pairs */
1716 	vsi->num_queue_pairs = offset + qcount;
1717 
1718 	/* Setup queue TC[0].qmap for given VSI context */
1719 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1720 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1721 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1722 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1723 
1724 	/* Reconfigure RSS for main VSI with max queue count */
1725 	vsi->rss_size = max_qcount;
1726 	ret = i40e_vsi_config_rss(vsi);
1727 	if (ret) {
1728 		dev_info(&vsi->back->pdev->dev,
1729 			 "Failed to reconfig rss for num_queues (%u)\n",
1730 			 max_qcount);
1731 		return ret;
1732 	}
1733 	vsi->reconfig_rss = true;
1734 	dev_dbg(&vsi->back->pdev->dev,
1735 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1736 
1737 	/* Find queue count available for channel VSIs and starting offset
1738 	 * for channel VSIs
1739 	 */
1740 	override_q = vsi->mqprio_qopt.qopt.count[0];
1741 	if (override_q && override_q < vsi->num_queue_pairs) {
1742 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1743 		vsi->next_base_queue = override_q;
1744 	}
1745 	return 0;
1746 }
1747 
1748 /**
1749  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1750  * @vsi: the VSI being setup
1751  * @ctxt: VSI context structure
1752  * @enabled_tc: Enabled TCs bitmap
1753  * @is_add: True if called before Add VSI
1754  *
1755  * Setup VSI queue mapping for enabled traffic classes.
1756  **/
1757 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1758 				     struct i40e_vsi_context *ctxt,
1759 				     u8 enabled_tc,
1760 				     bool is_add)
1761 {
1762 	struct i40e_pf *pf = vsi->back;
1763 	u16 sections = 0;
1764 	u8 netdev_tc = 0;
1765 	u16 numtc = 1;
1766 	u16 qcount;
1767 	u8 offset;
1768 	u16 qmap;
1769 	int i;
1770 	u16 num_tc_qps = 0;
1771 
1772 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1773 	offset = 0;
1774 
1775 	/* Number of queues per enabled TC */
1776 	num_tc_qps = vsi->alloc_queue_pairs;
1777 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1778 		/* Find numtc from enabled TC bitmap */
1779 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1780 			if (enabled_tc & BIT(i)) /* TC is enabled */
1781 				numtc++;
1782 		}
1783 		if (!numtc) {
1784 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1785 			numtc = 1;
1786 		}
1787 		num_tc_qps = num_tc_qps / numtc;
1788 		num_tc_qps = min_t(int, num_tc_qps,
1789 				   i40e_pf_get_max_q_per_tc(pf));
1790 	}
1791 
1792 	vsi->tc_config.numtc = numtc;
1793 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1794 
1795 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
1796 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1797 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
1798 
1799 	/* Setup queue offset/count for all TCs for given VSI */
1800 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1801 		/* See if the given TC is enabled for the given VSI */
1802 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1803 			/* TC is enabled */
1804 			int pow, num_qps;
1805 
1806 			switch (vsi->type) {
1807 			case I40E_VSI_MAIN:
1808 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
1809 				    I40E_FLAG_FD_ATR_ENABLED)) ||
1810 				    vsi->tc_config.enabled_tc != 1) {
1811 					qcount = min_t(int, pf->alloc_rss_size,
1812 						       num_tc_qps);
1813 					break;
1814 				}
1815 				/* fall through */
1816 			case I40E_VSI_FDIR:
1817 			case I40E_VSI_SRIOV:
1818 			case I40E_VSI_VMDQ2:
1819 			default:
1820 				qcount = num_tc_qps;
1821 				WARN_ON(i != 0);
1822 				break;
1823 			}
1824 			vsi->tc_config.tc_info[i].qoffset = offset;
1825 			vsi->tc_config.tc_info[i].qcount = qcount;
1826 
1827 			/* find the next higher power-of-2 of num queue pairs */
1828 			num_qps = qcount;
1829 			pow = 0;
1830 			while (num_qps && (BIT_ULL(pow) < qcount)) {
1831 				pow++;
1832 				num_qps >>= 1;
1833 			}
1834 
1835 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1836 			qmap =
1837 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1838 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1839 
1840 			offset += qcount;
1841 		} else {
1842 			/* TC is not enabled so set the offset to
1843 			 * default queue and allocate one queue
1844 			 * for the given TC.
1845 			 */
1846 			vsi->tc_config.tc_info[i].qoffset = 0;
1847 			vsi->tc_config.tc_info[i].qcount = 1;
1848 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1849 
1850 			qmap = 0;
1851 		}
1852 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1853 	}
1854 
1855 	/* Set actual Tx/Rx queue pairs */
1856 	vsi->num_queue_pairs = offset;
1857 	if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1858 		if (vsi->req_queue_pairs > 0)
1859 			vsi->num_queue_pairs = vsi->req_queue_pairs;
1860 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1861 			vsi->num_queue_pairs = pf->num_lan_msix;
1862 	}
1863 
1864 	/* Scheduler section valid can only be set for ADD VSI */
1865 	if (is_add) {
1866 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1867 
1868 		ctxt->info.up_enable_bits = enabled_tc;
1869 	}
1870 	if (vsi->type == I40E_VSI_SRIOV) {
1871 		ctxt->info.mapping_flags |=
1872 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1873 		for (i = 0; i < vsi->num_queue_pairs; i++)
1874 			ctxt->info.queue_mapping[i] =
1875 					       cpu_to_le16(vsi->base_queue + i);
1876 	} else {
1877 		ctxt->info.mapping_flags |=
1878 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1879 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1880 	}
1881 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1882 }
1883 
1884 /**
1885  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1886  * @netdev: the netdevice
1887  * @addr: address to add
1888  *
1889  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1890  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1891  */
1892 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1893 {
1894 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1895 	struct i40e_vsi *vsi = np->vsi;
1896 
1897 	if (i40e_add_mac_filter(vsi, addr))
1898 		return 0;
1899 	else
1900 		return -ENOMEM;
1901 }
1902 
1903 /**
1904  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1905  * @netdev: the netdevice
1906  * @addr: address to add
1907  *
1908  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1909  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1910  */
1911 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1912 {
1913 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1914 	struct i40e_vsi *vsi = np->vsi;
1915 
1916 	/* Under some circumstances, we might receive a request to delete
1917 	 * our own device address from our uc list. Because we store the
1918 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
1919 	 * such requests and not delete our device address from this list.
1920 	 */
1921 	if (ether_addr_equal(addr, netdev->dev_addr))
1922 		return 0;
1923 
1924 	i40e_del_mac_filter(vsi, addr);
1925 
1926 	return 0;
1927 }
1928 
1929 /**
1930  * i40e_set_rx_mode - NDO callback to set the netdev filters
1931  * @netdev: network interface device structure
1932  **/
1933 static void i40e_set_rx_mode(struct net_device *netdev)
1934 {
1935 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1936 	struct i40e_vsi *vsi = np->vsi;
1937 
1938 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1939 
1940 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1941 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1942 
1943 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1944 
1945 	/* check for other flag changes */
1946 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
1947 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1948 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1949 	}
1950 }
1951 
1952 /**
1953  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
1954  * @vsi: Pointer to VSI struct
1955  * @from: Pointer to list which contains MAC filter entries - changes to
1956  *        those entries needs to be undone.
1957  *
1958  * MAC filter entries from this list were slated for deletion.
1959  **/
1960 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1961 					 struct hlist_head *from)
1962 {
1963 	struct i40e_mac_filter *f;
1964 	struct hlist_node *h;
1965 
1966 	hlist_for_each_entry_safe(f, h, from, hlist) {
1967 		u64 key = i40e_addr_to_hkey(f->macaddr);
1968 
1969 		/* Move the element back into MAC filter list*/
1970 		hlist_del(&f->hlist);
1971 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1972 	}
1973 }
1974 
1975 /**
1976  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1977  * @vsi: Pointer to vsi struct
1978  * @from: Pointer to list which contains MAC filter entries - changes to
1979  *        those entries needs to be undone.
1980  *
1981  * MAC filter entries from this list were slated for addition.
1982  **/
1983 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
1984 					 struct hlist_head *from)
1985 {
1986 	struct i40e_new_mac_filter *new;
1987 	struct hlist_node *h;
1988 
1989 	hlist_for_each_entry_safe(new, h, from, hlist) {
1990 		/* We can simply free the wrapper structure */
1991 		hlist_del(&new->hlist);
1992 		kfree(new);
1993 	}
1994 }
1995 
1996 /**
1997  * i40e_next_entry - Get the next non-broadcast filter from a list
1998  * @next: pointer to filter in list
1999  *
2000  * Returns the next non-broadcast filter in the list. Required so that we
2001  * ignore broadcast filters within the list, since these are not handled via
2002  * the normal firmware update path.
2003  */
2004 static
2005 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2006 {
2007 	hlist_for_each_entry_continue(next, hlist) {
2008 		if (!is_broadcast_ether_addr(next->f->macaddr))
2009 			return next;
2010 	}
2011 
2012 	return NULL;
2013 }
2014 
2015 /**
2016  * i40e_update_filter_state - Update filter state based on return data
2017  * from firmware
2018  * @count: Number of filters added
2019  * @add_list: return data from fw
2020  * @add_head: pointer to first filter in current batch
2021  *
2022  * MAC filter entries from list were slated to be added to device. Returns
2023  * number of successful filters. Note that 0 does NOT mean success!
2024  **/
2025 static int
2026 i40e_update_filter_state(int count,
2027 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2028 			 struct i40e_new_mac_filter *add_head)
2029 {
2030 	int retval = 0;
2031 	int i;
2032 
2033 	for (i = 0; i < count; i++) {
2034 		/* Always check status of each filter. We don't need to check
2035 		 * the firmware return status because we pre-set the filter
2036 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2037 		 * request to the adminq. Thus, if it no longer matches then
2038 		 * we know the filter is active.
2039 		 */
2040 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2041 			add_head->state = I40E_FILTER_FAILED;
2042 		} else {
2043 			add_head->state = I40E_FILTER_ACTIVE;
2044 			retval++;
2045 		}
2046 
2047 		add_head = i40e_next_filter(add_head);
2048 		if (!add_head)
2049 			break;
2050 	}
2051 
2052 	return retval;
2053 }
2054 
2055 /**
2056  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2057  * @vsi: ptr to the VSI
2058  * @vsi_name: name to display in messages
2059  * @list: the list of filters to send to firmware
2060  * @num_del: the number of filters to delete
2061  * @retval: Set to -EIO on failure to delete
2062  *
2063  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2064  * *retval instead of a return value so that success does not force ret_val to
2065  * be set to 0. This ensures that a sequence of calls to this function
2066  * preserve the previous value of *retval on successful delete.
2067  */
2068 static
2069 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2070 			  struct i40e_aqc_remove_macvlan_element_data *list,
2071 			  int num_del, int *retval)
2072 {
2073 	struct i40e_hw *hw = &vsi->back->hw;
2074 	i40e_status aq_ret;
2075 	int aq_err;
2076 
2077 	aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
2078 	aq_err = hw->aq.asq_last_status;
2079 
2080 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2081 	if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
2082 		*retval = -EIO;
2083 		dev_info(&vsi->back->pdev->dev,
2084 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2085 			 vsi_name, i40e_stat_str(hw, aq_ret),
2086 			 i40e_aq_str(hw, aq_err));
2087 	}
2088 }
2089 
2090 /**
2091  * i40e_aqc_add_filters - Request firmware to add a set of filters
2092  * @vsi: ptr to the VSI
2093  * @vsi_name: name to display in messages
2094  * @list: the list of filters to send to firmware
2095  * @add_head: Position in the add hlist
2096  * @num_add: the number of filters to add
2097  *
2098  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2099  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2100  * space for more filters.
2101  */
2102 static
2103 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2104 			  struct i40e_aqc_add_macvlan_element_data *list,
2105 			  struct i40e_new_mac_filter *add_head,
2106 			  int num_add)
2107 {
2108 	struct i40e_hw *hw = &vsi->back->hw;
2109 	int aq_err, fcnt;
2110 
2111 	i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
2112 	aq_err = hw->aq.asq_last_status;
2113 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2114 
2115 	if (fcnt != num_add) {
2116 		if (vsi->type == I40E_VSI_MAIN) {
2117 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2118 			dev_warn(&vsi->back->pdev->dev,
2119 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2120 				 i40e_aq_str(hw, aq_err), vsi_name);
2121 		} else if (vsi->type == I40E_VSI_SRIOV ||
2122 			   vsi->type == I40E_VSI_VMDQ1 ||
2123 			   vsi->type == I40E_VSI_VMDQ2) {
2124 			dev_warn(&vsi->back->pdev->dev,
2125 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2126 				 i40e_aq_str(hw, aq_err), vsi_name, vsi_name);
2127 		} else {
2128 			dev_warn(&vsi->back->pdev->dev,
2129 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2130 				 i40e_aq_str(hw, aq_err), vsi_name, vsi->type);
2131 		}
2132 	}
2133 }
2134 
2135 /**
2136  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2137  * @vsi: pointer to the VSI
2138  * @vsi_name: the VSI name
2139  * @f: filter data
2140  *
2141  * This function sets or clears the promiscuous broadcast flags for VLAN
2142  * filters in order to properly receive broadcast frames. Assumes that only
2143  * broadcast filters are passed.
2144  *
2145  * Returns status indicating success or failure;
2146  **/
2147 static i40e_status
2148 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2149 			  struct i40e_mac_filter *f)
2150 {
2151 	bool enable = f->state == I40E_FILTER_NEW;
2152 	struct i40e_hw *hw = &vsi->back->hw;
2153 	i40e_status aq_ret;
2154 
2155 	if (f->vlan == I40E_VLAN_ANY) {
2156 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2157 						   vsi->seid,
2158 						   enable,
2159 						   NULL);
2160 	} else {
2161 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2162 							    vsi->seid,
2163 							    enable,
2164 							    f->vlan,
2165 							    NULL);
2166 	}
2167 
2168 	if (aq_ret) {
2169 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2170 		dev_warn(&vsi->back->pdev->dev,
2171 			 "Error %s, forcing overflow promiscuous on %s\n",
2172 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2173 			 vsi_name);
2174 	}
2175 
2176 	return aq_ret;
2177 }
2178 
2179 /**
2180  * i40e_set_promiscuous - set promiscuous mode
2181  * @pf: board private structure
2182  * @promisc: promisc on or off
2183  *
2184  * There are different ways of setting promiscuous mode on a PF depending on
2185  * what state/environment we're in.  This identifies and sets it appropriately.
2186  * Returns 0 on success.
2187  **/
2188 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2189 {
2190 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2191 	struct i40e_hw *hw = &pf->hw;
2192 	i40e_status aq_ret;
2193 
2194 	if (vsi->type == I40E_VSI_MAIN &&
2195 	    pf->lan_veb != I40E_NO_VEB &&
2196 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2197 		/* set defport ON for Main VSI instead of true promisc
2198 		 * this way we will get all unicast/multicast and VLAN
2199 		 * promisc behavior but will not get VF or VMDq traffic
2200 		 * replicated on the Main VSI.
2201 		 */
2202 		if (promisc)
2203 			aq_ret = i40e_aq_set_default_vsi(hw,
2204 							 vsi->seid,
2205 							 NULL);
2206 		else
2207 			aq_ret = i40e_aq_clear_default_vsi(hw,
2208 							   vsi->seid,
2209 							   NULL);
2210 		if (aq_ret) {
2211 			dev_info(&pf->pdev->dev,
2212 				 "Set default VSI failed, err %s, aq_err %s\n",
2213 				 i40e_stat_str(hw, aq_ret),
2214 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2215 		}
2216 	} else {
2217 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2218 						  hw,
2219 						  vsi->seid,
2220 						  promisc, NULL,
2221 						  true);
2222 		if (aq_ret) {
2223 			dev_info(&pf->pdev->dev,
2224 				 "set unicast promisc failed, err %s, aq_err %s\n",
2225 				 i40e_stat_str(hw, aq_ret),
2226 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2227 		}
2228 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2229 						  hw,
2230 						  vsi->seid,
2231 						  promisc, NULL);
2232 		if (aq_ret) {
2233 			dev_info(&pf->pdev->dev,
2234 				 "set multicast promisc failed, err %s, aq_err %s\n",
2235 				 i40e_stat_str(hw, aq_ret),
2236 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2237 		}
2238 	}
2239 
2240 	if (!aq_ret)
2241 		pf->cur_promisc = promisc;
2242 
2243 	return aq_ret;
2244 }
2245 
2246 /**
2247  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2248  * @vsi: ptr to the VSI
2249  *
2250  * Push any outstanding VSI filter changes through the AdminQ.
2251  *
2252  * Returns 0 or error value
2253  **/
2254 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2255 {
2256 	struct hlist_head tmp_add_list, tmp_del_list;
2257 	struct i40e_mac_filter *f;
2258 	struct i40e_new_mac_filter *new, *add_head = NULL;
2259 	struct i40e_hw *hw = &vsi->back->hw;
2260 	bool old_overflow, new_overflow;
2261 	unsigned int failed_filters = 0;
2262 	unsigned int vlan_filters = 0;
2263 	char vsi_name[16] = "PF";
2264 	int filter_list_len = 0;
2265 	i40e_status aq_ret = 0;
2266 	u32 changed_flags = 0;
2267 	struct hlist_node *h;
2268 	struct i40e_pf *pf;
2269 	int num_add = 0;
2270 	int num_del = 0;
2271 	int retval = 0;
2272 	u16 cmd_flags;
2273 	int list_size;
2274 	int bkt;
2275 
2276 	/* empty array typed pointers, kcalloc later */
2277 	struct i40e_aqc_add_macvlan_element_data *add_list;
2278 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2279 
2280 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2281 		usleep_range(1000, 2000);
2282 	pf = vsi->back;
2283 
2284 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2285 
2286 	if (vsi->netdev) {
2287 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2288 		vsi->current_netdev_flags = vsi->netdev->flags;
2289 	}
2290 
2291 	INIT_HLIST_HEAD(&tmp_add_list);
2292 	INIT_HLIST_HEAD(&tmp_del_list);
2293 
2294 	if (vsi->type == I40E_VSI_SRIOV)
2295 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2296 	else if (vsi->type != I40E_VSI_MAIN)
2297 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2298 
2299 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2300 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2301 
2302 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2303 		/* Create a list of filters to delete. */
2304 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2305 			if (f->state == I40E_FILTER_REMOVE) {
2306 				/* Move the element into temporary del_list */
2307 				hash_del(&f->hlist);
2308 				hlist_add_head(&f->hlist, &tmp_del_list);
2309 
2310 				/* Avoid counting removed filters */
2311 				continue;
2312 			}
2313 			if (f->state == I40E_FILTER_NEW) {
2314 				/* Create a temporary i40e_new_mac_filter */
2315 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2316 				if (!new)
2317 					goto err_no_memory_locked;
2318 
2319 				/* Store pointer to the real filter */
2320 				new->f = f;
2321 				new->state = f->state;
2322 
2323 				/* Add it to the hash list */
2324 				hlist_add_head(&new->hlist, &tmp_add_list);
2325 			}
2326 
2327 			/* Count the number of active (current and new) VLAN
2328 			 * filters we have now. Does not count filters which
2329 			 * are marked for deletion.
2330 			 */
2331 			if (f->vlan > 0)
2332 				vlan_filters++;
2333 		}
2334 
2335 		retval = i40e_correct_mac_vlan_filters(vsi,
2336 						       &tmp_add_list,
2337 						       &tmp_del_list,
2338 						       vlan_filters);
2339 		if (retval)
2340 			goto err_no_memory_locked;
2341 
2342 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2343 	}
2344 
2345 	/* Now process 'del_list' outside the lock */
2346 	if (!hlist_empty(&tmp_del_list)) {
2347 		filter_list_len = hw->aq.asq_buf_size /
2348 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2349 		list_size = filter_list_len *
2350 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2351 		del_list = kzalloc(list_size, GFP_ATOMIC);
2352 		if (!del_list)
2353 			goto err_no_memory;
2354 
2355 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2356 			cmd_flags = 0;
2357 
2358 			/* handle broadcast filters by updating the broadcast
2359 			 * promiscuous flag and release filter list.
2360 			 */
2361 			if (is_broadcast_ether_addr(f->macaddr)) {
2362 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2363 
2364 				hlist_del(&f->hlist);
2365 				kfree(f);
2366 				continue;
2367 			}
2368 
2369 			/* add to delete list */
2370 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2371 			if (f->vlan == I40E_VLAN_ANY) {
2372 				del_list[num_del].vlan_tag = 0;
2373 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2374 			} else {
2375 				del_list[num_del].vlan_tag =
2376 					cpu_to_le16((u16)(f->vlan));
2377 			}
2378 
2379 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2380 			del_list[num_del].flags = cmd_flags;
2381 			num_del++;
2382 
2383 			/* flush a full buffer */
2384 			if (num_del == filter_list_len) {
2385 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2386 						     num_del, &retval);
2387 				memset(del_list, 0, list_size);
2388 				num_del = 0;
2389 			}
2390 			/* Release memory for MAC filter entries which were
2391 			 * synced up with HW.
2392 			 */
2393 			hlist_del(&f->hlist);
2394 			kfree(f);
2395 		}
2396 
2397 		if (num_del) {
2398 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2399 					     num_del, &retval);
2400 		}
2401 
2402 		kfree(del_list);
2403 		del_list = NULL;
2404 	}
2405 
2406 	if (!hlist_empty(&tmp_add_list)) {
2407 		/* Do all the adds now. */
2408 		filter_list_len = hw->aq.asq_buf_size /
2409 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2410 		list_size = filter_list_len *
2411 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2412 		add_list = kzalloc(list_size, GFP_ATOMIC);
2413 		if (!add_list)
2414 			goto err_no_memory;
2415 
2416 		num_add = 0;
2417 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2418 			/* handle broadcast filters by updating the broadcast
2419 			 * promiscuous flag instead of adding a MAC filter.
2420 			 */
2421 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2422 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2423 							      new->f))
2424 					new->state = I40E_FILTER_FAILED;
2425 				else
2426 					new->state = I40E_FILTER_ACTIVE;
2427 				continue;
2428 			}
2429 
2430 			/* add to add array */
2431 			if (num_add == 0)
2432 				add_head = new;
2433 			cmd_flags = 0;
2434 			ether_addr_copy(add_list[num_add].mac_addr,
2435 					new->f->macaddr);
2436 			if (new->f->vlan == I40E_VLAN_ANY) {
2437 				add_list[num_add].vlan_tag = 0;
2438 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2439 			} else {
2440 				add_list[num_add].vlan_tag =
2441 					cpu_to_le16((u16)(new->f->vlan));
2442 			}
2443 			add_list[num_add].queue_number = 0;
2444 			/* set invalid match method for later detection */
2445 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2446 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2447 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2448 			num_add++;
2449 
2450 			/* flush a full buffer */
2451 			if (num_add == filter_list_len) {
2452 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2453 						     add_head, num_add);
2454 				memset(add_list, 0, list_size);
2455 				num_add = 0;
2456 			}
2457 		}
2458 		if (num_add) {
2459 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2460 					     num_add);
2461 		}
2462 		/* Now move all of the filters from the temp add list back to
2463 		 * the VSI's list.
2464 		 */
2465 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2466 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2467 			/* Only update the state if we're still NEW */
2468 			if (new->f->state == I40E_FILTER_NEW)
2469 				new->f->state = new->state;
2470 			hlist_del(&new->hlist);
2471 			kfree(new);
2472 		}
2473 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2474 		kfree(add_list);
2475 		add_list = NULL;
2476 	}
2477 
2478 	/* Determine the number of active and failed filters. */
2479 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2480 	vsi->active_filters = 0;
2481 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2482 		if (f->state == I40E_FILTER_ACTIVE)
2483 			vsi->active_filters++;
2484 		else if (f->state == I40E_FILTER_FAILED)
2485 			failed_filters++;
2486 	}
2487 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2488 
2489 	/* Check if we are able to exit overflow promiscuous mode. We can
2490 	 * safely exit if we didn't just enter, we no longer have any failed
2491 	 * filters, and we have reduced filters below the threshold value.
2492 	 */
2493 	if (old_overflow && !failed_filters &&
2494 	    vsi->active_filters < vsi->promisc_threshold) {
2495 		dev_info(&pf->pdev->dev,
2496 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2497 			 vsi_name);
2498 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2499 		vsi->promisc_threshold = 0;
2500 	}
2501 
2502 	/* if the VF is not trusted do not do promisc */
2503 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2504 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2505 		goto out;
2506 	}
2507 
2508 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2509 
2510 	/* If we are entering overflow promiscuous, we need to calculate a new
2511 	 * threshold for when we are safe to exit
2512 	 */
2513 	if (!old_overflow && new_overflow)
2514 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2515 
2516 	/* check for changes in promiscuous modes */
2517 	if (changed_flags & IFF_ALLMULTI) {
2518 		bool cur_multipromisc;
2519 
2520 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2521 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2522 							       vsi->seid,
2523 							       cur_multipromisc,
2524 							       NULL);
2525 		if (aq_ret) {
2526 			retval = i40e_aq_rc_to_posix(aq_ret,
2527 						     hw->aq.asq_last_status);
2528 			dev_info(&pf->pdev->dev,
2529 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2530 				 vsi_name,
2531 				 i40e_stat_str(hw, aq_ret),
2532 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2533 		} else {
2534 			dev_info(&pf->pdev->dev, "%s is %s allmulti mode.\n",
2535 				 vsi->netdev->name,
2536 				 cur_multipromisc ? "entering" : "leaving");
2537 		}
2538 	}
2539 
2540 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2541 		bool cur_promisc;
2542 
2543 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2544 			       new_overflow);
2545 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2546 		if (aq_ret) {
2547 			retval = i40e_aq_rc_to_posix(aq_ret,
2548 						     hw->aq.asq_last_status);
2549 			dev_info(&pf->pdev->dev,
2550 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2551 				 cur_promisc ? "on" : "off",
2552 				 vsi_name,
2553 				 i40e_stat_str(hw, aq_ret),
2554 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2555 		}
2556 	}
2557 out:
2558 	/* if something went wrong then set the changed flag so we try again */
2559 	if (retval)
2560 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2561 
2562 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2563 	return retval;
2564 
2565 err_no_memory:
2566 	/* Restore elements on the temporary add and delete lists */
2567 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2568 err_no_memory_locked:
2569 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2570 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2571 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2572 
2573 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2574 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2575 	return -ENOMEM;
2576 }
2577 
2578 /**
2579  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2580  * @pf: board private structure
2581  **/
2582 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2583 {
2584 	int v;
2585 
2586 	if (!pf)
2587 		return;
2588 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2589 		return;
2590 
2591 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2592 		if (pf->vsi[v] &&
2593 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2594 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2595 
2596 			if (ret) {
2597 				/* come back and try again later */
2598 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2599 					pf->state);
2600 				break;
2601 			}
2602 		}
2603 	}
2604 }
2605 
2606 /**
2607  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2608  * @vsi: the vsi
2609  **/
2610 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2611 {
2612 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2613 		return I40E_RXBUFFER_2048;
2614 	else
2615 		return I40E_RXBUFFER_3072;
2616 }
2617 
2618 /**
2619  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2620  * @netdev: network interface device structure
2621  * @new_mtu: new value for maximum frame size
2622  *
2623  * Returns 0 on success, negative on failure
2624  **/
2625 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2626 {
2627 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2628 	struct i40e_vsi *vsi = np->vsi;
2629 	struct i40e_pf *pf = vsi->back;
2630 
2631 	if (i40e_enabled_xdp_vsi(vsi)) {
2632 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2633 
2634 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2635 			return -EINVAL;
2636 	}
2637 
2638 	netdev_info(netdev, "changing MTU from %d to %d\n",
2639 		    netdev->mtu, new_mtu);
2640 	netdev->mtu = new_mtu;
2641 	if (netif_running(netdev))
2642 		i40e_vsi_reinit_locked(vsi);
2643 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2644 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2645 	return 0;
2646 }
2647 
2648 /**
2649  * i40e_ioctl - Access the hwtstamp interface
2650  * @netdev: network interface device structure
2651  * @ifr: interface request data
2652  * @cmd: ioctl command
2653  **/
2654 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2655 {
2656 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2657 	struct i40e_pf *pf = np->vsi->back;
2658 
2659 	switch (cmd) {
2660 	case SIOCGHWTSTAMP:
2661 		return i40e_ptp_get_ts_config(pf, ifr);
2662 	case SIOCSHWTSTAMP:
2663 		return i40e_ptp_set_ts_config(pf, ifr);
2664 	default:
2665 		return -EOPNOTSUPP;
2666 	}
2667 }
2668 
2669 /**
2670  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2671  * @vsi: the vsi being adjusted
2672  **/
2673 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2674 {
2675 	struct i40e_vsi_context ctxt;
2676 	i40e_status ret;
2677 
2678 	/* Don't modify stripping options if a port VLAN is active */
2679 	if (vsi->info.pvid)
2680 		return;
2681 
2682 	if ((vsi->info.valid_sections &
2683 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2684 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2685 		return;  /* already enabled */
2686 
2687 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2688 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2689 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2690 
2691 	ctxt.seid = vsi->seid;
2692 	ctxt.info = vsi->info;
2693 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2694 	if (ret) {
2695 		dev_info(&vsi->back->pdev->dev,
2696 			 "update vlan stripping failed, err %s aq_err %s\n",
2697 			 i40e_stat_str(&vsi->back->hw, ret),
2698 			 i40e_aq_str(&vsi->back->hw,
2699 				     vsi->back->hw.aq.asq_last_status));
2700 	}
2701 }
2702 
2703 /**
2704  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2705  * @vsi: the vsi being adjusted
2706  **/
2707 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2708 {
2709 	struct i40e_vsi_context ctxt;
2710 	i40e_status ret;
2711 
2712 	/* Don't modify stripping options if a port VLAN is active */
2713 	if (vsi->info.pvid)
2714 		return;
2715 
2716 	if ((vsi->info.valid_sections &
2717 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2718 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2719 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
2720 		return;  /* already disabled */
2721 
2722 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2723 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2724 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2725 
2726 	ctxt.seid = vsi->seid;
2727 	ctxt.info = vsi->info;
2728 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2729 	if (ret) {
2730 		dev_info(&vsi->back->pdev->dev,
2731 			 "update vlan stripping failed, err %s aq_err %s\n",
2732 			 i40e_stat_str(&vsi->back->hw, ret),
2733 			 i40e_aq_str(&vsi->back->hw,
2734 				     vsi->back->hw.aq.asq_last_status));
2735 	}
2736 }
2737 
2738 /**
2739  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2740  * @vsi: the vsi being configured
2741  * @vid: vlan id to be added (0 = untagged only , -1 = any)
2742  *
2743  * This is a helper function for adding a new MAC/VLAN filter with the
2744  * specified VLAN for each existing MAC address already in the hash table.
2745  * This function does *not* perform any accounting to update filters based on
2746  * VLAN mode.
2747  *
2748  * NOTE: this function expects to be called while under the
2749  * mac_filter_hash_lock
2750  **/
2751 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2752 {
2753 	struct i40e_mac_filter *f, *add_f;
2754 	struct hlist_node *h;
2755 	int bkt;
2756 
2757 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2758 		if (f->state == I40E_FILTER_REMOVE)
2759 			continue;
2760 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
2761 		if (!add_f) {
2762 			dev_info(&vsi->back->pdev->dev,
2763 				 "Could not add vlan filter %d for %pM\n",
2764 				 vid, f->macaddr);
2765 			return -ENOMEM;
2766 		}
2767 	}
2768 
2769 	return 0;
2770 }
2771 
2772 /**
2773  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2774  * @vsi: the VSI being configured
2775  * @vid: VLAN id to be added
2776  **/
2777 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
2778 {
2779 	int err;
2780 
2781 	if (vsi->info.pvid)
2782 		return -EINVAL;
2783 
2784 	/* The network stack will attempt to add VID=0, with the intention to
2785 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
2786 	 * these packets by default when configured to receive untagged
2787 	 * packets, so we don't need to add a filter for this case.
2788 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
2789 	 * receive *only* tagged traffic and stops receiving untagged traffic.
2790 	 * Thus, we do not want to actually add a filter for VID=0
2791 	 */
2792 	if (!vid)
2793 		return 0;
2794 
2795 	/* Locked once because all functions invoked below iterates list*/
2796 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2797 	err = i40e_add_vlan_all_mac(vsi, vid);
2798 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2799 	if (err)
2800 		return err;
2801 
2802 	/* schedule our worker thread which will take care of
2803 	 * applying the new filter changes
2804 	 */
2805 	i40e_service_event_schedule(vsi->back);
2806 	return 0;
2807 }
2808 
2809 /**
2810  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
2811  * @vsi: the vsi being configured
2812  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2813  *
2814  * This function should be used to remove all VLAN filters which match the
2815  * given VID. It does not schedule the service event and does not take the
2816  * mac_filter_hash_lock so it may be combined with other operations under
2817  * a single invocation of the mac_filter_hash_lock.
2818  *
2819  * NOTE: this function expects to be called while under the
2820  * mac_filter_hash_lock
2821  */
2822 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2823 {
2824 	struct i40e_mac_filter *f;
2825 	struct hlist_node *h;
2826 	int bkt;
2827 
2828 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2829 		if (f->vlan == vid)
2830 			__i40e_del_filter(vsi, f);
2831 	}
2832 }
2833 
2834 /**
2835  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2836  * @vsi: the VSI being configured
2837  * @vid: VLAN id to be removed
2838  **/
2839 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
2840 {
2841 	if (!vid || vsi->info.pvid)
2842 		return;
2843 
2844 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2845 	i40e_rm_vlan_all_mac(vsi, vid);
2846 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2847 
2848 	/* schedule our worker thread which will take care of
2849 	 * applying the new filter changes
2850 	 */
2851 	i40e_service_event_schedule(vsi->back);
2852 }
2853 
2854 /**
2855  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2856  * @netdev: network interface to be adjusted
2857  * @proto: unused protocol value
2858  * @vid: vlan id to be added
2859  *
2860  * net_device_ops implementation for adding vlan ids
2861  **/
2862 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2863 				__always_unused __be16 proto, u16 vid)
2864 {
2865 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2866 	struct i40e_vsi *vsi = np->vsi;
2867 	int ret = 0;
2868 
2869 	if (vid >= VLAN_N_VID)
2870 		return -EINVAL;
2871 
2872 	ret = i40e_vsi_add_vlan(vsi, vid);
2873 	if (!ret)
2874 		set_bit(vid, vsi->active_vlans);
2875 
2876 	return ret;
2877 }
2878 
2879 /**
2880  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
2881  * @netdev: network interface to be adjusted
2882  * @proto: unused protocol value
2883  * @vid: vlan id to be added
2884  **/
2885 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
2886 				    __always_unused __be16 proto, u16 vid)
2887 {
2888 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2889 	struct i40e_vsi *vsi = np->vsi;
2890 
2891 	if (vid >= VLAN_N_VID)
2892 		return;
2893 	set_bit(vid, vsi->active_vlans);
2894 }
2895 
2896 /**
2897  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2898  * @netdev: network interface to be adjusted
2899  * @proto: unused protocol value
2900  * @vid: vlan id to be removed
2901  *
2902  * net_device_ops implementation for removing vlan ids
2903  **/
2904 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2905 				 __always_unused __be16 proto, u16 vid)
2906 {
2907 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2908 	struct i40e_vsi *vsi = np->vsi;
2909 
2910 	/* return code is ignored as there is nothing a user
2911 	 * can do about failure to remove and a log message was
2912 	 * already printed from the other function
2913 	 */
2914 	i40e_vsi_kill_vlan(vsi, vid);
2915 
2916 	clear_bit(vid, vsi->active_vlans);
2917 
2918 	return 0;
2919 }
2920 
2921 /**
2922  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2923  * @vsi: the vsi being brought back up
2924  **/
2925 static void i40e_restore_vlan(struct i40e_vsi *vsi)
2926 {
2927 	u16 vid;
2928 
2929 	if (!vsi->netdev)
2930 		return;
2931 
2932 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2933 		i40e_vlan_stripping_enable(vsi);
2934 	else
2935 		i40e_vlan_stripping_disable(vsi);
2936 
2937 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2938 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
2939 					vid);
2940 }
2941 
2942 /**
2943  * i40e_vsi_add_pvid - Add pvid for the VSI
2944  * @vsi: the vsi being adjusted
2945  * @vid: the vlan id to set as a PVID
2946  **/
2947 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
2948 {
2949 	struct i40e_vsi_context ctxt;
2950 	i40e_status ret;
2951 
2952 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2953 	vsi->info.pvid = cpu_to_le16(vid);
2954 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2955 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
2956 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
2957 
2958 	ctxt.seid = vsi->seid;
2959 	ctxt.info = vsi->info;
2960 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2961 	if (ret) {
2962 		dev_info(&vsi->back->pdev->dev,
2963 			 "add pvid failed, err %s aq_err %s\n",
2964 			 i40e_stat_str(&vsi->back->hw, ret),
2965 			 i40e_aq_str(&vsi->back->hw,
2966 				     vsi->back->hw.aq.asq_last_status));
2967 		return -ENOENT;
2968 	}
2969 
2970 	return 0;
2971 }
2972 
2973 /**
2974  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2975  * @vsi: the vsi being adjusted
2976  *
2977  * Just use the vlan_rx_register() service to put it back to normal
2978  **/
2979 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2980 {
2981 	vsi->info.pvid = 0;
2982 
2983 	i40e_vlan_stripping_disable(vsi);
2984 }
2985 
2986 /**
2987  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2988  * @vsi: ptr to the VSI
2989  *
2990  * If this function returns with an error, then it's possible one or
2991  * more of the rings is populated (while the rest are not).  It is the
2992  * callers duty to clean those orphaned rings.
2993  *
2994  * Return 0 on success, negative on failure
2995  **/
2996 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2997 {
2998 	int i, err = 0;
2999 
3000 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3001 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3002 
3003 	if (!i40e_enabled_xdp_vsi(vsi))
3004 		return err;
3005 
3006 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3007 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3008 
3009 	return err;
3010 }
3011 
3012 /**
3013  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3014  * @vsi: ptr to the VSI
3015  *
3016  * Free VSI's transmit software resources
3017  **/
3018 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3019 {
3020 	int i;
3021 
3022 	if (vsi->tx_rings) {
3023 		for (i = 0; i < vsi->num_queue_pairs; i++)
3024 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3025 				i40e_free_tx_resources(vsi->tx_rings[i]);
3026 	}
3027 
3028 	if (vsi->xdp_rings) {
3029 		for (i = 0; i < vsi->num_queue_pairs; i++)
3030 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3031 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3032 	}
3033 }
3034 
3035 /**
3036  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3037  * @vsi: ptr to the VSI
3038  *
3039  * If this function returns with an error, then it's possible one or
3040  * more of the rings is populated (while the rest are not).  It is the
3041  * callers duty to clean those orphaned rings.
3042  *
3043  * Return 0 on success, negative on failure
3044  **/
3045 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3046 {
3047 	int i, err = 0;
3048 
3049 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3050 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3051 	return err;
3052 }
3053 
3054 /**
3055  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3056  * @vsi: ptr to the VSI
3057  *
3058  * Free all receive software resources
3059  **/
3060 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3061 {
3062 	int i;
3063 
3064 	if (!vsi->rx_rings)
3065 		return;
3066 
3067 	for (i = 0; i < vsi->num_queue_pairs; i++)
3068 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3069 			i40e_free_rx_resources(vsi->rx_rings[i]);
3070 }
3071 
3072 /**
3073  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3074  * @ring: The Tx ring to configure
3075  *
3076  * This enables/disables XPS for a given Tx descriptor ring
3077  * based on the TCs enabled for the VSI that ring belongs to.
3078  **/
3079 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3080 {
3081 	int cpu;
3082 
3083 	if (!ring->q_vector || !ring->netdev || ring->ch)
3084 		return;
3085 
3086 	/* We only initialize XPS once, so as not to overwrite user settings */
3087 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3088 		return;
3089 
3090 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3091 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3092 			    ring->queue_index);
3093 }
3094 
3095 /**
3096  * i40e_xsk_umem - Retrieve the AF_XDP ZC if XDP and ZC is enabled
3097  * @ring: The Tx or Rx ring
3098  *
3099  * Returns the UMEM or NULL.
3100  **/
3101 static struct xdp_umem *i40e_xsk_umem(struct i40e_ring *ring)
3102 {
3103 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3104 	int qid = ring->queue_index;
3105 
3106 	if (ring_is_xdp(ring))
3107 		qid -= ring->vsi->alloc_queue_pairs;
3108 
3109 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3110 		return NULL;
3111 
3112 	return xdp_get_umem_from_qid(ring->vsi->netdev, qid);
3113 }
3114 
3115 /**
3116  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3117  * @ring: The Tx ring to configure
3118  *
3119  * Configure the Tx descriptor ring in the HMC context.
3120  **/
3121 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3122 {
3123 	struct i40e_vsi *vsi = ring->vsi;
3124 	u16 pf_q = vsi->base_queue + ring->queue_index;
3125 	struct i40e_hw *hw = &vsi->back->hw;
3126 	struct i40e_hmc_obj_txq tx_ctx;
3127 	i40e_status err = 0;
3128 	u32 qtx_ctl = 0;
3129 
3130 	if (ring_is_xdp(ring))
3131 		ring->xsk_umem = i40e_xsk_umem(ring);
3132 
3133 	/* some ATR related tx ring init */
3134 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3135 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3136 		ring->atr_count = 0;
3137 	} else {
3138 		ring->atr_sample_rate = 0;
3139 	}
3140 
3141 	/* configure XPS */
3142 	i40e_config_xps_tx_ring(ring);
3143 
3144 	/* clear the context structure first */
3145 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3146 
3147 	tx_ctx.new_context = 1;
3148 	tx_ctx.base = (ring->dma / 128);
3149 	tx_ctx.qlen = ring->count;
3150 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3151 					       I40E_FLAG_FD_ATR_ENABLED));
3152 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3153 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3154 	if (vsi->type != I40E_VSI_FDIR)
3155 		tx_ctx.head_wb_ena = 1;
3156 	tx_ctx.head_wb_addr = ring->dma +
3157 			      (ring->count * sizeof(struct i40e_tx_desc));
3158 
3159 	/* As part of VSI creation/update, FW allocates certain
3160 	 * Tx arbitration queue sets for each TC enabled for
3161 	 * the VSI. The FW returns the handles to these queue
3162 	 * sets as part of the response buffer to Add VSI,
3163 	 * Update VSI, etc. AQ commands. It is expected that
3164 	 * these queue set handles be associated with the Tx
3165 	 * queues by the driver as part of the TX queue context
3166 	 * initialization. This has to be done regardless of
3167 	 * DCB as by default everything is mapped to TC0.
3168 	 */
3169 
3170 	if (ring->ch)
3171 		tx_ctx.rdylist =
3172 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3173 
3174 	else
3175 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3176 
3177 	tx_ctx.rdylist_act = 0;
3178 
3179 	/* clear the context in the HMC */
3180 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3181 	if (err) {
3182 		dev_info(&vsi->back->pdev->dev,
3183 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3184 			 ring->queue_index, pf_q, err);
3185 		return -ENOMEM;
3186 	}
3187 
3188 	/* set the context in the HMC */
3189 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3190 	if (err) {
3191 		dev_info(&vsi->back->pdev->dev,
3192 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3193 			 ring->queue_index, pf_q, err);
3194 		return -ENOMEM;
3195 	}
3196 
3197 	/* Now associate this queue with this PCI function */
3198 	if (ring->ch) {
3199 		if (ring->ch->type == I40E_VSI_VMDQ2)
3200 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3201 		else
3202 			return -EINVAL;
3203 
3204 		qtx_ctl |= (ring->ch->vsi_number <<
3205 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3206 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3207 	} else {
3208 		if (vsi->type == I40E_VSI_VMDQ2) {
3209 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3210 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3211 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3212 		} else {
3213 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3214 		}
3215 	}
3216 
3217 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3218 		    I40E_QTX_CTL_PF_INDX_MASK);
3219 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3220 	i40e_flush(hw);
3221 
3222 	/* cache tail off for easier writes later */
3223 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3224 
3225 	return 0;
3226 }
3227 
3228 /**
3229  * i40e_configure_rx_ring - Configure a receive ring context
3230  * @ring: The Rx ring to configure
3231  *
3232  * Configure the Rx descriptor ring in the HMC context.
3233  **/
3234 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3235 {
3236 	struct i40e_vsi *vsi = ring->vsi;
3237 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3238 	u16 pf_q = vsi->base_queue + ring->queue_index;
3239 	struct i40e_hw *hw = &vsi->back->hw;
3240 	struct i40e_hmc_obj_rxq rx_ctx;
3241 	i40e_status err = 0;
3242 	bool ok;
3243 	int ret;
3244 
3245 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3246 
3247 	/* clear the context structure first */
3248 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3249 
3250 	if (ring->vsi->type == I40E_VSI_MAIN)
3251 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3252 
3253 	ring->xsk_umem = i40e_xsk_umem(ring);
3254 	if (ring->xsk_umem) {
3255 		ring->rx_buf_len = ring->xsk_umem->chunk_size_nohr -
3256 				   XDP_PACKET_HEADROOM;
3257 		/* For AF_XDP ZC, we disallow packets to span on
3258 		 * multiple buffers, thus letting us skip that
3259 		 * handling in the fast-path.
3260 		 */
3261 		chain_len = 1;
3262 		ring->zca.free = i40e_zca_free;
3263 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3264 						 MEM_TYPE_ZERO_COPY,
3265 						 &ring->zca);
3266 		if (ret)
3267 			return ret;
3268 		dev_info(&vsi->back->pdev->dev,
3269 			 "Registered XDP mem model MEM_TYPE_ZERO_COPY on Rx ring %d\n",
3270 			 ring->queue_index);
3271 
3272 	} else {
3273 		ring->rx_buf_len = vsi->rx_buf_len;
3274 		if (ring->vsi->type == I40E_VSI_MAIN) {
3275 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3276 							 MEM_TYPE_PAGE_SHARED,
3277 							 NULL);
3278 			if (ret)
3279 				return ret;
3280 		}
3281 	}
3282 
3283 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3284 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3285 
3286 	rx_ctx.base = (ring->dma / 128);
3287 	rx_ctx.qlen = ring->count;
3288 
3289 	/* use 32 byte descriptors */
3290 	rx_ctx.dsize = 1;
3291 
3292 	/* descriptor type is always zero
3293 	 * rx_ctx.dtype = 0;
3294 	 */
3295 	rx_ctx.hsplit_0 = 0;
3296 
3297 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3298 	if (hw->revision_id == 0)
3299 		rx_ctx.lrxqthresh = 0;
3300 	else
3301 		rx_ctx.lrxqthresh = 1;
3302 	rx_ctx.crcstrip = 1;
3303 	rx_ctx.l2tsel = 1;
3304 	/* this controls whether VLAN is stripped from inner headers */
3305 	rx_ctx.showiv = 0;
3306 	/* set the prefena field to 1 because the manual says to */
3307 	rx_ctx.prefena = 1;
3308 
3309 	/* clear the context in the HMC */
3310 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3311 	if (err) {
3312 		dev_info(&vsi->back->pdev->dev,
3313 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3314 			 ring->queue_index, pf_q, err);
3315 		return -ENOMEM;
3316 	}
3317 
3318 	/* set the context in the HMC */
3319 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3320 	if (err) {
3321 		dev_info(&vsi->back->pdev->dev,
3322 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3323 			 ring->queue_index, pf_q, err);
3324 		return -ENOMEM;
3325 	}
3326 
3327 	/* configure Rx buffer alignment */
3328 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3329 		clear_ring_build_skb_enabled(ring);
3330 	else
3331 		set_ring_build_skb_enabled(ring);
3332 
3333 	/* cache tail for quicker writes, and clear the reg before use */
3334 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3335 	writel(0, ring->tail);
3336 
3337 	ok = ring->xsk_umem ?
3338 	     i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring)) :
3339 	     !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3340 	if (!ok) {
3341 		/* Log this in case the user has forgotten to give the kernel
3342 		 * any buffers, even later in the application.
3343 		 */
3344 		dev_info(&vsi->back->pdev->dev,
3345 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3346 			 ring->xsk_umem ? "UMEM enabled " : "",
3347 			 ring->queue_index, pf_q);
3348 	}
3349 
3350 	return 0;
3351 }
3352 
3353 /**
3354  * i40e_vsi_configure_tx - Configure the VSI for Tx
3355  * @vsi: VSI structure describing this set of rings and resources
3356  *
3357  * Configure the Tx VSI for operation.
3358  **/
3359 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3360 {
3361 	int err = 0;
3362 	u16 i;
3363 
3364 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3365 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3366 
3367 	if (!i40e_enabled_xdp_vsi(vsi))
3368 		return err;
3369 
3370 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3371 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3372 
3373 	return err;
3374 }
3375 
3376 /**
3377  * i40e_vsi_configure_rx - Configure the VSI for Rx
3378  * @vsi: the VSI being configured
3379  *
3380  * Configure the Rx VSI for operation.
3381  **/
3382 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3383 {
3384 	int err = 0;
3385 	u16 i;
3386 
3387 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3388 		vsi->max_frame = I40E_MAX_RXBUFFER;
3389 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3390 #if (PAGE_SIZE < 8192)
3391 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3392 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3393 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3394 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3395 #endif
3396 	} else {
3397 		vsi->max_frame = I40E_MAX_RXBUFFER;
3398 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3399 						       I40E_RXBUFFER_2048;
3400 	}
3401 
3402 	/* set up individual rings */
3403 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3404 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3405 
3406 	return err;
3407 }
3408 
3409 /**
3410  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3411  * @vsi: ptr to the VSI
3412  **/
3413 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3414 {
3415 	struct i40e_ring *tx_ring, *rx_ring;
3416 	u16 qoffset, qcount;
3417 	int i, n;
3418 
3419 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3420 		/* Reset the TC information */
3421 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3422 			rx_ring = vsi->rx_rings[i];
3423 			tx_ring = vsi->tx_rings[i];
3424 			rx_ring->dcb_tc = 0;
3425 			tx_ring->dcb_tc = 0;
3426 		}
3427 		return;
3428 	}
3429 
3430 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3431 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3432 			continue;
3433 
3434 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3435 		qcount = vsi->tc_config.tc_info[n].qcount;
3436 		for (i = qoffset; i < (qoffset + qcount); i++) {
3437 			rx_ring = vsi->rx_rings[i];
3438 			tx_ring = vsi->tx_rings[i];
3439 			rx_ring->dcb_tc = n;
3440 			tx_ring->dcb_tc = n;
3441 		}
3442 	}
3443 }
3444 
3445 /**
3446  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3447  * @vsi: ptr to the VSI
3448  **/
3449 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3450 {
3451 	if (vsi->netdev)
3452 		i40e_set_rx_mode(vsi->netdev);
3453 }
3454 
3455 /**
3456  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3457  * @vsi: Pointer to the targeted VSI
3458  *
3459  * This function replays the hlist on the hw where all the SB Flow Director
3460  * filters were saved.
3461  **/
3462 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3463 {
3464 	struct i40e_fdir_filter *filter;
3465 	struct i40e_pf *pf = vsi->back;
3466 	struct hlist_node *node;
3467 
3468 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3469 		return;
3470 
3471 	/* Reset FDir counters as we're replaying all existing filters */
3472 	pf->fd_tcp4_filter_cnt = 0;
3473 	pf->fd_udp4_filter_cnt = 0;
3474 	pf->fd_sctp4_filter_cnt = 0;
3475 	pf->fd_ip4_filter_cnt = 0;
3476 
3477 	hlist_for_each_entry_safe(filter, node,
3478 				  &pf->fdir_filter_list, fdir_node) {
3479 		i40e_add_del_fdir(vsi, filter, true);
3480 	}
3481 }
3482 
3483 /**
3484  * i40e_vsi_configure - Set up the VSI for action
3485  * @vsi: the VSI being configured
3486  **/
3487 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3488 {
3489 	int err;
3490 
3491 	i40e_set_vsi_rx_mode(vsi);
3492 	i40e_restore_vlan(vsi);
3493 	i40e_vsi_config_dcb_rings(vsi);
3494 	err = i40e_vsi_configure_tx(vsi);
3495 	if (!err)
3496 		err = i40e_vsi_configure_rx(vsi);
3497 
3498 	return err;
3499 }
3500 
3501 /**
3502  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3503  * @vsi: the VSI being configured
3504  **/
3505 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3506 {
3507 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3508 	struct i40e_pf *pf = vsi->back;
3509 	struct i40e_hw *hw = &pf->hw;
3510 	u16 vector;
3511 	int i, q;
3512 	u32 qp;
3513 
3514 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3515 	 * and PFINT_LNKLSTn registers, e.g.:
3516 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3517 	 */
3518 	qp = vsi->base_queue;
3519 	vector = vsi->base_vector;
3520 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3521 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3522 
3523 		q_vector->rx.next_update = jiffies + 1;
3524 		q_vector->rx.target_itr =
3525 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3526 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3527 		     q_vector->rx.target_itr);
3528 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3529 
3530 		q_vector->tx.next_update = jiffies + 1;
3531 		q_vector->tx.target_itr =
3532 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3533 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3534 		     q_vector->tx.target_itr);
3535 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3536 
3537 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3538 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3539 
3540 		/* Linked list for the queuepairs assigned to this vector */
3541 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3542 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3543 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3544 			u32 val;
3545 
3546 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3547 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3548 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3549 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3550 			      (I40E_QUEUE_TYPE_TX <<
3551 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3552 
3553 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3554 
3555 			if (has_xdp) {
3556 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3557 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3558 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3559 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3560 				      (I40E_QUEUE_TYPE_TX <<
3561 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3562 
3563 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3564 			}
3565 
3566 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3567 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3568 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3569 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3570 			      (I40E_QUEUE_TYPE_RX <<
3571 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3572 
3573 			/* Terminate the linked list */
3574 			if (q == (q_vector->num_ringpairs - 1))
3575 				val |= (I40E_QUEUE_END_OF_LIST <<
3576 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3577 
3578 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3579 			qp++;
3580 		}
3581 	}
3582 
3583 	i40e_flush(hw);
3584 }
3585 
3586 /**
3587  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3588  * @pf: pointer to private device data structure
3589  **/
3590 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3591 {
3592 	struct i40e_hw *hw = &pf->hw;
3593 	u32 val;
3594 
3595 	/* clear things first */
3596 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3597 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3598 
3599 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3600 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3601 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3602 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3603 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3604 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3605 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3606 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3607 
3608 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3609 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3610 
3611 	if (pf->flags & I40E_FLAG_PTP)
3612 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3613 
3614 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3615 
3616 	/* SW_ITR_IDX = 0, but don't change INTENA */
3617 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3618 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3619 
3620 	/* OTHER_ITR_IDX = 0 */
3621 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3622 }
3623 
3624 /**
3625  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3626  * @vsi: the VSI being configured
3627  **/
3628 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3629 {
3630 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3631 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3632 	struct i40e_pf *pf = vsi->back;
3633 	struct i40e_hw *hw = &pf->hw;
3634 	u32 val;
3635 
3636 	/* set the ITR configuration */
3637 	q_vector->rx.next_update = jiffies + 1;
3638 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3639 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr);
3640 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3641 	q_vector->tx.next_update = jiffies + 1;
3642 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3643 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr);
3644 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3645 
3646 	i40e_enable_misc_int_causes(pf);
3647 
3648 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3649 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3650 
3651 	/* Associate the queue pair to the vector and enable the queue int */
3652 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3653 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3654 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3655 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3656 
3657 	wr32(hw, I40E_QINT_RQCTL(0), val);
3658 
3659 	if (i40e_enabled_xdp_vsi(vsi)) {
3660 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
3661 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3662 		      (I40E_QUEUE_TYPE_TX
3663 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3664 
3665 		wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3666 	}
3667 
3668 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
3669 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3670 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3671 
3672 	wr32(hw, I40E_QINT_TQCTL(0), val);
3673 	i40e_flush(hw);
3674 }
3675 
3676 /**
3677  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3678  * @pf: board private structure
3679  **/
3680 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3681 {
3682 	struct i40e_hw *hw = &pf->hw;
3683 
3684 	wr32(hw, I40E_PFINT_DYN_CTL0,
3685 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3686 	i40e_flush(hw);
3687 }
3688 
3689 /**
3690  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3691  * @pf: board private structure
3692  **/
3693 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3694 {
3695 	struct i40e_hw *hw = &pf->hw;
3696 	u32 val;
3697 
3698 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
3699 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3700 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3701 
3702 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
3703 	i40e_flush(hw);
3704 }
3705 
3706 /**
3707  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3708  * @irq: interrupt number
3709  * @data: pointer to a q_vector
3710  **/
3711 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3712 {
3713 	struct i40e_q_vector *q_vector = data;
3714 
3715 	if (!q_vector->tx.ring && !q_vector->rx.ring)
3716 		return IRQ_HANDLED;
3717 
3718 	napi_schedule_irqoff(&q_vector->napi);
3719 
3720 	return IRQ_HANDLED;
3721 }
3722 
3723 /**
3724  * i40e_irq_affinity_notify - Callback for affinity changes
3725  * @notify: context as to what irq was changed
3726  * @mask: the new affinity mask
3727  *
3728  * This is a callback function used by the irq_set_affinity_notifier function
3729  * so that we may register to receive changes to the irq affinity masks.
3730  **/
3731 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3732 				     const cpumask_t *mask)
3733 {
3734 	struct i40e_q_vector *q_vector =
3735 		container_of(notify, struct i40e_q_vector, affinity_notify);
3736 
3737 	cpumask_copy(&q_vector->affinity_mask, mask);
3738 }
3739 
3740 /**
3741  * i40e_irq_affinity_release - Callback for affinity notifier release
3742  * @ref: internal core kernel usage
3743  *
3744  * This is a callback function used by the irq_set_affinity_notifier function
3745  * to inform the current notification subscriber that they will no longer
3746  * receive notifications.
3747  **/
3748 static void i40e_irq_affinity_release(struct kref *ref) {}
3749 
3750 /**
3751  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3752  * @vsi: the VSI being configured
3753  * @basename: name for the vector
3754  *
3755  * Allocates MSI-X vectors and requests interrupts from the kernel.
3756  **/
3757 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3758 {
3759 	int q_vectors = vsi->num_q_vectors;
3760 	struct i40e_pf *pf = vsi->back;
3761 	int base = vsi->base_vector;
3762 	int rx_int_idx = 0;
3763 	int tx_int_idx = 0;
3764 	int vector, err;
3765 	int irq_num;
3766 	int cpu;
3767 
3768 	for (vector = 0; vector < q_vectors; vector++) {
3769 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
3770 
3771 		irq_num = pf->msix_entries[base + vector].vector;
3772 
3773 		if (q_vector->tx.ring && q_vector->rx.ring) {
3774 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3775 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3776 			tx_int_idx++;
3777 		} else if (q_vector->rx.ring) {
3778 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3779 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
3780 		} else if (q_vector->tx.ring) {
3781 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3782 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
3783 		} else {
3784 			/* skip this unused q_vector */
3785 			continue;
3786 		}
3787 		err = request_irq(irq_num,
3788 				  vsi->irq_handler,
3789 				  0,
3790 				  q_vector->name,
3791 				  q_vector);
3792 		if (err) {
3793 			dev_info(&pf->pdev->dev,
3794 				 "MSIX request_irq failed, error: %d\n", err);
3795 			goto free_queue_irqs;
3796 		}
3797 
3798 		/* register for affinity change notifications */
3799 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3800 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
3801 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
3802 		/* Spread affinity hints out across online CPUs.
3803 		 *
3804 		 * get_cpu_mask returns a static constant mask with
3805 		 * a permanent lifetime so it's ok to pass to
3806 		 * irq_set_affinity_hint without making a copy.
3807 		 */
3808 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
3809 		irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
3810 	}
3811 
3812 	vsi->irqs_ready = true;
3813 	return 0;
3814 
3815 free_queue_irqs:
3816 	while (vector) {
3817 		vector--;
3818 		irq_num = pf->msix_entries[base + vector].vector;
3819 		irq_set_affinity_notifier(irq_num, NULL);
3820 		irq_set_affinity_hint(irq_num, NULL);
3821 		free_irq(irq_num, &vsi->q_vectors[vector]);
3822 	}
3823 	return err;
3824 }
3825 
3826 /**
3827  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3828  * @vsi: the VSI being un-configured
3829  **/
3830 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3831 {
3832 	struct i40e_pf *pf = vsi->back;
3833 	struct i40e_hw *hw = &pf->hw;
3834 	int base = vsi->base_vector;
3835 	int i;
3836 
3837 	/* disable interrupt causation from each queue */
3838 	for (i = 0; i < vsi->num_queue_pairs; i++) {
3839 		u32 val;
3840 
3841 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3842 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3843 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3844 
3845 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3846 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3847 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3848 
3849 		if (!i40e_enabled_xdp_vsi(vsi))
3850 			continue;
3851 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
3852 	}
3853 
3854 	/* disable each interrupt */
3855 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3856 		for (i = vsi->base_vector;
3857 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
3858 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3859 
3860 		i40e_flush(hw);
3861 		for (i = 0; i < vsi->num_q_vectors; i++)
3862 			synchronize_irq(pf->msix_entries[i + base].vector);
3863 	} else {
3864 		/* Legacy and MSI mode - this stops all interrupt handling */
3865 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3866 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3867 		i40e_flush(hw);
3868 		synchronize_irq(pf->pdev->irq);
3869 	}
3870 }
3871 
3872 /**
3873  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3874  * @vsi: the VSI being configured
3875  **/
3876 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3877 {
3878 	struct i40e_pf *pf = vsi->back;
3879 	int i;
3880 
3881 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3882 		for (i = 0; i < vsi->num_q_vectors; i++)
3883 			i40e_irq_dynamic_enable(vsi, i);
3884 	} else {
3885 		i40e_irq_dynamic_enable_icr0(pf);
3886 	}
3887 
3888 	i40e_flush(&pf->hw);
3889 	return 0;
3890 }
3891 
3892 /**
3893  * i40e_free_misc_vector - Free the vector that handles non-queue events
3894  * @pf: board private structure
3895  **/
3896 static void i40e_free_misc_vector(struct i40e_pf *pf)
3897 {
3898 	/* Disable ICR 0 */
3899 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3900 	i40e_flush(&pf->hw);
3901 
3902 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
3903 		synchronize_irq(pf->msix_entries[0].vector);
3904 		free_irq(pf->msix_entries[0].vector, pf);
3905 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
3906 	}
3907 }
3908 
3909 /**
3910  * i40e_intr - MSI/Legacy and non-queue interrupt handler
3911  * @irq: interrupt number
3912  * @data: pointer to a q_vector
3913  *
3914  * This is the handler used for all MSI/Legacy interrupts, and deals
3915  * with both queue and non-queue interrupts.  This is also used in
3916  * MSIX mode to handle the non-queue interrupts.
3917  **/
3918 static irqreturn_t i40e_intr(int irq, void *data)
3919 {
3920 	struct i40e_pf *pf = (struct i40e_pf *)data;
3921 	struct i40e_hw *hw = &pf->hw;
3922 	irqreturn_t ret = IRQ_NONE;
3923 	u32 icr0, icr0_remaining;
3924 	u32 val, ena_mask;
3925 
3926 	icr0 = rd32(hw, I40E_PFINT_ICR0);
3927 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
3928 
3929 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
3930 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
3931 		goto enable_intr;
3932 
3933 	/* if interrupt but no bits showing, must be SWINT */
3934 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3935 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3936 		pf->sw_int_count++;
3937 
3938 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3939 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3940 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3941 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
3942 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
3943 	}
3944 
3945 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3946 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
3947 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3948 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3949 
3950 		/* We do not have a way to disarm Queue causes while leaving
3951 		 * interrupt enabled for all other causes, ideally
3952 		 * interrupt should be disabled while we are in NAPI but
3953 		 * this is not a performance path and napi_schedule()
3954 		 * can deal with rescheduling.
3955 		 */
3956 		if (!test_bit(__I40E_DOWN, pf->state))
3957 			napi_schedule_irqoff(&q_vector->napi);
3958 	}
3959 
3960 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3961 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3962 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
3963 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
3964 	}
3965 
3966 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3967 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3968 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
3969 	}
3970 
3971 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3972 		ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3973 		set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
3974 	}
3975 
3976 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3977 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
3978 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
3979 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3980 		val = rd32(hw, I40E_GLGEN_RSTAT);
3981 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3982 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
3983 		if (val == I40E_RESET_CORER) {
3984 			pf->corer_count++;
3985 		} else if (val == I40E_RESET_GLOBR) {
3986 			pf->globr_count++;
3987 		} else if (val == I40E_RESET_EMPR) {
3988 			pf->empr_count++;
3989 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
3990 		}
3991 	}
3992 
3993 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3994 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3995 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
3996 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3997 			 rd32(hw, I40E_PFHMC_ERRORINFO),
3998 			 rd32(hw, I40E_PFHMC_ERRORDATA));
3999 	}
4000 
4001 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4002 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4003 
4004 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
4005 			icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4006 			i40e_ptp_tx_hwtstamp(pf);
4007 		}
4008 	}
4009 
4010 	/* If a critical error is pending we have no choice but to reset the
4011 	 * device.
4012 	 * Report and mask out any remaining unexpected interrupts.
4013 	 */
4014 	icr0_remaining = icr0 & ena_mask;
4015 	if (icr0_remaining) {
4016 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4017 			 icr0_remaining);
4018 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4019 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4020 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4021 			dev_info(&pf->pdev->dev, "device will be reset\n");
4022 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4023 			i40e_service_event_schedule(pf);
4024 		}
4025 		ena_mask &= ~icr0_remaining;
4026 	}
4027 	ret = IRQ_HANDLED;
4028 
4029 enable_intr:
4030 	/* re-enable interrupt causes */
4031 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4032 	if (!test_bit(__I40E_DOWN, pf->state) ||
4033 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4034 		i40e_service_event_schedule(pf);
4035 		i40e_irq_dynamic_enable_icr0(pf);
4036 	}
4037 
4038 	return ret;
4039 }
4040 
4041 /**
4042  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4043  * @tx_ring:  tx ring to clean
4044  * @budget:   how many cleans we're allowed
4045  *
4046  * Returns true if there's any budget left (e.g. the clean is finished)
4047  **/
4048 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4049 {
4050 	struct i40e_vsi *vsi = tx_ring->vsi;
4051 	u16 i = tx_ring->next_to_clean;
4052 	struct i40e_tx_buffer *tx_buf;
4053 	struct i40e_tx_desc *tx_desc;
4054 
4055 	tx_buf = &tx_ring->tx_bi[i];
4056 	tx_desc = I40E_TX_DESC(tx_ring, i);
4057 	i -= tx_ring->count;
4058 
4059 	do {
4060 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4061 
4062 		/* if next_to_watch is not set then there is no work pending */
4063 		if (!eop_desc)
4064 			break;
4065 
4066 		/* prevent any other reads prior to eop_desc */
4067 		smp_rmb();
4068 
4069 		/* if the descriptor isn't done, no work yet to do */
4070 		if (!(eop_desc->cmd_type_offset_bsz &
4071 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4072 			break;
4073 
4074 		/* clear next_to_watch to prevent false hangs */
4075 		tx_buf->next_to_watch = NULL;
4076 
4077 		tx_desc->buffer_addr = 0;
4078 		tx_desc->cmd_type_offset_bsz = 0;
4079 		/* move past filter desc */
4080 		tx_buf++;
4081 		tx_desc++;
4082 		i++;
4083 		if (unlikely(!i)) {
4084 			i -= tx_ring->count;
4085 			tx_buf = tx_ring->tx_bi;
4086 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4087 		}
4088 		/* unmap skb header data */
4089 		dma_unmap_single(tx_ring->dev,
4090 				 dma_unmap_addr(tx_buf, dma),
4091 				 dma_unmap_len(tx_buf, len),
4092 				 DMA_TO_DEVICE);
4093 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4094 			kfree(tx_buf->raw_buf);
4095 
4096 		tx_buf->raw_buf = NULL;
4097 		tx_buf->tx_flags = 0;
4098 		tx_buf->next_to_watch = NULL;
4099 		dma_unmap_len_set(tx_buf, len, 0);
4100 		tx_desc->buffer_addr = 0;
4101 		tx_desc->cmd_type_offset_bsz = 0;
4102 
4103 		/* move us past the eop_desc for start of next FD desc */
4104 		tx_buf++;
4105 		tx_desc++;
4106 		i++;
4107 		if (unlikely(!i)) {
4108 			i -= tx_ring->count;
4109 			tx_buf = tx_ring->tx_bi;
4110 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4111 		}
4112 
4113 		/* update budget accounting */
4114 		budget--;
4115 	} while (likely(budget));
4116 
4117 	i += tx_ring->count;
4118 	tx_ring->next_to_clean = i;
4119 
4120 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4121 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4122 
4123 	return budget > 0;
4124 }
4125 
4126 /**
4127  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4128  * @irq: interrupt number
4129  * @data: pointer to a q_vector
4130  **/
4131 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4132 {
4133 	struct i40e_q_vector *q_vector = data;
4134 	struct i40e_vsi *vsi;
4135 
4136 	if (!q_vector->tx.ring)
4137 		return IRQ_HANDLED;
4138 
4139 	vsi = q_vector->tx.ring->vsi;
4140 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4141 
4142 	return IRQ_HANDLED;
4143 }
4144 
4145 /**
4146  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4147  * @vsi: the VSI being configured
4148  * @v_idx: vector index
4149  * @qp_idx: queue pair index
4150  **/
4151 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4152 {
4153 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4154 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4155 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4156 
4157 	tx_ring->q_vector = q_vector;
4158 	tx_ring->next = q_vector->tx.ring;
4159 	q_vector->tx.ring = tx_ring;
4160 	q_vector->tx.count++;
4161 
4162 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4163 	if (i40e_enabled_xdp_vsi(vsi)) {
4164 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4165 
4166 		xdp_ring->q_vector = q_vector;
4167 		xdp_ring->next = q_vector->tx.ring;
4168 		q_vector->tx.ring = xdp_ring;
4169 		q_vector->tx.count++;
4170 	}
4171 
4172 	rx_ring->q_vector = q_vector;
4173 	rx_ring->next = q_vector->rx.ring;
4174 	q_vector->rx.ring = rx_ring;
4175 	q_vector->rx.count++;
4176 }
4177 
4178 /**
4179  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4180  * @vsi: the VSI being configured
4181  *
4182  * This function maps descriptor rings to the queue-specific vectors
4183  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4184  * one vector per queue pair, but on a constrained vector budget, we
4185  * group the queue pairs as "efficiently" as possible.
4186  **/
4187 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4188 {
4189 	int qp_remaining = vsi->num_queue_pairs;
4190 	int q_vectors = vsi->num_q_vectors;
4191 	int num_ringpairs;
4192 	int v_start = 0;
4193 	int qp_idx = 0;
4194 
4195 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4196 	 * group them so there are multiple queues per vector.
4197 	 * It is also important to go through all the vectors available to be
4198 	 * sure that if we don't use all the vectors, that the remaining vectors
4199 	 * are cleared. This is especially important when decreasing the
4200 	 * number of queues in use.
4201 	 */
4202 	for (; v_start < q_vectors; v_start++) {
4203 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4204 
4205 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4206 
4207 		q_vector->num_ringpairs = num_ringpairs;
4208 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4209 
4210 		q_vector->rx.count = 0;
4211 		q_vector->tx.count = 0;
4212 		q_vector->rx.ring = NULL;
4213 		q_vector->tx.ring = NULL;
4214 
4215 		while (num_ringpairs--) {
4216 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4217 			qp_idx++;
4218 			qp_remaining--;
4219 		}
4220 	}
4221 }
4222 
4223 /**
4224  * i40e_vsi_request_irq - Request IRQ from the OS
4225  * @vsi: the VSI being configured
4226  * @basename: name for the vector
4227  **/
4228 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4229 {
4230 	struct i40e_pf *pf = vsi->back;
4231 	int err;
4232 
4233 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4234 		err = i40e_vsi_request_irq_msix(vsi, basename);
4235 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4236 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4237 				  pf->int_name, pf);
4238 	else
4239 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4240 				  pf->int_name, pf);
4241 
4242 	if (err)
4243 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4244 
4245 	return err;
4246 }
4247 
4248 #ifdef CONFIG_NET_POLL_CONTROLLER
4249 /**
4250  * i40e_netpoll - A Polling 'interrupt' handler
4251  * @netdev: network interface device structure
4252  *
4253  * This is used by netconsole to send skbs without having to re-enable
4254  * interrupts.  It's not called while the normal interrupt routine is executing.
4255  **/
4256 static void i40e_netpoll(struct net_device *netdev)
4257 {
4258 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4259 	struct i40e_vsi *vsi = np->vsi;
4260 	struct i40e_pf *pf = vsi->back;
4261 	int i;
4262 
4263 	/* if interface is down do nothing */
4264 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4265 		return;
4266 
4267 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4268 		for (i = 0; i < vsi->num_q_vectors; i++)
4269 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4270 	} else {
4271 		i40e_intr(pf->pdev->irq, netdev);
4272 	}
4273 }
4274 #endif
4275 
4276 #define I40E_QTX_ENA_WAIT_COUNT 50
4277 
4278 /**
4279  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4280  * @pf: the PF being configured
4281  * @pf_q: the PF queue
4282  * @enable: enable or disable state of the queue
4283  *
4284  * This routine will wait for the given Tx queue of the PF to reach the
4285  * enabled or disabled state.
4286  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4287  * multiple retries; else will return 0 in case of success.
4288  **/
4289 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4290 {
4291 	int i;
4292 	u32 tx_reg;
4293 
4294 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4295 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4296 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4297 			break;
4298 
4299 		usleep_range(10, 20);
4300 	}
4301 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4302 		return -ETIMEDOUT;
4303 
4304 	return 0;
4305 }
4306 
4307 /**
4308  * i40e_control_tx_q - Start or stop a particular Tx queue
4309  * @pf: the PF structure
4310  * @pf_q: the PF queue to configure
4311  * @enable: start or stop the queue
4312  *
4313  * This function enables or disables a single queue. Note that any delay
4314  * required after the operation is expected to be handled by the caller of
4315  * this function.
4316  **/
4317 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4318 {
4319 	struct i40e_hw *hw = &pf->hw;
4320 	u32 tx_reg;
4321 	int i;
4322 
4323 	/* warn the TX unit of coming changes */
4324 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4325 	if (!enable)
4326 		usleep_range(10, 20);
4327 
4328 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4329 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4330 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4331 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4332 			break;
4333 		usleep_range(1000, 2000);
4334 	}
4335 
4336 	/* Skip if the queue is already in the requested state */
4337 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4338 		return;
4339 
4340 	/* turn on/off the queue */
4341 	if (enable) {
4342 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4343 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4344 	} else {
4345 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4346 	}
4347 
4348 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4349 }
4350 
4351 /**
4352  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4353  * @seid: VSI SEID
4354  * @pf: the PF structure
4355  * @pf_q: the PF queue to configure
4356  * @is_xdp: true if the queue is used for XDP
4357  * @enable: start or stop the queue
4358  **/
4359 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4360 			   bool is_xdp, bool enable)
4361 {
4362 	int ret;
4363 
4364 	i40e_control_tx_q(pf, pf_q, enable);
4365 
4366 	/* wait for the change to finish */
4367 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4368 	if (ret) {
4369 		dev_info(&pf->pdev->dev,
4370 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4371 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4372 			 (enable ? "en" : "dis"));
4373 	}
4374 
4375 	return ret;
4376 }
4377 
4378 /**
4379  * i40e_vsi_control_tx - Start or stop a VSI's rings
4380  * @vsi: the VSI being configured
4381  * @enable: start or stop the rings
4382  **/
4383 static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4384 {
4385 	struct i40e_pf *pf = vsi->back;
4386 	int i, pf_q, ret = 0;
4387 
4388 	pf_q = vsi->base_queue;
4389 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4390 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4391 					     pf_q,
4392 					     false /*is xdp*/, enable);
4393 		if (ret)
4394 			break;
4395 
4396 		if (!i40e_enabled_xdp_vsi(vsi))
4397 			continue;
4398 
4399 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4400 					     pf_q + vsi->alloc_queue_pairs,
4401 					     true /*is xdp*/, enable);
4402 		if (ret)
4403 			break;
4404 	}
4405 	return ret;
4406 }
4407 
4408 /**
4409  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4410  * @pf: the PF being configured
4411  * @pf_q: the PF queue
4412  * @enable: enable or disable state of the queue
4413  *
4414  * This routine will wait for the given Rx queue of the PF to reach the
4415  * enabled or disabled state.
4416  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4417  * multiple retries; else will return 0 in case of success.
4418  **/
4419 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4420 {
4421 	int i;
4422 	u32 rx_reg;
4423 
4424 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4425 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4426 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4427 			break;
4428 
4429 		usleep_range(10, 20);
4430 	}
4431 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4432 		return -ETIMEDOUT;
4433 
4434 	return 0;
4435 }
4436 
4437 /**
4438  * i40e_control_rx_q - Start or stop a particular Rx queue
4439  * @pf: the PF structure
4440  * @pf_q: the PF queue to configure
4441  * @enable: start or stop the queue
4442  *
4443  * This function enables or disables a single queue. Note that
4444  * any delay required after the operation is expected to be
4445  * handled by the caller of this function.
4446  **/
4447 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4448 {
4449 	struct i40e_hw *hw = &pf->hw;
4450 	u32 rx_reg;
4451 	int i;
4452 
4453 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4454 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4455 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4456 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4457 			break;
4458 		usleep_range(1000, 2000);
4459 	}
4460 
4461 	/* Skip if the queue is already in the requested state */
4462 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4463 		return;
4464 
4465 	/* turn on/off the queue */
4466 	if (enable)
4467 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4468 	else
4469 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4470 
4471 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4472 }
4473 
4474 /**
4475  * i40e_control_wait_rx_q
4476  * @pf: the PF structure
4477  * @pf_q: queue being configured
4478  * @enable: start or stop the rings
4479  *
4480  * This function enables or disables a single queue along with waiting
4481  * for the change to finish. The caller of this function should handle
4482  * the delays needed in the case of disabling queues.
4483  **/
4484 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4485 {
4486 	int ret = 0;
4487 
4488 	i40e_control_rx_q(pf, pf_q, enable);
4489 
4490 	/* wait for the change to finish */
4491 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4492 	if (ret)
4493 		return ret;
4494 
4495 	return ret;
4496 }
4497 
4498 /**
4499  * i40e_vsi_control_rx - Start or stop a VSI's rings
4500  * @vsi: the VSI being configured
4501  * @enable: start or stop the rings
4502  **/
4503 static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4504 {
4505 	struct i40e_pf *pf = vsi->back;
4506 	int i, pf_q, ret = 0;
4507 
4508 	pf_q = vsi->base_queue;
4509 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4510 		ret = i40e_control_wait_rx_q(pf, pf_q, enable);
4511 		if (ret) {
4512 			dev_info(&pf->pdev->dev,
4513 				 "VSI seid %d Rx ring %d %sable timeout\n",
4514 				 vsi->seid, pf_q, (enable ? "en" : "dis"));
4515 			break;
4516 		}
4517 	}
4518 
4519 	/* Due to HW errata, on Rx disable only, the register can indicate done
4520 	 * before it really is. Needs 50ms to be sure
4521 	 */
4522 	if (!enable)
4523 		mdelay(50);
4524 
4525 	return ret;
4526 }
4527 
4528 /**
4529  * i40e_vsi_start_rings - Start a VSI's rings
4530  * @vsi: the VSI being configured
4531  **/
4532 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4533 {
4534 	int ret = 0;
4535 
4536 	/* do rx first for enable and last for disable */
4537 	ret = i40e_vsi_control_rx(vsi, true);
4538 	if (ret)
4539 		return ret;
4540 	ret = i40e_vsi_control_tx(vsi, true);
4541 
4542 	return ret;
4543 }
4544 
4545 /**
4546  * i40e_vsi_stop_rings - Stop a VSI's rings
4547  * @vsi: the VSI being configured
4548  **/
4549 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4550 {
4551 	/* When port TX is suspended, don't wait */
4552 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4553 		return i40e_vsi_stop_rings_no_wait(vsi);
4554 
4555 	/* do rx first for enable and last for disable
4556 	 * Ignore return value, we need to shutdown whatever we can
4557 	 */
4558 	i40e_vsi_control_tx(vsi, false);
4559 	i40e_vsi_control_rx(vsi, false);
4560 }
4561 
4562 /**
4563  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4564  * @vsi: the VSI being shutdown
4565  *
4566  * This function stops all the rings for a VSI but does not delay to verify
4567  * that rings have been disabled. It is expected that the caller is shutting
4568  * down multiple VSIs at once and will delay together for all the VSIs after
4569  * initiating the shutdown. This is particularly useful for shutting down lots
4570  * of VFs together. Otherwise, a large delay can be incurred while configuring
4571  * each VSI in serial.
4572  **/
4573 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4574 {
4575 	struct i40e_pf *pf = vsi->back;
4576 	int i, pf_q;
4577 
4578 	pf_q = vsi->base_queue;
4579 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4580 		i40e_control_tx_q(pf, pf_q, false);
4581 		i40e_control_rx_q(pf, pf_q, false);
4582 	}
4583 }
4584 
4585 /**
4586  * i40e_vsi_free_irq - Free the irq association with the OS
4587  * @vsi: the VSI being configured
4588  **/
4589 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4590 {
4591 	struct i40e_pf *pf = vsi->back;
4592 	struct i40e_hw *hw = &pf->hw;
4593 	int base = vsi->base_vector;
4594 	u32 val, qp;
4595 	int i;
4596 
4597 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4598 		if (!vsi->q_vectors)
4599 			return;
4600 
4601 		if (!vsi->irqs_ready)
4602 			return;
4603 
4604 		vsi->irqs_ready = false;
4605 		for (i = 0; i < vsi->num_q_vectors; i++) {
4606 			int irq_num;
4607 			u16 vector;
4608 
4609 			vector = i + base;
4610 			irq_num = pf->msix_entries[vector].vector;
4611 
4612 			/* free only the irqs that were actually requested */
4613 			if (!vsi->q_vectors[i] ||
4614 			    !vsi->q_vectors[i]->num_ringpairs)
4615 				continue;
4616 
4617 			/* clear the affinity notifier in the IRQ descriptor */
4618 			irq_set_affinity_notifier(irq_num, NULL);
4619 			/* remove our suggested affinity mask for this IRQ */
4620 			irq_set_affinity_hint(irq_num, NULL);
4621 			synchronize_irq(irq_num);
4622 			free_irq(irq_num, vsi->q_vectors[i]);
4623 
4624 			/* Tear down the interrupt queue link list
4625 			 *
4626 			 * We know that they come in pairs and always
4627 			 * the Rx first, then the Tx.  To clear the
4628 			 * link list, stick the EOL value into the
4629 			 * next_q field of the registers.
4630 			 */
4631 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4632 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4633 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4634 			val |= I40E_QUEUE_END_OF_LIST
4635 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4636 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4637 
4638 			while (qp != I40E_QUEUE_END_OF_LIST) {
4639 				u32 next;
4640 
4641 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4642 
4643 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4644 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4645 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4646 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4647 
4648 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4649 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4650 
4651 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4652 
4653 				val = rd32(hw, I40E_QINT_TQCTL(qp));
4654 
4655 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4656 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4657 
4658 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4659 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4660 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4661 					 I40E_QINT_TQCTL_INTEVENT_MASK);
4662 
4663 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4664 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4665 
4666 				wr32(hw, I40E_QINT_TQCTL(qp), val);
4667 				qp = next;
4668 			}
4669 		}
4670 	} else {
4671 		free_irq(pf->pdev->irq, pf);
4672 
4673 		val = rd32(hw, I40E_PFINT_LNKLST0);
4674 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4675 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4676 		val |= I40E_QUEUE_END_OF_LIST
4677 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4678 		wr32(hw, I40E_PFINT_LNKLST0, val);
4679 
4680 		val = rd32(hw, I40E_QINT_RQCTL(qp));
4681 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4682 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4683 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4684 			 I40E_QINT_RQCTL_INTEVENT_MASK);
4685 
4686 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4687 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4688 
4689 		wr32(hw, I40E_QINT_RQCTL(qp), val);
4690 
4691 		val = rd32(hw, I40E_QINT_TQCTL(qp));
4692 
4693 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4694 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4695 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4696 			 I40E_QINT_TQCTL_INTEVENT_MASK);
4697 
4698 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4699 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4700 
4701 		wr32(hw, I40E_QINT_TQCTL(qp), val);
4702 	}
4703 }
4704 
4705 /**
4706  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4707  * @vsi: the VSI being configured
4708  * @v_idx: Index of vector to be freed
4709  *
4710  * This function frees the memory allocated to the q_vector.  In addition if
4711  * NAPI is enabled it will delete any references to the NAPI struct prior
4712  * to freeing the q_vector.
4713  **/
4714 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4715 {
4716 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4717 	struct i40e_ring *ring;
4718 
4719 	if (!q_vector)
4720 		return;
4721 
4722 	/* disassociate q_vector from rings */
4723 	i40e_for_each_ring(ring, q_vector->tx)
4724 		ring->q_vector = NULL;
4725 
4726 	i40e_for_each_ring(ring, q_vector->rx)
4727 		ring->q_vector = NULL;
4728 
4729 	/* only VSI w/ an associated netdev is set up w/ NAPI */
4730 	if (vsi->netdev)
4731 		netif_napi_del(&q_vector->napi);
4732 
4733 	vsi->q_vectors[v_idx] = NULL;
4734 
4735 	kfree_rcu(q_vector, rcu);
4736 }
4737 
4738 /**
4739  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4740  * @vsi: the VSI being un-configured
4741  *
4742  * This frees the memory allocated to the q_vectors and
4743  * deletes references to the NAPI struct.
4744  **/
4745 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4746 {
4747 	int v_idx;
4748 
4749 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4750 		i40e_free_q_vector(vsi, v_idx);
4751 }
4752 
4753 /**
4754  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4755  * @pf: board private structure
4756  **/
4757 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4758 {
4759 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4760 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4761 		pci_disable_msix(pf->pdev);
4762 		kfree(pf->msix_entries);
4763 		pf->msix_entries = NULL;
4764 		kfree(pf->irq_pile);
4765 		pf->irq_pile = NULL;
4766 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4767 		pci_disable_msi(pf->pdev);
4768 	}
4769 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4770 }
4771 
4772 /**
4773  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4774  * @pf: board private structure
4775  *
4776  * We go through and clear interrupt specific resources and reset the structure
4777  * to pre-load conditions
4778  **/
4779 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4780 {
4781 	int i;
4782 
4783 	i40e_free_misc_vector(pf);
4784 
4785 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4786 		      I40E_IWARP_IRQ_PILE_ID);
4787 
4788 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
4789 	for (i = 0; i < pf->num_alloc_vsi; i++)
4790 		if (pf->vsi[i])
4791 			i40e_vsi_free_q_vectors(pf->vsi[i]);
4792 	i40e_reset_interrupt_capability(pf);
4793 }
4794 
4795 /**
4796  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4797  * @vsi: the VSI being configured
4798  **/
4799 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4800 {
4801 	int q_idx;
4802 
4803 	if (!vsi->netdev)
4804 		return;
4805 
4806 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4807 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4808 
4809 		if (q_vector->rx.ring || q_vector->tx.ring)
4810 			napi_enable(&q_vector->napi);
4811 	}
4812 }
4813 
4814 /**
4815  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4816  * @vsi: the VSI being configured
4817  **/
4818 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4819 {
4820 	int q_idx;
4821 
4822 	if (!vsi->netdev)
4823 		return;
4824 
4825 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4826 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4827 
4828 		if (q_vector->rx.ring || q_vector->tx.ring)
4829 			napi_disable(&q_vector->napi);
4830 	}
4831 }
4832 
4833 /**
4834  * i40e_vsi_close - Shut down a VSI
4835  * @vsi: the vsi to be quelled
4836  **/
4837 static void i40e_vsi_close(struct i40e_vsi *vsi)
4838 {
4839 	struct i40e_pf *pf = vsi->back;
4840 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
4841 		i40e_down(vsi);
4842 	i40e_vsi_free_irq(vsi);
4843 	i40e_vsi_free_tx_resources(vsi);
4844 	i40e_vsi_free_rx_resources(vsi);
4845 	vsi->current_netdev_flags = 0;
4846 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
4847 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4848 		set_bit(__I40E_CLIENT_RESET, pf->state);
4849 }
4850 
4851 /**
4852  * i40e_quiesce_vsi - Pause a given VSI
4853  * @vsi: the VSI being paused
4854  **/
4855 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4856 {
4857 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4858 		return;
4859 
4860 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
4861 	if (vsi->netdev && netif_running(vsi->netdev))
4862 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
4863 	else
4864 		i40e_vsi_close(vsi);
4865 }
4866 
4867 /**
4868  * i40e_unquiesce_vsi - Resume a given VSI
4869  * @vsi: the VSI being resumed
4870  **/
4871 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4872 {
4873 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
4874 		return;
4875 
4876 	if (vsi->netdev && netif_running(vsi->netdev))
4877 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4878 	else
4879 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
4880 }
4881 
4882 /**
4883  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4884  * @pf: the PF
4885  **/
4886 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4887 {
4888 	int v;
4889 
4890 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4891 		if (pf->vsi[v])
4892 			i40e_quiesce_vsi(pf->vsi[v]);
4893 	}
4894 }
4895 
4896 /**
4897  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4898  * @pf: the PF
4899  **/
4900 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4901 {
4902 	int v;
4903 
4904 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4905 		if (pf->vsi[v])
4906 			i40e_unquiesce_vsi(pf->vsi[v]);
4907 	}
4908 }
4909 
4910 /**
4911  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
4912  * @vsi: the VSI being configured
4913  *
4914  * Wait until all queues on a given VSI have been disabled.
4915  **/
4916 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
4917 {
4918 	struct i40e_pf *pf = vsi->back;
4919 	int i, pf_q, ret;
4920 
4921 	pf_q = vsi->base_queue;
4922 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4923 		/* Check and wait for the Tx queue */
4924 		ret = i40e_pf_txq_wait(pf, pf_q, false);
4925 		if (ret) {
4926 			dev_info(&pf->pdev->dev,
4927 				 "VSI seid %d Tx ring %d disable timeout\n",
4928 				 vsi->seid, pf_q);
4929 			return ret;
4930 		}
4931 
4932 		if (!i40e_enabled_xdp_vsi(vsi))
4933 			goto wait_rx;
4934 
4935 		/* Check and wait for the XDP Tx queue */
4936 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
4937 				       false);
4938 		if (ret) {
4939 			dev_info(&pf->pdev->dev,
4940 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
4941 				 vsi->seid, pf_q);
4942 			return ret;
4943 		}
4944 wait_rx:
4945 		/* Check and wait for the Rx queue */
4946 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
4947 		if (ret) {
4948 			dev_info(&pf->pdev->dev,
4949 				 "VSI seid %d Rx ring %d disable timeout\n",
4950 				 vsi->seid, pf_q);
4951 			return ret;
4952 		}
4953 	}
4954 
4955 	return 0;
4956 }
4957 
4958 #ifdef CONFIG_I40E_DCB
4959 /**
4960  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
4961  * @pf: the PF
4962  *
4963  * This function waits for the queues to be in disabled state for all the
4964  * VSIs that are managed by this PF.
4965  **/
4966 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
4967 {
4968 	int v, ret = 0;
4969 
4970 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4971 		if (pf->vsi[v]) {
4972 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
4973 			if (ret)
4974 				break;
4975 		}
4976 	}
4977 
4978 	return ret;
4979 }
4980 
4981 #endif
4982 
4983 /**
4984  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
4985  * @pf: pointer to PF
4986  *
4987  * Get TC map for ISCSI PF type that will include iSCSI TC
4988  * and LAN TC.
4989  **/
4990 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4991 {
4992 	struct i40e_dcb_app_priority_table app;
4993 	struct i40e_hw *hw = &pf->hw;
4994 	u8 enabled_tc = 1; /* TC0 is always enabled */
4995 	u8 tc, i;
4996 	/* Get the iSCSI APP TLV */
4997 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4998 
4999 	for (i = 0; i < dcbcfg->numapps; i++) {
5000 		app = dcbcfg->app[i];
5001 		if (app.selector == I40E_APP_SEL_TCPIP &&
5002 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5003 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5004 			enabled_tc |= BIT(tc);
5005 			break;
5006 		}
5007 	}
5008 
5009 	return enabled_tc;
5010 }
5011 
5012 /**
5013  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5014  * @dcbcfg: the corresponding DCBx configuration structure
5015  *
5016  * Return the number of TCs from given DCBx configuration
5017  **/
5018 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5019 {
5020 	int i, tc_unused = 0;
5021 	u8 num_tc = 0;
5022 	u8 ret = 0;
5023 
5024 	/* Scan the ETS Config Priority Table to find
5025 	 * traffic class enabled for a given priority
5026 	 * and create a bitmask of enabled TCs
5027 	 */
5028 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5029 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5030 
5031 	/* Now scan the bitmask to check for
5032 	 * contiguous TCs starting with TC0
5033 	 */
5034 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5035 		if (num_tc & BIT(i)) {
5036 			if (!tc_unused) {
5037 				ret++;
5038 			} else {
5039 				pr_err("Non-contiguous TC - Disabling DCB\n");
5040 				return 1;
5041 			}
5042 		} else {
5043 			tc_unused = 1;
5044 		}
5045 	}
5046 
5047 	/* There is always at least TC0 */
5048 	if (!ret)
5049 		ret = 1;
5050 
5051 	return ret;
5052 }
5053 
5054 /**
5055  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5056  * @dcbcfg: the corresponding DCBx configuration structure
5057  *
5058  * Query the current DCB configuration and return the number of
5059  * traffic classes enabled from the given DCBX config
5060  **/
5061 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5062 {
5063 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5064 	u8 enabled_tc = 1;
5065 	u8 i;
5066 
5067 	for (i = 0; i < num_tc; i++)
5068 		enabled_tc |= BIT(i);
5069 
5070 	return enabled_tc;
5071 }
5072 
5073 /**
5074  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5075  * @pf: PF being queried
5076  *
5077  * Query the current MQPRIO configuration and return the number of
5078  * traffic classes enabled.
5079  **/
5080 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5081 {
5082 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5083 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5084 	u8 enabled_tc = 1, i;
5085 
5086 	for (i = 1; i < num_tc; i++)
5087 		enabled_tc |= BIT(i);
5088 	return enabled_tc;
5089 }
5090 
5091 /**
5092  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5093  * @pf: PF being queried
5094  *
5095  * Return number of traffic classes enabled for the given PF
5096  **/
5097 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5098 {
5099 	struct i40e_hw *hw = &pf->hw;
5100 	u8 i, enabled_tc = 1;
5101 	u8 num_tc = 0;
5102 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5103 
5104 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5105 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5106 
5107 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5108 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5109 		return 1;
5110 
5111 	/* SFP mode will be enabled for all TCs on port */
5112 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5113 		return i40e_dcb_get_num_tc(dcbcfg);
5114 
5115 	/* MFP mode return count of enabled TCs for this PF */
5116 	if (pf->hw.func_caps.iscsi)
5117 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5118 	else
5119 		return 1; /* Only TC0 */
5120 
5121 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5122 		if (enabled_tc & BIT(i))
5123 			num_tc++;
5124 	}
5125 	return num_tc;
5126 }
5127 
5128 /**
5129  * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
5130  * @pf: PF being queried
5131  *
5132  * Return a bitmap for enabled traffic classes for this PF.
5133  **/
5134 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5135 {
5136 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5137 		return i40e_mqprio_get_enabled_tc(pf);
5138 
5139 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5140 	 * default TC
5141 	 */
5142 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5143 		return I40E_DEFAULT_TRAFFIC_CLASS;
5144 
5145 	/* SFP mode we want PF to be enabled for all TCs */
5146 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5147 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5148 
5149 	/* MFP enabled and iSCSI PF type */
5150 	if (pf->hw.func_caps.iscsi)
5151 		return i40e_get_iscsi_tc_map(pf);
5152 	else
5153 		return I40E_DEFAULT_TRAFFIC_CLASS;
5154 }
5155 
5156 /**
5157  * i40e_vsi_get_bw_info - Query VSI BW Information
5158  * @vsi: the VSI being queried
5159  *
5160  * Returns 0 on success, negative value on failure
5161  **/
5162 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5163 {
5164 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5165 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5166 	struct i40e_pf *pf = vsi->back;
5167 	struct i40e_hw *hw = &pf->hw;
5168 	i40e_status ret;
5169 	u32 tc_bw_max;
5170 	int i;
5171 
5172 	/* Get the VSI level BW configuration */
5173 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5174 	if (ret) {
5175 		dev_info(&pf->pdev->dev,
5176 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5177 			 i40e_stat_str(&pf->hw, ret),
5178 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5179 		return -EINVAL;
5180 	}
5181 
5182 	/* Get the VSI level BW configuration per TC */
5183 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5184 					       NULL);
5185 	if (ret) {
5186 		dev_info(&pf->pdev->dev,
5187 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5188 			 i40e_stat_str(&pf->hw, ret),
5189 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5190 		return -EINVAL;
5191 	}
5192 
5193 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5194 		dev_info(&pf->pdev->dev,
5195 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5196 			 bw_config.tc_valid_bits,
5197 			 bw_ets_config.tc_valid_bits);
5198 		/* Still continuing */
5199 	}
5200 
5201 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5202 	vsi->bw_max_quanta = bw_config.max_bw;
5203 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5204 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5205 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5206 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5207 		vsi->bw_ets_limit_credits[i] =
5208 					le16_to_cpu(bw_ets_config.credits[i]);
5209 		/* 3 bits out of 4 for each TC */
5210 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5211 	}
5212 
5213 	return 0;
5214 }
5215 
5216 /**
5217  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5218  * @vsi: the VSI being configured
5219  * @enabled_tc: TC bitmap
5220  * @bw_share: BW shared credits per TC
5221  *
5222  * Returns 0 on success, negative value on failure
5223  **/
5224 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5225 				       u8 *bw_share)
5226 {
5227 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5228 	struct i40e_pf *pf = vsi->back;
5229 	i40e_status ret;
5230 	int i;
5231 
5232 	/* There is no need to reset BW when mqprio mode is on.  */
5233 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5234 		return 0;
5235 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5236 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5237 		if (ret)
5238 			dev_info(&pf->pdev->dev,
5239 				 "Failed to reset tx rate for vsi->seid %u\n",
5240 				 vsi->seid);
5241 		return ret;
5242 	}
5243 	bw_data.tc_valid_bits = enabled_tc;
5244 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5245 		bw_data.tc_bw_credits[i] = bw_share[i];
5246 
5247 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5248 	if (ret) {
5249 		dev_info(&pf->pdev->dev,
5250 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5251 			 pf->hw.aq.asq_last_status);
5252 		return -EINVAL;
5253 	}
5254 
5255 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5256 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5257 
5258 	return 0;
5259 }
5260 
5261 /**
5262  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5263  * @vsi: the VSI being configured
5264  * @enabled_tc: TC map to be enabled
5265  *
5266  **/
5267 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5268 {
5269 	struct net_device *netdev = vsi->netdev;
5270 	struct i40e_pf *pf = vsi->back;
5271 	struct i40e_hw *hw = &pf->hw;
5272 	u8 netdev_tc = 0;
5273 	int i;
5274 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5275 
5276 	if (!netdev)
5277 		return;
5278 
5279 	if (!enabled_tc) {
5280 		netdev_reset_tc(netdev);
5281 		return;
5282 	}
5283 
5284 	/* Set up actual enabled TCs on the VSI */
5285 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5286 		return;
5287 
5288 	/* set per TC queues for the VSI */
5289 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5290 		/* Only set TC queues for enabled tcs
5291 		 *
5292 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5293 		 * enabled_tc bitmap would be 0x00001001; the driver
5294 		 * will set the numtc for netdev as 2 that will be
5295 		 * referenced by the netdev layer as TC 0 and 1.
5296 		 */
5297 		if (vsi->tc_config.enabled_tc & BIT(i))
5298 			netdev_set_tc_queue(netdev,
5299 					vsi->tc_config.tc_info[i].netdev_tc,
5300 					vsi->tc_config.tc_info[i].qcount,
5301 					vsi->tc_config.tc_info[i].qoffset);
5302 	}
5303 
5304 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5305 		return;
5306 
5307 	/* Assign UP2TC map for the VSI */
5308 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5309 		/* Get the actual TC# for the UP */
5310 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5311 		/* Get the mapped netdev TC# for the UP */
5312 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5313 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5314 	}
5315 }
5316 
5317 /**
5318  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5319  * @vsi: the VSI being configured
5320  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5321  **/
5322 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5323 				      struct i40e_vsi_context *ctxt)
5324 {
5325 	/* copy just the sections touched not the entire info
5326 	 * since not all sections are valid as returned by
5327 	 * update vsi params
5328 	 */
5329 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5330 	memcpy(&vsi->info.queue_mapping,
5331 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5332 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5333 	       sizeof(vsi->info.tc_mapping));
5334 }
5335 
5336 /**
5337  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5338  * @vsi: VSI to be configured
5339  * @enabled_tc: TC bitmap
5340  *
5341  * This configures a particular VSI for TCs that are mapped to the
5342  * given TC bitmap. It uses default bandwidth share for TCs across
5343  * VSIs to configure TC for a particular VSI.
5344  *
5345  * NOTE:
5346  * It is expected that the VSI queues have been quisced before calling
5347  * this function.
5348  **/
5349 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5350 {
5351 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5352 	struct i40e_pf *pf = vsi->back;
5353 	struct i40e_hw *hw = &pf->hw;
5354 	struct i40e_vsi_context ctxt;
5355 	int ret = 0;
5356 	int i;
5357 
5358 	/* Check if enabled_tc is same as existing or new TCs */
5359 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5360 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5361 		return ret;
5362 
5363 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5364 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5365 		if (enabled_tc & BIT(i))
5366 			bw_share[i] = 1;
5367 	}
5368 
5369 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5370 	if (ret) {
5371 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5372 
5373 		dev_info(&pf->pdev->dev,
5374 			 "Failed configuring TC map %d for VSI %d\n",
5375 			 enabled_tc, vsi->seid);
5376 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5377 						  &bw_config, NULL);
5378 		if (ret) {
5379 			dev_info(&pf->pdev->dev,
5380 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5381 				 i40e_stat_str(hw, ret),
5382 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5383 			goto out;
5384 		}
5385 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5386 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5387 
5388 			if (!valid_tc)
5389 				valid_tc = bw_config.tc_valid_bits;
5390 			/* Always enable TC0, no matter what */
5391 			valid_tc |= 1;
5392 			dev_info(&pf->pdev->dev,
5393 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5394 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5395 			enabled_tc = valid_tc;
5396 		}
5397 
5398 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5399 		if (ret) {
5400 			dev_err(&pf->pdev->dev,
5401 				"Unable to  configure TC map %d for VSI %d\n",
5402 				enabled_tc, vsi->seid);
5403 			goto out;
5404 		}
5405 	}
5406 
5407 	/* Update Queue Pairs Mapping for currently enabled UPs */
5408 	ctxt.seid = vsi->seid;
5409 	ctxt.pf_num = vsi->back->hw.pf_id;
5410 	ctxt.vf_num = 0;
5411 	ctxt.uplink_seid = vsi->uplink_seid;
5412 	ctxt.info = vsi->info;
5413 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5414 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5415 		if (ret)
5416 			goto out;
5417 	} else {
5418 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5419 	}
5420 
5421 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5422 	 * queues changed.
5423 	 */
5424 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5425 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5426 				      vsi->num_queue_pairs);
5427 		ret = i40e_vsi_config_rss(vsi);
5428 		if (ret) {
5429 			dev_info(&vsi->back->pdev->dev,
5430 				 "Failed to reconfig rss for num_queues\n");
5431 			return ret;
5432 		}
5433 		vsi->reconfig_rss = false;
5434 	}
5435 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5436 		ctxt.info.valid_sections |=
5437 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5438 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5439 	}
5440 
5441 	/* Update the VSI after updating the VSI queue-mapping
5442 	 * information
5443 	 */
5444 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5445 	if (ret) {
5446 		dev_info(&pf->pdev->dev,
5447 			 "Update vsi tc config failed, err %s aq_err %s\n",
5448 			 i40e_stat_str(hw, ret),
5449 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5450 		goto out;
5451 	}
5452 	/* update the local VSI info with updated queue map */
5453 	i40e_vsi_update_queue_map(vsi, &ctxt);
5454 	vsi->info.valid_sections = 0;
5455 
5456 	/* Update current VSI BW information */
5457 	ret = i40e_vsi_get_bw_info(vsi);
5458 	if (ret) {
5459 		dev_info(&pf->pdev->dev,
5460 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5461 			 i40e_stat_str(hw, ret),
5462 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5463 		goto out;
5464 	}
5465 
5466 	/* Update the netdev TC setup */
5467 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5468 out:
5469 	return ret;
5470 }
5471 
5472 /**
5473  * i40e_get_link_speed - Returns link speed for the interface
5474  * @vsi: VSI to be configured
5475  *
5476  **/
5477 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5478 {
5479 	struct i40e_pf *pf = vsi->back;
5480 
5481 	switch (pf->hw.phy.link_info.link_speed) {
5482 	case I40E_LINK_SPEED_40GB:
5483 		return 40000;
5484 	case I40E_LINK_SPEED_25GB:
5485 		return 25000;
5486 	case I40E_LINK_SPEED_20GB:
5487 		return 20000;
5488 	case I40E_LINK_SPEED_10GB:
5489 		return 10000;
5490 	case I40E_LINK_SPEED_1GB:
5491 		return 1000;
5492 	default:
5493 		return -EINVAL;
5494 	}
5495 }
5496 
5497 /**
5498  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5499  * @vsi: VSI to be configured
5500  * @seid: seid of the channel/VSI
5501  * @max_tx_rate: max TX rate to be configured as BW limit
5502  *
5503  * Helper function to set BW limit for a given VSI
5504  **/
5505 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5506 {
5507 	struct i40e_pf *pf = vsi->back;
5508 	u64 credits = 0;
5509 	int speed = 0;
5510 	int ret = 0;
5511 
5512 	speed = i40e_get_link_speed(vsi);
5513 	if (max_tx_rate > speed) {
5514 		dev_err(&pf->pdev->dev,
5515 			"Invalid max tx rate %llu specified for VSI seid %d.",
5516 			max_tx_rate, seid);
5517 		return -EINVAL;
5518 	}
5519 	if (max_tx_rate && max_tx_rate < 50) {
5520 		dev_warn(&pf->pdev->dev,
5521 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5522 		max_tx_rate = 50;
5523 	}
5524 
5525 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5526 	credits = max_tx_rate;
5527 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5528 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5529 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5530 	if (ret)
5531 		dev_err(&pf->pdev->dev,
5532 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5533 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5534 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5535 	return ret;
5536 }
5537 
5538 /**
5539  * i40e_remove_queue_channels - Remove queue channels for the TCs
5540  * @vsi: VSI to be configured
5541  *
5542  * Remove queue channels for the TCs
5543  **/
5544 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5545 {
5546 	enum i40e_admin_queue_err last_aq_status;
5547 	struct i40e_cloud_filter *cfilter;
5548 	struct i40e_channel *ch, *ch_tmp;
5549 	struct i40e_pf *pf = vsi->back;
5550 	struct hlist_node *node;
5551 	int ret, i;
5552 
5553 	/* Reset rss size that was stored when reconfiguring rss for
5554 	 * channel VSIs with non-power-of-2 queue count.
5555 	 */
5556 	vsi->current_rss_size = 0;
5557 
5558 	/* perform cleanup for channels if they exist */
5559 	if (list_empty(&vsi->ch_list))
5560 		return;
5561 
5562 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5563 		struct i40e_vsi *p_vsi;
5564 
5565 		list_del(&ch->list);
5566 		p_vsi = ch->parent_vsi;
5567 		if (!p_vsi || !ch->initialized) {
5568 			kfree(ch);
5569 			continue;
5570 		}
5571 		/* Reset queue contexts */
5572 		for (i = 0; i < ch->num_queue_pairs; i++) {
5573 			struct i40e_ring *tx_ring, *rx_ring;
5574 			u16 pf_q;
5575 
5576 			pf_q = ch->base_queue + i;
5577 			tx_ring = vsi->tx_rings[pf_q];
5578 			tx_ring->ch = NULL;
5579 
5580 			rx_ring = vsi->rx_rings[pf_q];
5581 			rx_ring->ch = NULL;
5582 		}
5583 
5584 		/* Reset BW configured for this VSI via mqprio */
5585 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5586 		if (ret)
5587 			dev_info(&vsi->back->pdev->dev,
5588 				 "Failed to reset tx rate for ch->seid %u\n",
5589 				 ch->seid);
5590 
5591 		/* delete cloud filters associated with this channel */
5592 		hlist_for_each_entry_safe(cfilter, node,
5593 					  &pf->cloud_filter_list, cloud_node) {
5594 			if (cfilter->seid != ch->seid)
5595 				continue;
5596 
5597 			hash_del(&cfilter->cloud_node);
5598 			if (cfilter->dst_port)
5599 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
5600 									cfilter,
5601 									false);
5602 			else
5603 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
5604 								false);
5605 			last_aq_status = pf->hw.aq.asq_last_status;
5606 			if (ret)
5607 				dev_info(&pf->pdev->dev,
5608 					 "Failed to delete cloud filter, err %s aq_err %s\n",
5609 					 i40e_stat_str(&pf->hw, ret),
5610 					 i40e_aq_str(&pf->hw, last_aq_status));
5611 			kfree(cfilter);
5612 		}
5613 
5614 		/* delete VSI from FW */
5615 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5616 					     NULL);
5617 		if (ret)
5618 			dev_err(&vsi->back->pdev->dev,
5619 				"unable to remove channel (%d) for parent VSI(%d)\n",
5620 				ch->seid, p_vsi->seid);
5621 		kfree(ch);
5622 	}
5623 	INIT_LIST_HEAD(&vsi->ch_list);
5624 }
5625 
5626 /**
5627  * i40e_is_any_channel - channel exist or not
5628  * @vsi: ptr to VSI to which channels are associated with
5629  *
5630  * Returns true or false if channel(s) exist for associated VSI or not
5631  **/
5632 static bool i40e_is_any_channel(struct i40e_vsi *vsi)
5633 {
5634 	struct i40e_channel *ch, *ch_tmp;
5635 
5636 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5637 		if (ch->initialized)
5638 			return true;
5639 	}
5640 
5641 	return false;
5642 }
5643 
5644 /**
5645  * i40e_get_max_queues_for_channel
5646  * @vsi: ptr to VSI to which channels are associated with
5647  *
5648  * Helper function which returns max value among the queue counts set on the
5649  * channels/TCs created.
5650  **/
5651 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5652 {
5653 	struct i40e_channel *ch, *ch_tmp;
5654 	int max = 0;
5655 
5656 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5657 		if (!ch->initialized)
5658 			continue;
5659 		if (ch->num_queue_pairs > max)
5660 			max = ch->num_queue_pairs;
5661 	}
5662 
5663 	return max;
5664 }
5665 
5666 /**
5667  * i40e_validate_num_queues - validate num_queues w.r.t channel
5668  * @pf: ptr to PF device
5669  * @num_queues: number of queues
5670  * @vsi: the parent VSI
5671  * @reconfig_rss: indicates should the RSS be reconfigured or not
5672  *
5673  * This function validates number of queues in the context of new channel
5674  * which is being established and determines if RSS should be reconfigured
5675  * or not for parent VSI.
5676  **/
5677 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5678 				    struct i40e_vsi *vsi, bool *reconfig_rss)
5679 {
5680 	int max_ch_queues;
5681 
5682 	if (!reconfig_rss)
5683 		return -EINVAL;
5684 
5685 	*reconfig_rss = false;
5686 	if (vsi->current_rss_size) {
5687 		if (num_queues > vsi->current_rss_size) {
5688 			dev_dbg(&pf->pdev->dev,
5689 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
5690 				num_queues, vsi->current_rss_size);
5691 			return -EINVAL;
5692 		} else if ((num_queues < vsi->current_rss_size) &&
5693 			   (!is_power_of_2(num_queues))) {
5694 			dev_dbg(&pf->pdev->dev,
5695 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5696 				num_queues, vsi->current_rss_size);
5697 			return -EINVAL;
5698 		}
5699 	}
5700 
5701 	if (!is_power_of_2(num_queues)) {
5702 		/* Find the max num_queues configured for channel if channel
5703 		 * exist.
5704 		 * if channel exist, then enforce 'num_queues' to be more than
5705 		 * max ever queues configured for channel.
5706 		 */
5707 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5708 		if (num_queues < max_ch_queues) {
5709 			dev_dbg(&pf->pdev->dev,
5710 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
5711 				num_queues, max_ch_queues);
5712 			return -EINVAL;
5713 		}
5714 		*reconfig_rss = true;
5715 	}
5716 
5717 	return 0;
5718 }
5719 
5720 /**
5721  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5722  * @vsi: the VSI being setup
5723  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5724  *
5725  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5726  **/
5727 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5728 {
5729 	struct i40e_pf *pf = vsi->back;
5730 	u8 seed[I40E_HKEY_ARRAY_SIZE];
5731 	struct i40e_hw *hw = &pf->hw;
5732 	int local_rss_size;
5733 	u8 *lut;
5734 	int ret;
5735 
5736 	if (!vsi->rss_size)
5737 		return -EINVAL;
5738 
5739 	if (rss_size > vsi->rss_size)
5740 		return -EINVAL;
5741 
5742 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
5743 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5744 	if (!lut)
5745 		return -ENOMEM;
5746 
5747 	/* Ignoring user configured lut if there is one */
5748 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5749 
5750 	/* Use user configured hash key if there is one, otherwise
5751 	 * use default.
5752 	 */
5753 	if (vsi->rss_hkey_user)
5754 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5755 	else
5756 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5757 
5758 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5759 	if (ret) {
5760 		dev_info(&pf->pdev->dev,
5761 			 "Cannot set RSS lut, err %s aq_err %s\n",
5762 			 i40e_stat_str(hw, ret),
5763 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5764 		kfree(lut);
5765 		return ret;
5766 	}
5767 	kfree(lut);
5768 
5769 	/* Do the update w.r.t. storing rss_size */
5770 	if (!vsi->orig_rss_size)
5771 		vsi->orig_rss_size = vsi->rss_size;
5772 	vsi->current_rss_size = local_rss_size;
5773 
5774 	return ret;
5775 }
5776 
5777 /**
5778  * i40e_channel_setup_queue_map - Setup a channel queue map
5779  * @pf: ptr to PF device
5780  * @vsi: the VSI being setup
5781  * @ctxt: VSI context structure
5782  * @ch: ptr to channel structure
5783  *
5784  * Setup queue map for a specific channel
5785  **/
5786 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5787 					 struct i40e_vsi_context *ctxt,
5788 					 struct i40e_channel *ch)
5789 {
5790 	u16 qcount, qmap, sections = 0;
5791 	u8 offset = 0;
5792 	int pow;
5793 
5794 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5795 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5796 
5797 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5798 	ch->num_queue_pairs = qcount;
5799 
5800 	/* find the next higher power-of-2 of num queue pairs */
5801 	pow = ilog2(qcount);
5802 	if (!is_power_of_2(qcount))
5803 		pow++;
5804 
5805 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5806 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
5807 
5808 	/* Setup queue TC[0].qmap for given VSI context */
5809 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
5810 
5811 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
5812 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
5813 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
5814 	ctxt->info.valid_sections |= cpu_to_le16(sections);
5815 }
5816 
5817 /**
5818  * i40e_add_channel - add a channel by adding VSI
5819  * @pf: ptr to PF device
5820  * @uplink_seid: underlying HW switching element (VEB) ID
5821  * @ch: ptr to channel structure
5822  *
5823  * Add a channel (VSI) using add_vsi and queue_map
5824  **/
5825 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
5826 			    struct i40e_channel *ch)
5827 {
5828 	struct i40e_hw *hw = &pf->hw;
5829 	struct i40e_vsi_context ctxt;
5830 	u8 enabled_tc = 0x1; /* TC0 enabled */
5831 	int ret;
5832 
5833 	if (ch->type != I40E_VSI_VMDQ2) {
5834 		dev_info(&pf->pdev->dev,
5835 			 "add new vsi failed, ch->type %d\n", ch->type);
5836 		return -EINVAL;
5837 	}
5838 
5839 	memset(&ctxt, 0, sizeof(ctxt));
5840 	ctxt.pf_num = hw->pf_id;
5841 	ctxt.vf_num = 0;
5842 	ctxt.uplink_seid = uplink_seid;
5843 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5844 	if (ch->type == I40E_VSI_VMDQ2)
5845 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
5846 
5847 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
5848 		ctxt.info.valid_sections |=
5849 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5850 		ctxt.info.switch_id =
5851 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5852 	}
5853 
5854 	/* Set queue map for a given VSI context */
5855 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
5856 
5857 	/* Now time to create VSI */
5858 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
5859 	if (ret) {
5860 		dev_info(&pf->pdev->dev,
5861 			 "add new vsi failed, err %s aq_err %s\n",
5862 			 i40e_stat_str(&pf->hw, ret),
5863 			 i40e_aq_str(&pf->hw,
5864 				     pf->hw.aq.asq_last_status));
5865 		return -ENOENT;
5866 	}
5867 
5868 	/* Success, update channel, set enabled_tc only if the channel
5869 	 * is not a macvlan
5870 	 */
5871 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
5872 	ch->seid = ctxt.seid;
5873 	ch->vsi_number = ctxt.vsi_number;
5874 	ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx);
5875 
5876 	/* copy just the sections touched not the entire info
5877 	 * since not all sections are valid as returned by
5878 	 * update vsi params
5879 	 */
5880 	ch->info.mapping_flags = ctxt.info.mapping_flags;
5881 	memcpy(&ch->info.queue_mapping,
5882 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
5883 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
5884 	       sizeof(ctxt.info.tc_mapping));
5885 
5886 	return 0;
5887 }
5888 
5889 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
5890 				  u8 *bw_share)
5891 {
5892 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5893 	i40e_status ret;
5894 	int i;
5895 
5896 	bw_data.tc_valid_bits = ch->enabled_tc;
5897 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5898 		bw_data.tc_bw_credits[i] = bw_share[i];
5899 
5900 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
5901 				       &bw_data, NULL);
5902 	if (ret) {
5903 		dev_info(&vsi->back->pdev->dev,
5904 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
5905 			 vsi->back->hw.aq.asq_last_status, ch->seid);
5906 		return -EINVAL;
5907 	}
5908 
5909 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5910 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
5911 
5912 	return 0;
5913 }
5914 
5915 /**
5916  * i40e_channel_config_tx_ring - config TX ring associated with new channel
5917  * @pf: ptr to PF device
5918  * @vsi: the VSI being setup
5919  * @ch: ptr to channel structure
5920  *
5921  * Configure TX rings associated with channel (VSI) since queues are being
5922  * from parent VSI.
5923  **/
5924 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
5925 				       struct i40e_vsi *vsi,
5926 				       struct i40e_channel *ch)
5927 {
5928 	i40e_status ret;
5929 	int i;
5930 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5931 
5932 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5933 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5934 		if (ch->enabled_tc & BIT(i))
5935 			bw_share[i] = 1;
5936 	}
5937 
5938 	/* configure BW for new VSI */
5939 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
5940 	if (ret) {
5941 		dev_info(&vsi->back->pdev->dev,
5942 			 "Failed configuring TC map %d for channel (seid %u)\n",
5943 			 ch->enabled_tc, ch->seid);
5944 		return ret;
5945 	}
5946 
5947 	for (i = 0; i < ch->num_queue_pairs; i++) {
5948 		struct i40e_ring *tx_ring, *rx_ring;
5949 		u16 pf_q;
5950 
5951 		pf_q = ch->base_queue + i;
5952 
5953 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
5954 		 * context
5955 		 */
5956 		tx_ring = vsi->tx_rings[pf_q];
5957 		tx_ring->ch = ch;
5958 
5959 		/* Get the RX ring ptr */
5960 		rx_ring = vsi->rx_rings[pf_q];
5961 		rx_ring->ch = ch;
5962 	}
5963 
5964 	return 0;
5965 }
5966 
5967 /**
5968  * i40e_setup_hw_channel - setup new channel
5969  * @pf: ptr to PF device
5970  * @vsi: the VSI being setup
5971  * @ch: ptr to channel structure
5972  * @uplink_seid: underlying HW switching element (VEB) ID
5973  * @type: type of channel to be created (VMDq2/VF)
5974  *
5975  * Setup new channel (VSI) based on specified type (VMDq2/VF)
5976  * and configures TX rings accordingly
5977  **/
5978 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
5979 					struct i40e_vsi *vsi,
5980 					struct i40e_channel *ch,
5981 					u16 uplink_seid, u8 type)
5982 {
5983 	int ret;
5984 
5985 	ch->initialized = false;
5986 	ch->base_queue = vsi->next_base_queue;
5987 	ch->type = type;
5988 
5989 	/* Proceed with creation of channel (VMDq2) VSI */
5990 	ret = i40e_add_channel(pf, uplink_seid, ch);
5991 	if (ret) {
5992 		dev_info(&pf->pdev->dev,
5993 			 "failed to add_channel using uplink_seid %u\n",
5994 			 uplink_seid);
5995 		return ret;
5996 	}
5997 
5998 	/* Mark the successful creation of channel */
5999 	ch->initialized = true;
6000 
6001 	/* Reconfigure TX queues using QTX_CTL register */
6002 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6003 	if (ret) {
6004 		dev_info(&pf->pdev->dev,
6005 			 "failed to configure TX rings for channel %u\n",
6006 			 ch->seid);
6007 		return ret;
6008 	}
6009 
6010 	/* update 'next_base_queue' */
6011 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6012 	dev_dbg(&pf->pdev->dev,
6013 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6014 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6015 		ch->num_queue_pairs,
6016 		vsi->next_base_queue);
6017 	return ret;
6018 }
6019 
6020 /**
6021  * i40e_setup_channel - setup new channel using uplink element
6022  * @pf: ptr to PF device
6023  * @type: type of channel to be created (VMDq2/VF)
6024  * @uplink_seid: underlying HW switching element (VEB) ID
6025  * @ch: ptr to channel structure
6026  *
6027  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6028  * and uplink switching element (uplink_seid)
6029  **/
6030 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6031 			       struct i40e_channel *ch)
6032 {
6033 	u8 vsi_type;
6034 	u16 seid;
6035 	int ret;
6036 
6037 	if (vsi->type == I40E_VSI_MAIN) {
6038 		vsi_type = I40E_VSI_VMDQ2;
6039 	} else {
6040 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6041 			vsi->type);
6042 		return false;
6043 	}
6044 
6045 	/* underlying switching element */
6046 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6047 
6048 	/* create channel (VSI), configure TX rings */
6049 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6050 	if (ret) {
6051 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6052 		return false;
6053 	}
6054 
6055 	return ch->initialized ? true : false;
6056 }
6057 
6058 /**
6059  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6060  * @vsi: ptr to VSI which has PF backing
6061  *
6062  * Sets up switch mode correctly if it needs to be changed and perform
6063  * what are allowed modes.
6064  **/
6065 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6066 {
6067 	u8 mode;
6068 	struct i40e_pf *pf = vsi->back;
6069 	struct i40e_hw *hw = &pf->hw;
6070 	int ret;
6071 
6072 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6073 	if (ret)
6074 		return -EINVAL;
6075 
6076 	if (hw->dev_caps.switch_mode) {
6077 		/* if switch mode is set, support mode2 (non-tunneled for
6078 		 * cloud filter) for now
6079 		 */
6080 		u32 switch_mode = hw->dev_caps.switch_mode &
6081 				  I40E_SWITCH_MODE_MASK;
6082 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6083 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6084 				return 0;
6085 			dev_err(&pf->pdev->dev,
6086 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6087 				hw->dev_caps.switch_mode);
6088 			return -EINVAL;
6089 		}
6090 	}
6091 
6092 	/* Set Bit 7 to be valid */
6093 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6094 
6095 	/* Set L4type for TCP support */
6096 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6097 
6098 	/* Set cloud filter mode */
6099 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6100 
6101 	/* Prep mode field for set_switch_config */
6102 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6103 					pf->last_sw_conf_valid_flags,
6104 					mode, NULL);
6105 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6106 		dev_err(&pf->pdev->dev,
6107 			"couldn't set switch config bits, err %s aq_err %s\n",
6108 			i40e_stat_str(hw, ret),
6109 			i40e_aq_str(hw,
6110 				    hw->aq.asq_last_status));
6111 
6112 	return ret;
6113 }
6114 
6115 /**
6116  * i40e_create_queue_channel - function to create channel
6117  * @vsi: VSI to be configured
6118  * @ch: ptr to channel (it contains channel specific params)
6119  *
6120  * This function creates channel (VSI) using num_queues specified by user,
6121  * reconfigs RSS if needed.
6122  **/
6123 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6124 			      struct i40e_channel *ch)
6125 {
6126 	struct i40e_pf *pf = vsi->back;
6127 	bool reconfig_rss;
6128 	int err;
6129 
6130 	if (!ch)
6131 		return -EINVAL;
6132 
6133 	if (!ch->num_queue_pairs) {
6134 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6135 			ch->num_queue_pairs);
6136 		return -EINVAL;
6137 	}
6138 
6139 	/* validate user requested num_queues for channel */
6140 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6141 				       &reconfig_rss);
6142 	if (err) {
6143 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6144 			 ch->num_queue_pairs);
6145 		return -EINVAL;
6146 	}
6147 
6148 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6149 	 * VSI to be added switch to VEB mode.
6150 	 */
6151 	if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
6152 	    (!i40e_is_any_channel(vsi))) {
6153 		if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
6154 			dev_dbg(&pf->pdev->dev,
6155 				"Failed to create channel. Override queues (%u) not power of 2\n",
6156 				vsi->tc_config.tc_info[0].qcount);
6157 			return -EINVAL;
6158 		}
6159 
6160 		if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6161 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6162 
6163 			if (vsi->type == I40E_VSI_MAIN) {
6164 				if (pf->flags & I40E_FLAG_TC_MQPRIO)
6165 					i40e_do_reset(pf, I40E_PF_RESET_FLAG,
6166 						      true);
6167 				else
6168 					i40e_do_reset_safe(pf,
6169 							   I40E_PF_RESET_FLAG);
6170 			}
6171 		}
6172 		/* now onwards for main VSI, number of queues will be value
6173 		 * of TC0's queue count
6174 		 */
6175 	}
6176 
6177 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6178 	 * it should be more than num_queues
6179 	 */
6180 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6181 		dev_dbg(&pf->pdev->dev,
6182 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6183 			vsi->cnt_q_avail, ch->num_queue_pairs);
6184 		return -EINVAL;
6185 	}
6186 
6187 	/* reconfig_rss only if vsi type is MAIN_VSI */
6188 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6189 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6190 		if (err) {
6191 			dev_info(&pf->pdev->dev,
6192 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6193 				 ch->num_queue_pairs);
6194 			return -EINVAL;
6195 		}
6196 	}
6197 
6198 	if (!i40e_setup_channel(pf, vsi, ch)) {
6199 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6200 		return -EINVAL;
6201 	}
6202 
6203 	dev_info(&pf->pdev->dev,
6204 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6205 		 ch->seid, ch->num_queue_pairs);
6206 
6207 	/* configure VSI for BW limit */
6208 	if (ch->max_tx_rate) {
6209 		u64 credits = ch->max_tx_rate;
6210 
6211 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6212 			return -EINVAL;
6213 
6214 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6215 		dev_dbg(&pf->pdev->dev,
6216 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6217 			ch->max_tx_rate,
6218 			credits,
6219 			ch->seid);
6220 	}
6221 
6222 	/* in case of VF, this will be main SRIOV VSI */
6223 	ch->parent_vsi = vsi;
6224 
6225 	/* and update main_vsi's count for queue_available to use */
6226 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6227 
6228 	return 0;
6229 }
6230 
6231 /**
6232  * i40e_configure_queue_channels - Add queue channel for the given TCs
6233  * @vsi: VSI to be configured
6234  *
6235  * Configures queue channel mapping to the given TCs
6236  **/
6237 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6238 {
6239 	struct i40e_channel *ch;
6240 	u64 max_rate = 0;
6241 	int ret = 0, i;
6242 
6243 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6244 	vsi->tc_seid_map[0] = vsi->seid;
6245 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6246 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6247 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6248 			if (!ch) {
6249 				ret = -ENOMEM;
6250 				goto err_free;
6251 			}
6252 
6253 			INIT_LIST_HEAD(&ch->list);
6254 			ch->num_queue_pairs =
6255 				vsi->tc_config.tc_info[i].qcount;
6256 			ch->base_queue =
6257 				vsi->tc_config.tc_info[i].qoffset;
6258 
6259 			/* Bandwidth limit through tc interface is in bytes/s,
6260 			 * change to Mbit/s
6261 			 */
6262 			max_rate = vsi->mqprio_qopt.max_rate[i];
6263 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6264 			ch->max_tx_rate = max_rate;
6265 
6266 			list_add_tail(&ch->list, &vsi->ch_list);
6267 
6268 			ret = i40e_create_queue_channel(vsi, ch);
6269 			if (ret) {
6270 				dev_err(&vsi->back->pdev->dev,
6271 					"Failed creating queue channel with TC%d: queues %d\n",
6272 					i, ch->num_queue_pairs);
6273 				goto err_free;
6274 			}
6275 			vsi->tc_seid_map[i] = ch->seid;
6276 		}
6277 	}
6278 	return ret;
6279 
6280 err_free:
6281 	i40e_remove_queue_channels(vsi);
6282 	return ret;
6283 }
6284 
6285 /**
6286  * i40e_veb_config_tc - Configure TCs for given VEB
6287  * @veb: given VEB
6288  * @enabled_tc: TC bitmap
6289  *
6290  * Configures given TC bitmap for VEB (switching) element
6291  **/
6292 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6293 {
6294 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6295 	struct i40e_pf *pf = veb->pf;
6296 	int ret = 0;
6297 	int i;
6298 
6299 	/* No TCs or already enabled TCs just return */
6300 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6301 		return ret;
6302 
6303 	bw_data.tc_valid_bits = enabled_tc;
6304 	/* bw_data.absolute_credits is not set (relative) */
6305 
6306 	/* Enable ETS TCs with equal BW Share for now */
6307 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6308 		if (enabled_tc & BIT(i))
6309 			bw_data.tc_bw_share_credits[i] = 1;
6310 	}
6311 
6312 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6313 						   &bw_data, NULL);
6314 	if (ret) {
6315 		dev_info(&pf->pdev->dev,
6316 			 "VEB bw config failed, err %s aq_err %s\n",
6317 			 i40e_stat_str(&pf->hw, ret),
6318 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6319 		goto out;
6320 	}
6321 
6322 	/* Update the BW information */
6323 	ret = i40e_veb_get_bw_info(veb);
6324 	if (ret) {
6325 		dev_info(&pf->pdev->dev,
6326 			 "Failed getting veb bw config, err %s aq_err %s\n",
6327 			 i40e_stat_str(&pf->hw, ret),
6328 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6329 	}
6330 
6331 out:
6332 	return ret;
6333 }
6334 
6335 #ifdef CONFIG_I40E_DCB
6336 /**
6337  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6338  * @pf: PF struct
6339  *
6340  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6341  * the caller would've quiesce all the VSIs before calling
6342  * this function
6343  **/
6344 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6345 {
6346 	u8 tc_map = 0;
6347 	int ret;
6348 	u8 v;
6349 
6350 	/* Enable the TCs available on PF to all VEBs */
6351 	tc_map = i40e_pf_get_tc_map(pf);
6352 	for (v = 0; v < I40E_MAX_VEB; v++) {
6353 		if (!pf->veb[v])
6354 			continue;
6355 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6356 		if (ret) {
6357 			dev_info(&pf->pdev->dev,
6358 				 "Failed configuring TC for VEB seid=%d\n",
6359 				 pf->veb[v]->seid);
6360 			/* Will try to configure as many components */
6361 		}
6362 	}
6363 
6364 	/* Update each VSI */
6365 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6366 		if (!pf->vsi[v])
6367 			continue;
6368 
6369 		/* - Enable all TCs for the LAN VSI
6370 		 * - For all others keep them at TC0 for now
6371 		 */
6372 		if (v == pf->lan_vsi)
6373 			tc_map = i40e_pf_get_tc_map(pf);
6374 		else
6375 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6376 
6377 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6378 		if (ret) {
6379 			dev_info(&pf->pdev->dev,
6380 				 "Failed configuring TC for VSI seid=%d\n",
6381 				 pf->vsi[v]->seid);
6382 			/* Will try to configure as many components */
6383 		} else {
6384 			/* Re-configure VSI vectors based on updated TC map */
6385 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6386 			if (pf->vsi[v]->netdev)
6387 				i40e_dcbnl_set_all(pf->vsi[v]);
6388 		}
6389 	}
6390 }
6391 
6392 /**
6393  * i40e_resume_port_tx - Resume port Tx
6394  * @pf: PF struct
6395  *
6396  * Resume a port's Tx and issue a PF reset in case of failure to
6397  * resume.
6398  **/
6399 static int i40e_resume_port_tx(struct i40e_pf *pf)
6400 {
6401 	struct i40e_hw *hw = &pf->hw;
6402 	int ret;
6403 
6404 	ret = i40e_aq_resume_port_tx(hw, NULL);
6405 	if (ret) {
6406 		dev_info(&pf->pdev->dev,
6407 			 "Resume Port Tx failed, err %s aq_err %s\n",
6408 			  i40e_stat_str(&pf->hw, ret),
6409 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6410 		/* Schedule PF reset to recover */
6411 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6412 		i40e_service_event_schedule(pf);
6413 	}
6414 
6415 	return ret;
6416 }
6417 
6418 /**
6419  * i40e_update_dcb_config
6420  * @hw: pointer to the HW struct
6421  * @enable_mib_change: enable MIB change event
6422  *
6423  * Update DCB configuration from the firmware
6424  **/
6425 static enum i40e_status_code
6426 i40e_update_dcb_config(struct i40e_hw *hw, bool enable_mib_change)
6427 {
6428 	struct i40e_lldp_variables lldp_cfg;
6429 	i40e_status ret;
6430 
6431 	if (!hw->func_caps.dcb)
6432 		return I40E_NOT_SUPPORTED;
6433 
6434 	/* Read LLDP NVM area */
6435 	ret = i40e_read_lldp_cfg(hw, &lldp_cfg);
6436 	if (ret)
6437 		return I40E_ERR_NOT_READY;
6438 
6439 	/* Get DCBX status */
6440 	ret = i40e_get_dcbx_status(hw, &hw->dcbx_status);
6441 	if (ret)
6442 		return ret;
6443 
6444 	/* Check the DCBX Status */
6445 	if (hw->dcbx_status == I40E_DCBX_STATUS_DONE ||
6446 	    hw->dcbx_status == I40E_DCBX_STATUS_IN_PROGRESS) {
6447 		/* Get current DCBX configuration */
6448 		ret = i40e_get_dcb_config(hw);
6449 		if (ret)
6450 			return ret;
6451 	} else if (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED) {
6452 		return I40E_ERR_NOT_READY;
6453 	}
6454 
6455 	/* Configure the LLDP MIB change event */
6456 	if (enable_mib_change)
6457 		ret = i40e_aq_cfg_lldp_mib_change_event(hw, true, NULL);
6458 
6459 	return ret;
6460 }
6461 
6462 /**
6463  * i40e_init_pf_dcb - Initialize DCB configuration
6464  * @pf: PF being configured
6465  *
6466  * Query the current DCB configuration and cache it
6467  * in the hardware structure
6468  **/
6469 static int i40e_init_pf_dcb(struct i40e_pf *pf)
6470 {
6471 	struct i40e_hw *hw = &pf->hw;
6472 	int err = 0;
6473 
6474 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
6475 	 * Also do not enable DCBx if FW LLDP agent is disabled
6476 	 */
6477 	if ((pf->hw_features & I40E_HW_NO_DCB_SUPPORT) ||
6478 	    (pf->flags & I40E_FLAG_DISABLE_FW_LLDP)) {
6479 		dev_info(&pf->pdev->dev, "DCB is not supported or FW LLDP is disabled\n");
6480 		err = I40E_NOT_SUPPORTED;
6481 		goto out;
6482 	}
6483 
6484 	err = i40e_update_dcb_config(hw, true);
6485 	if (!err) {
6486 		/* Device/Function is not DCBX capable */
6487 		if ((!hw->func_caps.dcb) ||
6488 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6489 			dev_info(&pf->pdev->dev,
6490 				 "DCBX offload is not supported or is disabled for this PF.\n");
6491 		} else {
6492 			/* When status is not DISABLED then DCBX in FW */
6493 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6494 				       DCB_CAP_DCBX_VER_IEEE;
6495 
6496 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
6497 			/* Enable DCB tagging only when more than one TC
6498 			 * or explicitly disable if only one TC
6499 			 */
6500 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6501 				pf->flags |= I40E_FLAG_DCB_ENABLED;
6502 			else
6503 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6504 			dev_dbg(&pf->pdev->dev,
6505 				"DCBX offload is supported for this PF.\n");
6506 		}
6507 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
6508 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
6509 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
6510 	} else {
6511 		dev_info(&pf->pdev->dev,
6512 			 "Query for DCB configuration failed, err %s aq_err %s\n",
6513 			 i40e_stat_str(&pf->hw, err),
6514 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6515 	}
6516 
6517 out:
6518 	return err;
6519 }
6520 #endif /* CONFIG_I40E_DCB */
6521 #define SPEED_SIZE 14
6522 #define FC_SIZE 8
6523 /**
6524  * i40e_print_link_message - print link up or down
6525  * @vsi: the VSI for which link needs a message
6526  * @isup: true of link is up, false otherwise
6527  */
6528 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
6529 {
6530 	enum i40e_aq_link_speed new_speed;
6531 	struct i40e_pf *pf = vsi->back;
6532 	char *speed = "Unknown";
6533 	char *fc = "Unknown";
6534 	char *fec = "";
6535 	char *req_fec = "";
6536 	char *an = "";
6537 
6538 	if (isup)
6539 		new_speed = pf->hw.phy.link_info.link_speed;
6540 	else
6541 		new_speed = I40E_LINK_SPEED_UNKNOWN;
6542 
6543 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
6544 		return;
6545 	vsi->current_isup = isup;
6546 	vsi->current_speed = new_speed;
6547 	if (!isup) {
6548 		netdev_info(vsi->netdev, "NIC Link is Down\n");
6549 		return;
6550 	}
6551 
6552 	/* Warn user if link speed on NPAR enabled partition is not at
6553 	 * least 10GB
6554 	 */
6555 	if (pf->hw.func_caps.npar_enable &&
6556 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
6557 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
6558 		netdev_warn(vsi->netdev,
6559 			    "The partition detected link speed that is less than 10Gbps\n");
6560 
6561 	switch (pf->hw.phy.link_info.link_speed) {
6562 	case I40E_LINK_SPEED_40GB:
6563 		speed = "40 G";
6564 		break;
6565 	case I40E_LINK_SPEED_20GB:
6566 		speed = "20 G";
6567 		break;
6568 	case I40E_LINK_SPEED_25GB:
6569 		speed = "25 G";
6570 		break;
6571 	case I40E_LINK_SPEED_10GB:
6572 		speed = "10 G";
6573 		break;
6574 	case I40E_LINK_SPEED_5GB:
6575 		speed = "5 G";
6576 		break;
6577 	case I40E_LINK_SPEED_2_5GB:
6578 		speed = "2.5 G";
6579 		break;
6580 	case I40E_LINK_SPEED_1GB:
6581 		speed = "1000 M";
6582 		break;
6583 	case I40E_LINK_SPEED_100MB:
6584 		speed = "100 M";
6585 		break;
6586 	default:
6587 		break;
6588 	}
6589 
6590 	switch (pf->hw.fc.current_mode) {
6591 	case I40E_FC_FULL:
6592 		fc = "RX/TX";
6593 		break;
6594 	case I40E_FC_TX_PAUSE:
6595 		fc = "TX";
6596 		break;
6597 	case I40E_FC_RX_PAUSE:
6598 		fc = "RX";
6599 		break;
6600 	default:
6601 		fc = "None";
6602 		break;
6603 	}
6604 
6605 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
6606 		req_fec = ", Requested FEC: None";
6607 		fec = ", FEC: None";
6608 		an = ", Autoneg: False";
6609 
6610 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
6611 			an = ", Autoneg: True";
6612 
6613 		if (pf->hw.phy.link_info.fec_info &
6614 		    I40E_AQ_CONFIG_FEC_KR_ENA)
6615 			fec = ", FEC: CL74 FC-FEC/BASE-R";
6616 		else if (pf->hw.phy.link_info.fec_info &
6617 			 I40E_AQ_CONFIG_FEC_RS_ENA)
6618 			fec = ", FEC: CL108 RS-FEC";
6619 
6620 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
6621 		 * both RS and FC are requested
6622 		 */
6623 		if (vsi->back->hw.phy.link_info.req_fec_info &
6624 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
6625 			if (vsi->back->hw.phy.link_info.req_fec_info &
6626 			    I40E_AQ_REQUEST_FEC_RS)
6627 				req_fec = ", Requested FEC: CL108 RS-FEC";
6628 			else
6629 				req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
6630 		}
6631 	}
6632 
6633 	netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
6634 		    speed, req_fec, fec, an, fc);
6635 }
6636 
6637 /**
6638  * i40e_up_complete - Finish the last steps of bringing up a connection
6639  * @vsi: the VSI being configured
6640  **/
6641 static int i40e_up_complete(struct i40e_vsi *vsi)
6642 {
6643 	struct i40e_pf *pf = vsi->back;
6644 	int err;
6645 
6646 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6647 		i40e_vsi_configure_msix(vsi);
6648 	else
6649 		i40e_configure_msi_and_legacy(vsi);
6650 
6651 	/* start rings */
6652 	err = i40e_vsi_start_rings(vsi);
6653 	if (err)
6654 		return err;
6655 
6656 	clear_bit(__I40E_VSI_DOWN, vsi->state);
6657 	i40e_napi_enable_all(vsi);
6658 	i40e_vsi_enable_irq(vsi);
6659 
6660 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
6661 	    (vsi->netdev)) {
6662 		i40e_print_link_message(vsi, true);
6663 		netif_tx_start_all_queues(vsi->netdev);
6664 		netif_carrier_on(vsi->netdev);
6665 	}
6666 
6667 	/* replay FDIR SB filters */
6668 	if (vsi->type == I40E_VSI_FDIR) {
6669 		/* reset fd counters */
6670 		pf->fd_add_err = 0;
6671 		pf->fd_atr_cnt = 0;
6672 		i40e_fdir_filter_restore(vsi);
6673 	}
6674 
6675 	/* On the next run of the service_task, notify any clients of the new
6676 	 * opened netdev
6677 	 */
6678 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
6679 	i40e_service_event_schedule(pf);
6680 
6681 	return 0;
6682 }
6683 
6684 /**
6685  * i40e_vsi_reinit_locked - Reset the VSI
6686  * @vsi: the VSI being configured
6687  *
6688  * Rebuild the ring structs after some configuration
6689  * has changed, e.g. MTU size.
6690  **/
6691 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
6692 {
6693 	struct i40e_pf *pf = vsi->back;
6694 
6695 	WARN_ON(in_interrupt());
6696 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
6697 		usleep_range(1000, 2000);
6698 	i40e_down(vsi);
6699 
6700 	i40e_up(vsi);
6701 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
6702 }
6703 
6704 /**
6705  * i40e_up - Bring the connection back up after being down
6706  * @vsi: the VSI being configured
6707  **/
6708 int i40e_up(struct i40e_vsi *vsi)
6709 {
6710 	int err;
6711 
6712 	err = i40e_vsi_configure(vsi);
6713 	if (!err)
6714 		err = i40e_up_complete(vsi);
6715 
6716 	return err;
6717 }
6718 
6719 /**
6720  * i40e_force_link_state - Force the link status
6721  * @pf: board private structure
6722  * @is_up: whether the link state should be forced up or down
6723  **/
6724 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
6725 {
6726 	struct i40e_aq_get_phy_abilities_resp abilities;
6727 	struct i40e_aq_set_phy_config config = {0};
6728 	struct i40e_hw *hw = &pf->hw;
6729 	i40e_status err;
6730 	u64 mask;
6731 	u8 speed;
6732 
6733 	/* Card might've been put in an unstable state by other drivers
6734 	 * and applications, which causes incorrect speed values being
6735 	 * set on startup. In order to clear speed registers, we call
6736 	 * get_phy_capabilities twice, once to get initial state of
6737 	 * available speeds, and once to get current PHY config.
6738 	 */
6739 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
6740 					   NULL);
6741 	if (err) {
6742 		dev_err(&pf->pdev->dev,
6743 			"failed to get phy cap., ret =  %s last_status =  %s\n",
6744 			i40e_stat_str(hw, err),
6745 			i40e_aq_str(hw, hw->aq.asq_last_status));
6746 		return err;
6747 	}
6748 	speed = abilities.link_speed;
6749 
6750 	/* Get the current phy config */
6751 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
6752 					   NULL);
6753 	if (err) {
6754 		dev_err(&pf->pdev->dev,
6755 			"failed to get phy cap., ret =  %s last_status =  %s\n",
6756 			i40e_stat_str(hw, err),
6757 			i40e_aq_str(hw, hw->aq.asq_last_status));
6758 		return err;
6759 	}
6760 
6761 	/* If link needs to go up, but was not forced to go down,
6762 	 * and its speed values are OK, no need for a flap
6763 	 */
6764 	if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
6765 		return I40E_SUCCESS;
6766 
6767 	/* To force link we need to set bits for all supported PHY types,
6768 	 * but there are now more than 32, so we need to split the bitmap
6769 	 * across two fields.
6770 	 */
6771 	mask = I40E_PHY_TYPES_BITMASK;
6772 	config.phy_type = is_up ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
6773 	config.phy_type_ext = is_up ? (u8)((mask >> 32) & 0xff) : 0;
6774 	/* Copy the old settings, except of phy_type */
6775 	config.abilities = abilities.abilities;
6776 	if (abilities.link_speed != 0)
6777 		config.link_speed = abilities.link_speed;
6778 	else
6779 		config.link_speed = speed;
6780 	config.eee_capability = abilities.eee_capability;
6781 	config.eeer = abilities.eeer_val;
6782 	config.low_power_ctrl = abilities.d3_lpan;
6783 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
6784 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
6785 	err = i40e_aq_set_phy_config(hw, &config, NULL);
6786 
6787 	if (err) {
6788 		dev_err(&pf->pdev->dev,
6789 			"set phy config ret =  %s last_status =  %s\n",
6790 			i40e_stat_str(&pf->hw, err),
6791 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6792 		return err;
6793 	}
6794 
6795 	/* Update the link info */
6796 	err = i40e_update_link_info(hw);
6797 	if (err) {
6798 		/* Wait a little bit (on 40G cards it sometimes takes a really
6799 		 * long time for link to come back from the atomic reset)
6800 		 * and try once more
6801 		 */
6802 		msleep(1000);
6803 		i40e_update_link_info(hw);
6804 	}
6805 
6806 	i40e_aq_set_link_restart_an(hw, true, NULL);
6807 
6808 	return I40E_SUCCESS;
6809 }
6810 
6811 /**
6812  * i40e_down - Shutdown the connection processing
6813  * @vsi: the VSI being stopped
6814  **/
6815 void i40e_down(struct i40e_vsi *vsi)
6816 {
6817 	int i;
6818 
6819 	/* It is assumed that the caller of this function
6820 	 * sets the vsi->state __I40E_VSI_DOWN bit.
6821 	 */
6822 	if (vsi->netdev) {
6823 		netif_carrier_off(vsi->netdev);
6824 		netif_tx_disable(vsi->netdev);
6825 	}
6826 	i40e_vsi_disable_irq(vsi);
6827 	i40e_vsi_stop_rings(vsi);
6828 	if (vsi->type == I40E_VSI_MAIN &&
6829 	    vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED)
6830 		i40e_force_link_state(vsi->back, false);
6831 	i40e_napi_disable_all(vsi);
6832 
6833 	for (i = 0; i < vsi->num_queue_pairs; i++) {
6834 		i40e_clean_tx_ring(vsi->tx_rings[i]);
6835 		if (i40e_enabled_xdp_vsi(vsi)) {
6836 			/* Make sure that in-progress ndo_xdp_xmit
6837 			 * calls are completed.
6838 			 */
6839 			synchronize_rcu();
6840 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
6841 		}
6842 		i40e_clean_rx_ring(vsi->rx_rings[i]);
6843 	}
6844 
6845 }
6846 
6847 /**
6848  * i40e_validate_mqprio_qopt- validate queue mapping info
6849  * @vsi: the VSI being configured
6850  * @mqprio_qopt: queue parametrs
6851  **/
6852 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
6853 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
6854 {
6855 	u64 sum_max_rate = 0;
6856 	u64 max_rate = 0;
6857 	int i;
6858 
6859 	if (mqprio_qopt->qopt.offset[0] != 0 ||
6860 	    mqprio_qopt->qopt.num_tc < 1 ||
6861 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
6862 		return -EINVAL;
6863 	for (i = 0; ; i++) {
6864 		if (!mqprio_qopt->qopt.count[i])
6865 			return -EINVAL;
6866 		if (mqprio_qopt->min_rate[i]) {
6867 			dev_err(&vsi->back->pdev->dev,
6868 				"Invalid min tx rate (greater than 0) specified\n");
6869 			return -EINVAL;
6870 		}
6871 		max_rate = mqprio_qopt->max_rate[i];
6872 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6873 		sum_max_rate += max_rate;
6874 
6875 		if (i >= mqprio_qopt->qopt.num_tc - 1)
6876 			break;
6877 		if (mqprio_qopt->qopt.offset[i + 1] !=
6878 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
6879 			return -EINVAL;
6880 	}
6881 	if (vsi->num_queue_pairs <
6882 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
6883 		return -EINVAL;
6884 	}
6885 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
6886 		dev_err(&vsi->back->pdev->dev,
6887 			"Invalid max tx rate specified\n");
6888 		return -EINVAL;
6889 	}
6890 	return 0;
6891 }
6892 
6893 /**
6894  * i40e_vsi_set_default_tc_config - set default values for tc configuration
6895  * @vsi: the VSI being configured
6896  **/
6897 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
6898 {
6899 	u16 qcount;
6900 	int i;
6901 
6902 	/* Only TC0 is enabled */
6903 	vsi->tc_config.numtc = 1;
6904 	vsi->tc_config.enabled_tc = 1;
6905 	qcount = min_t(int, vsi->alloc_queue_pairs,
6906 		       i40e_pf_get_max_q_per_tc(vsi->back));
6907 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6908 		/* For the TC that is not enabled set the offset to to default
6909 		 * queue and allocate one queue for the given TC.
6910 		 */
6911 		vsi->tc_config.tc_info[i].qoffset = 0;
6912 		if (i == 0)
6913 			vsi->tc_config.tc_info[i].qcount = qcount;
6914 		else
6915 			vsi->tc_config.tc_info[i].qcount = 1;
6916 		vsi->tc_config.tc_info[i].netdev_tc = 0;
6917 	}
6918 }
6919 
6920 /**
6921  * i40e_del_macvlan_filter
6922  * @hw: pointer to the HW structure
6923  * @seid: seid of the channel VSI
6924  * @macaddr: the mac address to apply as a filter
6925  * @aq_err: store the admin Q error
6926  *
6927  * This function deletes a mac filter on the channel VSI which serves as the
6928  * macvlan. Returns 0 on success.
6929  **/
6930 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
6931 					   const u8 *macaddr, int *aq_err)
6932 {
6933 	struct i40e_aqc_remove_macvlan_element_data element;
6934 	i40e_status status;
6935 
6936 	memset(&element, 0, sizeof(element));
6937 	ether_addr_copy(element.mac_addr, macaddr);
6938 	element.vlan_tag = 0;
6939 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
6940 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
6941 	*aq_err = hw->aq.asq_last_status;
6942 
6943 	return status;
6944 }
6945 
6946 /**
6947  * i40e_add_macvlan_filter
6948  * @hw: pointer to the HW structure
6949  * @seid: seid of the channel VSI
6950  * @macaddr: the mac address to apply as a filter
6951  * @aq_err: store the admin Q error
6952  *
6953  * This function adds a mac filter on the channel VSI which serves as the
6954  * macvlan. Returns 0 on success.
6955  **/
6956 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
6957 					   const u8 *macaddr, int *aq_err)
6958 {
6959 	struct i40e_aqc_add_macvlan_element_data element;
6960 	i40e_status status;
6961 	u16 cmd_flags = 0;
6962 
6963 	ether_addr_copy(element.mac_addr, macaddr);
6964 	element.vlan_tag = 0;
6965 	element.queue_number = 0;
6966 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
6967 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
6968 	element.flags = cpu_to_le16(cmd_flags);
6969 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
6970 	*aq_err = hw->aq.asq_last_status;
6971 
6972 	return status;
6973 }
6974 
6975 /**
6976  * i40e_reset_ch_rings - Reset the queue contexts in a channel
6977  * @vsi: the VSI we want to access
6978  * @ch: the channel we want to access
6979  */
6980 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
6981 {
6982 	struct i40e_ring *tx_ring, *rx_ring;
6983 	u16 pf_q;
6984 	int i;
6985 
6986 	for (i = 0; i < ch->num_queue_pairs; i++) {
6987 		pf_q = ch->base_queue + i;
6988 		tx_ring = vsi->tx_rings[pf_q];
6989 		tx_ring->ch = NULL;
6990 		rx_ring = vsi->rx_rings[pf_q];
6991 		rx_ring->ch = NULL;
6992 	}
6993 }
6994 
6995 /**
6996  * i40e_free_macvlan_channels
6997  * @vsi: the VSI we want to access
6998  *
6999  * This function frees the Qs of the channel VSI from
7000  * the stack and also deletes the channel VSIs which
7001  * serve as macvlans.
7002  */
7003 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7004 {
7005 	struct i40e_channel *ch, *ch_tmp;
7006 	int ret;
7007 
7008 	if (list_empty(&vsi->macvlan_list))
7009 		return;
7010 
7011 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7012 		struct i40e_vsi *parent_vsi;
7013 
7014 		if (i40e_is_channel_macvlan(ch)) {
7015 			i40e_reset_ch_rings(vsi, ch);
7016 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7017 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7018 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7019 			kfree(ch->fwd);
7020 			ch->fwd = NULL;
7021 		}
7022 
7023 		list_del(&ch->list);
7024 		parent_vsi = ch->parent_vsi;
7025 		if (!parent_vsi || !ch->initialized) {
7026 			kfree(ch);
7027 			continue;
7028 		}
7029 
7030 		/* remove the VSI */
7031 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7032 					     NULL);
7033 		if (ret)
7034 			dev_err(&vsi->back->pdev->dev,
7035 				"unable to remove channel (%d) for parent VSI(%d)\n",
7036 				ch->seid, parent_vsi->seid);
7037 		kfree(ch);
7038 	}
7039 	vsi->macvlan_cnt = 0;
7040 }
7041 
7042 /**
7043  * i40e_fwd_ring_up - bring the macvlan device up
7044  * @vsi: the VSI we want to access
7045  * @vdev: macvlan netdevice
7046  * @fwd: the private fwd structure
7047  */
7048 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7049 			    struct i40e_fwd_adapter *fwd)
7050 {
7051 	int ret = 0, num_tc = 1,  i, aq_err;
7052 	struct i40e_channel *ch, *ch_tmp;
7053 	struct i40e_pf *pf = vsi->back;
7054 	struct i40e_hw *hw = &pf->hw;
7055 
7056 	if (list_empty(&vsi->macvlan_list))
7057 		return -EINVAL;
7058 
7059 	/* Go through the list and find an available channel */
7060 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7061 		if (!i40e_is_channel_macvlan(ch)) {
7062 			ch->fwd = fwd;
7063 			/* record configuration for macvlan interface in vdev */
7064 			for (i = 0; i < num_tc; i++)
7065 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7066 							     i,
7067 							     ch->num_queue_pairs,
7068 							     ch->base_queue);
7069 			for (i = 0; i < ch->num_queue_pairs; i++) {
7070 				struct i40e_ring *tx_ring, *rx_ring;
7071 				u16 pf_q;
7072 
7073 				pf_q = ch->base_queue + i;
7074 
7075 				/* Get to TX ring ptr */
7076 				tx_ring = vsi->tx_rings[pf_q];
7077 				tx_ring->ch = ch;
7078 
7079 				/* Get the RX ring ptr */
7080 				rx_ring = vsi->rx_rings[pf_q];
7081 				rx_ring->ch = ch;
7082 			}
7083 			break;
7084 		}
7085 	}
7086 
7087 	/* Guarantee all rings are updated before we update the
7088 	 * MAC address filter.
7089 	 */
7090 	wmb();
7091 
7092 	/* Add a mac filter */
7093 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7094 	if (ret) {
7095 		/* if we cannot add the MAC rule then disable the offload */
7096 		macvlan_release_l2fw_offload(vdev);
7097 		for (i = 0; i < ch->num_queue_pairs; i++) {
7098 			struct i40e_ring *rx_ring;
7099 			u16 pf_q;
7100 
7101 			pf_q = ch->base_queue + i;
7102 			rx_ring = vsi->rx_rings[pf_q];
7103 			rx_ring->netdev = NULL;
7104 		}
7105 		dev_info(&pf->pdev->dev,
7106 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7107 			  i40e_stat_str(hw, ret),
7108 			  i40e_aq_str(hw, aq_err));
7109 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7110 	}
7111 
7112 	return ret;
7113 }
7114 
7115 /**
7116  * i40e_setup_macvlans - create the channels which will be macvlans
7117  * @vsi: the VSI we want to access
7118  * @macvlan_cnt: no. of macvlans to be setup
7119  * @qcnt: no. of Qs per macvlan
7120  * @vdev: macvlan netdevice
7121  */
7122 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7123 			       struct net_device *vdev)
7124 {
7125 	struct i40e_pf *pf = vsi->back;
7126 	struct i40e_hw *hw = &pf->hw;
7127 	struct i40e_vsi_context ctxt;
7128 	u16 sections, qmap, num_qps;
7129 	struct i40e_channel *ch;
7130 	int i, pow, ret = 0;
7131 	u8 offset = 0;
7132 
7133 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7134 		return -EINVAL;
7135 
7136 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7137 
7138 	/* find the next higher power-of-2 of num queue pairs */
7139 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7140 
7141 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7142 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7143 
7144 	/* Setup context bits for the main VSI */
7145 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7146 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7147 	memset(&ctxt, 0, sizeof(ctxt));
7148 	ctxt.seid = vsi->seid;
7149 	ctxt.pf_num = vsi->back->hw.pf_id;
7150 	ctxt.vf_num = 0;
7151 	ctxt.uplink_seid = vsi->uplink_seid;
7152 	ctxt.info = vsi->info;
7153 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7154 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7155 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7156 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7157 
7158 	/* Reconfigure RSS for main VSI with new max queue count */
7159 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7160 	ret = i40e_vsi_config_rss(vsi);
7161 	if (ret) {
7162 		dev_info(&pf->pdev->dev,
7163 			 "Failed to reconfig RSS for num_queues (%u)\n",
7164 			 vsi->rss_size);
7165 		return ret;
7166 	}
7167 	vsi->reconfig_rss = true;
7168 	dev_dbg(&vsi->back->pdev->dev,
7169 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7170 	vsi->next_base_queue = num_qps;
7171 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7172 
7173 	/* Update the VSI after updating the VSI queue-mapping
7174 	 * information
7175 	 */
7176 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7177 	if (ret) {
7178 		dev_info(&pf->pdev->dev,
7179 			 "Update vsi tc config failed, err %s aq_err %s\n",
7180 			 i40e_stat_str(hw, ret),
7181 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7182 		return ret;
7183 	}
7184 	/* update the local VSI info with updated queue map */
7185 	i40e_vsi_update_queue_map(vsi, &ctxt);
7186 	vsi->info.valid_sections = 0;
7187 
7188 	/* Create channels for macvlans */
7189 	INIT_LIST_HEAD(&vsi->macvlan_list);
7190 	for (i = 0; i < macvlan_cnt; i++) {
7191 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7192 		if (!ch) {
7193 			ret = -ENOMEM;
7194 			goto err_free;
7195 		}
7196 		INIT_LIST_HEAD(&ch->list);
7197 		ch->num_queue_pairs = qcnt;
7198 		if (!i40e_setup_channel(pf, vsi, ch)) {
7199 			ret = -EINVAL;
7200 			goto err_free;
7201 		}
7202 		ch->parent_vsi = vsi;
7203 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7204 		vsi->macvlan_cnt++;
7205 		list_add_tail(&ch->list, &vsi->macvlan_list);
7206 	}
7207 
7208 	return ret;
7209 
7210 err_free:
7211 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7212 	i40e_free_macvlan_channels(vsi);
7213 
7214 	return ret;
7215 }
7216 
7217 /**
7218  * i40e_fwd_add - configure macvlans
7219  * @netdev: net device to configure
7220  * @vdev: macvlan netdevice
7221  **/
7222 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7223 {
7224 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7225 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7226 	struct i40e_vsi *vsi = np->vsi;
7227 	struct i40e_pf *pf = vsi->back;
7228 	struct i40e_fwd_adapter *fwd;
7229 	int avail_macvlan, ret;
7230 
7231 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7232 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7233 		return ERR_PTR(-EINVAL);
7234 	}
7235 	if ((pf->flags & I40E_FLAG_TC_MQPRIO)) {
7236 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7237 		return ERR_PTR(-EINVAL);
7238 	}
7239 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7240 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7241 		return ERR_PTR(-EINVAL);
7242 	}
7243 
7244 	/* The macvlan device has to be a single Q device so that the
7245 	 * tc_to_txq field can be reused to pick the tx queue.
7246 	 */
7247 	if (netif_is_multiqueue(vdev))
7248 		return ERR_PTR(-ERANGE);
7249 
7250 	if (!vsi->macvlan_cnt) {
7251 		/* reserve bit 0 for the pf device */
7252 		set_bit(0, vsi->fwd_bitmask);
7253 
7254 		/* Try to reserve as many queues as possible for macvlans. First
7255 		 * reserve 3/4th of max vectors, then half, then quarter and
7256 		 * calculate Qs per macvlan as you go
7257 		 */
7258 		vectors = pf->num_lan_msix;
7259 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7260 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7261 			q_per_macvlan = 4;
7262 			macvlan_cnt = (vectors - 32) / 4;
7263 		} else if (vectors <= 64 && vectors > 32) {
7264 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7265 			q_per_macvlan = 2;
7266 			macvlan_cnt = (vectors - 16) / 2;
7267 		} else if (vectors <= 32 && vectors > 16) {
7268 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7269 			q_per_macvlan = 1;
7270 			macvlan_cnt = vectors - 16;
7271 		} else if (vectors <= 16 && vectors > 8) {
7272 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7273 			q_per_macvlan = 1;
7274 			macvlan_cnt = vectors - 8;
7275 		} else {
7276 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7277 			q_per_macvlan = 1;
7278 			macvlan_cnt = vectors - 1;
7279 		}
7280 
7281 		if (macvlan_cnt == 0)
7282 			return ERR_PTR(-EBUSY);
7283 
7284 		/* Quiesce VSI queues */
7285 		i40e_quiesce_vsi(vsi);
7286 
7287 		/* sets up the macvlans but does not "enable" them */
7288 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
7289 					  vdev);
7290 		if (ret)
7291 			return ERR_PTR(ret);
7292 
7293 		/* Unquiesce VSI */
7294 		i40e_unquiesce_vsi(vsi);
7295 	}
7296 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
7297 					    vsi->macvlan_cnt);
7298 	if (avail_macvlan >= I40E_MAX_MACVLANS)
7299 		return ERR_PTR(-EBUSY);
7300 
7301 	/* create the fwd struct */
7302 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
7303 	if (!fwd)
7304 		return ERR_PTR(-ENOMEM);
7305 
7306 	set_bit(avail_macvlan, vsi->fwd_bitmask);
7307 	fwd->bit_no = avail_macvlan;
7308 	netdev_set_sb_channel(vdev, avail_macvlan);
7309 	fwd->netdev = vdev;
7310 
7311 	if (!netif_running(netdev))
7312 		return fwd;
7313 
7314 	/* Set fwd ring up */
7315 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
7316 	if (ret) {
7317 		/* unbind the queues and drop the subordinate channel config */
7318 		netdev_unbind_sb_channel(netdev, vdev);
7319 		netdev_set_sb_channel(vdev, 0);
7320 
7321 		kfree(fwd);
7322 		return ERR_PTR(-EINVAL);
7323 	}
7324 
7325 	return fwd;
7326 }
7327 
7328 /**
7329  * i40e_del_all_macvlans - Delete all the mac filters on the channels
7330  * @vsi: the VSI we want to access
7331  */
7332 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
7333 {
7334 	struct i40e_channel *ch, *ch_tmp;
7335 	struct i40e_pf *pf = vsi->back;
7336 	struct i40e_hw *hw = &pf->hw;
7337 	int aq_err, ret = 0;
7338 
7339 	if (list_empty(&vsi->macvlan_list))
7340 		return;
7341 
7342 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7343 		if (i40e_is_channel_macvlan(ch)) {
7344 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7345 						      i40e_channel_mac(ch),
7346 						      &aq_err);
7347 			if (!ret) {
7348 				/* Reset queue contexts */
7349 				i40e_reset_ch_rings(vsi, ch);
7350 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7351 				netdev_unbind_sb_channel(vsi->netdev,
7352 							 ch->fwd->netdev);
7353 				netdev_set_sb_channel(ch->fwd->netdev, 0);
7354 				kfree(ch->fwd);
7355 				ch->fwd = NULL;
7356 			}
7357 		}
7358 	}
7359 }
7360 
7361 /**
7362  * i40e_fwd_del - delete macvlan interfaces
7363  * @netdev: net device to configure
7364  * @vdev: macvlan netdevice
7365  */
7366 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
7367 {
7368 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7369 	struct i40e_fwd_adapter *fwd = vdev;
7370 	struct i40e_channel *ch, *ch_tmp;
7371 	struct i40e_vsi *vsi = np->vsi;
7372 	struct i40e_pf *pf = vsi->back;
7373 	struct i40e_hw *hw = &pf->hw;
7374 	int aq_err, ret = 0;
7375 
7376 	/* Find the channel associated with the macvlan and del mac filter */
7377 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7378 		if (i40e_is_channel_macvlan(ch) &&
7379 		    ether_addr_equal(i40e_channel_mac(ch),
7380 				     fwd->netdev->dev_addr)) {
7381 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7382 						      i40e_channel_mac(ch),
7383 						      &aq_err);
7384 			if (!ret) {
7385 				/* Reset queue contexts */
7386 				i40e_reset_ch_rings(vsi, ch);
7387 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7388 				netdev_unbind_sb_channel(netdev, fwd->netdev);
7389 				netdev_set_sb_channel(fwd->netdev, 0);
7390 				kfree(ch->fwd);
7391 				ch->fwd = NULL;
7392 			} else {
7393 				dev_info(&pf->pdev->dev,
7394 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
7395 					  i40e_stat_str(hw, ret),
7396 					  i40e_aq_str(hw, aq_err));
7397 			}
7398 			break;
7399 		}
7400 	}
7401 }
7402 
7403 /**
7404  * i40e_setup_tc - configure multiple traffic classes
7405  * @netdev: net device to configure
7406  * @type_data: tc offload data
7407  **/
7408 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
7409 {
7410 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
7411 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7412 	struct i40e_vsi *vsi = np->vsi;
7413 	struct i40e_pf *pf = vsi->back;
7414 	u8 enabled_tc = 0, num_tc, hw;
7415 	bool need_reset = false;
7416 	int old_queue_pairs;
7417 	int ret = -EINVAL;
7418 	u16 mode;
7419 	int i;
7420 
7421 	old_queue_pairs = vsi->num_queue_pairs;
7422 	num_tc = mqprio_qopt->qopt.num_tc;
7423 	hw = mqprio_qopt->qopt.hw;
7424 	mode = mqprio_qopt->mode;
7425 	if (!hw) {
7426 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7427 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
7428 		goto config_tc;
7429 	}
7430 
7431 	/* Check if MFP enabled */
7432 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
7433 		netdev_info(netdev,
7434 			    "Configuring TC not supported in MFP mode\n");
7435 		return ret;
7436 	}
7437 	switch (mode) {
7438 	case TC_MQPRIO_MODE_DCB:
7439 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7440 
7441 		/* Check if DCB enabled to continue */
7442 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7443 			netdev_info(netdev,
7444 				    "DCB is not enabled for adapter\n");
7445 			return ret;
7446 		}
7447 
7448 		/* Check whether tc count is within enabled limit */
7449 		if (num_tc > i40e_pf_get_num_tc(pf)) {
7450 			netdev_info(netdev,
7451 				    "TC count greater than enabled on link for adapter\n");
7452 			return ret;
7453 		}
7454 		break;
7455 	case TC_MQPRIO_MODE_CHANNEL:
7456 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
7457 			netdev_info(netdev,
7458 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
7459 			return ret;
7460 		}
7461 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7462 			return ret;
7463 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
7464 		if (ret)
7465 			return ret;
7466 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
7467 		       sizeof(*mqprio_qopt));
7468 		pf->flags |= I40E_FLAG_TC_MQPRIO;
7469 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7470 		break;
7471 	default:
7472 		return -EINVAL;
7473 	}
7474 
7475 config_tc:
7476 	/* Generate TC map for number of tc requested */
7477 	for (i = 0; i < num_tc; i++)
7478 		enabled_tc |= BIT(i);
7479 
7480 	/* Requesting same TC configuration as already enabled */
7481 	if (enabled_tc == vsi->tc_config.enabled_tc &&
7482 	    mode != TC_MQPRIO_MODE_CHANNEL)
7483 		return 0;
7484 
7485 	/* Quiesce VSI queues */
7486 	i40e_quiesce_vsi(vsi);
7487 
7488 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
7489 		i40e_remove_queue_channels(vsi);
7490 
7491 	/* Configure VSI for enabled TCs */
7492 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
7493 	if (ret) {
7494 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
7495 			    vsi->seid);
7496 		need_reset = true;
7497 		goto exit;
7498 	} else {
7499 		dev_info(&vsi->back->pdev->dev,
7500 			 "Setup channel (id:%u) utilizing num_queues %d\n",
7501 			 vsi->seid, vsi->tc_config.tc_info[0].qcount);
7502 	}
7503 
7504 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
7505 		if (vsi->mqprio_qopt.max_rate[0]) {
7506 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
7507 
7508 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
7509 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
7510 			if (!ret) {
7511 				u64 credits = max_tx_rate;
7512 
7513 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
7514 				dev_dbg(&vsi->back->pdev->dev,
7515 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
7516 					max_tx_rate,
7517 					credits,
7518 					vsi->seid);
7519 			} else {
7520 				need_reset = true;
7521 				goto exit;
7522 			}
7523 		}
7524 		ret = i40e_configure_queue_channels(vsi);
7525 		if (ret) {
7526 			vsi->num_queue_pairs = old_queue_pairs;
7527 			netdev_info(netdev,
7528 				    "Failed configuring queue channels\n");
7529 			need_reset = true;
7530 			goto exit;
7531 		}
7532 	}
7533 
7534 exit:
7535 	/* Reset the configuration data to defaults, only TC0 is enabled */
7536 	if (need_reset) {
7537 		i40e_vsi_set_default_tc_config(vsi);
7538 		need_reset = false;
7539 	}
7540 
7541 	/* Unquiesce VSI */
7542 	i40e_unquiesce_vsi(vsi);
7543 	return ret;
7544 }
7545 
7546 /**
7547  * i40e_set_cld_element - sets cloud filter element data
7548  * @filter: cloud filter rule
7549  * @cld: ptr to cloud filter element data
7550  *
7551  * This is helper function to copy data into cloud filter element
7552  **/
7553 static inline void
7554 i40e_set_cld_element(struct i40e_cloud_filter *filter,
7555 		     struct i40e_aqc_cloud_filters_element_data *cld)
7556 {
7557 	int i, j;
7558 	u32 ipa;
7559 
7560 	memset(cld, 0, sizeof(*cld));
7561 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
7562 	ether_addr_copy(cld->inner_mac, filter->src_mac);
7563 
7564 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
7565 		return;
7566 
7567 	if (filter->n_proto == ETH_P_IPV6) {
7568 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
7569 		for (i = 0, j = 0; i < ARRAY_SIZE(filter->dst_ipv6);
7570 		     i++, j += 2) {
7571 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
7572 			ipa = cpu_to_le32(ipa);
7573 			memcpy(&cld->ipaddr.raw_v6.data[j], &ipa, sizeof(ipa));
7574 		}
7575 	} else {
7576 		ipa = be32_to_cpu(filter->dst_ipv4);
7577 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
7578 	}
7579 
7580 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
7581 
7582 	/* tenant_id is not supported by FW now, once the support is enabled
7583 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
7584 	 */
7585 	if (filter->tenant_id)
7586 		return;
7587 }
7588 
7589 /**
7590  * i40e_add_del_cloud_filter - Add/del cloud filter
7591  * @vsi: pointer to VSI
7592  * @filter: cloud filter rule
7593  * @add: if true, add, if false, delete
7594  *
7595  * Add or delete a cloud filter for a specific flow spec.
7596  * Returns 0 if the filter were successfully added.
7597  **/
7598 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
7599 			      struct i40e_cloud_filter *filter, bool add)
7600 {
7601 	struct i40e_aqc_cloud_filters_element_data cld_filter;
7602 	struct i40e_pf *pf = vsi->back;
7603 	int ret;
7604 	static const u16 flag_table[128] = {
7605 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
7606 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
7607 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
7608 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
7609 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
7610 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
7611 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
7612 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
7613 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
7614 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
7615 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
7616 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
7617 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
7618 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
7619 	};
7620 
7621 	if (filter->flags >= ARRAY_SIZE(flag_table))
7622 		return I40E_ERR_CONFIG;
7623 
7624 	/* copy element needed to add cloud filter from filter */
7625 	i40e_set_cld_element(filter, &cld_filter);
7626 
7627 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
7628 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
7629 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
7630 
7631 	if (filter->n_proto == ETH_P_IPV6)
7632 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
7633 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
7634 	else
7635 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
7636 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
7637 
7638 	if (add)
7639 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
7640 						&cld_filter, 1);
7641 	else
7642 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
7643 						&cld_filter, 1);
7644 	if (ret)
7645 		dev_dbg(&pf->pdev->dev,
7646 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
7647 			add ? "add" : "delete", filter->dst_port, ret,
7648 			pf->hw.aq.asq_last_status);
7649 	else
7650 		dev_info(&pf->pdev->dev,
7651 			 "%s cloud filter for VSI: %d\n",
7652 			 add ? "Added" : "Deleted", filter->seid);
7653 	return ret;
7654 }
7655 
7656 /**
7657  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
7658  * @vsi: pointer to VSI
7659  * @filter: cloud filter rule
7660  * @add: if true, add, if false, delete
7661  *
7662  * Add or delete a cloud filter for a specific flow spec using big buffer.
7663  * Returns 0 if the filter were successfully added.
7664  **/
7665 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
7666 				      struct i40e_cloud_filter *filter,
7667 				      bool add)
7668 {
7669 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
7670 	struct i40e_pf *pf = vsi->back;
7671 	int ret;
7672 
7673 	/* Both (src/dst) valid mac_addr are not supported */
7674 	if ((is_valid_ether_addr(filter->dst_mac) &&
7675 	     is_valid_ether_addr(filter->src_mac)) ||
7676 	    (is_multicast_ether_addr(filter->dst_mac) &&
7677 	     is_multicast_ether_addr(filter->src_mac)))
7678 		return -EOPNOTSUPP;
7679 
7680 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
7681 	 * ports are not supported via big buffer now.
7682 	 */
7683 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
7684 		return -EOPNOTSUPP;
7685 
7686 	/* adding filter using src_port/src_ip is not supported at this stage */
7687 	if (filter->src_port || filter->src_ipv4 ||
7688 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
7689 		return -EOPNOTSUPP;
7690 
7691 	/* copy element needed to add cloud filter from filter */
7692 	i40e_set_cld_element(filter, &cld_filter.element);
7693 
7694 	if (is_valid_ether_addr(filter->dst_mac) ||
7695 	    is_valid_ether_addr(filter->src_mac) ||
7696 	    is_multicast_ether_addr(filter->dst_mac) ||
7697 	    is_multicast_ether_addr(filter->src_mac)) {
7698 		/* MAC + IP : unsupported mode */
7699 		if (filter->dst_ipv4)
7700 			return -EOPNOTSUPP;
7701 
7702 		/* since we validated that L4 port must be valid before
7703 		 * we get here, start with respective "flags" value
7704 		 * and update if vlan is present or not
7705 		 */
7706 		cld_filter.element.flags =
7707 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
7708 
7709 		if (filter->vlan_id) {
7710 			cld_filter.element.flags =
7711 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
7712 		}
7713 
7714 	} else if (filter->dst_ipv4 ||
7715 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
7716 		cld_filter.element.flags =
7717 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
7718 		if (filter->n_proto == ETH_P_IPV6)
7719 			cld_filter.element.flags |=
7720 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
7721 		else
7722 			cld_filter.element.flags |=
7723 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
7724 	} else {
7725 		dev_err(&pf->pdev->dev,
7726 			"either mac or ip has to be valid for cloud filter\n");
7727 		return -EINVAL;
7728 	}
7729 
7730 	/* Now copy L4 port in Byte 6..7 in general fields */
7731 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
7732 						be16_to_cpu(filter->dst_port);
7733 
7734 	if (add) {
7735 		/* Validate current device switch mode, change if necessary */
7736 		ret = i40e_validate_and_set_switch_mode(vsi);
7737 		if (ret) {
7738 			dev_err(&pf->pdev->dev,
7739 				"failed to set switch mode, ret %d\n",
7740 				ret);
7741 			return ret;
7742 		}
7743 
7744 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
7745 						   &cld_filter, 1);
7746 	} else {
7747 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
7748 						   &cld_filter, 1);
7749 	}
7750 
7751 	if (ret)
7752 		dev_dbg(&pf->pdev->dev,
7753 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
7754 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
7755 	else
7756 		dev_info(&pf->pdev->dev,
7757 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
7758 			 add ? "add" : "delete", filter->seid,
7759 			 ntohs(filter->dst_port));
7760 	return ret;
7761 }
7762 
7763 /**
7764  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
7765  * @vsi: Pointer to VSI
7766  * @cls_flower: Pointer to struct flow_cls_offload
7767  * @filter: Pointer to cloud filter structure
7768  *
7769  **/
7770 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
7771 				 struct flow_cls_offload *f,
7772 				 struct i40e_cloud_filter *filter)
7773 {
7774 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
7775 	struct flow_dissector *dissector = rule->match.dissector;
7776 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
7777 	struct i40e_pf *pf = vsi->back;
7778 	u8 field_flags = 0;
7779 
7780 	if (dissector->used_keys &
7781 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
7782 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
7783 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
7784 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
7785 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
7786 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
7787 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
7788 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
7789 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
7790 			dissector->used_keys);
7791 		return -EOPNOTSUPP;
7792 	}
7793 
7794 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
7795 		struct flow_match_enc_keyid match;
7796 
7797 		flow_rule_match_enc_keyid(rule, &match);
7798 		if (match.mask->keyid != 0)
7799 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
7800 
7801 		filter->tenant_id = be32_to_cpu(match.key->keyid);
7802 	}
7803 
7804 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
7805 		struct flow_match_basic match;
7806 
7807 		flow_rule_match_basic(rule, &match);
7808 		n_proto_key = ntohs(match.key->n_proto);
7809 		n_proto_mask = ntohs(match.mask->n_proto);
7810 
7811 		if (n_proto_key == ETH_P_ALL) {
7812 			n_proto_key = 0;
7813 			n_proto_mask = 0;
7814 		}
7815 		filter->n_proto = n_proto_key & n_proto_mask;
7816 		filter->ip_proto = match.key->ip_proto;
7817 	}
7818 
7819 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
7820 		struct flow_match_eth_addrs match;
7821 
7822 		flow_rule_match_eth_addrs(rule, &match);
7823 
7824 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
7825 		if (!is_zero_ether_addr(match.mask->dst)) {
7826 			if (is_broadcast_ether_addr(match.mask->dst)) {
7827 				field_flags |= I40E_CLOUD_FIELD_OMAC;
7828 			} else {
7829 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
7830 					match.mask->dst);
7831 				return I40E_ERR_CONFIG;
7832 			}
7833 		}
7834 
7835 		if (!is_zero_ether_addr(match.mask->src)) {
7836 			if (is_broadcast_ether_addr(match.mask->src)) {
7837 				field_flags |= I40E_CLOUD_FIELD_IMAC;
7838 			} else {
7839 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
7840 					match.mask->src);
7841 				return I40E_ERR_CONFIG;
7842 			}
7843 		}
7844 		ether_addr_copy(filter->dst_mac, match.key->dst);
7845 		ether_addr_copy(filter->src_mac, match.key->src);
7846 	}
7847 
7848 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
7849 		struct flow_match_vlan match;
7850 
7851 		flow_rule_match_vlan(rule, &match);
7852 		if (match.mask->vlan_id) {
7853 			if (match.mask->vlan_id == VLAN_VID_MASK) {
7854 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
7855 
7856 			} else {
7857 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
7858 					match.mask->vlan_id);
7859 				return I40E_ERR_CONFIG;
7860 			}
7861 		}
7862 
7863 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
7864 	}
7865 
7866 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
7867 		struct flow_match_control match;
7868 
7869 		flow_rule_match_control(rule, &match);
7870 		addr_type = match.key->addr_type;
7871 	}
7872 
7873 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
7874 		struct flow_match_ipv4_addrs match;
7875 
7876 		flow_rule_match_ipv4_addrs(rule, &match);
7877 		if (match.mask->dst) {
7878 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
7879 				field_flags |= I40E_CLOUD_FIELD_IIP;
7880 			} else {
7881 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
7882 					&match.mask->dst);
7883 				return I40E_ERR_CONFIG;
7884 			}
7885 		}
7886 
7887 		if (match.mask->src) {
7888 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
7889 				field_flags |= I40E_CLOUD_FIELD_IIP;
7890 			} else {
7891 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
7892 					&match.mask->src);
7893 				return I40E_ERR_CONFIG;
7894 			}
7895 		}
7896 
7897 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
7898 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
7899 			return I40E_ERR_CONFIG;
7900 		}
7901 		filter->dst_ipv4 = match.key->dst;
7902 		filter->src_ipv4 = match.key->src;
7903 	}
7904 
7905 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
7906 		struct flow_match_ipv6_addrs match;
7907 
7908 		flow_rule_match_ipv6_addrs(rule, &match);
7909 
7910 		/* src and dest IPV6 address should not be LOOPBACK
7911 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
7912 		 */
7913 		if (ipv6_addr_loopback(&match.key->dst) ||
7914 		    ipv6_addr_loopback(&match.key->src)) {
7915 			dev_err(&pf->pdev->dev,
7916 				"Bad ipv6, addr is LOOPBACK\n");
7917 			return I40E_ERR_CONFIG;
7918 		}
7919 		if (!ipv6_addr_any(&match.mask->dst) ||
7920 		    !ipv6_addr_any(&match.mask->src))
7921 			field_flags |= I40E_CLOUD_FIELD_IIP;
7922 
7923 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
7924 		       sizeof(filter->src_ipv6));
7925 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
7926 		       sizeof(filter->dst_ipv6));
7927 	}
7928 
7929 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
7930 		struct flow_match_ports match;
7931 
7932 		flow_rule_match_ports(rule, &match);
7933 		if (match.mask->src) {
7934 			if (match.mask->src == cpu_to_be16(0xffff)) {
7935 				field_flags |= I40E_CLOUD_FIELD_IIP;
7936 			} else {
7937 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
7938 					be16_to_cpu(match.mask->src));
7939 				return I40E_ERR_CONFIG;
7940 			}
7941 		}
7942 
7943 		if (match.mask->dst) {
7944 			if (match.mask->dst == cpu_to_be16(0xffff)) {
7945 				field_flags |= I40E_CLOUD_FIELD_IIP;
7946 			} else {
7947 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
7948 					be16_to_cpu(match.mask->dst));
7949 				return I40E_ERR_CONFIG;
7950 			}
7951 		}
7952 
7953 		filter->dst_port = match.key->dst;
7954 		filter->src_port = match.key->src;
7955 
7956 		switch (filter->ip_proto) {
7957 		case IPPROTO_TCP:
7958 		case IPPROTO_UDP:
7959 			break;
7960 		default:
7961 			dev_err(&pf->pdev->dev,
7962 				"Only UDP and TCP transport are supported\n");
7963 			return -EINVAL;
7964 		}
7965 	}
7966 	filter->flags = field_flags;
7967 	return 0;
7968 }
7969 
7970 /**
7971  * i40e_handle_tclass: Forward to a traffic class on the device
7972  * @vsi: Pointer to VSI
7973  * @tc: traffic class index on the device
7974  * @filter: Pointer to cloud filter structure
7975  *
7976  **/
7977 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
7978 			      struct i40e_cloud_filter *filter)
7979 {
7980 	struct i40e_channel *ch, *ch_tmp;
7981 
7982 	/* direct to a traffic class on the same device */
7983 	if (tc == 0) {
7984 		filter->seid = vsi->seid;
7985 		return 0;
7986 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
7987 		if (!filter->dst_port) {
7988 			dev_err(&vsi->back->pdev->dev,
7989 				"Specify destination port to direct to traffic class that is not default\n");
7990 			return -EINVAL;
7991 		}
7992 		if (list_empty(&vsi->ch_list))
7993 			return -EINVAL;
7994 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
7995 					 list) {
7996 			if (ch->seid == vsi->tc_seid_map[tc])
7997 				filter->seid = ch->seid;
7998 		}
7999 		return 0;
8000 	}
8001 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8002 	return -EINVAL;
8003 }
8004 
8005 /**
8006  * i40e_configure_clsflower - Configure tc flower filters
8007  * @vsi: Pointer to VSI
8008  * @cls_flower: Pointer to struct flow_cls_offload
8009  *
8010  **/
8011 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8012 				    struct flow_cls_offload *cls_flower)
8013 {
8014 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8015 	struct i40e_cloud_filter *filter = NULL;
8016 	struct i40e_pf *pf = vsi->back;
8017 	int err = 0;
8018 
8019 	if (tc < 0) {
8020 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8021 		return -EOPNOTSUPP;
8022 	}
8023 
8024 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8025 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8026 		return -EBUSY;
8027 
8028 	if (pf->fdir_pf_active_filters ||
8029 	    (!hlist_empty(&pf->fdir_filter_list))) {
8030 		dev_err(&vsi->back->pdev->dev,
8031 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8032 		return -EINVAL;
8033 	}
8034 
8035 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8036 		dev_err(&vsi->back->pdev->dev,
8037 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8038 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8039 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8040 	}
8041 
8042 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8043 	if (!filter)
8044 		return -ENOMEM;
8045 
8046 	filter->cookie = cls_flower->cookie;
8047 
8048 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8049 	if (err < 0)
8050 		goto err;
8051 
8052 	err = i40e_handle_tclass(vsi, tc, filter);
8053 	if (err < 0)
8054 		goto err;
8055 
8056 	/* Add cloud filter */
8057 	if (filter->dst_port)
8058 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8059 	else
8060 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8061 
8062 	if (err) {
8063 		dev_err(&pf->pdev->dev,
8064 			"Failed to add cloud filter, err %s\n",
8065 			i40e_stat_str(&pf->hw, err));
8066 		goto err;
8067 	}
8068 
8069 	/* add filter to the ordered list */
8070 	INIT_HLIST_NODE(&filter->cloud_node);
8071 
8072 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8073 
8074 	pf->num_cloud_filters++;
8075 
8076 	return err;
8077 err:
8078 	kfree(filter);
8079 	return err;
8080 }
8081 
8082 /**
8083  * i40e_find_cloud_filter - Find the could filter in the list
8084  * @vsi: Pointer to VSI
8085  * @cookie: filter specific cookie
8086  *
8087  **/
8088 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8089 							unsigned long *cookie)
8090 {
8091 	struct i40e_cloud_filter *filter = NULL;
8092 	struct hlist_node *node2;
8093 
8094 	hlist_for_each_entry_safe(filter, node2,
8095 				  &vsi->back->cloud_filter_list, cloud_node)
8096 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8097 			return filter;
8098 	return NULL;
8099 }
8100 
8101 /**
8102  * i40e_delete_clsflower - Remove tc flower filters
8103  * @vsi: Pointer to VSI
8104  * @cls_flower: Pointer to struct flow_cls_offload
8105  *
8106  **/
8107 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8108 				 struct flow_cls_offload *cls_flower)
8109 {
8110 	struct i40e_cloud_filter *filter = NULL;
8111 	struct i40e_pf *pf = vsi->back;
8112 	int err = 0;
8113 
8114 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8115 
8116 	if (!filter)
8117 		return -EINVAL;
8118 
8119 	hash_del(&filter->cloud_node);
8120 
8121 	if (filter->dst_port)
8122 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8123 	else
8124 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8125 
8126 	kfree(filter);
8127 	if (err) {
8128 		dev_err(&pf->pdev->dev,
8129 			"Failed to delete cloud filter, err %s\n",
8130 			i40e_stat_str(&pf->hw, err));
8131 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8132 	}
8133 
8134 	pf->num_cloud_filters--;
8135 	if (!pf->num_cloud_filters)
8136 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8137 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8138 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8139 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8140 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8141 		}
8142 	return 0;
8143 }
8144 
8145 /**
8146  * i40e_setup_tc_cls_flower - flower classifier offloads
8147  * @netdev: net device to configure
8148  * @type_data: offload data
8149  **/
8150 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8151 				    struct flow_cls_offload *cls_flower)
8152 {
8153 	struct i40e_vsi *vsi = np->vsi;
8154 
8155 	switch (cls_flower->command) {
8156 	case FLOW_CLS_REPLACE:
8157 		return i40e_configure_clsflower(vsi, cls_flower);
8158 	case FLOW_CLS_DESTROY:
8159 		return i40e_delete_clsflower(vsi, cls_flower);
8160 	case FLOW_CLS_STATS:
8161 		return -EOPNOTSUPP;
8162 	default:
8163 		return -EOPNOTSUPP;
8164 	}
8165 }
8166 
8167 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8168 				  void *cb_priv)
8169 {
8170 	struct i40e_netdev_priv *np = cb_priv;
8171 
8172 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8173 		return -EOPNOTSUPP;
8174 
8175 	switch (type) {
8176 	case TC_SETUP_CLSFLOWER:
8177 		return i40e_setup_tc_cls_flower(np, type_data);
8178 
8179 	default:
8180 		return -EOPNOTSUPP;
8181 	}
8182 }
8183 
8184 static LIST_HEAD(i40e_block_cb_list);
8185 
8186 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8187 			   void *type_data)
8188 {
8189 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8190 
8191 	switch (type) {
8192 	case TC_SETUP_QDISC_MQPRIO:
8193 		return i40e_setup_tc(netdev, type_data);
8194 	case TC_SETUP_BLOCK:
8195 		return flow_block_cb_setup_simple(type_data,
8196 						  &i40e_block_cb_list,
8197 						  i40e_setup_tc_block_cb,
8198 						  np, np, true);
8199 	default:
8200 		return -EOPNOTSUPP;
8201 	}
8202 }
8203 
8204 /**
8205  * i40e_open - Called when a network interface is made active
8206  * @netdev: network interface device structure
8207  *
8208  * The open entry point is called when a network interface is made
8209  * active by the system (IFF_UP).  At this point all resources needed
8210  * for transmit and receive operations are allocated, the interrupt
8211  * handler is registered with the OS, the netdev watchdog subtask is
8212  * enabled, and the stack is notified that the interface is ready.
8213  *
8214  * Returns 0 on success, negative value on failure
8215  **/
8216 int i40e_open(struct net_device *netdev)
8217 {
8218 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8219 	struct i40e_vsi *vsi = np->vsi;
8220 	struct i40e_pf *pf = vsi->back;
8221 	int err;
8222 
8223 	/* disallow open during test or if eeprom is broken */
8224 	if (test_bit(__I40E_TESTING, pf->state) ||
8225 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8226 		return -EBUSY;
8227 
8228 	netif_carrier_off(netdev);
8229 
8230 	if (i40e_force_link_state(pf, true))
8231 		return -EAGAIN;
8232 
8233 	err = i40e_vsi_open(vsi);
8234 	if (err)
8235 		return err;
8236 
8237 	/* configure global TSO hardware offload settings */
8238 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8239 						       TCP_FLAG_FIN) >> 16);
8240 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8241 						       TCP_FLAG_FIN |
8242 						       TCP_FLAG_CWR) >> 16);
8243 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8244 
8245 	udp_tunnel_get_rx_info(netdev);
8246 
8247 	return 0;
8248 }
8249 
8250 /**
8251  * i40e_vsi_open -
8252  * @vsi: the VSI to open
8253  *
8254  * Finish initialization of the VSI.
8255  *
8256  * Returns 0 on success, negative value on failure
8257  *
8258  * Note: expects to be called while under rtnl_lock()
8259  **/
8260 int i40e_vsi_open(struct i40e_vsi *vsi)
8261 {
8262 	struct i40e_pf *pf = vsi->back;
8263 	char int_name[I40E_INT_NAME_STR_LEN];
8264 	int err;
8265 
8266 	/* allocate descriptors */
8267 	err = i40e_vsi_setup_tx_resources(vsi);
8268 	if (err)
8269 		goto err_setup_tx;
8270 	err = i40e_vsi_setup_rx_resources(vsi);
8271 	if (err)
8272 		goto err_setup_rx;
8273 
8274 	err = i40e_vsi_configure(vsi);
8275 	if (err)
8276 		goto err_setup_rx;
8277 
8278 	if (vsi->netdev) {
8279 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
8280 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
8281 		err = i40e_vsi_request_irq(vsi, int_name);
8282 		if (err)
8283 			goto err_setup_rx;
8284 
8285 		/* Notify the stack of the actual queue counts. */
8286 		err = netif_set_real_num_tx_queues(vsi->netdev,
8287 						   vsi->num_queue_pairs);
8288 		if (err)
8289 			goto err_set_queues;
8290 
8291 		err = netif_set_real_num_rx_queues(vsi->netdev,
8292 						   vsi->num_queue_pairs);
8293 		if (err)
8294 			goto err_set_queues;
8295 
8296 	} else if (vsi->type == I40E_VSI_FDIR) {
8297 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
8298 			 dev_driver_string(&pf->pdev->dev),
8299 			 dev_name(&pf->pdev->dev));
8300 		err = i40e_vsi_request_irq(vsi, int_name);
8301 
8302 	} else {
8303 		err = -EINVAL;
8304 		goto err_setup_rx;
8305 	}
8306 
8307 	err = i40e_up_complete(vsi);
8308 	if (err)
8309 		goto err_up_complete;
8310 
8311 	return 0;
8312 
8313 err_up_complete:
8314 	i40e_down(vsi);
8315 err_set_queues:
8316 	i40e_vsi_free_irq(vsi);
8317 err_setup_rx:
8318 	i40e_vsi_free_rx_resources(vsi);
8319 err_setup_tx:
8320 	i40e_vsi_free_tx_resources(vsi);
8321 	if (vsi == pf->vsi[pf->lan_vsi])
8322 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
8323 
8324 	return err;
8325 }
8326 
8327 /**
8328  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
8329  * @pf: Pointer to PF
8330  *
8331  * This function destroys the hlist where all the Flow Director
8332  * filters were saved.
8333  **/
8334 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
8335 {
8336 	struct i40e_fdir_filter *filter;
8337 	struct i40e_flex_pit *pit_entry, *tmp;
8338 	struct hlist_node *node2;
8339 
8340 	hlist_for_each_entry_safe(filter, node2,
8341 				  &pf->fdir_filter_list, fdir_node) {
8342 		hlist_del(&filter->fdir_node);
8343 		kfree(filter);
8344 	}
8345 
8346 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
8347 		list_del(&pit_entry->list);
8348 		kfree(pit_entry);
8349 	}
8350 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
8351 
8352 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
8353 		list_del(&pit_entry->list);
8354 		kfree(pit_entry);
8355 	}
8356 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
8357 
8358 	pf->fdir_pf_active_filters = 0;
8359 	pf->fd_tcp4_filter_cnt = 0;
8360 	pf->fd_udp4_filter_cnt = 0;
8361 	pf->fd_sctp4_filter_cnt = 0;
8362 	pf->fd_ip4_filter_cnt = 0;
8363 
8364 	/* Reprogram the default input set for TCP/IPv4 */
8365 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8366 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8367 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8368 
8369 	/* Reprogram the default input set for UDP/IPv4 */
8370 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
8371 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8372 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8373 
8374 	/* Reprogram the default input set for SCTP/IPv4 */
8375 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
8376 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8377 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8378 
8379 	/* Reprogram the default input set for Other/IPv4 */
8380 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
8381 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8382 
8383 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
8384 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8385 }
8386 
8387 /**
8388  * i40e_cloud_filter_exit - Cleans up the cloud filters
8389  * @pf: Pointer to PF
8390  *
8391  * This function destroys the hlist where all the cloud filters
8392  * were saved.
8393  **/
8394 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
8395 {
8396 	struct i40e_cloud_filter *cfilter;
8397 	struct hlist_node *node;
8398 
8399 	hlist_for_each_entry_safe(cfilter, node,
8400 				  &pf->cloud_filter_list, cloud_node) {
8401 		hlist_del(&cfilter->cloud_node);
8402 		kfree(cfilter);
8403 	}
8404 	pf->num_cloud_filters = 0;
8405 
8406 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8407 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8408 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8409 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8410 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8411 	}
8412 }
8413 
8414 /**
8415  * i40e_close - Disables a network interface
8416  * @netdev: network interface device structure
8417  *
8418  * The close entry point is called when an interface is de-activated
8419  * by the OS.  The hardware is still under the driver's control, but
8420  * this netdev interface is disabled.
8421  *
8422  * Returns 0, this is not allowed to fail
8423  **/
8424 int i40e_close(struct net_device *netdev)
8425 {
8426 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8427 	struct i40e_vsi *vsi = np->vsi;
8428 
8429 	i40e_vsi_close(vsi);
8430 
8431 	return 0;
8432 }
8433 
8434 /**
8435  * i40e_do_reset - Start a PF or Core Reset sequence
8436  * @pf: board private structure
8437  * @reset_flags: which reset is requested
8438  * @lock_acquired: indicates whether or not the lock has been acquired
8439  * before this function was called.
8440  *
8441  * The essential difference in resets is that the PF Reset
8442  * doesn't clear the packet buffers, doesn't reset the PE
8443  * firmware, and doesn't bother the other PFs on the chip.
8444  **/
8445 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
8446 {
8447 	u32 val;
8448 
8449 	WARN_ON(in_interrupt());
8450 
8451 
8452 	/* do the biggest reset indicated */
8453 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
8454 
8455 		/* Request a Global Reset
8456 		 *
8457 		 * This will start the chip's countdown to the actual full
8458 		 * chip reset event, and a warning interrupt to be sent
8459 		 * to all PFs, including the requestor.  Our handler
8460 		 * for the warning interrupt will deal with the shutdown
8461 		 * and recovery of the switch setup.
8462 		 */
8463 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
8464 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8465 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
8466 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
8467 
8468 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
8469 
8470 		/* Request a Core Reset
8471 		 *
8472 		 * Same as Global Reset, except does *not* include the MAC/PHY
8473 		 */
8474 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
8475 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8476 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
8477 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
8478 		i40e_flush(&pf->hw);
8479 
8480 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
8481 
8482 		/* Request a PF Reset
8483 		 *
8484 		 * Resets only the PF-specific registers
8485 		 *
8486 		 * This goes directly to the tear-down and rebuild of
8487 		 * the switch, since we need to do all the recovery as
8488 		 * for the Core Reset.
8489 		 */
8490 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
8491 		i40e_handle_reset_warning(pf, lock_acquired);
8492 
8493 		dev_info(&pf->pdev->dev,
8494 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
8495 			 "FW LLDP is disabled\n" :
8496 			 "FW LLDP is enabled\n");
8497 
8498 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
8499 		int v;
8500 
8501 		/* Find the VSI(s) that requested a re-init */
8502 		dev_info(&pf->pdev->dev,
8503 			 "VSI reinit requested\n");
8504 		for (v = 0; v < pf->num_alloc_vsi; v++) {
8505 			struct i40e_vsi *vsi = pf->vsi[v];
8506 
8507 			if (vsi != NULL &&
8508 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
8509 					       vsi->state))
8510 				i40e_vsi_reinit_locked(pf->vsi[v]);
8511 		}
8512 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
8513 		int v;
8514 
8515 		/* Find the VSI(s) that needs to be brought down */
8516 		dev_info(&pf->pdev->dev, "VSI down requested\n");
8517 		for (v = 0; v < pf->num_alloc_vsi; v++) {
8518 			struct i40e_vsi *vsi = pf->vsi[v];
8519 
8520 			if (vsi != NULL &&
8521 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
8522 					       vsi->state)) {
8523 				set_bit(__I40E_VSI_DOWN, vsi->state);
8524 				i40e_down(vsi);
8525 			}
8526 		}
8527 	} else {
8528 		dev_info(&pf->pdev->dev,
8529 			 "bad reset request 0x%08x\n", reset_flags);
8530 	}
8531 }
8532 
8533 #ifdef CONFIG_I40E_DCB
8534 /**
8535  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
8536  * @pf: board private structure
8537  * @old_cfg: current DCB config
8538  * @new_cfg: new DCB config
8539  **/
8540 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
8541 			    struct i40e_dcbx_config *old_cfg,
8542 			    struct i40e_dcbx_config *new_cfg)
8543 {
8544 	bool need_reconfig = false;
8545 
8546 	/* Check if ETS configuration has changed */
8547 	if (memcmp(&new_cfg->etscfg,
8548 		   &old_cfg->etscfg,
8549 		   sizeof(new_cfg->etscfg))) {
8550 		/* If Priority Table has changed reconfig is needed */
8551 		if (memcmp(&new_cfg->etscfg.prioritytable,
8552 			   &old_cfg->etscfg.prioritytable,
8553 			   sizeof(new_cfg->etscfg.prioritytable))) {
8554 			need_reconfig = true;
8555 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
8556 		}
8557 
8558 		if (memcmp(&new_cfg->etscfg.tcbwtable,
8559 			   &old_cfg->etscfg.tcbwtable,
8560 			   sizeof(new_cfg->etscfg.tcbwtable)))
8561 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
8562 
8563 		if (memcmp(&new_cfg->etscfg.tsatable,
8564 			   &old_cfg->etscfg.tsatable,
8565 			   sizeof(new_cfg->etscfg.tsatable)))
8566 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
8567 	}
8568 
8569 	/* Check if PFC configuration has changed */
8570 	if (memcmp(&new_cfg->pfc,
8571 		   &old_cfg->pfc,
8572 		   sizeof(new_cfg->pfc))) {
8573 		need_reconfig = true;
8574 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
8575 	}
8576 
8577 	/* Check if APP Table has changed */
8578 	if (memcmp(&new_cfg->app,
8579 		   &old_cfg->app,
8580 		   sizeof(new_cfg->app))) {
8581 		need_reconfig = true;
8582 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
8583 	}
8584 
8585 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
8586 	return need_reconfig;
8587 }
8588 
8589 /**
8590  * i40e_handle_lldp_event - Handle LLDP Change MIB event
8591  * @pf: board private structure
8592  * @e: event info posted on ARQ
8593  **/
8594 static int i40e_handle_lldp_event(struct i40e_pf *pf,
8595 				  struct i40e_arq_event_info *e)
8596 {
8597 	struct i40e_aqc_lldp_get_mib *mib =
8598 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
8599 	struct i40e_hw *hw = &pf->hw;
8600 	struct i40e_dcbx_config tmp_dcbx_cfg;
8601 	bool need_reconfig = false;
8602 	int ret = 0;
8603 	u8 type;
8604 
8605 	/* Not DCB capable or capability disabled */
8606 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
8607 		return ret;
8608 
8609 	/* Ignore if event is not for Nearest Bridge */
8610 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
8611 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
8612 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
8613 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
8614 		return ret;
8615 
8616 	/* Check MIB Type and return if event for Remote MIB update */
8617 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
8618 	dev_dbg(&pf->pdev->dev,
8619 		"LLDP event mib type %s\n", type ? "remote" : "local");
8620 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
8621 		/* Update the remote cached instance and return */
8622 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
8623 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
8624 				&hw->remote_dcbx_config);
8625 		goto exit;
8626 	}
8627 
8628 	/* Store the old configuration */
8629 	tmp_dcbx_cfg = hw->local_dcbx_config;
8630 
8631 	/* Reset the old DCBx configuration data */
8632 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
8633 	/* Get updated DCBX data from firmware */
8634 	ret = i40e_get_dcb_config(&pf->hw);
8635 	if (ret) {
8636 		dev_info(&pf->pdev->dev,
8637 			 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
8638 			 i40e_stat_str(&pf->hw, ret),
8639 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8640 		goto exit;
8641 	}
8642 
8643 	/* No change detected in DCBX configs */
8644 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
8645 		    sizeof(tmp_dcbx_cfg))) {
8646 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
8647 		goto exit;
8648 	}
8649 
8650 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
8651 					       &hw->local_dcbx_config);
8652 
8653 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
8654 
8655 	if (!need_reconfig)
8656 		goto exit;
8657 
8658 	/* Enable DCB tagging only when more than one TC */
8659 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
8660 		pf->flags |= I40E_FLAG_DCB_ENABLED;
8661 	else
8662 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8663 
8664 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
8665 	/* Reconfiguration needed quiesce all VSIs */
8666 	i40e_pf_quiesce_all_vsi(pf);
8667 
8668 	/* Changes in configuration update VEB/VSI */
8669 	i40e_dcb_reconfigure(pf);
8670 
8671 	ret = i40e_resume_port_tx(pf);
8672 
8673 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
8674 	/* In case of error no point in resuming VSIs */
8675 	if (ret)
8676 		goto exit;
8677 
8678 	/* Wait for the PF's queues to be disabled */
8679 	ret = i40e_pf_wait_queues_disabled(pf);
8680 	if (ret) {
8681 		/* Schedule PF reset to recover */
8682 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
8683 		i40e_service_event_schedule(pf);
8684 	} else {
8685 		i40e_pf_unquiesce_all_vsi(pf);
8686 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
8687 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
8688 	}
8689 
8690 exit:
8691 	return ret;
8692 }
8693 #endif /* CONFIG_I40E_DCB */
8694 
8695 /**
8696  * i40e_do_reset_safe - Protected reset path for userland calls.
8697  * @pf: board private structure
8698  * @reset_flags: which reset is requested
8699  *
8700  **/
8701 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
8702 {
8703 	rtnl_lock();
8704 	i40e_do_reset(pf, reset_flags, true);
8705 	rtnl_unlock();
8706 }
8707 
8708 /**
8709  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
8710  * @pf: board private structure
8711  * @e: event info posted on ARQ
8712  *
8713  * Handler for LAN Queue Overflow Event generated by the firmware for PF
8714  * and VF queues
8715  **/
8716 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
8717 					   struct i40e_arq_event_info *e)
8718 {
8719 	struct i40e_aqc_lan_overflow *data =
8720 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
8721 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
8722 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
8723 	struct i40e_hw *hw = &pf->hw;
8724 	struct i40e_vf *vf;
8725 	u16 vf_id;
8726 
8727 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
8728 		queue, qtx_ctl);
8729 
8730 	/* Queue belongs to VF, find the VF and issue VF reset */
8731 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
8732 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
8733 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
8734 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
8735 		vf_id -= hw->func_caps.vf_base_id;
8736 		vf = &pf->vf[vf_id];
8737 		i40e_vc_notify_vf_reset(vf);
8738 		/* Allow VF to process pending reset notification */
8739 		msleep(20);
8740 		i40e_reset_vf(vf, false);
8741 	}
8742 }
8743 
8744 /**
8745  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
8746  * @pf: board private structure
8747  **/
8748 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
8749 {
8750 	u32 val, fcnt_prog;
8751 
8752 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8753 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
8754 	return fcnt_prog;
8755 }
8756 
8757 /**
8758  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
8759  * @pf: board private structure
8760  **/
8761 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
8762 {
8763 	u32 val, fcnt_prog;
8764 
8765 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8766 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
8767 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
8768 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
8769 	return fcnt_prog;
8770 }
8771 
8772 /**
8773  * i40e_get_global_fd_count - Get total FD filters programmed on device
8774  * @pf: board private structure
8775  **/
8776 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
8777 {
8778 	u32 val, fcnt_prog;
8779 
8780 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
8781 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
8782 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
8783 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
8784 	return fcnt_prog;
8785 }
8786 
8787 /**
8788  * i40e_reenable_fdir_sb - Restore FDir SB capability
8789  * @pf: board private structure
8790  **/
8791 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
8792 {
8793 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
8794 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8795 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
8796 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
8797 }
8798 
8799 /**
8800  * i40e_reenable_fdir_atr - Restore FDir ATR capability
8801  * @pf: board private structure
8802  **/
8803 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
8804 {
8805 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
8806 		/* ATR uses the same filtering logic as SB rules. It only
8807 		 * functions properly if the input set mask is at the default
8808 		 * settings. It is safe to restore the default input set
8809 		 * because there are no active TCPv4 filter rules.
8810 		 */
8811 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8812 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8813 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8814 
8815 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8816 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
8817 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
8818 	}
8819 }
8820 
8821 /**
8822  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
8823  * @pf: board private structure
8824  * @filter: FDir filter to remove
8825  */
8826 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
8827 				       struct i40e_fdir_filter *filter)
8828 {
8829 	/* Update counters */
8830 	pf->fdir_pf_active_filters--;
8831 	pf->fd_inv = 0;
8832 
8833 	switch (filter->flow_type) {
8834 	case TCP_V4_FLOW:
8835 		pf->fd_tcp4_filter_cnt--;
8836 		break;
8837 	case UDP_V4_FLOW:
8838 		pf->fd_udp4_filter_cnt--;
8839 		break;
8840 	case SCTP_V4_FLOW:
8841 		pf->fd_sctp4_filter_cnt--;
8842 		break;
8843 	case IP_USER_FLOW:
8844 		switch (filter->ip4_proto) {
8845 		case IPPROTO_TCP:
8846 			pf->fd_tcp4_filter_cnt--;
8847 			break;
8848 		case IPPROTO_UDP:
8849 			pf->fd_udp4_filter_cnt--;
8850 			break;
8851 		case IPPROTO_SCTP:
8852 			pf->fd_sctp4_filter_cnt--;
8853 			break;
8854 		case IPPROTO_IP:
8855 			pf->fd_ip4_filter_cnt--;
8856 			break;
8857 		}
8858 		break;
8859 	}
8860 
8861 	/* Remove the filter from the list and free memory */
8862 	hlist_del(&filter->fdir_node);
8863 	kfree(filter);
8864 }
8865 
8866 /**
8867  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
8868  * @pf: board private structure
8869  **/
8870 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
8871 {
8872 	struct i40e_fdir_filter *filter;
8873 	u32 fcnt_prog, fcnt_avail;
8874 	struct hlist_node *node;
8875 
8876 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
8877 		return;
8878 
8879 	/* Check if we have enough room to re-enable FDir SB capability. */
8880 	fcnt_prog = i40e_get_global_fd_count(pf);
8881 	fcnt_avail = pf->fdir_pf_filter_count;
8882 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
8883 	    (pf->fd_add_err == 0) ||
8884 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
8885 		i40e_reenable_fdir_sb(pf);
8886 
8887 	/* We should wait for even more space before re-enabling ATR.
8888 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
8889 	 * rules active.
8890 	 */
8891 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
8892 	    (pf->fd_tcp4_filter_cnt == 0))
8893 		i40e_reenable_fdir_atr(pf);
8894 
8895 	/* if hw had a problem adding a filter, delete it */
8896 	if (pf->fd_inv > 0) {
8897 		hlist_for_each_entry_safe(filter, node,
8898 					  &pf->fdir_filter_list, fdir_node)
8899 			if (filter->fd_id == pf->fd_inv)
8900 				i40e_delete_invalid_filter(pf, filter);
8901 	}
8902 }
8903 
8904 #define I40E_MIN_FD_FLUSH_INTERVAL 10
8905 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
8906 /**
8907  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
8908  * @pf: board private structure
8909  **/
8910 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
8911 {
8912 	unsigned long min_flush_time;
8913 	int flush_wait_retry = 50;
8914 	bool disable_atr = false;
8915 	int fd_room;
8916 	int reg;
8917 
8918 	if (!time_after(jiffies, pf->fd_flush_timestamp +
8919 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
8920 		return;
8921 
8922 	/* If the flush is happening too quick and we have mostly SB rules we
8923 	 * should not re-enable ATR for some time.
8924 	 */
8925 	min_flush_time = pf->fd_flush_timestamp +
8926 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
8927 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
8928 
8929 	if (!(time_after(jiffies, min_flush_time)) &&
8930 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
8931 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
8932 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
8933 		disable_atr = true;
8934 	}
8935 
8936 	pf->fd_flush_timestamp = jiffies;
8937 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
8938 	/* flush all filters */
8939 	wr32(&pf->hw, I40E_PFQF_CTL_1,
8940 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
8941 	i40e_flush(&pf->hw);
8942 	pf->fd_flush_cnt++;
8943 	pf->fd_add_err = 0;
8944 	do {
8945 		/* Check FD flush status every 5-6msec */
8946 		usleep_range(5000, 6000);
8947 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
8948 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
8949 			break;
8950 	} while (flush_wait_retry--);
8951 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
8952 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
8953 	} else {
8954 		/* replay sideband filters */
8955 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
8956 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
8957 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
8958 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
8959 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
8960 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
8961 	}
8962 }
8963 
8964 /**
8965  * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
8966  * @pf: board private structure
8967  **/
8968 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
8969 {
8970 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
8971 }
8972 
8973 /* We can see up to 256 filter programming desc in transit if the filters are
8974  * being applied really fast; before we see the first
8975  * filter miss error on Rx queue 0. Accumulating enough error messages before
8976  * reacting will make sure we don't cause flush too often.
8977  */
8978 #define I40E_MAX_FD_PROGRAM_ERROR 256
8979 
8980 /**
8981  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
8982  * @pf: board private structure
8983  **/
8984 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
8985 {
8986 
8987 	/* if interface is down do nothing */
8988 	if (test_bit(__I40E_DOWN, pf->state))
8989 		return;
8990 
8991 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
8992 		i40e_fdir_flush_and_replay(pf);
8993 
8994 	i40e_fdir_check_and_reenable(pf);
8995 
8996 }
8997 
8998 /**
8999  * i40e_vsi_link_event - notify VSI of a link event
9000  * @vsi: vsi to be notified
9001  * @link_up: link up or down
9002  **/
9003 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9004 {
9005 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9006 		return;
9007 
9008 	switch (vsi->type) {
9009 	case I40E_VSI_MAIN:
9010 		if (!vsi->netdev || !vsi->netdev_registered)
9011 			break;
9012 
9013 		if (link_up) {
9014 			netif_carrier_on(vsi->netdev);
9015 			netif_tx_wake_all_queues(vsi->netdev);
9016 		} else {
9017 			netif_carrier_off(vsi->netdev);
9018 			netif_tx_stop_all_queues(vsi->netdev);
9019 		}
9020 		break;
9021 
9022 	case I40E_VSI_SRIOV:
9023 	case I40E_VSI_VMDQ2:
9024 	case I40E_VSI_CTRL:
9025 	case I40E_VSI_IWARP:
9026 	case I40E_VSI_MIRROR:
9027 	default:
9028 		/* there is no notification for other VSIs */
9029 		break;
9030 	}
9031 }
9032 
9033 /**
9034  * i40e_veb_link_event - notify elements on the veb of a link event
9035  * @veb: veb to be notified
9036  * @link_up: link up or down
9037  **/
9038 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9039 {
9040 	struct i40e_pf *pf;
9041 	int i;
9042 
9043 	if (!veb || !veb->pf)
9044 		return;
9045 	pf = veb->pf;
9046 
9047 	/* depth first... */
9048 	for (i = 0; i < I40E_MAX_VEB; i++)
9049 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9050 			i40e_veb_link_event(pf->veb[i], link_up);
9051 
9052 	/* ... now the local VSIs */
9053 	for (i = 0; i < pf->num_alloc_vsi; i++)
9054 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9055 			i40e_vsi_link_event(pf->vsi[i], link_up);
9056 }
9057 
9058 /**
9059  * i40e_link_event - Update netif_carrier status
9060  * @pf: board private structure
9061  **/
9062 static void i40e_link_event(struct i40e_pf *pf)
9063 {
9064 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9065 	u8 new_link_speed, old_link_speed;
9066 	i40e_status status;
9067 	bool new_link, old_link;
9068 
9069 	/* set this to force the get_link_status call to refresh state */
9070 	pf->hw.phy.get_link_info = true;
9071 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9072 	status = i40e_get_link_status(&pf->hw, &new_link);
9073 
9074 	/* On success, disable temp link polling */
9075 	if (status == I40E_SUCCESS) {
9076 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9077 	} else {
9078 		/* Enable link polling temporarily until i40e_get_link_status
9079 		 * returns I40E_SUCCESS
9080 		 */
9081 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9082 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9083 			status);
9084 		return;
9085 	}
9086 
9087 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9088 	new_link_speed = pf->hw.phy.link_info.link_speed;
9089 
9090 	if (new_link == old_link &&
9091 	    new_link_speed == old_link_speed &&
9092 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9093 	     new_link == netif_carrier_ok(vsi->netdev)))
9094 		return;
9095 
9096 	i40e_print_link_message(vsi, new_link);
9097 
9098 	/* Notify the base of the switch tree connected to
9099 	 * the link.  Floating VEBs are not notified.
9100 	 */
9101 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9102 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9103 	else
9104 		i40e_vsi_link_event(vsi, new_link);
9105 
9106 	if (pf->vf)
9107 		i40e_vc_notify_link_state(pf);
9108 
9109 	if (pf->flags & I40E_FLAG_PTP)
9110 		i40e_ptp_set_increment(pf);
9111 }
9112 
9113 /**
9114  * i40e_watchdog_subtask - periodic checks not using event driven response
9115  * @pf: board private structure
9116  **/
9117 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9118 {
9119 	int i;
9120 
9121 	/* if interface is down do nothing */
9122 	if (test_bit(__I40E_DOWN, pf->state) ||
9123 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9124 		return;
9125 
9126 	/* make sure we don't do these things too often */
9127 	if (time_before(jiffies, (pf->service_timer_previous +
9128 				  pf->service_timer_period)))
9129 		return;
9130 	pf->service_timer_previous = jiffies;
9131 
9132 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9133 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9134 		i40e_link_event(pf);
9135 
9136 	/* Update the stats for active netdevs so the network stack
9137 	 * can look at updated numbers whenever it cares to
9138 	 */
9139 	for (i = 0; i < pf->num_alloc_vsi; i++)
9140 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9141 			i40e_update_stats(pf->vsi[i]);
9142 
9143 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9144 		/* Update the stats for the active switching components */
9145 		for (i = 0; i < I40E_MAX_VEB; i++)
9146 			if (pf->veb[i])
9147 				i40e_update_veb_stats(pf->veb[i]);
9148 	}
9149 
9150 	i40e_ptp_rx_hang(pf);
9151 	i40e_ptp_tx_hang(pf);
9152 }
9153 
9154 /**
9155  * i40e_reset_subtask - Set up for resetting the device and driver
9156  * @pf: board private structure
9157  **/
9158 static void i40e_reset_subtask(struct i40e_pf *pf)
9159 {
9160 	u32 reset_flags = 0;
9161 
9162 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
9163 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
9164 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
9165 	}
9166 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
9167 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
9168 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9169 	}
9170 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
9171 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
9172 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
9173 	}
9174 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
9175 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
9176 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
9177 	}
9178 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
9179 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
9180 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
9181 	}
9182 
9183 	/* If there's a recovery already waiting, it takes
9184 	 * precedence before starting a new reset sequence.
9185 	 */
9186 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
9187 		i40e_prep_for_reset(pf, false);
9188 		i40e_reset(pf);
9189 		i40e_rebuild(pf, false, false);
9190 	}
9191 
9192 	/* If we're already down or resetting, just bail */
9193 	if (reset_flags &&
9194 	    !test_bit(__I40E_DOWN, pf->state) &&
9195 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
9196 		i40e_do_reset(pf, reset_flags, false);
9197 	}
9198 }
9199 
9200 /**
9201  * i40e_handle_link_event - Handle link event
9202  * @pf: board private structure
9203  * @e: event info posted on ARQ
9204  **/
9205 static void i40e_handle_link_event(struct i40e_pf *pf,
9206 				   struct i40e_arq_event_info *e)
9207 {
9208 	struct i40e_aqc_get_link_status *status =
9209 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
9210 
9211 	/* Do a new status request to re-enable LSE reporting
9212 	 * and load new status information into the hw struct
9213 	 * This completely ignores any state information
9214 	 * in the ARQ event info, instead choosing to always
9215 	 * issue the AQ update link status command.
9216 	 */
9217 	i40e_link_event(pf);
9218 
9219 	/* Check if module meets thermal requirements */
9220 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
9221 		dev_err(&pf->pdev->dev,
9222 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
9223 		dev_err(&pf->pdev->dev,
9224 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9225 	} else {
9226 		/* check for unqualified module, if link is down, suppress
9227 		 * the message if link was forced to be down.
9228 		 */
9229 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
9230 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
9231 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
9232 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
9233 			dev_err(&pf->pdev->dev,
9234 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
9235 			dev_err(&pf->pdev->dev,
9236 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9237 		}
9238 	}
9239 }
9240 
9241 /**
9242  * i40e_clean_adminq_subtask - Clean the AdminQ rings
9243  * @pf: board private structure
9244  **/
9245 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
9246 {
9247 	struct i40e_arq_event_info event;
9248 	struct i40e_hw *hw = &pf->hw;
9249 	u16 pending, i = 0;
9250 	i40e_status ret;
9251 	u16 opcode;
9252 	u32 oldval;
9253 	u32 val;
9254 
9255 	/* Do not run clean AQ when PF reset fails */
9256 	if (test_bit(__I40E_RESET_FAILED, pf->state))
9257 		return;
9258 
9259 	/* check for error indications */
9260 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
9261 	oldval = val;
9262 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
9263 		if (hw->debug_mask & I40E_DEBUG_AQ)
9264 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
9265 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
9266 	}
9267 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
9268 		if (hw->debug_mask & I40E_DEBUG_AQ)
9269 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
9270 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
9271 		pf->arq_overflows++;
9272 	}
9273 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
9274 		if (hw->debug_mask & I40E_DEBUG_AQ)
9275 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
9276 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
9277 	}
9278 	if (oldval != val)
9279 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
9280 
9281 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
9282 	oldval = val;
9283 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
9284 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9285 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
9286 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
9287 	}
9288 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
9289 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9290 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
9291 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
9292 	}
9293 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
9294 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9295 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
9296 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
9297 	}
9298 	if (oldval != val)
9299 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
9300 
9301 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
9302 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
9303 	if (!event.msg_buf)
9304 		return;
9305 
9306 	do {
9307 		ret = i40e_clean_arq_element(hw, &event, &pending);
9308 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
9309 			break;
9310 		else if (ret) {
9311 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
9312 			break;
9313 		}
9314 
9315 		opcode = le16_to_cpu(event.desc.opcode);
9316 		switch (opcode) {
9317 
9318 		case i40e_aqc_opc_get_link_status:
9319 			i40e_handle_link_event(pf, &event);
9320 			break;
9321 		case i40e_aqc_opc_send_msg_to_pf:
9322 			ret = i40e_vc_process_vf_msg(pf,
9323 					le16_to_cpu(event.desc.retval),
9324 					le32_to_cpu(event.desc.cookie_high),
9325 					le32_to_cpu(event.desc.cookie_low),
9326 					event.msg_buf,
9327 					event.msg_len);
9328 			break;
9329 		case i40e_aqc_opc_lldp_update_mib:
9330 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
9331 #ifdef CONFIG_I40E_DCB
9332 			rtnl_lock();
9333 			ret = i40e_handle_lldp_event(pf, &event);
9334 			rtnl_unlock();
9335 #endif /* CONFIG_I40E_DCB */
9336 			break;
9337 		case i40e_aqc_opc_event_lan_overflow:
9338 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
9339 			i40e_handle_lan_overflow_event(pf, &event);
9340 			break;
9341 		case i40e_aqc_opc_send_msg_to_peer:
9342 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
9343 			break;
9344 		case i40e_aqc_opc_nvm_erase:
9345 		case i40e_aqc_opc_nvm_update:
9346 		case i40e_aqc_opc_oem_post_update:
9347 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
9348 				   "ARQ NVM operation 0x%04x completed\n",
9349 				   opcode);
9350 			break;
9351 		default:
9352 			dev_info(&pf->pdev->dev,
9353 				 "ARQ: Unknown event 0x%04x ignored\n",
9354 				 opcode);
9355 			break;
9356 		}
9357 	} while (i++ < pf->adminq_work_limit);
9358 
9359 	if (i < pf->adminq_work_limit)
9360 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
9361 
9362 	/* re-enable Admin queue interrupt cause */
9363 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
9364 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
9365 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
9366 	i40e_flush(hw);
9367 
9368 	kfree(event.msg_buf);
9369 }
9370 
9371 /**
9372  * i40e_verify_eeprom - make sure eeprom is good to use
9373  * @pf: board private structure
9374  **/
9375 static void i40e_verify_eeprom(struct i40e_pf *pf)
9376 {
9377 	int err;
9378 
9379 	err = i40e_diag_eeprom_test(&pf->hw);
9380 	if (err) {
9381 		/* retry in case of garbage read */
9382 		err = i40e_diag_eeprom_test(&pf->hw);
9383 		if (err) {
9384 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
9385 				 err);
9386 			set_bit(__I40E_BAD_EEPROM, pf->state);
9387 		}
9388 	}
9389 
9390 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
9391 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
9392 		clear_bit(__I40E_BAD_EEPROM, pf->state);
9393 	}
9394 }
9395 
9396 /**
9397  * i40e_enable_pf_switch_lb
9398  * @pf: pointer to the PF structure
9399  *
9400  * enable switch loop back or die - no point in a return value
9401  **/
9402 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
9403 {
9404 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9405 	struct i40e_vsi_context ctxt;
9406 	int ret;
9407 
9408 	ctxt.seid = pf->main_vsi_seid;
9409 	ctxt.pf_num = pf->hw.pf_id;
9410 	ctxt.vf_num = 0;
9411 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9412 	if (ret) {
9413 		dev_info(&pf->pdev->dev,
9414 			 "couldn't get PF vsi config, err %s aq_err %s\n",
9415 			 i40e_stat_str(&pf->hw, ret),
9416 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9417 		return;
9418 	}
9419 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9420 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9421 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9422 
9423 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
9424 	if (ret) {
9425 		dev_info(&pf->pdev->dev,
9426 			 "update vsi switch failed, err %s aq_err %s\n",
9427 			 i40e_stat_str(&pf->hw, ret),
9428 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9429 	}
9430 }
9431 
9432 /**
9433  * i40e_disable_pf_switch_lb
9434  * @pf: pointer to the PF structure
9435  *
9436  * disable switch loop back or die - no point in a return value
9437  **/
9438 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
9439 {
9440 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9441 	struct i40e_vsi_context ctxt;
9442 	int ret;
9443 
9444 	ctxt.seid = pf->main_vsi_seid;
9445 	ctxt.pf_num = pf->hw.pf_id;
9446 	ctxt.vf_num = 0;
9447 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9448 	if (ret) {
9449 		dev_info(&pf->pdev->dev,
9450 			 "couldn't get PF vsi config, err %s aq_err %s\n",
9451 			 i40e_stat_str(&pf->hw, ret),
9452 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9453 		return;
9454 	}
9455 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9456 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9457 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9458 
9459 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
9460 	if (ret) {
9461 		dev_info(&pf->pdev->dev,
9462 			 "update vsi switch failed, err %s aq_err %s\n",
9463 			 i40e_stat_str(&pf->hw, ret),
9464 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9465 	}
9466 }
9467 
9468 /**
9469  * i40e_config_bridge_mode - Configure the HW bridge mode
9470  * @veb: pointer to the bridge instance
9471  *
9472  * Configure the loop back mode for the LAN VSI that is downlink to the
9473  * specified HW bridge instance. It is expected this function is called
9474  * when a new HW bridge is instantiated.
9475  **/
9476 static void i40e_config_bridge_mode(struct i40e_veb *veb)
9477 {
9478 	struct i40e_pf *pf = veb->pf;
9479 
9480 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
9481 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
9482 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
9483 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
9484 		i40e_disable_pf_switch_lb(pf);
9485 	else
9486 		i40e_enable_pf_switch_lb(pf);
9487 }
9488 
9489 /**
9490  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
9491  * @veb: pointer to the VEB instance
9492  *
9493  * This is a recursive function that first builds the attached VSIs then
9494  * recurses in to build the next layer of VEB.  We track the connections
9495  * through our own index numbers because the seid's from the HW could
9496  * change across the reset.
9497  **/
9498 static int i40e_reconstitute_veb(struct i40e_veb *veb)
9499 {
9500 	struct i40e_vsi *ctl_vsi = NULL;
9501 	struct i40e_pf *pf = veb->pf;
9502 	int v, veb_idx;
9503 	int ret;
9504 
9505 	/* build VSI that owns this VEB, temporarily attached to base VEB */
9506 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
9507 		if (pf->vsi[v] &&
9508 		    pf->vsi[v]->veb_idx == veb->idx &&
9509 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
9510 			ctl_vsi = pf->vsi[v];
9511 			break;
9512 		}
9513 	}
9514 	if (!ctl_vsi) {
9515 		dev_info(&pf->pdev->dev,
9516 			 "missing owner VSI for veb_idx %d\n", veb->idx);
9517 		ret = -ENOENT;
9518 		goto end_reconstitute;
9519 	}
9520 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
9521 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
9522 	ret = i40e_add_vsi(ctl_vsi);
9523 	if (ret) {
9524 		dev_info(&pf->pdev->dev,
9525 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
9526 			 veb->idx, ret);
9527 		goto end_reconstitute;
9528 	}
9529 	i40e_vsi_reset_stats(ctl_vsi);
9530 
9531 	/* create the VEB in the switch and move the VSI onto the VEB */
9532 	ret = i40e_add_veb(veb, ctl_vsi);
9533 	if (ret)
9534 		goto end_reconstitute;
9535 
9536 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
9537 		veb->bridge_mode = BRIDGE_MODE_VEB;
9538 	else
9539 		veb->bridge_mode = BRIDGE_MODE_VEPA;
9540 	i40e_config_bridge_mode(veb);
9541 
9542 	/* create the remaining VSIs attached to this VEB */
9543 	for (v = 0; v < pf->num_alloc_vsi; v++) {
9544 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
9545 			continue;
9546 
9547 		if (pf->vsi[v]->veb_idx == veb->idx) {
9548 			struct i40e_vsi *vsi = pf->vsi[v];
9549 
9550 			vsi->uplink_seid = veb->seid;
9551 			ret = i40e_add_vsi(vsi);
9552 			if (ret) {
9553 				dev_info(&pf->pdev->dev,
9554 					 "rebuild of vsi_idx %d failed: %d\n",
9555 					 v, ret);
9556 				goto end_reconstitute;
9557 			}
9558 			i40e_vsi_reset_stats(vsi);
9559 		}
9560 	}
9561 
9562 	/* create any VEBs attached to this VEB - RECURSION */
9563 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
9564 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
9565 			pf->veb[veb_idx]->uplink_seid = veb->seid;
9566 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
9567 			if (ret)
9568 				break;
9569 		}
9570 	}
9571 
9572 end_reconstitute:
9573 	return ret;
9574 }
9575 
9576 /**
9577  * i40e_get_capabilities - get info about the HW
9578  * @pf: the PF struct
9579  **/
9580 static int i40e_get_capabilities(struct i40e_pf *pf,
9581 				 enum i40e_admin_queue_opc list_type)
9582 {
9583 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
9584 	u16 data_size;
9585 	int buf_len;
9586 	int err;
9587 
9588 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
9589 	do {
9590 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
9591 		if (!cap_buf)
9592 			return -ENOMEM;
9593 
9594 		/* this loads the data into the hw struct for us */
9595 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
9596 						    &data_size, list_type,
9597 						    NULL);
9598 		/* data loaded, buffer no longer needed */
9599 		kfree(cap_buf);
9600 
9601 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
9602 			/* retry with a larger buffer */
9603 			buf_len = data_size;
9604 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
9605 			dev_info(&pf->pdev->dev,
9606 				 "capability discovery failed, err %s aq_err %s\n",
9607 				 i40e_stat_str(&pf->hw, err),
9608 				 i40e_aq_str(&pf->hw,
9609 					     pf->hw.aq.asq_last_status));
9610 			return -ENODEV;
9611 		}
9612 	} while (err);
9613 
9614 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
9615 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
9616 			dev_info(&pf->pdev->dev,
9617 				 "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",
9618 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
9619 				 pf->hw.func_caps.num_msix_vectors,
9620 				 pf->hw.func_caps.num_msix_vectors_vf,
9621 				 pf->hw.func_caps.fd_filters_guaranteed,
9622 				 pf->hw.func_caps.fd_filters_best_effort,
9623 				 pf->hw.func_caps.num_tx_qp,
9624 				 pf->hw.func_caps.num_vsis);
9625 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
9626 			dev_info(&pf->pdev->dev,
9627 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
9628 				 pf->hw.dev_caps.switch_mode,
9629 				 pf->hw.dev_caps.valid_functions);
9630 			dev_info(&pf->pdev->dev,
9631 				 "SR-IOV=%d, num_vfs for all function=%u\n",
9632 				 pf->hw.dev_caps.sr_iov_1_1,
9633 				 pf->hw.dev_caps.num_vfs);
9634 			dev_info(&pf->pdev->dev,
9635 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
9636 				 pf->hw.dev_caps.num_vsis,
9637 				 pf->hw.dev_caps.num_rx_qp,
9638 				 pf->hw.dev_caps.num_tx_qp);
9639 		}
9640 	}
9641 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
9642 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
9643 		       + pf->hw.func_caps.num_vfs)
9644 		if (pf->hw.revision_id == 0 &&
9645 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
9646 			dev_info(&pf->pdev->dev,
9647 				 "got num_vsis %d, setting num_vsis to %d\n",
9648 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
9649 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
9650 		}
9651 	}
9652 	return 0;
9653 }
9654 
9655 static int i40e_vsi_clear(struct i40e_vsi *vsi);
9656 
9657 /**
9658  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
9659  * @pf: board private structure
9660  **/
9661 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
9662 {
9663 	struct i40e_vsi *vsi;
9664 
9665 	/* quick workaround for an NVM issue that leaves a critical register
9666 	 * uninitialized
9667 	 */
9668 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
9669 		static const u32 hkey[] = {
9670 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
9671 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
9672 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
9673 			0x95b3a76d};
9674 		int i;
9675 
9676 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
9677 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
9678 	}
9679 
9680 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
9681 		return;
9682 
9683 	/* find existing VSI and see if it needs configuring */
9684 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9685 
9686 	/* create a new VSI if none exists */
9687 	if (!vsi) {
9688 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
9689 				     pf->vsi[pf->lan_vsi]->seid, 0);
9690 		if (!vsi) {
9691 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
9692 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
9693 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
9694 			return;
9695 		}
9696 	}
9697 
9698 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
9699 }
9700 
9701 /**
9702  * i40e_fdir_teardown - release the Flow Director resources
9703  * @pf: board private structure
9704  **/
9705 static void i40e_fdir_teardown(struct i40e_pf *pf)
9706 {
9707 	struct i40e_vsi *vsi;
9708 
9709 	i40e_fdir_filter_exit(pf);
9710 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9711 	if (vsi)
9712 		i40e_vsi_release(vsi);
9713 }
9714 
9715 /**
9716  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
9717  * @vsi: PF main vsi
9718  * @seid: seid of main or channel VSIs
9719  *
9720  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
9721  * existed before reset
9722  **/
9723 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
9724 {
9725 	struct i40e_cloud_filter *cfilter;
9726 	struct i40e_pf *pf = vsi->back;
9727 	struct hlist_node *node;
9728 	i40e_status ret;
9729 
9730 	/* Add cloud filters back if they exist */
9731 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
9732 				  cloud_node) {
9733 		if (cfilter->seid != seid)
9734 			continue;
9735 
9736 		if (cfilter->dst_port)
9737 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
9738 								true);
9739 		else
9740 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
9741 
9742 		if (ret) {
9743 			dev_dbg(&pf->pdev->dev,
9744 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
9745 				i40e_stat_str(&pf->hw, ret),
9746 				i40e_aq_str(&pf->hw,
9747 					    pf->hw.aq.asq_last_status));
9748 			return ret;
9749 		}
9750 	}
9751 	return 0;
9752 }
9753 
9754 /**
9755  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
9756  * @vsi: PF main vsi
9757  *
9758  * Rebuilds channel VSIs if they existed before reset
9759  **/
9760 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
9761 {
9762 	struct i40e_channel *ch, *ch_tmp;
9763 	i40e_status ret;
9764 
9765 	if (list_empty(&vsi->ch_list))
9766 		return 0;
9767 
9768 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
9769 		if (!ch->initialized)
9770 			break;
9771 		/* Proceed with creation of channel (VMDq2) VSI */
9772 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
9773 		if (ret) {
9774 			dev_info(&vsi->back->pdev->dev,
9775 				 "failed to rebuild channels using uplink_seid %u\n",
9776 				 vsi->uplink_seid);
9777 			return ret;
9778 		}
9779 		/* Reconfigure TX queues using QTX_CTL register */
9780 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
9781 		if (ret) {
9782 			dev_info(&vsi->back->pdev->dev,
9783 				 "failed to configure TX rings for channel %u\n",
9784 				 ch->seid);
9785 			return ret;
9786 		}
9787 		/* update 'next_base_queue' */
9788 		vsi->next_base_queue = vsi->next_base_queue +
9789 							ch->num_queue_pairs;
9790 		if (ch->max_tx_rate) {
9791 			u64 credits = ch->max_tx_rate;
9792 
9793 			if (i40e_set_bw_limit(vsi, ch->seid,
9794 					      ch->max_tx_rate))
9795 				return -EINVAL;
9796 
9797 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
9798 			dev_dbg(&vsi->back->pdev->dev,
9799 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9800 				ch->max_tx_rate,
9801 				credits,
9802 				ch->seid);
9803 		}
9804 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
9805 		if (ret) {
9806 			dev_dbg(&vsi->back->pdev->dev,
9807 				"Failed to rebuild cloud filters for channel VSI %u\n",
9808 				ch->seid);
9809 			return ret;
9810 		}
9811 	}
9812 	return 0;
9813 }
9814 
9815 /**
9816  * i40e_prep_for_reset - prep for the core to reset
9817  * @pf: board private structure
9818  * @lock_acquired: indicates whether or not the lock has been acquired
9819  * before this function was called.
9820  *
9821  * Close up the VFs and other things in prep for PF Reset.
9822   **/
9823 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
9824 {
9825 	struct i40e_hw *hw = &pf->hw;
9826 	i40e_status ret = 0;
9827 	u32 v;
9828 
9829 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
9830 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
9831 		return;
9832 	if (i40e_check_asq_alive(&pf->hw))
9833 		i40e_vc_notify_reset(pf);
9834 
9835 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
9836 
9837 	/* quiesce the VSIs and their queues that are not already DOWN */
9838 	/* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
9839 	if (!lock_acquired)
9840 		rtnl_lock();
9841 	i40e_pf_quiesce_all_vsi(pf);
9842 	if (!lock_acquired)
9843 		rtnl_unlock();
9844 
9845 	for (v = 0; v < pf->num_alloc_vsi; v++) {
9846 		if (pf->vsi[v])
9847 			pf->vsi[v]->seid = 0;
9848 	}
9849 
9850 	i40e_shutdown_adminq(&pf->hw);
9851 
9852 	/* call shutdown HMC */
9853 	if (hw->hmc.hmc_obj) {
9854 		ret = i40e_shutdown_lan_hmc(hw);
9855 		if (ret)
9856 			dev_warn(&pf->pdev->dev,
9857 				 "shutdown_lan_hmc failed: %d\n", ret);
9858 	}
9859 
9860 	/* Save the current PTP time so that we can restore the time after the
9861 	 * reset completes.
9862 	 */
9863 	i40e_ptp_save_hw_time(pf);
9864 }
9865 
9866 /**
9867  * i40e_send_version - update firmware with driver version
9868  * @pf: PF struct
9869  */
9870 static void i40e_send_version(struct i40e_pf *pf)
9871 {
9872 	struct i40e_driver_version dv;
9873 
9874 	dv.major_version = DRV_VERSION_MAJOR;
9875 	dv.minor_version = DRV_VERSION_MINOR;
9876 	dv.build_version = DRV_VERSION_BUILD;
9877 	dv.subbuild_version = 0;
9878 	strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
9879 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
9880 }
9881 
9882 /**
9883  * i40e_get_oem_version - get OEM specific version information
9884  * @hw: pointer to the hardware structure
9885  **/
9886 static void i40e_get_oem_version(struct i40e_hw *hw)
9887 {
9888 	u16 block_offset = 0xffff;
9889 	u16 block_length = 0;
9890 	u16 capabilities = 0;
9891 	u16 gen_snap = 0;
9892 	u16 release = 0;
9893 
9894 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
9895 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
9896 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
9897 #define I40E_NVM_OEM_GEN_OFFSET			0x02
9898 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
9899 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
9900 #define I40E_NVM_OEM_LENGTH			3
9901 
9902 	/* Check if pointer to OEM version block is valid. */
9903 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
9904 	if (block_offset == 0xffff)
9905 		return;
9906 
9907 	/* Check if OEM version block has correct length. */
9908 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
9909 			   &block_length);
9910 	if (block_length < I40E_NVM_OEM_LENGTH)
9911 		return;
9912 
9913 	/* Check if OEM version format is as expected. */
9914 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
9915 			   &capabilities);
9916 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
9917 		return;
9918 
9919 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
9920 			   &gen_snap);
9921 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
9922 			   &release);
9923 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
9924 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
9925 }
9926 
9927 /**
9928  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
9929  * @pf: board private structure
9930  **/
9931 static int i40e_reset(struct i40e_pf *pf)
9932 {
9933 	struct i40e_hw *hw = &pf->hw;
9934 	i40e_status ret;
9935 
9936 	ret = i40e_pf_reset(hw);
9937 	if (ret) {
9938 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
9939 		set_bit(__I40E_RESET_FAILED, pf->state);
9940 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
9941 	} else {
9942 		pf->pfr_count++;
9943 	}
9944 	return ret;
9945 }
9946 
9947 /**
9948  * i40e_rebuild - rebuild using a saved config
9949  * @pf: board private structure
9950  * @reinit: if the Main VSI needs to re-initialized.
9951  * @lock_acquired: indicates whether or not the lock has been acquired
9952  * before this function was called.
9953  **/
9954 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
9955 {
9956 	int old_recovery_mode_bit = test_bit(__I40E_RECOVERY_MODE, pf->state);
9957 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9958 	struct i40e_hw *hw = &pf->hw;
9959 	u8 set_fc_aq_fail = 0;
9960 	i40e_status ret;
9961 	u32 val;
9962 	int v;
9963 
9964 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
9965 	    i40e_check_recovery_mode(pf)) {
9966 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
9967 	}
9968 
9969 	if (test_bit(__I40E_DOWN, pf->state) &&
9970 	    !test_bit(__I40E_RECOVERY_MODE, pf->state) &&
9971 	    !old_recovery_mode_bit)
9972 		goto clear_recovery;
9973 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
9974 
9975 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
9976 	ret = i40e_init_adminq(&pf->hw);
9977 	if (ret) {
9978 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, 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 		goto clear_recovery;
9982 	}
9983 	i40e_get_oem_version(&pf->hw);
9984 
9985 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
9986 	    ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) ||
9987 	     hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) {
9988 		/* The following delay is necessary for 4.33 firmware and older
9989 		 * to recover after EMP reset. 200 ms should suffice but we
9990 		 * put here 300 ms to be sure that FW is ready to operate
9991 		 * after reset.
9992 		 */
9993 		mdelay(300);
9994 	}
9995 
9996 	/* re-verify the eeprom if we just had an EMP reset */
9997 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
9998 		i40e_verify_eeprom(pf);
9999 
10000 	/* if we are going out of or into recovery mode we have to act
10001 	 * accordingly with regard to resources initialization
10002 	 * and deinitialization
10003 	 */
10004 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) ||
10005 	    old_recovery_mode_bit) {
10006 		if (i40e_get_capabilities(pf,
10007 					  i40e_aqc_opc_list_func_capabilities))
10008 			goto end_unlock;
10009 
10010 		if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10011 			/* we're staying in recovery mode so we'll reinitialize
10012 			 * misc vector here
10013 			 */
10014 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10015 				goto end_unlock;
10016 		} else {
10017 			if (!lock_acquired)
10018 				rtnl_lock();
10019 			/* we're going out of recovery mode so we'll free
10020 			 * the IRQ allocated specifically for recovery mode
10021 			 * and restore the interrupt scheme
10022 			 */
10023 			free_irq(pf->pdev->irq, pf);
10024 			i40e_clear_interrupt_scheme(pf);
10025 			if (i40e_restore_interrupt_scheme(pf))
10026 				goto end_unlock;
10027 		}
10028 
10029 		/* tell the firmware that we're starting */
10030 		i40e_send_version(pf);
10031 
10032 		/* bail out in case recovery mode was detected, as there is
10033 		 * no need for further configuration.
10034 		 */
10035 		goto end_unlock;
10036 	}
10037 
10038 	i40e_clear_pxe_mode(hw);
10039 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10040 	if (ret)
10041 		goto end_core_reset;
10042 
10043 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10044 				hw->func_caps.num_rx_qp, 0, 0);
10045 	if (ret) {
10046 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10047 		goto end_core_reset;
10048 	}
10049 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10050 	if (ret) {
10051 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10052 		goto end_core_reset;
10053 	}
10054 
10055 	/* Enable FW to write a default DCB config on link-up */
10056 	i40e_aq_set_dcb_parameters(hw, true, NULL);
10057 
10058 #ifdef CONFIG_I40E_DCB
10059 	ret = i40e_init_pf_dcb(pf);
10060 	if (ret) {
10061 		dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
10062 		pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10063 		/* Continue without DCB enabled */
10064 	}
10065 #endif /* CONFIG_I40E_DCB */
10066 	/* do basic switch setup */
10067 	if (!lock_acquired)
10068 		rtnl_lock();
10069 	ret = i40e_setup_pf_switch(pf, reinit);
10070 	if (ret)
10071 		goto end_unlock;
10072 
10073 	/* The driver only wants link up/down and module qualification
10074 	 * reports from firmware.  Note the negative logic.
10075 	 */
10076 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10077 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10078 					 I40E_AQ_EVENT_MEDIA_NA |
10079 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10080 	if (ret)
10081 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10082 			 i40e_stat_str(&pf->hw, ret),
10083 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10084 
10085 	/* make sure our flow control settings are restored */
10086 	ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
10087 	if (ret)
10088 		dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
10089 			i40e_stat_str(&pf->hw, ret),
10090 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10091 
10092 	/* Rebuild the VSIs and VEBs that existed before reset.
10093 	 * They are still in our local switch element arrays, so only
10094 	 * need to rebuild the switch model in the HW.
10095 	 *
10096 	 * If there were VEBs but the reconstitution failed, we'll try
10097 	 * try to recover minimal use by getting the basic PF VSI working.
10098 	 */
10099 	if (vsi->uplink_seid != pf->mac_seid) {
10100 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10101 		/* find the one VEB connected to the MAC, and find orphans */
10102 		for (v = 0; v < I40E_MAX_VEB; v++) {
10103 			if (!pf->veb[v])
10104 				continue;
10105 
10106 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10107 			    pf->veb[v]->uplink_seid == 0) {
10108 				ret = i40e_reconstitute_veb(pf->veb[v]);
10109 
10110 				if (!ret)
10111 					continue;
10112 
10113 				/* If Main VEB failed, we're in deep doodoo,
10114 				 * so give up rebuilding the switch and set up
10115 				 * for minimal rebuild of PF VSI.
10116 				 * If orphan failed, we'll report the error
10117 				 * but try to keep going.
10118 				 */
10119 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10120 					dev_info(&pf->pdev->dev,
10121 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10122 						 ret);
10123 					vsi->uplink_seid = pf->mac_seid;
10124 					break;
10125 				} else if (pf->veb[v]->uplink_seid == 0) {
10126 					dev_info(&pf->pdev->dev,
10127 						 "rebuild of orphan VEB failed: %d\n",
10128 						 ret);
10129 				}
10130 			}
10131 		}
10132 	}
10133 
10134 	if (vsi->uplink_seid == pf->mac_seid) {
10135 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10136 		/* no VEB, so rebuild only the Main VSI */
10137 		ret = i40e_add_vsi(vsi);
10138 		if (ret) {
10139 			dev_info(&pf->pdev->dev,
10140 				 "rebuild of Main VSI failed: %d\n", ret);
10141 			goto end_unlock;
10142 		}
10143 	}
10144 
10145 	if (vsi->mqprio_qopt.max_rate[0]) {
10146 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
10147 		u64 credits = 0;
10148 
10149 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
10150 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10151 		if (ret)
10152 			goto end_unlock;
10153 
10154 		credits = max_tx_rate;
10155 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10156 		dev_dbg(&vsi->back->pdev->dev,
10157 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10158 			max_tx_rate,
10159 			credits,
10160 			vsi->seid);
10161 	}
10162 
10163 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10164 	if (ret)
10165 		goto end_unlock;
10166 
10167 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
10168 	 * for this main VSI if they exist
10169 	 */
10170 	ret = i40e_rebuild_channels(vsi);
10171 	if (ret)
10172 		goto end_unlock;
10173 
10174 	/* Reconfigure hardware for allowing smaller MSS in the case
10175 	 * of TSO, so that we avoid the MDD being fired and causing
10176 	 * a reset in the case of small MSS+TSO.
10177 	 */
10178 #define I40E_REG_MSS          0x000E64DC
10179 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
10180 #define I40E_64BYTE_MSS       0x400000
10181 	val = rd32(hw, I40E_REG_MSS);
10182 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10183 		val &= ~I40E_REG_MSS_MIN_MASK;
10184 		val |= I40E_64BYTE_MSS;
10185 		wr32(hw, I40E_REG_MSS, val);
10186 	}
10187 
10188 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
10189 		msleep(75);
10190 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10191 		if (ret)
10192 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10193 				 i40e_stat_str(&pf->hw, ret),
10194 				 i40e_aq_str(&pf->hw,
10195 					     pf->hw.aq.asq_last_status));
10196 	}
10197 	/* reinit the misc interrupt */
10198 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10199 		ret = i40e_setup_misc_vector(pf);
10200 
10201 	/* Add a filter to drop all Flow control frames from any VSI from being
10202 	 * transmitted. By doing so we stop a malicious VF from sending out
10203 	 * PAUSE or PFC frames and potentially controlling traffic for other
10204 	 * PF/VF VSIs.
10205 	 * The FW can still send Flow control frames if enabled.
10206 	 */
10207 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
10208 						       pf->main_vsi_seid);
10209 
10210 	/* restart the VSIs that were rebuilt and running before the reset */
10211 	i40e_pf_unquiesce_all_vsi(pf);
10212 
10213 	/* Release the RTNL lock before we start resetting VFs */
10214 	if (!lock_acquired)
10215 		rtnl_unlock();
10216 
10217 	/* Restore promiscuous settings */
10218 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
10219 	if (ret)
10220 		dev_warn(&pf->pdev->dev,
10221 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
10222 			 pf->cur_promisc ? "on" : "off",
10223 			 i40e_stat_str(&pf->hw, ret),
10224 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10225 
10226 	i40e_reset_all_vfs(pf, true);
10227 
10228 	/* tell the firmware that we're starting */
10229 	i40e_send_version(pf);
10230 
10231 	/* We've already released the lock, so don't do it again */
10232 	goto end_core_reset;
10233 
10234 end_unlock:
10235 	if (!lock_acquired)
10236 		rtnl_unlock();
10237 end_core_reset:
10238 	clear_bit(__I40E_RESET_FAILED, pf->state);
10239 clear_recovery:
10240 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10241 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
10242 }
10243 
10244 /**
10245  * i40e_reset_and_rebuild - reset and rebuild using a saved config
10246  * @pf: board private structure
10247  * @reinit: if the Main VSI needs to re-initialized.
10248  * @lock_acquired: indicates whether or not the lock has been acquired
10249  * before this function was called.
10250  **/
10251 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
10252 				   bool lock_acquired)
10253 {
10254 	int ret;
10255 	/* Now we wait for GRST to settle out.
10256 	 * We don't have to delete the VEBs or VSIs from the hw switch
10257 	 * because the reset will make them disappear.
10258 	 */
10259 	ret = i40e_reset(pf);
10260 	if (!ret)
10261 		i40e_rebuild(pf, reinit, lock_acquired);
10262 }
10263 
10264 /**
10265  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
10266  * @pf: board private structure
10267  *
10268  * Close up the VFs and other things in prep for a Core Reset,
10269  * then get ready to rebuild the world.
10270  * @lock_acquired: indicates whether or not the lock has been acquired
10271  * before this function was called.
10272  **/
10273 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
10274 {
10275 	i40e_prep_for_reset(pf, lock_acquired);
10276 	i40e_reset_and_rebuild(pf, false, lock_acquired);
10277 }
10278 
10279 /**
10280  * i40e_handle_mdd_event
10281  * @pf: pointer to the PF structure
10282  *
10283  * Called from the MDD irq handler to identify possibly malicious vfs
10284  **/
10285 static void i40e_handle_mdd_event(struct i40e_pf *pf)
10286 {
10287 	struct i40e_hw *hw = &pf->hw;
10288 	bool mdd_detected = false;
10289 	struct i40e_vf *vf;
10290 	u32 reg;
10291 	int i;
10292 
10293 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
10294 		return;
10295 
10296 	/* find what triggered the MDD event */
10297 	reg = rd32(hw, I40E_GL_MDET_TX);
10298 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
10299 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
10300 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
10301 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
10302 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
10303 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
10304 				I40E_GL_MDET_TX_EVENT_SHIFT;
10305 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
10306 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
10307 				pf->hw.func_caps.base_queue;
10308 		if (netif_msg_tx_err(pf))
10309 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
10310 				 event, queue, pf_num, vf_num);
10311 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
10312 		mdd_detected = true;
10313 	}
10314 	reg = rd32(hw, I40E_GL_MDET_RX);
10315 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
10316 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
10317 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
10318 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
10319 				I40E_GL_MDET_RX_EVENT_SHIFT;
10320 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
10321 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
10322 				pf->hw.func_caps.base_queue;
10323 		if (netif_msg_rx_err(pf))
10324 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
10325 				 event, queue, func);
10326 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
10327 		mdd_detected = true;
10328 	}
10329 
10330 	if (mdd_detected) {
10331 		reg = rd32(hw, I40E_PF_MDET_TX);
10332 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
10333 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
10334 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
10335 		}
10336 		reg = rd32(hw, I40E_PF_MDET_RX);
10337 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
10338 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
10339 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
10340 		}
10341 	}
10342 
10343 	/* see if one of the VFs needs its hand slapped */
10344 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
10345 		vf = &(pf->vf[i]);
10346 		reg = rd32(hw, I40E_VP_MDET_TX(i));
10347 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
10348 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
10349 			vf->num_mdd_events++;
10350 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
10351 				 i);
10352 			dev_info(&pf->pdev->dev,
10353 				 "Use PF Control I/F to re-enable the VF\n");
10354 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10355 		}
10356 
10357 		reg = rd32(hw, I40E_VP_MDET_RX(i));
10358 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
10359 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
10360 			vf->num_mdd_events++;
10361 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
10362 				 i);
10363 			dev_info(&pf->pdev->dev,
10364 				 "Use PF Control I/F to re-enable the VF\n");
10365 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10366 		}
10367 	}
10368 
10369 	/* re-enable mdd interrupt cause */
10370 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
10371 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
10372 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
10373 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
10374 	i40e_flush(hw);
10375 }
10376 
10377 static const char *i40e_tunnel_name(u8 type)
10378 {
10379 	switch (type) {
10380 	case UDP_TUNNEL_TYPE_VXLAN:
10381 		return "vxlan";
10382 	case UDP_TUNNEL_TYPE_GENEVE:
10383 		return "geneve";
10384 	default:
10385 		return "unknown";
10386 	}
10387 }
10388 
10389 /**
10390  * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
10391  * @pf: board private structure
10392  **/
10393 static void i40e_sync_udp_filters(struct i40e_pf *pf)
10394 {
10395 	int i;
10396 
10397 	/* loop through and set pending bit for all active UDP filters */
10398 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
10399 		if (pf->udp_ports[i].port)
10400 			pf->pending_udp_bitmap |= BIT_ULL(i);
10401 	}
10402 
10403 	set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
10404 }
10405 
10406 /**
10407  * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
10408  * @pf: board private structure
10409  **/
10410 static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
10411 {
10412 	struct i40e_hw *hw = &pf->hw;
10413 	u8 filter_index, type;
10414 	u16 port;
10415 	int i;
10416 
10417 	if (!test_and_clear_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state))
10418 		return;
10419 
10420 	/* acquire RTNL to maintain state of flags and port requests */
10421 	rtnl_lock();
10422 
10423 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
10424 		if (pf->pending_udp_bitmap & BIT_ULL(i)) {
10425 			struct i40e_udp_port_config *udp_port;
10426 			i40e_status ret = 0;
10427 
10428 			udp_port = &pf->udp_ports[i];
10429 			pf->pending_udp_bitmap &= ~BIT_ULL(i);
10430 
10431 			port = READ_ONCE(udp_port->port);
10432 			type = READ_ONCE(udp_port->type);
10433 			filter_index = READ_ONCE(udp_port->filter_index);
10434 
10435 			/* release RTNL while we wait on AQ command */
10436 			rtnl_unlock();
10437 
10438 			if (port)
10439 				ret = i40e_aq_add_udp_tunnel(hw, port,
10440 							     type,
10441 							     &filter_index,
10442 							     NULL);
10443 			else if (filter_index != I40E_UDP_PORT_INDEX_UNUSED)
10444 				ret = i40e_aq_del_udp_tunnel(hw, filter_index,
10445 							     NULL);
10446 
10447 			/* reacquire RTNL so we can update filter_index */
10448 			rtnl_lock();
10449 
10450 			if (ret) {
10451 				dev_info(&pf->pdev->dev,
10452 					 "%s %s port %d, index %d failed, err %s aq_err %s\n",
10453 					 i40e_tunnel_name(type),
10454 					 port ? "add" : "delete",
10455 					 port,
10456 					 filter_index,
10457 					 i40e_stat_str(&pf->hw, ret),
10458 					 i40e_aq_str(&pf->hw,
10459 						     pf->hw.aq.asq_last_status));
10460 				if (port) {
10461 					/* failed to add, just reset port,
10462 					 * drop pending bit for any deletion
10463 					 */
10464 					udp_port->port = 0;
10465 					pf->pending_udp_bitmap &= ~BIT_ULL(i);
10466 				}
10467 			} else if (port) {
10468 				/* record filter index on success */
10469 				udp_port->filter_index = filter_index;
10470 			}
10471 		}
10472 	}
10473 
10474 	rtnl_unlock();
10475 }
10476 
10477 /**
10478  * i40e_service_task - Run the driver's async subtasks
10479  * @work: pointer to work_struct containing our data
10480  **/
10481 static void i40e_service_task(struct work_struct *work)
10482 {
10483 	struct i40e_pf *pf = container_of(work,
10484 					  struct i40e_pf,
10485 					  service_task);
10486 	unsigned long start_time = jiffies;
10487 
10488 	/* don't bother with service tasks if a reset is in progress */
10489 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
10490 	    test_bit(__I40E_SUSPENDED, pf->state))
10491 		return;
10492 
10493 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
10494 		return;
10495 
10496 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10497 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
10498 		i40e_sync_filters_subtask(pf);
10499 		i40e_reset_subtask(pf);
10500 		i40e_handle_mdd_event(pf);
10501 		i40e_vc_process_vflr_event(pf);
10502 		i40e_watchdog_subtask(pf);
10503 		i40e_fdir_reinit_subtask(pf);
10504 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
10505 			/* Client subtask will reopen next time through. */
10506 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
10507 							   true);
10508 		} else {
10509 			i40e_client_subtask(pf);
10510 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
10511 					       pf->state))
10512 				i40e_notify_client_of_l2_param_changes(
10513 								pf->vsi[pf->lan_vsi]);
10514 		}
10515 		i40e_sync_filters_subtask(pf);
10516 		i40e_sync_udp_filters_subtask(pf);
10517 	} else {
10518 		i40e_reset_subtask(pf);
10519 	}
10520 
10521 	i40e_clean_adminq_subtask(pf);
10522 
10523 	/* flush memory to make sure state is correct before next watchdog */
10524 	smp_mb__before_atomic();
10525 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
10526 
10527 	/* If the tasks have taken longer than one timer cycle or there
10528 	 * is more work to be done, reschedule the service task now
10529 	 * rather than wait for the timer to tick again.
10530 	 */
10531 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
10532 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
10533 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
10534 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
10535 		i40e_service_event_schedule(pf);
10536 }
10537 
10538 /**
10539  * i40e_service_timer - timer callback
10540  * @data: pointer to PF struct
10541  **/
10542 static void i40e_service_timer(struct timer_list *t)
10543 {
10544 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
10545 
10546 	mod_timer(&pf->service_timer,
10547 		  round_jiffies(jiffies + pf->service_timer_period));
10548 	i40e_service_event_schedule(pf);
10549 }
10550 
10551 /**
10552  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
10553  * @vsi: the VSI being configured
10554  **/
10555 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
10556 {
10557 	struct i40e_pf *pf = vsi->back;
10558 
10559 	switch (vsi->type) {
10560 	case I40E_VSI_MAIN:
10561 		vsi->alloc_queue_pairs = pf->num_lan_qps;
10562 		if (!vsi->num_tx_desc)
10563 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10564 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10565 		if (!vsi->num_rx_desc)
10566 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10567 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10568 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10569 			vsi->num_q_vectors = pf->num_lan_msix;
10570 		else
10571 			vsi->num_q_vectors = 1;
10572 
10573 		break;
10574 
10575 	case I40E_VSI_FDIR:
10576 		vsi->alloc_queue_pairs = 1;
10577 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
10578 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
10579 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
10580 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
10581 		vsi->num_q_vectors = pf->num_fdsb_msix;
10582 		break;
10583 
10584 	case I40E_VSI_VMDQ2:
10585 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
10586 		if (!vsi->num_tx_desc)
10587 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10588 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10589 		if (!vsi->num_rx_desc)
10590 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10591 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10592 		vsi->num_q_vectors = pf->num_vmdq_msix;
10593 		break;
10594 
10595 	case I40E_VSI_SRIOV:
10596 		vsi->alloc_queue_pairs = pf->num_vf_qps;
10597 		if (!vsi->num_tx_desc)
10598 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10599 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10600 		if (!vsi->num_rx_desc)
10601 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10602 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10603 		break;
10604 
10605 	default:
10606 		WARN_ON(1);
10607 		return -ENODATA;
10608 	}
10609 
10610 	return 0;
10611 }
10612 
10613 /**
10614  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
10615  * @vsi: VSI pointer
10616  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
10617  *
10618  * On error: returns error code (negative)
10619  * On success: returns 0
10620  **/
10621 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
10622 {
10623 	struct i40e_ring **next_rings;
10624 	int size;
10625 	int ret = 0;
10626 
10627 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
10628 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
10629 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
10630 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
10631 	if (!vsi->tx_rings)
10632 		return -ENOMEM;
10633 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
10634 	if (i40e_enabled_xdp_vsi(vsi)) {
10635 		vsi->xdp_rings = next_rings;
10636 		next_rings += vsi->alloc_queue_pairs;
10637 	}
10638 	vsi->rx_rings = next_rings;
10639 
10640 	if (alloc_qvectors) {
10641 		/* allocate memory for q_vector pointers */
10642 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
10643 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
10644 		if (!vsi->q_vectors) {
10645 			ret = -ENOMEM;
10646 			goto err_vectors;
10647 		}
10648 	}
10649 	return ret;
10650 
10651 err_vectors:
10652 	kfree(vsi->tx_rings);
10653 	return ret;
10654 }
10655 
10656 /**
10657  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
10658  * @pf: board private structure
10659  * @type: type of VSI
10660  *
10661  * On error: returns error code (negative)
10662  * On success: returns vsi index in PF (positive)
10663  **/
10664 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
10665 {
10666 	int ret = -ENODEV;
10667 	struct i40e_vsi *vsi;
10668 	int vsi_idx;
10669 	int i;
10670 
10671 	/* Need to protect the allocation of the VSIs at the PF level */
10672 	mutex_lock(&pf->switch_mutex);
10673 
10674 	/* VSI list may be fragmented if VSI creation/destruction has
10675 	 * been happening.  We can afford to do a quick scan to look
10676 	 * for any free VSIs in the list.
10677 	 *
10678 	 * find next empty vsi slot, looping back around if necessary
10679 	 */
10680 	i = pf->next_vsi;
10681 	while (i < pf->num_alloc_vsi && pf->vsi[i])
10682 		i++;
10683 	if (i >= pf->num_alloc_vsi) {
10684 		i = 0;
10685 		while (i < pf->next_vsi && pf->vsi[i])
10686 			i++;
10687 	}
10688 
10689 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
10690 		vsi_idx = i;             /* Found one! */
10691 	} else {
10692 		ret = -ENODEV;
10693 		goto unlock_pf;  /* out of VSI slots! */
10694 	}
10695 	pf->next_vsi = ++i;
10696 
10697 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
10698 	if (!vsi) {
10699 		ret = -ENOMEM;
10700 		goto unlock_pf;
10701 	}
10702 	vsi->type = type;
10703 	vsi->back = pf;
10704 	set_bit(__I40E_VSI_DOWN, vsi->state);
10705 	vsi->flags = 0;
10706 	vsi->idx = vsi_idx;
10707 	vsi->int_rate_limit = 0;
10708 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
10709 				pf->rss_table_size : 64;
10710 	vsi->netdev_registered = false;
10711 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
10712 	hash_init(vsi->mac_filter_hash);
10713 	vsi->irqs_ready = false;
10714 
10715 	if (type == I40E_VSI_MAIN) {
10716 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
10717 		if (!vsi->af_xdp_zc_qps)
10718 			goto err_rings;
10719 	}
10720 
10721 	ret = i40e_set_num_rings_in_vsi(vsi);
10722 	if (ret)
10723 		goto err_rings;
10724 
10725 	ret = i40e_vsi_alloc_arrays(vsi, true);
10726 	if (ret)
10727 		goto err_rings;
10728 
10729 	/* Setup default MSIX irq handler for VSI */
10730 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
10731 
10732 	/* Initialize VSI lock */
10733 	spin_lock_init(&vsi->mac_filter_hash_lock);
10734 	pf->vsi[vsi_idx] = vsi;
10735 	ret = vsi_idx;
10736 	goto unlock_pf;
10737 
10738 err_rings:
10739 	bitmap_free(vsi->af_xdp_zc_qps);
10740 	pf->next_vsi = i - 1;
10741 	kfree(vsi);
10742 unlock_pf:
10743 	mutex_unlock(&pf->switch_mutex);
10744 	return ret;
10745 }
10746 
10747 /**
10748  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
10749  * @vsi: VSI pointer
10750  * @free_qvectors: a bool to specify if q_vectors need to be freed.
10751  *
10752  * On error: returns error code (negative)
10753  * On success: returns 0
10754  **/
10755 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
10756 {
10757 	/* free the ring and vector containers */
10758 	if (free_qvectors) {
10759 		kfree(vsi->q_vectors);
10760 		vsi->q_vectors = NULL;
10761 	}
10762 	kfree(vsi->tx_rings);
10763 	vsi->tx_rings = NULL;
10764 	vsi->rx_rings = NULL;
10765 	vsi->xdp_rings = NULL;
10766 }
10767 
10768 /**
10769  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
10770  * and lookup table
10771  * @vsi: Pointer to VSI structure
10772  */
10773 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
10774 {
10775 	if (!vsi)
10776 		return;
10777 
10778 	kfree(vsi->rss_hkey_user);
10779 	vsi->rss_hkey_user = NULL;
10780 
10781 	kfree(vsi->rss_lut_user);
10782 	vsi->rss_lut_user = NULL;
10783 }
10784 
10785 /**
10786  * i40e_vsi_clear - Deallocate the VSI provided
10787  * @vsi: the VSI being un-configured
10788  **/
10789 static int i40e_vsi_clear(struct i40e_vsi *vsi)
10790 {
10791 	struct i40e_pf *pf;
10792 
10793 	if (!vsi)
10794 		return 0;
10795 
10796 	if (!vsi->back)
10797 		goto free_vsi;
10798 	pf = vsi->back;
10799 
10800 	mutex_lock(&pf->switch_mutex);
10801 	if (!pf->vsi[vsi->idx]) {
10802 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
10803 			vsi->idx, vsi->idx, vsi->type);
10804 		goto unlock_vsi;
10805 	}
10806 
10807 	if (pf->vsi[vsi->idx] != vsi) {
10808 		dev_err(&pf->pdev->dev,
10809 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
10810 			pf->vsi[vsi->idx]->idx,
10811 			pf->vsi[vsi->idx]->type,
10812 			vsi->idx, vsi->type);
10813 		goto unlock_vsi;
10814 	}
10815 
10816 	/* updates the PF for this cleared vsi */
10817 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
10818 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
10819 
10820 	bitmap_free(vsi->af_xdp_zc_qps);
10821 	i40e_vsi_free_arrays(vsi, true);
10822 	i40e_clear_rss_config_user(vsi);
10823 
10824 	pf->vsi[vsi->idx] = NULL;
10825 	if (vsi->idx < pf->next_vsi)
10826 		pf->next_vsi = vsi->idx;
10827 
10828 unlock_vsi:
10829 	mutex_unlock(&pf->switch_mutex);
10830 free_vsi:
10831 	kfree(vsi);
10832 
10833 	return 0;
10834 }
10835 
10836 /**
10837  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
10838  * @vsi: the VSI being cleaned
10839  **/
10840 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
10841 {
10842 	int i;
10843 
10844 	if (vsi->tx_rings && vsi->tx_rings[0]) {
10845 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10846 			kfree_rcu(vsi->tx_rings[i], rcu);
10847 			vsi->tx_rings[i] = NULL;
10848 			vsi->rx_rings[i] = NULL;
10849 			if (vsi->xdp_rings)
10850 				vsi->xdp_rings[i] = NULL;
10851 		}
10852 	}
10853 }
10854 
10855 /**
10856  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
10857  * @vsi: the VSI being configured
10858  **/
10859 static int i40e_alloc_rings(struct i40e_vsi *vsi)
10860 {
10861 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
10862 	struct i40e_pf *pf = vsi->back;
10863 	struct i40e_ring *ring;
10864 
10865 	/* Set basic values in the rings to be used later during open() */
10866 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10867 		/* allocate space for both Tx and Rx in one shot */
10868 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
10869 		if (!ring)
10870 			goto err_out;
10871 
10872 		ring->queue_index = i;
10873 		ring->reg_idx = vsi->base_queue + i;
10874 		ring->ring_active = false;
10875 		ring->vsi = vsi;
10876 		ring->netdev = vsi->netdev;
10877 		ring->dev = &pf->pdev->dev;
10878 		ring->count = vsi->num_tx_desc;
10879 		ring->size = 0;
10880 		ring->dcb_tc = 0;
10881 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10882 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10883 		ring->itr_setting = pf->tx_itr_default;
10884 		vsi->tx_rings[i] = ring++;
10885 
10886 		if (!i40e_enabled_xdp_vsi(vsi))
10887 			goto setup_rx;
10888 
10889 		ring->queue_index = vsi->alloc_queue_pairs + i;
10890 		ring->reg_idx = vsi->base_queue + ring->queue_index;
10891 		ring->ring_active = false;
10892 		ring->vsi = vsi;
10893 		ring->netdev = NULL;
10894 		ring->dev = &pf->pdev->dev;
10895 		ring->count = vsi->num_tx_desc;
10896 		ring->size = 0;
10897 		ring->dcb_tc = 0;
10898 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10899 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10900 		set_ring_xdp(ring);
10901 		ring->itr_setting = pf->tx_itr_default;
10902 		vsi->xdp_rings[i] = ring++;
10903 
10904 setup_rx:
10905 		ring->queue_index = i;
10906 		ring->reg_idx = vsi->base_queue + i;
10907 		ring->ring_active = false;
10908 		ring->vsi = vsi;
10909 		ring->netdev = vsi->netdev;
10910 		ring->dev = &pf->pdev->dev;
10911 		ring->count = vsi->num_rx_desc;
10912 		ring->size = 0;
10913 		ring->dcb_tc = 0;
10914 		ring->itr_setting = pf->rx_itr_default;
10915 		vsi->rx_rings[i] = ring;
10916 	}
10917 
10918 	return 0;
10919 
10920 err_out:
10921 	i40e_vsi_clear_rings(vsi);
10922 	return -ENOMEM;
10923 }
10924 
10925 /**
10926  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
10927  * @pf: board private structure
10928  * @vectors: the number of MSI-X vectors to request
10929  *
10930  * Returns the number of vectors reserved, or error
10931  **/
10932 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
10933 {
10934 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
10935 					I40E_MIN_MSIX, vectors);
10936 	if (vectors < 0) {
10937 		dev_info(&pf->pdev->dev,
10938 			 "MSI-X vector reservation failed: %d\n", vectors);
10939 		vectors = 0;
10940 	}
10941 
10942 	return vectors;
10943 }
10944 
10945 /**
10946  * i40e_init_msix - Setup the MSIX capability
10947  * @pf: board private structure
10948  *
10949  * Work with the OS to set up the MSIX vectors needed.
10950  *
10951  * Returns the number of vectors reserved or negative on failure
10952  **/
10953 static int i40e_init_msix(struct i40e_pf *pf)
10954 {
10955 	struct i40e_hw *hw = &pf->hw;
10956 	int cpus, extra_vectors;
10957 	int vectors_left;
10958 	int v_budget, i;
10959 	int v_actual;
10960 	int iwarp_requested = 0;
10961 
10962 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
10963 		return -ENODEV;
10964 
10965 	/* The number of vectors we'll request will be comprised of:
10966 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
10967 	 *   - The number of LAN queue pairs
10968 	 *	- Queues being used for RSS.
10969 	 *		We don't need as many as max_rss_size vectors.
10970 	 *		use rss_size instead in the calculation since that
10971 	 *		is governed by number of cpus in the system.
10972 	 *	- assumes symmetric Tx/Rx pairing
10973 	 *   - The number of VMDq pairs
10974 	 *   - The CPU count within the NUMA node if iWARP is enabled
10975 	 * Once we count this up, try the request.
10976 	 *
10977 	 * If we can't get what we want, we'll simplify to nearly nothing
10978 	 * and try again.  If that still fails, we punt.
10979 	 */
10980 	vectors_left = hw->func_caps.num_msix_vectors;
10981 	v_budget = 0;
10982 
10983 	/* reserve one vector for miscellaneous handler */
10984 	if (vectors_left) {
10985 		v_budget++;
10986 		vectors_left--;
10987 	}
10988 
10989 	/* reserve some vectors for the main PF traffic queues. Initially we
10990 	 * only reserve at most 50% of the available vectors, in the case that
10991 	 * the number of online CPUs is large. This ensures that we can enable
10992 	 * extra features as well. Once we've enabled the other features, we
10993 	 * will use any remaining vectors to reach as close as we can to the
10994 	 * number of online CPUs.
10995 	 */
10996 	cpus = num_online_cpus();
10997 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
10998 	vectors_left -= pf->num_lan_msix;
10999 
11000 	/* reserve one vector for sideband flow director */
11001 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11002 		if (vectors_left) {
11003 			pf->num_fdsb_msix = 1;
11004 			v_budget++;
11005 			vectors_left--;
11006 		} else {
11007 			pf->num_fdsb_msix = 0;
11008 		}
11009 	}
11010 
11011 	/* can we reserve enough for iWARP? */
11012 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11013 		iwarp_requested = pf->num_iwarp_msix;
11014 
11015 		if (!vectors_left)
11016 			pf->num_iwarp_msix = 0;
11017 		else if (vectors_left < pf->num_iwarp_msix)
11018 			pf->num_iwarp_msix = 1;
11019 		v_budget += pf->num_iwarp_msix;
11020 		vectors_left -= pf->num_iwarp_msix;
11021 	}
11022 
11023 	/* any vectors left over go for VMDq support */
11024 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11025 		if (!vectors_left) {
11026 			pf->num_vmdq_msix = 0;
11027 			pf->num_vmdq_qps = 0;
11028 		} else {
11029 			int vmdq_vecs_wanted =
11030 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11031 			int vmdq_vecs =
11032 				min_t(int, vectors_left, vmdq_vecs_wanted);
11033 
11034 			/* if we're short on vectors for what's desired, we limit
11035 			 * the queues per vmdq.  If this is still more than are
11036 			 * available, the user will need to change the number of
11037 			 * queues/vectors used by the PF later with the ethtool
11038 			 * channels command
11039 			 */
11040 			if (vectors_left < vmdq_vecs_wanted) {
11041 				pf->num_vmdq_qps = 1;
11042 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11043 				vmdq_vecs = min_t(int,
11044 						  vectors_left,
11045 						  vmdq_vecs_wanted);
11046 			}
11047 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11048 
11049 			v_budget += vmdq_vecs;
11050 			vectors_left -= vmdq_vecs;
11051 		}
11052 	}
11053 
11054 	/* On systems with a large number of SMP cores, we previously limited
11055 	 * the number of vectors for num_lan_msix to be at most 50% of the
11056 	 * available vectors, to allow for other features. Now, we add back
11057 	 * the remaining vectors. However, we ensure that the total
11058 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11059 	 * calculate the number of vectors we can add without going over the
11060 	 * cap of CPUs. For systems with a small number of CPUs this will be
11061 	 * zero.
11062 	 */
11063 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11064 	pf->num_lan_msix += extra_vectors;
11065 	vectors_left -= extra_vectors;
11066 
11067 	WARN(vectors_left < 0,
11068 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11069 
11070 	v_budget += pf->num_lan_msix;
11071 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11072 				   GFP_KERNEL);
11073 	if (!pf->msix_entries)
11074 		return -ENOMEM;
11075 
11076 	for (i = 0; i < v_budget; i++)
11077 		pf->msix_entries[i].entry = i;
11078 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11079 
11080 	if (v_actual < I40E_MIN_MSIX) {
11081 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11082 		kfree(pf->msix_entries);
11083 		pf->msix_entries = NULL;
11084 		pci_disable_msix(pf->pdev);
11085 		return -ENODEV;
11086 
11087 	} else if (v_actual == I40E_MIN_MSIX) {
11088 		/* Adjust for minimal MSIX use */
11089 		pf->num_vmdq_vsis = 0;
11090 		pf->num_vmdq_qps = 0;
11091 		pf->num_lan_qps = 1;
11092 		pf->num_lan_msix = 1;
11093 
11094 	} else if (v_actual != v_budget) {
11095 		/* If we have limited resources, we will start with no vectors
11096 		 * for the special features and then allocate vectors to some
11097 		 * of these features based on the policy and at the end disable
11098 		 * the features that did not get any vectors.
11099 		 */
11100 		int vec;
11101 
11102 		dev_info(&pf->pdev->dev,
11103 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11104 			 v_actual, v_budget);
11105 		/* reserve the misc vector */
11106 		vec = v_actual - 1;
11107 
11108 		/* Scale vector usage down */
11109 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11110 		pf->num_vmdq_vsis = 1;
11111 		pf->num_vmdq_qps = 1;
11112 
11113 		/* partition out the remaining vectors */
11114 		switch (vec) {
11115 		case 2:
11116 			pf->num_lan_msix = 1;
11117 			break;
11118 		case 3:
11119 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11120 				pf->num_lan_msix = 1;
11121 				pf->num_iwarp_msix = 1;
11122 			} else {
11123 				pf->num_lan_msix = 2;
11124 			}
11125 			break;
11126 		default:
11127 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11128 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11129 						 iwarp_requested);
11130 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11131 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11132 			} else {
11133 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11134 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11135 			}
11136 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11137 				pf->num_fdsb_msix = 1;
11138 				vec--;
11139 			}
11140 			pf->num_lan_msix = min_t(int,
11141 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11142 							      pf->num_lan_msix);
11143 			pf->num_lan_qps = pf->num_lan_msix;
11144 			break;
11145 		}
11146 	}
11147 
11148 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11149 	    (pf->num_fdsb_msix == 0)) {
11150 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11151 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11152 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11153 	}
11154 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11155 	    (pf->num_vmdq_msix == 0)) {
11156 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11157 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11158 	}
11159 
11160 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11161 	    (pf->num_iwarp_msix == 0)) {
11162 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11163 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11164 	}
11165 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11166 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11167 		   pf->num_lan_msix,
11168 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11169 		   pf->num_fdsb_msix,
11170 		   pf->num_iwarp_msix);
11171 
11172 	return v_actual;
11173 }
11174 
11175 /**
11176  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11177  * @vsi: the VSI being configured
11178  * @v_idx: index of the vector in the vsi struct
11179  * @cpu: cpu to be used on affinity_mask
11180  *
11181  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11182  **/
11183 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
11184 {
11185 	struct i40e_q_vector *q_vector;
11186 
11187 	/* allocate q_vector */
11188 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11189 	if (!q_vector)
11190 		return -ENOMEM;
11191 
11192 	q_vector->vsi = vsi;
11193 	q_vector->v_idx = v_idx;
11194 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11195 
11196 	if (vsi->netdev)
11197 		netif_napi_add(vsi->netdev, &q_vector->napi,
11198 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
11199 
11200 	/* tie q_vector and vsi together */
11201 	vsi->q_vectors[v_idx] = q_vector;
11202 
11203 	return 0;
11204 }
11205 
11206 /**
11207  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11208  * @vsi: the VSI being configured
11209  *
11210  * We allocate one q_vector per queue interrupt.  If allocation fails we
11211  * return -ENOMEM.
11212  **/
11213 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11214 {
11215 	struct i40e_pf *pf = vsi->back;
11216 	int err, v_idx, num_q_vectors, current_cpu;
11217 
11218 	/* if not MSIX, give the one vector only to the LAN VSI */
11219 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11220 		num_q_vectors = vsi->num_q_vectors;
11221 	else if (vsi == pf->vsi[pf->lan_vsi])
11222 		num_q_vectors = 1;
11223 	else
11224 		return -EINVAL;
11225 
11226 	current_cpu = cpumask_first(cpu_online_mask);
11227 
11228 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11229 		err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
11230 		if (err)
11231 			goto err_out;
11232 		current_cpu = cpumask_next(current_cpu, cpu_online_mask);
11233 		if (unlikely(current_cpu >= nr_cpu_ids))
11234 			current_cpu = cpumask_first(cpu_online_mask);
11235 	}
11236 
11237 	return 0;
11238 
11239 err_out:
11240 	while (v_idx--)
11241 		i40e_free_q_vector(vsi, v_idx);
11242 
11243 	return err;
11244 }
11245 
11246 /**
11247  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11248  * @pf: board private structure to initialize
11249  **/
11250 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11251 {
11252 	int vectors = 0;
11253 	ssize_t size;
11254 
11255 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11256 		vectors = i40e_init_msix(pf);
11257 		if (vectors < 0) {
11258 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11259 				       I40E_FLAG_IWARP_ENABLED	|
11260 				       I40E_FLAG_RSS_ENABLED	|
11261 				       I40E_FLAG_DCB_CAPABLE	|
11262 				       I40E_FLAG_DCB_ENABLED	|
11263 				       I40E_FLAG_SRIOV_ENABLED	|
11264 				       I40E_FLAG_FD_SB_ENABLED	|
11265 				       I40E_FLAG_FD_ATR_ENABLED	|
11266 				       I40E_FLAG_VMDQ_ENABLED);
11267 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11268 
11269 			/* rework the queue expectations without MSIX */
11270 			i40e_determine_queue_usage(pf);
11271 		}
11272 	}
11273 
11274 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11275 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
11276 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
11277 		vectors = pci_enable_msi(pf->pdev);
11278 		if (vectors < 0) {
11279 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
11280 				 vectors);
11281 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
11282 		}
11283 		vectors = 1;  /* one MSI or Legacy vector */
11284 	}
11285 
11286 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
11287 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
11288 
11289 	/* set up vector assignment tracking */
11290 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
11291 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
11292 	if (!pf->irq_pile)
11293 		return -ENOMEM;
11294 
11295 	pf->irq_pile->num_entries = vectors;
11296 	pf->irq_pile->search_hint = 0;
11297 
11298 	/* track first vector for misc interrupts, ignore return */
11299 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
11300 
11301 	return 0;
11302 }
11303 
11304 /**
11305  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
11306  * @pf: private board data structure
11307  *
11308  * Restore the interrupt scheme that was cleared when we suspended the
11309  * device. This should be called during resume to re-allocate the q_vectors
11310  * and reacquire IRQs.
11311  */
11312 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
11313 {
11314 	int err, i;
11315 
11316 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
11317 	 * scheme. We need to re-enabled them here in order to attempt to
11318 	 * re-acquire the MSI or MSI-X vectors
11319 	 */
11320 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
11321 
11322 	err = i40e_init_interrupt_scheme(pf);
11323 	if (err)
11324 		return err;
11325 
11326 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
11327 	 * rings together again.
11328 	 */
11329 	for (i = 0; i < pf->num_alloc_vsi; i++) {
11330 		if (pf->vsi[i]) {
11331 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
11332 			if (err)
11333 				goto err_unwind;
11334 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
11335 		}
11336 	}
11337 
11338 	err = i40e_setup_misc_vector(pf);
11339 	if (err)
11340 		goto err_unwind;
11341 
11342 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
11343 		i40e_client_update_msix_info(pf);
11344 
11345 	return 0;
11346 
11347 err_unwind:
11348 	while (i--) {
11349 		if (pf->vsi[i])
11350 			i40e_vsi_free_q_vectors(pf->vsi[i]);
11351 	}
11352 
11353 	return err;
11354 }
11355 
11356 /**
11357  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
11358  * non queue events in recovery mode
11359  * @pf: board private structure
11360  *
11361  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
11362  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
11363  * This is handled differently than in recovery mode since no Tx/Rx resources
11364  * are being allocated.
11365  **/
11366 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
11367 {
11368 	int err;
11369 
11370 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11371 		err = i40e_setup_misc_vector(pf);
11372 
11373 		if (err) {
11374 			dev_info(&pf->pdev->dev,
11375 				 "MSI-X misc vector request failed, error %d\n",
11376 				 err);
11377 			return err;
11378 		}
11379 	} else {
11380 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
11381 
11382 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
11383 				  pf->int_name, pf);
11384 
11385 		if (err) {
11386 			dev_info(&pf->pdev->dev,
11387 				 "MSI/legacy misc vector request failed, error %d\n",
11388 				 err);
11389 			return err;
11390 		}
11391 		i40e_enable_misc_int_causes(pf);
11392 		i40e_irq_dynamic_enable_icr0(pf);
11393 	}
11394 
11395 	return 0;
11396 }
11397 
11398 /**
11399  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
11400  * @pf: board private structure
11401  *
11402  * This sets up the handler for MSIX 0, which is used to manage the
11403  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
11404  * when in MSI or Legacy interrupt mode.
11405  **/
11406 static int i40e_setup_misc_vector(struct i40e_pf *pf)
11407 {
11408 	struct i40e_hw *hw = &pf->hw;
11409 	int err = 0;
11410 
11411 	/* Only request the IRQ once, the first time through. */
11412 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
11413 		err = request_irq(pf->msix_entries[0].vector,
11414 				  i40e_intr, 0, pf->int_name, pf);
11415 		if (err) {
11416 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
11417 			dev_info(&pf->pdev->dev,
11418 				 "request_irq for %s failed: %d\n",
11419 				 pf->int_name, err);
11420 			return -EFAULT;
11421 		}
11422 	}
11423 
11424 	i40e_enable_misc_int_causes(pf);
11425 
11426 	/* associate no queues to the misc vector */
11427 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
11428 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
11429 
11430 	i40e_flush(hw);
11431 
11432 	i40e_irq_dynamic_enable_icr0(pf);
11433 
11434 	return err;
11435 }
11436 
11437 /**
11438  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
11439  * @vsi: Pointer to vsi structure
11440  * @seed: Buffter to store the hash keys
11441  * @lut: Buffer to store the lookup table entries
11442  * @lut_size: Size of buffer to store the lookup table entries
11443  *
11444  * Return 0 on success, negative on failure
11445  */
11446 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
11447 			   u8 *lut, u16 lut_size)
11448 {
11449 	struct i40e_pf *pf = vsi->back;
11450 	struct i40e_hw *hw = &pf->hw;
11451 	int ret = 0;
11452 
11453 	if (seed) {
11454 		ret = i40e_aq_get_rss_key(hw, vsi->id,
11455 			(struct i40e_aqc_get_set_rss_key_data *)seed);
11456 		if (ret) {
11457 			dev_info(&pf->pdev->dev,
11458 				 "Cannot get RSS key, err %s aq_err %s\n",
11459 				 i40e_stat_str(&pf->hw, ret),
11460 				 i40e_aq_str(&pf->hw,
11461 					     pf->hw.aq.asq_last_status));
11462 			return ret;
11463 		}
11464 	}
11465 
11466 	if (lut) {
11467 		bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
11468 
11469 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
11470 		if (ret) {
11471 			dev_info(&pf->pdev->dev,
11472 				 "Cannot get RSS lut, err %s aq_err %s\n",
11473 				 i40e_stat_str(&pf->hw, ret),
11474 				 i40e_aq_str(&pf->hw,
11475 					     pf->hw.aq.asq_last_status));
11476 			return ret;
11477 		}
11478 	}
11479 
11480 	return ret;
11481 }
11482 
11483 /**
11484  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
11485  * @vsi: Pointer to vsi structure
11486  * @seed: RSS hash seed
11487  * @lut: Lookup table
11488  * @lut_size: Lookup table size
11489  *
11490  * Returns 0 on success, negative on failure
11491  **/
11492 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
11493 			       const u8 *lut, u16 lut_size)
11494 {
11495 	struct i40e_pf *pf = vsi->back;
11496 	struct i40e_hw *hw = &pf->hw;
11497 	u16 vf_id = vsi->vf_id;
11498 	u8 i;
11499 
11500 	/* Fill out hash function seed */
11501 	if (seed) {
11502 		u32 *seed_dw = (u32 *)seed;
11503 
11504 		if (vsi->type == I40E_VSI_MAIN) {
11505 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
11506 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
11507 		} else if (vsi->type == I40E_VSI_SRIOV) {
11508 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
11509 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
11510 		} else {
11511 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
11512 		}
11513 	}
11514 
11515 	if (lut) {
11516 		u32 *lut_dw = (u32 *)lut;
11517 
11518 		if (vsi->type == I40E_VSI_MAIN) {
11519 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
11520 				return -EINVAL;
11521 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11522 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
11523 		} else if (vsi->type == I40E_VSI_SRIOV) {
11524 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
11525 				return -EINVAL;
11526 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
11527 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
11528 		} else {
11529 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
11530 		}
11531 	}
11532 	i40e_flush(hw);
11533 
11534 	return 0;
11535 }
11536 
11537 /**
11538  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
11539  * @vsi: Pointer to VSI structure
11540  * @seed: Buffer to store the keys
11541  * @lut: Buffer to store the lookup table entries
11542  * @lut_size: Size of buffer to store the lookup table entries
11543  *
11544  * Returns 0 on success, negative on failure
11545  */
11546 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
11547 			    u8 *lut, u16 lut_size)
11548 {
11549 	struct i40e_pf *pf = vsi->back;
11550 	struct i40e_hw *hw = &pf->hw;
11551 	u16 i;
11552 
11553 	if (seed) {
11554 		u32 *seed_dw = (u32 *)seed;
11555 
11556 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
11557 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
11558 	}
11559 	if (lut) {
11560 		u32 *lut_dw = (u32 *)lut;
11561 
11562 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
11563 			return -EINVAL;
11564 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11565 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
11566 	}
11567 
11568 	return 0;
11569 }
11570 
11571 /**
11572  * i40e_config_rss - Configure RSS keys and lut
11573  * @vsi: Pointer to VSI structure
11574  * @seed: RSS hash seed
11575  * @lut: Lookup table
11576  * @lut_size: Lookup table size
11577  *
11578  * Returns 0 on success, negative on failure
11579  */
11580 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
11581 {
11582 	struct i40e_pf *pf = vsi->back;
11583 
11584 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
11585 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
11586 	else
11587 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
11588 }
11589 
11590 /**
11591  * i40e_get_rss - Get RSS keys and lut
11592  * @vsi: Pointer to VSI structure
11593  * @seed: Buffer to store the keys
11594  * @lut: Buffer to store the lookup table entries
11595  * @lut_size: Size of buffer to store the lookup table entries
11596  *
11597  * Returns 0 on success, negative on failure
11598  */
11599 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
11600 {
11601 	struct i40e_pf *pf = vsi->back;
11602 
11603 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
11604 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
11605 	else
11606 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
11607 }
11608 
11609 /**
11610  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
11611  * @pf: Pointer to board private structure
11612  * @lut: Lookup table
11613  * @rss_table_size: Lookup table size
11614  * @rss_size: Range of queue number for hashing
11615  */
11616 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
11617 		       u16 rss_table_size, u16 rss_size)
11618 {
11619 	u16 i;
11620 
11621 	for (i = 0; i < rss_table_size; i++)
11622 		lut[i] = i % rss_size;
11623 }
11624 
11625 /**
11626  * i40e_pf_config_rss - Prepare for RSS if used
11627  * @pf: board private structure
11628  **/
11629 static int i40e_pf_config_rss(struct i40e_pf *pf)
11630 {
11631 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
11632 	u8 seed[I40E_HKEY_ARRAY_SIZE];
11633 	u8 *lut;
11634 	struct i40e_hw *hw = &pf->hw;
11635 	u32 reg_val;
11636 	u64 hena;
11637 	int ret;
11638 
11639 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
11640 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
11641 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
11642 	hena |= i40e_pf_get_default_rss_hena(pf);
11643 
11644 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
11645 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
11646 
11647 	/* Determine the RSS table size based on the hardware capabilities */
11648 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
11649 	reg_val = (pf->rss_table_size == 512) ?
11650 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
11651 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
11652 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
11653 
11654 	/* Determine the RSS size of the VSI */
11655 	if (!vsi->rss_size) {
11656 		u16 qcount;
11657 		/* If the firmware does something weird during VSI init, we
11658 		 * could end up with zero TCs. Check for that to avoid
11659 		 * divide-by-zero. It probably won't pass traffic, but it also
11660 		 * won't panic.
11661 		 */
11662 		qcount = vsi->num_queue_pairs /
11663 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
11664 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
11665 	}
11666 	if (!vsi->rss_size)
11667 		return -EINVAL;
11668 
11669 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
11670 	if (!lut)
11671 		return -ENOMEM;
11672 
11673 	/* Use user configured lut if there is one, otherwise use default */
11674 	if (vsi->rss_lut_user)
11675 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
11676 	else
11677 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
11678 
11679 	/* Use user configured hash key if there is one, otherwise
11680 	 * use default.
11681 	 */
11682 	if (vsi->rss_hkey_user)
11683 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
11684 	else
11685 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
11686 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
11687 	kfree(lut);
11688 
11689 	return ret;
11690 }
11691 
11692 /**
11693  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
11694  * @pf: board private structure
11695  * @queue_count: the requested queue count for rss.
11696  *
11697  * returns 0 if rss is not enabled, if enabled returns the final rss queue
11698  * count which may be different from the requested queue count.
11699  * Note: expects to be called while under rtnl_lock()
11700  **/
11701 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
11702 {
11703 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
11704 	int new_rss_size;
11705 
11706 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
11707 		return 0;
11708 
11709 	queue_count = min_t(int, queue_count, num_online_cpus());
11710 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
11711 
11712 	if (queue_count != vsi->num_queue_pairs) {
11713 		u16 qcount;
11714 
11715 		vsi->req_queue_pairs = queue_count;
11716 		i40e_prep_for_reset(pf, true);
11717 
11718 		pf->alloc_rss_size = new_rss_size;
11719 
11720 		i40e_reset_and_rebuild(pf, true, true);
11721 
11722 		/* Discard the user configured hash keys and lut, if less
11723 		 * queues are enabled.
11724 		 */
11725 		if (queue_count < vsi->rss_size) {
11726 			i40e_clear_rss_config_user(vsi);
11727 			dev_dbg(&pf->pdev->dev,
11728 				"discard user configured hash keys and lut\n");
11729 		}
11730 
11731 		/* Reset vsi->rss_size, as number of enabled queues changed */
11732 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
11733 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
11734 
11735 		i40e_pf_config_rss(pf);
11736 	}
11737 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
11738 		 vsi->req_queue_pairs, pf->rss_size_max);
11739 	return pf->alloc_rss_size;
11740 }
11741 
11742 /**
11743  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
11744  * @pf: board private structure
11745  **/
11746 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
11747 {
11748 	i40e_status status;
11749 	bool min_valid, max_valid;
11750 	u32 max_bw, min_bw;
11751 
11752 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
11753 					   &min_valid, &max_valid);
11754 
11755 	if (!status) {
11756 		if (min_valid)
11757 			pf->min_bw = min_bw;
11758 		if (max_valid)
11759 			pf->max_bw = max_bw;
11760 	}
11761 
11762 	return status;
11763 }
11764 
11765 /**
11766  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
11767  * @pf: board private structure
11768  **/
11769 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
11770 {
11771 	struct i40e_aqc_configure_partition_bw_data bw_data;
11772 	i40e_status status;
11773 
11774 	/* Set the valid bit for this PF */
11775 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
11776 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
11777 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
11778 
11779 	/* Set the new bandwidths */
11780 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
11781 
11782 	return status;
11783 }
11784 
11785 /**
11786  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
11787  * @pf: board private structure
11788  **/
11789 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
11790 {
11791 	/* Commit temporary BW setting to permanent NVM image */
11792 	enum i40e_admin_queue_err last_aq_status;
11793 	i40e_status ret;
11794 	u16 nvm_word;
11795 
11796 	if (pf->hw.partition_id != 1) {
11797 		dev_info(&pf->pdev->dev,
11798 			 "Commit BW only works on partition 1! This is partition %d",
11799 			 pf->hw.partition_id);
11800 		ret = I40E_NOT_SUPPORTED;
11801 		goto bw_commit_out;
11802 	}
11803 
11804 	/* Acquire NVM for read access */
11805 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
11806 	last_aq_status = pf->hw.aq.asq_last_status;
11807 	if (ret) {
11808 		dev_info(&pf->pdev->dev,
11809 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
11810 			 i40e_stat_str(&pf->hw, ret),
11811 			 i40e_aq_str(&pf->hw, last_aq_status));
11812 		goto bw_commit_out;
11813 	}
11814 
11815 	/* Read word 0x10 of NVM - SW compatibility word 1 */
11816 	ret = i40e_aq_read_nvm(&pf->hw,
11817 			       I40E_SR_NVM_CONTROL_WORD,
11818 			       0x10, sizeof(nvm_word), &nvm_word,
11819 			       false, NULL);
11820 	/* Save off last admin queue command status before releasing
11821 	 * the NVM
11822 	 */
11823 	last_aq_status = pf->hw.aq.asq_last_status;
11824 	i40e_release_nvm(&pf->hw);
11825 	if (ret) {
11826 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
11827 			 i40e_stat_str(&pf->hw, ret),
11828 			 i40e_aq_str(&pf->hw, last_aq_status));
11829 		goto bw_commit_out;
11830 	}
11831 
11832 	/* Wait a bit for NVM release to complete */
11833 	msleep(50);
11834 
11835 	/* Acquire NVM for write access */
11836 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
11837 	last_aq_status = pf->hw.aq.asq_last_status;
11838 	if (ret) {
11839 		dev_info(&pf->pdev->dev,
11840 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
11841 			 i40e_stat_str(&pf->hw, ret),
11842 			 i40e_aq_str(&pf->hw, last_aq_status));
11843 		goto bw_commit_out;
11844 	}
11845 	/* Write it back out unchanged to initiate update NVM,
11846 	 * which will force a write of the shadow (alt) RAM to
11847 	 * the NVM - thus storing the bandwidth values permanently.
11848 	 */
11849 	ret = i40e_aq_update_nvm(&pf->hw,
11850 				 I40E_SR_NVM_CONTROL_WORD,
11851 				 0x10, sizeof(nvm_word),
11852 				 &nvm_word, true, 0, NULL);
11853 	/* Save off last admin queue command status before releasing
11854 	 * the NVM
11855 	 */
11856 	last_aq_status = pf->hw.aq.asq_last_status;
11857 	i40e_release_nvm(&pf->hw);
11858 	if (ret)
11859 		dev_info(&pf->pdev->dev,
11860 			 "BW settings NOT SAVED, err %s aq_err %s\n",
11861 			 i40e_stat_str(&pf->hw, ret),
11862 			 i40e_aq_str(&pf->hw, last_aq_status));
11863 bw_commit_out:
11864 
11865 	return ret;
11866 }
11867 
11868 /**
11869  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
11870  * @pf: board private structure to initialize
11871  *
11872  * i40e_sw_init initializes the Adapter private data structure.
11873  * Fields are initialized based on PCI device information and
11874  * OS network device settings (MTU size).
11875  **/
11876 static int i40e_sw_init(struct i40e_pf *pf)
11877 {
11878 	int err = 0;
11879 	int size;
11880 
11881 	/* Set default capability flags */
11882 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
11883 		    I40E_FLAG_MSI_ENABLED     |
11884 		    I40E_FLAG_MSIX_ENABLED;
11885 
11886 	/* Set default ITR */
11887 	pf->rx_itr_default = I40E_ITR_RX_DEF;
11888 	pf->tx_itr_default = I40E_ITR_TX_DEF;
11889 
11890 	/* Depending on PF configurations, it is possible that the RSS
11891 	 * maximum might end up larger than the available queues
11892 	 */
11893 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
11894 	pf->alloc_rss_size = 1;
11895 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
11896 	pf->rss_size_max = min_t(int, pf->rss_size_max,
11897 				 pf->hw.func_caps.num_tx_qp);
11898 	if (pf->hw.func_caps.rss) {
11899 		pf->flags |= I40E_FLAG_RSS_ENABLED;
11900 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
11901 					   num_online_cpus());
11902 	}
11903 
11904 	/* MFP mode enabled */
11905 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
11906 		pf->flags |= I40E_FLAG_MFP_ENABLED;
11907 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
11908 		if (i40e_get_partition_bw_setting(pf)) {
11909 			dev_warn(&pf->pdev->dev,
11910 				 "Could not get partition bw settings\n");
11911 		} else {
11912 			dev_info(&pf->pdev->dev,
11913 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
11914 				 pf->min_bw, pf->max_bw);
11915 
11916 			/* nudge the Tx scheduler */
11917 			i40e_set_partition_bw_setting(pf);
11918 		}
11919 	}
11920 
11921 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
11922 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
11923 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
11924 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
11925 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
11926 		    pf->hw.num_partitions > 1)
11927 			dev_info(&pf->pdev->dev,
11928 				 "Flow Director Sideband mode Disabled in MFP mode\n");
11929 		else
11930 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
11931 		pf->fdir_pf_filter_count =
11932 				 pf->hw.func_caps.fd_filters_guaranteed;
11933 		pf->hw.fdir_shared_filter_count =
11934 				 pf->hw.func_caps.fd_filters_best_effort;
11935 	}
11936 
11937 	if (pf->hw.mac.type == I40E_MAC_X722) {
11938 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
11939 				    I40E_HW_128_QP_RSS_CAPABLE |
11940 				    I40E_HW_ATR_EVICT_CAPABLE |
11941 				    I40E_HW_WB_ON_ITR_CAPABLE |
11942 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
11943 				    I40E_HW_NO_PCI_LINK_CHECK |
11944 				    I40E_HW_USE_SET_LLDP_MIB |
11945 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
11946 				    I40E_HW_PTP_L4_CAPABLE |
11947 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
11948 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
11949 
11950 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
11951 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
11952 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
11953 			dev_warn(&pf->pdev->dev,
11954 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
11955 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
11956 		}
11957 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
11958 		   ((pf->hw.aq.api_maj_ver == 1) &&
11959 		    (pf->hw.aq.api_min_ver > 4))) {
11960 		/* Supported in FW API version higher than 1.4 */
11961 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
11962 	}
11963 
11964 	/* Enable HW ATR eviction if possible */
11965 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
11966 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
11967 
11968 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11969 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
11970 	    (pf->hw.aq.fw_maj_ver < 4))) {
11971 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
11972 		/* No DCB support  for FW < v4.33 */
11973 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
11974 	}
11975 
11976 	/* Disable FW LLDP if FW < v4.3 */
11977 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11978 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
11979 	    (pf->hw.aq.fw_maj_ver < 4)))
11980 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
11981 
11982 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
11983 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11984 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
11985 	    (pf->hw.aq.fw_maj_ver >= 5)))
11986 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
11987 
11988 	/* Enable PTP L4 if FW > v6.0 */
11989 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
11990 	    pf->hw.aq.fw_maj_ver >= 6)
11991 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
11992 
11993 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
11994 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
11995 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
11996 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
11997 	}
11998 
11999 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12000 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12001 		/* IWARP needs one extra vector for CQP just like MISC.*/
12002 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12003 	}
12004 	/* Stopping FW LLDP engine is supported on XL710 and X722
12005 	 * starting from FW versions determined in i40e_init_adminq.
12006 	 * Stopping the FW LLDP engine is not supported on XL710
12007 	 * if NPAR is functioning so unset this hw flag in this case.
12008 	 */
12009 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12010 	    pf->hw.func_caps.npar_enable &&
12011 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12012 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12013 
12014 #ifdef CONFIG_PCI_IOV
12015 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12016 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12017 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12018 		pf->num_req_vfs = min_t(int,
12019 					pf->hw.func_caps.num_vfs,
12020 					I40E_MAX_VF_COUNT);
12021 	}
12022 #endif /* CONFIG_PCI_IOV */
12023 	pf->eeprom_version = 0xDEAD;
12024 	pf->lan_veb = I40E_NO_VEB;
12025 	pf->lan_vsi = I40E_NO_VSI;
12026 
12027 	/* By default FW has this off for performance reasons */
12028 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12029 
12030 	/* set up queue assignment tracking */
12031 	size = sizeof(struct i40e_lump_tracking)
12032 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12033 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12034 	if (!pf->qp_pile) {
12035 		err = -ENOMEM;
12036 		goto sw_init_done;
12037 	}
12038 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12039 	pf->qp_pile->search_hint = 0;
12040 
12041 	pf->tx_timeout_recovery_level = 1;
12042 
12043 	mutex_init(&pf->switch_mutex);
12044 
12045 sw_init_done:
12046 	return err;
12047 }
12048 
12049 /**
12050  * i40e_set_ntuple - set the ntuple feature flag and take action
12051  * @pf: board private structure to initialize
12052  * @features: the feature set that the stack is suggesting
12053  *
12054  * returns a bool to indicate if reset needs to happen
12055  **/
12056 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12057 {
12058 	bool need_reset = false;
12059 
12060 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12061 	 * the state changed, we need to reset.
12062 	 */
12063 	if (features & NETIF_F_NTUPLE) {
12064 		/* Enable filters and mark for reset */
12065 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12066 			need_reset = true;
12067 		/* enable FD_SB only if there is MSI-X vector and no cloud
12068 		 * filters exist
12069 		 */
12070 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12071 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12072 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12073 		}
12074 	} else {
12075 		/* turn off filters, mark for reset and clear SW filter list */
12076 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12077 			need_reset = true;
12078 			i40e_fdir_filter_exit(pf);
12079 		}
12080 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12081 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12082 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12083 
12084 		/* reset fd counters */
12085 		pf->fd_add_err = 0;
12086 		pf->fd_atr_cnt = 0;
12087 		/* if ATR was auto disabled it can be re-enabled. */
12088 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12089 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12090 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12091 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12092 	}
12093 	return need_reset;
12094 }
12095 
12096 /**
12097  * i40e_clear_rss_lut - clear the rx hash lookup table
12098  * @vsi: the VSI being configured
12099  **/
12100 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12101 {
12102 	struct i40e_pf *pf = vsi->back;
12103 	struct i40e_hw *hw = &pf->hw;
12104 	u16 vf_id = vsi->vf_id;
12105 	u8 i;
12106 
12107 	if (vsi->type == I40E_VSI_MAIN) {
12108 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12109 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12110 	} else if (vsi->type == I40E_VSI_SRIOV) {
12111 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12112 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12113 	} else {
12114 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12115 	}
12116 }
12117 
12118 /**
12119  * i40e_set_features - set the netdev feature flags
12120  * @netdev: ptr to the netdev being adjusted
12121  * @features: the feature set that the stack is suggesting
12122  * Note: expects to be called while under rtnl_lock()
12123  **/
12124 static int i40e_set_features(struct net_device *netdev,
12125 			     netdev_features_t features)
12126 {
12127 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12128 	struct i40e_vsi *vsi = np->vsi;
12129 	struct i40e_pf *pf = vsi->back;
12130 	bool need_reset;
12131 
12132 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12133 		i40e_pf_config_rss(pf);
12134 	else if (!(features & NETIF_F_RXHASH) &&
12135 		 netdev->features & NETIF_F_RXHASH)
12136 		i40e_clear_rss_lut(vsi);
12137 
12138 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12139 		i40e_vlan_stripping_enable(vsi);
12140 	else
12141 		i40e_vlan_stripping_disable(vsi);
12142 
12143 	if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12144 		dev_err(&pf->pdev->dev,
12145 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12146 		return -EINVAL;
12147 	}
12148 
12149 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12150 		i40e_del_all_macvlans(vsi);
12151 
12152 	need_reset = i40e_set_ntuple(pf, features);
12153 
12154 	if (need_reset)
12155 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12156 
12157 	return 0;
12158 }
12159 
12160 /**
12161  * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
12162  * @pf: board private structure
12163  * @port: The UDP port to look up
12164  *
12165  * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
12166  **/
12167 static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
12168 {
12169 	u8 i;
12170 
12171 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
12172 		/* Do not report ports with pending deletions as
12173 		 * being available.
12174 		 */
12175 		if (!port && (pf->pending_udp_bitmap & BIT_ULL(i)))
12176 			continue;
12177 		if (pf->udp_ports[i].port == port)
12178 			return i;
12179 	}
12180 
12181 	return i;
12182 }
12183 
12184 /**
12185  * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
12186  * @netdev: This physical port's netdev
12187  * @ti: Tunnel endpoint information
12188  **/
12189 static void i40e_udp_tunnel_add(struct net_device *netdev,
12190 				struct udp_tunnel_info *ti)
12191 {
12192 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12193 	struct i40e_vsi *vsi = np->vsi;
12194 	struct i40e_pf *pf = vsi->back;
12195 	u16 port = ntohs(ti->port);
12196 	u8 next_idx;
12197 	u8 idx;
12198 
12199 	idx = i40e_get_udp_port_idx(pf, port);
12200 
12201 	/* Check if port already exists */
12202 	if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
12203 		netdev_info(netdev, "port %d already offloaded\n", port);
12204 		return;
12205 	}
12206 
12207 	/* Now check if there is space to add the new port */
12208 	next_idx = i40e_get_udp_port_idx(pf, 0);
12209 
12210 	if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
12211 		netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
12212 			    port);
12213 		return;
12214 	}
12215 
12216 	switch (ti->type) {
12217 	case UDP_TUNNEL_TYPE_VXLAN:
12218 		pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
12219 		break;
12220 	case UDP_TUNNEL_TYPE_GENEVE:
12221 		if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
12222 			return;
12223 		pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
12224 		break;
12225 	default:
12226 		return;
12227 	}
12228 
12229 	/* New port: add it and mark its index in the bitmap */
12230 	pf->udp_ports[next_idx].port = port;
12231 	pf->udp_ports[next_idx].filter_index = I40E_UDP_PORT_INDEX_UNUSED;
12232 	pf->pending_udp_bitmap |= BIT_ULL(next_idx);
12233 	set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
12234 }
12235 
12236 /**
12237  * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
12238  * @netdev: This physical port's netdev
12239  * @ti: Tunnel endpoint information
12240  **/
12241 static void i40e_udp_tunnel_del(struct net_device *netdev,
12242 				struct udp_tunnel_info *ti)
12243 {
12244 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12245 	struct i40e_vsi *vsi = np->vsi;
12246 	struct i40e_pf *pf = vsi->back;
12247 	u16 port = ntohs(ti->port);
12248 	u8 idx;
12249 
12250 	idx = i40e_get_udp_port_idx(pf, port);
12251 
12252 	/* Check if port already exists */
12253 	if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
12254 		goto not_found;
12255 
12256 	switch (ti->type) {
12257 	case UDP_TUNNEL_TYPE_VXLAN:
12258 		if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
12259 			goto not_found;
12260 		break;
12261 	case UDP_TUNNEL_TYPE_GENEVE:
12262 		if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
12263 			goto not_found;
12264 		break;
12265 	default:
12266 		goto not_found;
12267 	}
12268 
12269 	/* if port exists, set it to 0 (mark for deletion)
12270 	 * and make it pending
12271 	 */
12272 	pf->udp_ports[idx].port = 0;
12273 
12274 	/* Toggle pending bit instead of setting it. This way if we are
12275 	 * deleting a port that has yet to be added we just clear the pending
12276 	 * bit and don't have to worry about it.
12277 	 */
12278 	pf->pending_udp_bitmap ^= BIT_ULL(idx);
12279 	set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
12280 
12281 	return;
12282 not_found:
12283 	netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
12284 		    port);
12285 }
12286 
12287 static int i40e_get_phys_port_id(struct net_device *netdev,
12288 				 struct netdev_phys_item_id *ppid)
12289 {
12290 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12291 	struct i40e_pf *pf = np->vsi->back;
12292 	struct i40e_hw *hw = &pf->hw;
12293 
12294 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
12295 		return -EOPNOTSUPP;
12296 
12297 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
12298 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
12299 
12300 	return 0;
12301 }
12302 
12303 /**
12304  * i40e_ndo_fdb_add - add an entry to the hardware database
12305  * @ndm: the input from the stack
12306  * @tb: pointer to array of nladdr (unused)
12307  * @dev: the net device pointer
12308  * @addr: the MAC address entry being added
12309  * @vid: VLAN ID
12310  * @flags: instructions from stack about fdb operation
12311  */
12312 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
12313 			    struct net_device *dev,
12314 			    const unsigned char *addr, u16 vid,
12315 			    u16 flags,
12316 			    struct netlink_ext_ack *extack)
12317 {
12318 	struct i40e_netdev_priv *np = netdev_priv(dev);
12319 	struct i40e_pf *pf = np->vsi->back;
12320 	int err = 0;
12321 
12322 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
12323 		return -EOPNOTSUPP;
12324 
12325 	if (vid) {
12326 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
12327 		return -EINVAL;
12328 	}
12329 
12330 	/* Hardware does not support aging addresses so if a
12331 	 * ndm_state is given only allow permanent addresses
12332 	 */
12333 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
12334 		netdev_info(dev, "FDB only supports static addresses\n");
12335 		return -EINVAL;
12336 	}
12337 
12338 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
12339 		err = dev_uc_add_excl(dev, addr);
12340 	else if (is_multicast_ether_addr(addr))
12341 		err = dev_mc_add_excl(dev, addr);
12342 	else
12343 		err = -EINVAL;
12344 
12345 	/* Only return duplicate errors if NLM_F_EXCL is set */
12346 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
12347 		err = 0;
12348 
12349 	return err;
12350 }
12351 
12352 /**
12353  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
12354  * @dev: the netdev being configured
12355  * @nlh: RTNL message
12356  * @flags: bridge flags
12357  * @extack: netlink extended ack
12358  *
12359  * Inserts a new hardware bridge if not already created and
12360  * enables the bridging mode requested (VEB or VEPA). If the
12361  * hardware bridge has already been inserted and the request
12362  * is to change the mode then that requires a PF reset to
12363  * allow rebuild of the components with required hardware
12364  * bridge mode enabled.
12365  *
12366  * Note: expects to be called while under rtnl_lock()
12367  **/
12368 static int i40e_ndo_bridge_setlink(struct net_device *dev,
12369 				   struct nlmsghdr *nlh,
12370 				   u16 flags,
12371 				   struct netlink_ext_ack *extack)
12372 {
12373 	struct i40e_netdev_priv *np = netdev_priv(dev);
12374 	struct i40e_vsi *vsi = np->vsi;
12375 	struct i40e_pf *pf = vsi->back;
12376 	struct i40e_veb *veb = NULL;
12377 	struct nlattr *attr, *br_spec;
12378 	int i, rem;
12379 
12380 	/* Only for PF VSI for now */
12381 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12382 		return -EOPNOTSUPP;
12383 
12384 	/* Find the HW bridge for PF VSI */
12385 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12386 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12387 			veb = pf->veb[i];
12388 	}
12389 
12390 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
12391 
12392 	nla_for_each_nested(attr, br_spec, rem) {
12393 		__u16 mode;
12394 
12395 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
12396 			continue;
12397 
12398 		mode = nla_get_u16(attr);
12399 		if ((mode != BRIDGE_MODE_VEPA) &&
12400 		    (mode != BRIDGE_MODE_VEB))
12401 			return -EINVAL;
12402 
12403 		/* Insert a new HW bridge */
12404 		if (!veb) {
12405 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12406 					     vsi->tc_config.enabled_tc);
12407 			if (veb) {
12408 				veb->bridge_mode = mode;
12409 				i40e_config_bridge_mode(veb);
12410 			} else {
12411 				/* No Bridge HW offload available */
12412 				return -ENOENT;
12413 			}
12414 			break;
12415 		} else if (mode != veb->bridge_mode) {
12416 			/* Existing HW bridge but different mode needs reset */
12417 			veb->bridge_mode = mode;
12418 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
12419 			if (mode == BRIDGE_MODE_VEB)
12420 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
12421 			else
12422 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12423 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12424 			break;
12425 		}
12426 	}
12427 
12428 	return 0;
12429 }
12430 
12431 /**
12432  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
12433  * @skb: skb buff
12434  * @pid: process id
12435  * @seq: RTNL message seq #
12436  * @dev: the netdev being configured
12437  * @filter_mask: unused
12438  * @nlflags: netlink flags passed in
12439  *
12440  * Return the mode in which the hardware bridge is operating in
12441  * i.e VEB or VEPA.
12442  **/
12443 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
12444 				   struct net_device *dev,
12445 				   u32 __always_unused filter_mask,
12446 				   int nlflags)
12447 {
12448 	struct i40e_netdev_priv *np = netdev_priv(dev);
12449 	struct i40e_vsi *vsi = np->vsi;
12450 	struct i40e_pf *pf = vsi->back;
12451 	struct i40e_veb *veb = NULL;
12452 	int i;
12453 
12454 	/* Only for PF VSI for now */
12455 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12456 		return -EOPNOTSUPP;
12457 
12458 	/* Find the HW bridge for the PF VSI */
12459 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12460 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12461 			veb = pf->veb[i];
12462 	}
12463 
12464 	if (!veb)
12465 		return 0;
12466 
12467 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
12468 				       0, 0, nlflags, filter_mask, NULL);
12469 }
12470 
12471 /**
12472  * i40e_features_check - Validate encapsulated packet conforms to limits
12473  * @skb: skb buff
12474  * @dev: This physical port's netdev
12475  * @features: Offload features that the stack believes apply
12476  **/
12477 static netdev_features_t i40e_features_check(struct sk_buff *skb,
12478 					     struct net_device *dev,
12479 					     netdev_features_t features)
12480 {
12481 	size_t len;
12482 
12483 	/* No point in doing any of this if neither checksum nor GSO are
12484 	 * being requested for this frame.  We can rule out both by just
12485 	 * checking for CHECKSUM_PARTIAL
12486 	 */
12487 	if (skb->ip_summed != CHECKSUM_PARTIAL)
12488 		return features;
12489 
12490 	/* We cannot support GSO if the MSS is going to be less than
12491 	 * 64 bytes.  If it is then we need to drop support for GSO.
12492 	 */
12493 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
12494 		features &= ~NETIF_F_GSO_MASK;
12495 
12496 	/* MACLEN can support at most 63 words */
12497 	len = skb_network_header(skb) - skb->data;
12498 	if (len & ~(63 * 2))
12499 		goto out_err;
12500 
12501 	/* IPLEN and EIPLEN can support at most 127 dwords */
12502 	len = skb_transport_header(skb) - skb_network_header(skb);
12503 	if (len & ~(127 * 4))
12504 		goto out_err;
12505 
12506 	if (skb->encapsulation) {
12507 		/* L4TUNLEN can support 127 words */
12508 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
12509 		if (len & ~(127 * 2))
12510 			goto out_err;
12511 
12512 		/* IPLEN can support at most 127 dwords */
12513 		len = skb_inner_transport_header(skb) -
12514 		      skb_inner_network_header(skb);
12515 		if (len & ~(127 * 4))
12516 			goto out_err;
12517 	}
12518 
12519 	/* No need to validate L4LEN as TCP is the only protocol with a
12520 	 * a flexible value and we support all possible values supported
12521 	 * by TCP, which is at most 15 dwords
12522 	 */
12523 
12524 	return features;
12525 out_err:
12526 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
12527 }
12528 
12529 /**
12530  * i40e_xdp_setup - add/remove an XDP program
12531  * @vsi: VSI to changed
12532  * @prog: XDP program
12533  **/
12534 static int i40e_xdp_setup(struct i40e_vsi *vsi,
12535 			  struct bpf_prog *prog)
12536 {
12537 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
12538 	struct i40e_pf *pf = vsi->back;
12539 	struct bpf_prog *old_prog;
12540 	bool need_reset;
12541 	int i;
12542 
12543 	/* Don't allow frames that span over multiple buffers */
12544 	if (frame_size > vsi->rx_buf_len)
12545 		return -EINVAL;
12546 
12547 	if (!i40e_enabled_xdp_vsi(vsi) && !prog)
12548 		return 0;
12549 
12550 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
12551 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
12552 
12553 	if (need_reset)
12554 		i40e_prep_for_reset(pf, true);
12555 
12556 	old_prog = xchg(&vsi->xdp_prog, prog);
12557 
12558 	if (need_reset)
12559 		i40e_reset_and_rebuild(pf, true, true);
12560 
12561 	for (i = 0; i < vsi->num_queue_pairs; i++)
12562 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
12563 
12564 	if (old_prog)
12565 		bpf_prog_put(old_prog);
12566 
12567 	/* Kick start the NAPI context if there is an AF_XDP socket open
12568 	 * on that queue id. This so that receiving will start.
12569 	 */
12570 	if (need_reset && prog)
12571 		for (i = 0; i < vsi->num_queue_pairs; i++)
12572 			if (vsi->xdp_rings[i]->xsk_umem)
12573 				(void)i40e_xsk_wakeup(vsi->netdev, i,
12574 						      XDP_WAKEUP_RX);
12575 
12576 	return 0;
12577 }
12578 
12579 /**
12580  * i40e_enter_busy_conf - Enters busy config state
12581  * @vsi: vsi
12582  *
12583  * Returns 0 on success, <0 for failure.
12584  **/
12585 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
12586 {
12587 	struct i40e_pf *pf = vsi->back;
12588 	int timeout = 50;
12589 
12590 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
12591 		timeout--;
12592 		if (!timeout)
12593 			return -EBUSY;
12594 		usleep_range(1000, 2000);
12595 	}
12596 
12597 	return 0;
12598 }
12599 
12600 /**
12601  * i40e_exit_busy_conf - Exits busy config state
12602  * @vsi: vsi
12603  **/
12604 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
12605 {
12606 	struct i40e_pf *pf = vsi->back;
12607 
12608 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
12609 }
12610 
12611 /**
12612  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
12613  * @vsi: vsi
12614  * @queue_pair: queue pair
12615  **/
12616 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
12617 {
12618 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
12619 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
12620 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
12621 	       sizeof(vsi->tx_rings[queue_pair]->stats));
12622 	if (i40e_enabled_xdp_vsi(vsi)) {
12623 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
12624 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
12625 	}
12626 }
12627 
12628 /**
12629  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
12630  * @vsi: vsi
12631  * @queue_pair: queue pair
12632  **/
12633 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
12634 {
12635 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
12636 	if (i40e_enabled_xdp_vsi(vsi)) {
12637 		/* Make sure that in-progress ndo_xdp_xmit calls are
12638 		 * completed.
12639 		 */
12640 		synchronize_rcu();
12641 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
12642 	}
12643 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
12644 }
12645 
12646 /**
12647  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
12648  * @vsi: vsi
12649  * @queue_pair: queue pair
12650  * @enable: true for enable, false for disable
12651  **/
12652 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
12653 					bool enable)
12654 {
12655 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12656 	struct i40e_q_vector *q_vector = rxr->q_vector;
12657 
12658 	if (!vsi->netdev)
12659 		return;
12660 
12661 	/* All rings in a qp belong to the same qvector. */
12662 	if (q_vector->rx.ring || q_vector->tx.ring) {
12663 		if (enable)
12664 			napi_enable(&q_vector->napi);
12665 		else
12666 			napi_disable(&q_vector->napi);
12667 	}
12668 }
12669 
12670 /**
12671  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
12672  * @vsi: vsi
12673  * @queue_pair: queue pair
12674  * @enable: true for enable, false for disable
12675  *
12676  * Returns 0 on success, <0 on failure.
12677  **/
12678 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
12679 					bool enable)
12680 {
12681 	struct i40e_pf *pf = vsi->back;
12682 	int pf_q, ret = 0;
12683 
12684 	pf_q = vsi->base_queue + queue_pair;
12685 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
12686 				     false /*is xdp*/, enable);
12687 	if (ret) {
12688 		dev_info(&pf->pdev->dev,
12689 			 "VSI seid %d Tx ring %d %sable timeout\n",
12690 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12691 		return ret;
12692 	}
12693 
12694 	i40e_control_rx_q(pf, pf_q, enable);
12695 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
12696 	if (ret) {
12697 		dev_info(&pf->pdev->dev,
12698 			 "VSI seid %d Rx ring %d %sable timeout\n",
12699 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12700 		return ret;
12701 	}
12702 
12703 	/* Due to HW errata, on Rx disable only, the register can
12704 	 * indicate done before it really is. Needs 50ms to be sure
12705 	 */
12706 	if (!enable)
12707 		mdelay(50);
12708 
12709 	if (!i40e_enabled_xdp_vsi(vsi))
12710 		return ret;
12711 
12712 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
12713 				     pf_q + vsi->alloc_queue_pairs,
12714 				     true /*is xdp*/, enable);
12715 	if (ret) {
12716 		dev_info(&pf->pdev->dev,
12717 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
12718 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12719 	}
12720 
12721 	return ret;
12722 }
12723 
12724 /**
12725  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
12726  * @vsi: vsi
12727  * @queue_pair: queue_pair
12728  **/
12729 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
12730 {
12731 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12732 	struct i40e_pf *pf = vsi->back;
12733 	struct i40e_hw *hw = &pf->hw;
12734 
12735 	/* All rings in a qp belong to the same qvector. */
12736 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
12737 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
12738 	else
12739 		i40e_irq_dynamic_enable_icr0(pf);
12740 
12741 	i40e_flush(hw);
12742 }
12743 
12744 /**
12745  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
12746  * @vsi: vsi
12747  * @queue_pair: queue_pair
12748  **/
12749 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
12750 {
12751 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12752 	struct i40e_pf *pf = vsi->back;
12753 	struct i40e_hw *hw = &pf->hw;
12754 
12755 	/* For simplicity, instead of removing the qp interrupt causes
12756 	 * from the interrupt linked list, we simply disable the interrupt, and
12757 	 * leave the list intact.
12758 	 *
12759 	 * All rings in a qp belong to the same qvector.
12760 	 */
12761 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12762 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
12763 
12764 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
12765 		i40e_flush(hw);
12766 		synchronize_irq(pf->msix_entries[intpf].vector);
12767 	} else {
12768 		/* Legacy and MSI mode - this stops all interrupt handling */
12769 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
12770 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
12771 		i40e_flush(hw);
12772 		synchronize_irq(pf->pdev->irq);
12773 	}
12774 }
12775 
12776 /**
12777  * i40e_queue_pair_disable - Disables a queue pair
12778  * @vsi: vsi
12779  * @queue_pair: queue pair
12780  *
12781  * Returns 0 on success, <0 on failure.
12782  **/
12783 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
12784 {
12785 	int err;
12786 
12787 	err = i40e_enter_busy_conf(vsi);
12788 	if (err)
12789 		return err;
12790 
12791 	i40e_queue_pair_disable_irq(vsi, queue_pair);
12792 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
12793 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
12794 	i40e_queue_pair_clean_rings(vsi, queue_pair);
12795 	i40e_queue_pair_reset_stats(vsi, queue_pair);
12796 
12797 	return err;
12798 }
12799 
12800 /**
12801  * i40e_queue_pair_enable - Enables a queue pair
12802  * @vsi: vsi
12803  * @queue_pair: queue pair
12804  *
12805  * Returns 0 on success, <0 on failure.
12806  **/
12807 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
12808 {
12809 	int err;
12810 
12811 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
12812 	if (err)
12813 		return err;
12814 
12815 	if (i40e_enabled_xdp_vsi(vsi)) {
12816 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
12817 		if (err)
12818 			return err;
12819 	}
12820 
12821 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
12822 	if (err)
12823 		return err;
12824 
12825 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
12826 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
12827 	i40e_queue_pair_enable_irq(vsi, queue_pair);
12828 
12829 	i40e_exit_busy_conf(vsi);
12830 
12831 	return err;
12832 }
12833 
12834 /**
12835  * i40e_xdp - implements ndo_bpf for i40e
12836  * @dev: netdevice
12837  * @xdp: XDP command
12838  **/
12839 static int i40e_xdp(struct net_device *dev,
12840 		    struct netdev_bpf *xdp)
12841 {
12842 	struct i40e_netdev_priv *np = netdev_priv(dev);
12843 	struct i40e_vsi *vsi = np->vsi;
12844 
12845 	if (vsi->type != I40E_VSI_MAIN)
12846 		return -EINVAL;
12847 
12848 	switch (xdp->command) {
12849 	case XDP_SETUP_PROG:
12850 		return i40e_xdp_setup(vsi, xdp->prog);
12851 	case XDP_QUERY_PROG:
12852 		xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
12853 		return 0;
12854 	case XDP_SETUP_XSK_UMEM:
12855 		return i40e_xsk_umem_setup(vsi, xdp->xsk.umem,
12856 					   xdp->xsk.queue_id);
12857 	default:
12858 		return -EINVAL;
12859 	}
12860 }
12861 
12862 static const struct net_device_ops i40e_netdev_ops = {
12863 	.ndo_open		= i40e_open,
12864 	.ndo_stop		= i40e_close,
12865 	.ndo_start_xmit		= i40e_lan_xmit_frame,
12866 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
12867 	.ndo_set_rx_mode	= i40e_set_rx_mode,
12868 	.ndo_validate_addr	= eth_validate_addr,
12869 	.ndo_set_mac_address	= i40e_set_mac,
12870 	.ndo_change_mtu		= i40e_change_mtu,
12871 	.ndo_do_ioctl		= i40e_ioctl,
12872 	.ndo_tx_timeout		= i40e_tx_timeout,
12873 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
12874 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
12875 #ifdef CONFIG_NET_POLL_CONTROLLER
12876 	.ndo_poll_controller	= i40e_netpoll,
12877 #endif
12878 	.ndo_setup_tc		= __i40e_setup_tc,
12879 	.ndo_set_features	= i40e_set_features,
12880 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
12881 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
12882 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
12883 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
12884 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
12885 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
12886 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
12887 	.ndo_udp_tunnel_add	= i40e_udp_tunnel_add,
12888 	.ndo_udp_tunnel_del	= i40e_udp_tunnel_del,
12889 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
12890 	.ndo_fdb_add		= i40e_ndo_fdb_add,
12891 	.ndo_features_check	= i40e_features_check,
12892 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
12893 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
12894 	.ndo_bpf		= i40e_xdp,
12895 	.ndo_xdp_xmit		= i40e_xdp_xmit,
12896 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
12897 	.ndo_dfwd_add_station	= i40e_fwd_add,
12898 	.ndo_dfwd_del_station	= i40e_fwd_del,
12899 };
12900 
12901 /**
12902  * i40e_config_netdev - Setup the netdev flags
12903  * @vsi: the VSI being configured
12904  *
12905  * Returns 0 on success, negative value on failure
12906  **/
12907 static int i40e_config_netdev(struct i40e_vsi *vsi)
12908 {
12909 	struct i40e_pf *pf = vsi->back;
12910 	struct i40e_hw *hw = &pf->hw;
12911 	struct i40e_netdev_priv *np;
12912 	struct net_device *netdev;
12913 	u8 broadcast[ETH_ALEN];
12914 	u8 mac_addr[ETH_ALEN];
12915 	int etherdev_size;
12916 	netdev_features_t hw_enc_features;
12917 	netdev_features_t hw_features;
12918 
12919 	etherdev_size = sizeof(struct i40e_netdev_priv);
12920 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
12921 	if (!netdev)
12922 		return -ENOMEM;
12923 
12924 	vsi->netdev = netdev;
12925 	np = netdev_priv(netdev);
12926 	np->vsi = vsi;
12927 
12928 	hw_enc_features = NETIF_F_SG			|
12929 			  NETIF_F_IP_CSUM		|
12930 			  NETIF_F_IPV6_CSUM		|
12931 			  NETIF_F_HIGHDMA		|
12932 			  NETIF_F_SOFT_FEATURES		|
12933 			  NETIF_F_TSO			|
12934 			  NETIF_F_TSO_ECN		|
12935 			  NETIF_F_TSO6			|
12936 			  NETIF_F_GSO_GRE		|
12937 			  NETIF_F_GSO_GRE_CSUM		|
12938 			  NETIF_F_GSO_PARTIAL		|
12939 			  NETIF_F_GSO_IPXIP4		|
12940 			  NETIF_F_GSO_IPXIP6		|
12941 			  NETIF_F_GSO_UDP_TUNNEL	|
12942 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
12943 			  NETIF_F_SCTP_CRC		|
12944 			  NETIF_F_RXHASH		|
12945 			  NETIF_F_RXCSUM		|
12946 			  0;
12947 
12948 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
12949 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
12950 
12951 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
12952 
12953 	netdev->hw_enc_features |= hw_enc_features;
12954 
12955 	/* record features VLANs can make use of */
12956 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
12957 
12958 	/* enable macvlan offloads */
12959 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
12960 
12961 	hw_features = hw_enc_features		|
12962 		      NETIF_F_HW_VLAN_CTAG_TX	|
12963 		      NETIF_F_HW_VLAN_CTAG_RX;
12964 
12965 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
12966 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
12967 
12968 	netdev->hw_features |= hw_features;
12969 
12970 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
12971 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
12972 
12973 	if (vsi->type == I40E_VSI_MAIN) {
12974 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
12975 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
12976 		/* The following steps are necessary for two reasons. First,
12977 		 * some older NVM configurations load a default MAC-VLAN
12978 		 * filter that will accept any tagged packet, and we want to
12979 		 * replace this with a normal filter. Additionally, it is
12980 		 * possible our MAC address was provided by the platform using
12981 		 * Open Firmware or similar.
12982 		 *
12983 		 * Thus, we need to remove the default filter and install one
12984 		 * specific to the MAC address.
12985 		 */
12986 		i40e_rm_default_mac_filter(vsi, mac_addr);
12987 		spin_lock_bh(&vsi->mac_filter_hash_lock);
12988 		i40e_add_mac_filter(vsi, mac_addr);
12989 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
12990 	} else {
12991 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
12992 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
12993 		 * the end, which is 4 bytes long, so force truncation of the
12994 		 * original name by IFNAMSIZ - 4
12995 		 */
12996 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
12997 			 IFNAMSIZ - 4,
12998 			 pf->vsi[pf->lan_vsi]->netdev->name);
12999 		eth_random_addr(mac_addr);
13000 
13001 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13002 		i40e_add_mac_filter(vsi, mac_addr);
13003 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13004 	}
13005 
13006 	/* Add the broadcast filter so that we initially will receive
13007 	 * broadcast packets. Note that when a new VLAN is first added the
13008 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13009 	 * specific filters as part of transitioning into "vlan" operation.
13010 	 * When more VLANs are added, the driver will copy each existing MAC
13011 	 * filter and add it for the new VLAN.
13012 	 *
13013 	 * Broadcast filters are handled specially by
13014 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13015 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13016 	 * filter. The subtask will update the correct broadcast promiscuous
13017 	 * bits as VLANs become active or inactive.
13018 	 */
13019 	eth_broadcast_addr(broadcast);
13020 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13021 	i40e_add_mac_filter(vsi, broadcast);
13022 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13023 
13024 	ether_addr_copy(netdev->dev_addr, mac_addr);
13025 	ether_addr_copy(netdev->perm_addr, mac_addr);
13026 
13027 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13028 	netdev->neigh_priv_len = sizeof(u32) * 4;
13029 
13030 	netdev->priv_flags |= IFF_UNICAST_FLT;
13031 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13032 	/* Setup netdev TC information */
13033 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13034 
13035 	netdev->netdev_ops = &i40e_netdev_ops;
13036 	netdev->watchdog_timeo = 5 * HZ;
13037 	i40e_set_ethtool_ops(netdev);
13038 
13039 	/* MTU range: 68 - 9706 */
13040 	netdev->min_mtu = ETH_MIN_MTU;
13041 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13042 
13043 	return 0;
13044 }
13045 
13046 /**
13047  * i40e_vsi_delete - Delete a VSI from the switch
13048  * @vsi: the VSI being removed
13049  *
13050  * Returns 0 on success, negative value on failure
13051  **/
13052 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13053 {
13054 	/* remove default VSI is not allowed */
13055 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13056 		return;
13057 
13058 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13059 }
13060 
13061 /**
13062  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13063  * @vsi: the VSI being queried
13064  *
13065  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13066  **/
13067 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13068 {
13069 	struct i40e_veb *veb;
13070 	struct i40e_pf *pf = vsi->back;
13071 
13072 	/* Uplink is not a bridge so default to VEB */
13073 	if (vsi->veb_idx >= I40E_MAX_VEB)
13074 		return 1;
13075 
13076 	veb = pf->veb[vsi->veb_idx];
13077 	if (!veb) {
13078 		dev_info(&pf->pdev->dev,
13079 			 "There is no veb associated with the bridge\n");
13080 		return -ENOENT;
13081 	}
13082 
13083 	/* Uplink is a bridge in VEPA mode */
13084 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13085 		return 0;
13086 	} else {
13087 		/* Uplink is a bridge in VEB mode */
13088 		return 1;
13089 	}
13090 
13091 	/* VEPA is now default bridge, so return 0 */
13092 	return 0;
13093 }
13094 
13095 /**
13096  * i40e_add_vsi - Add a VSI to the switch
13097  * @vsi: the VSI being configured
13098  *
13099  * This initializes a VSI context depending on the VSI type to be added and
13100  * passes it down to the add_vsi aq command.
13101  **/
13102 static int i40e_add_vsi(struct i40e_vsi *vsi)
13103 {
13104 	int ret = -ENODEV;
13105 	struct i40e_pf *pf = vsi->back;
13106 	struct i40e_hw *hw = &pf->hw;
13107 	struct i40e_vsi_context ctxt;
13108 	struct i40e_mac_filter *f;
13109 	struct hlist_node *h;
13110 	int bkt;
13111 
13112 	u8 enabled_tc = 0x1; /* TC0 enabled */
13113 	int f_count = 0;
13114 
13115 	memset(&ctxt, 0, sizeof(ctxt));
13116 	switch (vsi->type) {
13117 	case I40E_VSI_MAIN:
13118 		/* The PF's main VSI is already setup as part of the
13119 		 * device initialization, so we'll not bother with
13120 		 * the add_vsi call, but we will retrieve the current
13121 		 * VSI context.
13122 		 */
13123 		ctxt.seid = pf->main_vsi_seid;
13124 		ctxt.pf_num = pf->hw.pf_id;
13125 		ctxt.vf_num = 0;
13126 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13127 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13128 		if (ret) {
13129 			dev_info(&pf->pdev->dev,
13130 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13131 				 i40e_stat_str(&pf->hw, ret),
13132 				 i40e_aq_str(&pf->hw,
13133 					     pf->hw.aq.asq_last_status));
13134 			return -ENOENT;
13135 		}
13136 		vsi->info = ctxt.info;
13137 		vsi->info.valid_sections = 0;
13138 
13139 		vsi->seid = ctxt.seid;
13140 		vsi->id = ctxt.vsi_number;
13141 
13142 		enabled_tc = i40e_pf_get_tc_map(pf);
13143 
13144 		/* Source pruning is enabled by default, so the flag is
13145 		 * negative logic - if it's set, we need to fiddle with
13146 		 * the VSI to disable source pruning.
13147 		 */
13148 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13149 			memset(&ctxt, 0, sizeof(ctxt));
13150 			ctxt.seid = pf->main_vsi_seid;
13151 			ctxt.pf_num = pf->hw.pf_id;
13152 			ctxt.vf_num = 0;
13153 			ctxt.info.valid_sections |=
13154 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13155 			ctxt.info.switch_id =
13156 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13157 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13158 			if (ret) {
13159 				dev_info(&pf->pdev->dev,
13160 					 "update vsi failed, err %s aq_err %s\n",
13161 					 i40e_stat_str(&pf->hw, ret),
13162 					 i40e_aq_str(&pf->hw,
13163 						     pf->hw.aq.asq_last_status));
13164 				ret = -ENOENT;
13165 				goto err;
13166 			}
13167 		}
13168 
13169 		/* MFP mode setup queue map and update VSI */
13170 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13171 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13172 			memset(&ctxt, 0, sizeof(ctxt));
13173 			ctxt.seid = pf->main_vsi_seid;
13174 			ctxt.pf_num = pf->hw.pf_id;
13175 			ctxt.vf_num = 0;
13176 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13177 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13178 			if (ret) {
13179 				dev_info(&pf->pdev->dev,
13180 					 "update vsi failed, err %s aq_err %s\n",
13181 					 i40e_stat_str(&pf->hw, ret),
13182 					 i40e_aq_str(&pf->hw,
13183 						    pf->hw.aq.asq_last_status));
13184 				ret = -ENOENT;
13185 				goto err;
13186 			}
13187 			/* update the local VSI info queue map */
13188 			i40e_vsi_update_queue_map(vsi, &ctxt);
13189 			vsi->info.valid_sections = 0;
13190 		} else {
13191 			/* Default/Main VSI is only enabled for TC0
13192 			 * reconfigure it to enable all TCs that are
13193 			 * available on the port in SFP mode.
13194 			 * For MFP case the iSCSI PF would use this
13195 			 * flow to enable LAN+iSCSI TC.
13196 			 */
13197 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13198 			if (ret) {
13199 				/* Single TC condition is not fatal,
13200 				 * message and continue
13201 				 */
13202 				dev_info(&pf->pdev->dev,
13203 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13204 					 enabled_tc,
13205 					 i40e_stat_str(&pf->hw, ret),
13206 					 i40e_aq_str(&pf->hw,
13207 						    pf->hw.aq.asq_last_status));
13208 			}
13209 		}
13210 		break;
13211 
13212 	case I40E_VSI_FDIR:
13213 		ctxt.pf_num = hw->pf_id;
13214 		ctxt.vf_num = 0;
13215 		ctxt.uplink_seid = vsi->uplink_seid;
13216 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13217 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13218 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13219 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13220 			ctxt.info.valid_sections |=
13221 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13222 			ctxt.info.switch_id =
13223 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13224 		}
13225 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13226 		break;
13227 
13228 	case I40E_VSI_VMDQ2:
13229 		ctxt.pf_num = hw->pf_id;
13230 		ctxt.vf_num = 0;
13231 		ctxt.uplink_seid = vsi->uplink_seid;
13232 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13233 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13234 
13235 		/* This VSI is connected to VEB so the switch_id
13236 		 * should be set to zero by default.
13237 		 */
13238 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13239 			ctxt.info.valid_sections |=
13240 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13241 			ctxt.info.switch_id =
13242 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13243 		}
13244 
13245 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13246 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13247 		break;
13248 
13249 	case I40E_VSI_SRIOV:
13250 		ctxt.pf_num = hw->pf_id;
13251 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13252 		ctxt.uplink_seid = vsi->uplink_seid;
13253 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13254 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13255 
13256 		/* This VSI is connected to VEB so the switch_id
13257 		 * should be set to zero by default.
13258 		 */
13259 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13260 			ctxt.info.valid_sections |=
13261 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13262 			ctxt.info.switch_id =
13263 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13264 		}
13265 
13266 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
13267 			ctxt.info.valid_sections |=
13268 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
13269 			ctxt.info.queueing_opt_flags |=
13270 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
13271 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
13272 		}
13273 
13274 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
13275 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
13276 		if (pf->vf[vsi->vf_id].spoofchk) {
13277 			ctxt.info.valid_sections |=
13278 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
13279 			ctxt.info.sec_flags |=
13280 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
13281 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
13282 		}
13283 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13284 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13285 		break;
13286 
13287 	case I40E_VSI_IWARP:
13288 		/* send down message to iWARP */
13289 		break;
13290 
13291 	default:
13292 		return -ENODEV;
13293 	}
13294 
13295 	if (vsi->type != I40E_VSI_MAIN) {
13296 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
13297 		if (ret) {
13298 			dev_info(&vsi->back->pdev->dev,
13299 				 "add vsi failed, err %s aq_err %s\n",
13300 				 i40e_stat_str(&pf->hw, ret),
13301 				 i40e_aq_str(&pf->hw,
13302 					     pf->hw.aq.asq_last_status));
13303 			ret = -ENOENT;
13304 			goto err;
13305 		}
13306 		vsi->info = ctxt.info;
13307 		vsi->info.valid_sections = 0;
13308 		vsi->seid = ctxt.seid;
13309 		vsi->id = ctxt.vsi_number;
13310 	}
13311 
13312 	vsi->active_filters = 0;
13313 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
13314 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13315 	/* If macvlan filters already exist, force them to get loaded */
13316 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
13317 		f->state = I40E_FILTER_NEW;
13318 		f_count++;
13319 	}
13320 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13321 
13322 	if (f_count) {
13323 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
13324 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
13325 	}
13326 
13327 	/* Update VSI BW information */
13328 	ret = i40e_vsi_get_bw_info(vsi);
13329 	if (ret) {
13330 		dev_info(&pf->pdev->dev,
13331 			 "couldn't get vsi bw info, err %s aq_err %s\n",
13332 			 i40e_stat_str(&pf->hw, ret),
13333 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13334 		/* VSI is already added so not tearing that up */
13335 		ret = 0;
13336 	}
13337 
13338 err:
13339 	return ret;
13340 }
13341 
13342 /**
13343  * i40e_vsi_release - Delete a VSI and free its resources
13344  * @vsi: the VSI being removed
13345  *
13346  * Returns 0 on success or < 0 on error
13347  **/
13348 int i40e_vsi_release(struct i40e_vsi *vsi)
13349 {
13350 	struct i40e_mac_filter *f;
13351 	struct hlist_node *h;
13352 	struct i40e_veb *veb = NULL;
13353 	struct i40e_pf *pf;
13354 	u16 uplink_seid;
13355 	int i, n, bkt;
13356 
13357 	pf = vsi->back;
13358 
13359 	/* release of a VEB-owner or last VSI is not allowed */
13360 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
13361 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
13362 			 vsi->seid, vsi->uplink_seid);
13363 		return -ENODEV;
13364 	}
13365 	if (vsi == pf->vsi[pf->lan_vsi] &&
13366 	    !test_bit(__I40E_DOWN, pf->state)) {
13367 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
13368 		return -ENODEV;
13369 	}
13370 
13371 	uplink_seid = vsi->uplink_seid;
13372 	if (vsi->type != I40E_VSI_SRIOV) {
13373 		if (vsi->netdev_registered) {
13374 			vsi->netdev_registered = false;
13375 			if (vsi->netdev) {
13376 				/* results in a call to i40e_close() */
13377 				unregister_netdev(vsi->netdev);
13378 			}
13379 		} else {
13380 			i40e_vsi_close(vsi);
13381 		}
13382 		i40e_vsi_disable_irq(vsi);
13383 	}
13384 
13385 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13386 
13387 	/* clear the sync flag on all filters */
13388 	if (vsi->netdev) {
13389 		__dev_uc_unsync(vsi->netdev, NULL);
13390 		__dev_mc_unsync(vsi->netdev, NULL);
13391 	}
13392 
13393 	/* make sure any remaining filters are marked for deletion */
13394 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
13395 		__i40e_del_filter(vsi, f);
13396 
13397 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13398 
13399 	i40e_sync_vsi_filters(vsi);
13400 
13401 	i40e_vsi_delete(vsi);
13402 	i40e_vsi_free_q_vectors(vsi);
13403 	if (vsi->netdev) {
13404 		free_netdev(vsi->netdev);
13405 		vsi->netdev = NULL;
13406 	}
13407 	i40e_vsi_clear_rings(vsi);
13408 	i40e_vsi_clear(vsi);
13409 
13410 	/* If this was the last thing on the VEB, except for the
13411 	 * controlling VSI, remove the VEB, which puts the controlling
13412 	 * VSI onto the next level down in the switch.
13413 	 *
13414 	 * Well, okay, there's one more exception here: don't remove
13415 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
13416 	 * from up the network stack.
13417 	 */
13418 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
13419 		if (pf->vsi[i] &&
13420 		    pf->vsi[i]->uplink_seid == uplink_seid &&
13421 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13422 			n++;      /* count the VSIs */
13423 		}
13424 	}
13425 	for (i = 0; i < I40E_MAX_VEB; i++) {
13426 		if (!pf->veb[i])
13427 			continue;
13428 		if (pf->veb[i]->uplink_seid == uplink_seid)
13429 			n++;     /* count the VEBs */
13430 		if (pf->veb[i]->seid == uplink_seid)
13431 			veb = pf->veb[i];
13432 	}
13433 	if (n == 0 && veb && veb->uplink_seid != 0)
13434 		i40e_veb_release(veb);
13435 
13436 	return 0;
13437 }
13438 
13439 /**
13440  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
13441  * @vsi: ptr to the VSI
13442  *
13443  * This should only be called after i40e_vsi_mem_alloc() which allocates the
13444  * corresponding SW VSI structure and initializes num_queue_pairs for the
13445  * newly allocated VSI.
13446  *
13447  * Returns 0 on success or negative on failure
13448  **/
13449 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
13450 {
13451 	int ret = -ENOENT;
13452 	struct i40e_pf *pf = vsi->back;
13453 
13454 	if (vsi->q_vectors[0]) {
13455 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
13456 			 vsi->seid);
13457 		return -EEXIST;
13458 	}
13459 
13460 	if (vsi->base_vector) {
13461 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
13462 			 vsi->seid, vsi->base_vector);
13463 		return -EEXIST;
13464 	}
13465 
13466 	ret = i40e_vsi_alloc_q_vectors(vsi);
13467 	if (ret) {
13468 		dev_info(&pf->pdev->dev,
13469 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
13470 			 vsi->num_q_vectors, vsi->seid, ret);
13471 		vsi->num_q_vectors = 0;
13472 		goto vector_setup_out;
13473 	}
13474 
13475 	/* In Legacy mode, we do not have to get any other vector since we
13476 	 * piggyback on the misc/ICR0 for queue interrupts.
13477 	*/
13478 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
13479 		return ret;
13480 	if (vsi->num_q_vectors)
13481 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
13482 						 vsi->num_q_vectors, vsi->idx);
13483 	if (vsi->base_vector < 0) {
13484 		dev_info(&pf->pdev->dev,
13485 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
13486 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
13487 		i40e_vsi_free_q_vectors(vsi);
13488 		ret = -ENOENT;
13489 		goto vector_setup_out;
13490 	}
13491 
13492 vector_setup_out:
13493 	return ret;
13494 }
13495 
13496 /**
13497  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
13498  * @vsi: pointer to the vsi.
13499  *
13500  * This re-allocates a vsi's queue resources.
13501  *
13502  * Returns pointer to the successfully allocated and configured VSI sw struct
13503  * on success, otherwise returns NULL on failure.
13504  **/
13505 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
13506 {
13507 	u16 alloc_queue_pairs;
13508 	struct i40e_pf *pf;
13509 	u8 enabled_tc;
13510 	int ret;
13511 
13512 	if (!vsi)
13513 		return NULL;
13514 
13515 	pf = vsi->back;
13516 
13517 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
13518 	i40e_vsi_clear_rings(vsi);
13519 
13520 	i40e_vsi_free_arrays(vsi, false);
13521 	i40e_set_num_rings_in_vsi(vsi);
13522 	ret = i40e_vsi_alloc_arrays(vsi, false);
13523 	if (ret)
13524 		goto err_vsi;
13525 
13526 	alloc_queue_pairs = vsi->alloc_queue_pairs *
13527 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
13528 
13529 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
13530 	if (ret < 0) {
13531 		dev_info(&pf->pdev->dev,
13532 			 "failed to get tracking for %d queues for VSI %d err %d\n",
13533 			 alloc_queue_pairs, vsi->seid, ret);
13534 		goto err_vsi;
13535 	}
13536 	vsi->base_queue = ret;
13537 
13538 	/* Update the FW view of the VSI. Force a reset of TC and queue
13539 	 * layout configurations.
13540 	 */
13541 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
13542 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
13543 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
13544 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
13545 	if (vsi->type == I40E_VSI_MAIN)
13546 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
13547 
13548 	/* assign it some queues */
13549 	ret = i40e_alloc_rings(vsi);
13550 	if (ret)
13551 		goto err_rings;
13552 
13553 	/* map all of the rings to the q_vectors */
13554 	i40e_vsi_map_rings_to_vectors(vsi);
13555 	return vsi;
13556 
13557 err_rings:
13558 	i40e_vsi_free_q_vectors(vsi);
13559 	if (vsi->netdev_registered) {
13560 		vsi->netdev_registered = false;
13561 		unregister_netdev(vsi->netdev);
13562 		free_netdev(vsi->netdev);
13563 		vsi->netdev = NULL;
13564 	}
13565 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
13566 err_vsi:
13567 	i40e_vsi_clear(vsi);
13568 	return NULL;
13569 }
13570 
13571 /**
13572  * i40e_vsi_setup - Set up a VSI by a given type
13573  * @pf: board private structure
13574  * @type: VSI type
13575  * @uplink_seid: the switch element to link to
13576  * @param1: usage depends upon VSI type. For VF types, indicates VF id
13577  *
13578  * This allocates the sw VSI structure and its queue resources, then add a VSI
13579  * to the identified VEB.
13580  *
13581  * Returns pointer to the successfully allocated and configure VSI sw struct on
13582  * success, otherwise returns NULL on failure.
13583  **/
13584 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
13585 				u16 uplink_seid, u32 param1)
13586 {
13587 	struct i40e_vsi *vsi = NULL;
13588 	struct i40e_veb *veb = NULL;
13589 	u16 alloc_queue_pairs;
13590 	int ret, i;
13591 	int v_idx;
13592 
13593 	/* The requested uplink_seid must be either
13594 	 *     - the PF's port seid
13595 	 *              no VEB is needed because this is the PF
13596 	 *              or this is a Flow Director special case VSI
13597 	 *     - seid of an existing VEB
13598 	 *     - seid of a VSI that owns an existing VEB
13599 	 *     - seid of a VSI that doesn't own a VEB
13600 	 *              a new VEB is created and the VSI becomes the owner
13601 	 *     - seid of the PF VSI, which is what creates the first VEB
13602 	 *              this is a special case of the previous
13603 	 *
13604 	 * Find which uplink_seid we were given and create a new VEB if needed
13605 	 */
13606 	for (i = 0; i < I40E_MAX_VEB; i++) {
13607 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
13608 			veb = pf->veb[i];
13609 			break;
13610 		}
13611 	}
13612 
13613 	if (!veb && uplink_seid != pf->mac_seid) {
13614 
13615 		for (i = 0; i < pf->num_alloc_vsi; i++) {
13616 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
13617 				vsi = pf->vsi[i];
13618 				break;
13619 			}
13620 		}
13621 		if (!vsi) {
13622 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
13623 				 uplink_seid);
13624 			return NULL;
13625 		}
13626 
13627 		if (vsi->uplink_seid == pf->mac_seid)
13628 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
13629 					     vsi->tc_config.enabled_tc);
13630 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
13631 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13632 					     vsi->tc_config.enabled_tc);
13633 		if (veb) {
13634 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
13635 				dev_info(&vsi->back->pdev->dev,
13636 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
13637 				return NULL;
13638 			}
13639 			/* We come up by default in VEPA mode if SRIOV is not
13640 			 * already enabled, in which case we can't force VEPA
13641 			 * mode.
13642 			 */
13643 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
13644 				veb->bridge_mode = BRIDGE_MODE_VEPA;
13645 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13646 			}
13647 			i40e_config_bridge_mode(veb);
13648 		}
13649 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13650 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13651 				veb = pf->veb[i];
13652 		}
13653 		if (!veb) {
13654 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
13655 			return NULL;
13656 		}
13657 
13658 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
13659 		uplink_seid = veb->seid;
13660 	}
13661 
13662 	/* get vsi sw struct */
13663 	v_idx = i40e_vsi_mem_alloc(pf, type);
13664 	if (v_idx < 0)
13665 		goto err_alloc;
13666 	vsi = pf->vsi[v_idx];
13667 	if (!vsi)
13668 		goto err_alloc;
13669 	vsi->type = type;
13670 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
13671 
13672 	if (type == I40E_VSI_MAIN)
13673 		pf->lan_vsi = v_idx;
13674 	else if (type == I40E_VSI_SRIOV)
13675 		vsi->vf_id = param1;
13676 	/* assign it some queues */
13677 	alloc_queue_pairs = vsi->alloc_queue_pairs *
13678 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
13679 
13680 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
13681 	if (ret < 0) {
13682 		dev_info(&pf->pdev->dev,
13683 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
13684 			 alloc_queue_pairs, vsi->seid, ret);
13685 		goto err_vsi;
13686 	}
13687 	vsi->base_queue = ret;
13688 
13689 	/* get a VSI from the hardware */
13690 	vsi->uplink_seid = uplink_seid;
13691 	ret = i40e_add_vsi(vsi);
13692 	if (ret)
13693 		goto err_vsi;
13694 
13695 	switch (vsi->type) {
13696 	/* setup the netdev if needed */
13697 	case I40E_VSI_MAIN:
13698 	case I40E_VSI_VMDQ2:
13699 		ret = i40e_config_netdev(vsi);
13700 		if (ret)
13701 			goto err_netdev;
13702 		ret = register_netdev(vsi->netdev);
13703 		if (ret)
13704 			goto err_netdev;
13705 		vsi->netdev_registered = true;
13706 		netif_carrier_off(vsi->netdev);
13707 #ifdef CONFIG_I40E_DCB
13708 		/* Setup DCB netlink interface */
13709 		i40e_dcbnl_setup(vsi);
13710 #endif /* CONFIG_I40E_DCB */
13711 		/* fall through */
13712 
13713 	case I40E_VSI_FDIR:
13714 		/* set up vectors and rings if needed */
13715 		ret = i40e_vsi_setup_vectors(vsi);
13716 		if (ret)
13717 			goto err_msix;
13718 
13719 		ret = i40e_alloc_rings(vsi);
13720 		if (ret)
13721 			goto err_rings;
13722 
13723 		/* map all of the rings to the q_vectors */
13724 		i40e_vsi_map_rings_to_vectors(vsi);
13725 
13726 		i40e_vsi_reset_stats(vsi);
13727 		break;
13728 
13729 	default:
13730 		/* no netdev or rings for the other VSI types */
13731 		break;
13732 	}
13733 
13734 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
13735 	    (vsi->type == I40E_VSI_VMDQ2)) {
13736 		ret = i40e_vsi_config_rss(vsi);
13737 	}
13738 	return vsi;
13739 
13740 err_rings:
13741 	i40e_vsi_free_q_vectors(vsi);
13742 err_msix:
13743 	if (vsi->netdev_registered) {
13744 		vsi->netdev_registered = false;
13745 		unregister_netdev(vsi->netdev);
13746 		free_netdev(vsi->netdev);
13747 		vsi->netdev = NULL;
13748 	}
13749 err_netdev:
13750 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
13751 err_vsi:
13752 	i40e_vsi_clear(vsi);
13753 err_alloc:
13754 	return NULL;
13755 }
13756 
13757 /**
13758  * i40e_veb_get_bw_info - Query VEB BW information
13759  * @veb: the veb to query
13760  *
13761  * Query the Tx scheduler BW configuration data for given VEB
13762  **/
13763 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
13764 {
13765 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
13766 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
13767 	struct i40e_pf *pf = veb->pf;
13768 	struct i40e_hw *hw = &pf->hw;
13769 	u32 tc_bw_max;
13770 	int ret = 0;
13771 	int i;
13772 
13773 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
13774 						  &bw_data, NULL);
13775 	if (ret) {
13776 		dev_info(&pf->pdev->dev,
13777 			 "query veb bw config failed, err %s aq_err %s\n",
13778 			 i40e_stat_str(&pf->hw, ret),
13779 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
13780 		goto out;
13781 	}
13782 
13783 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
13784 						   &ets_data, NULL);
13785 	if (ret) {
13786 		dev_info(&pf->pdev->dev,
13787 			 "query veb bw ets config failed, err %s aq_err %s\n",
13788 			 i40e_stat_str(&pf->hw, ret),
13789 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
13790 		goto out;
13791 	}
13792 
13793 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
13794 	veb->bw_max_quanta = ets_data.tc_bw_max;
13795 	veb->is_abs_credits = bw_data.absolute_credits_enable;
13796 	veb->enabled_tc = ets_data.tc_valid_bits;
13797 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
13798 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
13799 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
13800 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
13801 		veb->bw_tc_limit_credits[i] =
13802 					le16_to_cpu(bw_data.tc_bw_limits[i]);
13803 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
13804 	}
13805 
13806 out:
13807 	return ret;
13808 }
13809 
13810 /**
13811  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
13812  * @pf: board private structure
13813  *
13814  * On error: returns error code (negative)
13815  * On success: returns vsi index in PF (positive)
13816  **/
13817 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
13818 {
13819 	int ret = -ENOENT;
13820 	struct i40e_veb *veb;
13821 	int i;
13822 
13823 	/* Need to protect the allocation of switch elements at the PF level */
13824 	mutex_lock(&pf->switch_mutex);
13825 
13826 	/* VEB list may be fragmented if VEB creation/destruction has
13827 	 * been happening.  We can afford to do a quick scan to look
13828 	 * for any free slots in the list.
13829 	 *
13830 	 * find next empty veb slot, looping back around if necessary
13831 	 */
13832 	i = 0;
13833 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
13834 		i++;
13835 	if (i >= I40E_MAX_VEB) {
13836 		ret = -ENOMEM;
13837 		goto err_alloc_veb;  /* out of VEB slots! */
13838 	}
13839 
13840 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
13841 	if (!veb) {
13842 		ret = -ENOMEM;
13843 		goto err_alloc_veb;
13844 	}
13845 	veb->pf = pf;
13846 	veb->idx = i;
13847 	veb->enabled_tc = 1;
13848 
13849 	pf->veb[i] = veb;
13850 	ret = i;
13851 err_alloc_veb:
13852 	mutex_unlock(&pf->switch_mutex);
13853 	return ret;
13854 }
13855 
13856 /**
13857  * i40e_switch_branch_release - Delete a branch of the switch tree
13858  * @branch: where to start deleting
13859  *
13860  * This uses recursion to find the tips of the branch to be
13861  * removed, deleting until we get back to and can delete this VEB.
13862  **/
13863 static void i40e_switch_branch_release(struct i40e_veb *branch)
13864 {
13865 	struct i40e_pf *pf = branch->pf;
13866 	u16 branch_seid = branch->seid;
13867 	u16 veb_idx = branch->idx;
13868 	int i;
13869 
13870 	/* release any VEBs on this VEB - RECURSION */
13871 	for (i = 0; i < I40E_MAX_VEB; i++) {
13872 		if (!pf->veb[i])
13873 			continue;
13874 		if (pf->veb[i]->uplink_seid == branch->seid)
13875 			i40e_switch_branch_release(pf->veb[i]);
13876 	}
13877 
13878 	/* Release the VSIs on this VEB, but not the owner VSI.
13879 	 *
13880 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
13881 	 *       the VEB itself, so don't use (*branch) after this loop.
13882 	 */
13883 	for (i = 0; i < pf->num_alloc_vsi; i++) {
13884 		if (!pf->vsi[i])
13885 			continue;
13886 		if (pf->vsi[i]->uplink_seid == branch_seid &&
13887 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13888 			i40e_vsi_release(pf->vsi[i]);
13889 		}
13890 	}
13891 
13892 	/* There's one corner case where the VEB might not have been
13893 	 * removed, so double check it here and remove it if needed.
13894 	 * This case happens if the veb was created from the debugfs
13895 	 * commands and no VSIs were added to it.
13896 	 */
13897 	if (pf->veb[veb_idx])
13898 		i40e_veb_release(pf->veb[veb_idx]);
13899 }
13900 
13901 /**
13902  * i40e_veb_clear - remove veb struct
13903  * @veb: the veb to remove
13904  **/
13905 static void i40e_veb_clear(struct i40e_veb *veb)
13906 {
13907 	if (!veb)
13908 		return;
13909 
13910 	if (veb->pf) {
13911 		struct i40e_pf *pf = veb->pf;
13912 
13913 		mutex_lock(&pf->switch_mutex);
13914 		if (pf->veb[veb->idx] == veb)
13915 			pf->veb[veb->idx] = NULL;
13916 		mutex_unlock(&pf->switch_mutex);
13917 	}
13918 
13919 	kfree(veb);
13920 }
13921 
13922 /**
13923  * i40e_veb_release - Delete a VEB and free its resources
13924  * @veb: the VEB being removed
13925  **/
13926 void i40e_veb_release(struct i40e_veb *veb)
13927 {
13928 	struct i40e_vsi *vsi = NULL;
13929 	struct i40e_pf *pf;
13930 	int i, n = 0;
13931 
13932 	pf = veb->pf;
13933 
13934 	/* find the remaining VSI and check for extras */
13935 	for (i = 0; i < pf->num_alloc_vsi; i++) {
13936 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
13937 			n++;
13938 			vsi = pf->vsi[i];
13939 		}
13940 	}
13941 	if (n != 1) {
13942 		dev_info(&pf->pdev->dev,
13943 			 "can't remove VEB %d with %d VSIs left\n",
13944 			 veb->seid, n);
13945 		return;
13946 	}
13947 
13948 	/* move the remaining VSI to uplink veb */
13949 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
13950 	if (veb->uplink_seid) {
13951 		vsi->uplink_seid = veb->uplink_seid;
13952 		if (veb->uplink_seid == pf->mac_seid)
13953 			vsi->veb_idx = I40E_NO_VEB;
13954 		else
13955 			vsi->veb_idx = veb->veb_idx;
13956 	} else {
13957 		/* floating VEB */
13958 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
13959 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
13960 	}
13961 
13962 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
13963 	i40e_veb_clear(veb);
13964 }
13965 
13966 /**
13967  * i40e_add_veb - create the VEB in the switch
13968  * @veb: the VEB to be instantiated
13969  * @vsi: the controlling VSI
13970  **/
13971 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
13972 {
13973 	struct i40e_pf *pf = veb->pf;
13974 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
13975 	int ret;
13976 
13977 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
13978 			      veb->enabled_tc, false,
13979 			      &veb->seid, enable_stats, NULL);
13980 
13981 	/* get a VEB from the hardware */
13982 	if (ret) {
13983 		dev_info(&pf->pdev->dev,
13984 			 "couldn't add VEB, err %s aq_err %s\n",
13985 			 i40e_stat_str(&pf->hw, ret),
13986 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13987 		return -EPERM;
13988 	}
13989 
13990 	/* get statistics counter */
13991 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
13992 					 &veb->stats_idx, NULL, NULL, NULL);
13993 	if (ret) {
13994 		dev_info(&pf->pdev->dev,
13995 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
13996 			 i40e_stat_str(&pf->hw, ret),
13997 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13998 		return -EPERM;
13999 	}
14000 	ret = i40e_veb_get_bw_info(veb);
14001 	if (ret) {
14002 		dev_info(&pf->pdev->dev,
14003 			 "couldn't get VEB bw info, err %s aq_err %s\n",
14004 			 i40e_stat_str(&pf->hw, ret),
14005 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14006 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14007 		return -ENOENT;
14008 	}
14009 
14010 	vsi->uplink_seid = veb->seid;
14011 	vsi->veb_idx = veb->idx;
14012 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14013 
14014 	return 0;
14015 }
14016 
14017 /**
14018  * i40e_veb_setup - Set up a VEB
14019  * @pf: board private structure
14020  * @flags: VEB setup flags
14021  * @uplink_seid: the switch element to link to
14022  * @vsi_seid: the initial VSI seid
14023  * @enabled_tc: Enabled TC bit-map
14024  *
14025  * This allocates the sw VEB structure and links it into the switch
14026  * It is possible and legal for this to be a duplicate of an already
14027  * existing VEB.  It is also possible for both uplink and vsi seids
14028  * to be zero, in order to create a floating VEB.
14029  *
14030  * Returns pointer to the successfully allocated VEB sw struct on
14031  * success, otherwise returns NULL on failure.
14032  **/
14033 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14034 				u16 uplink_seid, u16 vsi_seid,
14035 				u8 enabled_tc)
14036 {
14037 	struct i40e_veb *veb, *uplink_veb = NULL;
14038 	int vsi_idx, veb_idx;
14039 	int ret;
14040 
14041 	/* if one seid is 0, the other must be 0 to create a floating relay */
14042 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14043 	    (uplink_seid + vsi_seid != 0)) {
14044 		dev_info(&pf->pdev->dev,
14045 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14046 			 uplink_seid, vsi_seid);
14047 		return NULL;
14048 	}
14049 
14050 	/* make sure there is such a vsi and uplink */
14051 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14052 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14053 			break;
14054 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14055 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14056 			 vsi_seid);
14057 		return NULL;
14058 	}
14059 
14060 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14061 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14062 			if (pf->veb[veb_idx] &&
14063 			    pf->veb[veb_idx]->seid == uplink_seid) {
14064 				uplink_veb = pf->veb[veb_idx];
14065 				break;
14066 			}
14067 		}
14068 		if (!uplink_veb) {
14069 			dev_info(&pf->pdev->dev,
14070 				 "uplink seid %d not found\n", uplink_seid);
14071 			return NULL;
14072 		}
14073 	}
14074 
14075 	/* get veb sw struct */
14076 	veb_idx = i40e_veb_mem_alloc(pf);
14077 	if (veb_idx < 0)
14078 		goto err_alloc;
14079 	veb = pf->veb[veb_idx];
14080 	veb->flags = flags;
14081 	veb->uplink_seid = uplink_seid;
14082 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14083 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14084 
14085 	/* create the VEB in the switch */
14086 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14087 	if (ret)
14088 		goto err_veb;
14089 	if (vsi_idx == pf->lan_vsi)
14090 		pf->lan_veb = veb->idx;
14091 
14092 	return veb;
14093 
14094 err_veb:
14095 	i40e_veb_clear(veb);
14096 err_alloc:
14097 	return NULL;
14098 }
14099 
14100 /**
14101  * i40e_setup_pf_switch_element - set PF vars based on switch type
14102  * @pf: board private structure
14103  * @ele: element we are building info from
14104  * @num_reported: total number of elements
14105  * @printconfig: should we print the contents
14106  *
14107  * helper function to assist in extracting a few useful SEID values.
14108  **/
14109 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14110 				struct i40e_aqc_switch_config_element_resp *ele,
14111 				u16 num_reported, bool printconfig)
14112 {
14113 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14114 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14115 	u8 element_type = ele->element_type;
14116 	u16 seid = le16_to_cpu(ele->seid);
14117 
14118 	if (printconfig)
14119 		dev_info(&pf->pdev->dev,
14120 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14121 			 element_type, seid, uplink_seid, downlink_seid);
14122 
14123 	switch (element_type) {
14124 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14125 		pf->mac_seid = seid;
14126 		break;
14127 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14128 		/* Main VEB? */
14129 		if (uplink_seid != pf->mac_seid)
14130 			break;
14131 		if (pf->lan_veb >= I40E_MAX_VEB) {
14132 			int v;
14133 
14134 			/* find existing or else empty VEB */
14135 			for (v = 0; v < I40E_MAX_VEB; v++) {
14136 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14137 					pf->lan_veb = v;
14138 					break;
14139 				}
14140 			}
14141 			if (pf->lan_veb >= I40E_MAX_VEB) {
14142 				v = i40e_veb_mem_alloc(pf);
14143 				if (v < 0)
14144 					break;
14145 				pf->lan_veb = v;
14146 			}
14147 		}
14148 		if (pf->lan_veb >= I40E_MAX_VEB)
14149 			break;
14150 
14151 		pf->veb[pf->lan_veb]->seid = seid;
14152 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14153 		pf->veb[pf->lan_veb]->pf = pf;
14154 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14155 		break;
14156 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14157 		if (num_reported != 1)
14158 			break;
14159 		/* This is immediately after a reset so we can assume this is
14160 		 * the PF's VSI
14161 		 */
14162 		pf->mac_seid = uplink_seid;
14163 		pf->pf_seid = downlink_seid;
14164 		pf->main_vsi_seid = seid;
14165 		if (printconfig)
14166 			dev_info(&pf->pdev->dev,
14167 				 "pf_seid=%d main_vsi_seid=%d\n",
14168 				 pf->pf_seid, pf->main_vsi_seid);
14169 		break;
14170 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14171 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14172 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14173 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14174 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14175 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14176 		/* ignore these for now */
14177 		break;
14178 	default:
14179 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14180 			 element_type, seid);
14181 		break;
14182 	}
14183 }
14184 
14185 /**
14186  * i40e_fetch_switch_configuration - Get switch config from firmware
14187  * @pf: board private structure
14188  * @printconfig: should we print the contents
14189  *
14190  * Get the current switch configuration from the device and
14191  * extract a few useful SEID values.
14192  **/
14193 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14194 {
14195 	struct i40e_aqc_get_switch_config_resp *sw_config;
14196 	u16 next_seid = 0;
14197 	int ret = 0;
14198 	u8 *aq_buf;
14199 	int i;
14200 
14201 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14202 	if (!aq_buf)
14203 		return -ENOMEM;
14204 
14205 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14206 	do {
14207 		u16 num_reported, num_total;
14208 
14209 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14210 						I40E_AQ_LARGE_BUF,
14211 						&next_seid, NULL);
14212 		if (ret) {
14213 			dev_info(&pf->pdev->dev,
14214 				 "get switch config failed err %s aq_err %s\n",
14215 				 i40e_stat_str(&pf->hw, ret),
14216 				 i40e_aq_str(&pf->hw,
14217 					     pf->hw.aq.asq_last_status));
14218 			kfree(aq_buf);
14219 			return -ENOENT;
14220 		}
14221 
14222 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14223 		num_total = le16_to_cpu(sw_config->header.num_total);
14224 
14225 		if (printconfig)
14226 			dev_info(&pf->pdev->dev,
14227 				 "header: %d reported %d total\n",
14228 				 num_reported, num_total);
14229 
14230 		for (i = 0; i < num_reported; i++) {
14231 			struct i40e_aqc_switch_config_element_resp *ele =
14232 				&sw_config->element[i];
14233 
14234 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14235 						     printconfig);
14236 		}
14237 	} while (next_seid != 0);
14238 
14239 	kfree(aq_buf);
14240 	return ret;
14241 }
14242 
14243 /**
14244  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14245  * @pf: board private structure
14246  * @reinit: if the Main VSI needs to re-initialized.
14247  *
14248  * Returns 0 on success, negative value on failure
14249  **/
14250 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
14251 {
14252 	u16 flags = 0;
14253 	int ret;
14254 
14255 	/* find out what's out there already */
14256 	ret = i40e_fetch_switch_configuration(pf, false);
14257 	if (ret) {
14258 		dev_info(&pf->pdev->dev,
14259 			 "couldn't fetch switch config, err %s aq_err %s\n",
14260 			 i40e_stat_str(&pf->hw, ret),
14261 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14262 		return ret;
14263 	}
14264 	i40e_pf_reset_stats(pf);
14265 
14266 	/* set the switch config bit for the whole device to
14267 	 * support limited promisc or true promisc
14268 	 * when user requests promisc. The default is limited
14269 	 * promisc.
14270 	*/
14271 
14272 	if ((pf->hw.pf_id == 0) &&
14273 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
14274 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14275 		pf->last_sw_conf_flags = flags;
14276 	}
14277 
14278 	if (pf->hw.pf_id == 0) {
14279 		u16 valid_flags;
14280 
14281 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14282 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
14283 						NULL);
14284 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
14285 			dev_info(&pf->pdev->dev,
14286 				 "couldn't set switch config bits, err %s aq_err %s\n",
14287 				 i40e_stat_str(&pf->hw, ret),
14288 				 i40e_aq_str(&pf->hw,
14289 					     pf->hw.aq.asq_last_status));
14290 			/* not a fatal problem, just keep going */
14291 		}
14292 		pf->last_sw_conf_valid_flags = valid_flags;
14293 	}
14294 
14295 	/* first time setup */
14296 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
14297 		struct i40e_vsi *vsi = NULL;
14298 		u16 uplink_seid;
14299 
14300 		/* Set up the PF VSI associated with the PF's main VSI
14301 		 * that is already in the HW switch
14302 		 */
14303 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
14304 			uplink_seid = pf->veb[pf->lan_veb]->seid;
14305 		else
14306 			uplink_seid = pf->mac_seid;
14307 		if (pf->lan_vsi == I40E_NO_VSI)
14308 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
14309 		else if (reinit)
14310 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
14311 		if (!vsi) {
14312 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
14313 			i40e_cloud_filter_exit(pf);
14314 			i40e_fdir_teardown(pf);
14315 			return -EAGAIN;
14316 		}
14317 	} else {
14318 		/* force a reset of TC and queue layout configurations */
14319 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14320 
14321 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14322 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14323 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14324 	}
14325 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
14326 
14327 	i40e_fdir_sb_setup(pf);
14328 
14329 	/* Setup static PF queue filter control settings */
14330 	ret = i40e_setup_pf_filter_control(pf);
14331 	if (ret) {
14332 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
14333 			 ret);
14334 		/* Failure here should not stop continuing other steps */
14335 	}
14336 
14337 	/* enable RSS in the HW, even for only one queue, as the stack can use
14338 	 * the hash
14339 	 */
14340 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
14341 		i40e_pf_config_rss(pf);
14342 
14343 	/* fill in link information and enable LSE reporting */
14344 	i40e_link_event(pf);
14345 
14346 	/* Initialize user-specific link properties */
14347 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
14348 				  I40E_AQ_AN_COMPLETED) ? true : false);
14349 
14350 	i40e_ptp_init(pf);
14351 
14352 	/* repopulate tunnel port filters */
14353 	i40e_sync_udp_filters(pf);
14354 
14355 	return ret;
14356 }
14357 
14358 /**
14359  * i40e_determine_queue_usage - Work out queue distribution
14360  * @pf: board private structure
14361  **/
14362 static void i40e_determine_queue_usage(struct i40e_pf *pf)
14363 {
14364 	int queues_left;
14365 	int q_max;
14366 
14367 	pf->num_lan_qps = 0;
14368 
14369 	/* Find the max queues to be put into basic use.  We'll always be
14370 	 * using TC0, whether or not DCB is running, and TC0 will get the
14371 	 * big RSS set.
14372 	 */
14373 	queues_left = pf->hw.func_caps.num_tx_qp;
14374 
14375 	if ((queues_left == 1) ||
14376 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
14377 		/* one qp for PF, no queues for anything else */
14378 		queues_left = 0;
14379 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14380 
14381 		/* make sure all the fancies are disabled */
14382 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14383 			       I40E_FLAG_IWARP_ENABLED	|
14384 			       I40E_FLAG_FD_SB_ENABLED	|
14385 			       I40E_FLAG_FD_ATR_ENABLED	|
14386 			       I40E_FLAG_DCB_CAPABLE	|
14387 			       I40E_FLAG_DCB_ENABLED	|
14388 			       I40E_FLAG_SRIOV_ENABLED	|
14389 			       I40E_FLAG_VMDQ_ENABLED);
14390 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14391 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
14392 				  I40E_FLAG_FD_SB_ENABLED |
14393 				  I40E_FLAG_FD_ATR_ENABLED |
14394 				  I40E_FLAG_DCB_CAPABLE))) {
14395 		/* one qp for PF */
14396 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14397 		queues_left -= pf->num_lan_qps;
14398 
14399 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14400 			       I40E_FLAG_IWARP_ENABLED	|
14401 			       I40E_FLAG_FD_SB_ENABLED	|
14402 			       I40E_FLAG_FD_ATR_ENABLED	|
14403 			       I40E_FLAG_DCB_ENABLED	|
14404 			       I40E_FLAG_VMDQ_ENABLED);
14405 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14406 	} else {
14407 		/* Not enough queues for all TCs */
14408 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
14409 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
14410 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
14411 					I40E_FLAG_DCB_ENABLED);
14412 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
14413 		}
14414 
14415 		/* limit lan qps to the smaller of qps, cpus or msix */
14416 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
14417 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
14418 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
14419 		pf->num_lan_qps = q_max;
14420 
14421 		queues_left -= pf->num_lan_qps;
14422 	}
14423 
14424 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14425 		if (queues_left > 1) {
14426 			queues_left -= 1; /* save 1 queue for FD */
14427 		} else {
14428 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
14429 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14430 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
14431 		}
14432 	}
14433 
14434 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
14435 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
14436 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
14437 					(queues_left / pf->num_vf_qps));
14438 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
14439 	}
14440 
14441 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
14442 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
14443 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
14444 					  (queues_left / pf->num_vmdq_qps));
14445 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
14446 	}
14447 
14448 	pf->queues_left = queues_left;
14449 	dev_dbg(&pf->pdev->dev,
14450 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
14451 		pf->hw.func_caps.num_tx_qp,
14452 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
14453 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
14454 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
14455 		queues_left);
14456 }
14457 
14458 /**
14459  * i40e_setup_pf_filter_control - Setup PF static filter control
14460  * @pf: PF to be setup
14461  *
14462  * i40e_setup_pf_filter_control sets up a PF's initial filter control
14463  * settings. If PE/FCoE are enabled then it will also set the per PF
14464  * based filter sizes required for them. It also enables Flow director,
14465  * ethertype and macvlan type filter settings for the pf.
14466  *
14467  * Returns 0 on success, negative on failure
14468  **/
14469 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
14470 {
14471 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
14472 
14473 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
14474 
14475 	/* Flow Director is enabled */
14476 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
14477 		settings->enable_fdir = true;
14478 
14479 	/* Ethtype and MACVLAN filters enabled for PF */
14480 	settings->enable_ethtype = true;
14481 	settings->enable_macvlan = true;
14482 
14483 	if (i40e_set_filter_control(&pf->hw, settings))
14484 		return -ENOENT;
14485 
14486 	return 0;
14487 }
14488 
14489 #define INFO_STRING_LEN 255
14490 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
14491 static void i40e_print_features(struct i40e_pf *pf)
14492 {
14493 	struct i40e_hw *hw = &pf->hw;
14494 	char *buf;
14495 	int i;
14496 
14497 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
14498 	if (!buf)
14499 		return;
14500 
14501 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
14502 #ifdef CONFIG_PCI_IOV
14503 	i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
14504 #endif
14505 	i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
14506 		      pf->hw.func_caps.num_vsis,
14507 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
14508 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
14509 		i += snprintf(&buf[i], REMAIN(i), " RSS");
14510 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
14511 		i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
14512 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14513 		i += snprintf(&buf[i], REMAIN(i), " FD_SB");
14514 		i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
14515 	}
14516 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
14517 		i += snprintf(&buf[i], REMAIN(i), " DCB");
14518 	i += snprintf(&buf[i], REMAIN(i), " VxLAN");
14519 	i += snprintf(&buf[i], REMAIN(i), " Geneve");
14520 	if (pf->flags & I40E_FLAG_PTP)
14521 		i += snprintf(&buf[i], REMAIN(i), " PTP");
14522 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
14523 		i += snprintf(&buf[i], REMAIN(i), " VEB");
14524 	else
14525 		i += snprintf(&buf[i], REMAIN(i), " VEPA");
14526 
14527 	dev_info(&pf->pdev->dev, "%s\n", buf);
14528 	kfree(buf);
14529 	WARN_ON(i > INFO_STRING_LEN);
14530 }
14531 
14532 /**
14533  * i40e_get_platform_mac_addr - get platform-specific MAC address
14534  * @pdev: PCI device information struct
14535  * @pf: board private structure
14536  *
14537  * Look up the MAC address for the device. First we'll try
14538  * eth_platform_get_mac_address, which will check Open Firmware, or arch
14539  * specific fallback. Otherwise, we'll default to the stored value in
14540  * firmware.
14541  **/
14542 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
14543 {
14544 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
14545 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
14546 }
14547 
14548 /**
14549  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
14550  * @fec_cfg: FEC option to set in flags
14551  * @flags: ptr to flags in which we set FEC option
14552  **/
14553 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
14554 {
14555 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
14556 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
14557 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
14558 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
14559 		*flags |= I40E_FLAG_RS_FEC;
14560 		*flags &= ~I40E_FLAG_BASE_R_FEC;
14561 	}
14562 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
14563 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
14564 		*flags |= I40E_FLAG_BASE_R_FEC;
14565 		*flags &= ~I40E_FLAG_RS_FEC;
14566 	}
14567 	if (fec_cfg == 0)
14568 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
14569 }
14570 
14571 /**
14572  * i40e_check_recovery_mode - check if we are running transition firmware
14573  * @pf: board private structure
14574  *
14575  * Check registers indicating the firmware runs in recovery mode. Sets the
14576  * appropriate driver state.
14577  *
14578  * Returns true if the recovery mode was detected, false otherwise
14579  **/
14580 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
14581 {
14582 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
14583 
14584 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
14585 		dev_notice(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
14586 		dev_notice(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
14587 		set_bit(__I40E_RECOVERY_MODE, pf->state);
14588 
14589 		return true;
14590 	}
14591 	if (test_and_clear_bit(__I40E_RECOVERY_MODE, pf->state))
14592 		dev_info(&pf->pdev->dev, "Reinitializing in normal mode with full functionality.\n");
14593 
14594 	return false;
14595 }
14596 
14597 /**
14598  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
14599  * @pf: board private structure
14600  * @hw: ptr to the hardware info
14601  *
14602  * This function does a minimal setup of all subsystems needed for running
14603  * recovery mode.
14604  *
14605  * Returns 0 on success, negative on failure
14606  **/
14607 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
14608 {
14609 	struct i40e_vsi *vsi;
14610 	int err;
14611 	int v_idx;
14612 
14613 	pci_save_state(pf->pdev);
14614 
14615 	/* set up periodic task facility */
14616 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
14617 	pf->service_timer_period = HZ;
14618 
14619 	INIT_WORK(&pf->service_task, i40e_service_task);
14620 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
14621 
14622 	err = i40e_init_interrupt_scheme(pf);
14623 	if (err)
14624 		goto err_switch_setup;
14625 
14626 	/* The number of VSIs reported by the FW is the minimum guaranteed
14627 	 * to us; HW supports far more and we share the remaining pool with
14628 	 * the other PFs. We allocate space for more than the guarantee with
14629 	 * the understanding that we might not get them all later.
14630 	 */
14631 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
14632 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
14633 	else
14634 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
14635 
14636 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
14637 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
14638 			  GFP_KERNEL);
14639 	if (!pf->vsi) {
14640 		err = -ENOMEM;
14641 		goto err_switch_setup;
14642 	}
14643 
14644 	/* We allocate one VSI which is needed as absolute minimum
14645 	 * in order to register the netdev
14646 	 */
14647 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
14648 	if (v_idx < 0)
14649 		goto err_switch_setup;
14650 	pf->lan_vsi = v_idx;
14651 	vsi = pf->vsi[v_idx];
14652 	if (!vsi)
14653 		goto err_switch_setup;
14654 	vsi->alloc_queue_pairs = 1;
14655 	err = i40e_config_netdev(vsi);
14656 	if (err)
14657 		goto err_switch_setup;
14658 	err = register_netdev(vsi->netdev);
14659 	if (err)
14660 		goto err_switch_setup;
14661 	vsi->netdev_registered = true;
14662 	i40e_dbg_pf_init(pf);
14663 
14664 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
14665 	if (err)
14666 		goto err_switch_setup;
14667 
14668 	/* tell the firmware that we're starting */
14669 	i40e_send_version(pf);
14670 
14671 	/* since everything's happy, start the service_task timer */
14672 	mod_timer(&pf->service_timer,
14673 		  round_jiffies(jiffies + pf->service_timer_period));
14674 
14675 	return 0;
14676 
14677 err_switch_setup:
14678 	i40e_reset_interrupt_capability(pf);
14679 	del_timer_sync(&pf->service_timer);
14680 	i40e_shutdown_adminq(hw);
14681 	iounmap(hw->hw_addr);
14682 	pci_disable_pcie_error_reporting(pf->pdev);
14683 	pci_release_mem_regions(pf->pdev);
14684 	pci_disable_device(pf->pdev);
14685 	kfree(pf);
14686 
14687 	return err;
14688 }
14689 
14690 /**
14691  * i40e_probe - Device initialization routine
14692  * @pdev: PCI device information struct
14693  * @ent: entry in i40e_pci_tbl
14694  *
14695  * i40e_probe initializes a PF identified by a pci_dev structure.
14696  * The OS initialization, configuring of the PF private structure,
14697  * and a hardware reset occur.
14698  *
14699  * Returns 0 on success, negative on failure
14700  **/
14701 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
14702 {
14703 	struct i40e_aq_get_phy_abilities_resp abilities;
14704 	struct i40e_pf *pf;
14705 	struct i40e_hw *hw;
14706 	static u16 pfs_found;
14707 	u16 wol_nvm_bits;
14708 	u16 link_status;
14709 	int err;
14710 	u32 val;
14711 	u32 i;
14712 	u8 set_fc_aq_fail;
14713 
14714 	err = pci_enable_device_mem(pdev);
14715 	if (err)
14716 		return err;
14717 
14718 	/* set up for high or low dma */
14719 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
14720 	if (err) {
14721 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
14722 		if (err) {
14723 			dev_err(&pdev->dev,
14724 				"DMA configuration failed: 0x%x\n", err);
14725 			goto err_dma;
14726 		}
14727 	}
14728 
14729 	/* set up pci connections */
14730 	err = pci_request_mem_regions(pdev, i40e_driver_name);
14731 	if (err) {
14732 		dev_info(&pdev->dev,
14733 			 "pci_request_selected_regions failed %d\n", err);
14734 		goto err_pci_reg;
14735 	}
14736 
14737 	pci_enable_pcie_error_reporting(pdev);
14738 	pci_set_master(pdev);
14739 
14740 	/* Now that we have a PCI connection, we need to do the
14741 	 * low level device setup.  This is primarily setting up
14742 	 * the Admin Queue structures and then querying for the
14743 	 * device's current profile information.
14744 	 */
14745 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
14746 	if (!pf) {
14747 		err = -ENOMEM;
14748 		goto err_pf_alloc;
14749 	}
14750 	pf->next_vsi = 0;
14751 	pf->pdev = pdev;
14752 	set_bit(__I40E_DOWN, pf->state);
14753 
14754 	hw = &pf->hw;
14755 	hw->back = pf;
14756 
14757 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
14758 				I40E_MAX_CSR_SPACE);
14759 	/* We believe that the highest register to read is
14760 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
14761 	 * is not less than that before mapping to prevent a
14762 	 * kernel panic.
14763 	 */
14764 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
14765 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
14766 			pf->ioremap_len);
14767 		err = -ENOMEM;
14768 		goto err_ioremap;
14769 	}
14770 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
14771 	if (!hw->hw_addr) {
14772 		err = -EIO;
14773 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
14774 			 (unsigned int)pci_resource_start(pdev, 0),
14775 			 pf->ioremap_len, err);
14776 		goto err_ioremap;
14777 	}
14778 	hw->vendor_id = pdev->vendor;
14779 	hw->device_id = pdev->device;
14780 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
14781 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
14782 	hw->subsystem_device_id = pdev->subsystem_device;
14783 	hw->bus.device = PCI_SLOT(pdev->devfn);
14784 	hw->bus.func = PCI_FUNC(pdev->devfn);
14785 	hw->bus.bus_id = pdev->bus->number;
14786 	pf->instance = pfs_found;
14787 
14788 	/* Select something other than the 802.1ad ethertype for the
14789 	 * switch to use internally and drop on ingress.
14790 	 */
14791 	hw->switch_tag = 0xffff;
14792 	hw->first_tag = ETH_P_8021AD;
14793 	hw->second_tag = ETH_P_8021Q;
14794 
14795 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
14796 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
14797 	INIT_LIST_HEAD(&pf->ddp_old_prof);
14798 
14799 	/* set up the locks for the AQ, do this only once in probe
14800 	 * and destroy them only once in remove
14801 	 */
14802 	mutex_init(&hw->aq.asq_mutex);
14803 	mutex_init(&hw->aq.arq_mutex);
14804 
14805 	pf->msg_enable = netif_msg_init(debug,
14806 					NETIF_MSG_DRV |
14807 					NETIF_MSG_PROBE |
14808 					NETIF_MSG_LINK);
14809 	if (debug < -1)
14810 		pf->hw.debug_mask = debug;
14811 
14812 	/* do a special CORER for clearing PXE mode once at init */
14813 	if (hw->revision_id == 0 &&
14814 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
14815 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
14816 		i40e_flush(hw);
14817 		msleep(200);
14818 		pf->corer_count++;
14819 
14820 		i40e_clear_pxe_mode(hw);
14821 	}
14822 
14823 	/* Reset here to make sure all is clean and to define PF 'n' */
14824 	i40e_clear_hw(hw);
14825 	if (!i40e_check_recovery_mode(pf)) {
14826 		err = i40e_pf_reset(hw);
14827 		if (err) {
14828 			dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
14829 			goto err_pf_reset;
14830 		}
14831 		pf->pfr_count++;
14832 	}
14833 	hw->aq.num_arq_entries = I40E_AQ_LEN;
14834 	hw->aq.num_asq_entries = I40E_AQ_LEN;
14835 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
14836 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
14837 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
14838 
14839 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
14840 		 "%s-%s:misc",
14841 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
14842 
14843 	err = i40e_init_shared_code(hw);
14844 	if (err) {
14845 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
14846 			 err);
14847 		goto err_pf_reset;
14848 	}
14849 
14850 	/* set up a default setting for link flow control */
14851 	pf->hw.fc.requested_mode = I40E_FC_NONE;
14852 
14853 	err = i40e_init_adminq(hw);
14854 	if (err) {
14855 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
14856 			dev_info(&pdev->dev,
14857 				 "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",
14858 				 hw->aq.api_maj_ver,
14859 				 hw->aq.api_min_ver,
14860 				 I40E_FW_API_VERSION_MAJOR,
14861 				 I40E_FW_MINOR_VERSION(hw));
14862 		else
14863 			dev_info(&pdev->dev,
14864 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
14865 
14866 		goto err_pf_reset;
14867 	}
14868 	i40e_get_oem_version(hw);
14869 
14870 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
14871 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
14872 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
14873 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
14874 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
14875 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
14876 
14877 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
14878 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
14879 		dev_info(&pdev->dev,
14880 			 "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",
14881 			 hw->aq.api_maj_ver,
14882 			 hw->aq.api_min_ver,
14883 			 I40E_FW_API_VERSION_MAJOR,
14884 			 I40E_FW_MINOR_VERSION(hw));
14885 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
14886 		dev_info(&pdev->dev,
14887 			 "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",
14888 			 hw->aq.api_maj_ver,
14889 			 hw->aq.api_min_ver,
14890 			 I40E_FW_API_VERSION_MAJOR,
14891 			 I40E_FW_MINOR_VERSION(hw));
14892 
14893 	i40e_verify_eeprom(pf);
14894 
14895 	/* Rev 0 hardware was never productized */
14896 	if (hw->revision_id < 1)
14897 		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");
14898 
14899 	i40e_clear_pxe_mode(hw);
14900 
14901 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
14902 	if (err)
14903 		goto err_adminq_setup;
14904 
14905 	err = i40e_sw_init(pf);
14906 	if (err) {
14907 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
14908 		goto err_sw_init;
14909 	}
14910 
14911 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
14912 		return i40e_init_recovery_mode(pf, hw);
14913 
14914 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
14915 				hw->func_caps.num_rx_qp, 0, 0);
14916 	if (err) {
14917 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
14918 		goto err_init_lan_hmc;
14919 	}
14920 
14921 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
14922 	if (err) {
14923 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
14924 		err = -ENOENT;
14925 		goto err_configure_lan_hmc;
14926 	}
14927 
14928 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
14929 	 * Ignore error return codes because if it was already disabled via
14930 	 * hardware settings this will fail
14931 	 */
14932 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
14933 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
14934 		i40e_aq_stop_lldp(hw, true, false, NULL);
14935 	}
14936 
14937 	/* allow a platform config to override the HW addr */
14938 	i40e_get_platform_mac_addr(pdev, pf);
14939 
14940 	if (!is_valid_ether_addr(hw->mac.addr)) {
14941 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
14942 		err = -EIO;
14943 		goto err_mac_addr;
14944 	}
14945 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
14946 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
14947 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
14948 	if (is_valid_ether_addr(hw->mac.port_addr))
14949 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
14950 
14951 	pci_set_drvdata(pdev, pf);
14952 	pci_save_state(pdev);
14953 
14954 	dev_info(&pdev->dev,
14955 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
14956 			"FW LLDP is disabled\n" :
14957 			"FW LLDP is enabled\n");
14958 
14959 	/* Enable FW to write default DCB config on link-up */
14960 	i40e_aq_set_dcb_parameters(hw, true, NULL);
14961 
14962 #ifdef CONFIG_I40E_DCB
14963 	err = i40e_init_pf_dcb(pf);
14964 	if (err) {
14965 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
14966 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
14967 		/* Continue without DCB enabled */
14968 	}
14969 #endif /* CONFIG_I40E_DCB */
14970 
14971 	/* set up periodic task facility */
14972 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
14973 	pf->service_timer_period = HZ;
14974 
14975 	INIT_WORK(&pf->service_task, i40e_service_task);
14976 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
14977 
14978 	/* NVM bit on means WoL disabled for the port */
14979 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
14980 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
14981 		pf->wol_en = false;
14982 	else
14983 		pf->wol_en = true;
14984 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
14985 
14986 	/* set up the main switch operations */
14987 	i40e_determine_queue_usage(pf);
14988 	err = i40e_init_interrupt_scheme(pf);
14989 	if (err)
14990 		goto err_switch_setup;
14991 
14992 	/* The number of VSIs reported by the FW is the minimum guaranteed
14993 	 * to us; HW supports far more and we share the remaining pool with
14994 	 * the other PFs. We allocate space for more than the guarantee with
14995 	 * the understanding that we might not get them all later.
14996 	 */
14997 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
14998 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
14999 	else
15000 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15001 
15002 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15003 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15004 			  GFP_KERNEL);
15005 	if (!pf->vsi) {
15006 		err = -ENOMEM;
15007 		goto err_switch_setup;
15008 	}
15009 
15010 #ifdef CONFIG_PCI_IOV
15011 	/* prep for VF support */
15012 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15013 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15014 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15015 		if (pci_num_vf(pdev))
15016 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15017 	}
15018 #endif
15019 	err = i40e_setup_pf_switch(pf, false);
15020 	if (err) {
15021 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15022 		goto err_vsis;
15023 	}
15024 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15025 
15026 	/* Make sure flow control is set according to current settings */
15027 	err = i40e_set_fc(hw, &set_fc_aq_fail, true);
15028 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
15029 		dev_dbg(&pf->pdev->dev,
15030 			"Set fc with err %s aq_err %s on get_phy_cap\n",
15031 			i40e_stat_str(hw, err),
15032 			i40e_aq_str(hw, hw->aq.asq_last_status));
15033 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
15034 		dev_dbg(&pf->pdev->dev,
15035 			"Set fc with err %s aq_err %s on set_phy_config\n",
15036 			i40e_stat_str(hw, err),
15037 			i40e_aq_str(hw, hw->aq.asq_last_status));
15038 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
15039 		dev_dbg(&pf->pdev->dev,
15040 			"Set fc with err %s aq_err %s on get_link_info\n",
15041 			i40e_stat_str(hw, err),
15042 			i40e_aq_str(hw, hw->aq.asq_last_status));
15043 
15044 	/* if FDIR VSI was set up, start it now */
15045 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15046 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15047 			i40e_vsi_open(pf->vsi[i]);
15048 			break;
15049 		}
15050 	}
15051 
15052 	/* The driver only wants link up/down and module qualification
15053 	 * reports from firmware.  Note the negative logic.
15054 	 */
15055 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15056 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15057 					 I40E_AQ_EVENT_MEDIA_NA |
15058 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15059 	if (err)
15060 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15061 			 i40e_stat_str(&pf->hw, err),
15062 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15063 
15064 	/* Reconfigure hardware for allowing smaller MSS in the case
15065 	 * of TSO, so that we avoid the MDD being fired and causing
15066 	 * a reset in the case of small MSS+TSO.
15067 	 */
15068 	val = rd32(hw, I40E_REG_MSS);
15069 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15070 		val &= ~I40E_REG_MSS_MIN_MASK;
15071 		val |= I40E_64BYTE_MSS;
15072 		wr32(hw, I40E_REG_MSS, val);
15073 	}
15074 
15075 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15076 		msleep(75);
15077 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15078 		if (err)
15079 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15080 				 i40e_stat_str(&pf->hw, err),
15081 				 i40e_aq_str(&pf->hw,
15082 					     pf->hw.aq.asq_last_status));
15083 	}
15084 	/* The main driver is (mostly) up and happy. We need to set this state
15085 	 * before setting up the misc vector or we get a race and the vector
15086 	 * ends up disabled forever.
15087 	 */
15088 	clear_bit(__I40E_DOWN, pf->state);
15089 
15090 	/* In case of MSIX we are going to setup the misc vector right here
15091 	 * to handle admin queue events etc. In case of legacy and MSI
15092 	 * the misc functionality and queue processing is combined in
15093 	 * the same vector and that gets setup at open.
15094 	 */
15095 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15096 		err = i40e_setup_misc_vector(pf);
15097 		if (err) {
15098 			dev_info(&pdev->dev,
15099 				 "setup of misc vector failed: %d\n", err);
15100 			goto err_vsis;
15101 		}
15102 	}
15103 
15104 #ifdef CONFIG_PCI_IOV
15105 	/* prep for VF support */
15106 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15107 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15108 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15109 		/* disable link interrupts for VFs */
15110 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15111 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15112 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15113 		i40e_flush(hw);
15114 
15115 		if (pci_num_vf(pdev)) {
15116 			dev_info(&pdev->dev,
15117 				 "Active VFs found, allocating resources.\n");
15118 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15119 			if (err)
15120 				dev_info(&pdev->dev,
15121 					 "Error %d allocating resources for existing VFs\n",
15122 					 err);
15123 		}
15124 	}
15125 #endif /* CONFIG_PCI_IOV */
15126 
15127 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15128 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
15129 						      pf->num_iwarp_msix,
15130 						      I40E_IWARP_IRQ_PILE_ID);
15131 		if (pf->iwarp_base_vector < 0) {
15132 			dev_info(&pdev->dev,
15133 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
15134 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
15135 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
15136 		}
15137 	}
15138 
15139 	i40e_dbg_pf_init(pf);
15140 
15141 	/* tell the firmware that we're starting */
15142 	i40e_send_version(pf);
15143 
15144 	/* since everything's happy, start the service_task timer */
15145 	mod_timer(&pf->service_timer,
15146 		  round_jiffies(jiffies + pf->service_timer_period));
15147 
15148 	/* add this PF to client device list and launch a client service task */
15149 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15150 		err = i40e_lan_add_device(pf);
15151 		if (err)
15152 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
15153 				 err);
15154 	}
15155 
15156 #define PCI_SPEED_SIZE 8
15157 #define PCI_WIDTH_SIZE 8
15158 	/* Devices on the IOSF bus do not have this information
15159 	 * and will report PCI Gen 1 x 1 by default so don't bother
15160 	 * checking them.
15161 	 */
15162 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
15163 		char speed[PCI_SPEED_SIZE] = "Unknown";
15164 		char width[PCI_WIDTH_SIZE] = "Unknown";
15165 
15166 		/* Get the negotiated link width and speed from PCI config
15167 		 * space
15168 		 */
15169 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
15170 					  &link_status);
15171 
15172 		i40e_set_pci_config_data(hw, link_status);
15173 
15174 		switch (hw->bus.speed) {
15175 		case i40e_bus_speed_8000:
15176 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
15177 		case i40e_bus_speed_5000:
15178 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
15179 		case i40e_bus_speed_2500:
15180 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
15181 		default:
15182 			break;
15183 		}
15184 		switch (hw->bus.width) {
15185 		case i40e_bus_width_pcie_x8:
15186 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
15187 		case i40e_bus_width_pcie_x4:
15188 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
15189 		case i40e_bus_width_pcie_x2:
15190 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
15191 		case i40e_bus_width_pcie_x1:
15192 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
15193 		default:
15194 			break;
15195 		}
15196 
15197 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
15198 			 speed, width);
15199 
15200 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
15201 		    hw->bus.speed < i40e_bus_speed_8000) {
15202 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
15203 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
15204 		}
15205 	}
15206 
15207 	/* get the requested speeds from the fw */
15208 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
15209 	if (err)
15210 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
15211 			i40e_stat_str(&pf->hw, err),
15212 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15213 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
15214 
15215 	/* set the FEC config due to the board capabilities */
15216 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
15217 
15218 	/* get the supported phy types from the fw */
15219 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
15220 	if (err)
15221 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
15222 			i40e_stat_str(&pf->hw, err),
15223 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15224 
15225 	/* Add a filter to drop all Flow control frames from any VSI from being
15226 	 * transmitted. By doing so we stop a malicious VF from sending out
15227 	 * PAUSE or PFC frames and potentially controlling traffic for other
15228 	 * PF/VF VSIs.
15229 	 * The FW can still send Flow control frames if enabled.
15230 	 */
15231 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
15232 						       pf->main_vsi_seid);
15233 
15234 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
15235 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
15236 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
15237 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
15238 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
15239 	/* print a string summarizing features */
15240 	i40e_print_features(pf);
15241 
15242 	return 0;
15243 
15244 	/* Unwind what we've done if something failed in the setup */
15245 err_vsis:
15246 	set_bit(__I40E_DOWN, pf->state);
15247 	i40e_clear_interrupt_scheme(pf);
15248 	kfree(pf->vsi);
15249 err_switch_setup:
15250 	i40e_reset_interrupt_capability(pf);
15251 	del_timer_sync(&pf->service_timer);
15252 err_mac_addr:
15253 err_configure_lan_hmc:
15254 	(void)i40e_shutdown_lan_hmc(hw);
15255 err_init_lan_hmc:
15256 	kfree(pf->qp_pile);
15257 err_sw_init:
15258 err_adminq_setup:
15259 err_pf_reset:
15260 	iounmap(hw->hw_addr);
15261 err_ioremap:
15262 	kfree(pf);
15263 err_pf_alloc:
15264 	pci_disable_pcie_error_reporting(pdev);
15265 	pci_release_mem_regions(pdev);
15266 err_pci_reg:
15267 err_dma:
15268 	pci_disable_device(pdev);
15269 	return err;
15270 }
15271 
15272 /**
15273  * i40e_remove - Device removal routine
15274  * @pdev: PCI device information struct
15275  *
15276  * i40e_remove is called by the PCI subsystem to alert the driver
15277  * that is should release a PCI device.  This could be caused by a
15278  * Hot-Plug event, or because the driver is going to be removed from
15279  * memory.
15280  **/
15281 static void i40e_remove(struct pci_dev *pdev)
15282 {
15283 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15284 	struct i40e_hw *hw = &pf->hw;
15285 	i40e_status ret_code;
15286 	int i;
15287 
15288 	i40e_dbg_pf_exit(pf);
15289 
15290 	i40e_ptp_stop(pf);
15291 
15292 	/* Disable RSS in hw */
15293 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
15294 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
15295 
15296 	/* no more scheduling of any task */
15297 	set_bit(__I40E_SUSPENDED, pf->state);
15298 	set_bit(__I40E_DOWN, pf->state);
15299 	if (pf->service_timer.function)
15300 		del_timer_sync(&pf->service_timer);
15301 	if (pf->service_task.func)
15302 		cancel_work_sync(&pf->service_task);
15303 
15304 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
15305 		struct i40e_vsi *vsi = pf->vsi[0];
15306 
15307 		/* We know that we have allocated only one vsi for this PF,
15308 		 * it was just for registering netdevice, so the interface
15309 		 * could be visible in the 'ifconfig' output
15310 		 */
15311 		unregister_netdev(vsi->netdev);
15312 		free_netdev(vsi->netdev);
15313 
15314 		goto unmap;
15315 	}
15316 
15317 	/* Client close must be called explicitly here because the timer
15318 	 * has been stopped.
15319 	 */
15320 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15321 
15322 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
15323 		i40e_free_vfs(pf);
15324 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
15325 	}
15326 
15327 	i40e_fdir_teardown(pf);
15328 
15329 	/* If there is a switch structure or any orphans, remove them.
15330 	 * This will leave only the PF's VSI remaining.
15331 	 */
15332 	for (i = 0; i < I40E_MAX_VEB; i++) {
15333 		if (!pf->veb[i])
15334 			continue;
15335 
15336 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
15337 		    pf->veb[i]->uplink_seid == 0)
15338 			i40e_switch_branch_release(pf->veb[i]);
15339 	}
15340 
15341 	/* Now we can shutdown the PF's VSI, just before we kill
15342 	 * adminq and hmc.
15343 	 */
15344 	if (pf->vsi[pf->lan_vsi])
15345 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
15346 
15347 	i40e_cloud_filter_exit(pf);
15348 
15349 	/* remove attached clients */
15350 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15351 		ret_code = i40e_lan_del_device(pf);
15352 		if (ret_code)
15353 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
15354 				 ret_code);
15355 	}
15356 
15357 	/* shutdown and destroy the HMC */
15358 	if (hw->hmc.hmc_obj) {
15359 		ret_code = i40e_shutdown_lan_hmc(hw);
15360 		if (ret_code)
15361 			dev_warn(&pdev->dev,
15362 				 "Failed to destroy the HMC resources: %d\n",
15363 				 ret_code);
15364 	}
15365 
15366 unmap:
15367 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
15368 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
15369 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
15370 		free_irq(pf->pdev->irq, pf);
15371 
15372 	/* shutdown the adminq */
15373 	i40e_shutdown_adminq(hw);
15374 
15375 	/* destroy the locks only once, here */
15376 	mutex_destroy(&hw->aq.arq_mutex);
15377 	mutex_destroy(&hw->aq.asq_mutex);
15378 
15379 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
15380 	rtnl_lock();
15381 	i40e_clear_interrupt_scheme(pf);
15382 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15383 		if (pf->vsi[i]) {
15384 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
15385 				i40e_vsi_clear_rings(pf->vsi[i]);
15386 			i40e_vsi_clear(pf->vsi[i]);
15387 			pf->vsi[i] = NULL;
15388 		}
15389 	}
15390 	rtnl_unlock();
15391 
15392 	for (i = 0; i < I40E_MAX_VEB; i++) {
15393 		kfree(pf->veb[i]);
15394 		pf->veb[i] = NULL;
15395 	}
15396 
15397 	kfree(pf->qp_pile);
15398 	kfree(pf->vsi);
15399 
15400 	iounmap(hw->hw_addr);
15401 	kfree(pf);
15402 	pci_release_mem_regions(pdev);
15403 
15404 	pci_disable_pcie_error_reporting(pdev);
15405 	pci_disable_device(pdev);
15406 }
15407 
15408 /**
15409  * i40e_pci_error_detected - warning that something funky happened in PCI land
15410  * @pdev: PCI device information struct
15411  * @error: the type of PCI error
15412  *
15413  * Called to warn that something happened and the error handling steps
15414  * are in progress.  Allows the driver to quiesce things, be ready for
15415  * remediation.
15416  **/
15417 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
15418 						enum pci_channel_state error)
15419 {
15420 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15421 
15422 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
15423 
15424 	if (!pf) {
15425 		dev_info(&pdev->dev,
15426 			 "Cannot recover - error happened during device probe\n");
15427 		return PCI_ERS_RESULT_DISCONNECT;
15428 	}
15429 
15430 	/* shutdown all operations */
15431 	if (!test_bit(__I40E_SUSPENDED, pf->state))
15432 		i40e_prep_for_reset(pf, false);
15433 
15434 	/* Request a slot reset */
15435 	return PCI_ERS_RESULT_NEED_RESET;
15436 }
15437 
15438 /**
15439  * i40e_pci_error_slot_reset - a PCI slot reset just happened
15440  * @pdev: PCI device information struct
15441  *
15442  * Called to find if the driver can work with the device now that
15443  * the pci slot has been reset.  If a basic connection seems good
15444  * (registers are readable and have sane content) then return a
15445  * happy little PCI_ERS_RESULT_xxx.
15446  **/
15447 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
15448 {
15449 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15450 	pci_ers_result_t result;
15451 	u32 reg;
15452 
15453 	dev_dbg(&pdev->dev, "%s\n", __func__);
15454 	if (pci_enable_device_mem(pdev)) {
15455 		dev_info(&pdev->dev,
15456 			 "Cannot re-enable PCI device after reset.\n");
15457 		result = PCI_ERS_RESULT_DISCONNECT;
15458 	} else {
15459 		pci_set_master(pdev);
15460 		pci_restore_state(pdev);
15461 		pci_save_state(pdev);
15462 		pci_wake_from_d3(pdev, false);
15463 
15464 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
15465 		if (reg == 0)
15466 			result = PCI_ERS_RESULT_RECOVERED;
15467 		else
15468 			result = PCI_ERS_RESULT_DISCONNECT;
15469 	}
15470 
15471 	return result;
15472 }
15473 
15474 /**
15475  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
15476  * @pdev: PCI device information struct
15477  */
15478 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
15479 {
15480 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15481 
15482 	i40e_prep_for_reset(pf, false);
15483 }
15484 
15485 /**
15486  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
15487  * @pdev: PCI device information struct
15488  */
15489 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
15490 {
15491 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15492 
15493 	i40e_reset_and_rebuild(pf, false, false);
15494 }
15495 
15496 /**
15497  * i40e_pci_error_resume - restart operations after PCI error recovery
15498  * @pdev: PCI device information struct
15499  *
15500  * Called to allow the driver to bring things back up after PCI error
15501  * and/or reset recovery has finished.
15502  **/
15503 static void i40e_pci_error_resume(struct pci_dev *pdev)
15504 {
15505 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15506 
15507 	dev_dbg(&pdev->dev, "%s\n", __func__);
15508 	if (test_bit(__I40E_SUSPENDED, pf->state))
15509 		return;
15510 
15511 	i40e_handle_reset_warning(pf, false);
15512 }
15513 
15514 /**
15515  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
15516  * using the mac_address_write admin q function
15517  * @pf: pointer to i40e_pf struct
15518  **/
15519 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
15520 {
15521 	struct i40e_hw *hw = &pf->hw;
15522 	i40e_status ret;
15523 	u8 mac_addr[6];
15524 	u16 flags = 0;
15525 
15526 	/* Get current MAC address in case it's an LAA */
15527 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
15528 		ether_addr_copy(mac_addr,
15529 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
15530 	} else {
15531 		dev_err(&pf->pdev->dev,
15532 			"Failed to retrieve MAC address; using default\n");
15533 		ether_addr_copy(mac_addr, hw->mac.addr);
15534 	}
15535 
15536 	/* The FW expects the mac address write cmd to first be called with
15537 	 * one of these flags before calling it again with the multicast
15538 	 * enable flags.
15539 	 */
15540 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
15541 
15542 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
15543 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
15544 
15545 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
15546 	if (ret) {
15547 		dev_err(&pf->pdev->dev,
15548 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
15549 		return;
15550 	}
15551 
15552 	flags = I40E_AQC_MC_MAG_EN
15553 			| I40E_AQC_WOL_PRESERVE_ON_PFR
15554 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
15555 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
15556 	if (ret)
15557 		dev_err(&pf->pdev->dev,
15558 			"Failed to enable Multicast Magic Packet wake up\n");
15559 }
15560 
15561 /**
15562  * i40e_shutdown - PCI callback for shutting down
15563  * @pdev: PCI device information struct
15564  **/
15565 static void i40e_shutdown(struct pci_dev *pdev)
15566 {
15567 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15568 	struct i40e_hw *hw = &pf->hw;
15569 
15570 	set_bit(__I40E_SUSPENDED, pf->state);
15571 	set_bit(__I40E_DOWN, pf->state);
15572 
15573 	del_timer_sync(&pf->service_timer);
15574 	cancel_work_sync(&pf->service_task);
15575 	i40e_cloud_filter_exit(pf);
15576 	i40e_fdir_teardown(pf);
15577 
15578 	/* Client close must be called explicitly here because the timer
15579 	 * has been stopped.
15580 	 */
15581 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15582 
15583 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
15584 		i40e_enable_mc_magic_wake(pf);
15585 
15586 	i40e_prep_for_reset(pf, false);
15587 
15588 	wr32(hw, I40E_PFPM_APM,
15589 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
15590 	wr32(hw, I40E_PFPM_WUFC,
15591 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
15592 
15593 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
15594 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
15595 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
15596 		free_irq(pf->pdev->irq, pf);
15597 
15598 	/* Since we're going to destroy queues during the
15599 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
15600 	 * whole section
15601 	 */
15602 	rtnl_lock();
15603 	i40e_clear_interrupt_scheme(pf);
15604 	rtnl_unlock();
15605 
15606 	if (system_state == SYSTEM_POWER_OFF) {
15607 		pci_wake_from_d3(pdev, pf->wol_en);
15608 		pci_set_power_state(pdev, PCI_D3hot);
15609 	}
15610 }
15611 
15612 /**
15613  * i40e_suspend - PM callback for moving to D3
15614  * @dev: generic device information structure
15615  **/
15616 static int __maybe_unused i40e_suspend(struct device *dev)
15617 {
15618 	struct i40e_pf *pf = dev_get_drvdata(dev);
15619 	struct i40e_hw *hw = &pf->hw;
15620 
15621 	/* If we're already suspended, then there is nothing to do */
15622 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
15623 		return 0;
15624 
15625 	set_bit(__I40E_DOWN, pf->state);
15626 
15627 	/* Ensure service task will not be running */
15628 	del_timer_sync(&pf->service_timer);
15629 	cancel_work_sync(&pf->service_task);
15630 
15631 	/* Client close must be called explicitly here because the timer
15632 	 * has been stopped.
15633 	 */
15634 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15635 
15636 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
15637 		i40e_enable_mc_magic_wake(pf);
15638 
15639 	/* Since we're going to destroy queues during the
15640 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
15641 	 * whole section
15642 	 */
15643 	rtnl_lock();
15644 
15645 	i40e_prep_for_reset(pf, true);
15646 
15647 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
15648 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
15649 
15650 	/* Clear the interrupt scheme and release our IRQs so that the system
15651 	 * can safely hibernate even when there are a large number of CPUs.
15652 	 * Otherwise hibernation might fail when mapping all the vectors back
15653 	 * to CPU0.
15654 	 */
15655 	i40e_clear_interrupt_scheme(pf);
15656 
15657 	rtnl_unlock();
15658 
15659 	return 0;
15660 }
15661 
15662 /**
15663  * i40e_resume - PM callback for waking up from D3
15664  * @dev: generic device information structure
15665  **/
15666 static int __maybe_unused i40e_resume(struct device *dev)
15667 {
15668 	struct i40e_pf *pf = dev_get_drvdata(dev);
15669 	int err;
15670 
15671 	/* If we're not suspended, then there is nothing to do */
15672 	if (!test_bit(__I40E_SUSPENDED, pf->state))
15673 		return 0;
15674 
15675 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
15676 	 * since we're going to be restoring queues
15677 	 */
15678 	rtnl_lock();
15679 
15680 	/* We cleared the interrupt scheme when we suspended, so we need to
15681 	 * restore it now to resume device functionality.
15682 	 */
15683 	err = i40e_restore_interrupt_scheme(pf);
15684 	if (err) {
15685 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
15686 			err);
15687 	}
15688 
15689 	clear_bit(__I40E_DOWN, pf->state);
15690 	i40e_reset_and_rebuild(pf, false, true);
15691 
15692 	rtnl_unlock();
15693 
15694 	/* Clear suspended state last after everything is recovered */
15695 	clear_bit(__I40E_SUSPENDED, pf->state);
15696 
15697 	/* Restart the service task */
15698 	mod_timer(&pf->service_timer,
15699 		  round_jiffies(jiffies + pf->service_timer_period));
15700 
15701 	return 0;
15702 }
15703 
15704 static const struct pci_error_handlers i40e_err_handler = {
15705 	.error_detected = i40e_pci_error_detected,
15706 	.slot_reset = i40e_pci_error_slot_reset,
15707 	.reset_prepare = i40e_pci_error_reset_prepare,
15708 	.reset_done = i40e_pci_error_reset_done,
15709 	.resume = i40e_pci_error_resume,
15710 };
15711 
15712 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
15713 
15714 static struct pci_driver i40e_driver = {
15715 	.name     = i40e_driver_name,
15716 	.id_table = i40e_pci_tbl,
15717 	.probe    = i40e_probe,
15718 	.remove   = i40e_remove,
15719 	.driver   = {
15720 		.pm = &i40e_pm_ops,
15721 	},
15722 	.shutdown = i40e_shutdown,
15723 	.err_handler = &i40e_err_handler,
15724 	.sriov_configure = i40e_pci_sriov_configure,
15725 };
15726 
15727 /**
15728  * i40e_init_module - Driver registration routine
15729  *
15730  * i40e_init_module is the first routine called when the driver is
15731  * loaded. All it does is register with the PCI subsystem.
15732  **/
15733 static int __init i40e_init_module(void)
15734 {
15735 	pr_info("%s: %s - version %s\n", i40e_driver_name,
15736 		i40e_driver_string, i40e_driver_version_str);
15737 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
15738 
15739 	/* There is no need to throttle the number of active tasks because
15740 	 * each device limits its own task using a state bit for scheduling
15741 	 * the service task, and the device tasks do not interfere with each
15742 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
15743 	 * since we need to be able to guarantee forward progress even under
15744 	 * memory pressure.
15745 	 */
15746 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
15747 	if (!i40e_wq) {
15748 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
15749 		return -ENOMEM;
15750 	}
15751 
15752 	i40e_dbg_init();
15753 	return pci_register_driver(&i40e_driver);
15754 }
15755 module_init(i40e_init_module);
15756 
15757 /**
15758  * i40e_exit_module - Driver exit cleanup routine
15759  *
15760  * i40e_exit_module is called just before the driver is removed
15761  * from memory.
15762  **/
15763 static void __exit i40e_exit_module(void)
15764 {
15765 	pci_unregister_driver(&i40e_driver);
15766 	destroy_workqueue(i40e_wq);
15767 	i40e_dbg_exit();
15768 }
15769 module_exit(i40e_exit_module);
15770