1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2021 Intel Corporation. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/of_net.h>
6 #include <linux/pci.h>
7 #include <linux/bpf.h>
8 #include <generated/utsrelease.h>
9 #include <linux/crash_dump.h>
10 
11 /* Local includes */
12 #include "i40e.h"
13 #include "i40e_diag.h"
14 #include "i40e_xsk.h"
15 #include <net/udp_tunnel.h>
16 #include <net/xdp_sock_drv.h>
17 /* All i40e tracepoints are defined by the include below, which
18  * must be included exactly once across the whole kernel with
19  * CREATE_TRACE_POINTS defined
20  */
21 #define CREATE_TRACE_POINTS
22 #include "i40e_trace.h"
23 
24 const char i40e_driver_name[] = "i40e";
25 static const char i40e_driver_string[] =
26 			"Intel(R) Ethernet Connection XL710 Network Driver";
27 
28 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
29 
30 /* a bit of forward declarations */
31 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
32 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
33 static int i40e_add_vsi(struct i40e_vsi *vsi);
34 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
35 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
36 static int i40e_setup_misc_vector(struct i40e_pf *pf);
37 static void i40e_determine_queue_usage(struct i40e_pf *pf);
38 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
39 static void i40e_prep_for_reset(struct i40e_pf *pf);
40 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
41 				   bool lock_acquired);
42 static int i40e_reset(struct i40e_pf *pf);
43 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
44 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
45 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
46 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
47 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
48 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
49 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
50 static int i40e_get_capabilities(struct i40e_pf *pf,
51 				 enum i40e_admin_queue_opc list_type);
52 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
53 
54 /* i40e_pci_tbl - PCI Device ID Table
55  *
56  * Last entry must be all 0s
57  *
58  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
59  *   Class, Class Mask, private data (not used) }
60  */
61 static const struct pci_device_id i40e_pci_tbl[] = {
62 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
63 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
64 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
86 	/* required last entry */
87 	{0, }
88 };
89 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
90 
91 #define I40E_MAX_VF_COUNT 128
92 static int debug = -1;
93 module_param(debug, uint, 0);
94 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
95 
96 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
97 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
98 MODULE_LICENSE("GPL v2");
99 
100 static struct workqueue_struct *i40e_wq;
101 
102 /**
103  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
104  * @hw:   pointer to the HW structure
105  * @mem:  ptr to mem struct to fill out
106  * @size: size of memory requested
107  * @alignment: what to align the allocation to
108  **/
109 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
110 			    u64 size, u32 alignment)
111 {
112 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
113 
114 	mem->size = ALIGN(size, alignment);
115 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
116 				     GFP_KERNEL);
117 	if (!mem->va)
118 		return -ENOMEM;
119 
120 	return 0;
121 }
122 
123 /**
124  * i40e_free_dma_mem_d - OS specific memory free for shared code
125  * @hw:   pointer to the HW structure
126  * @mem:  ptr to mem struct to free
127  **/
128 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
129 {
130 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
131 
132 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
133 	mem->va = NULL;
134 	mem->pa = 0;
135 	mem->size = 0;
136 
137 	return 0;
138 }
139 
140 /**
141  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
142  * @hw:   pointer to the HW structure
143  * @mem:  ptr to mem struct to fill out
144  * @size: size of memory requested
145  **/
146 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
147 			     u32 size)
148 {
149 	mem->size = size;
150 	mem->va = kzalloc(size, GFP_KERNEL);
151 
152 	if (!mem->va)
153 		return -ENOMEM;
154 
155 	return 0;
156 }
157 
158 /**
159  * i40e_free_virt_mem_d - OS specific memory free for shared code
160  * @hw:   pointer to the HW structure
161  * @mem:  ptr to mem struct to free
162  **/
163 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
164 {
165 	/* it's ok to kfree a NULL pointer */
166 	kfree(mem->va);
167 	mem->va = NULL;
168 	mem->size = 0;
169 
170 	return 0;
171 }
172 
173 /**
174  * i40e_get_lump - find a lump of free generic resource
175  * @pf: board private structure
176  * @pile: the pile of resource to search
177  * @needed: the number of items needed
178  * @id: an owner id to stick on the items assigned
179  *
180  * Returns the base item index of the lump, or negative for error
181  *
182  * The search_hint trick and lack of advanced fit-finding only work
183  * because we're highly likely to have all the same size lump requests.
184  * Linear search time and any fragmentation should be minimal.
185  **/
186 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
187 			 u16 needed, u16 id)
188 {
189 	int ret = -ENOMEM;
190 	int i, j;
191 
192 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
193 		dev_info(&pf->pdev->dev,
194 			 "param err: pile=%s needed=%d id=0x%04x\n",
195 			 pile ? "<valid>" : "<null>", needed, id);
196 		return -EINVAL;
197 	}
198 
199 	/* start the linear search with an imperfect hint */
200 	i = pile->search_hint;
201 	while (i < pile->num_entries) {
202 		/* skip already allocated entries */
203 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
204 			i++;
205 			continue;
206 		}
207 
208 		/* do we have enough in this lump? */
209 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
210 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
211 				break;
212 		}
213 
214 		if (j == needed) {
215 			/* there was enough, so assign it to the requestor */
216 			for (j = 0; j < needed; j++)
217 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
218 			ret = i;
219 			pile->search_hint = i + j;
220 			break;
221 		}
222 
223 		/* not enough, so skip over it and continue looking */
224 		i += j;
225 	}
226 
227 	return ret;
228 }
229 
230 /**
231  * i40e_put_lump - return a lump of generic resource
232  * @pile: the pile of resource to search
233  * @index: the base item index
234  * @id: the owner id of the items assigned
235  *
236  * Returns the count of items in the lump
237  **/
238 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
239 {
240 	int valid_id = (id | I40E_PILE_VALID_BIT);
241 	int count = 0;
242 	int i;
243 
244 	if (!pile || index >= pile->num_entries)
245 		return -EINVAL;
246 
247 	for (i = index;
248 	     i < pile->num_entries && pile->list[i] == valid_id;
249 	     i++) {
250 		pile->list[i] = 0;
251 		count++;
252 	}
253 
254 	if (count && index < pile->search_hint)
255 		pile->search_hint = index;
256 
257 	return count;
258 }
259 
260 /**
261  * i40e_find_vsi_from_id - searches for the vsi with the given id
262  * @pf: the pf structure to search for the vsi
263  * @id: id of the vsi it is searching for
264  **/
265 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
266 {
267 	int i;
268 
269 	for (i = 0; i < pf->num_alloc_vsi; i++)
270 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
271 			return pf->vsi[i];
272 
273 	return NULL;
274 }
275 
276 /**
277  * i40e_service_event_schedule - Schedule the service task to wake up
278  * @pf: board private structure
279  *
280  * If not already scheduled, this puts the task into the work queue
281  **/
282 void i40e_service_event_schedule(struct i40e_pf *pf)
283 {
284 	if ((!test_bit(__I40E_DOWN, pf->state) &&
285 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
286 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
287 		queue_work(i40e_wq, &pf->service_task);
288 }
289 
290 /**
291  * i40e_tx_timeout - Respond to a Tx Hang
292  * @netdev: network interface device structure
293  * @txqueue: queue number timing out
294  *
295  * If any port has noticed a Tx timeout, it is likely that the whole
296  * device is munged, not just the one netdev port, so go for the full
297  * reset.
298  **/
299 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
300 {
301 	struct i40e_netdev_priv *np = netdev_priv(netdev);
302 	struct i40e_vsi *vsi = np->vsi;
303 	struct i40e_pf *pf = vsi->back;
304 	struct i40e_ring *tx_ring = NULL;
305 	unsigned int i;
306 	u32 head, val;
307 
308 	pf->tx_timeout_count++;
309 
310 	/* with txqueue index, find the tx_ring struct */
311 	for (i = 0; i < vsi->num_queue_pairs; i++) {
312 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
313 			if (txqueue ==
314 			    vsi->tx_rings[i]->queue_index) {
315 				tx_ring = vsi->tx_rings[i];
316 				break;
317 			}
318 		}
319 	}
320 
321 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
322 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
323 	else if (time_before(jiffies,
324 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
325 		return;   /* don't do any new action before the next timeout */
326 
327 	/* don't kick off another recovery if one is already pending */
328 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
329 		return;
330 
331 	if (tx_ring) {
332 		head = i40e_get_head(tx_ring);
333 		/* Read interrupt register */
334 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
335 			val = rd32(&pf->hw,
336 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
337 						tx_ring->vsi->base_vector - 1));
338 		else
339 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
340 
341 		netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
342 			    vsi->seid, txqueue, tx_ring->next_to_clean,
343 			    head, tx_ring->next_to_use,
344 			    readl(tx_ring->tail), val);
345 	}
346 
347 	pf->tx_timeout_last_recovery = jiffies;
348 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
349 		    pf->tx_timeout_recovery_level, txqueue);
350 
351 	switch (pf->tx_timeout_recovery_level) {
352 	case 1:
353 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
354 		break;
355 	case 2:
356 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
357 		break;
358 	case 3:
359 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
360 		break;
361 	default:
362 		netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
363 		break;
364 	}
365 
366 	i40e_service_event_schedule(pf);
367 	pf->tx_timeout_recovery_level++;
368 }
369 
370 /**
371  * i40e_get_vsi_stats_struct - Get System Network Statistics
372  * @vsi: the VSI we care about
373  *
374  * Returns the address of the device statistics structure.
375  * The statistics are actually updated from the service task.
376  **/
377 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
378 {
379 	return &vsi->net_stats;
380 }
381 
382 /**
383  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
384  * @ring: Tx ring to get statistics from
385  * @stats: statistics entry to be updated
386  **/
387 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
388 					    struct rtnl_link_stats64 *stats)
389 {
390 	u64 bytes, packets;
391 	unsigned int start;
392 
393 	do {
394 		start = u64_stats_fetch_begin_irq(&ring->syncp);
395 		packets = ring->stats.packets;
396 		bytes   = ring->stats.bytes;
397 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
398 
399 	stats->tx_packets += packets;
400 	stats->tx_bytes   += bytes;
401 }
402 
403 /**
404  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
405  * @netdev: network interface device structure
406  * @stats: data structure to store statistics
407  *
408  * Returns the address of the device statistics structure.
409  * The statistics are actually updated from the service task.
410  **/
411 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
412 				  struct rtnl_link_stats64 *stats)
413 {
414 	struct i40e_netdev_priv *np = netdev_priv(netdev);
415 	struct i40e_vsi *vsi = np->vsi;
416 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
417 	struct i40e_ring *ring;
418 	int i;
419 
420 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
421 		return;
422 
423 	if (!vsi->tx_rings)
424 		return;
425 
426 	rcu_read_lock();
427 	for (i = 0; i < vsi->num_queue_pairs; i++) {
428 		u64 bytes, packets;
429 		unsigned int start;
430 
431 		ring = READ_ONCE(vsi->tx_rings[i]);
432 		if (!ring)
433 			continue;
434 		i40e_get_netdev_stats_struct_tx(ring, stats);
435 
436 		if (i40e_enabled_xdp_vsi(vsi)) {
437 			ring = READ_ONCE(vsi->xdp_rings[i]);
438 			if (!ring)
439 				continue;
440 			i40e_get_netdev_stats_struct_tx(ring, stats);
441 		}
442 
443 		ring = READ_ONCE(vsi->rx_rings[i]);
444 		if (!ring)
445 			continue;
446 		do {
447 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
448 			packets = ring->stats.packets;
449 			bytes   = ring->stats.bytes;
450 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
451 
452 		stats->rx_packets += packets;
453 		stats->rx_bytes   += bytes;
454 
455 	}
456 	rcu_read_unlock();
457 
458 	/* following stats updated by i40e_watchdog_subtask() */
459 	stats->multicast	= vsi_stats->multicast;
460 	stats->tx_errors	= vsi_stats->tx_errors;
461 	stats->tx_dropped	= vsi_stats->tx_dropped;
462 	stats->rx_errors	= vsi_stats->rx_errors;
463 	stats->rx_dropped	= vsi_stats->rx_dropped;
464 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
465 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
466 }
467 
468 /**
469  * i40e_vsi_reset_stats - Resets all stats of the given vsi
470  * @vsi: the VSI to have its stats reset
471  **/
472 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
473 {
474 	struct rtnl_link_stats64 *ns;
475 	int i;
476 
477 	if (!vsi)
478 		return;
479 
480 	ns = i40e_get_vsi_stats_struct(vsi);
481 	memset(ns, 0, sizeof(*ns));
482 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
483 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
484 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
485 	if (vsi->rx_rings && vsi->rx_rings[0]) {
486 		for (i = 0; i < vsi->num_queue_pairs; i++) {
487 			memset(&vsi->rx_rings[i]->stats, 0,
488 			       sizeof(vsi->rx_rings[i]->stats));
489 			memset(&vsi->rx_rings[i]->rx_stats, 0,
490 			       sizeof(vsi->rx_rings[i]->rx_stats));
491 			memset(&vsi->tx_rings[i]->stats, 0,
492 			       sizeof(vsi->tx_rings[i]->stats));
493 			memset(&vsi->tx_rings[i]->tx_stats, 0,
494 			       sizeof(vsi->tx_rings[i]->tx_stats));
495 		}
496 	}
497 	vsi->stat_offsets_loaded = false;
498 }
499 
500 /**
501  * i40e_pf_reset_stats - Reset all of the stats for the given PF
502  * @pf: the PF to be reset
503  **/
504 void i40e_pf_reset_stats(struct i40e_pf *pf)
505 {
506 	int i;
507 
508 	memset(&pf->stats, 0, sizeof(pf->stats));
509 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
510 	pf->stat_offsets_loaded = false;
511 
512 	for (i = 0; i < I40E_MAX_VEB; i++) {
513 		if (pf->veb[i]) {
514 			memset(&pf->veb[i]->stats, 0,
515 			       sizeof(pf->veb[i]->stats));
516 			memset(&pf->veb[i]->stats_offsets, 0,
517 			       sizeof(pf->veb[i]->stats_offsets));
518 			memset(&pf->veb[i]->tc_stats, 0,
519 			       sizeof(pf->veb[i]->tc_stats));
520 			memset(&pf->veb[i]->tc_stats_offsets, 0,
521 			       sizeof(pf->veb[i]->tc_stats_offsets));
522 			pf->veb[i]->stat_offsets_loaded = false;
523 		}
524 	}
525 	pf->hw_csum_rx_error = 0;
526 }
527 
528 /**
529  * i40e_stat_update48 - read and update a 48 bit stat from the chip
530  * @hw: ptr to the hardware info
531  * @hireg: the high 32 bit reg to read
532  * @loreg: the low 32 bit reg to read
533  * @offset_loaded: has the initial offset been loaded yet
534  * @offset: ptr to current offset value
535  * @stat: ptr to the stat
536  *
537  * Since the device stats are not reset at PFReset, they likely will not
538  * be zeroed when the driver starts.  We'll save the first values read
539  * and use them as offsets to be subtracted from the raw values in order
540  * to report stats that count from zero.  In the process, we also manage
541  * the potential roll-over.
542  **/
543 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
544 			       bool offset_loaded, u64 *offset, u64 *stat)
545 {
546 	u64 new_data;
547 
548 	if (hw->device_id == I40E_DEV_ID_QEMU) {
549 		new_data = rd32(hw, loreg);
550 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
551 	} else {
552 		new_data = rd64(hw, loreg);
553 	}
554 	if (!offset_loaded)
555 		*offset = new_data;
556 	if (likely(new_data >= *offset))
557 		*stat = new_data - *offset;
558 	else
559 		*stat = (new_data + BIT_ULL(48)) - *offset;
560 	*stat &= 0xFFFFFFFFFFFFULL;
561 }
562 
563 /**
564  * i40e_stat_update32 - read and update a 32 bit stat from the chip
565  * @hw: ptr to the hardware info
566  * @reg: the hw reg to read
567  * @offset_loaded: has the initial offset been loaded yet
568  * @offset: ptr to current offset value
569  * @stat: ptr to the stat
570  **/
571 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
572 			       bool offset_loaded, u64 *offset, u64 *stat)
573 {
574 	u32 new_data;
575 
576 	new_data = rd32(hw, reg);
577 	if (!offset_loaded)
578 		*offset = new_data;
579 	if (likely(new_data >= *offset))
580 		*stat = (u32)(new_data - *offset);
581 	else
582 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
583 }
584 
585 /**
586  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
587  * @hw: ptr to the hardware info
588  * @reg: the hw reg to read and clear
589  * @stat: ptr to the stat
590  **/
591 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
592 {
593 	u32 new_data = rd32(hw, reg);
594 
595 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
596 	*stat += new_data;
597 }
598 
599 /**
600  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
601  * @vsi: the VSI to be updated
602  **/
603 void i40e_update_eth_stats(struct i40e_vsi *vsi)
604 {
605 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
606 	struct i40e_pf *pf = vsi->back;
607 	struct i40e_hw *hw = &pf->hw;
608 	struct i40e_eth_stats *oes;
609 	struct i40e_eth_stats *es;     /* device's eth stats */
610 
611 	es = &vsi->eth_stats;
612 	oes = &vsi->eth_stats_offsets;
613 
614 	/* Gather up the stats that the hw collects */
615 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
616 			   vsi->stat_offsets_loaded,
617 			   &oes->tx_errors, &es->tx_errors);
618 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
619 			   vsi->stat_offsets_loaded,
620 			   &oes->rx_discards, &es->rx_discards);
621 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
622 			   vsi->stat_offsets_loaded,
623 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
624 
625 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
626 			   I40E_GLV_GORCL(stat_idx),
627 			   vsi->stat_offsets_loaded,
628 			   &oes->rx_bytes, &es->rx_bytes);
629 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
630 			   I40E_GLV_UPRCL(stat_idx),
631 			   vsi->stat_offsets_loaded,
632 			   &oes->rx_unicast, &es->rx_unicast);
633 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
634 			   I40E_GLV_MPRCL(stat_idx),
635 			   vsi->stat_offsets_loaded,
636 			   &oes->rx_multicast, &es->rx_multicast);
637 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
638 			   I40E_GLV_BPRCL(stat_idx),
639 			   vsi->stat_offsets_loaded,
640 			   &oes->rx_broadcast, &es->rx_broadcast);
641 
642 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
643 			   I40E_GLV_GOTCL(stat_idx),
644 			   vsi->stat_offsets_loaded,
645 			   &oes->tx_bytes, &es->tx_bytes);
646 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
647 			   I40E_GLV_UPTCL(stat_idx),
648 			   vsi->stat_offsets_loaded,
649 			   &oes->tx_unicast, &es->tx_unicast);
650 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
651 			   I40E_GLV_MPTCL(stat_idx),
652 			   vsi->stat_offsets_loaded,
653 			   &oes->tx_multicast, &es->tx_multicast);
654 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
655 			   I40E_GLV_BPTCL(stat_idx),
656 			   vsi->stat_offsets_loaded,
657 			   &oes->tx_broadcast, &es->tx_broadcast);
658 	vsi->stat_offsets_loaded = true;
659 }
660 
661 /**
662  * i40e_update_veb_stats - Update Switch component statistics
663  * @veb: the VEB being updated
664  **/
665 void i40e_update_veb_stats(struct i40e_veb *veb)
666 {
667 	struct i40e_pf *pf = veb->pf;
668 	struct i40e_hw *hw = &pf->hw;
669 	struct i40e_eth_stats *oes;
670 	struct i40e_eth_stats *es;     /* device's eth stats */
671 	struct i40e_veb_tc_stats *veb_oes;
672 	struct i40e_veb_tc_stats *veb_es;
673 	int i, idx = 0;
674 
675 	idx = veb->stats_idx;
676 	es = &veb->stats;
677 	oes = &veb->stats_offsets;
678 	veb_es = &veb->tc_stats;
679 	veb_oes = &veb->tc_stats_offsets;
680 
681 	/* Gather up the stats that the hw collects */
682 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
683 			   veb->stat_offsets_loaded,
684 			   &oes->tx_discards, &es->tx_discards);
685 	if (hw->revision_id > 0)
686 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
687 				   veb->stat_offsets_loaded,
688 				   &oes->rx_unknown_protocol,
689 				   &es->rx_unknown_protocol);
690 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
691 			   veb->stat_offsets_loaded,
692 			   &oes->rx_bytes, &es->rx_bytes);
693 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
694 			   veb->stat_offsets_loaded,
695 			   &oes->rx_unicast, &es->rx_unicast);
696 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
697 			   veb->stat_offsets_loaded,
698 			   &oes->rx_multicast, &es->rx_multicast);
699 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
700 			   veb->stat_offsets_loaded,
701 			   &oes->rx_broadcast, &es->rx_broadcast);
702 
703 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
704 			   veb->stat_offsets_loaded,
705 			   &oes->tx_bytes, &es->tx_bytes);
706 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
707 			   veb->stat_offsets_loaded,
708 			   &oes->tx_unicast, &es->tx_unicast);
709 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
710 			   veb->stat_offsets_loaded,
711 			   &oes->tx_multicast, &es->tx_multicast);
712 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
713 			   veb->stat_offsets_loaded,
714 			   &oes->tx_broadcast, &es->tx_broadcast);
715 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
716 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
717 				   I40E_GLVEBTC_RPCL(i, idx),
718 				   veb->stat_offsets_loaded,
719 				   &veb_oes->tc_rx_packets[i],
720 				   &veb_es->tc_rx_packets[i]);
721 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
722 				   I40E_GLVEBTC_RBCL(i, idx),
723 				   veb->stat_offsets_loaded,
724 				   &veb_oes->tc_rx_bytes[i],
725 				   &veb_es->tc_rx_bytes[i]);
726 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
727 				   I40E_GLVEBTC_TPCL(i, idx),
728 				   veb->stat_offsets_loaded,
729 				   &veb_oes->tc_tx_packets[i],
730 				   &veb_es->tc_tx_packets[i]);
731 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
732 				   I40E_GLVEBTC_TBCL(i, idx),
733 				   veb->stat_offsets_loaded,
734 				   &veb_oes->tc_tx_bytes[i],
735 				   &veb_es->tc_tx_bytes[i]);
736 	}
737 	veb->stat_offsets_loaded = true;
738 }
739 
740 /**
741  * i40e_update_vsi_stats - Update the vsi statistics counters.
742  * @vsi: the VSI to be updated
743  *
744  * There are a few instances where we store the same stat in a
745  * couple of different structs.  This is partly because we have
746  * the netdev stats that need to be filled out, which is slightly
747  * different from the "eth_stats" defined by the chip and used in
748  * VF communications.  We sort it out here.
749  **/
750 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
751 {
752 	struct i40e_pf *pf = vsi->back;
753 	struct rtnl_link_stats64 *ons;
754 	struct rtnl_link_stats64 *ns;   /* netdev stats */
755 	struct i40e_eth_stats *oes;
756 	struct i40e_eth_stats *es;     /* device's eth stats */
757 	u32 tx_restart, tx_busy;
758 	struct i40e_ring *p;
759 	u32 rx_page, rx_buf;
760 	u64 bytes, packets;
761 	unsigned int start;
762 	u64 tx_linearize;
763 	u64 tx_force_wb;
764 	u64 rx_p, rx_b;
765 	u64 tx_p, tx_b;
766 	u16 q;
767 
768 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
769 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
770 		return;
771 
772 	ns = i40e_get_vsi_stats_struct(vsi);
773 	ons = &vsi->net_stats_offsets;
774 	es = &vsi->eth_stats;
775 	oes = &vsi->eth_stats_offsets;
776 
777 	/* Gather up the netdev and vsi stats that the driver collects
778 	 * on the fly during packet processing
779 	 */
780 	rx_b = rx_p = 0;
781 	tx_b = tx_p = 0;
782 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
783 	rx_page = 0;
784 	rx_buf = 0;
785 	rcu_read_lock();
786 	for (q = 0; q < vsi->num_queue_pairs; q++) {
787 		/* locate Tx ring */
788 		p = READ_ONCE(vsi->tx_rings[q]);
789 		if (!p)
790 			continue;
791 
792 		do {
793 			start = u64_stats_fetch_begin_irq(&p->syncp);
794 			packets = p->stats.packets;
795 			bytes = p->stats.bytes;
796 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
797 		tx_b += bytes;
798 		tx_p += packets;
799 		tx_restart += p->tx_stats.restart_queue;
800 		tx_busy += p->tx_stats.tx_busy;
801 		tx_linearize += p->tx_stats.tx_linearize;
802 		tx_force_wb += p->tx_stats.tx_force_wb;
803 
804 		/* locate Rx ring */
805 		p = READ_ONCE(vsi->rx_rings[q]);
806 		if (!p)
807 			continue;
808 
809 		do {
810 			start = u64_stats_fetch_begin_irq(&p->syncp);
811 			packets = p->stats.packets;
812 			bytes = p->stats.bytes;
813 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
814 		rx_b += bytes;
815 		rx_p += packets;
816 		rx_buf += p->rx_stats.alloc_buff_failed;
817 		rx_page += p->rx_stats.alloc_page_failed;
818 
819 		if (i40e_enabled_xdp_vsi(vsi)) {
820 			/* locate XDP ring */
821 			p = READ_ONCE(vsi->xdp_rings[q]);
822 			if (!p)
823 				continue;
824 
825 			do {
826 				start = u64_stats_fetch_begin_irq(&p->syncp);
827 				packets = p->stats.packets;
828 				bytes = p->stats.bytes;
829 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
830 			tx_b += bytes;
831 			tx_p += packets;
832 			tx_restart += p->tx_stats.restart_queue;
833 			tx_busy += p->tx_stats.tx_busy;
834 			tx_linearize += p->tx_stats.tx_linearize;
835 			tx_force_wb += p->tx_stats.tx_force_wb;
836 		}
837 	}
838 	rcu_read_unlock();
839 	vsi->tx_restart = tx_restart;
840 	vsi->tx_busy = tx_busy;
841 	vsi->tx_linearize = tx_linearize;
842 	vsi->tx_force_wb = tx_force_wb;
843 	vsi->rx_page_failed = rx_page;
844 	vsi->rx_buf_failed = rx_buf;
845 
846 	ns->rx_packets = rx_p;
847 	ns->rx_bytes = rx_b;
848 	ns->tx_packets = tx_p;
849 	ns->tx_bytes = tx_b;
850 
851 	/* update netdev stats from eth stats */
852 	i40e_update_eth_stats(vsi);
853 	ons->tx_errors = oes->tx_errors;
854 	ns->tx_errors = es->tx_errors;
855 	ons->multicast = oes->rx_multicast;
856 	ns->multicast = es->rx_multicast;
857 	ons->rx_dropped = oes->rx_discards;
858 	ns->rx_dropped = es->rx_discards;
859 	ons->tx_dropped = oes->tx_discards;
860 	ns->tx_dropped = es->tx_discards;
861 
862 	/* pull in a couple PF stats if this is the main vsi */
863 	if (vsi == pf->vsi[pf->lan_vsi]) {
864 		ns->rx_crc_errors = pf->stats.crc_errors;
865 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
866 		ns->rx_length_errors = pf->stats.rx_length_errors;
867 	}
868 }
869 
870 /**
871  * i40e_update_pf_stats - Update the PF statistics counters.
872  * @pf: the PF to be updated
873  **/
874 static void i40e_update_pf_stats(struct i40e_pf *pf)
875 {
876 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
877 	struct i40e_hw_port_stats *nsd = &pf->stats;
878 	struct i40e_hw *hw = &pf->hw;
879 	u32 val;
880 	int i;
881 
882 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
883 			   I40E_GLPRT_GORCL(hw->port),
884 			   pf->stat_offsets_loaded,
885 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
886 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
887 			   I40E_GLPRT_GOTCL(hw->port),
888 			   pf->stat_offsets_loaded,
889 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
890 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
891 			   pf->stat_offsets_loaded,
892 			   &osd->eth.rx_discards,
893 			   &nsd->eth.rx_discards);
894 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
895 			   I40E_GLPRT_UPRCL(hw->port),
896 			   pf->stat_offsets_loaded,
897 			   &osd->eth.rx_unicast,
898 			   &nsd->eth.rx_unicast);
899 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
900 			   I40E_GLPRT_MPRCL(hw->port),
901 			   pf->stat_offsets_loaded,
902 			   &osd->eth.rx_multicast,
903 			   &nsd->eth.rx_multicast);
904 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
905 			   I40E_GLPRT_BPRCL(hw->port),
906 			   pf->stat_offsets_loaded,
907 			   &osd->eth.rx_broadcast,
908 			   &nsd->eth.rx_broadcast);
909 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
910 			   I40E_GLPRT_UPTCL(hw->port),
911 			   pf->stat_offsets_loaded,
912 			   &osd->eth.tx_unicast,
913 			   &nsd->eth.tx_unicast);
914 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
915 			   I40E_GLPRT_MPTCL(hw->port),
916 			   pf->stat_offsets_loaded,
917 			   &osd->eth.tx_multicast,
918 			   &nsd->eth.tx_multicast);
919 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
920 			   I40E_GLPRT_BPTCL(hw->port),
921 			   pf->stat_offsets_loaded,
922 			   &osd->eth.tx_broadcast,
923 			   &nsd->eth.tx_broadcast);
924 
925 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
926 			   pf->stat_offsets_loaded,
927 			   &osd->tx_dropped_link_down,
928 			   &nsd->tx_dropped_link_down);
929 
930 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
931 			   pf->stat_offsets_loaded,
932 			   &osd->crc_errors, &nsd->crc_errors);
933 
934 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
935 			   pf->stat_offsets_loaded,
936 			   &osd->illegal_bytes, &nsd->illegal_bytes);
937 
938 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
939 			   pf->stat_offsets_loaded,
940 			   &osd->mac_local_faults,
941 			   &nsd->mac_local_faults);
942 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
943 			   pf->stat_offsets_loaded,
944 			   &osd->mac_remote_faults,
945 			   &nsd->mac_remote_faults);
946 
947 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
948 			   pf->stat_offsets_loaded,
949 			   &osd->rx_length_errors,
950 			   &nsd->rx_length_errors);
951 
952 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
953 			   pf->stat_offsets_loaded,
954 			   &osd->link_xon_rx, &nsd->link_xon_rx);
955 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
956 			   pf->stat_offsets_loaded,
957 			   &osd->link_xon_tx, &nsd->link_xon_tx);
958 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
959 			   pf->stat_offsets_loaded,
960 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
961 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
962 			   pf->stat_offsets_loaded,
963 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
964 
965 	for (i = 0; i < 8; i++) {
966 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
967 				   pf->stat_offsets_loaded,
968 				   &osd->priority_xoff_rx[i],
969 				   &nsd->priority_xoff_rx[i]);
970 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
971 				   pf->stat_offsets_loaded,
972 				   &osd->priority_xon_rx[i],
973 				   &nsd->priority_xon_rx[i]);
974 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
975 				   pf->stat_offsets_loaded,
976 				   &osd->priority_xon_tx[i],
977 				   &nsd->priority_xon_tx[i]);
978 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
979 				   pf->stat_offsets_loaded,
980 				   &osd->priority_xoff_tx[i],
981 				   &nsd->priority_xoff_tx[i]);
982 		i40e_stat_update32(hw,
983 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
984 				   pf->stat_offsets_loaded,
985 				   &osd->priority_xon_2_xoff[i],
986 				   &nsd->priority_xon_2_xoff[i]);
987 	}
988 
989 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
990 			   I40E_GLPRT_PRC64L(hw->port),
991 			   pf->stat_offsets_loaded,
992 			   &osd->rx_size_64, &nsd->rx_size_64);
993 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
994 			   I40E_GLPRT_PRC127L(hw->port),
995 			   pf->stat_offsets_loaded,
996 			   &osd->rx_size_127, &nsd->rx_size_127);
997 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
998 			   I40E_GLPRT_PRC255L(hw->port),
999 			   pf->stat_offsets_loaded,
1000 			   &osd->rx_size_255, &nsd->rx_size_255);
1001 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1002 			   I40E_GLPRT_PRC511L(hw->port),
1003 			   pf->stat_offsets_loaded,
1004 			   &osd->rx_size_511, &nsd->rx_size_511);
1005 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1006 			   I40E_GLPRT_PRC1023L(hw->port),
1007 			   pf->stat_offsets_loaded,
1008 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1009 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1010 			   I40E_GLPRT_PRC1522L(hw->port),
1011 			   pf->stat_offsets_loaded,
1012 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1013 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1014 			   I40E_GLPRT_PRC9522L(hw->port),
1015 			   pf->stat_offsets_loaded,
1016 			   &osd->rx_size_big, &nsd->rx_size_big);
1017 
1018 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1019 			   I40E_GLPRT_PTC64L(hw->port),
1020 			   pf->stat_offsets_loaded,
1021 			   &osd->tx_size_64, &nsd->tx_size_64);
1022 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1023 			   I40E_GLPRT_PTC127L(hw->port),
1024 			   pf->stat_offsets_loaded,
1025 			   &osd->tx_size_127, &nsd->tx_size_127);
1026 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1027 			   I40E_GLPRT_PTC255L(hw->port),
1028 			   pf->stat_offsets_loaded,
1029 			   &osd->tx_size_255, &nsd->tx_size_255);
1030 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1031 			   I40E_GLPRT_PTC511L(hw->port),
1032 			   pf->stat_offsets_loaded,
1033 			   &osd->tx_size_511, &nsd->tx_size_511);
1034 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1035 			   I40E_GLPRT_PTC1023L(hw->port),
1036 			   pf->stat_offsets_loaded,
1037 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1038 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1039 			   I40E_GLPRT_PTC1522L(hw->port),
1040 			   pf->stat_offsets_loaded,
1041 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1042 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1043 			   I40E_GLPRT_PTC9522L(hw->port),
1044 			   pf->stat_offsets_loaded,
1045 			   &osd->tx_size_big, &nsd->tx_size_big);
1046 
1047 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1048 			   pf->stat_offsets_loaded,
1049 			   &osd->rx_undersize, &nsd->rx_undersize);
1050 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1051 			   pf->stat_offsets_loaded,
1052 			   &osd->rx_fragments, &nsd->rx_fragments);
1053 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1054 			   pf->stat_offsets_loaded,
1055 			   &osd->rx_oversize, &nsd->rx_oversize);
1056 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1057 			   pf->stat_offsets_loaded,
1058 			   &osd->rx_jabber, &nsd->rx_jabber);
1059 
1060 	/* FDIR stats */
1061 	i40e_stat_update_and_clear32(hw,
1062 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1063 			&nsd->fd_atr_match);
1064 	i40e_stat_update_and_clear32(hw,
1065 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1066 			&nsd->fd_sb_match);
1067 	i40e_stat_update_and_clear32(hw,
1068 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1069 			&nsd->fd_atr_tunnel_match);
1070 
1071 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1072 	nsd->tx_lpi_status =
1073 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1074 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1075 	nsd->rx_lpi_status =
1076 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1077 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1078 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1079 			   pf->stat_offsets_loaded,
1080 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1081 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1082 			   pf->stat_offsets_loaded,
1083 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1084 
1085 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1086 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1087 		nsd->fd_sb_status = true;
1088 	else
1089 		nsd->fd_sb_status = false;
1090 
1091 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1092 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1093 		nsd->fd_atr_status = true;
1094 	else
1095 		nsd->fd_atr_status = false;
1096 
1097 	pf->stat_offsets_loaded = true;
1098 }
1099 
1100 /**
1101  * i40e_update_stats - Update the various statistics counters.
1102  * @vsi: the VSI to be updated
1103  *
1104  * Update the various stats for this VSI and its related entities.
1105  **/
1106 void i40e_update_stats(struct i40e_vsi *vsi)
1107 {
1108 	struct i40e_pf *pf = vsi->back;
1109 
1110 	if (vsi == pf->vsi[pf->lan_vsi])
1111 		i40e_update_pf_stats(pf);
1112 
1113 	i40e_update_vsi_stats(vsi);
1114 }
1115 
1116 /**
1117  * i40e_count_filters - counts VSI mac filters
1118  * @vsi: the VSI to be searched
1119  *
1120  * Returns count of mac filters
1121  **/
1122 int i40e_count_filters(struct i40e_vsi *vsi)
1123 {
1124 	struct i40e_mac_filter *f;
1125 	struct hlist_node *h;
1126 	int bkt;
1127 	int cnt = 0;
1128 
1129 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1130 		++cnt;
1131 
1132 	return cnt;
1133 }
1134 
1135 /**
1136  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1137  * @vsi: the VSI to be searched
1138  * @macaddr: the MAC address
1139  * @vlan: the vlan
1140  *
1141  * Returns ptr to the filter object or NULL
1142  **/
1143 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1144 						const u8 *macaddr, s16 vlan)
1145 {
1146 	struct i40e_mac_filter *f;
1147 	u64 key;
1148 
1149 	if (!vsi || !macaddr)
1150 		return NULL;
1151 
1152 	key = i40e_addr_to_hkey(macaddr);
1153 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1154 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1155 		    (vlan == f->vlan))
1156 			return f;
1157 	}
1158 	return NULL;
1159 }
1160 
1161 /**
1162  * i40e_find_mac - Find a mac addr in the macvlan filters list
1163  * @vsi: the VSI to be searched
1164  * @macaddr: the MAC address we are searching for
1165  *
1166  * Returns the first filter with the provided MAC address or NULL if
1167  * MAC address was not found
1168  **/
1169 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1170 {
1171 	struct i40e_mac_filter *f;
1172 	u64 key;
1173 
1174 	if (!vsi || !macaddr)
1175 		return NULL;
1176 
1177 	key = i40e_addr_to_hkey(macaddr);
1178 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1179 		if ((ether_addr_equal(macaddr, f->macaddr)))
1180 			return f;
1181 	}
1182 	return NULL;
1183 }
1184 
1185 /**
1186  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1187  * @vsi: the VSI to be searched
1188  *
1189  * Returns true if VSI is in vlan mode or false otherwise
1190  **/
1191 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1192 {
1193 	/* If we have a PVID, always operate in VLAN mode */
1194 	if (vsi->info.pvid)
1195 		return true;
1196 
1197 	/* We need to operate in VLAN mode whenever we have any filters with
1198 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1199 	 * time, incurring search cost repeatedly. However, we can notice two
1200 	 * things:
1201 	 *
1202 	 * 1) the only place where we can gain a VLAN filter is in
1203 	 *    i40e_add_filter.
1204 	 *
1205 	 * 2) the only place where filters are actually removed is in
1206 	 *    i40e_sync_filters_subtask.
1207 	 *
1208 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1209 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1210 	 * we have to perform the full search after deleting filters in
1211 	 * i40e_sync_filters_subtask, but we already have to search
1212 	 * filters here and can perform the check at the same time. This
1213 	 * results in avoiding embedding a loop for VLAN mode inside another
1214 	 * loop over all the filters, and should maintain correctness as noted
1215 	 * above.
1216 	 */
1217 	return vsi->has_vlan_filter;
1218 }
1219 
1220 /**
1221  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1222  * @vsi: the VSI to configure
1223  * @tmp_add_list: list of filters ready to be added
1224  * @tmp_del_list: list of filters ready to be deleted
1225  * @vlan_filters: the number of active VLAN filters
1226  *
1227  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1228  * behave as expected. If we have any active VLAN filters remaining or about
1229  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1230  * so that they only match against untagged traffic. If we no longer have any
1231  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1232  * so that they match against both tagged and untagged traffic. In this way,
1233  * we ensure that we correctly receive the desired traffic. This ensures that
1234  * when we have an active VLAN we will receive only untagged traffic and
1235  * traffic matching active VLANs. If we have no active VLANs then we will
1236  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1237  *
1238  * Finally, in a similar fashion, this function also corrects filters when
1239  * there is an active PVID assigned to this VSI.
1240  *
1241  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1242  *
1243  * This function is only expected to be called from within
1244  * i40e_sync_vsi_filters.
1245  *
1246  * NOTE: This function expects to be called while under the
1247  * mac_filter_hash_lock
1248  */
1249 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1250 					 struct hlist_head *tmp_add_list,
1251 					 struct hlist_head *tmp_del_list,
1252 					 int vlan_filters)
1253 {
1254 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1255 	struct i40e_mac_filter *f, *add_head;
1256 	struct i40e_new_mac_filter *new;
1257 	struct hlist_node *h;
1258 	int bkt, new_vlan;
1259 
1260 	/* To determine if a particular filter needs to be replaced we
1261 	 * have the three following conditions:
1262 	 *
1263 	 * a) if we have a PVID assigned, then all filters which are
1264 	 *    not marked as VLAN=PVID must be replaced with filters that
1265 	 *    are.
1266 	 * b) otherwise, if we have any active VLANS, all filters
1267 	 *    which are marked as VLAN=-1 must be replaced with
1268 	 *    filters marked as VLAN=0
1269 	 * c) finally, if we do not have any active VLANS, all filters
1270 	 *    which are marked as VLAN=0 must be replaced with filters
1271 	 *    marked as VLAN=-1
1272 	 */
1273 
1274 	/* Update the filters about to be added in place */
1275 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1276 		if (pvid && new->f->vlan != pvid)
1277 			new->f->vlan = pvid;
1278 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1279 			new->f->vlan = 0;
1280 		else if (!vlan_filters && new->f->vlan == 0)
1281 			new->f->vlan = I40E_VLAN_ANY;
1282 	}
1283 
1284 	/* Update the remaining active filters */
1285 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1286 		/* Combine the checks for whether a filter needs to be changed
1287 		 * and then determine the new VLAN inside the if block, in
1288 		 * order to avoid duplicating code for adding the new filter
1289 		 * then deleting the old filter.
1290 		 */
1291 		if ((pvid && f->vlan != pvid) ||
1292 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1293 		    (!vlan_filters && f->vlan == 0)) {
1294 			/* Determine the new vlan we will be adding */
1295 			if (pvid)
1296 				new_vlan = pvid;
1297 			else if (vlan_filters)
1298 				new_vlan = 0;
1299 			else
1300 				new_vlan = I40E_VLAN_ANY;
1301 
1302 			/* Create the new filter */
1303 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1304 			if (!add_head)
1305 				return -ENOMEM;
1306 
1307 			/* Create a temporary i40e_new_mac_filter */
1308 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1309 			if (!new)
1310 				return -ENOMEM;
1311 
1312 			new->f = add_head;
1313 			new->state = add_head->state;
1314 
1315 			/* Add the new filter to the tmp list */
1316 			hlist_add_head(&new->hlist, tmp_add_list);
1317 
1318 			/* Put the original filter into the delete list */
1319 			f->state = I40E_FILTER_REMOVE;
1320 			hash_del(&f->hlist);
1321 			hlist_add_head(&f->hlist, tmp_del_list);
1322 		}
1323 	}
1324 
1325 	vsi->has_vlan_filter = !!vlan_filters;
1326 
1327 	return 0;
1328 }
1329 
1330 /**
1331  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1332  * @vsi: the PF Main VSI - inappropriate for any other VSI
1333  * @macaddr: the MAC address
1334  *
1335  * Remove whatever filter the firmware set up so the driver can manage
1336  * its own filtering intelligently.
1337  **/
1338 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1339 {
1340 	struct i40e_aqc_remove_macvlan_element_data element;
1341 	struct i40e_pf *pf = vsi->back;
1342 
1343 	/* Only appropriate for the PF main VSI */
1344 	if (vsi->type != I40E_VSI_MAIN)
1345 		return;
1346 
1347 	memset(&element, 0, sizeof(element));
1348 	ether_addr_copy(element.mac_addr, macaddr);
1349 	element.vlan_tag = 0;
1350 	/* Ignore error returns, some firmware does it this way... */
1351 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1352 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1353 
1354 	memset(&element, 0, sizeof(element));
1355 	ether_addr_copy(element.mac_addr, macaddr);
1356 	element.vlan_tag = 0;
1357 	/* ...and some firmware does it this way. */
1358 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1359 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1360 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1361 }
1362 
1363 /**
1364  * i40e_add_filter - Add a mac/vlan filter to the VSI
1365  * @vsi: the VSI to be searched
1366  * @macaddr: the MAC address
1367  * @vlan: the vlan
1368  *
1369  * Returns ptr to the filter object or NULL when no memory available.
1370  *
1371  * NOTE: This function is expected to be called with mac_filter_hash_lock
1372  * being held.
1373  **/
1374 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1375 					const u8 *macaddr, s16 vlan)
1376 {
1377 	struct i40e_mac_filter *f;
1378 	u64 key;
1379 
1380 	if (!vsi || !macaddr)
1381 		return NULL;
1382 
1383 	f = i40e_find_filter(vsi, macaddr, vlan);
1384 	if (!f) {
1385 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1386 		if (!f)
1387 			return NULL;
1388 
1389 		/* Update the boolean indicating if we need to function in
1390 		 * VLAN mode.
1391 		 */
1392 		if (vlan >= 0)
1393 			vsi->has_vlan_filter = true;
1394 
1395 		ether_addr_copy(f->macaddr, macaddr);
1396 		f->vlan = vlan;
1397 		f->state = I40E_FILTER_NEW;
1398 		INIT_HLIST_NODE(&f->hlist);
1399 
1400 		key = i40e_addr_to_hkey(macaddr);
1401 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1402 
1403 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1404 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1405 	}
1406 
1407 	/* If we're asked to add a filter that has been marked for removal, it
1408 	 * is safe to simply restore it to active state. __i40e_del_filter
1409 	 * will have simply deleted any filters which were previously marked
1410 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1411 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1412 	 * task, just restore this filter to the ACTIVE state so that the
1413 	 * sync task leaves it in place
1414 	 */
1415 	if (f->state == I40E_FILTER_REMOVE)
1416 		f->state = I40E_FILTER_ACTIVE;
1417 
1418 	return f;
1419 }
1420 
1421 /**
1422  * __i40e_del_filter - Remove a specific filter from the VSI
1423  * @vsi: VSI to remove from
1424  * @f: the filter to remove from the list
1425  *
1426  * This function should be called instead of i40e_del_filter only if you know
1427  * the exact filter you will remove already, such as via i40e_find_filter or
1428  * i40e_find_mac.
1429  *
1430  * NOTE: This function is expected to be called with mac_filter_hash_lock
1431  * being held.
1432  * ANOTHER NOTE: This function MUST be called from within the context of
1433  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1434  * instead of list_for_each_entry().
1435  **/
1436 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1437 {
1438 	if (!f)
1439 		return;
1440 
1441 	/* If the filter was never added to firmware then we can just delete it
1442 	 * directly and we don't want to set the status to remove or else an
1443 	 * admin queue command will unnecessarily fire.
1444 	 */
1445 	if ((f->state == I40E_FILTER_FAILED) ||
1446 	    (f->state == I40E_FILTER_NEW)) {
1447 		hash_del(&f->hlist);
1448 		kfree(f);
1449 	} else {
1450 		f->state = I40E_FILTER_REMOVE;
1451 	}
1452 
1453 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1454 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1455 }
1456 
1457 /**
1458  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1459  * @vsi: the VSI to be searched
1460  * @macaddr: the MAC address
1461  * @vlan: the VLAN
1462  *
1463  * NOTE: This function is expected to be called with mac_filter_hash_lock
1464  * being held.
1465  * ANOTHER NOTE: This function MUST be called from within the context of
1466  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1467  * instead of list_for_each_entry().
1468  **/
1469 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1470 {
1471 	struct i40e_mac_filter *f;
1472 
1473 	if (!vsi || !macaddr)
1474 		return;
1475 
1476 	f = i40e_find_filter(vsi, macaddr, vlan);
1477 	__i40e_del_filter(vsi, f);
1478 }
1479 
1480 /**
1481  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1482  * @vsi: the VSI to be searched
1483  * @macaddr: the mac address to be filtered
1484  *
1485  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1486  * go through all the macvlan filters and add a macvlan filter for each
1487  * unique vlan that already exists. If a PVID has been assigned, instead only
1488  * add the macaddr to that VLAN.
1489  *
1490  * Returns last filter added on success, else NULL
1491  **/
1492 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1493 					    const u8 *macaddr)
1494 {
1495 	struct i40e_mac_filter *f, *add = NULL;
1496 	struct hlist_node *h;
1497 	int bkt;
1498 
1499 	if (vsi->info.pvid)
1500 		return i40e_add_filter(vsi, macaddr,
1501 				       le16_to_cpu(vsi->info.pvid));
1502 
1503 	if (!i40e_is_vsi_in_vlan(vsi))
1504 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1505 
1506 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1507 		if (f->state == I40E_FILTER_REMOVE)
1508 			continue;
1509 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1510 		if (!add)
1511 			return NULL;
1512 	}
1513 
1514 	return add;
1515 }
1516 
1517 /**
1518  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1519  * @vsi: the VSI to be searched
1520  * @macaddr: the mac address to be removed
1521  *
1522  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1523  * associated with.
1524  *
1525  * Returns 0 for success, or error
1526  **/
1527 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1528 {
1529 	struct i40e_mac_filter *f;
1530 	struct hlist_node *h;
1531 	bool found = false;
1532 	int bkt;
1533 
1534 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1535 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1536 		if (ether_addr_equal(macaddr, f->macaddr)) {
1537 			__i40e_del_filter(vsi, f);
1538 			found = true;
1539 		}
1540 	}
1541 
1542 	if (found)
1543 		return 0;
1544 	else
1545 		return -ENOENT;
1546 }
1547 
1548 /**
1549  * i40e_set_mac - NDO callback to set mac address
1550  * @netdev: network interface device structure
1551  * @p: pointer to an address structure
1552  *
1553  * Returns 0 on success, negative on failure
1554  **/
1555 static int i40e_set_mac(struct net_device *netdev, void *p)
1556 {
1557 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1558 	struct i40e_vsi *vsi = np->vsi;
1559 	struct i40e_pf *pf = vsi->back;
1560 	struct i40e_hw *hw = &pf->hw;
1561 	struct sockaddr *addr = p;
1562 
1563 	if (!is_valid_ether_addr(addr->sa_data))
1564 		return -EADDRNOTAVAIL;
1565 
1566 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1567 		netdev_info(netdev, "already using mac address %pM\n",
1568 			    addr->sa_data);
1569 		return 0;
1570 	}
1571 
1572 	if (test_bit(__I40E_DOWN, pf->state) ||
1573 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1574 		return -EADDRNOTAVAIL;
1575 
1576 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1577 		netdev_info(netdev, "returning to hw mac address %pM\n",
1578 			    hw->mac.addr);
1579 	else
1580 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1581 
1582 	/* Copy the address first, so that we avoid a possible race with
1583 	 * .set_rx_mode().
1584 	 * - Remove old address from MAC filter
1585 	 * - Copy new address
1586 	 * - Add new address to MAC filter
1587 	 */
1588 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1589 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1590 	ether_addr_copy(netdev->dev_addr, addr->sa_data);
1591 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1592 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1593 
1594 	if (vsi->type == I40E_VSI_MAIN) {
1595 		i40e_status ret;
1596 
1597 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1598 						addr->sa_data, NULL);
1599 		if (ret)
1600 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1601 				    i40e_stat_str(hw, ret),
1602 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1603 	}
1604 
1605 	/* schedule our worker thread which will take care of
1606 	 * applying the new filter changes
1607 	 */
1608 	i40e_service_event_schedule(pf);
1609 	return 0;
1610 }
1611 
1612 /**
1613  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1614  * @vsi: vsi structure
1615  * @seed: RSS hash seed
1616  * @lut: pointer to lookup table of lut_size
1617  * @lut_size: size of the lookup table
1618  **/
1619 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1620 			      u8 *lut, u16 lut_size)
1621 {
1622 	struct i40e_pf *pf = vsi->back;
1623 	struct i40e_hw *hw = &pf->hw;
1624 	int ret = 0;
1625 
1626 	if (seed) {
1627 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1628 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1629 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1630 		if (ret) {
1631 			dev_info(&pf->pdev->dev,
1632 				 "Cannot set RSS key, err %s aq_err %s\n",
1633 				 i40e_stat_str(hw, ret),
1634 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1635 			return ret;
1636 		}
1637 	}
1638 	if (lut) {
1639 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1640 
1641 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1642 		if (ret) {
1643 			dev_info(&pf->pdev->dev,
1644 				 "Cannot set RSS lut, err %s aq_err %s\n",
1645 				 i40e_stat_str(hw, ret),
1646 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1647 			return ret;
1648 		}
1649 	}
1650 	return ret;
1651 }
1652 
1653 /**
1654  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1655  * @vsi: VSI structure
1656  **/
1657 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1658 {
1659 	struct i40e_pf *pf = vsi->back;
1660 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1661 	u8 *lut;
1662 	int ret;
1663 
1664 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1665 		return 0;
1666 	if (!vsi->rss_size)
1667 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1668 				      vsi->num_queue_pairs);
1669 	if (!vsi->rss_size)
1670 		return -EINVAL;
1671 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1672 	if (!lut)
1673 		return -ENOMEM;
1674 
1675 	/* Use the user configured hash keys and lookup table if there is one,
1676 	 * otherwise use default
1677 	 */
1678 	if (vsi->rss_lut_user)
1679 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1680 	else
1681 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1682 	if (vsi->rss_hkey_user)
1683 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1684 	else
1685 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1686 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1687 	kfree(lut);
1688 	return ret;
1689 }
1690 
1691 /**
1692  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1693  * @vsi: the VSI being configured,
1694  * @ctxt: VSI context structure
1695  * @enabled_tc: number of traffic classes to enable
1696  *
1697  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1698  **/
1699 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1700 					   struct i40e_vsi_context *ctxt,
1701 					   u8 enabled_tc)
1702 {
1703 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1704 	int i, override_q, pow, num_qps, ret;
1705 	u8 netdev_tc = 0, offset = 0;
1706 
1707 	if (vsi->type != I40E_VSI_MAIN)
1708 		return -EINVAL;
1709 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1710 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1711 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1712 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1713 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1714 
1715 	/* find the next higher power-of-2 of num queue pairs */
1716 	pow = ilog2(num_qps);
1717 	if (!is_power_of_2(num_qps))
1718 		pow++;
1719 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1720 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1721 
1722 	/* Setup queue offset/count for all TCs for given VSI */
1723 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1724 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1725 		/* See if the given TC is enabled for the given VSI */
1726 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1727 			offset = vsi->mqprio_qopt.qopt.offset[i];
1728 			qcount = vsi->mqprio_qopt.qopt.count[i];
1729 			if (qcount > max_qcount)
1730 				max_qcount = qcount;
1731 			vsi->tc_config.tc_info[i].qoffset = offset;
1732 			vsi->tc_config.tc_info[i].qcount = qcount;
1733 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1734 		} else {
1735 			/* TC is not enabled so set the offset to
1736 			 * default queue and allocate one queue
1737 			 * for the given TC.
1738 			 */
1739 			vsi->tc_config.tc_info[i].qoffset = 0;
1740 			vsi->tc_config.tc_info[i].qcount = 1;
1741 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1742 		}
1743 	}
1744 
1745 	/* Set actual Tx/Rx queue pairs */
1746 	vsi->num_queue_pairs = offset + qcount;
1747 
1748 	/* Setup queue TC[0].qmap for given VSI context */
1749 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1750 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1751 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1752 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1753 
1754 	/* Reconfigure RSS for main VSI with max queue count */
1755 	vsi->rss_size = max_qcount;
1756 	ret = i40e_vsi_config_rss(vsi);
1757 	if (ret) {
1758 		dev_info(&vsi->back->pdev->dev,
1759 			 "Failed to reconfig rss for num_queues (%u)\n",
1760 			 max_qcount);
1761 		return ret;
1762 	}
1763 	vsi->reconfig_rss = true;
1764 	dev_dbg(&vsi->back->pdev->dev,
1765 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1766 
1767 	/* Find queue count available for channel VSIs and starting offset
1768 	 * for channel VSIs
1769 	 */
1770 	override_q = vsi->mqprio_qopt.qopt.count[0];
1771 	if (override_q && override_q < vsi->num_queue_pairs) {
1772 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1773 		vsi->next_base_queue = override_q;
1774 	}
1775 	return 0;
1776 }
1777 
1778 /**
1779  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1780  * @vsi: the VSI being setup
1781  * @ctxt: VSI context structure
1782  * @enabled_tc: Enabled TCs bitmap
1783  * @is_add: True if called before Add VSI
1784  *
1785  * Setup VSI queue mapping for enabled traffic classes.
1786  **/
1787 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1788 				     struct i40e_vsi_context *ctxt,
1789 				     u8 enabled_tc,
1790 				     bool is_add)
1791 {
1792 	struct i40e_pf *pf = vsi->back;
1793 	u16 sections = 0;
1794 	u8 netdev_tc = 0;
1795 	u16 numtc = 1;
1796 	u16 qcount;
1797 	u8 offset;
1798 	u16 qmap;
1799 	int i;
1800 	u16 num_tc_qps = 0;
1801 
1802 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1803 	offset = 0;
1804 
1805 	/* Number of queues per enabled TC */
1806 	num_tc_qps = vsi->alloc_queue_pairs;
1807 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1808 		/* Find numtc from enabled TC bitmap */
1809 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1810 			if (enabled_tc & BIT(i)) /* TC is enabled */
1811 				numtc++;
1812 		}
1813 		if (!numtc) {
1814 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1815 			numtc = 1;
1816 		}
1817 		num_tc_qps = num_tc_qps / numtc;
1818 		num_tc_qps = min_t(int, num_tc_qps,
1819 				   i40e_pf_get_max_q_per_tc(pf));
1820 	}
1821 
1822 	vsi->tc_config.numtc = numtc;
1823 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1824 
1825 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
1826 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1827 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
1828 
1829 	/* Setup queue offset/count for all TCs for given VSI */
1830 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1831 		/* See if the given TC is enabled for the given VSI */
1832 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1833 			/* TC is enabled */
1834 			int pow, num_qps;
1835 
1836 			switch (vsi->type) {
1837 			case I40E_VSI_MAIN:
1838 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
1839 				    I40E_FLAG_FD_ATR_ENABLED)) ||
1840 				    vsi->tc_config.enabled_tc != 1) {
1841 					qcount = min_t(int, pf->alloc_rss_size,
1842 						       num_tc_qps);
1843 					break;
1844 				}
1845 				fallthrough;
1846 			case I40E_VSI_FDIR:
1847 			case I40E_VSI_SRIOV:
1848 			case I40E_VSI_VMDQ2:
1849 			default:
1850 				qcount = num_tc_qps;
1851 				WARN_ON(i != 0);
1852 				break;
1853 			}
1854 			vsi->tc_config.tc_info[i].qoffset = offset;
1855 			vsi->tc_config.tc_info[i].qcount = qcount;
1856 
1857 			/* find the next higher power-of-2 of num queue pairs */
1858 			num_qps = qcount;
1859 			pow = 0;
1860 			while (num_qps && (BIT_ULL(pow) < qcount)) {
1861 				pow++;
1862 				num_qps >>= 1;
1863 			}
1864 
1865 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1866 			qmap =
1867 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1868 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1869 
1870 			offset += qcount;
1871 		} else {
1872 			/* TC is not enabled so set the offset to
1873 			 * default queue and allocate one queue
1874 			 * for the given TC.
1875 			 */
1876 			vsi->tc_config.tc_info[i].qoffset = 0;
1877 			vsi->tc_config.tc_info[i].qcount = 1;
1878 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1879 
1880 			qmap = 0;
1881 		}
1882 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1883 	}
1884 
1885 	/* Set actual Tx/Rx queue pairs */
1886 	vsi->num_queue_pairs = offset;
1887 	if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1888 		if (vsi->req_queue_pairs > 0)
1889 			vsi->num_queue_pairs = vsi->req_queue_pairs;
1890 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1891 			vsi->num_queue_pairs = pf->num_lan_msix;
1892 	}
1893 
1894 	/* Scheduler section valid can only be set for ADD VSI */
1895 	if (is_add) {
1896 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1897 
1898 		ctxt->info.up_enable_bits = enabled_tc;
1899 	}
1900 	if (vsi->type == I40E_VSI_SRIOV) {
1901 		ctxt->info.mapping_flags |=
1902 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1903 		for (i = 0; i < vsi->num_queue_pairs; i++)
1904 			ctxt->info.queue_mapping[i] =
1905 					       cpu_to_le16(vsi->base_queue + i);
1906 	} else {
1907 		ctxt->info.mapping_flags |=
1908 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1909 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1910 	}
1911 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1912 }
1913 
1914 /**
1915  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1916  * @netdev: the netdevice
1917  * @addr: address to add
1918  *
1919  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1920  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1921  */
1922 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1923 {
1924 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1925 	struct i40e_vsi *vsi = np->vsi;
1926 
1927 	if (i40e_add_mac_filter(vsi, addr))
1928 		return 0;
1929 	else
1930 		return -ENOMEM;
1931 }
1932 
1933 /**
1934  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1935  * @netdev: the netdevice
1936  * @addr: address to add
1937  *
1938  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1939  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1940  */
1941 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1942 {
1943 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1944 	struct i40e_vsi *vsi = np->vsi;
1945 
1946 	/* Under some circumstances, we might receive a request to delete
1947 	 * our own device address from our uc list. Because we store the
1948 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
1949 	 * such requests and not delete our device address from this list.
1950 	 */
1951 	if (ether_addr_equal(addr, netdev->dev_addr))
1952 		return 0;
1953 
1954 	i40e_del_mac_filter(vsi, addr);
1955 
1956 	return 0;
1957 }
1958 
1959 /**
1960  * i40e_set_rx_mode - NDO callback to set the netdev filters
1961  * @netdev: network interface device structure
1962  **/
1963 static void i40e_set_rx_mode(struct net_device *netdev)
1964 {
1965 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1966 	struct i40e_vsi *vsi = np->vsi;
1967 
1968 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1969 
1970 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1971 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1972 
1973 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1974 
1975 	/* check for other flag changes */
1976 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
1977 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1978 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1979 	}
1980 }
1981 
1982 /**
1983  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
1984  * @vsi: Pointer to VSI struct
1985  * @from: Pointer to list which contains MAC filter entries - changes to
1986  *        those entries needs to be undone.
1987  *
1988  * MAC filter entries from this list were slated for deletion.
1989  **/
1990 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1991 					 struct hlist_head *from)
1992 {
1993 	struct i40e_mac_filter *f;
1994 	struct hlist_node *h;
1995 
1996 	hlist_for_each_entry_safe(f, h, from, hlist) {
1997 		u64 key = i40e_addr_to_hkey(f->macaddr);
1998 
1999 		/* Move the element back into MAC filter list*/
2000 		hlist_del(&f->hlist);
2001 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2002 	}
2003 }
2004 
2005 /**
2006  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2007  * @vsi: Pointer to vsi struct
2008  * @from: Pointer to list which contains MAC filter entries - changes to
2009  *        those entries needs to be undone.
2010  *
2011  * MAC filter entries from this list were slated for addition.
2012  **/
2013 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2014 					 struct hlist_head *from)
2015 {
2016 	struct i40e_new_mac_filter *new;
2017 	struct hlist_node *h;
2018 
2019 	hlist_for_each_entry_safe(new, h, from, hlist) {
2020 		/* We can simply free the wrapper structure */
2021 		hlist_del(&new->hlist);
2022 		kfree(new);
2023 	}
2024 }
2025 
2026 /**
2027  * i40e_next_filter - Get the next non-broadcast filter from a list
2028  * @next: pointer to filter in list
2029  *
2030  * Returns the next non-broadcast filter in the list. Required so that we
2031  * ignore broadcast filters within the list, since these are not handled via
2032  * the normal firmware update path.
2033  */
2034 static
2035 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2036 {
2037 	hlist_for_each_entry_continue(next, hlist) {
2038 		if (!is_broadcast_ether_addr(next->f->macaddr))
2039 			return next;
2040 	}
2041 
2042 	return NULL;
2043 }
2044 
2045 /**
2046  * i40e_update_filter_state - Update filter state based on return data
2047  * from firmware
2048  * @count: Number of filters added
2049  * @add_list: return data from fw
2050  * @add_head: pointer to first filter in current batch
2051  *
2052  * MAC filter entries from list were slated to be added to device. Returns
2053  * number of successful filters. Note that 0 does NOT mean success!
2054  **/
2055 static int
2056 i40e_update_filter_state(int count,
2057 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2058 			 struct i40e_new_mac_filter *add_head)
2059 {
2060 	int retval = 0;
2061 	int i;
2062 
2063 	for (i = 0; i < count; i++) {
2064 		/* Always check status of each filter. We don't need to check
2065 		 * the firmware return status because we pre-set the filter
2066 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2067 		 * request to the adminq. Thus, if it no longer matches then
2068 		 * we know the filter is active.
2069 		 */
2070 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2071 			add_head->state = I40E_FILTER_FAILED;
2072 		} else {
2073 			add_head->state = I40E_FILTER_ACTIVE;
2074 			retval++;
2075 		}
2076 
2077 		add_head = i40e_next_filter(add_head);
2078 		if (!add_head)
2079 			break;
2080 	}
2081 
2082 	return retval;
2083 }
2084 
2085 /**
2086  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2087  * @vsi: ptr to the VSI
2088  * @vsi_name: name to display in messages
2089  * @list: the list of filters to send to firmware
2090  * @num_del: the number of filters to delete
2091  * @retval: Set to -EIO on failure to delete
2092  *
2093  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2094  * *retval instead of a return value so that success does not force ret_val to
2095  * be set to 0. This ensures that a sequence of calls to this function
2096  * preserve the previous value of *retval on successful delete.
2097  */
2098 static
2099 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2100 			  struct i40e_aqc_remove_macvlan_element_data *list,
2101 			  int num_del, int *retval)
2102 {
2103 	struct i40e_hw *hw = &vsi->back->hw;
2104 	i40e_status aq_ret;
2105 	int aq_err;
2106 
2107 	aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
2108 	aq_err = hw->aq.asq_last_status;
2109 
2110 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2111 	if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
2112 		*retval = -EIO;
2113 		dev_info(&vsi->back->pdev->dev,
2114 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2115 			 vsi_name, i40e_stat_str(hw, aq_ret),
2116 			 i40e_aq_str(hw, aq_err));
2117 	}
2118 }
2119 
2120 /**
2121  * i40e_aqc_add_filters - Request firmware to add a set of filters
2122  * @vsi: ptr to the VSI
2123  * @vsi_name: name to display in messages
2124  * @list: the list of filters to send to firmware
2125  * @add_head: Position in the add hlist
2126  * @num_add: the number of filters to add
2127  *
2128  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2129  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2130  * space for more filters.
2131  */
2132 static
2133 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2134 			  struct i40e_aqc_add_macvlan_element_data *list,
2135 			  struct i40e_new_mac_filter *add_head,
2136 			  int num_add)
2137 {
2138 	struct i40e_hw *hw = &vsi->back->hw;
2139 	int aq_err, fcnt;
2140 
2141 	i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
2142 	aq_err = hw->aq.asq_last_status;
2143 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2144 
2145 	if (fcnt != num_add) {
2146 		if (vsi->type == I40E_VSI_MAIN) {
2147 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2148 			dev_warn(&vsi->back->pdev->dev,
2149 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2150 				 i40e_aq_str(hw, aq_err), vsi_name);
2151 		} else if (vsi->type == I40E_VSI_SRIOV ||
2152 			   vsi->type == I40E_VSI_VMDQ1 ||
2153 			   vsi->type == I40E_VSI_VMDQ2) {
2154 			dev_warn(&vsi->back->pdev->dev,
2155 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2156 				 i40e_aq_str(hw, aq_err), vsi_name, vsi_name);
2157 		} else {
2158 			dev_warn(&vsi->back->pdev->dev,
2159 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2160 				 i40e_aq_str(hw, aq_err), vsi_name, vsi->type);
2161 		}
2162 	}
2163 }
2164 
2165 /**
2166  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2167  * @vsi: pointer to the VSI
2168  * @vsi_name: the VSI name
2169  * @f: filter data
2170  *
2171  * This function sets or clears the promiscuous broadcast flags for VLAN
2172  * filters in order to properly receive broadcast frames. Assumes that only
2173  * broadcast filters are passed.
2174  *
2175  * Returns status indicating success or failure;
2176  **/
2177 static i40e_status
2178 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2179 			  struct i40e_mac_filter *f)
2180 {
2181 	bool enable = f->state == I40E_FILTER_NEW;
2182 	struct i40e_hw *hw = &vsi->back->hw;
2183 	i40e_status aq_ret;
2184 
2185 	if (f->vlan == I40E_VLAN_ANY) {
2186 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2187 						   vsi->seid,
2188 						   enable,
2189 						   NULL);
2190 	} else {
2191 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2192 							    vsi->seid,
2193 							    enable,
2194 							    f->vlan,
2195 							    NULL);
2196 	}
2197 
2198 	if (aq_ret) {
2199 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2200 		dev_warn(&vsi->back->pdev->dev,
2201 			 "Error %s, forcing overflow promiscuous on %s\n",
2202 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2203 			 vsi_name);
2204 	}
2205 
2206 	return aq_ret;
2207 }
2208 
2209 /**
2210  * i40e_set_promiscuous - set promiscuous mode
2211  * @pf: board private structure
2212  * @promisc: promisc on or off
2213  *
2214  * There are different ways of setting promiscuous mode on a PF depending on
2215  * what state/environment we're in.  This identifies and sets it appropriately.
2216  * Returns 0 on success.
2217  **/
2218 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2219 {
2220 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2221 	struct i40e_hw *hw = &pf->hw;
2222 	i40e_status aq_ret;
2223 
2224 	if (vsi->type == I40E_VSI_MAIN &&
2225 	    pf->lan_veb != I40E_NO_VEB &&
2226 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2227 		/* set defport ON for Main VSI instead of true promisc
2228 		 * this way we will get all unicast/multicast and VLAN
2229 		 * promisc behavior but will not get VF or VMDq traffic
2230 		 * replicated on the Main VSI.
2231 		 */
2232 		if (promisc)
2233 			aq_ret = i40e_aq_set_default_vsi(hw,
2234 							 vsi->seid,
2235 							 NULL);
2236 		else
2237 			aq_ret = i40e_aq_clear_default_vsi(hw,
2238 							   vsi->seid,
2239 							   NULL);
2240 		if (aq_ret) {
2241 			dev_info(&pf->pdev->dev,
2242 				 "Set default VSI failed, err %s, aq_err %s\n",
2243 				 i40e_stat_str(hw, aq_ret),
2244 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2245 		}
2246 	} else {
2247 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2248 						  hw,
2249 						  vsi->seid,
2250 						  promisc, NULL,
2251 						  true);
2252 		if (aq_ret) {
2253 			dev_info(&pf->pdev->dev,
2254 				 "set unicast promisc failed, err %s, aq_err %s\n",
2255 				 i40e_stat_str(hw, aq_ret),
2256 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2257 		}
2258 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2259 						  hw,
2260 						  vsi->seid,
2261 						  promisc, NULL);
2262 		if (aq_ret) {
2263 			dev_info(&pf->pdev->dev,
2264 				 "set multicast promisc failed, err %s, aq_err %s\n",
2265 				 i40e_stat_str(hw, aq_ret),
2266 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2267 		}
2268 	}
2269 
2270 	if (!aq_ret)
2271 		pf->cur_promisc = promisc;
2272 
2273 	return aq_ret;
2274 }
2275 
2276 /**
2277  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2278  * @vsi: ptr to the VSI
2279  *
2280  * Push any outstanding VSI filter changes through the AdminQ.
2281  *
2282  * Returns 0 or error value
2283  **/
2284 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2285 {
2286 	struct hlist_head tmp_add_list, tmp_del_list;
2287 	struct i40e_mac_filter *f;
2288 	struct i40e_new_mac_filter *new, *add_head = NULL;
2289 	struct i40e_hw *hw = &vsi->back->hw;
2290 	bool old_overflow, new_overflow;
2291 	unsigned int failed_filters = 0;
2292 	unsigned int vlan_filters = 0;
2293 	char vsi_name[16] = "PF";
2294 	int filter_list_len = 0;
2295 	i40e_status aq_ret = 0;
2296 	u32 changed_flags = 0;
2297 	struct hlist_node *h;
2298 	struct i40e_pf *pf;
2299 	int num_add = 0;
2300 	int num_del = 0;
2301 	int retval = 0;
2302 	u16 cmd_flags;
2303 	int list_size;
2304 	int bkt;
2305 
2306 	/* empty array typed pointers, kcalloc later */
2307 	struct i40e_aqc_add_macvlan_element_data *add_list;
2308 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2309 
2310 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2311 		usleep_range(1000, 2000);
2312 	pf = vsi->back;
2313 
2314 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2315 
2316 	if (vsi->netdev) {
2317 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2318 		vsi->current_netdev_flags = vsi->netdev->flags;
2319 	}
2320 
2321 	INIT_HLIST_HEAD(&tmp_add_list);
2322 	INIT_HLIST_HEAD(&tmp_del_list);
2323 
2324 	if (vsi->type == I40E_VSI_SRIOV)
2325 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2326 	else if (vsi->type != I40E_VSI_MAIN)
2327 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2328 
2329 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2330 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2331 
2332 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2333 		/* Create a list of filters to delete. */
2334 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2335 			if (f->state == I40E_FILTER_REMOVE) {
2336 				/* Move the element into temporary del_list */
2337 				hash_del(&f->hlist);
2338 				hlist_add_head(&f->hlist, &tmp_del_list);
2339 
2340 				/* Avoid counting removed filters */
2341 				continue;
2342 			}
2343 			if (f->state == I40E_FILTER_NEW) {
2344 				/* Create a temporary i40e_new_mac_filter */
2345 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2346 				if (!new)
2347 					goto err_no_memory_locked;
2348 
2349 				/* Store pointer to the real filter */
2350 				new->f = f;
2351 				new->state = f->state;
2352 
2353 				/* Add it to the hash list */
2354 				hlist_add_head(&new->hlist, &tmp_add_list);
2355 			}
2356 
2357 			/* Count the number of active (current and new) VLAN
2358 			 * filters we have now. Does not count filters which
2359 			 * are marked for deletion.
2360 			 */
2361 			if (f->vlan > 0)
2362 				vlan_filters++;
2363 		}
2364 
2365 		retval = i40e_correct_mac_vlan_filters(vsi,
2366 						       &tmp_add_list,
2367 						       &tmp_del_list,
2368 						       vlan_filters);
2369 		if (retval)
2370 			goto err_no_memory_locked;
2371 
2372 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2373 	}
2374 
2375 	/* Now process 'del_list' outside the lock */
2376 	if (!hlist_empty(&tmp_del_list)) {
2377 		filter_list_len = hw->aq.asq_buf_size /
2378 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2379 		list_size = filter_list_len *
2380 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2381 		del_list = kzalloc(list_size, GFP_ATOMIC);
2382 		if (!del_list)
2383 			goto err_no_memory;
2384 
2385 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2386 			cmd_flags = 0;
2387 
2388 			/* handle broadcast filters by updating the broadcast
2389 			 * promiscuous flag and release filter list.
2390 			 */
2391 			if (is_broadcast_ether_addr(f->macaddr)) {
2392 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2393 
2394 				hlist_del(&f->hlist);
2395 				kfree(f);
2396 				continue;
2397 			}
2398 
2399 			/* add to delete list */
2400 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2401 			if (f->vlan == I40E_VLAN_ANY) {
2402 				del_list[num_del].vlan_tag = 0;
2403 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2404 			} else {
2405 				del_list[num_del].vlan_tag =
2406 					cpu_to_le16((u16)(f->vlan));
2407 			}
2408 
2409 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2410 			del_list[num_del].flags = cmd_flags;
2411 			num_del++;
2412 
2413 			/* flush a full buffer */
2414 			if (num_del == filter_list_len) {
2415 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2416 						     num_del, &retval);
2417 				memset(del_list, 0, list_size);
2418 				num_del = 0;
2419 			}
2420 			/* Release memory for MAC filter entries which were
2421 			 * synced up with HW.
2422 			 */
2423 			hlist_del(&f->hlist);
2424 			kfree(f);
2425 		}
2426 
2427 		if (num_del) {
2428 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2429 					     num_del, &retval);
2430 		}
2431 
2432 		kfree(del_list);
2433 		del_list = NULL;
2434 	}
2435 
2436 	if (!hlist_empty(&tmp_add_list)) {
2437 		/* Do all the adds now. */
2438 		filter_list_len = hw->aq.asq_buf_size /
2439 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2440 		list_size = filter_list_len *
2441 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2442 		add_list = kzalloc(list_size, GFP_ATOMIC);
2443 		if (!add_list)
2444 			goto err_no_memory;
2445 
2446 		num_add = 0;
2447 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2448 			/* handle broadcast filters by updating the broadcast
2449 			 * promiscuous flag instead of adding a MAC filter.
2450 			 */
2451 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2452 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2453 							      new->f))
2454 					new->state = I40E_FILTER_FAILED;
2455 				else
2456 					new->state = I40E_FILTER_ACTIVE;
2457 				continue;
2458 			}
2459 
2460 			/* add to add array */
2461 			if (num_add == 0)
2462 				add_head = new;
2463 			cmd_flags = 0;
2464 			ether_addr_copy(add_list[num_add].mac_addr,
2465 					new->f->macaddr);
2466 			if (new->f->vlan == I40E_VLAN_ANY) {
2467 				add_list[num_add].vlan_tag = 0;
2468 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2469 			} else {
2470 				add_list[num_add].vlan_tag =
2471 					cpu_to_le16((u16)(new->f->vlan));
2472 			}
2473 			add_list[num_add].queue_number = 0;
2474 			/* set invalid match method for later detection */
2475 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2476 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2477 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2478 			num_add++;
2479 
2480 			/* flush a full buffer */
2481 			if (num_add == filter_list_len) {
2482 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2483 						     add_head, num_add);
2484 				memset(add_list, 0, list_size);
2485 				num_add = 0;
2486 			}
2487 		}
2488 		if (num_add) {
2489 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2490 					     num_add);
2491 		}
2492 		/* Now move all of the filters from the temp add list back to
2493 		 * the VSI's list.
2494 		 */
2495 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2496 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2497 			/* Only update the state if we're still NEW */
2498 			if (new->f->state == I40E_FILTER_NEW)
2499 				new->f->state = new->state;
2500 			hlist_del(&new->hlist);
2501 			kfree(new);
2502 		}
2503 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2504 		kfree(add_list);
2505 		add_list = NULL;
2506 	}
2507 
2508 	/* Determine the number of active and failed filters. */
2509 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2510 	vsi->active_filters = 0;
2511 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2512 		if (f->state == I40E_FILTER_ACTIVE)
2513 			vsi->active_filters++;
2514 		else if (f->state == I40E_FILTER_FAILED)
2515 			failed_filters++;
2516 	}
2517 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2518 
2519 	/* Check if we are able to exit overflow promiscuous mode. We can
2520 	 * safely exit if we didn't just enter, we no longer have any failed
2521 	 * filters, and we have reduced filters below the threshold value.
2522 	 */
2523 	if (old_overflow && !failed_filters &&
2524 	    vsi->active_filters < vsi->promisc_threshold) {
2525 		dev_info(&pf->pdev->dev,
2526 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2527 			 vsi_name);
2528 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2529 		vsi->promisc_threshold = 0;
2530 	}
2531 
2532 	/* if the VF is not trusted do not do promisc */
2533 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2534 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2535 		goto out;
2536 	}
2537 
2538 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2539 
2540 	/* If we are entering overflow promiscuous, we need to calculate a new
2541 	 * threshold for when we are safe to exit
2542 	 */
2543 	if (!old_overflow && new_overflow)
2544 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2545 
2546 	/* check for changes in promiscuous modes */
2547 	if (changed_flags & IFF_ALLMULTI) {
2548 		bool cur_multipromisc;
2549 
2550 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2551 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2552 							       vsi->seid,
2553 							       cur_multipromisc,
2554 							       NULL);
2555 		if (aq_ret) {
2556 			retval = i40e_aq_rc_to_posix(aq_ret,
2557 						     hw->aq.asq_last_status);
2558 			dev_info(&pf->pdev->dev,
2559 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2560 				 vsi_name,
2561 				 i40e_stat_str(hw, aq_ret),
2562 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2563 		} else {
2564 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2565 				 cur_multipromisc ? "entering" : "leaving");
2566 		}
2567 	}
2568 
2569 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2570 		bool cur_promisc;
2571 
2572 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2573 			       new_overflow);
2574 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2575 		if (aq_ret) {
2576 			retval = i40e_aq_rc_to_posix(aq_ret,
2577 						     hw->aq.asq_last_status);
2578 			dev_info(&pf->pdev->dev,
2579 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2580 				 cur_promisc ? "on" : "off",
2581 				 vsi_name,
2582 				 i40e_stat_str(hw, aq_ret),
2583 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2584 		}
2585 	}
2586 out:
2587 	/* if something went wrong then set the changed flag so we try again */
2588 	if (retval)
2589 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2590 
2591 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2592 	return retval;
2593 
2594 err_no_memory:
2595 	/* Restore elements on the temporary add and delete lists */
2596 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2597 err_no_memory_locked:
2598 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2599 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2600 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2601 
2602 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2603 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2604 	return -ENOMEM;
2605 }
2606 
2607 /**
2608  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2609  * @pf: board private structure
2610  **/
2611 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2612 {
2613 	int v;
2614 
2615 	if (!pf)
2616 		return;
2617 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2618 		return;
2619 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2620 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2621 		return;
2622 	}
2623 
2624 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2625 		if (pf->vsi[v] &&
2626 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2627 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2628 
2629 			if (ret) {
2630 				/* come back and try again later */
2631 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2632 					pf->state);
2633 				break;
2634 			}
2635 		}
2636 	}
2637 }
2638 
2639 /**
2640  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2641  * @vsi: the vsi
2642  **/
2643 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2644 {
2645 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2646 		return I40E_RXBUFFER_2048;
2647 	else
2648 		return I40E_RXBUFFER_3072;
2649 }
2650 
2651 /**
2652  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2653  * @netdev: network interface device structure
2654  * @new_mtu: new value for maximum frame size
2655  *
2656  * Returns 0 on success, negative on failure
2657  **/
2658 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2659 {
2660 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2661 	struct i40e_vsi *vsi = np->vsi;
2662 	struct i40e_pf *pf = vsi->back;
2663 
2664 	if (i40e_enabled_xdp_vsi(vsi)) {
2665 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2666 
2667 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2668 			return -EINVAL;
2669 	}
2670 
2671 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2672 		   netdev->mtu, new_mtu);
2673 	netdev->mtu = new_mtu;
2674 	if (netif_running(netdev))
2675 		i40e_vsi_reinit_locked(vsi);
2676 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2677 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2678 	return 0;
2679 }
2680 
2681 /**
2682  * i40e_ioctl - Access the hwtstamp interface
2683  * @netdev: network interface device structure
2684  * @ifr: interface request data
2685  * @cmd: ioctl command
2686  **/
2687 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2688 {
2689 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2690 	struct i40e_pf *pf = np->vsi->back;
2691 
2692 	switch (cmd) {
2693 	case SIOCGHWTSTAMP:
2694 		return i40e_ptp_get_ts_config(pf, ifr);
2695 	case SIOCSHWTSTAMP:
2696 		return i40e_ptp_set_ts_config(pf, ifr);
2697 	default:
2698 		return -EOPNOTSUPP;
2699 	}
2700 }
2701 
2702 /**
2703  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2704  * @vsi: the vsi being adjusted
2705  **/
2706 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2707 {
2708 	struct i40e_vsi_context ctxt;
2709 	i40e_status ret;
2710 
2711 	/* Don't modify stripping options if a port VLAN is active */
2712 	if (vsi->info.pvid)
2713 		return;
2714 
2715 	if ((vsi->info.valid_sections &
2716 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2717 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2718 		return;  /* already enabled */
2719 
2720 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2721 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2722 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2723 
2724 	ctxt.seid = vsi->seid;
2725 	ctxt.info = vsi->info;
2726 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2727 	if (ret) {
2728 		dev_info(&vsi->back->pdev->dev,
2729 			 "update vlan stripping failed, err %s aq_err %s\n",
2730 			 i40e_stat_str(&vsi->back->hw, ret),
2731 			 i40e_aq_str(&vsi->back->hw,
2732 				     vsi->back->hw.aq.asq_last_status));
2733 	}
2734 }
2735 
2736 /**
2737  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2738  * @vsi: the vsi being adjusted
2739  **/
2740 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2741 {
2742 	struct i40e_vsi_context ctxt;
2743 	i40e_status ret;
2744 
2745 	/* Don't modify stripping options if a port VLAN is active */
2746 	if (vsi->info.pvid)
2747 		return;
2748 
2749 	if ((vsi->info.valid_sections &
2750 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2751 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2752 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
2753 		return;  /* already disabled */
2754 
2755 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2756 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2757 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2758 
2759 	ctxt.seid = vsi->seid;
2760 	ctxt.info = vsi->info;
2761 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2762 	if (ret) {
2763 		dev_info(&vsi->back->pdev->dev,
2764 			 "update vlan stripping failed, err %s aq_err %s\n",
2765 			 i40e_stat_str(&vsi->back->hw, ret),
2766 			 i40e_aq_str(&vsi->back->hw,
2767 				     vsi->back->hw.aq.asq_last_status));
2768 	}
2769 }
2770 
2771 /**
2772  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2773  * @vsi: the vsi being configured
2774  * @vid: vlan id to be added (0 = untagged only , -1 = any)
2775  *
2776  * This is a helper function for adding a new MAC/VLAN filter with the
2777  * specified VLAN for each existing MAC address already in the hash table.
2778  * This function does *not* perform any accounting to update filters based on
2779  * VLAN mode.
2780  *
2781  * NOTE: this function expects to be called while under the
2782  * mac_filter_hash_lock
2783  **/
2784 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2785 {
2786 	struct i40e_mac_filter *f, *add_f;
2787 	struct hlist_node *h;
2788 	int bkt;
2789 
2790 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2791 		if (f->state == I40E_FILTER_REMOVE)
2792 			continue;
2793 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
2794 		if (!add_f) {
2795 			dev_info(&vsi->back->pdev->dev,
2796 				 "Could not add vlan filter %d for %pM\n",
2797 				 vid, f->macaddr);
2798 			return -ENOMEM;
2799 		}
2800 	}
2801 
2802 	return 0;
2803 }
2804 
2805 /**
2806  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2807  * @vsi: the VSI being configured
2808  * @vid: VLAN id to be added
2809  **/
2810 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
2811 {
2812 	int err;
2813 
2814 	if (vsi->info.pvid)
2815 		return -EINVAL;
2816 
2817 	/* The network stack will attempt to add VID=0, with the intention to
2818 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
2819 	 * these packets by default when configured to receive untagged
2820 	 * packets, so we don't need to add a filter for this case.
2821 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
2822 	 * receive *only* tagged traffic and stops receiving untagged traffic.
2823 	 * Thus, we do not want to actually add a filter for VID=0
2824 	 */
2825 	if (!vid)
2826 		return 0;
2827 
2828 	/* Locked once because all functions invoked below iterates list*/
2829 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2830 	err = i40e_add_vlan_all_mac(vsi, vid);
2831 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2832 	if (err)
2833 		return err;
2834 
2835 	/* schedule our worker thread which will take care of
2836 	 * applying the new filter changes
2837 	 */
2838 	i40e_service_event_schedule(vsi->back);
2839 	return 0;
2840 }
2841 
2842 /**
2843  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
2844  * @vsi: the vsi being configured
2845  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2846  *
2847  * This function should be used to remove all VLAN filters which match the
2848  * given VID. It does not schedule the service event and does not take the
2849  * mac_filter_hash_lock so it may be combined with other operations under
2850  * a single invocation of the mac_filter_hash_lock.
2851  *
2852  * NOTE: this function expects to be called while under the
2853  * mac_filter_hash_lock
2854  */
2855 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2856 {
2857 	struct i40e_mac_filter *f;
2858 	struct hlist_node *h;
2859 	int bkt;
2860 
2861 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2862 		if (f->vlan == vid)
2863 			__i40e_del_filter(vsi, f);
2864 	}
2865 }
2866 
2867 /**
2868  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2869  * @vsi: the VSI being configured
2870  * @vid: VLAN id to be removed
2871  **/
2872 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
2873 {
2874 	if (!vid || vsi->info.pvid)
2875 		return;
2876 
2877 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2878 	i40e_rm_vlan_all_mac(vsi, vid);
2879 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2880 
2881 	/* schedule our worker thread which will take care of
2882 	 * applying the new filter changes
2883 	 */
2884 	i40e_service_event_schedule(vsi->back);
2885 }
2886 
2887 /**
2888  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2889  * @netdev: network interface to be adjusted
2890  * @proto: unused protocol value
2891  * @vid: vlan id to be added
2892  *
2893  * net_device_ops implementation for adding vlan ids
2894  **/
2895 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2896 				__always_unused __be16 proto, u16 vid)
2897 {
2898 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2899 	struct i40e_vsi *vsi = np->vsi;
2900 	int ret = 0;
2901 
2902 	if (vid >= VLAN_N_VID)
2903 		return -EINVAL;
2904 
2905 	ret = i40e_vsi_add_vlan(vsi, vid);
2906 	if (!ret)
2907 		set_bit(vid, vsi->active_vlans);
2908 
2909 	return ret;
2910 }
2911 
2912 /**
2913  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
2914  * @netdev: network interface to be adjusted
2915  * @proto: unused protocol value
2916  * @vid: vlan id to be added
2917  **/
2918 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
2919 				    __always_unused __be16 proto, u16 vid)
2920 {
2921 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2922 	struct i40e_vsi *vsi = np->vsi;
2923 
2924 	if (vid >= VLAN_N_VID)
2925 		return;
2926 	set_bit(vid, vsi->active_vlans);
2927 }
2928 
2929 /**
2930  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2931  * @netdev: network interface to be adjusted
2932  * @proto: unused protocol value
2933  * @vid: vlan id to be removed
2934  *
2935  * net_device_ops implementation for removing vlan ids
2936  **/
2937 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2938 				 __always_unused __be16 proto, u16 vid)
2939 {
2940 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2941 	struct i40e_vsi *vsi = np->vsi;
2942 
2943 	/* return code is ignored as there is nothing a user
2944 	 * can do about failure to remove and a log message was
2945 	 * already printed from the other function
2946 	 */
2947 	i40e_vsi_kill_vlan(vsi, vid);
2948 
2949 	clear_bit(vid, vsi->active_vlans);
2950 
2951 	return 0;
2952 }
2953 
2954 /**
2955  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2956  * @vsi: the vsi being brought back up
2957  **/
2958 static void i40e_restore_vlan(struct i40e_vsi *vsi)
2959 {
2960 	u16 vid;
2961 
2962 	if (!vsi->netdev)
2963 		return;
2964 
2965 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2966 		i40e_vlan_stripping_enable(vsi);
2967 	else
2968 		i40e_vlan_stripping_disable(vsi);
2969 
2970 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2971 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
2972 					vid);
2973 }
2974 
2975 /**
2976  * i40e_vsi_add_pvid - Add pvid for the VSI
2977  * @vsi: the vsi being adjusted
2978  * @vid: the vlan id to set as a PVID
2979  **/
2980 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
2981 {
2982 	struct i40e_vsi_context ctxt;
2983 	i40e_status ret;
2984 
2985 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2986 	vsi->info.pvid = cpu_to_le16(vid);
2987 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2988 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
2989 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
2990 
2991 	ctxt.seid = vsi->seid;
2992 	ctxt.info = vsi->info;
2993 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2994 	if (ret) {
2995 		dev_info(&vsi->back->pdev->dev,
2996 			 "add pvid failed, err %s aq_err %s\n",
2997 			 i40e_stat_str(&vsi->back->hw, ret),
2998 			 i40e_aq_str(&vsi->back->hw,
2999 				     vsi->back->hw.aq.asq_last_status));
3000 		return -ENOENT;
3001 	}
3002 
3003 	return 0;
3004 }
3005 
3006 /**
3007  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3008  * @vsi: the vsi being adjusted
3009  *
3010  * Just use the vlan_rx_register() service to put it back to normal
3011  **/
3012 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3013 {
3014 	vsi->info.pvid = 0;
3015 
3016 	i40e_vlan_stripping_disable(vsi);
3017 }
3018 
3019 /**
3020  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3021  * @vsi: ptr to the VSI
3022  *
3023  * If this function returns with an error, then it's possible one or
3024  * more of the rings is populated (while the rest are not).  It is the
3025  * callers duty to clean those orphaned rings.
3026  *
3027  * Return 0 on success, negative on failure
3028  **/
3029 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3030 {
3031 	int i, err = 0;
3032 
3033 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3034 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3035 
3036 	if (!i40e_enabled_xdp_vsi(vsi))
3037 		return err;
3038 
3039 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3040 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3041 
3042 	return err;
3043 }
3044 
3045 /**
3046  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3047  * @vsi: ptr to the VSI
3048  *
3049  * Free VSI's transmit software resources
3050  **/
3051 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3052 {
3053 	int i;
3054 
3055 	if (vsi->tx_rings) {
3056 		for (i = 0; i < vsi->num_queue_pairs; i++)
3057 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3058 				i40e_free_tx_resources(vsi->tx_rings[i]);
3059 	}
3060 
3061 	if (vsi->xdp_rings) {
3062 		for (i = 0; i < vsi->num_queue_pairs; i++)
3063 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3064 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3065 	}
3066 }
3067 
3068 /**
3069  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3070  * @vsi: ptr to the VSI
3071  *
3072  * If this function returns with an error, then it's possible one or
3073  * more of the rings is populated (while the rest are not).  It is the
3074  * callers duty to clean those orphaned rings.
3075  *
3076  * Return 0 on success, negative on failure
3077  **/
3078 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3079 {
3080 	int i, err = 0;
3081 
3082 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3083 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3084 	return err;
3085 }
3086 
3087 /**
3088  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3089  * @vsi: ptr to the VSI
3090  *
3091  * Free all receive software resources
3092  **/
3093 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3094 {
3095 	int i;
3096 
3097 	if (!vsi->rx_rings)
3098 		return;
3099 
3100 	for (i = 0; i < vsi->num_queue_pairs; i++)
3101 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3102 			i40e_free_rx_resources(vsi->rx_rings[i]);
3103 }
3104 
3105 /**
3106  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3107  * @ring: The Tx ring to configure
3108  *
3109  * This enables/disables XPS for a given Tx descriptor ring
3110  * based on the TCs enabled for the VSI that ring belongs to.
3111  **/
3112 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3113 {
3114 	int cpu;
3115 
3116 	if (!ring->q_vector || !ring->netdev || ring->ch)
3117 		return;
3118 
3119 	/* We only initialize XPS once, so as not to overwrite user settings */
3120 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3121 		return;
3122 
3123 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3124 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3125 			    ring->queue_index);
3126 }
3127 
3128 /**
3129  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3130  * @ring: The Tx or Rx ring
3131  *
3132  * Returns the AF_XDP buffer pool or NULL.
3133  **/
3134 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3135 {
3136 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3137 	int qid = ring->queue_index;
3138 
3139 	if (ring_is_xdp(ring))
3140 		qid -= ring->vsi->alloc_queue_pairs;
3141 
3142 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3143 		return NULL;
3144 
3145 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3146 }
3147 
3148 /**
3149  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3150  * @ring: The Tx ring to configure
3151  *
3152  * Configure the Tx descriptor ring in the HMC context.
3153  **/
3154 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3155 {
3156 	struct i40e_vsi *vsi = ring->vsi;
3157 	u16 pf_q = vsi->base_queue + ring->queue_index;
3158 	struct i40e_hw *hw = &vsi->back->hw;
3159 	struct i40e_hmc_obj_txq tx_ctx;
3160 	i40e_status err = 0;
3161 	u32 qtx_ctl = 0;
3162 
3163 	if (ring_is_xdp(ring))
3164 		ring->xsk_pool = i40e_xsk_pool(ring);
3165 
3166 	/* some ATR related tx ring init */
3167 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3168 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3169 		ring->atr_count = 0;
3170 	} else {
3171 		ring->atr_sample_rate = 0;
3172 	}
3173 
3174 	/* configure XPS */
3175 	i40e_config_xps_tx_ring(ring);
3176 
3177 	/* clear the context structure first */
3178 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3179 
3180 	tx_ctx.new_context = 1;
3181 	tx_ctx.base = (ring->dma / 128);
3182 	tx_ctx.qlen = ring->count;
3183 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3184 					       I40E_FLAG_FD_ATR_ENABLED));
3185 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3186 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3187 	if (vsi->type != I40E_VSI_FDIR)
3188 		tx_ctx.head_wb_ena = 1;
3189 	tx_ctx.head_wb_addr = ring->dma +
3190 			      (ring->count * sizeof(struct i40e_tx_desc));
3191 
3192 	/* As part of VSI creation/update, FW allocates certain
3193 	 * Tx arbitration queue sets for each TC enabled for
3194 	 * the VSI. The FW returns the handles to these queue
3195 	 * sets as part of the response buffer to Add VSI,
3196 	 * Update VSI, etc. AQ commands. It is expected that
3197 	 * these queue set handles be associated with the Tx
3198 	 * queues by the driver as part of the TX queue context
3199 	 * initialization. This has to be done regardless of
3200 	 * DCB as by default everything is mapped to TC0.
3201 	 */
3202 
3203 	if (ring->ch)
3204 		tx_ctx.rdylist =
3205 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3206 
3207 	else
3208 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3209 
3210 	tx_ctx.rdylist_act = 0;
3211 
3212 	/* clear the context in the HMC */
3213 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3214 	if (err) {
3215 		dev_info(&vsi->back->pdev->dev,
3216 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3217 			 ring->queue_index, pf_q, err);
3218 		return -ENOMEM;
3219 	}
3220 
3221 	/* set the context in the HMC */
3222 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3223 	if (err) {
3224 		dev_info(&vsi->back->pdev->dev,
3225 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3226 			 ring->queue_index, pf_q, err);
3227 		return -ENOMEM;
3228 	}
3229 
3230 	/* Now associate this queue with this PCI function */
3231 	if (ring->ch) {
3232 		if (ring->ch->type == I40E_VSI_VMDQ2)
3233 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3234 		else
3235 			return -EINVAL;
3236 
3237 		qtx_ctl |= (ring->ch->vsi_number <<
3238 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3239 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3240 	} else {
3241 		if (vsi->type == I40E_VSI_VMDQ2) {
3242 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3243 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3244 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3245 		} else {
3246 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3247 		}
3248 	}
3249 
3250 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3251 		    I40E_QTX_CTL_PF_INDX_MASK);
3252 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3253 	i40e_flush(hw);
3254 
3255 	/* cache tail off for easier writes later */
3256 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3257 
3258 	return 0;
3259 }
3260 
3261 /**
3262  * i40e_rx_offset - Return expected offset into page to access data
3263  * @rx_ring: Ring we are requesting offset of
3264  *
3265  * Returns the offset value for ring into the data buffer.
3266  */
3267 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3268 {
3269 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3270 }
3271 
3272 /**
3273  * i40e_configure_rx_ring - Configure a receive ring context
3274  * @ring: The Rx ring to configure
3275  *
3276  * Configure the Rx descriptor ring in the HMC context.
3277  **/
3278 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3279 {
3280 	struct i40e_vsi *vsi = ring->vsi;
3281 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3282 	u16 pf_q = vsi->base_queue + ring->queue_index;
3283 	struct i40e_hw *hw = &vsi->back->hw;
3284 	struct i40e_hmc_obj_rxq rx_ctx;
3285 	i40e_status err = 0;
3286 	bool ok;
3287 	int ret;
3288 
3289 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3290 
3291 	/* clear the context structure first */
3292 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3293 
3294 	if (ring->vsi->type == I40E_VSI_MAIN)
3295 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3296 
3297 	kfree(ring->rx_bi);
3298 	ring->xsk_pool = i40e_xsk_pool(ring);
3299 	if (ring->xsk_pool) {
3300 		ret = i40e_alloc_rx_bi_zc(ring);
3301 		if (ret)
3302 			return ret;
3303 		ring->rx_buf_len =
3304 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3305 		/* For AF_XDP ZC, we disallow packets to span on
3306 		 * multiple buffers, thus letting us skip that
3307 		 * handling in the fast-path.
3308 		 */
3309 		chain_len = 1;
3310 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3311 						 MEM_TYPE_XSK_BUFF_POOL,
3312 						 NULL);
3313 		if (ret)
3314 			return ret;
3315 		dev_info(&vsi->back->pdev->dev,
3316 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3317 			 ring->queue_index);
3318 
3319 	} else {
3320 		ret = i40e_alloc_rx_bi(ring);
3321 		if (ret)
3322 			return ret;
3323 		ring->rx_buf_len = vsi->rx_buf_len;
3324 		if (ring->vsi->type == I40E_VSI_MAIN) {
3325 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3326 							 MEM_TYPE_PAGE_SHARED,
3327 							 NULL);
3328 			if (ret)
3329 				return ret;
3330 		}
3331 	}
3332 
3333 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3334 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3335 
3336 	rx_ctx.base = (ring->dma / 128);
3337 	rx_ctx.qlen = ring->count;
3338 
3339 	/* use 16 byte descriptors */
3340 	rx_ctx.dsize = 0;
3341 
3342 	/* descriptor type is always zero
3343 	 * rx_ctx.dtype = 0;
3344 	 */
3345 	rx_ctx.hsplit_0 = 0;
3346 
3347 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3348 	if (hw->revision_id == 0)
3349 		rx_ctx.lrxqthresh = 0;
3350 	else
3351 		rx_ctx.lrxqthresh = 1;
3352 	rx_ctx.crcstrip = 1;
3353 	rx_ctx.l2tsel = 1;
3354 	/* this controls whether VLAN is stripped from inner headers */
3355 	rx_ctx.showiv = 0;
3356 	/* set the prefena field to 1 because the manual says to */
3357 	rx_ctx.prefena = 1;
3358 
3359 	/* clear the context in the HMC */
3360 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3361 	if (err) {
3362 		dev_info(&vsi->back->pdev->dev,
3363 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3364 			 ring->queue_index, pf_q, err);
3365 		return -ENOMEM;
3366 	}
3367 
3368 	/* set the context in the HMC */
3369 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3370 	if (err) {
3371 		dev_info(&vsi->back->pdev->dev,
3372 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3373 			 ring->queue_index, pf_q, err);
3374 		return -ENOMEM;
3375 	}
3376 
3377 	/* configure Rx buffer alignment */
3378 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3379 		clear_ring_build_skb_enabled(ring);
3380 	else
3381 		set_ring_build_skb_enabled(ring);
3382 
3383 	ring->rx_offset = i40e_rx_offset(ring);
3384 
3385 	/* cache tail for quicker writes, and clear the reg before use */
3386 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3387 	writel(0, ring->tail);
3388 
3389 	if (ring->xsk_pool) {
3390 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3391 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3392 	} else {
3393 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3394 	}
3395 	if (!ok) {
3396 		/* Log this in case the user has forgotten to give the kernel
3397 		 * any buffers, even later in the application.
3398 		 */
3399 		dev_info(&vsi->back->pdev->dev,
3400 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3401 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3402 			 ring->queue_index, pf_q);
3403 	}
3404 
3405 	return 0;
3406 }
3407 
3408 /**
3409  * i40e_vsi_configure_tx - Configure the VSI for Tx
3410  * @vsi: VSI structure describing this set of rings and resources
3411  *
3412  * Configure the Tx VSI for operation.
3413  **/
3414 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3415 {
3416 	int err = 0;
3417 	u16 i;
3418 
3419 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3420 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3421 
3422 	if (err || !i40e_enabled_xdp_vsi(vsi))
3423 		return err;
3424 
3425 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3426 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3427 
3428 	return err;
3429 }
3430 
3431 /**
3432  * i40e_vsi_configure_rx - Configure the VSI for Rx
3433  * @vsi: the VSI being configured
3434  *
3435  * Configure the Rx VSI for operation.
3436  **/
3437 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3438 {
3439 	int err = 0;
3440 	u16 i;
3441 
3442 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3443 		vsi->max_frame = I40E_MAX_RXBUFFER;
3444 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3445 #if (PAGE_SIZE < 8192)
3446 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3447 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3448 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3449 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3450 #endif
3451 	} else {
3452 		vsi->max_frame = I40E_MAX_RXBUFFER;
3453 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3454 						       I40E_RXBUFFER_2048;
3455 	}
3456 
3457 	/* set up individual rings */
3458 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3459 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3460 
3461 	return err;
3462 }
3463 
3464 /**
3465  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3466  * @vsi: ptr to the VSI
3467  **/
3468 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3469 {
3470 	struct i40e_ring *tx_ring, *rx_ring;
3471 	u16 qoffset, qcount;
3472 	int i, n;
3473 
3474 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3475 		/* Reset the TC information */
3476 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3477 			rx_ring = vsi->rx_rings[i];
3478 			tx_ring = vsi->tx_rings[i];
3479 			rx_ring->dcb_tc = 0;
3480 			tx_ring->dcb_tc = 0;
3481 		}
3482 		return;
3483 	}
3484 
3485 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3486 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3487 			continue;
3488 
3489 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3490 		qcount = vsi->tc_config.tc_info[n].qcount;
3491 		for (i = qoffset; i < (qoffset + qcount); i++) {
3492 			rx_ring = vsi->rx_rings[i];
3493 			tx_ring = vsi->tx_rings[i];
3494 			rx_ring->dcb_tc = n;
3495 			tx_ring->dcb_tc = n;
3496 		}
3497 	}
3498 }
3499 
3500 /**
3501  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3502  * @vsi: ptr to the VSI
3503  **/
3504 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3505 {
3506 	if (vsi->netdev)
3507 		i40e_set_rx_mode(vsi->netdev);
3508 }
3509 
3510 /**
3511  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3512  * @pf: Pointer to the targeted PF
3513  *
3514  * Set all flow director counters to 0.
3515  */
3516 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3517 {
3518 	pf->fd_tcp4_filter_cnt = 0;
3519 	pf->fd_udp4_filter_cnt = 0;
3520 	pf->fd_sctp4_filter_cnt = 0;
3521 	pf->fd_ip4_filter_cnt = 0;
3522 	pf->fd_tcp6_filter_cnt = 0;
3523 	pf->fd_udp6_filter_cnt = 0;
3524 	pf->fd_sctp6_filter_cnt = 0;
3525 	pf->fd_ip6_filter_cnt = 0;
3526 }
3527 
3528 /**
3529  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3530  * @vsi: Pointer to the targeted VSI
3531  *
3532  * This function replays the hlist on the hw where all the SB Flow Director
3533  * filters were saved.
3534  **/
3535 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3536 {
3537 	struct i40e_fdir_filter *filter;
3538 	struct i40e_pf *pf = vsi->back;
3539 	struct hlist_node *node;
3540 
3541 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3542 		return;
3543 
3544 	/* Reset FDir counters as we're replaying all existing filters */
3545 	i40e_reset_fdir_filter_cnt(pf);
3546 
3547 	hlist_for_each_entry_safe(filter, node,
3548 				  &pf->fdir_filter_list, fdir_node) {
3549 		i40e_add_del_fdir(vsi, filter, true);
3550 	}
3551 }
3552 
3553 /**
3554  * i40e_vsi_configure - Set up the VSI for action
3555  * @vsi: the VSI being configured
3556  **/
3557 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3558 {
3559 	int err;
3560 
3561 	i40e_set_vsi_rx_mode(vsi);
3562 	i40e_restore_vlan(vsi);
3563 	i40e_vsi_config_dcb_rings(vsi);
3564 	err = i40e_vsi_configure_tx(vsi);
3565 	if (!err)
3566 		err = i40e_vsi_configure_rx(vsi);
3567 
3568 	return err;
3569 }
3570 
3571 /**
3572  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3573  * @vsi: the VSI being configured
3574  **/
3575 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3576 {
3577 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3578 	struct i40e_pf *pf = vsi->back;
3579 	struct i40e_hw *hw = &pf->hw;
3580 	u16 vector;
3581 	int i, q;
3582 	u32 qp;
3583 
3584 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3585 	 * and PFINT_LNKLSTn registers, e.g.:
3586 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3587 	 */
3588 	qp = vsi->base_queue;
3589 	vector = vsi->base_vector;
3590 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3591 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3592 
3593 		q_vector->rx.next_update = jiffies + 1;
3594 		q_vector->rx.target_itr =
3595 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3596 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3597 		     q_vector->rx.target_itr >> 1);
3598 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3599 
3600 		q_vector->tx.next_update = jiffies + 1;
3601 		q_vector->tx.target_itr =
3602 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3603 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3604 		     q_vector->tx.target_itr >> 1);
3605 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3606 
3607 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3608 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3609 
3610 		/* Linked list for the queuepairs assigned to this vector */
3611 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3612 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3613 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3614 			u32 val;
3615 
3616 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3617 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3618 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3619 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3620 			      (I40E_QUEUE_TYPE_TX <<
3621 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3622 
3623 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3624 
3625 			if (has_xdp) {
3626 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3627 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3628 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3629 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3630 				      (I40E_QUEUE_TYPE_TX <<
3631 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3632 
3633 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3634 			}
3635 
3636 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3637 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3638 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3639 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3640 			      (I40E_QUEUE_TYPE_RX <<
3641 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3642 
3643 			/* Terminate the linked list */
3644 			if (q == (q_vector->num_ringpairs - 1))
3645 				val |= (I40E_QUEUE_END_OF_LIST <<
3646 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3647 
3648 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3649 			qp++;
3650 		}
3651 	}
3652 
3653 	i40e_flush(hw);
3654 }
3655 
3656 /**
3657  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3658  * @pf: pointer to private device data structure
3659  **/
3660 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3661 {
3662 	struct i40e_hw *hw = &pf->hw;
3663 	u32 val;
3664 
3665 	/* clear things first */
3666 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3667 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3668 
3669 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3670 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3671 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3672 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3673 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3674 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3675 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3676 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3677 
3678 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3679 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3680 
3681 	if (pf->flags & I40E_FLAG_PTP)
3682 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3683 
3684 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3685 
3686 	/* SW_ITR_IDX = 0, but don't change INTENA */
3687 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3688 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3689 
3690 	/* OTHER_ITR_IDX = 0 */
3691 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3692 }
3693 
3694 /**
3695  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3696  * @vsi: the VSI being configured
3697  **/
3698 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3699 {
3700 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3701 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3702 	struct i40e_pf *pf = vsi->back;
3703 	struct i40e_hw *hw = &pf->hw;
3704 	u32 val;
3705 
3706 	/* set the ITR configuration */
3707 	q_vector->rx.next_update = jiffies + 1;
3708 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3709 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3710 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3711 	q_vector->tx.next_update = jiffies + 1;
3712 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3713 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3714 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3715 
3716 	i40e_enable_misc_int_causes(pf);
3717 
3718 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3719 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3720 
3721 	/* Associate the queue pair to the vector and enable the queue int */
3722 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3723 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3724 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3725 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3726 
3727 	wr32(hw, I40E_QINT_RQCTL(0), val);
3728 
3729 	if (i40e_enabled_xdp_vsi(vsi)) {
3730 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
3731 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3732 		      (I40E_QUEUE_TYPE_TX
3733 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3734 
3735 		wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3736 	}
3737 
3738 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
3739 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3740 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3741 
3742 	wr32(hw, I40E_QINT_TQCTL(0), val);
3743 	i40e_flush(hw);
3744 }
3745 
3746 /**
3747  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3748  * @pf: board private structure
3749  **/
3750 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3751 {
3752 	struct i40e_hw *hw = &pf->hw;
3753 
3754 	wr32(hw, I40E_PFINT_DYN_CTL0,
3755 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3756 	i40e_flush(hw);
3757 }
3758 
3759 /**
3760  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3761  * @pf: board private structure
3762  **/
3763 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3764 {
3765 	struct i40e_hw *hw = &pf->hw;
3766 	u32 val;
3767 
3768 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
3769 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3770 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3771 
3772 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
3773 	i40e_flush(hw);
3774 }
3775 
3776 /**
3777  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3778  * @irq: interrupt number
3779  * @data: pointer to a q_vector
3780  **/
3781 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3782 {
3783 	struct i40e_q_vector *q_vector = data;
3784 
3785 	if (!q_vector->tx.ring && !q_vector->rx.ring)
3786 		return IRQ_HANDLED;
3787 
3788 	napi_schedule_irqoff(&q_vector->napi);
3789 
3790 	return IRQ_HANDLED;
3791 }
3792 
3793 /**
3794  * i40e_irq_affinity_notify - Callback for affinity changes
3795  * @notify: context as to what irq was changed
3796  * @mask: the new affinity mask
3797  *
3798  * This is a callback function used by the irq_set_affinity_notifier function
3799  * so that we may register to receive changes to the irq affinity masks.
3800  **/
3801 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3802 				     const cpumask_t *mask)
3803 {
3804 	struct i40e_q_vector *q_vector =
3805 		container_of(notify, struct i40e_q_vector, affinity_notify);
3806 
3807 	cpumask_copy(&q_vector->affinity_mask, mask);
3808 }
3809 
3810 /**
3811  * i40e_irq_affinity_release - Callback for affinity notifier release
3812  * @ref: internal core kernel usage
3813  *
3814  * This is a callback function used by the irq_set_affinity_notifier function
3815  * to inform the current notification subscriber that they will no longer
3816  * receive notifications.
3817  **/
3818 static void i40e_irq_affinity_release(struct kref *ref) {}
3819 
3820 /**
3821  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3822  * @vsi: the VSI being configured
3823  * @basename: name for the vector
3824  *
3825  * Allocates MSI-X vectors and requests interrupts from the kernel.
3826  **/
3827 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3828 {
3829 	int q_vectors = vsi->num_q_vectors;
3830 	struct i40e_pf *pf = vsi->back;
3831 	int base = vsi->base_vector;
3832 	int rx_int_idx = 0;
3833 	int tx_int_idx = 0;
3834 	int vector, err;
3835 	int irq_num;
3836 	int cpu;
3837 
3838 	for (vector = 0; vector < q_vectors; vector++) {
3839 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
3840 
3841 		irq_num = pf->msix_entries[base + vector].vector;
3842 
3843 		if (q_vector->tx.ring && q_vector->rx.ring) {
3844 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3845 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3846 			tx_int_idx++;
3847 		} else if (q_vector->rx.ring) {
3848 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3849 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
3850 		} else if (q_vector->tx.ring) {
3851 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3852 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
3853 		} else {
3854 			/* skip this unused q_vector */
3855 			continue;
3856 		}
3857 		err = request_irq(irq_num,
3858 				  vsi->irq_handler,
3859 				  0,
3860 				  q_vector->name,
3861 				  q_vector);
3862 		if (err) {
3863 			dev_info(&pf->pdev->dev,
3864 				 "MSIX request_irq failed, error: %d\n", err);
3865 			goto free_queue_irqs;
3866 		}
3867 
3868 		/* register for affinity change notifications */
3869 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3870 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
3871 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
3872 		/* Spread affinity hints out across online CPUs.
3873 		 *
3874 		 * get_cpu_mask returns a static constant mask with
3875 		 * a permanent lifetime so it's ok to pass to
3876 		 * irq_set_affinity_hint without making a copy.
3877 		 */
3878 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
3879 		irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
3880 	}
3881 
3882 	vsi->irqs_ready = true;
3883 	return 0;
3884 
3885 free_queue_irqs:
3886 	while (vector) {
3887 		vector--;
3888 		irq_num = pf->msix_entries[base + vector].vector;
3889 		irq_set_affinity_notifier(irq_num, NULL);
3890 		irq_set_affinity_hint(irq_num, NULL);
3891 		free_irq(irq_num, &vsi->q_vectors[vector]);
3892 	}
3893 	return err;
3894 }
3895 
3896 /**
3897  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3898  * @vsi: the VSI being un-configured
3899  **/
3900 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3901 {
3902 	struct i40e_pf *pf = vsi->back;
3903 	struct i40e_hw *hw = &pf->hw;
3904 	int base = vsi->base_vector;
3905 	int i;
3906 
3907 	/* disable interrupt causation from each queue */
3908 	for (i = 0; i < vsi->num_queue_pairs; i++) {
3909 		u32 val;
3910 
3911 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3912 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3913 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3914 
3915 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3916 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3917 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3918 
3919 		if (!i40e_enabled_xdp_vsi(vsi))
3920 			continue;
3921 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
3922 	}
3923 
3924 	/* disable each interrupt */
3925 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3926 		for (i = vsi->base_vector;
3927 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
3928 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3929 
3930 		i40e_flush(hw);
3931 		for (i = 0; i < vsi->num_q_vectors; i++)
3932 			synchronize_irq(pf->msix_entries[i + base].vector);
3933 	} else {
3934 		/* Legacy and MSI mode - this stops all interrupt handling */
3935 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3936 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3937 		i40e_flush(hw);
3938 		synchronize_irq(pf->pdev->irq);
3939 	}
3940 }
3941 
3942 /**
3943  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3944  * @vsi: the VSI being configured
3945  **/
3946 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3947 {
3948 	struct i40e_pf *pf = vsi->back;
3949 	int i;
3950 
3951 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3952 		for (i = 0; i < vsi->num_q_vectors; i++)
3953 			i40e_irq_dynamic_enable(vsi, i);
3954 	} else {
3955 		i40e_irq_dynamic_enable_icr0(pf);
3956 	}
3957 
3958 	i40e_flush(&pf->hw);
3959 	return 0;
3960 }
3961 
3962 /**
3963  * i40e_free_misc_vector - Free the vector that handles non-queue events
3964  * @pf: board private structure
3965  **/
3966 static void i40e_free_misc_vector(struct i40e_pf *pf)
3967 {
3968 	/* Disable ICR 0 */
3969 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3970 	i40e_flush(&pf->hw);
3971 
3972 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
3973 		synchronize_irq(pf->msix_entries[0].vector);
3974 		free_irq(pf->msix_entries[0].vector, pf);
3975 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
3976 	}
3977 }
3978 
3979 /**
3980  * i40e_intr - MSI/Legacy and non-queue interrupt handler
3981  * @irq: interrupt number
3982  * @data: pointer to a q_vector
3983  *
3984  * This is the handler used for all MSI/Legacy interrupts, and deals
3985  * with both queue and non-queue interrupts.  This is also used in
3986  * MSIX mode to handle the non-queue interrupts.
3987  **/
3988 static irqreturn_t i40e_intr(int irq, void *data)
3989 {
3990 	struct i40e_pf *pf = (struct i40e_pf *)data;
3991 	struct i40e_hw *hw = &pf->hw;
3992 	irqreturn_t ret = IRQ_NONE;
3993 	u32 icr0, icr0_remaining;
3994 	u32 val, ena_mask;
3995 
3996 	icr0 = rd32(hw, I40E_PFINT_ICR0);
3997 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
3998 
3999 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4000 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4001 		goto enable_intr;
4002 
4003 	/* if interrupt but no bits showing, must be SWINT */
4004 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4005 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4006 		pf->sw_int_count++;
4007 
4008 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4009 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4010 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4011 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4012 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4013 	}
4014 
4015 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4016 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4017 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4018 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4019 
4020 		/* We do not have a way to disarm Queue causes while leaving
4021 		 * interrupt enabled for all other causes, ideally
4022 		 * interrupt should be disabled while we are in NAPI but
4023 		 * this is not a performance path and napi_schedule()
4024 		 * can deal with rescheduling.
4025 		 */
4026 		if (!test_bit(__I40E_DOWN, pf->state))
4027 			napi_schedule_irqoff(&q_vector->napi);
4028 	}
4029 
4030 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4031 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4032 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4033 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4034 	}
4035 
4036 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4037 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4038 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4039 	}
4040 
4041 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4042 		/* disable any further VFLR event notifications */
4043 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4044 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4045 
4046 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4047 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4048 		} else {
4049 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4050 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4051 		}
4052 	}
4053 
4054 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4055 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4056 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4057 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4058 		val = rd32(hw, I40E_GLGEN_RSTAT);
4059 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4060 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4061 		if (val == I40E_RESET_CORER) {
4062 			pf->corer_count++;
4063 		} else if (val == I40E_RESET_GLOBR) {
4064 			pf->globr_count++;
4065 		} else if (val == I40E_RESET_EMPR) {
4066 			pf->empr_count++;
4067 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4068 		}
4069 	}
4070 
4071 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4072 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4073 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4074 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4075 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4076 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4077 	}
4078 
4079 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4080 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4081 
4082 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4083 			schedule_work(&pf->ptp_extts0_work);
4084 
4085 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4086 			i40e_ptp_tx_hwtstamp(pf);
4087 
4088 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4089 	}
4090 
4091 	/* If a critical error is pending we have no choice but to reset the
4092 	 * device.
4093 	 * Report and mask out any remaining unexpected interrupts.
4094 	 */
4095 	icr0_remaining = icr0 & ena_mask;
4096 	if (icr0_remaining) {
4097 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4098 			 icr0_remaining);
4099 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4100 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4101 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4102 			dev_info(&pf->pdev->dev, "device will be reset\n");
4103 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4104 			i40e_service_event_schedule(pf);
4105 		}
4106 		ena_mask &= ~icr0_remaining;
4107 	}
4108 	ret = IRQ_HANDLED;
4109 
4110 enable_intr:
4111 	/* re-enable interrupt causes */
4112 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4113 	if (!test_bit(__I40E_DOWN, pf->state) ||
4114 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4115 		i40e_service_event_schedule(pf);
4116 		i40e_irq_dynamic_enable_icr0(pf);
4117 	}
4118 
4119 	return ret;
4120 }
4121 
4122 /**
4123  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4124  * @tx_ring:  tx ring to clean
4125  * @budget:   how many cleans we're allowed
4126  *
4127  * Returns true if there's any budget left (e.g. the clean is finished)
4128  **/
4129 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4130 {
4131 	struct i40e_vsi *vsi = tx_ring->vsi;
4132 	u16 i = tx_ring->next_to_clean;
4133 	struct i40e_tx_buffer *tx_buf;
4134 	struct i40e_tx_desc *tx_desc;
4135 
4136 	tx_buf = &tx_ring->tx_bi[i];
4137 	tx_desc = I40E_TX_DESC(tx_ring, i);
4138 	i -= tx_ring->count;
4139 
4140 	do {
4141 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4142 
4143 		/* if next_to_watch is not set then there is no work pending */
4144 		if (!eop_desc)
4145 			break;
4146 
4147 		/* prevent any other reads prior to eop_desc */
4148 		smp_rmb();
4149 
4150 		/* if the descriptor isn't done, no work yet to do */
4151 		if (!(eop_desc->cmd_type_offset_bsz &
4152 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4153 			break;
4154 
4155 		/* clear next_to_watch to prevent false hangs */
4156 		tx_buf->next_to_watch = NULL;
4157 
4158 		tx_desc->buffer_addr = 0;
4159 		tx_desc->cmd_type_offset_bsz = 0;
4160 		/* move past filter desc */
4161 		tx_buf++;
4162 		tx_desc++;
4163 		i++;
4164 		if (unlikely(!i)) {
4165 			i -= tx_ring->count;
4166 			tx_buf = tx_ring->tx_bi;
4167 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4168 		}
4169 		/* unmap skb header data */
4170 		dma_unmap_single(tx_ring->dev,
4171 				 dma_unmap_addr(tx_buf, dma),
4172 				 dma_unmap_len(tx_buf, len),
4173 				 DMA_TO_DEVICE);
4174 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4175 			kfree(tx_buf->raw_buf);
4176 
4177 		tx_buf->raw_buf = NULL;
4178 		tx_buf->tx_flags = 0;
4179 		tx_buf->next_to_watch = NULL;
4180 		dma_unmap_len_set(tx_buf, len, 0);
4181 		tx_desc->buffer_addr = 0;
4182 		tx_desc->cmd_type_offset_bsz = 0;
4183 
4184 		/* move us past the eop_desc for start of next FD desc */
4185 		tx_buf++;
4186 		tx_desc++;
4187 		i++;
4188 		if (unlikely(!i)) {
4189 			i -= tx_ring->count;
4190 			tx_buf = tx_ring->tx_bi;
4191 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4192 		}
4193 
4194 		/* update budget accounting */
4195 		budget--;
4196 	} while (likely(budget));
4197 
4198 	i += tx_ring->count;
4199 	tx_ring->next_to_clean = i;
4200 
4201 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4202 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4203 
4204 	return budget > 0;
4205 }
4206 
4207 /**
4208  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4209  * @irq: interrupt number
4210  * @data: pointer to a q_vector
4211  **/
4212 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4213 {
4214 	struct i40e_q_vector *q_vector = data;
4215 	struct i40e_vsi *vsi;
4216 
4217 	if (!q_vector->tx.ring)
4218 		return IRQ_HANDLED;
4219 
4220 	vsi = q_vector->tx.ring->vsi;
4221 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4222 
4223 	return IRQ_HANDLED;
4224 }
4225 
4226 /**
4227  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4228  * @vsi: the VSI being configured
4229  * @v_idx: vector index
4230  * @qp_idx: queue pair index
4231  **/
4232 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4233 {
4234 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4235 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4236 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4237 
4238 	tx_ring->q_vector = q_vector;
4239 	tx_ring->next = q_vector->tx.ring;
4240 	q_vector->tx.ring = tx_ring;
4241 	q_vector->tx.count++;
4242 
4243 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4244 	if (i40e_enabled_xdp_vsi(vsi)) {
4245 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4246 
4247 		xdp_ring->q_vector = q_vector;
4248 		xdp_ring->next = q_vector->tx.ring;
4249 		q_vector->tx.ring = xdp_ring;
4250 		q_vector->tx.count++;
4251 	}
4252 
4253 	rx_ring->q_vector = q_vector;
4254 	rx_ring->next = q_vector->rx.ring;
4255 	q_vector->rx.ring = rx_ring;
4256 	q_vector->rx.count++;
4257 }
4258 
4259 /**
4260  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4261  * @vsi: the VSI being configured
4262  *
4263  * This function maps descriptor rings to the queue-specific vectors
4264  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4265  * one vector per queue pair, but on a constrained vector budget, we
4266  * group the queue pairs as "efficiently" as possible.
4267  **/
4268 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4269 {
4270 	int qp_remaining = vsi->num_queue_pairs;
4271 	int q_vectors = vsi->num_q_vectors;
4272 	int num_ringpairs;
4273 	int v_start = 0;
4274 	int qp_idx = 0;
4275 
4276 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4277 	 * group them so there are multiple queues per vector.
4278 	 * It is also important to go through all the vectors available to be
4279 	 * sure that if we don't use all the vectors, that the remaining vectors
4280 	 * are cleared. This is especially important when decreasing the
4281 	 * number of queues in use.
4282 	 */
4283 	for (; v_start < q_vectors; v_start++) {
4284 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4285 
4286 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4287 
4288 		q_vector->num_ringpairs = num_ringpairs;
4289 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4290 
4291 		q_vector->rx.count = 0;
4292 		q_vector->tx.count = 0;
4293 		q_vector->rx.ring = NULL;
4294 		q_vector->tx.ring = NULL;
4295 
4296 		while (num_ringpairs--) {
4297 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4298 			qp_idx++;
4299 			qp_remaining--;
4300 		}
4301 	}
4302 }
4303 
4304 /**
4305  * i40e_vsi_request_irq - Request IRQ from the OS
4306  * @vsi: the VSI being configured
4307  * @basename: name for the vector
4308  **/
4309 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4310 {
4311 	struct i40e_pf *pf = vsi->back;
4312 	int err;
4313 
4314 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4315 		err = i40e_vsi_request_irq_msix(vsi, basename);
4316 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4317 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4318 				  pf->int_name, pf);
4319 	else
4320 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4321 				  pf->int_name, pf);
4322 
4323 	if (err)
4324 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4325 
4326 	return err;
4327 }
4328 
4329 #ifdef CONFIG_NET_POLL_CONTROLLER
4330 /**
4331  * i40e_netpoll - A Polling 'interrupt' handler
4332  * @netdev: network interface device structure
4333  *
4334  * This is used by netconsole to send skbs without having to re-enable
4335  * interrupts.  It's not called while the normal interrupt routine is executing.
4336  **/
4337 static void i40e_netpoll(struct net_device *netdev)
4338 {
4339 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4340 	struct i40e_vsi *vsi = np->vsi;
4341 	struct i40e_pf *pf = vsi->back;
4342 	int i;
4343 
4344 	/* if interface is down do nothing */
4345 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4346 		return;
4347 
4348 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4349 		for (i = 0; i < vsi->num_q_vectors; i++)
4350 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4351 	} else {
4352 		i40e_intr(pf->pdev->irq, netdev);
4353 	}
4354 }
4355 #endif
4356 
4357 #define I40E_QTX_ENA_WAIT_COUNT 50
4358 
4359 /**
4360  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4361  * @pf: the PF being configured
4362  * @pf_q: the PF queue
4363  * @enable: enable or disable state of the queue
4364  *
4365  * This routine will wait for the given Tx queue of the PF to reach the
4366  * enabled or disabled state.
4367  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4368  * multiple retries; else will return 0 in case of success.
4369  **/
4370 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4371 {
4372 	int i;
4373 	u32 tx_reg;
4374 
4375 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4376 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4377 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4378 			break;
4379 
4380 		usleep_range(10, 20);
4381 	}
4382 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4383 		return -ETIMEDOUT;
4384 
4385 	return 0;
4386 }
4387 
4388 /**
4389  * i40e_control_tx_q - Start or stop a particular Tx queue
4390  * @pf: the PF structure
4391  * @pf_q: the PF queue to configure
4392  * @enable: start or stop the queue
4393  *
4394  * This function enables or disables a single queue. Note that any delay
4395  * required after the operation is expected to be handled by the caller of
4396  * this function.
4397  **/
4398 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4399 {
4400 	struct i40e_hw *hw = &pf->hw;
4401 	u32 tx_reg;
4402 	int i;
4403 
4404 	/* warn the TX unit of coming changes */
4405 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4406 	if (!enable)
4407 		usleep_range(10, 20);
4408 
4409 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4410 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4411 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4412 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4413 			break;
4414 		usleep_range(1000, 2000);
4415 	}
4416 
4417 	/* Skip if the queue is already in the requested state */
4418 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4419 		return;
4420 
4421 	/* turn on/off the queue */
4422 	if (enable) {
4423 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4424 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4425 	} else {
4426 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4427 	}
4428 
4429 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4430 }
4431 
4432 /**
4433  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4434  * @seid: VSI SEID
4435  * @pf: the PF structure
4436  * @pf_q: the PF queue to configure
4437  * @is_xdp: true if the queue is used for XDP
4438  * @enable: start or stop the queue
4439  **/
4440 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4441 			   bool is_xdp, bool enable)
4442 {
4443 	int ret;
4444 
4445 	i40e_control_tx_q(pf, pf_q, enable);
4446 
4447 	/* wait for the change to finish */
4448 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4449 	if (ret) {
4450 		dev_info(&pf->pdev->dev,
4451 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4452 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4453 			 (enable ? "en" : "dis"));
4454 	}
4455 
4456 	return ret;
4457 }
4458 
4459 /**
4460  * i40e_vsi_control_tx - Start or stop a VSI's rings
4461  * @vsi: the VSI being configured
4462  * @enable: start or stop the rings
4463  **/
4464 static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4465 {
4466 	struct i40e_pf *pf = vsi->back;
4467 	int i, pf_q, ret = 0;
4468 
4469 	pf_q = vsi->base_queue;
4470 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4471 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4472 					     pf_q,
4473 					     false /*is xdp*/, enable);
4474 		if (ret)
4475 			break;
4476 
4477 		if (!i40e_enabled_xdp_vsi(vsi))
4478 			continue;
4479 
4480 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4481 					     pf_q + vsi->alloc_queue_pairs,
4482 					     true /*is xdp*/, enable);
4483 		if (ret)
4484 			break;
4485 	}
4486 	return ret;
4487 }
4488 
4489 /**
4490  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4491  * @pf: the PF being configured
4492  * @pf_q: the PF queue
4493  * @enable: enable or disable state of the queue
4494  *
4495  * This routine will wait for the given Rx queue of the PF to reach the
4496  * enabled or disabled state.
4497  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4498  * multiple retries; else will return 0 in case of success.
4499  **/
4500 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4501 {
4502 	int i;
4503 	u32 rx_reg;
4504 
4505 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4506 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4507 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4508 			break;
4509 
4510 		usleep_range(10, 20);
4511 	}
4512 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4513 		return -ETIMEDOUT;
4514 
4515 	return 0;
4516 }
4517 
4518 /**
4519  * i40e_control_rx_q - Start or stop a particular Rx queue
4520  * @pf: the PF structure
4521  * @pf_q: the PF queue to configure
4522  * @enable: start or stop the queue
4523  *
4524  * This function enables or disables a single queue. Note that
4525  * any delay required after the operation is expected to be
4526  * handled by the caller of this function.
4527  **/
4528 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4529 {
4530 	struct i40e_hw *hw = &pf->hw;
4531 	u32 rx_reg;
4532 	int i;
4533 
4534 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4535 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4536 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4537 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4538 			break;
4539 		usleep_range(1000, 2000);
4540 	}
4541 
4542 	/* Skip if the queue is already in the requested state */
4543 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4544 		return;
4545 
4546 	/* turn on/off the queue */
4547 	if (enable)
4548 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4549 	else
4550 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4551 
4552 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4553 }
4554 
4555 /**
4556  * i40e_control_wait_rx_q
4557  * @pf: the PF structure
4558  * @pf_q: queue being configured
4559  * @enable: start or stop the rings
4560  *
4561  * This function enables or disables a single queue along with waiting
4562  * for the change to finish. The caller of this function should handle
4563  * the delays needed in the case of disabling queues.
4564  **/
4565 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4566 {
4567 	int ret = 0;
4568 
4569 	i40e_control_rx_q(pf, pf_q, enable);
4570 
4571 	/* wait for the change to finish */
4572 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4573 	if (ret)
4574 		return ret;
4575 
4576 	return ret;
4577 }
4578 
4579 /**
4580  * i40e_vsi_control_rx - Start or stop a VSI's rings
4581  * @vsi: the VSI being configured
4582  * @enable: start or stop the rings
4583  **/
4584 static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4585 {
4586 	struct i40e_pf *pf = vsi->back;
4587 	int i, pf_q, ret = 0;
4588 
4589 	pf_q = vsi->base_queue;
4590 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4591 		ret = i40e_control_wait_rx_q(pf, pf_q, enable);
4592 		if (ret) {
4593 			dev_info(&pf->pdev->dev,
4594 				 "VSI seid %d Rx ring %d %sable timeout\n",
4595 				 vsi->seid, pf_q, (enable ? "en" : "dis"));
4596 			break;
4597 		}
4598 	}
4599 
4600 	/* Due to HW errata, on Rx disable only, the register can indicate done
4601 	 * before it really is. Needs 50ms to be sure
4602 	 */
4603 	if (!enable)
4604 		mdelay(50);
4605 
4606 	return ret;
4607 }
4608 
4609 /**
4610  * i40e_vsi_start_rings - Start a VSI's rings
4611  * @vsi: the VSI being configured
4612  **/
4613 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4614 {
4615 	int ret = 0;
4616 
4617 	/* do rx first for enable and last for disable */
4618 	ret = i40e_vsi_control_rx(vsi, true);
4619 	if (ret)
4620 		return ret;
4621 	ret = i40e_vsi_control_tx(vsi, true);
4622 
4623 	return ret;
4624 }
4625 
4626 /**
4627  * i40e_vsi_stop_rings - Stop a VSI's rings
4628  * @vsi: the VSI being configured
4629  **/
4630 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4631 {
4632 	/* When port TX is suspended, don't wait */
4633 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4634 		return i40e_vsi_stop_rings_no_wait(vsi);
4635 
4636 	/* do rx first for enable and last for disable
4637 	 * Ignore return value, we need to shutdown whatever we can
4638 	 */
4639 	i40e_vsi_control_tx(vsi, false);
4640 	i40e_vsi_control_rx(vsi, false);
4641 }
4642 
4643 /**
4644  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4645  * @vsi: the VSI being shutdown
4646  *
4647  * This function stops all the rings for a VSI but does not delay to verify
4648  * that rings have been disabled. It is expected that the caller is shutting
4649  * down multiple VSIs at once and will delay together for all the VSIs after
4650  * initiating the shutdown. This is particularly useful for shutting down lots
4651  * of VFs together. Otherwise, a large delay can be incurred while configuring
4652  * each VSI in serial.
4653  **/
4654 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4655 {
4656 	struct i40e_pf *pf = vsi->back;
4657 	int i, pf_q;
4658 
4659 	pf_q = vsi->base_queue;
4660 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4661 		i40e_control_tx_q(pf, pf_q, false);
4662 		i40e_control_rx_q(pf, pf_q, false);
4663 	}
4664 }
4665 
4666 /**
4667  * i40e_vsi_free_irq - Free the irq association with the OS
4668  * @vsi: the VSI being configured
4669  **/
4670 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4671 {
4672 	struct i40e_pf *pf = vsi->back;
4673 	struct i40e_hw *hw = &pf->hw;
4674 	int base = vsi->base_vector;
4675 	u32 val, qp;
4676 	int i;
4677 
4678 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4679 		if (!vsi->q_vectors)
4680 			return;
4681 
4682 		if (!vsi->irqs_ready)
4683 			return;
4684 
4685 		vsi->irqs_ready = false;
4686 		for (i = 0; i < vsi->num_q_vectors; i++) {
4687 			int irq_num;
4688 			u16 vector;
4689 
4690 			vector = i + base;
4691 			irq_num = pf->msix_entries[vector].vector;
4692 
4693 			/* free only the irqs that were actually requested */
4694 			if (!vsi->q_vectors[i] ||
4695 			    !vsi->q_vectors[i]->num_ringpairs)
4696 				continue;
4697 
4698 			/* clear the affinity notifier in the IRQ descriptor */
4699 			irq_set_affinity_notifier(irq_num, NULL);
4700 			/* remove our suggested affinity mask for this IRQ */
4701 			irq_set_affinity_hint(irq_num, NULL);
4702 			synchronize_irq(irq_num);
4703 			free_irq(irq_num, vsi->q_vectors[i]);
4704 
4705 			/* Tear down the interrupt queue link list
4706 			 *
4707 			 * We know that they come in pairs and always
4708 			 * the Rx first, then the Tx.  To clear the
4709 			 * link list, stick the EOL value into the
4710 			 * next_q field of the registers.
4711 			 */
4712 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4713 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4714 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4715 			val |= I40E_QUEUE_END_OF_LIST
4716 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4717 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4718 
4719 			while (qp != I40E_QUEUE_END_OF_LIST) {
4720 				u32 next;
4721 
4722 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4723 
4724 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4725 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4726 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4727 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4728 
4729 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4730 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4731 
4732 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4733 
4734 				val = rd32(hw, I40E_QINT_TQCTL(qp));
4735 
4736 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4737 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4738 
4739 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4740 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4741 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4742 					 I40E_QINT_TQCTL_INTEVENT_MASK);
4743 
4744 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4745 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4746 
4747 				wr32(hw, I40E_QINT_TQCTL(qp), val);
4748 				qp = next;
4749 			}
4750 		}
4751 	} else {
4752 		free_irq(pf->pdev->irq, pf);
4753 
4754 		val = rd32(hw, I40E_PFINT_LNKLST0);
4755 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4756 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4757 		val |= I40E_QUEUE_END_OF_LIST
4758 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4759 		wr32(hw, I40E_PFINT_LNKLST0, val);
4760 
4761 		val = rd32(hw, I40E_QINT_RQCTL(qp));
4762 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4763 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4764 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4765 			 I40E_QINT_RQCTL_INTEVENT_MASK);
4766 
4767 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4768 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4769 
4770 		wr32(hw, I40E_QINT_RQCTL(qp), val);
4771 
4772 		val = rd32(hw, I40E_QINT_TQCTL(qp));
4773 
4774 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4775 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4776 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4777 			 I40E_QINT_TQCTL_INTEVENT_MASK);
4778 
4779 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4780 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4781 
4782 		wr32(hw, I40E_QINT_TQCTL(qp), val);
4783 	}
4784 }
4785 
4786 /**
4787  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4788  * @vsi: the VSI being configured
4789  * @v_idx: Index of vector to be freed
4790  *
4791  * This function frees the memory allocated to the q_vector.  In addition if
4792  * NAPI is enabled it will delete any references to the NAPI struct prior
4793  * to freeing the q_vector.
4794  **/
4795 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4796 {
4797 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4798 	struct i40e_ring *ring;
4799 
4800 	if (!q_vector)
4801 		return;
4802 
4803 	/* disassociate q_vector from rings */
4804 	i40e_for_each_ring(ring, q_vector->tx)
4805 		ring->q_vector = NULL;
4806 
4807 	i40e_for_each_ring(ring, q_vector->rx)
4808 		ring->q_vector = NULL;
4809 
4810 	/* only VSI w/ an associated netdev is set up w/ NAPI */
4811 	if (vsi->netdev)
4812 		netif_napi_del(&q_vector->napi);
4813 
4814 	vsi->q_vectors[v_idx] = NULL;
4815 
4816 	kfree_rcu(q_vector, rcu);
4817 }
4818 
4819 /**
4820  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4821  * @vsi: the VSI being un-configured
4822  *
4823  * This frees the memory allocated to the q_vectors and
4824  * deletes references to the NAPI struct.
4825  **/
4826 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4827 {
4828 	int v_idx;
4829 
4830 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4831 		i40e_free_q_vector(vsi, v_idx);
4832 }
4833 
4834 /**
4835  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4836  * @pf: board private structure
4837  **/
4838 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4839 {
4840 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4841 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4842 		pci_disable_msix(pf->pdev);
4843 		kfree(pf->msix_entries);
4844 		pf->msix_entries = NULL;
4845 		kfree(pf->irq_pile);
4846 		pf->irq_pile = NULL;
4847 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4848 		pci_disable_msi(pf->pdev);
4849 	}
4850 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4851 }
4852 
4853 /**
4854  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4855  * @pf: board private structure
4856  *
4857  * We go through and clear interrupt specific resources and reset the structure
4858  * to pre-load conditions
4859  **/
4860 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4861 {
4862 	int i;
4863 
4864 	i40e_free_misc_vector(pf);
4865 
4866 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4867 		      I40E_IWARP_IRQ_PILE_ID);
4868 
4869 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
4870 	for (i = 0; i < pf->num_alloc_vsi; i++)
4871 		if (pf->vsi[i])
4872 			i40e_vsi_free_q_vectors(pf->vsi[i]);
4873 	i40e_reset_interrupt_capability(pf);
4874 }
4875 
4876 /**
4877  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4878  * @vsi: the VSI being configured
4879  **/
4880 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4881 {
4882 	int q_idx;
4883 
4884 	if (!vsi->netdev)
4885 		return;
4886 
4887 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4888 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4889 
4890 		if (q_vector->rx.ring || q_vector->tx.ring)
4891 			napi_enable(&q_vector->napi);
4892 	}
4893 }
4894 
4895 /**
4896  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4897  * @vsi: the VSI being configured
4898  **/
4899 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4900 {
4901 	int q_idx;
4902 
4903 	if (!vsi->netdev)
4904 		return;
4905 
4906 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4907 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4908 
4909 		if (q_vector->rx.ring || q_vector->tx.ring)
4910 			napi_disable(&q_vector->napi);
4911 	}
4912 }
4913 
4914 /**
4915  * i40e_vsi_close - Shut down a VSI
4916  * @vsi: the vsi to be quelled
4917  **/
4918 static void i40e_vsi_close(struct i40e_vsi *vsi)
4919 {
4920 	struct i40e_pf *pf = vsi->back;
4921 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
4922 		i40e_down(vsi);
4923 	i40e_vsi_free_irq(vsi);
4924 	i40e_vsi_free_tx_resources(vsi);
4925 	i40e_vsi_free_rx_resources(vsi);
4926 	vsi->current_netdev_flags = 0;
4927 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
4928 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4929 		set_bit(__I40E_CLIENT_RESET, pf->state);
4930 }
4931 
4932 /**
4933  * i40e_quiesce_vsi - Pause a given VSI
4934  * @vsi: the VSI being paused
4935  **/
4936 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4937 {
4938 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4939 		return;
4940 
4941 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
4942 	if (vsi->netdev && netif_running(vsi->netdev))
4943 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
4944 	else
4945 		i40e_vsi_close(vsi);
4946 }
4947 
4948 /**
4949  * i40e_unquiesce_vsi - Resume a given VSI
4950  * @vsi: the VSI being resumed
4951  **/
4952 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4953 {
4954 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
4955 		return;
4956 
4957 	if (vsi->netdev && netif_running(vsi->netdev))
4958 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4959 	else
4960 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
4961 }
4962 
4963 /**
4964  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4965  * @pf: the PF
4966  **/
4967 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4968 {
4969 	int v;
4970 
4971 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4972 		if (pf->vsi[v])
4973 			i40e_quiesce_vsi(pf->vsi[v]);
4974 	}
4975 }
4976 
4977 /**
4978  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4979  * @pf: the PF
4980  **/
4981 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4982 {
4983 	int v;
4984 
4985 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4986 		if (pf->vsi[v])
4987 			i40e_unquiesce_vsi(pf->vsi[v]);
4988 	}
4989 }
4990 
4991 /**
4992  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
4993  * @vsi: the VSI being configured
4994  *
4995  * Wait until all queues on a given VSI have been disabled.
4996  **/
4997 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
4998 {
4999 	struct i40e_pf *pf = vsi->back;
5000 	int i, pf_q, ret;
5001 
5002 	pf_q = vsi->base_queue;
5003 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5004 		/* Check and wait for the Tx queue */
5005 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5006 		if (ret) {
5007 			dev_info(&pf->pdev->dev,
5008 				 "VSI seid %d Tx ring %d disable timeout\n",
5009 				 vsi->seid, pf_q);
5010 			return ret;
5011 		}
5012 
5013 		if (!i40e_enabled_xdp_vsi(vsi))
5014 			goto wait_rx;
5015 
5016 		/* Check and wait for the XDP Tx queue */
5017 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5018 				       false);
5019 		if (ret) {
5020 			dev_info(&pf->pdev->dev,
5021 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5022 				 vsi->seid, pf_q);
5023 			return ret;
5024 		}
5025 wait_rx:
5026 		/* Check and wait for the Rx queue */
5027 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5028 		if (ret) {
5029 			dev_info(&pf->pdev->dev,
5030 				 "VSI seid %d Rx ring %d disable timeout\n",
5031 				 vsi->seid, pf_q);
5032 			return ret;
5033 		}
5034 	}
5035 
5036 	return 0;
5037 }
5038 
5039 #ifdef CONFIG_I40E_DCB
5040 /**
5041  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5042  * @pf: the PF
5043  *
5044  * This function waits for the queues to be in disabled state for all the
5045  * VSIs that are managed by this PF.
5046  **/
5047 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5048 {
5049 	int v, ret = 0;
5050 
5051 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5052 		if (pf->vsi[v]) {
5053 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5054 			if (ret)
5055 				break;
5056 		}
5057 	}
5058 
5059 	return ret;
5060 }
5061 
5062 #endif
5063 
5064 /**
5065  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5066  * @pf: pointer to PF
5067  *
5068  * Get TC map for ISCSI PF type that will include iSCSI TC
5069  * and LAN TC.
5070  **/
5071 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5072 {
5073 	struct i40e_dcb_app_priority_table app;
5074 	struct i40e_hw *hw = &pf->hw;
5075 	u8 enabled_tc = 1; /* TC0 is always enabled */
5076 	u8 tc, i;
5077 	/* Get the iSCSI APP TLV */
5078 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5079 
5080 	for (i = 0; i < dcbcfg->numapps; i++) {
5081 		app = dcbcfg->app[i];
5082 		if (app.selector == I40E_APP_SEL_TCPIP &&
5083 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5084 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5085 			enabled_tc |= BIT(tc);
5086 			break;
5087 		}
5088 	}
5089 
5090 	return enabled_tc;
5091 }
5092 
5093 /**
5094  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5095  * @dcbcfg: the corresponding DCBx configuration structure
5096  *
5097  * Return the number of TCs from given DCBx configuration
5098  **/
5099 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5100 {
5101 	int i, tc_unused = 0;
5102 	u8 num_tc = 0;
5103 	u8 ret = 0;
5104 
5105 	/* Scan the ETS Config Priority Table to find
5106 	 * traffic class enabled for a given priority
5107 	 * and create a bitmask of enabled TCs
5108 	 */
5109 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5110 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5111 
5112 	/* Now scan the bitmask to check for
5113 	 * contiguous TCs starting with TC0
5114 	 */
5115 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5116 		if (num_tc & BIT(i)) {
5117 			if (!tc_unused) {
5118 				ret++;
5119 			} else {
5120 				pr_err("Non-contiguous TC - Disabling DCB\n");
5121 				return 1;
5122 			}
5123 		} else {
5124 			tc_unused = 1;
5125 		}
5126 	}
5127 
5128 	/* There is always at least TC0 */
5129 	if (!ret)
5130 		ret = 1;
5131 
5132 	return ret;
5133 }
5134 
5135 /**
5136  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5137  * @dcbcfg: the corresponding DCBx configuration structure
5138  *
5139  * Query the current DCB configuration and return the number of
5140  * traffic classes enabled from the given DCBX config
5141  **/
5142 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5143 {
5144 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5145 	u8 enabled_tc = 1;
5146 	u8 i;
5147 
5148 	for (i = 0; i < num_tc; i++)
5149 		enabled_tc |= BIT(i);
5150 
5151 	return enabled_tc;
5152 }
5153 
5154 /**
5155  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5156  * @pf: PF being queried
5157  *
5158  * Query the current MQPRIO configuration and return the number of
5159  * traffic classes enabled.
5160  **/
5161 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5162 {
5163 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5164 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5165 	u8 enabled_tc = 1, i;
5166 
5167 	for (i = 1; i < num_tc; i++)
5168 		enabled_tc |= BIT(i);
5169 	return enabled_tc;
5170 }
5171 
5172 /**
5173  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5174  * @pf: PF being queried
5175  *
5176  * Return number of traffic classes enabled for the given PF
5177  **/
5178 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5179 {
5180 	struct i40e_hw *hw = &pf->hw;
5181 	u8 i, enabled_tc = 1;
5182 	u8 num_tc = 0;
5183 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5184 
5185 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5186 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5187 
5188 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5189 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5190 		return 1;
5191 
5192 	/* SFP mode will be enabled for all TCs on port */
5193 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5194 		return i40e_dcb_get_num_tc(dcbcfg);
5195 
5196 	/* MFP mode return count of enabled TCs for this PF */
5197 	if (pf->hw.func_caps.iscsi)
5198 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5199 	else
5200 		return 1; /* Only TC0 */
5201 
5202 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5203 		if (enabled_tc & BIT(i))
5204 			num_tc++;
5205 	}
5206 	return num_tc;
5207 }
5208 
5209 /**
5210  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5211  * @pf: PF being queried
5212  *
5213  * Return a bitmap for enabled traffic classes for this PF.
5214  **/
5215 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5216 {
5217 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5218 		return i40e_mqprio_get_enabled_tc(pf);
5219 
5220 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5221 	 * default TC
5222 	 */
5223 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5224 		return I40E_DEFAULT_TRAFFIC_CLASS;
5225 
5226 	/* SFP mode we want PF to be enabled for all TCs */
5227 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5228 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5229 
5230 	/* MFP enabled and iSCSI PF type */
5231 	if (pf->hw.func_caps.iscsi)
5232 		return i40e_get_iscsi_tc_map(pf);
5233 	else
5234 		return I40E_DEFAULT_TRAFFIC_CLASS;
5235 }
5236 
5237 /**
5238  * i40e_vsi_get_bw_info - Query VSI BW Information
5239  * @vsi: the VSI being queried
5240  *
5241  * Returns 0 on success, negative value on failure
5242  **/
5243 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5244 {
5245 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5246 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5247 	struct i40e_pf *pf = vsi->back;
5248 	struct i40e_hw *hw = &pf->hw;
5249 	i40e_status ret;
5250 	u32 tc_bw_max;
5251 	int i;
5252 
5253 	/* Get the VSI level BW configuration */
5254 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5255 	if (ret) {
5256 		dev_info(&pf->pdev->dev,
5257 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5258 			 i40e_stat_str(&pf->hw, ret),
5259 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5260 		return -EINVAL;
5261 	}
5262 
5263 	/* Get the VSI level BW configuration per TC */
5264 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5265 					       NULL);
5266 	if (ret) {
5267 		dev_info(&pf->pdev->dev,
5268 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5269 			 i40e_stat_str(&pf->hw, ret),
5270 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5271 		return -EINVAL;
5272 	}
5273 
5274 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5275 		dev_info(&pf->pdev->dev,
5276 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5277 			 bw_config.tc_valid_bits,
5278 			 bw_ets_config.tc_valid_bits);
5279 		/* Still continuing */
5280 	}
5281 
5282 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5283 	vsi->bw_max_quanta = bw_config.max_bw;
5284 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5285 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5286 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5287 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5288 		vsi->bw_ets_limit_credits[i] =
5289 					le16_to_cpu(bw_ets_config.credits[i]);
5290 		/* 3 bits out of 4 for each TC */
5291 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5292 	}
5293 
5294 	return 0;
5295 }
5296 
5297 /**
5298  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5299  * @vsi: the VSI being configured
5300  * @enabled_tc: TC bitmap
5301  * @bw_share: BW shared credits per TC
5302  *
5303  * Returns 0 on success, negative value on failure
5304  **/
5305 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5306 				       u8 *bw_share)
5307 {
5308 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5309 	struct i40e_pf *pf = vsi->back;
5310 	i40e_status ret;
5311 	int i;
5312 
5313 	/* There is no need to reset BW when mqprio mode is on.  */
5314 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5315 		return 0;
5316 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5317 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5318 		if (ret)
5319 			dev_info(&pf->pdev->dev,
5320 				 "Failed to reset tx rate for vsi->seid %u\n",
5321 				 vsi->seid);
5322 		return ret;
5323 	}
5324 	memset(&bw_data, 0, sizeof(bw_data));
5325 	bw_data.tc_valid_bits = enabled_tc;
5326 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5327 		bw_data.tc_bw_credits[i] = bw_share[i];
5328 
5329 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5330 	if (ret) {
5331 		dev_info(&pf->pdev->dev,
5332 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5333 			 pf->hw.aq.asq_last_status);
5334 		return -EINVAL;
5335 	}
5336 
5337 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5338 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5339 
5340 	return 0;
5341 }
5342 
5343 /**
5344  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5345  * @vsi: the VSI being configured
5346  * @enabled_tc: TC map to be enabled
5347  *
5348  **/
5349 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5350 {
5351 	struct net_device *netdev = vsi->netdev;
5352 	struct i40e_pf *pf = vsi->back;
5353 	struct i40e_hw *hw = &pf->hw;
5354 	u8 netdev_tc = 0;
5355 	int i;
5356 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5357 
5358 	if (!netdev)
5359 		return;
5360 
5361 	if (!enabled_tc) {
5362 		netdev_reset_tc(netdev);
5363 		return;
5364 	}
5365 
5366 	/* Set up actual enabled TCs on the VSI */
5367 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5368 		return;
5369 
5370 	/* set per TC queues for the VSI */
5371 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5372 		/* Only set TC queues for enabled tcs
5373 		 *
5374 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5375 		 * enabled_tc bitmap would be 0x00001001; the driver
5376 		 * will set the numtc for netdev as 2 that will be
5377 		 * referenced by the netdev layer as TC 0 and 1.
5378 		 */
5379 		if (vsi->tc_config.enabled_tc & BIT(i))
5380 			netdev_set_tc_queue(netdev,
5381 					vsi->tc_config.tc_info[i].netdev_tc,
5382 					vsi->tc_config.tc_info[i].qcount,
5383 					vsi->tc_config.tc_info[i].qoffset);
5384 	}
5385 
5386 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5387 		return;
5388 
5389 	/* Assign UP2TC map for the VSI */
5390 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5391 		/* Get the actual TC# for the UP */
5392 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5393 		/* Get the mapped netdev TC# for the UP */
5394 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5395 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5396 	}
5397 }
5398 
5399 /**
5400  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5401  * @vsi: the VSI being configured
5402  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5403  **/
5404 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5405 				      struct i40e_vsi_context *ctxt)
5406 {
5407 	/* copy just the sections touched not the entire info
5408 	 * since not all sections are valid as returned by
5409 	 * update vsi params
5410 	 */
5411 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5412 	memcpy(&vsi->info.queue_mapping,
5413 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5414 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5415 	       sizeof(vsi->info.tc_mapping));
5416 }
5417 
5418 /**
5419  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5420  * @vsi: VSI to be configured
5421  * @enabled_tc: TC bitmap
5422  *
5423  * This configures a particular VSI for TCs that are mapped to the
5424  * given TC bitmap. It uses default bandwidth share for TCs across
5425  * VSIs to configure TC for a particular VSI.
5426  *
5427  * NOTE:
5428  * It is expected that the VSI queues have been quisced before calling
5429  * this function.
5430  **/
5431 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5432 {
5433 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5434 	struct i40e_pf *pf = vsi->back;
5435 	struct i40e_hw *hw = &pf->hw;
5436 	struct i40e_vsi_context ctxt;
5437 	int ret = 0;
5438 	int i;
5439 
5440 	/* Check if enabled_tc is same as existing or new TCs */
5441 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5442 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5443 		return ret;
5444 
5445 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5446 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5447 		if (enabled_tc & BIT(i))
5448 			bw_share[i] = 1;
5449 	}
5450 
5451 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5452 	if (ret) {
5453 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5454 
5455 		dev_info(&pf->pdev->dev,
5456 			 "Failed configuring TC map %d for VSI %d\n",
5457 			 enabled_tc, vsi->seid);
5458 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5459 						  &bw_config, NULL);
5460 		if (ret) {
5461 			dev_info(&pf->pdev->dev,
5462 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5463 				 i40e_stat_str(hw, ret),
5464 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5465 			goto out;
5466 		}
5467 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5468 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5469 
5470 			if (!valid_tc)
5471 				valid_tc = bw_config.tc_valid_bits;
5472 			/* Always enable TC0, no matter what */
5473 			valid_tc |= 1;
5474 			dev_info(&pf->pdev->dev,
5475 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5476 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5477 			enabled_tc = valid_tc;
5478 		}
5479 
5480 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5481 		if (ret) {
5482 			dev_err(&pf->pdev->dev,
5483 				"Unable to  configure TC map %d for VSI %d\n",
5484 				enabled_tc, vsi->seid);
5485 			goto out;
5486 		}
5487 	}
5488 
5489 	/* Update Queue Pairs Mapping for currently enabled UPs */
5490 	ctxt.seid = vsi->seid;
5491 	ctxt.pf_num = vsi->back->hw.pf_id;
5492 	ctxt.vf_num = 0;
5493 	ctxt.uplink_seid = vsi->uplink_seid;
5494 	ctxt.info = vsi->info;
5495 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5496 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5497 		if (ret)
5498 			goto out;
5499 	} else {
5500 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5501 	}
5502 
5503 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5504 	 * queues changed.
5505 	 */
5506 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5507 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5508 				      vsi->num_queue_pairs);
5509 		ret = i40e_vsi_config_rss(vsi);
5510 		if (ret) {
5511 			dev_info(&vsi->back->pdev->dev,
5512 				 "Failed to reconfig rss for num_queues\n");
5513 			return ret;
5514 		}
5515 		vsi->reconfig_rss = false;
5516 	}
5517 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5518 		ctxt.info.valid_sections |=
5519 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5520 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5521 	}
5522 
5523 	/* Update the VSI after updating the VSI queue-mapping
5524 	 * information
5525 	 */
5526 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5527 	if (ret) {
5528 		dev_info(&pf->pdev->dev,
5529 			 "Update vsi tc config failed, err %s aq_err %s\n",
5530 			 i40e_stat_str(hw, ret),
5531 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5532 		goto out;
5533 	}
5534 	/* update the local VSI info with updated queue map */
5535 	i40e_vsi_update_queue_map(vsi, &ctxt);
5536 	vsi->info.valid_sections = 0;
5537 
5538 	/* Update current VSI BW information */
5539 	ret = i40e_vsi_get_bw_info(vsi);
5540 	if (ret) {
5541 		dev_info(&pf->pdev->dev,
5542 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5543 			 i40e_stat_str(hw, ret),
5544 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5545 		goto out;
5546 	}
5547 
5548 	/* Update the netdev TC setup */
5549 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5550 out:
5551 	return ret;
5552 }
5553 
5554 /**
5555  * i40e_get_link_speed - Returns link speed for the interface
5556  * @vsi: VSI to be configured
5557  *
5558  **/
5559 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5560 {
5561 	struct i40e_pf *pf = vsi->back;
5562 
5563 	switch (pf->hw.phy.link_info.link_speed) {
5564 	case I40E_LINK_SPEED_40GB:
5565 		return 40000;
5566 	case I40E_LINK_SPEED_25GB:
5567 		return 25000;
5568 	case I40E_LINK_SPEED_20GB:
5569 		return 20000;
5570 	case I40E_LINK_SPEED_10GB:
5571 		return 10000;
5572 	case I40E_LINK_SPEED_1GB:
5573 		return 1000;
5574 	default:
5575 		return -EINVAL;
5576 	}
5577 }
5578 
5579 /**
5580  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5581  * @vsi: VSI to be configured
5582  * @seid: seid of the channel/VSI
5583  * @max_tx_rate: max TX rate to be configured as BW limit
5584  *
5585  * Helper function to set BW limit for a given VSI
5586  **/
5587 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5588 {
5589 	struct i40e_pf *pf = vsi->back;
5590 	u64 credits = 0;
5591 	int speed = 0;
5592 	int ret = 0;
5593 
5594 	speed = i40e_get_link_speed(vsi);
5595 	if (max_tx_rate > speed) {
5596 		dev_err(&pf->pdev->dev,
5597 			"Invalid max tx rate %llu specified for VSI seid %d.",
5598 			max_tx_rate, seid);
5599 		return -EINVAL;
5600 	}
5601 	if (max_tx_rate && max_tx_rate < 50) {
5602 		dev_warn(&pf->pdev->dev,
5603 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5604 		max_tx_rate = 50;
5605 	}
5606 
5607 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5608 	credits = max_tx_rate;
5609 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5610 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5611 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5612 	if (ret)
5613 		dev_err(&pf->pdev->dev,
5614 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5615 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5616 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5617 	return ret;
5618 }
5619 
5620 /**
5621  * i40e_remove_queue_channels - Remove queue channels for the TCs
5622  * @vsi: VSI to be configured
5623  *
5624  * Remove queue channels for the TCs
5625  **/
5626 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5627 {
5628 	enum i40e_admin_queue_err last_aq_status;
5629 	struct i40e_cloud_filter *cfilter;
5630 	struct i40e_channel *ch, *ch_tmp;
5631 	struct i40e_pf *pf = vsi->back;
5632 	struct hlist_node *node;
5633 	int ret, i;
5634 
5635 	/* Reset rss size that was stored when reconfiguring rss for
5636 	 * channel VSIs with non-power-of-2 queue count.
5637 	 */
5638 	vsi->current_rss_size = 0;
5639 
5640 	/* perform cleanup for channels if they exist */
5641 	if (list_empty(&vsi->ch_list))
5642 		return;
5643 
5644 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5645 		struct i40e_vsi *p_vsi;
5646 
5647 		list_del(&ch->list);
5648 		p_vsi = ch->parent_vsi;
5649 		if (!p_vsi || !ch->initialized) {
5650 			kfree(ch);
5651 			continue;
5652 		}
5653 		/* Reset queue contexts */
5654 		for (i = 0; i < ch->num_queue_pairs; i++) {
5655 			struct i40e_ring *tx_ring, *rx_ring;
5656 			u16 pf_q;
5657 
5658 			pf_q = ch->base_queue + i;
5659 			tx_ring = vsi->tx_rings[pf_q];
5660 			tx_ring->ch = NULL;
5661 
5662 			rx_ring = vsi->rx_rings[pf_q];
5663 			rx_ring->ch = NULL;
5664 		}
5665 
5666 		/* Reset BW configured for this VSI via mqprio */
5667 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5668 		if (ret)
5669 			dev_info(&vsi->back->pdev->dev,
5670 				 "Failed to reset tx rate for ch->seid %u\n",
5671 				 ch->seid);
5672 
5673 		/* delete cloud filters associated with this channel */
5674 		hlist_for_each_entry_safe(cfilter, node,
5675 					  &pf->cloud_filter_list, cloud_node) {
5676 			if (cfilter->seid != ch->seid)
5677 				continue;
5678 
5679 			hash_del(&cfilter->cloud_node);
5680 			if (cfilter->dst_port)
5681 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
5682 									cfilter,
5683 									false);
5684 			else
5685 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
5686 								false);
5687 			last_aq_status = pf->hw.aq.asq_last_status;
5688 			if (ret)
5689 				dev_info(&pf->pdev->dev,
5690 					 "Failed to delete cloud filter, err %s aq_err %s\n",
5691 					 i40e_stat_str(&pf->hw, ret),
5692 					 i40e_aq_str(&pf->hw, last_aq_status));
5693 			kfree(cfilter);
5694 		}
5695 
5696 		/* delete VSI from FW */
5697 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5698 					     NULL);
5699 		if (ret)
5700 			dev_err(&vsi->back->pdev->dev,
5701 				"unable to remove channel (%d) for parent VSI(%d)\n",
5702 				ch->seid, p_vsi->seid);
5703 		kfree(ch);
5704 	}
5705 	INIT_LIST_HEAD(&vsi->ch_list);
5706 }
5707 
5708 /**
5709  * i40e_is_any_channel - channel exist or not
5710  * @vsi: ptr to VSI to which channels are associated with
5711  *
5712  * Returns true or false if channel(s) exist for associated VSI or not
5713  **/
5714 static bool i40e_is_any_channel(struct i40e_vsi *vsi)
5715 {
5716 	struct i40e_channel *ch, *ch_tmp;
5717 
5718 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5719 		if (ch->initialized)
5720 			return true;
5721 	}
5722 
5723 	return false;
5724 }
5725 
5726 /**
5727  * i40e_get_max_queues_for_channel
5728  * @vsi: ptr to VSI to which channels are associated with
5729  *
5730  * Helper function which returns max value among the queue counts set on the
5731  * channels/TCs created.
5732  **/
5733 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5734 {
5735 	struct i40e_channel *ch, *ch_tmp;
5736 	int max = 0;
5737 
5738 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5739 		if (!ch->initialized)
5740 			continue;
5741 		if (ch->num_queue_pairs > max)
5742 			max = ch->num_queue_pairs;
5743 	}
5744 
5745 	return max;
5746 }
5747 
5748 /**
5749  * i40e_validate_num_queues - validate num_queues w.r.t channel
5750  * @pf: ptr to PF device
5751  * @num_queues: number of queues
5752  * @vsi: the parent VSI
5753  * @reconfig_rss: indicates should the RSS be reconfigured or not
5754  *
5755  * This function validates number of queues in the context of new channel
5756  * which is being established and determines if RSS should be reconfigured
5757  * or not for parent VSI.
5758  **/
5759 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5760 				    struct i40e_vsi *vsi, bool *reconfig_rss)
5761 {
5762 	int max_ch_queues;
5763 
5764 	if (!reconfig_rss)
5765 		return -EINVAL;
5766 
5767 	*reconfig_rss = false;
5768 	if (vsi->current_rss_size) {
5769 		if (num_queues > vsi->current_rss_size) {
5770 			dev_dbg(&pf->pdev->dev,
5771 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
5772 				num_queues, vsi->current_rss_size);
5773 			return -EINVAL;
5774 		} else if ((num_queues < vsi->current_rss_size) &&
5775 			   (!is_power_of_2(num_queues))) {
5776 			dev_dbg(&pf->pdev->dev,
5777 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5778 				num_queues, vsi->current_rss_size);
5779 			return -EINVAL;
5780 		}
5781 	}
5782 
5783 	if (!is_power_of_2(num_queues)) {
5784 		/* Find the max num_queues configured for channel if channel
5785 		 * exist.
5786 		 * if channel exist, then enforce 'num_queues' to be more than
5787 		 * max ever queues configured for channel.
5788 		 */
5789 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5790 		if (num_queues < max_ch_queues) {
5791 			dev_dbg(&pf->pdev->dev,
5792 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
5793 				num_queues, max_ch_queues);
5794 			return -EINVAL;
5795 		}
5796 		*reconfig_rss = true;
5797 	}
5798 
5799 	return 0;
5800 }
5801 
5802 /**
5803  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5804  * @vsi: the VSI being setup
5805  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5806  *
5807  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5808  **/
5809 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5810 {
5811 	struct i40e_pf *pf = vsi->back;
5812 	u8 seed[I40E_HKEY_ARRAY_SIZE];
5813 	struct i40e_hw *hw = &pf->hw;
5814 	int local_rss_size;
5815 	u8 *lut;
5816 	int ret;
5817 
5818 	if (!vsi->rss_size)
5819 		return -EINVAL;
5820 
5821 	if (rss_size > vsi->rss_size)
5822 		return -EINVAL;
5823 
5824 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
5825 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5826 	if (!lut)
5827 		return -ENOMEM;
5828 
5829 	/* Ignoring user configured lut if there is one */
5830 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5831 
5832 	/* Use user configured hash key if there is one, otherwise
5833 	 * use default.
5834 	 */
5835 	if (vsi->rss_hkey_user)
5836 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5837 	else
5838 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5839 
5840 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5841 	if (ret) {
5842 		dev_info(&pf->pdev->dev,
5843 			 "Cannot set RSS lut, err %s aq_err %s\n",
5844 			 i40e_stat_str(hw, ret),
5845 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5846 		kfree(lut);
5847 		return ret;
5848 	}
5849 	kfree(lut);
5850 
5851 	/* Do the update w.r.t. storing rss_size */
5852 	if (!vsi->orig_rss_size)
5853 		vsi->orig_rss_size = vsi->rss_size;
5854 	vsi->current_rss_size = local_rss_size;
5855 
5856 	return ret;
5857 }
5858 
5859 /**
5860  * i40e_channel_setup_queue_map - Setup a channel queue map
5861  * @pf: ptr to PF device
5862  * @ctxt: VSI context structure
5863  * @ch: ptr to channel structure
5864  *
5865  * Setup queue map for a specific channel
5866  **/
5867 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5868 					 struct i40e_vsi_context *ctxt,
5869 					 struct i40e_channel *ch)
5870 {
5871 	u16 qcount, qmap, sections = 0;
5872 	u8 offset = 0;
5873 	int pow;
5874 
5875 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5876 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5877 
5878 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5879 	ch->num_queue_pairs = qcount;
5880 
5881 	/* find the next higher power-of-2 of num queue pairs */
5882 	pow = ilog2(qcount);
5883 	if (!is_power_of_2(qcount))
5884 		pow++;
5885 
5886 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5887 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
5888 
5889 	/* Setup queue TC[0].qmap for given VSI context */
5890 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
5891 
5892 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
5893 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
5894 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
5895 	ctxt->info.valid_sections |= cpu_to_le16(sections);
5896 }
5897 
5898 /**
5899  * i40e_add_channel - add a channel by adding VSI
5900  * @pf: ptr to PF device
5901  * @uplink_seid: underlying HW switching element (VEB) ID
5902  * @ch: ptr to channel structure
5903  *
5904  * Add a channel (VSI) using add_vsi and queue_map
5905  **/
5906 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
5907 			    struct i40e_channel *ch)
5908 {
5909 	struct i40e_hw *hw = &pf->hw;
5910 	struct i40e_vsi_context ctxt;
5911 	u8 enabled_tc = 0x1; /* TC0 enabled */
5912 	int ret;
5913 
5914 	if (ch->type != I40E_VSI_VMDQ2) {
5915 		dev_info(&pf->pdev->dev,
5916 			 "add new vsi failed, ch->type %d\n", ch->type);
5917 		return -EINVAL;
5918 	}
5919 
5920 	memset(&ctxt, 0, sizeof(ctxt));
5921 	ctxt.pf_num = hw->pf_id;
5922 	ctxt.vf_num = 0;
5923 	ctxt.uplink_seid = uplink_seid;
5924 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5925 	if (ch->type == I40E_VSI_VMDQ2)
5926 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
5927 
5928 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
5929 		ctxt.info.valid_sections |=
5930 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5931 		ctxt.info.switch_id =
5932 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5933 	}
5934 
5935 	/* Set queue map for a given VSI context */
5936 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
5937 
5938 	/* Now time to create VSI */
5939 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
5940 	if (ret) {
5941 		dev_info(&pf->pdev->dev,
5942 			 "add new vsi failed, err %s aq_err %s\n",
5943 			 i40e_stat_str(&pf->hw, ret),
5944 			 i40e_aq_str(&pf->hw,
5945 				     pf->hw.aq.asq_last_status));
5946 		return -ENOENT;
5947 	}
5948 
5949 	/* Success, update channel, set enabled_tc only if the channel
5950 	 * is not a macvlan
5951 	 */
5952 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
5953 	ch->seid = ctxt.seid;
5954 	ch->vsi_number = ctxt.vsi_number;
5955 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
5956 
5957 	/* copy just the sections touched not the entire info
5958 	 * since not all sections are valid as returned by
5959 	 * update vsi params
5960 	 */
5961 	ch->info.mapping_flags = ctxt.info.mapping_flags;
5962 	memcpy(&ch->info.queue_mapping,
5963 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
5964 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
5965 	       sizeof(ctxt.info.tc_mapping));
5966 
5967 	return 0;
5968 }
5969 
5970 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
5971 				  u8 *bw_share)
5972 {
5973 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5974 	i40e_status ret;
5975 	int i;
5976 
5977 	memset(&bw_data, 0, sizeof(bw_data));
5978 	bw_data.tc_valid_bits = ch->enabled_tc;
5979 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5980 		bw_data.tc_bw_credits[i] = bw_share[i];
5981 
5982 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
5983 				       &bw_data, NULL);
5984 	if (ret) {
5985 		dev_info(&vsi->back->pdev->dev,
5986 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
5987 			 vsi->back->hw.aq.asq_last_status, ch->seid);
5988 		return -EINVAL;
5989 	}
5990 
5991 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5992 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
5993 
5994 	return 0;
5995 }
5996 
5997 /**
5998  * i40e_channel_config_tx_ring - config TX ring associated with new channel
5999  * @pf: ptr to PF device
6000  * @vsi: the VSI being setup
6001  * @ch: ptr to channel structure
6002  *
6003  * Configure TX rings associated with channel (VSI) since queues are being
6004  * from parent VSI.
6005  **/
6006 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6007 				       struct i40e_vsi *vsi,
6008 				       struct i40e_channel *ch)
6009 {
6010 	i40e_status ret;
6011 	int i;
6012 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6013 
6014 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6015 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6016 		if (ch->enabled_tc & BIT(i))
6017 			bw_share[i] = 1;
6018 	}
6019 
6020 	/* configure BW for new VSI */
6021 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6022 	if (ret) {
6023 		dev_info(&vsi->back->pdev->dev,
6024 			 "Failed configuring TC map %d for channel (seid %u)\n",
6025 			 ch->enabled_tc, ch->seid);
6026 		return ret;
6027 	}
6028 
6029 	for (i = 0; i < ch->num_queue_pairs; i++) {
6030 		struct i40e_ring *tx_ring, *rx_ring;
6031 		u16 pf_q;
6032 
6033 		pf_q = ch->base_queue + i;
6034 
6035 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6036 		 * context
6037 		 */
6038 		tx_ring = vsi->tx_rings[pf_q];
6039 		tx_ring->ch = ch;
6040 
6041 		/* Get the RX ring ptr */
6042 		rx_ring = vsi->rx_rings[pf_q];
6043 		rx_ring->ch = ch;
6044 	}
6045 
6046 	return 0;
6047 }
6048 
6049 /**
6050  * i40e_setup_hw_channel - setup new channel
6051  * @pf: ptr to PF device
6052  * @vsi: the VSI being setup
6053  * @ch: ptr to channel structure
6054  * @uplink_seid: underlying HW switching element (VEB) ID
6055  * @type: type of channel to be created (VMDq2/VF)
6056  *
6057  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6058  * and configures TX rings accordingly
6059  **/
6060 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6061 					struct i40e_vsi *vsi,
6062 					struct i40e_channel *ch,
6063 					u16 uplink_seid, u8 type)
6064 {
6065 	int ret;
6066 
6067 	ch->initialized = false;
6068 	ch->base_queue = vsi->next_base_queue;
6069 	ch->type = type;
6070 
6071 	/* Proceed with creation of channel (VMDq2) VSI */
6072 	ret = i40e_add_channel(pf, uplink_seid, ch);
6073 	if (ret) {
6074 		dev_info(&pf->pdev->dev,
6075 			 "failed to add_channel using uplink_seid %u\n",
6076 			 uplink_seid);
6077 		return ret;
6078 	}
6079 
6080 	/* Mark the successful creation of channel */
6081 	ch->initialized = true;
6082 
6083 	/* Reconfigure TX queues using QTX_CTL register */
6084 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6085 	if (ret) {
6086 		dev_info(&pf->pdev->dev,
6087 			 "failed to configure TX rings for channel %u\n",
6088 			 ch->seid);
6089 		return ret;
6090 	}
6091 
6092 	/* update 'next_base_queue' */
6093 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6094 	dev_dbg(&pf->pdev->dev,
6095 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6096 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6097 		ch->num_queue_pairs,
6098 		vsi->next_base_queue);
6099 	return ret;
6100 }
6101 
6102 /**
6103  * i40e_setup_channel - setup new channel using uplink element
6104  * @pf: ptr to PF device
6105  * @vsi: pointer to the VSI to set up the channel within
6106  * @ch: ptr to channel structure
6107  *
6108  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6109  * and uplink switching element (uplink_seid)
6110  **/
6111 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6112 			       struct i40e_channel *ch)
6113 {
6114 	u8 vsi_type;
6115 	u16 seid;
6116 	int ret;
6117 
6118 	if (vsi->type == I40E_VSI_MAIN) {
6119 		vsi_type = I40E_VSI_VMDQ2;
6120 	} else {
6121 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6122 			vsi->type);
6123 		return false;
6124 	}
6125 
6126 	/* underlying switching element */
6127 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6128 
6129 	/* create channel (VSI), configure TX rings */
6130 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6131 	if (ret) {
6132 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6133 		return false;
6134 	}
6135 
6136 	return ch->initialized ? true : false;
6137 }
6138 
6139 /**
6140  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6141  * @vsi: ptr to VSI which has PF backing
6142  *
6143  * Sets up switch mode correctly if it needs to be changed and perform
6144  * what are allowed modes.
6145  **/
6146 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6147 {
6148 	u8 mode;
6149 	struct i40e_pf *pf = vsi->back;
6150 	struct i40e_hw *hw = &pf->hw;
6151 	int ret;
6152 
6153 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6154 	if (ret)
6155 		return -EINVAL;
6156 
6157 	if (hw->dev_caps.switch_mode) {
6158 		/* if switch mode is set, support mode2 (non-tunneled for
6159 		 * cloud filter) for now
6160 		 */
6161 		u32 switch_mode = hw->dev_caps.switch_mode &
6162 				  I40E_SWITCH_MODE_MASK;
6163 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6164 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6165 				return 0;
6166 			dev_err(&pf->pdev->dev,
6167 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6168 				hw->dev_caps.switch_mode);
6169 			return -EINVAL;
6170 		}
6171 	}
6172 
6173 	/* Set Bit 7 to be valid */
6174 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6175 
6176 	/* Set L4type for TCP support */
6177 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6178 
6179 	/* Set cloud filter mode */
6180 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6181 
6182 	/* Prep mode field for set_switch_config */
6183 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6184 					pf->last_sw_conf_valid_flags,
6185 					mode, NULL);
6186 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6187 		dev_err(&pf->pdev->dev,
6188 			"couldn't set switch config bits, err %s aq_err %s\n",
6189 			i40e_stat_str(hw, ret),
6190 			i40e_aq_str(hw,
6191 				    hw->aq.asq_last_status));
6192 
6193 	return ret;
6194 }
6195 
6196 /**
6197  * i40e_create_queue_channel - function to create channel
6198  * @vsi: VSI to be configured
6199  * @ch: ptr to channel (it contains channel specific params)
6200  *
6201  * This function creates channel (VSI) using num_queues specified by user,
6202  * reconfigs RSS if needed.
6203  **/
6204 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6205 			      struct i40e_channel *ch)
6206 {
6207 	struct i40e_pf *pf = vsi->back;
6208 	bool reconfig_rss;
6209 	int err;
6210 
6211 	if (!ch)
6212 		return -EINVAL;
6213 
6214 	if (!ch->num_queue_pairs) {
6215 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6216 			ch->num_queue_pairs);
6217 		return -EINVAL;
6218 	}
6219 
6220 	/* validate user requested num_queues for channel */
6221 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6222 				       &reconfig_rss);
6223 	if (err) {
6224 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6225 			 ch->num_queue_pairs);
6226 		return -EINVAL;
6227 	}
6228 
6229 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6230 	 * VSI to be added switch to VEB mode.
6231 	 */
6232 	if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
6233 	    (!i40e_is_any_channel(vsi))) {
6234 		if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
6235 			dev_dbg(&pf->pdev->dev,
6236 				"Failed to create channel. Override queues (%u) not power of 2\n",
6237 				vsi->tc_config.tc_info[0].qcount);
6238 			return -EINVAL;
6239 		}
6240 
6241 		if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6242 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6243 
6244 			if (vsi->type == I40E_VSI_MAIN) {
6245 				if (pf->flags & I40E_FLAG_TC_MQPRIO)
6246 					i40e_do_reset(pf, I40E_PF_RESET_FLAG,
6247 						      true);
6248 				else
6249 					i40e_do_reset_safe(pf,
6250 							   I40E_PF_RESET_FLAG);
6251 			}
6252 		}
6253 		/* now onwards for main VSI, number of queues will be value
6254 		 * of TC0's queue count
6255 		 */
6256 	}
6257 
6258 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6259 	 * it should be more than num_queues
6260 	 */
6261 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6262 		dev_dbg(&pf->pdev->dev,
6263 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6264 			vsi->cnt_q_avail, ch->num_queue_pairs);
6265 		return -EINVAL;
6266 	}
6267 
6268 	/* reconfig_rss only if vsi type is MAIN_VSI */
6269 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6270 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6271 		if (err) {
6272 			dev_info(&pf->pdev->dev,
6273 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6274 				 ch->num_queue_pairs);
6275 			return -EINVAL;
6276 		}
6277 	}
6278 
6279 	if (!i40e_setup_channel(pf, vsi, ch)) {
6280 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6281 		return -EINVAL;
6282 	}
6283 
6284 	dev_info(&pf->pdev->dev,
6285 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6286 		 ch->seid, ch->num_queue_pairs);
6287 
6288 	/* configure VSI for BW limit */
6289 	if (ch->max_tx_rate) {
6290 		u64 credits = ch->max_tx_rate;
6291 
6292 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6293 			return -EINVAL;
6294 
6295 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6296 		dev_dbg(&pf->pdev->dev,
6297 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6298 			ch->max_tx_rate,
6299 			credits,
6300 			ch->seid);
6301 	}
6302 
6303 	/* in case of VF, this will be main SRIOV VSI */
6304 	ch->parent_vsi = vsi;
6305 
6306 	/* and update main_vsi's count for queue_available to use */
6307 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6308 
6309 	return 0;
6310 }
6311 
6312 /**
6313  * i40e_configure_queue_channels - Add queue channel for the given TCs
6314  * @vsi: VSI to be configured
6315  *
6316  * Configures queue channel mapping to the given TCs
6317  **/
6318 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6319 {
6320 	struct i40e_channel *ch;
6321 	u64 max_rate = 0;
6322 	int ret = 0, i;
6323 
6324 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6325 	vsi->tc_seid_map[0] = vsi->seid;
6326 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6327 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6328 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6329 			if (!ch) {
6330 				ret = -ENOMEM;
6331 				goto err_free;
6332 			}
6333 
6334 			INIT_LIST_HEAD(&ch->list);
6335 			ch->num_queue_pairs =
6336 				vsi->tc_config.tc_info[i].qcount;
6337 			ch->base_queue =
6338 				vsi->tc_config.tc_info[i].qoffset;
6339 
6340 			/* Bandwidth limit through tc interface is in bytes/s,
6341 			 * change to Mbit/s
6342 			 */
6343 			max_rate = vsi->mqprio_qopt.max_rate[i];
6344 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6345 			ch->max_tx_rate = max_rate;
6346 
6347 			list_add_tail(&ch->list, &vsi->ch_list);
6348 
6349 			ret = i40e_create_queue_channel(vsi, ch);
6350 			if (ret) {
6351 				dev_err(&vsi->back->pdev->dev,
6352 					"Failed creating queue channel with TC%d: queues %d\n",
6353 					i, ch->num_queue_pairs);
6354 				goto err_free;
6355 			}
6356 			vsi->tc_seid_map[i] = ch->seid;
6357 		}
6358 	}
6359 	return ret;
6360 
6361 err_free:
6362 	i40e_remove_queue_channels(vsi);
6363 	return ret;
6364 }
6365 
6366 /**
6367  * i40e_veb_config_tc - Configure TCs for given VEB
6368  * @veb: given VEB
6369  * @enabled_tc: TC bitmap
6370  *
6371  * Configures given TC bitmap for VEB (switching) element
6372  **/
6373 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6374 {
6375 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6376 	struct i40e_pf *pf = veb->pf;
6377 	int ret = 0;
6378 	int i;
6379 
6380 	/* No TCs or already enabled TCs just return */
6381 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6382 		return ret;
6383 
6384 	bw_data.tc_valid_bits = enabled_tc;
6385 	/* bw_data.absolute_credits is not set (relative) */
6386 
6387 	/* Enable ETS TCs with equal BW Share for now */
6388 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6389 		if (enabled_tc & BIT(i))
6390 			bw_data.tc_bw_share_credits[i] = 1;
6391 	}
6392 
6393 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6394 						   &bw_data, NULL);
6395 	if (ret) {
6396 		dev_info(&pf->pdev->dev,
6397 			 "VEB bw config failed, err %s aq_err %s\n",
6398 			 i40e_stat_str(&pf->hw, ret),
6399 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6400 		goto out;
6401 	}
6402 
6403 	/* Update the BW information */
6404 	ret = i40e_veb_get_bw_info(veb);
6405 	if (ret) {
6406 		dev_info(&pf->pdev->dev,
6407 			 "Failed getting veb bw config, err %s aq_err %s\n",
6408 			 i40e_stat_str(&pf->hw, ret),
6409 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6410 	}
6411 
6412 out:
6413 	return ret;
6414 }
6415 
6416 #ifdef CONFIG_I40E_DCB
6417 /**
6418  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6419  * @pf: PF struct
6420  *
6421  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6422  * the caller would've quiesce all the VSIs before calling
6423  * this function
6424  **/
6425 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6426 {
6427 	u8 tc_map = 0;
6428 	int ret;
6429 	u8 v;
6430 
6431 	/* Enable the TCs available on PF to all VEBs */
6432 	tc_map = i40e_pf_get_tc_map(pf);
6433 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6434 		return;
6435 
6436 	for (v = 0; v < I40E_MAX_VEB; v++) {
6437 		if (!pf->veb[v])
6438 			continue;
6439 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6440 		if (ret) {
6441 			dev_info(&pf->pdev->dev,
6442 				 "Failed configuring TC for VEB seid=%d\n",
6443 				 pf->veb[v]->seid);
6444 			/* Will try to configure as many components */
6445 		}
6446 	}
6447 
6448 	/* Update each VSI */
6449 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6450 		if (!pf->vsi[v])
6451 			continue;
6452 
6453 		/* - Enable all TCs for the LAN VSI
6454 		 * - For all others keep them at TC0 for now
6455 		 */
6456 		if (v == pf->lan_vsi)
6457 			tc_map = i40e_pf_get_tc_map(pf);
6458 		else
6459 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6460 
6461 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6462 		if (ret) {
6463 			dev_info(&pf->pdev->dev,
6464 				 "Failed configuring TC for VSI seid=%d\n",
6465 				 pf->vsi[v]->seid);
6466 			/* Will try to configure as many components */
6467 		} else {
6468 			/* Re-configure VSI vectors based on updated TC map */
6469 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6470 			if (pf->vsi[v]->netdev)
6471 				i40e_dcbnl_set_all(pf->vsi[v]);
6472 		}
6473 	}
6474 }
6475 
6476 /**
6477  * i40e_resume_port_tx - Resume port Tx
6478  * @pf: PF struct
6479  *
6480  * Resume a port's Tx and issue a PF reset in case of failure to
6481  * resume.
6482  **/
6483 static int i40e_resume_port_tx(struct i40e_pf *pf)
6484 {
6485 	struct i40e_hw *hw = &pf->hw;
6486 	int ret;
6487 
6488 	ret = i40e_aq_resume_port_tx(hw, NULL);
6489 	if (ret) {
6490 		dev_info(&pf->pdev->dev,
6491 			 "Resume Port Tx failed, err %s aq_err %s\n",
6492 			  i40e_stat_str(&pf->hw, ret),
6493 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6494 		/* Schedule PF reset to recover */
6495 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6496 		i40e_service_event_schedule(pf);
6497 	}
6498 
6499 	return ret;
6500 }
6501 
6502 /**
6503  * i40e_suspend_port_tx - Suspend port Tx
6504  * @pf: PF struct
6505  *
6506  * Suspend a port's Tx and issue a PF reset in case of failure.
6507  **/
6508 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6509 {
6510 	struct i40e_hw *hw = &pf->hw;
6511 	int ret;
6512 
6513 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6514 	if (ret) {
6515 		dev_info(&pf->pdev->dev,
6516 			 "Suspend Port Tx failed, err %s aq_err %s\n",
6517 			 i40e_stat_str(&pf->hw, ret),
6518 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6519 		/* Schedule PF reset to recover */
6520 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6521 		i40e_service_event_schedule(pf);
6522 	}
6523 
6524 	return ret;
6525 }
6526 
6527 /**
6528  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6529  * @pf: PF being configured
6530  * @new_cfg: New DCBX configuration
6531  *
6532  * Program DCB settings into HW and reconfigure VEB/VSIs on
6533  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6534  **/
6535 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6536 				  struct i40e_dcbx_config *new_cfg)
6537 {
6538 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6539 	int ret;
6540 
6541 	/* Check if need reconfiguration */
6542 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6543 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6544 		return 0;
6545 	}
6546 
6547 	/* Config change disable all VSIs */
6548 	i40e_pf_quiesce_all_vsi(pf);
6549 
6550 	/* Copy the new config to the current config */
6551 	*old_cfg = *new_cfg;
6552 	old_cfg->etsrec = old_cfg->etscfg;
6553 	ret = i40e_set_dcb_config(&pf->hw);
6554 	if (ret) {
6555 		dev_info(&pf->pdev->dev,
6556 			 "Set DCB Config failed, err %s aq_err %s\n",
6557 			 i40e_stat_str(&pf->hw, ret),
6558 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6559 		goto out;
6560 	}
6561 
6562 	/* Changes in configuration update VEB/VSI */
6563 	i40e_dcb_reconfigure(pf);
6564 out:
6565 	/* In case of reset do not try to resume anything */
6566 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6567 		/* Re-start the VSIs if disabled */
6568 		ret = i40e_resume_port_tx(pf);
6569 		/* In case of error no point in resuming VSIs */
6570 		if (ret)
6571 			goto err;
6572 		i40e_pf_unquiesce_all_vsi(pf);
6573 	}
6574 err:
6575 	return ret;
6576 }
6577 
6578 /**
6579  * i40e_hw_dcb_config - Program new DCBX settings into HW
6580  * @pf: PF being configured
6581  * @new_cfg: New DCBX configuration
6582  *
6583  * Program DCB settings into HW and reconfigure VEB/VSIs on
6584  * given PF
6585  **/
6586 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6587 {
6588 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6589 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6590 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6591 	struct i40e_dcbx_config *old_cfg;
6592 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6593 	struct i40e_rx_pb_config pb_cfg;
6594 	struct i40e_hw *hw = &pf->hw;
6595 	u8 num_ports = hw->num_ports;
6596 	bool need_reconfig;
6597 	int ret = -EINVAL;
6598 	u8 lltc_map = 0;
6599 	u8 tc_map = 0;
6600 	u8 new_numtc;
6601 	u8 i;
6602 
6603 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6604 	/* Un-pack information to Program ETS HW via shared API
6605 	 * numtc, tcmap
6606 	 * LLTC map
6607 	 * ETS/NON-ETS arbiter mode
6608 	 * max exponent (credit refills)
6609 	 * Total number of ports
6610 	 * PFC priority bit-map
6611 	 * Priority Table
6612 	 * BW % per TC
6613 	 * Arbiter mode between UPs sharing same TC
6614 	 * TSA table (ETS or non-ETS)
6615 	 * EEE enabled or not
6616 	 * MFS TC table
6617 	 */
6618 
6619 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6620 
6621 	memset(&ets_data, 0, sizeof(ets_data));
6622 	for (i = 0; i < new_numtc; i++) {
6623 		tc_map |= BIT(i);
6624 		switch (new_cfg->etscfg.tsatable[i]) {
6625 		case I40E_IEEE_TSA_ETS:
6626 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6627 			ets_data.tc_bw_share_credits[i] =
6628 					new_cfg->etscfg.tcbwtable[i];
6629 			break;
6630 		case I40E_IEEE_TSA_STRICT:
6631 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6632 			lltc_map |= BIT(i);
6633 			ets_data.tc_bw_share_credits[i] =
6634 					I40E_DCB_STRICT_PRIO_CREDITS;
6635 			break;
6636 		default:
6637 			/* Invalid TSA type */
6638 			need_reconfig = false;
6639 			goto out;
6640 		}
6641 	}
6642 
6643 	old_cfg = &hw->local_dcbx_config;
6644 	/* Check if need reconfiguration */
6645 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6646 
6647 	/* If needed, enable/disable frame tagging, disable all VSIs
6648 	 * and suspend port tx
6649 	 */
6650 	if (need_reconfig) {
6651 		/* Enable DCB tagging only when more than one TC */
6652 		if (new_numtc > 1)
6653 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6654 		else
6655 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6656 
6657 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6658 		/* Reconfiguration needed quiesce all VSIs */
6659 		i40e_pf_quiesce_all_vsi(pf);
6660 		ret = i40e_suspend_port_tx(pf);
6661 		if (ret)
6662 			goto err;
6663 	}
6664 
6665 	/* Configure Port ETS Tx Scheduler */
6666 	ets_data.tc_valid_bits = tc_map;
6667 	ets_data.tc_strict_priority_flags = lltc_map;
6668 	ret = i40e_aq_config_switch_comp_ets
6669 		(hw, pf->mac_seid, &ets_data,
6670 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
6671 	if (ret) {
6672 		dev_info(&pf->pdev->dev,
6673 			 "Modify Port ETS failed, err %s aq_err %s\n",
6674 			 i40e_stat_str(&pf->hw, ret),
6675 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6676 		goto out;
6677 	}
6678 
6679 	/* Configure Rx ETS HW */
6680 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
6681 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
6682 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
6683 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
6684 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
6685 				   lltc_map);
6686 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
6687 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
6688 				     prio_type);
6689 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
6690 			       new_cfg->etscfg.prioritytable);
6691 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
6692 
6693 	/* Configure Rx Packet Buffers in HW */
6694 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6695 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
6696 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
6697 	}
6698 
6699 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
6700 					 false, new_cfg->pfc.pfcenable,
6701 					 mfs_tc, &pb_cfg);
6702 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
6703 
6704 	/* Update the local Rx Packet buffer config */
6705 	pf->pb_cfg = pb_cfg;
6706 
6707 	/* Inform the FW about changes to DCB configuration */
6708 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
6709 	if (ret) {
6710 		dev_info(&pf->pdev->dev,
6711 			 "DCB Updated failed, err %s aq_err %s\n",
6712 			 i40e_stat_str(&pf->hw, ret),
6713 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6714 		goto out;
6715 	}
6716 
6717 	/* Update the port DCBx configuration */
6718 	*old_cfg = *new_cfg;
6719 
6720 	/* Changes in configuration update VEB/VSI */
6721 	i40e_dcb_reconfigure(pf);
6722 out:
6723 	/* Re-start the VSIs if disabled */
6724 	if (need_reconfig) {
6725 		ret = i40e_resume_port_tx(pf);
6726 
6727 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
6728 		/* In case of error no point in resuming VSIs */
6729 		if (ret)
6730 			goto err;
6731 
6732 		/* Wait for the PF's queues to be disabled */
6733 		ret = i40e_pf_wait_queues_disabled(pf);
6734 		if (ret) {
6735 			/* Schedule PF reset to recover */
6736 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6737 			i40e_service_event_schedule(pf);
6738 			goto err;
6739 		} else {
6740 			i40e_pf_unquiesce_all_vsi(pf);
6741 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
6742 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
6743 		}
6744 		/* registers are set, lets apply */
6745 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
6746 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
6747 	}
6748 
6749 err:
6750 	return ret;
6751 }
6752 
6753 /**
6754  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
6755  * @pf: PF being queried
6756  *
6757  * Set default DCB configuration in case DCB is to be done in SW.
6758  **/
6759 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
6760 {
6761 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
6762 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6763 	struct i40e_hw *hw = &pf->hw;
6764 	int err;
6765 
6766 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
6767 		/* Update the local cached instance with TC0 ETS */
6768 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
6769 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
6770 		pf->tmp_cfg.etscfg.maxtcs = 0;
6771 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
6772 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
6773 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
6774 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
6775 		/* FW needs one App to configure HW */
6776 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
6777 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
6778 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
6779 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
6780 
6781 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
6782 	}
6783 
6784 	memset(&ets_data, 0, sizeof(ets_data));
6785 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
6786 	ets_data.tc_strict_priority_flags = 0; /* ETS */
6787 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
6788 
6789 	/* Enable ETS on the Physical port */
6790 	err = i40e_aq_config_switch_comp_ets
6791 		(hw, pf->mac_seid, &ets_data,
6792 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
6793 	if (err) {
6794 		dev_info(&pf->pdev->dev,
6795 			 "Enable Port ETS failed, err %s aq_err %s\n",
6796 			 i40e_stat_str(&pf->hw, err),
6797 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6798 		err = -ENOENT;
6799 		goto out;
6800 	}
6801 
6802 	/* Update the local cached instance with TC0 ETS */
6803 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
6804 	dcb_cfg->etscfg.cbs = 0;
6805 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
6806 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
6807 
6808 out:
6809 	return err;
6810 }
6811 
6812 /**
6813  * i40e_init_pf_dcb - Initialize DCB configuration
6814  * @pf: PF being configured
6815  *
6816  * Query the current DCB configuration and cache it
6817  * in the hardware structure
6818  **/
6819 static int i40e_init_pf_dcb(struct i40e_pf *pf)
6820 {
6821 	struct i40e_hw *hw = &pf->hw;
6822 	int err;
6823 
6824 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
6825 	 * Also do not enable DCBx if FW LLDP agent is disabled
6826 	 */
6827 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
6828 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
6829 		err = I40E_NOT_SUPPORTED;
6830 		goto out;
6831 	}
6832 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
6833 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
6834 		err = i40e_dcb_sw_default_config(pf);
6835 		if (err) {
6836 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
6837 			goto out;
6838 		}
6839 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
6840 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
6841 			       DCB_CAP_DCBX_VER_IEEE;
6842 		/* at init capable but disabled */
6843 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
6844 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6845 		goto out;
6846 	}
6847 	err = i40e_init_dcb(hw, true);
6848 	if (!err) {
6849 		/* Device/Function is not DCBX capable */
6850 		if ((!hw->func_caps.dcb) ||
6851 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6852 			dev_info(&pf->pdev->dev,
6853 				 "DCBX offload is not supported or is disabled for this PF.\n");
6854 		} else {
6855 			/* When status is not DISABLED then DCBX in FW */
6856 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6857 				       DCB_CAP_DCBX_VER_IEEE;
6858 
6859 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
6860 			/* Enable DCB tagging only when more than one TC
6861 			 * or explicitly disable if only one TC
6862 			 */
6863 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6864 				pf->flags |= I40E_FLAG_DCB_ENABLED;
6865 			else
6866 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6867 			dev_dbg(&pf->pdev->dev,
6868 				"DCBX offload is supported for this PF.\n");
6869 		}
6870 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
6871 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
6872 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
6873 	} else {
6874 		dev_info(&pf->pdev->dev,
6875 			 "Query for DCB configuration failed, err %s aq_err %s\n",
6876 			 i40e_stat_str(&pf->hw, err),
6877 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6878 	}
6879 
6880 out:
6881 	return err;
6882 }
6883 #endif /* CONFIG_I40E_DCB */
6884 
6885 /**
6886  * i40e_print_link_message - print link up or down
6887  * @vsi: the VSI for which link needs a message
6888  * @isup: true of link is up, false otherwise
6889  */
6890 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
6891 {
6892 	enum i40e_aq_link_speed new_speed;
6893 	struct i40e_pf *pf = vsi->back;
6894 	char *speed = "Unknown";
6895 	char *fc = "Unknown";
6896 	char *fec = "";
6897 	char *req_fec = "";
6898 	char *an = "";
6899 
6900 	if (isup)
6901 		new_speed = pf->hw.phy.link_info.link_speed;
6902 	else
6903 		new_speed = I40E_LINK_SPEED_UNKNOWN;
6904 
6905 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
6906 		return;
6907 	vsi->current_isup = isup;
6908 	vsi->current_speed = new_speed;
6909 	if (!isup) {
6910 		netdev_info(vsi->netdev, "NIC Link is Down\n");
6911 		return;
6912 	}
6913 
6914 	/* Warn user if link speed on NPAR enabled partition is not at
6915 	 * least 10GB
6916 	 */
6917 	if (pf->hw.func_caps.npar_enable &&
6918 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
6919 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
6920 		netdev_warn(vsi->netdev,
6921 			    "The partition detected link speed that is less than 10Gbps\n");
6922 
6923 	switch (pf->hw.phy.link_info.link_speed) {
6924 	case I40E_LINK_SPEED_40GB:
6925 		speed = "40 G";
6926 		break;
6927 	case I40E_LINK_SPEED_20GB:
6928 		speed = "20 G";
6929 		break;
6930 	case I40E_LINK_SPEED_25GB:
6931 		speed = "25 G";
6932 		break;
6933 	case I40E_LINK_SPEED_10GB:
6934 		speed = "10 G";
6935 		break;
6936 	case I40E_LINK_SPEED_5GB:
6937 		speed = "5 G";
6938 		break;
6939 	case I40E_LINK_SPEED_2_5GB:
6940 		speed = "2.5 G";
6941 		break;
6942 	case I40E_LINK_SPEED_1GB:
6943 		speed = "1000 M";
6944 		break;
6945 	case I40E_LINK_SPEED_100MB:
6946 		speed = "100 M";
6947 		break;
6948 	default:
6949 		break;
6950 	}
6951 
6952 	switch (pf->hw.fc.current_mode) {
6953 	case I40E_FC_FULL:
6954 		fc = "RX/TX";
6955 		break;
6956 	case I40E_FC_TX_PAUSE:
6957 		fc = "TX";
6958 		break;
6959 	case I40E_FC_RX_PAUSE:
6960 		fc = "RX";
6961 		break;
6962 	default:
6963 		fc = "None";
6964 		break;
6965 	}
6966 
6967 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
6968 		req_fec = "None";
6969 		fec = "None";
6970 		an = "False";
6971 
6972 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
6973 			an = "True";
6974 
6975 		if (pf->hw.phy.link_info.fec_info &
6976 		    I40E_AQ_CONFIG_FEC_KR_ENA)
6977 			fec = "CL74 FC-FEC/BASE-R";
6978 		else if (pf->hw.phy.link_info.fec_info &
6979 			 I40E_AQ_CONFIG_FEC_RS_ENA)
6980 			fec = "CL108 RS-FEC";
6981 
6982 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
6983 		 * both RS and FC are requested
6984 		 */
6985 		if (vsi->back->hw.phy.link_info.req_fec_info &
6986 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
6987 			if (vsi->back->hw.phy.link_info.req_fec_info &
6988 			    I40E_AQ_REQUEST_FEC_RS)
6989 				req_fec = "CL108 RS-FEC";
6990 			else
6991 				req_fec = "CL74 FC-FEC/BASE-R";
6992 		}
6993 		netdev_info(vsi->netdev,
6994 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
6995 			    speed, req_fec, fec, an, fc);
6996 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
6997 		req_fec = "None";
6998 		fec = "None";
6999 		an = "False";
7000 
7001 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7002 			an = "True";
7003 
7004 		if (pf->hw.phy.link_info.fec_info &
7005 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7006 			fec = "CL74 FC-FEC/BASE-R";
7007 
7008 		if (pf->hw.phy.link_info.req_fec_info &
7009 		    I40E_AQ_REQUEST_FEC_KR)
7010 			req_fec = "CL74 FC-FEC/BASE-R";
7011 
7012 		netdev_info(vsi->netdev,
7013 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7014 			    speed, req_fec, fec, an, fc);
7015 	} else {
7016 		netdev_info(vsi->netdev,
7017 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7018 			    speed, fc);
7019 	}
7020 
7021 }
7022 
7023 /**
7024  * i40e_up_complete - Finish the last steps of bringing up a connection
7025  * @vsi: the VSI being configured
7026  **/
7027 static int i40e_up_complete(struct i40e_vsi *vsi)
7028 {
7029 	struct i40e_pf *pf = vsi->back;
7030 	int err;
7031 
7032 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7033 		i40e_vsi_configure_msix(vsi);
7034 	else
7035 		i40e_configure_msi_and_legacy(vsi);
7036 
7037 	/* start rings */
7038 	err = i40e_vsi_start_rings(vsi);
7039 	if (err)
7040 		return err;
7041 
7042 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7043 	i40e_napi_enable_all(vsi);
7044 	i40e_vsi_enable_irq(vsi);
7045 
7046 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7047 	    (vsi->netdev)) {
7048 		i40e_print_link_message(vsi, true);
7049 		netif_tx_start_all_queues(vsi->netdev);
7050 		netif_carrier_on(vsi->netdev);
7051 	}
7052 
7053 	/* replay FDIR SB filters */
7054 	if (vsi->type == I40E_VSI_FDIR) {
7055 		/* reset fd counters */
7056 		pf->fd_add_err = 0;
7057 		pf->fd_atr_cnt = 0;
7058 		i40e_fdir_filter_restore(vsi);
7059 	}
7060 
7061 	/* On the next run of the service_task, notify any clients of the new
7062 	 * opened netdev
7063 	 */
7064 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7065 	i40e_service_event_schedule(pf);
7066 
7067 	return 0;
7068 }
7069 
7070 /**
7071  * i40e_vsi_reinit_locked - Reset the VSI
7072  * @vsi: the VSI being configured
7073  *
7074  * Rebuild the ring structs after some configuration
7075  * has changed, e.g. MTU size.
7076  **/
7077 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7078 {
7079 	struct i40e_pf *pf = vsi->back;
7080 
7081 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7082 		usleep_range(1000, 2000);
7083 	i40e_down(vsi);
7084 
7085 	i40e_up(vsi);
7086 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7087 }
7088 
7089 /**
7090  * i40e_force_link_state - Force the link status
7091  * @pf: board private structure
7092  * @is_up: whether the link state should be forced up or down
7093  **/
7094 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7095 {
7096 	struct i40e_aq_get_phy_abilities_resp abilities;
7097 	struct i40e_aq_set_phy_config config = {0};
7098 	bool non_zero_phy_type = is_up;
7099 	struct i40e_hw *hw = &pf->hw;
7100 	i40e_status err;
7101 	u64 mask;
7102 	u8 speed;
7103 
7104 	/* Card might've been put in an unstable state by other drivers
7105 	 * and applications, which causes incorrect speed values being
7106 	 * set on startup. In order to clear speed registers, we call
7107 	 * get_phy_capabilities twice, once to get initial state of
7108 	 * available speeds, and once to get current PHY config.
7109 	 */
7110 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7111 					   NULL);
7112 	if (err) {
7113 		dev_err(&pf->pdev->dev,
7114 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7115 			i40e_stat_str(hw, err),
7116 			i40e_aq_str(hw, hw->aq.asq_last_status));
7117 		return err;
7118 	}
7119 	speed = abilities.link_speed;
7120 
7121 	/* Get the current phy config */
7122 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7123 					   NULL);
7124 	if (err) {
7125 		dev_err(&pf->pdev->dev,
7126 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7127 			i40e_stat_str(hw, err),
7128 			i40e_aq_str(hw, hw->aq.asq_last_status));
7129 		return err;
7130 	}
7131 
7132 	/* If link needs to go up, but was not forced to go down,
7133 	 * and its speed values are OK, no need for a flap
7134 	 * if non_zero_phy_type was set, still need to force up
7135 	 */
7136 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7137 		non_zero_phy_type = true;
7138 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7139 		return I40E_SUCCESS;
7140 
7141 	/* To force link we need to set bits for all supported PHY types,
7142 	 * but there are now more than 32, so we need to split the bitmap
7143 	 * across two fields.
7144 	 */
7145 	mask = I40E_PHY_TYPES_BITMASK;
7146 	config.phy_type =
7147 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7148 	config.phy_type_ext =
7149 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7150 	/* Copy the old settings, except of phy_type */
7151 	config.abilities = abilities.abilities;
7152 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7153 		if (is_up)
7154 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7155 		else
7156 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7157 	}
7158 	if (abilities.link_speed != 0)
7159 		config.link_speed = abilities.link_speed;
7160 	else
7161 		config.link_speed = speed;
7162 	config.eee_capability = abilities.eee_capability;
7163 	config.eeer = abilities.eeer_val;
7164 	config.low_power_ctrl = abilities.d3_lpan;
7165 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7166 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7167 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7168 
7169 	if (err) {
7170 		dev_err(&pf->pdev->dev,
7171 			"set phy config ret =  %s last_status =  %s\n",
7172 			i40e_stat_str(&pf->hw, err),
7173 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7174 		return err;
7175 	}
7176 
7177 	/* Update the link info */
7178 	err = i40e_update_link_info(hw);
7179 	if (err) {
7180 		/* Wait a little bit (on 40G cards it sometimes takes a really
7181 		 * long time for link to come back from the atomic reset)
7182 		 * and try once more
7183 		 */
7184 		msleep(1000);
7185 		i40e_update_link_info(hw);
7186 	}
7187 
7188 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7189 
7190 	return I40E_SUCCESS;
7191 }
7192 
7193 /**
7194  * i40e_up - Bring the connection back up after being down
7195  * @vsi: the VSI being configured
7196  **/
7197 int i40e_up(struct i40e_vsi *vsi)
7198 {
7199 	int err;
7200 
7201 	if (vsi->type == I40E_VSI_MAIN &&
7202 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7203 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7204 		i40e_force_link_state(vsi->back, true);
7205 
7206 	err = i40e_vsi_configure(vsi);
7207 	if (!err)
7208 		err = i40e_up_complete(vsi);
7209 
7210 	return err;
7211 }
7212 
7213 /**
7214  * i40e_down - Shutdown the connection processing
7215  * @vsi: the VSI being stopped
7216  **/
7217 void i40e_down(struct i40e_vsi *vsi)
7218 {
7219 	int i;
7220 
7221 	/* It is assumed that the caller of this function
7222 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7223 	 */
7224 	if (vsi->netdev) {
7225 		netif_carrier_off(vsi->netdev);
7226 		netif_tx_disable(vsi->netdev);
7227 	}
7228 	i40e_vsi_disable_irq(vsi);
7229 	i40e_vsi_stop_rings(vsi);
7230 	if (vsi->type == I40E_VSI_MAIN &&
7231 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7232 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7233 		i40e_force_link_state(vsi->back, false);
7234 	i40e_napi_disable_all(vsi);
7235 
7236 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7237 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7238 		if (i40e_enabled_xdp_vsi(vsi)) {
7239 			/* Make sure that in-progress ndo_xdp_xmit and
7240 			 * ndo_xsk_wakeup calls are completed.
7241 			 */
7242 			synchronize_rcu();
7243 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7244 		}
7245 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7246 	}
7247 
7248 }
7249 
7250 /**
7251  * i40e_validate_mqprio_qopt- validate queue mapping info
7252  * @vsi: the VSI being configured
7253  * @mqprio_qopt: queue parametrs
7254  **/
7255 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7256 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7257 {
7258 	u64 sum_max_rate = 0;
7259 	u64 max_rate = 0;
7260 	int i;
7261 
7262 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7263 	    mqprio_qopt->qopt.num_tc < 1 ||
7264 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7265 		return -EINVAL;
7266 	for (i = 0; ; i++) {
7267 		if (!mqprio_qopt->qopt.count[i])
7268 			return -EINVAL;
7269 		if (mqprio_qopt->min_rate[i]) {
7270 			dev_err(&vsi->back->pdev->dev,
7271 				"Invalid min tx rate (greater than 0) specified\n");
7272 			return -EINVAL;
7273 		}
7274 		max_rate = mqprio_qopt->max_rate[i];
7275 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7276 		sum_max_rate += max_rate;
7277 
7278 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7279 			break;
7280 		if (mqprio_qopt->qopt.offset[i + 1] !=
7281 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7282 			return -EINVAL;
7283 	}
7284 	if (vsi->num_queue_pairs <
7285 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7286 		return -EINVAL;
7287 	}
7288 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7289 		dev_err(&vsi->back->pdev->dev,
7290 			"Invalid max tx rate specified\n");
7291 		return -EINVAL;
7292 	}
7293 	return 0;
7294 }
7295 
7296 /**
7297  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7298  * @vsi: the VSI being configured
7299  **/
7300 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7301 {
7302 	u16 qcount;
7303 	int i;
7304 
7305 	/* Only TC0 is enabled */
7306 	vsi->tc_config.numtc = 1;
7307 	vsi->tc_config.enabled_tc = 1;
7308 	qcount = min_t(int, vsi->alloc_queue_pairs,
7309 		       i40e_pf_get_max_q_per_tc(vsi->back));
7310 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7311 		/* For the TC that is not enabled set the offset to default
7312 		 * queue and allocate one queue for the given TC.
7313 		 */
7314 		vsi->tc_config.tc_info[i].qoffset = 0;
7315 		if (i == 0)
7316 			vsi->tc_config.tc_info[i].qcount = qcount;
7317 		else
7318 			vsi->tc_config.tc_info[i].qcount = 1;
7319 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7320 	}
7321 }
7322 
7323 /**
7324  * i40e_del_macvlan_filter
7325  * @hw: pointer to the HW structure
7326  * @seid: seid of the channel VSI
7327  * @macaddr: the mac address to apply as a filter
7328  * @aq_err: store the admin Q error
7329  *
7330  * This function deletes a mac filter on the channel VSI which serves as the
7331  * macvlan. Returns 0 on success.
7332  **/
7333 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7334 					   const u8 *macaddr, int *aq_err)
7335 {
7336 	struct i40e_aqc_remove_macvlan_element_data element;
7337 	i40e_status status;
7338 
7339 	memset(&element, 0, sizeof(element));
7340 	ether_addr_copy(element.mac_addr, macaddr);
7341 	element.vlan_tag = 0;
7342 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7343 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7344 	*aq_err = hw->aq.asq_last_status;
7345 
7346 	return status;
7347 }
7348 
7349 /**
7350  * i40e_add_macvlan_filter
7351  * @hw: pointer to the HW structure
7352  * @seid: seid of the channel VSI
7353  * @macaddr: the mac address to apply as a filter
7354  * @aq_err: store the admin Q error
7355  *
7356  * This function adds a mac filter on the channel VSI which serves as the
7357  * macvlan. Returns 0 on success.
7358  **/
7359 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7360 					   const u8 *macaddr, int *aq_err)
7361 {
7362 	struct i40e_aqc_add_macvlan_element_data element;
7363 	i40e_status status;
7364 	u16 cmd_flags = 0;
7365 
7366 	ether_addr_copy(element.mac_addr, macaddr);
7367 	element.vlan_tag = 0;
7368 	element.queue_number = 0;
7369 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7370 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7371 	element.flags = cpu_to_le16(cmd_flags);
7372 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7373 	*aq_err = hw->aq.asq_last_status;
7374 
7375 	return status;
7376 }
7377 
7378 /**
7379  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7380  * @vsi: the VSI we want to access
7381  * @ch: the channel we want to access
7382  */
7383 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7384 {
7385 	struct i40e_ring *tx_ring, *rx_ring;
7386 	u16 pf_q;
7387 	int i;
7388 
7389 	for (i = 0; i < ch->num_queue_pairs; i++) {
7390 		pf_q = ch->base_queue + i;
7391 		tx_ring = vsi->tx_rings[pf_q];
7392 		tx_ring->ch = NULL;
7393 		rx_ring = vsi->rx_rings[pf_q];
7394 		rx_ring->ch = NULL;
7395 	}
7396 }
7397 
7398 /**
7399  * i40e_free_macvlan_channels
7400  * @vsi: the VSI we want to access
7401  *
7402  * This function frees the Qs of the channel VSI from
7403  * the stack and also deletes the channel VSIs which
7404  * serve as macvlans.
7405  */
7406 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7407 {
7408 	struct i40e_channel *ch, *ch_tmp;
7409 	int ret;
7410 
7411 	if (list_empty(&vsi->macvlan_list))
7412 		return;
7413 
7414 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7415 		struct i40e_vsi *parent_vsi;
7416 
7417 		if (i40e_is_channel_macvlan(ch)) {
7418 			i40e_reset_ch_rings(vsi, ch);
7419 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7420 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7421 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7422 			kfree(ch->fwd);
7423 			ch->fwd = NULL;
7424 		}
7425 
7426 		list_del(&ch->list);
7427 		parent_vsi = ch->parent_vsi;
7428 		if (!parent_vsi || !ch->initialized) {
7429 			kfree(ch);
7430 			continue;
7431 		}
7432 
7433 		/* remove the VSI */
7434 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7435 					     NULL);
7436 		if (ret)
7437 			dev_err(&vsi->back->pdev->dev,
7438 				"unable to remove channel (%d) for parent VSI(%d)\n",
7439 				ch->seid, parent_vsi->seid);
7440 		kfree(ch);
7441 	}
7442 	vsi->macvlan_cnt = 0;
7443 }
7444 
7445 /**
7446  * i40e_fwd_ring_up - bring the macvlan device up
7447  * @vsi: the VSI we want to access
7448  * @vdev: macvlan netdevice
7449  * @fwd: the private fwd structure
7450  */
7451 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7452 			    struct i40e_fwd_adapter *fwd)
7453 {
7454 	int ret = 0, num_tc = 1,  i, aq_err;
7455 	struct i40e_channel *ch, *ch_tmp;
7456 	struct i40e_pf *pf = vsi->back;
7457 	struct i40e_hw *hw = &pf->hw;
7458 
7459 	if (list_empty(&vsi->macvlan_list))
7460 		return -EINVAL;
7461 
7462 	/* Go through the list and find an available channel */
7463 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7464 		if (!i40e_is_channel_macvlan(ch)) {
7465 			ch->fwd = fwd;
7466 			/* record configuration for macvlan interface in vdev */
7467 			for (i = 0; i < num_tc; i++)
7468 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7469 							     i,
7470 							     ch->num_queue_pairs,
7471 							     ch->base_queue);
7472 			for (i = 0; i < ch->num_queue_pairs; i++) {
7473 				struct i40e_ring *tx_ring, *rx_ring;
7474 				u16 pf_q;
7475 
7476 				pf_q = ch->base_queue + i;
7477 
7478 				/* Get to TX ring ptr */
7479 				tx_ring = vsi->tx_rings[pf_q];
7480 				tx_ring->ch = ch;
7481 
7482 				/* Get the RX ring ptr */
7483 				rx_ring = vsi->rx_rings[pf_q];
7484 				rx_ring->ch = ch;
7485 			}
7486 			break;
7487 		}
7488 	}
7489 
7490 	/* Guarantee all rings are updated before we update the
7491 	 * MAC address filter.
7492 	 */
7493 	wmb();
7494 
7495 	/* Add a mac filter */
7496 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7497 	if (ret) {
7498 		/* if we cannot add the MAC rule then disable the offload */
7499 		macvlan_release_l2fw_offload(vdev);
7500 		for (i = 0; i < ch->num_queue_pairs; i++) {
7501 			struct i40e_ring *rx_ring;
7502 			u16 pf_q;
7503 
7504 			pf_q = ch->base_queue + i;
7505 			rx_ring = vsi->rx_rings[pf_q];
7506 			rx_ring->netdev = NULL;
7507 		}
7508 		dev_info(&pf->pdev->dev,
7509 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7510 			  i40e_stat_str(hw, ret),
7511 			  i40e_aq_str(hw, aq_err));
7512 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7513 	}
7514 
7515 	return ret;
7516 }
7517 
7518 /**
7519  * i40e_setup_macvlans - create the channels which will be macvlans
7520  * @vsi: the VSI we want to access
7521  * @macvlan_cnt: no. of macvlans to be setup
7522  * @qcnt: no. of Qs per macvlan
7523  * @vdev: macvlan netdevice
7524  */
7525 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7526 			       struct net_device *vdev)
7527 {
7528 	struct i40e_pf *pf = vsi->back;
7529 	struct i40e_hw *hw = &pf->hw;
7530 	struct i40e_vsi_context ctxt;
7531 	u16 sections, qmap, num_qps;
7532 	struct i40e_channel *ch;
7533 	int i, pow, ret = 0;
7534 	u8 offset = 0;
7535 
7536 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7537 		return -EINVAL;
7538 
7539 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7540 
7541 	/* find the next higher power-of-2 of num queue pairs */
7542 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7543 
7544 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7545 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7546 
7547 	/* Setup context bits for the main VSI */
7548 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7549 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7550 	memset(&ctxt, 0, sizeof(ctxt));
7551 	ctxt.seid = vsi->seid;
7552 	ctxt.pf_num = vsi->back->hw.pf_id;
7553 	ctxt.vf_num = 0;
7554 	ctxt.uplink_seid = vsi->uplink_seid;
7555 	ctxt.info = vsi->info;
7556 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7557 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7558 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7559 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7560 
7561 	/* Reconfigure RSS for main VSI with new max queue count */
7562 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7563 	ret = i40e_vsi_config_rss(vsi);
7564 	if (ret) {
7565 		dev_info(&pf->pdev->dev,
7566 			 "Failed to reconfig RSS for num_queues (%u)\n",
7567 			 vsi->rss_size);
7568 		return ret;
7569 	}
7570 	vsi->reconfig_rss = true;
7571 	dev_dbg(&vsi->back->pdev->dev,
7572 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7573 	vsi->next_base_queue = num_qps;
7574 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7575 
7576 	/* Update the VSI after updating the VSI queue-mapping
7577 	 * information
7578 	 */
7579 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7580 	if (ret) {
7581 		dev_info(&pf->pdev->dev,
7582 			 "Update vsi tc config failed, err %s aq_err %s\n",
7583 			 i40e_stat_str(hw, ret),
7584 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7585 		return ret;
7586 	}
7587 	/* update the local VSI info with updated queue map */
7588 	i40e_vsi_update_queue_map(vsi, &ctxt);
7589 	vsi->info.valid_sections = 0;
7590 
7591 	/* Create channels for macvlans */
7592 	INIT_LIST_HEAD(&vsi->macvlan_list);
7593 	for (i = 0; i < macvlan_cnt; i++) {
7594 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7595 		if (!ch) {
7596 			ret = -ENOMEM;
7597 			goto err_free;
7598 		}
7599 		INIT_LIST_HEAD(&ch->list);
7600 		ch->num_queue_pairs = qcnt;
7601 		if (!i40e_setup_channel(pf, vsi, ch)) {
7602 			ret = -EINVAL;
7603 			kfree(ch);
7604 			goto err_free;
7605 		}
7606 		ch->parent_vsi = vsi;
7607 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7608 		vsi->macvlan_cnt++;
7609 		list_add_tail(&ch->list, &vsi->macvlan_list);
7610 	}
7611 
7612 	return ret;
7613 
7614 err_free:
7615 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7616 	i40e_free_macvlan_channels(vsi);
7617 
7618 	return ret;
7619 }
7620 
7621 /**
7622  * i40e_fwd_add - configure macvlans
7623  * @netdev: net device to configure
7624  * @vdev: macvlan netdevice
7625  **/
7626 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7627 {
7628 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7629 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7630 	struct i40e_vsi *vsi = np->vsi;
7631 	struct i40e_pf *pf = vsi->back;
7632 	struct i40e_fwd_adapter *fwd;
7633 	int avail_macvlan, ret;
7634 
7635 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7636 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7637 		return ERR_PTR(-EINVAL);
7638 	}
7639 	if ((pf->flags & I40E_FLAG_TC_MQPRIO)) {
7640 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7641 		return ERR_PTR(-EINVAL);
7642 	}
7643 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7644 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7645 		return ERR_PTR(-EINVAL);
7646 	}
7647 
7648 	/* The macvlan device has to be a single Q device so that the
7649 	 * tc_to_txq field can be reused to pick the tx queue.
7650 	 */
7651 	if (netif_is_multiqueue(vdev))
7652 		return ERR_PTR(-ERANGE);
7653 
7654 	if (!vsi->macvlan_cnt) {
7655 		/* reserve bit 0 for the pf device */
7656 		set_bit(0, vsi->fwd_bitmask);
7657 
7658 		/* Try to reserve as many queues as possible for macvlans. First
7659 		 * reserve 3/4th of max vectors, then half, then quarter and
7660 		 * calculate Qs per macvlan as you go
7661 		 */
7662 		vectors = pf->num_lan_msix;
7663 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7664 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7665 			q_per_macvlan = 4;
7666 			macvlan_cnt = (vectors - 32) / 4;
7667 		} else if (vectors <= 64 && vectors > 32) {
7668 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7669 			q_per_macvlan = 2;
7670 			macvlan_cnt = (vectors - 16) / 2;
7671 		} else if (vectors <= 32 && vectors > 16) {
7672 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7673 			q_per_macvlan = 1;
7674 			macvlan_cnt = vectors - 16;
7675 		} else if (vectors <= 16 && vectors > 8) {
7676 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7677 			q_per_macvlan = 1;
7678 			macvlan_cnt = vectors - 8;
7679 		} else {
7680 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7681 			q_per_macvlan = 1;
7682 			macvlan_cnt = vectors - 1;
7683 		}
7684 
7685 		if (macvlan_cnt == 0)
7686 			return ERR_PTR(-EBUSY);
7687 
7688 		/* Quiesce VSI queues */
7689 		i40e_quiesce_vsi(vsi);
7690 
7691 		/* sets up the macvlans but does not "enable" them */
7692 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
7693 					  vdev);
7694 		if (ret)
7695 			return ERR_PTR(ret);
7696 
7697 		/* Unquiesce VSI */
7698 		i40e_unquiesce_vsi(vsi);
7699 	}
7700 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
7701 					    vsi->macvlan_cnt);
7702 	if (avail_macvlan >= I40E_MAX_MACVLANS)
7703 		return ERR_PTR(-EBUSY);
7704 
7705 	/* create the fwd struct */
7706 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
7707 	if (!fwd)
7708 		return ERR_PTR(-ENOMEM);
7709 
7710 	set_bit(avail_macvlan, vsi->fwd_bitmask);
7711 	fwd->bit_no = avail_macvlan;
7712 	netdev_set_sb_channel(vdev, avail_macvlan);
7713 	fwd->netdev = vdev;
7714 
7715 	if (!netif_running(netdev))
7716 		return fwd;
7717 
7718 	/* Set fwd ring up */
7719 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
7720 	if (ret) {
7721 		/* unbind the queues and drop the subordinate channel config */
7722 		netdev_unbind_sb_channel(netdev, vdev);
7723 		netdev_set_sb_channel(vdev, 0);
7724 
7725 		kfree(fwd);
7726 		return ERR_PTR(-EINVAL);
7727 	}
7728 
7729 	return fwd;
7730 }
7731 
7732 /**
7733  * i40e_del_all_macvlans - Delete all the mac filters on the channels
7734  * @vsi: the VSI we want to access
7735  */
7736 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
7737 {
7738 	struct i40e_channel *ch, *ch_tmp;
7739 	struct i40e_pf *pf = vsi->back;
7740 	struct i40e_hw *hw = &pf->hw;
7741 	int aq_err, ret = 0;
7742 
7743 	if (list_empty(&vsi->macvlan_list))
7744 		return;
7745 
7746 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7747 		if (i40e_is_channel_macvlan(ch)) {
7748 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7749 						      i40e_channel_mac(ch),
7750 						      &aq_err);
7751 			if (!ret) {
7752 				/* Reset queue contexts */
7753 				i40e_reset_ch_rings(vsi, ch);
7754 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7755 				netdev_unbind_sb_channel(vsi->netdev,
7756 							 ch->fwd->netdev);
7757 				netdev_set_sb_channel(ch->fwd->netdev, 0);
7758 				kfree(ch->fwd);
7759 				ch->fwd = NULL;
7760 			}
7761 		}
7762 	}
7763 }
7764 
7765 /**
7766  * i40e_fwd_del - delete macvlan interfaces
7767  * @netdev: net device to configure
7768  * @vdev: macvlan netdevice
7769  */
7770 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
7771 {
7772 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7773 	struct i40e_fwd_adapter *fwd = vdev;
7774 	struct i40e_channel *ch, *ch_tmp;
7775 	struct i40e_vsi *vsi = np->vsi;
7776 	struct i40e_pf *pf = vsi->back;
7777 	struct i40e_hw *hw = &pf->hw;
7778 	int aq_err, ret = 0;
7779 
7780 	/* Find the channel associated with the macvlan and del mac filter */
7781 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7782 		if (i40e_is_channel_macvlan(ch) &&
7783 		    ether_addr_equal(i40e_channel_mac(ch),
7784 				     fwd->netdev->dev_addr)) {
7785 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7786 						      i40e_channel_mac(ch),
7787 						      &aq_err);
7788 			if (!ret) {
7789 				/* Reset queue contexts */
7790 				i40e_reset_ch_rings(vsi, ch);
7791 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7792 				netdev_unbind_sb_channel(netdev, fwd->netdev);
7793 				netdev_set_sb_channel(fwd->netdev, 0);
7794 				kfree(ch->fwd);
7795 				ch->fwd = NULL;
7796 			} else {
7797 				dev_info(&pf->pdev->dev,
7798 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
7799 					  i40e_stat_str(hw, ret),
7800 					  i40e_aq_str(hw, aq_err));
7801 			}
7802 			break;
7803 		}
7804 	}
7805 }
7806 
7807 /**
7808  * i40e_setup_tc - configure multiple traffic classes
7809  * @netdev: net device to configure
7810  * @type_data: tc offload data
7811  **/
7812 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
7813 {
7814 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
7815 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7816 	struct i40e_vsi *vsi = np->vsi;
7817 	struct i40e_pf *pf = vsi->back;
7818 	u8 enabled_tc = 0, num_tc, hw;
7819 	bool need_reset = false;
7820 	int old_queue_pairs;
7821 	int ret = -EINVAL;
7822 	u16 mode;
7823 	int i;
7824 
7825 	old_queue_pairs = vsi->num_queue_pairs;
7826 	num_tc = mqprio_qopt->qopt.num_tc;
7827 	hw = mqprio_qopt->qopt.hw;
7828 	mode = mqprio_qopt->mode;
7829 	if (!hw) {
7830 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7831 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
7832 		goto config_tc;
7833 	}
7834 
7835 	/* Check if MFP enabled */
7836 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
7837 		netdev_info(netdev,
7838 			    "Configuring TC not supported in MFP mode\n");
7839 		return ret;
7840 	}
7841 	switch (mode) {
7842 	case TC_MQPRIO_MODE_DCB:
7843 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7844 
7845 		/* Check if DCB enabled to continue */
7846 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7847 			netdev_info(netdev,
7848 				    "DCB is not enabled for adapter\n");
7849 			return ret;
7850 		}
7851 
7852 		/* Check whether tc count is within enabled limit */
7853 		if (num_tc > i40e_pf_get_num_tc(pf)) {
7854 			netdev_info(netdev,
7855 				    "TC count greater than enabled on link for adapter\n");
7856 			return ret;
7857 		}
7858 		break;
7859 	case TC_MQPRIO_MODE_CHANNEL:
7860 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
7861 			netdev_info(netdev,
7862 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
7863 			return ret;
7864 		}
7865 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7866 			return ret;
7867 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
7868 		if (ret)
7869 			return ret;
7870 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
7871 		       sizeof(*mqprio_qopt));
7872 		pf->flags |= I40E_FLAG_TC_MQPRIO;
7873 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7874 		break;
7875 	default:
7876 		return -EINVAL;
7877 	}
7878 
7879 config_tc:
7880 	/* Generate TC map for number of tc requested */
7881 	for (i = 0; i < num_tc; i++)
7882 		enabled_tc |= BIT(i);
7883 
7884 	/* Requesting same TC configuration as already enabled */
7885 	if (enabled_tc == vsi->tc_config.enabled_tc &&
7886 	    mode != TC_MQPRIO_MODE_CHANNEL)
7887 		return 0;
7888 
7889 	/* Quiesce VSI queues */
7890 	i40e_quiesce_vsi(vsi);
7891 
7892 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
7893 		i40e_remove_queue_channels(vsi);
7894 
7895 	/* Configure VSI for enabled TCs */
7896 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
7897 	if (ret) {
7898 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
7899 			    vsi->seid);
7900 		need_reset = true;
7901 		goto exit;
7902 	} else {
7903 		dev_info(&vsi->back->pdev->dev,
7904 			 "Setup channel (id:%u) utilizing num_queues %d\n",
7905 			 vsi->seid, vsi->tc_config.tc_info[0].qcount);
7906 	}
7907 
7908 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
7909 		if (vsi->mqprio_qopt.max_rate[0]) {
7910 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
7911 
7912 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
7913 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
7914 			if (!ret) {
7915 				u64 credits = max_tx_rate;
7916 
7917 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
7918 				dev_dbg(&vsi->back->pdev->dev,
7919 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
7920 					max_tx_rate,
7921 					credits,
7922 					vsi->seid);
7923 			} else {
7924 				need_reset = true;
7925 				goto exit;
7926 			}
7927 		}
7928 		ret = i40e_configure_queue_channels(vsi);
7929 		if (ret) {
7930 			vsi->num_queue_pairs = old_queue_pairs;
7931 			netdev_info(netdev,
7932 				    "Failed configuring queue channels\n");
7933 			need_reset = true;
7934 			goto exit;
7935 		}
7936 	}
7937 
7938 exit:
7939 	/* Reset the configuration data to defaults, only TC0 is enabled */
7940 	if (need_reset) {
7941 		i40e_vsi_set_default_tc_config(vsi);
7942 		need_reset = false;
7943 	}
7944 
7945 	/* Unquiesce VSI */
7946 	i40e_unquiesce_vsi(vsi);
7947 	return ret;
7948 }
7949 
7950 /**
7951  * i40e_set_cld_element - sets cloud filter element data
7952  * @filter: cloud filter rule
7953  * @cld: ptr to cloud filter element data
7954  *
7955  * This is helper function to copy data into cloud filter element
7956  **/
7957 static inline void
7958 i40e_set_cld_element(struct i40e_cloud_filter *filter,
7959 		     struct i40e_aqc_cloud_filters_element_data *cld)
7960 {
7961 	u32 ipa;
7962 	int i;
7963 
7964 	memset(cld, 0, sizeof(*cld));
7965 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
7966 	ether_addr_copy(cld->inner_mac, filter->src_mac);
7967 
7968 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
7969 		return;
7970 
7971 	if (filter->n_proto == ETH_P_IPV6) {
7972 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
7973 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
7974 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
7975 
7976 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
7977 		}
7978 	} else {
7979 		ipa = be32_to_cpu(filter->dst_ipv4);
7980 
7981 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
7982 	}
7983 
7984 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
7985 
7986 	/* tenant_id is not supported by FW now, once the support is enabled
7987 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
7988 	 */
7989 	if (filter->tenant_id)
7990 		return;
7991 }
7992 
7993 /**
7994  * i40e_add_del_cloud_filter - Add/del cloud filter
7995  * @vsi: pointer to VSI
7996  * @filter: cloud filter rule
7997  * @add: if true, add, if false, delete
7998  *
7999  * Add or delete a cloud filter for a specific flow spec.
8000  * Returns 0 if the filter were successfully added.
8001  **/
8002 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8003 			      struct i40e_cloud_filter *filter, bool add)
8004 {
8005 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8006 	struct i40e_pf *pf = vsi->back;
8007 	int ret;
8008 	static const u16 flag_table[128] = {
8009 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8010 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8011 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8012 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8013 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8014 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8015 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8016 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8017 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8018 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8019 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8020 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8021 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8022 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8023 	};
8024 
8025 	if (filter->flags >= ARRAY_SIZE(flag_table))
8026 		return I40E_ERR_CONFIG;
8027 
8028 	memset(&cld_filter, 0, sizeof(cld_filter));
8029 
8030 	/* copy element needed to add cloud filter from filter */
8031 	i40e_set_cld_element(filter, &cld_filter);
8032 
8033 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8034 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8035 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8036 
8037 	if (filter->n_proto == ETH_P_IPV6)
8038 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8039 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8040 	else
8041 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8042 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8043 
8044 	if (add)
8045 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8046 						&cld_filter, 1);
8047 	else
8048 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8049 						&cld_filter, 1);
8050 	if (ret)
8051 		dev_dbg(&pf->pdev->dev,
8052 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8053 			add ? "add" : "delete", filter->dst_port, ret,
8054 			pf->hw.aq.asq_last_status);
8055 	else
8056 		dev_info(&pf->pdev->dev,
8057 			 "%s cloud filter for VSI: %d\n",
8058 			 add ? "Added" : "Deleted", filter->seid);
8059 	return ret;
8060 }
8061 
8062 /**
8063  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8064  * @vsi: pointer to VSI
8065  * @filter: cloud filter rule
8066  * @add: if true, add, if false, delete
8067  *
8068  * Add or delete a cloud filter for a specific flow spec using big buffer.
8069  * Returns 0 if the filter were successfully added.
8070  **/
8071 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8072 				      struct i40e_cloud_filter *filter,
8073 				      bool add)
8074 {
8075 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8076 	struct i40e_pf *pf = vsi->back;
8077 	int ret;
8078 
8079 	/* Both (src/dst) valid mac_addr are not supported */
8080 	if ((is_valid_ether_addr(filter->dst_mac) &&
8081 	     is_valid_ether_addr(filter->src_mac)) ||
8082 	    (is_multicast_ether_addr(filter->dst_mac) &&
8083 	     is_multicast_ether_addr(filter->src_mac)))
8084 		return -EOPNOTSUPP;
8085 
8086 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8087 	 * ports are not supported via big buffer now.
8088 	 */
8089 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8090 		return -EOPNOTSUPP;
8091 
8092 	/* adding filter using src_port/src_ip is not supported at this stage */
8093 	if (filter->src_port ||
8094 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8095 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8096 		return -EOPNOTSUPP;
8097 
8098 	memset(&cld_filter, 0, sizeof(cld_filter));
8099 
8100 	/* copy element needed to add cloud filter from filter */
8101 	i40e_set_cld_element(filter, &cld_filter.element);
8102 
8103 	if (is_valid_ether_addr(filter->dst_mac) ||
8104 	    is_valid_ether_addr(filter->src_mac) ||
8105 	    is_multicast_ether_addr(filter->dst_mac) ||
8106 	    is_multicast_ether_addr(filter->src_mac)) {
8107 		/* MAC + IP : unsupported mode */
8108 		if (filter->dst_ipv4)
8109 			return -EOPNOTSUPP;
8110 
8111 		/* since we validated that L4 port must be valid before
8112 		 * we get here, start with respective "flags" value
8113 		 * and update if vlan is present or not
8114 		 */
8115 		cld_filter.element.flags =
8116 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8117 
8118 		if (filter->vlan_id) {
8119 			cld_filter.element.flags =
8120 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8121 		}
8122 
8123 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8124 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8125 		cld_filter.element.flags =
8126 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8127 		if (filter->n_proto == ETH_P_IPV6)
8128 			cld_filter.element.flags |=
8129 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8130 		else
8131 			cld_filter.element.flags |=
8132 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8133 	} else {
8134 		dev_err(&pf->pdev->dev,
8135 			"either mac or ip has to be valid for cloud filter\n");
8136 		return -EINVAL;
8137 	}
8138 
8139 	/* Now copy L4 port in Byte 6..7 in general fields */
8140 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8141 						be16_to_cpu(filter->dst_port);
8142 
8143 	if (add) {
8144 		/* Validate current device switch mode, change if necessary */
8145 		ret = i40e_validate_and_set_switch_mode(vsi);
8146 		if (ret) {
8147 			dev_err(&pf->pdev->dev,
8148 				"failed to set switch mode, ret %d\n",
8149 				ret);
8150 			return ret;
8151 		}
8152 
8153 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8154 						   &cld_filter, 1);
8155 	} else {
8156 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8157 						   &cld_filter, 1);
8158 	}
8159 
8160 	if (ret)
8161 		dev_dbg(&pf->pdev->dev,
8162 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8163 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8164 	else
8165 		dev_info(&pf->pdev->dev,
8166 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8167 			 add ? "add" : "delete", filter->seid,
8168 			 ntohs(filter->dst_port));
8169 	return ret;
8170 }
8171 
8172 /**
8173  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8174  * @vsi: Pointer to VSI
8175  * @f: Pointer to struct flow_cls_offload
8176  * @filter: Pointer to cloud filter structure
8177  *
8178  **/
8179 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8180 				 struct flow_cls_offload *f,
8181 				 struct i40e_cloud_filter *filter)
8182 {
8183 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8184 	struct flow_dissector *dissector = rule->match.dissector;
8185 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8186 	struct i40e_pf *pf = vsi->back;
8187 	u8 field_flags = 0;
8188 
8189 	if (dissector->used_keys &
8190 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8191 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8192 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8193 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8194 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8195 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8196 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8197 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8198 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8199 			dissector->used_keys);
8200 		return -EOPNOTSUPP;
8201 	}
8202 
8203 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8204 		struct flow_match_enc_keyid match;
8205 
8206 		flow_rule_match_enc_keyid(rule, &match);
8207 		if (match.mask->keyid != 0)
8208 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8209 
8210 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8211 	}
8212 
8213 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8214 		struct flow_match_basic match;
8215 
8216 		flow_rule_match_basic(rule, &match);
8217 		n_proto_key = ntohs(match.key->n_proto);
8218 		n_proto_mask = ntohs(match.mask->n_proto);
8219 
8220 		if (n_proto_key == ETH_P_ALL) {
8221 			n_proto_key = 0;
8222 			n_proto_mask = 0;
8223 		}
8224 		filter->n_proto = n_proto_key & n_proto_mask;
8225 		filter->ip_proto = match.key->ip_proto;
8226 	}
8227 
8228 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8229 		struct flow_match_eth_addrs match;
8230 
8231 		flow_rule_match_eth_addrs(rule, &match);
8232 
8233 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8234 		if (!is_zero_ether_addr(match.mask->dst)) {
8235 			if (is_broadcast_ether_addr(match.mask->dst)) {
8236 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8237 			} else {
8238 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8239 					match.mask->dst);
8240 				return I40E_ERR_CONFIG;
8241 			}
8242 		}
8243 
8244 		if (!is_zero_ether_addr(match.mask->src)) {
8245 			if (is_broadcast_ether_addr(match.mask->src)) {
8246 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8247 			} else {
8248 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8249 					match.mask->src);
8250 				return I40E_ERR_CONFIG;
8251 			}
8252 		}
8253 		ether_addr_copy(filter->dst_mac, match.key->dst);
8254 		ether_addr_copy(filter->src_mac, match.key->src);
8255 	}
8256 
8257 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8258 		struct flow_match_vlan match;
8259 
8260 		flow_rule_match_vlan(rule, &match);
8261 		if (match.mask->vlan_id) {
8262 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8263 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8264 
8265 			} else {
8266 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8267 					match.mask->vlan_id);
8268 				return I40E_ERR_CONFIG;
8269 			}
8270 		}
8271 
8272 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8273 	}
8274 
8275 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8276 		struct flow_match_control match;
8277 
8278 		flow_rule_match_control(rule, &match);
8279 		addr_type = match.key->addr_type;
8280 	}
8281 
8282 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8283 		struct flow_match_ipv4_addrs match;
8284 
8285 		flow_rule_match_ipv4_addrs(rule, &match);
8286 		if (match.mask->dst) {
8287 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8288 				field_flags |= I40E_CLOUD_FIELD_IIP;
8289 			} else {
8290 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8291 					&match.mask->dst);
8292 				return I40E_ERR_CONFIG;
8293 			}
8294 		}
8295 
8296 		if (match.mask->src) {
8297 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8298 				field_flags |= I40E_CLOUD_FIELD_IIP;
8299 			} else {
8300 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8301 					&match.mask->src);
8302 				return I40E_ERR_CONFIG;
8303 			}
8304 		}
8305 
8306 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8307 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8308 			return I40E_ERR_CONFIG;
8309 		}
8310 		filter->dst_ipv4 = match.key->dst;
8311 		filter->src_ipv4 = match.key->src;
8312 	}
8313 
8314 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8315 		struct flow_match_ipv6_addrs match;
8316 
8317 		flow_rule_match_ipv6_addrs(rule, &match);
8318 
8319 		/* src and dest IPV6 address should not be LOOPBACK
8320 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8321 		 */
8322 		if (ipv6_addr_loopback(&match.key->dst) ||
8323 		    ipv6_addr_loopback(&match.key->src)) {
8324 			dev_err(&pf->pdev->dev,
8325 				"Bad ipv6, addr is LOOPBACK\n");
8326 			return I40E_ERR_CONFIG;
8327 		}
8328 		if (!ipv6_addr_any(&match.mask->dst) ||
8329 		    !ipv6_addr_any(&match.mask->src))
8330 			field_flags |= I40E_CLOUD_FIELD_IIP;
8331 
8332 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8333 		       sizeof(filter->src_ipv6));
8334 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8335 		       sizeof(filter->dst_ipv6));
8336 	}
8337 
8338 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8339 		struct flow_match_ports match;
8340 
8341 		flow_rule_match_ports(rule, &match);
8342 		if (match.mask->src) {
8343 			if (match.mask->src == cpu_to_be16(0xffff)) {
8344 				field_flags |= I40E_CLOUD_FIELD_IIP;
8345 			} else {
8346 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8347 					be16_to_cpu(match.mask->src));
8348 				return I40E_ERR_CONFIG;
8349 			}
8350 		}
8351 
8352 		if (match.mask->dst) {
8353 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8354 				field_flags |= I40E_CLOUD_FIELD_IIP;
8355 			} else {
8356 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8357 					be16_to_cpu(match.mask->dst));
8358 				return I40E_ERR_CONFIG;
8359 			}
8360 		}
8361 
8362 		filter->dst_port = match.key->dst;
8363 		filter->src_port = match.key->src;
8364 
8365 		switch (filter->ip_proto) {
8366 		case IPPROTO_TCP:
8367 		case IPPROTO_UDP:
8368 			break;
8369 		default:
8370 			dev_err(&pf->pdev->dev,
8371 				"Only UDP and TCP transport are supported\n");
8372 			return -EINVAL;
8373 		}
8374 	}
8375 	filter->flags = field_flags;
8376 	return 0;
8377 }
8378 
8379 /**
8380  * i40e_handle_tclass: Forward to a traffic class on the device
8381  * @vsi: Pointer to VSI
8382  * @tc: traffic class index on the device
8383  * @filter: Pointer to cloud filter structure
8384  *
8385  **/
8386 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8387 			      struct i40e_cloud_filter *filter)
8388 {
8389 	struct i40e_channel *ch, *ch_tmp;
8390 
8391 	/* direct to a traffic class on the same device */
8392 	if (tc == 0) {
8393 		filter->seid = vsi->seid;
8394 		return 0;
8395 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8396 		if (!filter->dst_port) {
8397 			dev_err(&vsi->back->pdev->dev,
8398 				"Specify destination port to direct to traffic class that is not default\n");
8399 			return -EINVAL;
8400 		}
8401 		if (list_empty(&vsi->ch_list))
8402 			return -EINVAL;
8403 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8404 					 list) {
8405 			if (ch->seid == vsi->tc_seid_map[tc])
8406 				filter->seid = ch->seid;
8407 		}
8408 		return 0;
8409 	}
8410 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8411 	return -EINVAL;
8412 }
8413 
8414 /**
8415  * i40e_configure_clsflower - Configure tc flower filters
8416  * @vsi: Pointer to VSI
8417  * @cls_flower: Pointer to struct flow_cls_offload
8418  *
8419  **/
8420 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8421 				    struct flow_cls_offload *cls_flower)
8422 {
8423 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8424 	struct i40e_cloud_filter *filter = NULL;
8425 	struct i40e_pf *pf = vsi->back;
8426 	int err = 0;
8427 
8428 	if (tc < 0) {
8429 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8430 		return -EOPNOTSUPP;
8431 	}
8432 
8433 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8434 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8435 		return -EBUSY;
8436 
8437 	if (pf->fdir_pf_active_filters ||
8438 	    (!hlist_empty(&pf->fdir_filter_list))) {
8439 		dev_err(&vsi->back->pdev->dev,
8440 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8441 		return -EINVAL;
8442 	}
8443 
8444 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8445 		dev_err(&vsi->back->pdev->dev,
8446 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8447 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8448 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8449 	}
8450 
8451 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8452 	if (!filter)
8453 		return -ENOMEM;
8454 
8455 	filter->cookie = cls_flower->cookie;
8456 
8457 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8458 	if (err < 0)
8459 		goto err;
8460 
8461 	err = i40e_handle_tclass(vsi, tc, filter);
8462 	if (err < 0)
8463 		goto err;
8464 
8465 	/* Add cloud filter */
8466 	if (filter->dst_port)
8467 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8468 	else
8469 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8470 
8471 	if (err) {
8472 		dev_err(&pf->pdev->dev,
8473 			"Failed to add cloud filter, err %s\n",
8474 			i40e_stat_str(&pf->hw, err));
8475 		goto err;
8476 	}
8477 
8478 	/* add filter to the ordered list */
8479 	INIT_HLIST_NODE(&filter->cloud_node);
8480 
8481 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8482 
8483 	pf->num_cloud_filters++;
8484 
8485 	return err;
8486 err:
8487 	kfree(filter);
8488 	return err;
8489 }
8490 
8491 /**
8492  * i40e_find_cloud_filter - Find the could filter in the list
8493  * @vsi: Pointer to VSI
8494  * @cookie: filter specific cookie
8495  *
8496  **/
8497 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8498 							unsigned long *cookie)
8499 {
8500 	struct i40e_cloud_filter *filter = NULL;
8501 	struct hlist_node *node2;
8502 
8503 	hlist_for_each_entry_safe(filter, node2,
8504 				  &vsi->back->cloud_filter_list, cloud_node)
8505 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8506 			return filter;
8507 	return NULL;
8508 }
8509 
8510 /**
8511  * i40e_delete_clsflower - Remove tc flower filters
8512  * @vsi: Pointer to VSI
8513  * @cls_flower: Pointer to struct flow_cls_offload
8514  *
8515  **/
8516 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8517 				 struct flow_cls_offload *cls_flower)
8518 {
8519 	struct i40e_cloud_filter *filter = NULL;
8520 	struct i40e_pf *pf = vsi->back;
8521 	int err = 0;
8522 
8523 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8524 
8525 	if (!filter)
8526 		return -EINVAL;
8527 
8528 	hash_del(&filter->cloud_node);
8529 
8530 	if (filter->dst_port)
8531 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8532 	else
8533 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8534 
8535 	kfree(filter);
8536 	if (err) {
8537 		dev_err(&pf->pdev->dev,
8538 			"Failed to delete cloud filter, err %s\n",
8539 			i40e_stat_str(&pf->hw, err));
8540 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8541 	}
8542 
8543 	pf->num_cloud_filters--;
8544 	if (!pf->num_cloud_filters)
8545 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8546 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8547 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8548 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8549 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8550 		}
8551 	return 0;
8552 }
8553 
8554 /**
8555  * i40e_setup_tc_cls_flower - flower classifier offloads
8556  * @np: net device to configure
8557  * @cls_flower: offload data
8558  **/
8559 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8560 				    struct flow_cls_offload *cls_flower)
8561 {
8562 	struct i40e_vsi *vsi = np->vsi;
8563 
8564 	switch (cls_flower->command) {
8565 	case FLOW_CLS_REPLACE:
8566 		return i40e_configure_clsflower(vsi, cls_flower);
8567 	case FLOW_CLS_DESTROY:
8568 		return i40e_delete_clsflower(vsi, cls_flower);
8569 	case FLOW_CLS_STATS:
8570 		return -EOPNOTSUPP;
8571 	default:
8572 		return -EOPNOTSUPP;
8573 	}
8574 }
8575 
8576 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8577 				  void *cb_priv)
8578 {
8579 	struct i40e_netdev_priv *np = cb_priv;
8580 
8581 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8582 		return -EOPNOTSUPP;
8583 
8584 	switch (type) {
8585 	case TC_SETUP_CLSFLOWER:
8586 		return i40e_setup_tc_cls_flower(np, type_data);
8587 
8588 	default:
8589 		return -EOPNOTSUPP;
8590 	}
8591 }
8592 
8593 static LIST_HEAD(i40e_block_cb_list);
8594 
8595 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8596 			   void *type_data)
8597 {
8598 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8599 
8600 	switch (type) {
8601 	case TC_SETUP_QDISC_MQPRIO:
8602 		return i40e_setup_tc(netdev, type_data);
8603 	case TC_SETUP_BLOCK:
8604 		return flow_block_cb_setup_simple(type_data,
8605 						  &i40e_block_cb_list,
8606 						  i40e_setup_tc_block_cb,
8607 						  np, np, true);
8608 	default:
8609 		return -EOPNOTSUPP;
8610 	}
8611 }
8612 
8613 /**
8614  * i40e_open - Called when a network interface is made active
8615  * @netdev: network interface device structure
8616  *
8617  * The open entry point is called when a network interface is made
8618  * active by the system (IFF_UP).  At this point all resources needed
8619  * for transmit and receive operations are allocated, the interrupt
8620  * handler is registered with the OS, the netdev watchdog subtask is
8621  * enabled, and the stack is notified that the interface is ready.
8622  *
8623  * Returns 0 on success, negative value on failure
8624  **/
8625 int i40e_open(struct net_device *netdev)
8626 {
8627 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8628 	struct i40e_vsi *vsi = np->vsi;
8629 	struct i40e_pf *pf = vsi->back;
8630 	int err;
8631 
8632 	/* disallow open during test or if eeprom is broken */
8633 	if (test_bit(__I40E_TESTING, pf->state) ||
8634 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8635 		return -EBUSY;
8636 
8637 	netif_carrier_off(netdev);
8638 
8639 	if (i40e_force_link_state(pf, true))
8640 		return -EAGAIN;
8641 
8642 	err = i40e_vsi_open(vsi);
8643 	if (err)
8644 		return err;
8645 
8646 	/* configure global TSO hardware offload settings */
8647 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8648 						       TCP_FLAG_FIN) >> 16);
8649 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8650 						       TCP_FLAG_FIN |
8651 						       TCP_FLAG_CWR) >> 16);
8652 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8653 	udp_tunnel_get_rx_info(netdev);
8654 
8655 	return 0;
8656 }
8657 
8658 /**
8659  * i40e_vsi_open -
8660  * @vsi: the VSI to open
8661  *
8662  * Finish initialization of the VSI.
8663  *
8664  * Returns 0 on success, negative value on failure
8665  *
8666  * Note: expects to be called while under rtnl_lock()
8667  **/
8668 int i40e_vsi_open(struct i40e_vsi *vsi)
8669 {
8670 	struct i40e_pf *pf = vsi->back;
8671 	char int_name[I40E_INT_NAME_STR_LEN];
8672 	int err;
8673 
8674 	/* allocate descriptors */
8675 	err = i40e_vsi_setup_tx_resources(vsi);
8676 	if (err)
8677 		goto err_setup_tx;
8678 	err = i40e_vsi_setup_rx_resources(vsi);
8679 	if (err)
8680 		goto err_setup_rx;
8681 
8682 	err = i40e_vsi_configure(vsi);
8683 	if (err)
8684 		goto err_setup_rx;
8685 
8686 	if (vsi->netdev) {
8687 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
8688 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
8689 		err = i40e_vsi_request_irq(vsi, int_name);
8690 		if (err)
8691 			goto err_setup_rx;
8692 
8693 		/* Notify the stack of the actual queue counts. */
8694 		err = netif_set_real_num_tx_queues(vsi->netdev,
8695 						   vsi->num_queue_pairs);
8696 		if (err)
8697 			goto err_set_queues;
8698 
8699 		err = netif_set_real_num_rx_queues(vsi->netdev,
8700 						   vsi->num_queue_pairs);
8701 		if (err)
8702 			goto err_set_queues;
8703 
8704 	} else if (vsi->type == I40E_VSI_FDIR) {
8705 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
8706 			 dev_driver_string(&pf->pdev->dev),
8707 			 dev_name(&pf->pdev->dev));
8708 		err = i40e_vsi_request_irq(vsi, int_name);
8709 		if (err)
8710 			goto err_setup_rx;
8711 
8712 	} else {
8713 		err = -EINVAL;
8714 		goto err_setup_rx;
8715 	}
8716 
8717 	err = i40e_up_complete(vsi);
8718 	if (err)
8719 		goto err_up_complete;
8720 
8721 	return 0;
8722 
8723 err_up_complete:
8724 	i40e_down(vsi);
8725 err_set_queues:
8726 	i40e_vsi_free_irq(vsi);
8727 err_setup_rx:
8728 	i40e_vsi_free_rx_resources(vsi);
8729 err_setup_tx:
8730 	i40e_vsi_free_tx_resources(vsi);
8731 	if (vsi == pf->vsi[pf->lan_vsi])
8732 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
8733 
8734 	return err;
8735 }
8736 
8737 /**
8738  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
8739  * @pf: Pointer to PF
8740  *
8741  * This function destroys the hlist where all the Flow Director
8742  * filters were saved.
8743  **/
8744 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
8745 {
8746 	struct i40e_fdir_filter *filter;
8747 	struct i40e_flex_pit *pit_entry, *tmp;
8748 	struct hlist_node *node2;
8749 
8750 	hlist_for_each_entry_safe(filter, node2,
8751 				  &pf->fdir_filter_list, fdir_node) {
8752 		hlist_del(&filter->fdir_node);
8753 		kfree(filter);
8754 	}
8755 
8756 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
8757 		list_del(&pit_entry->list);
8758 		kfree(pit_entry);
8759 	}
8760 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
8761 
8762 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
8763 		list_del(&pit_entry->list);
8764 		kfree(pit_entry);
8765 	}
8766 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
8767 
8768 	pf->fdir_pf_active_filters = 0;
8769 	i40e_reset_fdir_filter_cnt(pf);
8770 
8771 	/* Reprogram the default input set for TCP/IPv4 */
8772 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8773 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8774 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8775 
8776 	/* Reprogram the default input set for TCP/IPv6 */
8777 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
8778 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8779 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8780 
8781 	/* Reprogram the default input set for UDP/IPv4 */
8782 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
8783 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8784 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8785 
8786 	/* Reprogram the default input set for UDP/IPv6 */
8787 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
8788 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8789 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8790 
8791 	/* Reprogram the default input set for SCTP/IPv4 */
8792 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
8793 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8794 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8795 
8796 	/* Reprogram the default input set for SCTP/IPv6 */
8797 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
8798 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8799 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8800 
8801 	/* Reprogram the default input set for Other/IPv4 */
8802 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
8803 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8804 
8805 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
8806 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8807 
8808 	/* Reprogram the default input set for Other/IPv6 */
8809 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
8810 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8811 
8812 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
8813 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8814 }
8815 
8816 /**
8817  * i40e_cloud_filter_exit - Cleans up the cloud filters
8818  * @pf: Pointer to PF
8819  *
8820  * This function destroys the hlist where all the cloud filters
8821  * were saved.
8822  **/
8823 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
8824 {
8825 	struct i40e_cloud_filter *cfilter;
8826 	struct hlist_node *node;
8827 
8828 	hlist_for_each_entry_safe(cfilter, node,
8829 				  &pf->cloud_filter_list, cloud_node) {
8830 		hlist_del(&cfilter->cloud_node);
8831 		kfree(cfilter);
8832 	}
8833 	pf->num_cloud_filters = 0;
8834 
8835 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8836 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8837 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8838 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8839 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8840 	}
8841 }
8842 
8843 /**
8844  * i40e_close - Disables a network interface
8845  * @netdev: network interface device structure
8846  *
8847  * The close entry point is called when an interface is de-activated
8848  * by the OS.  The hardware is still under the driver's control, but
8849  * this netdev interface is disabled.
8850  *
8851  * Returns 0, this is not allowed to fail
8852  **/
8853 int i40e_close(struct net_device *netdev)
8854 {
8855 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8856 	struct i40e_vsi *vsi = np->vsi;
8857 
8858 	i40e_vsi_close(vsi);
8859 
8860 	return 0;
8861 }
8862 
8863 /**
8864  * i40e_do_reset - Start a PF or Core Reset sequence
8865  * @pf: board private structure
8866  * @reset_flags: which reset is requested
8867  * @lock_acquired: indicates whether or not the lock has been acquired
8868  * before this function was called.
8869  *
8870  * The essential difference in resets is that the PF Reset
8871  * doesn't clear the packet buffers, doesn't reset the PE
8872  * firmware, and doesn't bother the other PFs on the chip.
8873  **/
8874 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
8875 {
8876 	u32 val;
8877 
8878 	/* do the biggest reset indicated */
8879 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
8880 
8881 		/* Request a Global Reset
8882 		 *
8883 		 * This will start the chip's countdown to the actual full
8884 		 * chip reset event, and a warning interrupt to be sent
8885 		 * to all PFs, including the requestor.  Our handler
8886 		 * for the warning interrupt will deal with the shutdown
8887 		 * and recovery of the switch setup.
8888 		 */
8889 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
8890 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8891 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
8892 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
8893 
8894 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
8895 
8896 		/* Request a Core Reset
8897 		 *
8898 		 * Same as Global Reset, except does *not* include the MAC/PHY
8899 		 */
8900 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
8901 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8902 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
8903 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
8904 		i40e_flush(&pf->hw);
8905 
8906 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
8907 
8908 		/* Request a PF Reset
8909 		 *
8910 		 * Resets only the PF-specific registers
8911 		 *
8912 		 * This goes directly to the tear-down and rebuild of
8913 		 * the switch, since we need to do all the recovery as
8914 		 * for the Core Reset.
8915 		 */
8916 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
8917 		i40e_handle_reset_warning(pf, lock_acquired);
8918 
8919 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
8920 		/* Request a PF Reset
8921 		 *
8922 		 * Resets PF and reinitializes PFs VSI.
8923 		 */
8924 		i40e_prep_for_reset(pf);
8925 		i40e_reset_and_rebuild(pf, true, lock_acquired);
8926 		dev_info(&pf->pdev->dev,
8927 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
8928 			 "FW LLDP is disabled\n" :
8929 			 "FW LLDP is enabled\n");
8930 
8931 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
8932 		int v;
8933 
8934 		/* Find the VSI(s) that requested a re-init */
8935 		dev_info(&pf->pdev->dev,
8936 			 "VSI reinit requested\n");
8937 		for (v = 0; v < pf->num_alloc_vsi; v++) {
8938 			struct i40e_vsi *vsi = pf->vsi[v];
8939 
8940 			if (vsi != NULL &&
8941 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
8942 					       vsi->state))
8943 				i40e_vsi_reinit_locked(pf->vsi[v]);
8944 		}
8945 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
8946 		int v;
8947 
8948 		/* Find the VSI(s) that needs to be brought down */
8949 		dev_info(&pf->pdev->dev, "VSI down requested\n");
8950 		for (v = 0; v < pf->num_alloc_vsi; v++) {
8951 			struct i40e_vsi *vsi = pf->vsi[v];
8952 
8953 			if (vsi != NULL &&
8954 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
8955 					       vsi->state)) {
8956 				set_bit(__I40E_VSI_DOWN, vsi->state);
8957 				i40e_down(vsi);
8958 			}
8959 		}
8960 	} else {
8961 		dev_info(&pf->pdev->dev,
8962 			 "bad reset request 0x%08x\n", reset_flags);
8963 	}
8964 }
8965 
8966 #ifdef CONFIG_I40E_DCB
8967 /**
8968  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
8969  * @pf: board private structure
8970  * @old_cfg: current DCB config
8971  * @new_cfg: new DCB config
8972  **/
8973 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
8974 			    struct i40e_dcbx_config *old_cfg,
8975 			    struct i40e_dcbx_config *new_cfg)
8976 {
8977 	bool need_reconfig = false;
8978 
8979 	/* Check if ETS configuration has changed */
8980 	if (memcmp(&new_cfg->etscfg,
8981 		   &old_cfg->etscfg,
8982 		   sizeof(new_cfg->etscfg))) {
8983 		/* If Priority Table has changed reconfig is needed */
8984 		if (memcmp(&new_cfg->etscfg.prioritytable,
8985 			   &old_cfg->etscfg.prioritytable,
8986 			   sizeof(new_cfg->etscfg.prioritytable))) {
8987 			need_reconfig = true;
8988 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
8989 		}
8990 
8991 		if (memcmp(&new_cfg->etscfg.tcbwtable,
8992 			   &old_cfg->etscfg.tcbwtable,
8993 			   sizeof(new_cfg->etscfg.tcbwtable)))
8994 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
8995 
8996 		if (memcmp(&new_cfg->etscfg.tsatable,
8997 			   &old_cfg->etscfg.tsatable,
8998 			   sizeof(new_cfg->etscfg.tsatable)))
8999 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9000 	}
9001 
9002 	/* Check if PFC configuration has changed */
9003 	if (memcmp(&new_cfg->pfc,
9004 		   &old_cfg->pfc,
9005 		   sizeof(new_cfg->pfc))) {
9006 		need_reconfig = true;
9007 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9008 	}
9009 
9010 	/* Check if APP Table has changed */
9011 	if (memcmp(&new_cfg->app,
9012 		   &old_cfg->app,
9013 		   sizeof(new_cfg->app))) {
9014 		need_reconfig = true;
9015 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9016 	}
9017 
9018 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9019 	return need_reconfig;
9020 }
9021 
9022 /**
9023  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9024  * @pf: board private structure
9025  * @e: event info posted on ARQ
9026  **/
9027 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9028 				  struct i40e_arq_event_info *e)
9029 {
9030 	struct i40e_aqc_lldp_get_mib *mib =
9031 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9032 	struct i40e_hw *hw = &pf->hw;
9033 	struct i40e_dcbx_config tmp_dcbx_cfg;
9034 	bool need_reconfig = false;
9035 	int ret = 0;
9036 	u8 type;
9037 
9038 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9039 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9040 	    (hw->phy.link_info.link_speed &
9041 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9042 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9043 		/* let firmware decide if the DCB should be disabled */
9044 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9045 
9046 	/* Not DCB capable or capability disabled */
9047 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9048 		return ret;
9049 
9050 	/* Ignore if event is not for Nearest Bridge */
9051 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9052 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9053 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9054 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9055 		return ret;
9056 
9057 	/* Check MIB Type and return if event for Remote MIB update */
9058 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9059 	dev_dbg(&pf->pdev->dev,
9060 		"LLDP event mib type %s\n", type ? "remote" : "local");
9061 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9062 		/* Update the remote cached instance and return */
9063 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9064 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9065 				&hw->remote_dcbx_config);
9066 		goto exit;
9067 	}
9068 
9069 	/* Store the old configuration */
9070 	tmp_dcbx_cfg = hw->local_dcbx_config;
9071 
9072 	/* Reset the old DCBx configuration data */
9073 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9074 	/* Get updated DCBX data from firmware */
9075 	ret = i40e_get_dcb_config(&pf->hw);
9076 	if (ret) {
9077 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9078 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9079 		    (hw->phy.link_info.link_speed &
9080 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9081 			dev_warn(&pf->pdev->dev,
9082 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9083 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9084 		} else {
9085 			dev_info(&pf->pdev->dev,
9086 				 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
9087 				 i40e_stat_str(&pf->hw, ret),
9088 				 i40e_aq_str(&pf->hw,
9089 					     pf->hw.aq.asq_last_status));
9090 		}
9091 		goto exit;
9092 	}
9093 
9094 	/* No change detected in DCBX configs */
9095 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9096 		    sizeof(tmp_dcbx_cfg))) {
9097 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9098 		goto exit;
9099 	}
9100 
9101 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9102 					       &hw->local_dcbx_config);
9103 
9104 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9105 
9106 	if (!need_reconfig)
9107 		goto exit;
9108 
9109 	/* Enable DCB tagging only when more than one TC */
9110 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9111 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9112 	else
9113 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9114 
9115 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9116 	/* Reconfiguration needed quiesce all VSIs */
9117 	i40e_pf_quiesce_all_vsi(pf);
9118 
9119 	/* Changes in configuration update VEB/VSI */
9120 	i40e_dcb_reconfigure(pf);
9121 
9122 	ret = i40e_resume_port_tx(pf);
9123 
9124 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9125 	/* In case of error no point in resuming VSIs */
9126 	if (ret)
9127 		goto exit;
9128 
9129 	/* Wait for the PF's queues to be disabled */
9130 	ret = i40e_pf_wait_queues_disabled(pf);
9131 	if (ret) {
9132 		/* Schedule PF reset to recover */
9133 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9134 		i40e_service_event_schedule(pf);
9135 	} else {
9136 		i40e_pf_unquiesce_all_vsi(pf);
9137 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9138 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9139 	}
9140 
9141 exit:
9142 	return ret;
9143 }
9144 #endif /* CONFIG_I40E_DCB */
9145 
9146 /**
9147  * i40e_do_reset_safe - Protected reset path for userland calls.
9148  * @pf: board private structure
9149  * @reset_flags: which reset is requested
9150  *
9151  **/
9152 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9153 {
9154 	rtnl_lock();
9155 	i40e_do_reset(pf, reset_flags, true);
9156 	rtnl_unlock();
9157 }
9158 
9159 /**
9160  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9161  * @pf: board private structure
9162  * @e: event info posted on ARQ
9163  *
9164  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9165  * and VF queues
9166  **/
9167 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9168 					   struct i40e_arq_event_info *e)
9169 {
9170 	struct i40e_aqc_lan_overflow *data =
9171 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9172 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9173 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9174 	struct i40e_hw *hw = &pf->hw;
9175 	struct i40e_vf *vf;
9176 	u16 vf_id;
9177 
9178 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9179 		queue, qtx_ctl);
9180 
9181 	/* Queue belongs to VF, find the VF and issue VF reset */
9182 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9183 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9184 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9185 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9186 		vf_id -= hw->func_caps.vf_base_id;
9187 		vf = &pf->vf[vf_id];
9188 		i40e_vc_notify_vf_reset(vf);
9189 		/* Allow VF to process pending reset notification */
9190 		msleep(20);
9191 		i40e_reset_vf(vf, false);
9192 	}
9193 }
9194 
9195 /**
9196  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9197  * @pf: board private structure
9198  **/
9199 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9200 {
9201 	u32 val, fcnt_prog;
9202 
9203 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9204 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9205 	return fcnt_prog;
9206 }
9207 
9208 /**
9209  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9210  * @pf: board private structure
9211  **/
9212 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9213 {
9214 	u32 val, fcnt_prog;
9215 
9216 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9217 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9218 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9219 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9220 	return fcnt_prog;
9221 }
9222 
9223 /**
9224  * i40e_get_global_fd_count - Get total FD filters programmed on device
9225  * @pf: board private structure
9226  **/
9227 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9228 {
9229 	u32 val, fcnt_prog;
9230 
9231 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9232 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9233 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9234 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9235 	return fcnt_prog;
9236 }
9237 
9238 /**
9239  * i40e_reenable_fdir_sb - Restore FDir SB capability
9240  * @pf: board private structure
9241  **/
9242 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9243 {
9244 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9245 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9246 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9247 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9248 }
9249 
9250 /**
9251  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9252  * @pf: board private structure
9253  **/
9254 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9255 {
9256 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9257 		/* ATR uses the same filtering logic as SB rules. It only
9258 		 * functions properly if the input set mask is at the default
9259 		 * settings. It is safe to restore the default input set
9260 		 * because there are no active TCPv4 filter rules.
9261 		 */
9262 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9263 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9264 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9265 
9266 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9267 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9268 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9269 	}
9270 }
9271 
9272 /**
9273  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9274  * @pf: board private structure
9275  * @filter: FDir filter to remove
9276  */
9277 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9278 				       struct i40e_fdir_filter *filter)
9279 {
9280 	/* Update counters */
9281 	pf->fdir_pf_active_filters--;
9282 	pf->fd_inv = 0;
9283 
9284 	switch (filter->flow_type) {
9285 	case TCP_V4_FLOW:
9286 		pf->fd_tcp4_filter_cnt--;
9287 		break;
9288 	case UDP_V4_FLOW:
9289 		pf->fd_udp4_filter_cnt--;
9290 		break;
9291 	case SCTP_V4_FLOW:
9292 		pf->fd_sctp4_filter_cnt--;
9293 		break;
9294 	case TCP_V6_FLOW:
9295 		pf->fd_tcp6_filter_cnt--;
9296 		break;
9297 	case UDP_V6_FLOW:
9298 		pf->fd_udp6_filter_cnt--;
9299 		break;
9300 	case SCTP_V6_FLOW:
9301 		pf->fd_udp6_filter_cnt--;
9302 		break;
9303 	case IP_USER_FLOW:
9304 		switch (filter->ipl4_proto) {
9305 		case IPPROTO_TCP:
9306 			pf->fd_tcp4_filter_cnt--;
9307 			break;
9308 		case IPPROTO_UDP:
9309 			pf->fd_udp4_filter_cnt--;
9310 			break;
9311 		case IPPROTO_SCTP:
9312 			pf->fd_sctp4_filter_cnt--;
9313 			break;
9314 		case IPPROTO_IP:
9315 			pf->fd_ip4_filter_cnt--;
9316 			break;
9317 		}
9318 		break;
9319 	case IPV6_USER_FLOW:
9320 		switch (filter->ipl4_proto) {
9321 		case IPPROTO_TCP:
9322 			pf->fd_tcp6_filter_cnt--;
9323 			break;
9324 		case IPPROTO_UDP:
9325 			pf->fd_udp6_filter_cnt--;
9326 			break;
9327 		case IPPROTO_SCTP:
9328 			pf->fd_sctp6_filter_cnt--;
9329 			break;
9330 		case IPPROTO_IP:
9331 			pf->fd_ip6_filter_cnt--;
9332 			break;
9333 		}
9334 		break;
9335 	}
9336 
9337 	/* Remove the filter from the list and free memory */
9338 	hlist_del(&filter->fdir_node);
9339 	kfree(filter);
9340 }
9341 
9342 /**
9343  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9344  * @pf: board private structure
9345  **/
9346 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9347 {
9348 	struct i40e_fdir_filter *filter;
9349 	u32 fcnt_prog, fcnt_avail;
9350 	struct hlist_node *node;
9351 
9352 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9353 		return;
9354 
9355 	/* Check if we have enough room to re-enable FDir SB capability. */
9356 	fcnt_prog = i40e_get_global_fd_count(pf);
9357 	fcnt_avail = pf->fdir_pf_filter_count;
9358 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9359 	    (pf->fd_add_err == 0) ||
9360 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9361 		i40e_reenable_fdir_sb(pf);
9362 
9363 	/* We should wait for even more space before re-enabling ATR.
9364 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9365 	 * rules active.
9366 	 */
9367 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9368 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9369 		i40e_reenable_fdir_atr(pf);
9370 
9371 	/* if hw had a problem adding a filter, delete it */
9372 	if (pf->fd_inv > 0) {
9373 		hlist_for_each_entry_safe(filter, node,
9374 					  &pf->fdir_filter_list, fdir_node)
9375 			if (filter->fd_id == pf->fd_inv)
9376 				i40e_delete_invalid_filter(pf, filter);
9377 	}
9378 }
9379 
9380 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9381 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9382 /**
9383  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9384  * @pf: board private structure
9385  **/
9386 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9387 {
9388 	unsigned long min_flush_time;
9389 	int flush_wait_retry = 50;
9390 	bool disable_atr = false;
9391 	int fd_room;
9392 	int reg;
9393 
9394 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9395 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9396 		return;
9397 
9398 	/* If the flush is happening too quick and we have mostly SB rules we
9399 	 * should not re-enable ATR for some time.
9400 	 */
9401 	min_flush_time = pf->fd_flush_timestamp +
9402 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9403 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9404 
9405 	if (!(time_after(jiffies, min_flush_time)) &&
9406 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9407 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9408 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9409 		disable_atr = true;
9410 	}
9411 
9412 	pf->fd_flush_timestamp = jiffies;
9413 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9414 	/* flush all filters */
9415 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9416 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9417 	i40e_flush(&pf->hw);
9418 	pf->fd_flush_cnt++;
9419 	pf->fd_add_err = 0;
9420 	do {
9421 		/* Check FD flush status every 5-6msec */
9422 		usleep_range(5000, 6000);
9423 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9424 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9425 			break;
9426 	} while (flush_wait_retry--);
9427 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9428 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9429 	} else {
9430 		/* replay sideband filters */
9431 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9432 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9433 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9434 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9435 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9436 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9437 	}
9438 }
9439 
9440 /**
9441  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9442  * @pf: board private structure
9443  **/
9444 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9445 {
9446 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9447 }
9448 
9449 /**
9450  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9451  * @pf: board private structure
9452  **/
9453 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9454 {
9455 
9456 	/* if interface is down do nothing */
9457 	if (test_bit(__I40E_DOWN, pf->state))
9458 		return;
9459 
9460 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9461 		i40e_fdir_flush_and_replay(pf);
9462 
9463 	i40e_fdir_check_and_reenable(pf);
9464 
9465 }
9466 
9467 /**
9468  * i40e_vsi_link_event - notify VSI of a link event
9469  * @vsi: vsi to be notified
9470  * @link_up: link up or down
9471  **/
9472 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9473 {
9474 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9475 		return;
9476 
9477 	switch (vsi->type) {
9478 	case I40E_VSI_MAIN:
9479 		if (!vsi->netdev || !vsi->netdev_registered)
9480 			break;
9481 
9482 		if (link_up) {
9483 			netif_carrier_on(vsi->netdev);
9484 			netif_tx_wake_all_queues(vsi->netdev);
9485 		} else {
9486 			netif_carrier_off(vsi->netdev);
9487 			netif_tx_stop_all_queues(vsi->netdev);
9488 		}
9489 		break;
9490 
9491 	case I40E_VSI_SRIOV:
9492 	case I40E_VSI_VMDQ2:
9493 	case I40E_VSI_CTRL:
9494 	case I40E_VSI_IWARP:
9495 	case I40E_VSI_MIRROR:
9496 	default:
9497 		/* there is no notification for other VSIs */
9498 		break;
9499 	}
9500 }
9501 
9502 /**
9503  * i40e_veb_link_event - notify elements on the veb of a link event
9504  * @veb: veb to be notified
9505  * @link_up: link up or down
9506  **/
9507 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9508 {
9509 	struct i40e_pf *pf;
9510 	int i;
9511 
9512 	if (!veb || !veb->pf)
9513 		return;
9514 	pf = veb->pf;
9515 
9516 	/* depth first... */
9517 	for (i = 0; i < I40E_MAX_VEB; i++)
9518 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9519 			i40e_veb_link_event(pf->veb[i], link_up);
9520 
9521 	/* ... now the local VSIs */
9522 	for (i = 0; i < pf->num_alloc_vsi; i++)
9523 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9524 			i40e_vsi_link_event(pf->vsi[i], link_up);
9525 }
9526 
9527 /**
9528  * i40e_link_event - Update netif_carrier status
9529  * @pf: board private structure
9530  **/
9531 static void i40e_link_event(struct i40e_pf *pf)
9532 {
9533 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9534 	u8 new_link_speed, old_link_speed;
9535 	i40e_status status;
9536 	bool new_link, old_link;
9537 #ifdef CONFIG_I40E_DCB
9538 	int err;
9539 #endif /* CONFIG_I40E_DCB */
9540 
9541 	/* set this to force the get_link_status call to refresh state */
9542 	pf->hw.phy.get_link_info = true;
9543 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9544 	status = i40e_get_link_status(&pf->hw, &new_link);
9545 
9546 	/* On success, disable temp link polling */
9547 	if (status == I40E_SUCCESS) {
9548 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9549 	} else {
9550 		/* Enable link polling temporarily until i40e_get_link_status
9551 		 * returns I40E_SUCCESS
9552 		 */
9553 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9554 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9555 			status);
9556 		return;
9557 	}
9558 
9559 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9560 	new_link_speed = pf->hw.phy.link_info.link_speed;
9561 
9562 	if (new_link == old_link &&
9563 	    new_link_speed == old_link_speed &&
9564 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9565 	     new_link == netif_carrier_ok(vsi->netdev)))
9566 		return;
9567 
9568 	i40e_print_link_message(vsi, new_link);
9569 
9570 	/* Notify the base of the switch tree connected to
9571 	 * the link.  Floating VEBs are not notified.
9572 	 */
9573 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9574 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9575 	else
9576 		i40e_vsi_link_event(vsi, new_link);
9577 
9578 	if (pf->vf)
9579 		i40e_vc_notify_link_state(pf);
9580 
9581 	if (pf->flags & I40E_FLAG_PTP)
9582 		i40e_ptp_set_increment(pf);
9583 #ifdef CONFIG_I40E_DCB
9584 	if (new_link == old_link)
9585 		return;
9586 	/* Not SW DCB so firmware will take care of default settings */
9587 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9588 		return;
9589 
9590 	/* We cover here only link down, as after link up in case of SW DCB
9591 	 * SW LLDP agent will take care of setting it up
9592 	 */
9593 	if (!new_link) {
9594 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9595 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9596 		err = i40e_dcb_sw_default_config(pf);
9597 		if (err) {
9598 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9599 				       I40E_FLAG_DCB_ENABLED);
9600 		} else {
9601 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9602 				       DCB_CAP_DCBX_VER_IEEE;
9603 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9604 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9605 		}
9606 	}
9607 #endif /* CONFIG_I40E_DCB */
9608 }
9609 
9610 /**
9611  * i40e_watchdog_subtask - periodic checks not using event driven response
9612  * @pf: board private structure
9613  **/
9614 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9615 {
9616 	int i;
9617 
9618 	/* if interface is down do nothing */
9619 	if (test_bit(__I40E_DOWN, pf->state) ||
9620 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9621 		return;
9622 
9623 	/* make sure we don't do these things too often */
9624 	if (time_before(jiffies, (pf->service_timer_previous +
9625 				  pf->service_timer_period)))
9626 		return;
9627 	pf->service_timer_previous = jiffies;
9628 
9629 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9630 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9631 		i40e_link_event(pf);
9632 
9633 	/* Update the stats for active netdevs so the network stack
9634 	 * can look at updated numbers whenever it cares to
9635 	 */
9636 	for (i = 0; i < pf->num_alloc_vsi; i++)
9637 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9638 			i40e_update_stats(pf->vsi[i]);
9639 
9640 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9641 		/* Update the stats for the active switching components */
9642 		for (i = 0; i < I40E_MAX_VEB; i++)
9643 			if (pf->veb[i])
9644 				i40e_update_veb_stats(pf->veb[i]);
9645 	}
9646 
9647 	i40e_ptp_rx_hang(pf);
9648 	i40e_ptp_tx_hang(pf);
9649 }
9650 
9651 /**
9652  * i40e_reset_subtask - Set up for resetting the device and driver
9653  * @pf: board private structure
9654  **/
9655 static void i40e_reset_subtask(struct i40e_pf *pf)
9656 {
9657 	u32 reset_flags = 0;
9658 
9659 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
9660 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
9661 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
9662 	}
9663 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
9664 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
9665 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9666 	}
9667 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
9668 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
9669 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
9670 	}
9671 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
9672 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
9673 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
9674 	}
9675 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
9676 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
9677 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
9678 	}
9679 
9680 	/* If there's a recovery already waiting, it takes
9681 	 * precedence before starting a new reset sequence.
9682 	 */
9683 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
9684 		i40e_prep_for_reset(pf);
9685 		i40e_reset(pf);
9686 		i40e_rebuild(pf, false, false);
9687 	}
9688 
9689 	/* If we're already down or resetting, just bail */
9690 	if (reset_flags &&
9691 	    !test_bit(__I40E_DOWN, pf->state) &&
9692 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
9693 		i40e_do_reset(pf, reset_flags, false);
9694 	}
9695 }
9696 
9697 /**
9698  * i40e_handle_link_event - Handle link event
9699  * @pf: board private structure
9700  * @e: event info posted on ARQ
9701  **/
9702 static void i40e_handle_link_event(struct i40e_pf *pf,
9703 				   struct i40e_arq_event_info *e)
9704 {
9705 	struct i40e_aqc_get_link_status *status =
9706 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
9707 
9708 	/* Do a new status request to re-enable LSE reporting
9709 	 * and load new status information into the hw struct
9710 	 * This completely ignores any state information
9711 	 * in the ARQ event info, instead choosing to always
9712 	 * issue the AQ update link status command.
9713 	 */
9714 	i40e_link_event(pf);
9715 
9716 	/* Check if module meets thermal requirements */
9717 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
9718 		dev_err(&pf->pdev->dev,
9719 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
9720 		dev_err(&pf->pdev->dev,
9721 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9722 	} else {
9723 		/* check for unqualified module, if link is down, suppress
9724 		 * the message if link was forced to be down.
9725 		 */
9726 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
9727 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
9728 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
9729 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
9730 			dev_err(&pf->pdev->dev,
9731 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
9732 			dev_err(&pf->pdev->dev,
9733 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9734 		}
9735 	}
9736 }
9737 
9738 /**
9739  * i40e_clean_adminq_subtask - Clean the AdminQ rings
9740  * @pf: board private structure
9741  **/
9742 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
9743 {
9744 	struct i40e_arq_event_info event;
9745 	struct i40e_hw *hw = &pf->hw;
9746 	u16 pending, i = 0;
9747 	i40e_status ret;
9748 	u16 opcode;
9749 	u32 oldval;
9750 	u32 val;
9751 
9752 	/* Do not run clean AQ when PF reset fails */
9753 	if (test_bit(__I40E_RESET_FAILED, pf->state))
9754 		return;
9755 
9756 	/* check for error indications */
9757 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
9758 	oldval = val;
9759 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
9760 		if (hw->debug_mask & I40E_DEBUG_AQ)
9761 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
9762 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
9763 	}
9764 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
9765 		if (hw->debug_mask & I40E_DEBUG_AQ)
9766 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
9767 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
9768 		pf->arq_overflows++;
9769 	}
9770 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
9771 		if (hw->debug_mask & I40E_DEBUG_AQ)
9772 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
9773 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
9774 	}
9775 	if (oldval != val)
9776 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
9777 
9778 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
9779 	oldval = val;
9780 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
9781 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9782 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
9783 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
9784 	}
9785 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
9786 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9787 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
9788 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
9789 	}
9790 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
9791 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9792 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
9793 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
9794 	}
9795 	if (oldval != val)
9796 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
9797 
9798 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
9799 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
9800 	if (!event.msg_buf)
9801 		return;
9802 
9803 	do {
9804 		ret = i40e_clean_arq_element(hw, &event, &pending);
9805 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
9806 			break;
9807 		else if (ret) {
9808 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
9809 			break;
9810 		}
9811 
9812 		opcode = le16_to_cpu(event.desc.opcode);
9813 		switch (opcode) {
9814 
9815 		case i40e_aqc_opc_get_link_status:
9816 			rtnl_lock();
9817 			i40e_handle_link_event(pf, &event);
9818 			rtnl_unlock();
9819 			break;
9820 		case i40e_aqc_opc_send_msg_to_pf:
9821 			ret = i40e_vc_process_vf_msg(pf,
9822 					le16_to_cpu(event.desc.retval),
9823 					le32_to_cpu(event.desc.cookie_high),
9824 					le32_to_cpu(event.desc.cookie_low),
9825 					event.msg_buf,
9826 					event.msg_len);
9827 			break;
9828 		case i40e_aqc_opc_lldp_update_mib:
9829 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
9830 #ifdef CONFIG_I40E_DCB
9831 			rtnl_lock();
9832 			i40e_handle_lldp_event(pf, &event);
9833 			rtnl_unlock();
9834 #endif /* CONFIG_I40E_DCB */
9835 			break;
9836 		case i40e_aqc_opc_event_lan_overflow:
9837 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
9838 			i40e_handle_lan_overflow_event(pf, &event);
9839 			break;
9840 		case i40e_aqc_opc_send_msg_to_peer:
9841 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
9842 			break;
9843 		case i40e_aqc_opc_nvm_erase:
9844 		case i40e_aqc_opc_nvm_update:
9845 		case i40e_aqc_opc_oem_post_update:
9846 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
9847 				   "ARQ NVM operation 0x%04x completed\n",
9848 				   opcode);
9849 			break;
9850 		default:
9851 			dev_info(&pf->pdev->dev,
9852 				 "ARQ: Unknown event 0x%04x ignored\n",
9853 				 opcode);
9854 			break;
9855 		}
9856 	} while (i++ < pf->adminq_work_limit);
9857 
9858 	if (i < pf->adminq_work_limit)
9859 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
9860 
9861 	/* re-enable Admin queue interrupt cause */
9862 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
9863 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
9864 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
9865 	i40e_flush(hw);
9866 
9867 	kfree(event.msg_buf);
9868 }
9869 
9870 /**
9871  * i40e_verify_eeprom - make sure eeprom is good to use
9872  * @pf: board private structure
9873  **/
9874 static void i40e_verify_eeprom(struct i40e_pf *pf)
9875 {
9876 	int err;
9877 
9878 	err = i40e_diag_eeprom_test(&pf->hw);
9879 	if (err) {
9880 		/* retry in case of garbage read */
9881 		err = i40e_diag_eeprom_test(&pf->hw);
9882 		if (err) {
9883 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
9884 				 err);
9885 			set_bit(__I40E_BAD_EEPROM, pf->state);
9886 		}
9887 	}
9888 
9889 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
9890 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
9891 		clear_bit(__I40E_BAD_EEPROM, pf->state);
9892 	}
9893 }
9894 
9895 /**
9896  * i40e_enable_pf_switch_lb
9897  * @pf: pointer to the PF structure
9898  *
9899  * enable switch loop back or die - no point in a return value
9900  **/
9901 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
9902 {
9903 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9904 	struct i40e_vsi_context ctxt;
9905 	int ret;
9906 
9907 	ctxt.seid = pf->main_vsi_seid;
9908 	ctxt.pf_num = pf->hw.pf_id;
9909 	ctxt.vf_num = 0;
9910 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9911 	if (ret) {
9912 		dev_info(&pf->pdev->dev,
9913 			 "couldn't get PF vsi config, err %s aq_err %s\n",
9914 			 i40e_stat_str(&pf->hw, ret),
9915 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9916 		return;
9917 	}
9918 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9919 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9920 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9921 
9922 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
9923 	if (ret) {
9924 		dev_info(&pf->pdev->dev,
9925 			 "update vsi switch failed, err %s aq_err %s\n",
9926 			 i40e_stat_str(&pf->hw, ret),
9927 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9928 	}
9929 }
9930 
9931 /**
9932  * i40e_disable_pf_switch_lb
9933  * @pf: pointer to the PF structure
9934  *
9935  * disable switch loop back or die - no point in a return value
9936  **/
9937 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
9938 {
9939 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9940 	struct i40e_vsi_context ctxt;
9941 	int ret;
9942 
9943 	ctxt.seid = pf->main_vsi_seid;
9944 	ctxt.pf_num = pf->hw.pf_id;
9945 	ctxt.vf_num = 0;
9946 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9947 	if (ret) {
9948 		dev_info(&pf->pdev->dev,
9949 			 "couldn't get PF vsi config, err %s aq_err %s\n",
9950 			 i40e_stat_str(&pf->hw, ret),
9951 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9952 		return;
9953 	}
9954 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9955 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9956 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9957 
9958 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
9959 	if (ret) {
9960 		dev_info(&pf->pdev->dev,
9961 			 "update vsi switch failed, err %s aq_err %s\n",
9962 			 i40e_stat_str(&pf->hw, ret),
9963 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9964 	}
9965 }
9966 
9967 /**
9968  * i40e_config_bridge_mode - Configure the HW bridge mode
9969  * @veb: pointer to the bridge instance
9970  *
9971  * Configure the loop back mode for the LAN VSI that is downlink to the
9972  * specified HW bridge instance. It is expected this function is called
9973  * when a new HW bridge is instantiated.
9974  **/
9975 static void i40e_config_bridge_mode(struct i40e_veb *veb)
9976 {
9977 	struct i40e_pf *pf = veb->pf;
9978 
9979 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
9980 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
9981 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
9982 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
9983 		i40e_disable_pf_switch_lb(pf);
9984 	else
9985 		i40e_enable_pf_switch_lb(pf);
9986 }
9987 
9988 /**
9989  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
9990  * @veb: pointer to the VEB instance
9991  *
9992  * This is a recursive function that first builds the attached VSIs then
9993  * recurses in to build the next layer of VEB.  We track the connections
9994  * through our own index numbers because the seid's from the HW could
9995  * change across the reset.
9996  **/
9997 static int i40e_reconstitute_veb(struct i40e_veb *veb)
9998 {
9999 	struct i40e_vsi *ctl_vsi = NULL;
10000 	struct i40e_pf *pf = veb->pf;
10001 	int v, veb_idx;
10002 	int ret;
10003 
10004 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10005 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10006 		if (pf->vsi[v] &&
10007 		    pf->vsi[v]->veb_idx == veb->idx &&
10008 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10009 			ctl_vsi = pf->vsi[v];
10010 			break;
10011 		}
10012 	}
10013 	if (!ctl_vsi) {
10014 		dev_info(&pf->pdev->dev,
10015 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10016 		ret = -ENOENT;
10017 		goto end_reconstitute;
10018 	}
10019 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10020 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10021 	ret = i40e_add_vsi(ctl_vsi);
10022 	if (ret) {
10023 		dev_info(&pf->pdev->dev,
10024 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10025 			 veb->idx, ret);
10026 		goto end_reconstitute;
10027 	}
10028 	i40e_vsi_reset_stats(ctl_vsi);
10029 
10030 	/* create the VEB in the switch and move the VSI onto the VEB */
10031 	ret = i40e_add_veb(veb, ctl_vsi);
10032 	if (ret)
10033 		goto end_reconstitute;
10034 
10035 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10036 		veb->bridge_mode = BRIDGE_MODE_VEB;
10037 	else
10038 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10039 	i40e_config_bridge_mode(veb);
10040 
10041 	/* create the remaining VSIs attached to this VEB */
10042 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10043 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10044 			continue;
10045 
10046 		if (pf->vsi[v]->veb_idx == veb->idx) {
10047 			struct i40e_vsi *vsi = pf->vsi[v];
10048 
10049 			vsi->uplink_seid = veb->seid;
10050 			ret = i40e_add_vsi(vsi);
10051 			if (ret) {
10052 				dev_info(&pf->pdev->dev,
10053 					 "rebuild of vsi_idx %d failed: %d\n",
10054 					 v, ret);
10055 				goto end_reconstitute;
10056 			}
10057 			i40e_vsi_reset_stats(vsi);
10058 		}
10059 	}
10060 
10061 	/* create any VEBs attached to this VEB - RECURSION */
10062 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10063 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10064 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10065 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10066 			if (ret)
10067 				break;
10068 		}
10069 	}
10070 
10071 end_reconstitute:
10072 	return ret;
10073 }
10074 
10075 /**
10076  * i40e_get_capabilities - get info about the HW
10077  * @pf: the PF struct
10078  * @list_type: AQ capability to be queried
10079  **/
10080 static int i40e_get_capabilities(struct i40e_pf *pf,
10081 				 enum i40e_admin_queue_opc list_type)
10082 {
10083 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10084 	u16 data_size;
10085 	int buf_len;
10086 	int err;
10087 
10088 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10089 	do {
10090 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10091 		if (!cap_buf)
10092 			return -ENOMEM;
10093 
10094 		/* this loads the data into the hw struct for us */
10095 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10096 						    &data_size, list_type,
10097 						    NULL);
10098 		/* data loaded, buffer no longer needed */
10099 		kfree(cap_buf);
10100 
10101 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10102 			/* retry with a larger buffer */
10103 			buf_len = data_size;
10104 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
10105 			dev_info(&pf->pdev->dev,
10106 				 "capability discovery failed, err %s aq_err %s\n",
10107 				 i40e_stat_str(&pf->hw, err),
10108 				 i40e_aq_str(&pf->hw,
10109 					     pf->hw.aq.asq_last_status));
10110 			return -ENODEV;
10111 		}
10112 	} while (err);
10113 
10114 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10115 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10116 			dev_info(&pf->pdev->dev,
10117 				 "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",
10118 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10119 				 pf->hw.func_caps.num_msix_vectors,
10120 				 pf->hw.func_caps.num_msix_vectors_vf,
10121 				 pf->hw.func_caps.fd_filters_guaranteed,
10122 				 pf->hw.func_caps.fd_filters_best_effort,
10123 				 pf->hw.func_caps.num_tx_qp,
10124 				 pf->hw.func_caps.num_vsis);
10125 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10126 			dev_info(&pf->pdev->dev,
10127 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10128 				 pf->hw.dev_caps.switch_mode,
10129 				 pf->hw.dev_caps.valid_functions);
10130 			dev_info(&pf->pdev->dev,
10131 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10132 				 pf->hw.dev_caps.sr_iov_1_1,
10133 				 pf->hw.dev_caps.num_vfs);
10134 			dev_info(&pf->pdev->dev,
10135 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10136 				 pf->hw.dev_caps.num_vsis,
10137 				 pf->hw.dev_caps.num_rx_qp,
10138 				 pf->hw.dev_caps.num_tx_qp);
10139 		}
10140 	}
10141 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10142 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10143 		       + pf->hw.func_caps.num_vfs)
10144 		if (pf->hw.revision_id == 0 &&
10145 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10146 			dev_info(&pf->pdev->dev,
10147 				 "got num_vsis %d, setting num_vsis to %d\n",
10148 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10149 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10150 		}
10151 	}
10152 	return 0;
10153 }
10154 
10155 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10156 
10157 /**
10158  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10159  * @pf: board private structure
10160  **/
10161 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10162 {
10163 	struct i40e_vsi *vsi;
10164 
10165 	/* quick workaround for an NVM issue that leaves a critical register
10166 	 * uninitialized
10167 	 */
10168 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10169 		static const u32 hkey[] = {
10170 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10171 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10172 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10173 			0x95b3a76d};
10174 		int i;
10175 
10176 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10177 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10178 	}
10179 
10180 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10181 		return;
10182 
10183 	/* find existing VSI and see if it needs configuring */
10184 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10185 
10186 	/* create a new VSI if none exists */
10187 	if (!vsi) {
10188 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10189 				     pf->vsi[pf->lan_vsi]->seid, 0);
10190 		if (!vsi) {
10191 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10192 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10193 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10194 			return;
10195 		}
10196 	}
10197 
10198 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10199 }
10200 
10201 /**
10202  * i40e_fdir_teardown - release the Flow Director resources
10203  * @pf: board private structure
10204  **/
10205 static void i40e_fdir_teardown(struct i40e_pf *pf)
10206 {
10207 	struct i40e_vsi *vsi;
10208 
10209 	i40e_fdir_filter_exit(pf);
10210 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10211 	if (vsi)
10212 		i40e_vsi_release(vsi);
10213 }
10214 
10215 /**
10216  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10217  * @vsi: PF main vsi
10218  * @seid: seid of main or channel VSIs
10219  *
10220  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10221  * existed before reset
10222  **/
10223 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10224 {
10225 	struct i40e_cloud_filter *cfilter;
10226 	struct i40e_pf *pf = vsi->back;
10227 	struct hlist_node *node;
10228 	i40e_status ret;
10229 
10230 	/* Add cloud filters back if they exist */
10231 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10232 				  cloud_node) {
10233 		if (cfilter->seid != seid)
10234 			continue;
10235 
10236 		if (cfilter->dst_port)
10237 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10238 								true);
10239 		else
10240 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10241 
10242 		if (ret) {
10243 			dev_dbg(&pf->pdev->dev,
10244 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
10245 				i40e_stat_str(&pf->hw, ret),
10246 				i40e_aq_str(&pf->hw,
10247 					    pf->hw.aq.asq_last_status));
10248 			return ret;
10249 		}
10250 	}
10251 	return 0;
10252 }
10253 
10254 /**
10255  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10256  * @vsi: PF main vsi
10257  *
10258  * Rebuilds channel VSIs if they existed before reset
10259  **/
10260 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10261 {
10262 	struct i40e_channel *ch, *ch_tmp;
10263 	i40e_status ret;
10264 
10265 	if (list_empty(&vsi->ch_list))
10266 		return 0;
10267 
10268 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10269 		if (!ch->initialized)
10270 			break;
10271 		/* Proceed with creation of channel (VMDq2) VSI */
10272 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10273 		if (ret) {
10274 			dev_info(&vsi->back->pdev->dev,
10275 				 "failed to rebuild channels using uplink_seid %u\n",
10276 				 vsi->uplink_seid);
10277 			return ret;
10278 		}
10279 		/* Reconfigure TX queues using QTX_CTL register */
10280 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10281 		if (ret) {
10282 			dev_info(&vsi->back->pdev->dev,
10283 				 "failed to configure TX rings for channel %u\n",
10284 				 ch->seid);
10285 			return ret;
10286 		}
10287 		/* update 'next_base_queue' */
10288 		vsi->next_base_queue = vsi->next_base_queue +
10289 							ch->num_queue_pairs;
10290 		if (ch->max_tx_rate) {
10291 			u64 credits = ch->max_tx_rate;
10292 
10293 			if (i40e_set_bw_limit(vsi, ch->seid,
10294 					      ch->max_tx_rate))
10295 				return -EINVAL;
10296 
10297 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10298 			dev_dbg(&vsi->back->pdev->dev,
10299 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10300 				ch->max_tx_rate,
10301 				credits,
10302 				ch->seid);
10303 		}
10304 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10305 		if (ret) {
10306 			dev_dbg(&vsi->back->pdev->dev,
10307 				"Failed to rebuild cloud filters for channel VSI %u\n",
10308 				ch->seid);
10309 			return ret;
10310 		}
10311 	}
10312 	return 0;
10313 }
10314 
10315 /**
10316  * i40e_prep_for_reset - prep for the core to reset
10317  * @pf: board private structure
10318  *
10319  * Close up the VFs and other things in prep for PF Reset.
10320   **/
10321 static void i40e_prep_for_reset(struct i40e_pf *pf)
10322 {
10323 	struct i40e_hw *hw = &pf->hw;
10324 	i40e_status ret = 0;
10325 	u32 v;
10326 
10327 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10328 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10329 		return;
10330 	if (i40e_check_asq_alive(&pf->hw))
10331 		i40e_vc_notify_reset(pf);
10332 
10333 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10334 
10335 	/* quiesce the VSIs and their queues that are not already DOWN */
10336 	i40e_pf_quiesce_all_vsi(pf);
10337 
10338 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10339 		if (pf->vsi[v])
10340 			pf->vsi[v]->seid = 0;
10341 	}
10342 
10343 	i40e_shutdown_adminq(&pf->hw);
10344 
10345 	/* call shutdown HMC */
10346 	if (hw->hmc.hmc_obj) {
10347 		ret = i40e_shutdown_lan_hmc(hw);
10348 		if (ret)
10349 			dev_warn(&pf->pdev->dev,
10350 				 "shutdown_lan_hmc failed: %d\n", ret);
10351 	}
10352 
10353 	/* Save the current PTP time so that we can restore the time after the
10354 	 * reset completes.
10355 	 */
10356 	i40e_ptp_save_hw_time(pf);
10357 }
10358 
10359 /**
10360  * i40e_send_version - update firmware with driver version
10361  * @pf: PF struct
10362  */
10363 static void i40e_send_version(struct i40e_pf *pf)
10364 {
10365 	struct i40e_driver_version dv;
10366 
10367 	dv.major_version = 0xff;
10368 	dv.minor_version = 0xff;
10369 	dv.build_version = 0xff;
10370 	dv.subbuild_version = 0;
10371 	strlcpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10372 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10373 }
10374 
10375 /**
10376  * i40e_get_oem_version - get OEM specific version information
10377  * @hw: pointer to the hardware structure
10378  **/
10379 static void i40e_get_oem_version(struct i40e_hw *hw)
10380 {
10381 	u16 block_offset = 0xffff;
10382 	u16 block_length = 0;
10383 	u16 capabilities = 0;
10384 	u16 gen_snap = 0;
10385 	u16 release = 0;
10386 
10387 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10388 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10389 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10390 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10391 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10392 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10393 #define I40E_NVM_OEM_LENGTH			3
10394 
10395 	/* Check if pointer to OEM version block is valid. */
10396 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10397 	if (block_offset == 0xffff)
10398 		return;
10399 
10400 	/* Check if OEM version block has correct length. */
10401 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10402 			   &block_length);
10403 	if (block_length < I40E_NVM_OEM_LENGTH)
10404 		return;
10405 
10406 	/* Check if OEM version format is as expected. */
10407 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10408 			   &capabilities);
10409 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10410 		return;
10411 
10412 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10413 			   &gen_snap);
10414 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10415 			   &release);
10416 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10417 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10418 }
10419 
10420 /**
10421  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10422  * @pf: board private structure
10423  **/
10424 static int i40e_reset(struct i40e_pf *pf)
10425 {
10426 	struct i40e_hw *hw = &pf->hw;
10427 	i40e_status ret;
10428 
10429 	ret = i40e_pf_reset(hw);
10430 	if (ret) {
10431 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10432 		set_bit(__I40E_RESET_FAILED, pf->state);
10433 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10434 	} else {
10435 		pf->pfr_count++;
10436 	}
10437 	return ret;
10438 }
10439 
10440 /**
10441  * i40e_rebuild - rebuild using a saved config
10442  * @pf: board private structure
10443  * @reinit: if the Main VSI needs to re-initialized.
10444  * @lock_acquired: indicates whether or not the lock has been acquired
10445  * before this function was called.
10446  **/
10447 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10448 {
10449 	int old_recovery_mode_bit = test_bit(__I40E_RECOVERY_MODE, pf->state);
10450 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10451 	struct i40e_hw *hw = &pf->hw;
10452 	i40e_status ret;
10453 	u32 val;
10454 	int v;
10455 
10456 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10457 	    i40e_check_recovery_mode(pf)) {
10458 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10459 	}
10460 
10461 	if (test_bit(__I40E_DOWN, pf->state) &&
10462 	    !test_bit(__I40E_RECOVERY_MODE, pf->state) &&
10463 	    !old_recovery_mode_bit)
10464 		goto clear_recovery;
10465 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10466 
10467 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10468 	ret = i40e_init_adminq(&pf->hw);
10469 	if (ret) {
10470 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10471 			 i40e_stat_str(&pf->hw, ret),
10472 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10473 		goto clear_recovery;
10474 	}
10475 	i40e_get_oem_version(&pf->hw);
10476 
10477 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10478 	    ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) ||
10479 	     hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) {
10480 		/* The following delay is necessary for 4.33 firmware and older
10481 		 * to recover after EMP reset. 200 ms should suffice but we
10482 		 * put here 300 ms to be sure that FW is ready to operate
10483 		 * after reset.
10484 		 */
10485 		mdelay(300);
10486 	}
10487 
10488 	/* re-verify the eeprom if we just had an EMP reset */
10489 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10490 		i40e_verify_eeprom(pf);
10491 
10492 	/* if we are going out of or into recovery mode we have to act
10493 	 * accordingly with regard to resources initialization
10494 	 * and deinitialization
10495 	 */
10496 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) ||
10497 	    old_recovery_mode_bit) {
10498 		if (i40e_get_capabilities(pf,
10499 					  i40e_aqc_opc_list_func_capabilities))
10500 			goto end_unlock;
10501 
10502 		if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10503 			/* we're staying in recovery mode so we'll reinitialize
10504 			 * misc vector here
10505 			 */
10506 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10507 				goto end_unlock;
10508 		} else {
10509 			if (!lock_acquired)
10510 				rtnl_lock();
10511 			/* we're going out of recovery mode so we'll free
10512 			 * the IRQ allocated specifically for recovery mode
10513 			 * and restore the interrupt scheme
10514 			 */
10515 			free_irq(pf->pdev->irq, pf);
10516 			i40e_clear_interrupt_scheme(pf);
10517 			if (i40e_restore_interrupt_scheme(pf))
10518 				goto end_unlock;
10519 		}
10520 
10521 		/* tell the firmware that we're starting */
10522 		i40e_send_version(pf);
10523 
10524 		/* bail out in case recovery mode was detected, as there is
10525 		 * no need for further configuration.
10526 		 */
10527 		goto end_unlock;
10528 	}
10529 
10530 	i40e_clear_pxe_mode(hw);
10531 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10532 	if (ret)
10533 		goto end_core_reset;
10534 
10535 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10536 				hw->func_caps.num_rx_qp, 0, 0);
10537 	if (ret) {
10538 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10539 		goto end_core_reset;
10540 	}
10541 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10542 	if (ret) {
10543 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10544 		goto end_core_reset;
10545 	}
10546 
10547 #ifdef CONFIG_I40E_DCB
10548 	/* Enable FW to write a default DCB config on link-up
10549 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10550 	 * is not supported with new link speed
10551 	 */
10552 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
10553 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10554 	} else {
10555 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10556 		    (hw->phy.link_info.link_speed &
10557 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10558 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10559 			dev_warn(&pf->pdev->dev,
10560 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10561 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10562 		} else {
10563 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10564 			ret = i40e_init_pf_dcb(pf);
10565 			if (ret) {
10566 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10567 					 ret);
10568 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10569 				/* Continue without DCB enabled */
10570 			}
10571 		}
10572 	}
10573 
10574 #endif /* CONFIG_I40E_DCB */
10575 	if (!lock_acquired)
10576 		rtnl_lock();
10577 	ret = i40e_setup_pf_switch(pf, reinit, true);
10578 	if (ret)
10579 		goto end_unlock;
10580 
10581 	/* The driver only wants link up/down and module qualification
10582 	 * reports from firmware.  Note the negative logic.
10583 	 */
10584 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10585 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10586 					 I40E_AQ_EVENT_MEDIA_NA |
10587 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10588 	if (ret)
10589 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10590 			 i40e_stat_str(&pf->hw, ret),
10591 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10592 
10593 	/* Rebuild the VSIs and VEBs that existed before reset.
10594 	 * They are still in our local switch element arrays, so only
10595 	 * need to rebuild the switch model in the HW.
10596 	 *
10597 	 * If there were VEBs but the reconstitution failed, we'll try
10598 	 * to recover minimal use by getting the basic PF VSI working.
10599 	 */
10600 	if (vsi->uplink_seid != pf->mac_seid) {
10601 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10602 		/* find the one VEB connected to the MAC, and find orphans */
10603 		for (v = 0; v < I40E_MAX_VEB; v++) {
10604 			if (!pf->veb[v])
10605 				continue;
10606 
10607 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10608 			    pf->veb[v]->uplink_seid == 0) {
10609 				ret = i40e_reconstitute_veb(pf->veb[v]);
10610 
10611 				if (!ret)
10612 					continue;
10613 
10614 				/* If Main VEB failed, we're in deep doodoo,
10615 				 * so give up rebuilding the switch and set up
10616 				 * for minimal rebuild of PF VSI.
10617 				 * If orphan failed, we'll report the error
10618 				 * but try to keep going.
10619 				 */
10620 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10621 					dev_info(&pf->pdev->dev,
10622 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10623 						 ret);
10624 					vsi->uplink_seid = pf->mac_seid;
10625 					break;
10626 				} else if (pf->veb[v]->uplink_seid == 0) {
10627 					dev_info(&pf->pdev->dev,
10628 						 "rebuild of orphan VEB failed: %d\n",
10629 						 ret);
10630 				}
10631 			}
10632 		}
10633 	}
10634 
10635 	if (vsi->uplink_seid == pf->mac_seid) {
10636 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10637 		/* no VEB, so rebuild only the Main VSI */
10638 		ret = i40e_add_vsi(vsi);
10639 		if (ret) {
10640 			dev_info(&pf->pdev->dev,
10641 				 "rebuild of Main VSI failed: %d\n", ret);
10642 			goto end_unlock;
10643 		}
10644 	}
10645 
10646 	if (vsi->mqprio_qopt.max_rate[0]) {
10647 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
10648 		u64 credits = 0;
10649 
10650 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
10651 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10652 		if (ret)
10653 			goto end_unlock;
10654 
10655 		credits = max_tx_rate;
10656 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10657 		dev_dbg(&vsi->back->pdev->dev,
10658 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10659 			max_tx_rate,
10660 			credits,
10661 			vsi->seid);
10662 	}
10663 
10664 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10665 	if (ret)
10666 		goto end_unlock;
10667 
10668 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
10669 	 * for this main VSI if they exist
10670 	 */
10671 	ret = i40e_rebuild_channels(vsi);
10672 	if (ret)
10673 		goto end_unlock;
10674 
10675 	/* Reconfigure hardware for allowing smaller MSS in the case
10676 	 * of TSO, so that we avoid the MDD being fired and causing
10677 	 * a reset in the case of small MSS+TSO.
10678 	 */
10679 #define I40E_REG_MSS          0x000E64DC
10680 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
10681 #define I40E_64BYTE_MSS       0x400000
10682 	val = rd32(hw, I40E_REG_MSS);
10683 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10684 		val &= ~I40E_REG_MSS_MIN_MASK;
10685 		val |= I40E_64BYTE_MSS;
10686 		wr32(hw, I40E_REG_MSS, val);
10687 	}
10688 
10689 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
10690 		msleep(75);
10691 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10692 		if (ret)
10693 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10694 				 i40e_stat_str(&pf->hw, ret),
10695 				 i40e_aq_str(&pf->hw,
10696 					     pf->hw.aq.asq_last_status));
10697 	}
10698 	/* reinit the misc interrupt */
10699 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10700 		ret = i40e_setup_misc_vector(pf);
10701 
10702 	/* Add a filter to drop all Flow control frames from any VSI from being
10703 	 * transmitted. By doing so we stop a malicious VF from sending out
10704 	 * PAUSE or PFC frames and potentially controlling traffic for other
10705 	 * PF/VF VSIs.
10706 	 * The FW can still send Flow control frames if enabled.
10707 	 */
10708 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
10709 						       pf->main_vsi_seid);
10710 
10711 	/* restart the VSIs that were rebuilt and running before the reset */
10712 	i40e_pf_unquiesce_all_vsi(pf);
10713 
10714 	/* Release the RTNL lock before we start resetting VFs */
10715 	if (!lock_acquired)
10716 		rtnl_unlock();
10717 
10718 	/* Restore promiscuous settings */
10719 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
10720 	if (ret)
10721 		dev_warn(&pf->pdev->dev,
10722 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
10723 			 pf->cur_promisc ? "on" : "off",
10724 			 i40e_stat_str(&pf->hw, ret),
10725 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10726 
10727 	i40e_reset_all_vfs(pf, true);
10728 
10729 	/* tell the firmware that we're starting */
10730 	i40e_send_version(pf);
10731 
10732 	/* We've already released the lock, so don't do it again */
10733 	goto end_core_reset;
10734 
10735 end_unlock:
10736 	if (!lock_acquired)
10737 		rtnl_unlock();
10738 end_core_reset:
10739 	clear_bit(__I40E_RESET_FAILED, pf->state);
10740 clear_recovery:
10741 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10742 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
10743 }
10744 
10745 /**
10746  * i40e_reset_and_rebuild - reset and rebuild using a saved config
10747  * @pf: board private structure
10748  * @reinit: if the Main VSI needs to re-initialized.
10749  * @lock_acquired: indicates whether or not the lock has been acquired
10750  * before this function was called.
10751  **/
10752 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
10753 				   bool lock_acquired)
10754 {
10755 	int ret;
10756 	/* Now we wait for GRST to settle out.
10757 	 * We don't have to delete the VEBs or VSIs from the hw switch
10758 	 * because the reset will make them disappear.
10759 	 */
10760 	ret = i40e_reset(pf);
10761 	if (!ret)
10762 		i40e_rebuild(pf, reinit, lock_acquired);
10763 }
10764 
10765 /**
10766  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
10767  * @pf: board private structure
10768  *
10769  * Close up the VFs and other things in prep for a Core Reset,
10770  * then get ready to rebuild the world.
10771  * @lock_acquired: indicates whether or not the lock has been acquired
10772  * before this function was called.
10773  **/
10774 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
10775 {
10776 	i40e_prep_for_reset(pf);
10777 	i40e_reset_and_rebuild(pf, false, lock_acquired);
10778 }
10779 
10780 /**
10781  * i40e_handle_mdd_event
10782  * @pf: pointer to the PF structure
10783  *
10784  * Called from the MDD irq handler to identify possibly malicious vfs
10785  **/
10786 static void i40e_handle_mdd_event(struct i40e_pf *pf)
10787 {
10788 	struct i40e_hw *hw = &pf->hw;
10789 	bool mdd_detected = false;
10790 	struct i40e_vf *vf;
10791 	u32 reg;
10792 	int i;
10793 
10794 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
10795 		return;
10796 
10797 	/* find what triggered the MDD event */
10798 	reg = rd32(hw, I40E_GL_MDET_TX);
10799 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
10800 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
10801 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
10802 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
10803 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
10804 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
10805 				I40E_GL_MDET_TX_EVENT_SHIFT;
10806 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
10807 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
10808 				pf->hw.func_caps.base_queue;
10809 		if (netif_msg_tx_err(pf))
10810 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
10811 				 event, queue, pf_num, vf_num);
10812 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
10813 		mdd_detected = true;
10814 	}
10815 	reg = rd32(hw, I40E_GL_MDET_RX);
10816 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
10817 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
10818 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
10819 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
10820 				I40E_GL_MDET_RX_EVENT_SHIFT;
10821 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
10822 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
10823 				pf->hw.func_caps.base_queue;
10824 		if (netif_msg_rx_err(pf))
10825 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
10826 				 event, queue, func);
10827 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
10828 		mdd_detected = true;
10829 	}
10830 
10831 	if (mdd_detected) {
10832 		reg = rd32(hw, I40E_PF_MDET_TX);
10833 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
10834 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
10835 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
10836 		}
10837 		reg = rd32(hw, I40E_PF_MDET_RX);
10838 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
10839 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
10840 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
10841 		}
10842 	}
10843 
10844 	/* see if one of the VFs needs its hand slapped */
10845 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
10846 		vf = &(pf->vf[i]);
10847 		reg = rd32(hw, I40E_VP_MDET_TX(i));
10848 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
10849 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
10850 			vf->num_mdd_events++;
10851 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
10852 				 i);
10853 			dev_info(&pf->pdev->dev,
10854 				 "Use PF Control I/F to re-enable the VF\n");
10855 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10856 		}
10857 
10858 		reg = rd32(hw, I40E_VP_MDET_RX(i));
10859 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
10860 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
10861 			vf->num_mdd_events++;
10862 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
10863 				 i);
10864 			dev_info(&pf->pdev->dev,
10865 				 "Use PF Control I/F to re-enable the VF\n");
10866 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10867 		}
10868 	}
10869 
10870 	/* re-enable mdd interrupt cause */
10871 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
10872 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
10873 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
10874 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
10875 	i40e_flush(hw);
10876 }
10877 
10878 /**
10879  * i40e_service_task - Run the driver's async subtasks
10880  * @work: pointer to work_struct containing our data
10881  **/
10882 static void i40e_service_task(struct work_struct *work)
10883 {
10884 	struct i40e_pf *pf = container_of(work,
10885 					  struct i40e_pf,
10886 					  service_task);
10887 	unsigned long start_time = jiffies;
10888 
10889 	/* don't bother with service tasks if a reset is in progress */
10890 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
10891 	    test_bit(__I40E_SUSPENDED, pf->state))
10892 		return;
10893 
10894 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
10895 		return;
10896 
10897 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10898 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
10899 		i40e_sync_filters_subtask(pf);
10900 		i40e_reset_subtask(pf);
10901 		i40e_handle_mdd_event(pf);
10902 		i40e_vc_process_vflr_event(pf);
10903 		i40e_watchdog_subtask(pf);
10904 		i40e_fdir_reinit_subtask(pf);
10905 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
10906 			/* Client subtask will reopen next time through. */
10907 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
10908 							   true);
10909 		} else {
10910 			i40e_client_subtask(pf);
10911 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
10912 					       pf->state))
10913 				i40e_notify_client_of_l2_param_changes(
10914 								pf->vsi[pf->lan_vsi]);
10915 		}
10916 		i40e_sync_filters_subtask(pf);
10917 	} else {
10918 		i40e_reset_subtask(pf);
10919 	}
10920 
10921 	i40e_clean_adminq_subtask(pf);
10922 
10923 	/* flush memory to make sure state is correct before next watchdog */
10924 	smp_mb__before_atomic();
10925 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
10926 
10927 	/* If the tasks have taken longer than one timer cycle or there
10928 	 * is more work to be done, reschedule the service task now
10929 	 * rather than wait for the timer to tick again.
10930 	 */
10931 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
10932 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
10933 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
10934 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
10935 		i40e_service_event_schedule(pf);
10936 }
10937 
10938 /**
10939  * i40e_service_timer - timer callback
10940  * @t: timer list pointer
10941  **/
10942 static void i40e_service_timer(struct timer_list *t)
10943 {
10944 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
10945 
10946 	mod_timer(&pf->service_timer,
10947 		  round_jiffies(jiffies + pf->service_timer_period));
10948 	i40e_service_event_schedule(pf);
10949 }
10950 
10951 /**
10952  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
10953  * @vsi: the VSI being configured
10954  **/
10955 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
10956 {
10957 	struct i40e_pf *pf = vsi->back;
10958 
10959 	switch (vsi->type) {
10960 	case I40E_VSI_MAIN:
10961 		vsi->alloc_queue_pairs = pf->num_lan_qps;
10962 		if (!vsi->num_tx_desc)
10963 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10964 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10965 		if (!vsi->num_rx_desc)
10966 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10967 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10968 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10969 			vsi->num_q_vectors = pf->num_lan_msix;
10970 		else
10971 			vsi->num_q_vectors = 1;
10972 
10973 		break;
10974 
10975 	case I40E_VSI_FDIR:
10976 		vsi->alloc_queue_pairs = 1;
10977 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
10978 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
10979 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
10980 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
10981 		vsi->num_q_vectors = pf->num_fdsb_msix;
10982 		break;
10983 
10984 	case I40E_VSI_VMDQ2:
10985 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
10986 		if (!vsi->num_tx_desc)
10987 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10988 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10989 		if (!vsi->num_rx_desc)
10990 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10991 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10992 		vsi->num_q_vectors = pf->num_vmdq_msix;
10993 		break;
10994 
10995 	case I40E_VSI_SRIOV:
10996 		vsi->alloc_queue_pairs = pf->num_vf_qps;
10997 		if (!vsi->num_tx_desc)
10998 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10999 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11000 		if (!vsi->num_rx_desc)
11001 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11002 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11003 		break;
11004 
11005 	default:
11006 		WARN_ON(1);
11007 		return -ENODATA;
11008 	}
11009 
11010 	if (is_kdump_kernel()) {
11011 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11012 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11013 	}
11014 
11015 	return 0;
11016 }
11017 
11018 /**
11019  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11020  * @vsi: VSI pointer
11021  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11022  *
11023  * On error: returns error code (negative)
11024  * On success: returns 0
11025  **/
11026 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11027 {
11028 	struct i40e_ring **next_rings;
11029 	int size;
11030 	int ret = 0;
11031 
11032 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11033 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11034 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11035 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11036 	if (!vsi->tx_rings)
11037 		return -ENOMEM;
11038 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11039 	if (i40e_enabled_xdp_vsi(vsi)) {
11040 		vsi->xdp_rings = next_rings;
11041 		next_rings += vsi->alloc_queue_pairs;
11042 	}
11043 	vsi->rx_rings = next_rings;
11044 
11045 	if (alloc_qvectors) {
11046 		/* allocate memory for q_vector pointers */
11047 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11048 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11049 		if (!vsi->q_vectors) {
11050 			ret = -ENOMEM;
11051 			goto err_vectors;
11052 		}
11053 	}
11054 	return ret;
11055 
11056 err_vectors:
11057 	kfree(vsi->tx_rings);
11058 	return ret;
11059 }
11060 
11061 /**
11062  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11063  * @pf: board private structure
11064  * @type: type of VSI
11065  *
11066  * On error: returns error code (negative)
11067  * On success: returns vsi index in PF (positive)
11068  **/
11069 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11070 {
11071 	int ret = -ENODEV;
11072 	struct i40e_vsi *vsi;
11073 	int vsi_idx;
11074 	int i;
11075 
11076 	/* Need to protect the allocation of the VSIs at the PF level */
11077 	mutex_lock(&pf->switch_mutex);
11078 
11079 	/* VSI list may be fragmented if VSI creation/destruction has
11080 	 * been happening.  We can afford to do a quick scan to look
11081 	 * for any free VSIs in the list.
11082 	 *
11083 	 * find next empty vsi slot, looping back around if necessary
11084 	 */
11085 	i = pf->next_vsi;
11086 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11087 		i++;
11088 	if (i >= pf->num_alloc_vsi) {
11089 		i = 0;
11090 		while (i < pf->next_vsi && pf->vsi[i])
11091 			i++;
11092 	}
11093 
11094 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11095 		vsi_idx = i;             /* Found one! */
11096 	} else {
11097 		ret = -ENODEV;
11098 		goto unlock_pf;  /* out of VSI slots! */
11099 	}
11100 	pf->next_vsi = ++i;
11101 
11102 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11103 	if (!vsi) {
11104 		ret = -ENOMEM;
11105 		goto unlock_pf;
11106 	}
11107 	vsi->type = type;
11108 	vsi->back = pf;
11109 	set_bit(__I40E_VSI_DOWN, vsi->state);
11110 	vsi->flags = 0;
11111 	vsi->idx = vsi_idx;
11112 	vsi->int_rate_limit = 0;
11113 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11114 				pf->rss_table_size : 64;
11115 	vsi->netdev_registered = false;
11116 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11117 	hash_init(vsi->mac_filter_hash);
11118 	vsi->irqs_ready = false;
11119 
11120 	if (type == I40E_VSI_MAIN) {
11121 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11122 		if (!vsi->af_xdp_zc_qps)
11123 			goto err_rings;
11124 	}
11125 
11126 	ret = i40e_set_num_rings_in_vsi(vsi);
11127 	if (ret)
11128 		goto err_rings;
11129 
11130 	ret = i40e_vsi_alloc_arrays(vsi, true);
11131 	if (ret)
11132 		goto err_rings;
11133 
11134 	/* Setup default MSIX irq handler for VSI */
11135 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11136 
11137 	/* Initialize VSI lock */
11138 	spin_lock_init(&vsi->mac_filter_hash_lock);
11139 	pf->vsi[vsi_idx] = vsi;
11140 	ret = vsi_idx;
11141 	goto unlock_pf;
11142 
11143 err_rings:
11144 	bitmap_free(vsi->af_xdp_zc_qps);
11145 	pf->next_vsi = i - 1;
11146 	kfree(vsi);
11147 unlock_pf:
11148 	mutex_unlock(&pf->switch_mutex);
11149 	return ret;
11150 }
11151 
11152 /**
11153  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11154  * @vsi: VSI pointer
11155  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11156  *
11157  * On error: returns error code (negative)
11158  * On success: returns 0
11159  **/
11160 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11161 {
11162 	/* free the ring and vector containers */
11163 	if (free_qvectors) {
11164 		kfree(vsi->q_vectors);
11165 		vsi->q_vectors = NULL;
11166 	}
11167 	kfree(vsi->tx_rings);
11168 	vsi->tx_rings = NULL;
11169 	vsi->rx_rings = NULL;
11170 	vsi->xdp_rings = NULL;
11171 }
11172 
11173 /**
11174  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11175  * and lookup table
11176  * @vsi: Pointer to VSI structure
11177  */
11178 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11179 {
11180 	if (!vsi)
11181 		return;
11182 
11183 	kfree(vsi->rss_hkey_user);
11184 	vsi->rss_hkey_user = NULL;
11185 
11186 	kfree(vsi->rss_lut_user);
11187 	vsi->rss_lut_user = NULL;
11188 }
11189 
11190 /**
11191  * i40e_vsi_clear - Deallocate the VSI provided
11192  * @vsi: the VSI being un-configured
11193  **/
11194 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11195 {
11196 	struct i40e_pf *pf;
11197 
11198 	if (!vsi)
11199 		return 0;
11200 
11201 	if (!vsi->back)
11202 		goto free_vsi;
11203 	pf = vsi->back;
11204 
11205 	mutex_lock(&pf->switch_mutex);
11206 	if (!pf->vsi[vsi->idx]) {
11207 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11208 			vsi->idx, vsi->idx, vsi->type);
11209 		goto unlock_vsi;
11210 	}
11211 
11212 	if (pf->vsi[vsi->idx] != vsi) {
11213 		dev_err(&pf->pdev->dev,
11214 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11215 			pf->vsi[vsi->idx]->idx,
11216 			pf->vsi[vsi->idx]->type,
11217 			vsi->idx, vsi->type);
11218 		goto unlock_vsi;
11219 	}
11220 
11221 	/* updates the PF for this cleared vsi */
11222 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11223 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11224 
11225 	bitmap_free(vsi->af_xdp_zc_qps);
11226 	i40e_vsi_free_arrays(vsi, true);
11227 	i40e_clear_rss_config_user(vsi);
11228 
11229 	pf->vsi[vsi->idx] = NULL;
11230 	if (vsi->idx < pf->next_vsi)
11231 		pf->next_vsi = vsi->idx;
11232 
11233 unlock_vsi:
11234 	mutex_unlock(&pf->switch_mutex);
11235 free_vsi:
11236 	kfree(vsi);
11237 
11238 	return 0;
11239 }
11240 
11241 /**
11242  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11243  * @vsi: the VSI being cleaned
11244  **/
11245 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11246 {
11247 	int i;
11248 
11249 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11250 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11251 			kfree_rcu(vsi->tx_rings[i], rcu);
11252 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11253 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11254 			if (vsi->xdp_rings)
11255 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11256 		}
11257 	}
11258 }
11259 
11260 /**
11261  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11262  * @vsi: the VSI being configured
11263  **/
11264 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11265 {
11266 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11267 	struct i40e_pf *pf = vsi->back;
11268 	struct i40e_ring *ring;
11269 
11270 	/* Set basic values in the rings to be used later during open() */
11271 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11272 		/* allocate space for both Tx and Rx in one shot */
11273 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11274 		if (!ring)
11275 			goto err_out;
11276 
11277 		ring->queue_index = i;
11278 		ring->reg_idx = vsi->base_queue + i;
11279 		ring->ring_active = false;
11280 		ring->vsi = vsi;
11281 		ring->netdev = vsi->netdev;
11282 		ring->dev = &pf->pdev->dev;
11283 		ring->count = vsi->num_tx_desc;
11284 		ring->size = 0;
11285 		ring->dcb_tc = 0;
11286 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11287 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11288 		ring->itr_setting = pf->tx_itr_default;
11289 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11290 
11291 		if (!i40e_enabled_xdp_vsi(vsi))
11292 			goto setup_rx;
11293 
11294 		ring->queue_index = vsi->alloc_queue_pairs + i;
11295 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11296 		ring->ring_active = false;
11297 		ring->vsi = vsi;
11298 		ring->netdev = NULL;
11299 		ring->dev = &pf->pdev->dev;
11300 		ring->count = vsi->num_tx_desc;
11301 		ring->size = 0;
11302 		ring->dcb_tc = 0;
11303 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11304 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11305 		set_ring_xdp(ring);
11306 		ring->itr_setting = pf->tx_itr_default;
11307 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11308 
11309 setup_rx:
11310 		ring->queue_index = i;
11311 		ring->reg_idx = vsi->base_queue + i;
11312 		ring->ring_active = false;
11313 		ring->vsi = vsi;
11314 		ring->netdev = vsi->netdev;
11315 		ring->dev = &pf->pdev->dev;
11316 		ring->count = vsi->num_rx_desc;
11317 		ring->size = 0;
11318 		ring->dcb_tc = 0;
11319 		ring->itr_setting = pf->rx_itr_default;
11320 		WRITE_ONCE(vsi->rx_rings[i], ring);
11321 	}
11322 
11323 	return 0;
11324 
11325 err_out:
11326 	i40e_vsi_clear_rings(vsi);
11327 	return -ENOMEM;
11328 }
11329 
11330 /**
11331  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11332  * @pf: board private structure
11333  * @vectors: the number of MSI-X vectors to request
11334  *
11335  * Returns the number of vectors reserved, or error
11336  **/
11337 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11338 {
11339 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11340 					I40E_MIN_MSIX, vectors);
11341 	if (vectors < 0) {
11342 		dev_info(&pf->pdev->dev,
11343 			 "MSI-X vector reservation failed: %d\n", vectors);
11344 		vectors = 0;
11345 	}
11346 
11347 	return vectors;
11348 }
11349 
11350 /**
11351  * i40e_init_msix - Setup the MSIX capability
11352  * @pf: board private structure
11353  *
11354  * Work with the OS to set up the MSIX vectors needed.
11355  *
11356  * Returns the number of vectors reserved or negative on failure
11357  **/
11358 static int i40e_init_msix(struct i40e_pf *pf)
11359 {
11360 	struct i40e_hw *hw = &pf->hw;
11361 	int cpus, extra_vectors;
11362 	int vectors_left;
11363 	int v_budget, i;
11364 	int v_actual;
11365 	int iwarp_requested = 0;
11366 
11367 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11368 		return -ENODEV;
11369 
11370 	/* The number of vectors we'll request will be comprised of:
11371 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11372 	 *   - The number of LAN queue pairs
11373 	 *	- Queues being used for RSS.
11374 	 *		We don't need as many as max_rss_size vectors.
11375 	 *		use rss_size instead in the calculation since that
11376 	 *		is governed by number of cpus in the system.
11377 	 *	- assumes symmetric Tx/Rx pairing
11378 	 *   - The number of VMDq pairs
11379 	 *   - The CPU count within the NUMA node if iWARP is enabled
11380 	 * Once we count this up, try the request.
11381 	 *
11382 	 * If we can't get what we want, we'll simplify to nearly nothing
11383 	 * and try again.  If that still fails, we punt.
11384 	 */
11385 	vectors_left = hw->func_caps.num_msix_vectors;
11386 	v_budget = 0;
11387 
11388 	/* reserve one vector for miscellaneous handler */
11389 	if (vectors_left) {
11390 		v_budget++;
11391 		vectors_left--;
11392 	}
11393 
11394 	/* reserve some vectors for the main PF traffic queues. Initially we
11395 	 * only reserve at most 50% of the available vectors, in the case that
11396 	 * the number of online CPUs is large. This ensures that we can enable
11397 	 * extra features as well. Once we've enabled the other features, we
11398 	 * will use any remaining vectors to reach as close as we can to the
11399 	 * number of online CPUs.
11400 	 */
11401 	cpus = num_online_cpus();
11402 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11403 	vectors_left -= pf->num_lan_msix;
11404 
11405 	/* reserve one vector for sideband flow director */
11406 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11407 		if (vectors_left) {
11408 			pf->num_fdsb_msix = 1;
11409 			v_budget++;
11410 			vectors_left--;
11411 		} else {
11412 			pf->num_fdsb_msix = 0;
11413 		}
11414 	}
11415 
11416 	/* can we reserve enough for iWARP? */
11417 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11418 		iwarp_requested = pf->num_iwarp_msix;
11419 
11420 		if (!vectors_left)
11421 			pf->num_iwarp_msix = 0;
11422 		else if (vectors_left < pf->num_iwarp_msix)
11423 			pf->num_iwarp_msix = 1;
11424 		v_budget += pf->num_iwarp_msix;
11425 		vectors_left -= pf->num_iwarp_msix;
11426 	}
11427 
11428 	/* any vectors left over go for VMDq support */
11429 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11430 		if (!vectors_left) {
11431 			pf->num_vmdq_msix = 0;
11432 			pf->num_vmdq_qps = 0;
11433 		} else {
11434 			int vmdq_vecs_wanted =
11435 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11436 			int vmdq_vecs =
11437 				min_t(int, vectors_left, vmdq_vecs_wanted);
11438 
11439 			/* if we're short on vectors for what's desired, we limit
11440 			 * the queues per vmdq.  If this is still more than are
11441 			 * available, the user will need to change the number of
11442 			 * queues/vectors used by the PF later with the ethtool
11443 			 * channels command
11444 			 */
11445 			if (vectors_left < vmdq_vecs_wanted) {
11446 				pf->num_vmdq_qps = 1;
11447 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11448 				vmdq_vecs = min_t(int,
11449 						  vectors_left,
11450 						  vmdq_vecs_wanted);
11451 			}
11452 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11453 
11454 			v_budget += vmdq_vecs;
11455 			vectors_left -= vmdq_vecs;
11456 		}
11457 	}
11458 
11459 	/* On systems with a large number of SMP cores, we previously limited
11460 	 * the number of vectors for num_lan_msix to be at most 50% of the
11461 	 * available vectors, to allow for other features. Now, we add back
11462 	 * the remaining vectors. However, we ensure that the total
11463 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11464 	 * calculate the number of vectors we can add without going over the
11465 	 * cap of CPUs. For systems with a small number of CPUs this will be
11466 	 * zero.
11467 	 */
11468 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11469 	pf->num_lan_msix += extra_vectors;
11470 	vectors_left -= extra_vectors;
11471 
11472 	WARN(vectors_left < 0,
11473 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11474 
11475 	v_budget += pf->num_lan_msix;
11476 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11477 				   GFP_KERNEL);
11478 	if (!pf->msix_entries)
11479 		return -ENOMEM;
11480 
11481 	for (i = 0; i < v_budget; i++)
11482 		pf->msix_entries[i].entry = i;
11483 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11484 
11485 	if (v_actual < I40E_MIN_MSIX) {
11486 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11487 		kfree(pf->msix_entries);
11488 		pf->msix_entries = NULL;
11489 		pci_disable_msix(pf->pdev);
11490 		return -ENODEV;
11491 
11492 	} else if (v_actual == I40E_MIN_MSIX) {
11493 		/* Adjust for minimal MSIX use */
11494 		pf->num_vmdq_vsis = 0;
11495 		pf->num_vmdq_qps = 0;
11496 		pf->num_lan_qps = 1;
11497 		pf->num_lan_msix = 1;
11498 
11499 	} else if (v_actual != v_budget) {
11500 		/* If we have limited resources, we will start with no vectors
11501 		 * for the special features and then allocate vectors to some
11502 		 * of these features based on the policy and at the end disable
11503 		 * the features that did not get any vectors.
11504 		 */
11505 		int vec;
11506 
11507 		dev_info(&pf->pdev->dev,
11508 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11509 			 v_actual, v_budget);
11510 		/* reserve the misc vector */
11511 		vec = v_actual - 1;
11512 
11513 		/* Scale vector usage down */
11514 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11515 		pf->num_vmdq_vsis = 1;
11516 		pf->num_vmdq_qps = 1;
11517 
11518 		/* partition out the remaining vectors */
11519 		switch (vec) {
11520 		case 2:
11521 			pf->num_lan_msix = 1;
11522 			break;
11523 		case 3:
11524 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11525 				pf->num_lan_msix = 1;
11526 				pf->num_iwarp_msix = 1;
11527 			} else {
11528 				pf->num_lan_msix = 2;
11529 			}
11530 			break;
11531 		default:
11532 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11533 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11534 						 iwarp_requested);
11535 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11536 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11537 			} else {
11538 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11539 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11540 			}
11541 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11542 				pf->num_fdsb_msix = 1;
11543 				vec--;
11544 			}
11545 			pf->num_lan_msix = min_t(int,
11546 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11547 							      pf->num_lan_msix);
11548 			pf->num_lan_qps = pf->num_lan_msix;
11549 			break;
11550 		}
11551 	}
11552 
11553 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11554 	    (pf->num_fdsb_msix == 0)) {
11555 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11556 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11557 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11558 	}
11559 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11560 	    (pf->num_vmdq_msix == 0)) {
11561 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11562 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11563 	}
11564 
11565 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11566 	    (pf->num_iwarp_msix == 0)) {
11567 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11568 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11569 	}
11570 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11571 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11572 		   pf->num_lan_msix,
11573 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11574 		   pf->num_fdsb_msix,
11575 		   pf->num_iwarp_msix);
11576 
11577 	return v_actual;
11578 }
11579 
11580 /**
11581  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11582  * @vsi: the VSI being configured
11583  * @v_idx: index of the vector in the vsi struct
11584  *
11585  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11586  **/
11587 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11588 {
11589 	struct i40e_q_vector *q_vector;
11590 
11591 	/* allocate q_vector */
11592 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11593 	if (!q_vector)
11594 		return -ENOMEM;
11595 
11596 	q_vector->vsi = vsi;
11597 	q_vector->v_idx = v_idx;
11598 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11599 
11600 	if (vsi->netdev)
11601 		netif_napi_add(vsi->netdev, &q_vector->napi,
11602 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
11603 
11604 	/* tie q_vector and vsi together */
11605 	vsi->q_vectors[v_idx] = q_vector;
11606 
11607 	return 0;
11608 }
11609 
11610 /**
11611  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11612  * @vsi: the VSI being configured
11613  *
11614  * We allocate one q_vector per queue interrupt.  If allocation fails we
11615  * return -ENOMEM.
11616  **/
11617 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11618 {
11619 	struct i40e_pf *pf = vsi->back;
11620 	int err, v_idx, num_q_vectors;
11621 
11622 	/* if not MSIX, give the one vector only to the LAN VSI */
11623 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11624 		num_q_vectors = vsi->num_q_vectors;
11625 	else if (vsi == pf->vsi[pf->lan_vsi])
11626 		num_q_vectors = 1;
11627 	else
11628 		return -EINVAL;
11629 
11630 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11631 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11632 		if (err)
11633 			goto err_out;
11634 	}
11635 
11636 	return 0;
11637 
11638 err_out:
11639 	while (v_idx--)
11640 		i40e_free_q_vector(vsi, v_idx);
11641 
11642 	return err;
11643 }
11644 
11645 /**
11646  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11647  * @pf: board private structure to initialize
11648  **/
11649 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11650 {
11651 	int vectors = 0;
11652 	ssize_t size;
11653 
11654 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11655 		vectors = i40e_init_msix(pf);
11656 		if (vectors < 0) {
11657 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11658 				       I40E_FLAG_IWARP_ENABLED	|
11659 				       I40E_FLAG_RSS_ENABLED	|
11660 				       I40E_FLAG_DCB_CAPABLE	|
11661 				       I40E_FLAG_DCB_ENABLED	|
11662 				       I40E_FLAG_SRIOV_ENABLED	|
11663 				       I40E_FLAG_FD_SB_ENABLED	|
11664 				       I40E_FLAG_FD_ATR_ENABLED	|
11665 				       I40E_FLAG_VMDQ_ENABLED);
11666 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11667 
11668 			/* rework the queue expectations without MSIX */
11669 			i40e_determine_queue_usage(pf);
11670 		}
11671 	}
11672 
11673 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11674 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
11675 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
11676 		vectors = pci_enable_msi(pf->pdev);
11677 		if (vectors < 0) {
11678 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
11679 				 vectors);
11680 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
11681 		}
11682 		vectors = 1;  /* one MSI or Legacy vector */
11683 	}
11684 
11685 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
11686 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
11687 
11688 	/* set up vector assignment tracking */
11689 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
11690 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
11691 	if (!pf->irq_pile)
11692 		return -ENOMEM;
11693 
11694 	pf->irq_pile->num_entries = vectors;
11695 	pf->irq_pile->search_hint = 0;
11696 
11697 	/* track first vector for misc interrupts, ignore return */
11698 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
11699 
11700 	return 0;
11701 }
11702 
11703 /**
11704  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
11705  * @pf: private board data structure
11706  *
11707  * Restore the interrupt scheme that was cleared when we suspended the
11708  * device. This should be called during resume to re-allocate the q_vectors
11709  * and reacquire IRQs.
11710  */
11711 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
11712 {
11713 	int err, i;
11714 
11715 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
11716 	 * scheme. We need to re-enabled them here in order to attempt to
11717 	 * re-acquire the MSI or MSI-X vectors
11718 	 */
11719 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
11720 
11721 	err = i40e_init_interrupt_scheme(pf);
11722 	if (err)
11723 		return err;
11724 
11725 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
11726 	 * rings together again.
11727 	 */
11728 	for (i = 0; i < pf->num_alloc_vsi; i++) {
11729 		if (pf->vsi[i]) {
11730 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
11731 			if (err)
11732 				goto err_unwind;
11733 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
11734 		}
11735 	}
11736 
11737 	err = i40e_setup_misc_vector(pf);
11738 	if (err)
11739 		goto err_unwind;
11740 
11741 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
11742 		i40e_client_update_msix_info(pf);
11743 
11744 	return 0;
11745 
11746 err_unwind:
11747 	while (i--) {
11748 		if (pf->vsi[i])
11749 			i40e_vsi_free_q_vectors(pf->vsi[i]);
11750 	}
11751 
11752 	return err;
11753 }
11754 
11755 /**
11756  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
11757  * non queue events in recovery mode
11758  * @pf: board private structure
11759  *
11760  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
11761  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
11762  * This is handled differently than in recovery mode since no Tx/Rx resources
11763  * are being allocated.
11764  **/
11765 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
11766 {
11767 	int err;
11768 
11769 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11770 		err = i40e_setup_misc_vector(pf);
11771 
11772 		if (err) {
11773 			dev_info(&pf->pdev->dev,
11774 				 "MSI-X misc vector request failed, error %d\n",
11775 				 err);
11776 			return err;
11777 		}
11778 	} else {
11779 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
11780 
11781 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
11782 				  pf->int_name, pf);
11783 
11784 		if (err) {
11785 			dev_info(&pf->pdev->dev,
11786 				 "MSI/legacy misc vector request failed, error %d\n",
11787 				 err);
11788 			return err;
11789 		}
11790 		i40e_enable_misc_int_causes(pf);
11791 		i40e_irq_dynamic_enable_icr0(pf);
11792 	}
11793 
11794 	return 0;
11795 }
11796 
11797 /**
11798  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
11799  * @pf: board private structure
11800  *
11801  * This sets up the handler for MSIX 0, which is used to manage the
11802  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
11803  * when in MSI or Legacy interrupt mode.
11804  **/
11805 static int i40e_setup_misc_vector(struct i40e_pf *pf)
11806 {
11807 	struct i40e_hw *hw = &pf->hw;
11808 	int err = 0;
11809 
11810 	/* Only request the IRQ once, the first time through. */
11811 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
11812 		err = request_irq(pf->msix_entries[0].vector,
11813 				  i40e_intr, 0, pf->int_name, pf);
11814 		if (err) {
11815 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
11816 			dev_info(&pf->pdev->dev,
11817 				 "request_irq for %s failed: %d\n",
11818 				 pf->int_name, err);
11819 			return -EFAULT;
11820 		}
11821 	}
11822 
11823 	i40e_enable_misc_int_causes(pf);
11824 
11825 	/* associate no queues to the misc vector */
11826 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
11827 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
11828 
11829 	i40e_flush(hw);
11830 
11831 	i40e_irq_dynamic_enable_icr0(pf);
11832 
11833 	return err;
11834 }
11835 
11836 /**
11837  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
11838  * @vsi: Pointer to vsi structure
11839  * @seed: Buffter to store the hash keys
11840  * @lut: Buffer to store the lookup table entries
11841  * @lut_size: Size of buffer to store the lookup table entries
11842  *
11843  * Return 0 on success, negative on failure
11844  */
11845 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
11846 			   u8 *lut, u16 lut_size)
11847 {
11848 	struct i40e_pf *pf = vsi->back;
11849 	struct i40e_hw *hw = &pf->hw;
11850 	int ret = 0;
11851 
11852 	if (seed) {
11853 		ret = i40e_aq_get_rss_key(hw, vsi->id,
11854 			(struct i40e_aqc_get_set_rss_key_data *)seed);
11855 		if (ret) {
11856 			dev_info(&pf->pdev->dev,
11857 				 "Cannot get RSS key, err %s aq_err %s\n",
11858 				 i40e_stat_str(&pf->hw, ret),
11859 				 i40e_aq_str(&pf->hw,
11860 					     pf->hw.aq.asq_last_status));
11861 			return ret;
11862 		}
11863 	}
11864 
11865 	if (lut) {
11866 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
11867 
11868 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
11869 		if (ret) {
11870 			dev_info(&pf->pdev->dev,
11871 				 "Cannot get RSS lut, err %s aq_err %s\n",
11872 				 i40e_stat_str(&pf->hw, ret),
11873 				 i40e_aq_str(&pf->hw,
11874 					     pf->hw.aq.asq_last_status));
11875 			return ret;
11876 		}
11877 	}
11878 
11879 	return ret;
11880 }
11881 
11882 /**
11883  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
11884  * @vsi: Pointer to vsi structure
11885  * @seed: RSS hash seed
11886  * @lut: Lookup table
11887  * @lut_size: Lookup table size
11888  *
11889  * Returns 0 on success, negative on failure
11890  **/
11891 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
11892 			       const u8 *lut, u16 lut_size)
11893 {
11894 	struct i40e_pf *pf = vsi->back;
11895 	struct i40e_hw *hw = &pf->hw;
11896 	u16 vf_id = vsi->vf_id;
11897 	u8 i;
11898 
11899 	/* Fill out hash function seed */
11900 	if (seed) {
11901 		u32 *seed_dw = (u32 *)seed;
11902 
11903 		if (vsi->type == I40E_VSI_MAIN) {
11904 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
11905 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
11906 		} else if (vsi->type == I40E_VSI_SRIOV) {
11907 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
11908 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
11909 		} else {
11910 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
11911 		}
11912 	}
11913 
11914 	if (lut) {
11915 		u32 *lut_dw = (u32 *)lut;
11916 
11917 		if (vsi->type == I40E_VSI_MAIN) {
11918 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
11919 				return -EINVAL;
11920 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11921 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
11922 		} else if (vsi->type == I40E_VSI_SRIOV) {
11923 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
11924 				return -EINVAL;
11925 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
11926 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
11927 		} else {
11928 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
11929 		}
11930 	}
11931 	i40e_flush(hw);
11932 
11933 	return 0;
11934 }
11935 
11936 /**
11937  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
11938  * @vsi: Pointer to VSI structure
11939  * @seed: Buffer to store the keys
11940  * @lut: Buffer to store the lookup table entries
11941  * @lut_size: Size of buffer to store the lookup table entries
11942  *
11943  * Returns 0 on success, negative on failure
11944  */
11945 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
11946 			    u8 *lut, u16 lut_size)
11947 {
11948 	struct i40e_pf *pf = vsi->back;
11949 	struct i40e_hw *hw = &pf->hw;
11950 	u16 i;
11951 
11952 	if (seed) {
11953 		u32 *seed_dw = (u32 *)seed;
11954 
11955 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
11956 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
11957 	}
11958 	if (lut) {
11959 		u32 *lut_dw = (u32 *)lut;
11960 
11961 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
11962 			return -EINVAL;
11963 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11964 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
11965 	}
11966 
11967 	return 0;
11968 }
11969 
11970 /**
11971  * i40e_config_rss - Configure RSS keys and lut
11972  * @vsi: Pointer to VSI structure
11973  * @seed: RSS hash seed
11974  * @lut: Lookup table
11975  * @lut_size: Lookup table size
11976  *
11977  * Returns 0 on success, negative on failure
11978  */
11979 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
11980 {
11981 	struct i40e_pf *pf = vsi->back;
11982 
11983 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
11984 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
11985 	else
11986 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
11987 }
11988 
11989 /**
11990  * i40e_get_rss - Get RSS keys and lut
11991  * @vsi: Pointer to VSI structure
11992  * @seed: Buffer to store the keys
11993  * @lut: Buffer to store the lookup table entries
11994  * @lut_size: Size of buffer to store the lookup table entries
11995  *
11996  * Returns 0 on success, negative on failure
11997  */
11998 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
11999 {
12000 	struct i40e_pf *pf = vsi->back;
12001 
12002 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12003 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12004 	else
12005 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12006 }
12007 
12008 /**
12009  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12010  * @pf: Pointer to board private structure
12011  * @lut: Lookup table
12012  * @rss_table_size: Lookup table size
12013  * @rss_size: Range of queue number for hashing
12014  */
12015 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12016 		       u16 rss_table_size, u16 rss_size)
12017 {
12018 	u16 i;
12019 
12020 	for (i = 0; i < rss_table_size; i++)
12021 		lut[i] = i % rss_size;
12022 }
12023 
12024 /**
12025  * i40e_pf_config_rss - Prepare for RSS if used
12026  * @pf: board private structure
12027  **/
12028 static int i40e_pf_config_rss(struct i40e_pf *pf)
12029 {
12030 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12031 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12032 	u8 *lut;
12033 	struct i40e_hw *hw = &pf->hw;
12034 	u32 reg_val;
12035 	u64 hena;
12036 	int ret;
12037 
12038 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12039 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12040 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12041 	hena |= i40e_pf_get_default_rss_hena(pf);
12042 
12043 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12044 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12045 
12046 	/* Determine the RSS table size based on the hardware capabilities */
12047 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12048 	reg_val = (pf->rss_table_size == 512) ?
12049 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12050 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12051 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12052 
12053 	/* Determine the RSS size of the VSI */
12054 	if (!vsi->rss_size) {
12055 		u16 qcount;
12056 		/* If the firmware does something weird during VSI init, we
12057 		 * could end up with zero TCs. Check for that to avoid
12058 		 * divide-by-zero. It probably won't pass traffic, but it also
12059 		 * won't panic.
12060 		 */
12061 		qcount = vsi->num_queue_pairs /
12062 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12063 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12064 	}
12065 	if (!vsi->rss_size)
12066 		return -EINVAL;
12067 
12068 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12069 	if (!lut)
12070 		return -ENOMEM;
12071 
12072 	/* Use user configured lut if there is one, otherwise use default */
12073 	if (vsi->rss_lut_user)
12074 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12075 	else
12076 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12077 
12078 	/* Use user configured hash key if there is one, otherwise
12079 	 * use default.
12080 	 */
12081 	if (vsi->rss_hkey_user)
12082 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12083 	else
12084 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12085 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12086 	kfree(lut);
12087 
12088 	return ret;
12089 }
12090 
12091 /**
12092  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12093  * @pf: board private structure
12094  * @queue_count: the requested queue count for rss.
12095  *
12096  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12097  * count which may be different from the requested queue count.
12098  * Note: expects to be called while under rtnl_lock()
12099  **/
12100 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12101 {
12102 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12103 	int new_rss_size;
12104 
12105 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12106 		return 0;
12107 
12108 	queue_count = min_t(int, queue_count, num_online_cpus());
12109 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12110 
12111 	if (queue_count != vsi->num_queue_pairs) {
12112 		u16 qcount;
12113 
12114 		vsi->req_queue_pairs = queue_count;
12115 		i40e_prep_for_reset(pf);
12116 
12117 		pf->alloc_rss_size = new_rss_size;
12118 
12119 		i40e_reset_and_rebuild(pf, true, true);
12120 
12121 		/* Discard the user configured hash keys and lut, if less
12122 		 * queues are enabled.
12123 		 */
12124 		if (queue_count < vsi->rss_size) {
12125 			i40e_clear_rss_config_user(vsi);
12126 			dev_dbg(&pf->pdev->dev,
12127 				"discard user configured hash keys and lut\n");
12128 		}
12129 
12130 		/* Reset vsi->rss_size, as number of enabled queues changed */
12131 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12132 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12133 
12134 		i40e_pf_config_rss(pf);
12135 	}
12136 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12137 		 vsi->req_queue_pairs, pf->rss_size_max);
12138 	return pf->alloc_rss_size;
12139 }
12140 
12141 /**
12142  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12143  * @pf: board private structure
12144  **/
12145 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
12146 {
12147 	i40e_status status;
12148 	bool min_valid, max_valid;
12149 	u32 max_bw, min_bw;
12150 
12151 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12152 					   &min_valid, &max_valid);
12153 
12154 	if (!status) {
12155 		if (min_valid)
12156 			pf->min_bw = min_bw;
12157 		if (max_valid)
12158 			pf->max_bw = max_bw;
12159 	}
12160 
12161 	return status;
12162 }
12163 
12164 /**
12165  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12166  * @pf: board private structure
12167  **/
12168 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
12169 {
12170 	struct i40e_aqc_configure_partition_bw_data bw_data;
12171 	i40e_status status;
12172 
12173 	memset(&bw_data, 0, sizeof(bw_data));
12174 
12175 	/* Set the valid bit for this PF */
12176 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12177 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12178 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12179 
12180 	/* Set the new bandwidths */
12181 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12182 
12183 	return status;
12184 }
12185 
12186 /**
12187  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12188  * @pf: board private structure
12189  **/
12190 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12191 {
12192 	/* Commit temporary BW setting to permanent NVM image */
12193 	enum i40e_admin_queue_err last_aq_status;
12194 	i40e_status ret;
12195 	u16 nvm_word;
12196 
12197 	if (pf->hw.partition_id != 1) {
12198 		dev_info(&pf->pdev->dev,
12199 			 "Commit BW only works on partition 1! This is partition %d",
12200 			 pf->hw.partition_id);
12201 		ret = I40E_NOT_SUPPORTED;
12202 		goto bw_commit_out;
12203 	}
12204 
12205 	/* Acquire NVM for read access */
12206 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12207 	last_aq_status = pf->hw.aq.asq_last_status;
12208 	if (ret) {
12209 		dev_info(&pf->pdev->dev,
12210 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
12211 			 i40e_stat_str(&pf->hw, ret),
12212 			 i40e_aq_str(&pf->hw, last_aq_status));
12213 		goto bw_commit_out;
12214 	}
12215 
12216 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12217 	ret = i40e_aq_read_nvm(&pf->hw,
12218 			       I40E_SR_NVM_CONTROL_WORD,
12219 			       0x10, sizeof(nvm_word), &nvm_word,
12220 			       false, NULL);
12221 	/* Save off last admin queue command status before releasing
12222 	 * the NVM
12223 	 */
12224 	last_aq_status = pf->hw.aq.asq_last_status;
12225 	i40e_release_nvm(&pf->hw);
12226 	if (ret) {
12227 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
12228 			 i40e_stat_str(&pf->hw, ret),
12229 			 i40e_aq_str(&pf->hw, last_aq_status));
12230 		goto bw_commit_out;
12231 	}
12232 
12233 	/* Wait a bit for NVM release to complete */
12234 	msleep(50);
12235 
12236 	/* Acquire NVM for write access */
12237 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12238 	last_aq_status = pf->hw.aq.asq_last_status;
12239 	if (ret) {
12240 		dev_info(&pf->pdev->dev,
12241 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
12242 			 i40e_stat_str(&pf->hw, ret),
12243 			 i40e_aq_str(&pf->hw, last_aq_status));
12244 		goto bw_commit_out;
12245 	}
12246 	/* Write it back out unchanged to initiate update NVM,
12247 	 * which will force a write of the shadow (alt) RAM to
12248 	 * the NVM - thus storing the bandwidth values permanently.
12249 	 */
12250 	ret = i40e_aq_update_nvm(&pf->hw,
12251 				 I40E_SR_NVM_CONTROL_WORD,
12252 				 0x10, sizeof(nvm_word),
12253 				 &nvm_word, true, 0, NULL);
12254 	/* Save off last admin queue command status before releasing
12255 	 * the NVM
12256 	 */
12257 	last_aq_status = pf->hw.aq.asq_last_status;
12258 	i40e_release_nvm(&pf->hw);
12259 	if (ret)
12260 		dev_info(&pf->pdev->dev,
12261 			 "BW settings NOT SAVED, err %s aq_err %s\n",
12262 			 i40e_stat_str(&pf->hw, ret),
12263 			 i40e_aq_str(&pf->hw, last_aq_status));
12264 bw_commit_out:
12265 
12266 	return ret;
12267 }
12268 
12269 /**
12270  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12271  * if total port shutdown feature is enabled for this PF
12272  * @pf: board private structure
12273  **/
12274 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12275 {
12276 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12277 #define I40E_FEATURES_ENABLE_PTR		0x2A
12278 #define I40E_CURRENT_SETTING_PTR		0x2B
12279 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12280 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12281 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12282 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12283 	i40e_status read_status = I40E_SUCCESS;
12284 	u16 sr_emp_sr_settings_ptr = 0;
12285 	u16 features_enable = 0;
12286 	u16 link_behavior = 0;
12287 	bool ret = false;
12288 
12289 	read_status = i40e_read_nvm_word(&pf->hw,
12290 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12291 					 &sr_emp_sr_settings_ptr);
12292 	if (read_status)
12293 		goto err_nvm;
12294 	read_status = i40e_read_nvm_word(&pf->hw,
12295 					 sr_emp_sr_settings_ptr +
12296 					 I40E_FEATURES_ENABLE_PTR,
12297 					 &features_enable);
12298 	if (read_status)
12299 		goto err_nvm;
12300 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12301 		read_status = i40e_read_nvm_module_data(&pf->hw,
12302 							I40E_SR_EMP_SR_SETTINGS_PTR,
12303 							I40E_CURRENT_SETTING_PTR,
12304 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12305 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12306 							&link_behavior);
12307 		if (read_status)
12308 			goto err_nvm;
12309 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12310 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12311 	}
12312 	return ret;
12313 
12314 err_nvm:
12315 	dev_warn(&pf->pdev->dev,
12316 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
12317 		 i40e_stat_str(&pf->hw, read_status));
12318 	return ret;
12319 }
12320 
12321 /**
12322  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12323  * @pf: board private structure to initialize
12324  *
12325  * i40e_sw_init initializes the Adapter private data structure.
12326  * Fields are initialized based on PCI device information and
12327  * OS network device settings (MTU size).
12328  **/
12329 static int i40e_sw_init(struct i40e_pf *pf)
12330 {
12331 	int err = 0;
12332 	int size;
12333 	u16 pow;
12334 
12335 	/* Set default capability flags */
12336 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12337 		    I40E_FLAG_MSI_ENABLED     |
12338 		    I40E_FLAG_MSIX_ENABLED;
12339 
12340 	/* Set default ITR */
12341 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12342 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12343 
12344 	/* Depending on PF configurations, it is possible that the RSS
12345 	 * maximum might end up larger than the available queues
12346 	 */
12347 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12348 	pf->alloc_rss_size = 1;
12349 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12350 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12351 				 pf->hw.func_caps.num_tx_qp);
12352 
12353 	/* find the next higher power-of-2 of num cpus */
12354 	pow = roundup_pow_of_two(num_online_cpus());
12355 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12356 
12357 	if (pf->hw.func_caps.rss) {
12358 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12359 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12360 					   num_online_cpus());
12361 	}
12362 
12363 	/* MFP mode enabled */
12364 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12365 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12366 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12367 		if (i40e_get_partition_bw_setting(pf)) {
12368 			dev_warn(&pf->pdev->dev,
12369 				 "Could not get partition bw settings\n");
12370 		} else {
12371 			dev_info(&pf->pdev->dev,
12372 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12373 				 pf->min_bw, pf->max_bw);
12374 
12375 			/* nudge the Tx scheduler */
12376 			i40e_set_partition_bw_setting(pf);
12377 		}
12378 	}
12379 
12380 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12381 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12382 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12383 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12384 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12385 		    pf->hw.num_partitions > 1)
12386 			dev_info(&pf->pdev->dev,
12387 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12388 		else
12389 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12390 		pf->fdir_pf_filter_count =
12391 				 pf->hw.func_caps.fd_filters_guaranteed;
12392 		pf->hw.fdir_shared_filter_count =
12393 				 pf->hw.func_caps.fd_filters_best_effort;
12394 	}
12395 
12396 	if (pf->hw.mac.type == I40E_MAC_X722) {
12397 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12398 				    I40E_HW_128_QP_RSS_CAPABLE |
12399 				    I40E_HW_ATR_EVICT_CAPABLE |
12400 				    I40E_HW_WB_ON_ITR_CAPABLE |
12401 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12402 				    I40E_HW_NO_PCI_LINK_CHECK |
12403 				    I40E_HW_USE_SET_LLDP_MIB |
12404 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12405 				    I40E_HW_PTP_L4_CAPABLE |
12406 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12407 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12408 
12409 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12410 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12411 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12412 			dev_warn(&pf->pdev->dev,
12413 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12414 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12415 		}
12416 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12417 		   ((pf->hw.aq.api_maj_ver == 1) &&
12418 		    (pf->hw.aq.api_min_ver > 4))) {
12419 		/* Supported in FW API version higher than 1.4 */
12420 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12421 	}
12422 
12423 	/* Enable HW ATR eviction if possible */
12424 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12425 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12426 
12427 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12428 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12429 	    (pf->hw.aq.fw_maj_ver < 4))) {
12430 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12431 		/* No DCB support  for FW < v4.33 */
12432 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12433 	}
12434 
12435 	/* Disable FW LLDP if FW < v4.3 */
12436 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12437 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12438 	    (pf->hw.aq.fw_maj_ver < 4)))
12439 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12440 
12441 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12442 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12443 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12444 	    (pf->hw.aq.fw_maj_ver >= 5)))
12445 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12446 
12447 	/* Enable PTP L4 if FW > v6.0 */
12448 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12449 	    pf->hw.aq.fw_maj_ver >= 6)
12450 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12451 
12452 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12453 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12454 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12455 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12456 	}
12457 
12458 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12459 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12460 		/* IWARP needs one extra vector for CQP just like MISC.*/
12461 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12462 	}
12463 	/* Stopping FW LLDP engine is supported on XL710 and X722
12464 	 * starting from FW versions determined in i40e_init_adminq.
12465 	 * Stopping the FW LLDP engine is not supported on XL710
12466 	 * if NPAR is functioning so unset this hw flag in this case.
12467 	 */
12468 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12469 	    pf->hw.func_caps.npar_enable &&
12470 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12471 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12472 
12473 #ifdef CONFIG_PCI_IOV
12474 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12475 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12476 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12477 		pf->num_req_vfs = min_t(int,
12478 					pf->hw.func_caps.num_vfs,
12479 					I40E_MAX_VF_COUNT);
12480 	}
12481 #endif /* CONFIG_PCI_IOV */
12482 	pf->eeprom_version = 0xDEAD;
12483 	pf->lan_veb = I40E_NO_VEB;
12484 	pf->lan_vsi = I40E_NO_VSI;
12485 
12486 	/* By default FW has this off for performance reasons */
12487 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12488 
12489 	/* set up queue assignment tracking */
12490 	size = sizeof(struct i40e_lump_tracking)
12491 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12492 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12493 	if (!pf->qp_pile) {
12494 		err = -ENOMEM;
12495 		goto sw_init_done;
12496 	}
12497 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12498 	pf->qp_pile->search_hint = 0;
12499 
12500 	pf->tx_timeout_recovery_level = 1;
12501 
12502 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12503 	    i40e_is_total_port_shutdown_enabled(pf)) {
12504 		/* Link down on close must be on when total port shutdown
12505 		 * is enabled for a given port
12506 		 */
12507 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12508 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12509 		dev_info(&pf->pdev->dev,
12510 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12511 	}
12512 	mutex_init(&pf->switch_mutex);
12513 
12514 sw_init_done:
12515 	return err;
12516 }
12517 
12518 /**
12519  * i40e_set_ntuple - set the ntuple feature flag and take action
12520  * @pf: board private structure to initialize
12521  * @features: the feature set that the stack is suggesting
12522  *
12523  * returns a bool to indicate if reset needs to happen
12524  **/
12525 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12526 {
12527 	bool need_reset = false;
12528 
12529 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12530 	 * the state changed, we need to reset.
12531 	 */
12532 	if (features & NETIF_F_NTUPLE) {
12533 		/* Enable filters and mark for reset */
12534 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12535 			need_reset = true;
12536 		/* enable FD_SB only if there is MSI-X vector and no cloud
12537 		 * filters exist
12538 		 */
12539 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12540 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12541 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12542 		}
12543 	} else {
12544 		/* turn off filters, mark for reset and clear SW filter list */
12545 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12546 			need_reset = true;
12547 			i40e_fdir_filter_exit(pf);
12548 		}
12549 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12550 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12551 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12552 
12553 		/* reset fd counters */
12554 		pf->fd_add_err = 0;
12555 		pf->fd_atr_cnt = 0;
12556 		/* if ATR was auto disabled it can be re-enabled. */
12557 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12558 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12559 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12560 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12561 	}
12562 	return need_reset;
12563 }
12564 
12565 /**
12566  * i40e_clear_rss_lut - clear the rx hash lookup table
12567  * @vsi: the VSI being configured
12568  **/
12569 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12570 {
12571 	struct i40e_pf *pf = vsi->back;
12572 	struct i40e_hw *hw = &pf->hw;
12573 	u16 vf_id = vsi->vf_id;
12574 	u8 i;
12575 
12576 	if (vsi->type == I40E_VSI_MAIN) {
12577 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12578 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12579 	} else if (vsi->type == I40E_VSI_SRIOV) {
12580 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12581 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12582 	} else {
12583 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12584 	}
12585 }
12586 
12587 /**
12588  * i40e_set_features - set the netdev feature flags
12589  * @netdev: ptr to the netdev being adjusted
12590  * @features: the feature set that the stack is suggesting
12591  * Note: expects to be called while under rtnl_lock()
12592  **/
12593 static int i40e_set_features(struct net_device *netdev,
12594 			     netdev_features_t features)
12595 {
12596 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12597 	struct i40e_vsi *vsi = np->vsi;
12598 	struct i40e_pf *pf = vsi->back;
12599 	bool need_reset;
12600 
12601 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12602 		i40e_pf_config_rss(pf);
12603 	else if (!(features & NETIF_F_RXHASH) &&
12604 		 netdev->features & NETIF_F_RXHASH)
12605 		i40e_clear_rss_lut(vsi);
12606 
12607 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12608 		i40e_vlan_stripping_enable(vsi);
12609 	else
12610 		i40e_vlan_stripping_disable(vsi);
12611 
12612 	if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12613 		dev_err(&pf->pdev->dev,
12614 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12615 		return -EINVAL;
12616 	}
12617 
12618 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12619 		i40e_del_all_macvlans(vsi);
12620 
12621 	need_reset = i40e_set_ntuple(pf, features);
12622 
12623 	if (need_reset)
12624 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12625 
12626 	return 0;
12627 }
12628 
12629 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12630 				    unsigned int table, unsigned int idx,
12631 				    struct udp_tunnel_info *ti)
12632 {
12633 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12634 	struct i40e_hw *hw = &np->vsi->back->hw;
12635 	u8 type, filter_index;
12636 	i40e_status ret;
12637 
12638 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12639 						   I40E_AQC_TUNNEL_TYPE_NGE;
12640 
12641 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12642 				     NULL);
12643 	if (ret) {
12644 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12645 			    i40e_stat_str(hw, ret),
12646 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12647 		return -EIO;
12648 	}
12649 
12650 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12651 	return 0;
12652 }
12653 
12654 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12655 				      unsigned int table, unsigned int idx,
12656 				      struct udp_tunnel_info *ti)
12657 {
12658 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12659 	struct i40e_hw *hw = &np->vsi->back->hw;
12660 	i40e_status ret;
12661 
12662 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12663 	if (ret) {
12664 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
12665 			    i40e_stat_str(hw, ret),
12666 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12667 		return -EIO;
12668 	}
12669 
12670 	return 0;
12671 }
12672 
12673 static int i40e_get_phys_port_id(struct net_device *netdev,
12674 				 struct netdev_phys_item_id *ppid)
12675 {
12676 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12677 	struct i40e_pf *pf = np->vsi->back;
12678 	struct i40e_hw *hw = &pf->hw;
12679 
12680 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
12681 		return -EOPNOTSUPP;
12682 
12683 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
12684 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
12685 
12686 	return 0;
12687 }
12688 
12689 /**
12690  * i40e_ndo_fdb_add - add an entry to the hardware database
12691  * @ndm: the input from the stack
12692  * @tb: pointer to array of nladdr (unused)
12693  * @dev: the net device pointer
12694  * @addr: the MAC address entry being added
12695  * @vid: VLAN ID
12696  * @flags: instructions from stack about fdb operation
12697  * @extack: netlink extended ack, unused currently
12698  */
12699 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
12700 			    struct net_device *dev,
12701 			    const unsigned char *addr, u16 vid,
12702 			    u16 flags,
12703 			    struct netlink_ext_ack *extack)
12704 {
12705 	struct i40e_netdev_priv *np = netdev_priv(dev);
12706 	struct i40e_pf *pf = np->vsi->back;
12707 	int err = 0;
12708 
12709 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
12710 		return -EOPNOTSUPP;
12711 
12712 	if (vid) {
12713 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
12714 		return -EINVAL;
12715 	}
12716 
12717 	/* Hardware does not support aging addresses so if a
12718 	 * ndm_state is given only allow permanent addresses
12719 	 */
12720 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
12721 		netdev_info(dev, "FDB only supports static addresses\n");
12722 		return -EINVAL;
12723 	}
12724 
12725 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
12726 		err = dev_uc_add_excl(dev, addr);
12727 	else if (is_multicast_ether_addr(addr))
12728 		err = dev_mc_add_excl(dev, addr);
12729 	else
12730 		err = -EINVAL;
12731 
12732 	/* Only return duplicate errors if NLM_F_EXCL is set */
12733 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
12734 		err = 0;
12735 
12736 	return err;
12737 }
12738 
12739 /**
12740  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
12741  * @dev: the netdev being configured
12742  * @nlh: RTNL message
12743  * @flags: bridge flags
12744  * @extack: netlink extended ack
12745  *
12746  * Inserts a new hardware bridge if not already created and
12747  * enables the bridging mode requested (VEB or VEPA). If the
12748  * hardware bridge has already been inserted and the request
12749  * is to change the mode then that requires a PF reset to
12750  * allow rebuild of the components with required hardware
12751  * bridge mode enabled.
12752  *
12753  * Note: expects to be called while under rtnl_lock()
12754  **/
12755 static int i40e_ndo_bridge_setlink(struct net_device *dev,
12756 				   struct nlmsghdr *nlh,
12757 				   u16 flags,
12758 				   struct netlink_ext_ack *extack)
12759 {
12760 	struct i40e_netdev_priv *np = netdev_priv(dev);
12761 	struct i40e_vsi *vsi = np->vsi;
12762 	struct i40e_pf *pf = vsi->back;
12763 	struct i40e_veb *veb = NULL;
12764 	struct nlattr *attr, *br_spec;
12765 	int i, rem;
12766 
12767 	/* Only for PF VSI for now */
12768 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12769 		return -EOPNOTSUPP;
12770 
12771 	/* Find the HW bridge for PF VSI */
12772 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12773 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12774 			veb = pf->veb[i];
12775 	}
12776 
12777 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
12778 
12779 	nla_for_each_nested(attr, br_spec, rem) {
12780 		__u16 mode;
12781 
12782 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
12783 			continue;
12784 
12785 		mode = nla_get_u16(attr);
12786 		if ((mode != BRIDGE_MODE_VEPA) &&
12787 		    (mode != BRIDGE_MODE_VEB))
12788 			return -EINVAL;
12789 
12790 		/* Insert a new HW bridge */
12791 		if (!veb) {
12792 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12793 					     vsi->tc_config.enabled_tc);
12794 			if (veb) {
12795 				veb->bridge_mode = mode;
12796 				i40e_config_bridge_mode(veb);
12797 			} else {
12798 				/* No Bridge HW offload available */
12799 				return -ENOENT;
12800 			}
12801 			break;
12802 		} else if (mode != veb->bridge_mode) {
12803 			/* Existing HW bridge but different mode needs reset */
12804 			veb->bridge_mode = mode;
12805 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
12806 			if (mode == BRIDGE_MODE_VEB)
12807 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
12808 			else
12809 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12810 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12811 			break;
12812 		}
12813 	}
12814 
12815 	return 0;
12816 }
12817 
12818 /**
12819  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
12820  * @skb: skb buff
12821  * @pid: process id
12822  * @seq: RTNL message seq #
12823  * @dev: the netdev being configured
12824  * @filter_mask: unused
12825  * @nlflags: netlink flags passed in
12826  *
12827  * Return the mode in which the hardware bridge is operating in
12828  * i.e VEB or VEPA.
12829  **/
12830 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
12831 				   struct net_device *dev,
12832 				   u32 __always_unused filter_mask,
12833 				   int nlflags)
12834 {
12835 	struct i40e_netdev_priv *np = netdev_priv(dev);
12836 	struct i40e_vsi *vsi = np->vsi;
12837 	struct i40e_pf *pf = vsi->back;
12838 	struct i40e_veb *veb = NULL;
12839 	int i;
12840 
12841 	/* Only for PF VSI for now */
12842 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12843 		return -EOPNOTSUPP;
12844 
12845 	/* Find the HW bridge for the PF VSI */
12846 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12847 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12848 			veb = pf->veb[i];
12849 	}
12850 
12851 	if (!veb)
12852 		return 0;
12853 
12854 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
12855 				       0, 0, nlflags, filter_mask, NULL);
12856 }
12857 
12858 /**
12859  * i40e_features_check - Validate encapsulated packet conforms to limits
12860  * @skb: skb buff
12861  * @dev: This physical port's netdev
12862  * @features: Offload features that the stack believes apply
12863  **/
12864 static netdev_features_t i40e_features_check(struct sk_buff *skb,
12865 					     struct net_device *dev,
12866 					     netdev_features_t features)
12867 {
12868 	size_t len;
12869 
12870 	/* No point in doing any of this if neither checksum nor GSO are
12871 	 * being requested for this frame.  We can rule out both by just
12872 	 * checking for CHECKSUM_PARTIAL
12873 	 */
12874 	if (skb->ip_summed != CHECKSUM_PARTIAL)
12875 		return features;
12876 
12877 	/* We cannot support GSO if the MSS is going to be less than
12878 	 * 64 bytes.  If it is then we need to drop support for GSO.
12879 	 */
12880 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
12881 		features &= ~NETIF_F_GSO_MASK;
12882 
12883 	/* MACLEN can support at most 63 words */
12884 	len = skb_network_header(skb) - skb->data;
12885 	if (len & ~(63 * 2))
12886 		goto out_err;
12887 
12888 	/* IPLEN and EIPLEN can support at most 127 dwords */
12889 	len = skb_transport_header(skb) - skb_network_header(skb);
12890 	if (len & ~(127 * 4))
12891 		goto out_err;
12892 
12893 	if (skb->encapsulation) {
12894 		/* L4TUNLEN can support 127 words */
12895 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
12896 		if (len & ~(127 * 2))
12897 			goto out_err;
12898 
12899 		/* IPLEN can support at most 127 dwords */
12900 		len = skb_inner_transport_header(skb) -
12901 		      skb_inner_network_header(skb);
12902 		if (len & ~(127 * 4))
12903 			goto out_err;
12904 	}
12905 
12906 	/* No need to validate L4LEN as TCP is the only protocol with a
12907 	 * a flexible value and we support all possible values supported
12908 	 * by TCP, which is at most 15 dwords
12909 	 */
12910 
12911 	return features;
12912 out_err:
12913 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
12914 }
12915 
12916 /**
12917  * i40e_xdp_setup - add/remove an XDP program
12918  * @vsi: VSI to changed
12919  * @prog: XDP program
12920  * @extack: netlink extended ack
12921  **/
12922 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
12923 			  struct netlink_ext_ack *extack)
12924 {
12925 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
12926 	struct i40e_pf *pf = vsi->back;
12927 	struct bpf_prog *old_prog;
12928 	bool need_reset;
12929 	int i;
12930 
12931 	/* Don't allow frames that span over multiple buffers */
12932 	if (frame_size > vsi->rx_buf_len) {
12933 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
12934 		return -EINVAL;
12935 	}
12936 
12937 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
12938 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
12939 
12940 	if (need_reset)
12941 		i40e_prep_for_reset(pf);
12942 
12943 	old_prog = xchg(&vsi->xdp_prog, prog);
12944 
12945 	if (need_reset) {
12946 		if (!prog)
12947 			/* Wait until ndo_xsk_wakeup completes. */
12948 			synchronize_rcu();
12949 		i40e_reset_and_rebuild(pf, true, true);
12950 	}
12951 
12952 	for (i = 0; i < vsi->num_queue_pairs; i++)
12953 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
12954 
12955 	if (old_prog)
12956 		bpf_prog_put(old_prog);
12957 
12958 	/* Kick start the NAPI context if there is an AF_XDP socket open
12959 	 * on that queue id. This so that receiving will start.
12960 	 */
12961 	if (need_reset && prog)
12962 		for (i = 0; i < vsi->num_queue_pairs; i++)
12963 			if (vsi->xdp_rings[i]->xsk_pool)
12964 				(void)i40e_xsk_wakeup(vsi->netdev, i,
12965 						      XDP_WAKEUP_RX);
12966 
12967 	return 0;
12968 }
12969 
12970 /**
12971  * i40e_enter_busy_conf - Enters busy config state
12972  * @vsi: vsi
12973  *
12974  * Returns 0 on success, <0 for failure.
12975  **/
12976 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
12977 {
12978 	struct i40e_pf *pf = vsi->back;
12979 	int timeout = 50;
12980 
12981 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
12982 		timeout--;
12983 		if (!timeout)
12984 			return -EBUSY;
12985 		usleep_range(1000, 2000);
12986 	}
12987 
12988 	return 0;
12989 }
12990 
12991 /**
12992  * i40e_exit_busy_conf - Exits busy config state
12993  * @vsi: vsi
12994  **/
12995 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
12996 {
12997 	struct i40e_pf *pf = vsi->back;
12998 
12999 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13000 }
13001 
13002 /**
13003  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13004  * @vsi: vsi
13005  * @queue_pair: queue pair
13006  **/
13007 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13008 {
13009 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13010 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13011 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13012 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13013 	if (i40e_enabled_xdp_vsi(vsi)) {
13014 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13015 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13016 	}
13017 }
13018 
13019 /**
13020  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13021  * @vsi: vsi
13022  * @queue_pair: queue pair
13023  **/
13024 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13025 {
13026 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13027 	if (i40e_enabled_xdp_vsi(vsi)) {
13028 		/* Make sure that in-progress ndo_xdp_xmit calls are
13029 		 * completed.
13030 		 */
13031 		synchronize_rcu();
13032 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13033 	}
13034 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13035 }
13036 
13037 /**
13038  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13039  * @vsi: vsi
13040  * @queue_pair: queue pair
13041  * @enable: true for enable, false for disable
13042  **/
13043 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13044 					bool enable)
13045 {
13046 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13047 	struct i40e_q_vector *q_vector = rxr->q_vector;
13048 
13049 	if (!vsi->netdev)
13050 		return;
13051 
13052 	/* All rings in a qp belong to the same qvector. */
13053 	if (q_vector->rx.ring || q_vector->tx.ring) {
13054 		if (enable)
13055 			napi_enable(&q_vector->napi);
13056 		else
13057 			napi_disable(&q_vector->napi);
13058 	}
13059 }
13060 
13061 /**
13062  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13063  * @vsi: vsi
13064  * @queue_pair: queue pair
13065  * @enable: true for enable, false for disable
13066  *
13067  * Returns 0 on success, <0 on failure.
13068  **/
13069 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13070 					bool enable)
13071 {
13072 	struct i40e_pf *pf = vsi->back;
13073 	int pf_q, ret = 0;
13074 
13075 	pf_q = vsi->base_queue + queue_pair;
13076 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13077 				     false /*is xdp*/, enable);
13078 	if (ret) {
13079 		dev_info(&pf->pdev->dev,
13080 			 "VSI seid %d Tx ring %d %sable timeout\n",
13081 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13082 		return ret;
13083 	}
13084 
13085 	i40e_control_rx_q(pf, pf_q, enable);
13086 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13087 	if (ret) {
13088 		dev_info(&pf->pdev->dev,
13089 			 "VSI seid %d Rx ring %d %sable timeout\n",
13090 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13091 		return ret;
13092 	}
13093 
13094 	/* Due to HW errata, on Rx disable only, the register can
13095 	 * indicate done before it really is. Needs 50ms to be sure
13096 	 */
13097 	if (!enable)
13098 		mdelay(50);
13099 
13100 	if (!i40e_enabled_xdp_vsi(vsi))
13101 		return ret;
13102 
13103 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13104 				     pf_q + vsi->alloc_queue_pairs,
13105 				     true /*is xdp*/, enable);
13106 	if (ret) {
13107 		dev_info(&pf->pdev->dev,
13108 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13109 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13110 	}
13111 
13112 	return ret;
13113 }
13114 
13115 /**
13116  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13117  * @vsi: vsi
13118  * @queue_pair: queue_pair
13119  **/
13120 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13121 {
13122 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13123 	struct i40e_pf *pf = vsi->back;
13124 	struct i40e_hw *hw = &pf->hw;
13125 
13126 	/* All rings in a qp belong to the same qvector. */
13127 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13128 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13129 	else
13130 		i40e_irq_dynamic_enable_icr0(pf);
13131 
13132 	i40e_flush(hw);
13133 }
13134 
13135 /**
13136  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13137  * @vsi: vsi
13138  * @queue_pair: queue_pair
13139  **/
13140 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13141 {
13142 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13143 	struct i40e_pf *pf = vsi->back;
13144 	struct i40e_hw *hw = &pf->hw;
13145 
13146 	/* For simplicity, instead of removing the qp interrupt causes
13147 	 * from the interrupt linked list, we simply disable the interrupt, and
13148 	 * leave the list intact.
13149 	 *
13150 	 * All rings in a qp belong to the same qvector.
13151 	 */
13152 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13153 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13154 
13155 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13156 		i40e_flush(hw);
13157 		synchronize_irq(pf->msix_entries[intpf].vector);
13158 	} else {
13159 		/* Legacy and MSI mode - this stops all interrupt handling */
13160 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13161 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13162 		i40e_flush(hw);
13163 		synchronize_irq(pf->pdev->irq);
13164 	}
13165 }
13166 
13167 /**
13168  * i40e_queue_pair_disable - Disables a queue pair
13169  * @vsi: vsi
13170  * @queue_pair: queue pair
13171  *
13172  * Returns 0 on success, <0 on failure.
13173  **/
13174 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13175 {
13176 	int err;
13177 
13178 	err = i40e_enter_busy_conf(vsi);
13179 	if (err)
13180 		return err;
13181 
13182 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13183 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13184 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13185 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13186 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13187 
13188 	return err;
13189 }
13190 
13191 /**
13192  * i40e_queue_pair_enable - Enables a queue pair
13193  * @vsi: vsi
13194  * @queue_pair: queue pair
13195  *
13196  * Returns 0 on success, <0 on failure.
13197  **/
13198 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13199 {
13200 	int err;
13201 
13202 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13203 	if (err)
13204 		return err;
13205 
13206 	if (i40e_enabled_xdp_vsi(vsi)) {
13207 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13208 		if (err)
13209 			return err;
13210 	}
13211 
13212 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13213 	if (err)
13214 		return err;
13215 
13216 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13217 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13218 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13219 
13220 	i40e_exit_busy_conf(vsi);
13221 
13222 	return err;
13223 }
13224 
13225 /**
13226  * i40e_xdp - implements ndo_bpf for i40e
13227  * @dev: netdevice
13228  * @xdp: XDP command
13229  **/
13230 static int i40e_xdp(struct net_device *dev,
13231 		    struct netdev_bpf *xdp)
13232 {
13233 	struct i40e_netdev_priv *np = netdev_priv(dev);
13234 	struct i40e_vsi *vsi = np->vsi;
13235 
13236 	if (vsi->type != I40E_VSI_MAIN)
13237 		return -EINVAL;
13238 
13239 	switch (xdp->command) {
13240 	case XDP_SETUP_PROG:
13241 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13242 	case XDP_SETUP_XSK_POOL:
13243 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13244 					   xdp->xsk.queue_id);
13245 	default:
13246 		return -EINVAL;
13247 	}
13248 }
13249 
13250 static const struct net_device_ops i40e_netdev_ops = {
13251 	.ndo_open		= i40e_open,
13252 	.ndo_stop		= i40e_close,
13253 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13254 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13255 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13256 	.ndo_validate_addr	= eth_validate_addr,
13257 	.ndo_set_mac_address	= i40e_set_mac,
13258 	.ndo_change_mtu		= i40e_change_mtu,
13259 	.ndo_eth_ioctl		= i40e_ioctl,
13260 	.ndo_tx_timeout		= i40e_tx_timeout,
13261 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13262 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13263 #ifdef CONFIG_NET_POLL_CONTROLLER
13264 	.ndo_poll_controller	= i40e_netpoll,
13265 #endif
13266 	.ndo_setup_tc		= __i40e_setup_tc,
13267 	.ndo_set_features	= i40e_set_features,
13268 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13269 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13270 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13271 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13272 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13273 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13274 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13275 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13276 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13277 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13278 	.ndo_features_check	= i40e_features_check,
13279 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13280 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13281 	.ndo_bpf		= i40e_xdp,
13282 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13283 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13284 	.ndo_dfwd_add_station	= i40e_fwd_add,
13285 	.ndo_dfwd_del_station	= i40e_fwd_del,
13286 };
13287 
13288 /**
13289  * i40e_config_netdev - Setup the netdev flags
13290  * @vsi: the VSI being configured
13291  *
13292  * Returns 0 on success, negative value on failure
13293  **/
13294 static int i40e_config_netdev(struct i40e_vsi *vsi)
13295 {
13296 	struct i40e_pf *pf = vsi->back;
13297 	struct i40e_hw *hw = &pf->hw;
13298 	struct i40e_netdev_priv *np;
13299 	struct net_device *netdev;
13300 	u8 broadcast[ETH_ALEN];
13301 	u8 mac_addr[ETH_ALEN];
13302 	int etherdev_size;
13303 	netdev_features_t hw_enc_features;
13304 	netdev_features_t hw_features;
13305 
13306 	etherdev_size = sizeof(struct i40e_netdev_priv);
13307 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13308 	if (!netdev)
13309 		return -ENOMEM;
13310 
13311 	vsi->netdev = netdev;
13312 	np = netdev_priv(netdev);
13313 	np->vsi = vsi;
13314 
13315 	hw_enc_features = NETIF_F_SG			|
13316 			  NETIF_F_IP_CSUM		|
13317 			  NETIF_F_IPV6_CSUM		|
13318 			  NETIF_F_HIGHDMA		|
13319 			  NETIF_F_SOFT_FEATURES		|
13320 			  NETIF_F_TSO			|
13321 			  NETIF_F_TSO_ECN		|
13322 			  NETIF_F_TSO6			|
13323 			  NETIF_F_GSO_GRE		|
13324 			  NETIF_F_GSO_GRE_CSUM		|
13325 			  NETIF_F_GSO_PARTIAL		|
13326 			  NETIF_F_GSO_IPXIP4		|
13327 			  NETIF_F_GSO_IPXIP6		|
13328 			  NETIF_F_GSO_UDP_TUNNEL	|
13329 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13330 			  NETIF_F_GSO_UDP_L4		|
13331 			  NETIF_F_SCTP_CRC		|
13332 			  NETIF_F_RXHASH		|
13333 			  NETIF_F_RXCSUM		|
13334 			  0;
13335 
13336 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13337 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13338 
13339 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13340 
13341 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13342 
13343 	netdev->hw_enc_features |= hw_enc_features;
13344 
13345 	/* record features VLANs can make use of */
13346 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13347 
13348 	/* enable macvlan offloads */
13349 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13350 
13351 	hw_features = hw_enc_features		|
13352 		      NETIF_F_HW_VLAN_CTAG_TX	|
13353 		      NETIF_F_HW_VLAN_CTAG_RX;
13354 
13355 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13356 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13357 
13358 	netdev->hw_features |= hw_features;
13359 
13360 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13361 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13362 
13363 	if (vsi->type == I40E_VSI_MAIN) {
13364 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13365 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13366 		/* The following steps are necessary for two reasons. First,
13367 		 * some older NVM configurations load a default MAC-VLAN
13368 		 * filter that will accept any tagged packet, and we want to
13369 		 * replace this with a normal filter. Additionally, it is
13370 		 * possible our MAC address was provided by the platform using
13371 		 * Open Firmware or similar.
13372 		 *
13373 		 * Thus, we need to remove the default filter and install one
13374 		 * specific to the MAC address.
13375 		 */
13376 		i40e_rm_default_mac_filter(vsi, mac_addr);
13377 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13378 		i40e_add_mac_filter(vsi, mac_addr);
13379 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13380 	} else {
13381 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13382 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13383 		 * the end, which is 4 bytes long, so force truncation of the
13384 		 * original name by IFNAMSIZ - 4
13385 		 */
13386 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13387 			 IFNAMSIZ - 4,
13388 			 pf->vsi[pf->lan_vsi]->netdev->name);
13389 		eth_random_addr(mac_addr);
13390 
13391 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13392 		i40e_add_mac_filter(vsi, mac_addr);
13393 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13394 	}
13395 
13396 	/* Add the broadcast filter so that we initially will receive
13397 	 * broadcast packets. Note that when a new VLAN is first added the
13398 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13399 	 * specific filters as part of transitioning into "vlan" operation.
13400 	 * When more VLANs are added, the driver will copy each existing MAC
13401 	 * filter and add it for the new VLAN.
13402 	 *
13403 	 * Broadcast filters are handled specially by
13404 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13405 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13406 	 * filter. The subtask will update the correct broadcast promiscuous
13407 	 * bits as VLANs become active or inactive.
13408 	 */
13409 	eth_broadcast_addr(broadcast);
13410 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13411 	i40e_add_mac_filter(vsi, broadcast);
13412 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13413 
13414 	ether_addr_copy(netdev->dev_addr, mac_addr);
13415 	ether_addr_copy(netdev->perm_addr, mac_addr);
13416 
13417 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13418 	netdev->neigh_priv_len = sizeof(u32) * 4;
13419 
13420 	netdev->priv_flags |= IFF_UNICAST_FLT;
13421 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13422 	/* Setup netdev TC information */
13423 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13424 
13425 	netdev->netdev_ops = &i40e_netdev_ops;
13426 	netdev->watchdog_timeo = 5 * HZ;
13427 	i40e_set_ethtool_ops(netdev);
13428 
13429 	/* MTU range: 68 - 9706 */
13430 	netdev->min_mtu = ETH_MIN_MTU;
13431 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13432 
13433 	return 0;
13434 }
13435 
13436 /**
13437  * i40e_vsi_delete - Delete a VSI from the switch
13438  * @vsi: the VSI being removed
13439  *
13440  * Returns 0 on success, negative value on failure
13441  **/
13442 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13443 {
13444 	/* remove default VSI is not allowed */
13445 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13446 		return;
13447 
13448 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13449 }
13450 
13451 /**
13452  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13453  * @vsi: the VSI being queried
13454  *
13455  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13456  **/
13457 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13458 {
13459 	struct i40e_veb *veb;
13460 	struct i40e_pf *pf = vsi->back;
13461 
13462 	/* Uplink is not a bridge so default to VEB */
13463 	if (vsi->veb_idx >= I40E_MAX_VEB)
13464 		return 1;
13465 
13466 	veb = pf->veb[vsi->veb_idx];
13467 	if (!veb) {
13468 		dev_info(&pf->pdev->dev,
13469 			 "There is no veb associated with the bridge\n");
13470 		return -ENOENT;
13471 	}
13472 
13473 	/* Uplink is a bridge in VEPA mode */
13474 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13475 		return 0;
13476 	} else {
13477 		/* Uplink is a bridge in VEB mode */
13478 		return 1;
13479 	}
13480 
13481 	/* VEPA is now default bridge, so return 0 */
13482 	return 0;
13483 }
13484 
13485 /**
13486  * i40e_add_vsi - Add a VSI to the switch
13487  * @vsi: the VSI being configured
13488  *
13489  * This initializes a VSI context depending on the VSI type to be added and
13490  * passes it down to the add_vsi aq command.
13491  **/
13492 static int i40e_add_vsi(struct i40e_vsi *vsi)
13493 {
13494 	int ret = -ENODEV;
13495 	struct i40e_pf *pf = vsi->back;
13496 	struct i40e_hw *hw = &pf->hw;
13497 	struct i40e_vsi_context ctxt;
13498 	struct i40e_mac_filter *f;
13499 	struct hlist_node *h;
13500 	int bkt;
13501 
13502 	u8 enabled_tc = 0x1; /* TC0 enabled */
13503 	int f_count = 0;
13504 
13505 	memset(&ctxt, 0, sizeof(ctxt));
13506 	switch (vsi->type) {
13507 	case I40E_VSI_MAIN:
13508 		/* The PF's main VSI is already setup as part of the
13509 		 * device initialization, so we'll not bother with
13510 		 * the add_vsi call, but we will retrieve the current
13511 		 * VSI context.
13512 		 */
13513 		ctxt.seid = pf->main_vsi_seid;
13514 		ctxt.pf_num = pf->hw.pf_id;
13515 		ctxt.vf_num = 0;
13516 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13517 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13518 		if (ret) {
13519 			dev_info(&pf->pdev->dev,
13520 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13521 				 i40e_stat_str(&pf->hw, ret),
13522 				 i40e_aq_str(&pf->hw,
13523 					     pf->hw.aq.asq_last_status));
13524 			return -ENOENT;
13525 		}
13526 		vsi->info = ctxt.info;
13527 		vsi->info.valid_sections = 0;
13528 
13529 		vsi->seid = ctxt.seid;
13530 		vsi->id = ctxt.vsi_number;
13531 
13532 		enabled_tc = i40e_pf_get_tc_map(pf);
13533 
13534 		/* Source pruning is enabled by default, so the flag is
13535 		 * negative logic - if it's set, we need to fiddle with
13536 		 * the VSI to disable source pruning.
13537 		 */
13538 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13539 			memset(&ctxt, 0, sizeof(ctxt));
13540 			ctxt.seid = pf->main_vsi_seid;
13541 			ctxt.pf_num = pf->hw.pf_id;
13542 			ctxt.vf_num = 0;
13543 			ctxt.info.valid_sections |=
13544 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13545 			ctxt.info.switch_id =
13546 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13547 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13548 			if (ret) {
13549 				dev_info(&pf->pdev->dev,
13550 					 "update vsi failed, err %s aq_err %s\n",
13551 					 i40e_stat_str(&pf->hw, ret),
13552 					 i40e_aq_str(&pf->hw,
13553 						     pf->hw.aq.asq_last_status));
13554 				ret = -ENOENT;
13555 				goto err;
13556 			}
13557 		}
13558 
13559 		/* MFP mode setup queue map and update VSI */
13560 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13561 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13562 			memset(&ctxt, 0, sizeof(ctxt));
13563 			ctxt.seid = pf->main_vsi_seid;
13564 			ctxt.pf_num = pf->hw.pf_id;
13565 			ctxt.vf_num = 0;
13566 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13567 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13568 			if (ret) {
13569 				dev_info(&pf->pdev->dev,
13570 					 "update vsi failed, err %s aq_err %s\n",
13571 					 i40e_stat_str(&pf->hw, ret),
13572 					 i40e_aq_str(&pf->hw,
13573 						    pf->hw.aq.asq_last_status));
13574 				ret = -ENOENT;
13575 				goto err;
13576 			}
13577 			/* update the local VSI info queue map */
13578 			i40e_vsi_update_queue_map(vsi, &ctxt);
13579 			vsi->info.valid_sections = 0;
13580 		} else {
13581 			/* Default/Main VSI is only enabled for TC0
13582 			 * reconfigure it to enable all TCs that are
13583 			 * available on the port in SFP mode.
13584 			 * For MFP case the iSCSI PF would use this
13585 			 * flow to enable LAN+iSCSI TC.
13586 			 */
13587 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13588 			if (ret) {
13589 				/* Single TC condition is not fatal,
13590 				 * message and continue
13591 				 */
13592 				dev_info(&pf->pdev->dev,
13593 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13594 					 enabled_tc,
13595 					 i40e_stat_str(&pf->hw, ret),
13596 					 i40e_aq_str(&pf->hw,
13597 						    pf->hw.aq.asq_last_status));
13598 			}
13599 		}
13600 		break;
13601 
13602 	case I40E_VSI_FDIR:
13603 		ctxt.pf_num = hw->pf_id;
13604 		ctxt.vf_num = 0;
13605 		ctxt.uplink_seid = vsi->uplink_seid;
13606 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13607 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13608 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13609 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13610 			ctxt.info.valid_sections |=
13611 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13612 			ctxt.info.switch_id =
13613 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13614 		}
13615 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13616 		break;
13617 
13618 	case I40E_VSI_VMDQ2:
13619 		ctxt.pf_num = hw->pf_id;
13620 		ctxt.vf_num = 0;
13621 		ctxt.uplink_seid = vsi->uplink_seid;
13622 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13623 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13624 
13625 		/* This VSI is connected to VEB so the switch_id
13626 		 * should be set to zero by default.
13627 		 */
13628 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13629 			ctxt.info.valid_sections |=
13630 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13631 			ctxt.info.switch_id =
13632 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13633 		}
13634 
13635 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13636 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13637 		break;
13638 
13639 	case I40E_VSI_SRIOV:
13640 		ctxt.pf_num = hw->pf_id;
13641 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13642 		ctxt.uplink_seid = vsi->uplink_seid;
13643 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13644 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13645 
13646 		/* This VSI is connected to VEB so the switch_id
13647 		 * should be set to zero by default.
13648 		 */
13649 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13650 			ctxt.info.valid_sections |=
13651 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13652 			ctxt.info.switch_id =
13653 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13654 		}
13655 
13656 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
13657 			ctxt.info.valid_sections |=
13658 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
13659 			ctxt.info.queueing_opt_flags |=
13660 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
13661 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
13662 		}
13663 
13664 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
13665 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
13666 		if (pf->vf[vsi->vf_id].spoofchk) {
13667 			ctxt.info.valid_sections |=
13668 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
13669 			ctxt.info.sec_flags |=
13670 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
13671 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
13672 		}
13673 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13674 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13675 		break;
13676 
13677 	case I40E_VSI_IWARP:
13678 		/* send down message to iWARP */
13679 		break;
13680 
13681 	default:
13682 		return -ENODEV;
13683 	}
13684 
13685 	if (vsi->type != I40E_VSI_MAIN) {
13686 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
13687 		if (ret) {
13688 			dev_info(&vsi->back->pdev->dev,
13689 				 "add vsi failed, err %s aq_err %s\n",
13690 				 i40e_stat_str(&pf->hw, ret),
13691 				 i40e_aq_str(&pf->hw,
13692 					     pf->hw.aq.asq_last_status));
13693 			ret = -ENOENT;
13694 			goto err;
13695 		}
13696 		vsi->info = ctxt.info;
13697 		vsi->info.valid_sections = 0;
13698 		vsi->seid = ctxt.seid;
13699 		vsi->id = ctxt.vsi_number;
13700 	}
13701 
13702 	vsi->active_filters = 0;
13703 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
13704 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13705 	/* If macvlan filters already exist, force them to get loaded */
13706 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
13707 		f->state = I40E_FILTER_NEW;
13708 		f_count++;
13709 	}
13710 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13711 
13712 	if (f_count) {
13713 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
13714 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
13715 	}
13716 
13717 	/* Update VSI BW information */
13718 	ret = i40e_vsi_get_bw_info(vsi);
13719 	if (ret) {
13720 		dev_info(&pf->pdev->dev,
13721 			 "couldn't get vsi bw info, err %s aq_err %s\n",
13722 			 i40e_stat_str(&pf->hw, ret),
13723 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13724 		/* VSI is already added so not tearing that up */
13725 		ret = 0;
13726 	}
13727 
13728 err:
13729 	return ret;
13730 }
13731 
13732 /**
13733  * i40e_vsi_release - Delete a VSI and free its resources
13734  * @vsi: the VSI being removed
13735  *
13736  * Returns 0 on success or < 0 on error
13737  **/
13738 int i40e_vsi_release(struct i40e_vsi *vsi)
13739 {
13740 	struct i40e_mac_filter *f;
13741 	struct hlist_node *h;
13742 	struct i40e_veb *veb = NULL;
13743 	struct i40e_pf *pf;
13744 	u16 uplink_seid;
13745 	int i, n, bkt;
13746 
13747 	pf = vsi->back;
13748 
13749 	/* release of a VEB-owner or last VSI is not allowed */
13750 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
13751 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
13752 			 vsi->seid, vsi->uplink_seid);
13753 		return -ENODEV;
13754 	}
13755 	if (vsi == pf->vsi[pf->lan_vsi] &&
13756 	    !test_bit(__I40E_DOWN, pf->state)) {
13757 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
13758 		return -ENODEV;
13759 	}
13760 
13761 	uplink_seid = vsi->uplink_seid;
13762 	if (vsi->type != I40E_VSI_SRIOV) {
13763 		if (vsi->netdev_registered) {
13764 			vsi->netdev_registered = false;
13765 			if (vsi->netdev) {
13766 				/* results in a call to i40e_close() */
13767 				unregister_netdev(vsi->netdev);
13768 			}
13769 		} else {
13770 			i40e_vsi_close(vsi);
13771 		}
13772 		i40e_vsi_disable_irq(vsi);
13773 	}
13774 
13775 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13776 
13777 	/* clear the sync flag on all filters */
13778 	if (vsi->netdev) {
13779 		__dev_uc_unsync(vsi->netdev, NULL);
13780 		__dev_mc_unsync(vsi->netdev, NULL);
13781 	}
13782 
13783 	/* make sure any remaining filters are marked for deletion */
13784 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
13785 		__i40e_del_filter(vsi, f);
13786 
13787 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13788 
13789 	i40e_sync_vsi_filters(vsi);
13790 
13791 	i40e_vsi_delete(vsi);
13792 	i40e_vsi_free_q_vectors(vsi);
13793 	if (vsi->netdev) {
13794 		free_netdev(vsi->netdev);
13795 		vsi->netdev = NULL;
13796 	}
13797 	i40e_vsi_clear_rings(vsi);
13798 	i40e_vsi_clear(vsi);
13799 
13800 	/* If this was the last thing on the VEB, except for the
13801 	 * controlling VSI, remove the VEB, which puts the controlling
13802 	 * VSI onto the next level down in the switch.
13803 	 *
13804 	 * Well, okay, there's one more exception here: don't remove
13805 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
13806 	 * from up the network stack.
13807 	 */
13808 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
13809 		if (pf->vsi[i] &&
13810 		    pf->vsi[i]->uplink_seid == uplink_seid &&
13811 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13812 			n++;      /* count the VSIs */
13813 		}
13814 	}
13815 	for (i = 0; i < I40E_MAX_VEB; i++) {
13816 		if (!pf->veb[i])
13817 			continue;
13818 		if (pf->veb[i]->uplink_seid == uplink_seid)
13819 			n++;     /* count the VEBs */
13820 		if (pf->veb[i]->seid == uplink_seid)
13821 			veb = pf->veb[i];
13822 	}
13823 	if (n == 0 && veb && veb->uplink_seid != 0)
13824 		i40e_veb_release(veb);
13825 
13826 	return 0;
13827 }
13828 
13829 /**
13830  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
13831  * @vsi: ptr to the VSI
13832  *
13833  * This should only be called after i40e_vsi_mem_alloc() which allocates the
13834  * corresponding SW VSI structure and initializes num_queue_pairs for the
13835  * newly allocated VSI.
13836  *
13837  * Returns 0 on success or negative on failure
13838  **/
13839 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
13840 {
13841 	int ret = -ENOENT;
13842 	struct i40e_pf *pf = vsi->back;
13843 
13844 	if (vsi->q_vectors[0]) {
13845 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
13846 			 vsi->seid);
13847 		return -EEXIST;
13848 	}
13849 
13850 	if (vsi->base_vector) {
13851 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
13852 			 vsi->seid, vsi->base_vector);
13853 		return -EEXIST;
13854 	}
13855 
13856 	ret = i40e_vsi_alloc_q_vectors(vsi);
13857 	if (ret) {
13858 		dev_info(&pf->pdev->dev,
13859 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
13860 			 vsi->num_q_vectors, vsi->seid, ret);
13861 		vsi->num_q_vectors = 0;
13862 		goto vector_setup_out;
13863 	}
13864 
13865 	/* In Legacy mode, we do not have to get any other vector since we
13866 	 * piggyback on the misc/ICR0 for queue interrupts.
13867 	*/
13868 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
13869 		return ret;
13870 	if (vsi->num_q_vectors)
13871 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
13872 						 vsi->num_q_vectors, vsi->idx);
13873 	if (vsi->base_vector < 0) {
13874 		dev_info(&pf->pdev->dev,
13875 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
13876 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
13877 		i40e_vsi_free_q_vectors(vsi);
13878 		ret = -ENOENT;
13879 		goto vector_setup_out;
13880 	}
13881 
13882 vector_setup_out:
13883 	return ret;
13884 }
13885 
13886 /**
13887  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
13888  * @vsi: pointer to the vsi.
13889  *
13890  * This re-allocates a vsi's queue resources.
13891  *
13892  * Returns pointer to the successfully allocated and configured VSI sw struct
13893  * on success, otherwise returns NULL on failure.
13894  **/
13895 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
13896 {
13897 	u16 alloc_queue_pairs;
13898 	struct i40e_pf *pf;
13899 	u8 enabled_tc;
13900 	int ret;
13901 
13902 	if (!vsi)
13903 		return NULL;
13904 
13905 	pf = vsi->back;
13906 
13907 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
13908 	i40e_vsi_clear_rings(vsi);
13909 
13910 	i40e_vsi_free_arrays(vsi, false);
13911 	i40e_set_num_rings_in_vsi(vsi);
13912 	ret = i40e_vsi_alloc_arrays(vsi, false);
13913 	if (ret)
13914 		goto err_vsi;
13915 
13916 	alloc_queue_pairs = vsi->alloc_queue_pairs *
13917 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
13918 
13919 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
13920 	if (ret < 0) {
13921 		dev_info(&pf->pdev->dev,
13922 			 "failed to get tracking for %d queues for VSI %d err %d\n",
13923 			 alloc_queue_pairs, vsi->seid, ret);
13924 		goto err_vsi;
13925 	}
13926 	vsi->base_queue = ret;
13927 
13928 	/* Update the FW view of the VSI. Force a reset of TC and queue
13929 	 * layout configurations.
13930 	 */
13931 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
13932 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
13933 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
13934 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
13935 	if (vsi->type == I40E_VSI_MAIN)
13936 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
13937 
13938 	/* assign it some queues */
13939 	ret = i40e_alloc_rings(vsi);
13940 	if (ret)
13941 		goto err_rings;
13942 
13943 	/* map all of the rings to the q_vectors */
13944 	i40e_vsi_map_rings_to_vectors(vsi);
13945 	return vsi;
13946 
13947 err_rings:
13948 	i40e_vsi_free_q_vectors(vsi);
13949 	if (vsi->netdev_registered) {
13950 		vsi->netdev_registered = false;
13951 		unregister_netdev(vsi->netdev);
13952 		free_netdev(vsi->netdev);
13953 		vsi->netdev = NULL;
13954 	}
13955 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
13956 err_vsi:
13957 	i40e_vsi_clear(vsi);
13958 	return NULL;
13959 }
13960 
13961 /**
13962  * i40e_vsi_setup - Set up a VSI by a given type
13963  * @pf: board private structure
13964  * @type: VSI type
13965  * @uplink_seid: the switch element to link to
13966  * @param1: usage depends upon VSI type. For VF types, indicates VF id
13967  *
13968  * This allocates the sw VSI structure and its queue resources, then add a VSI
13969  * to the identified VEB.
13970  *
13971  * Returns pointer to the successfully allocated and configure VSI sw struct on
13972  * success, otherwise returns NULL on failure.
13973  **/
13974 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
13975 				u16 uplink_seid, u32 param1)
13976 {
13977 	struct i40e_vsi *vsi = NULL;
13978 	struct i40e_veb *veb = NULL;
13979 	u16 alloc_queue_pairs;
13980 	int ret, i;
13981 	int v_idx;
13982 
13983 	/* The requested uplink_seid must be either
13984 	 *     - the PF's port seid
13985 	 *              no VEB is needed because this is the PF
13986 	 *              or this is a Flow Director special case VSI
13987 	 *     - seid of an existing VEB
13988 	 *     - seid of a VSI that owns an existing VEB
13989 	 *     - seid of a VSI that doesn't own a VEB
13990 	 *              a new VEB is created and the VSI becomes the owner
13991 	 *     - seid of the PF VSI, which is what creates the first VEB
13992 	 *              this is a special case of the previous
13993 	 *
13994 	 * Find which uplink_seid we were given and create a new VEB if needed
13995 	 */
13996 	for (i = 0; i < I40E_MAX_VEB; i++) {
13997 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
13998 			veb = pf->veb[i];
13999 			break;
14000 		}
14001 	}
14002 
14003 	if (!veb && uplink_seid != pf->mac_seid) {
14004 
14005 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14006 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14007 				vsi = pf->vsi[i];
14008 				break;
14009 			}
14010 		}
14011 		if (!vsi) {
14012 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14013 				 uplink_seid);
14014 			return NULL;
14015 		}
14016 
14017 		if (vsi->uplink_seid == pf->mac_seid)
14018 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14019 					     vsi->tc_config.enabled_tc);
14020 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14021 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14022 					     vsi->tc_config.enabled_tc);
14023 		if (veb) {
14024 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14025 				dev_info(&vsi->back->pdev->dev,
14026 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14027 				return NULL;
14028 			}
14029 			/* We come up by default in VEPA mode if SRIOV is not
14030 			 * already enabled, in which case we can't force VEPA
14031 			 * mode.
14032 			 */
14033 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14034 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14035 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14036 			}
14037 			i40e_config_bridge_mode(veb);
14038 		}
14039 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14040 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14041 				veb = pf->veb[i];
14042 		}
14043 		if (!veb) {
14044 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14045 			return NULL;
14046 		}
14047 
14048 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14049 		uplink_seid = veb->seid;
14050 	}
14051 
14052 	/* get vsi sw struct */
14053 	v_idx = i40e_vsi_mem_alloc(pf, type);
14054 	if (v_idx < 0)
14055 		goto err_alloc;
14056 	vsi = pf->vsi[v_idx];
14057 	if (!vsi)
14058 		goto err_alloc;
14059 	vsi->type = type;
14060 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14061 
14062 	if (type == I40E_VSI_MAIN)
14063 		pf->lan_vsi = v_idx;
14064 	else if (type == I40E_VSI_SRIOV)
14065 		vsi->vf_id = param1;
14066 	/* assign it some queues */
14067 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14068 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14069 
14070 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14071 	if (ret < 0) {
14072 		dev_info(&pf->pdev->dev,
14073 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14074 			 alloc_queue_pairs, vsi->seid, ret);
14075 		goto err_vsi;
14076 	}
14077 	vsi->base_queue = ret;
14078 
14079 	/* get a VSI from the hardware */
14080 	vsi->uplink_seid = uplink_seid;
14081 	ret = i40e_add_vsi(vsi);
14082 	if (ret)
14083 		goto err_vsi;
14084 
14085 	switch (vsi->type) {
14086 	/* setup the netdev if needed */
14087 	case I40E_VSI_MAIN:
14088 	case I40E_VSI_VMDQ2:
14089 		ret = i40e_config_netdev(vsi);
14090 		if (ret)
14091 			goto err_netdev;
14092 		ret = register_netdev(vsi->netdev);
14093 		if (ret)
14094 			goto err_netdev;
14095 		vsi->netdev_registered = true;
14096 		netif_carrier_off(vsi->netdev);
14097 #ifdef CONFIG_I40E_DCB
14098 		/* Setup DCB netlink interface */
14099 		i40e_dcbnl_setup(vsi);
14100 #endif /* CONFIG_I40E_DCB */
14101 		fallthrough;
14102 	case I40E_VSI_FDIR:
14103 		/* set up vectors and rings if needed */
14104 		ret = i40e_vsi_setup_vectors(vsi);
14105 		if (ret)
14106 			goto err_msix;
14107 
14108 		ret = i40e_alloc_rings(vsi);
14109 		if (ret)
14110 			goto err_rings;
14111 
14112 		/* map all of the rings to the q_vectors */
14113 		i40e_vsi_map_rings_to_vectors(vsi);
14114 
14115 		i40e_vsi_reset_stats(vsi);
14116 		break;
14117 	default:
14118 		/* no netdev or rings for the other VSI types */
14119 		break;
14120 	}
14121 
14122 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14123 	    (vsi->type == I40E_VSI_VMDQ2)) {
14124 		ret = i40e_vsi_config_rss(vsi);
14125 	}
14126 	return vsi;
14127 
14128 err_rings:
14129 	i40e_vsi_free_q_vectors(vsi);
14130 err_msix:
14131 	if (vsi->netdev_registered) {
14132 		vsi->netdev_registered = false;
14133 		unregister_netdev(vsi->netdev);
14134 		free_netdev(vsi->netdev);
14135 		vsi->netdev = NULL;
14136 	}
14137 err_netdev:
14138 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14139 err_vsi:
14140 	i40e_vsi_clear(vsi);
14141 err_alloc:
14142 	return NULL;
14143 }
14144 
14145 /**
14146  * i40e_veb_get_bw_info - Query VEB BW information
14147  * @veb: the veb to query
14148  *
14149  * Query the Tx scheduler BW configuration data for given VEB
14150  **/
14151 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14152 {
14153 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14154 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14155 	struct i40e_pf *pf = veb->pf;
14156 	struct i40e_hw *hw = &pf->hw;
14157 	u32 tc_bw_max;
14158 	int ret = 0;
14159 	int i;
14160 
14161 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14162 						  &bw_data, NULL);
14163 	if (ret) {
14164 		dev_info(&pf->pdev->dev,
14165 			 "query veb bw config failed, err %s aq_err %s\n",
14166 			 i40e_stat_str(&pf->hw, ret),
14167 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14168 		goto out;
14169 	}
14170 
14171 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14172 						   &ets_data, NULL);
14173 	if (ret) {
14174 		dev_info(&pf->pdev->dev,
14175 			 "query veb bw ets config failed, err %s aq_err %s\n",
14176 			 i40e_stat_str(&pf->hw, ret),
14177 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14178 		goto out;
14179 	}
14180 
14181 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14182 	veb->bw_max_quanta = ets_data.tc_bw_max;
14183 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14184 	veb->enabled_tc = ets_data.tc_valid_bits;
14185 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14186 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14187 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14188 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14189 		veb->bw_tc_limit_credits[i] =
14190 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14191 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14192 	}
14193 
14194 out:
14195 	return ret;
14196 }
14197 
14198 /**
14199  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14200  * @pf: board private structure
14201  *
14202  * On error: returns error code (negative)
14203  * On success: returns vsi index in PF (positive)
14204  **/
14205 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14206 {
14207 	int ret = -ENOENT;
14208 	struct i40e_veb *veb;
14209 	int i;
14210 
14211 	/* Need to protect the allocation of switch elements at the PF level */
14212 	mutex_lock(&pf->switch_mutex);
14213 
14214 	/* VEB list may be fragmented if VEB creation/destruction has
14215 	 * been happening.  We can afford to do a quick scan to look
14216 	 * for any free slots in the list.
14217 	 *
14218 	 * find next empty veb slot, looping back around if necessary
14219 	 */
14220 	i = 0;
14221 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14222 		i++;
14223 	if (i >= I40E_MAX_VEB) {
14224 		ret = -ENOMEM;
14225 		goto err_alloc_veb;  /* out of VEB slots! */
14226 	}
14227 
14228 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14229 	if (!veb) {
14230 		ret = -ENOMEM;
14231 		goto err_alloc_veb;
14232 	}
14233 	veb->pf = pf;
14234 	veb->idx = i;
14235 	veb->enabled_tc = 1;
14236 
14237 	pf->veb[i] = veb;
14238 	ret = i;
14239 err_alloc_veb:
14240 	mutex_unlock(&pf->switch_mutex);
14241 	return ret;
14242 }
14243 
14244 /**
14245  * i40e_switch_branch_release - Delete a branch of the switch tree
14246  * @branch: where to start deleting
14247  *
14248  * This uses recursion to find the tips of the branch to be
14249  * removed, deleting until we get back to and can delete this VEB.
14250  **/
14251 static void i40e_switch_branch_release(struct i40e_veb *branch)
14252 {
14253 	struct i40e_pf *pf = branch->pf;
14254 	u16 branch_seid = branch->seid;
14255 	u16 veb_idx = branch->idx;
14256 	int i;
14257 
14258 	/* release any VEBs on this VEB - RECURSION */
14259 	for (i = 0; i < I40E_MAX_VEB; i++) {
14260 		if (!pf->veb[i])
14261 			continue;
14262 		if (pf->veb[i]->uplink_seid == branch->seid)
14263 			i40e_switch_branch_release(pf->veb[i]);
14264 	}
14265 
14266 	/* Release the VSIs on this VEB, but not the owner VSI.
14267 	 *
14268 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14269 	 *       the VEB itself, so don't use (*branch) after this loop.
14270 	 */
14271 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14272 		if (!pf->vsi[i])
14273 			continue;
14274 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14275 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14276 			i40e_vsi_release(pf->vsi[i]);
14277 		}
14278 	}
14279 
14280 	/* There's one corner case where the VEB might not have been
14281 	 * removed, so double check it here and remove it if needed.
14282 	 * This case happens if the veb was created from the debugfs
14283 	 * commands and no VSIs were added to it.
14284 	 */
14285 	if (pf->veb[veb_idx])
14286 		i40e_veb_release(pf->veb[veb_idx]);
14287 }
14288 
14289 /**
14290  * i40e_veb_clear - remove veb struct
14291  * @veb: the veb to remove
14292  **/
14293 static void i40e_veb_clear(struct i40e_veb *veb)
14294 {
14295 	if (!veb)
14296 		return;
14297 
14298 	if (veb->pf) {
14299 		struct i40e_pf *pf = veb->pf;
14300 
14301 		mutex_lock(&pf->switch_mutex);
14302 		if (pf->veb[veb->idx] == veb)
14303 			pf->veb[veb->idx] = NULL;
14304 		mutex_unlock(&pf->switch_mutex);
14305 	}
14306 
14307 	kfree(veb);
14308 }
14309 
14310 /**
14311  * i40e_veb_release - Delete a VEB and free its resources
14312  * @veb: the VEB being removed
14313  **/
14314 void i40e_veb_release(struct i40e_veb *veb)
14315 {
14316 	struct i40e_vsi *vsi = NULL;
14317 	struct i40e_pf *pf;
14318 	int i, n = 0;
14319 
14320 	pf = veb->pf;
14321 
14322 	/* find the remaining VSI and check for extras */
14323 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14324 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14325 			n++;
14326 			vsi = pf->vsi[i];
14327 		}
14328 	}
14329 	if (n != 1) {
14330 		dev_info(&pf->pdev->dev,
14331 			 "can't remove VEB %d with %d VSIs left\n",
14332 			 veb->seid, n);
14333 		return;
14334 	}
14335 
14336 	/* move the remaining VSI to uplink veb */
14337 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14338 	if (veb->uplink_seid) {
14339 		vsi->uplink_seid = veb->uplink_seid;
14340 		if (veb->uplink_seid == pf->mac_seid)
14341 			vsi->veb_idx = I40E_NO_VEB;
14342 		else
14343 			vsi->veb_idx = veb->veb_idx;
14344 	} else {
14345 		/* floating VEB */
14346 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14347 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14348 	}
14349 
14350 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14351 	i40e_veb_clear(veb);
14352 }
14353 
14354 /**
14355  * i40e_add_veb - create the VEB in the switch
14356  * @veb: the VEB to be instantiated
14357  * @vsi: the controlling VSI
14358  **/
14359 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14360 {
14361 	struct i40e_pf *pf = veb->pf;
14362 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14363 	int ret;
14364 
14365 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14366 			      veb->enabled_tc, false,
14367 			      &veb->seid, enable_stats, NULL);
14368 
14369 	/* get a VEB from the hardware */
14370 	if (ret) {
14371 		dev_info(&pf->pdev->dev,
14372 			 "couldn't add VEB, err %s aq_err %s\n",
14373 			 i40e_stat_str(&pf->hw, ret),
14374 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14375 		return -EPERM;
14376 	}
14377 
14378 	/* get statistics counter */
14379 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14380 					 &veb->stats_idx, NULL, NULL, NULL);
14381 	if (ret) {
14382 		dev_info(&pf->pdev->dev,
14383 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
14384 			 i40e_stat_str(&pf->hw, ret),
14385 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14386 		return -EPERM;
14387 	}
14388 	ret = i40e_veb_get_bw_info(veb);
14389 	if (ret) {
14390 		dev_info(&pf->pdev->dev,
14391 			 "couldn't get VEB bw info, err %s aq_err %s\n",
14392 			 i40e_stat_str(&pf->hw, ret),
14393 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14394 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14395 		return -ENOENT;
14396 	}
14397 
14398 	vsi->uplink_seid = veb->seid;
14399 	vsi->veb_idx = veb->idx;
14400 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14401 
14402 	return 0;
14403 }
14404 
14405 /**
14406  * i40e_veb_setup - Set up a VEB
14407  * @pf: board private structure
14408  * @flags: VEB setup flags
14409  * @uplink_seid: the switch element to link to
14410  * @vsi_seid: the initial VSI seid
14411  * @enabled_tc: Enabled TC bit-map
14412  *
14413  * This allocates the sw VEB structure and links it into the switch
14414  * It is possible and legal for this to be a duplicate of an already
14415  * existing VEB.  It is also possible for both uplink and vsi seids
14416  * to be zero, in order to create a floating VEB.
14417  *
14418  * Returns pointer to the successfully allocated VEB sw struct on
14419  * success, otherwise returns NULL on failure.
14420  **/
14421 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14422 				u16 uplink_seid, u16 vsi_seid,
14423 				u8 enabled_tc)
14424 {
14425 	struct i40e_veb *veb, *uplink_veb = NULL;
14426 	int vsi_idx, veb_idx;
14427 	int ret;
14428 
14429 	/* if one seid is 0, the other must be 0 to create a floating relay */
14430 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14431 	    (uplink_seid + vsi_seid != 0)) {
14432 		dev_info(&pf->pdev->dev,
14433 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14434 			 uplink_seid, vsi_seid);
14435 		return NULL;
14436 	}
14437 
14438 	/* make sure there is such a vsi and uplink */
14439 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14440 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14441 			break;
14442 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14443 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14444 			 vsi_seid);
14445 		return NULL;
14446 	}
14447 
14448 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14449 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14450 			if (pf->veb[veb_idx] &&
14451 			    pf->veb[veb_idx]->seid == uplink_seid) {
14452 				uplink_veb = pf->veb[veb_idx];
14453 				break;
14454 			}
14455 		}
14456 		if (!uplink_veb) {
14457 			dev_info(&pf->pdev->dev,
14458 				 "uplink seid %d not found\n", uplink_seid);
14459 			return NULL;
14460 		}
14461 	}
14462 
14463 	/* get veb sw struct */
14464 	veb_idx = i40e_veb_mem_alloc(pf);
14465 	if (veb_idx < 0)
14466 		goto err_alloc;
14467 	veb = pf->veb[veb_idx];
14468 	veb->flags = flags;
14469 	veb->uplink_seid = uplink_seid;
14470 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14471 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14472 
14473 	/* create the VEB in the switch */
14474 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14475 	if (ret)
14476 		goto err_veb;
14477 	if (vsi_idx == pf->lan_vsi)
14478 		pf->lan_veb = veb->idx;
14479 
14480 	return veb;
14481 
14482 err_veb:
14483 	i40e_veb_clear(veb);
14484 err_alloc:
14485 	return NULL;
14486 }
14487 
14488 /**
14489  * i40e_setup_pf_switch_element - set PF vars based on switch type
14490  * @pf: board private structure
14491  * @ele: element we are building info from
14492  * @num_reported: total number of elements
14493  * @printconfig: should we print the contents
14494  *
14495  * helper function to assist in extracting a few useful SEID values.
14496  **/
14497 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14498 				struct i40e_aqc_switch_config_element_resp *ele,
14499 				u16 num_reported, bool printconfig)
14500 {
14501 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14502 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14503 	u8 element_type = ele->element_type;
14504 	u16 seid = le16_to_cpu(ele->seid);
14505 
14506 	if (printconfig)
14507 		dev_info(&pf->pdev->dev,
14508 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14509 			 element_type, seid, uplink_seid, downlink_seid);
14510 
14511 	switch (element_type) {
14512 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14513 		pf->mac_seid = seid;
14514 		break;
14515 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14516 		/* Main VEB? */
14517 		if (uplink_seid != pf->mac_seid)
14518 			break;
14519 		if (pf->lan_veb >= I40E_MAX_VEB) {
14520 			int v;
14521 
14522 			/* find existing or else empty VEB */
14523 			for (v = 0; v < I40E_MAX_VEB; v++) {
14524 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14525 					pf->lan_veb = v;
14526 					break;
14527 				}
14528 			}
14529 			if (pf->lan_veb >= I40E_MAX_VEB) {
14530 				v = i40e_veb_mem_alloc(pf);
14531 				if (v < 0)
14532 					break;
14533 				pf->lan_veb = v;
14534 			}
14535 		}
14536 		if (pf->lan_veb >= I40E_MAX_VEB)
14537 			break;
14538 
14539 		pf->veb[pf->lan_veb]->seid = seid;
14540 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14541 		pf->veb[pf->lan_veb]->pf = pf;
14542 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14543 		break;
14544 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14545 		if (num_reported != 1)
14546 			break;
14547 		/* This is immediately after a reset so we can assume this is
14548 		 * the PF's VSI
14549 		 */
14550 		pf->mac_seid = uplink_seid;
14551 		pf->pf_seid = downlink_seid;
14552 		pf->main_vsi_seid = seid;
14553 		if (printconfig)
14554 			dev_info(&pf->pdev->dev,
14555 				 "pf_seid=%d main_vsi_seid=%d\n",
14556 				 pf->pf_seid, pf->main_vsi_seid);
14557 		break;
14558 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14559 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14560 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14561 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14562 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14563 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14564 		/* ignore these for now */
14565 		break;
14566 	default:
14567 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14568 			 element_type, seid);
14569 		break;
14570 	}
14571 }
14572 
14573 /**
14574  * i40e_fetch_switch_configuration - Get switch config from firmware
14575  * @pf: board private structure
14576  * @printconfig: should we print the contents
14577  *
14578  * Get the current switch configuration from the device and
14579  * extract a few useful SEID values.
14580  **/
14581 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14582 {
14583 	struct i40e_aqc_get_switch_config_resp *sw_config;
14584 	u16 next_seid = 0;
14585 	int ret = 0;
14586 	u8 *aq_buf;
14587 	int i;
14588 
14589 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14590 	if (!aq_buf)
14591 		return -ENOMEM;
14592 
14593 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14594 	do {
14595 		u16 num_reported, num_total;
14596 
14597 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14598 						I40E_AQ_LARGE_BUF,
14599 						&next_seid, NULL);
14600 		if (ret) {
14601 			dev_info(&pf->pdev->dev,
14602 				 "get switch config failed err %s aq_err %s\n",
14603 				 i40e_stat_str(&pf->hw, ret),
14604 				 i40e_aq_str(&pf->hw,
14605 					     pf->hw.aq.asq_last_status));
14606 			kfree(aq_buf);
14607 			return -ENOENT;
14608 		}
14609 
14610 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14611 		num_total = le16_to_cpu(sw_config->header.num_total);
14612 
14613 		if (printconfig)
14614 			dev_info(&pf->pdev->dev,
14615 				 "header: %d reported %d total\n",
14616 				 num_reported, num_total);
14617 
14618 		for (i = 0; i < num_reported; i++) {
14619 			struct i40e_aqc_switch_config_element_resp *ele =
14620 				&sw_config->element[i];
14621 
14622 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14623 						     printconfig);
14624 		}
14625 	} while (next_seid != 0);
14626 
14627 	kfree(aq_buf);
14628 	return ret;
14629 }
14630 
14631 /**
14632  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14633  * @pf: board private structure
14634  * @reinit: if the Main VSI needs to re-initialized.
14635  * @lock_acquired: indicates whether or not the lock has been acquired
14636  *
14637  * Returns 0 on success, negative value on failure
14638  **/
14639 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14640 {
14641 	u16 flags = 0;
14642 	int ret;
14643 
14644 	/* find out what's out there already */
14645 	ret = i40e_fetch_switch_configuration(pf, false);
14646 	if (ret) {
14647 		dev_info(&pf->pdev->dev,
14648 			 "couldn't fetch switch config, err %s aq_err %s\n",
14649 			 i40e_stat_str(&pf->hw, ret),
14650 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14651 		return ret;
14652 	}
14653 	i40e_pf_reset_stats(pf);
14654 
14655 	/* set the switch config bit for the whole device to
14656 	 * support limited promisc or true promisc
14657 	 * when user requests promisc. The default is limited
14658 	 * promisc.
14659 	*/
14660 
14661 	if ((pf->hw.pf_id == 0) &&
14662 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
14663 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14664 		pf->last_sw_conf_flags = flags;
14665 	}
14666 
14667 	if (pf->hw.pf_id == 0) {
14668 		u16 valid_flags;
14669 
14670 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14671 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
14672 						NULL);
14673 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
14674 			dev_info(&pf->pdev->dev,
14675 				 "couldn't set switch config bits, err %s aq_err %s\n",
14676 				 i40e_stat_str(&pf->hw, ret),
14677 				 i40e_aq_str(&pf->hw,
14678 					     pf->hw.aq.asq_last_status));
14679 			/* not a fatal problem, just keep going */
14680 		}
14681 		pf->last_sw_conf_valid_flags = valid_flags;
14682 	}
14683 
14684 	/* first time setup */
14685 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
14686 		struct i40e_vsi *vsi = NULL;
14687 		u16 uplink_seid;
14688 
14689 		/* Set up the PF VSI associated with the PF's main VSI
14690 		 * that is already in the HW switch
14691 		 */
14692 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
14693 			uplink_seid = pf->veb[pf->lan_veb]->seid;
14694 		else
14695 			uplink_seid = pf->mac_seid;
14696 		if (pf->lan_vsi == I40E_NO_VSI)
14697 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
14698 		else if (reinit)
14699 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
14700 		if (!vsi) {
14701 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
14702 			i40e_cloud_filter_exit(pf);
14703 			i40e_fdir_teardown(pf);
14704 			return -EAGAIN;
14705 		}
14706 	} else {
14707 		/* force a reset of TC and queue layout configurations */
14708 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14709 
14710 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14711 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14712 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14713 	}
14714 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
14715 
14716 	i40e_fdir_sb_setup(pf);
14717 
14718 	/* Setup static PF queue filter control settings */
14719 	ret = i40e_setup_pf_filter_control(pf);
14720 	if (ret) {
14721 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
14722 			 ret);
14723 		/* Failure here should not stop continuing other steps */
14724 	}
14725 
14726 	/* enable RSS in the HW, even for only one queue, as the stack can use
14727 	 * the hash
14728 	 */
14729 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
14730 		i40e_pf_config_rss(pf);
14731 
14732 	/* fill in link information and enable LSE reporting */
14733 	i40e_link_event(pf);
14734 
14735 	/* Initialize user-specific link properties */
14736 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
14737 				  I40E_AQ_AN_COMPLETED) ? true : false);
14738 
14739 	i40e_ptp_init(pf);
14740 
14741 	if (!lock_acquired)
14742 		rtnl_lock();
14743 
14744 	/* repopulate tunnel port filters */
14745 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
14746 
14747 	if (!lock_acquired)
14748 		rtnl_unlock();
14749 
14750 	return ret;
14751 }
14752 
14753 /**
14754  * i40e_determine_queue_usage - Work out queue distribution
14755  * @pf: board private structure
14756  **/
14757 static void i40e_determine_queue_usage(struct i40e_pf *pf)
14758 {
14759 	int queues_left;
14760 	int q_max;
14761 
14762 	pf->num_lan_qps = 0;
14763 
14764 	/* Find the max queues to be put into basic use.  We'll always be
14765 	 * using TC0, whether or not DCB is running, and TC0 will get the
14766 	 * big RSS set.
14767 	 */
14768 	queues_left = pf->hw.func_caps.num_tx_qp;
14769 
14770 	if ((queues_left == 1) ||
14771 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
14772 		/* one qp for PF, no queues for anything else */
14773 		queues_left = 0;
14774 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14775 
14776 		/* make sure all the fancies are disabled */
14777 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14778 			       I40E_FLAG_IWARP_ENABLED	|
14779 			       I40E_FLAG_FD_SB_ENABLED	|
14780 			       I40E_FLAG_FD_ATR_ENABLED	|
14781 			       I40E_FLAG_DCB_CAPABLE	|
14782 			       I40E_FLAG_DCB_ENABLED	|
14783 			       I40E_FLAG_SRIOV_ENABLED	|
14784 			       I40E_FLAG_VMDQ_ENABLED);
14785 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14786 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
14787 				  I40E_FLAG_FD_SB_ENABLED |
14788 				  I40E_FLAG_FD_ATR_ENABLED |
14789 				  I40E_FLAG_DCB_CAPABLE))) {
14790 		/* one qp for PF */
14791 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14792 		queues_left -= pf->num_lan_qps;
14793 
14794 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14795 			       I40E_FLAG_IWARP_ENABLED	|
14796 			       I40E_FLAG_FD_SB_ENABLED	|
14797 			       I40E_FLAG_FD_ATR_ENABLED	|
14798 			       I40E_FLAG_DCB_ENABLED	|
14799 			       I40E_FLAG_VMDQ_ENABLED);
14800 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14801 	} else {
14802 		/* Not enough queues for all TCs */
14803 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
14804 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
14805 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
14806 					I40E_FLAG_DCB_ENABLED);
14807 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
14808 		}
14809 
14810 		/* limit lan qps to the smaller of qps, cpus or msix */
14811 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
14812 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
14813 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
14814 		pf->num_lan_qps = q_max;
14815 
14816 		queues_left -= pf->num_lan_qps;
14817 	}
14818 
14819 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14820 		if (queues_left > 1) {
14821 			queues_left -= 1; /* save 1 queue for FD */
14822 		} else {
14823 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
14824 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14825 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
14826 		}
14827 	}
14828 
14829 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
14830 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
14831 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
14832 					(queues_left / pf->num_vf_qps));
14833 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
14834 	}
14835 
14836 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
14837 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
14838 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
14839 					  (queues_left / pf->num_vmdq_qps));
14840 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
14841 	}
14842 
14843 	pf->queues_left = queues_left;
14844 	dev_dbg(&pf->pdev->dev,
14845 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
14846 		pf->hw.func_caps.num_tx_qp,
14847 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
14848 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
14849 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
14850 		queues_left);
14851 }
14852 
14853 /**
14854  * i40e_setup_pf_filter_control - Setup PF static filter control
14855  * @pf: PF to be setup
14856  *
14857  * i40e_setup_pf_filter_control sets up a PF's initial filter control
14858  * settings. If PE/FCoE are enabled then it will also set the per PF
14859  * based filter sizes required for them. It also enables Flow director,
14860  * ethertype and macvlan type filter settings for the pf.
14861  *
14862  * Returns 0 on success, negative on failure
14863  **/
14864 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
14865 {
14866 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
14867 
14868 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
14869 
14870 	/* Flow Director is enabled */
14871 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
14872 		settings->enable_fdir = true;
14873 
14874 	/* Ethtype and MACVLAN filters enabled for PF */
14875 	settings->enable_ethtype = true;
14876 	settings->enable_macvlan = true;
14877 
14878 	if (i40e_set_filter_control(&pf->hw, settings))
14879 		return -ENOENT;
14880 
14881 	return 0;
14882 }
14883 
14884 #define INFO_STRING_LEN 255
14885 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
14886 static void i40e_print_features(struct i40e_pf *pf)
14887 {
14888 	struct i40e_hw *hw = &pf->hw;
14889 	char *buf;
14890 	int i;
14891 
14892 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
14893 	if (!buf)
14894 		return;
14895 
14896 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
14897 #ifdef CONFIG_PCI_IOV
14898 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
14899 #endif
14900 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
14901 		      pf->hw.func_caps.num_vsis,
14902 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
14903 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
14904 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
14905 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
14906 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
14907 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14908 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
14909 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
14910 	}
14911 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
14912 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
14913 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
14914 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
14915 	if (pf->flags & I40E_FLAG_PTP)
14916 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
14917 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
14918 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
14919 	else
14920 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
14921 
14922 	dev_info(&pf->pdev->dev, "%s\n", buf);
14923 	kfree(buf);
14924 	WARN_ON(i > INFO_STRING_LEN);
14925 }
14926 
14927 /**
14928  * i40e_get_platform_mac_addr - get platform-specific MAC address
14929  * @pdev: PCI device information struct
14930  * @pf: board private structure
14931  *
14932  * Look up the MAC address for the device. First we'll try
14933  * eth_platform_get_mac_address, which will check Open Firmware, or arch
14934  * specific fallback. Otherwise, we'll default to the stored value in
14935  * firmware.
14936  **/
14937 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
14938 {
14939 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
14940 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
14941 }
14942 
14943 /**
14944  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
14945  * @fec_cfg: FEC option to set in flags
14946  * @flags: ptr to flags in which we set FEC option
14947  **/
14948 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
14949 {
14950 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
14951 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
14952 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
14953 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
14954 		*flags |= I40E_FLAG_RS_FEC;
14955 		*flags &= ~I40E_FLAG_BASE_R_FEC;
14956 	}
14957 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
14958 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
14959 		*flags |= I40E_FLAG_BASE_R_FEC;
14960 		*flags &= ~I40E_FLAG_RS_FEC;
14961 	}
14962 	if (fec_cfg == 0)
14963 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
14964 }
14965 
14966 /**
14967  * i40e_check_recovery_mode - check if we are running transition firmware
14968  * @pf: board private structure
14969  *
14970  * Check registers indicating the firmware runs in recovery mode. Sets the
14971  * appropriate driver state.
14972  *
14973  * Returns true if the recovery mode was detected, false otherwise
14974  **/
14975 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
14976 {
14977 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
14978 
14979 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
14980 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
14981 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
14982 		set_bit(__I40E_RECOVERY_MODE, pf->state);
14983 
14984 		return true;
14985 	}
14986 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
14987 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
14988 
14989 	return false;
14990 }
14991 
14992 /**
14993  * i40e_pf_loop_reset - perform reset in a loop.
14994  * @pf: board private structure
14995  *
14996  * This function is useful when a NIC is about to enter recovery mode.
14997  * When a NIC's internal data structures are corrupted the NIC's
14998  * firmware is going to enter recovery mode.
14999  * Right after a POR it takes about 7 minutes for firmware to enter
15000  * recovery mode. Until that time a NIC is in some kind of intermediate
15001  * state. After that time period the NIC almost surely enters
15002  * recovery mode. The only way for a driver to detect intermediate
15003  * state is to issue a series of pf-resets and check a return value.
15004  * If a PF reset returns success then the firmware could be in recovery
15005  * mode so the caller of this code needs to check for recovery mode
15006  * if this function returns success. There is a little chance that
15007  * firmware will hang in intermediate state forever.
15008  * Since waiting 7 minutes is quite a lot of time this function waits
15009  * 10 seconds and then gives up by returning an error.
15010  *
15011  * Return 0 on success, negative on failure.
15012  **/
15013 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
15014 {
15015 	/* wait max 10 seconds for PF reset to succeed */
15016 	const unsigned long time_end = jiffies + 10 * HZ;
15017 
15018 	struct i40e_hw *hw = &pf->hw;
15019 	i40e_status ret;
15020 
15021 	ret = i40e_pf_reset(hw);
15022 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15023 		usleep_range(10000, 20000);
15024 		ret = i40e_pf_reset(hw);
15025 	}
15026 
15027 	if (ret == I40E_SUCCESS)
15028 		pf->pfr_count++;
15029 	else
15030 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15031 
15032 	return ret;
15033 }
15034 
15035 /**
15036  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15037  * @pf: board private structure
15038  *
15039  * Check FW registers to determine if FW issued unexpected EMP Reset.
15040  * Every time when unexpected EMP Reset occurs the FW increments
15041  * a counter of unexpected EMP Resets. When the counter reaches 10
15042  * the FW should enter the Recovery mode
15043  *
15044  * Returns true if FW issued unexpected EMP Reset
15045  **/
15046 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15047 {
15048 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15049 			   I40E_GL_FWSTS_FWS1B_MASK;
15050 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15051 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15052 }
15053 
15054 /**
15055  * i40e_handle_resets - handle EMP resets and PF resets
15056  * @pf: board private structure
15057  *
15058  * Handle both EMP resets and PF resets and conclude whether there are
15059  * any issues regarding these resets. If there are any issues then
15060  * generate log entry.
15061  *
15062  * Return 0 if NIC is healthy or negative value when there are issues
15063  * with resets
15064  **/
15065 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
15066 {
15067 	const i40e_status pfr = i40e_pf_loop_reset(pf);
15068 	const bool is_empr = i40e_check_fw_empr(pf);
15069 
15070 	if (is_empr || pfr != I40E_SUCCESS)
15071 		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");
15072 
15073 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15074 }
15075 
15076 /**
15077  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15078  * @pf: board private structure
15079  * @hw: ptr to the hardware info
15080  *
15081  * This function does a minimal setup of all subsystems needed for running
15082  * recovery mode.
15083  *
15084  * Returns 0 on success, negative on failure
15085  **/
15086 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15087 {
15088 	struct i40e_vsi *vsi;
15089 	int err;
15090 	int v_idx;
15091 
15092 	pci_save_state(pf->pdev);
15093 
15094 	/* set up periodic task facility */
15095 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15096 	pf->service_timer_period = HZ;
15097 
15098 	INIT_WORK(&pf->service_task, i40e_service_task);
15099 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15100 
15101 	err = i40e_init_interrupt_scheme(pf);
15102 	if (err)
15103 		goto err_switch_setup;
15104 
15105 	/* The number of VSIs reported by the FW is the minimum guaranteed
15106 	 * to us; HW supports far more and we share the remaining pool with
15107 	 * the other PFs. We allocate space for more than the guarantee with
15108 	 * the understanding that we might not get them all later.
15109 	 */
15110 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15111 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15112 	else
15113 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15114 
15115 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15116 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15117 			  GFP_KERNEL);
15118 	if (!pf->vsi) {
15119 		err = -ENOMEM;
15120 		goto err_switch_setup;
15121 	}
15122 
15123 	/* We allocate one VSI which is needed as absolute minimum
15124 	 * in order to register the netdev
15125 	 */
15126 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15127 	if (v_idx < 0) {
15128 		err = v_idx;
15129 		goto err_switch_setup;
15130 	}
15131 	pf->lan_vsi = v_idx;
15132 	vsi = pf->vsi[v_idx];
15133 	if (!vsi) {
15134 		err = -EFAULT;
15135 		goto err_switch_setup;
15136 	}
15137 	vsi->alloc_queue_pairs = 1;
15138 	err = i40e_config_netdev(vsi);
15139 	if (err)
15140 		goto err_switch_setup;
15141 	err = register_netdev(vsi->netdev);
15142 	if (err)
15143 		goto err_switch_setup;
15144 	vsi->netdev_registered = true;
15145 	i40e_dbg_pf_init(pf);
15146 
15147 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15148 	if (err)
15149 		goto err_switch_setup;
15150 
15151 	/* tell the firmware that we're starting */
15152 	i40e_send_version(pf);
15153 
15154 	/* since everything's happy, start the service_task timer */
15155 	mod_timer(&pf->service_timer,
15156 		  round_jiffies(jiffies + pf->service_timer_period));
15157 
15158 	return 0;
15159 
15160 err_switch_setup:
15161 	i40e_reset_interrupt_capability(pf);
15162 	del_timer_sync(&pf->service_timer);
15163 	i40e_shutdown_adminq(hw);
15164 	iounmap(hw->hw_addr);
15165 	pci_disable_pcie_error_reporting(pf->pdev);
15166 	pci_release_mem_regions(pf->pdev);
15167 	pci_disable_device(pf->pdev);
15168 	kfree(pf);
15169 
15170 	return err;
15171 }
15172 
15173 /**
15174  * i40e_set_subsystem_device_id - set subsystem device id
15175  * @hw: pointer to the hardware info
15176  *
15177  * Set PCI subsystem device id either from a pci_dev structure or
15178  * a specific FW register.
15179  **/
15180 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15181 {
15182 	struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15183 
15184 	hw->subsystem_device_id = pdev->subsystem_device ?
15185 		pdev->subsystem_device :
15186 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15187 }
15188 
15189 /**
15190  * i40e_probe - Device initialization routine
15191  * @pdev: PCI device information struct
15192  * @ent: entry in i40e_pci_tbl
15193  *
15194  * i40e_probe initializes a PF identified by a pci_dev structure.
15195  * The OS initialization, configuring of the PF private structure,
15196  * and a hardware reset occur.
15197  *
15198  * Returns 0 on success, negative on failure
15199  **/
15200 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15201 {
15202 	struct i40e_aq_get_phy_abilities_resp abilities;
15203 #ifdef CONFIG_I40E_DCB
15204 	enum i40e_get_fw_lldp_status_resp lldp_status;
15205 	i40e_status status;
15206 #endif /* CONFIG_I40E_DCB */
15207 	struct i40e_pf *pf;
15208 	struct i40e_hw *hw;
15209 	static u16 pfs_found;
15210 	u16 wol_nvm_bits;
15211 	u16 link_status;
15212 	int err;
15213 	u32 val;
15214 	u32 i;
15215 
15216 	err = pci_enable_device_mem(pdev);
15217 	if (err)
15218 		return err;
15219 
15220 	/* set up for high or low dma */
15221 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15222 	if (err) {
15223 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
15224 		if (err) {
15225 			dev_err(&pdev->dev,
15226 				"DMA configuration failed: 0x%x\n", err);
15227 			goto err_dma;
15228 		}
15229 	}
15230 
15231 	/* set up pci connections */
15232 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15233 	if (err) {
15234 		dev_info(&pdev->dev,
15235 			 "pci_request_selected_regions failed %d\n", err);
15236 		goto err_pci_reg;
15237 	}
15238 
15239 	pci_enable_pcie_error_reporting(pdev);
15240 	pci_set_master(pdev);
15241 
15242 	/* Now that we have a PCI connection, we need to do the
15243 	 * low level device setup.  This is primarily setting up
15244 	 * the Admin Queue structures and then querying for the
15245 	 * device's current profile information.
15246 	 */
15247 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15248 	if (!pf) {
15249 		err = -ENOMEM;
15250 		goto err_pf_alloc;
15251 	}
15252 	pf->next_vsi = 0;
15253 	pf->pdev = pdev;
15254 	set_bit(__I40E_DOWN, pf->state);
15255 
15256 	hw = &pf->hw;
15257 	hw->back = pf;
15258 
15259 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15260 				I40E_MAX_CSR_SPACE);
15261 	/* We believe that the highest register to read is
15262 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15263 	 * is not less than that before mapping to prevent a
15264 	 * kernel panic.
15265 	 */
15266 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15267 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15268 			pf->ioremap_len);
15269 		err = -ENOMEM;
15270 		goto err_ioremap;
15271 	}
15272 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15273 	if (!hw->hw_addr) {
15274 		err = -EIO;
15275 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15276 			 (unsigned int)pci_resource_start(pdev, 0),
15277 			 pf->ioremap_len, err);
15278 		goto err_ioremap;
15279 	}
15280 	hw->vendor_id = pdev->vendor;
15281 	hw->device_id = pdev->device;
15282 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15283 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15284 	i40e_set_subsystem_device_id(hw);
15285 	hw->bus.device = PCI_SLOT(pdev->devfn);
15286 	hw->bus.func = PCI_FUNC(pdev->devfn);
15287 	hw->bus.bus_id = pdev->bus->number;
15288 	pf->instance = pfs_found;
15289 
15290 	/* Select something other than the 802.1ad ethertype for the
15291 	 * switch to use internally and drop on ingress.
15292 	 */
15293 	hw->switch_tag = 0xffff;
15294 	hw->first_tag = ETH_P_8021AD;
15295 	hw->second_tag = ETH_P_8021Q;
15296 
15297 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15298 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15299 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15300 
15301 	/* set up the locks for the AQ, do this only once in probe
15302 	 * and destroy them only once in remove
15303 	 */
15304 	mutex_init(&hw->aq.asq_mutex);
15305 	mutex_init(&hw->aq.arq_mutex);
15306 
15307 	pf->msg_enable = netif_msg_init(debug,
15308 					NETIF_MSG_DRV |
15309 					NETIF_MSG_PROBE |
15310 					NETIF_MSG_LINK);
15311 	if (debug < -1)
15312 		pf->hw.debug_mask = debug;
15313 
15314 	/* do a special CORER for clearing PXE mode once at init */
15315 	if (hw->revision_id == 0 &&
15316 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15317 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15318 		i40e_flush(hw);
15319 		msleep(200);
15320 		pf->corer_count++;
15321 
15322 		i40e_clear_pxe_mode(hw);
15323 	}
15324 
15325 	/* Reset here to make sure all is clean and to define PF 'n' */
15326 	i40e_clear_hw(hw);
15327 
15328 	err = i40e_set_mac_type(hw);
15329 	if (err) {
15330 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15331 			 err);
15332 		goto err_pf_reset;
15333 	}
15334 
15335 	err = i40e_handle_resets(pf);
15336 	if (err)
15337 		goto err_pf_reset;
15338 
15339 	i40e_check_recovery_mode(pf);
15340 
15341 	if (is_kdump_kernel()) {
15342 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15343 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15344 	} else {
15345 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15346 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15347 	}
15348 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15349 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15350 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15351 
15352 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15353 		 "%s-%s:misc",
15354 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15355 
15356 	err = i40e_init_shared_code(hw);
15357 	if (err) {
15358 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15359 			 err);
15360 		goto err_pf_reset;
15361 	}
15362 
15363 	/* set up a default setting for link flow control */
15364 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15365 
15366 	err = i40e_init_adminq(hw);
15367 	if (err) {
15368 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15369 			dev_info(&pdev->dev,
15370 				 "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",
15371 				 hw->aq.api_maj_ver,
15372 				 hw->aq.api_min_ver,
15373 				 I40E_FW_API_VERSION_MAJOR,
15374 				 I40E_FW_MINOR_VERSION(hw));
15375 		else
15376 			dev_info(&pdev->dev,
15377 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15378 
15379 		goto err_pf_reset;
15380 	}
15381 	i40e_get_oem_version(hw);
15382 
15383 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15384 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15385 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15386 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15387 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15388 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15389 
15390 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15391 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15392 		dev_info(&pdev->dev,
15393 			 "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",
15394 			 hw->aq.api_maj_ver,
15395 			 hw->aq.api_min_ver,
15396 			 I40E_FW_API_VERSION_MAJOR,
15397 			 I40E_FW_MINOR_VERSION(hw));
15398 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15399 		dev_info(&pdev->dev,
15400 			 "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",
15401 			 hw->aq.api_maj_ver,
15402 			 hw->aq.api_min_ver,
15403 			 I40E_FW_API_VERSION_MAJOR,
15404 			 I40E_FW_MINOR_VERSION(hw));
15405 
15406 	i40e_verify_eeprom(pf);
15407 
15408 	/* Rev 0 hardware was never productized */
15409 	if (hw->revision_id < 1)
15410 		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");
15411 
15412 	i40e_clear_pxe_mode(hw);
15413 
15414 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15415 	if (err)
15416 		goto err_adminq_setup;
15417 
15418 	err = i40e_sw_init(pf);
15419 	if (err) {
15420 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15421 		goto err_sw_init;
15422 	}
15423 
15424 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15425 		return i40e_init_recovery_mode(pf, hw);
15426 
15427 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15428 				hw->func_caps.num_rx_qp, 0, 0);
15429 	if (err) {
15430 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15431 		goto err_init_lan_hmc;
15432 	}
15433 
15434 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15435 	if (err) {
15436 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15437 		err = -ENOENT;
15438 		goto err_configure_lan_hmc;
15439 	}
15440 
15441 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15442 	 * Ignore error return codes because if it was already disabled via
15443 	 * hardware settings this will fail
15444 	 */
15445 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15446 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15447 		i40e_aq_stop_lldp(hw, true, false, NULL);
15448 	}
15449 
15450 	/* allow a platform config to override the HW addr */
15451 	i40e_get_platform_mac_addr(pdev, pf);
15452 
15453 	if (!is_valid_ether_addr(hw->mac.addr)) {
15454 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15455 		err = -EIO;
15456 		goto err_mac_addr;
15457 	}
15458 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15459 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15460 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15461 	if (is_valid_ether_addr(hw->mac.port_addr))
15462 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15463 
15464 	i40e_ptp_alloc_pins(pf);
15465 	pci_set_drvdata(pdev, pf);
15466 	pci_save_state(pdev);
15467 
15468 #ifdef CONFIG_I40E_DCB
15469 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15470 	(!status &&
15471 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15472 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15473 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15474 	dev_info(&pdev->dev,
15475 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15476 			"FW LLDP is disabled\n" :
15477 			"FW LLDP is enabled\n");
15478 
15479 	/* Enable FW to write default DCB config on link-up */
15480 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15481 
15482 	err = i40e_init_pf_dcb(pf);
15483 	if (err) {
15484 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15485 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15486 		/* Continue without DCB enabled */
15487 	}
15488 #endif /* CONFIG_I40E_DCB */
15489 
15490 	/* set up periodic task facility */
15491 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15492 	pf->service_timer_period = HZ;
15493 
15494 	INIT_WORK(&pf->service_task, i40e_service_task);
15495 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15496 
15497 	/* NVM bit on means WoL disabled for the port */
15498 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15499 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15500 		pf->wol_en = false;
15501 	else
15502 		pf->wol_en = true;
15503 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15504 
15505 	/* set up the main switch operations */
15506 	i40e_determine_queue_usage(pf);
15507 	err = i40e_init_interrupt_scheme(pf);
15508 	if (err)
15509 		goto err_switch_setup;
15510 
15511 	/* Reduce Tx and Rx pairs for kdump
15512 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15513 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15514 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15515 	 */
15516 	if (is_kdump_kernel())
15517 		pf->num_lan_msix = 1;
15518 
15519 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15520 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15521 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15522 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15523 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15524 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15525 						    UDP_TUNNEL_TYPE_GENEVE;
15526 
15527 	/* The number of VSIs reported by the FW is the minimum guaranteed
15528 	 * to us; HW supports far more and we share the remaining pool with
15529 	 * the other PFs. We allocate space for more than the guarantee with
15530 	 * the understanding that we might not get them all later.
15531 	 */
15532 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15533 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15534 	else
15535 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15536 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15537 		dev_warn(&pf->pdev->dev,
15538 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15539 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15540 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15541 	}
15542 
15543 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15544 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15545 			  GFP_KERNEL);
15546 	if (!pf->vsi) {
15547 		err = -ENOMEM;
15548 		goto err_switch_setup;
15549 	}
15550 
15551 #ifdef CONFIG_PCI_IOV
15552 	/* prep for VF support */
15553 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15554 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15555 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15556 		if (pci_num_vf(pdev))
15557 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15558 	}
15559 #endif
15560 	err = i40e_setup_pf_switch(pf, false, false);
15561 	if (err) {
15562 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15563 		goto err_vsis;
15564 	}
15565 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15566 
15567 	/* if FDIR VSI was set up, start it now */
15568 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15569 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15570 			i40e_vsi_open(pf->vsi[i]);
15571 			break;
15572 		}
15573 	}
15574 
15575 	/* The driver only wants link up/down and module qualification
15576 	 * reports from firmware.  Note the negative logic.
15577 	 */
15578 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15579 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15580 					 I40E_AQ_EVENT_MEDIA_NA |
15581 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15582 	if (err)
15583 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15584 			 i40e_stat_str(&pf->hw, err),
15585 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15586 
15587 	/* Reconfigure hardware for allowing smaller MSS in the case
15588 	 * of TSO, so that we avoid the MDD being fired and causing
15589 	 * a reset in the case of small MSS+TSO.
15590 	 */
15591 	val = rd32(hw, I40E_REG_MSS);
15592 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15593 		val &= ~I40E_REG_MSS_MIN_MASK;
15594 		val |= I40E_64BYTE_MSS;
15595 		wr32(hw, I40E_REG_MSS, val);
15596 	}
15597 
15598 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15599 		msleep(75);
15600 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15601 		if (err)
15602 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15603 				 i40e_stat_str(&pf->hw, err),
15604 				 i40e_aq_str(&pf->hw,
15605 					     pf->hw.aq.asq_last_status));
15606 	}
15607 	/* The main driver is (mostly) up and happy. We need to set this state
15608 	 * before setting up the misc vector or we get a race and the vector
15609 	 * ends up disabled forever.
15610 	 */
15611 	clear_bit(__I40E_DOWN, pf->state);
15612 
15613 	/* In case of MSIX we are going to setup the misc vector right here
15614 	 * to handle admin queue events etc. In case of legacy and MSI
15615 	 * the misc functionality and queue processing is combined in
15616 	 * the same vector and that gets setup at open.
15617 	 */
15618 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15619 		err = i40e_setup_misc_vector(pf);
15620 		if (err) {
15621 			dev_info(&pdev->dev,
15622 				 "setup of misc vector failed: %d\n", err);
15623 			i40e_cloud_filter_exit(pf);
15624 			i40e_fdir_teardown(pf);
15625 			goto err_vsis;
15626 		}
15627 	}
15628 
15629 #ifdef CONFIG_PCI_IOV
15630 	/* prep for VF support */
15631 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15632 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15633 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15634 		/* disable link interrupts for VFs */
15635 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15636 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15637 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15638 		i40e_flush(hw);
15639 
15640 		if (pci_num_vf(pdev)) {
15641 			dev_info(&pdev->dev,
15642 				 "Active VFs found, allocating resources.\n");
15643 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15644 			if (err)
15645 				dev_info(&pdev->dev,
15646 					 "Error %d allocating resources for existing VFs\n",
15647 					 err);
15648 		}
15649 	}
15650 #endif /* CONFIG_PCI_IOV */
15651 
15652 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15653 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
15654 						      pf->num_iwarp_msix,
15655 						      I40E_IWARP_IRQ_PILE_ID);
15656 		if (pf->iwarp_base_vector < 0) {
15657 			dev_info(&pdev->dev,
15658 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
15659 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
15660 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
15661 		}
15662 	}
15663 
15664 	i40e_dbg_pf_init(pf);
15665 
15666 	/* tell the firmware that we're starting */
15667 	i40e_send_version(pf);
15668 
15669 	/* since everything's happy, start the service_task timer */
15670 	mod_timer(&pf->service_timer,
15671 		  round_jiffies(jiffies + pf->service_timer_period));
15672 
15673 	/* add this PF to client device list and launch a client service task */
15674 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15675 		err = i40e_lan_add_device(pf);
15676 		if (err)
15677 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
15678 				 err);
15679 	}
15680 
15681 #define PCI_SPEED_SIZE 8
15682 #define PCI_WIDTH_SIZE 8
15683 	/* Devices on the IOSF bus do not have this information
15684 	 * and will report PCI Gen 1 x 1 by default so don't bother
15685 	 * checking them.
15686 	 */
15687 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
15688 		char speed[PCI_SPEED_SIZE] = "Unknown";
15689 		char width[PCI_WIDTH_SIZE] = "Unknown";
15690 
15691 		/* Get the negotiated link width and speed from PCI config
15692 		 * space
15693 		 */
15694 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
15695 					  &link_status);
15696 
15697 		i40e_set_pci_config_data(hw, link_status);
15698 
15699 		switch (hw->bus.speed) {
15700 		case i40e_bus_speed_8000:
15701 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
15702 		case i40e_bus_speed_5000:
15703 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
15704 		case i40e_bus_speed_2500:
15705 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
15706 		default:
15707 			break;
15708 		}
15709 		switch (hw->bus.width) {
15710 		case i40e_bus_width_pcie_x8:
15711 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
15712 		case i40e_bus_width_pcie_x4:
15713 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
15714 		case i40e_bus_width_pcie_x2:
15715 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
15716 		case i40e_bus_width_pcie_x1:
15717 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
15718 		default:
15719 			break;
15720 		}
15721 
15722 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
15723 			 speed, width);
15724 
15725 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
15726 		    hw->bus.speed < i40e_bus_speed_8000) {
15727 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
15728 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
15729 		}
15730 	}
15731 
15732 	/* get the requested speeds from the fw */
15733 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
15734 	if (err)
15735 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
15736 			i40e_stat_str(&pf->hw, err),
15737 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15738 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
15739 
15740 	/* set the FEC config due to the board capabilities */
15741 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
15742 
15743 	/* get the supported phy types from the fw */
15744 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
15745 	if (err)
15746 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
15747 			i40e_stat_str(&pf->hw, err),
15748 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15749 
15750 	/* make sure the MFS hasn't been set lower than the default */
15751 #define MAX_FRAME_SIZE_DEFAULT 0x2600
15752 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
15753 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
15754 	if (val < MAX_FRAME_SIZE_DEFAULT)
15755 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
15756 			 i, val);
15757 
15758 	/* Add a filter to drop all Flow control frames from any VSI from being
15759 	 * transmitted. By doing so we stop a malicious VF from sending out
15760 	 * PAUSE or PFC frames and potentially controlling traffic for other
15761 	 * PF/VF VSIs.
15762 	 * The FW can still send Flow control frames if enabled.
15763 	 */
15764 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
15765 						       pf->main_vsi_seid);
15766 
15767 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
15768 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
15769 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
15770 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
15771 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
15772 	/* print a string summarizing features */
15773 	i40e_print_features(pf);
15774 
15775 	return 0;
15776 
15777 	/* Unwind what we've done if something failed in the setup */
15778 err_vsis:
15779 	set_bit(__I40E_DOWN, pf->state);
15780 	i40e_clear_interrupt_scheme(pf);
15781 	kfree(pf->vsi);
15782 err_switch_setup:
15783 	i40e_reset_interrupt_capability(pf);
15784 	del_timer_sync(&pf->service_timer);
15785 err_mac_addr:
15786 err_configure_lan_hmc:
15787 	(void)i40e_shutdown_lan_hmc(hw);
15788 err_init_lan_hmc:
15789 	kfree(pf->qp_pile);
15790 err_sw_init:
15791 err_adminq_setup:
15792 err_pf_reset:
15793 	iounmap(hw->hw_addr);
15794 err_ioremap:
15795 	kfree(pf);
15796 err_pf_alloc:
15797 	pci_disable_pcie_error_reporting(pdev);
15798 	pci_release_mem_regions(pdev);
15799 err_pci_reg:
15800 err_dma:
15801 	pci_disable_device(pdev);
15802 	return err;
15803 }
15804 
15805 /**
15806  * i40e_remove - Device removal routine
15807  * @pdev: PCI device information struct
15808  *
15809  * i40e_remove is called by the PCI subsystem to alert the driver
15810  * that is should release a PCI device.  This could be caused by a
15811  * Hot-Plug event, or because the driver is going to be removed from
15812  * memory.
15813  **/
15814 static void i40e_remove(struct pci_dev *pdev)
15815 {
15816 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15817 	struct i40e_hw *hw = &pf->hw;
15818 	i40e_status ret_code;
15819 	int i;
15820 
15821 	i40e_dbg_pf_exit(pf);
15822 
15823 	i40e_ptp_stop(pf);
15824 
15825 	/* Disable RSS in hw */
15826 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
15827 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
15828 
15829 	while (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
15830 		usleep_range(1000, 2000);
15831 
15832 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
15833 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
15834 		i40e_free_vfs(pf);
15835 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
15836 	}
15837 	/* no more scheduling of any task */
15838 	set_bit(__I40E_SUSPENDED, pf->state);
15839 	set_bit(__I40E_DOWN, pf->state);
15840 	if (pf->service_timer.function)
15841 		del_timer_sync(&pf->service_timer);
15842 	if (pf->service_task.func)
15843 		cancel_work_sync(&pf->service_task);
15844 
15845 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
15846 		struct i40e_vsi *vsi = pf->vsi[0];
15847 
15848 		/* We know that we have allocated only one vsi for this PF,
15849 		 * it was just for registering netdevice, so the interface
15850 		 * could be visible in the 'ifconfig' output
15851 		 */
15852 		unregister_netdev(vsi->netdev);
15853 		free_netdev(vsi->netdev);
15854 
15855 		goto unmap;
15856 	}
15857 
15858 	/* Client close must be called explicitly here because the timer
15859 	 * has been stopped.
15860 	 */
15861 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15862 
15863 	i40e_fdir_teardown(pf);
15864 
15865 	/* If there is a switch structure or any orphans, remove them.
15866 	 * This will leave only the PF's VSI remaining.
15867 	 */
15868 	for (i = 0; i < I40E_MAX_VEB; i++) {
15869 		if (!pf->veb[i])
15870 			continue;
15871 
15872 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
15873 		    pf->veb[i]->uplink_seid == 0)
15874 			i40e_switch_branch_release(pf->veb[i]);
15875 	}
15876 
15877 	/* Now we can shutdown the PF's VSI, just before we kill
15878 	 * adminq and hmc.
15879 	 */
15880 	if (pf->vsi[pf->lan_vsi])
15881 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
15882 
15883 	i40e_cloud_filter_exit(pf);
15884 
15885 	/* remove attached clients */
15886 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15887 		ret_code = i40e_lan_del_device(pf);
15888 		if (ret_code)
15889 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
15890 				 ret_code);
15891 	}
15892 
15893 	/* shutdown and destroy the HMC */
15894 	if (hw->hmc.hmc_obj) {
15895 		ret_code = i40e_shutdown_lan_hmc(hw);
15896 		if (ret_code)
15897 			dev_warn(&pdev->dev,
15898 				 "Failed to destroy the HMC resources: %d\n",
15899 				 ret_code);
15900 	}
15901 
15902 unmap:
15903 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
15904 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
15905 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
15906 		free_irq(pf->pdev->irq, pf);
15907 
15908 	/* shutdown the adminq */
15909 	i40e_shutdown_adminq(hw);
15910 
15911 	/* destroy the locks only once, here */
15912 	mutex_destroy(&hw->aq.arq_mutex);
15913 	mutex_destroy(&hw->aq.asq_mutex);
15914 
15915 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
15916 	rtnl_lock();
15917 	i40e_clear_interrupt_scheme(pf);
15918 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15919 		if (pf->vsi[i]) {
15920 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
15921 				i40e_vsi_clear_rings(pf->vsi[i]);
15922 			i40e_vsi_clear(pf->vsi[i]);
15923 			pf->vsi[i] = NULL;
15924 		}
15925 	}
15926 	rtnl_unlock();
15927 
15928 	for (i = 0; i < I40E_MAX_VEB; i++) {
15929 		kfree(pf->veb[i]);
15930 		pf->veb[i] = NULL;
15931 	}
15932 
15933 	kfree(pf->qp_pile);
15934 	kfree(pf->vsi);
15935 
15936 	iounmap(hw->hw_addr);
15937 	kfree(pf);
15938 	pci_release_mem_regions(pdev);
15939 
15940 	pci_disable_pcie_error_reporting(pdev);
15941 	pci_disable_device(pdev);
15942 }
15943 
15944 /**
15945  * i40e_pci_error_detected - warning that something funky happened in PCI land
15946  * @pdev: PCI device information struct
15947  * @error: the type of PCI error
15948  *
15949  * Called to warn that something happened and the error handling steps
15950  * are in progress.  Allows the driver to quiesce things, be ready for
15951  * remediation.
15952  **/
15953 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
15954 						pci_channel_state_t error)
15955 {
15956 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15957 
15958 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
15959 
15960 	if (!pf) {
15961 		dev_info(&pdev->dev,
15962 			 "Cannot recover - error happened during device probe\n");
15963 		return PCI_ERS_RESULT_DISCONNECT;
15964 	}
15965 
15966 	/* shutdown all operations */
15967 	if (!test_bit(__I40E_SUSPENDED, pf->state))
15968 		i40e_prep_for_reset(pf);
15969 
15970 	/* Request a slot reset */
15971 	return PCI_ERS_RESULT_NEED_RESET;
15972 }
15973 
15974 /**
15975  * i40e_pci_error_slot_reset - a PCI slot reset just happened
15976  * @pdev: PCI device information struct
15977  *
15978  * Called to find if the driver can work with the device now that
15979  * the pci slot has been reset.  If a basic connection seems good
15980  * (registers are readable and have sane content) then return a
15981  * happy little PCI_ERS_RESULT_xxx.
15982  **/
15983 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
15984 {
15985 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15986 	pci_ers_result_t result;
15987 	u32 reg;
15988 
15989 	dev_dbg(&pdev->dev, "%s\n", __func__);
15990 	if (pci_enable_device_mem(pdev)) {
15991 		dev_info(&pdev->dev,
15992 			 "Cannot re-enable PCI device after reset.\n");
15993 		result = PCI_ERS_RESULT_DISCONNECT;
15994 	} else {
15995 		pci_set_master(pdev);
15996 		pci_restore_state(pdev);
15997 		pci_save_state(pdev);
15998 		pci_wake_from_d3(pdev, false);
15999 
16000 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16001 		if (reg == 0)
16002 			result = PCI_ERS_RESULT_RECOVERED;
16003 		else
16004 			result = PCI_ERS_RESULT_DISCONNECT;
16005 	}
16006 
16007 	return result;
16008 }
16009 
16010 /**
16011  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16012  * @pdev: PCI device information struct
16013  */
16014 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16015 {
16016 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16017 
16018 	i40e_prep_for_reset(pf);
16019 }
16020 
16021 /**
16022  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16023  * @pdev: PCI device information struct
16024  */
16025 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16026 {
16027 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16028 
16029 	i40e_reset_and_rebuild(pf, false, false);
16030 }
16031 
16032 /**
16033  * i40e_pci_error_resume - restart operations after PCI error recovery
16034  * @pdev: PCI device information struct
16035  *
16036  * Called to allow the driver to bring things back up after PCI error
16037  * and/or reset recovery has finished.
16038  **/
16039 static void i40e_pci_error_resume(struct pci_dev *pdev)
16040 {
16041 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16042 
16043 	dev_dbg(&pdev->dev, "%s\n", __func__);
16044 	if (test_bit(__I40E_SUSPENDED, pf->state))
16045 		return;
16046 
16047 	i40e_handle_reset_warning(pf, false);
16048 }
16049 
16050 /**
16051  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16052  * using the mac_address_write admin q function
16053  * @pf: pointer to i40e_pf struct
16054  **/
16055 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16056 {
16057 	struct i40e_hw *hw = &pf->hw;
16058 	i40e_status ret;
16059 	u8 mac_addr[6];
16060 	u16 flags = 0;
16061 
16062 	/* Get current MAC address in case it's an LAA */
16063 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16064 		ether_addr_copy(mac_addr,
16065 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16066 	} else {
16067 		dev_err(&pf->pdev->dev,
16068 			"Failed to retrieve MAC address; using default\n");
16069 		ether_addr_copy(mac_addr, hw->mac.addr);
16070 	}
16071 
16072 	/* The FW expects the mac address write cmd to first be called with
16073 	 * one of these flags before calling it again with the multicast
16074 	 * enable flags.
16075 	 */
16076 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16077 
16078 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16079 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16080 
16081 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16082 	if (ret) {
16083 		dev_err(&pf->pdev->dev,
16084 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16085 		return;
16086 	}
16087 
16088 	flags = I40E_AQC_MC_MAG_EN
16089 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16090 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16091 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16092 	if (ret)
16093 		dev_err(&pf->pdev->dev,
16094 			"Failed to enable Multicast Magic Packet wake up\n");
16095 }
16096 
16097 /**
16098  * i40e_shutdown - PCI callback for shutting down
16099  * @pdev: PCI device information struct
16100  **/
16101 static void i40e_shutdown(struct pci_dev *pdev)
16102 {
16103 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16104 	struct i40e_hw *hw = &pf->hw;
16105 
16106 	set_bit(__I40E_SUSPENDED, pf->state);
16107 	set_bit(__I40E_DOWN, pf->state);
16108 
16109 	del_timer_sync(&pf->service_timer);
16110 	cancel_work_sync(&pf->service_task);
16111 	i40e_cloud_filter_exit(pf);
16112 	i40e_fdir_teardown(pf);
16113 
16114 	/* Client close must be called explicitly here because the timer
16115 	 * has been stopped.
16116 	 */
16117 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16118 
16119 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16120 		i40e_enable_mc_magic_wake(pf);
16121 
16122 	i40e_prep_for_reset(pf);
16123 
16124 	wr32(hw, I40E_PFPM_APM,
16125 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16126 	wr32(hw, I40E_PFPM_WUFC,
16127 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16128 
16129 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16130 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16131 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16132 		free_irq(pf->pdev->irq, pf);
16133 
16134 	/* Since we're going to destroy queues during the
16135 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16136 	 * whole section
16137 	 */
16138 	rtnl_lock();
16139 	i40e_clear_interrupt_scheme(pf);
16140 	rtnl_unlock();
16141 
16142 	if (system_state == SYSTEM_POWER_OFF) {
16143 		pci_wake_from_d3(pdev, pf->wol_en);
16144 		pci_set_power_state(pdev, PCI_D3hot);
16145 	}
16146 }
16147 
16148 /**
16149  * i40e_suspend - PM callback for moving to D3
16150  * @dev: generic device information structure
16151  **/
16152 static int __maybe_unused i40e_suspend(struct device *dev)
16153 {
16154 	struct i40e_pf *pf = dev_get_drvdata(dev);
16155 	struct i40e_hw *hw = &pf->hw;
16156 
16157 	/* If we're already suspended, then there is nothing to do */
16158 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16159 		return 0;
16160 
16161 	set_bit(__I40E_DOWN, pf->state);
16162 
16163 	/* Ensure service task will not be running */
16164 	del_timer_sync(&pf->service_timer);
16165 	cancel_work_sync(&pf->service_task);
16166 
16167 	/* Client close must be called explicitly here because the timer
16168 	 * has been stopped.
16169 	 */
16170 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16171 
16172 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16173 		i40e_enable_mc_magic_wake(pf);
16174 
16175 	/* Since we're going to destroy queues during the
16176 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16177 	 * whole section
16178 	 */
16179 	rtnl_lock();
16180 
16181 	i40e_prep_for_reset(pf);
16182 
16183 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16184 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16185 
16186 	/* Clear the interrupt scheme and release our IRQs so that the system
16187 	 * can safely hibernate even when there are a large number of CPUs.
16188 	 * Otherwise hibernation might fail when mapping all the vectors back
16189 	 * to CPU0.
16190 	 */
16191 	i40e_clear_interrupt_scheme(pf);
16192 
16193 	rtnl_unlock();
16194 
16195 	return 0;
16196 }
16197 
16198 /**
16199  * i40e_resume - PM callback for waking up from D3
16200  * @dev: generic device information structure
16201  **/
16202 static int __maybe_unused i40e_resume(struct device *dev)
16203 {
16204 	struct i40e_pf *pf = dev_get_drvdata(dev);
16205 	int err;
16206 
16207 	/* If we're not suspended, then there is nothing to do */
16208 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16209 		return 0;
16210 
16211 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16212 	 * since we're going to be restoring queues
16213 	 */
16214 	rtnl_lock();
16215 
16216 	/* We cleared the interrupt scheme when we suspended, so we need to
16217 	 * restore it now to resume device functionality.
16218 	 */
16219 	err = i40e_restore_interrupt_scheme(pf);
16220 	if (err) {
16221 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16222 			err);
16223 	}
16224 
16225 	clear_bit(__I40E_DOWN, pf->state);
16226 	i40e_reset_and_rebuild(pf, false, true);
16227 
16228 	rtnl_unlock();
16229 
16230 	/* Clear suspended state last after everything is recovered */
16231 	clear_bit(__I40E_SUSPENDED, pf->state);
16232 
16233 	/* Restart the service task */
16234 	mod_timer(&pf->service_timer,
16235 		  round_jiffies(jiffies + pf->service_timer_period));
16236 
16237 	return 0;
16238 }
16239 
16240 static const struct pci_error_handlers i40e_err_handler = {
16241 	.error_detected = i40e_pci_error_detected,
16242 	.slot_reset = i40e_pci_error_slot_reset,
16243 	.reset_prepare = i40e_pci_error_reset_prepare,
16244 	.reset_done = i40e_pci_error_reset_done,
16245 	.resume = i40e_pci_error_resume,
16246 };
16247 
16248 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16249 
16250 static struct pci_driver i40e_driver = {
16251 	.name     = i40e_driver_name,
16252 	.id_table = i40e_pci_tbl,
16253 	.probe    = i40e_probe,
16254 	.remove   = i40e_remove,
16255 	.driver   = {
16256 		.pm = &i40e_pm_ops,
16257 	},
16258 	.shutdown = i40e_shutdown,
16259 	.err_handler = &i40e_err_handler,
16260 	.sriov_configure = i40e_pci_sriov_configure,
16261 };
16262 
16263 /**
16264  * i40e_init_module - Driver registration routine
16265  *
16266  * i40e_init_module is the first routine called when the driver is
16267  * loaded. All it does is register with the PCI subsystem.
16268  **/
16269 static int __init i40e_init_module(void)
16270 {
16271 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16272 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16273 
16274 	/* There is no need to throttle the number of active tasks because
16275 	 * each device limits its own task using a state bit for scheduling
16276 	 * the service task, and the device tasks do not interfere with each
16277 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16278 	 * since we need to be able to guarantee forward progress even under
16279 	 * memory pressure.
16280 	 */
16281 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16282 	if (!i40e_wq) {
16283 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16284 		return -ENOMEM;
16285 	}
16286 
16287 	i40e_dbg_init();
16288 	return pci_register_driver(&i40e_driver);
16289 }
16290 module_init(i40e_init_module);
16291 
16292 /**
16293  * i40e_exit_module - Driver exit cleanup routine
16294  *
16295  * i40e_exit_module is called just before the driver is removed
16296  * from memory.
16297  **/
16298 static void __exit i40e_exit_module(void)
16299 {
16300 	pci_unregister_driver(&i40e_driver);
16301 	destroy_workqueue(i40e_wq);
16302 	ida_destroy(&i40e_client_ida);
16303 	i40e_dbg_exit();
16304 }
16305 module_exit(i40e_exit_module);
16306