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 #include <generated/utsrelease.h>
9 
10 /* Local includes */
11 #include "i40e.h"
12 #include "i40e_diag.h"
13 #include "i40e_xsk.h"
14 #include <net/udp_tunnel.h>
15 #include <net/xdp_sock_drv.h>
16 /* All i40e tracepoints are defined by the include below, which
17  * must be included exactly once across the whole kernel with
18  * CREATE_TRACE_POINTS defined
19  */
20 #define CREATE_TRACE_POINTS
21 #include "i40e_trace.h"
22 
23 const char i40e_driver_name[] = "i40e";
24 static const char i40e_driver_string[] =
25 			"Intel(R) Ethernet Connection XL710 Network Driver";
26 
27 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
28 
29 /* a bit of forward declarations */
30 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
31 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
32 static int i40e_add_vsi(struct i40e_vsi *vsi);
33 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
34 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
35 static int i40e_setup_misc_vector(struct i40e_pf *pf);
36 static void i40e_determine_queue_usage(struct i40e_pf *pf);
37 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
38 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
39 static int i40e_reset(struct i40e_pf *pf);
40 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
41 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
42 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
43 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
44 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
45 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
46 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
47 static int i40e_get_capabilities(struct i40e_pf *pf,
48 				 enum i40e_admin_queue_opc list_type);
49 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
50 
51 /* i40e_pci_tbl - PCI Device ID Table
52  *
53  * Last entry must be all 0s
54  *
55  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
56  *   Class, Class Mask, private data (not used) }
57  */
58 static const struct pci_device_id i40e_pci_tbl[] = {
59 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
60 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
61 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
62 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
63 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
64 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
83 	/* required last entry */
84 	{0, }
85 };
86 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
87 
88 #define I40E_MAX_VF_COUNT 128
89 static int debug = -1;
90 module_param(debug, uint, 0);
91 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
92 
93 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
94 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
95 MODULE_LICENSE("GPL v2");
96 
97 static struct workqueue_struct *i40e_wq;
98 
99 /**
100  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
101  * @hw:   pointer to the HW structure
102  * @mem:  ptr to mem struct to fill out
103  * @size: size of memory requested
104  * @alignment: what to align the allocation to
105  **/
106 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
107 			    u64 size, u32 alignment)
108 {
109 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
110 
111 	mem->size = ALIGN(size, alignment);
112 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
113 				     GFP_KERNEL);
114 	if (!mem->va)
115 		return -ENOMEM;
116 
117 	return 0;
118 }
119 
120 /**
121  * i40e_free_dma_mem_d - OS specific memory free for shared code
122  * @hw:   pointer to the HW structure
123  * @mem:  ptr to mem struct to free
124  **/
125 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
126 {
127 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
128 
129 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
130 	mem->va = NULL;
131 	mem->pa = 0;
132 	mem->size = 0;
133 
134 	return 0;
135 }
136 
137 /**
138  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
139  * @hw:   pointer to the HW structure
140  * @mem:  ptr to mem struct to fill out
141  * @size: size of memory requested
142  **/
143 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
144 			     u32 size)
145 {
146 	mem->size = size;
147 	mem->va = kzalloc(size, GFP_KERNEL);
148 
149 	if (!mem->va)
150 		return -ENOMEM;
151 
152 	return 0;
153 }
154 
155 /**
156  * i40e_free_virt_mem_d - OS specific memory free for shared code
157  * @hw:   pointer to the HW structure
158  * @mem:  ptr to mem struct to free
159  **/
160 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
161 {
162 	/* it's ok to kfree a NULL pointer */
163 	kfree(mem->va);
164 	mem->va = NULL;
165 	mem->size = 0;
166 
167 	return 0;
168 }
169 
170 /**
171  * i40e_get_lump - find a lump of free generic resource
172  * @pf: board private structure
173  * @pile: the pile of resource to search
174  * @needed: the number of items needed
175  * @id: an owner id to stick on the items assigned
176  *
177  * Returns the base item index of the lump, or negative for error
178  *
179  * The search_hint trick and lack of advanced fit-finding only work
180  * because we're highly likely to have all the same size lump requests.
181  * Linear search time and any fragmentation should be minimal.
182  **/
183 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
184 			 u16 needed, u16 id)
185 {
186 	int ret = -ENOMEM;
187 	int i, j;
188 
189 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
190 		dev_info(&pf->pdev->dev,
191 			 "param err: pile=%s needed=%d id=0x%04x\n",
192 			 pile ? "<valid>" : "<null>", needed, id);
193 		return -EINVAL;
194 	}
195 
196 	/* start the linear search with an imperfect hint */
197 	i = pile->search_hint;
198 	while (i < pile->num_entries) {
199 		/* skip already allocated entries */
200 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
201 			i++;
202 			continue;
203 		}
204 
205 		/* do we have enough in this lump? */
206 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
207 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
208 				break;
209 		}
210 
211 		if (j == needed) {
212 			/* there was enough, so assign it to the requestor */
213 			for (j = 0; j < needed; j++)
214 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
215 			ret = i;
216 			pile->search_hint = i + j;
217 			break;
218 		}
219 
220 		/* not enough, so skip over it and continue looking */
221 		i += j;
222 	}
223 
224 	return ret;
225 }
226 
227 /**
228  * i40e_put_lump - return a lump of generic resource
229  * @pile: the pile of resource to search
230  * @index: the base item index
231  * @id: the owner id of the items assigned
232  *
233  * Returns the count of items in the lump
234  **/
235 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
236 {
237 	int valid_id = (id | I40E_PILE_VALID_BIT);
238 	int count = 0;
239 	int i;
240 
241 	if (!pile || index >= pile->num_entries)
242 		return -EINVAL;
243 
244 	for (i = index;
245 	     i < pile->num_entries && pile->list[i] == valid_id;
246 	     i++) {
247 		pile->list[i] = 0;
248 		count++;
249 	}
250 
251 	if (count && index < pile->search_hint)
252 		pile->search_hint = index;
253 
254 	return count;
255 }
256 
257 /**
258  * i40e_find_vsi_from_id - searches for the vsi with the given id
259  * @pf: the pf structure to search for the vsi
260  * @id: id of the vsi it is searching for
261  **/
262 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
263 {
264 	int i;
265 
266 	for (i = 0; i < pf->num_alloc_vsi; i++)
267 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
268 			return pf->vsi[i];
269 
270 	return NULL;
271 }
272 
273 /**
274  * i40e_service_event_schedule - Schedule the service task to wake up
275  * @pf: board private structure
276  *
277  * If not already scheduled, this puts the task into the work queue
278  **/
279 void i40e_service_event_schedule(struct i40e_pf *pf)
280 {
281 	if ((!test_bit(__I40E_DOWN, pf->state) &&
282 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
283 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
284 		queue_work(i40e_wq, &pf->service_task);
285 }
286 
287 /**
288  * i40e_tx_timeout - Respond to a Tx Hang
289  * @netdev: network interface device structure
290  * @txqueue: queue number timing out
291  *
292  * If any port has noticed a Tx timeout, it is likely that the whole
293  * device is munged, not just the one netdev port, so go for the full
294  * reset.
295  **/
296 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
297 {
298 	struct i40e_netdev_priv *np = netdev_priv(netdev);
299 	struct i40e_vsi *vsi = np->vsi;
300 	struct i40e_pf *pf = vsi->back;
301 	struct i40e_ring *tx_ring = NULL;
302 	unsigned int i;
303 	u32 head, val;
304 
305 	pf->tx_timeout_count++;
306 
307 	/* with txqueue index, find the tx_ring struct */
308 	for (i = 0; i < vsi->num_queue_pairs; i++) {
309 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
310 			if (txqueue ==
311 			    vsi->tx_rings[i]->queue_index) {
312 				tx_ring = vsi->tx_rings[i];
313 				break;
314 			}
315 		}
316 	}
317 
318 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
319 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
320 	else if (time_before(jiffies,
321 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
322 		return;   /* don't do any new action before the next timeout */
323 
324 	/* don't kick off another recovery if one is already pending */
325 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
326 		return;
327 
328 	if (tx_ring) {
329 		head = i40e_get_head(tx_ring);
330 		/* Read interrupt register */
331 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
332 			val = rd32(&pf->hw,
333 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
334 						tx_ring->vsi->base_vector - 1));
335 		else
336 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
337 
338 		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",
339 			    vsi->seid, txqueue, tx_ring->next_to_clean,
340 			    head, tx_ring->next_to_use,
341 			    readl(tx_ring->tail), val);
342 	}
343 
344 	pf->tx_timeout_last_recovery = jiffies;
345 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
346 		    pf->tx_timeout_recovery_level, txqueue);
347 
348 	switch (pf->tx_timeout_recovery_level) {
349 	case 1:
350 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
351 		break;
352 	case 2:
353 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
354 		break;
355 	case 3:
356 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
357 		break;
358 	default:
359 		netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
360 		break;
361 	}
362 
363 	i40e_service_event_schedule(pf);
364 	pf->tx_timeout_recovery_level++;
365 }
366 
367 /**
368  * i40e_get_vsi_stats_struct - Get System Network Statistics
369  * @vsi: the VSI we care about
370  *
371  * Returns the address of the device statistics structure.
372  * The statistics are actually updated from the service task.
373  **/
374 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
375 {
376 	return &vsi->net_stats;
377 }
378 
379 /**
380  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
381  * @ring: Tx ring to get statistics from
382  * @stats: statistics entry to be updated
383  **/
384 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
385 					    struct rtnl_link_stats64 *stats)
386 {
387 	u64 bytes, packets;
388 	unsigned int start;
389 
390 	do {
391 		start = u64_stats_fetch_begin_irq(&ring->syncp);
392 		packets = ring->stats.packets;
393 		bytes   = ring->stats.bytes;
394 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
395 
396 	stats->tx_packets += packets;
397 	stats->tx_bytes   += bytes;
398 }
399 
400 /**
401  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
402  * @netdev: network interface device structure
403  * @stats: data structure to store statistics
404  *
405  * Returns the address of the device statistics structure.
406  * The statistics are actually updated from the service task.
407  **/
408 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
409 				  struct rtnl_link_stats64 *stats)
410 {
411 	struct i40e_netdev_priv *np = netdev_priv(netdev);
412 	struct i40e_vsi *vsi = np->vsi;
413 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
414 	struct i40e_ring *ring;
415 	int i;
416 
417 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
418 		return;
419 
420 	if (!vsi->tx_rings)
421 		return;
422 
423 	rcu_read_lock();
424 	for (i = 0; i < vsi->num_queue_pairs; i++) {
425 		u64 bytes, packets;
426 		unsigned int start;
427 
428 		ring = READ_ONCE(vsi->tx_rings[i]);
429 		if (!ring)
430 			continue;
431 		i40e_get_netdev_stats_struct_tx(ring, stats);
432 
433 		if (i40e_enabled_xdp_vsi(vsi)) {
434 			ring = READ_ONCE(vsi->xdp_rings[i]);
435 			if (!ring)
436 				continue;
437 			i40e_get_netdev_stats_struct_tx(ring, stats);
438 		}
439 
440 		ring = READ_ONCE(vsi->rx_rings[i]);
441 		if (!ring)
442 			continue;
443 		do {
444 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
445 			packets = ring->stats.packets;
446 			bytes   = ring->stats.bytes;
447 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
448 
449 		stats->rx_packets += packets;
450 		stats->rx_bytes   += bytes;
451 
452 	}
453 	rcu_read_unlock();
454 
455 	/* following stats updated by i40e_watchdog_subtask() */
456 	stats->multicast	= vsi_stats->multicast;
457 	stats->tx_errors	= vsi_stats->tx_errors;
458 	stats->tx_dropped	= vsi_stats->tx_dropped;
459 	stats->rx_errors	= vsi_stats->rx_errors;
460 	stats->rx_dropped	= vsi_stats->rx_dropped;
461 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
462 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
463 }
464 
465 /**
466  * i40e_vsi_reset_stats - Resets all stats of the given vsi
467  * @vsi: the VSI to have its stats reset
468  **/
469 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
470 {
471 	struct rtnl_link_stats64 *ns;
472 	int i;
473 
474 	if (!vsi)
475 		return;
476 
477 	ns = i40e_get_vsi_stats_struct(vsi);
478 	memset(ns, 0, sizeof(*ns));
479 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
480 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
481 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
482 	if (vsi->rx_rings && vsi->rx_rings[0]) {
483 		for (i = 0; i < vsi->num_queue_pairs; i++) {
484 			memset(&vsi->rx_rings[i]->stats, 0,
485 			       sizeof(vsi->rx_rings[i]->stats));
486 			memset(&vsi->rx_rings[i]->rx_stats, 0,
487 			       sizeof(vsi->rx_rings[i]->rx_stats));
488 			memset(&vsi->tx_rings[i]->stats, 0,
489 			       sizeof(vsi->tx_rings[i]->stats));
490 			memset(&vsi->tx_rings[i]->tx_stats, 0,
491 			       sizeof(vsi->tx_rings[i]->tx_stats));
492 		}
493 	}
494 	vsi->stat_offsets_loaded = false;
495 }
496 
497 /**
498  * i40e_pf_reset_stats - Reset all of the stats for the given PF
499  * @pf: the PF to be reset
500  **/
501 void i40e_pf_reset_stats(struct i40e_pf *pf)
502 {
503 	int i;
504 
505 	memset(&pf->stats, 0, sizeof(pf->stats));
506 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
507 	pf->stat_offsets_loaded = false;
508 
509 	for (i = 0; i < I40E_MAX_VEB; i++) {
510 		if (pf->veb[i]) {
511 			memset(&pf->veb[i]->stats, 0,
512 			       sizeof(pf->veb[i]->stats));
513 			memset(&pf->veb[i]->stats_offsets, 0,
514 			       sizeof(pf->veb[i]->stats_offsets));
515 			memset(&pf->veb[i]->tc_stats, 0,
516 			       sizeof(pf->veb[i]->tc_stats));
517 			memset(&pf->veb[i]->tc_stats_offsets, 0,
518 			       sizeof(pf->veb[i]->tc_stats_offsets));
519 			pf->veb[i]->stat_offsets_loaded = false;
520 		}
521 	}
522 	pf->hw_csum_rx_error = 0;
523 }
524 
525 /**
526  * i40e_stat_update48 - read and update a 48 bit stat from the chip
527  * @hw: ptr to the hardware info
528  * @hireg: the high 32 bit reg to read
529  * @loreg: the low 32 bit reg to read
530  * @offset_loaded: has the initial offset been loaded yet
531  * @offset: ptr to current offset value
532  * @stat: ptr to the stat
533  *
534  * Since the device stats are not reset at PFReset, they likely will not
535  * be zeroed when the driver starts.  We'll save the first values read
536  * and use them as offsets to be subtracted from the raw values in order
537  * to report stats that count from zero.  In the process, we also manage
538  * the potential roll-over.
539  **/
540 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
541 			       bool offset_loaded, u64 *offset, u64 *stat)
542 {
543 	u64 new_data;
544 
545 	if (hw->device_id == I40E_DEV_ID_QEMU) {
546 		new_data = rd32(hw, loreg);
547 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
548 	} else {
549 		new_data = rd64(hw, loreg);
550 	}
551 	if (!offset_loaded)
552 		*offset = new_data;
553 	if (likely(new_data >= *offset))
554 		*stat = new_data - *offset;
555 	else
556 		*stat = (new_data + BIT_ULL(48)) - *offset;
557 	*stat &= 0xFFFFFFFFFFFFULL;
558 }
559 
560 /**
561  * i40e_stat_update32 - read and update a 32 bit stat from the chip
562  * @hw: ptr to the hardware info
563  * @reg: the hw reg to read
564  * @offset_loaded: has the initial offset been loaded yet
565  * @offset: ptr to current offset value
566  * @stat: ptr to the stat
567  **/
568 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
569 			       bool offset_loaded, u64 *offset, u64 *stat)
570 {
571 	u32 new_data;
572 
573 	new_data = rd32(hw, reg);
574 	if (!offset_loaded)
575 		*offset = new_data;
576 	if (likely(new_data >= *offset))
577 		*stat = (u32)(new_data - *offset);
578 	else
579 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
580 }
581 
582 /**
583  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
584  * @hw: ptr to the hardware info
585  * @reg: the hw reg to read and clear
586  * @stat: ptr to the stat
587  **/
588 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
589 {
590 	u32 new_data = rd32(hw, reg);
591 
592 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
593 	*stat += new_data;
594 }
595 
596 /**
597  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
598  * @vsi: the VSI to be updated
599  **/
600 void i40e_update_eth_stats(struct i40e_vsi *vsi)
601 {
602 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
603 	struct i40e_pf *pf = vsi->back;
604 	struct i40e_hw *hw = &pf->hw;
605 	struct i40e_eth_stats *oes;
606 	struct i40e_eth_stats *es;     /* device's eth stats */
607 
608 	es = &vsi->eth_stats;
609 	oes = &vsi->eth_stats_offsets;
610 
611 	/* Gather up the stats that the hw collects */
612 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
613 			   vsi->stat_offsets_loaded,
614 			   &oes->tx_errors, &es->tx_errors);
615 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
616 			   vsi->stat_offsets_loaded,
617 			   &oes->rx_discards, &es->rx_discards);
618 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
619 			   vsi->stat_offsets_loaded,
620 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
621 
622 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
623 			   I40E_GLV_GORCL(stat_idx),
624 			   vsi->stat_offsets_loaded,
625 			   &oes->rx_bytes, &es->rx_bytes);
626 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
627 			   I40E_GLV_UPRCL(stat_idx),
628 			   vsi->stat_offsets_loaded,
629 			   &oes->rx_unicast, &es->rx_unicast);
630 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
631 			   I40E_GLV_MPRCL(stat_idx),
632 			   vsi->stat_offsets_loaded,
633 			   &oes->rx_multicast, &es->rx_multicast);
634 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
635 			   I40E_GLV_BPRCL(stat_idx),
636 			   vsi->stat_offsets_loaded,
637 			   &oes->rx_broadcast, &es->rx_broadcast);
638 
639 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
640 			   I40E_GLV_GOTCL(stat_idx),
641 			   vsi->stat_offsets_loaded,
642 			   &oes->tx_bytes, &es->tx_bytes);
643 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
644 			   I40E_GLV_UPTCL(stat_idx),
645 			   vsi->stat_offsets_loaded,
646 			   &oes->tx_unicast, &es->tx_unicast);
647 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
648 			   I40E_GLV_MPTCL(stat_idx),
649 			   vsi->stat_offsets_loaded,
650 			   &oes->tx_multicast, &es->tx_multicast);
651 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
652 			   I40E_GLV_BPTCL(stat_idx),
653 			   vsi->stat_offsets_loaded,
654 			   &oes->tx_broadcast, &es->tx_broadcast);
655 	vsi->stat_offsets_loaded = true;
656 }
657 
658 /**
659  * i40e_update_veb_stats - Update Switch component statistics
660  * @veb: the VEB being updated
661  **/
662 void i40e_update_veb_stats(struct i40e_veb *veb)
663 {
664 	struct i40e_pf *pf = veb->pf;
665 	struct i40e_hw *hw = &pf->hw;
666 	struct i40e_eth_stats *oes;
667 	struct i40e_eth_stats *es;     /* device's eth stats */
668 	struct i40e_veb_tc_stats *veb_oes;
669 	struct i40e_veb_tc_stats *veb_es;
670 	int i, idx = 0;
671 
672 	idx = veb->stats_idx;
673 	es = &veb->stats;
674 	oes = &veb->stats_offsets;
675 	veb_es = &veb->tc_stats;
676 	veb_oes = &veb->tc_stats_offsets;
677 
678 	/* Gather up the stats that the hw collects */
679 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
680 			   veb->stat_offsets_loaded,
681 			   &oes->tx_discards, &es->tx_discards);
682 	if (hw->revision_id > 0)
683 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
684 				   veb->stat_offsets_loaded,
685 				   &oes->rx_unknown_protocol,
686 				   &es->rx_unknown_protocol);
687 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
688 			   veb->stat_offsets_loaded,
689 			   &oes->rx_bytes, &es->rx_bytes);
690 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
691 			   veb->stat_offsets_loaded,
692 			   &oes->rx_unicast, &es->rx_unicast);
693 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
694 			   veb->stat_offsets_loaded,
695 			   &oes->rx_multicast, &es->rx_multicast);
696 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
697 			   veb->stat_offsets_loaded,
698 			   &oes->rx_broadcast, &es->rx_broadcast);
699 
700 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
701 			   veb->stat_offsets_loaded,
702 			   &oes->tx_bytes, &es->tx_bytes);
703 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
704 			   veb->stat_offsets_loaded,
705 			   &oes->tx_unicast, &es->tx_unicast);
706 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
707 			   veb->stat_offsets_loaded,
708 			   &oes->tx_multicast, &es->tx_multicast);
709 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
710 			   veb->stat_offsets_loaded,
711 			   &oes->tx_broadcast, &es->tx_broadcast);
712 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
713 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
714 				   I40E_GLVEBTC_RPCL(i, idx),
715 				   veb->stat_offsets_loaded,
716 				   &veb_oes->tc_rx_packets[i],
717 				   &veb_es->tc_rx_packets[i]);
718 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
719 				   I40E_GLVEBTC_RBCL(i, idx),
720 				   veb->stat_offsets_loaded,
721 				   &veb_oes->tc_rx_bytes[i],
722 				   &veb_es->tc_rx_bytes[i]);
723 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
724 				   I40E_GLVEBTC_TPCL(i, idx),
725 				   veb->stat_offsets_loaded,
726 				   &veb_oes->tc_tx_packets[i],
727 				   &veb_es->tc_tx_packets[i]);
728 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
729 				   I40E_GLVEBTC_TBCL(i, idx),
730 				   veb->stat_offsets_loaded,
731 				   &veb_oes->tc_tx_bytes[i],
732 				   &veb_es->tc_tx_bytes[i]);
733 	}
734 	veb->stat_offsets_loaded = true;
735 }
736 
737 /**
738  * i40e_update_vsi_stats - Update the vsi statistics counters.
739  * @vsi: the VSI to be updated
740  *
741  * There are a few instances where we store the same stat in a
742  * couple of different structs.  This is partly because we have
743  * the netdev stats that need to be filled out, which is slightly
744  * different from the "eth_stats" defined by the chip and used in
745  * VF communications.  We sort it out here.
746  **/
747 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
748 {
749 	struct i40e_pf *pf = vsi->back;
750 	struct rtnl_link_stats64 *ons;
751 	struct rtnl_link_stats64 *ns;   /* netdev stats */
752 	struct i40e_eth_stats *oes;
753 	struct i40e_eth_stats *es;     /* device's eth stats */
754 	u32 tx_restart, tx_busy;
755 	struct i40e_ring *p;
756 	u32 rx_page, rx_buf;
757 	u64 bytes, packets;
758 	unsigned int start;
759 	u64 tx_linearize;
760 	u64 tx_force_wb;
761 	u64 rx_p, rx_b;
762 	u64 tx_p, tx_b;
763 	u16 q;
764 
765 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
766 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
767 		return;
768 
769 	ns = i40e_get_vsi_stats_struct(vsi);
770 	ons = &vsi->net_stats_offsets;
771 	es = &vsi->eth_stats;
772 	oes = &vsi->eth_stats_offsets;
773 
774 	/* Gather up the netdev and vsi stats that the driver collects
775 	 * on the fly during packet processing
776 	 */
777 	rx_b = rx_p = 0;
778 	tx_b = tx_p = 0;
779 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
780 	rx_page = 0;
781 	rx_buf = 0;
782 	rcu_read_lock();
783 	for (q = 0; q < vsi->num_queue_pairs; q++) {
784 		/* locate Tx ring */
785 		p = READ_ONCE(vsi->tx_rings[q]);
786 		if (!p)
787 			continue;
788 
789 		do {
790 			start = u64_stats_fetch_begin_irq(&p->syncp);
791 			packets = p->stats.packets;
792 			bytes = p->stats.bytes;
793 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
794 		tx_b += bytes;
795 		tx_p += packets;
796 		tx_restart += p->tx_stats.restart_queue;
797 		tx_busy += p->tx_stats.tx_busy;
798 		tx_linearize += p->tx_stats.tx_linearize;
799 		tx_force_wb += p->tx_stats.tx_force_wb;
800 
801 		/* locate Rx ring */
802 		p = READ_ONCE(vsi->rx_rings[q]);
803 		if (!p)
804 			continue;
805 
806 		do {
807 			start = u64_stats_fetch_begin_irq(&p->syncp);
808 			packets = p->stats.packets;
809 			bytes = p->stats.bytes;
810 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
811 		rx_b += bytes;
812 		rx_p += packets;
813 		rx_buf += p->rx_stats.alloc_buff_failed;
814 		rx_page += p->rx_stats.alloc_page_failed;
815 
816 		if (i40e_enabled_xdp_vsi(vsi)) {
817 			/* locate XDP ring */
818 			p = READ_ONCE(vsi->xdp_rings[q]);
819 			if (!p)
820 				continue;
821 
822 			do {
823 				start = u64_stats_fetch_begin_irq(&p->syncp);
824 				packets = p->stats.packets;
825 				bytes = p->stats.bytes;
826 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
827 			tx_b += bytes;
828 			tx_p += packets;
829 			tx_restart += p->tx_stats.restart_queue;
830 			tx_busy += p->tx_stats.tx_busy;
831 			tx_linearize += p->tx_stats.tx_linearize;
832 			tx_force_wb += p->tx_stats.tx_force_wb;
833 		}
834 	}
835 	rcu_read_unlock();
836 	vsi->tx_restart = tx_restart;
837 	vsi->tx_busy = tx_busy;
838 	vsi->tx_linearize = tx_linearize;
839 	vsi->tx_force_wb = tx_force_wb;
840 	vsi->rx_page_failed = rx_page;
841 	vsi->rx_buf_failed = rx_buf;
842 
843 	ns->rx_packets = rx_p;
844 	ns->rx_bytes = rx_b;
845 	ns->tx_packets = tx_p;
846 	ns->tx_bytes = tx_b;
847 
848 	/* update netdev stats from eth stats */
849 	i40e_update_eth_stats(vsi);
850 	ons->tx_errors = oes->tx_errors;
851 	ns->tx_errors = es->tx_errors;
852 	ons->multicast = oes->rx_multicast;
853 	ns->multicast = es->rx_multicast;
854 	ons->rx_dropped = oes->rx_discards;
855 	ns->rx_dropped = es->rx_discards;
856 	ons->tx_dropped = oes->tx_discards;
857 	ns->tx_dropped = es->tx_discards;
858 
859 	/* pull in a couple PF stats if this is the main vsi */
860 	if (vsi == pf->vsi[pf->lan_vsi]) {
861 		ns->rx_crc_errors = pf->stats.crc_errors;
862 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
863 		ns->rx_length_errors = pf->stats.rx_length_errors;
864 	}
865 }
866 
867 /**
868  * i40e_update_pf_stats - Update the PF statistics counters.
869  * @pf: the PF to be updated
870  **/
871 static void i40e_update_pf_stats(struct i40e_pf *pf)
872 {
873 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
874 	struct i40e_hw_port_stats *nsd = &pf->stats;
875 	struct i40e_hw *hw = &pf->hw;
876 	u32 val;
877 	int i;
878 
879 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
880 			   I40E_GLPRT_GORCL(hw->port),
881 			   pf->stat_offsets_loaded,
882 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
883 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
884 			   I40E_GLPRT_GOTCL(hw->port),
885 			   pf->stat_offsets_loaded,
886 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
887 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
888 			   pf->stat_offsets_loaded,
889 			   &osd->eth.rx_discards,
890 			   &nsd->eth.rx_discards);
891 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
892 			   I40E_GLPRT_UPRCL(hw->port),
893 			   pf->stat_offsets_loaded,
894 			   &osd->eth.rx_unicast,
895 			   &nsd->eth.rx_unicast);
896 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
897 			   I40E_GLPRT_MPRCL(hw->port),
898 			   pf->stat_offsets_loaded,
899 			   &osd->eth.rx_multicast,
900 			   &nsd->eth.rx_multicast);
901 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
902 			   I40E_GLPRT_BPRCL(hw->port),
903 			   pf->stat_offsets_loaded,
904 			   &osd->eth.rx_broadcast,
905 			   &nsd->eth.rx_broadcast);
906 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
907 			   I40E_GLPRT_UPTCL(hw->port),
908 			   pf->stat_offsets_loaded,
909 			   &osd->eth.tx_unicast,
910 			   &nsd->eth.tx_unicast);
911 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
912 			   I40E_GLPRT_MPTCL(hw->port),
913 			   pf->stat_offsets_loaded,
914 			   &osd->eth.tx_multicast,
915 			   &nsd->eth.tx_multicast);
916 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
917 			   I40E_GLPRT_BPTCL(hw->port),
918 			   pf->stat_offsets_loaded,
919 			   &osd->eth.tx_broadcast,
920 			   &nsd->eth.tx_broadcast);
921 
922 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
923 			   pf->stat_offsets_loaded,
924 			   &osd->tx_dropped_link_down,
925 			   &nsd->tx_dropped_link_down);
926 
927 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
928 			   pf->stat_offsets_loaded,
929 			   &osd->crc_errors, &nsd->crc_errors);
930 
931 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
932 			   pf->stat_offsets_loaded,
933 			   &osd->illegal_bytes, &nsd->illegal_bytes);
934 
935 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
936 			   pf->stat_offsets_loaded,
937 			   &osd->mac_local_faults,
938 			   &nsd->mac_local_faults);
939 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
940 			   pf->stat_offsets_loaded,
941 			   &osd->mac_remote_faults,
942 			   &nsd->mac_remote_faults);
943 
944 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
945 			   pf->stat_offsets_loaded,
946 			   &osd->rx_length_errors,
947 			   &nsd->rx_length_errors);
948 
949 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
950 			   pf->stat_offsets_loaded,
951 			   &osd->link_xon_rx, &nsd->link_xon_rx);
952 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
953 			   pf->stat_offsets_loaded,
954 			   &osd->link_xon_tx, &nsd->link_xon_tx);
955 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
956 			   pf->stat_offsets_loaded,
957 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
958 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
959 			   pf->stat_offsets_loaded,
960 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
961 
962 	for (i = 0; i < 8; i++) {
963 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
964 				   pf->stat_offsets_loaded,
965 				   &osd->priority_xoff_rx[i],
966 				   &nsd->priority_xoff_rx[i]);
967 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
968 				   pf->stat_offsets_loaded,
969 				   &osd->priority_xon_rx[i],
970 				   &nsd->priority_xon_rx[i]);
971 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
972 				   pf->stat_offsets_loaded,
973 				   &osd->priority_xon_tx[i],
974 				   &nsd->priority_xon_tx[i]);
975 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
976 				   pf->stat_offsets_loaded,
977 				   &osd->priority_xoff_tx[i],
978 				   &nsd->priority_xoff_tx[i]);
979 		i40e_stat_update32(hw,
980 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
981 				   pf->stat_offsets_loaded,
982 				   &osd->priority_xon_2_xoff[i],
983 				   &nsd->priority_xon_2_xoff[i]);
984 	}
985 
986 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
987 			   I40E_GLPRT_PRC64L(hw->port),
988 			   pf->stat_offsets_loaded,
989 			   &osd->rx_size_64, &nsd->rx_size_64);
990 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
991 			   I40E_GLPRT_PRC127L(hw->port),
992 			   pf->stat_offsets_loaded,
993 			   &osd->rx_size_127, &nsd->rx_size_127);
994 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
995 			   I40E_GLPRT_PRC255L(hw->port),
996 			   pf->stat_offsets_loaded,
997 			   &osd->rx_size_255, &nsd->rx_size_255);
998 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
999 			   I40E_GLPRT_PRC511L(hw->port),
1000 			   pf->stat_offsets_loaded,
1001 			   &osd->rx_size_511, &nsd->rx_size_511);
1002 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1003 			   I40E_GLPRT_PRC1023L(hw->port),
1004 			   pf->stat_offsets_loaded,
1005 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1006 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1007 			   I40E_GLPRT_PRC1522L(hw->port),
1008 			   pf->stat_offsets_loaded,
1009 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1010 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1011 			   I40E_GLPRT_PRC9522L(hw->port),
1012 			   pf->stat_offsets_loaded,
1013 			   &osd->rx_size_big, &nsd->rx_size_big);
1014 
1015 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1016 			   I40E_GLPRT_PTC64L(hw->port),
1017 			   pf->stat_offsets_loaded,
1018 			   &osd->tx_size_64, &nsd->tx_size_64);
1019 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1020 			   I40E_GLPRT_PTC127L(hw->port),
1021 			   pf->stat_offsets_loaded,
1022 			   &osd->tx_size_127, &nsd->tx_size_127);
1023 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1024 			   I40E_GLPRT_PTC255L(hw->port),
1025 			   pf->stat_offsets_loaded,
1026 			   &osd->tx_size_255, &nsd->tx_size_255);
1027 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1028 			   I40E_GLPRT_PTC511L(hw->port),
1029 			   pf->stat_offsets_loaded,
1030 			   &osd->tx_size_511, &nsd->tx_size_511);
1031 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1032 			   I40E_GLPRT_PTC1023L(hw->port),
1033 			   pf->stat_offsets_loaded,
1034 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1035 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1036 			   I40E_GLPRT_PTC1522L(hw->port),
1037 			   pf->stat_offsets_loaded,
1038 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1039 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1040 			   I40E_GLPRT_PTC9522L(hw->port),
1041 			   pf->stat_offsets_loaded,
1042 			   &osd->tx_size_big, &nsd->tx_size_big);
1043 
1044 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1045 			   pf->stat_offsets_loaded,
1046 			   &osd->rx_undersize, &nsd->rx_undersize);
1047 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1048 			   pf->stat_offsets_loaded,
1049 			   &osd->rx_fragments, &nsd->rx_fragments);
1050 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1051 			   pf->stat_offsets_loaded,
1052 			   &osd->rx_oversize, &nsd->rx_oversize);
1053 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1054 			   pf->stat_offsets_loaded,
1055 			   &osd->rx_jabber, &nsd->rx_jabber);
1056 
1057 	/* FDIR stats */
1058 	i40e_stat_update_and_clear32(hw,
1059 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1060 			&nsd->fd_atr_match);
1061 	i40e_stat_update_and_clear32(hw,
1062 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1063 			&nsd->fd_sb_match);
1064 	i40e_stat_update_and_clear32(hw,
1065 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1066 			&nsd->fd_atr_tunnel_match);
1067 
1068 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1069 	nsd->tx_lpi_status =
1070 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1071 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1072 	nsd->rx_lpi_status =
1073 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1074 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1075 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1076 			   pf->stat_offsets_loaded,
1077 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1078 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1079 			   pf->stat_offsets_loaded,
1080 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1081 
1082 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1083 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1084 		nsd->fd_sb_status = true;
1085 	else
1086 		nsd->fd_sb_status = false;
1087 
1088 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1089 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1090 		nsd->fd_atr_status = true;
1091 	else
1092 		nsd->fd_atr_status = false;
1093 
1094 	pf->stat_offsets_loaded = true;
1095 }
1096 
1097 /**
1098  * i40e_update_stats - Update the various statistics counters.
1099  * @vsi: the VSI to be updated
1100  *
1101  * Update the various stats for this VSI and its related entities.
1102  **/
1103 void i40e_update_stats(struct i40e_vsi *vsi)
1104 {
1105 	struct i40e_pf *pf = vsi->back;
1106 
1107 	if (vsi == pf->vsi[pf->lan_vsi])
1108 		i40e_update_pf_stats(pf);
1109 
1110 	i40e_update_vsi_stats(vsi);
1111 }
1112 
1113 /**
1114  * i40e_count_filters - counts VSI mac filters
1115  * @vsi: the VSI to be searched
1116  *
1117  * Returns count of mac filters
1118  **/
1119 int i40e_count_filters(struct i40e_vsi *vsi)
1120 {
1121 	struct i40e_mac_filter *f;
1122 	struct hlist_node *h;
1123 	int bkt;
1124 	int cnt = 0;
1125 
1126 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1127 		++cnt;
1128 
1129 	return cnt;
1130 }
1131 
1132 /**
1133  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1134  * @vsi: the VSI to be searched
1135  * @macaddr: the MAC address
1136  * @vlan: the vlan
1137  *
1138  * Returns ptr to the filter object or NULL
1139  **/
1140 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1141 						const u8 *macaddr, s16 vlan)
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 		    (vlan == f->vlan))
1153 			return f;
1154 	}
1155 	return NULL;
1156 }
1157 
1158 /**
1159  * i40e_find_mac - Find a mac addr in the macvlan filters list
1160  * @vsi: the VSI to be searched
1161  * @macaddr: the MAC address we are searching for
1162  *
1163  * Returns the first filter with the provided MAC address or NULL if
1164  * MAC address was not found
1165  **/
1166 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1167 {
1168 	struct i40e_mac_filter *f;
1169 	u64 key;
1170 
1171 	if (!vsi || !macaddr)
1172 		return NULL;
1173 
1174 	key = i40e_addr_to_hkey(macaddr);
1175 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1176 		if ((ether_addr_equal(macaddr, f->macaddr)))
1177 			return f;
1178 	}
1179 	return NULL;
1180 }
1181 
1182 /**
1183  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1184  * @vsi: the VSI to be searched
1185  *
1186  * Returns true if VSI is in vlan mode or false otherwise
1187  **/
1188 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1189 {
1190 	/* If we have a PVID, always operate in VLAN mode */
1191 	if (vsi->info.pvid)
1192 		return true;
1193 
1194 	/* We need to operate in VLAN mode whenever we have any filters with
1195 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1196 	 * time, incurring search cost repeatedly. However, we can notice two
1197 	 * things:
1198 	 *
1199 	 * 1) the only place where we can gain a VLAN filter is in
1200 	 *    i40e_add_filter.
1201 	 *
1202 	 * 2) the only place where filters are actually removed is in
1203 	 *    i40e_sync_filters_subtask.
1204 	 *
1205 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1206 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1207 	 * we have to perform the full search after deleting filters in
1208 	 * i40e_sync_filters_subtask, but we already have to search
1209 	 * filters here and can perform the check at the same time. This
1210 	 * results in avoiding embedding a loop for VLAN mode inside another
1211 	 * loop over all the filters, and should maintain correctness as noted
1212 	 * above.
1213 	 */
1214 	return vsi->has_vlan_filter;
1215 }
1216 
1217 /**
1218  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1219  * @vsi: the VSI to configure
1220  * @tmp_add_list: list of filters ready to be added
1221  * @tmp_del_list: list of filters ready to be deleted
1222  * @vlan_filters: the number of active VLAN filters
1223  *
1224  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1225  * behave as expected. If we have any active VLAN filters remaining or about
1226  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1227  * so that they only match against untagged traffic. If we no longer have any
1228  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1229  * so that they match against both tagged and untagged traffic. In this way,
1230  * we ensure that we correctly receive the desired traffic. This ensures that
1231  * when we have an active VLAN we will receive only untagged traffic and
1232  * traffic matching active VLANs. If we have no active VLANs then we will
1233  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1234  *
1235  * Finally, in a similar fashion, this function also corrects filters when
1236  * there is an active PVID assigned to this VSI.
1237  *
1238  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1239  *
1240  * This function is only expected to be called from within
1241  * i40e_sync_vsi_filters.
1242  *
1243  * NOTE: This function expects to be called while under the
1244  * mac_filter_hash_lock
1245  */
1246 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1247 					 struct hlist_head *tmp_add_list,
1248 					 struct hlist_head *tmp_del_list,
1249 					 int vlan_filters)
1250 {
1251 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1252 	struct i40e_mac_filter *f, *add_head;
1253 	struct i40e_new_mac_filter *new;
1254 	struct hlist_node *h;
1255 	int bkt, new_vlan;
1256 
1257 	/* To determine if a particular filter needs to be replaced we
1258 	 * have the three following conditions:
1259 	 *
1260 	 * a) if we have a PVID assigned, then all filters which are
1261 	 *    not marked as VLAN=PVID must be replaced with filters that
1262 	 *    are.
1263 	 * b) otherwise, if we have any active VLANS, all filters
1264 	 *    which are marked as VLAN=-1 must be replaced with
1265 	 *    filters marked as VLAN=0
1266 	 * c) finally, if we do not have any active VLANS, all filters
1267 	 *    which are marked as VLAN=0 must be replaced with filters
1268 	 *    marked as VLAN=-1
1269 	 */
1270 
1271 	/* Update the filters about to be added in place */
1272 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1273 		if (pvid && new->f->vlan != pvid)
1274 			new->f->vlan = pvid;
1275 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1276 			new->f->vlan = 0;
1277 		else if (!vlan_filters && new->f->vlan == 0)
1278 			new->f->vlan = I40E_VLAN_ANY;
1279 	}
1280 
1281 	/* Update the remaining active filters */
1282 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1283 		/* Combine the checks for whether a filter needs to be changed
1284 		 * and then determine the new VLAN inside the if block, in
1285 		 * order to avoid duplicating code for adding the new filter
1286 		 * then deleting the old filter.
1287 		 */
1288 		if ((pvid && f->vlan != pvid) ||
1289 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1290 		    (!vlan_filters && f->vlan == 0)) {
1291 			/* Determine the new vlan we will be adding */
1292 			if (pvid)
1293 				new_vlan = pvid;
1294 			else if (vlan_filters)
1295 				new_vlan = 0;
1296 			else
1297 				new_vlan = I40E_VLAN_ANY;
1298 
1299 			/* Create the new filter */
1300 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1301 			if (!add_head)
1302 				return -ENOMEM;
1303 
1304 			/* Create a temporary i40e_new_mac_filter */
1305 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1306 			if (!new)
1307 				return -ENOMEM;
1308 
1309 			new->f = add_head;
1310 			new->state = add_head->state;
1311 
1312 			/* Add the new filter to the tmp list */
1313 			hlist_add_head(&new->hlist, tmp_add_list);
1314 
1315 			/* Put the original filter into the delete list */
1316 			f->state = I40E_FILTER_REMOVE;
1317 			hash_del(&f->hlist);
1318 			hlist_add_head(&f->hlist, tmp_del_list);
1319 		}
1320 	}
1321 
1322 	vsi->has_vlan_filter = !!vlan_filters;
1323 
1324 	return 0;
1325 }
1326 
1327 /**
1328  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1329  * @vsi: the PF Main VSI - inappropriate for any other VSI
1330  * @macaddr: the MAC address
1331  *
1332  * Remove whatever filter the firmware set up so the driver can manage
1333  * its own filtering intelligently.
1334  **/
1335 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1336 {
1337 	struct i40e_aqc_remove_macvlan_element_data element;
1338 	struct i40e_pf *pf = vsi->back;
1339 
1340 	/* Only appropriate for the PF main VSI */
1341 	if (vsi->type != I40E_VSI_MAIN)
1342 		return;
1343 
1344 	memset(&element, 0, sizeof(element));
1345 	ether_addr_copy(element.mac_addr, macaddr);
1346 	element.vlan_tag = 0;
1347 	/* Ignore error returns, some firmware does it this way... */
1348 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1349 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1350 
1351 	memset(&element, 0, sizeof(element));
1352 	ether_addr_copy(element.mac_addr, macaddr);
1353 	element.vlan_tag = 0;
1354 	/* ...and some firmware does it this way. */
1355 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1356 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1357 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1358 }
1359 
1360 /**
1361  * i40e_add_filter - Add a mac/vlan filter to the VSI
1362  * @vsi: the VSI to be searched
1363  * @macaddr: the MAC address
1364  * @vlan: the vlan
1365  *
1366  * Returns ptr to the filter object or NULL when no memory available.
1367  *
1368  * NOTE: This function is expected to be called with mac_filter_hash_lock
1369  * being held.
1370  **/
1371 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1372 					const u8 *macaddr, s16 vlan)
1373 {
1374 	struct i40e_mac_filter *f;
1375 	u64 key;
1376 
1377 	if (!vsi || !macaddr)
1378 		return NULL;
1379 
1380 	f = i40e_find_filter(vsi, macaddr, vlan);
1381 	if (!f) {
1382 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1383 		if (!f)
1384 			return NULL;
1385 
1386 		/* Update the boolean indicating if we need to function in
1387 		 * VLAN mode.
1388 		 */
1389 		if (vlan >= 0)
1390 			vsi->has_vlan_filter = true;
1391 
1392 		ether_addr_copy(f->macaddr, macaddr);
1393 		f->vlan = vlan;
1394 		f->state = I40E_FILTER_NEW;
1395 		INIT_HLIST_NODE(&f->hlist);
1396 
1397 		key = i40e_addr_to_hkey(macaddr);
1398 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1399 
1400 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1401 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1402 	}
1403 
1404 	/* If we're asked to add a filter that has been marked for removal, it
1405 	 * is safe to simply restore it to active state. __i40e_del_filter
1406 	 * will have simply deleted any filters which were previously marked
1407 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1408 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1409 	 * task, just restore this filter to the ACTIVE state so that the
1410 	 * sync task leaves it in place
1411 	 */
1412 	if (f->state == I40E_FILTER_REMOVE)
1413 		f->state = I40E_FILTER_ACTIVE;
1414 
1415 	return f;
1416 }
1417 
1418 /**
1419  * __i40e_del_filter - Remove a specific filter from the VSI
1420  * @vsi: VSI to remove from
1421  * @f: the filter to remove from the list
1422  *
1423  * This function should be called instead of i40e_del_filter only if you know
1424  * the exact filter you will remove already, such as via i40e_find_filter or
1425  * i40e_find_mac.
1426  *
1427  * NOTE: This function is expected to be called with mac_filter_hash_lock
1428  * being held.
1429  * ANOTHER NOTE: This function MUST be called from within the context of
1430  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1431  * instead of list_for_each_entry().
1432  **/
1433 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1434 {
1435 	if (!f)
1436 		return;
1437 
1438 	/* If the filter was never added to firmware then we can just delete it
1439 	 * directly and we don't want to set the status to remove or else an
1440 	 * admin queue command will unnecessarily fire.
1441 	 */
1442 	if ((f->state == I40E_FILTER_FAILED) ||
1443 	    (f->state == I40E_FILTER_NEW)) {
1444 		hash_del(&f->hlist);
1445 		kfree(f);
1446 	} else {
1447 		f->state = I40E_FILTER_REMOVE;
1448 	}
1449 
1450 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1451 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1452 }
1453 
1454 /**
1455  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1456  * @vsi: the VSI to be searched
1457  * @macaddr: the MAC address
1458  * @vlan: the VLAN
1459  *
1460  * NOTE: This function is expected to be called with mac_filter_hash_lock
1461  * being held.
1462  * ANOTHER NOTE: This function MUST be called from within the context of
1463  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1464  * instead of list_for_each_entry().
1465  **/
1466 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1467 {
1468 	struct i40e_mac_filter *f;
1469 
1470 	if (!vsi || !macaddr)
1471 		return;
1472 
1473 	f = i40e_find_filter(vsi, macaddr, vlan);
1474 	__i40e_del_filter(vsi, f);
1475 }
1476 
1477 /**
1478  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1479  * @vsi: the VSI to be searched
1480  * @macaddr: the mac address to be filtered
1481  *
1482  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1483  * go through all the macvlan filters and add a macvlan filter for each
1484  * unique vlan that already exists. If a PVID has been assigned, instead only
1485  * add the macaddr to that VLAN.
1486  *
1487  * Returns last filter added on success, else NULL
1488  **/
1489 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1490 					    const u8 *macaddr)
1491 {
1492 	struct i40e_mac_filter *f, *add = NULL;
1493 	struct hlist_node *h;
1494 	int bkt;
1495 
1496 	if (vsi->info.pvid)
1497 		return i40e_add_filter(vsi, macaddr,
1498 				       le16_to_cpu(vsi->info.pvid));
1499 
1500 	if (!i40e_is_vsi_in_vlan(vsi))
1501 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1502 
1503 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1504 		if (f->state == I40E_FILTER_REMOVE)
1505 			continue;
1506 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1507 		if (!add)
1508 			return NULL;
1509 	}
1510 
1511 	return add;
1512 }
1513 
1514 /**
1515  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1516  * @vsi: the VSI to be searched
1517  * @macaddr: the mac address to be removed
1518  *
1519  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1520  * associated with.
1521  *
1522  * Returns 0 for success, or error
1523  **/
1524 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1525 {
1526 	struct i40e_mac_filter *f;
1527 	struct hlist_node *h;
1528 	bool found = false;
1529 	int bkt;
1530 
1531 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1532 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1533 		if (ether_addr_equal(macaddr, f->macaddr)) {
1534 			__i40e_del_filter(vsi, f);
1535 			found = true;
1536 		}
1537 	}
1538 
1539 	if (found)
1540 		return 0;
1541 	else
1542 		return -ENOENT;
1543 }
1544 
1545 /**
1546  * i40e_set_mac - NDO callback to set mac address
1547  * @netdev: network interface device structure
1548  * @p: pointer to an address structure
1549  *
1550  * Returns 0 on success, negative on failure
1551  **/
1552 static int i40e_set_mac(struct net_device *netdev, void *p)
1553 {
1554 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1555 	struct i40e_vsi *vsi = np->vsi;
1556 	struct i40e_pf *pf = vsi->back;
1557 	struct i40e_hw *hw = &pf->hw;
1558 	struct sockaddr *addr = p;
1559 
1560 	if (!is_valid_ether_addr(addr->sa_data))
1561 		return -EADDRNOTAVAIL;
1562 
1563 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1564 		netdev_info(netdev, "already using mac address %pM\n",
1565 			    addr->sa_data);
1566 		return 0;
1567 	}
1568 
1569 	if (test_bit(__I40E_DOWN, pf->state) ||
1570 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1571 		return -EADDRNOTAVAIL;
1572 
1573 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1574 		netdev_info(netdev, "returning to hw mac address %pM\n",
1575 			    hw->mac.addr);
1576 	else
1577 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1578 
1579 	/* Copy the address first, so that we avoid a possible race with
1580 	 * .set_rx_mode().
1581 	 * - Remove old address from MAC filter
1582 	 * - Copy new address
1583 	 * - Add new address to MAC filter
1584 	 */
1585 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1586 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1587 	ether_addr_copy(netdev->dev_addr, addr->sa_data);
1588 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1589 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1590 
1591 	if (vsi->type == I40E_VSI_MAIN) {
1592 		i40e_status ret;
1593 
1594 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1595 						addr->sa_data, NULL);
1596 		if (ret)
1597 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1598 				    i40e_stat_str(hw, ret),
1599 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1600 	}
1601 
1602 	/* schedule our worker thread which will take care of
1603 	 * applying the new filter changes
1604 	 */
1605 	i40e_service_event_schedule(pf);
1606 	return 0;
1607 }
1608 
1609 /**
1610  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1611  * @vsi: vsi structure
1612  * @seed: RSS hash seed
1613  * @lut: pointer to lookup table of lut_size
1614  * @lut_size: size of the lookup table
1615  **/
1616 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1617 			      u8 *lut, u16 lut_size)
1618 {
1619 	struct i40e_pf *pf = vsi->back;
1620 	struct i40e_hw *hw = &pf->hw;
1621 	int ret = 0;
1622 
1623 	if (seed) {
1624 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1625 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1626 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1627 		if (ret) {
1628 			dev_info(&pf->pdev->dev,
1629 				 "Cannot set RSS key, err %s aq_err %s\n",
1630 				 i40e_stat_str(hw, ret),
1631 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1632 			return ret;
1633 		}
1634 	}
1635 	if (lut) {
1636 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1637 
1638 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1639 		if (ret) {
1640 			dev_info(&pf->pdev->dev,
1641 				 "Cannot set RSS lut, err %s aq_err %s\n",
1642 				 i40e_stat_str(hw, ret),
1643 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1644 			return ret;
1645 		}
1646 	}
1647 	return ret;
1648 }
1649 
1650 /**
1651  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1652  * @vsi: VSI structure
1653  **/
1654 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1655 {
1656 	struct i40e_pf *pf = vsi->back;
1657 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1658 	u8 *lut;
1659 	int ret;
1660 
1661 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1662 		return 0;
1663 	if (!vsi->rss_size)
1664 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1665 				      vsi->num_queue_pairs);
1666 	if (!vsi->rss_size)
1667 		return -EINVAL;
1668 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1669 	if (!lut)
1670 		return -ENOMEM;
1671 
1672 	/* Use the user configured hash keys and lookup table if there is one,
1673 	 * otherwise use default
1674 	 */
1675 	if (vsi->rss_lut_user)
1676 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1677 	else
1678 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1679 	if (vsi->rss_hkey_user)
1680 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1681 	else
1682 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1683 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1684 	kfree(lut);
1685 	return ret;
1686 }
1687 
1688 /**
1689  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1690  * @vsi: the VSI being configured,
1691  * @ctxt: VSI context structure
1692  * @enabled_tc: number of traffic classes to enable
1693  *
1694  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1695  **/
1696 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1697 					   struct i40e_vsi_context *ctxt,
1698 					   u8 enabled_tc)
1699 {
1700 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1701 	int i, override_q, pow, num_qps, ret;
1702 	u8 netdev_tc = 0, offset = 0;
1703 
1704 	if (vsi->type != I40E_VSI_MAIN)
1705 		return -EINVAL;
1706 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1707 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1708 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1709 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1710 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1711 
1712 	/* find the next higher power-of-2 of num queue pairs */
1713 	pow = ilog2(num_qps);
1714 	if (!is_power_of_2(num_qps))
1715 		pow++;
1716 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1717 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1718 
1719 	/* Setup queue offset/count for all TCs for given VSI */
1720 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1721 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1722 		/* See if the given TC is enabled for the given VSI */
1723 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1724 			offset = vsi->mqprio_qopt.qopt.offset[i];
1725 			qcount = vsi->mqprio_qopt.qopt.count[i];
1726 			if (qcount > max_qcount)
1727 				max_qcount = qcount;
1728 			vsi->tc_config.tc_info[i].qoffset = offset;
1729 			vsi->tc_config.tc_info[i].qcount = qcount;
1730 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1731 		} else {
1732 			/* TC is not enabled so set the offset to
1733 			 * default queue and allocate one queue
1734 			 * for the given TC.
1735 			 */
1736 			vsi->tc_config.tc_info[i].qoffset = 0;
1737 			vsi->tc_config.tc_info[i].qcount = 1;
1738 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1739 		}
1740 	}
1741 
1742 	/* Set actual Tx/Rx queue pairs */
1743 	vsi->num_queue_pairs = offset + qcount;
1744 
1745 	/* Setup queue TC[0].qmap for given VSI context */
1746 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1747 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1748 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1749 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1750 
1751 	/* Reconfigure RSS for main VSI with max queue count */
1752 	vsi->rss_size = max_qcount;
1753 	ret = i40e_vsi_config_rss(vsi);
1754 	if (ret) {
1755 		dev_info(&vsi->back->pdev->dev,
1756 			 "Failed to reconfig rss for num_queues (%u)\n",
1757 			 max_qcount);
1758 		return ret;
1759 	}
1760 	vsi->reconfig_rss = true;
1761 	dev_dbg(&vsi->back->pdev->dev,
1762 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1763 
1764 	/* Find queue count available for channel VSIs and starting offset
1765 	 * for channel VSIs
1766 	 */
1767 	override_q = vsi->mqprio_qopt.qopt.count[0];
1768 	if (override_q && override_q < vsi->num_queue_pairs) {
1769 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1770 		vsi->next_base_queue = override_q;
1771 	}
1772 	return 0;
1773 }
1774 
1775 /**
1776  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1777  * @vsi: the VSI being setup
1778  * @ctxt: VSI context structure
1779  * @enabled_tc: Enabled TCs bitmap
1780  * @is_add: True if called before Add VSI
1781  *
1782  * Setup VSI queue mapping for enabled traffic classes.
1783  **/
1784 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1785 				     struct i40e_vsi_context *ctxt,
1786 				     u8 enabled_tc,
1787 				     bool is_add)
1788 {
1789 	struct i40e_pf *pf = vsi->back;
1790 	u16 sections = 0;
1791 	u8 netdev_tc = 0;
1792 	u16 numtc = 1;
1793 	u16 qcount;
1794 	u8 offset;
1795 	u16 qmap;
1796 	int i;
1797 	u16 num_tc_qps = 0;
1798 
1799 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1800 	offset = 0;
1801 
1802 	/* Number of queues per enabled TC */
1803 	num_tc_qps = vsi->alloc_queue_pairs;
1804 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1805 		/* Find numtc from enabled TC bitmap */
1806 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1807 			if (enabled_tc & BIT(i)) /* TC is enabled */
1808 				numtc++;
1809 		}
1810 		if (!numtc) {
1811 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1812 			numtc = 1;
1813 		}
1814 		num_tc_qps = num_tc_qps / numtc;
1815 		num_tc_qps = min_t(int, num_tc_qps,
1816 				   i40e_pf_get_max_q_per_tc(pf));
1817 	}
1818 
1819 	vsi->tc_config.numtc = numtc;
1820 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1821 
1822 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
1823 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1824 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
1825 
1826 	/* Setup queue offset/count for all TCs for given VSI */
1827 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1828 		/* See if the given TC is enabled for the given VSI */
1829 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1830 			/* TC is enabled */
1831 			int pow, num_qps;
1832 
1833 			switch (vsi->type) {
1834 			case I40E_VSI_MAIN:
1835 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
1836 				    I40E_FLAG_FD_ATR_ENABLED)) ||
1837 				    vsi->tc_config.enabled_tc != 1) {
1838 					qcount = min_t(int, pf->alloc_rss_size,
1839 						       num_tc_qps);
1840 					break;
1841 				}
1842 				fallthrough;
1843 			case I40E_VSI_FDIR:
1844 			case I40E_VSI_SRIOV:
1845 			case I40E_VSI_VMDQ2:
1846 			default:
1847 				qcount = num_tc_qps;
1848 				WARN_ON(i != 0);
1849 				break;
1850 			}
1851 			vsi->tc_config.tc_info[i].qoffset = offset;
1852 			vsi->tc_config.tc_info[i].qcount = qcount;
1853 
1854 			/* find the next higher power-of-2 of num queue pairs */
1855 			num_qps = qcount;
1856 			pow = 0;
1857 			while (num_qps && (BIT_ULL(pow) < qcount)) {
1858 				pow++;
1859 				num_qps >>= 1;
1860 			}
1861 
1862 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1863 			qmap =
1864 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1865 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1866 
1867 			offset += qcount;
1868 		} else {
1869 			/* TC is not enabled so set the offset to
1870 			 * default queue and allocate one queue
1871 			 * for the given TC.
1872 			 */
1873 			vsi->tc_config.tc_info[i].qoffset = 0;
1874 			vsi->tc_config.tc_info[i].qcount = 1;
1875 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1876 
1877 			qmap = 0;
1878 		}
1879 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1880 	}
1881 
1882 	/* Set actual Tx/Rx queue pairs */
1883 	vsi->num_queue_pairs = offset;
1884 	if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1885 		if (vsi->req_queue_pairs > 0)
1886 			vsi->num_queue_pairs = vsi->req_queue_pairs;
1887 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1888 			vsi->num_queue_pairs = pf->num_lan_msix;
1889 	}
1890 
1891 	/* Scheduler section valid can only be set for ADD VSI */
1892 	if (is_add) {
1893 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1894 
1895 		ctxt->info.up_enable_bits = enabled_tc;
1896 	}
1897 	if (vsi->type == I40E_VSI_SRIOV) {
1898 		ctxt->info.mapping_flags |=
1899 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1900 		for (i = 0; i < vsi->num_queue_pairs; i++)
1901 			ctxt->info.queue_mapping[i] =
1902 					       cpu_to_le16(vsi->base_queue + i);
1903 	} else {
1904 		ctxt->info.mapping_flags |=
1905 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1906 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1907 	}
1908 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1909 }
1910 
1911 /**
1912  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1913  * @netdev: the netdevice
1914  * @addr: address to add
1915  *
1916  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1917  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1918  */
1919 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1920 {
1921 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1922 	struct i40e_vsi *vsi = np->vsi;
1923 
1924 	if (i40e_add_mac_filter(vsi, addr))
1925 		return 0;
1926 	else
1927 		return -ENOMEM;
1928 }
1929 
1930 /**
1931  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1932  * @netdev: the netdevice
1933  * @addr: address to add
1934  *
1935  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1936  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1937  */
1938 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1939 {
1940 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1941 	struct i40e_vsi *vsi = np->vsi;
1942 
1943 	/* Under some circumstances, we might receive a request to delete
1944 	 * our own device address from our uc list. Because we store the
1945 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
1946 	 * such requests and not delete our device address from this list.
1947 	 */
1948 	if (ether_addr_equal(addr, netdev->dev_addr))
1949 		return 0;
1950 
1951 	i40e_del_mac_filter(vsi, addr);
1952 
1953 	return 0;
1954 }
1955 
1956 /**
1957  * i40e_set_rx_mode - NDO callback to set the netdev filters
1958  * @netdev: network interface device structure
1959  **/
1960 static void i40e_set_rx_mode(struct net_device *netdev)
1961 {
1962 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1963 	struct i40e_vsi *vsi = np->vsi;
1964 
1965 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1966 
1967 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1968 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1969 
1970 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1971 
1972 	/* check for other flag changes */
1973 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
1974 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1975 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1976 	}
1977 }
1978 
1979 /**
1980  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
1981  * @vsi: Pointer to VSI struct
1982  * @from: Pointer to list which contains MAC filter entries - changes to
1983  *        those entries needs to be undone.
1984  *
1985  * MAC filter entries from this list were slated for deletion.
1986  **/
1987 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1988 					 struct hlist_head *from)
1989 {
1990 	struct i40e_mac_filter *f;
1991 	struct hlist_node *h;
1992 
1993 	hlist_for_each_entry_safe(f, h, from, hlist) {
1994 		u64 key = i40e_addr_to_hkey(f->macaddr);
1995 
1996 		/* Move the element back into MAC filter list*/
1997 		hlist_del(&f->hlist);
1998 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1999 	}
2000 }
2001 
2002 /**
2003  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2004  * @vsi: Pointer to vsi struct
2005  * @from: Pointer to list which contains MAC filter entries - changes to
2006  *        those entries needs to be undone.
2007  *
2008  * MAC filter entries from this list were slated for addition.
2009  **/
2010 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2011 					 struct hlist_head *from)
2012 {
2013 	struct i40e_new_mac_filter *new;
2014 	struct hlist_node *h;
2015 
2016 	hlist_for_each_entry_safe(new, h, from, hlist) {
2017 		/* We can simply free the wrapper structure */
2018 		hlist_del(&new->hlist);
2019 		kfree(new);
2020 	}
2021 }
2022 
2023 /**
2024  * i40e_next_entry - Get the next non-broadcast filter from a list
2025  * @next: pointer to filter in list
2026  *
2027  * Returns the next non-broadcast filter in the list. Required so that we
2028  * ignore broadcast filters within the list, since these are not handled via
2029  * the normal firmware update path.
2030  */
2031 static
2032 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2033 {
2034 	hlist_for_each_entry_continue(next, hlist) {
2035 		if (!is_broadcast_ether_addr(next->f->macaddr))
2036 			return next;
2037 	}
2038 
2039 	return NULL;
2040 }
2041 
2042 /**
2043  * i40e_update_filter_state - Update filter state based on return data
2044  * from firmware
2045  * @count: Number of filters added
2046  * @add_list: return data from fw
2047  * @add_head: pointer to first filter in current batch
2048  *
2049  * MAC filter entries from list were slated to be added to device. Returns
2050  * number of successful filters. Note that 0 does NOT mean success!
2051  **/
2052 static int
2053 i40e_update_filter_state(int count,
2054 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2055 			 struct i40e_new_mac_filter *add_head)
2056 {
2057 	int retval = 0;
2058 	int i;
2059 
2060 	for (i = 0; i < count; i++) {
2061 		/* Always check status of each filter. We don't need to check
2062 		 * the firmware return status because we pre-set the filter
2063 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2064 		 * request to the adminq. Thus, if it no longer matches then
2065 		 * we know the filter is active.
2066 		 */
2067 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2068 			add_head->state = I40E_FILTER_FAILED;
2069 		} else {
2070 			add_head->state = I40E_FILTER_ACTIVE;
2071 			retval++;
2072 		}
2073 
2074 		add_head = i40e_next_filter(add_head);
2075 		if (!add_head)
2076 			break;
2077 	}
2078 
2079 	return retval;
2080 }
2081 
2082 /**
2083  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2084  * @vsi: ptr to the VSI
2085  * @vsi_name: name to display in messages
2086  * @list: the list of filters to send to firmware
2087  * @num_del: the number of filters to delete
2088  * @retval: Set to -EIO on failure to delete
2089  *
2090  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2091  * *retval instead of a return value so that success does not force ret_val to
2092  * be set to 0. This ensures that a sequence of calls to this function
2093  * preserve the previous value of *retval on successful delete.
2094  */
2095 static
2096 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2097 			  struct i40e_aqc_remove_macvlan_element_data *list,
2098 			  int num_del, int *retval)
2099 {
2100 	struct i40e_hw *hw = &vsi->back->hw;
2101 	i40e_status aq_ret;
2102 	int aq_err;
2103 
2104 	aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
2105 	aq_err = hw->aq.asq_last_status;
2106 
2107 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2108 	if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
2109 		*retval = -EIO;
2110 		dev_info(&vsi->back->pdev->dev,
2111 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2112 			 vsi_name, i40e_stat_str(hw, aq_ret),
2113 			 i40e_aq_str(hw, aq_err));
2114 	}
2115 }
2116 
2117 /**
2118  * i40e_aqc_add_filters - Request firmware to add a set of filters
2119  * @vsi: ptr to the VSI
2120  * @vsi_name: name to display in messages
2121  * @list: the list of filters to send to firmware
2122  * @add_head: Position in the add hlist
2123  * @num_add: the number of filters to add
2124  *
2125  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2126  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2127  * space for more filters.
2128  */
2129 static
2130 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2131 			  struct i40e_aqc_add_macvlan_element_data *list,
2132 			  struct i40e_new_mac_filter *add_head,
2133 			  int num_add)
2134 {
2135 	struct i40e_hw *hw = &vsi->back->hw;
2136 	int aq_err, fcnt;
2137 
2138 	i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
2139 	aq_err = hw->aq.asq_last_status;
2140 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2141 
2142 	if (fcnt != num_add) {
2143 		if (vsi->type == I40E_VSI_MAIN) {
2144 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2145 			dev_warn(&vsi->back->pdev->dev,
2146 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2147 				 i40e_aq_str(hw, aq_err), vsi_name);
2148 		} else if (vsi->type == I40E_VSI_SRIOV ||
2149 			   vsi->type == I40E_VSI_VMDQ1 ||
2150 			   vsi->type == I40E_VSI_VMDQ2) {
2151 			dev_warn(&vsi->back->pdev->dev,
2152 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2153 				 i40e_aq_str(hw, aq_err), vsi_name, vsi_name);
2154 		} else {
2155 			dev_warn(&vsi->back->pdev->dev,
2156 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2157 				 i40e_aq_str(hw, aq_err), vsi_name, vsi->type);
2158 		}
2159 	}
2160 }
2161 
2162 /**
2163  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2164  * @vsi: pointer to the VSI
2165  * @vsi_name: the VSI name
2166  * @f: filter data
2167  *
2168  * This function sets or clears the promiscuous broadcast flags for VLAN
2169  * filters in order to properly receive broadcast frames. Assumes that only
2170  * broadcast filters are passed.
2171  *
2172  * Returns status indicating success or failure;
2173  **/
2174 static i40e_status
2175 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2176 			  struct i40e_mac_filter *f)
2177 {
2178 	bool enable = f->state == I40E_FILTER_NEW;
2179 	struct i40e_hw *hw = &vsi->back->hw;
2180 	i40e_status aq_ret;
2181 
2182 	if (f->vlan == I40E_VLAN_ANY) {
2183 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2184 						   vsi->seid,
2185 						   enable,
2186 						   NULL);
2187 	} else {
2188 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2189 							    vsi->seid,
2190 							    enable,
2191 							    f->vlan,
2192 							    NULL);
2193 	}
2194 
2195 	if (aq_ret) {
2196 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2197 		dev_warn(&vsi->back->pdev->dev,
2198 			 "Error %s, forcing overflow promiscuous on %s\n",
2199 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2200 			 vsi_name);
2201 	}
2202 
2203 	return aq_ret;
2204 }
2205 
2206 /**
2207  * i40e_set_promiscuous - set promiscuous mode
2208  * @pf: board private structure
2209  * @promisc: promisc on or off
2210  *
2211  * There are different ways of setting promiscuous mode on a PF depending on
2212  * what state/environment we're in.  This identifies and sets it appropriately.
2213  * Returns 0 on success.
2214  **/
2215 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2216 {
2217 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2218 	struct i40e_hw *hw = &pf->hw;
2219 	i40e_status aq_ret;
2220 
2221 	if (vsi->type == I40E_VSI_MAIN &&
2222 	    pf->lan_veb != I40E_NO_VEB &&
2223 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2224 		/* set defport ON for Main VSI instead of true promisc
2225 		 * this way we will get all unicast/multicast and VLAN
2226 		 * promisc behavior but will not get VF or VMDq traffic
2227 		 * replicated on the Main VSI.
2228 		 */
2229 		if (promisc)
2230 			aq_ret = i40e_aq_set_default_vsi(hw,
2231 							 vsi->seid,
2232 							 NULL);
2233 		else
2234 			aq_ret = i40e_aq_clear_default_vsi(hw,
2235 							   vsi->seid,
2236 							   NULL);
2237 		if (aq_ret) {
2238 			dev_info(&pf->pdev->dev,
2239 				 "Set default VSI failed, err %s, aq_err %s\n",
2240 				 i40e_stat_str(hw, aq_ret),
2241 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2242 		}
2243 	} else {
2244 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2245 						  hw,
2246 						  vsi->seid,
2247 						  promisc, NULL,
2248 						  true);
2249 		if (aq_ret) {
2250 			dev_info(&pf->pdev->dev,
2251 				 "set unicast promisc failed, err %s, aq_err %s\n",
2252 				 i40e_stat_str(hw, aq_ret),
2253 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2254 		}
2255 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2256 						  hw,
2257 						  vsi->seid,
2258 						  promisc, NULL);
2259 		if (aq_ret) {
2260 			dev_info(&pf->pdev->dev,
2261 				 "set multicast promisc failed, err %s, aq_err %s\n",
2262 				 i40e_stat_str(hw, aq_ret),
2263 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2264 		}
2265 	}
2266 
2267 	if (!aq_ret)
2268 		pf->cur_promisc = promisc;
2269 
2270 	return aq_ret;
2271 }
2272 
2273 /**
2274  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2275  * @vsi: ptr to the VSI
2276  *
2277  * Push any outstanding VSI filter changes through the AdminQ.
2278  *
2279  * Returns 0 or error value
2280  **/
2281 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2282 {
2283 	struct hlist_head tmp_add_list, tmp_del_list;
2284 	struct i40e_mac_filter *f;
2285 	struct i40e_new_mac_filter *new, *add_head = NULL;
2286 	struct i40e_hw *hw = &vsi->back->hw;
2287 	bool old_overflow, new_overflow;
2288 	unsigned int failed_filters = 0;
2289 	unsigned int vlan_filters = 0;
2290 	char vsi_name[16] = "PF";
2291 	int filter_list_len = 0;
2292 	i40e_status aq_ret = 0;
2293 	u32 changed_flags = 0;
2294 	struct hlist_node *h;
2295 	struct i40e_pf *pf;
2296 	int num_add = 0;
2297 	int num_del = 0;
2298 	int retval = 0;
2299 	u16 cmd_flags;
2300 	int list_size;
2301 	int bkt;
2302 
2303 	/* empty array typed pointers, kcalloc later */
2304 	struct i40e_aqc_add_macvlan_element_data *add_list;
2305 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2306 
2307 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2308 		usleep_range(1000, 2000);
2309 	pf = vsi->back;
2310 
2311 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2312 
2313 	if (vsi->netdev) {
2314 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2315 		vsi->current_netdev_flags = vsi->netdev->flags;
2316 	}
2317 
2318 	INIT_HLIST_HEAD(&tmp_add_list);
2319 	INIT_HLIST_HEAD(&tmp_del_list);
2320 
2321 	if (vsi->type == I40E_VSI_SRIOV)
2322 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2323 	else if (vsi->type != I40E_VSI_MAIN)
2324 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2325 
2326 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2327 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2328 
2329 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2330 		/* Create a list of filters to delete. */
2331 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2332 			if (f->state == I40E_FILTER_REMOVE) {
2333 				/* Move the element into temporary del_list */
2334 				hash_del(&f->hlist);
2335 				hlist_add_head(&f->hlist, &tmp_del_list);
2336 
2337 				/* Avoid counting removed filters */
2338 				continue;
2339 			}
2340 			if (f->state == I40E_FILTER_NEW) {
2341 				/* Create a temporary i40e_new_mac_filter */
2342 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2343 				if (!new)
2344 					goto err_no_memory_locked;
2345 
2346 				/* Store pointer to the real filter */
2347 				new->f = f;
2348 				new->state = f->state;
2349 
2350 				/* Add it to the hash list */
2351 				hlist_add_head(&new->hlist, &tmp_add_list);
2352 			}
2353 
2354 			/* Count the number of active (current and new) VLAN
2355 			 * filters we have now. Does not count filters which
2356 			 * are marked for deletion.
2357 			 */
2358 			if (f->vlan > 0)
2359 				vlan_filters++;
2360 		}
2361 
2362 		retval = i40e_correct_mac_vlan_filters(vsi,
2363 						       &tmp_add_list,
2364 						       &tmp_del_list,
2365 						       vlan_filters);
2366 		if (retval)
2367 			goto err_no_memory_locked;
2368 
2369 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2370 	}
2371 
2372 	/* Now process 'del_list' outside the lock */
2373 	if (!hlist_empty(&tmp_del_list)) {
2374 		filter_list_len = hw->aq.asq_buf_size /
2375 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2376 		list_size = filter_list_len *
2377 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2378 		del_list = kzalloc(list_size, GFP_ATOMIC);
2379 		if (!del_list)
2380 			goto err_no_memory;
2381 
2382 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2383 			cmd_flags = 0;
2384 
2385 			/* handle broadcast filters by updating the broadcast
2386 			 * promiscuous flag and release filter list.
2387 			 */
2388 			if (is_broadcast_ether_addr(f->macaddr)) {
2389 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2390 
2391 				hlist_del(&f->hlist);
2392 				kfree(f);
2393 				continue;
2394 			}
2395 
2396 			/* add to delete list */
2397 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2398 			if (f->vlan == I40E_VLAN_ANY) {
2399 				del_list[num_del].vlan_tag = 0;
2400 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2401 			} else {
2402 				del_list[num_del].vlan_tag =
2403 					cpu_to_le16((u16)(f->vlan));
2404 			}
2405 
2406 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2407 			del_list[num_del].flags = cmd_flags;
2408 			num_del++;
2409 
2410 			/* flush a full buffer */
2411 			if (num_del == filter_list_len) {
2412 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2413 						     num_del, &retval);
2414 				memset(del_list, 0, list_size);
2415 				num_del = 0;
2416 			}
2417 			/* Release memory for MAC filter entries which were
2418 			 * synced up with HW.
2419 			 */
2420 			hlist_del(&f->hlist);
2421 			kfree(f);
2422 		}
2423 
2424 		if (num_del) {
2425 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2426 					     num_del, &retval);
2427 		}
2428 
2429 		kfree(del_list);
2430 		del_list = NULL;
2431 	}
2432 
2433 	if (!hlist_empty(&tmp_add_list)) {
2434 		/* Do all the adds now. */
2435 		filter_list_len = hw->aq.asq_buf_size /
2436 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2437 		list_size = filter_list_len *
2438 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2439 		add_list = kzalloc(list_size, GFP_ATOMIC);
2440 		if (!add_list)
2441 			goto err_no_memory;
2442 
2443 		num_add = 0;
2444 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2445 			/* handle broadcast filters by updating the broadcast
2446 			 * promiscuous flag instead of adding a MAC filter.
2447 			 */
2448 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2449 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2450 							      new->f))
2451 					new->state = I40E_FILTER_FAILED;
2452 				else
2453 					new->state = I40E_FILTER_ACTIVE;
2454 				continue;
2455 			}
2456 
2457 			/* add to add array */
2458 			if (num_add == 0)
2459 				add_head = new;
2460 			cmd_flags = 0;
2461 			ether_addr_copy(add_list[num_add].mac_addr,
2462 					new->f->macaddr);
2463 			if (new->f->vlan == I40E_VLAN_ANY) {
2464 				add_list[num_add].vlan_tag = 0;
2465 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2466 			} else {
2467 				add_list[num_add].vlan_tag =
2468 					cpu_to_le16((u16)(new->f->vlan));
2469 			}
2470 			add_list[num_add].queue_number = 0;
2471 			/* set invalid match method for later detection */
2472 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2473 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2474 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2475 			num_add++;
2476 
2477 			/* flush a full buffer */
2478 			if (num_add == filter_list_len) {
2479 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2480 						     add_head, num_add);
2481 				memset(add_list, 0, list_size);
2482 				num_add = 0;
2483 			}
2484 		}
2485 		if (num_add) {
2486 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2487 					     num_add);
2488 		}
2489 		/* Now move all of the filters from the temp add list back to
2490 		 * the VSI's list.
2491 		 */
2492 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2493 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2494 			/* Only update the state if we're still NEW */
2495 			if (new->f->state == I40E_FILTER_NEW)
2496 				new->f->state = new->state;
2497 			hlist_del(&new->hlist);
2498 			kfree(new);
2499 		}
2500 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2501 		kfree(add_list);
2502 		add_list = NULL;
2503 	}
2504 
2505 	/* Determine the number of active and failed filters. */
2506 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2507 	vsi->active_filters = 0;
2508 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2509 		if (f->state == I40E_FILTER_ACTIVE)
2510 			vsi->active_filters++;
2511 		else if (f->state == I40E_FILTER_FAILED)
2512 			failed_filters++;
2513 	}
2514 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2515 
2516 	/* Check if we are able to exit overflow promiscuous mode. We can
2517 	 * safely exit if we didn't just enter, we no longer have any failed
2518 	 * filters, and we have reduced filters below the threshold value.
2519 	 */
2520 	if (old_overflow && !failed_filters &&
2521 	    vsi->active_filters < vsi->promisc_threshold) {
2522 		dev_info(&pf->pdev->dev,
2523 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2524 			 vsi_name);
2525 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2526 		vsi->promisc_threshold = 0;
2527 	}
2528 
2529 	/* if the VF is not trusted do not do promisc */
2530 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2531 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2532 		goto out;
2533 	}
2534 
2535 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2536 
2537 	/* If we are entering overflow promiscuous, we need to calculate a new
2538 	 * threshold for when we are safe to exit
2539 	 */
2540 	if (!old_overflow && new_overflow)
2541 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2542 
2543 	/* check for changes in promiscuous modes */
2544 	if (changed_flags & IFF_ALLMULTI) {
2545 		bool cur_multipromisc;
2546 
2547 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2548 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2549 							       vsi->seid,
2550 							       cur_multipromisc,
2551 							       NULL);
2552 		if (aq_ret) {
2553 			retval = i40e_aq_rc_to_posix(aq_ret,
2554 						     hw->aq.asq_last_status);
2555 			dev_info(&pf->pdev->dev,
2556 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2557 				 vsi_name,
2558 				 i40e_stat_str(hw, aq_ret),
2559 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2560 		} else {
2561 			dev_info(&pf->pdev->dev, "%s is %s allmulti mode.\n",
2562 				 vsi->netdev->name,
2563 				 cur_multipromisc ? "entering" : "leaving");
2564 		}
2565 	}
2566 
2567 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2568 		bool cur_promisc;
2569 
2570 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2571 			       new_overflow);
2572 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2573 		if (aq_ret) {
2574 			retval = i40e_aq_rc_to_posix(aq_ret,
2575 						     hw->aq.asq_last_status);
2576 			dev_info(&pf->pdev->dev,
2577 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2578 				 cur_promisc ? "on" : "off",
2579 				 vsi_name,
2580 				 i40e_stat_str(hw, aq_ret),
2581 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2582 		}
2583 	}
2584 out:
2585 	/* if something went wrong then set the changed flag so we try again */
2586 	if (retval)
2587 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2588 
2589 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2590 	return retval;
2591 
2592 err_no_memory:
2593 	/* Restore elements on the temporary add and delete lists */
2594 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2595 err_no_memory_locked:
2596 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2597 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2598 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2599 
2600 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2601 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2602 	return -ENOMEM;
2603 }
2604 
2605 /**
2606  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2607  * @pf: board private structure
2608  **/
2609 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2610 {
2611 	int v;
2612 
2613 	if (!pf)
2614 		return;
2615 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2616 		return;
2617 	if (test_and_set_bit(__I40E_VF_DISABLE, pf->state)) {
2618 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2619 		return;
2620 	}
2621 
2622 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2623 		if (pf->vsi[v] &&
2624 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2625 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2626 
2627 			if (ret) {
2628 				/* come back and try again later */
2629 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2630 					pf->state);
2631 				break;
2632 			}
2633 		}
2634 	}
2635 	clear_bit(__I40E_VF_DISABLE, pf->state);
2636 }
2637 
2638 /**
2639  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2640  * @vsi: the vsi
2641  **/
2642 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2643 {
2644 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2645 		return I40E_RXBUFFER_2048;
2646 	else
2647 		return I40E_RXBUFFER_3072;
2648 }
2649 
2650 /**
2651  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2652  * @netdev: network interface device structure
2653  * @new_mtu: new value for maximum frame size
2654  *
2655  * Returns 0 on success, negative on failure
2656  **/
2657 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2658 {
2659 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2660 	struct i40e_vsi *vsi = np->vsi;
2661 	struct i40e_pf *pf = vsi->back;
2662 
2663 	if (i40e_enabled_xdp_vsi(vsi)) {
2664 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2665 
2666 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2667 			return -EINVAL;
2668 	}
2669 
2670 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2671 		   netdev->mtu, new_mtu);
2672 	netdev->mtu = new_mtu;
2673 	if (netif_running(netdev))
2674 		i40e_vsi_reinit_locked(vsi);
2675 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2676 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2677 	return 0;
2678 }
2679 
2680 /**
2681  * i40e_ioctl - Access the hwtstamp interface
2682  * @netdev: network interface device structure
2683  * @ifr: interface request data
2684  * @cmd: ioctl command
2685  **/
2686 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2687 {
2688 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2689 	struct i40e_pf *pf = np->vsi->back;
2690 
2691 	switch (cmd) {
2692 	case SIOCGHWTSTAMP:
2693 		return i40e_ptp_get_ts_config(pf, ifr);
2694 	case SIOCSHWTSTAMP:
2695 		return i40e_ptp_set_ts_config(pf, ifr);
2696 	default:
2697 		return -EOPNOTSUPP;
2698 	}
2699 }
2700 
2701 /**
2702  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2703  * @vsi: the vsi being adjusted
2704  **/
2705 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2706 {
2707 	struct i40e_vsi_context ctxt;
2708 	i40e_status ret;
2709 
2710 	/* Don't modify stripping options if a port VLAN is active */
2711 	if (vsi->info.pvid)
2712 		return;
2713 
2714 	if ((vsi->info.valid_sections &
2715 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2716 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2717 		return;  /* already enabled */
2718 
2719 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2720 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2721 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2722 
2723 	ctxt.seid = vsi->seid;
2724 	ctxt.info = vsi->info;
2725 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2726 	if (ret) {
2727 		dev_info(&vsi->back->pdev->dev,
2728 			 "update vlan stripping failed, err %s aq_err %s\n",
2729 			 i40e_stat_str(&vsi->back->hw, ret),
2730 			 i40e_aq_str(&vsi->back->hw,
2731 				     vsi->back->hw.aq.asq_last_status));
2732 	}
2733 }
2734 
2735 /**
2736  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2737  * @vsi: the vsi being adjusted
2738  **/
2739 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2740 {
2741 	struct i40e_vsi_context ctxt;
2742 	i40e_status ret;
2743 
2744 	/* Don't modify stripping options if a port VLAN is active */
2745 	if (vsi->info.pvid)
2746 		return;
2747 
2748 	if ((vsi->info.valid_sections &
2749 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2750 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2751 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
2752 		return;  /* already disabled */
2753 
2754 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2755 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2756 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2757 
2758 	ctxt.seid = vsi->seid;
2759 	ctxt.info = vsi->info;
2760 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2761 	if (ret) {
2762 		dev_info(&vsi->back->pdev->dev,
2763 			 "update vlan stripping failed, err %s aq_err %s\n",
2764 			 i40e_stat_str(&vsi->back->hw, ret),
2765 			 i40e_aq_str(&vsi->back->hw,
2766 				     vsi->back->hw.aq.asq_last_status));
2767 	}
2768 }
2769 
2770 /**
2771  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2772  * @vsi: the vsi being configured
2773  * @vid: vlan id to be added (0 = untagged only , -1 = any)
2774  *
2775  * This is a helper function for adding a new MAC/VLAN filter with the
2776  * specified VLAN for each existing MAC address already in the hash table.
2777  * This function does *not* perform any accounting to update filters based on
2778  * VLAN mode.
2779  *
2780  * NOTE: this function expects to be called while under the
2781  * mac_filter_hash_lock
2782  **/
2783 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2784 {
2785 	struct i40e_mac_filter *f, *add_f;
2786 	struct hlist_node *h;
2787 	int bkt;
2788 
2789 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2790 		if (f->state == I40E_FILTER_REMOVE)
2791 			continue;
2792 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
2793 		if (!add_f) {
2794 			dev_info(&vsi->back->pdev->dev,
2795 				 "Could not add vlan filter %d for %pM\n",
2796 				 vid, f->macaddr);
2797 			return -ENOMEM;
2798 		}
2799 	}
2800 
2801 	return 0;
2802 }
2803 
2804 /**
2805  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2806  * @vsi: the VSI being configured
2807  * @vid: VLAN id to be added
2808  **/
2809 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
2810 {
2811 	int err;
2812 
2813 	if (vsi->info.pvid)
2814 		return -EINVAL;
2815 
2816 	/* The network stack will attempt to add VID=0, with the intention to
2817 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
2818 	 * these packets by default when configured to receive untagged
2819 	 * packets, so we don't need to add a filter for this case.
2820 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
2821 	 * receive *only* tagged traffic and stops receiving untagged traffic.
2822 	 * Thus, we do not want to actually add a filter for VID=0
2823 	 */
2824 	if (!vid)
2825 		return 0;
2826 
2827 	/* Locked once because all functions invoked below iterates list*/
2828 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2829 	err = i40e_add_vlan_all_mac(vsi, vid);
2830 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2831 	if (err)
2832 		return err;
2833 
2834 	/* schedule our worker thread which will take care of
2835 	 * applying the new filter changes
2836 	 */
2837 	i40e_service_event_schedule(vsi->back);
2838 	return 0;
2839 }
2840 
2841 /**
2842  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
2843  * @vsi: the vsi being configured
2844  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2845  *
2846  * This function should be used to remove all VLAN filters which match the
2847  * given VID. It does not schedule the service event and does not take the
2848  * mac_filter_hash_lock so it may be combined with other operations under
2849  * a single invocation of the mac_filter_hash_lock.
2850  *
2851  * NOTE: this function expects to be called while under the
2852  * mac_filter_hash_lock
2853  */
2854 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2855 {
2856 	struct i40e_mac_filter *f;
2857 	struct hlist_node *h;
2858 	int bkt;
2859 
2860 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2861 		if (f->vlan == vid)
2862 			__i40e_del_filter(vsi, f);
2863 	}
2864 }
2865 
2866 /**
2867  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2868  * @vsi: the VSI being configured
2869  * @vid: VLAN id to be removed
2870  **/
2871 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
2872 {
2873 	if (!vid || vsi->info.pvid)
2874 		return;
2875 
2876 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2877 	i40e_rm_vlan_all_mac(vsi, vid);
2878 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2879 
2880 	/* schedule our worker thread which will take care of
2881 	 * applying the new filter changes
2882 	 */
2883 	i40e_service_event_schedule(vsi->back);
2884 }
2885 
2886 /**
2887  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2888  * @netdev: network interface to be adjusted
2889  * @proto: unused protocol value
2890  * @vid: vlan id to be added
2891  *
2892  * net_device_ops implementation for adding vlan ids
2893  **/
2894 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2895 				__always_unused __be16 proto, u16 vid)
2896 {
2897 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2898 	struct i40e_vsi *vsi = np->vsi;
2899 	int ret = 0;
2900 
2901 	if (vid >= VLAN_N_VID)
2902 		return -EINVAL;
2903 
2904 	ret = i40e_vsi_add_vlan(vsi, vid);
2905 	if (!ret)
2906 		set_bit(vid, vsi->active_vlans);
2907 
2908 	return ret;
2909 }
2910 
2911 /**
2912  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
2913  * @netdev: network interface to be adjusted
2914  * @proto: unused protocol value
2915  * @vid: vlan id to be added
2916  **/
2917 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
2918 				    __always_unused __be16 proto, u16 vid)
2919 {
2920 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2921 	struct i40e_vsi *vsi = np->vsi;
2922 
2923 	if (vid >= VLAN_N_VID)
2924 		return;
2925 	set_bit(vid, vsi->active_vlans);
2926 }
2927 
2928 /**
2929  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2930  * @netdev: network interface to be adjusted
2931  * @proto: unused protocol value
2932  * @vid: vlan id to be removed
2933  *
2934  * net_device_ops implementation for removing vlan ids
2935  **/
2936 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2937 				 __always_unused __be16 proto, u16 vid)
2938 {
2939 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2940 	struct i40e_vsi *vsi = np->vsi;
2941 
2942 	/* return code is ignored as there is nothing a user
2943 	 * can do about failure to remove and a log message was
2944 	 * already printed from the other function
2945 	 */
2946 	i40e_vsi_kill_vlan(vsi, vid);
2947 
2948 	clear_bit(vid, vsi->active_vlans);
2949 
2950 	return 0;
2951 }
2952 
2953 /**
2954  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2955  * @vsi: the vsi being brought back up
2956  **/
2957 static void i40e_restore_vlan(struct i40e_vsi *vsi)
2958 {
2959 	u16 vid;
2960 
2961 	if (!vsi->netdev)
2962 		return;
2963 
2964 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2965 		i40e_vlan_stripping_enable(vsi);
2966 	else
2967 		i40e_vlan_stripping_disable(vsi);
2968 
2969 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2970 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
2971 					vid);
2972 }
2973 
2974 /**
2975  * i40e_vsi_add_pvid - Add pvid for the VSI
2976  * @vsi: the vsi being adjusted
2977  * @vid: the vlan id to set as a PVID
2978  **/
2979 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
2980 {
2981 	struct i40e_vsi_context ctxt;
2982 	i40e_status ret;
2983 
2984 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2985 	vsi->info.pvid = cpu_to_le16(vid);
2986 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2987 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
2988 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
2989 
2990 	ctxt.seid = vsi->seid;
2991 	ctxt.info = vsi->info;
2992 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2993 	if (ret) {
2994 		dev_info(&vsi->back->pdev->dev,
2995 			 "add pvid failed, err %s aq_err %s\n",
2996 			 i40e_stat_str(&vsi->back->hw, ret),
2997 			 i40e_aq_str(&vsi->back->hw,
2998 				     vsi->back->hw.aq.asq_last_status));
2999 		return -ENOENT;
3000 	}
3001 
3002 	return 0;
3003 }
3004 
3005 /**
3006  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3007  * @vsi: the vsi being adjusted
3008  *
3009  * Just use the vlan_rx_register() service to put it back to normal
3010  **/
3011 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3012 {
3013 	vsi->info.pvid = 0;
3014 
3015 	i40e_vlan_stripping_disable(vsi);
3016 }
3017 
3018 /**
3019  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3020  * @vsi: ptr to the VSI
3021  *
3022  * If this function returns with an error, then it's possible one or
3023  * more of the rings is populated (while the rest are not).  It is the
3024  * callers duty to clean those orphaned rings.
3025  *
3026  * Return 0 on success, negative on failure
3027  **/
3028 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3029 {
3030 	int i, err = 0;
3031 
3032 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3033 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3034 
3035 	if (!i40e_enabled_xdp_vsi(vsi))
3036 		return err;
3037 
3038 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3039 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3040 
3041 	return err;
3042 }
3043 
3044 /**
3045  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3046  * @vsi: ptr to the VSI
3047  *
3048  * Free VSI's transmit software resources
3049  **/
3050 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3051 {
3052 	int i;
3053 
3054 	if (vsi->tx_rings) {
3055 		for (i = 0; i < vsi->num_queue_pairs; i++)
3056 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3057 				i40e_free_tx_resources(vsi->tx_rings[i]);
3058 	}
3059 
3060 	if (vsi->xdp_rings) {
3061 		for (i = 0; i < vsi->num_queue_pairs; i++)
3062 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3063 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3064 	}
3065 }
3066 
3067 /**
3068  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3069  * @vsi: ptr to the VSI
3070  *
3071  * If this function returns with an error, then it's possible one or
3072  * more of the rings is populated (while the rest are not).  It is the
3073  * callers duty to clean those orphaned rings.
3074  *
3075  * Return 0 on success, negative on failure
3076  **/
3077 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3078 {
3079 	int i, err = 0;
3080 
3081 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3082 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3083 	return err;
3084 }
3085 
3086 /**
3087  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3088  * @vsi: ptr to the VSI
3089  *
3090  * Free all receive software resources
3091  **/
3092 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3093 {
3094 	int i;
3095 
3096 	if (!vsi->rx_rings)
3097 		return;
3098 
3099 	for (i = 0; i < vsi->num_queue_pairs; i++)
3100 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3101 			i40e_free_rx_resources(vsi->rx_rings[i]);
3102 }
3103 
3104 /**
3105  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3106  * @ring: The Tx ring to configure
3107  *
3108  * This enables/disables XPS for a given Tx descriptor ring
3109  * based on the TCs enabled for the VSI that ring belongs to.
3110  **/
3111 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3112 {
3113 	int cpu;
3114 
3115 	if (!ring->q_vector || !ring->netdev || ring->ch)
3116 		return;
3117 
3118 	/* We only initialize XPS once, so as not to overwrite user settings */
3119 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3120 		return;
3121 
3122 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3123 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3124 			    ring->queue_index);
3125 }
3126 
3127 /**
3128  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3129  * @ring: The Tx or Rx ring
3130  *
3131  * Returns the AF_XDP buffer pool or NULL.
3132  **/
3133 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3134 {
3135 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3136 	int qid = ring->queue_index;
3137 
3138 	if (ring_is_xdp(ring))
3139 		qid -= ring->vsi->alloc_queue_pairs;
3140 
3141 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3142 		return NULL;
3143 
3144 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3145 }
3146 
3147 /**
3148  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3149  * @ring: The Tx ring to configure
3150  *
3151  * Configure the Tx descriptor ring in the HMC context.
3152  **/
3153 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3154 {
3155 	struct i40e_vsi *vsi = ring->vsi;
3156 	u16 pf_q = vsi->base_queue + ring->queue_index;
3157 	struct i40e_hw *hw = &vsi->back->hw;
3158 	struct i40e_hmc_obj_txq tx_ctx;
3159 	i40e_status err = 0;
3160 	u32 qtx_ctl = 0;
3161 
3162 	if (ring_is_xdp(ring))
3163 		ring->xsk_pool = i40e_xsk_pool(ring);
3164 
3165 	/* some ATR related tx ring init */
3166 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3167 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3168 		ring->atr_count = 0;
3169 	} else {
3170 		ring->atr_sample_rate = 0;
3171 	}
3172 
3173 	/* configure XPS */
3174 	i40e_config_xps_tx_ring(ring);
3175 
3176 	/* clear the context structure first */
3177 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3178 
3179 	tx_ctx.new_context = 1;
3180 	tx_ctx.base = (ring->dma / 128);
3181 	tx_ctx.qlen = ring->count;
3182 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3183 					       I40E_FLAG_FD_ATR_ENABLED));
3184 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3185 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3186 	if (vsi->type != I40E_VSI_FDIR)
3187 		tx_ctx.head_wb_ena = 1;
3188 	tx_ctx.head_wb_addr = ring->dma +
3189 			      (ring->count * sizeof(struct i40e_tx_desc));
3190 
3191 	/* As part of VSI creation/update, FW allocates certain
3192 	 * Tx arbitration queue sets for each TC enabled for
3193 	 * the VSI. The FW returns the handles to these queue
3194 	 * sets as part of the response buffer to Add VSI,
3195 	 * Update VSI, etc. AQ commands. It is expected that
3196 	 * these queue set handles be associated with the Tx
3197 	 * queues by the driver as part of the TX queue context
3198 	 * initialization. This has to be done regardless of
3199 	 * DCB as by default everything is mapped to TC0.
3200 	 */
3201 
3202 	if (ring->ch)
3203 		tx_ctx.rdylist =
3204 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3205 
3206 	else
3207 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3208 
3209 	tx_ctx.rdylist_act = 0;
3210 
3211 	/* clear the context in the HMC */
3212 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3213 	if (err) {
3214 		dev_info(&vsi->back->pdev->dev,
3215 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3216 			 ring->queue_index, pf_q, err);
3217 		return -ENOMEM;
3218 	}
3219 
3220 	/* set the context in the HMC */
3221 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3222 	if (err) {
3223 		dev_info(&vsi->back->pdev->dev,
3224 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3225 			 ring->queue_index, pf_q, err);
3226 		return -ENOMEM;
3227 	}
3228 
3229 	/* Now associate this queue with this PCI function */
3230 	if (ring->ch) {
3231 		if (ring->ch->type == I40E_VSI_VMDQ2)
3232 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3233 		else
3234 			return -EINVAL;
3235 
3236 		qtx_ctl |= (ring->ch->vsi_number <<
3237 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3238 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3239 	} else {
3240 		if (vsi->type == I40E_VSI_VMDQ2) {
3241 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3242 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3243 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3244 		} else {
3245 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3246 		}
3247 	}
3248 
3249 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3250 		    I40E_QTX_CTL_PF_INDX_MASK);
3251 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3252 	i40e_flush(hw);
3253 
3254 	/* cache tail off for easier writes later */
3255 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3256 
3257 	return 0;
3258 }
3259 
3260 /**
3261  * i40e_configure_rx_ring - Configure a receive ring context
3262  * @ring: The Rx ring to configure
3263  *
3264  * Configure the Rx descriptor ring in the HMC context.
3265  **/
3266 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3267 {
3268 	struct i40e_vsi *vsi = ring->vsi;
3269 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3270 	u16 pf_q = vsi->base_queue + ring->queue_index;
3271 	struct i40e_hw *hw = &vsi->back->hw;
3272 	struct i40e_hmc_obj_rxq rx_ctx;
3273 	i40e_status err = 0;
3274 	bool ok;
3275 	int ret;
3276 
3277 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3278 
3279 	/* clear the context structure first */
3280 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3281 
3282 	if (ring->vsi->type == I40E_VSI_MAIN)
3283 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3284 
3285 	kfree(ring->rx_bi);
3286 	ring->xsk_pool = i40e_xsk_pool(ring);
3287 	if (ring->xsk_pool) {
3288 		ret = i40e_alloc_rx_bi_zc(ring);
3289 		if (ret)
3290 			return ret;
3291 		ring->rx_buf_len =
3292 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3293 		/* For AF_XDP ZC, we disallow packets to span on
3294 		 * multiple buffers, thus letting us skip that
3295 		 * handling in the fast-path.
3296 		 */
3297 		chain_len = 1;
3298 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3299 						 MEM_TYPE_XSK_BUFF_POOL,
3300 						 NULL);
3301 		if (ret)
3302 			return ret;
3303 		dev_info(&vsi->back->pdev->dev,
3304 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3305 			 ring->queue_index);
3306 
3307 	} else {
3308 		ret = i40e_alloc_rx_bi(ring);
3309 		if (ret)
3310 			return ret;
3311 		ring->rx_buf_len = vsi->rx_buf_len;
3312 		if (ring->vsi->type == I40E_VSI_MAIN) {
3313 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3314 							 MEM_TYPE_PAGE_SHARED,
3315 							 NULL);
3316 			if (ret)
3317 				return ret;
3318 		}
3319 	}
3320 
3321 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3322 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3323 
3324 	rx_ctx.base = (ring->dma / 128);
3325 	rx_ctx.qlen = ring->count;
3326 
3327 	/* use 16 byte descriptors */
3328 	rx_ctx.dsize = 0;
3329 
3330 	/* descriptor type is always zero
3331 	 * rx_ctx.dtype = 0;
3332 	 */
3333 	rx_ctx.hsplit_0 = 0;
3334 
3335 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3336 	if (hw->revision_id == 0)
3337 		rx_ctx.lrxqthresh = 0;
3338 	else
3339 		rx_ctx.lrxqthresh = 1;
3340 	rx_ctx.crcstrip = 1;
3341 	rx_ctx.l2tsel = 1;
3342 	/* this controls whether VLAN is stripped from inner headers */
3343 	rx_ctx.showiv = 0;
3344 	/* set the prefena field to 1 because the manual says to */
3345 	rx_ctx.prefena = 1;
3346 
3347 	/* clear the context in the HMC */
3348 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3349 	if (err) {
3350 		dev_info(&vsi->back->pdev->dev,
3351 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3352 			 ring->queue_index, pf_q, err);
3353 		return -ENOMEM;
3354 	}
3355 
3356 	/* set the context in the HMC */
3357 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3358 	if (err) {
3359 		dev_info(&vsi->back->pdev->dev,
3360 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3361 			 ring->queue_index, pf_q, err);
3362 		return -ENOMEM;
3363 	}
3364 
3365 	/* configure Rx buffer alignment */
3366 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3367 		clear_ring_build_skb_enabled(ring);
3368 	else
3369 		set_ring_build_skb_enabled(ring);
3370 
3371 	/* cache tail for quicker writes, and clear the reg before use */
3372 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3373 	writel(0, ring->tail);
3374 
3375 	if (ring->xsk_pool) {
3376 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3377 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3378 	} else {
3379 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3380 	}
3381 	if (!ok) {
3382 		/* Log this in case the user has forgotten to give the kernel
3383 		 * any buffers, even later in the application.
3384 		 */
3385 		dev_info(&vsi->back->pdev->dev,
3386 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3387 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3388 			 ring->queue_index, pf_q);
3389 	}
3390 
3391 	return 0;
3392 }
3393 
3394 /**
3395  * i40e_vsi_configure_tx - Configure the VSI for Tx
3396  * @vsi: VSI structure describing this set of rings and resources
3397  *
3398  * Configure the Tx VSI for operation.
3399  **/
3400 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3401 {
3402 	int err = 0;
3403 	u16 i;
3404 
3405 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3406 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3407 
3408 	if (err || !i40e_enabled_xdp_vsi(vsi))
3409 		return err;
3410 
3411 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3412 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3413 
3414 	return err;
3415 }
3416 
3417 /**
3418  * i40e_vsi_configure_rx - Configure the VSI for Rx
3419  * @vsi: the VSI being configured
3420  *
3421  * Configure the Rx VSI for operation.
3422  **/
3423 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3424 {
3425 	int err = 0;
3426 	u16 i;
3427 
3428 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3429 		vsi->max_frame = I40E_MAX_RXBUFFER;
3430 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3431 #if (PAGE_SIZE < 8192)
3432 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3433 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3434 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3435 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3436 #endif
3437 	} else {
3438 		vsi->max_frame = I40E_MAX_RXBUFFER;
3439 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3440 						       I40E_RXBUFFER_2048;
3441 	}
3442 
3443 	/* set up individual rings */
3444 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3445 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3446 
3447 	return err;
3448 }
3449 
3450 /**
3451  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3452  * @vsi: ptr to the VSI
3453  **/
3454 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3455 {
3456 	struct i40e_ring *tx_ring, *rx_ring;
3457 	u16 qoffset, qcount;
3458 	int i, n;
3459 
3460 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3461 		/* Reset the TC information */
3462 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3463 			rx_ring = vsi->rx_rings[i];
3464 			tx_ring = vsi->tx_rings[i];
3465 			rx_ring->dcb_tc = 0;
3466 			tx_ring->dcb_tc = 0;
3467 		}
3468 		return;
3469 	}
3470 
3471 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3472 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3473 			continue;
3474 
3475 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3476 		qcount = vsi->tc_config.tc_info[n].qcount;
3477 		for (i = qoffset; i < (qoffset + qcount); i++) {
3478 			rx_ring = vsi->rx_rings[i];
3479 			tx_ring = vsi->tx_rings[i];
3480 			rx_ring->dcb_tc = n;
3481 			tx_ring->dcb_tc = n;
3482 		}
3483 	}
3484 }
3485 
3486 /**
3487  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3488  * @vsi: ptr to the VSI
3489  **/
3490 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3491 {
3492 	if (vsi->netdev)
3493 		i40e_set_rx_mode(vsi->netdev);
3494 }
3495 
3496 /**
3497  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3498  * @vsi: Pointer to the targeted VSI
3499  *
3500  * This function replays the hlist on the hw where all the SB Flow Director
3501  * filters were saved.
3502  **/
3503 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3504 {
3505 	struct i40e_fdir_filter *filter;
3506 	struct i40e_pf *pf = vsi->back;
3507 	struct hlist_node *node;
3508 
3509 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3510 		return;
3511 
3512 	/* Reset FDir counters as we're replaying all existing filters */
3513 	pf->fd_tcp4_filter_cnt = 0;
3514 	pf->fd_udp4_filter_cnt = 0;
3515 	pf->fd_sctp4_filter_cnt = 0;
3516 	pf->fd_ip4_filter_cnt = 0;
3517 
3518 	hlist_for_each_entry_safe(filter, node,
3519 				  &pf->fdir_filter_list, fdir_node) {
3520 		i40e_add_del_fdir(vsi, filter, true);
3521 	}
3522 }
3523 
3524 /**
3525  * i40e_vsi_configure - Set up the VSI for action
3526  * @vsi: the VSI being configured
3527  **/
3528 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3529 {
3530 	int err;
3531 
3532 	i40e_set_vsi_rx_mode(vsi);
3533 	i40e_restore_vlan(vsi);
3534 	i40e_vsi_config_dcb_rings(vsi);
3535 	err = i40e_vsi_configure_tx(vsi);
3536 	if (!err)
3537 		err = i40e_vsi_configure_rx(vsi);
3538 
3539 	return err;
3540 }
3541 
3542 /**
3543  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3544  * @vsi: the VSI being configured
3545  **/
3546 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3547 {
3548 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3549 	struct i40e_pf *pf = vsi->back;
3550 	struct i40e_hw *hw = &pf->hw;
3551 	u16 vector;
3552 	int i, q;
3553 	u32 qp;
3554 
3555 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3556 	 * and PFINT_LNKLSTn registers, e.g.:
3557 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3558 	 */
3559 	qp = vsi->base_queue;
3560 	vector = vsi->base_vector;
3561 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3562 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3563 
3564 		q_vector->rx.next_update = jiffies + 1;
3565 		q_vector->rx.target_itr =
3566 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3567 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3568 		     q_vector->rx.target_itr >> 1);
3569 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3570 
3571 		q_vector->tx.next_update = jiffies + 1;
3572 		q_vector->tx.target_itr =
3573 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3574 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3575 		     q_vector->tx.target_itr >> 1);
3576 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3577 
3578 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3579 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3580 
3581 		/* Linked list for the queuepairs assigned to this vector */
3582 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3583 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3584 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3585 			u32 val;
3586 
3587 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3588 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3589 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3590 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3591 			      (I40E_QUEUE_TYPE_TX <<
3592 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3593 
3594 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3595 
3596 			if (has_xdp) {
3597 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3598 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3599 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3600 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3601 				      (I40E_QUEUE_TYPE_TX <<
3602 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3603 
3604 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3605 			}
3606 
3607 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3608 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3609 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3610 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3611 			      (I40E_QUEUE_TYPE_RX <<
3612 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3613 
3614 			/* Terminate the linked list */
3615 			if (q == (q_vector->num_ringpairs - 1))
3616 				val |= (I40E_QUEUE_END_OF_LIST <<
3617 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3618 
3619 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3620 			qp++;
3621 		}
3622 	}
3623 
3624 	i40e_flush(hw);
3625 }
3626 
3627 /**
3628  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3629  * @pf: pointer to private device data structure
3630  **/
3631 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3632 {
3633 	struct i40e_hw *hw = &pf->hw;
3634 	u32 val;
3635 
3636 	/* clear things first */
3637 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3638 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3639 
3640 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3641 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3642 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3643 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3644 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3645 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3646 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3647 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3648 
3649 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3650 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3651 
3652 	if (pf->flags & I40E_FLAG_PTP)
3653 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3654 
3655 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3656 
3657 	/* SW_ITR_IDX = 0, but don't change INTENA */
3658 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3659 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3660 
3661 	/* OTHER_ITR_IDX = 0 */
3662 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3663 }
3664 
3665 /**
3666  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3667  * @vsi: the VSI being configured
3668  **/
3669 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3670 {
3671 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3672 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3673 	struct i40e_pf *pf = vsi->back;
3674 	struct i40e_hw *hw = &pf->hw;
3675 	u32 val;
3676 
3677 	/* set the ITR configuration */
3678 	q_vector->rx.next_update = jiffies + 1;
3679 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3680 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3681 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3682 	q_vector->tx.next_update = jiffies + 1;
3683 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3684 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3685 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3686 
3687 	i40e_enable_misc_int_causes(pf);
3688 
3689 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3690 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3691 
3692 	/* Associate the queue pair to the vector and enable the queue int */
3693 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3694 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3695 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3696 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3697 
3698 	wr32(hw, I40E_QINT_RQCTL(0), val);
3699 
3700 	if (i40e_enabled_xdp_vsi(vsi)) {
3701 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
3702 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3703 		      (I40E_QUEUE_TYPE_TX
3704 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3705 
3706 		wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3707 	}
3708 
3709 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
3710 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3711 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3712 
3713 	wr32(hw, I40E_QINT_TQCTL(0), val);
3714 	i40e_flush(hw);
3715 }
3716 
3717 /**
3718  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3719  * @pf: board private structure
3720  **/
3721 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3722 {
3723 	struct i40e_hw *hw = &pf->hw;
3724 
3725 	wr32(hw, I40E_PFINT_DYN_CTL0,
3726 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3727 	i40e_flush(hw);
3728 }
3729 
3730 /**
3731  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3732  * @pf: board private structure
3733  **/
3734 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3735 {
3736 	struct i40e_hw *hw = &pf->hw;
3737 	u32 val;
3738 
3739 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
3740 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3741 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3742 
3743 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
3744 	i40e_flush(hw);
3745 }
3746 
3747 /**
3748  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3749  * @irq: interrupt number
3750  * @data: pointer to a q_vector
3751  **/
3752 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3753 {
3754 	struct i40e_q_vector *q_vector = data;
3755 
3756 	if (!q_vector->tx.ring && !q_vector->rx.ring)
3757 		return IRQ_HANDLED;
3758 
3759 	napi_schedule_irqoff(&q_vector->napi);
3760 
3761 	return IRQ_HANDLED;
3762 }
3763 
3764 /**
3765  * i40e_irq_affinity_notify - Callback for affinity changes
3766  * @notify: context as to what irq was changed
3767  * @mask: the new affinity mask
3768  *
3769  * This is a callback function used by the irq_set_affinity_notifier function
3770  * so that we may register to receive changes to the irq affinity masks.
3771  **/
3772 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3773 				     const cpumask_t *mask)
3774 {
3775 	struct i40e_q_vector *q_vector =
3776 		container_of(notify, struct i40e_q_vector, affinity_notify);
3777 
3778 	cpumask_copy(&q_vector->affinity_mask, mask);
3779 }
3780 
3781 /**
3782  * i40e_irq_affinity_release - Callback for affinity notifier release
3783  * @ref: internal core kernel usage
3784  *
3785  * This is a callback function used by the irq_set_affinity_notifier function
3786  * to inform the current notification subscriber that they will no longer
3787  * receive notifications.
3788  **/
3789 static void i40e_irq_affinity_release(struct kref *ref) {}
3790 
3791 /**
3792  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3793  * @vsi: the VSI being configured
3794  * @basename: name for the vector
3795  *
3796  * Allocates MSI-X vectors and requests interrupts from the kernel.
3797  **/
3798 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3799 {
3800 	int q_vectors = vsi->num_q_vectors;
3801 	struct i40e_pf *pf = vsi->back;
3802 	int base = vsi->base_vector;
3803 	int rx_int_idx = 0;
3804 	int tx_int_idx = 0;
3805 	int vector, err;
3806 	int irq_num;
3807 	int cpu;
3808 
3809 	for (vector = 0; vector < q_vectors; vector++) {
3810 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
3811 
3812 		irq_num = pf->msix_entries[base + vector].vector;
3813 
3814 		if (q_vector->tx.ring && q_vector->rx.ring) {
3815 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3816 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3817 			tx_int_idx++;
3818 		} else if (q_vector->rx.ring) {
3819 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3820 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
3821 		} else if (q_vector->tx.ring) {
3822 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3823 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
3824 		} else {
3825 			/* skip this unused q_vector */
3826 			continue;
3827 		}
3828 		err = request_irq(irq_num,
3829 				  vsi->irq_handler,
3830 				  0,
3831 				  q_vector->name,
3832 				  q_vector);
3833 		if (err) {
3834 			dev_info(&pf->pdev->dev,
3835 				 "MSIX request_irq failed, error: %d\n", err);
3836 			goto free_queue_irqs;
3837 		}
3838 
3839 		/* register for affinity change notifications */
3840 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3841 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
3842 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
3843 		/* Spread affinity hints out across online CPUs.
3844 		 *
3845 		 * get_cpu_mask returns a static constant mask with
3846 		 * a permanent lifetime so it's ok to pass to
3847 		 * irq_set_affinity_hint without making a copy.
3848 		 */
3849 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
3850 		irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
3851 	}
3852 
3853 	vsi->irqs_ready = true;
3854 	return 0;
3855 
3856 free_queue_irqs:
3857 	while (vector) {
3858 		vector--;
3859 		irq_num = pf->msix_entries[base + vector].vector;
3860 		irq_set_affinity_notifier(irq_num, NULL);
3861 		irq_set_affinity_hint(irq_num, NULL);
3862 		free_irq(irq_num, &vsi->q_vectors[vector]);
3863 	}
3864 	return err;
3865 }
3866 
3867 /**
3868  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3869  * @vsi: the VSI being un-configured
3870  **/
3871 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3872 {
3873 	struct i40e_pf *pf = vsi->back;
3874 	struct i40e_hw *hw = &pf->hw;
3875 	int base = vsi->base_vector;
3876 	int i;
3877 
3878 	/* disable interrupt causation from each queue */
3879 	for (i = 0; i < vsi->num_queue_pairs; i++) {
3880 		u32 val;
3881 
3882 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3883 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3884 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3885 
3886 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3887 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3888 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3889 
3890 		if (!i40e_enabled_xdp_vsi(vsi))
3891 			continue;
3892 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
3893 	}
3894 
3895 	/* disable each interrupt */
3896 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3897 		for (i = vsi->base_vector;
3898 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
3899 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3900 
3901 		i40e_flush(hw);
3902 		for (i = 0; i < vsi->num_q_vectors; i++)
3903 			synchronize_irq(pf->msix_entries[i + base].vector);
3904 	} else {
3905 		/* Legacy and MSI mode - this stops all interrupt handling */
3906 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3907 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3908 		i40e_flush(hw);
3909 		synchronize_irq(pf->pdev->irq);
3910 	}
3911 }
3912 
3913 /**
3914  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3915  * @vsi: the VSI being configured
3916  **/
3917 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3918 {
3919 	struct i40e_pf *pf = vsi->back;
3920 	int i;
3921 
3922 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3923 		for (i = 0; i < vsi->num_q_vectors; i++)
3924 			i40e_irq_dynamic_enable(vsi, i);
3925 	} else {
3926 		i40e_irq_dynamic_enable_icr0(pf);
3927 	}
3928 
3929 	i40e_flush(&pf->hw);
3930 	return 0;
3931 }
3932 
3933 /**
3934  * i40e_free_misc_vector - Free the vector that handles non-queue events
3935  * @pf: board private structure
3936  **/
3937 static void i40e_free_misc_vector(struct i40e_pf *pf)
3938 {
3939 	/* Disable ICR 0 */
3940 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3941 	i40e_flush(&pf->hw);
3942 
3943 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
3944 		synchronize_irq(pf->msix_entries[0].vector);
3945 		free_irq(pf->msix_entries[0].vector, pf);
3946 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
3947 	}
3948 }
3949 
3950 /**
3951  * i40e_intr - MSI/Legacy and non-queue interrupt handler
3952  * @irq: interrupt number
3953  * @data: pointer to a q_vector
3954  *
3955  * This is the handler used for all MSI/Legacy interrupts, and deals
3956  * with both queue and non-queue interrupts.  This is also used in
3957  * MSIX mode to handle the non-queue interrupts.
3958  **/
3959 static irqreturn_t i40e_intr(int irq, void *data)
3960 {
3961 	struct i40e_pf *pf = (struct i40e_pf *)data;
3962 	struct i40e_hw *hw = &pf->hw;
3963 	irqreturn_t ret = IRQ_NONE;
3964 	u32 icr0, icr0_remaining;
3965 	u32 val, ena_mask;
3966 
3967 	icr0 = rd32(hw, I40E_PFINT_ICR0);
3968 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
3969 
3970 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
3971 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
3972 		goto enable_intr;
3973 
3974 	/* if interrupt but no bits showing, must be SWINT */
3975 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3976 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3977 		pf->sw_int_count++;
3978 
3979 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3980 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3981 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3982 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
3983 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
3984 	}
3985 
3986 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3987 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
3988 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3989 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3990 
3991 		/* We do not have a way to disarm Queue causes while leaving
3992 		 * interrupt enabled for all other causes, ideally
3993 		 * interrupt should be disabled while we are in NAPI but
3994 		 * this is not a performance path and napi_schedule()
3995 		 * can deal with rescheduling.
3996 		 */
3997 		if (!test_bit(__I40E_DOWN, pf->state))
3998 			napi_schedule_irqoff(&q_vector->napi);
3999 	}
4000 
4001 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4002 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4003 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4004 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4005 	}
4006 
4007 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4008 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4009 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4010 	}
4011 
4012 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4013 		ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4014 		set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4015 	}
4016 
4017 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4018 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4019 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4020 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4021 		val = rd32(hw, I40E_GLGEN_RSTAT);
4022 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4023 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4024 		if (val == I40E_RESET_CORER) {
4025 			pf->corer_count++;
4026 		} else if (val == I40E_RESET_GLOBR) {
4027 			pf->globr_count++;
4028 		} else if (val == I40E_RESET_EMPR) {
4029 			pf->empr_count++;
4030 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4031 		}
4032 	}
4033 
4034 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4035 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4036 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4037 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4038 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4039 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4040 	}
4041 
4042 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4043 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4044 
4045 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
4046 			icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4047 			i40e_ptp_tx_hwtstamp(pf);
4048 		}
4049 	}
4050 
4051 	/* If a critical error is pending we have no choice but to reset the
4052 	 * device.
4053 	 * Report and mask out any remaining unexpected interrupts.
4054 	 */
4055 	icr0_remaining = icr0 & ena_mask;
4056 	if (icr0_remaining) {
4057 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4058 			 icr0_remaining);
4059 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4060 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4061 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4062 			dev_info(&pf->pdev->dev, "device will be reset\n");
4063 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4064 			i40e_service_event_schedule(pf);
4065 		}
4066 		ena_mask &= ~icr0_remaining;
4067 	}
4068 	ret = IRQ_HANDLED;
4069 
4070 enable_intr:
4071 	/* re-enable interrupt causes */
4072 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4073 	if (!test_bit(__I40E_DOWN, pf->state) ||
4074 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4075 		i40e_service_event_schedule(pf);
4076 		i40e_irq_dynamic_enable_icr0(pf);
4077 	}
4078 
4079 	return ret;
4080 }
4081 
4082 /**
4083  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4084  * @tx_ring:  tx ring to clean
4085  * @budget:   how many cleans we're allowed
4086  *
4087  * Returns true if there's any budget left (e.g. the clean is finished)
4088  **/
4089 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4090 {
4091 	struct i40e_vsi *vsi = tx_ring->vsi;
4092 	u16 i = tx_ring->next_to_clean;
4093 	struct i40e_tx_buffer *tx_buf;
4094 	struct i40e_tx_desc *tx_desc;
4095 
4096 	tx_buf = &tx_ring->tx_bi[i];
4097 	tx_desc = I40E_TX_DESC(tx_ring, i);
4098 	i -= tx_ring->count;
4099 
4100 	do {
4101 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4102 
4103 		/* if next_to_watch is not set then there is no work pending */
4104 		if (!eop_desc)
4105 			break;
4106 
4107 		/* prevent any other reads prior to eop_desc */
4108 		smp_rmb();
4109 
4110 		/* if the descriptor isn't done, no work yet to do */
4111 		if (!(eop_desc->cmd_type_offset_bsz &
4112 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4113 			break;
4114 
4115 		/* clear next_to_watch to prevent false hangs */
4116 		tx_buf->next_to_watch = NULL;
4117 
4118 		tx_desc->buffer_addr = 0;
4119 		tx_desc->cmd_type_offset_bsz = 0;
4120 		/* move past filter desc */
4121 		tx_buf++;
4122 		tx_desc++;
4123 		i++;
4124 		if (unlikely(!i)) {
4125 			i -= tx_ring->count;
4126 			tx_buf = tx_ring->tx_bi;
4127 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4128 		}
4129 		/* unmap skb header data */
4130 		dma_unmap_single(tx_ring->dev,
4131 				 dma_unmap_addr(tx_buf, dma),
4132 				 dma_unmap_len(tx_buf, len),
4133 				 DMA_TO_DEVICE);
4134 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4135 			kfree(tx_buf->raw_buf);
4136 
4137 		tx_buf->raw_buf = NULL;
4138 		tx_buf->tx_flags = 0;
4139 		tx_buf->next_to_watch = NULL;
4140 		dma_unmap_len_set(tx_buf, len, 0);
4141 		tx_desc->buffer_addr = 0;
4142 		tx_desc->cmd_type_offset_bsz = 0;
4143 
4144 		/* move us past the eop_desc for start of next FD desc */
4145 		tx_buf++;
4146 		tx_desc++;
4147 		i++;
4148 		if (unlikely(!i)) {
4149 			i -= tx_ring->count;
4150 			tx_buf = tx_ring->tx_bi;
4151 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4152 		}
4153 
4154 		/* update budget accounting */
4155 		budget--;
4156 	} while (likely(budget));
4157 
4158 	i += tx_ring->count;
4159 	tx_ring->next_to_clean = i;
4160 
4161 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4162 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4163 
4164 	return budget > 0;
4165 }
4166 
4167 /**
4168  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4169  * @irq: interrupt number
4170  * @data: pointer to a q_vector
4171  **/
4172 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4173 {
4174 	struct i40e_q_vector *q_vector = data;
4175 	struct i40e_vsi *vsi;
4176 
4177 	if (!q_vector->tx.ring)
4178 		return IRQ_HANDLED;
4179 
4180 	vsi = q_vector->tx.ring->vsi;
4181 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4182 
4183 	return IRQ_HANDLED;
4184 }
4185 
4186 /**
4187  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4188  * @vsi: the VSI being configured
4189  * @v_idx: vector index
4190  * @qp_idx: queue pair index
4191  **/
4192 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4193 {
4194 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4195 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4196 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4197 
4198 	tx_ring->q_vector = q_vector;
4199 	tx_ring->next = q_vector->tx.ring;
4200 	q_vector->tx.ring = tx_ring;
4201 	q_vector->tx.count++;
4202 
4203 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4204 	if (i40e_enabled_xdp_vsi(vsi)) {
4205 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4206 
4207 		xdp_ring->q_vector = q_vector;
4208 		xdp_ring->next = q_vector->tx.ring;
4209 		q_vector->tx.ring = xdp_ring;
4210 		q_vector->tx.count++;
4211 	}
4212 
4213 	rx_ring->q_vector = q_vector;
4214 	rx_ring->next = q_vector->rx.ring;
4215 	q_vector->rx.ring = rx_ring;
4216 	q_vector->rx.count++;
4217 }
4218 
4219 /**
4220  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4221  * @vsi: the VSI being configured
4222  *
4223  * This function maps descriptor rings to the queue-specific vectors
4224  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4225  * one vector per queue pair, but on a constrained vector budget, we
4226  * group the queue pairs as "efficiently" as possible.
4227  **/
4228 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4229 {
4230 	int qp_remaining = vsi->num_queue_pairs;
4231 	int q_vectors = vsi->num_q_vectors;
4232 	int num_ringpairs;
4233 	int v_start = 0;
4234 	int qp_idx = 0;
4235 
4236 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4237 	 * group them so there are multiple queues per vector.
4238 	 * It is also important to go through all the vectors available to be
4239 	 * sure that if we don't use all the vectors, that the remaining vectors
4240 	 * are cleared. This is especially important when decreasing the
4241 	 * number of queues in use.
4242 	 */
4243 	for (; v_start < q_vectors; v_start++) {
4244 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4245 
4246 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4247 
4248 		q_vector->num_ringpairs = num_ringpairs;
4249 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4250 
4251 		q_vector->rx.count = 0;
4252 		q_vector->tx.count = 0;
4253 		q_vector->rx.ring = NULL;
4254 		q_vector->tx.ring = NULL;
4255 
4256 		while (num_ringpairs--) {
4257 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4258 			qp_idx++;
4259 			qp_remaining--;
4260 		}
4261 	}
4262 }
4263 
4264 /**
4265  * i40e_vsi_request_irq - Request IRQ from the OS
4266  * @vsi: the VSI being configured
4267  * @basename: name for the vector
4268  **/
4269 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4270 {
4271 	struct i40e_pf *pf = vsi->back;
4272 	int err;
4273 
4274 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4275 		err = i40e_vsi_request_irq_msix(vsi, basename);
4276 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4277 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4278 				  pf->int_name, pf);
4279 	else
4280 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4281 				  pf->int_name, pf);
4282 
4283 	if (err)
4284 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4285 
4286 	return err;
4287 }
4288 
4289 #ifdef CONFIG_NET_POLL_CONTROLLER
4290 /**
4291  * i40e_netpoll - A Polling 'interrupt' handler
4292  * @netdev: network interface device structure
4293  *
4294  * This is used by netconsole to send skbs without having to re-enable
4295  * interrupts.  It's not called while the normal interrupt routine is executing.
4296  **/
4297 static void i40e_netpoll(struct net_device *netdev)
4298 {
4299 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4300 	struct i40e_vsi *vsi = np->vsi;
4301 	struct i40e_pf *pf = vsi->back;
4302 	int i;
4303 
4304 	/* if interface is down do nothing */
4305 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4306 		return;
4307 
4308 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4309 		for (i = 0; i < vsi->num_q_vectors; i++)
4310 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4311 	} else {
4312 		i40e_intr(pf->pdev->irq, netdev);
4313 	}
4314 }
4315 #endif
4316 
4317 #define I40E_QTX_ENA_WAIT_COUNT 50
4318 
4319 /**
4320  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4321  * @pf: the PF being configured
4322  * @pf_q: the PF queue
4323  * @enable: enable or disable state of the queue
4324  *
4325  * This routine will wait for the given Tx queue of the PF to reach the
4326  * enabled or disabled state.
4327  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4328  * multiple retries; else will return 0 in case of success.
4329  **/
4330 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4331 {
4332 	int i;
4333 	u32 tx_reg;
4334 
4335 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4336 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4337 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4338 			break;
4339 
4340 		usleep_range(10, 20);
4341 	}
4342 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4343 		return -ETIMEDOUT;
4344 
4345 	return 0;
4346 }
4347 
4348 /**
4349  * i40e_control_tx_q - Start or stop a particular Tx queue
4350  * @pf: the PF structure
4351  * @pf_q: the PF queue to configure
4352  * @enable: start or stop the queue
4353  *
4354  * This function enables or disables a single queue. Note that any delay
4355  * required after the operation is expected to be handled by the caller of
4356  * this function.
4357  **/
4358 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4359 {
4360 	struct i40e_hw *hw = &pf->hw;
4361 	u32 tx_reg;
4362 	int i;
4363 
4364 	/* warn the TX unit of coming changes */
4365 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4366 	if (!enable)
4367 		usleep_range(10, 20);
4368 
4369 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4370 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4371 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4372 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4373 			break;
4374 		usleep_range(1000, 2000);
4375 	}
4376 
4377 	/* Skip if the queue is already in the requested state */
4378 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4379 		return;
4380 
4381 	/* turn on/off the queue */
4382 	if (enable) {
4383 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4384 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4385 	} else {
4386 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4387 	}
4388 
4389 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4390 }
4391 
4392 /**
4393  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4394  * @seid: VSI SEID
4395  * @pf: the PF structure
4396  * @pf_q: the PF queue to configure
4397  * @is_xdp: true if the queue is used for XDP
4398  * @enable: start or stop the queue
4399  **/
4400 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4401 			   bool is_xdp, bool enable)
4402 {
4403 	int ret;
4404 
4405 	i40e_control_tx_q(pf, pf_q, enable);
4406 
4407 	/* wait for the change to finish */
4408 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4409 	if (ret) {
4410 		dev_info(&pf->pdev->dev,
4411 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4412 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4413 			 (enable ? "en" : "dis"));
4414 	}
4415 
4416 	return ret;
4417 }
4418 
4419 /**
4420  * i40e_vsi_control_tx - Start or stop a VSI's rings
4421  * @vsi: the VSI being configured
4422  * @enable: start or stop the rings
4423  **/
4424 static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4425 {
4426 	struct i40e_pf *pf = vsi->back;
4427 	int i, pf_q, ret = 0;
4428 
4429 	pf_q = vsi->base_queue;
4430 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4431 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4432 					     pf_q,
4433 					     false /*is xdp*/, enable);
4434 		if (ret)
4435 			break;
4436 
4437 		if (!i40e_enabled_xdp_vsi(vsi))
4438 			continue;
4439 
4440 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4441 					     pf_q + vsi->alloc_queue_pairs,
4442 					     true /*is xdp*/, enable);
4443 		if (ret)
4444 			break;
4445 	}
4446 	return ret;
4447 }
4448 
4449 /**
4450  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4451  * @pf: the PF being configured
4452  * @pf_q: the PF queue
4453  * @enable: enable or disable state of the queue
4454  *
4455  * This routine will wait for the given Rx queue of the PF to reach the
4456  * enabled or disabled state.
4457  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4458  * multiple retries; else will return 0 in case of success.
4459  **/
4460 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4461 {
4462 	int i;
4463 	u32 rx_reg;
4464 
4465 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4466 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4467 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4468 			break;
4469 
4470 		usleep_range(10, 20);
4471 	}
4472 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4473 		return -ETIMEDOUT;
4474 
4475 	return 0;
4476 }
4477 
4478 /**
4479  * i40e_control_rx_q - Start or stop a particular Rx queue
4480  * @pf: the PF structure
4481  * @pf_q: the PF queue to configure
4482  * @enable: start or stop the queue
4483  *
4484  * This function enables or disables a single queue. Note that
4485  * any delay required after the operation is expected to be
4486  * handled by the caller of this function.
4487  **/
4488 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4489 {
4490 	struct i40e_hw *hw = &pf->hw;
4491 	u32 rx_reg;
4492 	int i;
4493 
4494 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4495 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4496 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4497 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4498 			break;
4499 		usleep_range(1000, 2000);
4500 	}
4501 
4502 	/* Skip if the queue is already in the requested state */
4503 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4504 		return;
4505 
4506 	/* turn on/off the queue */
4507 	if (enable)
4508 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4509 	else
4510 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4511 
4512 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4513 }
4514 
4515 /**
4516  * i40e_control_wait_rx_q
4517  * @pf: the PF structure
4518  * @pf_q: queue being configured
4519  * @enable: start or stop the rings
4520  *
4521  * This function enables or disables a single queue along with waiting
4522  * for the change to finish. The caller of this function should handle
4523  * the delays needed in the case of disabling queues.
4524  **/
4525 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4526 {
4527 	int ret = 0;
4528 
4529 	i40e_control_rx_q(pf, pf_q, enable);
4530 
4531 	/* wait for the change to finish */
4532 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4533 	if (ret)
4534 		return ret;
4535 
4536 	return ret;
4537 }
4538 
4539 /**
4540  * i40e_vsi_control_rx - Start or stop a VSI's rings
4541  * @vsi: the VSI being configured
4542  * @enable: start or stop the rings
4543  **/
4544 static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4545 {
4546 	struct i40e_pf *pf = vsi->back;
4547 	int i, pf_q, ret = 0;
4548 
4549 	pf_q = vsi->base_queue;
4550 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4551 		ret = i40e_control_wait_rx_q(pf, pf_q, enable);
4552 		if (ret) {
4553 			dev_info(&pf->pdev->dev,
4554 				 "VSI seid %d Rx ring %d %sable timeout\n",
4555 				 vsi->seid, pf_q, (enable ? "en" : "dis"));
4556 			break;
4557 		}
4558 	}
4559 
4560 	/* Due to HW errata, on Rx disable only, the register can indicate done
4561 	 * before it really is. Needs 50ms to be sure
4562 	 */
4563 	if (!enable)
4564 		mdelay(50);
4565 
4566 	return ret;
4567 }
4568 
4569 /**
4570  * i40e_vsi_start_rings - Start a VSI's rings
4571  * @vsi: the VSI being configured
4572  **/
4573 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4574 {
4575 	int ret = 0;
4576 
4577 	/* do rx first for enable and last for disable */
4578 	ret = i40e_vsi_control_rx(vsi, true);
4579 	if (ret)
4580 		return ret;
4581 	ret = i40e_vsi_control_tx(vsi, true);
4582 
4583 	return ret;
4584 }
4585 
4586 /**
4587  * i40e_vsi_stop_rings - Stop a VSI's rings
4588  * @vsi: the VSI being configured
4589  **/
4590 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4591 {
4592 	/* When port TX is suspended, don't wait */
4593 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4594 		return i40e_vsi_stop_rings_no_wait(vsi);
4595 
4596 	/* do rx first for enable and last for disable
4597 	 * Ignore return value, we need to shutdown whatever we can
4598 	 */
4599 	i40e_vsi_control_tx(vsi, false);
4600 	i40e_vsi_control_rx(vsi, false);
4601 }
4602 
4603 /**
4604  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4605  * @vsi: the VSI being shutdown
4606  *
4607  * This function stops all the rings for a VSI but does not delay to verify
4608  * that rings have been disabled. It is expected that the caller is shutting
4609  * down multiple VSIs at once and will delay together for all the VSIs after
4610  * initiating the shutdown. This is particularly useful for shutting down lots
4611  * of VFs together. Otherwise, a large delay can be incurred while configuring
4612  * each VSI in serial.
4613  **/
4614 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4615 {
4616 	struct i40e_pf *pf = vsi->back;
4617 	int i, pf_q;
4618 
4619 	pf_q = vsi->base_queue;
4620 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4621 		i40e_control_tx_q(pf, pf_q, false);
4622 		i40e_control_rx_q(pf, pf_q, false);
4623 	}
4624 }
4625 
4626 /**
4627  * i40e_vsi_free_irq - Free the irq association with the OS
4628  * @vsi: the VSI being configured
4629  **/
4630 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4631 {
4632 	struct i40e_pf *pf = vsi->back;
4633 	struct i40e_hw *hw = &pf->hw;
4634 	int base = vsi->base_vector;
4635 	u32 val, qp;
4636 	int i;
4637 
4638 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4639 		if (!vsi->q_vectors)
4640 			return;
4641 
4642 		if (!vsi->irqs_ready)
4643 			return;
4644 
4645 		vsi->irqs_ready = false;
4646 		for (i = 0; i < vsi->num_q_vectors; i++) {
4647 			int irq_num;
4648 			u16 vector;
4649 
4650 			vector = i + base;
4651 			irq_num = pf->msix_entries[vector].vector;
4652 
4653 			/* free only the irqs that were actually requested */
4654 			if (!vsi->q_vectors[i] ||
4655 			    !vsi->q_vectors[i]->num_ringpairs)
4656 				continue;
4657 
4658 			/* clear the affinity notifier in the IRQ descriptor */
4659 			irq_set_affinity_notifier(irq_num, NULL);
4660 			/* remove our suggested affinity mask for this IRQ */
4661 			irq_set_affinity_hint(irq_num, NULL);
4662 			synchronize_irq(irq_num);
4663 			free_irq(irq_num, vsi->q_vectors[i]);
4664 
4665 			/* Tear down the interrupt queue link list
4666 			 *
4667 			 * We know that they come in pairs and always
4668 			 * the Rx first, then the Tx.  To clear the
4669 			 * link list, stick the EOL value into the
4670 			 * next_q field of the registers.
4671 			 */
4672 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4673 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4674 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4675 			val |= I40E_QUEUE_END_OF_LIST
4676 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4677 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4678 
4679 			while (qp != I40E_QUEUE_END_OF_LIST) {
4680 				u32 next;
4681 
4682 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4683 
4684 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4685 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4686 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4687 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4688 
4689 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4690 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4691 
4692 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4693 
4694 				val = rd32(hw, I40E_QINT_TQCTL(qp));
4695 
4696 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4697 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4698 
4699 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4700 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4701 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4702 					 I40E_QINT_TQCTL_INTEVENT_MASK);
4703 
4704 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4705 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4706 
4707 				wr32(hw, I40E_QINT_TQCTL(qp), val);
4708 				qp = next;
4709 			}
4710 		}
4711 	} else {
4712 		free_irq(pf->pdev->irq, pf);
4713 
4714 		val = rd32(hw, I40E_PFINT_LNKLST0);
4715 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4716 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4717 		val |= I40E_QUEUE_END_OF_LIST
4718 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4719 		wr32(hw, I40E_PFINT_LNKLST0, val);
4720 
4721 		val = rd32(hw, I40E_QINT_RQCTL(qp));
4722 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4723 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4724 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4725 			 I40E_QINT_RQCTL_INTEVENT_MASK);
4726 
4727 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4728 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4729 
4730 		wr32(hw, I40E_QINT_RQCTL(qp), val);
4731 
4732 		val = rd32(hw, I40E_QINT_TQCTL(qp));
4733 
4734 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4735 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4736 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4737 			 I40E_QINT_TQCTL_INTEVENT_MASK);
4738 
4739 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4740 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4741 
4742 		wr32(hw, I40E_QINT_TQCTL(qp), val);
4743 	}
4744 }
4745 
4746 /**
4747  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4748  * @vsi: the VSI being configured
4749  * @v_idx: Index of vector to be freed
4750  *
4751  * This function frees the memory allocated to the q_vector.  In addition if
4752  * NAPI is enabled it will delete any references to the NAPI struct prior
4753  * to freeing the q_vector.
4754  **/
4755 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4756 {
4757 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4758 	struct i40e_ring *ring;
4759 
4760 	if (!q_vector)
4761 		return;
4762 
4763 	/* disassociate q_vector from rings */
4764 	i40e_for_each_ring(ring, q_vector->tx)
4765 		ring->q_vector = NULL;
4766 
4767 	i40e_for_each_ring(ring, q_vector->rx)
4768 		ring->q_vector = NULL;
4769 
4770 	/* only VSI w/ an associated netdev is set up w/ NAPI */
4771 	if (vsi->netdev)
4772 		netif_napi_del(&q_vector->napi);
4773 
4774 	vsi->q_vectors[v_idx] = NULL;
4775 
4776 	kfree_rcu(q_vector, rcu);
4777 }
4778 
4779 /**
4780  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4781  * @vsi: the VSI being un-configured
4782  *
4783  * This frees the memory allocated to the q_vectors and
4784  * deletes references to the NAPI struct.
4785  **/
4786 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4787 {
4788 	int v_idx;
4789 
4790 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4791 		i40e_free_q_vector(vsi, v_idx);
4792 }
4793 
4794 /**
4795  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4796  * @pf: board private structure
4797  **/
4798 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4799 {
4800 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4801 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4802 		pci_disable_msix(pf->pdev);
4803 		kfree(pf->msix_entries);
4804 		pf->msix_entries = NULL;
4805 		kfree(pf->irq_pile);
4806 		pf->irq_pile = NULL;
4807 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4808 		pci_disable_msi(pf->pdev);
4809 	}
4810 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4811 }
4812 
4813 /**
4814  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4815  * @pf: board private structure
4816  *
4817  * We go through and clear interrupt specific resources and reset the structure
4818  * to pre-load conditions
4819  **/
4820 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4821 {
4822 	int i;
4823 
4824 	i40e_free_misc_vector(pf);
4825 
4826 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4827 		      I40E_IWARP_IRQ_PILE_ID);
4828 
4829 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
4830 	for (i = 0; i < pf->num_alloc_vsi; i++)
4831 		if (pf->vsi[i])
4832 			i40e_vsi_free_q_vectors(pf->vsi[i]);
4833 	i40e_reset_interrupt_capability(pf);
4834 }
4835 
4836 /**
4837  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4838  * @vsi: the VSI being configured
4839  **/
4840 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4841 {
4842 	int q_idx;
4843 
4844 	if (!vsi->netdev)
4845 		return;
4846 
4847 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4848 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4849 
4850 		if (q_vector->rx.ring || q_vector->tx.ring)
4851 			napi_enable(&q_vector->napi);
4852 	}
4853 }
4854 
4855 /**
4856  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4857  * @vsi: the VSI being configured
4858  **/
4859 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4860 {
4861 	int q_idx;
4862 
4863 	if (!vsi->netdev)
4864 		return;
4865 
4866 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4867 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4868 
4869 		if (q_vector->rx.ring || q_vector->tx.ring)
4870 			napi_disable(&q_vector->napi);
4871 	}
4872 }
4873 
4874 /**
4875  * i40e_vsi_close - Shut down a VSI
4876  * @vsi: the vsi to be quelled
4877  **/
4878 static void i40e_vsi_close(struct i40e_vsi *vsi)
4879 {
4880 	struct i40e_pf *pf = vsi->back;
4881 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
4882 		i40e_down(vsi);
4883 	i40e_vsi_free_irq(vsi);
4884 	i40e_vsi_free_tx_resources(vsi);
4885 	i40e_vsi_free_rx_resources(vsi);
4886 	vsi->current_netdev_flags = 0;
4887 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
4888 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4889 		set_bit(__I40E_CLIENT_RESET, pf->state);
4890 }
4891 
4892 /**
4893  * i40e_quiesce_vsi - Pause a given VSI
4894  * @vsi: the VSI being paused
4895  **/
4896 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4897 {
4898 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4899 		return;
4900 
4901 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
4902 	if (vsi->netdev && netif_running(vsi->netdev))
4903 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
4904 	else
4905 		i40e_vsi_close(vsi);
4906 }
4907 
4908 /**
4909  * i40e_unquiesce_vsi - Resume a given VSI
4910  * @vsi: the VSI being resumed
4911  **/
4912 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4913 {
4914 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
4915 		return;
4916 
4917 	if (vsi->netdev && netif_running(vsi->netdev))
4918 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4919 	else
4920 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
4921 }
4922 
4923 /**
4924  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4925  * @pf: the PF
4926  **/
4927 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4928 {
4929 	int v;
4930 
4931 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4932 		if (pf->vsi[v])
4933 			i40e_quiesce_vsi(pf->vsi[v]);
4934 	}
4935 }
4936 
4937 /**
4938  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4939  * @pf: the PF
4940  **/
4941 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4942 {
4943 	int v;
4944 
4945 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4946 		if (pf->vsi[v])
4947 			i40e_unquiesce_vsi(pf->vsi[v]);
4948 	}
4949 }
4950 
4951 /**
4952  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
4953  * @vsi: the VSI being configured
4954  *
4955  * Wait until all queues on a given VSI have been disabled.
4956  **/
4957 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
4958 {
4959 	struct i40e_pf *pf = vsi->back;
4960 	int i, pf_q, ret;
4961 
4962 	pf_q = vsi->base_queue;
4963 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4964 		/* Check and wait for the Tx queue */
4965 		ret = i40e_pf_txq_wait(pf, pf_q, false);
4966 		if (ret) {
4967 			dev_info(&pf->pdev->dev,
4968 				 "VSI seid %d Tx ring %d disable timeout\n",
4969 				 vsi->seid, pf_q);
4970 			return ret;
4971 		}
4972 
4973 		if (!i40e_enabled_xdp_vsi(vsi))
4974 			goto wait_rx;
4975 
4976 		/* Check and wait for the XDP Tx queue */
4977 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
4978 				       false);
4979 		if (ret) {
4980 			dev_info(&pf->pdev->dev,
4981 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
4982 				 vsi->seid, pf_q);
4983 			return ret;
4984 		}
4985 wait_rx:
4986 		/* Check and wait for the Rx queue */
4987 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
4988 		if (ret) {
4989 			dev_info(&pf->pdev->dev,
4990 				 "VSI seid %d Rx ring %d disable timeout\n",
4991 				 vsi->seid, pf_q);
4992 			return ret;
4993 		}
4994 	}
4995 
4996 	return 0;
4997 }
4998 
4999 #ifdef CONFIG_I40E_DCB
5000 /**
5001  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5002  * @pf: the PF
5003  *
5004  * This function waits for the queues to be in disabled state for all the
5005  * VSIs that are managed by this PF.
5006  **/
5007 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5008 {
5009 	int v, ret = 0;
5010 
5011 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5012 		if (pf->vsi[v]) {
5013 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5014 			if (ret)
5015 				break;
5016 		}
5017 	}
5018 
5019 	return ret;
5020 }
5021 
5022 #endif
5023 
5024 /**
5025  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5026  * @pf: pointer to PF
5027  *
5028  * Get TC map for ISCSI PF type that will include iSCSI TC
5029  * and LAN TC.
5030  **/
5031 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5032 {
5033 	struct i40e_dcb_app_priority_table app;
5034 	struct i40e_hw *hw = &pf->hw;
5035 	u8 enabled_tc = 1; /* TC0 is always enabled */
5036 	u8 tc, i;
5037 	/* Get the iSCSI APP TLV */
5038 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5039 
5040 	for (i = 0; i < dcbcfg->numapps; i++) {
5041 		app = dcbcfg->app[i];
5042 		if (app.selector == I40E_APP_SEL_TCPIP &&
5043 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5044 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5045 			enabled_tc |= BIT(tc);
5046 			break;
5047 		}
5048 	}
5049 
5050 	return enabled_tc;
5051 }
5052 
5053 /**
5054  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5055  * @dcbcfg: the corresponding DCBx configuration structure
5056  *
5057  * Return the number of TCs from given DCBx configuration
5058  **/
5059 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5060 {
5061 	int i, tc_unused = 0;
5062 	u8 num_tc = 0;
5063 	u8 ret = 0;
5064 
5065 	/* Scan the ETS Config Priority Table to find
5066 	 * traffic class enabled for a given priority
5067 	 * and create a bitmask of enabled TCs
5068 	 */
5069 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5070 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5071 
5072 	/* Now scan the bitmask to check for
5073 	 * contiguous TCs starting with TC0
5074 	 */
5075 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5076 		if (num_tc & BIT(i)) {
5077 			if (!tc_unused) {
5078 				ret++;
5079 			} else {
5080 				pr_err("Non-contiguous TC - Disabling DCB\n");
5081 				return 1;
5082 			}
5083 		} else {
5084 			tc_unused = 1;
5085 		}
5086 	}
5087 
5088 	/* There is always at least TC0 */
5089 	if (!ret)
5090 		ret = 1;
5091 
5092 	return ret;
5093 }
5094 
5095 /**
5096  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5097  * @dcbcfg: the corresponding DCBx configuration structure
5098  *
5099  * Query the current DCB configuration and return the number of
5100  * traffic classes enabled from the given DCBX config
5101  **/
5102 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5103 {
5104 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5105 	u8 enabled_tc = 1;
5106 	u8 i;
5107 
5108 	for (i = 0; i < num_tc; i++)
5109 		enabled_tc |= BIT(i);
5110 
5111 	return enabled_tc;
5112 }
5113 
5114 /**
5115  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5116  * @pf: PF being queried
5117  *
5118  * Query the current MQPRIO configuration and return the number of
5119  * traffic classes enabled.
5120  **/
5121 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5122 {
5123 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5124 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5125 	u8 enabled_tc = 1, i;
5126 
5127 	for (i = 1; i < num_tc; i++)
5128 		enabled_tc |= BIT(i);
5129 	return enabled_tc;
5130 }
5131 
5132 /**
5133  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5134  * @pf: PF being queried
5135  *
5136  * Return number of traffic classes enabled for the given PF
5137  **/
5138 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5139 {
5140 	struct i40e_hw *hw = &pf->hw;
5141 	u8 i, enabled_tc = 1;
5142 	u8 num_tc = 0;
5143 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5144 
5145 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5146 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5147 
5148 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5149 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5150 		return 1;
5151 
5152 	/* SFP mode will be enabled for all TCs on port */
5153 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5154 		return i40e_dcb_get_num_tc(dcbcfg);
5155 
5156 	/* MFP mode return count of enabled TCs for this PF */
5157 	if (pf->hw.func_caps.iscsi)
5158 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5159 	else
5160 		return 1; /* Only TC0 */
5161 
5162 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5163 		if (enabled_tc & BIT(i))
5164 			num_tc++;
5165 	}
5166 	return num_tc;
5167 }
5168 
5169 /**
5170  * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
5171  * @pf: PF being queried
5172  *
5173  * Return a bitmap for enabled traffic classes for this PF.
5174  **/
5175 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5176 {
5177 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5178 		return i40e_mqprio_get_enabled_tc(pf);
5179 
5180 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5181 	 * default TC
5182 	 */
5183 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5184 		return I40E_DEFAULT_TRAFFIC_CLASS;
5185 
5186 	/* SFP mode we want PF to be enabled for all TCs */
5187 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5188 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5189 
5190 	/* MFP enabled and iSCSI PF type */
5191 	if (pf->hw.func_caps.iscsi)
5192 		return i40e_get_iscsi_tc_map(pf);
5193 	else
5194 		return I40E_DEFAULT_TRAFFIC_CLASS;
5195 }
5196 
5197 /**
5198  * i40e_vsi_get_bw_info - Query VSI BW Information
5199  * @vsi: the VSI being queried
5200  *
5201  * Returns 0 on success, negative value on failure
5202  **/
5203 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5204 {
5205 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5206 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5207 	struct i40e_pf *pf = vsi->back;
5208 	struct i40e_hw *hw = &pf->hw;
5209 	i40e_status ret;
5210 	u32 tc_bw_max;
5211 	int i;
5212 
5213 	/* Get the VSI level BW configuration */
5214 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5215 	if (ret) {
5216 		dev_info(&pf->pdev->dev,
5217 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5218 			 i40e_stat_str(&pf->hw, ret),
5219 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5220 		return -EINVAL;
5221 	}
5222 
5223 	/* Get the VSI level BW configuration per TC */
5224 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5225 					       NULL);
5226 	if (ret) {
5227 		dev_info(&pf->pdev->dev,
5228 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5229 			 i40e_stat_str(&pf->hw, ret),
5230 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5231 		return -EINVAL;
5232 	}
5233 
5234 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5235 		dev_info(&pf->pdev->dev,
5236 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5237 			 bw_config.tc_valid_bits,
5238 			 bw_ets_config.tc_valid_bits);
5239 		/* Still continuing */
5240 	}
5241 
5242 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5243 	vsi->bw_max_quanta = bw_config.max_bw;
5244 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5245 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5246 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5247 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5248 		vsi->bw_ets_limit_credits[i] =
5249 					le16_to_cpu(bw_ets_config.credits[i]);
5250 		/* 3 bits out of 4 for each TC */
5251 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5252 	}
5253 
5254 	return 0;
5255 }
5256 
5257 /**
5258  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5259  * @vsi: the VSI being configured
5260  * @enabled_tc: TC bitmap
5261  * @bw_share: BW shared credits per TC
5262  *
5263  * Returns 0 on success, negative value on failure
5264  **/
5265 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5266 				       u8 *bw_share)
5267 {
5268 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5269 	struct i40e_pf *pf = vsi->back;
5270 	i40e_status ret;
5271 	int i;
5272 
5273 	/* There is no need to reset BW when mqprio mode is on.  */
5274 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5275 		return 0;
5276 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5277 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5278 		if (ret)
5279 			dev_info(&pf->pdev->dev,
5280 				 "Failed to reset tx rate for vsi->seid %u\n",
5281 				 vsi->seid);
5282 		return ret;
5283 	}
5284 	bw_data.tc_valid_bits = enabled_tc;
5285 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5286 		bw_data.tc_bw_credits[i] = bw_share[i];
5287 
5288 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5289 	if (ret) {
5290 		dev_info(&pf->pdev->dev,
5291 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5292 			 pf->hw.aq.asq_last_status);
5293 		return -EINVAL;
5294 	}
5295 
5296 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5297 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5298 
5299 	return 0;
5300 }
5301 
5302 /**
5303  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5304  * @vsi: the VSI being configured
5305  * @enabled_tc: TC map to be enabled
5306  *
5307  **/
5308 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5309 {
5310 	struct net_device *netdev = vsi->netdev;
5311 	struct i40e_pf *pf = vsi->back;
5312 	struct i40e_hw *hw = &pf->hw;
5313 	u8 netdev_tc = 0;
5314 	int i;
5315 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5316 
5317 	if (!netdev)
5318 		return;
5319 
5320 	if (!enabled_tc) {
5321 		netdev_reset_tc(netdev);
5322 		return;
5323 	}
5324 
5325 	/* Set up actual enabled TCs on the VSI */
5326 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5327 		return;
5328 
5329 	/* set per TC queues for the VSI */
5330 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5331 		/* Only set TC queues for enabled tcs
5332 		 *
5333 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5334 		 * enabled_tc bitmap would be 0x00001001; the driver
5335 		 * will set the numtc for netdev as 2 that will be
5336 		 * referenced by the netdev layer as TC 0 and 1.
5337 		 */
5338 		if (vsi->tc_config.enabled_tc & BIT(i))
5339 			netdev_set_tc_queue(netdev,
5340 					vsi->tc_config.tc_info[i].netdev_tc,
5341 					vsi->tc_config.tc_info[i].qcount,
5342 					vsi->tc_config.tc_info[i].qoffset);
5343 	}
5344 
5345 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5346 		return;
5347 
5348 	/* Assign UP2TC map for the VSI */
5349 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5350 		/* Get the actual TC# for the UP */
5351 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5352 		/* Get the mapped netdev TC# for the UP */
5353 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5354 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5355 	}
5356 }
5357 
5358 /**
5359  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5360  * @vsi: the VSI being configured
5361  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5362  **/
5363 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5364 				      struct i40e_vsi_context *ctxt)
5365 {
5366 	/* copy just the sections touched not the entire info
5367 	 * since not all sections are valid as returned by
5368 	 * update vsi params
5369 	 */
5370 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5371 	memcpy(&vsi->info.queue_mapping,
5372 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5373 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5374 	       sizeof(vsi->info.tc_mapping));
5375 }
5376 
5377 /**
5378  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5379  * @vsi: VSI to be configured
5380  * @enabled_tc: TC bitmap
5381  *
5382  * This configures a particular VSI for TCs that are mapped to the
5383  * given TC bitmap. It uses default bandwidth share for TCs across
5384  * VSIs to configure TC for a particular VSI.
5385  *
5386  * NOTE:
5387  * It is expected that the VSI queues have been quisced before calling
5388  * this function.
5389  **/
5390 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5391 {
5392 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5393 	struct i40e_pf *pf = vsi->back;
5394 	struct i40e_hw *hw = &pf->hw;
5395 	struct i40e_vsi_context ctxt;
5396 	int ret = 0;
5397 	int i;
5398 
5399 	/* Check if enabled_tc is same as existing or new TCs */
5400 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5401 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5402 		return ret;
5403 
5404 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5405 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5406 		if (enabled_tc & BIT(i))
5407 			bw_share[i] = 1;
5408 	}
5409 
5410 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5411 	if (ret) {
5412 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5413 
5414 		dev_info(&pf->pdev->dev,
5415 			 "Failed configuring TC map %d for VSI %d\n",
5416 			 enabled_tc, vsi->seid);
5417 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5418 						  &bw_config, NULL);
5419 		if (ret) {
5420 			dev_info(&pf->pdev->dev,
5421 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5422 				 i40e_stat_str(hw, ret),
5423 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5424 			goto out;
5425 		}
5426 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5427 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5428 
5429 			if (!valid_tc)
5430 				valid_tc = bw_config.tc_valid_bits;
5431 			/* Always enable TC0, no matter what */
5432 			valid_tc |= 1;
5433 			dev_info(&pf->pdev->dev,
5434 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5435 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5436 			enabled_tc = valid_tc;
5437 		}
5438 
5439 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5440 		if (ret) {
5441 			dev_err(&pf->pdev->dev,
5442 				"Unable to  configure TC map %d for VSI %d\n",
5443 				enabled_tc, vsi->seid);
5444 			goto out;
5445 		}
5446 	}
5447 
5448 	/* Update Queue Pairs Mapping for currently enabled UPs */
5449 	ctxt.seid = vsi->seid;
5450 	ctxt.pf_num = vsi->back->hw.pf_id;
5451 	ctxt.vf_num = 0;
5452 	ctxt.uplink_seid = vsi->uplink_seid;
5453 	ctxt.info = vsi->info;
5454 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5455 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5456 		if (ret)
5457 			goto out;
5458 	} else {
5459 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5460 	}
5461 
5462 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5463 	 * queues changed.
5464 	 */
5465 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5466 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5467 				      vsi->num_queue_pairs);
5468 		ret = i40e_vsi_config_rss(vsi);
5469 		if (ret) {
5470 			dev_info(&vsi->back->pdev->dev,
5471 				 "Failed to reconfig rss for num_queues\n");
5472 			return ret;
5473 		}
5474 		vsi->reconfig_rss = false;
5475 	}
5476 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5477 		ctxt.info.valid_sections |=
5478 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5479 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5480 	}
5481 
5482 	/* Update the VSI after updating the VSI queue-mapping
5483 	 * information
5484 	 */
5485 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5486 	if (ret) {
5487 		dev_info(&pf->pdev->dev,
5488 			 "Update vsi tc config failed, err %s aq_err %s\n",
5489 			 i40e_stat_str(hw, ret),
5490 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5491 		goto out;
5492 	}
5493 	/* update the local VSI info with updated queue map */
5494 	i40e_vsi_update_queue_map(vsi, &ctxt);
5495 	vsi->info.valid_sections = 0;
5496 
5497 	/* Update current VSI BW information */
5498 	ret = i40e_vsi_get_bw_info(vsi);
5499 	if (ret) {
5500 		dev_info(&pf->pdev->dev,
5501 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5502 			 i40e_stat_str(hw, ret),
5503 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5504 		goto out;
5505 	}
5506 
5507 	/* Update the netdev TC setup */
5508 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5509 out:
5510 	return ret;
5511 }
5512 
5513 /**
5514  * i40e_get_link_speed - Returns link speed for the interface
5515  * @vsi: VSI to be configured
5516  *
5517  **/
5518 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5519 {
5520 	struct i40e_pf *pf = vsi->back;
5521 
5522 	switch (pf->hw.phy.link_info.link_speed) {
5523 	case I40E_LINK_SPEED_40GB:
5524 		return 40000;
5525 	case I40E_LINK_SPEED_25GB:
5526 		return 25000;
5527 	case I40E_LINK_SPEED_20GB:
5528 		return 20000;
5529 	case I40E_LINK_SPEED_10GB:
5530 		return 10000;
5531 	case I40E_LINK_SPEED_1GB:
5532 		return 1000;
5533 	default:
5534 		return -EINVAL;
5535 	}
5536 }
5537 
5538 /**
5539  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5540  * @vsi: VSI to be configured
5541  * @seid: seid of the channel/VSI
5542  * @max_tx_rate: max TX rate to be configured as BW limit
5543  *
5544  * Helper function to set BW limit for a given VSI
5545  **/
5546 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5547 {
5548 	struct i40e_pf *pf = vsi->back;
5549 	u64 credits = 0;
5550 	int speed = 0;
5551 	int ret = 0;
5552 
5553 	speed = i40e_get_link_speed(vsi);
5554 	if (max_tx_rate > speed) {
5555 		dev_err(&pf->pdev->dev,
5556 			"Invalid max tx rate %llu specified for VSI seid %d.",
5557 			max_tx_rate, seid);
5558 		return -EINVAL;
5559 	}
5560 	if (max_tx_rate && max_tx_rate < 50) {
5561 		dev_warn(&pf->pdev->dev,
5562 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5563 		max_tx_rate = 50;
5564 	}
5565 
5566 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5567 	credits = max_tx_rate;
5568 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5569 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5570 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5571 	if (ret)
5572 		dev_err(&pf->pdev->dev,
5573 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5574 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5575 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5576 	return ret;
5577 }
5578 
5579 /**
5580  * i40e_remove_queue_channels - Remove queue channels for the TCs
5581  * @vsi: VSI to be configured
5582  *
5583  * Remove queue channels for the TCs
5584  **/
5585 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5586 {
5587 	enum i40e_admin_queue_err last_aq_status;
5588 	struct i40e_cloud_filter *cfilter;
5589 	struct i40e_channel *ch, *ch_tmp;
5590 	struct i40e_pf *pf = vsi->back;
5591 	struct hlist_node *node;
5592 	int ret, i;
5593 
5594 	/* Reset rss size that was stored when reconfiguring rss for
5595 	 * channel VSIs with non-power-of-2 queue count.
5596 	 */
5597 	vsi->current_rss_size = 0;
5598 
5599 	/* perform cleanup for channels if they exist */
5600 	if (list_empty(&vsi->ch_list))
5601 		return;
5602 
5603 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5604 		struct i40e_vsi *p_vsi;
5605 
5606 		list_del(&ch->list);
5607 		p_vsi = ch->parent_vsi;
5608 		if (!p_vsi || !ch->initialized) {
5609 			kfree(ch);
5610 			continue;
5611 		}
5612 		/* Reset queue contexts */
5613 		for (i = 0; i < ch->num_queue_pairs; i++) {
5614 			struct i40e_ring *tx_ring, *rx_ring;
5615 			u16 pf_q;
5616 
5617 			pf_q = ch->base_queue + i;
5618 			tx_ring = vsi->tx_rings[pf_q];
5619 			tx_ring->ch = NULL;
5620 
5621 			rx_ring = vsi->rx_rings[pf_q];
5622 			rx_ring->ch = NULL;
5623 		}
5624 
5625 		/* Reset BW configured for this VSI via mqprio */
5626 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5627 		if (ret)
5628 			dev_info(&vsi->back->pdev->dev,
5629 				 "Failed to reset tx rate for ch->seid %u\n",
5630 				 ch->seid);
5631 
5632 		/* delete cloud filters associated with this channel */
5633 		hlist_for_each_entry_safe(cfilter, node,
5634 					  &pf->cloud_filter_list, cloud_node) {
5635 			if (cfilter->seid != ch->seid)
5636 				continue;
5637 
5638 			hash_del(&cfilter->cloud_node);
5639 			if (cfilter->dst_port)
5640 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
5641 									cfilter,
5642 									false);
5643 			else
5644 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
5645 								false);
5646 			last_aq_status = pf->hw.aq.asq_last_status;
5647 			if (ret)
5648 				dev_info(&pf->pdev->dev,
5649 					 "Failed to delete cloud filter, err %s aq_err %s\n",
5650 					 i40e_stat_str(&pf->hw, ret),
5651 					 i40e_aq_str(&pf->hw, last_aq_status));
5652 			kfree(cfilter);
5653 		}
5654 
5655 		/* delete VSI from FW */
5656 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5657 					     NULL);
5658 		if (ret)
5659 			dev_err(&vsi->back->pdev->dev,
5660 				"unable to remove channel (%d) for parent VSI(%d)\n",
5661 				ch->seid, p_vsi->seid);
5662 		kfree(ch);
5663 	}
5664 	INIT_LIST_HEAD(&vsi->ch_list);
5665 }
5666 
5667 /**
5668  * i40e_is_any_channel - channel exist or not
5669  * @vsi: ptr to VSI to which channels are associated with
5670  *
5671  * Returns true or false if channel(s) exist for associated VSI or not
5672  **/
5673 static bool i40e_is_any_channel(struct i40e_vsi *vsi)
5674 {
5675 	struct i40e_channel *ch, *ch_tmp;
5676 
5677 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5678 		if (ch->initialized)
5679 			return true;
5680 	}
5681 
5682 	return false;
5683 }
5684 
5685 /**
5686  * i40e_get_max_queues_for_channel
5687  * @vsi: ptr to VSI to which channels are associated with
5688  *
5689  * Helper function which returns max value among the queue counts set on the
5690  * channels/TCs created.
5691  **/
5692 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5693 {
5694 	struct i40e_channel *ch, *ch_tmp;
5695 	int max = 0;
5696 
5697 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5698 		if (!ch->initialized)
5699 			continue;
5700 		if (ch->num_queue_pairs > max)
5701 			max = ch->num_queue_pairs;
5702 	}
5703 
5704 	return max;
5705 }
5706 
5707 /**
5708  * i40e_validate_num_queues - validate num_queues w.r.t channel
5709  * @pf: ptr to PF device
5710  * @num_queues: number of queues
5711  * @vsi: the parent VSI
5712  * @reconfig_rss: indicates should the RSS be reconfigured or not
5713  *
5714  * This function validates number of queues in the context of new channel
5715  * which is being established and determines if RSS should be reconfigured
5716  * or not for parent VSI.
5717  **/
5718 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5719 				    struct i40e_vsi *vsi, bool *reconfig_rss)
5720 {
5721 	int max_ch_queues;
5722 
5723 	if (!reconfig_rss)
5724 		return -EINVAL;
5725 
5726 	*reconfig_rss = false;
5727 	if (vsi->current_rss_size) {
5728 		if (num_queues > vsi->current_rss_size) {
5729 			dev_dbg(&pf->pdev->dev,
5730 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
5731 				num_queues, vsi->current_rss_size);
5732 			return -EINVAL;
5733 		} else if ((num_queues < vsi->current_rss_size) &&
5734 			   (!is_power_of_2(num_queues))) {
5735 			dev_dbg(&pf->pdev->dev,
5736 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5737 				num_queues, vsi->current_rss_size);
5738 			return -EINVAL;
5739 		}
5740 	}
5741 
5742 	if (!is_power_of_2(num_queues)) {
5743 		/* Find the max num_queues configured for channel if channel
5744 		 * exist.
5745 		 * if channel exist, then enforce 'num_queues' to be more than
5746 		 * max ever queues configured for channel.
5747 		 */
5748 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5749 		if (num_queues < max_ch_queues) {
5750 			dev_dbg(&pf->pdev->dev,
5751 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
5752 				num_queues, max_ch_queues);
5753 			return -EINVAL;
5754 		}
5755 		*reconfig_rss = true;
5756 	}
5757 
5758 	return 0;
5759 }
5760 
5761 /**
5762  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5763  * @vsi: the VSI being setup
5764  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5765  *
5766  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5767  **/
5768 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5769 {
5770 	struct i40e_pf *pf = vsi->back;
5771 	u8 seed[I40E_HKEY_ARRAY_SIZE];
5772 	struct i40e_hw *hw = &pf->hw;
5773 	int local_rss_size;
5774 	u8 *lut;
5775 	int ret;
5776 
5777 	if (!vsi->rss_size)
5778 		return -EINVAL;
5779 
5780 	if (rss_size > vsi->rss_size)
5781 		return -EINVAL;
5782 
5783 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
5784 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5785 	if (!lut)
5786 		return -ENOMEM;
5787 
5788 	/* Ignoring user configured lut if there is one */
5789 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5790 
5791 	/* Use user configured hash key if there is one, otherwise
5792 	 * use default.
5793 	 */
5794 	if (vsi->rss_hkey_user)
5795 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5796 	else
5797 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5798 
5799 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5800 	if (ret) {
5801 		dev_info(&pf->pdev->dev,
5802 			 "Cannot set RSS lut, err %s aq_err %s\n",
5803 			 i40e_stat_str(hw, ret),
5804 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5805 		kfree(lut);
5806 		return ret;
5807 	}
5808 	kfree(lut);
5809 
5810 	/* Do the update w.r.t. storing rss_size */
5811 	if (!vsi->orig_rss_size)
5812 		vsi->orig_rss_size = vsi->rss_size;
5813 	vsi->current_rss_size = local_rss_size;
5814 
5815 	return ret;
5816 }
5817 
5818 /**
5819  * i40e_channel_setup_queue_map - Setup a channel queue map
5820  * @pf: ptr to PF device
5821  * @ctxt: VSI context structure
5822  * @ch: ptr to channel structure
5823  *
5824  * Setup queue map for a specific channel
5825  **/
5826 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5827 					 struct i40e_vsi_context *ctxt,
5828 					 struct i40e_channel *ch)
5829 {
5830 	u16 qcount, qmap, sections = 0;
5831 	u8 offset = 0;
5832 	int pow;
5833 
5834 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5835 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5836 
5837 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5838 	ch->num_queue_pairs = qcount;
5839 
5840 	/* find the next higher power-of-2 of num queue pairs */
5841 	pow = ilog2(qcount);
5842 	if (!is_power_of_2(qcount))
5843 		pow++;
5844 
5845 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5846 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
5847 
5848 	/* Setup queue TC[0].qmap for given VSI context */
5849 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
5850 
5851 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
5852 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
5853 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
5854 	ctxt->info.valid_sections |= cpu_to_le16(sections);
5855 }
5856 
5857 /**
5858  * i40e_add_channel - add a channel by adding VSI
5859  * @pf: ptr to PF device
5860  * @uplink_seid: underlying HW switching element (VEB) ID
5861  * @ch: ptr to channel structure
5862  *
5863  * Add a channel (VSI) using add_vsi and queue_map
5864  **/
5865 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
5866 			    struct i40e_channel *ch)
5867 {
5868 	struct i40e_hw *hw = &pf->hw;
5869 	struct i40e_vsi_context ctxt;
5870 	u8 enabled_tc = 0x1; /* TC0 enabled */
5871 	int ret;
5872 
5873 	if (ch->type != I40E_VSI_VMDQ2) {
5874 		dev_info(&pf->pdev->dev,
5875 			 "add new vsi failed, ch->type %d\n", ch->type);
5876 		return -EINVAL;
5877 	}
5878 
5879 	memset(&ctxt, 0, sizeof(ctxt));
5880 	ctxt.pf_num = hw->pf_id;
5881 	ctxt.vf_num = 0;
5882 	ctxt.uplink_seid = uplink_seid;
5883 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5884 	if (ch->type == I40E_VSI_VMDQ2)
5885 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
5886 
5887 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
5888 		ctxt.info.valid_sections |=
5889 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5890 		ctxt.info.switch_id =
5891 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5892 	}
5893 
5894 	/* Set queue map for a given VSI context */
5895 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
5896 
5897 	/* Now time to create VSI */
5898 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
5899 	if (ret) {
5900 		dev_info(&pf->pdev->dev,
5901 			 "add new vsi failed, err %s aq_err %s\n",
5902 			 i40e_stat_str(&pf->hw, ret),
5903 			 i40e_aq_str(&pf->hw,
5904 				     pf->hw.aq.asq_last_status));
5905 		return -ENOENT;
5906 	}
5907 
5908 	/* Success, update channel, set enabled_tc only if the channel
5909 	 * is not a macvlan
5910 	 */
5911 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
5912 	ch->seid = ctxt.seid;
5913 	ch->vsi_number = ctxt.vsi_number;
5914 	ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx);
5915 
5916 	/* copy just the sections touched not the entire info
5917 	 * since not all sections are valid as returned by
5918 	 * update vsi params
5919 	 */
5920 	ch->info.mapping_flags = ctxt.info.mapping_flags;
5921 	memcpy(&ch->info.queue_mapping,
5922 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
5923 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
5924 	       sizeof(ctxt.info.tc_mapping));
5925 
5926 	return 0;
5927 }
5928 
5929 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
5930 				  u8 *bw_share)
5931 {
5932 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5933 	i40e_status ret;
5934 	int i;
5935 
5936 	bw_data.tc_valid_bits = ch->enabled_tc;
5937 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5938 		bw_data.tc_bw_credits[i] = bw_share[i];
5939 
5940 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
5941 				       &bw_data, NULL);
5942 	if (ret) {
5943 		dev_info(&vsi->back->pdev->dev,
5944 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
5945 			 vsi->back->hw.aq.asq_last_status, ch->seid);
5946 		return -EINVAL;
5947 	}
5948 
5949 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5950 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
5951 
5952 	return 0;
5953 }
5954 
5955 /**
5956  * i40e_channel_config_tx_ring - config TX ring associated with new channel
5957  * @pf: ptr to PF device
5958  * @vsi: the VSI being setup
5959  * @ch: ptr to channel structure
5960  *
5961  * Configure TX rings associated with channel (VSI) since queues are being
5962  * from parent VSI.
5963  **/
5964 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
5965 				       struct i40e_vsi *vsi,
5966 				       struct i40e_channel *ch)
5967 {
5968 	i40e_status ret;
5969 	int i;
5970 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5971 
5972 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5973 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5974 		if (ch->enabled_tc & BIT(i))
5975 			bw_share[i] = 1;
5976 	}
5977 
5978 	/* configure BW for new VSI */
5979 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
5980 	if (ret) {
5981 		dev_info(&vsi->back->pdev->dev,
5982 			 "Failed configuring TC map %d for channel (seid %u)\n",
5983 			 ch->enabled_tc, ch->seid);
5984 		return ret;
5985 	}
5986 
5987 	for (i = 0; i < ch->num_queue_pairs; i++) {
5988 		struct i40e_ring *tx_ring, *rx_ring;
5989 		u16 pf_q;
5990 
5991 		pf_q = ch->base_queue + i;
5992 
5993 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
5994 		 * context
5995 		 */
5996 		tx_ring = vsi->tx_rings[pf_q];
5997 		tx_ring->ch = ch;
5998 
5999 		/* Get the RX ring ptr */
6000 		rx_ring = vsi->rx_rings[pf_q];
6001 		rx_ring->ch = ch;
6002 	}
6003 
6004 	return 0;
6005 }
6006 
6007 /**
6008  * i40e_setup_hw_channel - setup new channel
6009  * @pf: ptr to PF device
6010  * @vsi: the VSI being setup
6011  * @ch: ptr to channel structure
6012  * @uplink_seid: underlying HW switching element (VEB) ID
6013  * @type: type of channel to be created (VMDq2/VF)
6014  *
6015  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6016  * and configures TX rings accordingly
6017  **/
6018 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6019 					struct i40e_vsi *vsi,
6020 					struct i40e_channel *ch,
6021 					u16 uplink_seid, u8 type)
6022 {
6023 	int ret;
6024 
6025 	ch->initialized = false;
6026 	ch->base_queue = vsi->next_base_queue;
6027 	ch->type = type;
6028 
6029 	/* Proceed with creation of channel (VMDq2) VSI */
6030 	ret = i40e_add_channel(pf, uplink_seid, ch);
6031 	if (ret) {
6032 		dev_info(&pf->pdev->dev,
6033 			 "failed to add_channel using uplink_seid %u\n",
6034 			 uplink_seid);
6035 		return ret;
6036 	}
6037 
6038 	/* Mark the successful creation of channel */
6039 	ch->initialized = true;
6040 
6041 	/* Reconfigure TX queues using QTX_CTL register */
6042 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6043 	if (ret) {
6044 		dev_info(&pf->pdev->dev,
6045 			 "failed to configure TX rings for channel %u\n",
6046 			 ch->seid);
6047 		return ret;
6048 	}
6049 
6050 	/* update 'next_base_queue' */
6051 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6052 	dev_dbg(&pf->pdev->dev,
6053 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6054 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6055 		ch->num_queue_pairs,
6056 		vsi->next_base_queue);
6057 	return ret;
6058 }
6059 
6060 /**
6061  * i40e_setup_channel - setup new channel using uplink element
6062  * @pf: ptr to PF device
6063  * @vsi: pointer to the VSI to set up the channel within
6064  * @ch: ptr to channel structure
6065  *
6066  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6067  * and uplink switching element (uplink_seid)
6068  **/
6069 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6070 			       struct i40e_channel *ch)
6071 {
6072 	u8 vsi_type;
6073 	u16 seid;
6074 	int ret;
6075 
6076 	if (vsi->type == I40E_VSI_MAIN) {
6077 		vsi_type = I40E_VSI_VMDQ2;
6078 	} else {
6079 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6080 			vsi->type);
6081 		return false;
6082 	}
6083 
6084 	/* underlying switching element */
6085 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6086 
6087 	/* create channel (VSI), configure TX rings */
6088 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6089 	if (ret) {
6090 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6091 		return false;
6092 	}
6093 
6094 	return ch->initialized ? true : false;
6095 }
6096 
6097 /**
6098  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6099  * @vsi: ptr to VSI which has PF backing
6100  *
6101  * Sets up switch mode correctly if it needs to be changed and perform
6102  * what are allowed modes.
6103  **/
6104 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6105 {
6106 	u8 mode;
6107 	struct i40e_pf *pf = vsi->back;
6108 	struct i40e_hw *hw = &pf->hw;
6109 	int ret;
6110 
6111 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6112 	if (ret)
6113 		return -EINVAL;
6114 
6115 	if (hw->dev_caps.switch_mode) {
6116 		/* if switch mode is set, support mode2 (non-tunneled for
6117 		 * cloud filter) for now
6118 		 */
6119 		u32 switch_mode = hw->dev_caps.switch_mode &
6120 				  I40E_SWITCH_MODE_MASK;
6121 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6122 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6123 				return 0;
6124 			dev_err(&pf->pdev->dev,
6125 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6126 				hw->dev_caps.switch_mode);
6127 			return -EINVAL;
6128 		}
6129 	}
6130 
6131 	/* Set Bit 7 to be valid */
6132 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6133 
6134 	/* Set L4type for TCP support */
6135 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6136 
6137 	/* Set cloud filter mode */
6138 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6139 
6140 	/* Prep mode field for set_switch_config */
6141 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6142 					pf->last_sw_conf_valid_flags,
6143 					mode, NULL);
6144 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6145 		dev_err(&pf->pdev->dev,
6146 			"couldn't set switch config bits, err %s aq_err %s\n",
6147 			i40e_stat_str(hw, ret),
6148 			i40e_aq_str(hw,
6149 				    hw->aq.asq_last_status));
6150 
6151 	return ret;
6152 }
6153 
6154 /**
6155  * i40e_create_queue_channel - function to create channel
6156  * @vsi: VSI to be configured
6157  * @ch: ptr to channel (it contains channel specific params)
6158  *
6159  * This function creates channel (VSI) using num_queues specified by user,
6160  * reconfigs RSS if needed.
6161  **/
6162 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6163 			      struct i40e_channel *ch)
6164 {
6165 	struct i40e_pf *pf = vsi->back;
6166 	bool reconfig_rss;
6167 	int err;
6168 
6169 	if (!ch)
6170 		return -EINVAL;
6171 
6172 	if (!ch->num_queue_pairs) {
6173 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6174 			ch->num_queue_pairs);
6175 		return -EINVAL;
6176 	}
6177 
6178 	/* validate user requested num_queues for channel */
6179 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6180 				       &reconfig_rss);
6181 	if (err) {
6182 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6183 			 ch->num_queue_pairs);
6184 		return -EINVAL;
6185 	}
6186 
6187 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6188 	 * VSI to be added switch to VEB mode.
6189 	 */
6190 	if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
6191 	    (!i40e_is_any_channel(vsi))) {
6192 		if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
6193 			dev_dbg(&pf->pdev->dev,
6194 				"Failed to create channel. Override queues (%u) not power of 2\n",
6195 				vsi->tc_config.tc_info[0].qcount);
6196 			return -EINVAL;
6197 		}
6198 
6199 		if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6200 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6201 
6202 			if (vsi->type == I40E_VSI_MAIN) {
6203 				if (pf->flags & I40E_FLAG_TC_MQPRIO)
6204 					i40e_do_reset(pf, I40E_PF_RESET_FLAG,
6205 						      true);
6206 				else
6207 					i40e_do_reset_safe(pf,
6208 							   I40E_PF_RESET_FLAG);
6209 			}
6210 		}
6211 		/* now onwards for main VSI, number of queues will be value
6212 		 * of TC0's queue count
6213 		 */
6214 	}
6215 
6216 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6217 	 * it should be more than num_queues
6218 	 */
6219 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6220 		dev_dbg(&pf->pdev->dev,
6221 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6222 			vsi->cnt_q_avail, ch->num_queue_pairs);
6223 		return -EINVAL;
6224 	}
6225 
6226 	/* reconfig_rss only if vsi type is MAIN_VSI */
6227 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6228 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6229 		if (err) {
6230 			dev_info(&pf->pdev->dev,
6231 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6232 				 ch->num_queue_pairs);
6233 			return -EINVAL;
6234 		}
6235 	}
6236 
6237 	if (!i40e_setup_channel(pf, vsi, ch)) {
6238 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6239 		return -EINVAL;
6240 	}
6241 
6242 	dev_info(&pf->pdev->dev,
6243 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6244 		 ch->seid, ch->num_queue_pairs);
6245 
6246 	/* configure VSI for BW limit */
6247 	if (ch->max_tx_rate) {
6248 		u64 credits = ch->max_tx_rate;
6249 
6250 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6251 			return -EINVAL;
6252 
6253 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6254 		dev_dbg(&pf->pdev->dev,
6255 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6256 			ch->max_tx_rate,
6257 			credits,
6258 			ch->seid);
6259 	}
6260 
6261 	/* in case of VF, this will be main SRIOV VSI */
6262 	ch->parent_vsi = vsi;
6263 
6264 	/* and update main_vsi's count for queue_available to use */
6265 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6266 
6267 	return 0;
6268 }
6269 
6270 /**
6271  * i40e_configure_queue_channels - Add queue channel for the given TCs
6272  * @vsi: VSI to be configured
6273  *
6274  * Configures queue channel mapping to the given TCs
6275  **/
6276 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6277 {
6278 	struct i40e_channel *ch;
6279 	u64 max_rate = 0;
6280 	int ret = 0, i;
6281 
6282 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6283 	vsi->tc_seid_map[0] = vsi->seid;
6284 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6285 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6286 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6287 			if (!ch) {
6288 				ret = -ENOMEM;
6289 				goto err_free;
6290 			}
6291 
6292 			INIT_LIST_HEAD(&ch->list);
6293 			ch->num_queue_pairs =
6294 				vsi->tc_config.tc_info[i].qcount;
6295 			ch->base_queue =
6296 				vsi->tc_config.tc_info[i].qoffset;
6297 
6298 			/* Bandwidth limit through tc interface is in bytes/s,
6299 			 * change to Mbit/s
6300 			 */
6301 			max_rate = vsi->mqprio_qopt.max_rate[i];
6302 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6303 			ch->max_tx_rate = max_rate;
6304 
6305 			list_add_tail(&ch->list, &vsi->ch_list);
6306 
6307 			ret = i40e_create_queue_channel(vsi, ch);
6308 			if (ret) {
6309 				dev_err(&vsi->back->pdev->dev,
6310 					"Failed creating queue channel with TC%d: queues %d\n",
6311 					i, ch->num_queue_pairs);
6312 				goto err_free;
6313 			}
6314 			vsi->tc_seid_map[i] = ch->seid;
6315 		}
6316 	}
6317 	return ret;
6318 
6319 err_free:
6320 	i40e_remove_queue_channels(vsi);
6321 	return ret;
6322 }
6323 
6324 /**
6325  * i40e_veb_config_tc - Configure TCs for given VEB
6326  * @veb: given VEB
6327  * @enabled_tc: TC bitmap
6328  *
6329  * Configures given TC bitmap for VEB (switching) element
6330  **/
6331 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6332 {
6333 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6334 	struct i40e_pf *pf = veb->pf;
6335 	int ret = 0;
6336 	int i;
6337 
6338 	/* No TCs or already enabled TCs just return */
6339 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6340 		return ret;
6341 
6342 	bw_data.tc_valid_bits = enabled_tc;
6343 	/* bw_data.absolute_credits is not set (relative) */
6344 
6345 	/* Enable ETS TCs with equal BW Share for now */
6346 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6347 		if (enabled_tc & BIT(i))
6348 			bw_data.tc_bw_share_credits[i] = 1;
6349 	}
6350 
6351 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6352 						   &bw_data, NULL);
6353 	if (ret) {
6354 		dev_info(&pf->pdev->dev,
6355 			 "VEB bw config failed, err %s aq_err %s\n",
6356 			 i40e_stat_str(&pf->hw, ret),
6357 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6358 		goto out;
6359 	}
6360 
6361 	/* Update the BW information */
6362 	ret = i40e_veb_get_bw_info(veb);
6363 	if (ret) {
6364 		dev_info(&pf->pdev->dev,
6365 			 "Failed getting veb bw config, err %s aq_err %s\n",
6366 			 i40e_stat_str(&pf->hw, ret),
6367 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6368 	}
6369 
6370 out:
6371 	return ret;
6372 }
6373 
6374 #ifdef CONFIG_I40E_DCB
6375 /**
6376  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6377  * @pf: PF struct
6378  *
6379  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6380  * the caller would've quiesce all the VSIs before calling
6381  * this function
6382  **/
6383 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6384 {
6385 	u8 tc_map = 0;
6386 	int ret;
6387 	u8 v;
6388 
6389 	/* Enable the TCs available on PF to all VEBs */
6390 	tc_map = i40e_pf_get_tc_map(pf);
6391 	for (v = 0; v < I40E_MAX_VEB; v++) {
6392 		if (!pf->veb[v])
6393 			continue;
6394 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6395 		if (ret) {
6396 			dev_info(&pf->pdev->dev,
6397 				 "Failed configuring TC for VEB seid=%d\n",
6398 				 pf->veb[v]->seid);
6399 			/* Will try to configure as many components */
6400 		}
6401 	}
6402 
6403 	/* Update each VSI */
6404 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6405 		if (!pf->vsi[v])
6406 			continue;
6407 
6408 		/* - Enable all TCs for the LAN VSI
6409 		 * - For all others keep them at TC0 for now
6410 		 */
6411 		if (v == pf->lan_vsi)
6412 			tc_map = i40e_pf_get_tc_map(pf);
6413 		else
6414 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6415 
6416 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6417 		if (ret) {
6418 			dev_info(&pf->pdev->dev,
6419 				 "Failed configuring TC for VSI seid=%d\n",
6420 				 pf->vsi[v]->seid);
6421 			/* Will try to configure as many components */
6422 		} else {
6423 			/* Re-configure VSI vectors based on updated TC map */
6424 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6425 			if (pf->vsi[v]->netdev)
6426 				i40e_dcbnl_set_all(pf->vsi[v]);
6427 		}
6428 	}
6429 }
6430 
6431 /**
6432  * i40e_resume_port_tx - Resume port Tx
6433  * @pf: PF struct
6434  *
6435  * Resume a port's Tx and issue a PF reset in case of failure to
6436  * resume.
6437  **/
6438 static int i40e_resume_port_tx(struct i40e_pf *pf)
6439 {
6440 	struct i40e_hw *hw = &pf->hw;
6441 	int ret;
6442 
6443 	ret = i40e_aq_resume_port_tx(hw, NULL);
6444 	if (ret) {
6445 		dev_info(&pf->pdev->dev,
6446 			 "Resume Port Tx failed, err %s aq_err %s\n",
6447 			  i40e_stat_str(&pf->hw, ret),
6448 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6449 		/* Schedule PF reset to recover */
6450 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6451 		i40e_service_event_schedule(pf);
6452 	}
6453 
6454 	return ret;
6455 }
6456 
6457 /**
6458  * i40e_init_pf_dcb - Initialize DCB configuration
6459  * @pf: PF being configured
6460  *
6461  * Query the current DCB configuration and cache it
6462  * in the hardware structure
6463  **/
6464 static int i40e_init_pf_dcb(struct i40e_pf *pf)
6465 {
6466 	struct i40e_hw *hw = &pf->hw;
6467 	int err = 0;
6468 
6469 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
6470 	 * Also do not enable DCBx if FW LLDP agent is disabled
6471 	 */
6472 	if ((pf->hw_features & I40E_HW_NO_DCB_SUPPORT) ||
6473 	    (pf->flags & I40E_FLAG_DISABLE_FW_LLDP)) {
6474 		dev_info(&pf->pdev->dev, "DCB is not supported or FW LLDP is disabled\n");
6475 		err = I40E_NOT_SUPPORTED;
6476 		goto out;
6477 	}
6478 
6479 	err = i40e_init_dcb(hw, true);
6480 	if (!err) {
6481 		/* Device/Function is not DCBX capable */
6482 		if ((!hw->func_caps.dcb) ||
6483 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6484 			dev_info(&pf->pdev->dev,
6485 				 "DCBX offload is not supported or is disabled for this PF.\n");
6486 		} else {
6487 			/* When status is not DISABLED then DCBX in FW */
6488 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6489 				       DCB_CAP_DCBX_VER_IEEE;
6490 
6491 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
6492 			/* Enable DCB tagging only when more than one TC
6493 			 * or explicitly disable if only one TC
6494 			 */
6495 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6496 				pf->flags |= I40E_FLAG_DCB_ENABLED;
6497 			else
6498 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6499 			dev_dbg(&pf->pdev->dev,
6500 				"DCBX offload is supported for this PF.\n");
6501 		}
6502 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
6503 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
6504 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
6505 	} else {
6506 		dev_info(&pf->pdev->dev,
6507 			 "Query for DCB configuration failed, err %s aq_err %s\n",
6508 			 i40e_stat_str(&pf->hw, err),
6509 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6510 	}
6511 
6512 out:
6513 	return err;
6514 }
6515 #endif /* CONFIG_I40E_DCB */
6516 
6517 /**
6518  * i40e_print_link_message - print link up or down
6519  * @vsi: the VSI for which link needs a message
6520  * @isup: true of link is up, false otherwise
6521  */
6522 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
6523 {
6524 	enum i40e_aq_link_speed new_speed;
6525 	struct i40e_pf *pf = vsi->back;
6526 	char *speed = "Unknown";
6527 	char *fc = "Unknown";
6528 	char *fec = "";
6529 	char *req_fec = "";
6530 	char *an = "";
6531 
6532 	if (isup)
6533 		new_speed = pf->hw.phy.link_info.link_speed;
6534 	else
6535 		new_speed = I40E_LINK_SPEED_UNKNOWN;
6536 
6537 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
6538 		return;
6539 	vsi->current_isup = isup;
6540 	vsi->current_speed = new_speed;
6541 	if (!isup) {
6542 		netdev_info(vsi->netdev, "NIC Link is Down\n");
6543 		return;
6544 	}
6545 
6546 	/* Warn user if link speed on NPAR enabled partition is not at
6547 	 * least 10GB
6548 	 */
6549 	if (pf->hw.func_caps.npar_enable &&
6550 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
6551 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
6552 		netdev_warn(vsi->netdev,
6553 			    "The partition detected link speed that is less than 10Gbps\n");
6554 
6555 	switch (pf->hw.phy.link_info.link_speed) {
6556 	case I40E_LINK_SPEED_40GB:
6557 		speed = "40 G";
6558 		break;
6559 	case I40E_LINK_SPEED_20GB:
6560 		speed = "20 G";
6561 		break;
6562 	case I40E_LINK_SPEED_25GB:
6563 		speed = "25 G";
6564 		break;
6565 	case I40E_LINK_SPEED_10GB:
6566 		speed = "10 G";
6567 		break;
6568 	case I40E_LINK_SPEED_5GB:
6569 		speed = "5 G";
6570 		break;
6571 	case I40E_LINK_SPEED_2_5GB:
6572 		speed = "2.5 G";
6573 		break;
6574 	case I40E_LINK_SPEED_1GB:
6575 		speed = "1000 M";
6576 		break;
6577 	case I40E_LINK_SPEED_100MB:
6578 		speed = "100 M";
6579 		break;
6580 	default:
6581 		break;
6582 	}
6583 
6584 	switch (pf->hw.fc.current_mode) {
6585 	case I40E_FC_FULL:
6586 		fc = "RX/TX";
6587 		break;
6588 	case I40E_FC_TX_PAUSE:
6589 		fc = "TX";
6590 		break;
6591 	case I40E_FC_RX_PAUSE:
6592 		fc = "RX";
6593 		break;
6594 	default:
6595 		fc = "None";
6596 		break;
6597 	}
6598 
6599 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
6600 		req_fec = "None";
6601 		fec = "None";
6602 		an = "False";
6603 
6604 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
6605 			an = "True";
6606 
6607 		if (pf->hw.phy.link_info.fec_info &
6608 		    I40E_AQ_CONFIG_FEC_KR_ENA)
6609 			fec = "CL74 FC-FEC/BASE-R";
6610 		else if (pf->hw.phy.link_info.fec_info &
6611 			 I40E_AQ_CONFIG_FEC_RS_ENA)
6612 			fec = "CL108 RS-FEC";
6613 
6614 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
6615 		 * both RS and FC are requested
6616 		 */
6617 		if (vsi->back->hw.phy.link_info.req_fec_info &
6618 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
6619 			if (vsi->back->hw.phy.link_info.req_fec_info &
6620 			    I40E_AQ_REQUEST_FEC_RS)
6621 				req_fec = "CL108 RS-FEC";
6622 			else
6623 				req_fec = "CL74 FC-FEC/BASE-R";
6624 		}
6625 		netdev_info(vsi->netdev,
6626 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
6627 			    speed, req_fec, fec, an, fc);
6628 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
6629 		req_fec = "None";
6630 		fec = "None";
6631 		an = "False";
6632 
6633 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
6634 			an = "True";
6635 
6636 		if (pf->hw.phy.link_info.fec_info &
6637 		    I40E_AQ_CONFIG_FEC_KR_ENA)
6638 			fec = "CL74 FC-FEC/BASE-R";
6639 
6640 		if (pf->hw.phy.link_info.req_fec_info &
6641 		    I40E_AQ_REQUEST_FEC_KR)
6642 			req_fec = "CL74 FC-FEC/BASE-R";
6643 
6644 		netdev_info(vsi->netdev,
6645 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
6646 			    speed, req_fec, fec, an, fc);
6647 	} else {
6648 		netdev_info(vsi->netdev,
6649 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
6650 			    speed, fc);
6651 	}
6652 
6653 }
6654 
6655 /**
6656  * i40e_up_complete - Finish the last steps of bringing up a connection
6657  * @vsi: the VSI being configured
6658  **/
6659 static int i40e_up_complete(struct i40e_vsi *vsi)
6660 {
6661 	struct i40e_pf *pf = vsi->back;
6662 	int err;
6663 
6664 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6665 		i40e_vsi_configure_msix(vsi);
6666 	else
6667 		i40e_configure_msi_and_legacy(vsi);
6668 
6669 	/* start rings */
6670 	err = i40e_vsi_start_rings(vsi);
6671 	if (err)
6672 		return err;
6673 
6674 	clear_bit(__I40E_VSI_DOWN, vsi->state);
6675 	i40e_napi_enable_all(vsi);
6676 	i40e_vsi_enable_irq(vsi);
6677 
6678 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
6679 	    (vsi->netdev)) {
6680 		i40e_print_link_message(vsi, true);
6681 		netif_tx_start_all_queues(vsi->netdev);
6682 		netif_carrier_on(vsi->netdev);
6683 	}
6684 
6685 	/* replay FDIR SB filters */
6686 	if (vsi->type == I40E_VSI_FDIR) {
6687 		/* reset fd counters */
6688 		pf->fd_add_err = 0;
6689 		pf->fd_atr_cnt = 0;
6690 		i40e_fdir_filter_restore(vsi);
6691 	}
6692 
6693 	/* On the next run of the service_task, notify any clients of the new
6694 	 * opened netdev
6695 	 */
6696 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
6697 	i40e_service_event_schedule(pf);
6698 
6699 	return 0;
6700 }
6701 
6702 /**
6703  * i40e_vsi_reinit_locked - Reset the VSI
6704  * @vsi: the VSI being configured
6705  *
6706  * Rebuild the ring structs after some configuration
6707  * has changed, e.g. MTU size.
6708  **/
6709 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
6710 {
6711 	struct i40e_pf *pf = vsi->back;
6712 
6713 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
6714 		usleep_range(1000, 2000);
6715 	i40e_down(vsi);
6716 
6717 	i40e_up(vsi);
6718 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
6719 }
6720 
6721 /**
6722  * i40e_force_link_state - Force the link status
6723  * @pf: board private structure
6724  * @is_up: whether the link state should be forced up or down
6725  **/
6726 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
6727 {
6728 	struct i40e_aq_get_phy_abilities_resp abilities;
6729 	struct i40e_aq_set_phy_config config = {0};
6730 	bool non_zero_phy_type = is_up;
6731 	struct i40e_hw *hw = &pf->hw;
6732 	i40e_status err;
6733 	u64 mask;
6734 	u8 speed;
6735 
6736 	/* Card might've been put in an unstable state by other drivers
6737 	 * and applications, which causes incorrect speed values being
6738 	 * set on startup. In order to clear speed registers, we call
6739 	 * get_phy_capabilities twice, once to get initial state of
6740 	 * available speeds, and once to get current PHY config.
6741 	 */
6742 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
6743 					   NULL);
6744 	if (err) {
6745 		dev_err(&pf->pdev->dev,
6746 			"failed to get phy cap., ret =  %s last_status =  %s\n",
6747 			i40e_stat_str(hw, err),
6748 			i40e_aq_str(hw, hw->aq.asq_last_status));
6749 		return err;
6750 	}
6751 	speed = abilities.link_speed;
6752 
6753 	/* Get the current phy config */
6754 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
6755 					   NULL);
6756 	if (err) {
6757 		dev_err(&pf->pdev->dev,
6758 			"failed to get phy cap., ret =  %s last_status =  %s\n",
6759 			i40e_stat_str(hw, err),
6760 			i40e_aq_str(hw, hw->aq.asq_last_status));
6761 		return err;
6762 	}
6763 
6764 	/* If link needs to go up, but was not forced to go down,
6765 	 * and its speed values are OK, no need for a flap
6766 	 * if non_zero_phy_type was set, still need to force up
6767 	 */
6768 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
6769 		non_zero_phy_type = true;
6770 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
6771 		return I40E_SUCCESS;
6772 
6773 	/* To force link we need to set bits for all supported PHY types,
6774 	 * but there are now more than 32, so we need to split the bitmap
6775 	 * across two fields.
6776 	 */
6777 	mask = I40E_PHY_TYPES_BITMASK;
6778 	config.phy_type =
6779 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
6780 	config.phy_type_ext =
6781 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
6782 	/* Copy the old settings, except of phy_type */
6783 	config.abilities = abilities.abilities;
6784 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
6785 		if (is_up)
6786 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
6787 		else
6788 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
6789 	}
6790 	if (abilities.link_speed != 0)
6791 		config.link_speed = abilities.link_speed;
6792 	else
6793 		config.link_speed = speed;
6794 	config.eee_capability = abilities.eee_capability;
6795 	config.eeer = abilities.eeer_val;
6796 	config.low_power_ctrl = abilities.d3_lpan;
6797 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
6798 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
6799 	err = i40e_aq_set_phy_config(hw, &config, NULL);
6800 
6801 	if (err) {
6802 		dev_err(&pf->pdev->dev,
6803 			"set phy config ret =  %s last_status =  %s\n",
6804 			i40e_stat_str(&pf->hw, err),
6805 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6806 		return err;
6807 	}
6808 
6809 	/* Update the link info */
6810 	err = i40e_update_link_info(hw);
6811 	if (err) {
6812 		/* Wait a little bit (on 40G cards it sometimes takes a really
6813 		 * long time for link to come back from the atomic reset)
6814 		 * and try once more
6815 		 */
6816 		msleep(1000);
6817 		i40e_update_link_info(hw);
6818 	}
6819 
6820 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
6821 
6822 	return I40E_SUCCESS;
6823 }
6824 
6825 /**
6826  * i40e_up - Bring the connection back up after being down
6827  * @vsi: the VSI being configured
6828  **/
6829 int i40e_up(struct i40e_vsi *vsi)
6830 {
6831 	int err;
6832 
6833 	if (vsi->type == I40E_VSI_MAIN &&
6834 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
6835 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
6836 		i40e_force_link_state(vsi->back, true);
6837 
6838 	err = i40e_vsi_configure(vsi);
6839 	if (!err)
6840 		err = i40e_up_complete(vsi);
6841 
6842 	return err;
6843 }
6844 
6845 /**
6846  * i40e_down - Shutdown the connection processing
6847  * @vsi: the VSI being stopped
6848  **/
6849 void i40e_down(struct i40e_vsi *vsi)
6850 {
6851 	int i;
6852 
6853 	/* It is assumed that the caller of this function
6854 	 * sets the vsi->state __I40E_VSI_DOWN bit.
6855 	 */
6856 	if (vsi->netdev) {
6857 		netif_carrier_off(vsi->netdev);
6858 		netif_tx_disable(vsi->netdev);
6859 	}
6860 	i40e_vsi_disable_irq(vsi);
6861 	i40e_vsi_stop_rings(vsi);
6862 	if (vsi->type == I40E_VSI_MAIN &&
6863 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
6864 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
6865 		i40e_force_link_state(vsi->back, false);
6866 	i40e_napi_disable_all(vsi);
6867 
6868 	for (i = 0; i < vsi->num_queue_pairs; i++) {
6869 		i40e_clean_tx_ring(vsi->tx_rings[i]);
6870 		if (i40e_enabled_xdp_vsi(vsi)) {
6871 			/* Make sure that in-progress ndo_xdp_xmit and
6872 			 * ndo_xsk_wakeup calls are completed.
6873 			 */
6874 			synchronize_rcu();
6875 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
6876 		}
6877 		i40e_clean_rx_ring(vsi->rx_rings[i]);
6878 	}
6879 
6880 }
6881 
6882 /**
6883  * i40e_validate_mqprio_qopt- validate queue mapping info
6884  * @vsi: the VSI being configured
6885  * @mqprio_qopt: queue parametrs
6886  **/
6887 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
6888 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
6889 {
6890 	u64 sum_max_rate = 0;
6891 	u64 max_rate = 0;
6892 	int i;
6893 
6894 	if (mqprio_qopt->qopt.offset[0] != 0 ||
6895 	    mqprio_qopt->qopt.num_tc < 1 ||
6896 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
6897 		return -EINVAL;
6898 	for (i = 0; ; i++) {
6899 		if (!mqprio_qopt->qopt.count[i])
6900 			return -EINVAL;
6901 		if (mqprio_qopt->min_rate[i]) {
6902 			dev_err(&vsi->back->pdev->dev,
6903 				"Invalid min tx rate (greater than 0) specified\n");
6904 			return -EINVAL;
6905 		}
6906 		max_rate = mqprio_qopt->max_rate[i];
6907 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6908 		sum_max_rate += max_rate;
6909 
6910 		if (i >= mqprio_qopt->qopt.num_tc - 1)
6911 			break;
6912 		if (mqprio_qopt->qopt.offset[i + 1] !=
6913 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
6914 			return -EINVAL;
6915 	}
6916 	if (vsi->num_queue_pairs <
6917 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
6918 		return -EINVAL;
6919 	}
6920 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
6921 		dev_err(&vsi->back->pdev->dev,
6922 			"Invalid max tx rate specified\n");
6923 		return -EINVAL;
6924 	}
6925 	return 0;
6926 }
6927 
6928 /**
6929  * i40e_vsi_set_default_tc_config - set default values for tc configuration
6930  * @vsi: the VSI being configured
6931  **/
6932 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
6933 {
6934 	u16 qcount;
6935 	int i;
6936 
6937 	/* Only TC0 is enabled */
6938 	vsi->tc_config.numtc = 1;
6939 	vsi->tc_config.enabled_tc = 1;
6940 	qcount = min_t(int, vsi->alloc_queue_pairs,
6941 		       i40e_pf_get_max_q_per_tc(vsi->back));
6942 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6943 		/* For the TC that is not enabled set the offset to to default
6944 		 * queue and allocate one queue for the given TC.
6945 		 */
6946 		vsi->tc_config.tc_info[i].qoffset = 0;
6947 		if (i == 0)
6948 			vsi->tc_config.tc_info[i].qcount = qcount;
6949 		else
6950 			vsi->tc_config.tc_info[i].qcount = 1;
6951 		vsi->tc_config.tc_info[i].netdev_tc = 0;
6952 	}
6953 }
6954 
6955 /**
6956  * i40e_del_macvlan_filter
6957  * @hw: pointer to the HW structure
6958  * @seid: seid of the channel VSI
6959  * @macaddr: the mac address to apply as a filter
6960  * @aq_err: store the admin Q error
6961  *
6962  * This function deletes a mac filter on the channel VSI which serves as the
6963  * macvlan. Returns 0 on success.
6964  **/
6965 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
6966 					   const u8 *macaddr, int *aq_err)
6967 {
6968 	struct i40e_aqc_remove_macvlan_element_data element;
6969 	i40e_status status;
6970 
6971 	memset(&element, 0, sizeof(element));
6972 	ether_addr_copy(element.mac_addr, macaddr);
6973 	element.vlan_tag = 0;
6974 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
6975 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
6976 	*aq_err = hw->aq.asq_last_status;
6977 
6978 	return status;
6979 }
6980 
6981 /**
6982  * i40e_add_macvlan_filter
6983  * @hw: pointer to the HW structure
6984  * @seid: seid of the channel VSI
6985  * @macaddr: the mac address to apply as a filter
6986  * @aq_err: store the admin Q error
6987  *
6988  * This function adds a mac filter on the channel VSI which serves as the
6989  * macvlan. Returns 0 on success.
6990  **/
6991 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
6992 					   const u8 *macaddr, int *aq_err)
6993 {
6994 	struct i40e_aqc_add_macvlan_element_data element;
6995 	i40e_status status;
6996 	u16 cmd_flags = 0;
6997 
6998 	ether_addr_copy(element.mac_addr, macaddr);
6999 	element.vlan_tag = 0;
7000 	element.queue_number = 0;
7001 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7002 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7003 	element.flags = cpu_to_le16(cmd_flags);
7004 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7005 	*aq_err = hw->aq.asq_last_status;
7006 
7007 	return status;
7008 }
7009 
7010 /**
7011  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7012  * @vsi: the VSI we want to access
7013  * @ch: the channel we want to access
7014  */
7015 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7016 {
7017 	struct i40e_ring *tx_ring, *rx_ring;
7018 	u16 pf_q;
7019 	int i;
7020 
7021 	for (i = 0; i < ch->num_queue_pairs; i++) {
7022 		pf_q = ch->base_queue + i;
7023 		tx_ring = vsi->tx_rings[pf_q];
7024 		tx_ring->ch = NULL;
7025 		rx_ring = vsi->rx_rings[pf_q];
7026 		rx_ring->ch = NULL;
7027 	}
7028 }
7029 
7030 /**
7031  * i40e_free_macvlan_channels
7032  * @vsi: the VSI we want to access
7033  *
7034  * This function frees the Qs of the channel VSI from
7035  * the stack and also deletes the channel VSIs which
7036  * serve as macvlans.
7037  */
7038 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7039 {
7040 	struct i40e_channel *ch, *ch_tmp;
7041 	int ret;
7042 
7043 	if (list_empty(&vsi->macvlan_list))
7044 		return;
7045 
7046 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7047 		struct i40e_vsi *parent_vsi;
7048 
7049 		if (i40e_is_channel_macvlan(ch)) {
7050 			i40e_reset_ch_rings(vsi, ch);
7051 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7052 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7053 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7054 			kfree(ch->fwd);
7055 			ch->fwd = NULL;
7056 		}
7057 
7058 		list_del(&ch->list);
7059 		parent_vsi = ch->parent_vsi;
7060 		if (!parent_vsi || !ch->initialized) {
7061 			kfree(ch);
7062 			continue;
7063 		}
7064 
7065 		/* remove the VSI */
7066 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7067 					     NULL);
7068 		if (ret)
7069 			dev_err(&vsi->back->pdev->dev,
7070 				"unable to remove channel (%d) for parent VSI(%d)\n",
7071 				ch->seid, parent_vsi->seid);
7072 		kfree(ch);
7073 	}
7074 	vsi->macvlan_cnt = 0;
7075 }
7076 
7077 /**
7078  * i40e_fwd_ring_up - bring the macvlan device up
7079  * @vsi: the VSI we want to access
7080  * @vdev: macvlan netdevice
7081  * @fwd: the private fwd structure
7082  */
7083 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7084 			    struct i40e_fwd_adapter *fwd)
7085 {
7086 	int ret = 0, num_tc = 1,  i, aq_err;
7087 	struct i40e_channel *ch, *ch_tmp;
7088 	struct i40e_pf *pf = vsi->back;
7089 	struct i40e_hw *hw = &pf->hw;
7090 
7091 	if (list_empty(&vsi->macvlan_list))
7092 		return -EINVAL;
7093 
7094 	/* Go through the list and find an available channel */
7095 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7096 		if (!i40e_is_channel_macvlan(ch)) {
7097 			ch->fwd = fwd;
7098 			/* record configuration for macvlan interface in vdev */
7099 			for (i = 0; i < num_tc; i++)
7100 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7101 							     i,
7102 							     ch->num_queue_pairs,
7103 							     ch->base_queue);
7104 			for (i = 0; i < ch->num_queue_pairs; i++) {
7105 				struct i40e_ring *tx_ring, *rx_ring;
7106 				u16 pf_q;
7107 
7108 				pf_q = ch->base_queue + i;
7109 
7110 				/* Get to TX ring ptr */
7111 				tx_ring = vsi->tx_rings[pf_q];
7112 				tx_ring->ch = ch;
7113 
7114 				/* Get the RX ring ptr */
7115 				rx_ring = vsi->rx_rings[pf_q];
7116 				rx_ring->ch = ch;
7117 			}
7118 			break;
7119 		}
7120 	}
7121 
7122 	/* Guarantee all rings are updated before we update the
7123 	 * MAC address filter.
7124 	 */
7125 	wmb();
7126 
7127 	/* Add a mac filter */
7128 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7129 	if (ret) {
7130 		/* if we cannot add the MAC rule then disable the offload */
7131 		macvlan_release_l2fw_offload(vdev);
7132 		for (i = 0; i < ch->num_queue_pairs; i++) {
7133 			struct i40e_ring *rx_ring;
7134 			u16 pf_q;
7135 
7136 			pf_q = ch->base_queue + i;
7137 			rx_ring = vsi->rx_rings[pf_q];
7138 			rx_ring->netdev = NULL;
7139 		}
7140 		dev_info(&pf->pdev->dev,
7141 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7142 			  i40e_stat_str(hw, ret),
7143 			  i40e_aq_str(hw, aq_err));
7144 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7145 	}
7146 
7147 	return ret;
7148 }
7149 
7150 /**
7151  * i40e_setup_macvlans - create the channels which will be macvlans
7152  * @vsi: the VSI we want to access
7153  * @macvlan_cnt: no. of macvlans to be setup
7154  * @qcnt: no. of Qs per macvlan
7155  * @vdev: macvlan netdevice
7156  */
7157 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7158 			       struct net_device *vdev)
7159 {
7160 	struct i40e_pf *pf = vsi->back;
7161 	struct i40e_hw *hw = &pf->hw;
7162 	struct i40e_vsi_context ctxt;
7163 	u16 sections, qmap, num_qps;
7164 	struct i40e_channel *ch;
7165 	int i, pow, ret = 0;
7166 	u8 offset = 0;
7167 
7168 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7169 		return -EINVAL;
7170 
7171 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7172 
7173 	/* find the next higher power-of-2 of num queue pairs */
7174 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7175 
7176 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7177 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7178 
7179 	/* Setup context bits for the main VSI */
7180 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7181 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7182 	memset(&ctxt, 0, sizeof(ctxt));
7183 	ctxt.seid = vsi->seid;
7184 	ctxt.pf_num = vsi->back->hw.pf_id;
7185 	ctxt.vf_num = 0;
7186 	ctxt.uplink_seid = vsi->uplink_seid;
7187 	ctxt.info = vsi->info;
7188 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7189 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7190 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7191 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7192 
7193 	/* Reconfigure RSS for main VSI with new max queue count */
7194 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7195 	ret = i40e_vsi_config_rss(vsi);
7196 	if (ret) {
7197 		dev_info(&pf->pdev->dev,
7198 			 "Failed to reconfig RSS for num_queues (%u)\n",
7199 			 vsi->rss_size);
7200 		return ret;
7201 	}
7202 	vsi->reconfig_rss = true;
7203 	dev_dbg(&vsi->back->pdev->dev,
7204 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7205 	vsi->next_base_queue = num_qps;
7206 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7207 
7208 	/* Update the VSI after updating the VSI queue-mapping
7209 	 * information
7210 	 */
7211 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7212 	if (ret) {
7213 		dev_info(&pf->pdev->dev,
7214 			 "Update vsi tc config failed, err %s aq_err %s\n",
7215 			 i40e_stat_str(hw, ret),
7216 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7217 		return ret;
7218 	}
7219 	/* update the local VSI info with updated queue map */
7220 	i40e_vsi_update_queue_map(vsi, &ctxt);
7221 	vsi->info.valid_sections = 0;
7222 
7223 	/* Create channels for macvlans */
7224 	INIT_LIST_HEAD(&vsi->macvlan_list);
7225 	for (i = 0; i < macvlan_cnt; i++) {
7226 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7227 		if (!ch) {
7228 			ret = -ENOMEM;
7229 			goto err_free;
7230 		}
7231 		INIT_LIST_HEAD(&ch->list);
7232 		ch->num_queue_pairs = qcnt;
7233 		if (!i40e_setup_channel(pf, vsi, ch)) {
7234 			ret = -EINVAL;
7235 			kfree(ch);
7236 			goto err_free;
7237 		}
7238 		ch->parent_vsi = vsi;
7239 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7240 		vsi->macvlan_cnt++;
7241 		list_add_tail(&ch->list, &vsi->macvlan_list);
7242 	}
7243 
7244 	return ret;
7245 
7246 err_free:
7247 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7248 	i40e_free_macvlan_channels(vsi);
7249 
7250 	return ret;
7251 }
7252 
7253 /**
7254  * i40e_fwd_add - configure macvlans
7255  * @netdev: net device to configure
7256  * @vdev: macvlan netdevice
7257  **/
7258 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7259 {
7260 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7261 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7262 	struct i40e_vsi *vsi = np->vsi;
7263 	struct i40e_pf *pf = vsi->back;
7264 	struct i40e_fwd_adapter *fwd;
7265 	int avail_macvlan, ret;
7266 
7267 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7268 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7269 		return ERR_PTR(-EINVAL);
7270 	}
7271 	if ((pf->flags & I40E_FLAG_TC_MQPRIO)) {
7272 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7273 		return ERR_PTR(-EINVAL);
7274 	}
7275 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7276 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7277 		return ERR_PTR(-EINVAL);
7278 	}
7279 
7280 	/* The macvlan device has to be a single Q device so that the
7281 	 * tc_to_txq field can be reused to pick the tx queue.
7282 	 */
7283 	if (netif_is_multiqueue(vdev))
7284 		return ERR_PTR(-ERANGE);
7285 
7286 	if (!vsi->macvlan_cnt) {
7287 		/* reserve bit 0 for the pf device */
7288 		set_bit(0, vsi->fwd_bitmask);
7289 
7290 		/* Try to reserve as many queues as possible for macvlans. First
7291 		 * reserve 3/4th of max vectors, then half, then quarter and
7292 		 * calculate Qs per macvlan as you go
7293 		 */
7294 		vectors = pf->num_lan_msix;
7295 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7296 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7297 			q_per_macvlan = 4;
7298 			macvlan_cnt = (vectors - 32) / 4;
7299 		} else if (vectors <= 64 && vectors > 32) {
7300 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7301 			q_per_macvlan = 2;
7302 			macvlan_cnt = (vectors - 16) / 2;
7303 		} else if (vectors <= 32 && vectors > 16) {
7304 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7305 			q_per_macvlan = 1;
7306 			macvlan_cnt = vectors - 16;
7307 		} else if (vectors <= 16 && vectors > 8) {
7308 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7309 			q_per_macvlan = 1;
7310 			macvlan_cnt = vectors - 8;
7311 		} else {
7312 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7313 			q_per_macvlan = 1;
7314 			macvlan_cnt = vectors - 1;
7315 		}
7316 
7317 		if (macvlan_cnt == 0)
7318 			return ERR_PTR(-EBUSY);
7319 
7320 		/* Quiesce VSI queues */
7321 		i40e_quiesce_vsi(vsi);
7322 
7323 		/* sets up the macvlans but does not "enable" them */
7324 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
7325 					  vdev);
7326 		if (ret)
7327 			return ERR_PTR(ret);
7328 
7329 		/* Unquiesce VSI */
7330 		i40e_unquiesce_vsi(vsi);
7331 	}
7332 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
7333 					    vsi->macvlan_cnt);
7334 	if (avail_macvlan >= I40E_MAX_MACVLANS)
7335 		return ERR_PTR(-EBUSY);
7336 
7337 	/* create the fwd struct */
7338 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
7339 	if (!fwd)
7340 		return ERR_PTR(-ENOMEM);
7341 
7342 	set_bit(avail_macvlan, vsi->fwd_bitmask);
7343 	fwd->bit_no = avail_macvlan;
7344 	netdev_set_sb_channel(vdev, avail_macvlan);
7345 	fwd->netdev = vdev;
7346 
7347 	if (!netif_running(netdev))
7348 		return fwd;
7349 
7350 	/* Set fwd ring up */
7351 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
7352 	if (ret) {
7353 		/* unbind the queues and drop the subordinate channel config */
7354 		netdev_unbind_sb_channel(netdev, vdev);
7355 		netdev_set_sb_channel(vdev, 0);
7356 
7357 		kfree(fwd);
7358 		return ERR_PTR(-EINVAL);
7359 	}
7360 
7361 	return fwd;
7362 }
7363 
7364 /**
7365  * i40e_del_all_macvlans - Delete all the mac filters on the channels
7366  * @vsi: the VSI we want to access
7367  */
7368 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
7369 {
7370 	struct i40e_channel *ch, *ch_tmp;
7371 	struct i40e_pf *pf = vsi->back;
7372 	struct i40e_hw *hw = &pf->hw;
7373 	int aq_err, ret = 0;
7374 
7375 	if (list_empty(&vsi->macvlan_list))
7376 		return;
7377 
7378 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7379 		if (i40e_is_channel_macvlan(ch)) {
7380 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7381 						      i40e_channel_mac(ch),
7382 						      &aq_err);
7383 			if (!ret) {
7384 				/* Reset queue contexts */
7385 				i40e_reset_ch_rings(vsi, ch);
7386 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7387 				netdev_unbind_sb_channel(vsi->netdev,
7388 							 ch->fwd->netdev);
7389 				netdev_set_sb_channel(ch->fwd->netdev, 0);
7390 				kfree(ch->fwd);
7391 				ch->fwd = NULL;
7392 			}
7393 		}
7394 	}
7395 }
7396 
7397 /**
7398  * i40e_fwd_del - delete macvlan interfaces
7399  * @netdev: net device to configure
7400  * @vdev: macvlan netdevice
7401  */
7402 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
7403 {
7404 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7405 	struct i40e_fwd_adapter *fwd = vdev;
7406 	struct i40e_channel *ch, *ch_tmp;
7407 	struct i40e_vsi *vsi = np->vsi;
7408 	struct i40e_pf *pf = vsi->back;
7409 	struct i40e_hw *hw = &pf->hw;
7410 	int aq_err, ret = 0;
7411 
7412 	/* Find the channel associated with the macvlan and del mac filter */
7413 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7414 		if (i40e_is_channel_macvlan(ch) &&
7415 		    ether_addr_equal(i40e_channel_mac(ch),
7416 				     fwd->netdev->dev_addr)) {
7417 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7418 						      i40e_channel_mac(ch),
7419 						      &aq_err);
7420 			if (!ret) {
7421 				/* Reset queue contexts */
7422 				i40e_reset_ch_rings(vsi, ch);
7423 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7424 				netdev_unbind_sb_channel(netdev, fwd->netdev);
7425 				netdev_set_sb_channel(fwd->netdev, 0);
7426 				kfree(ch->fwd);
7427 				ch->fwd = NULL;
7428 			} else {
7429 				dev_info(&pf->pdev->dev,
7430 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
7431 					  i40e_stat_str(hw, ret),
7432 					  i40e_aq_str(hw, aq_err));
7433 			}
7434 			break;
7435 		}
7436 	}
7437 }
7438 
7439 /**
7440  * i40e_setup_tc - configure multiple traffic classes
7441  * @netdev: net device to configure
7442  * @type_data: tc offload data
7443  **/
7444 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
7445 {
7446 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
7447 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7448 	struct i40e_vsi *vsi = np->vsi;
7449 	struct i40e_pf *pf = vsi->back;
7450 	u8 enabled_tc = 0, num_tc, hw;
7451 	bool need_reset = false;
7452 	int old_queue_pairs;
7453 	int ret = -EINVAL;
7454 	u16 mode;
7455 	int i;
7456 
7457 	old_queue_pairs = vsi->num_queue_pairs;
7458 	num_tc = mqprio_qopt->qopt.num_tc;
7459 	hw = mqprio_qopt->qopt.hw;
7460 	mode = mqprio_qopt->mode;
7461 	if (!hw) {
7462 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7463 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
7464 		goto config_tc;
7465 	}
7466 
7467 	/* Check if MFP enabled */
7468 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
7469 		netdev_info(netdev,
7470 			    "Configuring TC not supported in MFP mode\n");
7471 		return ret;
7472 	}
7473 	switch (mode) {
7474 	case TC_MQPRIO_MODE_DCB:
7475 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7476 
7477 		/* Check if DCB enabled to continue */
7478 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7479 			netdev_info(netdev,
7480 				    "DCB is not enabled for adapter\n");
7481 			return ret;
7482 		}
7483 
7484 		/* Check whether tc count is within enabled limit */
7485 		if (num_tc > i40e_pf_get_num_tc(pf)) {
7486 			netdev_info(netdev,
7487 				    "TC count greater than enabled on link for adapter\n");
7488 			return ret;
7489 		}
7490 		break;
7491 	case TC_MQPRIO_MODE_CHANNEL:
7492 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
7493 			netdev_info(netdev,
7494 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
7495 			return ret;
7496 		}
7497 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7498 			return ret;
7499 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
7500 		if (ret)
7501 			return ret;
7502 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
7503 		       sizeof(*mqprio_qopt));
7504 		pf->flags |= I40E_FLAG_TC_MQPRIO;
7505 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7506 		break;
7507 	default:
7508 		return -EINVAL;
7509 	}
7510 
7511 config_tc:
7512 	/* Generate TC map for number of tc requested */
7513 	for (i = 0; i < num_tc; i++)
7514 		enabled_tc |= BIT(i);
7515 
7516 	/* Requesting same TC configuration as already enabled */
7517 	if (enabled_tc == vsi->tc_config.enabled_tc &&
7518 	    mode != TC_MQPRIO_MODE_CHANNEL)
7519 		return 0;
7520 
7521 	/* Quiesce VSI queues */
7522 	i40e_quiesce_vsi(vsi);
7523 
7524 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
7525 		i40e_remove_queue_channels(vsi);
7526 
7527 	/* Configure VSI for enabled TCs */
7528 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
7529 	if (ret) {
7530 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
7531 			    vsi->seid);
7532 		need_reset = true;
7533 		goto exit;
7534 	} else {
7535 		dev_info(&vsi->back->pdev->dev,
7536 			 "Setup channel (id:%u) utilizing num_queues %d\n",
7537 			 vsi->seid, vsi->tc_config.tc_info[0].qcount);
7538 	}
7539 
7540 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
7541 		if (vsi->mqprio_qopt.max_rate[0]) {
7542 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
7543 
7544 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
7545 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
7546 			if (!ret) {
7547 				u64 credits = max_tx_rate;
7548 
7549 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
7550 				dev_dbg(&vsi->back->pdev->dev,
7551 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
7552 					max_tx_rate,
7553 					credits,
7554 					vsi->seid);
7555 			} else {
7556 				need_reset = true;
7557 				goto exit;
7558 			}
7559 		}
7560 		ret = i40e_configure_queue_channels(vsi);
7561 		if (ret) {
7562 			vsi->num_queue_pairs = old_queue_pairs;
7563 			netdev_info(netdev,
7564 				    "Failed configuring queue channels\n");
7565 			need_reset = true;
7566 			goto exit;
7567 		}
7568 	}
7569 
7570 exit:
7571 	/* Reset the configuration data to defaults, only TC0 is enabled */
7572 	if (need_reset) {
7573 		i40e_vsi_set_default_tc_config(vsi);
7574 		need_reset = false;
7575 	}
7576 
7577 	/* Unquiesce VSI */
7578 	i40e_unquiesce_vsi(vsi);
7579 	return ret;
7580 }
7581 
7582 /**
7583  * i40e_set_cld_element - sets cloud filter element data
7584  * @filter: cloud filter rule
7585  * @cld: ptr to cloud filter element data
7586  *
7587  * This is helper function to copy data into cloud filter element
7588  **/
7589 static inline void
7590 i40e_set_cld_element(struct i40e_cloud_filter *filter,
7591 		     struct i40e_aqc_cloud_filters_element_data *cld)
7592 {
7593 	int i, j;
7594 	u32 ipa;
7595 
7596 	memset(cld, 0, sizeof(*cld));
7597 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
7598 	ether_addr_copy(cld->inner_mac, filter->src_mac);
7599 
7600 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
7601 		return;
7602 
7603 	if (filter->n_proto == ETH_P_IPV6) {
7604 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
7605 		for (i = 0, j = 0; i < ARRAY_SIZE(filter->dst_ipv6);
7606 		     i++, j += 2) {
7607 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
7608 			ipa = cpu_to_le32(ipa);
7609 			memcpy(&cld->ipaddr.raw_v6.data[j], &ipa, sizeof(ipa));
7610 		}
7611 	} else {
7612 		ipa = be32_to_cpu(filter->dst_ipv4);
7613 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
7614 	}
7615 
7616 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
7617 
7618 	/* tenant_id is not supported by FW now, once the support is enabled
7619 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
7620 	 */
7621 	if (filter->tenant_id)
7622 		return;
7623 }
7624 
7625 /**
7626  * i40e_add_del_cloud_filter - Add/del cloud filter
7627  * @vsi: pointer to VSI
7628  * @filter: cloud filter rule
7629  * @add: if true, add, if false, delete
7630  *
7631  * Add or delete a cloud filter for a specific flow spec.
7632  * Returns 0 if the filter were successfully added.
7633  **/
7634 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
7635 			      struct i40e_cloud_filter *filter, bool add)
7636 {
7637 	struct i40e_aqc_cloud_filters_element_data cld_filter;
7638 	struct i40e_pf *pf = vsi->back;
7639 	int ret;
7640 	static const u16 flag_table[128] = {
7641 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
7642 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
7643 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
7644 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
7645 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
7646 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
7647 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
7648 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
7649 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
7650 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
7651 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
7652 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
7653 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
7654 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
7655 	};
7656 
7657 	if (filter->flags >= ARRAY_SIZE(flag_table))
7658 		return I40E_ERR_CONFIG;
7659 
7660 	/* copy element needed to add cloud filter from filter */
7661 	i40e_set_cld_element(filter, &cld_filter);
7662 
7663 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
7664 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
7665 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
7666 
7667 	if (filter->n_proto == ETH_P_IPV6)
7668 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
7669 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
7670 	else
7671 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
7672 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
7673 
7674 	if (add)
7675 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
7676 						&cld_filter, 1);
7677 	else
7678 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
7679 						&cld_filter, 1);
7680 	if (ret)
7681 		dev_dbg(&pf->pdev->dev,
7682 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
7683 			add ? "add" : "delete", filter->dst_port, ret,
7684 			pf->hw.aq.asq_last_status);
7685 	else
7686 		dev_info(&pf->pdev->dev,
7687 			 "%s cloud filter for VSI: %d\n",
7688 			 add ? "Added" : "Deleted", filter->seid);
7689 	return ret;
7690 }
7691 
7692 /**
7693  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
7694  * @vsi: pointer to VSI
7695  * @filter: cloud filter rule
7696  * @add: if true, add, if false, delete
7697  *
7698  * Add or delete a cloud filter for a specific flow spec using big buffer.
7699  * Returns 0 if the filter were successfully added.
7700  **/
7701 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
7702 				      struct i40e_cloud_filter *filter,
7703 				      bool add)
7704 {
7705 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
7706 	struct i40e_pf *pf = vsi->back;
7707 	int ret;
7708 
7709 	/* Both (src/dst) valid mac_addr are not supported */
7710 	if ((is_valid_ether_addr(filter->dst_mac) &&
7711 	     is_valid_ether_addr(filter->src_mac)) ||
7712 	    (is_multicast_ether_addr(filter->dst_mac) &&
7713 	     is_multicast_ether_addr(filter->src_mac)))
7714 		return -EOPNOTSUPP;
7715 
7716 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
7717 	 * ports are not supported via big buffer now.
7718 	 */
7719 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
7720 		return -EOPNOTSUPP;
7721 
7722 	/* adding filter using src_port/src_ip is not supported at this stage */
7723 	if (filter->src_port || filter->src_ipv4 ||
7724 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
7725 		return -EOPNOTSUPP;
7726 
7727 	/* copy element needed to add cloud filter from filter */
7728 	i40e_set_cld_element(filter, &cld_filter.element);
7729 
7730 	if (is_valid_ether_addr(filter->dst_mac) ||
7731 	    is_valid_ether_addr(filter->src_mac) ||
7732 	    is_multicast_ether_addr(filter->dst_mac) ||
7733 	    is_multicast_ether_addr(filter->src_mac)) {
7734 		/* MAC + IP : unsupported mode */
7735 		if (filter->dst_ipv4)
7736 			return -EOPNOTSUPP;
7737 
7738 		/* since we validated that L4 port must be valid before
7739 		 * we get here, start with respective "flags" value
7740 		 * and update if vlan is present or not
7741 		 */
7742 		cld_filter.element.flags =
7743 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
7744 
7745 		if (filter->vlan_id) {
7746 			cld_filter.element.flags =
7747 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
7748 		}
7749 
7750 	} else if (filter->dst_ipv4 ||
7751 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
7752 		cld_filter.element.flags =
7753 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
7754 		if (filter->n_proto == ETH_P_IPV6)
7755 			cld_filter.element.flags |=
7756 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
7757 		else
7758 			cld_filter.element.flags |=
7759 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
7760 	} else {
7761 		dev_err(&pf->pdev->dev,
7762 			"either mac or ip has to be valid for cloud filter\n");
7763 		return -EINVAL;
7764 	}
7765 
7766 	/* Now copy L4 port in Byte 6..7 in general fields */
7767 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
7768 						be16_to_cpu(filter->dst_port);
7769 
7770 	if (add) {
7771 		/* Validate current device switch mode, change if necessary */
7772 		ret = i40e_validate_and_set_switch_mode(vsi);
7773 		if (ret) {
7774 			dev_err(&pf->pdev->dev,
7775 				"failed to set switch mode, ret %d\n",
7776 				ret);
7777 			return ret;
7778 		}
7779 
7780 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
7781 						   &cld_filter, 1);
7782 	} else {
7783 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
7784 						   &cld_filter, 1);
7785 	}
7786 
7787 	if (ret)
7788 		dev_dbg(&pf->pdev->dev,
7789 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
7790 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
7791 	else
7792 		dev_info(&pf->pdev->dev,
7793 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
7794 			 add ? "add" : "delete", filter->seid,
7795 			 ntohs(filter->dst_port));
7796 	return ret;
7797 }
7798 
7799 /**
7800  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
7801  * @vsi: Pointer to VSI
7802  * @f: Pointer to struct flow_cls_offload
7803  * @filter: Pointer to cloud filter structure
7804  *
7805  **/
7806 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
7807 				 struct flow_cls_offload *f,
7808 				 struct i40e_cloud_filter *filter)
7809 {
7810 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
7811 	struct flow_dissector *dissector = rule->match.dissector;
7812 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
7813 	struct i40e_pf *pf = vsi->back;
7814 	u8 field_flags = 0;
7815 
7816 	if (dissector->used_keys &
7817 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
7818 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
7819 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
7820 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
7821 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
7822 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
7823 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
7824 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
7825 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
7826 			dissector->used_keys);
7827 		return -EOPNOTSUPP;
7828 	}
7829 
7830 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
7831 		struct flow_match_enc_keyid match;
7832 
7833 		flow_rule_match_enc_keyid(rule, &match);
7834 		if (match.mask->keyid != 0)
7835 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
7836 
7837 		filter->tenant_id = be32_to_cpu(match.key->keyid);
7838 	}
7839 
7840 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
7841 		struct flow_match_basic match;
7842 
7843 		flow_rule_match_basic(rule, &match);
7844 		n_proto_key = ntohs(match.key->n_proto);
7845 		n_proto_mask = ntohs(match.mask->n_proto);
7846 
7847 		if (n_proto_key == ETH_P_ALL) {
7848 			n_proto_key = 0;
7849 			n_proto_mask = 0;
7850 		}
7851 		filter->n_proto = n_proto_key & n_proto_mask;
7852 		filter->ip_proto = match.key->ip_proto;
7853 	}
7854 
7855 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
7856 		struct flow_match_eth_addrs match;
7857 
7858 		flow_rule_match_eth_addrs(rule, &match);
7859 
7860 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
7861 		if (!is_zero_ether_addr(match.mask->dst)) {
7862 			if (is_broadcast_ether_addr(match.mask->dst)) {
7863 				field_flags |= I40E_CLOUD_FIELD_OMAC;
7864 			} else {
7865 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
7866 					match.mask->dst);
7867 				return I40E_ERR_CONFIG;
7868 			}
7869 		}
7870 
7871 		if (!is_zero_ether_addr(match.mask->src)) {
7872 			if (is_broadcast_ether_addr(match.mask->src)) {
7873 				field_flags |= I40E_CLOUD_FIELD_IMAC;
7874 			} else {
7875 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
7876 					match.mask->src);
7877 				return I40E_ERR_CONFIG;
7878 			}
7879 		}
7880 		ether_addr_copy(filter->dst_mac, match.key->dst);
7881 		ether_addr_copy(filter->src_mac, match.key->src);
7882 	}
7883 
7884 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
7885 		struct flow_match_vlan match;
7886 
7887 		flow_rule_match_vlan(rule, &match);
7888 		if (match.mask->vlan_id) {
7889 			if (match.mask->vlan_id == VLAN_VID_MASK) {
7890 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
7891 
7892 			} else {
7893 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
7894 					match.mask->vlan_id);
7895 				return I40E_ERR_CONFIG;
7896 			}
7897 		}
7898 
7899 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
7900 	}
7901 
7902 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
7903 		struct flow_match_control match;
7904 
7905 		flow_rule_match_control(rule, &match);
7906 		addr_type = match.key->addr_type;
7907 	}
7908 
7909 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
7910 		struct flow_match_ipv4_addrs match;
7911 
7912 		flow_rule_match_ipv4_addrs(rule, &match);
7913 		if (match.mask->dst) {
7914 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
7915 				field_flags |= I40E_CLOUD_FIELD_IIP;
7916 			} else {
7917 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
7918 					&match.mask->dst);
7919 				return I40E_ERR_CONFIG;
7920 			}
7921 		}
7922 
7923 		if (match.mask->src) {
7924 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
7925 				field_flags |= I40E_CLOUD_FIELD_IIP;
7926 			} else {
7927 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
7928 					&match.mask->src);
7929 				return I40E_ERR_CONFIG;
7930 			}
7931 		}
7932 
7933 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
7934 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
7935 			return I40E_ERR_CONFIG;
7936 		}
7937 		filter->dst_ipv4 = match.key->dst;
7938 		filter->src_ipv4 = match.key->src;
7939 	}
7940 
7941 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
7942 		struct flow_match_ipv6_addrs match;
7943 
7944 		flow_rule_match_ipv6_addrs(rule, &match);
7945 
7946 		/* src and dest IPV6 address should not be LOOPBACK
7947 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
7948 		 */
7949 		if (ipv6_addr_loopback(&match.key->dst) ||
7950 		    ipv6_addr_loopback(&match.key->src)) {
7951 			dev_err(&pf->pdev->dev,
7952 				"Bad ipv6, addr is LOOPBACK\n");
7953 			return I40E_ERR_CONFIG;
7954 		}
7955 		if (!ipv6_addr_any(&match.mask->dst) ||
7956 		    !ipv6_addr_any(&match.mask->src))
7957 			field_flags |= I40E_CLOUD_FIELD_IIP;
7958 
7959 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
7960 		       sizeof(filter->src_ipv6));
7961 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
7962 		       sizeof(filter->dst_ipv6));
7963 	}
7964 
7965 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
7966 		struct flow_match_ports match;
7967 
7968 		flow_rule_match_ports(rule, &match);
7969 		if (match.mask->src) {
7970 			if (match.mask->src == cpu_to_be16(0xffff)) {
7971 				field_flags |= I40E_CLOUD_FIELD_IIP;
7972 			} else {
7973 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
7974 					be16_to_cpu(match.mask->src));
7975 				return I40E_ERR_CONFIG;
7976 			}
7977 		}
7978 
7979 		if (match.mask->dst) {
7980 			if (match.mask->dst == cpu_to_be16(0xffff)) {
7981 				field_flags |= I40E_CLOUD_FIELD_IIP;
7982 			} else {
7983 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
7984 					be16_to_cpu(match.mask->dst));
7985 				return I40E_ERR_CONFIG;
7986 			}
7987 		}
7988 
7989 		filter->dst_port = match.key->dst;
7990 		filter->src_port = match.key->src;
7991 
7992 		switch (filter->ip_proto) {
7993 		case IPPROTO_TCP:
7994 		case IPPROTO_UDP:
7995 			break;
7996 		default:
7997 			dev_err(&pf->pdev->dev,
7998 				"Only UDP and TCP transport are supported\n");
7999 			return -EINVAL;
8000 		}
8001 	}
8002 	filter->flags = field_flags;
8003 	return 0;
8004 }
8005 
8006 /**
8007  * i40e_handle_tclass: Forward to a traffic class on the device
8008  * @vsi: Pointer to VSI
8009  * @tc: traffic class index on the device
8010  * @filter: Pointer to cloud filter structure
8011  *
8012  **/
8013 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8014 			      struct i40e_cloud_filter *filter)
8015 {
8016 	struct i40e_channel *ch, *ch_tmp;
8017 
8018 	/* direct to a traffic class on the same device */
8019 	if (tc == 0) {
8020 		filter->seid = vsi->seid;
8021 		return 0;
8022 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8023 		if (!filter->dst_port) {
8024 			dev_err(&vsi->back->pdev->dev,
8025 				"Specify destination port to direct to traffic class that is not default\n");
8026 			return -EINVAL;
8027 		}
8028 		if (list_empty(&vsi->ch_list))
8029 			return -EINVAL;
8030 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8031 					 list) {
8032 			if (ch->seid == vsi->tc_seid_map[tc])
8033 				filter->seid = ch->seid;
8034 		}
8035 		return 0;
8036 	}
8037 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8038 	return -EINVAL;
8039 }
8040 
8041 /**
8042  * i40e_configure_clsflower - Configure tc flower filters
8043  * @vsi: Pointer to VSI
8044  * @cls_flower: Pointer to struct flow_cls_offload
8045  *
8046  **/
8047 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8048 				    struct flow_cls_offload *cls_flower)
8049 {
8050 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8051 	struct i40e_cloud_filter *filter = NULL;
8052 	struct i40e_pf *pf = vsi->back;
8053 	int err = 0;
8054 
8055 	if (tc < 0) {
8056 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8057 		return -EOPNOTSUPP;
8058 	}
8059 
8060 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8061 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8062 		return -EBUSY;
8063 
8064 	if (pf->fdir_pf_active_filters ||
8065 	    (!hlist_empty(&pf->fdir_filter_list))) {
8066 		dev_err(&vsi->back->pdev->dev,
8067 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8068 		return -EINVAL;
8069 	}
8070 
8071 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8072 		dev_err(&vsi->back->pdev->dev,
8073 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8074 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8075 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8076 	}
8077 
8078 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8079 	if (!filter)
8080 		return -ENOMEM;
8081 
8082 	filter->cookie = cls_flower->cookie;
8083 
8084 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8085 	if (err < 0)
8086 		goto err;
8087 
8088 	err = i40e_handle_tclass(vsi, tc, filter);
8089 	if (err < 0)
8090 		goto err;
8091 
8092 	/* Add cloud filter */
8093 	if (filter->dst_port)
8094 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8095 	else
8096 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8097 
8098 	if (err) {
8099 		dev_err(&pf->pdev->dev,
8100 			"Failed to add cloud filter, err %s\n",
8101 			i40e_stat_str(&pf->hw, err));
8102 		goto err;
8103 	}
8104 
8105 	/* add filter to the ordered list */
8106 	INIT_HLIST_NODE(&filter->cloud_node);
8107 
8108 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8109 
8110 	pf->num_cloud_filters++;
8111 
8112 	return err;
8113 err:
8114 	kfree(filter);
8115 	return err;
8116 }
8117 
8118 /**
8119  * i40e_find_cloud_filter - Find the could filter in the list
8120  * @vsi: Pointer to VSI
8121  * @cookie: filter specific cookie
8122  *
8123  **/
8124 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8125 							unsigned long *cookie)
8126 {
8127 	struct i40e_cloud_filter *filter = NULL;
8128 	struct hlist_node *node2;
8129 
8130 	hlist_for_each_entry_safe(filter, node2,
8131 				  &vsi->back->cloud_filter_list, cloud_node)
8132 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8133 			return filter;
8134 	return NULL;
8135 }
8136 
8137 /**
8138  * i40e_delete_clsflower - Remove tc flower filters
8139  * @vsi: Pointer to VSI
8140  * @cls_flower: Pointer to struct flow_cls_offload
8141  *
8142  **/
8143 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8144 				 struct flow_cls_offload *cls_flower)
8145 {
8146 	struct i40e_cloud_filter *filter = NULL;
8147 	struct i40e_pf *pf = vsi->back;
8148 	int err = 0;
8149 
8150 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8151 
8152 	if (!filter)
8153 		return -EINVAL;
8154 
8155 	hash_del(&filter->cloud_node);
8156 
8157 	if (filter->dst_port)
8158 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8159 	else
8160 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8161 
8162 	kfree(filter);
8163 	if (err) {
8164 		dev_err(&pf->pdev->dev,
8165 			"Failed to delete cloud filter, err %s\n",
8166 			i40e_stat_str(&pf->hw, err));
8167 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8168 	}
8169 
8170 	pf->num_cloud_filters--;
8171 	if (!pf->num_cloud_filters)
8172 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8173 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8174 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8175 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8176 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8177 		}
8178 	return 0;
8179 }
8180 
8181 /**
8182  * i40e_setup_tc_cls_flower - flower classifier offloads
8183  * @np: net device to configure
8184  * @cls_flower: offload data
8185  **/
8186 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8187 				    struct flow_cls_offload *cls_flower)
8188 {
8189 	struct i40e_vsi *vsi = np->vsi;
8190 
8191 	switch (cls_flower->command) {
8192 	case FLOW_CLS_REPLACE:
8193 		return i40e_configure_clsflower(vsi, cls_flower);
8194 	case FLOW_CLS_DESTROY:
8195 		return i40e_delete_clsflower(vsi, cls_flower);
8196 	case FLOW_CLS_STATS:
8197 		return -EOPNOTSUPP;
8198 	default:
8199 		return -EOPNOTSUPP;
8200 	}
8201 }
8202 
8203 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8204 				  void *cb_priv)
8205 {
8206 	struct i40e_netdev_priv *np = cb_priv;
8207 
8208 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8209 		return -EOPNOTSUPP;
8210 
8211 	switch (type) {
8212 	case TC_SETUP_CLSFLOWER:
8213 		return i40e_setup_tc_cls_flower(np, type_data);
8214 
8215 	default:
8216 		return -EOPNOTSUPP;
8217 	}
8218 }
8219 
8220 static LIST_HEAD(i40e_block_cb_list);
8221 
8222 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8223 			   void *type_data)
8224 {
8225 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8226 
8227 	switch (type) {
8228 	case TC_SETUP_QDISC_MQPRIO:
8229 		return i40e_setup_tc(netdev, type_data);
8230 	case TC_SETUP_BLOCK:
8231 		return flow_block_cb_setup_simple(type_data,
8232 						  &i40e_block_cb_list,
8233 						  i40e_setup_tc_block_cb,
8234 						  np, np, true);
8235 	default:
8236 		return -EOPNOTSUPP;
8237 	}
8238 }
8239 
8240 /**
8241  * i40e_open - Called when a network interface is made active
8242  * @netdev: network interface device structure
8243  *
8244  * The open entry point is called when a network interface is made
8245  * active by the system (IFF_UP).  At this point all resources needed
8246  * for transmit and receive operations are allocated, the interrupt
8247  * handler is registered with the OS, the netdev watchdog subtask is
8248  * enabled, and the stack is notified that the interface is ready.
8249  *
8250  * Returns 0 on success, negative value on failure
8251  **/
8252 int i40e_open(struct net_device *netdev)
8253 {
8254 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8255 	struct i40e_vsi *vsi = np->vsi;
8256 	struct i40e_pf *pf = vsi->back;
8257 	int err;
8258 
8259 	/* disallow open during test or if eeprom is broken */
8260 	if (test_bit(__I40E_TESTING, pf->state) ||
8261 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8262 		return -EBUSY;
8263 
8264 	netif_carrier_off(netdev);
8265 
8266 	if (i40e_force_link_state(pf, true))
8267 		return -EAGAIN;
8268 
8269 	err = i40e_vsi_open(vsi);
8270 	if (err)
8271 		return err;
8272 
8273 	/* configure global TSO hardware offload settings */
8274 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8275 						       TCP_FLAG_FIN) >> 16);
8276 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8277 						       TCP_FLAG_FIN |
8278 						       TCP_FLAG_CWR) >> 16);
8279 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8280 
8281 	udp_tunnel_get_rx_info(netdev);
8282 
8283 	return 0;
8284 }
8285 
8286 /**
8287  * i40e_vsi_open -
8288  * @vsi: the VSI to open
8289  *
8290  * Finish initialization of the VSI.
8291  *
8292  * Returns 0 on success, negative value on failure
8293  *
8294  * Note: expects to be called while under rtnl_lock()
8295  **/
8296 int i40e_vsi_open(struct i40e_vsi *vsi)
8297 {
8298 	struct i40e_pf *pf = vsi->back;
8299 	char int_name[I40E_INT_NAME_STR_LEN];
8300 	int err;
8301 
8302 	/* allocate descriptors */
8303 	err = i40e_vsi_setup_tx_resources(vsi);
8304 	if (err)
8305 		goto err_setup_tx;
8306 	err = i40e_vsi_setup_rx_resources(vsi);
8307 	if (err)
8308 		goto err_setup_rx;
8309 
8310 	err = i40e_vsi_configure(vsi);
8311 	if (err)
8312 		goto err_setup_rx;
8313 
8314 	if (vsi->netdev) {
8315 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
8316 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
8317 		err = i40e_vsi_request_irq(vsi, int_name);
8318 		if (err)
8319 			goto err_setup_rx;
8320 
8321 		/* Notify the stack of the actual queue counts. */
8322 		err = netif_set_real_num_tx_queues(vsi->netdev,
8323 						   vsi->num_queue_pairs);
8324 		if (err)
8325 			goto err_set_queues;
8326 
8327 		err = netif_set_real_num_rx_queues(vsi->netdev,
8328 						   vsi->num_queue_pairs);
8329 		if (err)
8330 			goto err_set_queues;
8331 
8332 	} else if (vsi->type == I40E_VSI_FDIR) {
8333 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
8334 			 dev_driver_string(&pf->pdev->dev),
8335 			 dev_name(&pf->pdev->dev));
8336 		err = i40e_vsi_request_irq(vsi, int_name);
8337 
8338 	} else {
8339 		err = -EINVAL;
8340 		goto err_setup_rx;
8341 	}
8342 
8343 	err = i40e_up_complete(vsi);
8344 	if (err)
8345 		goto err_up_complete;
8346 
8347 	return 0;
8348 
8349 err_up_complete:
8350 	i40e_down(vsi);
8351 err_set_queues:
8352 	i40e_vsi_free_irq(vsi);
8353 err_setup_rx:
8354 	i40e_vsi_free_rx_resources(vsi);
8355 err_setup_tx:
8356 	i40e_vsi_free_tx_resources(vsi);
8357 	if (vsi == pf->vsi[pf->lan_vsi])
8358 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
8359 
8360 	return err;
8361 }
8362 
8363 /**
8364  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
8365  * @pf: Pointer to PF
8366  *
8367  * This function destroys the hlist where all the Flow Director
8368  * filters were saved.
8369  **/
8370 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
8371 {
8372 	struct i40e_fdir_filter *filter;
8373 	struct i40e_flex_pit *pit_entry, *tmp;
8374 	struct hlist_node *node2;
8375 
8376 	hlist_for_each_entry_safe(filter, node2,
8377 				  &pf->fdir_filter_list, fdir_node) {
8378 		hlist_del(&filter->fdir_node);
8379 		kfree(filter);
8380 	}
8381 
8382 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
8383 		list_del(&pit_entry->list);
8384 		kfree(pit_entry);
8385 	}
8386 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
8387 
8388 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
8389 		list_del(&pit_entry->list);
8390 		kfree(pit_entry);
8391 	}
8392 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
8393 
8394 	pf->fdir_pf_active_filters = 0;
8395 	pf->fd_tcp4_filter_cnt = 0;
8396 	pf->fd_udp4_filter_cnt = 0;
8397 	pf->fd_sctp4_filter_cnt = 0;
8398 	pf->fd_ip4_filter_cnt = 0;
8399 
8400 	/* Reprogram the default input set for TCP/IPv4 */
8401 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8402 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8403 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8404 
8405 	/* Reprogram the default input set for UDP/IPv4 */
8406 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
8407 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8408 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8409 
8410 	/* Reprogram the default input set for SCTP/IPv4 */
8411 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
8412 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8413 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8414 
8415 	/* Reprogram the default input set for Other/IPv4 */
8416 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
8417 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8418 
8419 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
8420 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8421 }
8422 
8423 /**
8424  * i40e_cloud_filter_exit - Cleans up the cloud filters
8425  * @pf: Pointer to PF
8426  *
8427  * This function destroys the hlist where all the cloud filters
8428  * were saved.
8429  **/
8430 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
8431 {
8432 	struct i40e_cloud_filter *cfilter;
8433 	struct hlist_node *node;
8434 
8435 	hlist_for_each_entry_safe(cfilter, node,
8436 				  &pf->cloud_filter_list, cloud_node) {
8437 		hlist_del(&cfilter->cloud_node);
8438 		kfree(cfilter);
8439 	}
8440 	pf->num_cloud_filters = 0;
8441 
8442 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8443 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8444 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8445 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8446 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8447 	}
8448 }
8449 
8450 /**
8451  * i40e_close - Disables a network interface
8452  * @netdev: network interface device structure
8453  *
8454  * The close entry point is called when an interface is de-activated
8455  * by the OS.  The hardware is still under the driver's control, but
8456  * this netdev interface is disabled.
8457  *
8458  * Returns 0, this is not allowed to fail
8459  **/
8460 int i40e_close(struct net_device *netdev)
8461 {
8462 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8463 	struct i40e_vsi *vsi = np->vsi;
8464 
8465 	i40e_vsi_close(vsi);
8466 
8467 	return 0;
8468 }
8469 
8470 /**
8471  * i40e_do_reset - Start a PF or Core Reset sequence
8472  * @pf: board private structure
8473  * @reset_flags: which reset is requested
8474  * @lock_acquired: indicates whether or not the lock has been acquired
8475  * before this function was called.
8476  *
8477  * The essential difference in resets is that the PF Reset
8478  * doesn't clear the packet buffers, doesn't reset the PE
8479  * firmware, and doesn't bother the other PFs on the chip.
8480  **/
8481 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
8482 {
8483 	u32 val;
8484 
8485 	/* do the biggest reset indicated */
8486 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
8487 
8488 		/* Request a Global Reset
8489 		 *
8490 		 * This will start the chip's countdown to the actual full
8491 		 * chip reset event, and a warning interrupt to be sent
8492 		 * to all PFs, including the requestor.  Our handler
8493 		 * for the warning interrupt will deal with the shutdown
8494 		 * and recovery of the switch setup.
8495 		 */
8496 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
8497 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8498 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
8499 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
8500 
8501 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
8502 
8503 		/* Request a Core Reset
8504 		 *
8505 		 * Same as Global Reset, except does *not* include the MAC/PHY
8506 		 */
8507 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
8508 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8509 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
8510 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
8511 		i40e_flush(&pf->hw);
8512 
8513 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
8514 
8515 		/* Request a PF Reset
8516 		 *
8517 		 * Resets only the PF-specific registers
8518 		 *
8519 		 * This goes directly to the tear-down and rebuild of
8520 		 * the switch, since we need to do all the recovery as
8521 		 * for the Core Reset.
8522 		 */
8523 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
8524 		i40e_handle_reset_warning(pf, lock_acquired);
8525 
8526 		dev_info(&pf->pdev->dev,
8527 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
8528 			 "FW LLDP is disabled\n" :
8529 			 "FW LLDP is enabled\n");
8530 
8531 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
8532 		int v;
8533 
8534 		/* Find the VSI(s) that requested a re-init */
8535 		dev_info(&pf->pdev->dev,
8536 			 "VSI reinit requested\n");
8537 		for (v = 0; v < pf->num_alloc_vsi; v++) {
8538 			struct i40e_vsi *vsi = pf->vsi[v];
8539 
8540 			if (vsi != NULL &&
8541 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
8542 					       vsi->state))
8543 				i40e_vsi_reinit_locked(pf->vsi[v]);
8544 		}
8545 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
8546 		int v;
8547 
8548 		/* Find the VSI(s) that needs to be brought down */
8549 		dev_info(&pf->pdev->dev, "VSI down requested\n");
8550 		for (v = 0; v < pf->num_alloc_vsi; v++) {
8551 			struct i40e_vsi *vsi = pf->vsi[v];
8552 
8553 			if (vsi != NULL &&
8554 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
8555 					       vsi->state)) {
8556 				set_bit(__I40E_VSI_DOWN, vsi->state);
8557 				i40e_down(vsi);
8558 			}
8559 		}
8560 	} else {
8561 		dev_info(&pf->pdev->dev,
8562 			 "bad reset request 0x%08x\n", reset_flags);
8563 	}
8564 }
8565 
8566 #ifdef CONFIG_I40E_DCB
8567 /**
8568  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
8569  * @pf: board private structure
8570  * @old_cfg: current DCB config
8571  * @new_cfg: new DCB config
8572  **/
8573 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
8574 			    struct i40e_dcbx_config *old_cfg,
8575 			    struct i40e_dcbx_config *new_cfg)
8576 {
8577 	bool need_reconfig = false;
8578 
8579 	/* Check if ETS configuration has changed */
8580 	if (memcmp(&new_cfg->etscfg,
8581 		   &old_cfg->etscfg,
8582 		   sizeof(new_cfg->etscfg))) {
8583 		/* If Priority Table has changed reconfig is needed */
8584 		if (memcmp(&new_cfg->etscfg.prioritytable,
8585 			   &old_cfg->etscfg.prioritytable,
8586 			   sizeof(new_cfg->etscfg.prioritytable))) {
8587 			need_reconfig = true;
8588 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
8589 		}
8590 
8591 		if (memcmp(&new_cfg->etscfg.tcbwtable,
8592 			   &old_cfg->etscfg.tcbwtable,
8593 			   sizeof(new_cfg->etscfg.tcbwtable)))
8594 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
8595 
8596 		if (memcmp(&new_cfg->etscfg.tsatable,
8597 			   &old_cfg->etscfg.tsatable,
8598 			   sizeof(new_cfg->etscfg.tsatable)))
8599 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
8600 	}
8601 
8602 	/* Check if PFC configuration has changed */
8603 	if (memcmp(&new_cfg->pfc,
8604 		   &old_cfg->pfc,
8605 		   sizeof(new_cfg->pfc))) {
8606 		need_reconfig = true;
8607 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
8608 	}
8609 
8610 	/* Check if APP Table has changed */
8611 	if (memcmp(&new_cfg->app,
8612 		   &old_cfg->app,
8613 		   sizeof(new_cfg->app))) {
8614 		need_reconfig = true;
8615 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
8616 	}
8617 
8618 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
8619 	return need_reconfig;
8620 }
8621 
8622 /**
8623  * i40e_handle_lldp_event - Handle LLDP Change MIB event
8624  * @pf: board private structure
8625  * @e: event info posted on ARQ
8626  **/
8627 static int i40e_handle_lldp_event(struct i40e_pf *pf,
8628 				  struct i40e_arq_event_info *e)
8629 {
8630 	struct i40e_aqc_lldp_get_mib *mib =
8631 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
8632 	struct i40e_hw *hw = &pf->hw;
8633 	struct i40e_dcbx_config tmp_dcbx_cfg;
8634 	bool need_reconfig = false;
8635 	int ret = 0;
8636 	u8 type;
8637 
8638 	/* Not DCB capable or capability disabled */
8639 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
8640 		return ret;
8641 
8642 	/* Ignore if event is not for Nearest Bridge */
8643 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
8644 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
8645 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
8646 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
8647 		return ret;
8648 
8649 	/* Check MIB Type and return if event for Remote MIB update */
8650 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
8651 	dev_dbg(&pf->pdev->dev,
8652 		"LLDP event mib type %s\n", type ? "remote" : "local");
8653 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
8654 		/* Update the remote cached instance and return */
8655 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
8656 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
8657 				&hw->remote_dcbx_config);
8658 		goto exit;
8659 	}
8660 
8661 	/* Store the old configuration */
8662 	tmp_dcbx_cfg = hw->local_dcbx_config;
8663 
8664 	/* Reset the old DCBx configuration data */
8665 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
8666 	/* Get updated DCBX data from firmware */
8667 	ret = i40e_get_dcb_config(&pf->hw);
8668 	if (ret) {
8669 		dev_info(&pf->pdev->dev,
8670 			 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
8671 			 i40e_stat_str(&pf->hw, ret),
8672 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8673 		goto exit;
8674 	}
8675 
8676 	/* No change detected in DCBX configs */
8677 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
8678 		    sizeof(tmp_dcbx_cfg))) {
8679 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
8680 		goto exit;
8681 	}
8682 
8683 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
8684 					       &hw->local_dcbx_config);
8685 
8686 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
8687 
8688 	if (!need_reconfig)
8689 		goto exit;
8690 
8691 	/* Enable DCB tagging only when more than one TC */
8692 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
8693 		pf->flags |= I40E_FLAG_DCB_ENABLED;
8694 	else
8695 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8696 
8697 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
8698 	/* Reconfiguration needed quiesce all VSIs */
8699 	i40e_pf_quiesce_all_vsi(pf);
8700 
8701 	/* Changes in configuration update VEB/VSI */
8702 	i40e_dcb_reconfigure(pf);
8703 
8704 	ret = i40e_resume_port_tx(pf);
8705 
8706 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
8707 	/* In case of error no point in resuming VSIs */
8708 	if (ret)
8709 		goto exit;
8710 
8711 	/* Wait for the PF's queues to be disabled */
8712 	ret = i40e_pf_wait_queues_disabled(pf);
8713 	if (ret) {
8714 		/* Schedule PF reset to recover */
8715 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
8716 		i40e_service_event_schedule(pf);
8717 	} else {
8718 		i40e_pf_unquiesce_all_vsi(pf);
8719 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
8720 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
8721 	}
8722 
8723 exit:
8724 	return ret;
8725 }
8726 #endif /* CONFIG_I40E_DCB */
8727 
8728 /**
8729  * i40e_do_reset_safe - Protected reset path for userland calls.
8730  * @pf: board private structure
8731  * @reset_flags: which reset is requested
8732  *
8733  **/
8734 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
8735 {
8736 	rtnl_lock();
8737 	i40e_do_reset(pf, reset_flags, true);
8738 	rtnl_unlock();
8739 }
8740 
8741 /**
8742  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
8743  * @pf: board private structure
8744  * @e: event info posted on ARQ
8745  *
8746  * Handler for LAN Queue Overflow Event generated by the firmware for PF
8747  * and VF queues
8748  **/
8749 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
8750 					   struct i40e_arq_event_info *e)
8751 {
8752 	struct i40e_aqc_lan_overflow *data =
8753 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
8754 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
8755 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
8756 	struct i40e_hw *hw = &pf->hw;
8757 	struct i40e_vf *vf;
8758 	u16 vf_id;
8759 
8760 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
8761 		queue, qtx_ctl);
8762 
8763 	/* Queue belongs to VF, find the VF and issue VF reset */
8764 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
8765 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
8766 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
8767 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
8768 		vf_id -= hw->func_caps.vf_base_id;
8769 		vf = &pf->vf[vf_id];
8770 		i40e_vc_notify_vf_reset(vf);
8771 		/* Allow VF to process pending reset notification */
8772 		msleep(20);
8773 		i40e_reset_vf(vf, false);
8774 	}
8775 }
8776 
8777 /**
8778  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
8779  * @pf: board private structure
8780  **/
8781 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
8782 {
8783 	u32 val, fcnt_prog;
8784 
8785 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8786 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
8787 	return fcnt_prog;
8788 }
8789 
8790 /**
8791  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
8792  * @pf: board private structure
8793  **/
8794 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
8795 {
8796 	u32 val, fcnt_prog;
8797 
8798 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8799 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
8800 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
8801 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
8802 	return fcnt_prog;
8803 }
8804 
8805 /**
8806  * i40e_get_global_fd_count - Get total FD filters programmed on device
8807  * @pf: board private structure
8808  **/
8809 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
8810 {
8811 	u32 val, fcnt_prog;
8812 
8813 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
8814 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
8815 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
8816 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
8817 	return fcnt_prog;
8818 }
8819 
8820 /**
8821  * i40e_reenable_fdir_sb - Restore FDir SB capability
8822  * @pf: board private structure
8823  **/
8824 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
8825 {
8826 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
8827 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8828 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
8829 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
8830 }
8831 
8832 /**
8833  * i40e_reenable_fdir_atr - Restore FDir ATR capability
8834  * @pf: board private structure
8835  **/
8836 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
8837 {
8838 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
8839 		/* ATR uses the same filtering logic as SB rules. It only
8840 		 * functions properly if the input set mask is at the default
8841 		 * settings. It is safe to restore the default input set
8842 		 * because there are no active TCPv4 filter rules.
8843 		 */
8844 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8845 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8846 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8847 
8848 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8849 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
8850 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
8851 	}
8852 }
8853 
8854 /**
8855  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
8856  * @pf: board private structure
8857  * @filter: FDir filter to remove
8858  */
8859 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
8860 				       struct i40e_fdir_filter *filter)
8861 {
8862 	/* Update counters */
8863 	pf->fdir_pf_active_filters--;
8864 	pf->fd_inv = 0;
8865 
8866 	switch (filter->flow_type) {
8867 	case TCP_V4_FLOW:
8868 		pf->fd_tcp4_filter_cnt--;
8869 		break;
8870 	case UDP_V4_FLOW:
8871 		pf->fd_udp4_filter_cnt--;
8872 		break;
8873 	case SCTP_V4_FLOW:
8874 		pf->fd_sctp4_filter_cnt--;
8875 		break;
8876 	case IP_USER_FLOW:
8877 		switch (filter->ip4_proto) {
8878 		case IPPROTO_TCP:
8879 			pf->fd_tcp4_filter_cnt--;
8880 			break;
8881 		case IPPROTO_UDP:
8882 			pf->fd_udp4_filter_cnt--;
8883 			break;
8884 		case IPPROTO_SCTP:
8885 			pf->fd_sctp4_filter_cnt--;
8886 			break;
8887 		case IPPROTO_IP:
8888 			pf->fd_ip4_filter_cnt--;
8889 			break;
8890 		}
8891 		break;
8892 	}
8893 
8894 	/* Remove the filter from the list and free memory */
8895 	hlist_del(&filter->fdir_node);
8896 	kfree(filter);
8897 }
8898 
8899 /**
8900  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
8901  * @pf: board private structure
8902  **/
8903 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
8904 {
8905 	struct i40e_fdir_filter *filter;
8906 	u32 fcnt_prog, fcnt_avail;
8907 	struct hlist_node *node;
8908 
8909 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
8910 		return;
8911 
8912 	/* Check if we have enough room to re-enable FDir SB capability. */
8913 	fcnt_prog = i40e_get_global_fd_count(pf);
8914 	fcnt_avail = pf->fdir_pf_filter_count;
8915 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
8916 	    (pf->fd_add_err == 0) ||
8917 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
8918 		i40e_reenable_fdir_sb(pf);
8919 
8920 	/* We should wait for even more space before re-enabling ATR.
8921 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
8922 	 * rules active.
8923 	 */
8924 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
8925 	    (pf->fd_tcp4_filter_cnt == 0))
8926 		i40e_reenable_fdir_atr(pf);
8927 
8928 	/* if hw had a problem adding a filter, delete it */
8929 	if (pf->fd_inv > 0) {
8930 		hlist_for_each_entry_safe(filter, node,
8931 					  &pf->fdir_filter_list, fdir_node)
8932 			if (filter->fd_id == pf->fd_inv)
8933 				i40e_delete_invalid_filter(pf, filter);
8934 	}
8935 }
8936 
8937 #define I40E_MIN_FD_FLUSH_INTERVAL 10
8938 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
8939 /**
8940  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
8941  * @pf: board private structure
8942  **/
8943 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
8944 {
8945 	unsigned long min_flush_time;
8946 	int flush_wait_retry = 50;
8947 	bool disable_atr = false;
8948 	int fd_room;
8949 	int reg;
8950 
8951 	if (!time_after(jiffies, pf->fd_flush_timestamp +
8952 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
8953 		return;
8954 
8955 	/* If the flush is happening too quick and we have mostly SB rules we
8956 	 * should not re-enable ATR for some time.
8957 	 */
8958 	min_flush_time = pf->fd_flush_timestamp +
8959 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
8960 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
8961 
8962 	if (!(time_after(jiffies, min_flush_time)) &&
8963 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
8964 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
8965 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
8966 		disable_atr = true;
8967 	}
8968 
8969 	pf->fd_flush_timestamp = jiffies;
8970 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
8971 	/* flush all filters */
8972 	wr32(&pf->hw, I40E_PFQF_CTL_1,
8973 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
8974 	i40e_flush(&pf->hw);
8975 	pf->fd_flush_cnt++;
8976 	pf->fd_add_err = 0;
8977 	do {
8978 		/* Check FD flush status every 5-6msec */
8979 		usleep_range(5000, 6000);
8980 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
8981 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
8982 			break;
8983 	} while (flush_wait_retry--);
8984 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
8985 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
8986 	} else {
8987 		/* replay sideband filters */
8988 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
8989 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
8990 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
8991 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
8992 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
8993 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
8994 	}
8995 }
8996 
8997 /**
8998  * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
8999  * @pf: board private structure
9000  **/
9001 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9002 {
9003 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9004 }
9005 
9006 /**
9007  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9008  * @pf: board private structure
9009  **/
9010 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9011 {
9012 
9013 	/* if interface is down do nothing */
9014 	if (test_bit(__I40E_DOWN, pf->state))
9015 		return;
9016 
9017 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9018 		i40e_fdir_flush_and_replay(pf);
9019 
9020 	i40e_fdir_check_and_reenable(pf);
9021 
9022 }
9023 
9024 /**
9025  * i40e_vsi_link_event - notify VSI of a link event
9026  * @vsi: vsi to be notified
9027  * @link_up: link up or down
9028  **/
9029 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9030 {
9031 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9032 		return;
9033 
9034 	switch (vsi->type) {
9035 	case I40E_VSI_MAIN:
9036 		if (!vsi->netdev || !vsi->netdev_registered)
9037 			break;
9038 
9039 		if (link_up) {
9040 			netif_carrier_on(vsi->netdev);
9041 			netif_tx_wake_all_queues(vsi->netdev);
9042 		} else {
9043 			netif_carrier_off(vsi->netdev);
9044 			netif_tx_stop_all_queues(vsi->netdev);
9045 		}
9046 		break;
9047 
9048 	case I40E_VSI_SRIOV:
9049 	case I40E_VSI_VMDQ2:
9050 	case I40E_VSI_CTRL:
9051 	case I40E_VSI_IWARP:
9052 	case I40E_VSI_MIRROR:
9053 	default:
9054 		/* there is no notification for other VSIs */
9055 		break;
9056 	}
9057 }
9058 
9059 /**
9060  * i40e_veb_link_event - notify elements on the veb of a link event
9061  * @veb: veb to be notified
9062  * @link_up: link up or down
9063  **/
9064 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9065 {
9066 	struct i40e_pf *pf;
9067 	int i;
9068 
9069 	if (!veb || !veb->pf)
9070 		return;
9071 	pf = veb->pf;
9072 
9073 	/* depth first... */
9074 	for (i = 0; i < I40E_MAX_VEB; i++)
9075 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9076 			i40e_veb_link_event(pf->veb[i], link_up);
9077 
9078 	/* ... now the local VSIs */
9079 	for (i = 0; i < pf->num_alloc_vsi; i++)
9080 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9081 			i40e_vsi_link_event(pf->vsi[i], link_up);
9082 }
9083 
9084 /**
9085  * i40e_link_event - Update netif_carrier status
9086  * @pf: board private structure
9087  **/
9088 static void i40e_link_event(struct i40e_pf *pf)
9089 {
9090 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9091 	u8 new_link_speed, old_link_speed;
9092 	i40e_status status;
9093 	bool new_link, old_link;
9094 
9095 	/* set this to force the get_link_status call to refresh state */
9096 	pf->hw.phy.get_link_info = true;
9097 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9098 	status = i40e_get_link_status(&pf->hw, &new_link);
9099 
9100 	/* On success, disable temp link polling */
9101 	if (status == I40E_SUCCESS) {
9102 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9103 	} else {
9104 		/* Enable link polling temporarily until i40e_get_link_status
9105 		 * returns I40E_SUCCESS
9106 		 */
9107 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9108 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9109 			status);
9110 		return;
9111 	}
9112 
9113 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9114 	new_link_speed = pf->hw.phy.link_info.link_speed;
9115 
9116 	if (new_link == old_link &&
9117 	    new_link_speed == old_link_speed &&
9118 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9119 	     new_link == netif_carrier_ok(vsi->netdev)))
9120 		return;
9121 
9122 	i40e_print_link_message(vsi, new_link);
9123 
9124 	/* Notify the base of the switch tree connected to
9125 	 * the link.  Floating VEBs are not notified.
9126 	 */
9127 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9128 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9129 	else
9130 		i40e_vsi_link_event(vsi, new_link);
9131 
9132 	if (pf->vf)
9133 		i40e_vc_notify_link_state(pf);
9134 
9135 	if (pf->flags & I40E_FLAG_PTP)
9136 		i40e_ptp_set_increment(pf);
9137 }
9138 
9139 /**
9140  * i40e_watchdog_subtask - periodic checks not using event driven response
9141  * @pf: board private structure
9142  **/
9143 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9144 {
9145 	int i;
9146 
9147 	/* if interface is down do nothing */
9148 	if (test_bit(__I40E_DOWN, pf->state) ||
9149 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9150 		return;
9151 
9152 	/* make sure we don't do these things too often */
9153 	if (time_before(jiffies, (pf->service_timer_previous +
9154 				  pf->service_timer_period)))
9155 		return;
9156 	pf->service_timer_previous = jiffies;
9157 
9158 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9159 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9160 		i40e_link_event(pf);
9161 
9162 	/* Update the stats for active netdevs so the network stack
9163 	 * can look at updated numbers whenever it cares to
9164 	 */
9165 	for (i = 0; i < pf->num_alloc_vsi; i++)
9166 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9167 			i40e_update_stats(pf->vsi[i]);
9168 
9169 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9170 		/* Update the stats for the active switching components */
9171 		for (i = 0; i < I40E_MAX_VEB; i++)
9172 			if (pf->veb[i])
9173 				i40e_update_veb_stats(pf->veb[i]);
9174 	}
9175 
9176 	i40e_ptp_rx_hang(pf);
9177 	i40e_ptp_tx_hang(pf);
9178 }
9179 
9180 /**
9181  * i40e_reset_subtask - Set up for resetting the device and driver
9182  * @pf: board private structure
9183  **/
9184 static void i40e_reset_subtask(struct i40e_pf *pf)
9185 {
9186 	u32 reset_flags = 0;
9187 
9188 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
9189 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
9190 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
9191 	}
9192 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
9193 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
9194 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9195 	}
9196 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
9197 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
9198 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
9199 	}
9200 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
9201 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
9202 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
9203 	}
9204 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
9205 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
9206 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
9207 	}
9208 
9209 	/* If there's a recovery already waiting, it takes
9210 	 * precedence before starting a new reset sequence.
9211 	 */
9212 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
9213 		i40e_prep_for_reset(pf, false);
9214 		i40e_reset(pf);
9215 		i40e_rebuild(pf, false, false);
9216 	}
9217 
9218 	/* If we're already down or resetting, just bail */
9219 	if (reset_flags &&
9220 	    !test_bit(__I40E_DOWN, pf->state) &&
9221 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
9222 		i40e_do_reset(pf, reset_flags, false);
9223 	}
9224 }
9225 
9226 /**
9227  * i40e_handle_link_event - Handle link event
9228  * @pf: board private structure
9229  * @e: event info posted on ARQ
9230  **/
9231 static void i40e_handle_link_event(struct i40e_pf *pf,
9232 				   struct i40e_arq_event_info *e)
9233 {
9234 	struct i40e_aqc_get_link_status *status =
9235 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
9236 
9237 	/* Do a new status request to re-enable LSE reporting
9238 	 * and load new status information into the hw struct
9239 	 * This completely ignores any state information
9240 	 * in the ARQ event info, instead choosing to always
9241 	 * issue the AQ update link status command.
9242 	 */
9243 	i40e_link_event(pf);
9244 
9245 	/* Check if module meets thermal requirements */
9246 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
9247 		dev_err(&pf->pdev->dev,
9248 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
9249 		dev_err(&pf->pdev->dev,
9250 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9251 	} else {
9252 		/* check for unqualified module, if link is down, suppress
9253 		 * the message if link was forced to be down.
9254 		 */
9255 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
9256 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
9257 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
9258 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
9259 			dev_err(&pf->pdev->dev,
9260 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
9261 			dev_err(&pf->pdev->dev,
9262 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9263 		}
9264 	}
9265 }
9266 
9267 /**
9268  * i40e_clean_adminq_subtask - Clean the AdminQ rings
9269  * @pf: board private structure
9270  **/
9271 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
9272 {
9273 	struct i40e_arq_event_info event;
9274 	struct i40e_hw *hw = &pf->hw;
9275 	u16 pending, i = 0;
9276 	i40e_status ret;
9277 	u16 opcode;
9278 	u32 oldval;
9279 	u32 val;
9280 
9281 	/* Do not run clean AQ when PF reset fails */
9282 	if (test_bit(__I40E_RESET_FAILED, pf->state))
9283 		return;
9284 
9285 	/* check for error indications */
9286 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
9287 	oldval = val;
9288 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
9289 		if (hw->debug_mask & I40E_DEBUG_AQ)
9290 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
9291 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
9292 	}
9293 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
9294 		if (hw->debug_mask & I40E_DEBUG_AQ)
9295 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
9296 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
9297 		pf->arq_overflows++;
9298 	}
9299 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
9300 		if (hw->debug_mask & I40E_DEBUG_AQ)
9301 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
9302 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
9303 	}
9304 	if (oldval != val)
9305 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
9306 
9307 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
9308 	oldval = val;
9309 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
9310 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9311 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
9312 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
9313 	}
9314 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
9315 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9316 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
9317 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
9318 	}
9319 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
9320 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9321 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
9322 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
9323 	}
9324 	if (oldval != val)
9325 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
9326 
9327 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
9328 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
9329 	if (!event.msg_buf)
9330 		return;
9331 
9332 	do {
9333 		ret = i40e_clean_arq_element(hw, &event, &pending);
9334 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
9335 			break;
9336 		else if (ret) {
9337 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
9338 			break;
9339 		}
9340 
9341 		opcode = le16_to_cpu(event.desc.opcode);
9342 		switch (opcode) {
9343 
9344 		case i40e_aqc_opc_get_link_status:
9345 			i40e_handle_link_event(pf, &event);
9346 			break;
9347 		case i40e_aqc_opc_send_msg_to_pf:
9348 			ret = i40e_vc_process_vf_msg(pf,
9349 					le16_to_cpu(event.desc.retval),
9350 					le32_to_cpu(event.desc.cookie_high),
9351 					le32_to_cpu(event.desc.cookie_low),
9352 					event.msg_buf,
9353 					event.msg_len);
9354 			break;
9355 		case i40e_aqc_opc_lldp_update_mib:
9356 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
9357 #ifdef CONFIG_I40E_DCB
9358 			rtnl_lock();
9359 			ret = i40e_handle_lldp_event(pf, &event);
9360 			rtnl_unlock();
9361 #endif /* CONFIG_I40E_DCB */
9362 			break;
9363 		case i40e_aqc_opc_event_lan_overflow:
9364 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
9365 			i40e_handle_lan_overflow_event(pf, &event);
9366 			break;
9367 		case i40e_aqc_opc_send_msg_to_peer:
9368 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
9369 			break;
9370 		case i40e_aqc_opc_nvm_erase:
9371 		case i40e_aqc_opc_nvm_update:
9372 		case i40e_aqc_opc_oem_post_update:
9373 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
9374 				   "ARQ NVM operation 0x%04x completed\n",
9375 				   opcode);
9376 			break;
9377 		default:
9378 			dev_info(&pf->pdev->dev,
9379 				 "ARQ: Unknown event 0x%04x ignored\n",
9380 				 opcode);
9381 			break;
9382 		}
9383 	} while (i++ < pf->adminq_work_limit);
9384 
9385 	if (i < pf->adminq_work_limit)
9386 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
9387 
9388 	/* re-enable Admin queue interrupt cause */
9389 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
9390 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
9391 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
9392 	i40e_flush(hw);
9393 
9394 	kfree(event.msg_buf);
9395 }
9396 
9397 /**
9398  * i40e_verify_eeprom - make sure eeprom is good to use
9399  * @pf: board private structure
9400  **/
9401 static void i40e_verify_eeprom(struct i40e_pf *pf)
9402 {
9403 	int err;
9404 
9405 	err = i40e_diag_eeprom_test(&pf->hw);
9406 	if (err) {
9407 		/* retry in case of garbage read */
9408 		err = i40e_diag_eeprom_test(&pf->hw);
9409 		if (err) {
9410 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
9411 				 err);
9412 			set_bit(__I40E_BAD_EEPROM, pf->state);
9413 		}
9414 	}
9415 
9416 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
9417 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
9418 		clear_bit(__I40E_BAD_EEPROM, pf->state);
9419 	}
9420 }
9421 
9422 /**
9423  * i40e_enable_pf_switch_lb
9424  * @pf: pointer to the PF structure
9425  *
9426  * enable switch loop back or die - no point in a return value
9427  **/
9428 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
9429 {
9430 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9431 	struct i40e_vsi_context ctxt;
9432 	int ret;
9433 
9434 	ctxt.seid = pf->main_vsi_seid;
9435 	ctxt.pf_num = pf->hw.pf_id;
9436 	ctxt.vf_num = 0;
9437 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9438 	if (ret) {
9439 		dev_info(&pf->pdev->dev,
9440 			 "couldn't get PF vsi config, err %s aq_err %s\n",
9441 			 i40e_stat_str(&pf->hw, ret),
9442 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9443 		return;
9444 	}
9445 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9446 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9447 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9448 
9449 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
9450 	if (ret) {
9451 		dev_info(&pf->pdev->dev,
9452 			 "update vsi switch failed, err %s aq_err %s\n",
9453 			 i40e_stat_str(&pf->hw, ret),
9454 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9455 	}
9456 }
9457 
9458 /**
9459  * i40e_disable_pf_switch_lb
9460  * @pf: pointer to the PF structure
9461  *
9462  * disable switch loop back or die - no point in a return value
9463  **/
9464 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
9465 {
9466 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9467 	struct i40e_vsi_context ctxt;
9468 	int ret;
9469 
9470 	ctxt.seid = pf->main_vsi_seid;
9471 	ctxt.pf_num = pf->hw.pf_id;
9472 	ctxt.vf_num = 0;
9473 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9474 	if (ret) {
9475 		dev_info(&pf->pdev->dev,
9476 			 "couldn't get PF vsi config, err %s aq_err %s\n",
9477 			 i40e_stat_str(&pf->hw, ret),
9478 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9479 		return;
9480 	}
9481 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9482 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9483 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9484 
9485 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
9486 	if (ret) {
9487 		dev_info(&pf->pdev->dev,
9488 			 "update vsi switch failed, err %s aq_err %s\n",
9489 			 i40e_stat_str(&pf->hw, ret),
9490 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9491 	}
9492 }
9493 
9494 /**
9495  * i40e_config_bridge_mode - Configure the HW bridge mode
9496  * @veb: pointer to the bridge instance
9497  *
9498  * Configure the loop back mode for the LAN VSI that is downlink to the
9499  * specified HW bridge instance. It is expected this function is called
9500  * when a new HW bridge is instantiated.
9501  **/
9502 static void i40e_config_bridge_mode(struct i40e_veb *veb)
9503 {
9504 	struct i40e_pf *pf = veb->pf;
9505 
9506 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
9507 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
9508 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
9509 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
9510 		i40e_disable_pf_switch_lb(pf);
9511 	else
9512 		i40e_enable_pf_switch_lb(pf);
9513 }
9514 
9515 /**
9516  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
9517  * @veb: pointer to the VEB instance
9518  *
9519  * This is a recursive function that first builds the attached VSIs then
9520  * recurses in to build the next layer of VEB.  We track the connections
9521  * through our own index numbers because the seid's from the HW could
9522  * change across the reset.
9523  **/
9524 static int i40e_reconstitute_veb(struct i40e_veb *veb)
9525 {
9526 	struct i40e_vsi *ctl_vsi = NULL;
9527 	struct i40e_pf *pf = veb->pf;
9528 	int v, veb_idx;
9529 	int ret;
9530 
9531 	/* build VSI that owns this VEB, temporarily attached to base VEB */
9532 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
9533 		if (pf->vsi[v] &&
9534 		    pf->vsi[v]->veb_idx == veb->idx &&
9535 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
9536 			ctl_vsi = pf->vsi[v];
9537 			break;
9538 		}
9539 	}
9540 	if (!ctl_vsi) {
9541 		dev_info(&pf->pdev->dev,
9542 			 "missing owner VSI for veb_idx %d\n", veb->idx);
9543 		ret = -ENOENT;
9544 		goto end_reconstitute;
9545 	}
9546 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
9547 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
9548 	ret = i40e_add_vsi(ctl_vsi);
9549 	if (ret) {
9550 		dev_info(&pf->pdev->dev,
9551 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
9552 			 veb->idx, ret);
9553 		goto end_reconstitute;
9554 	}
9555 	i40e_vsi_reset_stats(ctl_vsi);
9556 
9557 	/* create the VEB in the switch and move the VSI onto the VEB */
9558 	ret = i40e_add_veb(veb, ctl_vsi);
9559 	if (ret)
9560 		goto end_reconstitute;
9561 
9562 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
9563 		veb->bridge_mode = BRIDGE_MODE_VEB;
9564 	else
9565 		veb->bridge_mode = BRIDGE_MODE_VEPA;
9566 	i40e_config_bridge_mode(veb);
9567 
9568 	/* create the remaining VSIs attached to this VEB */
9569 	for (v = 0; v < pf->num_alloc_vsi; v++) {
9570 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
9571 			continue;
9572 
9573 		if (pf->vsi[v]->veb_idx == veb->idx) {
9574 			struct i40e_vsi *vsi = pf->vsi[v];
9575 
9576 			vsi->uplink_seid = veb->seid;
9577 			ret = i40e_add_vsi(vsi);
9578 			if (ret) {
9579 				dev_info(&pf->pdev->dev,
9580 					 "rebuild of vsi_idx %d failed: %d\n",
9581 					 v, ret);
9582 				goto end_reconstitute;
9583 			}
9584 			i40e_vsi_reset_stats(vsi);
9585 		}
9586 	}
9587 
9588 	/* create any VEBs attached to this VEB - RECURSION */
9589 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
9590 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
9591 			pf->veb[veb_idx]->uplink_seid = veb->seid;
9592 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
9593 			if (ret)
9594 				break;
9595 		}
9596 	}
9597 
9598 end_reconstitute:
9599 	return ret;
9600 }
9601 
9602 /**
9603  * i40e_get_capabilities - get info about the HW
9604  * @pf: the PF struct
9605  * @list_type: AQ capability to be queried
9606  **/
9607 static int i40e_get_capabilities(struct i40e_pf *pf,
9608 				 enum i40e_admin_queue_opc list_type)
9609 {
9610 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
9611 	u16 data_size;
9612 	int buf_len;
9613 	int err;
9614 
9615 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
9616 	do {
9617 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
9618 		if (!cap_buf)
9619 			return -ENOMEM;
9620 
9621 		/* this loads the data into the hw struct for us */
9622 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
9623 						    &data_size, list_type,
9624 						    NULL);
9625 		/* data loaded, buffer no longer needed */
9626 		kfree(cap_buf);
9627 
9628 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
9629 			/* retry with a larger buffer */
9630 			buf_len = data_size;
9631 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
9632 			dev_info(&pf->pdev->dev,
9633 				 "capability discovery failed, err %s aq_err %s\n",
9634 				 i40e_stat_str(&pf->hw, err),
9635 				 i40e_aq_str(&pf->hw,
9636 					     pf->hw.aq.asq_last_status));
9637 			return -ENODEV;
9638 		}
9639 	} while (err);
9640 
9641 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
9642 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
9643 			dev_info(&pf->pdev->dev,
9644 				 "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",
9645 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
9646 				 pf->hw.func_caps.num_msix_vectors,
9647 				 pf->hw.func_caps.num_msix_vectors_vf,
9648 				 pf->hw.func_caps.fd_filters_guaranteed,
9649 				 pf->hw.func_caps.fd_filters_best_effort,
9650 				 pf->hw.func_caps.num_tx_qp,
9651 				 pf->hw.func_caps.num_vsis);
9652 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
9653 			dev_info(&pf->pdev->dev,
9654 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
9655 				 pf->hw.dev_caps.switch_mode,
9656 				 pf->hw.dev_caps.valid_functions);
9657 			dev_info(&pf->pdev->dev,
9658 				 "SR-IOV=%d, num_vfs for all function=%u\n",
9659 				 pf->hw.dev_caps.sr_iov_1_1,
9660 				 pf->hw.dev_caps.num_vfs);
9661 			dev_info(&pf->pdev->dev,
9662 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
9663 				 pf->hw.dev_caps.num_vsis,
9664 				 pf->hw.dev_caps.num_rx_qp,
9665 				 pf->hw.dev_caps.num_tx_qp);
9666 		}
9667 	}
9668 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
9669 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
9670 		       + pf->hw.func_caps.num_vfs)
9671 		if (pf->hw.revision_id == 0 &&
9672 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
9673 			dev_info(&pf->pdev->dev,
9674 				 "got num_vsis %d, setting num_vsis to %d\n",
9675 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
9676 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
9677 		}
9678 	}
9679 	return 0;
9680 }
9681 
9682 static int i40e_vsi_clear(struct i40e_vsi *vsi);
9683 
9684 /**
9685  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
9686  * @pf: board private structure
9687  **/
9688 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
9689 {
9690 	struct i40e_vsi *vsi;
9691 
9692 	/* quick workaround for an NVM issue that leaves a critical register
9693 	 * uninitialized
9694 	 */
9695 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
9696 		static const u32 hkey[] = {
9697 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
9698 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
9699 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
9700 			0x95b3a76d};
9701 		int i;
9702 
9703 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
9704 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
9705 	}
9706 
9707 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
9708 		return;
9709 
9710 	/* find existing VSI and see if it needs configuring */
9711 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9712 
9713 	/* create a new VSI if none exists */
9714 	if (!vsi) {
9715 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
9716 				     pf->vsi[pf->lan_vsi]->seid, 0);
9717 		if (!vsi) {
9718 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
9719 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
9720 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
9721 			return;
9722 		}
9723 	}
9724 
9725 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
9726 }
9727 
9728 /**
9729  * i40e_fdir_teardown - release the Flow Director resources
9730  * @pf: board private structure
9731  **/
9732 static void i40e_fdir_teardown(struct i40e_pf *pf)
9733 {
9734 	struct i40e_vsi *vsi;
9735 
9736 	i40e_fdir_filter_exit(pf);
9737 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9738 	if (vsi)
9739 		i40e_vsi_release(vsi);
9740 }
9741 
9742 /**
9743  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
9744  * @vsi: PF main vsi
9745  * @seid: seid of main or channel VSIs
9746  *
9747  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
9748  * existed before reset
9749  **/
9750 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
9751 {
9752 	struct i40e_cloud_filter *cfilter;
9753 	struct i40e_pf *pf = vsi->back;
9754 	struct hlist_node *node;
9755 	i40e_status ret;
9756 
9757 	/* Add cloud filters back if they exist */
9758 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
9759 				  cloud_node) {
9760 		if (cfilter->seid != seid)
9761 			continue;
9762 
9763 		if (cfilter->dst_port)
9764 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
9765 								true);
9766 		else
9767 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
9768 
9769 		if (ret) {
9770 			dev_dbg(&pf->pdev->dev,
9771 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
9772 				i40e_stat_str(&pf->hw, ret),
9773 				i40e_aq_str(&pf->hw,
9774 					    pf->hw.aq.asq_last_status));
9775 			return ret;
9776 		}
9777 	}
9778 	return 0;
9779 }
9780 
9781 /**
9782  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
9783  * @vsi: PF main vsi
9784  *
9785  * Rebuilds channel VSIs if they existed before reset
9786  **/
9787 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
9788 {
9789 	struct i40e_channel *ch, *ch_tmp;
9790 	i40e_status ret;
9791 
9792 	if (list_empty(&vsi->ch_list))
9793 		return 0;
9794 
9795 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
9796 		if (!ch->initialized)
9797 			break;
9798 		/* Proceed with creation of channel (VMDq2) VSI */
9799 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
9800 		if (ret) {
9801 			dev_info(&vsi->back->pdev->dev,
9802 				 "failed to rebuild channels using uplink_seid %u\n",
9803 				 vsi->uplink_seid);
9804 			return ret;
9805 		}
9806 		/* Reconfigure TX queues using QTX_CTL register */
9807 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
9808 		if (ret) {
9809 			dev_info(&vsi->back->pdev->dev,
9810 				 "failed to configure TX rings for channel %u\n",
9811 				 ch->seid);
9812 			return ret;
9813 		}
9814 		/* update 'next_base_queue' */
9815 		vsi->next_base_queue = vsi->next_base_queue +
9816 							ch->num_queue_pairs;
9817 		if (ch->max_tx_rate) {
9818 			u64 credits = ch->max_tx_rate;
9819 
9820 			if (i40e_set_bw_limit(vsi, ch->seid,
9821 					      ch->max_tx_rate))
9822 				return -EINVAL;
9823 
9824 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
9825 			dev_dbg(&vsi->back->pdev->dev,
9826 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9827 				ch->max_tx_rate,
9828 				credits,
9829 				ch->seid);
9830 		}
9831 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
9832 		if (ret) {
9833 			dev_dbg(&vsi->back->pdev->dev,
9834 				"Failed to rebuild cloud filters for channel VSI %u\n",
9835 				ch->seid);
9836 			return ret;
9837 		}
9838 	}
9839 	return 0;
9840 }
9841 
9842 /**
9843  * i40e_prep_for_reset - prep for the core to reset
9844  * @pf: board private structure
9845  * @lock_acquired: indicates whether or not the lock has been acquired
9846  * before this function was called.
9847  *
9848  * Close up the VFs and other things in prep for PF Reset.
9849   **/
9850 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
9851 {
9852 	struct i40e_hw *hw = &pf->hw;
9853 	i40e_status ret = 0;
9854 	u32 v;
9855 
9856 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
9857 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
9858 		return;
9859 	if (i40e_check_asq_alive(&pf->hw))
9860 		i40e_vc_notify_reset(pf);
9861 
9862 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
9863 
9864 	/* quiesce the VSIs and their queues that are not already DOWN */
9865 	/* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
9866 	if (!lock_acquired)
9867 		rtnl_lock();
9868 	i40e_pf_quiesce_all_vsi(pf);
9869 	if (!lock_acquired)
9870 		rtnl_unlock();
9871 
9872 	for (v = 0; v < pf->num_alloc_vsi; v++) {
9873 		if (pf->vsi[v])
9874 			pf->vsi[v]->seid = 0;
9875 	}
9876 
9877 	i40e_shutdown_adminq(&pf->hw);
9878 
9879 	/* call shutdown HMC */
9880 	if (hw->hmc.hmc_obj) {
9881 		ret = i40e_shutdown_lan_hmc(hw);
9882 		if (ret)
9883 			dev_warn(&pf->pdev->dev,
9884 				 "shutdown_lan_hmc failed: %d\n", ret);
9885 	}
9886 
9887 	/* Save the current PTP time so that we can restore the time after the
9888 	 * reset completes.
9889 	 */
9890 	i40e_ptp_save_hw_time(pf);
9891 }
9892 
9893 /**
9894  * i40e_send_version - update firmware with driver version
9895  * @pf: PF struct
9896  */
9897 static void i40e_send_version(struct i40e_pf *pf)
9898 {
9899 	struct i40e_driver_version dv;
9900 
9901 	dv.major_version = 0xff;
9902 	dv.minor_version = 0xff;
9903 	dv.build_version = 0xff;
9904 	dv.subbuild_version = 0;
9905 	strlcpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
9906 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
9907 }
9908 
9909 /**
9910  * i40e_get_oem_version - get OEM specific version information
9911  * @hw: pointer to the hardware structure
9912  **/
9913 static void i40e_get_oem_version(struct i40e_hw *hw)
9914 {
9915 	u16 block_offset = 0xffff;
9916 	u16 block_length = 0;
9917 	u16 capabilities = 0;
9918 	u16 gen_snap = 0;
9919 	u16 release = 0;
9920 
9921 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
9922 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
9923 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
9924 #define I40E_NVM_OEM_GEN_OFFSET			0x02
9925 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
9926 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
9927 #define I40E_NVM_OEM_LENGTH			3
9928 
9929 	/* Check if pointer to OEM version block is valid. */
9930 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
9931 	if (block_offset == 0xffff)
9932 		return;
9933 
9934 	/* Check if OEM version block has correct length. */
9935 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
9936 			   &block_length);
9937 	if (block_length < I40E_NVM_OEM_LENGTH)
9938 		return;
9939 
9940 	/* Check if OEM version format is as expected. */
9941 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
9942 			   &capabilities);
9943 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
9944 		return;
9945 
9946 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
9947 			   &gen_snap);
9948 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
9949 			   &release);
9950 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
9951 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
9952 }
9953 
9954 /**
9955  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
9956  * @pf: board private structure
9957  **/
9958 static int i40e_reset(struct i40e_pf *pf)
9959 {
9960 	struct i40e_hw *hw = &pf->hw;
9961 	i40e_status ret;
9962 
9963 	ret = i40e_pf_reset(hw);
9964 	if (ret) {
9965 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
9966 		set_bit(__I40E_RESET_FAILED, pf->state);
9967 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
9968 	} else {
9969 		pf->pfr_count++;
9970 	}
9971 	return ret;
9972 }
9973 
9974 /**
9975  * i40e_rebuild - rebuild using a saved config
9976  * @pf: board private structure
9977  * @reinit: if the Main VSI needs to re-initialized.
9978  * @lock_acquired: indicates whether or not the lock has been acquired
9979  * before this function was called.
9980  **/
9981 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
9982 {
9983 	int old_recovery_mode_bit = test_bit(__I40E_RECOVERY_MODE, pf->state);
9984 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9985 	struct i40e_hw *hw = &pf->hw;
9986 	u8 set_fc_aq_fail = 0;
9987 	i40e_status ret;
9988 	u32 val;
9989 	int v;
9990 
9991 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
9992 	    i40e_check_recovery_mode(pf)) {
9993 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
9994 	}
9995 
9996 	if (test_bit(__I40E_DOWN, pf->state) &&
9997 	    !test_bit(__I40E_RECOVERY_MODE, pf->state) &&
9998 	    !old_recovery_mode_bit)
9999 		goto clear_recovery;
10000 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10001 
10002 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10003 	ret = i40e_init_adminq(&pf->hw);
10004 	if (ret) {
10005 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10006 			 i40e_stat_str(&pf->hw, ret),
10007 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10008 		goto clear_recovery;
10009 	}
10010 	i40e_get_oem_version(&pf->hw);
10011 
10012 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10013 	    ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) ||
10014 	     hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) {
10015 		/* The following delay is necessary for 4.33 firmware and older
10016 		 * to recover after EMP reset. 200 ms should suffice but we
10017 		 * put here 300 ms to be sure that FW is ready to operate
10018 		 * after reset.
10019 		 */
10020 		mdelay(300);
10021 	}
10022 
10023 	/* re-verify the eeprom if we just had an EMP reset */
10024 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10025 		i40e_verify_eeprom(pf);
10026 
10027 	/* if we are going out of or into recovery mode we have to act
10028 	 * accordingly with regard to resources initialization
10029 	 * and deinitialization
10030 	 */
10031 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) ||
10032 	    old_recovery_mode_bit) {
10033 		if (i40e_get_capabilities(pf,
10034 					  i40e_aqc_opc_list_func_capabilities))
10035 			goto end_unlock;
10036 
10037 		if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10038 			/* we're staying in recovery mode so we'll reinitialize
10039 			 * misc vector here
10040 			 */
10041 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10042 				goto end_unlock;
10043 		} else {
10044 			if (!lock_acquired)
10045 				rtnl_lock();
10046 			/* we're going out of recovery mode so we'll free
10047 			 * the IRQ allocated specifically for recovery mode
10048 			 * and restore the interrupt scheme
10049 			 */
10050 			free_irq(pf->pdev->irq, pf);
10051 			i40e_clear_interrupt_scheme(pf);
10052 			if (i40e_restore_interrupt_scheme(pf))
10053 				goto end_unlock;
10054 		}
10055 
10056 		/* tell the firmware that we're starting */
10057 		i40e_send_version(pf);
10058 
10059 		/* bail out in case recovery mode was detected, as there is
10060 		 * no need for further configuration.
10061 		 */
10062 		goto end_unlock;
10063 	}
10064 
10065 	i40e_clear_pxe_mode(hw);
10066 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10067 	if (ret)
10068 		goto end_core_reset;
10069 
10070 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10071 				hw->func_caps.num_rx_qp, 0, 0);
10072 	if (ret) {
10073 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10074 		goto end_core_reset;
10075 	}
10076 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10077 	if (ret) {
10078 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10079 		goto end_core_reset;
10080 	}
10081 
10082 	/* Enable FW to write a default DCB config on link-up */
10083 	i40e_aq_set_dcb_parameters(hw, true, NULL);
10084 
10085 #ifdef CONFIG_I40E_DCB
10086 	ret = i40e_init_pf_dcb(pf);
10087 	if (ret) {
10088 		dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
10089 		pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10090 		/* Continue without DCB enabled */
10091 	}
10092 #endif /* CONFIG_I40E_DCB */
10093 	/* do basic switch setup */
10094 	if (!lock_acquired)
10095 		rtnl_lock();
10096 	ret = i40e_setup_pf_switch(pf, reinit);
10097 	if (ret)
10098 		goto end_unlock;
10099 
10100 	/* The driver only wants link up/down and module qualification
10101 	 * reports from firmware.  Note the negative logic.
10102 	 */
10103 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10104 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10105 					 I40E_AQ_EVENT_MEDIA_NA |
10106 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10107 	if (ret)
10108 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10109 			 i40e_stat_str(&pf->hw, ret),
10110 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10111 
10112 	/* make sure our flow control settings are restored */
10113 	ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
10114 	if (ret)
10115 		dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
10116 			i40e_stat_str(&pf->hw, ret),
10117 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10118 
10119 	/* Rebuild the VSIs and VEBs that existed before reset.
10120 	 * They are still in our local switch element arrays, so only
10121 	 * need to rebuild the switch model in the HW.
10122 	 *
10123 	 * If there were VEBs but the reconstitution failed, we'll try
10124 	 * try to recover minimal use by getting the basic PF VSI working.
10125 	 */
10126 	if (vsi->uplink_seid != pf->mac_seid) {
10127 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10128 		/* find the one VEB connected to the MAC, and find orphans */
10129 		for (v = 0; v < I40E_MAX_VEB; v++) {
10130 			if (!pf->veb[v])
10131 				continue;
10132 
10133 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10134 			    pf->veb[v]->uplink_seid == 0) {
10135 				ret = i40e_reconstitute_veb(pf->veb[v]);
10136 
10137 				if (!ret)
10138 					continue;
10139 
10140 				/* If Main VEB failed, we're in deep doodoo,
10141 				 * so give up rebuilding the switch and set up
10142 				 * for minimal rebuild of PF VSI.
10143 				 * If orphan failed, we'll report the error
10144 				 * but try to keep going.
10145 				 */
10146 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10147 					dev_info(&pf->pdev->dev,
10148 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10149 						 ret);
10150 					vsi->uplink_seid = pf->mac_seid;
10151 					break;
10152 				} else if (pf->veb[v]->uplink_seid == 0) {
10153 					dev_info(&pf->pdev->dev,
10154 						 "rebuild of orphan VEB failed: %d\n",
10155 						 ret);
10156 				}
10157 			}
10158 		}
10159 	}
10160 
10161 	if (vsi->uplink_seid == pf->mac_seid) {
10162 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10163 		/* no VEB, so rebuild only the Main VSI */
10164 		ret = i40e_add_vsi(vsi);
10165 		if (ret) {
10166 			dev_info(&pf->pdev->dev,
10167 				 "rebuild of Main VSI failed: %d\n", ret);
10168 			goto end_unlock;
10169 		}
10170 	}
10171 
10172 	if (vsi->mqprio_qopt.max_rate[0]) {
10173 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
10174 		u64 credits = 0;
10175 
10176 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
10177 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10178 		if (ret)
10179 			goto end_unlock;
10180 
10181 		credits = max_tx_rate;
10182 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10183 		dev_dbg(&vsi->back->pdev->dev,
10184 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10185 			max_tx_rate,
10186 			credits,
10187 			vsi->seid);
10188 	}
10189 
10190 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10191 	if (ret)
10192 		goto end_unlock;
10193 
10194 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
10195 	 * for this main VSI if they exist
10196 	 */
10197 	ret = i40e_rebuild_channels(vsi);
10198 	if (ret)
10199 		goto end_unlock;
10200 
10201 	/* Reconfigure hardware for allowing smaller MSS in the case
10202 	 * of TSO, so that we avoid the MDD being fired and causing
10203 	 * a reset in the case of small MSS+TSO.
10204 	 */
10205 #define I40E_REG_MSS          0x000E64DC
10206 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
10207 #define I40E_64BYTE_MSS       0x400000
10208 	val = rd32(hw, I40E_REG_MSS);
10209 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10210 		val &= ~I40E_REG_MSS_MIN_MASK;
10211 		val |= I40E_64BYTE_MSS;
10212 		wr32(hw, I40E_REG_MSS, val);
10213 	}
10214 
10215 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
10216 		msleep(75);
10217 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10218 		if (ret)
10219 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10220 				 i40e_stat_str(&pf->hw, ret),
10221 				 i40e_aq_str(&pf->hw,
10222 					     pf->hw.aq.asq_last_status));
10223 	}
10224 	/* reinit the misc interrupt */
10225 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10226 		ret = i40e_setup_misc_vector(pf);
10227 
10228 	/* Add a filter to drop all Flow control frames from any VSI from being
10229 	 * transmitted. By doing so we stop a malicious VF from sending out
10230 	 * PAUSE or PFC frames and potentially controlling traffic for other
10231 	 * PF/VF VSIs.
10232 	 * The FW can still send Flow control frames if enabled.
10233 	 */
10234 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
10235 						       pf->main_vsi_seid);
10236 
10237 	/* restart the VSIs that were rebuilt and running before the reset */
10238 	i40e_pf_unquiesce_all_vsi(pf);
10239 
10240 	/* Release the RTNL lock before we start resetting VFs */
10241 	if (!lock_acquired)
10242 		rtnl_unlock();
10243 
10244 	/* Restore promiscuous settings */
10245 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
10246 	if (ret)
10247 		dev_warn(&pf->pdev->dev,
10248 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
10249 			 pf->cur_promisc ? "on" : "off",
10250 			 i40e_stat_str(&pf->hw, ret),
10251 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10252 
10253 	i40e_reset_all_vfs(pf, true);
10254 
10255 	/* tell the firmware that we're starting */
10256 	i40e_send_version(pf);
10257 
10258 	/* We've already released the lock, so don't do it again */
10259 	goto end_core_reset;
10260 
10261 end_unlock:
10262 	if (!lock_acquired)
10263 		rtnl_unlock();
10264 end_core_reset:
10265 	clear_bit(__I40E_RESET_FAILED, pf->state);
10266 clear_recovery:
10267 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10268 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
10269 }
10270 
10271 /**
10272  * i40e_reset_and_rebuild - reset and rebuild using a saved config
10273  * @pf: board private structure
10274  * @reinit: if the Main VSI needs to re-initialized.
10275  * @lock_acquired: indicates whether or not the lock has been acquired
10276  * before this function was called.
10277  **/
10278 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
10279 				   bool lock_acquired)
10280 {
10281 	int ret;
10282 	/* Now we wait for GRST to settle out.
10283 	 * We don't have to delete the VEBs or VSIs from the hw switch
10284 	 * because the reset will make them disappear.
10285 	 */
10286 	ret = i40e_reset(pf);
10287 	if (!ret)
10288 		i40e_rebuild(pf, reinit, lock_acquired);
10289 }
10290 
10291 /**
10292  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
10293  * @pf: board private structure
10294  *
10295  * Close up the VFs and other things in prep for a Core Reset,
10296  * then get ready to rebuild the world.
10297  * @lock_acquired: indicates whether or not the lock has been acquired
10298  * before this function was called.
10299  **/
10300 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
10301 {
10302 	i40e_prep_for_reset(pf, lock_acquired);
10303 	i40e_reset_and_rebuild(pf, false, lock_acquired);
10304 }
10305 
10306 /**
10307  * i40e_handle_mdd_event
10308  * @pf: pointer to the PF structure
10309  *
10310  * Called from the MDD irq handler to identify possibly malicious vfs
10311  **/
10312 static void i40e_handle_mdd_event(struct i40e_pf *pf)
10313 {
10314 	struct i40e_hw *hw = &pf->hw;
10315 	bool mdd_detected = false;
10316 	struct i40e_vf *vf;
10317 	u32 reg;
10318 	int i;
10319 
10320 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
10321 		return;
10322 
10323 	/* find what triggered the MDD event */
10324 	reg = rd32(hw, I40E_GL_MDET_TX);
10325 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
10326 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
10327 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
10328 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
10329 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
10330 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
10331 				I40E_GL_MDET_TX_EVENT_SHIFT;
10332 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
10333 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
10334 				pf->hw.func_caps.base_queue;
10335 		if (netif_msg_tx_err(pf))
10336 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
10337 				 event, queue, pf_num, vf_num);
10338 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
10339 		mdd_detected = true;
10340 	}
10341 	reg = rd32(hw, I40E_GL_MDET_RX);
10342 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
10343 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
10344 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
10345 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
10346 				I40E_GL_MDET_RX_EVENT_SHIFT;
10347 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
10348 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
10349 				pf->hw.func_caps.base_queue;
10350 		if (netif_msg_rx_err(pf))
10351 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
10352 				 event, queue, func);
10353 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
10354 		mdd_detected = true;
10355 	}
10356 
10357 	if (mdd_detected) {
10358 		reg = rd32(hw, I40E_PF_MDET_TX);
10359 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
10360 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
10361 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
10362 		}
10363 		reg = rd32(hw, I40E_PF_MDET_RX);
10364 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
10365 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
10366 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
10367 		}
10368 	}
10369 
10370 	/* see if one of the VFs needs its hand slapped */
10371 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
10372 		vf = &(pf->vf[i]);
10373 		reg = rd32(hw, I40E_VP_MDET_TX(i));
10374 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
10375 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
10376 			vf->num_mdd_events++;
10377 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
10378 				 i);
10379 			dev_info(&pf->pdev->dev,
10380 				 "Use PF Control I/F to re-enable the VF\n");
10381 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10382 		}
10383 
10384 		reg = rd32(hw, I40E_VP_MDET_RX(i));
10385 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
10386 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
10387 			vf->num_mdd_events++;
10388 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
10389 				 i);
10390 			dev_info(&pf->pdev->dev,
10391 				 "Use PF Control I/F to re-enable the VF\n");
10392 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10393 		}
10394 	}
10395 
10396 	/* re-enable mdd interrupt cause */
10397 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
10398 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
10399 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
10400 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
10401 	i40e_flush(hw);
10402 }
10403 
10404 /**
10405  * i40e_service_task - Run the driver's async subtasks
10406  * @work: pointer to work_struct containing our data
10407  **/
10408 static void i40e_service_task(struct work_struct *work)
10409 {
10410 	struct i40e_pf *pf = container_of(work,
10411 					  struct i40e_pf,
10412 					  service_task);
10413 	unsigned long start_time = jiffies;
10414 
10415 	/* don't bother with service tasks if a reset is in progress */
10416 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
10417 	    test_bit(__I40E_SUSPENDED, pf->state))
10418 		return;
10419 
10420 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
10421 		return;
10422 
10423 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10424 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
10425 		i40e_sync_filters_subtask(pf);
10426 		i40e_reset_subtask(pf);
10427 		i40e_handle_mdd_event(pf);
10428 		i40e_vc_process_vflr_event(pf);
10429 		i40e_watchdog_subtask(pf);
10430 		i40e_fdir_reinit_subtask(pf);
10431 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
10432 			/* Client subtask will reopen next time through. */
10433 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
10434 							   true);
10435 		} else {
10436 			i40e_client_subtask(pf);
10437 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
10438 					       pf->state))
10439 				i40e_notify_client_of_l2_param_changes(
10440 								pf->vsi[pf->lan_vsi]);
10441 		}
10442 		i40e_sync_filters_subtask(pf);
10443 	} else {
10444 		i40e_reset_subtask(pf);
10445 	}
10446 
10447 	i40e_clean_adminq_subtask(pf);
10448 
10449 	/* flush memory to make sure state is correct before next watchdog */
10450 	smp_mb__before_atomic();
10451 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
10452 
10453 	/* If the tasks have taken longer than one timer cycle or there
10454 	 * is more work to be done, reschedule the service task now
10455 	 * rather than wait for the timer to tick again.
10456 	 */
10457 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
10458 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
10459 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
10460 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
10461 		i40e_service_event_schedule(pf);
10462 }
10463 
10464 /**
10465  * i40e_service_timer - timer callback
10466  * @t: timer list pointer
10467  **/
10468 static void i40e_service_timer(struct timer_list *t)
10469 {
10470 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
10471 
10472 	mod_timer(&pf->service_timer,
10473 		  round_jiffies(jiffies + pf->service_timer_period));
10474 	i40e_service_event_schedule(pf);
10475 }
10476 
10477 /**
10478  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
10479  * @vsi: the VSI being configured
10480  **/
10481 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
10482 {
10483 	struct i40e_pf *pf = vsi->back;
10484 
10485 	switch (vsi->type) {
10486 	case I40E_VSI_MAIN:
10487 		vsi->alloc_queue_pairs = pf->num_lan_qps;
10488 		if (!vsi->num_tx_desc)
10489 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10490 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10491 		if (!vsi->num_rx_desc)
10492 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10493 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10494 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10495 			vsi->num_q_vectors = pf->num_lan_msix;
10496 		else
10497 			vsi->num_q_vectors = 1;
10498 
10499 		break;
10500 
10501 	case I40E_VSI_FDIR:
10502 		vsi->alloc_queue_pairs = 1;
10503 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
10504 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
10505 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
10506 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
10507 		vsi->num_q_vectors = pf->num_fdsb_msix;
10508 		break;
10509 
10510 	case I40E_VSI_VMDQ2:
10511 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
10512 		if (!vsi->num_tx_desc)
10513 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10514 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10515 		if (!vsi->num_rx_desc)
10516 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10517 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10518 		vsi->num_q_vectors = pf->num_vmdq_msix;
10519 		break;
10520 
10521 	case I40E_VSI_SRIOV:
10522 		vsi->alloc_queue_pairs = pf->num_vf_qps;
10523 		if (!vsi->num_tx_desc)
10524 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10525 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10526 		if (!vsi->num_rx_desc)
10527 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10528 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10529 		break;
10530 
10531 	default:
10532 		WARN_ON(1);
10533 		return -ENODATA;
10534 	}
10535 
10536 	return 0;
10537 }
10538 
10539 /**
10540  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
10541  * @vsi: VSI pointer
10542  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
10543  *
10544  * On error: returns error code (negative)
10545  * On success: returns 0
10546  **/
10547 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
10548 {
10549 	struct i40e_ring **next_rings;
10550 	int size;
10551 	int ret = 0;
10552 
10553 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
10554 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
10555 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
10556 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
10557 	if (!vsi->tx_rings)
10558 		return -ENOMEM;
10559 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
10560 	if (i40e_enabled_xdp_vsi(vsi)) {
10561 		vsi->xdp_rings = next_rings;
10562 		next_rings += vsi->alloc_queue_pairs;
10563 	}
10564 	vsi->rx_rings = next_rings;
10565 
10566 	if (alloc_qvectors) {
10567 		/* allocate memory for q_vector pointers */
10568 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
10569 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
10570 		if (!vsi->q_vectors) {
10571 			ret = -ENOMEM;
10572 			goto err_vectors;
10573 		}
10574 	}
10575 	return ret;
10576 
10577 err_vectors:
10578 	kfree(vsi->tx_rings);
10579 	return ret;
10580 }
10581 
10582 /**
10583  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
10584  * @pf: board private structure
10585  * @type: type of VSI
10586  *
10587  * On error: returns error code (negative)
10588  * On success: returns vsi index in PF (positive)
10589  **/
10590 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
10591 {
10592 	int ret = -ENODEV;
10593 	struct i40e_vsi *vsi;
10594 	int vsi_idx;
10595 	int i;
10596 
10597 	/* Need to protect the allocation of the VSIs at the PF level */
10598 	mutex_lock(&pf->switch_mutex);
10599 
10600 	/* VSI list may be fragmented if VSI creation/destruction has
10601 	 * been happening.  We can afford to do a quick scan to look
10602 	 * for any free VSIs in the list.
10603 	 *
10604 	 * find next empty vsi slot, looping back around if necessary
10605 	 */
10606 	i = pf->next_vsi;
10607 	while (i < pf->num_alloc_vsi && pf->vsi[i])
10608 		i++;
10609 	if (i >= pf->num_alloc_vsi) {
10610 		i = 0;
10611 		while (i < pf->next_vsi && pf->vsi[i])
10612 			i++;
10613 	}
10614 
10615 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
10616 		vsi_idx = i;             /* Found one! */
10617 	} else {
10618 		ret = -ENODEV;
10619 		goto unlock_pf;  /* out of VSI slots! */
10620 	}
10621 	pf->next_vsi = ++i;
10622 
10623 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
10624 	if (!vsi) {
10625 		ret = -ENOMEM;
10626 		goto unlock_pf;
10627 	}
10628 	vsi->type = type;
10629 	vsi->back = pf;
10630 	set_bit(__I40E_VSI_DOWN, vsi->state);
10631 	vsi->flags = 0;
10632 	vsi->idx = vsi_idx;
10633 	vsi->int_rate_limit = 0;
10634 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
10635 				pf->rss_table_size : 64;
10636 	vsi->netdev_registered = false;
10637 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
10638 	hash_init(vsi->mac_filter_hash);
10639 	vsi->irqs_ready = false;
10640 
10641 	if (type == I40E_VSI_MAIN) {
10642 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
10643 		if (!vsi->af_xdp_zc_qps)
10644 			goto err_rings;
10645 	}
10646 
10647 	ret = i40e_set_num_rings_in_vsi(vsi);
10648 	if (ret)
10649 		goto err_rings;
10650 
10651 	ret = i40e_vsi_alloc_arrays(vsi, true);
10652 	if (ret)
10653 		goto err_rings;
10654 
10655 	/* Setup default MSIX irq handler for VSI */
10656 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
10657 
10658 	/* Initialize VSI lock */
10659 	spin_lock_init(&vsi->mac_filter_hash_lock);
10660 	pf->vsi[vsi_idx] = vsi;
10661 	ret = vsi_idx;
10662 	goto unlock_pf;
10663 
10664 err_rings:
10665 	bitmap_free(vsi->af_xdp_zc_qps);
10666 	pf->next_vsi = i - 1;
10667 	kfree(vsi);
10668 unlock_pf:
10669 	mutex_unlock(&pf->switch_mutex);
10670 	return ret;
10671 }
10672 
10673 /**
10674  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
10675  * @vsi: VSI pointer
10676  * @free_qvectors: a bool to specify if q_vectors need to be freed.
10677  *
10678  * On error: returns error code (negative)
10679  * On success: returns 0
10680  **/
10681 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
10682 {
10683 	/* free the ring and vector containers */
10684 	if (free_qvectors) {
10685 		kfree(vsi->q_vectors);
10686 		vsi->q_vectors = NULL;
10687 	}
10688 	kfree(vsi->tx_rings);
10689 	vsi->tx_rings = NULL;
10690 	vsi->rx_rings = NULL;
10691 	vsi->xdp_rings = NULL;
10692 }
10693 
10694 /**
10695  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
10696  * and lookup table
10697  * @vsi: Pointer to VSI structure
10698  */
10699 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
10700 {
10701 	if (!vsi)
10702 		return;
10703 
10704 	kfree(vsi->rss_hkey_user);
10705 	vsi->rss_hkey_user = NULL;
10706 
10707 	kfree(vsi->rss_lut_user);
10708 	vsi->rss_lut_user = NULL;
10709 }
10710 
10711 /**
10712  * i40e_vsi_clear - Deallocate the VSI provided
10713  * @vsi: the VSI being un-configured
10714  **/
10715 static int i40e_vsi_clear(struct i40e_vsi *vsi)
10716 {
10717 	struct i40e_pf *pf;
10718 
10719 	if (!vsi)
10720 		return 0;
10721 
10722 	if (!vsi->back)
10723 		goto free_vsi;
10724 	pf = vsi->back;
10725 
10726 	mutex_lock(&pf->switch_mutex);
10727 	if (!pf->vsi[vsi->idx]) {
10728 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
10729 			vsi->idx, vsi->idx, vsi->type);
10730 		goto unlock_vsi;
10731 	}
10732 
10733 	if (pf->vsi[vsi->idx] != vsi) {
10734 		dev_err(&pf->pdev->dev,
10735 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
10736 			pf->vsi[vsi->idx]->idx,
10737 			pf->vsi[vsi->idx]->type,
10738 			vsi->idx, vsi->type);
10739 		goto unlock_vsi;
10740 	}
10741 
10742 	/* updates the PF for this cleared vsi */
10743 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
10744 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
10745 
10746 	bitmap_free(vsi->af_xdp_zc_qps);
10747 	i40e_vsi_free_arrays(vsi, true);
10748 	i40e_clear_rss_config_user(vsi);
10749 
10750 	pf->vsi[vsi->idx] = NULL;
10751 	if (vsi->idx < pf->next_vsi)
10752 		pf->next_vsi = vsi->idx;
10753 
10754 unlock_vsi:
10755 	mutex_unlock(&pf->switch_mutex);
10756 free_vsi:
10757 	kfree(vsi);
10758 
10759 	return 0;
10760 }
10761 
10762 /**
10763  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
10764  * @vsi: the VSI being cleaned
10765  **/
10766 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
10767 {
10768 	int i;
10769 
10770 	if (vsi->tx_rings && vsi->tx_rings[0]) {
10771 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10772 			kfree_rcu(vsi->tx_rings[i], rcu);
10773 			WRITE_ONCE(vsi->tx_rings[i], NULL);
10774 			WRITE_ONCE(vsi->rx_rings[i], NULL);
10775 			if (vsi->xdp_rings)
10776 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
10777 		}
10778 	}
10779 }
10780 
10781 /**
10782  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
10783  * @vsi: the VSI being configured
10784  **/
10785 static int i40e_alloc_rings(struct i40e_vsi *vsi)
10786 {
10787 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
10788 	struct i40e_pf *pf = vsi->back;
10789 	struct i40e_ring *ring;
10790 
10791 	/* Set basic values in the rings to be used later during open() */
10792 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10793 		/* allocate space for both Tx and Rx in one shot */
10794 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
10795 		if (!ring)
10796 			goto err_out;
10797 
10798 		ring->queue_index = i;
10799 		ring->reg_idx = vsi->base_queue + i;
10800 		ring->ring_active = false;
10801 		ring->vsi = vsi;
10802 		ring->netdev = vsi->netdev;
10803 		ring->dev = &pf->pdev->dev;
10804 		ring->count = vsi->num_tx_desc;
10805 		ring->size = 0;
10806 		ring->dcb_tc = 0;
10807 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10808 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10809 		ring->itr_setting = pf->tx_itr_default;
10810 		WRITE_ONCE(vsi->tx_rings[i], ring++);
10811 
10812 		if (!i40e_enabled_xdp_vsi(vsi))
10813 			goto setup_rx;
10814 
10815 		ring->queue_index = vsi->alloc_queue_pairs + i;
10816 		ring->reg_idx = vsi->base_queue + ring->queue_index;
10817 		ring->ring_active = false;
10818 		ring->vsi = vsi;
10819 		ring->netdev = NULL;
10820 		ring->dev = &pf->pdev->dev;
10821 		ring->count = vsi->num_tx_desc;
10822 		ring->size = 0;
10823 		ring->dcb_tc = 0;
10824 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10825 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10826 		set_ring_xdp(ring);
10827 		ring->itr_setting = pf->tx_itr_default;
10828 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
10829 
10830 setup_rx:
10831 		ring->queue_index = i;
10832 		ring->reg_idx = vsi->base_queue + i;
10833 		ring->ring_active = false;
10834 		ring->vsi = vsi;
10835 		ring->netdev = vsi->netdev;
10836 		ring->dev = &pf->pdev->dev;
10837 		ring->count = vsi->num_rx_desc;
10838 		ring->size = 0;
10839 		ring->dcb_tc = 0;
10840 		ring->itr_setting = pf->rx_itr_default;
10841 		WRITE_ONCE(vsi->rx_rings[i], ring);
10842 	}
10843 
10844 	return 0;
10845 
10846 err_out:
10847 	i40e_vsi_clear_rings(vsi);
10848 	return -ENOMEM;
10849 }
10850 
10851 /**
10852  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
10853  * @pf: board private structure
10854  * @vectors: the number of MSI-X vectors to request
10855  *
10856  * Returns the number of vectors reserved, or error
10857  **/
10858 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
10859 {
10860 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
10861 					I40E_MIN_MSIX, vectors);
10862 	if (vectors < 0) {
10863 		dev_info(&pf->pdev->dev,
10864 			 "MSI-X vector reservation failed: %d\n", vectors);
10865 		vectors = 0;
10866 	}
10867 
10868 	return vectors;
10869 }
10870 
10871 /**
10872  * i40e_init_msix - Setup the MSIX capability
10873  * @pf: board private structure
10874  *
10875  * Work with the OS to set up the MSIX vectors needed.
10876  *
10877  * Returns the number of vectors reserved or negative on failure
10878  **/
10879 static int i40e_init_msix(struct i40e_pf *pf)
10880 {
10881 	struct i40e_hw *hw = &pf->hw;
10882 	int cpus, extra_vectors;
10883 	int vectors_left;
10884 	int v_budget, i;
10885 	int v_actual;
10886 	int iwarp_requested = 0;
10887 
10888 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
10889 		return -ENODEV;
10890 
10891 	/* The number of vectors we'll request will be comprised of:
10892 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
10893 	 *   - The number of LAN queue pairs
10894 	 *	- Queues being used for RSS.
10895 	 *		We don't need as many as max_rss_size vectors.
10896 	 *		use rss_size instead in the calculation since that
10897 	 *		is governed by number of cpus in the system.
10898 	 *	- assumes symmetric Tx/Rx pairing
10899 	 *   - The number of VMDq pairs
10900 	 *   - The CPU count within the NUMA node if iWARP is enabled
10901 	 * Once we count this up, try the request.
10902 	 *
10903 	 * If we can't get what we want, we'll simplify to nearly nothing
10904 	 * and try again.  If that still fails, we punt.
10905 	 */
10906 	vectors_left = hw->func_caps.num_msix_vectors;
10907 	v_budget = 0;
10908 
10909 	/* reserve one vector for miscellaneous handler */
10910 	if (vectors_left) {
10911 		v_budget++;
10912 		vectors_left--;
10913 	}
10914 
10915 	/* reserve some vectors for the main PF traffic queues. Initially we
10916 	 * only reserve at most 50% of the available vectors, in the case that
10917 	 * the number of online CPUs is large. This ensures that we can enable
10918 	 * extra features as well. Once we've enabled the other features, we
10919 	 * will use any remaining vectors to reach as close as we can to the
10920 	 * number of online CPUs.
10921 	 */
10922 	cpus = num_online_cpus();
10923 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
10924 	vectors_left -= pf->num_lan_msix;
10925 
10926 	/* reserve one vector for sideband flow director */
10927 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10928 		if (vectors_left) {
10929 			pf->num_fdsb_msix = 1;
10930 			v_budget++;
10931 			vectors_left--;
10932 		} else {
10933 			pf->num_fdsb_msix = 0;
10934 		}
10935 	}
10936 
10937 	/* can we reserve enough for iWARP? */
10938 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10939 		iwarp_requested = pf->num_iwarp_msix;
10940 
10941 		if (!vectors_left)
10942 			pf->num_iwarp_msix = 0;
10943 		else if (vectors_left < pf->num_iwarp_msix)
10944 			pf->num_iwarp_msix = 1;
10945 		v_budget += pf->num_iwarp_msix;
10946 		vectors_left -= pf->num_iwarp_msix;
10947 	}
10948 
10949 	/* any vectors left over go for VMDq support */
10950 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
10951 		if (!vectors_left) {
10952 			pf->num_vmdq_msix = 0;
10953 			pf->num_vmdq_qps = 0;
10954 		} else {
10955 			int vmdq_vecs_wanted =
10956 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
10957 			int vmdq_vecs =
10958 				min_t(int, vectors_left, vmdq_vecs_wanted);
10959 
10960 			/* if we're short on vectors for what's desired, we limit
10961 			 * the queues per vmdq.  If this is still more than are
10962 			 * available, the user will need to change the number of
10963 			 * queues/vectors used by the PF later with the ethtool
10964 			 * channels command
10965 			 */
10966 			if (vectors_left < vmdq_vecs_wanted) {
10967 				pf->num_vmdq_qps = 1;
10968 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
10969 				vmdq_vecs = min_t(int,
10970 						  vectors_left,
10971 						  vmdq_vecs_wanted);
10972 			}
10973 			pf->num_vmdq_msix = pf->num_vmdq_qps;
10974 
10975 			v_budget += vmdq_vecs;
10976 			vectors_left -= vmdq_vecs;
10977 		}
10978 	}
10979 
10980 	/* On systems with a large number of SMP cores, we previously limited
10981 	 * the number of vectors for num_lan_msix to be at most 50% of the
10982 	 * available vectors, to allow for other features. Now, we add back
10983 	 * the remaining vectors. However, we ensure that the total
10984 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
10985 	 * calculate the number of vectors we can add without going over the
10986 	 * cap of CPUs. For systems with a small number of CPUs this will be
10987 	 * zero.
10988 	 */
10989 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
10990 	pf->num_lan_msix += extra_vectors;
10991 	vectors_left -= extra_vectors;
10992 
10993 	WARN(vectors_left < 0,
10994 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
10995 
10996 	v_budget += pf->num_lan_msix;
10997 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
10998 				   GFP_KERNEL);
10999 	if (!pf->msix_entries)
11000 		return -ENOMEM;
11001 
11002 	for (i = 0; i < v_budget; i++)
11003 		pf->msix_entries[i].entry = i;
11004 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11005 
11006 	if (v_actual < I40E_MIN_MSIX) {
11007 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11008 		kfree(pf->msix_entries);
11009 		pf->msix_entries = NULL;
11010 		pci_disable_msix(pf->pdev);
11011 		return -ENODEV;
11012 
11013 	} else if (v_actual == I40E_MIN_MSIX) {
11014 		/* Adjust for minimal MSIX use */
11015 		pf->num_vmdq_vsis = 0;
11016 		pf->num_vmdq_qps = 0;
11017 		pf->num_lan_qps = 1;
11018 		pf->num_lan_msix = 1;
11019 
11020 	} else if (v_actual != v_budget) {
11021 		/* If we have limited resources, we will start with no vectors
11022 		 * for the special features and then allocate vectors to some
11023 		 * of these features based on the policy and at the end disable
11024 		 * the features that did not get any vectors.
11025 		 */
11026 		int vec;
11027 
11028 		dev_info(&pf->pdev->dev,
11029 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11030 			 v_actual, v_budget);
11031 		/* reserve the misc vector */
11032 		vec = v_actual - 1;
11033 
11034 		/* Scale vector usage down */
11035 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11036 		pf->num_vmdq_vsis = 1;
11037 		pf->num_vmdq_qps = 1;
11038 
11039 		/* partition out the remaining vectors */
11040 		switch (vec) {
11041 		case 2:
11042 			pf->num_lan_msix = 1;
11043 			break;
11044 		case 3:
11045 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11046 				pf->num_lan_msix = 1;
11047 				pf->num_iwarp_msix = 1;
11048 			} else {
11049 				pf->num_lan_msix = 2;
11050 			}
11051 			break;
11052 		default:
11053 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11054 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11055 						 iwarp_requested);
11056 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11057 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11058 			} else {
11059 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11060 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11061 			}
11062 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11063 				pf->num_fdsb_msix = 1;
11064 				vec--;
11065 			}
11066 			pf->num_lan_msix = min_t(int,
11067 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11068 							      pf->num_lan_msix);
11069 			pf->num_lan_qps = pf->num_lan_msix;
11070 			break;
11071 		}
11072 	}
11073 
11074 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11075 	    (pf->num_fdsb_msix == 0)) {
11076 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11077 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11078 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11079 	}
11080 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11081 	    (pf->num_vmdq_msix == 0)) {
11082 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11083 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11084 	}
11085 
11086 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11087 	    (pf->num_iwarp_msix == 0)) {
11088 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11089 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11090 	}
11091 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11092 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11093 		   pf->num_lan_msix,
11094 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11095 		   pf->num_fdsb_msix,
11096 		   pf->num_iwarp_msix);
11097 
11098 	return v_actual;
11099 }
11100 
11101 /**
11102  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11103  * @vsi: the VSI being configured
11104  * @v_idx: index of the vector in the vsi struct
11105  *
11106  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11107  **/
11108 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11109 {
11110 	struct i40e_q_vector *q_vector;
11111 
11112 	/* allocate q_vector */
11113 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11114 	if (!q_vector)
11115 		return -ENOMEM;
11116 
11117 	q_vector->vsi = vsi;
11118 	q_vector->v_idx = v_idx;
11119 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11120 
11121 	if (vsi->netdev)
11122 		netif_napi_add(vsi->netdev, &q_vector->napi,
11123 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
11124 
11125 	/* tie q_vector and vsi together */
11126 	vsi->q_vectors[v_idx] = q_vector;
11127 
11128 	return 0;
11129 }
11130 
11131 /**
11132  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11133  * @vsi: the VSI being configured
11134  *
11135  * We allocate one q_vector per queue interrupt.  If allocation fails we
11136  * return -ENOMEM.
11137  **/
11138 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11139 {
11140 	struct i40e_pf *pf = vsi->back;
11141 	int err, v_idx, num_q_vectors;
11142 
11143 	/* if not MSIX, give the one vector only to the LAN VSI */
11144 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11145 		num_q_vectors = vsi->num_q_vectors;
11146 	else if (vsi == pf->vsi[pf->lan_vsi])
11147 		num_q_vectors = 1;
11148 	else
11149 		return -EINVAL;
11150 
11151 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11152 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11153 		if (err)
11154 			goto err_out;
11155 	}
11156 
11157 	return 0;
11158 
11159 err_out:
11160 	while (v_idx--)
11161 		i40e_free_q_vector(vsi, v_idx);
11162 
11163 	return err;
11164 }
11165 
11166 /**
11167  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11168  * @pf: board private structure to initialize
11169  **/
11170 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11171 {
11172 	int vectors = 0;
11173 	ssize_t size;
11174 
11175 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11176 		vectors = i40e_init_msix(pf);
11177 		if (vectors < 0) {
11178 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11179 				       I40E_FLAG_IWARP_ENABLED	|
11180 				       I40E_FLAG_RSS_ENABLED	|
11181 				       I40E_FLAG_DCB_CAPABLE	|
11182 				       I40E_FLAG_DCB_ENABLED	|
11183 				       I40E_FLAG_SRIOV_ENABLED	|
11184 				       I40E_FLAG_FD_SB_ENABLED	|
11185 				       I40E_FLAG_FD_ATR_ENABLED	|
11186 				       I40E_FLAG_VMDQ_ENABLED);
11187 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11188 
11189 			/* rework the queue expectations without MSIX */
11190 			i40e_determine_queue_usage(pf);
11191 		}
11192 	}
11193 
11194 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11195 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
11196 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
11197 		vectors = pci_enable_msi(pf->pdev);
11198 		if (vectors < 0) {
11199 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
11200 				 vectors);
11201 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
11202 		}
11203 		vectors = 1;  /* one MSI or Legacy vector */
11204 	}
11205 
11206 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
11207 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
11208 
11209 	/* set up vector assignment tracking */
11210 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
11211 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
11212 	if (!pf->irq_pile)
11213 		return -ENOMEM;
11214 
11215 	pf->irq_pile->num_entries = vectors;
11216 	pf->irq_pile->search_hint = 0;
11217 
11218 	/* track first vector for misc interrupts, ignore return */
11219 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
11220 
11221 	return 0;
11222 }
11223 
11224 /**
11225  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
11226  * @pf: private board data structure
11227  *
11228  * Restore the interrupt scheme that was cleared when we suspended the
11229  * device. This should be called during resume to re-allocate the q_vectors
11230  * and reacquire IRQs.
11231  */
11232 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
11233 {
11234 	int err, i;
11235 
11236 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
11237 	 * scheme. We need to re-enabled them here in order to attempt to
11238 	 * re-acquire the MSI or MSI-X vectors
11239 	 */
11240 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
11241 
11242 	err = i40e_init_interrupt_scheme(pf);
11243 	if (err)
11244 		return err;
11245 
11246 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
11247 	 * rings together again.
11248 	 */
11249 	for (i = 0; i < pf->num_alloc_vsi; i++) {
11250 		if (pf->vsi[i]) {
11251 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
11252 			if (err)
11253 				goto err_unwind;
11254 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
11255 		}
11256 	}
11257 
11258 	err = i40e_setup_misc_vector(pf);
11259 	if (err)
11260 		goto err_unwind;
11261 
11262 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
11263 		i40e_client_update_msix_info(pf);
11264 
11265 	return 0;
11266 
11267 err_unwind:
11268 	while (i--) {
11269 		if (pf->vsi[i])
11270 			i40e_vsi_free_q_vectors(pf->vsi[i]);
11271 	}
11272 
11273 	return err;
11274 }
11275 
11276 /**
11277  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
11278  * non queue events in recovery mode
11279  * @pf: board private structure
11280  *
11281  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
11282  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
11283  * This is handled differently than in recovery mode since no Tx/Rx resources
11284  * are being allocated.
11285  **/
11286 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
11287 {
11288 	int err;
11289 
11290 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11291 		err = i40e_setup_misc_vector(pf);
11292 
11293 		if (err) {
11294 			dev_info(&pf->pdev->dev,
11295 				 "MSI-X misc vector request failed, error %d\n",
11296 				 err);
11297 			return err;
11298 		}
11299 	} else {
11300 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
11301 
11302 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
11303 				  pf->int_name, pf);
11304 
11305 		if (err) {
11306 			dev_info(&pf->pdev->dev,
11307 				 "MSI/legacy misc vector request failed, error %d\n",
11308 				 err);
11309 			return err;
11310 		}
11311 		i40e_enable_misc_int_causes(pf);
11312 		i40e_irq_dynamic_enable_icr0(pf);
11313 	}
11314 
11315 	return 0;
11316 }
11317 
11318 /**
11319  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
11320  * @pf: board private structure
11321  *
11322  * This sets up the handler for MSIX 0, which is used to manage the
11323  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
11324  * when in MSI or Legacy interrupt mode.
11325  **/
11326 static int i40e_setup_misc_vector(struct i40e_pf *pf)
11327 {
11328 	struct i40e_hw *hw = &pf->hw;
11329 	int err = 0;
11330 
11331 	/* Only request the IRQ once, the first time through. */
11332 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
11333 		err = request_irq(pf->msix_entries[0].vector,
11334 				  i40e_intr, 0, pf->int_name, pf);
11335 		if (err) {
11336 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
11337 			dev_info(&pf->pdev->dev,
11338 				 "request_irq for %s failed: %d\n",
11339 				 pf->int_name, err);
11340 			return -EFAULT;
11341 		}
11342 	}
11343 
11344 	i40e_enable_misc_int_causes(pf);
11345 
11346 	/* associate no queues to the misc vector */
11347 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
11348 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
11349 
11350 	i40e_flush(hw);
11351 
11352 	i40e_irq_dynamic_enable_icr0(pf);
11353 
11354 	return err;
11355 }
11356 
11357 /**
11358  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
11359  * @vsi: Pointer to vsi structure
11360  * @seed: Buffter to store the hash keys
11361  * @lut: Buffer to store the lookup table entries
11362  * @lut_size: Size of buffer to store the lookup table entries
11363  *
11364  * Return 0 on success, negative on failure
11365  */
11366 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
11367 			   u8 *lut, u16 lut_size)
11368 {
11369 	struct i40e_pf *pf = vsi->back;
11370 	struct i40e_hw *hw = &pf->hw;
11371 	int ret = 0;
11372 
11373 	if (seed) {
11374 		ret = i40e_aq_get_rss_key(hw, vsi->id,
11375 			(struct i40e_aqc_get_set_rss_key_data *)seed);
11376 		if (ret) {
11377 			dev_info(&pf->pdev->dev,
11378 				 "Cannot get RSS key, err %s aq_err %s\n",
11379 				 i40e_stat_str(&pf->hw, ret),
11380 				 i40e_aq_str(&pf->hw,
11381 					     pf->hw.aq.asq_last_status));
11382 			return ret;
11383 		}
11384 	}
11385 
11386 	if (lut) {
11387 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
11388 
11389 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
11390 		if (ret) {
11391 			dev_info(&pf->pdev->dev,
11392 				 "Cannot get RSS lut, err %s aq_err %s\n",
11393 				 i40e_stat_str(&pf->hw, ret),
11394 				 i40e_aq_str(&pf->hw,
11395 					     pf->hw.aq.asq_last_status));
11396 			return ret;
11397 		}
11398 	}
11399 
11400 	return ret;
11401 }
11402 
11403 /**
11404  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
11405  * @vsi: Pointer to vsi structure
11406  * @seed: RSS hash seed
11407  * @lut: Lookup table
11408  * @lut_size: Lookup table size
11409  *
11410  * Returns 0 on success, negative on failure
11411  **/
11412 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
11413 			       const u8 *lut, u16 lut_size)
11414 {
11415 	struct i40e_pf *pf = vsi->back;
11416 	struct i40e_hw *hw = &pf->hw;
11417 	u16 vf_id = vsi->vf_id;
11418 	u8 i;
11419 
11420 	/* Fill out hash function seed */
11421 	if (seed) {
11422 		u32 *seed_dw = (u32 *)seed;
11423 
11424 		if (vsi->type == I40E_VSI_MAIN) {
11425 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
11426 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
11427 		} else if (vsi->type == I40E_VSI_SRIOV) {
11428 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
11429 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
11430 		} else {
11431 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
11432 		}
11433 	}
11434 
11435 	if (lut) {
11436 		u32 *lut_dw = (u32 *)lut;
11437 
11438 		if (vsi->type == I40E_VSI_MAIN) {
11439 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
11440 				return -EINVAL;
11441 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11442 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
11443 		} else if (vsi->type == I40E_VSI_SRIOV) {
11444 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
11445 				return -EINVAL;
11446 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
11447 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
11448 		} else {
11449 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
11450 		}
11451 	}
11452 	i40e_flush(hw);
11453 
11454 	return 0;
11455 }
11456 
11457 /**
11458  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
11459  * @vsi: Pointer to VSI structure
11460  * @seed: Buffer to store the keys
11461  * @lut: Buffer to store the lookup table entries
11462  * @lut_size: Size of buffer to store the lookup table entries
11463  *
11464  * Returns 0 on success, negative on failure
11465  */
11466 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
11467 			    u8 *lut, u16 lut_size)
11468 {
11469 	struct i40e_pf *pf = vsi->back;
11470 	struct i40e_hw *hw = &pf->hw;
11471 	u16 i;
11472 
11473 	if (seed) {
11474 		u32 *seed_dw = (u32 *)seed;
11475 
11476 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
11477 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
11478 	}
11479 	if (lut) {
11480 		u32 *lut_dw = (u32 *)lut;
11481 
11482 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
11483 			return -EINVAL;
11484 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11485 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
11486 	}
11487 
11488 	return 0;
11489 }
11490 
11491 /**
11492  * i40e_config_rss - Configure RSS keys and lut
11493  * @vsi: Pointer to VSI structure
11494  * @seed: RSS hash seed
11495  * @lut: Lookup table
11496  * @lut_size: Lookup table size
11497  *
11498  * Returns 0 on success, negative on failure
11499  */
11500 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
11501 {
11502 	struct i40e_pf *pf = vsi->back;
11503 
11504 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
11505 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
11506 	else
11507 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
11508 }
11509 
11510 /**
11511  * i40e_get_rss - Get RSS keys and lut
11512  * @vsi: Pointer to VSI structure
11513  * @seed: Buffer to store the keys
11514  * @lut: Buffer to store the lookup table entries
11515  * @lut_size: Size of buffer to store the lookup table entries
11516  *
11517  * Returns 0 on success, negative on failure
11518  */
11519 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
11520 {
11521 	struct i40e_pf *pf = vsi->back;
11522 
11523 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
11524 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
11525 	else
11526 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
11527 }
11528 
11529 /**
11530  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
11531  * @pf: Pointer to board private structure
11532  * @lut: Lookup table
11533  * @rss_table_size: Lookup table size
11534  * @rss_size: Range of queue number for hashing
11535  */
11536 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
11537 		       u16 rss_table_size, u16 rss_size)
11538 {
11539 	u16 i;
11540 
11541 	for (i = 0; i < rss_table_size; i++)
11542 		lut[i] = i % rss_size;
11543 }
11544 
11545 /**
11546  * i40e_pf_config_rss - Prepare for RSS if used
11547  * @pf: board private structure
11548  **/
11549 static int i40e_pf_config_rss(struct i40e_pf *pf)
11550 {
11551 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
11552 	u8 seed[I40E_HKEY_ARRAY_SIZE];
11553 	u8 *lut;
11554 	struct i40e_hw *hw = &pf->hw;
11555 	u32 reg_val;
11556 	u64 hena;
11557 	int ret;
11558 
11559 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
11560 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
11561 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
11562 	hena |= i40e_pf_get_default_rss_hena(pf);
11563 
11564 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
11565 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
11566 
11567 	/* Determine the RSS table size based on the hardware capabilities */
11568 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
11569 	reg_val = (pf->rss_table_size == 512) ?
11570 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
11571 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
11572 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
11573 
11574 	/* Determine the RSS size of the VSI */
11575 	if (!vsi->rss_size) {
11576 		u16 qcount;
11577 		/* If the firmware does something weird during VSI init, we
11578 		 * could end up with zero TCs. Check for that to avoid
11579 		 * divide-by-zero. It probably won't pass traffic, but it also
11580 		 * won't panic.
11581 		 */
11582 		qcount = vsi->num_queue_pairs /
11583 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
11584 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
11585 	}
11586 	if (!vsi->rss_size)
11587 		return -EINVAL;
11588 
11589 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
11590 	if (!lut)
11591 		return -ENOMEM;
11592 
11593 	/* Use user configured lut if there is one, otherwise use default */
11594 	if (vsi->rss_lut_user)
11595 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
11596 	else
11597 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
11598 
11599 	/* Use user configured hash key if there is one, otherwise
11600 	 * use default.
11601 	 */
11602 	if (vsi->rss_hkey_user)
11603 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
11604 	else
11605 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
11606 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
11607 	kfree(lut);
11608 
11609 	return ret;
11610 }
11611 
11612 /**
11613  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
11614  * @pf: board private structure
11615  * @queue_count: the requested queue count for rss.
11616  *
11617  * returns 0 if rss is not enabled, if enabled returns the final rss queue
11618  * count which may be different from the requested queue count.
11619  * Note: expects to be called while under rtnl_lock()
11620  **/
11621 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
11622 {
11623 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
11624 	int new_rss_size;
11625 
11626 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
11627 		return 0;
11628 
11629 	queue_count = min_t(int, queue_count, num_online_cpus());
11630 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
11631 
11632 	if (queue_count != vsi->num_queue_pairs) {
11633 		u16 qcount;
11634 
11635 		vsi->req_queue_pairs = queue_count;
11636 		i40e_prep_for_reset(pf, true);
11637 
11638 		pf->alloc_rss_size = new_rss_size;
11639 
11640 		i40e_reset_and_rebuild(pf, true, true);
11641 
11642 		/* Discard the user configured hash keys and lut, if less
11643 		 * queues are enabled.
11644 		 */
11645 		if (queue_count < vsi->rss_size) {
11646 			i40e_clear_rss_config_user(vsi);
11647 			dev_dbg(&pf->pdev->dev,
11648 				"discard user configured hash keys and lut\n");
11649 		}
11650 
11651 		/* Reset vsi->rss_size, as number of enabled queues changed */
11652 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
11653 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
11654 
11655 		i40e_pf_config_rss(pf);
11656 	}
11657 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
11658 		 vsi->req_queue_pairs, pf->rss_size_max);
11659 	return pf->alloc_rss_size;
11660 }
11661 
11662 /**
11663  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
11664  * @pf: board private structure
11665  **/
11666 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
11667 {
11668 	i40e_status status;
11669 	bool min_valid, max_valid;
11670 	u32 max_bw, min_bw;
11671 
11672 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
11673 					   &min_valid, &max_valid);
11674 
11675 	if (!status) {
11676 		if (min_valid)
11677 			pf->min_bw = min_bw;
11678 		if (max_valid)
11679 			pf->max_bw = max_bw;
11680 	}
11681 
11682 	return status;
11683 }
11684 
11685 /**
11686  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
11687  * @pf: board private structure
11688  **/
11689 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
11690 {
11691 	struct i40e_aqc_configure_partition_bw_data bw_data;
11692 	i40e_status status;
11693 
11694 	/* Set the valid bit for this PF */
11695 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
11696 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
11697 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
11698 
11699 	/* Set the new bandwidths */
11700 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
11701 
11702 	return status;
11703 }
11704 
11705 /**
11706  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
11707  * @pf: board private structure
11708  **/
11709 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
11710 {
11711 	/* Commit temporary BW setting to permanent NVM image */
11712 	enum i40e_admin_queue_err last_aq_status;
11713 	i40e_status ret;
11714 	u16 nvm_word;
11715 
11716 	if (pf->hw.partition_id != 1) {
11717 		dev_info(&pf->pdev->dev,
11718 			 "Commit BW only works on partition 1! This is partition %d",
11719 			 pf->hw.partition_id);
11720 		ret = I40E_NOT_SUPPORTED;
11721 		goto bw_commit_out;
11722 	}
11723 
11724 	/* Acquire NVM for read access */
11725 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
11726 	last_aq_status = pf->hw.aq.asq_last_status;
11727 	if (ret) {
11728 		dev_info(&pf->pdev->dev,
11729 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
11730 			 i40e_stat_str(&pf->hw, ret),
11731 			 i40e_aq_str(&pf->hw, last_aq_status));
11732 		goto bw_commit_out;
11733 	}
11734 
11735 	/* Read word 0x10 of NVM - SW compatibility word 1 */
11736 	ret = i40e_aq_read_nvm(&pf->hw,
11737 			       I40E_SR_NVM_CONTROL_WORD,
11738 			       0x10, sizeof(nvm_word), &nvm_word,
11739 			       false, NULL);
11740 	/* Save off last admin queue command status before releasing
11741 	 * the NVM
11742 	 */
11743 	last_aq_status = pf->hw.aq.asq_last_status;
11744 	i40e_release_nvm(&pf->hw);
11745 	if (ret) {
11746 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
11747 			 i40e_stat_str(&pf->hw, ret),
11748 			 i40e_aq_str(&pf->hw, last_aq_status));
11749 		goto bw_commit_out;
11750 	}
11751 
11752 	/* Wait a bit for NVM release to complete */
11753 	msleep(50);
11754 
11755 	/* Acquire NVM for write access */
11756 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
11757 	last_aq_status = pf->hw.aq.asq_last_status;
11758 	if (ret) {
11759 		dev_info(&pf->pdev->dev,
11760 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
11761 			 i40e_stat_str(&pf->hw, ret),
11762 			 i40e_aq_str(&pf->hw, last_aq_status));
11763 		goto bw_commit_out;
11764 	}
11765 	/* Write it back out unchanged to initiate update NVM,
11766 	 * which will force a write of the shadow (alt) RAM to
11767 	 * the NVM - thus storing the bandwidth values permanently.
11768 	 */
11769 	ret = i40e_aq_update_nvm(&pf->hw,
11770 				 I40E_SR_NVM_CONTROL_WORD,
11771 				 0x10, sizeof(nvm_word),
11772 				 &nvm_word, true, 0, NULL);
11773 	/* Save off last admin queue command status before releasing
11774 	 * the NVM
11775 	 */
11776 	last_aq_status = pf->hw.aq.asq_last_status;
11777 	i40e_release_nvm(&pf->hw);
11778 	if (ret)
11779 		dev_info(&pf->pdev->dev,
11780 			 "BW settings NOT SAVED, err %s aq_err %s\n",
11781 			 i40e_stat_str(&pf->hw, ret),
11782 			 i40e_aq_str(&pf->hw, last_aq_status));
11783 bw_commit_out:
11784 
11785 	return ret;
11786 }
11787 
11788 /**
11789  * i40e_is_total_port_shutdown_enabled - read NVM and return value
11790  * if total port shutdown feature is enabled for this PF
11791  * @pf: board private structure
11792  **/
11793 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
11794 {
11795 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
11796 #define I40E_FEATURES_ENABLE_PTR		0x2A
11797 #define I40E_CURRENT_SETTING_PTR		0x2B
11798 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
11799 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
11800 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
11801 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
11802 	i40e_status read_status = I40E_SUCCESS;
11803 	u16 sr_emp_sr_settings_ptr = 0;
11804 	u16 features_enable = 0;
11805 	u16 link_behavior = 0;
11806 	bool ret = false;
11807 
11808 	read_status = i40e_read_nvm_word(&pf->hw,
11809 					 I40E_SR_EMP_SR_SETTINGS_PTR,
11810 					 &sr_emp_sr_settings_ptr);
11811 	if (read_status)
11812 		goto err_nvm;
11813 	read_status = i40e_read_nvm_word(&pf->hw,
11814 					 sr_emp_sr_settings_ptr +
11815 					 I40E_FEATURES_ENABLE_PTR,
11816 					 &features_enable);
11817 	if (read_status)
11818 		goto err_nvm;
11819 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
11820 		read_status = i40e_read_nvm_module_data(&pf->hw,
11821 							I40E_SR_EMP_SR_SETTINGS_PTR,
11822 							I40E_CURRENT_SETTING_PTR,
11823 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
11824 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
11825 							&link_behavior);
11826 		if (read_status)
11827 			goto err_nvm;
11828 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
11829 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
11830 	}
11831 	return ret;
11832 
11833 err_nvm:
11834 	dev_warn(&pf->pdev->dev,
11835 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
11836 		 i40e_stat_str(&pf->hw, read_status));
11837 	return ret;
11838 }
11839 
11840 /**
11841  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
11842  * @pf: board private structure to initialize
11843  *
11844  * i40e_sw_init initializes the Adapter private data structure.
11845  * Fields are initialized based on PCI device information and
11846  * OS network device settings (MTU size).
11847  **/
11848 static int i40e_sw_init(struct i40e_pf *pf)
11849 {
11850 	int err = 0;
11851 	int size;
11852 
11853 	/* Set default capability flags */
11854 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
11855 		    I40E_FLAG_MSI_ENABLED     |
11856 		    I40E_FLAG_MSIX_ENABLED;
11857 
11858 	/* Set default ITR */
11859 	pf->rx_itr_default = I40E_ITR_RX_DEF;
11860 	pf->tx_itr_default = I40E_ITR_TX_DEF;
11861 
11862 	/* Depending on PF configurations, it is possible that the RSS
11863 	 * maximum might end up larger than the available queues
11864 	 */
11865 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
11866 	pf->alloc_rss_size = 1;
11867 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
11868 	pf->rss_size_max = min_t(int, pf->rss_size_max,
11869 				 pf->hw.func_caps.num_tx_qp);
11870 	if (pf->hw.func_caps.rss) {
11871 		pf->flags |= I40E_FLAG_RSS_ENABLED;
11872 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
11873 					   num_online_cpus());
11874 	}
11875 
11876 	/* MFP mode enabled */
11877 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
11878 		pf->flags |= I40E_FLAG_MFP_ENABLED;
11879 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
11880 		if (i40e_get_partition_bw_setting(pf)) {
11881 			dev_warn(&pf->pdev->dev,
11882 				 "Could not get partition bw settings\n");
11883 		} else {
11884 			dev_info(&pf->pdev->dev,
11885 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
11886 				 pf->min_bw, pf->max_bw);
11887 
11888 			/* nudge the Tx scheduler */
11889 			i40e_set_partition_bw_setting(pf);
11890 		}
11891 	}
11892 
11893 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
11894 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
11895 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
11896 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
11897 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
11898 		    pf->hw.num_partitions > 1)
11899 			dev_info(&pf->pdev->dev,
11900 				 "Flow Director Sideband mode Disabled in MFP mode\n");
11901 		else
11902 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
11903 		pf->fdir_pf_filter_count =
11904 				 pf->hw.func_caps.fd_filters_guaranteed;
11905 		pf->hw.fdir_shared_filter_count =
11906 				 pf->hw.func_caps.fd_filters_best_effort;
11907 	}
11908 
11909 	if (pf->hw.mac.type == I40E_MAC_X722) {
11910 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
11911 				    I40E_HW_128_QP_RSS_CAPABLE |
11912 				    I40E_HW_ATR_EVICT_CAPABLE |
11913 				    I40E_HW_WB_ON_ITR_CAPABLE |
11914 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
11915 				    I40E_HW_NO_PCI_LINK_CHECK |
11916 				    I40E_HW_USE_SET_LLDP_MIB |
11917 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
11918 				    I40E_HW_PTP_L4_CAPABLE |
11919 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
11920 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
11921 
11922 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
11923 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
11924 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
11925 			dev_warn(&pf->pdev->dev,
11926 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
11927 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
11928 		}
11929 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
11930 		   ((pf->hw.aq.api_maj_ver == 1) &&
11931 		    (pf->hw.aq.api_min_ver > 4))) {
11932 		/* Supported in FW API version higher than 1.4 */
11933 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
11934 	}
11935 
11936 	/* Enable HW ATR eviction if possible */
11937 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
11938 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
11939 
11940 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11941 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
11942 	    (pf->hw.aq.fw_maj_ver < 4))) {
11943 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
11944 		/* No DCB support  for FW < v4.33 */
11945 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
11946 	}
11947 
11948 	/* Disable FW LLDP if FW < v4.3 */
11949 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11950 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
11951 	    (pf->hw.aq.fw_maj_ver < 4)))
11952 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
11953 
11954 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
11955 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11956 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
11957 	    (pf->hw.aq.fw_maj_ver >= 5)))
11958 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
11959 
11960 	/* Enable PTP L4 if FW > v6.0 */
11961 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
11962 	    pf->hw.aq.fw_maj_ver >= 6)
11963 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
11964 
11965 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
11966 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
11967 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
11968 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
11969 	}
11970 
11971 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
11972 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
11973 		/* IWARP needs one extra vector for CQP just like MISC.*/
11974 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
11975 	}
11976 	/* Stopping FW LLDP engine is supported on XL710 and X722
11977 	 * starting from FW versions determined in i40e_init_adminq.
11978 	 * Stopping the FW LLDP engine is not supported on XL710
11979 	 * if NPAR is functioning so unset this hw flag in this case.
11980 	 */
11981 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
11982 	    pf->hw.func_caps.npar_enable &&
11983 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
11984 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
11985 
11986 #ifdef CONFIG_PCI_IOV
11987 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
11988 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
11989 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
11990 		pf->num_req_vfs = min_t(int,
11991 					pf->hw.func_caps.num_vfs,
11992 					I40E_MAX_VF_COUNT);
11993 	}
11994 #endif /* CONFIG_PCI_IOV */
11995 	pf->eeprom_version = 0xDEAD;
11996 	pf->lan_veb = I40E_NO_VEB;
11997 	pf->lan_vsi = I40E_NO_VSI;
11998 
11999 	/* By default FW has this off for performance reasons */
12000 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12001 
12002 	/* set up queue assignment tracking */
12003 	size = sizeof(struct i40e_lump_tracking)
12004 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12005 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12006 	if (!pf->qp_pile) {
12007 		err = -ENOMEM;
12008 		goto sw_init_done;
12009 	}
12010 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12011 	pf->qp_pile->search_hint = 0;
12012 
12013 	pf->tx_timeout_recovery_level = 1;
12014 
12015 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12016 	    i40e_is_total_port_shutdown_enabled(pf)) {
12017 		/* Link down on close must be on when total port shutdown
12018 		 * is enabled for a given port
12019 		 */
12020 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12021 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12022 		dev_info(&pf->pdev->dev,
12023 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12024 	}
12025 	mutex_init(&pf->switch_mutex);
12026 
12027 sw_init_done:
12028 	return err;
12029 }
12030 
12031 /**
12032  * i40e_set_ntuple - set the ntuple feature flag and take action
12033  * @pf: board private structure to initialize
12034  * @features: the feature set that the stack is suggesting
12035  *
12036  * returns a bool to indicate if reset needs to happen
12037  **/
12038 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12039 {
12040 	bool need_reset = false;
12041 
12042 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12043 	 * the state changed, we need to reset.
12044 	 */
12045 	if (features & NETIF_F_NTUPLE) {
12046 		/* Enable filters and mark for reset */
12047 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12048 			need_reset = true;
12049 		/* enable FD_SB only if there is MSI-X vector and no cloud
12050 		 * filters exist
12051 		 */
12052 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12053 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12054 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12055 		}
12056 	} else {
12057 		/* turn off filters, mark for reset and clear SW filter list */
12058 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12059 			need_reset = true;
12060 			i40e_fdir_filter_exit(pf);
12061 		}
12062 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12063 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12064 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12065 
12066 		/* reset fd counters */
12067 		pf->fd_add_err = 0;
12068 		pf->fd_atr_cnt = 0;
12069 		/* if ATR was auto disabled it can be re-enabled. */
12070 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12071 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12072 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12073 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12074 	}
12075 	return need_reset;
12076 }
12077 
12078 /**
12079  * i40e_clear_rss_lut - clear the rx hash lookup table
12080  * @vsi: the VSI being configured
12081  **/
12082 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12083 {
12084 	struct i40e_pf *pf = vsi->back;
12085 	struct i40e_hw *hw = &pf->hw;
12086 	u16 vf_id = vsi->vf_id;
12087 	u8 i;
12088 
12089 	if (vsi->type == I40E_VSI_MAIN) {
12090 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12091 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12092 	} else if (vsi->type == I40E_VSI_SRIOV) {
12093 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12094 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12095 	} else {
12096 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12097 	}
12098 }
12099 
12100 /**
12101  * i40e_set_features - set the netdev feature flags
12102  * @netdev: ptr to the netdev being adjusted
12103  * @features: the feature set that the stack is suggesting
12104  * Note: expects to be called while under rtnl_lock()
12105  **/
12106 static int i40e_set_features(struct net_device *netdev,
12107 			     netdev_features_t features)
12108 {
12109 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12110 	struct i40e_vsi *vsi = np->vsi;
12111 	struct i40e_pf *pf = vsi->back;
12112 	bool need_reset;
12113 
12114 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12115 		i40e_pf_config_rss(pf);
12116 	else if (!(features & NETIF_F_RXHASH) &&
12117 		 netdev->features & NETIF_F_RXHASH)
12118 		i40e_clear_rss_lut(vsi);
12119 
12120 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12121 		i40e_vlan_stripping_enable(vsi);
12122 	else
12123 		i40e_vlan_stripping_disable(vsi);
12124 
12125 	if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12126 		dev_err(&pf->pdev->dev,
12127 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12128 		return -EINVAL;
12129 	}
12130 
12131 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12132 		i40e_del_all_macvlans(vsi);
12133 
12134 	need_reset = i40e_set_ntuple(pf, features);
12135 
12136 	if (need_reset)
12137 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12138 
12139 	return 0;
12140 }
12141 
12142 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12143 				    unsigned int table, unsigned int idx,
12144 				    struct udp_tunnel_info *ti)
12145 {
12146 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12147 	struct i40e_hw *hw = &np->vsi->back->hw;
12148 	u8 type, filter_index;
12149 	i40e_status ret;
12150 
12151 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12152 						   I40E_AQC_TUNNEL_TYPE_NGE;
12153 
12154 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12155 				     NULL);
12156 	if (ret) {
12157 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12158 			    i40e_stat_str(hw, ret),
12159 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12160 		return -EIO;
12161 	}
12162 
12163 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12164 	return 0;
12165 }
12166 
12167 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12168 				      unsigned int table, unsigned int idx,
12169 				      struct udp_tunnel_info *ti)
12170 {
12171 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12172 	struct i40e_hw *hw = &np->vsi->back->hw;
12173 	i40e_status ret;
12174 
12175 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12176 	if (ret) {
12177 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
12178 			    i40e_stat_str(hw, ret),
12179 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12180 		return -EIO;
12181 	}
12182 
12183 	return 0;
12184 }
12185 
12186 static int i40e_get_phys_port_id(struct net_device *netdev,
12187 				 struct netdev_phys_item_id *ppid)
12188 {
12189 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12190 	struct i40e_pf *pf = np->vsi->back;
12191 	struct i40e_hw *hw = &pf->hw;
12192 
12193 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
12194 		return -EOPNOTSUPP;
12195 
12196 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
12197 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
12198 
12199 	return 0;
12200 }
12201 
12202 /**
12203  * i40e_ndo_fdb_add - add an entry to the hardware database
12204  * @ndm: the input from the stack
12205  * @tb: pointer to array of nladdr (unused)
12206  * @dev: the net device pointer
12207  * @addr: the MAC address entry being added
12208  * @vid: VLAN ID
12209  * @flags: instructions from stack about fdb operation
12210  * @extack: netlink extended ack, unused currently
12211  */
12212 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
12213 			    struct net_device *dev,
12214 			    const unsigned char *addr, u16 vid,
12215 			    u16 flags,
12216 			    struct netlink_ext_ack *extack)
12217 {
12218 	struct i40e_netdev_priv *np = netdev_priv(dev);
12219 	struct i40e_pf *pf = np->vsi->back;
12220 	int err = 0;
12221 
12222 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
12223 		return -EOPNOTSUPP;
12224 
12225 	if (vid) {
12226 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
12227 		return -EINVAL;
12228 	}
12229 
12230 	/* Hardware does not support aging addresses so if a
12231 	 * ndm_state is given only allow permanent addresses
12232 	 */
12233 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
12234 		netdev_info(dev, "FDB only supports static addresses\n");
12235 		return -EINVAL;
12236 	}
12237 
12238 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
12239 		err = dev_uc_add_excl(dev, addr);
12240 	else if (is_multicast_ether_addr(addr))
12241 		err = dev_mc_add_excl(dev, addr);
12242 	else
12243 		err = -EINVAL;
12244 
12245 	/* Only return duplicate errors if NLM_F_EXCL is set */
12246 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
12247 		err = 0;
12248 
12249 	return err;
12250 }
12251 
12252 /**
12253  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
12254  * @dev: the netdev being configured
12255  * @nlh: RTNL message
12256  * @flags: bridge flags
12257  * @extack: netlink extended ack
12258  *
12259  * Inserts a new hardware bridge if not already created and
12260  * enables the bridging mode requested (VEB or VEPA). If the
12261  * hardware bridge has already been inserted and the request
12262  * is to change the mode then that requires a PF reset to
12263  * allow rebuild of the components with required hardware
12264  * bridge mode enabled.
12265  *
12266  * Note: expects to be called while under rtnl_lock()
12267  **/
12268 static int i40e_ndo_bridge_setlink(struct net_device *dev,
12269 				   struct nlmsghdr *nlh,
12270 				   u16 flags,
12271 				   struct netlink_ext_ack *extack)
12272 {
12273 	struct i40e_netdev_priv *np = netdev_priv(dev);
12274 	struct i40e_vsi *vsi = np->vsi;
12275 	struct i40e_pf *pf = vsi->back;
12276 	struct i40e_veb *veb = NULL;
12277 	struct nlattr *attr, *br_spec;
12278 	int i, rem;
12279 
12280 	/* Only for PF VSI for now */
12281 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12282 		return -EOPNOTSUPP;
12283 
12284 	/* Find the HW bridge for PF VSI */
12285 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12286 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12287 			veb = pf->veb[i];
12288 	}
12289 
12290 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
12291 
12292 	nla_for_each_nested(attr, br_spec, rem) {
12293 		__u16 mode;
12294 
12295 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
12296 			continue;
12297 
12298 		mode = nla_get_u16(attr);
12299 		if ((mode != BRIDGE_MODE_VEPA) &&
12300 		    (mode != BRIDGE_MODE_VEB))
12301 			return -EINVAL;
12302 
12303 		/* Insert a new HW bridge */
12304 		if (!veb) {
12305 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12306 					     vsi->tc_config.enabled_tc);
12307 			if (veb) {
12308 				veb->bridge_mode = mode;
12309 				i40e_config_bridge_mode(veb);
12310 			} else {
12311 				/* No Bridge HW offload available */
12312 				return -ENOENT;
12313 			}
12314 			break;
12315 		} else if (mode != veb->bridge_mode) {
12316 			/* Existing HW bridge but different mode needs reset */
12317 			veb->bridge_mode = mode;
12318 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
12319 			if (mode == BRIDGE_MODE_VEB)
12320 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
12321 			else
12322 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12323 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12324 			break;
12325 		}
12326 	}
12327 
12328 	return 0;
12329 }
12330 
12331 /**
12332  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
12333  * @skb: skb buff
12334  * @pid: process id
12335  * @seq: RTNL message seq #
12336  * @dev: the netdev being configured
12337  * @filter_mask: unused
12338  * @nlflags: netlink flags passed in
12339  *
12340  * Return the mode in which the hardware bridge is operating in
12341  * i.e VEB or VEPA.
12342  **/
12343 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
12344 				   struct net_device *dev,
12345 				   u32 __always_unused filter_mask,
12346 				   int nlflags)
12347 {
12348 	struct i40e_netdev_priv *np = netdev_priv(dev);
12349 	struct i40e_vsi *vsi = np->vsi;
12350 	struct i40e_pf *pf = vsi->back;
12351 	struct i40e_veb *veb = NULL;
12352 	int i;
12353 
12354 	/* Only for PF VSI for now */
12355 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12356 		return -EOPNOTSUPP;
12357 
12358 	/* Find the HW bridge for the PF VSI */
12359 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12360 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12361 			veb = pf->veb[i];
12362 	}
12363 
12364 	if (!veb)
12365 		return 0;
12366 
12367 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
12368 				       0, 0, nlflags, filter_mask, NULL);
12369 }
12370 
12371 /**
12372  * i40e_features_check - Validate encapsulated packet conforms to limits
12373  * @skb: skb buff
12374  * @dev: This physical port's netdev
12375  * @features: Offload features that the stack believes apply
12376  **/
12377 static netdev_features_t i40e_features_check(struct sk_buff *skb,
12378 					     struct net_device *dev,
12379 					     netdev_features_t features)
12380 {
12381 	size_t len;
12382 
12383 	/* No point in doing any of this if neither checksum nor GSO are
12384 	 * being requested for this frame.  We can rule out both by just
12385 	 * checking for CHECKSUM_PARTIAL
12386 	 */
12387 	if (skb->ip_summed != CHECKSUM_PARTIAL)
12388 		return features;
12389 
12390 	/* We cannot support GSO if the MSS is going to be less than
12391 	 * 64 bytes.  If it is then we need to drop support for GSO.
12392 	 */
12393 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
12394 		features &= ~NETIF_F_GSO_MASK;
12395 
12396 	/* MACLEN can support at most 63 words */
12397 	len = skb_network_header(skb) - skb->data;
12398 	if (len & ~(63 * 2))
12399 		goto out_err;
12400 
12401 	/* IPLEN and EIPLEN can support at most 127 dwords */
12402 	len = skb_transport_header(skb) - skb_network_header(skb);
12403 	if (len & ~(127 * 4))
12404 		goto out_err;
12405 
12406 	if (skb->encapsulation) {
12407 		/* L4TUNLEN can support 127 words */
12408 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
12409 		if (len & ~(127 * 2))
12410 			goto out_err;
12411 
12412 		/* IPLEN can support at most 127 dwords */
12413 		len = skb_inner_transport_header(skb) -
12414 		      skb_inner_network_header(skb);
12415 		if (len & ~(127 * 4))
12416 			goto out_err;
12417 	}
12418 
12419 	/* No need to validate L4LEN as TCP is the only protocol with a
12420 	 * a flexible value and we support all possible values supported
12421 	 * by TCP, which is at most 15 dwords
12422 	 */
12423 
12424 	return features;
12425 out_err:
12426 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
12427 }
12428 
12429 /**
12430  * i40e_xdp_setup - add/remove an XDP program
12431  * @vsi: VSI to changed
12432  * @prog: XDP program
12433  **/
12434 static int i40e_xdp_setup(struct i40e_vsi *vsi,
12435 			  struct bpf_prog *prog)
12436 {
12437 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
12438 	struct i40e_pf *pf = vsi->back;
12439 	struct bpf_prog *old_prog;
12440 	bool need_reset;
12441 	int i;
12442 
12443 	/* Don't allow frames that span over multiple buffers */
12444 	if (frame_size > vsi->rx_buf_len)
12445 		return -EINVAL;
12446 
12447 	if (!i40e_enabled_xdp_vsi(vsi) && !prog)
12448 		return 0;
12449 
12450 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
12451 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
12452 
12453 	if (need_reset)
12454 		i40e_prep_for_reset(pf, true);
12455 
12456 	old_prog = xchg(&vsi->xdp_prog, prog);
12457 
12458 	if (need_reset) {
12459 		if (!prog)
12460 			/* Wait until ndo_xsk_wakeup completes. */
12461 			synchronize_rcu();
12462 		i40e_reset_and_rebuild(pf, true, true);
12463 	}
12464 
12465 	for (i = 0; i < vsi->num_queue_pairs; i++)
12466 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
12467 
12468 	if (old_prog)
12469 		bpf_prog_put(old_prog);
12470 
12471 	/* Kick start the NAPI context if there is an AF_XDP socket open
12472 	 * on that queue id. This so that receiving will start.
12473 	 */
12474 	if (need_reset && prog)
12475 		for (i = 0; i < vsi->num_queue_pairs; i++)
12476 			if (vsi->xdp_rings[i]->xsk_pool)
12477 				(void)i40e_xsk_wakeup(vsi->netdev, i,
12478 						      XDP_WAKEUP_RX);
12479 
12480 	return 0;
12481 }
12482 
12483 /**
12484  * i40e_enter_busy_conf - Enters busy config state
12485  * @vsi: vsi
12486  *
12487  * Returns 0 on success, <0 for failure.
12488  **/
12489 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
12490 {
12491 	struct i40e_pf *pf = vsi->back;
12492 	int timeout = 50;
12493 
12494 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
12495 		timeout--;
12496 		if (!timeout)
12497 			return -EBUSY;
12498 		usleep_range(1000, 2000);
12499 	}
12500 
12501 	return 0;
12502 }
12503 
12504 /**
12505  * i40e_exit_busy_conf - Exits busy config state
12506  * @vsi: vsi
12507  **/
12508 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
12509 {
12510 	struct i40e_pf *pf = vsi->back;
12511 
12512 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
12513 }
12514 
12515 /**
12516  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
12517  * @vsi: vsi
12518  * @queue_pair: queue pair
12519  **/
12520 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
12521 {
12522 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
12523 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
12524 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
12525 	       sizeof(vsi->tx_rings[queue_pair]->stats));
12526 	if (i40e_enabled_xdp_vsi(vsi)) {
12527 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
12528 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
12529 	}
12530 }
12531 
12532 /**
12533  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
12534  * @vsi: vsi
12535  * @queue_pair: queue pair
12536  **/
12537 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
12538 {
12539 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
12540 	if (i40e_enabled_xdp_vsi(vsi)) {
12541 		/* Make sure that in-progress ndo_xdp_xmit calls are
12542 		 * completed.
12543 		 */
12544 		synchronize_rcu();
12545 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
12546 	}
12547 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
12548 }
12549 
12550 /**
12551  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
12552  * @vsi: vsi
12553  * @queue_pair: queue pair
12554  * @enable: true for enable, false for disable
12555  **/
12556 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
12557 					bool enable)
12558 {
12559 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12560 	struct i40e_q_vector *q_vector = rxr->q_vector;
12561 
12562 	if (!vsi->netdev)
12563 		return;
12564 
12565 	/* All rings in a qp belong to the same qvector. */
12566 	if (q_vector->rx.ring || q_vector->tx.ring) {
12567 		if (enable)
12568 			napi_enable(&q_vector->napi);
12569 		else
12570 			napi_disable(&q_vector->napi);
12571 	}
12572 }
12573 
12574 /**
12575  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
12576  * @vsi: vsi
12577  * @queue_pair: queue pair
12578  * @enable: true for enable, false for disable
12579  *
12580  * Returns 0 on success, <0 on failure.
12581  **/
12582 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
12583 					bool enable)
12584 {
12585 	struct i40e_pf *pf = vsi->back;
12586 	int pf_q, ret = 0;
12587 
12588 	pf_q = vsi->base_queue + queue_pair;
12589 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
12590 				     false /*is xdp*/, enable);
12591 	if (ret) {
12592 		dev_info(&pf->pdev->dev,
12593 			 "VSI seid %d Tx ring %d %sable timeout\n",
12594 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12595 		return ret;
12596 	}
12597 
12598 	i40e_control_rx_q(pf, pf_q, enable);
12599 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
12600 	if (ret) {
12601 		dev_info(&pf->pdev->dev,
12602 			 "VSI seid %d Rx ring %d %sable timeout\n",
12603 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12604 		return ret;
12605 	}
12606 
12607 	/* Due to HW errata, on Rx disable only, the register can
12608 	 * indicate done before it really is. Needs 50ms to be sure
12609 	 */
12610 	if (!enable)
12611 		mdelay(50);
12612 
12613 	if (!i40e_enabled_xdp_vsi(vsi))
12614 		return ret;
12615 
12616 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
12617 				     pf_q + vsi->alloc_queue_pairs,
12618 				     true /*is xdp*/, enable);
12619 	if (ret) {
12620 		dev_info(&pf->pdev->dev,
12621 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
12622 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12623 	}
12624 
12625 	return ret;
12626 }
12627 
12628 /**
12629  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
12630  * @vsi: vsi
12631  * @queue_pair: queue_pair
12632  **/
12633 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
12634 {
12635 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12636 	struct i40e_pf *pf = vsi->back;
12637 	struct i40e_hw *hw = &pf->hw;
12638 
12639 	/* All rings in a qp belong to the same qvector. */
12640 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
12641 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
12642 	else
12643 		i40e_irq_dynamic_enable_icr0(pf);
12644 
12645 	i40e_flush(hw);
12646 }
12647 
12648 /**
12649  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
12650  * @vsi: vsi
12651  * @queue_pair: queue_pair
12652  **/
12653 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
12654 {
12655 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12656 	struct i40e_pf *pf = vsi->back;
12657 	struct i40e_hw *hw = &pf->hw;
12658 
12659 	/* For simplicity, instead of removing the qp interrupt causes
12660 	 * from the interrupt linked list, we simply disable the interrupt, and
12661 	 * leave the list intact.
12662 	 *
12663 	 * All rings in a qp belong to the same qvector.
12664 	 */
12665 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12666 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
12667 
12668 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
12669 		i40e_flush(hw);
12670 		synchronize_irq(pf->msix_entries[intpf].vector);
12671 	} else {
12672 		/* Legacy and MSI mode - this stops all interrupt handling */
12673 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
12674 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
12675 		i40e_flush(hw);
12676 		synchronize_irq(pf->pdev->irq);
12677 	}
12678 }
12679 
12680 /**
12681  * i40e_queue_pair_disable - Disables a queue pair
12682  * @vsi: vsi
12683  * @queue_pair: queue pair
12684  *
12685  * Returns 0 on success, <0 on failure.
12686  **/
12687 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
12688 {
12689 	int err;
12690 
12691 	err = i40e_enter_busy_conf(vsi);
12692 	if (err)
12693 		return err;
12694 
12695 	i40e_queue_pair_disable_irq(vsi, queue_pair);
12696 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
12697 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
12698 	i40e_queue_pair_clean_rings(vsi, queue_pair);
12699 	i40e_queue_pair_reset_stats(vsi, queue_pair);
12700 
12701 	return err;
12702 }
12703 
12704 /**
12705  * i40e_queue_pair_enable - Enables a queue pair
12706  * @vsi: vsi
12707  * @queue_pair: queue pair
12708  *
12709  * Returns 0 on success, <0 on failure.
12710  **/
12711 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
12712 {
12713 	int err;
12714 
12715 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
12716 	if (err)
12717 		return err;
12718 
12719 	if (i40e_enabled_xdp_vsi(vsi)) {
12720 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
12721 		if (err)
12722 			return err;
12723 	}
12724 
12725 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
12726 	if (err)
12727 		return err;
12728 
12729 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
12730 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
12731 	i40e_queue_pair_enable_irq(vsi, queue_pair);
12732 
12733 	i40e_exit_busy_conf(vsi);
12734 
12735 	return err;
12736 }
12737 
12738 /**
12739  * i40e_xdp - implements ndo_bpf for i40e
12740  * @dev: netdevice
12741  * @xdp: XDP command
12742  **/
12743 static int i40e_xdp(struct net_device *dev,
12744 		    struct netdev_bpf *xdp)
12745 {
12746 	struct i40e_netdev_priv *np = netdev_priv(dev);
12747 	struct i40e_vsi *vsi = np->vsi;
12748 
12749 	if (vsi->type != I40E_VSI_MAIN)
12750 		return -EINVAL;
12751 
12752 	switch (xdp->command) {
12753 	case XDP_SETUP_PROG:
12754 		return i40e_xdp_setup(vsi, xdp->prog);
12755 	case XDP_SETUP_XSK_POOL:
12756 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
12757 					   xdp->xsk.queue_id);
12758 	default:
12759 		return -EINVAL;
12760 	}
12761 }
12762 
12763 static const struct net_device_ops i40e_netdev_ops = {
12764 	.ndo_open		= i40e_open,
12765 	.ndo_stop		= i40e_close,
12766 	.ndo_start_xmit		= i40e_lan_xmit_frame,
12767 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
12768 	.ndo_set_rx_mode	= i40e_set_rx_mode,
12769 	.ndo_validate_addr	= eth_validate_addr,
12770 	.ndo_set_mac_address	= i40e_set_mac,
12771 	.ndo_change_mtu		= i40e_change_mtu,
12772 	.ndo_do_ioctl		= i40e_ioctl,
12773 	.ndo_tx_timeout		= i40e_tx_timeout,
12774 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
12775 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
12776 #ifdef CONFIG_NET_POLL_CONTROLLER
12777 	.ndo_poll_controller	= i40e_netpoll,
12778 #endif
12779 	.ndo_setup_tc		= __i40e_setup_tc,
12780 	.ndo_set_features	= i40e_set_features,
12781 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
12782 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
12783 	.ndo_get_vf_stats	= i40e_get_vf_stats,
12784 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
12785 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
12786 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
12787 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
12788 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
12789 	.ndo_udp_tunnel_add	= udp_tunnel_nic_add_port,
12790 	.ndo_udp_tunnel_del	= udp_tunnel_nic_del_port,
12791 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
12792 	.ndo_fdb_add		= i40e_ndo_fdb_add,
12793 	.ndo_features_check	= i40e_features_check,
12794 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
12795 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
12796 	.ndo_bpf		= i40e_xdp,
12797 	.ndo_xdp_xmit		= i40e_xdp_xmit,
12798 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
12799 	.ndo_dfwd_add_station	= i40e_fwd_add,
12800 	.ndo_dfwd_del_station	= i40e_fwd_del,
12801 };
12802 
12803 /**
12804  * i40e_config_netdev - Setup the netdev flags
12805  * @vsi: the VSI being configured
12806  *
12807  * Returns 0 on success, negative value on failure
12808  **/
12809 static int i40e_config_netdev(struct i40e_vsi *vsi)
12810 {
12811 	struct i40e_pf *pf = vsi->back;
12812 	struct i40e_hw *hw = &pf->hw;
12813 	struct i40e_netdev_priv *np;
12814 	struct net_device *netdev;
12815 	u8 broadcast[ETH_ALEN];
12816 	u8 mac_addr[ETH_ALEN];
12817 	int etherdev_size;
12818 	netdev_features_t hw_enc_features;
12819 	netdev_features_t hw_features;
12820 
12821 	etherdev_size = sizeof(struct i40e_netdev_priv);
12822 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
12823 	if (!netdev)
12824 		return -ENOMEM;
12825 
12826 	vsi->netdev = netdev;
12827 	np = netdev_priv(netdev);
12828 	np->vsi = vsi;
12829 
12830 	hw_enc_features = NETIF_F_SG			|
12831 			  NETIF_F_IP_CSUM		|
12832 			  NETIF_F_IPV6_CSUM		|
12833 			  NETIF_F_HIGHDMA		|
12834 			  NETIF_F_SOFT_FEATURES		|
12835 			  NETIF_F_TSO			|
12836 			  NETIF_F_TSO_ECN		|
12837 			  NETIF_F_TSO6			|
12838 			  NETIF_F_GSO_GRE		|
12839 			  NETIF_F_GSO_GRE_CSUM		|
12840 			  NETIF_F_GSO_PARTIAL		|
12841 			  NETIF_F_GSO_IPXIP4		|
12842 			  NETIF_F_GSO_IPXIP6		|
12843 			  NETIF_F_GSO_UDP_TUNNEL	|
12844 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
12845 			  NETIF_F_GSO_UDP_L4		|
12846 			  NETIF_F_SCTP_CRC		|
12847 			  NETIF_F_RXHASH		|
12848 			  NETIF_F_RXCSUM		|
12849 			  0;
12850 
12851 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
12852 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
12853 
12854 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
12855 
12856 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
12857 
12858 	netdev->hw_enc_features |= hw_enc_features;
12859 
12860 	/* record features VLANs can make use of */
12861 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
12862 
12863 	/* enable macvlan offloads */
12864 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
12865 
12866 	hw_features = hw_enc_features		|
12867 		      NETIF_F_HW_VLAN_CTAG_TX	|
12868 		      NETIF_F_HW_VLAN_CTAG_RX;
12869 
12870 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
12871 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
12872 
12873 	netdev->hw_features |= hw_features;
12874 
12875 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
12876 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
12877 
12878 	if (vsi->type == I40E_VSI_MAIN) {
12879 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
12880 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
12881 		/* The following steps are necessary for two reasons. First,
12882 		 * some older NVM configurations load a default MAC-VLAN
12883 		 * filter that will accept any tagged packet, and we want to
12884 		 * replace this with a normal filter. Additionally, it is
12885 		 * possible our MAC address was provided by the platform using
12886 		 * Open Firmware or similar.
12887 		 *
12888 		 * Thus, we need to remove the default filter and install one
12889 		 * specific to the MAC address.
12890 		 */
12891 		i40e_rm_default_mac_filter(vsi, mac_addr);
12892 		spin_lock_bh(&vsi->mac_filter_hash_lock);
12893 		i40e_add_mac_filter(vsi, mac_addr);
12894 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
12895 	} else {
12896 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
12897 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
12898 		 * the end, which is 4 bytes long, so force truncation of the
12899 		 * original name by IFNAMSIZ - 4
12900 		 */
12901 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
12902 			 IFNAMSIZ - 4,
12903 			 pf->vsi[pf->lan_vsi]->netdev->name);
12904 		eth_random_addr(mac_addr);
12905 
12906 		spin_lock_bh(&vsi->mac_filter_hash_lock);
12907 		i40e_add_mac_filter(vsi, mac_addr);
12908 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
12909 	}
12910 
12911 	/* Add the broadcast filter so that we initially will receive
12912 	 * broadcast packets. Note that when a new VLAN is first added the
12913 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
12914 	 * specific filters as part of transitioning into "vlan" operation.
12915 	 * When more VLANs are added, the driver will copy each existing MAC
12916 	 * filter and add it for the new VLAN.
12917 	 *
12918 	 * Broadcast filters are handled specially by
12919 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
12920 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
12921 	 * filter. The subtask will update the correct broadcast promiscuous
12922 	 * bits as VLANs become active or inactive.
12923 	 */
12924 	eth_broadcast_addr(broadcast);
12925 	spin_lock_bh(&vsi->mac_filter_hash_lock);
12926 	i40e_add_mac_filter(vsi, broadcast);
12927 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
12928 
12929 	ether_addr_copy(netdev->dev_addr, mac_addr);
12930 	ether_addr_copy(netdev->perm_addr, mac_addr);
12931 
12932 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
12933 	netdev->neigh_priv_len = sizeof(u32) * 4;
12934 
12935 	netdev->priv_flags |= IFF_UNICAST_FLT;
12936 	netdev->priv_flags |= IFF_SUPP_NOFCS;
12937 	/* Setup netdev TC information */
12938 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
12939 
12940 	netdev->netdev_ops = &i40e_netdev_ops;
12941 	netdev->watchdog_timeo = 5 * HZ;
12942 	i40e_set_ethtool_ops(netdev);
12943 
12944 	/* MTU range: 68 - 9706 */
12945 	netdev->min_mtu = ETH_MIN_MTU;
12946 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
12947 
12948 	return 0;
12949 }
12950 
12951 /**
12952  * i40e_vsi_delete - Delete a VSI from the switch
12953  * @vsi: the VSI being removed
12954  *
12955  * Returns 0 on success, negative value on failure
12956  **/
12957 static void i40e_vsi_delete(struct i40e_vsi *vsi)
12958 {
12959 	/* remove default VSI is not allowed */
12960 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
12961 		return;
12962 
12963 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
12964 }
12965 
12966 /**
12967  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
12968  * @vsi: the VSI being queried
12969  *
12970  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
12971  **/
12972 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
12973 {
12974 	struct i40e_veb *veb;
12975 	struct i40e_pf *pf = vsi->back;
12976 
12977 	/* Uplink is not a bridge so default to VEB */
12978 	if (vsi->veb_idx >= I40E_MAX_VEB)
12979 		return 1;
12980 
12981 	veb = pf->veb[vsi->veb_idx];
12982 	if (!veb) {
12983 		dev_info(&pf->pdev->dev,
12984 			 "There is no veb associated with the bridge\n");
12985 		return -ENOENT;
12986 	}
12987 
12988 	/* Uplink is a bridge in VEPA mode */
12989 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
12990 		return 0;
12991 	} else {
12992 		/* Uplink is a bridge in VEB mode */
12993 		return 1;
12994 	}
12995 
12996 	/* VEPA is now default bridge, so return 0 */
12997 	return 0;
12998 }
12999 
13000 /**
13001  * i40e_add_vsi - Add a VSI to the switch
13002  * @vsi: the VSI being configured
13003  *
13004  * This initializes a VSI context depending on the VSI type to be added and
13005  * passes it down to the add_vsi aq command.
13006  **/
13007 static int i40e_add_vsi(struct i40e_vsi *vsi)
13008 {
13009 	int ret = -ENODEV;
13010 	struct i40e_pf *pf = vsi->back;
13011 	struct i40e_hw *hw = &pf->hw;
13012 	struct i40e_vsi_context ctxt;
13013 	struct i40e_mac_filter *f;
13014 	struct hlist_node *h;
13015 	int bkt;
13016 
13017 	u8 enabled_tc = 0x1; /* TC0 enabled */
13018 	int f_count = 0;
13019 
13020 	memset(&ctxt, 0, sizeof(ctxt));
13021 	switch (vsi->type) {
13022 	case I40E_VSI_MAIN:
13023 		/* The PF's main VSI is already setup as part of the
13024 		 * device initialization, so we'll not bother with
13025 		 * the add_vsi call, but we will retrieve the current
13026 		 * VSI context.
13027 		 */
13028 		ctxt.seid = pf->main_vsi_seid;
13029 		ctxt.pf_num = pf->hw.pf_id;
13030 		ctxt.vf_num = 0;
13031 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13032 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13033 		if (ret) {
13034 			dev_info(&pf->pdev->dev,
13035 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13036 				 i40e_stat_str(&pf->hw, ret),
13037 				 i40e_aq_str(&pf->hw,
13038 					     pf->hw.aq.asq_last_status));
13039 			return -ENOENT;
13040 		}
13041 		vsi->info = ctxt.info;
13042 		vsi->info.valid_sections = 0;
13043 
13044 		vsi->seid = ctxt.seid;
13045 		vsi->id = ctxt.vsi_number;
13046 
13047 		enabled_tc = i40e_pf_get_tc_map(pf);
13048 
13049 		/* Source pruning is enabled by default, so the flag is
13050 		 * negative logic - if it's set, we need to fiddle with
13051 		 * the VSI to disable source pruning.
13052 		 */
13053 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13054 			memset(&ctxt, 0, sizeof(ctxt));
13055 			ctxt.seid = pf->main_vsi_seid;
13056 			ctxt.pf_num = pf->hw.pf_id;
13057 			ctxt.vf_num = 0;
13058 			ctxt.info.valid_sections |=
13059 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13060 			ctxt.info.switch_id =
13061 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13062 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13063 			if (ret) {
13064 				dev_info(&pf->pdev->dev,
13065 					 "update vsi failed, err %s aq_err %s\n",
13066 					 i40e_stat_str(&pf->hw, ret),
13067 					 i40e_aq_str(&pf->hw,
13068 						     pf->hw.aq.asq_last_status));
13069 				ret = -ENOENT;
13070 				goto err;
13071 			}
13072 		}
13073 
13074 		/* MFP mode setup queue map and update VSI */
13075 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13076 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13077 			memset(&ctxt, 0, sizeof(ctxt));
13078 			ctxt.seid = pf->main_vsi_seid;
13079 			ctxt.pf_num = pf->hw.pf_id;
13080 			ctxt.vf_num = 0;
13081 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13082 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13083 			if (ret) {
13084 				dev_info(&pf->pdev->dev,
13085 					 "update vsi failed, err %s aq_err %s\n",
13086 					 i40e_stat_str(&pf->hw, ret),
13087 					 i40e_aq_str(&pf->hw,
13088 						    pf->hw.aq.asq_last_status));
13089 				ret = -ENOENT;
13090 				goto err;
13091 			}
13092 			/* update the local VSI info queue map */
13093 			i40e_vsi_update_queue_map(vsi, &ctxt);
13094 			vsi->info.valid_sections = 0;
13095 		} else {
13096 			/* Default/Main VSI is only enabled for TC0
13097 			 * reconfigure it to enable all TCs that are
13098 			 * available on the port in SFP mode.
13099 			 * For MFP case the iSCSI PF would use this
13100 			 * flow to enable LAN+iSCSI TC.
13101 			 */
13102 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13103 			if (ret) {
13104 				/* Single TC condition is not fatal,
13105 				 * message and continue
13106 				 */
13107 				dev_info(&pf->pdev->dev,
13108 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13109 					 enabled_tc,
13110 					 i40e_stat_str(&pf->hw, ret),
13111 					 i40e_aq_str(&pf->hw,
13112 						    pf->hw.aq.asq_last_status));
13113 			}
13114 		}
13115 		break;
13116 
13117 	case I40E_VSI_FDIR:
13118 		ctxt.pf_num = hw->pf_id;
13119 		ctxt.vf_num = 0;
13120 		ctxt.uplink_seid = vsi->uplink_seid;
13121 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13122 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13123 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13124 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13125 			ctxt.info.valid_sections |=
13126 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13127 			ctxt.info.switch_id =
13128 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13129 		}
13130 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13131 		break;
13132 
13133 	case I40E_VSI_VMDQ2:
13134 		ctxt.pf_num = hw->pf_id;
13135 		ctxt.vf_num = 0;
13136 		ctxt.uplink_seid = vsi->uplink_seid;
13137 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13138 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13139 
13140 		/* This VSI is connected to VEB so the switch_id
13141 		 * should be set to zero by default.
13142 		 */
13143 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13144 			ctxt.info.valid_sections |=
13145 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13146 			ctxt.info.switch_id =
13147 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13148 		}
13149 
13150 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13151 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13152 		break;
13153 
13154 	case I40E_VSI_SRIOV:
13155 		ctxt.pf_num = hw->pf_id;
13156 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13157 		ctxt.uplink_seid = vsi->uplink_seid;
13158 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13159 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13160 
13161 		/* This VSI is connected to VEB so the switch_id
13162 		 * should be set to zero by default.
13163 		 */
13164 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13165 			ctxt.info.valid_sections |=
13166 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13167 			ctxt.info.switch_id =
13168 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13169 		}
13170 
13171 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
13172 			ctxt.info.valid_sections |=
13173 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
13174 			ctxt.info.queueing_opt_flags |=
13175 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
13176 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
13177 		}
13178 
13179 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
13180 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
13181 		if (pf->vf[vsi->vf_id].spoofchk) {
13182 			ctxt.info.valid_sections |=
13183 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
13184 			ctxt.info.sec_flags |=
13185 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
13186 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
13187 		}
13188 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13189 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13190 		break;
13191 
13192 	case I40E_VSI_IWARP:
13193 		/* send down message to iWARP */
13194 		break;
13195 
13196 	default:
13197 		return -ENODEV;
13198 	}
13199 
13200 	if (vsi->type != I40E_VSI_MAIN) {
13201 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
13202 		if (ret) {
13203 			dev_info(&vsi->back->pdev->dev,
13204 				 "add vsi failed, err %s aq_err %s\n",
13205 				 i40e_stat_str(&pf->hw, ret),
13206 				 i40e_aq_str(&pf->hw,
13207 					     pf->hw.aq.asq_last_status));
13208 			ret = -ENOENT;
13209 			goto err;
13210 		}
13211 		vsi->info = ctxt.info;
13212 		vsi->info.valid_sections = 0;
13213 		vsi->seid = ctxt.seid;
13214 		vsi->id = ctxt.vsi_number;
13215 	}
13216 
13217 	vsi->active_filters = 0;
13218 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
13219 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13220 	/* If macvlan filters already exist, force them to get loaded */
13221 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
13222 		f->state = I40E_FILTER_NEW;
13223 		f_count++;
13224 	}
13225 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13226 
13227 	if (f_count) {
13228 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
13229 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
13230 	}
13231 
13232 	/* Update VSI BW information */
13233 	ret = i40e_vsi_get_bw_info(vsi);
13234 	if (ret) {
13235 		dev_info(&pf->pdev->dev,
13236 			 "couldn't get vsi bw info, err %s aq_err %s\n",
13237 			 i40e_stat_str(&pf->hw, ret),
13238 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13239 		/* VSI is already added so not tearing that up */
13240 		ret = 0;
13241 	}
13242 
13243 err:
13244 	return ret;
13245 }
13246 
13247 /**
13248  * i40e_vsi_release - Delete a VSI and free its resources
13249  * @vsi: the VSI being removed
13250  *
13251  * Returns 0 on success or < 0 on error
13252  **/
13253 int i40e_vsi_release(struct i40e_vsi *vsi)
13254 {
13255 	struct i40e_mac_filter *f;
13256 	struct hlist_node *h;
13257 	struct i40e_veb *veb = NULL;
13258 	struct i40e_pf *pf;
13259 	u16 uplink_seid;
13260 	int i, n, bkt;
13261 
13262 	pf = vsi->back;
13263 
13264 	/* release of a VEB-owner or last VSI is not allowed */
13265 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
13266 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
13267 			 vsi->seid, vsi->uplink_seid);
13268 		return -ENODEV;
13269 	}
13270 	if (vsi == pf->vsi[pf->lan_vsi] &&
13271 	    !test_bit(__I40E_DOWN, pf->state)) {
13272 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
13273 		return -ENODEV;
13274 	}
13275 
13276 	uplink_seid = vsi->uplink_seid;
13277 	if (vsi->type != I40E_VSI_SRIOV) {
13278 		if (vsi->netdev_registered) {
13279 			vsi->netdev_registered = false;
13280 			if (vsi->netdev) {
13281 				/* results in a call to i40e_close() */
13282 				unregister_netdev(vsi->netdev);
13283 			}
13284 		} else {
13285 			i40e_vsi_close(vsi);
13286 		}
13287 		i40e_vsi_disable_irq(vsi);
13288 	}
13289 
13290 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13291 
13292 	/* clear the sync flag on all filters */
13293 	if (vsi->netdev) {
13294 		__dev_uc_unsync(vsi->netdev, NULL);
13295 		__dev_mc_unsync(vsi->netdev, NULL);
13296 	}
13297 
13298 	/* make sure any remaining filters are marked for deletion */
13299 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
13300 		__i40e_del_filter(vsi, f);
13301 
13302 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13303 
13304 	i40e_sync_vsi_filters(vsi);
13305 
13306 	i40e_vsi_delete(vsi);
13307 	i40e_vsi_free_q_vectors(vsi);
13308 	if (vsi->netdev) {
13309 		free_netdev(vsi->netdev);
13310 		vsi->netdev = NULL;
13311 	}
13312 	i40e_vsi_clear_rings(vsi);
13313 	i40e_vsi_clear(vsi);
13314 
13315 	/* If this was the last thing on the VEB, except for the
13316 	 * controlling VSI, remove the VEB, which puts the controlling
13317 	 * VSI onto the next level down in the switch.
13318 	 *
13319 	 * Well, okay, there's one more exception here: don't remove
13320 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
13321 	 * from up the network stack.
13322 	 */
13323 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
13324 		if (pf->vsi[i] &&
13325 		    pf->vsi[i]->uplink_seid == uplink_seid &&
13326 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13327 			n++;      /* count the VSIs */
13328 		}
13329 	}
13330 	for (i = 0; i < I40E_MAX_VEB; i++) {
13331 		if (!pf->veb[i])
13332 			continue;
13333 		if (pf->veb[i]->uplink_seid == uplink_seid)
13334 			n++;     /* count the VEBs */
13335 		if (pf->veb[i]->seid == uplink_seid)
13336 			veb = pf->veb[i];
13337 	}
13338 	if (n == 0 && veb && veb->uplink_seid != 0)
13339 		i40e_veb_release(veb);
13340 
13341 	return 0;
13342 }
13343 
13344 /**
13345  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
13346  * @vsi: ptr to the VSI
13347  *
13348  * This should only be called after i40e_vsi_mem_alloc() which allocates the
13349  * corresponding SW VSI structure and initializes num_queue_pairs for the
13350  * newly allocated VSI.
13351  *
13352  * Returns 0 on success or negative on failure
13353  **/
13354 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
13355 {
13356 	int ret = -ENOENT;
13357 	struct i40e_pf *pf = vsi->back;
13358 
13359 	if (vsi->q_vectors[0]) {
13360 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
13361 			 vsi->seid);
13362 		return -EEXIST;
13363 	}
13364 
13365 	if (vsi->base_vector) {
13366 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
13367 			 vsi->seid, vsi->base_vector);
13368 		return -EEXIST;
13369 	}
13370 
13371 	ret = i40e_vsi_alloc_q_vectors(vsi);
13372 	if (ret) {
13373 		dev_info(&pf->pdev->dev,
13374 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
13375 			 vsi->num_q_vectors, vsi->seid, ret);
13376 		vsi->num_q_vectors = 0;
13377 		goto vector_setup_out;
13378 	}
13379 
13380 	/* In Legacy mode, we do not have to get any other vector since we
13381 	 * piggyback on the misc/ICR0 for queue interrupts.
13382 	*/
13383 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
13384 		return ret;
13385 	if (vsi->num_q_vectors)
13386 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
13387 						 vsi->num_q_vectors, vsi->idx);
13388 	if (vsi->base_vector < 0) {
13389 		dev_info(&pf->pdev->dev,
13390 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
13391 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
13392 		i40e_vsi_free_q_vectors(vsi);
13393 		ret = -ENOENT;
13394 		goto vector_setup_out;
13395 	}
13396 
13397 vector_setup_out:
13398 	return ret;
13399 }
13400 
13401 /**
13402  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
13403  * @vsi: pointer to the vsi.
13404  *
13405  * This re-allocates a vsi's queue resources.
13406  *
13407  * Returns pointer to the successfully allocated and configured VSI sw struct
13408  * on success, otherwise returns NULL on failure.
13409  **/
13410 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
13411 {
13412 	u16 alloc_queue_pairs;
13413 	struct i40e_pf *pf;
13414 	u8 enabled_tc;
13415 	int ret;
13416 
13417 	if (!vsi)
13418 		return NULL;
13419 
13420 	pf = vsi->back;
13421 
13422 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
13423 	i40e_vsi_clear_rings(vsi);
13424 
13425 	i40e_vsi_free_arrays(vsi, false);
13426 	i40e_set_num_rings_in_vsi(vsi);
13427 	ret = i40e_vsi_alloc_arrays(vsi, false);
13428 	if (ret)
13429 		goto err_vsi;
13430 
13431 	alloc_queue_pairs = vsi->alloc_queue_pairs *
13432 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
13433 
13434 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
13435 	if (ret < 0) {
13436 		dev_info(&pf->pdev->dev,
13437 			 "failed to get tracking for %d queues for VSI %d err %d\n",
13438 			 alloc_queue_pairs, vsi->seid, ret);
13439 		goto err_vsi;
13440 	}
13441 	vsi->base_queue = ret;
13442 
13443 	/* Update the FW view of the VSI. Force a reset of TC and queue
13444 	 * layout configurations.
13445 	 */
13446 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
13447 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
13448 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
13449 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
13450 	if (vsi->type == I40E_VSI_MAIN)
13451 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
13452 
13453 	/* assign it some queues */
13454 	ret = i40e_alloc_rings(vsi);
13455 	if (ret)
13456 		goto err_rings;
13457 
13458 	/* map all of the rings to the q_vectors */
13459 	i40e_vsi_map_rings_to_vectors(vsi);
13460 	return vsi;
13461 
13462 err_rings:
13463 	i40e_vsi_free_q_vectors(vsi);
13464 	if (vsi->netdev_registered) {
13465 		vsi->netdev_registered = false;
13466 		unregister_netdev(vsi->netdev);
13467 		free_netdev(vsi->netdev);
13468 		vsi->netdev = NULL;
13469 	}
13470 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
13471 err_vsi:
13472 	i40e_vsi_clear(vsi);
13473 	return NULL;
13474 }
13475 
13476 /**
13477  * i40e_vsi_setup - Set up a VSI by a given type
13478  * @pf: board private structure
13479  * @type: VSI type
13480  * @uplink_seid: the switch element to link to
13481  * @param1: usage depends upon VSI type. For VF types, indicates VF id
13482  *
13483  * This allocates the sw VSI structure and its queue resources, then add a VSI
13484  * to the identified VEB.
13485  *
13486  * Returns pointer to the successfully allocated and configure VSI sw struct on
13487  * success, otherwise returns NULL on failure.
13488  **/
13489 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
13490 				u16 uplink_seid, u32 param1)
13491 {
13492 	struct i40e_vsi *vsi = NULL;
13493 	struct i40e_veb *veb = NULL;
13494 	u16 alloc_queue_pairs;
13495 	int ret, i;
13496 	int v_idx;
13497 
13498 	/* The requested uplink_seid must be either
13499 	 *     - the PF's port seid
13500 	 *              no VEB is needed because this is the PF
13501 	 *              or this is a Flow Director special case VSI
13502 	 *     - seid of an existing VEB
13503 	 *     - seid of a VSI that owns an existing VEB
13504 	 *     - seid of a VSI that doesn't own a VEB
13505 	 *              a new VEB is created and the VSI becomes the owner
13506 	 *     - seid of the PF VSI, which is what creates the first VEB
13507 	 *              this is a special case of the previous
13508 	 *
13509 	 * Find which uplink_seid we were given and create a new VEB if needed
13510 	 */
13511 	for (i = 0; i < I40E_MAX_VEB; i++) {
13512 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
13513 			veb = pf->veb[i];
13514 			break;
13515 		}
13516 	}
13517 
13518 	if (!veb && uplink_seid != pf->mac_seid) {
13519 
13520 		for (i = 0; i < pf->num_alloc_vsi; i++) {
13521 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
13522 				vsi = pf->vsi[i];
13523 				break;
13524 			}
13525 		}
13526 		if (!vsi) {
13527 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
13528 				 uplink_seid);
13529 			return NULL;
13530 		}
13531 
13532 		if (vsi->uplink_seid == pf->mac_seid)
13533 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
13534 					     vsi->tc_config.enabled_tc);
13535 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
13536 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13537 					     vsi->tc_config.enabled_tc);
13538 		if (veb) {
13539 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
13540 				dev_info(&vsi->back->pdev->dev,
13541 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
13542 				return NULL;
13543 			}
13544 			/* We come up by default in VEPA mode if SRIOV is not
13545 			 * already enabled, in which case we can't force VEPA
13546 			 * mode.
13547 			 */
13548 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
13549 				veb->bridge_mode = BRIDGE_MODE_VEPA;
13550 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13551 			}
13552 			i40e_config_bridge_mode(veb);
13553 		}
13554 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13555 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13556 				veb = pf->veb[i];
13557 		}
13558 		if (!veb) {
13559 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
13560 			return NULL;
13561 		}
13562 
13563 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
13564 		uplink_seid = veb->seid;
13565 	}
13566 
13567 	/* get vsi sw struct */
13568 	v_idx = i40e_vsi_mem_alloc(pf, type);
13569 	if (v_idx < 0)
13570 		goto err_alloc;
13571 	vsi = pf->vsi[v_idx];
13572 	if (!vsi)
13573 		goto err_alloc;
13574 	vsi->type = type;
13575 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
13576 
13577 	if (type == I40E_VSI_MAIN)
13578 		pf->lan_vsi = v_idx;
13579 	else if (type == I40E_VSI_SRIOV)
13580 		vsi->vf_id = param1;
13581 	/* assign it some queues */
13582 	alloc_queue_pairs = vsi->alloc_queue_pairs *
13583 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
13584 
13585 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
13586 	if (ret < 0) {
13587 		dev_info(&pf->pdev->dev,
13588 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
13589 			 alloc_queue_pairs, vsi->seid, ret);
13590 		goto err_vsi;
13591 	}
13592 	vsi->base_queue = ret;
13593 
13594 	/* get a VSI from the hardware */
13595 	vsi->uplink_seid = uplink_seid;
13596 	ret = i40e_add_vsi(vsi);
13597 	if (ret)
13598 		goto err_vsi;
13599 
13600 	switch (vsi->type) {
13601 	/* setup the netdev if needed */
13602 	case I40E_VSI_MAIN:
13603 	case I40E_VSI_VMDQ2:
13604 		ret = i40e_config_netdev(vsi);
13605 		if (ret)
13606 			goto err_netdev;
13607 		ret = register_netdev(vsi->netdev);
13608 		if (ret)
13609 			goto err_netdev;
13610 		vsi->netdev_registered = true;
13611 		netif_carrier_off(vsi->netdev);
13612 #ifdef CONFIG_I40E_DCB
13613 		/* Setup DCB netlink interface */
13614 		i40e_dcbnl_setup(vsi);
13615 #endif /* CONFIG_I40E_DCB */
13616 		fallthrough;
13617 	case I40E_VSI_FDIR:
13618 		/* set up vectors and rings if needed */
13619 		ret = i40e_vsi_setup_vectors(vsi);
13620 		if (ret)
13621 			goto err_msix;
13622 
13623 		ret = i40e_alloc_rings(vsi);
13624 		if (ret)
13625 			goto err_rings;
13626 
13627 		/* map all of the rings to the q_vectors */
13628 		i40e_vsi_map_rings_to_vectors(vsi);
13629 
13630 		i40e_vsi_reset_stats(vsi);
13631 		break;
13632 	default:
13633 		/* no netdev or rings for the other VSI types */
13634 		break;
13635 	}
13636 
13637 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
13638 	    (vsi->type == I40E_VSI_VMDQ2)) {
13639 		ret = i40e_vsi_config_rss(vsi);
13640 	}
13641 	return vsi;
13642 
13643 err_rings:
13644 	i40e_vsi_free_q_vectors(vsi);
13645 err_msix:
13646 	if (vsi->netdev_registered) {
13647 		vsi->netdev_registered = false;
13648 		unregister_netdev(vsi->netdev);
13649 		free_netdev(vsi->netdev);
13650 		vsi->netdev = NULL;
13651 	}
13652 err_netdev:
13653 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
13654 err_vsi:
13655 	i40e_vsi_clear(vsi);
13656 err_alloc:
13657 	return NULL;
13658 }
13659 
13660 /**
13661  * i40e_veb_get_bw_info - Query VEB BW information
13662  * @veb: the veb to query
13663  *
13664  * Query the Tx scheduler BW configuration data for given VEB
13665  **/
13666 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
13667 {
13668 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
13669 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
13670 	struct i40e_pf *pf = veb->pf;
13671 	struct i40e_hw *hw = &pf->hw;
13672 	u32 tc_bw_max;
13673 	int ret = 0;
13674 	int i;
13675 
13676 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
13677 						  &bw_data, NULL);
13678 	if (ret) {
13679 		dev_info(&pf->pdev->dev,
13680 			 "query veb bw config failed, err %s aq_err %s\n",
13681 			 i40e_stat_str(&pf->hw, ret),
13682 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
13683 		goto out;
13684 	}
13685 
13686 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
13687 						   &ets_data, NULL);
13688 	if (ret) {
13689 		dev_info(&pf->pdev->dev,
13690 			 "query veb bw ets config failed, err %s aq_err %s\n",
13691 			 i40e_stat_str(&pf->hw, ret),
13692 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
13693 		goto out;
13694 	}
13695 
13696 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
13697 	veb->bw_max_quanta = ets_data.tc_bw_max;
13698 	veb->is_abs_credits = bw_data.absolute_credits_enable;
13699 	veb->enabled_tc = ets_data.tc_valid_bits;
13700 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
13701 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
13702 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
13703 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
13704 		veb->bw_tc_limit_credits[i] =
13705 					le16_to_cpu(bw_data.tc_bw_limits[i]);
13706 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
13707 	}
13708 
13709 out:
13710 	return ret;
13711 }
13712 
13713 /**
13714  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
13715  * @pf: board private structure
13716  *
13717  * On error: returns error code (negative)
13718  * On success: returns vsi index in PF (positive)
13719  **/
13720 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
13721 {
13722 	int ret = -ENOENT;
13723 	struct i40e_veb *veb;
13724 	int i;
13725 
13726 	/* Need to protect the allocation of switch elements at the PF level */
13727 	mutex_lock(&pf->switch_mutex);
13728 
13729 	/* VEB list may be fragmented if VEB creation/destruction has
13730 	 * been happening.  We can afford to do a quick scan to look
13731 	 * for any free slots in the list.
13732 	 *
13733 	 * find next empty veb slot, looping back around if necessary
13734 	 */
13735 	i = 0;
13736 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
13737 		i++;
13738 	if (i >= I40E_MAX_VEB) {
13739 		ret = -ENOMEM;
13740 		goto err_alloc_veb;  /* out of VEB slots! */
13741 	}
13742 
13743 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
13744 	if (!veb) {
13745 		ret = -ENOMEM;
13746 		goto err_alloc_veb;
13747 	}
13748 	veb->pf = pf;
13749 	veb->idx = i;
13750 	veb->enabled_tc = 1;
13751 
13752 	pf->veb[i] = veb;
13753 	ret = i;
13754 err_alloc_veb:
13755 	mutex_unlock(&pf->switch_mutex);
13756 	return ret;
13757 }
13758 
13759 /**
13760  * i40e_switch_branch_release - Delete a branch of the switch tree
13761  * @branch: where to start deleting
13762  *
13763  * This uses recursion to find the tips of the branch to be
13764  * removed, deleting until we get back to and can delete this VEB.
13765  **/
13766 static void i40e_switch_branch_release(struct i40e_veb *branch)
13767 {
13768 	struct i40e_pf *pf = branch->pf;
13769 	u16 branch_seid = branch->seid;
13770 	u16 veb_idx = branch->idx;
13771 	int i;
13772 
13773 	/* release any VEBs on this VEB - RECURSION */
13774 	for (i = 0; i < I40E_MAX_VEB; i++) {
13775 		if (!pf->veb[i])
13776 			continue;
13777 		if (pf->veb[i]->uplink_seid == branch->seid)
13778 			i40e_switch_branch_release(pf->veb[i]);
13779 	}
13780 
13781 	/* Release the VSIs on this VEB, but not the owner VSI.
13782 	 *
13783 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
13784 	 *       the VEB itself, so don't use (*branch) after this loop.
13785 	 */
13786 	for (i = 0; i < pf->num_alloc_vsi; i++) {
13787 		if (!pf->vsi[i])
13788 			continue;
13789 		if (pf->vsi[i]->uplink_seid == branch_seid &&
13790 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13791 			i40e_vsi_release(pf->vsi[i]);
13792 		}
13793 	}
13794 
13795 	/* There's one corner case where the VEB might not have been
13796 	 * removed, so double check it here and remove it if needed.
13797 	 * This case happens if the veb was created from the debugfs
13798 	 * commands and no VSIs were added to it.
13799 	 */
13800 	if (pf->veb[veb_idx])
13801 		i40e_veb_release(pf->veb[veb_idx]);
13802 }
13803 
13804 /**
13805  * i40e_veb_clear - remove veb struct
13806  * @veb: the veb to remove
13807  **/
13808 static void i40e_veb_clear(struct i40e_veb *veb)
13809 {
13810 	if (!veb)
13811 		return;
13812 
13813 	if (veb->pf) {
13814 		struct i40e_pf *pf = veb->pf;
13815 
13816 		mutex_lock(&pf->switch_mutex);
13817 		if (pf->veb[veb->idx] == veb)
13818 			pf->veb[veb->idx] = NULL;
13819 		mutex_unlock(&pf->switch_mutex);
13820 	}
13821 
13822 	kfree(veb);
13823 }
13824 
13825 /**
13826  * i40e_veb_release - Delete a VEB and free its resources
13827  * @veb: the VEB being removed
13828  **/
13829 void i40e_veb_release(struct i40e_veb *veb)
13830 {
13831 	struct i40e_vsi *vsi = NULL;
13832 	struct i40e_pf *pf;
13833 	int i, n = 0;
13834 
13835 	pf = veb->pf;
13836 
13837 	/* find the remaining VSI and check for extras */
13838 	for (i = 0; i < pf->num_alloc_vsi; i++) {
13839 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
13840 			n++;
13841 			vsi = pf->vsi[i];
13842 		}
13843 	}
13844 	if (n != 1) {
13845 		dev_info(&pf->pdev->dev,
13846 			 "can't remove VEB %d with %d VSIs left\n",
13847 			 veb->seid, n);
13848 		return;
13849 	}
13850 
13851 	/* move the remaining VSI to uplink veb */
13852 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
13853 	if (veb->uplink_seid) {
13854 		vsi->uplink_seid = veb->uplink_seid;
13855 		if (veb->uplink_seid == pf->mac_seid)
13856 			vsi->veb_idx = I40E_NO_VEB;
13857 		else
13858 			vsi->veb_idx = veb->veb_idx;
13859 	} else {
13860 		/* floating VEB */
13861 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
13862 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
13863 	}
13864 
13865 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
13866 	i40e_veb_clear(veb);
13867 }
13868 
13869 /**
13870  * i40e_add_veb - create the VEB in the switch
13871  * @veb: the VEB to be instantiated
13872  * @vsi: the controlling VSI
13873  **/
13874 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
13875 {
13876 	struct i40e_pf *pf = veb->pf;
13877 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
13878 	int ret;
13879 
13880 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
13881 			      veb->enabled_tc, false,
13882 			      &veb->seid, enable_stats, NULL);
13883 
13884 	/* get a VEB from the hardware */
13885 	if (ret) {
13886 		dev_info(&pf->pdev->dev,
13887 			 "couldn't add VEB, err %s aq_err %s\n",
13888 			 i40e_stat_str(&pf->hw, ret),
13889 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13890 		return -EPERM;
13891 	}
13892 
13893 	/* get statistics counter */
13894 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
13895 					 &veb->stats_idx, NULL, NULL, NULL);
13896 	if (ret) {
13897 		dev_info(&pf->pdev->dev,
13898 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
13899 			 i40e_stat_str(&pf->hw, ret),
13900 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13901 		return -EPERM;
13902 	}
13903 	ret = i40e_veb_get_bw_info(veb);
13904 	if (ret) {
13905 		dev_info(&pf->pdev->dev,
13906 			 "couldn't get VEB bw info, err %s aq_err %s\n",
13907 			 i40e_stat_str(&pf->hw, ret),
13908 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13909 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
13910 		return -ENOENT;
13911 	}
13912 
13913 	vsi->uplink_seid = veb->seid;
13914 	vsi->veb_idx = veb->idx;
13915 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
13916 
13917 	return 0;
13918 }
13919 
13920 /**
13921  * i40e_veb_setup - Set up a VEB
13922  * @pf: board private structure
13923  * @flags: VEB setup flags
13924  * @uplink_seid: the switch element to link to
13925  * @vsi_seid: the initial VSI seid
13926  * @enabled_tc: Enabled TC bit-map
13927  *
13928  * This allocates the sw VEB structure and links it into the switch
13929  * It is possible and legal for this to be a duplicate of an already
13930  * existing VEB.  It is also possible for both uplink and vsi seids
13931  * to be zero, in order to create a floating VEB.
13932  *
13933  * Returns pointer to the successfully allocated VEB sw struct on
13934  * success, otherwise returns NULL on failure.
13935  **/
13936 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
13937 				u16 uplink_seid, u16 vsi_seid,
13938 				u8 enabled_tc)
13939 {
13940 	struct i40e_veb *veb, *uplink_veb = NULL;
13941 	int vsi_idx, veb_idx;
13942 	int ret;
13943 
13944 	/* if one seid is 0, the other must be 0 to create a floating relay */
13945 	if ((uplink_seid == 0 || vsi_seid == 0) &&
13946 	    (uplink_seid + vsi_seid != 0)) {
13947 		dev_info(&pf->pdev->dev,
13948 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
13949 			 uplink_seid, vsi_seid);
13950 		return NULL;
13951 	}
13952 
13953 	/* make sure there is such a vsi and uplink */
13954 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
13955 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
13956 			break;
13957 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
13958 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
13959 			 vsi_seid);
13960 		return NULL;
13961 	}
13962 
13963 	if (uplink_seid && uplink_seid != pf->mac_seid) {
13964 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
13965 			if (pf->veb[veb_idx] &&
13966 			    pf->veb[veb_idx]->seid == uplink_seid) {
13967 				uplink_veb = pf->veb[veb_idx];
13968 				break;
13969 			}
13970 		}
13971 		if (!uplink_veb) {
13972 			dev_info(&pf->pdev->dev,
13973 				 "uplink seid %d not found\n", uplink_seid);
13974 			return NULL;
13975 		}
13976 	}
13977 
13978 	/* get veb sw struct */
13979 	veb_idx = i40e_veb_mem_alloc(pf);
13980 	if (veb_idx < 0)
13981 		goto err_alloc;
13982 	veb = pf->veb[veb_idx];
13983 	veb->flags = flags;
13984 	veb->uplink_seid = uplink_seid;
13985 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
13986 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
13987 
13988 	/* create the VEB in the switch */
13989 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
13990 	if (ret)
13991 		goto err_veb;
13992 	if (vsi_idx == pf->lan_vsi)
13993 		pf->lan_veb = veb->idx;
13994 
13995 	return veb;
13996 
13997 err_veb:
13998 	i40e_veb_clear(veb);
13999 err_alloc:
14000 	return NULL;
14001 }
14002 
14003 /**
14004  * i40e_setup_pf_switch_element - set PF vars based on switch type
14005  * @pf: board private structure
14006  * @ele: element we are building info from
14007  * @num_reported: total number of elements
14008  * @printconfig: should we print the contents
14009  *
14010  * helper function to assist in extracting a few useful SEID values.
14011  **/
14012 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14013 				struct i40e_aqc_switch_config_element_resp *ele,
14014 				u16 num_reported, bool printconfig)
14015 {
14016 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14017 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14018 	u8 element_type = ele->element_type;
14019 	u16 seid = le16_to_cpu(ele->seid);
14020 
14021 	if (printconfig)
14022 		dev_info(&pf->pdev->dev,
14023 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14024 			 element_type, seid, uplink_seid, downlink_seid);
14025 
14026 	switch (element_type) {
14027 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14028 		pf->mac_seid = seid;
14029 		break;
14030 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14031 		/* Main VEB? */
14032 		if (uplink_seid != pf->mac_seid)
14033 			break;
14034 		if (pf->lan_veb >= I40E_MAX_VEB) {
14035 			int v;
14036 
14037 			/* find existing or else empty VEB */
14038 			for (v = 0; v < I40E_MAX_VEB; v++) {
14039 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14040 					pf->lan_veb = v;
14041 					break;
14042 				}
14043 			}
14044 			if (pf->lan_veb >= I40E_MAX_VEB) {
14045 				v = i40e_veb_mem_alloc(pf);
14046 				if (v < 0)
14047 					break;
14048 				pf->lan_veb = v;
14049 			}
14050 		}
14051 		if (pf->lan_veb >= I40E_MAX_VEB)
14052 			break;
14053 
14054 		pf->veb[pf->lan_veb]->seid = seid;
14055 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14056 		pf->veb[pf->lan_veb]->pf = pf;
14057 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14058 		break;
14059 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14060 		if (num_reported != 1)
14061 			break;
14062 		/* This is immediately after a reset so we can assume this is
14063 		 * the PF's VSI
14064 		 */
14065 		pf->mac_seid = uplink_seid;
14066 		pf->pf_seid = downlink_seid;
14067 		pf->main_vsi_seid = seid;
14068 		if (printconfig)
14069 			dev_info(&pf->pdev->dev,
14070 				 "pf_seid=%d main_vsi_seid=%d\n",
14071 				 pf->pf_seid, pf->main_vsi_seid);
14072 		break;
14073 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14074 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14075 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14076 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14077 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14078 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14079 		/* ignore these for now */
14080 		break;
14081 	default:
14082 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14083 			 element_type, seid);
14084 		break;
14085 	}
14086 }
14087 
14088 /**
14089  * i40e_fetch_switch_configuration - Get switch config from firmware
14090  * @pf: board private structure
14091  * @printconfig: should we print the contents
14092  *
14093  * Get the current switch configuration from the device and
14094  * extract a few useful SEID values.
14095  **/
14096 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14097 {
14098 	struct i40e_aqc_get_switch_config_resp *sw_config;
14099 	u16 next_seid = 0;
14100 	int ret = 0;
14101 	u8 *aq_buf;
14102 	int i;
14103 
14104 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14105 	if (!aq_buf)
14106 		return -ENOMEM;
14107 
14108 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14109 	do {
14110 		u16 num_reported, num_total;
14111 
14112 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14113 						I40E_AQ_LARGE_BUF,
14114 						&next_seid, NULL);
14115 		if (ret) {
14116 			dev_info(&pf->pdev->dev,
14117 				 "get switch config failed err %s aq_err %s\n",
14118 				 i40e_stat_str(&pf->hw, ret),
14119 				 i40e_aq_str(&pf->hw,
14120 					     pf->hw.aq.asq_last_status));
14121 			kfree(aq_buf);
14122 			return -ENOENT;
14123 		}
14124 
14125 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14126 		num_total = le16_to_cpu(sw_config->header.num_total);
14127 
14128 		if (printconfig)
14129 			dev_info(&pf->pdev->dev,
14130 				 "header: %d reported %d total\n",
14131 				 num_reported, num_total);
14132 
14133 		for (i = 0; i < num_reported; i++) {
14134 			struct i40e_aqc_switch_config_element_resp *ele =
14135 				&sw_config->element[i];
14136 
14137 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14138 						     printconfig);
14139 		}
14140 	} while (next_seid != 0);
14141 
14142 	kfree(aq_buf);
14143 	return ret;
14144 }
14145 
14146 /**
14147  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14148  * @pf: board private structure
14149  * @reinit: if the Main VSI needs to re-initialized.
14150  *
14151  * Returns 0 on success, negative value on failure
14152  **/
14153 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
14154 {
14155 	u16 flags = 0;
14156 	int ret;
14157 
14158 	/* find out what's out there already */
14159 	ret = i40e_fetch_switch_configuration(pf, false);
14160 	if (ret) {
14161 		dev_info(&pf->pdev->dev,
14162 			 "couldn't fetch switch config, err %s aq_err %s\n",
14163 			 i40e_stat_str(&pf->hw, ret),
14164 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14165 		return ret;
14166 	}
14167 	i40e_pf_reset_stats(pf);
14168 
14169 	/* set the switch config bit for the whole device to
14170 	 * support limited promisc or true promisc
14171 	 * when user requests promisc. The default is limited
14172 	 * promisc.
14173 	*/
14174 
14175 	if ((pf->hw.pf_id == 0) &&
14176 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
14177 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14178 		pf->last_sw_conf_flags = flags;
14179 	}
14180 
14181 	if (pf->hw.pf_id == 0) {
14182 		u16 valid_flags;
14183 
14184 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14185 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
14186 						NULL);
14187 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
14188 			dev_info(&pf->pdev->dev,
14189 				 "couldn't set switch config bits, err %s aq_err %s\n",
14190 				 i40e_stat_str(&pf->hw, ret),
14191 				 i40e_aq_str(&pf->hw,
14192 					     pf->hw.aq.asq_last_status));
14193 			/* not a fatal problem, just keep going */
14194 		}
14195 		pf->last_sw_conf_valid_flags = valid_flags;
14196 	}
14197 
14198 	/* first time setup */
14199 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
14200 		struct i40e_vsi *vsi = NULL;
14201 		u16 uplink_seid;
14202 
14203 		/* Set up the PF VSI associated with the PF's main VSI
14204 		 * that is already in the HW switch
14205 		 */
14206 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
14207 			uplink_seid = pf->veb[pf->lan_veb]->seid;
14208 		else
14209 			uplink_seid = pf->mac_seid;
14210 		if (pf->lan_vsi == I40E_NO_VSI)
14211 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
14212 		else if (reinit)
14213 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
14214 		if (!vsi) {
14215 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
14216 			i40e_cloud_filter_exit(pf);
14217 			i40e_fdir_teardown(pf);
14218 			return -EAGAIN;
14219 		}
14220 	} else {
14221 		/* force a reset of TC and queue layout configurations */
14222 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14223 
14224 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14225 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14226 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14227 	}
14228 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
14229 
14230 	i40e_fdir_sb_setup(pf);
14231 
14232 	/* Setup static PF queue filter control settings */
14233 	ret = i40e_setup_pf_filter_control(pf);
14234 	if (ret) {
14235 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
14236 			 ret);
14237 		/* Failure here should not stop continuing other steps */
14238 	}
14239 
14240 	/* enable RSS in the HW, even for only one queue, as the stack can use
14241 	 * the hash
14242 	 */
14243 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
14244 		i40e_pf_config_rss(pf);
14245 
14246 	/* fill in link information and enable LSE reporting */
14247 	i40e_link_event(pf);
14248 
14249 	/* Initialize user-specific link properties */
14250 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
14251 				  I40E_AQ_AN_COMPLETED) ? true : false);
14252 
14253 	i40e_ptp_init(pf);
14254 
14255 	/* repopulate tunnel port filters */
14256 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
14257 
14258 	return ret;
14259 }
14260 
14261 /**
14262  * i40e_determine_queue_usage - Work out queue distribution
14263  * @pf: board private structure
14264  **/
14265 static void i40e_determine_queue_usage(struct i40e_pf *pf)
14266 {
14267 	int queues_left;
14268 	int q_max;
14269 
14270 	pf->num_lan_qps = 0;
14271 
14272 	/* Find the max queues to be put into basic use.  We'll always be
14273 	 * using TC0, whether or not DCB is running, and TC0 will get the
14274 	 * big RSS set.
14275 	 */
14276 	queues_left = pf->hw.func_caps.num_tx_qp;
14277 
14278 	if ((queues_left == 1) ||
14279 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
14280 		/* one qp for PF, no queues for anything else */
14281 		queues_left = 0;
14282 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14283 
14284 		/* make sure all the fancies are disabled */
14285 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14286 			       I40E_FLAG_IWARP_ENABLED	|
14287 			       I40E_FLAG_FD_SB_ENABLED	|
14288 			       I40E_FLAG_FD_ATR_ENABLED	|
14289 			       I40E_FLAG_DCB_CAPABLE	|
14290 			       I40E_FLAG_DCB_ENABLED	|
14291 			       I40E_FLAG_SRIOV_ENABLED	|
14292 			       I40E_FLAG_VMDQ_ENABLED);
14293 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14294 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
14295 				  I40E_FLAG_FD_SB_ENABLED |
14296 				  I40E_FLAG_FD_ATR_ENABLED |
14297 				  I40E_FLAG_DCB_CAPABLE))) {
14298 		/* one qp for PF */
14299 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14300 		queues_left -= pf->num_lan_qps;
14301 
14302 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14303 			       I40E_FLAG_IWARP_ENABLED	|
14304 			       I40E_FLAG_FD_SB_ENABLED	|
14305 			       I40E_FLAG_FD_ATR_ENABLED	|
14306 			       I40E_FLAG_DCB_ENABLED	|
14307 			       I40E_FLAG_VMDQ_ENABLED);
14308 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14309 	} else {
14310 		/* Not enough queues for all TCs */
14311 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
14312 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
14313 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
14314 					I40E_FLAG_DCB_ENABLED);
14315 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
14316 		}
14317 
14318 		/* limit lan qps to the smaller of qps, cpus or msix */
14319 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
14320 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
14321 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
14322 		pf->num_lan_qps = q_max;
14323 
14324 		queues_left -= pf->num_lan_qps;
14325 	}
14326 
14327 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14328 		if (queues_left > 1) {
14329 			queues_left -= 1; /* save 1 queue for FD */
14330 		} else {
14331 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
14332 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14333 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
14334 		}
14335 	}
14336 
14337 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
14338 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
14339 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
14340 					(queues_left / pf->num_vf_qps));
14341 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
14342 	}
14343 
14344 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
14345 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
14346 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
14347 					  (queues_left / pf->num_vmdq_qps));
14348 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
14349 	}
14350 
14351 	pf->queues_left = queues_left;
14352 	dev_dbg(&pf->pdev->dev,
14353 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
14354 		pf->hw.func_caps.num_tx_qp,
14355 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
14356 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
14357 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
14358 		queues_left);
14359 }
14360 
14361 /**
14362  * i40e_setup_pf_filter_control - Setup PF static filter control
14363  * @pf: PF to be setup
14364  *
14365  * i40e_setup_pf_filter_control sets up a PF's initial filter control
14366  * settings. If PE/FCoE are enabled then it will also set the per PF
14367  * based filter sizes required for them. It also enables Flow director,
14368  * ethertype and macvlan type filter settings for the pf.
14369  *
14370  * Returns 0 on success, negative on failure
14371  **/
14372 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
14373 {
14374 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
14375 
14376 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
14377 
14378 	/* Flow Director is enabled */
14379 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
14380 		settings->enable_fdir = true;
14381 
14382 	/* Ethtype and MACVLAN filters enabled for PF */
14383 	settings->enable_ethtype = true;
14384 	settings->enable_macvlan = true;
14385 
14386 	if (i40e_set_filter_control(&pf->hw, settings))
14387 		return -ENOENT;
14388 
14389 	return 0;
14390 }
14391 
14392 #define INFO_STRING_LEN 255
14393 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
14394 static void i40e_print_features(struct i40e_pf *pf)
14395 {
14396 	struct i40e_hw *hw = &pf->hw;
14397 	char *buf;
14398 	int i;
14399 
14400 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
14401 	if (!buf)
14402 		return;
14403 
14404 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
14405 #ifdef CONFIG_PCI_IOV
14406 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
14407 #endif
14408 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
14409 		      pf->hw.func_caps.num_vsis,
14410 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
14411 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
14412 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
14413 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
14414 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
14415 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14416 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
14417 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
14418 	}
14419 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
14420 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
14421 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
14422 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
14423 	if (pf->flags & I40E_FLAG_PTP)
14424 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
14425 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
14426 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
14427 	else
14428 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
14429 
14430 	dev_info(&pf->pdev->dev, "%s\n", buf);
14431 	kfree(buf);
14432 	WARN_ON(i > INFO_STRING_LEN);
14433 }
14434 
14435 /**
14436  * i40e_get_platform_mac_addr - get platform-specific MAC address
14437  * @pdev: PCI device information struct
14438  * @pf: board private structure
14439  *
14440  * Look up the MAC address for the device. First we'll try
14441  * eth_platform_get_mac_address, which will check Open Firmware, or arch
14442  * specific fallback. Otherwise, we'll default to the stored value in
14443  * firmware.
14444  **/
14445 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
14446 {
14447 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
14448 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
14449 }
14450 
14451 /**
14452  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
14453  * @fec_cfg: FEC option to set in flags
14454  * @flags: ptr to flags in which we set FEC option
14455  **/
14456 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
14457 {
14458 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
14459 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
14460 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
14461 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
14462 		*flags |= I40E_FLAG_RS_FEC;
14463 		*flags &= ~I40E_FLAG_BASE_R_FEC;
14464 	}
14465 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
14466 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
14467 		*flags |= I40E_FLAG_BASE_R_FEC;
14468 		*flags &= ~I40E_FLAG_RS_FEC;
14469 	}
14470 	if (fec_cfg == 0)
14471 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
14472 }
14473 
14474 /**
14475  * i40e_check_recovery_mode - check if we are running transition firmware
14476  * @pf: board private structure
14477  *
14478  * Check registers indicating the firmware runs in recovery mode. Sets the
14479  * appropriate driver state.
14480  *
14481  * Returns true if the recovery mode was detected, false otherwise
14482  **/
14483 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
14484 {
14485 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
14486 
14487 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
14488 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
14489 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
14490 		set_bit(__I40E_RECOVERY_MODE, pf->state);
14491 
14492 		return true;
14493 	}
14494 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
14495 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
14496 
14497 	return false;
14498 }
14499 
14500 /**
14501  * i40e_pf_loop_reset - perform reset in a loop.
14502  * @pf: board private structure
14503  *
14504  * This function is useful when a NIC is about to enter recovery mode.
14505  * When a NIC's internal data structures are corrupted the NIC's
14506  * firmware is going to enter recovery mode.
14507  * Right after a POR it takes about 7 minutes for firmware to enter
14508  * recovery mode. Until that time a NIC is in some kind of intermediate
14509  * state. After that time period the NIC almost surely enters
14510  * recovery mode. The only way for a driver to detect intermediate
14511  * state is to issue a series of pf-resets and check a return value.
14512  * If a PF reset returns success then the firmware could be in recovery
14513  * mode so the caller of this code needs to check for recovery mode
14514  * if this function returns success. There is a little chance that
14515  * firmware will hang in intermediate state forever.
14516  * Since waiting 7 minutes is quite a lot of time this function waits
14517  * 10 seconds and then gives up by returning an error.
14518  *
14519  * Return 0 on success, negative on failure.
14520  **/
14521 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
14522 {
14523 	/* wait max 10 seconds for PF reset to succeed */
14524 	const unsigned long time_end = jiffies + 10 * HZ;
14525 
14526 	struct i40e_hw *hw = &pf->hw;
14527 	i40e_status ret;
14528 
14529 	ret = i40e_pf_reset(hw);
14530 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
14531 		usleep_range(10000, 20000);
14532 		ret = i40e_pf_reset(hw);
14533 	}
14534 
14535 	if (ret == I40E_SUCCESS)
14536 		pf->pfr_count++;
14537 	else
14538 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
14539 
14540 	return ret;
14541 }
14542 
14543 /**
14544  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
14545  * @pf: board private structure
14546  *
14547  * Check FW registers to determine if FW issued unexpected EMP Reset.
14548  * Every time when unexpected EMP Reset occurs the FW increments
14549  * a counter of unexpected EMP Resets. When the counter reaches 10
14550  * the FW should enter the Recovery mode
14551  *
14552  * Returns true if FW issued unexpected EMP Reset
14553  **/
14554 static bool i40e_check_fw_empr(struct i40e_pf *pf)
14555 {
14556 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
14557 			   I40E_GL_FWSTS_FWS1B_MASK;
14558 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
14559 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
14560 }
14561 
14562 /**
14563  * i40e_handle_resets - handle EMP resets and PF resets
14564  * @pf: board private structure
14565  *
14566  * Handle both EMP resets and PF resets and conclude whether there are
14567  * any issues regarding these resets. If there are any issues then
14568  * generate log entry.
14569  *
14570  * Return 0 if NIC is healthy or negative value when there are issues
14571  * with resets
14572  **/
14573 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
14574 {
14575 	const i40e_status pfr = i40e_pf_loop_reset(pf);
14576 	const bool is_empr = i40e_check_fw_empr(pf);
14577 
14578 	if (is_empr || pfr != I40E_SUCCESS)
14579 		dev_crit(&pf->pdev->dev, "Entering recovery mode due to repeated FW resets. This may take several minutes. Refer to the Intel(R) Ethernet Adapters and Devices User Guide.\n");
14580 
14581 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
14582 }
14583 
14584 /**
14585  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
14586  * @pf: board private structure
14587  * @hw: ptr to the hardware info
14588  *
14589  * This function does a minimal setup of all subsystems needed for running
14590  * recovery mode.
14591  *
14592  * Returns 0 on success, negative on failure
14593  **/
14594 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
14595 {
14596 	struct i40e_vsi *vsi;
14597 	int err;
14598 	int v_idx;
14599 
14600 	pci_save_state(pf->pdev);
14601 
14602 	/* set up periodic task facility */
14603 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
14604 	pf->service_timer_period = HZ;
14605 
14606 	INIT_WORK(&pf->service_task, i40e_service_task);
14607 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
14608 
14609 	err = i40e_init_interrupt_scheme(pf);
14610 	if (err)
14611 		goto err_switch_setup;
14612 
14613 	/* The number of VSIs reported by the FW is the minimum guaranteed
14614 	 * to us; HW supports far more and we share the remaining pool with
14615 	 * the other PFs. We allocate space for more than the guarantee with
14616 	 * the understanding that we might not get them all later.
14617 	 */
14618 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
14619 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
14620 	else
14621 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
14622 
14623 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
14624 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
14625 			  GFP_KERNEL);
14626 	if (!pf->vsi) {
14627 		err = -ENOMEM;
14628 		goto err_switch_setup;
14629 	}
14630 
14631 	/* We allocate one VSI which is needed as absolute minimum
14632 	 * in order to register the netdev
14633 	 */
14634 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
14635 	if (v_idx < 0)
14636 		goto err_switch_setup;
14637 	pf->lan_vsi = v_idx;
14638 	vsi = pf->vsi[v_idx];
14639 	if (!vsi)
14640 		goto err_switch_setup;
14641 	vsi->alloc_queue_pairs = 1;
14642 	err = i40e_config_netdev(vsi);
14643 	if (err)
14644 		goto err_switch_setup;
14645 	err = register_netdev(vsi->netdev);
14646 	if (err)
14647 		goto err_switch_setup;
14648 	vsi->netdev_registered = true;
14649 	i40e_dbg_pf_init(pf);
14650 
14651 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
14652 	if (err)
14653 		goto err_switch_setup;
14654 
14655 	/* tell the firmware that we're starting */
14656 	i40e_send_version(pf);
14657 
14658 	/* since everything's happy, start the service_task timer */
14659 	mod_timer(&pf->service_timer,
14660 		  round_jiffies(jiffies + pf->service_timer_period));
14661 
14662 	return 0;
14663 
14664 err_switch_setup:
14665 	i40e_reset_interrupt_capability(pf);
14666 	del_timer_sync(&pf->service_timer);
14667 	i40e_shutdown_adminq(hw);
14668 	iounmap(hw->hw_addr);
14669 	pci_disable_pcie_error_reporting(pf->pdev);
14670 	pci_release_mem_regions(pf->pdev);
14671 	pci_disable_device(pf->pdev);
14672 	kfree(pf);
14673 
14674 	return err;
14675 }
14676 
14677 /**
14678  * i40e_probe - Device initialization routine
14679  * @pdev: PCI device information struct
14680  * @ent: entry in i40e_pci_tbl
14681  *
14682  * i40e_probe initializes a PF identified by a pci_dev structure.
14683  * The OS initialization, configuring of the PF private structure,
14684  * and a hardware reset occur.
14685  *
14686  * Returns 0 on success, negative on failure
14687  **/
14688 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
14689 {
14690 	struct i40e_aq_get_phy_abilities_resp abilities;
14691 	struct i40e_pf *pf;
14692 	struct i40e_hw *hw;
14693 	static u16 pfs_found;
14694 	u16 wol_nvm_bits;
14695 	u16 link_status;
14696 	int err;
14697 	u32 val;
14698 	u32 i;
14699 	u8 set_fc_aq_fail;
14700 
14701 	err = pci_enable_device_mem(pdev);
14702 	if (err)
14703 		return err;
14704 
14705 	/* set up for high or low dma */
14706 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
14707 	if (err) {
14708 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
14709 		if (err) {
14710 			dev_err(&pdev->dev,
14711 				"DMA configuration failed: 0x%x\n", err);
14712 			goto err_dma;
14713 		}
14714 	}
14715 
14716 	/* set up pci connections */
14717 	err = pci_request_mem_regions(pdev, i40e_driver_name);
14718 	if (err) {
14719 		dev_info(&pdev->dev,
14720 			 "pci_request_selected_regions failed %d\n", err);
14721 		goto err_pci_reg;
14722 	}
14723 
14724 	pci_enable_pcie_error_reporting(pdev);
14725 	pci_set_master(pdev);
14726 
14727 	/* Now that we have a PCI connection, we need to do the
14728 	 * low level device setup.  This is primarily setting up
14729 	 * the Admin Queue structures and then querying for the
14730 	 * device's current profile information.
14731 	 */
14732 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
14733 	if (!pf) {
14734 		err = -ENOMEM;
14735 		goto err_pf_alloc;
14736 	}
14737 	pf->next_vsi = 0;
14738 	pf->pdev = pdev;
14739 	set_bit(__I40E_DOWN, pf->state);
14740 
14741 	hw = &pf->hw;
14742 	hw->back = pf;
14743 
14744 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
14745 				I40E_MAX_CSR_SPACE);
14746 	/* We believe that the highest register to read is
14747 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
14748 	 * is not less than that before mapping to prevent a
14749 	 * kernel panic.
14750 	 */
14751 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
14752 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
14753 			pf->ioremap_len);
14754 		err = -ENOMEM;
14755 		goto err_ioremap;
14756 	}
14757 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
14758 	if (!hw->hw_addr) {
14759 		err = -EIO;
14760 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
14761 			 (unsigned int)pci_resource_start(pdev, 0),
14762 			 pf->ioremap_len, err);
14763 		goto err_ioremap;
14764 	}
14765 	hw->vendor_id = pdev->vendor;
14766 	hw->device_id = pdev->device;
14767 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
14768 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
14769 	hw->subsystem_device_id = pdev->subsystem_device;
14770 	hw->bus.device = PCI_SLOT(pdev->devfn);
14771 	hw->bus.func = PCI_FUNC(pdev->devfn);
14772 	hw->bus.bus_id = pdev->bus->number;
14773 	pf->instance = pfs_found;
14774 
14775 	/* Select something other than the 802.1ad ethertype for the
14776 	 * switch to use internally and drop on ingress.
14777 	 */
14778 	hw->switch_tag = 0xffff;
14779 	hw->first_tag = ETH_P_8021AD;
14780 	hw->second_tag = ETH_P_8021Q;
14781 
14782 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
14783 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
14784 	INIT_LIST_HEAD(&pf->ddp_old_prof);
14785 
14786 	/* set up the locks for the AQ, do this only once in probe
14787 	 * and destroy them only once in remove
14788 	 */
14789 	mutex_init(&hw->aq.asq_mutex);
14790 	mutex_init(&hw->aq.arq_mutex);
14791 
14792 	pf->msg_enable = netif_msg_init(debug,
14793 					NETIF_MSG_DRV |
14794 					NETIF_MSG_PROBE |
14795 					NETIF_MSG_LINK);
14796 	if (debug < -1)
14797 		pf->hw.debug_mask = debug;
14798 
14799 	/* do a special CORER for clearing PXE mode once at init */
14800 	if (hw->revision_id == 0 &&
14801 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
14802 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
14803 		i40e_flush(hw);
14804 		msleep(200);
14805 		pf->corer_count++;
14806 
14807 		i40e_clear_pxe_mode(hw);
14808 	}
14809 
14810 	/* Reset here to make sure all is clean and to define PF 'n' */
14811 	i40e_clear_hw(hw);
14812 
14813 	err = i40e_set_mac_type(hw);
14814 	if (err) {
14815 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
14816 			 err);
14817 		goto err_pf_reset;
14818 	}
14819 
14820 	err = i40e_handle_resets(pf);
14821 	if (err)
14822 		goto err_pf_reset;
14823 
14824 	i40e_check_recovery_mode(pf);
14825 
14826 	hw->aq.num_arq_entries = I40E_AQ_LEN;
14827 	hw->aq.num_asq_entries = I40E_AQ_LEN;
14828 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
14829 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
14830 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
14831 
14832 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
14833 		 "%s-%s:misc",
14834 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
14835 
14836 	err = i40e_init_shared_code(hw);
14837 	if (err) {
14838 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
14839 			 err);
14840 		goto err_pf_reset;
14841 	}
14842 
14843 	/* set up a default setting for link flow control */
14844 	pf->hw.fc.requested_mode = I40E_FC_NONE;
14845 
14846 	err = i40e_init_adminq(hw);
14847 	if (err) {
14848 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
14849 			dev_info(&pdev->dev,
14850 				 "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",
14851 				 hw->aq.api_maj_ver,
14852 				 hw->aq.api_min_ver,
14853 				 I40E_FW_API_VERSION_MAJOR,
14854 				 I40E_FW_MINOR_VERSION(hw));
14855 		else
14856 			dev_info(&pdev->dev,
14857 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
14858 
14859 		goto err_pf_reset;
14860 	}
14861 	i40e_get_oem_version(hw);
14862 
14863 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
14864 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
14865 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
14866 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
14867 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
14868 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
14869 
14870 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
14871 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
14872 		dev_info(&pdev->dev,
14873 			 "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",
14874 			 hw->aq.api_maj_ver,
14875 			 hw->aq.api_min_ver,
14876 			 I40E_FW_API_VERSION_MAJOR,
14877 			 I40E_FW_MINOR_VERSION(hw));
14878 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
14879 		dev_info(&pdev->dev,
14880 			 "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",
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 
14886 	i40e_verify_eeprom(pf);
14887 
14888 	/* Rev 0 hardware was never productized */
14889 	if (hw->revision_id < 1)
14890 		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");
14891 
14892 	i40e_clear_pxe_mode(hw);
14893 
14894 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
14895 	if (err)
14896 		goto err_adminq_setup;
14897 
14898 	err = i40e_sw_init(pf);
14899 	if (err) {
14900 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
14901 		goto err_sw_init;
14902 	}
14903 
14904 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
14905 		return i40e_init_recovery_mode(pf, hw);
14906 
14907 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
14908 				hw->func_caps.num_rx_qp, 0, 0);
14909 	if (err) {
14910 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
14911 		goto err_init_lan_hmc;
14912 	}
14913 
14914 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
14915 	if (err) {
14916 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
14917 		err = -ENOENT;
14918 		goto err_configure_lan_hmc;
14919 	}
14920 
14921 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
14922 	 * Ignore error return codes because if it was already disabled via
14923 	 * hardware settings this will fail
14924 	 */
14925 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
14926 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
14927 		i40e_aq_stop_lldp(hw, true, false, NULL);
14928 	}
14929 
14930 	/* allow a platform config to override the HW addr */
14931 	i40e_get_platform_mac_addr(pdev, pf);
14932 
14933 	if (!is_valid_ether_addr(hw->mac.addr)) {
14934 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
14935 		err = -EIO;
14936 		goto err_mac_addr;
14937 	}
14938 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
14939 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
14940 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
14941 	if (is_valid_ether_addr(hw->mac.port_addr))
14942 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
14943 
14944 	pci_set_drvdata(pdev, pf);
14945 	pci_save_state(pdev);
14946 
14947 	dev_info(&pdev->dev,
14948 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
14949 			"FW LLDP is disabled\n" :
14950 			"FW LLDP is enabled\n");
14951 
14952 	/* Enable FW to write default DCB config on link-up */
14953 	i40e_aq_set_dcb_parameters(hw, true, NULL);
14954 
14955 #ifdef CONFIG_I40E_DCB
14956 	err = i40e_init_pf_dcb(pf);
14957 	if (err) {
14958 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
14959 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
14960 		/* Continue without DCB enabled */
14961 	}
14962 #endif /* CONFIG_I40E_DCB */
14963 
14964 	/* set up periodic task facility */
14965 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
14966 	pf->service_timer_period = HZ;
14967 
14968 	INIT_WORK(&pf->service_task, i40e_service_task);
14969 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
14970 
14971 	/* NVM bit on means WoL disabled for the port */
14972 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
14973 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
14974 		pf->wol_en = false;
14975 	else
14976 		pf->wol_en = true;
14977 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
14978 
14979 	/* set up the main switch operations */
14980 	i40e_determine_queue_usage(pf);
14981 	err = i40e_init_interrupt_scheme(pf);
14982 	if (err)
14983 		goto err_switch_setup;
14984 
14985 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
14986 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
14987 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
14988 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
14989 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
14990 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
14991 						    UDP_TUNNEL_TYPE_GENEVE;
14992 
14993 	/* The number of VSIs reported by the FW is the minimum guaranteed
14994 	 * to us; HW supports far more and we share the remaining pool with
14995 	 * the other PFs. We allocate space for more than the guarantee with
14996 	 * the understanding that we might not get them all later.
14997 	 */
14998 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
14999 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15000 	else
15001 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15002 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15003 		dev_warn(&pf->pdev->dev,
15004 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15005 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15006 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15007 	}
15008 
15009 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15010 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15011 			  GFP_KERNEL);
15012 	if (!pf->vsi) {
15013 		err = -ENOMEM;
15014 		goto err_switch_setup;
15015 	}
15016 
15017 #ifdef CONFIG_PCI_IOV
15018 	/* prep for VF support */
15019 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15020 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15021 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15022 		if (pci_num_vf(pdev))
15023 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15024 	}
15025 #endif
15026 	err = i40e_setup_pf_switch(pf, false);
15027 	if (err) {
15028 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15029 		goto err_vsis;
15030 	}
15031 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15032 
15033 	/* Make sure flow control is set according to current settings */
15034 	err = i40e_set_fc(hw, &set_fc_aq_fail, true);
15035 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
15036 		dev_dbg(&pf->pdev->dev,
15037 			"Set fc with err %s aq_err %s on get_phy_cap\n",
15038 			i40e_stat_str(hw, err),
15039 			i40e_aq_str(hw, hw->aq.asq_last_status));
15040 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
15041 		dev_dbg(&pf->pdev->dev,
15042 			"Set fc with err %s aq_err %s on set_phy_config\n",
15043 			i40e_stat_str(hw, err),
15044 			i40e_aq_str(hw, hw->aq.asq_last_status));
15045 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
15046 		dev_dbg(&pf->pdev->dev,
15047 			"Set fc with err %s aq_err %s on get_link_info\n",
15048 			i40e_stat_str(hw, err),
15049 			i40e_aq_str(hw, hw->aq.asq_last_status));
15050 
15051 	/* if FDIR VSI was set up, start it now */
15052 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15053 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15054 			i40e_vsi_open(pf->vsi[i]);
15055 			break;
15056 		}
15057 	}
15058 
15059 	/* The driver only wants link up/down and module qualification
15060 	 * reports from firmware.  Note the negative logic.
15061 	 */
15062 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15063 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15064 					 I40E_AQ_EVENT_MEDIA_NA |
15065 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15066 	if (err)
15067 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15068 			 i40e_stat_str(&pf->hw, err),
15069 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15070 
15071 	/* Reconfigure hardware for allowing smaller MSS in the case
15072 	 * of TSO, so that we avoid the MDD being fired and causing
15073 	 * a reset in the case of small MSS+TSO.
15074 	 */
15075 	val = rd32(hw, I40E_REG_MSS);
15076 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15077 		val &= ~I40E_REG_MSS_MIN_MASK;
15078 		val |= I40E_64BYTE_MSS;
15079 		wr32(hw, I40E_REG_MSS, val);
15080 	}
15081 
15082 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15083 		msleep(75);
15084 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15085 		if (err)
15086 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15087 				 i40e_stat_str(&pf->hw, err),
15088 				 i40e_aq_str(&pf->hw,
15089 					     pf->hw.aq.asq_last_status));
15090 	}
15091 	/* The main driver is (mostly) up and happy. We need to set this state
15092 	 * before setting up the misc vector or we get a race and the vector
15093 	 * ends up disabled forever.
15094 	 */
15095 	clear_bit(__I40E_DOWN, pf->state);
15096 
15097 	/* In case of MSIX we are going to setup the misc vector right here
15098 	 * to handle admin queue events etc. In case of legacy and MSI
15099 	 * the misc functionality and queue processing is combined in
15100 	 * the same vector and that gets setup at open.
15101 	 */
15102 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15103 		err = i40e_setup_misc_vector(pf);
15104 		if (err) {
15105 			dev_info(&pdev->dev,
15106 				 "setup of misc vector failed: %d\n", err);
15107 			goto err_vsis;
15108 		}
15109 	}
15110 
15111 #ifdef CONFIG_PCI_IOV
15112 	/* prep for VF support */
15113 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15114 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15115 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15116 		/* disable link interrupts for VFs */
15117 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15118 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15119 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15120 		i40e_flush(hw);
15121 
15122 		if (pci_num_vf(pdev)) {
15123 			dev_info(&pdev->dev,
15124 				 "Active VFs found, allocating resources.\n");
15125 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15126 			if (err)
15127 				dev_info(&pdev->dev,
15128 					 "Error %d allocating resources for existing VFs\n",
15129 					 err);
15130 		}
15131 	}
15132 #endif /* CONFIG_PCI_IOV */
15133 
15134 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15135 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
15136 						      pf->num_iwarp_msix,
15137 						      I40E_IWARP_IRQ_PILE_ID);
15138 		if (pf->iwarp_base_vector < 0) {
15139 			dev_info(&pdev->dev,
15140 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
15141 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
15142 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
15143 		}
15144 	}
15145 
15146 	i40e_dbg_pf_init(pf);
15147 
15148 	/* tell the firmware that we're starting */
15149 	i40e_send_version(pf);
15150 
15151 	/* since everything's happy, start the service_task timer */
15152 	mod_timer(&pf->service_timer,
15153 		  round_jiffies(jiffies + pf->service_timer_period));
15154 
15155 	/* add this PF to client device list and launch a client service task */
15156 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15157 		err = i40e_lan_add_device(pf);
15158 		if (err)
15159 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
15160 				 err);
15161 	}
15162 
15163 #define PCI_SPEED_SIZE 8
15164 #define PCI_WIDTH_SIZE 8
15165 	/* Devices on the IOSF bus do not have this information
15166 	 * and will report PCI Gen 1 x 1 by default so don't bother
15167 	 * checking them.
15168 	 */
15169 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
15170 		char speed[PCI_SPEED_SIZE] = "Unknown";
15171 		char width[PCI_WIDTH_SIZE] = "Unknown";
15172 
15173 		/* Get the negotiated link width and speed from PCI config
15174 		 * space
15175 		 */
15176 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
15177 					  &link_status);
15178 
15179 		i40e_set_pci_config_data(hw, link_status);
15180 
15181 		switch (hw->bus.speed) {
15182 		case i40e_bus_speed_8000:
15183 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
15184 		case i40e_bus_speed_5000:
15185 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
15186 		case i40e_bus_speed_2500:
15187 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
15188 		default:
15189 			break;
15190 		}
15191 		switch (hw->bus.width) {
15192 		case i40e_bus_width_pcie_x8:
15193 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
15194 		case i40e_bus_width_pcie_x4:
15195 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
15196 		case i40e_bus_width_pcie_x2:
15197 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
15198 		case i40e_bus_width_pcie_x1:
15199 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
15200 		default:
15201 			break;
15202 		}
15203 
15204 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
15205 			 speed, width);
15206 
15207 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
15208 		    hw->bus.speed < i40e_bus_speed_8000) {
15209 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
15210 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
15211 		}
15212 	}
15213 
15214 	/* get the requested speeds from the fw */
15215 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
15216 	if (err)
15217 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
15218 			i40e_stat_str(&pf->hw, err),
15219 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15220 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
15221 
15222 	/* set the FEC config due to the board capabilities */
15223 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
15224 
15225 	/* get the supported phy types from the fw */
15226 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
15227 	if (err)
15228 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
15229 			i40e_stat_str(&pf->hw, err),
15230 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15231 
15232 	/* make sure the MFS hasn't been set lower than the default */
15233 #define MAX_FRAME_SIZE_DEFAULT 0x2600
15234 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
15235 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
15236 	if (val < MAX_FRAME_SIZE_DEFAULT)
15237 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
15238 			 i, val);
15239 
15240 	/* Add a filter to drop all Flow control frames from any VSI from being
15241 	 * transmitted. By doing so we stop a malicious VF from sending out
15242 	 * PAUSE or PFC frames and potentially controlling traffic for other
15243 	 * PF/VF VSIs.
15244 	 * The FW can still send Flow control frames if enabled.
15245 	 */
15246 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
15247 						       pf->main_vsi_seid);
15248 
15249 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
15250 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
15251 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
15252 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
15253 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
15254 	/* print a string summarizing features */
15255 	i40e_print_features(pf);
15256 
15257 	return 0;
15258 
15259 	/* Unwind what we've done if something failed in the setup */
15260 err_vsis:
15261 	set_bit(__I40E_DOWN, pf->state);
15262 	i40e_clear_interrupt_scheme(pf);
15263 	kfree(pf->vsi);
15264 err_switch_setup:
15265 	i40e_reset_interrupt_capability(pf);
15266 	del_timer_sync(&pf->service_timer);
15267 err_mac_addr:
15268 err_configure_lan_hmc:
15269 	(void)i40e_shutdown_lan_hmc(hw);
15270 err_init_lan_hmc:
15271 	kfree(pf->qp_pile);
15272 err_sw_init:
15273 err_adminq_setup:
15274 err_pf_reset:
15275 	iounmap(hw->hw_addr);
15276 err_ioremap:
15277 	kfree(pf);
15278 err_pf_alloc:
15279 	pci_disable_pcie_error_reporting(pdev);
15280 	pci_release_mem_regions(pdev);
15281 err_pci_reg:
15282 err_dma:
15283 	pci_disable_device(pdev);
15284 	return err;
15285 }
15286 
15287 /**
15288  * i40e_remove - Device removal routine
15289  * @pdev: PCI device information struct
15290  *
15291  * i40e_remove is called by the PCI subsystem to alert the driver
15292  * that is should release a PCI device.  This could be caused by a
15293  * Hot-Plug event, or because the driver is going to be removed from
15294  * memory.
15295  **/
15296 static void i40e_remove(struct pci_dev *pdev)
15297 {
15298 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15299 	struct i40e_hw *hw = &pf->hw;
15300 	i40e_status ret_code;
15301 	int i;
15302 
15303 	i40e_dbg_pf_exit(pf);
15304 
15305 	i40e_ptp_stop(pf);
15306 
15307 	/* Disable RSS in hw */
15308 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
15309 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
15310 
15311 	while (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
15312 		usleep_range(1000, 2000);
15313 
15314 	/* no more scheduling of any task */
15315 	set_bit(__I40E_SUSPENDED, pf->state);
15316 	set_bit(__I40E_DOWN, pf->state);
15317 	if (pf->service_timer.function)
15318 		del_timer_sync(&pf->service_timer);
15319 	if (pf->service_task.func)
15320 		cancel_work_sync(&pf->service_task);
15321 
15322 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
15323 		struct i40e_vsi *vsi = pf->vsi[0];
15324 
15325 		/* We know that we have allocated only one vsi for this PF,
15326 		 * it was just for registering netdevice, so the interface
15327 		 * could be visible in the 'ifconfig' output
15328 		 */
15329 		unregister_netdev(vsi->netdev);
15330 		free_netdev(vsi->netdev);
15331 
15332 		goto unmap;
15333 	}
15334 
15335 	/* Client close must be called explicitly here because the timer
15336 	 * has been stopped.
15337 	 */
15338 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15339 
15340 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
15341 		i40e_free_vfs(pf);
15342 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
15343 	}
15344 
15345 	i40e_fdir_teardown(pf);
15346 
15347 	/* If there is a switch structure or any orphans, remove them.
15348 	 * This will leave only the PF's VSI remaining.
15349 	 */
15350 	for (i = 0; i < I40E_MAX_VEB; i++) {
15351 		if (!pf->veb[i])
15352 			continue;
15353 
15354 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
15355 		    pf->veb[i]->uplink_seid == 0)
15356 			i40e_switch_branch_release(pf->veb[i]);
15357 	}
15358 
15359 	/* Now we can shutdown the PF's VSI, just before we kill
15360 	 * adminq and hmc.
15361 	 */
15362 	if (pf->vsi[pf->lan_vsi])
15363 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
15364 
15365 	i40e_cloud_filter_exit(pf);
15366 
15367 	/* remove attached clients */
15368 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15369 		ret_code = i40e_lan_del_device(pf);
15370 		if (ret_code)
15371 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
15372 				 ret_code);
15373 	}
15374 
15375 	/* shutdown and destroy the HMC */
15376 	if (hw->hmc.hmc_obj) {
15377 		ret_code = i40e_shutdown_lan_hmc(hw);
15378 		if (ret_code)
15379 			dev_warn(&pdev->dev,
15380 				 "Failed to destroy the HMC resources: %d\n",
15381 				 ret_code);
15382 	}
15383 
15384 unmap:
15385 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
15386 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
15387 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
15388 		free_irq(pf->pdev->irq, pf);
15389 
15390 	/* shutdown the adminq */
15391 	i40e_shutdown_adminq(hw);
15392 
15393 	/* destroy the locks only once, here */
15394 	mutex_destroy(&hw->aq.arq_mutex);
15395 	mutex_destroy(&hw->aq.asq_mutex);
15396 
15397 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
15398 	rtnl_lock();
15399 	i40e_clear_interrupt_scheme(pf);
15400 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15401 		if (pf->vsi[i]) {
15402 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
15403 				i40e_vsi_clear_rings(pf->vsi[i]);
15404 			i40e_vsi_clear(pf->vsi[i]);
15405 			pf->vsi[i] = NULL;
15406 		}
15407 	}
15408 	rtnl_unlock();
15409 
15410 	for (i = 0; i < I40E_MAX_VEB; i++) {
15411 		kfree(pf->veb[i]);
15412 		pf->veb[i] = NULL;
15413 	}
15414 
15415 	kfree(pf->qp_pile);
15416 	kfree(pf->vsi);
15417 
15418 	iounmap(hw->hw_addr);
15419 	kfree(pf);
15420 	pci_release_mem_regions(pdev);
15421 
15422 	pci_disable_pcie_error_reporting(pdev);
15423 	pci_disable_device(pdev);
15424 }
15425 
15426 /**
15427  * i40e_pci_error_detected - warning that something funky happened in PCI land
15428  * @pdev: PCI device information struct
15429  * @error: the type of PCI error
15430  *
15431  * Called to warn that something happened and the error handling steps
15432  * are in progress.  Allows the driver to quiesce things, be ready for
15433  * remediation.
15434  **/
15435 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
15436 						pci_channel_state_t error)
15437 {
15438 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15439 
15440 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
15441 
15442 	if (!pf) {
15443 		dev_info(&pdev->dev,
15444 			 "Cannot recover - error happened during device probe\n");
15445 		return PCI_ERS_RESULT_DISCONNECT;
15446 	}
15447 
15448 	/* shutdown all operations */
15449 	if (!test_bit(__I40E_SUSPENDED, pf->state))
15450 		i40e_prep_for_reset(pf, false);
15451 
15452 	/* Request a slot reset */
15453 	return PCI_ERS_RESULT_NEED_RESET;
15454 }
15455 
15456 /**
15457  * i40e_pci_error_slot_reset - a PCI slot reset just happened
15458  * @pdev: PCI device information struct
15459  *
15460  * Called to find if the driver can work with the device now that
15461  * the pci slot has been reset.  If a basic connection seems good
15462  * (registers are readable and have sane content) then return a
15463  * happy little PCI_ERS_RESULT_xxx.
15464  **/
15465 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
15466 {
15467 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15468 	pci_ers_result_t result;
15469 	u32 reg;
15470 
15471 	dev_dbg(&pdev->dev, "%s\n", __func__);
15472 	if (pci_enable_device_mem(pdev)) {
15473 		dev_info(&pdev->dev,
15474 			 "Cannot re-enable PCI device after reset.\n");
15475 		result = PCI_ERS_RESULT_DISCONNECT;
15476 	} else {
15477 		pci_set_master(pdev);
15478 		pci_restore_state(pdev);
15479 		pci_save_state(pdev);
15480 		pci_wake_from_d3(pdev, false);
15481 
15482 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
15483 		if (reg == 0)
15484 			result = PCI_ERS_RESULT_RECOVERED;
15485 		else
15486 			result = PCI_ERS_RESULT_DISCONNECT;
15487 	}
15488 
15489 	return result;
15490 }
15491 
15492 /**
15493  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
15494  * @pdev: PCI device information struct
15495  */
15496 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
15497 {
15498 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15499 
15500 	i40e_prep_for_reset(pf, false);
15501 }
15502 
15503 /**
15504  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
15505  * @pdev: PCI device information struct
15506  */
15507 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
15508 {
15509 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15510 
15511 	i40e_reset_and_rebuild(pf, false, false);
15512 }
15513 
15514 /**
15515  * i40e_pci_error_resume - restart operations after PCI error recovery
15516  * @pdev: PCI device information struct
15517  *
15518  * Called to allow the driver to bring things back up after PCI error
15519  * and/or reset recovery has finished.
15520  **/
15521 static void i40e_pci_error_resume(struct pci_dev *pdev)
15522 {
15523 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15524 
15525 	dev_dbg(&pdev->dev, "%s\n", __func__);
15526 	if (test_bit(__I40E_SUSPENDED, pf->state))
15527 		return;
15528 
15529 	i40e_handle_reset_warning(pf, false);
15530 }
15531 
15532 /**
15533  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
15534  * using the mac_address_write admin q function
15535  * @pf: pointer to i40e_pf struct
15536  **/
15537 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
15538 {
15539 	struct i40e_hw *hw = &pf->hw;
15540 	i40e_status ret;
15541 	u8 mac_addr[6];
15542 	u16 flags = 0;
15543 
15544 	/* Get current MAC address in case it's an LAA */
15545 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
15546 		ether_addr_copy(mac_addr,
15547 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
15548 	} else {
15549 		dev_err(&pf->pdev->dev,
15550 			"Failed to retrieve MAC address; using default\n");
15551 		ether_addr_copy(mac_addr, hw->mac.addr);
15552 	}
15553 
15554 	/* The FW expects the mac address write cmd to first be called with
15555 	 * one of these flags before calling it again with the multicast
15556 	 * enable flags.
15557 	 */
15558 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
15559 
15560 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
15561 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
15562 
15563 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
15564 	if (ret) {
15565 		dev_err(&pf->pdev->dev,
15566 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
15567 		return;
15568 	}
15569 
15570 	flags = I40E_AQC_MC_MAG_EN
15571 			| I40E_AQC_WOL_PRESERVE_ON_PFR
15572 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
15573 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
15574 	if (ret)
15575 		dev_err(&pf->pdev->dev,
15576 			"Failed to enable Multicast Magic Packet wake up\n");
15577 }
15578 
15579 /**
15580  * i40e_shutdown - PCI callback for shutting down
15581  * @pdev: PCI device information struct
15582  **/
15583 static void i40e_shutdown(struct pci_dev *pdev)
15584 {
15585 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15586 	struct i40e_hw *hw = &pf->hw;
15587 
15588 	set_bit(__I40E_SUSPENDED, pf->state);
15589 	set_bit(__I40E_DOWN, pf->state);
15590 
15591 	del_timer_sync(&pf->service_timer);
15592 	cancel_work_sync(&pf->service_task);
15593 	i40e_cloud_filter_exit(pf);
15594 	i40e_fdir_teardown(pf);
15595 
15596 	/* Client close must be called explicitly here because the timer
15597 	 * has been stopped.
15598 	 */
15599 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15600 
15601 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
15602 		i40e_enable_mc_magic_wake(pf);
15603 
15604 	i40e_prep_for_reset(pf, false);
15605 
15606 	wr32(hw, I40E_PFPM_APM,
15607 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
15608 	wr32(hw, I40E_PFPM_WUFC,
15609 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
15610 
15611 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
15612 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
15613 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
15614 		free_irq(pf->pdev->irq, pf);
15615 
15616 	/* Since we're going to destroy queues during the
15617 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
15618 	 * whole section
15619 	 */
15620 	rtnl_lock();
15621 	i40e_clear_interrupt_scheme(pf);
15622 	rtnl_unlock();
15623 
15624 	if (system_state == SYSTEM_POWER_OFF) {
15625 		pci_wake_from_d3(pdev, pf->wol_en);
15626 		pci_set_power_state(pdev, PCI_D3hot);
15627 	}
15628 }
15629 
15630 /**
15631  * i40e_suspend - PM callback for moving to D3
15632  * @dev: generic device information structure
15633  **/
15634 static int __maybe_unused i40e_suspend(struct device *dev)
15635 {
15636 	struct i40e_pf *pf = dev_get_drvdata(dev);
15637 	struct i40e_hw *hw = &pf->hw;
15638 
15639 	/* If we're already suspended, then there is nothing to do */
15640 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
15641 		return 0;
15642 
15643 	set_bit(__I40E_DOWN, pf->state);
15644 
15645 	/* Ensure service task will not be running */
15646 	del_timer_sync(&pf->service_timer);
15647 	cancel_work_sync(&pf->service_task);
15648 
15649 	/* Client close must be called explicitly here because the timer
15650 	 * has been stopped.
15651 	 */
15652 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15653 
15654 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
15655 		i40e_enable_mc_magic_wake(pf);
15656 
15657 	/* Since we're going to destroy queues during the
15658 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
15659 	 * whole section
15660 	 */
15661 	rtnl_lock();
15662 
15663 	i40e_prep_for_reset(pf, true);
15664 
15665 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
15666 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
15667 
15668 	/* Clear the interrupt scheme and release our IRQs so that the system
15669 	 * can safely hibernate even when there are a large number of CPUs.
15670 	 * Otherwise hibernation might fail when mapping all the vectors back
15671 	 * to CPU0.
15672 	 */
15673 	i40e_clear_interrupt_scheme(pf);
15674 
15675 	rtnl_unlock();
15676 
15677 	return 0;
15678 }
15679 
15680 /**
15681  * i40e_resume - PM callback for waking up from D3
15682  * @dev: generic device information structure
15683  **/
15684 static int __maybe_unused i40e_resume(struct device *dev)
15685 {
15686 	struct i40e_pf *pf = dev_get_drvdata(dev);
15687 	int err;
15688 
15689 	/* If we're not suspended, then there is nothing to do */
15690 	if (!test_bit(__I40E_SUSPENDED, pf->state))
15691 		return 0;
15692 
15693 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
15694 	 * since we're going to be restoring queues
15695 	 */
15696 	rtnl_lock();
15697 
15698 	/* We cleared the interrupt scheme when we suspended, so we need to
15699 	 * restore it now to resume device functionality.
15700 	 */
15701 	err = i40e_restore_interrupt_scheme(pf);
15702 	if (err) {
15703 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
15704 			err);
15705 	}
15706 
15707 	clear_bit(__I40E_DOWN, pf->state);
15708 	i40e_reset_and_rebuild(pf, false, true);
15709 
15710 	rtnl_unlock();
15711 
15712 	/* Clear suspended state last after everything is recovered */
15713 	clear_bit(__I40E_SUSPENDED, pf->state);
15714 
15715 	/* Restart the service task */
15716 	mod_timer(&pf->service_timer,
15717 		  round_jiffies(jiffies + pf->service_timer_period));
15718 
15719 	return 0;
15720 }
15721 
15722 static const struct pci_error_handlers i40e_err_handler = {
15723 	.error_detected = i40e_pci_error_detected,
15724 	.slot_reset = i40e_pci_error_slot_reset,
15725 	.reset_prepare = i40e_pci_error_reset_prepare,
15726 	.reset_done = i40e_pci_error_reset_done,
15727 	.resume = i40e_pci_error_resume,
15728 };
15729 
15730 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
15731 
15732 static struct pci_driver i40e_driver = {
15733 	.name     = i40e_driver_name,
15734 	.id_table = i40e_pci_tbl,
15735 	.probe    = i40e_probe,
15736 	.remove   = i40e_remove,
15737 	.driver   = {
15738 		.pm = &i40e_pm_ops,
15739 	},
15740 	.shutdown = i40e_shutdown,
15741 	.err_handler = &i40e_err_handler,
15742 	.sriov_configure = i40e_pci_sriov_configure,
15743 };
15744 
15745 /**
15746  * i40e_init_module - Driver registration routine
15747  *
15748  * i40e_init_module is the first routine called when the driver is
15749  * loaded. All it does is register with the PCI subsystem.
15750  **/
15751 static int __init i40e_init_module(void)
15752 {
15753 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
15754 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
15755 
15756 	/* There is no need to throttle the number of active tasks because
15757 	 * each device limits its own task using a state bit for scheduling
15758 	 * the service task, and the device tasks do not interfere with each
15759 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
15760 	 * since we need to be able to guarantee forward progress even under
15761 	 * memory pressure.
15762 	 */
15763 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
15764 	if (!i40e_wq) {
15765 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
15766 		return -ENOMEM;
15767 	}
15768 
15769 	i40e_dbg_init();
15770 	return pci_register_driver(&i40e_driver);
15771 }
15772 module_init(i40e_init_module);
15773 
15774 /**
15775  * i40e_exit_module - Driver exit cleanup routine
15776  *
15777  * i40e_exit_module is called just before the driver is removed
15778  * from memory.
15779  **/
15780 static void __exit i40e_exit_module(void)
15781 {
15782 	pci_unregister_driver(&i40e_driver);
15783 	destroy_workqueue(i40e_wq);
15784 	i40e_dbg_exit();
15785 }
15786 module_exit(i40e_exit_module);
15787