xref: /linux/drivers/net/ethernet/intel/i40e/i40e_main.c (revision 27b3f70553432114b3d26f4d9c72cf02f38b84ee)
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_SFP_X722_A), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
87 	/* required last entry */
88 	{0, }
89 };
90 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
91 
92 #define I40E_MAX_VF_COUNT 128
93 static int debug = -1;
94 module_param(debug, uint, 0);
95 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
96 
97 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
98 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
99 MODULE_LICENSE("GPL v2");
100 
101 static struct workqueue_struct *i40e_wq;
102 
103 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
104 				  struct net_device *netdev, int delta)
105 {
106 	struct netdev_hw_addr *ha;
107 
108 	if (!f || !netdev)
109 		return;
110 
111 	netdev_for_each_mc_addr(ha, netdev) {
112 		if (ether_addr_equal(ha->addr, f->macaddr)) {
113 			ha->refcount += delta;
114 			if (ha->refcount <= 0)
115 				ha->refcount = 1;
116 			break;
117 		}
118 	}
119 }
120 
121 /**
122  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
123  * @hw:   pointer to the HW structure
124  * @mem:  ptr to mem struct to fill out
125  * @size: size of memory requested
126  * @alignment: what to align the allocation to
127  **/
128 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
129 			    u64 size, u32 alignment)
130 {
131 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
132 
133 	mem->size = ALIGN(size, alignment);
134 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
135 				     GFP_KERNEL);
136 	if (!mem->va)
137 		return -ENOMEM;
138 
139 	return 0;
140 }
141 
142 /**
143  * i40e_free_dma_mem_d - OS specific memory free for shared code
144  * @hw:   pointer to the HW structure
145  * @mem:  ptr to mem struct to free
146  **/
147 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
148 {
149 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
150 
151 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
152 	mem->va = NULL;
153 	mem->pa = 0;
154 	mem->size = 0;
155 
156 	return 0;
157 }
158 
159 /**
160  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
161  * @hw:   pointer to the HW structure
162  * @mem:  ptr to mem struct to fill out
163  * @size: size of memory requested
164  **/
165 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
166 			     u32 size)
167 {
168 	mem->size = size;
169 	mem->va = kzalloc(size, GFP_KERNEL);
170 
171 	if (!mem->va)
172 		return -ENOMEM;
173 
174 	return 0;
175 }
176 
177 /**
178  * i40e_free_virt_mem_d - OS specific memory free for shared code
179  * @hw:   pointer to the HW structure
180  * @mem:  ptr to mem struct to free
181  **/
182 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
183 {
184 	/* it's ok to kfree a NULL pointer */
185 	kfree(mem->va);
186 	mem->va = NULL;
187 	mem->size = 0;
188 
189 	return 0;
190 }
191 
192 /**
193  * i40e_get_lump - find a lump of free generic resource
194  * @pf: board private structure
195  * @pile: the pile of resource to search
196  * @needed: the number of items needed
197  * @id: an owner id to stick on the items assigned
198  *
199  * Returns the base item index of the lump, or negative for error
200  **/
201 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
202 			 u16 needed, u16 id)
203 {
204 	int ret = -ENOMEM;
205 	int i, j;
206 
207 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
208 		dev_info(&pf->pdev->dev,
209 			 "param err: pile=%s needed=%d id=0x%04x\n",
210 			 pile ? "<valid>" : "<null>", needed, id);
211 		return -EINVAL;
212 	}
213 
214 	/* Allocate last queue in the pile for FDIR VSI queue
215 	 * so it doesn't fragment the qp_pile
216 	 */
217 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
218 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
219 			dev_err(&pf->pdev->dev,
220 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
221 				pile->num_entries - 1);
222 			return -ENOMEM;
223 		}
224 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
225 		return pile->num_entries - 1;
226 	}
227 
228 	i = 0;
229 	while (i < pile->num_entries) {
230 		/* skip already allocated entries */
231 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
232 			i++;
233 			continue;
234 		}
235 
236 		/* do we have enough in this lump? */
237 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
238 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
239 				break;
240 		}
241 
242 		if (j == needed) {
243 			/* there was enough, so assign it to the requestor */
244 			for (j = 0; j < needed; j++)
245 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
246 			ret = i;
247 			break;
248 		}
249 
250 		/* not enough, so skip over it and continue looking */
251 		i += j;
252 	}
253 
254 	return ret;
255 }
256 
257 /**
258  * i40e_put_lump - return a lump of generic resource
259  * @pile: the pile of resource to search
260  * @index: the base item index
261  * @id: the owner id of the items assigned
262  *
263  * Returns the count of items in the lump
264  **/
265 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
266 {
267 	int valid_id = (id | I40E_PILE_VALID_BIT);
268 	int count = 0;
269 	u16 i;
270 
271 	if (!pile || index >= pile->num_entries)
272 		return -EINVAL;
273 
274 	for (i = index;
275 	     i < pile->num_entries && pile->list[i] == valid_id;
276 	     i++) {
277 		pile->list[i] = 0;
278 		count++;
279 	}
280 
281 
282 	return count;
283 }
284 
285 /**
286  * i40e_find_vsi_from_id - searches for the vsi with the given id
287  * @pf: the pf structure to search for the vsi
288  * @id: id of the vsi it is searching for
289  **/
290 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
291 {
292 	int i;
293 
294 	for (i = 0; i < pf->num_alloc_vsi; i++)
295 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
296 			return pf->vsi[i];
297 
298 	return NULL;
299 }
300 
301 /**
302  * i40e_service_event_schedule - Schedule the service task to wake up
303  * @pf: board private structure
304  *
305  * If not already scheduled, this puts the task into the work queue
306  **/
307 void i40e_service_event_schedule(struct i40e_pf *pf)
308 {
309 	if ((!test_bit(__I40E_DOWN, pf->state) &&
310 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
311 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
312 		queue_work(i40e_wq, &pf->service_task);
313 }
314 
315 /**
316  * i40e_tx_timeout - Respond to a Tx Hang
317  * @netdev: network interface device structure
318  * @txqueue: queue number timing out
319  *
320  * If any port has noticed a Tx timeout, it is likely that the whole
321  * device is munged, not just the one netdev port, so go for the full
322  * reset.
323  **/
324 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
325 {
326 	struct i40e_netdev_priv *np = netdev_priv(netdev);
327 	struct i40e_vsi *vsi = np->vsi;
328 	struct i40e_pf *pf = vsi->back;
329 	struct i40e_ring *tx_ring = NULL;
330 	unsigned int i;
331 	u32 head, val;
332 
333 	pf->tx_timeout_count++;
334 
335 	/* with txqueue index, find the tx_ring struct */
336 	for (i = 0; i < vsi->num_queue_pairs; i++) {
337 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
338 			if (txqueue ==
339 			    vsi->tx_rings[i]->queue_index) {
340 				tx_ring = vsi->tx_rings[i];
341 				break;
342 			}
343 		}
344 	}
345 
346 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
347 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
348 	else if (time_before(jiffies,
349 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
350 		return;   /* don't do any new action before the next timeout */
351 
352 	/* don't kick off another recovery if one is already pending */
353 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
354 		return;
355 
356 	if (tx_ring) {
357 		head = i40e_get_head(tx_ring);
358 		/* Read interrupt register */
359 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
360 			val = rd32(&pf->hw,
361 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
362 						tx_ring->vsi->base_vector - 1));
363 		else
364 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
365 
366 		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",
367 			    vsi->seid, txqueue, tx_ring->next_to_clean,
368 			    head, tx_ring->next_to_use,
369 			    readl(tx_ring->tail), val);
370 	}
371 
372 	pf->tx_timeout_last_recovery = jiffies;
373 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
374 		    pf->tx_timeout_recovery_level, txqueue);
375 
376 	switch (pf->tx_timeout_recovery_level) {
377 	case 1:
378 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
379 		break;
380 	case 2:
381 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
382 		break;
383 	case 3:
384 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
385 		break;
386 	default:
387 		netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
388 		break;
389 	}
390 
391 	i40e_service_event_schedule(pf);
392 	pf->tx_timeout_recovery_level++;
393 }
394 
395 /**
396  * i40e_get_vsi_stats_struct - Get System Network Statistics
397  * @vsi: the VSI we care about
398  *
399  * Returns the address of the device statistics structure.
400  * The statistics are actually updated from the service task.
401  **/
402 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
403 {
404 	return &vsi->net_stats;
405 }
406 
407 /**
408  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
409  * @ring: Tx ring to get statistics from
410  * @stats: statistics entry to be updated
411  **/
412 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
413 					    struct rtnl_link_stats64 *stats)
414 {
415 	u64 bytes, packets;
416 	unsigned int start;
417 
418 	do {
419 		start = u64_stats_fetch_begin_irq(&ring->syncp);
420 		packets = ring->stats.packets;
421 		bytes   = ring->stats.bytes;
422 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
423 
424 	stats->tx_packets += packets;
425 	stats->tx_bytes   += bytes;
426 }
427 
428 /**
429  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
430  * @netdev: network interface device structure
431  * @stats: data structure to store statistics
432  *
433  * Returns the address of the device statistics structure.
434  * The statistics are actually updated from the service task.
435  **/
436 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
437 				  struct rtnl_link_stats64 *stats)
438 {
439 	struct i40e_netdev_priv *np = netdev_priv(netdev);
440 	struct i40e_vsi *vsi = np->vsi;
441 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
442 	struct i40e_ring *ring;
443 	int i;
444 
445 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
446 		return;
447 
448 	if (!vsi->tx_rings)
449 		return;
450 
451 	rcu_read_lock();
452 	for (i = 0; i < vsi->num_queue_pairs; i++) {
453 		u64 bytes, packets;
454 		unsigned int start;
455 
456 		ring = READ_ONCE(vsi->tx_rings[i]);
457 		if (!ring)
458 			continue;
459 		i40e_get_netdev_stats_struct_tx(ring, stats);
460 
461 		if (i40e_enabled_xdp_vsi(vsi)) {
462 			ring = READ_ONCE(vsi->xdp_rings[i]);
463 			if (!ring)
464 				continue;
465 			i40e_get_netdev_stats_struct_tx(ring, stats);
466 		}
467 
468 		ring = READ_ONCE(vsi->rx_rings[i]);
469 		if (!ring)
470 			continue;
471 		do {
472 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
473 			packets = ring->stats.packets;
474 			bytes   = ring->stats.bytes;
475 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
476 
477 		stats->rx_packets += packets;
478 		stats->rx_bytes   += bytes;
479 
480 	}
481 	rcu_read_unlock();
482 
483 	/* following stats updated by i40e_watchdog_subtask() */
484 	stats->multicast	= vsi_stats->multicast;
485 	stats->tx_errors	= vsi_stats->tx_errors;
486 	stats->tx_dropped	= vsi_stats->tx_dropped;
487 	stats->rx_errors	= vsi_stats->rx_errors;
488 	stats->rx_dropped	= vsi_stats->rx_dropped;
489 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
490 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
491 }
492 
493 /**
494  * i40e_vsi_reset_stats - Resets all stats of the given vsi
495  * @vsi: the VSI to have its stats reset
496  **/
497 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
498 {
499 	struct rtnl_link_stats64 *ns;
500 	int i;
501 
502 	if (!vsi)
503 		return;
504 
505 	ns = i40e_get_vsi_stats_struct(vsi);
506 	memset(ns, 0, sizeof(*ns));
507 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
508 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
509 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
510 	if (vsi->rx_rings && vsi->rx_rings[0]) {
511 		for (i = 0; i < vsi->num_queue_pairs; i++) {
512 			memset(&vsi->rx_rings[i]->stats, 0,
513 			       sizeof(vsi->rx_rings[i]->stats));
514 			memset(&vsi->rx_rings[i]->rx_stats, 0,
515 			       sizeof(vsi->rx_rings[i]->rx_stats));
516 			memset(&vsi->tx_rings[i]->stats, 0,
517 			       sizeof(vsi->tx_rings[i]->stats));
518 			memset(&vsi->tx_rings[i]->tx_stats, 0,
519 			       sizeof(vsi->tx_rings[i]->tx_stats));
520 		}
521 	}
522 	vsi->stat_offsets_loaded = false;
523 }
524 
525 /**
526  * i40e_pf_reset_stats - Reset all of the stats for the given PF
527  * @pf: the PF to be reset
528  **/
529 void i40e_pf_reset_stats(struct i40e_pf *pf)
530 {
531 	int i;
532 
533 	memset(&pf->stats, 0, sizeof(pf->stats));
534 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
535 	pf->stat_offsets_loaded = false;
536 
537 	for (i = 0; i < I40E_MAX_VEB; i++) {
538 		if (pf->veb[i]) {
539 			memset(&pf->veb[i]->stats, 0,
540 			       sizeof(pf->veb[i]->stats));
541 			memset(&pf->veb[i]->stats_offsets, 0,
542 			       sizeof(pf->veb[i]->stats_offsets));
543 			memset(&pf->veb[i]->tc_stats, 0,
544 			       sizeof(pf->veb[i]->tc_stats));
545 			memset(&pf->veb[i]->tc_stats_offsets, 0,
546 			       sizeof(pf->veb[i]->tc_stats_offsets));
547 			pf->veb[i]->stat_offsets_loaded = false;
548 		}
549 	}
550 	pf->hw_csum_rx_error = 0;
551 }
552 
553 /**
554  * i40e_stat_update48 - read and update a 48 bit stat from the chip
555  * @hw: ptr to the hardware info
556  * @hireg: the high 32 bit reg to read
557  * @loreg: the low 32 bit reg to read
558  * @offset_loaded: has the initial offset been loaded yet
559  * @offset: ptr to current offset value
560  * @stat: ptr to the stat
561  *
562  * Since the device stats are not reset at PFReset, they likely will not
563  * be zeroed when the driver starts.  We'll save the first values read
564  * and use them as offsets to be subtracted from the raw values in order
565  * to report stats that count from zero.  In the process, we also manage
566  * the potential roll-over.
567  **/
568 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
569 			       bool offset_loaded, u64 *offset, u64 *stat)
570 {
571 	u64 new_data;
572 
573 	if (hw->device_id == I40E_DEV_ID_QEMU) {
574 		new_data = rd32(hw, loreg);
575 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
576 	} else {
577 		new_data = rd64(hw, loreg);
578 	}
579 	if (!offset_loaded)
580 		*offset = new_data;
581 	if (likely(new_data >= *offset))
582 		*stat = new_data - *offset;
583 	else
584 		*stat = (new_data + BIT_ULL(48)) - *offset;
585 	*stat &= 0xFFFFFFFFFFFFULL;
586 }
587 
588 /**
589  * i40e_stat_update32 - read and update a 32 bit stat from the chip
590  * @hw: ptr to the hardware info
591  * @reg: the hw reg to read
592  * @offset_loaded: has the initial offset been loaded yet
593  * @offset: ptr to current offset value
594  * @stat: ptr to the stat
595  **/
596 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
597 			       bool offset_loaded, u64 *offset, u64 *stat)
598 {
599 	u32 new_data;
600 
601 	new_data = rd32(hw, reg);
602 	if (!offset_loaded)
603 		*offset = new_data;
604 	if (likely(new_data >= *offset))
605 		*stat = (u32)(new_data - *offset);
606 	else
607 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
608 }
609 
610 /**
611  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
612  * @hw: ptr to the hardware info
613  * @reg: the hw reg to read and clear
614  * @stat: ptr to the stat
615  **/
616 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
617 {
618 	u32 new_data = rd32(hw, reg);
619 
620 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
621 	*stat += new_data;
622 }
623 
624 /**
625  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
626  * @vsi: the VSI to be updated
627  **/
628 void i40e_update_eth_stats(struct i40e_vsi *vsi)
629 {
630 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
631 	struct i40e_pf *pf = vsi->back;
632 	struct i40e_hw *hw = &pf->hw;
633 	struct i40e_eth_stats *oes;
634 	struct i40e_eth_stats *es;     /* device's eth stats */
635 
636 	es = &vsi->eth_stats;
637 	oes = &vsi->eth_stats_offsets;
638 
639 	/* Gather up the stats that the hw collects */
640 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
641 			   vsi->stat_offsets_loaded,
642 			   &oes->tx_errors, &es->tx_errors);
643 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
644 			   vsi->stat_offsets_loaded,
645 			   &oes->rx_discards, &es->rx_discards);
646 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
647 			   vsi->stat_offsets_loaded,
648 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
649 
650 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
651 			   I40E_GLV_GORCL(stat_idx),
652 			   vsi->stat_offsets_loaded,
653 			   &oes->rx_bytes, &es->rx_bytes);
654 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
655 			   I40E_GLV_UPRCL(stat_idx),
656 			   vsi->stat_offsets_loaded,
657 			   &oes->rx_unicast, &es->rx_unicast);
658 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
659 			   I40E_GLV_MPRCL(stat_idx),
660 			   vsi->stat_offsets_loaded,
661 			   &oes->rx_multicast, &es->rx_multicast);
662 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
663 			   I40E_GLV_BPRCL(stat_idx),
664 			   vsi->stat_offsets_loaded,
665 			   &oes->rx_broadcast, &es->rx_broadcast);
666 
667 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
668 			   I40E_GLV_GOTCL(stat_idx),
669 			   vsi->stat_offsets_loaded,
670 			   &oes->tx_bytes, &es->tx_bytes);
671 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
672 			   I40E_GLV_UPTCL(stat_idx),
673 			   vsi->stat_offsets_loaded,
674 			   &oes->tx_unicast, &es->tx_unicast);
675 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
676 			   I40E_GLV_MPTCL(stat_idx),
677 			   vsi->stat_offsets_loaded,
678 			   &oes->tx_multicast, &es->tx_multicast);
679 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
680 			   I40E_GLV_BPTCL(stat_idx),
681 			   vsi->stat_offsets_loaded,
682 			   &oes->tx_broadcast, &es->tx_broadcast);
683 	vsi->stat_offsets_loaded = true;
684 }
685 
686 /**
687  * i40e_update_veb_stats - Update Switch component statistics
688  * @veb: the VEB being updated
689  **/
690 void i40e_update_veb_stats(struct i40e_veb *veb)
691 {
692 	struct i40e_pf *pf = veb->pf;
693 	struct i40e_hw *hw = &pf->hw;
694 	struct i40e_eth_stats *oes;
695 	struct i40e_eth_stats *es;     /* device's eth stats */
696 	struct i40e_veb_tc_stats *veb_oes;
697 	struct i40e_veb_tc_stats *veb_es;
698 	int i, idx = 0;
699 
700 	idx = veb->stats_idx;
701 	es = &veb->stats;
702 	oes = &veb->stats_offsets;
703 	veb_es = &veb->tc_stats;
704 	veb_oes = &veb->tc_stats_offsets;
705 
706 	/* Gather up the stats that the hw collects */
707 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
708 			   veb->stat_offsets_loaded,
709 			   &oes->tx_discards, &es->tx_discards);
710 	if (hw->revision_id > 0)
711 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
712 				   veb->stat_offsets_loaded,
713 				   &oes->rx_unknown_protocol,
714 				   &es->rx_unknown_protocol);
715 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
716 			   veb->stat_offsets_loaded,
717 			   &oes->rx_bytes, &es->rx_bytes);
718 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
719 			   veb->stat_offsets_loaded,
720 			   &oes->rx_unicast, &es->rx_unicast);
721 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
722 			   veb->stat_offsets_loaded,
723 			   &oes->rx_multicast, &es->rx_multicast);
724 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
725 			   veb->stat_offsets_loaded,
726 			   &oes->rx_broadcast, &es->rx_broadcast);
727 
728 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
729 			   veb->stat_offsets_loaded,
730 			   &oes->tx_bytes, &es->tx_bytes);
731 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
732 			   veb->stat_offsets_loaded,
733 			   &oes->tx_unicast, &es->tx_unicast);
734 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
735 			   veb->stat_offsets_loaded,
736 			   &oes->tx_multicast, &es->tx_multicast);
737 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
738 			   veb->stat_offsets_loaded,
739 			   &oes->tx_broadcast, &es->tx_broadcast);
740 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
741 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
742 				   I40E_GLVEBTC_RPCL(i, idx),
743 				   veb->stat_offsets_loaded,
744 				   &veb_oes->tc_rx_packets[i],
745 				   &veb_es->tc_rx_packets[i]);
746 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
747 				   I40E_GLVEBTC_RBCL(i, idx),
748 				   veb->stat_offsets_loaded,
749 				   &veb_oes->tc_rx_bytes[i],
750 				   &veb_es->tc_rx_bytes[i]);
751 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
752 				   I40E_GLVEBTC_TPCL(i, idx),
753 				   veb->stat_offsets_loaded,
754 				   &veb_oes->tc_tx_packets[i],
755 				   &veb_es->tc_tx_packets[i]);
756 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
757 				   I40E_GLVEBTC_TBCL(i, idx),
758 				   veb->stat_offsets_loaded,
759 				   &veb_oes->tc_tx_bytes[i],
760 				   &veb_es->tc_tx_bytes[i]);
761 	}
762 	veb->stat_offsets_loaded = true;
763 }
764 
765 /**
766  * i40e_update_vsi_stats - Update the vsi statistics counters.
767  * @vsi: the VSI to be updated
768  *
769  * There are a few instances where we store the same stat in a
770  * couple of different structs.  This is partly because we have
771  * the netdev stats that need to be filled out, which is slightly
772  * different from the "eth_stats" defined by the chip and used in
773  * VF communications.  We sort it out here.
774  **/
775 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
776 {
777 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
778 	struct i40e_pf *pf = vsi->back;
779 	struct rtnl_link_stats64 *ons;
780 	struct rtnl_link_stats64 *ns;   /* netdev stats */
781 	struct i40e_eth_stats *oes;
782 	struct i40e_eth_stats *es;     /* device's eth stats */
783 	u64 tx_restart, tx_busy;
784 	struct i40e_ring *p;
785 	u64 bytes, packets;
786 	unsigned int start;
787 	u64 tx_linearize;
788 	u64 tx_force_wb;
789 	u64 tx_stopped;
790 	u64 rx_p, rx_b;
791 	u64 tx_p, tx_b;
792 	u16 q;
793 
794 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
795 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
796 		return;
797 
798 	ns = i40e_get_vsi_stats_struct(vsi);
799 	ons = &vsi->net_stats_offsets;
800 	es = &vsi->eth_stats;
801 	oes = &vsi->eth_stats_offsets;
802 
803 	/* Gather up the netdev and vsi stats that the driver collects
804 	 * on the fly during packet processing
805 	 */
806 	rx_b = rx_p = 0;
807 	tx_b = tx_p = 0;
808 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
809 	tx_stopped = 0;
810 	rx_page = 0;
811 	rx_buf = 0;
812 	rx_reuse = 0;
813 	rx_alloc = 0;
814 	rx_waive = 0;
815 	rx_busy = 0;
816 	rcu_read_lock();
817 	for (q = 0; q < vsi->num_queue_pairs; q++) {
818 		/* locate Tx ring */
819 		p = READ_ONCE(vsi->tx_rings[q]);
820 		if (!p)
821 			continue;
822 
823 		do {
824 			start = u64_stats_fetch_begin_irq(&p->syncp);
825 			packets = p->stats.packets;
826 			bytes = p->stats.bytes;
827 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
828 		tx_b += bytes;
829 		tx_p += packets;
830 		tx_restart += p->tx_stats.restart_queue;
831 		tx_busy += p->tx_stats.tx_busy;
832 		tx_linearize += p->tx_stats.tx_linearize;
833 		tx_force_wb += p->tx_stats.tx_force_wb;
834 		tx_stopped += p->tx_stats.tx_stopped;
835 
836 		/* locate Rx ring */
837 		p = READ_ONCE(vsi->rx_rings[q]);
838 		if (!p)
839 			continue;
840 
841 		do {
842 			start = u64_stats_fetch_begin_irq(&p->syncp);
843 			packets = p->stats.packets;
844 			bytes = p->stats.bytes;
845 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
846 		rx_b += bytes;
847 		rx_p += packets;
848 		rx_buf += p->rx_stats.alloc_buff_failed;
849 		rx_page += p->rx_stats.alloc_page_failed;
850 		rx_reuse += p->rx_stats.page_reuse_count;
851 		rx_alloc += p->rx_stats.page_alloc_count;
852 		rx_waive += p->rx_stats.page_waive_count;
853 		rx_busy += p->rx_stats.page_busy_count;
854 
855 		if (i40e_enabled_xdp_vsi(vsi)) {
856 			/* locate XDP ring */
857 			p = READ_ONCE(vsi->xdp_rings[q]);
858 			if (!p)
859 				continue;
860 
861 			do {
862 				start = u64_stats_fetch_begin_irq(&p->syncp);
863 				packets = p->stats.packets;
864 				bytes = p->stats.bytes;
865 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
866 			tx_b += bytes;
867 			tx_p += packets;
868 			tx_restart += p->tx_stats.restart_queue;
869 			tx_busy += p->tx_stats.tx_busy;
870 			tx_linearize += p->tx_stats.tx_linearize;
871 			tx_force_wb += p->tx_stats.tx_force_wb;
872 		}
873 	}
874 	rcu_read_unlock();
875 	vsi->tx_restart = tx_restart;
876 	vsi->tx_busy = tx_busy;
877 	vsi->tx_linearize = tx_linearize;
878 	vsi->tx_force_wb = tx_force_wb;
879 	vsi->tx_stopped = tx_stopped;
880 	vsi->rx_page_failed = rx_page;
881 	vsi->rx_buf_failed = rx_buf;
882 	vsi->rx_page_reuse = rx_reuse;
883 	vsi->rx_page_alloc = rx_alloc;
884 	vsi->rx_page_waive = rx_waive;
885 	vsi->rx_page_busy = rx_busy;
886 
887 	ns->rx_packets = rx_p;
888 	ns->rx_bytes = rx_b;
889 	ns->tx_packets = tx_p;
890 	ns->tx_bytes = tx_b;
891 
892 	/* update netdev stats from eth stats */
893 	i40e_update_eth_stats(vsi);
894 	ons->tx_errors = oes->tx_errors;
895 	ns->tx_errors = es->tx_errors;
896 	ons->multicast = oes->rx_multicast;
897 	ns->multicast = es->rx_multicast;
898 	ons->rx_dropped = oes->rx_discards;
899 	ns->rx_dropped = es->rx_discards;
900 	ons->tx_dropped = oes->tx_discards;
901 	ns->tx_dropped = es->tx_discards;
902 
903 	/* pull in a couple PF stats if this is the main vsi */
904 	if (vsi == pf->vsi[pf->lan_vsi]) {
905 		ns->rx_crc_errors = pf->stats.crc_errors;
906 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
907 		ns->rx_length_errors = pf->stats.rx_length_errors;
908 	}
909 }
910 
911 /**
912  * i40e_update_pf_stats - Update the PF statistics counters.
913  * @pf: the PF to be updated
914  **/
915 static void i40e_update_pf_stats(struct i40e_pf *pf)
916 {
917 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
918 	struct i40e_hw_port_stats *nsd = &pf->stats;
919 	struct i40e_hw *hw = &pf->hw;
920 	u32 val;
921 	int i;
922 
923 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
924 			   I40E_GLPRT_GORCL(hw->port),
925 			   pf->stat_offsets_loaded,
926 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
927 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
928 			   I40E_GLPRT_GOTCL(hw->port),
929 			   pf->stat_offsets_loaded,
930 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
931 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
932 			   pf->stat_offsets_loaded,
933 			   &osd->eth.rx_discards,
934 			   &nsd->eth.rx_discards);
935 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
936 			   I40E_GLPRT_UPRCL(hw->port),
937 			   pf->stat_offsets_loaded,
938 			   &osd->eth.rx_unicast,
939 			   &nsd->eth.rx_unicast);
940 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
941 			   I40E_GLPRT_MPRCL(hw->port),
942 			   pf->stat_offsets_loaded,
943 			   &osd->eth.rx_multicast,
944 			   &nsd->eth.rx_multicast);
945 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
946 			   I40E_GLPRT_BPRCL(hw->port),
947 			   pf->stat_offsets_loaded,
948 			   &osd->eth.rx_broadcast,
949 			   &nsd->eth.rx_broadcast);
950 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
951 			   I40E_GLPRT_UPTCL(hw->port),
952 			   pf->stat_offsets_loaded,
953 			   &osd->eth.tx_unicast,
954 			   &nsd->eth.tx_unicast);
955 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
956 			   I40E_GLPRT_MPTCL(hw->port),
957 			   pf->stat_offsets_loaded,
958 			   &osd->eth.tx_multicast,
959 			   &nsd->eth.tx_multicast);
960 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
961 			   I40E_GLPRT_BPTCL(hw->port),
962 			   pf->stat_offsets_loaded,
963 			   &osd->eth.tx_broadcast,
964 			   &nsd->eth.tx_broadcast);
965 
966 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
967 			   pf->stat_offsets_loaded,
968 			   &osd->tx_dropped_link_down,
969 			   &nsd->tx_dropped_link_down);
970 
971 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
972 			   pf->stat_offsets_loaded,
973 			   &osd->crc_errors, &nsd->crc_errors);
974 
975 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
976 			   pf->stat_offsets_loaded,
977 			   &osd->illegal_bytes, &nsd->illegal_bytes);
978 
979 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
980 			   pf->stat_offsets_loaded,
981 			   &osd->mac_local_faults,
982 			   &nsd->mac_local_faults);
983 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
984 			   pf->stat_offsets_loaded,
985 			   &osd->mac_remote_faults,
986 			   &nsd->mac_remote_faults);
987 
988 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
989 			   pf->stat_offsets_loaded,
990 			   &osd->rx_length_errors,
991 			   &nsd->rx_length_errors);
992 
993 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
994 			   pf->stat_offsets_loaded,
995 			   &osd->link_xon_rx, &nsd->link_xon_rx);
996 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
997 			   pf->stat_offsets_loaded,
998 			   &osd->link_xon_tx, &nsd->link_xon_tx);
999 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1000 			   pf->stat_offsets_loaded,
1001 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1002 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1003 			   pf->stat_offsets_loaded,
1004 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1005 
1006 	for (i = 0; i < 8; i++) {
1007 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1008 				   pf->stat_offsets_loaded,
1009 				   &osd->priority_xoff_rx[i],
1010 				   &nsd->priority_xoff_rx[i]);
1011 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1012 				   pf->stat_offsets_loaded,
1013 				   &osd->priority_xon_rx[i],
1014 				   &nsd->priority_xon_rx[i]);
1015 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1016 				   pf->stat_offsets_loaded,
1017 				   &osd->priority_xon_tx[i],
1018 				   &nsd->priority_xon_tx[i]);
1019 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1020 				   pf->stat_offsets_loaded,
1021 				   &osd->priority_xoff_tx[i],
1022 				   &nsd->priority_xoff_tx[i]);
1023 		i40e_stat_update32(hw,
1024 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1025 				   pf->stat_offsets_loaded,
1026 				   &osd->priority_xon_2_xoff[i],
1027 				   &nsd->priority_xon_2_xoff[i]);
1028 	}
1029 
1030 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1031 			   I40E_GLPRT_PRC64L(hw->port),
1032 			   pf->stat_offsets_loaded,
1033 			   &osd->rx_size_64, &nsd->rx_size_64);
1034 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1035 			   I40E_GLPRT_PRC127L(hw->port),
1036 			   pf->stat_offsets_loaded,
1037 			   &osd->rx_size_127, &nsd->rx_size_127);
1038 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1039 			   I40E_GLPRT_PRC255L(hw->port),
1040 			   pf->stat_offsets_loaded,
1041 			   &osd->rx_size_255, &nsd->rx_size_255);
1042 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1043 			   I40E_GLPRT_PRC511L(hw->port),
1044 			   pf->stat_offsets_loaded,
1045 			   &osd->rx_size_511, &nsd->rx_size_511);
1046 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1047 			   I40E_GLPRT_PRC1023L(hw->port),
1048 			   pf->stat_offsets_loaded,
1049 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1050 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1051 			   I40E_GLPRT_PRC1522L(hw->port),
1052 			   pf->stat_offsets_loaded,
1053 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1054 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1055 			   I40E_GLPRT_PRC9522L(hw->port),
1056 			   pf->stat_offsets_loaded,
1057 			   &osd->rx_size_big, &nsd->rx_size_big);
1058 
1059 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1060 			   I40E_GLPRT_PTC64L(hw->port),
1061 			   pf->stat_offsets_loaded,
1062 			   &osd->tx_size_64, &nsd->tx_size_64);
1063 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1064 			   I40E_GLPRT_PTC127L(hw->port),
1065 			   pf->stat_offsets_loaded,
1066 			   &osd->tx_size_127, &nsd->tx_size_127);
1067 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1068 			   I40E_GLPRT_PTC255L(hw->port),
1069 			   pf->stat_offsets_loaded,
1070 			   &osd->tx_size_255, &nsd->tx_size_255);
1071 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1072 			   I40E_GLPRT_PTC511L(hw->port),
1073 			   pf->stat_offsets_loaded,
1074 			   &osd->tx_size_511, &nsd->tx_size_511);
1075 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1076 			   I40E_GLPRT_PTC1023L(hw->port),
1077 			   pf->stat_offsets_loaded,
1078 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1079 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1080 			   I40E_GLPRT_PTC1522L(hw->port),
1081 			   pf->stat_offsets_loaded,
1082 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1083 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1084 			   I40E_GLPRT_PTC9522L(hw->port),
1085 			   pf->stat_offsets_loaded,
1086 			   &osd->tx_size_big, &nsd->tx_size_big);
1087 
1088 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1089 			   pf->stat_offsets_loaded,
1090 			   &osd->rx_undersize, &nsd->rx_undersize);
1091 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1092 			   pf->stat_offsets_loaded,
1093 			   &osd->rx_fragments, &nsd->rx_fragments);
1094 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1095 			   pf->stat_offsets_loaded,
1096 			   &osd->rx_oversize, &nsd->rx_oversize);
1097 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1098 			   pf->stat_offsets_loaded,
1099 			   &osd->rx_jabber, &nsd->rx_jabber);
1100 
1101 	/* FDIR stats */
1102 	i40e_stat_update_and_clear32(hw,
1103 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1104 			&nsd->fd_atr_match);
1105 	i40e_stat_update_and_clear32(hw,
1106 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1107 			&nsd->fd_sb_match);
1108 	i40e_stat_update_and_clear32(hw,
1109 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1110 			&nsd->fd_atr_tunnel_match);
1111 
1112 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1113 	nsd->tx_lpi_status =
1114 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1115 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1116 	nsd->rx_lpi_status =
1117 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1118 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1119 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1120 			   pf->stat_offsets_loaded,
1121 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1122 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1123 			   pf->stat_offsets_loaded,
1124 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1125 
1126 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1127 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1128 		nsd->fd_sb_status = true;
1129 	else
1130 		nsd->fd_sb_status = false;
1131 
1132 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1133 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1134 		nsd->fd_atr_status = true;
1135 	else
1136 		nsd->fd_atr_status = false;
1137 
1138 	pf->stat_offsets_loaded = true;
1139 }
1140 
1141 /**
1142  * i40e_update_stats - Update the various statistics counters.
1143  * @vsi: the VSI to be updated
1144  *
1145  * Update the various stats for this VSI and its related entities.
1146  **/
1147 void i40e_update_stats(struct i40e_vsi *vsi)
1148 {
1149 	struct i40e_pf *pf = vsi->back;
1150 
1151 	if (vsi == pf->vsi[pf->lan_vsi])
1152 		i40e_update_pf_stats(pf);
1153 
1154 	i40e_update_vsi_stats(vsi);
1155 }
1156 
1157 /**
1158  * i40e_count_filters - counts VSI mac filters
1159  * @vsi: the VSI to be searched
1160  *
1161  * Returns count of mac filters
1162  **/
1163 int i40e_count_filters(struct i40e_vsi *vsi)
1164 {
1165 	struct i40e_mac_filter *f;
1166 	struct hlist_node *h;
1167 	int bkt;
1168 	int cnt = 0;
1169 
1170 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1171 		++cnt;
1172 
1173 	return cnt;
1174 }
1175 
1176 /**
1177  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1178  * @vsi: the VSI to be searched
1179  * @macaddr: the MAC address
1180  * @vlan: the vlan
1181  *
1182  * Returns ptr to the filter object or NULL
1183  **/
1184 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1185 						const u8 *macaddr, s16 vlan)
1186 {
1187 	struct i40e_mac_filter *f;
1188 	u64 key;
1189 
1190 	if (!vsi || !macaddr)
1191 		return NULL;
1192 
1193 	key = i40e_addr_to_hkey(macaddr);
1194 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1195 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1196 		    (vlan == f->vlan))
1197 			return f;
1198 	}
1199 	return NULL;
1200 }
1201 
1202 /**
1203  * i40e_find_mac - Find a mac addr in the macvlan filters list
1204  * @vsi: the VSI to be searched
1205  * @macaddr: the MAC address we are searching for
1206  *
1207  * Returns the first filter with the provided MAC address or NULL if
1208  * MAC address was not found
1209  **/
1210 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1211 {
1212 	struct i40e_mac_filter *f;
1213 	u64 key;
1214 
1215 	if (!vsi || !macaddr)
1216 		return NULL;
1217 
1218 	key = i40e_addr_to_hkey(macaddr);
1219 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1220 		if ((ether_addr_equal(macaddr, f->macaddr)))
1221 			return f;
1222 	}
1223 	return NULL;
1224 }
1225 
1226 /**
1227  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1228  * @vsi: the VSI to be searched
1229  *
1230  * Returns true if VSI is in vlan mode or false otherwise
1231  **/
1232 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1233 {
1234 	/* If we have a PVID, always operate in VLAN mode */
1235 	if (vsi->info.pvid)
1236 		return true;
1237 
1238 	/* We need to operate in VLAN mode whenever we have any filters with
1239 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1240 	 * time, incurring search cost repeatedly. However, we can notice two
1241 	 * things:
1242 	 *
1243 	 * 1) the only place where we can gain a VLAN filter is in
1244 	 *    i40e_add_filter.
1245 	 *
1246 	 * 2) the only place where filters are actually removed is in
1247 	 *    i40e_sync_filters_subtask.
1248 	 *
1249 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1250 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1251 	 * we have to perform the full search after deleting filters in
1252 	 * i40e_sync_filters_subtask, but we already have to search
1253 	 * filters here and can perform the check at the same time. This
1254 	 * results in avoiding embedding a loop for VLAN mode inside another
1255 	 * loop over all the filters, and should maintain correctness as noted
1256 	 * above.
1257 	 */
1258 	return vsi->has_vlan_filter;
1259 }
1260 
1261 /**
1262  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1263  * @vsi: the VSI to configure
1264  * @tmp_add_list: list of filters ready to be added
1265  * @tmp_del_list: list of filters ready to be deleted
1266  * @vlan_filters: the number of active VLAN filters
1267  *
1268  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1269  * behave as expected. If we have any active VLAN filters remaining or about
1270  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1271  * so that they only match against untagged traffic. If we no longer have any
1272  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1273  * so that they match against both tagged and untagged traffic. In this way,
1274  * we ensure that we correctly receive the desired traffic. This ensures that
1275  * when we have an active VLAN we will receive only untagged traffic and
1276  * traffic matching active VLANs. If we have no active VLANs then we will
1277  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1278  *
1279  * Finally, in a similar fashion, this function also corrects filters when
1280  * there is an active PVID assigned to this VSI.
1281  *
1282  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1283  *
1284  * This function is only expected to be called from within
1285  * i40e_sync_vsi_filters.
1286  *
1287  * NOTE: This function expects to be called while under the
1288  * mac_filter_hash_lock
1289  */
1290 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1291 					 struct hlist_head *tmp_add_list,
1292 					 struct hlist_head *tmp_del_list,
1293 					 int vlan_filters)
1294 {
1295 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1296 	struct i40e_mac_filter *f, *add_head;
1297 	struct i40e_new_mac_filter *new;
1298 	struct hlist_node *h;
1299 	int bkt, new_vlan;
1300 
1301 	/* To determine if a particular filter needs to be replaced we
1302 	 * have the three following conditions:
1303 	 *
1304 	 * a) if we have a PVID assigned, then all filters which are
1305 	 *    not marked as VLAN=PVID must be replaced with filters that
1306 	 *    are.
1307 	 * b) otherwise, if we have any active VLANS, all filters
1308 	 *    which are marked as VLAN=-1 must be replaced with
1309 	 *    filters marked as VLAN=0
1310 	 * c) finally, if we do not have any active VLANS, all filters
1311 	 *    which are marked as VLAN=0 must be replaced with filters
1312 	 *    marked as VLAN=-1
1313 	 */
1314 
1315 	/* Update the filters about to be added in place */
1316 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1317 		if (pvid && new->f->vlan != pvid)
1318 			new->f->vlan = pvid;
1319 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1320 			new->f->vlan = 0;
1321 		else if (!vlan_filters && new->f->vlan == 0)
1322 			new->f->vlan = I40E_VLAN_ANY;
1323 	}
1324 
1325 	/* Update the remaining active filters */
1326 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1327 		/* Combine the checks for whether a filter needs to be changed
1328 		 * and then determine the new VLAN inside the if block, in
1329 		 * order to avoid duplicating code for adding the new filter
1330 		 * then deleting the old filter.
1331 		 */
1332 		if ((pvid && f->vlan != pvid) ||
1333 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1334 		    (!vlan_filters && f->vlan == 0)) {
1335 			/* Determine the new vlan we will be adding */
1336 			if (pvid)
1337 				new_vlan = pvid;
1338 			else if (vlan_filters)
1339 				new_vlan = 0;
1340 			else
1341 				new_vlan = I40E_VLAN_ANY;
1342 
1343 			/* Create the new filter */
1344 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1345 			if (!add_head)
1346 				return -ENOMEM;
1347 
1348 			/* Create a temporary i40e_new_mac_filter */
1349 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1350 			if (!new)
1351 				return -ENOMEM;
1352 
1353 			new->f = add_head;
1354 			new->state = add_head->state;
1355 
1356 			/* Add the new filter to the tmp list */
1357 			hlist_add_head(&new->hlist, tmp_add_list);
1358 
1359 			/* Put the original filter into the delete list */
1360 			f->state = I40E_FILTER_REMOVE;
1361 			hash_del(&f->hlist);
1362 			hlist_add_head(&f->hlist, tmp_del_list);
1363 		}
1364 	}
1365 
1366 	vsi->has_vlan_filter = !!vlan_filters;
1367 
1368 	return 0;
1369 }
1370 
1371 /**
1372  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1373  * @vsi: the vsi to configure
1374  * @new_mac: new mac filter to be added
1375  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1376  * @vlan_filters: the number of active VLAN filters
1377  * @trusted: flag if the VF is trusted
1378  *
1379  * Get new VLAN id based on current VLAN filters, trust, PVID
1380  * and vf-vlan-prune-disable flag.
1381  *
1382  * Returns the value of the new vlan filter or
1383  * the old value if no new filter is needed.
1384  */
1385 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1386 				struct i40e_new_mac_filter *new_mac,
1387 				struct i40e_mac_filter *f,
1388 				int vlan_filters,
1389 				bool trusted)
1390 {
1391 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1392 	struct i40e_pf *pf = vsi->back;
1393 	bool is_any;
1394 
1395 	if (new_mac)
1396 		f = new_mac->f;
1397 
1398 	if (pvid && f->vlan != pvid)
1399 		return pvid;
1400 
1401 	is_any = (trusted ||
1402 		  !(pf->flags & I40E_FLAG_VF_VLAN_PRUNING));
1403 
1404 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1405 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1406 	    (is_any && !vlan_filters && f->vlan == 0)) {
1407 		if (is_any)
1408 			return I40E_VLAN_ANY;
1409 		else
1410 			return 0;
1411 	}
1412 
1413 	return f->vlan;
1414 }
1415 
1416 /**
1417  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1418  * @vsi: the vsi to configure
1419  * @tmp_add_list: list of filters ready to be added
1420  * @tmp_del_list: list of filters ready to be deleted
1421  * @vlan_filters: the number of active VLAN filters
1422  * @trusted: flag if the VF is trusted
1423  *
1424  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1425  * and vf-vlan-prune-disable flag.
1426  *
1427  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1428  *
1429  * This function is only expected to be called from within
1430  * i40e_sync_vsi_filters.
1431  *
1432  * NOTE: This function expects to be called while under the
1433  * mac_filter_hash_lock
1434  */
1435 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1436 					    struct hlist_head *tmp_add_list,
1437 					    struct hlist_head *tmp_del_list,
1438 					    int vlan_filters,
1439 					    bool trusted)
1440 {
1441 	struct i40e_mac_filter *f, *add_head;
1442 	struct i40e_new_mac_filter *new_mac;
1443 	struct hlist_node *h;
1444 	int bkt, new_vlan;
1445 
1446 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1447 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1448 							vlan_filters, trusted);
1449 	}
1450 
1451 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1452 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1453 						trusted);
1454 		if (new_vlan != f->vlan) {
1455 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1456 			if (!add_head)
1457 				return -ENOMEM;
1458 			/* Create a temporary i40e_new_mac_filter */
1459 			new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1460 			if (!new_mac)
1461 				return -ENOMEM;
1462 			new_mac->f = add_head;
1463 			new_mac->state = add_head->state;
1464 
1465 			/* Add the new filter to the tmp list */
1466 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1467 
1468 			/* Put the original filter into the delete list */
1469 			f->state = I40E_FILTER_REMOVE;
1470 			hash_del(&f->hlist);
1471 			hlist_add_head(&f->hlist, tmp_del_list);
1472 		}
1473 	}
1474 
1475 	vsi->has_vlan_filter = !!vlan_filters;
1476 	return 0;
1477 }
1478 
1479 /**
1480  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1481  * @vsi: the PF Main VSI - inappropriate for any other VSI
1482  * @macaddr: the MAC address
1483  *
1484  * Remove whatever filter the firmware set up so the driver can manage
1485  * its own filtering intelligently.
1486  **/
1487 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1488 {
1489 	struct i40e_aqc_remove_macvlan_element_data element;
1490 	struct i40e_pf *pf = vsi->back;
1491 
1492 	/* Only appropriate for the PF main VSI */
1493 	if (vsi->type != I40E_VSI_MAIN)
1494 		return;
1495 
1496 	memset(&element, 0, sizeof(element));
1497 	ether_addr_copy(element.mac_addr, macaddr);
1498 	element.vlan_tag = 0;
1499 	/* Ignore error returns, some firmware does it this way... */
1500 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1501 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1502 
1503 	memset(&element, 0, sizeof(element));
1504 	ether_addr_copy(element.mac_addr, macaddr);
1505 	element.vlan_tag = 0;
1506 	/* ...and some firmware does it this way. */
1507 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1508 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1509 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1510 }
1511 
1512 /**
1513  * i40e_add_filter - Add a mac/vlan filter to the VSI
1514  * @vsi: the VSI to be searched
1515  * @macaddr: the MAC address
1516  * @vlan: the vlan
1517  *
1518  * Returns ptr to the filter object or NULL when no memory available.
1519  *
1520  * NOTE: This function is expected to be called with mac_filter_hash_lock
1521  * being held.
1522  **/
1523 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1524 					const u8 *macaddr, s16 vlan)
1525 {
1526 	struct i40e_mac_filter *f;
1527 	u64 key;
1528 
1529 	if (!vsi || !macaddr)
1530 		return NULL;
1531 
1532 	f = i40e_find_filter(vsi, macaddr, vlan);
1533 	if (!f) {
1534 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1535 		if (!f)
1536 			return NULL;
1537 
1538 		/* Update the boolean indicating if we need to function in
1539 		 * VLAN mode.
1540 		 */
1541 		if (vlan >= 0)
1542 			vsi->has_vlan_filter = true;
1543 
1544 		ether_addr_copy(f->macaddr, macaddr);
1545 		f->vlan = vlan;
1546 		f->state = I40E_FILTER_NEW;
1547 		INIT_HLIST_NODE(&f->hlist);
1548 
1549 		key = i40e_addr_to_hkey(macaddr);
1550 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1551 
1552 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1553 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1554 	}
1555 
1556 	/* If we're asked to add a filter that has been marked for removal, it
1557 	 * is safe to simply restore it to active state. __i40e_del_filter
1558 	 * will have simply deleted any filters which were previously marked
1559 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1560 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1561 	 * task, just restore this filter to the ACTIVE state so that the
1562 	 * sync task leaves it in place
1563 	 */
1564 	if (f->state == I40E_FILTER_REMOVE)
1565 		f->state = I40E_FILTER_ACTIVE;
1566 
1567 	return f;
1568 }
1569 
1570 /**
1571  * __i40e_del_filter - Remove a specific filter from the VSI
1572  * @vsi: VSI to remove from
1573  * @f: the filter to remove from the list
1574  *
1575  * This function should be called instead of i40e_del_filter only if you know
1576  * the exact filter you will remove already, such as via i40e_find_filter or
1577  * i40e_find_mac.
1578  *
1579  * NOTE: This function is expected to be called with mac_filter_hash_lock
1580  * being held.
1581  * ANOTHER NOTE: This function MUST be called from within the context of
1582  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1583  * instead of list_for_each_entry().
1584  **/
1585 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1586 {
1587 	if (!f)
1588 		return;
1589 
1590 	/* If the filter was never added to firmware then we can just delete it
1591 	 * directly and we don't want to set the status to remove or else an
1592 	 * admin queue command will unnecessarily fire.
1593 	 */
1594 	if ((f->state == I40E_FILTER_FAILED) ||
1595 	    (f->state == I40E_FILTER_NEW)) {
1596 		hash_del(&f->hlist);
1597 		kfree(f);
1598 	} else {
1599 		f->state = I40E_FILTER_REMOVE;
1600 	}
1601 
1602 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1603 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1604 }
1605 
1606 /**
1607  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1608  * @vsi: the VSI to be searched
1609  * @macaddr: the MAC address
1610  * @vlan: the VLAN
1611  *
1612  * NOTE: This function is expected to be called with mac_filter_hash_lock
1613  * being held.
1614  * ANOTHER NOTE: This function MUST be called from within the context of
1615  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1616  * instead of list_for_each_entry().
1617  **/
1618 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1619 {
1620 	struct i40e_mac_filter *f;
1621 
1622 	if (!vsi || !macaddr)
1623 		return;
1624 
1625 	f = i40e_find_filter(vsi, macaddr, vlan);
1626 	__i40e_del_filter(vsi, f);
1627 }
1628 
1629 /**
1630  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1631  * @vsi: the VSI to be searched
1632  * @macaddr: the mac address to be filtered
1633  *
1634  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1635  * go through all the macvlan filters and add a macvlan filter for each
1636  * unique vlan that already exists. If a PVID has been assigned, instead only
1637  * add the macaddr to that VLAN.
1638  *
1639  * Returns last filter added on success, else NULL
1640  **/
1641 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1642 					    const u8 *macaddr)
1643 {
1644 	struct i40e_mac_filter *f, *add = NULL;
1645 	struct hlist_node *h;
1646 	int bkt;
1647 
1648 	if (vsi->info.pvid)
1649 		return i40e_add_filter(vsi, macaddr,
1650 				       le16_to_cpu(vsi->info.pvid));
1651 
1652 	if (!i40e_is_vsi_in_vlan(vsi))
1653 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1654 
1655 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1656 		if (f->state == I40E_FILTER_REMOVE)
1657 			continue;
1658 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1659 		if (!add)
1660 			return NULL;
1661 	}
1662 
1663 	return add;
1664 }
1665 
1666 /**
1667  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1668  * @vsi: the VSI to be searched
1669  * @macaddr: the mac address to be removed
1670  *
1671  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1672  * associated with.
1673  *
1674  * Returns 0 for success, or error
1675  **/
1676 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1677 {
1678 	struct i40e_mac_filter *f;
1679 	struct hlist_node *h;
1680 	bool found = false;
1681 	int bkt;
1682 
1683 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1684 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1685 		if (ether_addr_equal(macaddr, f->macaddr)) {
1686 			__i40e_del_filter(vsi, f);
1687 			found = true;
1688 		}
1689 	}
1690 
1691 	if (found)
1692 		return 0;
1693 	else
1694 		return -ENOENT;
1695 }
1696 
1697 /**
1698  * i40e_set_mac - NDO callback to set mac address
1699  * @netdev: network interface device structure
1700  * @p: pointer to an address structure
1701  *
1702  * Returns 0 on success, negative on failure
1703  **/
1704 static int i40e_set_mac(struct net_device *netdev, void *p)
1705 {
1706 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1707 	struct i40e_vsi *vsi = np->vsi;
1708 	struct i40e_pf *pf = vsi->back;
1709 	struct i40e_hw *hw = &pf->hw;
1710 	struct sockaddr *addr = p;
1711 
1712 	if (!is_valid_ether_addr(addr->sa_data))
1713 		return -EADDRNOTAVAIL;
1714 
1715 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1716 		netdev_info(netdev, "already using mac address %pM\n",
1717 			    addr->sa_data);
1718 		return 0;
1719 	}
1720 
1721 	if (test_bit(__I40E_DOWN, pf->state) ||
1722 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1723 		return -EADDRNOTAVAIL;
1724 
1725 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1726 		netdev_info(netdev, "returning to hw mac address %pM\n",
1727 			    hw->mac.addr);
1728 	else
1729 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1730 
1731 	/* Copy the address first, so that we avoid a possible race with
1732 	 * .set_rx_mode().
1733 	 * - Remove old address from MAC filter
1734 	 * - Copy new address
1735 	 * - Add new address to MAC filter
1736 	 */
1737 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1738 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1739 	eth_hw_addr_set(netdev, addr->sa_data);
1740 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1741 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1742 
1743 	if (vsi->type == I40E_VSI_MAIN) {
1744 		i40e_status ret;
1745 
1746 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1747 						addr->sa_data, NULL);
1748 		if (ret)
1749 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1750 				    i40e_stat_str(hw, ret),
1751 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1752 	}
1753 
1754 	/* schedule our worker thread which will take care of
1755 	 * applying the new filter changes
1756 	 */
1757 	i40e_service_event_schedule(pf);
1758 	return 0;
1759 }
1760 
1761 /**
1762  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1763  * @vsi: vsi structure
1764  * @seed: RSS hash seed
1765  * @lut: pointer to lookup table of lut_size
1766  * @lut_size: size of the lookup table
1767  **/
1768 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1769 			      u8 *lut, u16 lut_size)
1770 {
1771 	struct i40e_pf *pf = vsi->back;
1772 	struct i40e_hw *hw = &pf->hw;
1773 	int ret = 0;
1774 
1775 	if (seed) {
1776 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1777 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1778 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1779 		if (ret) {
1780 			dev_info(&pf->pdev->dev,
1781 				 "Cannot set RSS key, err %s aq_err %s\n",
1782 				 i40e_stat_str(hw, ret),
1783 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1784 			return ret;
1785 		}
1786 	}
1787 	if (lut) {
1788 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1789 
1790 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1791 		if (ret) {
1792 			dev_info(&pf->pdev->dev,
1793 				 "Cannot set RSS lut, err %s aq_err %s\n",
1794 				 i40e_stat_str(hw, ret),
1795 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1796 			return ret;
1797 		}
1798 	}
1799 	return ret;
1800 }
1801 
1802 /**
1803  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1804  * @vsi: VSI structure
1805  **/
1806 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1807 {
1808 	struct i40e_pf *pf = vsi->back;
1809 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1810 	u8 *lut;
1811 	int ret;
1812 
1813 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1814 		return 0;
1815 	if (!vsi->rss_size)
1816 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1817 				      vsi->num_queue_pairs);
1818 	if (!vsi->rss_size)
1819 		return -EINVAL;
1820 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1821 	if (!lut)
1822 		return -ENOMEM;
1823 
1824 	/* Use the user configured hash keys and lookup table if there is one,
1825 	 * otherwise use default
1826 	 */
1827 	if (vsi->rss_lut_user)
1828 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1829 	else
1830 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1831 	if (vsi->rss_hkey_user)
1832 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1833 	else
1834 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1835 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1836 	kfree(lut);
1837 	return ret;
1838 }
1839 
1840 /**
1841  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1842  * @vsi: the VSI being configured,
1843  * @ctxt: VSI context structure
1844  * @enabled_tc: number of traffic classes to enable
1845  *
1846  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1847  **/
1848 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1849 					   struct i40e_vsi_context *ctxt,
1850 					   u8 enabled_tc)
1851 {
1852 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1853 	int i, override_q, pow, num_qps, ret;
1854 	u8 netdev_tc = 0, offset = 0;
1855 
1856 	if (vsi->type != I40E_VSI_MAIN)
1857 		return -EINVAL;
1858 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1859 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1860 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1861 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1862 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1863 
1864 	/* find the next higher power-of-2 of num queue pairs */
1865 	pow = ilog2(num_qps);
1866 	if (!is_power_of_2(num_qps))
1867 		pow++;
1868 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1869 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1870 
1871 	/* Setup queue offset/count for all TCs for given VSI */
1872 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1873 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1874 		/* See if the given TC is enabled for the given VSI */
1875 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1876 			offset = vsi->mqprio_qopt.qopt.offset[i];
1877 			qcount = vsi->mqprio_qopt.qopt.count[i];
1878 			if (qcount > max_qcount)
1879 				max_qcount = qcount;
1880 			vsi->tc_config.tc_info[i].qoffset = offset;
1881 			vsi->tc_config.tc_info[i].qcount = qcount;
1882 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1883 		} else {
1884 			/* TC is not enabled so set the offset to
1885 			 * default queue and allocate one queue
1886 			 * for the given TC.
1887 			 */
1888 			vsi->tc_config.tc_info[i].qoffset = 0;
1889 			vsi->tc_config.tc_info[i].qcount = 1;
1890 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1891 		}
1892 	}
1893 
1894 	/* Set actual Tx/Rx queue pairs */
1895 	vsi->num_queue_pairs = offset + qcount;
1896 
1897 	/* Setup queue TC[0].qmap for given VSI context */
1898 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1899 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1900 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1901 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1902 
1903 	/* Reconfigure RSS for main VSI with max queue count */
1904 	vsi->rss_size = max_qcount;
1905 	ret = i40e_vsi_config_rss(vsi);
1906 	if (ret) {
1907 		dev_info(&vsi->back->pdev->dev,
1908 			 "Failed to reconfig rss for num_queues (%u)\n",
1909 			 max_qcount);
1910 		return ret;
1911 	}
1912 	vsi->reconfig_rss = true;
1913 	dev_dbg(&vsi->back->pdev->dev,
1914 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1915 
1916 	/* Find queue count available for channel VSIs and starting offset
1917 	 * for channel VSIs
1918 	 */
1919 	override_q = vsi->mqprio_qopt.qopt.count[0];
1920 	if (override_q && override_q < vsi->num_queue_pairs) {
1921 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1922 		vsi->next_base_queue = override_q;
1923 	}
1924 	return 0;
1925 }
1926 
1927 /**
1928  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1929  * @vsi: the VSI being setup
1930  * @ctxt: VSI context structure
1931  * @enabled_tc: Enabled TCs bitmap
1932  * @is_add: True if called before Add VSI
1933  *
1934  * Setup VSI queue mapping for enabled traffic classes.
1935  **/
1936 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1937 				     struct i40e_vsi_context *ctxt,
1938 				     u8 enabled_tc,
1939 				     bool is_add)
1940 {
1941 	struct i40e_pf *pf = vsi->back;
1942 	u16 num_tc_qps = 0;
1943 	u16 sections = 0;
1944 	u8 netdev_tc = 0;
1945 	u16 numtc = 1;
1946 	u16 qcount;
1947 	u8 offset;
1948 	u16 qmap;
1949 	int i;
1950 
1951 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1952 	offset = 0;
1953 	/* zero out queue mapping, it will get updated on the end of the function */
1954 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
1955 
1956 	if (vsi->type == I40E_VSI_MAIN) {
1957 		/* This code helps add more queue to the VSI if we have
1958 		 * more cores than RSS can support, the higher cores will
1959 		 * be served by ATR or other filters. Furthermore, the
1960 		 * non-zero req_queue_pairs says that user requested a new
1961 		 * queue count via ethtool's set_channels, so use this
1962 		 * value for queues distribution across traffic classes
1963 		 */
1964 		if (vsi->req_queue_pairs > 0)
1965 			vsi->num_queue_pairs = vsi->req_queue_pairs;
1966 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1967 			vsi->num_queue_pairs = pf->num_lan_msix;
1968 	}
1969 
1970 	/* Number of queues per enabled TC */
1971 	if (vsi->type == I40E_VSI_MAIN ||
1972 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
1973 		num_tc_qps = vsi->num_queue_pairs;
1974 	else
1975 		num_tc_qps = vsi->alloc_queue_pairs;
1976 
1977 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1978 		/* Find numtc from enabled TC bitmap */
1979 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1980 			if (enabled_tc & BIT(i)) /* TC is enabled */
1981 				numtc++;
1982 		}
1983 		if (!numtc) {
1984 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1985 			numtc = 1;
1986 		}
1987 		num_tc_qps = num_tc_qps / numtc;
1988 		num_tc_qps = min_t(int, num_tc_qps,
1989 				   i40e_pf_get_max_q_per_tc(pf));
1990 	}
1991 
1992 	vsi->tc_config.numtc = numtc;
1993 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1994 
1995 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
1996 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1997 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
1998 
1999 	/* Setup queue offset/count for all TCs for given VSI */
2000 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2001 		/* See if the given TC is enabled for the given VSI */
2002 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2003 			/* TC is enabled */
2004 			int pow, num_qps;
2005 
2006 			switch (vsi->type) {
2007 			case I40E_VSI_MAIN:
2008 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
2009 				    I40E_FLAG_FD_ATR_ENABLED)) ||
2010 				    vsi->tc_config.enabled_tc != 1) {
2011 					qcount = min_t(int, pf->alloc_rss_size,
2012 						       num_tc_qps);
2013 					break;
2014 				}
2015 				fallthrough;
2016 			case I40E_VSI_FDIR:
2017 			case I40E_VSI_SRIOV:
2018 			case I40E_VSI_VMDQ2:
2019 			default:
2020 				qcount = num_tc_qps;
2021 				WARN_ON(i != 0);
2022 				break;
2023 			}
2024 			vsi->tc_config.tc_info[i].qoffset = offset;
2025 			vsi->tc_config.tc_info[i].qcount = qcount;
2026 
2027 			/* find the next higher power-of-2 of num queue pairs */
2028 			num_qps = qcount;
2029 			pow = 0;
2030 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2031 				pow++;
2032 				num_qps >>= 1;
2033 			}
2034 
2035 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2036 			qmap =
2037 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2038 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2039 
2040 			offset += qcount;
2041 		} else {
2042 			/* TC is not enabled so set the offset to
2043 			 * default queue and allocate one queue
2044 			 * for the given TC.
2045 			 */
2046 			vsi->tc_config.tc_info[i].qoffset = 0;
2047 			vsi->tc_config.tc_info[i].qcount = 1;
2048 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2049 
2050 			qmap = 0;
2051 		}
2052 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2053 	}
2054 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2055 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2056 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2057 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2058 		vsi->num_queue_pairs = offset;
2059 
2060 	/* Scheduler section valid can only be set for ADD VSI */
2061 	if (is_add) {
2062 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2063 
2064 		ctxt->info.up_enable_bits = enabled_tc;
2065 	}
2066 	if (vsi->type == I40E_VSI_SRIOV) {
2067 		ctxt->info.mapping_flags |=
2068 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2069 		for (i = 0; i < vsi->num_queue_pairs; i++)
2070 			ctxt->info.queue_mapping[i] =
2071 					       cpu_to_le16(vsi->base_queue + i);
2072 	} else {
2073 		ctxt->info.mapping_flags |=
2074 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2075 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2076 	}
2077 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2078 }
2079 
2080 /**
2081  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2082  * @netdev: the netdevice
2083  * @addr: address to add
2084  *
2085  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2086  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2087  */
2088 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2089 {
2090 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2091 	struct i40e_vsi *vsi = np->vsi;
2092 
2093 	if (i40e_add_mac_filter(vsi, addr))
2094 		return 0;
2095 	else
2096 		return -ENOMEM;
2097 }
2098 
2099 /**
2100  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2101  * @netdev: the netdevice
2102  * @addr: address to add
2103  *
2104  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2105  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2106  */
2107 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2108 {
2109 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2110 	struct i40e_vsi *vsi = np->vsi;
2111 
2112 	/* Under some circumstances, we might receive a request to delete
2113 	 * our own device address from our uc list. Because we store the
2114 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2115 	 * such requests and not delete our device address from this list.
2116 	 */
2117 	if (ether_addr_equal(addr, netdev->dev_addr))
2118 		return 0;
2119 
2120 	i40e_del_mac_filter(vsi, addr);
2121 
2122 	return 0;
2123 }
2124 
2125 /**
2126  * i40e_set_rx_mode - NDO callback to set the netdev filters
2127  * @netdev: network interface device structure
2128  **/
2129 static void i40e_set_rx_mode(struct net_device *netdev)
2130 {
2131 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2132 	struct i40e_vsi *vsi = np->vsi;
2133 
2134 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2135 
2136 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2137 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2138 
2139 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2140 
2141 	/* check for other flag changes */
2142 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2143 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2144 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2145 	}
2146 }
2147 
2148 /**
2149  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2150  * @vsi: Pointer to VSI struct
2151  * @from: Pointer to list which contains MAC filter entries - changes to
2152  *        those entries needs to be undone.
2153  *
2154  * MAC filter entries from this list were slated for deletion.
2155  **/
2156 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2157 					 struct hlist_head *from)
2158 {
2159 	struct i40e_mac_filter *f;
2160 	struct hlist_node *h;
2161 
2162 	hlist_for_each_entry_safe(f, h, from, hlist) {
2163 		u64 key = i40e_addr_to_hkey(f->macaddr);
2164 
2165 		/* Move the element back into MAC filter list*/
2166 		hlist_del(&f->hlist);
2167 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2168 	}
2169 }
2170 
2171 /**
2172  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2173  * @vsi: Pointer to vsi struct
2174  * @from: Pointer to list which contains MAC filter entries - changes to
2175  *        those entries needs to be undone.
2176  *
2177  * MAC filter entries from this list were slated for addition.
2178  **/
2179 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2180 					 struct hlist_head *from)
2181 {
2182 	struct i40e_new_mac_filter *new;
2183 	struct hlist_node *h;
2184 
2185 	hlist_for_each_entry_safe(new, h, from, hlist) {
2186 		/* We can simply free the wrapper structure */
2187 		hlist_del(&new->hlist);
2188 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2189 		kfree(new);
2190 	}
2191 }
2192 
2193 /**
2194  * i40e_next_filter - Get the next non-broadcast filter from a list
2195  * @next: pointer to filter in list
2196  *
2197  * Returns the next non-broadcast filter in the list. Required so that we
2198  * ignore broadcast filters within the list, since these are not handled via
2199  * the normal firmware update path.
2200  */
2201 static
2202 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2203 {
2204 	hlist_for_each_entry_continue(next, hlist) {
2205 		if (!is_broadcast_ether_addr(next->f->macaddr))
2206 			return next;
2207 	}
2208 
2209 	return NULL;
2210 }
2211 
2212 /**
2213  * i40e_update_filter_state - Update filter state based on return data
2214  * from firmware
2215  * @count: Number of filters added
2216  * @add_list: return data from fw
2217  * @add_head: pointer to first filter in current batch
2218  *
2219  * MAC filter entries from list were slated to be added to device. Returns
2220  * number of successful filters. Note that 0 does NOT mean success!
2221  **/
2222 static int
2223 i40e_update_filter_state(int count,
2224 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2225 			 struct i40e_new_mac_filter *add_head)
2226 {
2227 	int retval = 0;
2228 	int i;
2229 
2230 	for (i = 0; i < count; i++) {
2231 		/* Always check status of each filter. We don't need to check
2232 		 * the firmware return status because we pre-set the filter
2233 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2234 		 * request to the adminq. Thus, if it no longer matches then
2235 		 * we know the filter is active.
2236 		 */
2237 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2238 			add_head->state = I40E_FILTER_FAILED;
2239 		} else {
2240 			add_head->state = I40E_FILTER_ACTIVE;
2241 			retval++;
2242 		}
2243 
2244 		add_head = i40e_next_filter(add_head);
2245 		if (!add_head)
2246 			break;
2247 	}
2248 
2249 	return retval;
2250 }
2251 
2252 /**
2253  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2254  * @vsi: ptr to the VSI
2255  * @vsi_name: name to display in messages
2256  * @list: the list of filters to send to firmware
2257  * @num_del: the number of filters to delete
2258  * @retval: Set to -EIO on failure to delete
2259  *
2260  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2261  * *retval instead of a return value so that success does not force ret_val to
2262  * be set to 0. This ensures that a sequence of calls to this function
2263  * preserve the previous value of *retval on successful delete.
2264  */
2265 static
2266 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2267 			  struct i40e_aqc_remove_macvlan_element_data *list,
2268 			  int num_del, int *retval)
2269 {
2270 	struct i40e_hw *hw = &vsi->back->hw;
2271 	enum i40e_admin_queue_err aq_status;
2272 	i40e_status aq_ret;
2273 
2274 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2275 					   &aq_status);
2276 
2277 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2278 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2279 		*retval = -EIO;
2280 		dev_info(&vsi->back->pdev->dev,
2281 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2282 			 vsi_name, i40e_stat_str(hw, aq_ret),
2283 			 i40e_aq_str(hw, aq_status));
2284 	}
2285 }
2286 
2287 /**
2288  * i40e_aqc_add_filters - Request firmware to add a set of filters
2289  * @vsi: ptr to the VSI
2290  * @vsi_name: name to display in messages
2291  * @list: the list of filters to send to firmware
2292  * @add_head: Position in the add hlist
2293  * @num_add: the number of filters to add
2294  *
2295  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2296  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2297  * space for more filters.
2298  */
2299 static
2300 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2301 			  struct i40e_aqc_add_macvlan_element_data *list,
2302 			  struct i40e_new_mac_filter *add_head,
2303 			  int num_add)
2304 {
2305 	struct i40e_hw *hw = &vsi->back->hw;
2306 	enum i40e_admin_queue_err aq_status;
2307 	int fcnt;
2308 
2309 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2310 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2311 
2312 	if (fcnt != num_add) {
2313 		if (vsi->type == I40E_VSI_MAIN) {
2314 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2315 			dev_warn(&vsi->back->pdev->dev,
2316 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2317 				 i40e_aq_str(hw, aq_status), vsi_name);
2318 		} else if (vsi->type == I40E_VSI_SRIOV ||
2319 			   vsi->type == I40E_VSI_VMDQ1 ||
2320 			   vsi->type == I40E_VSI_VMDQ2) {
2321 			dev_warn(&vsi->back->pdev->dev,
2322 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2323 				 i40e_aq_str(hw, aq_status), vsi_name,
2324 					     vsi_name);
2325 		} else {
2326 			dev_warn(&vsi->back->pdev->dev,
2327 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2328 				 i40e_aq_str(hw, aq_status), vsi_name,
2329 					     vsi->type);
2330 		}
2331 	}
2332 }
2333 
2334 /**
2335  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2336  * @vsi: pointer to the VSI
2337  * @vsi_name: the VSI name
2338  * @f: filter data
2339  *
2340  * This function sets or clears the promiscuous broadcast flags for VLAN
2341  * filters in order to properly receive broadcast frames. Assumes that only
2342  * broadcast filters are passed.
2343  *
2344  * Returns status indicating success or failure;
2345  **/
2346 static i40e_status
2347 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2348 			  struct i40e_mac_filter *f)
2349 {
2350 	bool enable = f->state == I40E_FILTER_NEW;
2351 	struct i40e_hw *hw = &vsi->back->hw;
2352 	i40e_status aq_ret;
2353 
2354 	if (f->vlan == I40E_VLAN_ANY) {
2355 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2356 						   vsi->seid,
2357 						   enable,
2358 						   NULL);
2359 	} else {
2360 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2361 							    vsi->seid,
2362 							    enable,
2363 							    f->vlan,
2364 							    NULL);
2365 	}
2366 
2367 	if (aq_ret) {
2368 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2369 		dev_warn(&vsi->back->pdev->dev,
2370 			 "Error %s, forcing overflow promiscuous on %s\n",
2371 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2372 			 vsi_name);
2373 	}
2374 
2375 	return aq_ret;
2376 }
2377 
2378 /**
2379  * i40e_set_promiscuous - set promiscuous mode
2380  * @pf: board private structure
2381  * @promisc: promisc on or off
2382  *
2383  * There are different ways of setting promiscuous mode on a PF depending on
2384  * what state/environment we're in.  This identifies and sets it appropriately.
2385  * Returns 0 on success.
2386  **/
2387 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2388 {
2389 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2390 	struct i40e_hw *hw = &pf->hw;
2391 	i40e_status aq_ret;
2392 
2393 	if (vsi->type == I40E_VSI_MAIN &&
2394 	    pf->lan_veb != I40E_NO_VEB &&
2395 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2396 		/* set defport ON for Main VSI instead of true promisc
2397 		 * this way we will get all unicast/multicast and VLAN
2398 		 * promisc behavior but will not get VF or VMDq traffic
2399 		 * replicated on the Main VSI.
2400 		 */
2401 		if (promisc)
2402 			aq_ret = i40e_aq_set_default_vsi(hw,
2403 							 vsi->seid,
2404 							 NULL);
2405 		else
2406 			aq_ret = i40e_aq_clear_default_vsi(hw,
2407 							   vsi->seid,
2408 							   NULL);
2409 		if (aq_ret) {
2410 			dev_info(&pf->pdev->dev,
2411 				 "Set default VSI failed, err %s, aq_err %s\n",
2412 				 i40e_stat_str(hw, aq_ret),
2413 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2414 		}
2415 	} else {
2416 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2417 						  hw,
2418 						  vsi->seid,
2419 						  promisc, NULL,
2420 						  true);
2421 		if (aq_ret) {
2422 			dev_info(&pf->pdev->dev,
2423 				 "set unicast promisc failed, err %s, aq_err %s\n",
2424 				 i40e_stat_str(hw, aq_ret),
2425 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2426 		}
2427 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2428 						  hw,
2429 						  vsi->seid,
2430 						  promisc, NULL);
2431 		if (aq_ret) {
2432 			dev_info(&pf->pdev->dev,
2433 				 "set multicast promisc failed, err %s, aq_err %s\n",
2434 				 i40e_stat_str(hw, aq_ret),
2435 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2436 		}
2437 	}
2438 
2439 	if (!aq_ret)
2440 		pf->cur_promisc = promisc;
2441 
2442 	return aq_ret;
2443 }
2444 
2445 /**
2446  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2447  * @vsi: ptr to the VSI
2448  *
2449  * Push any outstanding VSI filter changes through the AdminQ.
2450  *
2451  * Returns 0 or error value
2452  **/
2453 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2454 {
2455 	struct hlist_head tmp_add_list, tmp_del_list;
2456 	struct i40e_mac_filter *f;
2457 	struct i40e_new_mac_filter *new, *add_head = NULL;
2458 	struct i40e_hw *hw = &vsi->back->hw;
2459 	bool old_overflow, new_overflow;
2460 	unsigned int failed_filters = 0;
2461 	unsigned int vlan_filters = 0;
2462 	char vsi_name[16] = "PF";
2463 	int filter_list_len = 0;
2464 	i40e_status aq_ret = 0;
2465 	u32 changed_flags = 0;
2466 	struct hlist_node *h;
2467 	struct i40e_pf *pf;
2468 	int num_add = 0;
2469 	int num_del = 0;
2470 	int retval = 0;
2471 	u16 cmd_flags;
2472 	int list_size;
2473 	int bkt;
2474 
2475 	/* empty array typed pointers, kcalloc later */
2476 	struct i40e_aqc_add_macvlan_element_data *add_list;
2477 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2478 
2479 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2480 		usleep_range(1000, 2000);
2481 	pf = vsi->back;
2482 
2483 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2484 
2485 	if (vsi->netdev) {
2486 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2487 		vsi->current_netdev_flags = vsi->netdev->flags;
2488 	}
2489 
2490 	INIT_HLIST_HEAD(&tmp_add_list);
2491 	INIT_HLIST_HEAD(&tmp_del_list);
2492 
2493 	if (vsi->type == I40E_VSI_SRIOV)
2494 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2495 	else if (vsi->type != I40E_VSI_MAIN)
2496 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2497 
2498 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2499 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2500 
2501 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2502 		/* Create a list of filters to delete. */
2503 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2504 			if (f->state == I40E_FILTER_REMOVE) {
2505 				/* Move the element into temporary del_list */
2506 				hash_del(&f->hlist);
2507 				hlist_add_head(&f->hlist, &tmp_del_list);
2508 
2509 				/* Avoid counting removed filters */
2510 				continue;
2511 			}
2512 			if (f->state == I40E_FILTER_NEW) {
2513 				/* Create a temporary i40e_new_mac_filter */
2514 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2515 				if (!new)
2516 					goto err_no_memory_locked;
2517 
2518 				/* Store pointer to the real filter */
2519 				new->f = f;
2520 				new->state = f->state;
2521 
2522 				/* Add it to the hash list */
2523 				hlist_add_head(&new->hlist, &tmp_add_list);
2524 			}
2525 
2526 			/* Count the number of active (current and new) VLAN
2527 			 * filters we have now. Does not count filters which
2528 			 * are marked for deletion.
2529 			 */
2530 			if (f->vlan > 0)
2531 				vlan_filters++;
2532 		}
2533 
2534 		if (vsi->type != I40E_VSI_SRIOV)
2535 			retval = i40e_correct_mac_vlan_filters
2536 				(vsi, &tmp_add_list, &tmp_del_list,
2537 				 vlan_filters);
2538 		else
2539 			retval = i40e_correct_vf_mac_vlan_filters
2540 				(vsi, &tmp_add_list, &tmp_del_list,
2541 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2542 
2543 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2544 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2545 
2546 		if (retval)
2547 			goto err_no_memory_locked;
2548 
2549 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2550 	}
2551 
2552 	/* Now process 'del_list' outside the lock */
2553 	if (!hlist_empty(&tmp_del_list)) {
2554 		filter_list_len = hw->aq.asq_buf_size /
2555 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2556 		list_size = filter_list_len *
2557 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2558 		del_list = kzalloc(list_size, GFP_ATOMIC);
2559 		if (!del_list)
2560 			goto err_no_memory;
2561 
2562 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2563 			cmd_flags = 0;
2564 
2565 			/* handle broadcast filters by updating the broadcast
2566 			 * promiscuous flag and release filter list.
2567 			 */
2568 			if (is_broadcast_ether_addr(f->macaddr)) {
2569 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2570 
2571 				hlist_del(&f->hlist);
2572 				kfree(f);
2573 				continue;
2574 			}
2575 
2576 			/* add to delete list */
2577 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2578 			if (f->vlan == I40E_VLAN_ANY) {
2579 				del_list[num_del].vlan_tag = 0;
2580 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2581 			} else {
2582 				del_list[num_del].vlan_tag =
2583 					cpu_to_le16((u16)(f->vlan));
2584 			}
2585 
2586 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2587 			del_list[num_del].flags = cmd_flags;
2588 			num_del++;
2589 
2590 			/* flush a full buffer */
2591 			if (num_del == filter_list_len) {
2592 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2593 						     num_del, &retval);
2594 				memset(del_list, 0, list_size);
2595 				num_del = 0;
2596 			}
2597 			/* Release memory for MAC filter entries which were
2598 			 * synced up with HW.
2599 			 */
2600 			hlist_del(&f->hlist);
2601 			kfree(f);
2602 		}
2603 
2604 		if (num_del) {
2605 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2606 					     num_del, &retval);
2607 		}
2608 
2609 		kfree(del_list);
2610 		del_list = NULL;
2611 	}
2612 
2613 	if (!hlist_empty(&tmp_add_list)) {
2614 		/* Do all the adds now. */
2615 		filter_list_len = hw->aq.asq_buf_size /
2616 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2617 		list_size = filter_list_len *
2618 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2619 		add_list = kzalloc(list_size, GFP_ATOMIC);
2620 		if (!add_list)
2621 			goto err_no_memory;
2622 
2623 		num_add = 0;
2624 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2625 			/* handle broadcast filters by updating the broadcast
2626 			 * promiscuous flag instead of adding a MAC filter.
2627 			 */
2628 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2629 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2630 							      new->f))
2631 					new->state = I40E_FILTER_FAILED;
2632 				else
2633 					new->state = I40E_FILTER_ACTIVE;
2634 				continue;
2635 			}
2636 
2637 			/* add to add array */
2638 			if (num_add == 0)
2639 				add_head = new;
2640 			cmd_flags = 0;
2641 			ether_addr_copy(add_list[num_add].mac_addr,
2642 					new->f->macaddr);
2643 			if (new->f->vlan == I40E_VLAN_ANY) {
2644 				add_list[num_add].vlan_tag = 0;
2645 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2646 			} else {
2647 				add_list[num_add].vlan_tag =
2648 					cpu_to_le16((u16)(new->f->vlan));
2649 			}
2650 			add_list[num_add].queue_number = 0;
2651 			/* set invalid match method for later detection */
2652 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2653 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2654 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2655 			num_add++;
2656 
2657 			/* flush a full buffer */
2658 			if (num_add == filter_list_len) {
2659 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2660 						     add_head, num_add);
2661 				memset(add_list, 0, list_size);
2662 				num_add = 0;
2663 			}
2664 		}
2665 		if (num_add) {
2666 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2667 					     num_add);
2668 		}
2669 		/* Now move all of the filters from the temp add list back to
2670 		 * the VSI's list.
2671 		 */
2672 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2673 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2674 			/* Only update the state if we're still NEW */
2675 			if (new->f->state == I40E_FILTER_NEW)
2676 				new->f->state = new->state;
2677 			hlist_del(&new->hlist);
2678 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2679 			kfree(new);
2680 		}
2681 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2682 		kfree(add_list);
2683 		add_list = NULL;
2684 	}
2685 
2686 	/* Determine the number of active and failed filters. */
2687 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2688 	vsi->active_filters = 0;
2689 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2690 		if (f->state == I40E_FILTER_ACTIVE)
2691 			vsi->active_filters++;
2692 		else if (f->state == I40E_FILTER_FAILED)
2693 			failed_filters++;
2694 	}
2695 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2696 
2697 	/* Check if we are able to exit overflow promiscuous mode. We can
2698 	 * safely exit if we didn't just enter, we no longer have any failed
2699 	 * filters, and we have reduced filters below the threshold value.
2700 	 */
2701 	if (old_overflow && !failed_filters &&
2702 	    vsi->active_filters < vsi->promisc_threshold) {
2703 		dev_info(&pf->pdev->dev,
2704 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2705 			 vsi_name);
2706 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2707 		vsi->promisc_threshold = 0;
2708 	}
2709 
2710 	/* if the VF is not trusted do not do promisc */
2711 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2712 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2713 		goto out;
2714 	}
2715 
2716 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2717 
2718 	/* If we are entering overflow promiscuous, we need to calculate a new
2719 	 * threshold for when we are safe to exit
2720 	 */
2721 	if (!old_overflow && new_overflow)
2722 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2723 
2724 	/* check for changes in promiscuous modes */
2725 	if (changed_flags & IFF_ALLMULTI) {
2726 		bool cur_multipromisc;
2727 
2728 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2729 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2730 							       vsi->seid,
2731 							       cur_multipromisc,
2732 							       NULL);
2733 		if (aq_ret) {
2734 			retval = i40e_aq_rc_to_posix(aq_ret,
2735 						     hw->aq.asq_last_status);
2736 			dev_info(&pf->pdev->dev,
2737 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2738 				 vsi_name,
2739 				 i40e_stat_str(hw, aq_ret),
2740 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2741 		} else {
2742 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2743 				 cur_multipromisc ? "entering" : "leaving");
2744 		}
2745 	}
2746 
2747 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2748 		bool cur_promisc;
2749 
2750 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2751 			       new_overflow);
2752 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2753 		if (aq_ret) {
2754 			retval = i40e_aq_rc_to_posix(aq_ret,
2755 						     hw->aq.asq_last_status);
2756 			dev_info(&pf->pdev->dev,
2757 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2758 				 cur_promisc ? "on" : "off",
2759 				 vsi_name,
2760 				 i40e_stat_str(hw, aq_ret),
2761 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2762 		}
2763 	}
2764 out:
2765 	/* if something went wrong then set the changed flag so we try again */
2766 	if (retval)
2767 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2768 
2769 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2770 	return retval;
2771 
2772 err_no_memory:
2773 	/* Restore elements on the temporary add and delete lists */
2774 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2775 err_no_memory_locked:
2776 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2777 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2778 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2779 
2780 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2781 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2782 	return -ENOMEM;
2783 }
2784 
2785 /**
2786  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2787  * @pf: board private structure
2788  **/
2789 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2790 {
2791 	int v;
2792 
2793 	if (!pf)
2794 		return;
2795 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2796 		return;
2797 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2798 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2799 		return;
2800 	}
2801 
2802 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2803 		if (pf->vsi[v] &&
2804 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2805 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2806 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2807 
2808 			if (ret) {
2809 				/* come back and try again later */
2810 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2811 					pf->state);
2812 				break;
2813 			}
2814 		}
2815 	}
2816 }
2817 
2818 /**
2819  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2820  * @vsi: the vsi
2821  **/
2822 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2823 {
2824 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2825 		return I40E_RXBUFFER_2048;
2826 	else
2827 		return I40E_RXBUFFER_3072;
2828 }
2829 
2830 /**
2831  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2832  * @netdev: network interface device structure
2833  * @new_mtu: new value for maximum frame size
2834  *
2835  * Returns 0 on success, negative on failure
2836  **/
2837 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2838 {
2839 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2840 	struct i40e_vsi *vsi = np->vsi;
2841 	struct i40e_pf *pf = vsi->back;
2842 
2843 	if (i40e_enabled_xdp_vsi(vsi)) {
2844 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2845 
2846 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2847 			return -EINVAL;
2848 	}
2849 
2850 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2851 		   netdev->mtu, new_mtu);
2852 	netdev->mtu = new_mtu;
2853 	if (netif_running(netdev))
2854 		i40e_vsi_reinit_locked(vsi);
2855 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2856 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2857 	return 0;
2858 }
2859 
2860 /**
2861  * i40e_ioctl - Access the hwtstamp interface
2862  * @netdev: network interface device structure
2863  * @ifr: interface request data
2864  * @cmd: ioctl command
2865  **/
2866 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2867 {
2868 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2869 	struct i40e_pf *pf = np->vsi->back;
2870 
2871 	switch (cmd) {
2872 	case SIOCGHWTSTAMP:
2873 		return i40e_ptp_get_ts_config(pf, ifr);
2874 	case SIOCSHWTSTAMP:
2875 		return i40e_ptp_set_ts_config(pf, ifr);
2876 	default:
2877 		return -EOPNOTSUPP;
2878 	}
2879 }
2880 
2881 /**
2882  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2883  * @vsi: the vsi being adjusted
2884  **/
2885 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2886 {
2887 	struct i40e_vsi_context ctxt;
2888 	i40e_status ret;
2889 
2890 	/* Don't modify stripping options if a port VLAN is active */
2891 	if (vsi->info.pvid)
2892 		return;
2893 
2894 	if ((vsi->info.valid_sections &
2895 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2896 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2897 		return;  /* already enabled */
2898 
2899 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2900 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2901 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2902 
2903 	ctxt.seid = vsi->seid;
2904 	ctxt.info = vsi->info;
2905 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2906 	if (ret) {
2907 		dev_info(&vsi->back->pdev->dev,
2908 			 "update vlan stripping failed, err %s aq_err %s\n",
2909 			 i40e_stat_str(&vsi->back->hw, ret),
2910 			 i40e_aq_str(&vsi->back->hw,
2911 				     vsi->back->hw.aq.asq_last_status));
2912 	}
2913 }
2914 
2915 /**
2916  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2917  * @vsi: the vsi being adjusted
2918  **/
2919 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2920 {
2921 	struct i40e_vsi_context ctxt;
2922 	i40e_status ret;
2923 
2924 	/* Don't modify stripping options if a port VLAN is active */
2925 	if (vsi->info.pvid)
2926 		return;
2927 
2928 	if ((vsi->info.valid_sections &
2929 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2930 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2931 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
2932 		return;  /* already disabled */
2933 
2934 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2935 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2936 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2937 
2938 	ctxt.seid = vsi->seid;
2939 	ctxt.info = vsi->info;
2940 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2941 	if (ret) {
2942 		dev_info(&vsi->back->pdev->dev,
2943 			 "update vlan stripping failed, err %s aq_err %s\n",
2944 			 i40e_stat_str(&vsi->back->hw, ret),
2945 			 i40e_aq_str(&vsi->back->hw,
2946 				     vsi->back->hw.aq.asq_last_status));
2947 	}
2948 }
2949 
2950 /**
2951  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2952  * @vsi: the vsi being configured
2953  * @vid: vlan id to be added (0 = untagged only , -1 = any)
2954  *
2955  * This is a helper function for adding a new MAC/VLAN filter with the
2956  * specified VLAN for each existing MAC address already in the hash table.
2957  * This function does *not* perform any accounting to update filters based on
2958  * VLAN mode.
2959  *
2960  * NOTE: this function expects to be called while under the
2961  * mac_filter_hash_lock
2962  **/
2963 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2964 {
2965 	struct i40e_mac_filter *f, *add_f;
2966 	struct hlist_node *h;
2967 	int bkt;
2968 
2969 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2970 		/* If we're asked to add a filter that has been marked for
2971 		 * removal, it is safe to simply restore it to active state.
2972 		 * __i40e_del_filter will have simply deleted any filters which
2973 		 * were previously marked NEW or FAILED, so if it is currently
2974 		 * marked REMOVE it must have previously been ACTIVE. Since we
2975 		 * haven't yet run the sync filters task, just restore this
2976 		 * filter to the ACTIVE state so that the sync task leaves it
2977 		 * in place.
2978 		 */
2979 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
2980 			f->state = I40E_FILTER_ACTIVE;
2981 			continue;
2982 		} else if (f->state == I40E_FILTER_REMOVE) {
2983 			continue;
2984 		}
2985 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
2986 		if (!add_f) {
2987 			dev_info(&vsi->back->pdev->dev,
2988 				 "Could not add vlan filter %d for %pM\n",
2989 				 vid, f->macaddr);
2990 			return -ENOMEM;
2991 		}
2992 	}
2993 
2994 	return 0;
2995 }
2996 
2997 /**
2998  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2999  * @vsi: the VSI being configured
3000  * @vid: VLAN id to be added
3001  **/
3002 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3003 {
3004 	int err;
3005 
3006 	if (vsi->info.pvid)
3007 		return -EINVAL;
3008 
3009 	/* The network stack will attempt to add VID=0, with the intention to
3010 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3011 	 * these packets by default when configured to receive untagged
3012 	 * packets, so we don't need to add a filter for this case.
3013 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3014 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3015 	 * Thus, we do not want to actually add a filter for VID=0
3016 	 */
3017 	if (!vid)
3018 		return 0;
3019 
3020 	/* Locked once because all functions invoked below iterates list*/
3021 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3022 	err = i40e_add_vlan_all_mac(vsi, vid);
3023 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3024 	if (err)
3025 		return err;
3026 
3027 	/* schedule our worker thread which will take care of
3028 	 * applying the new filter changes
3029 	 */
3030 	i40e_service_event_schedule(vsi->back);
3031 	return 0;
3032 }
3033 
3034 /**
3035  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3036  * @vsi: the vsi being configured
3037  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3038  *
3039  * This function should be used to remove all VLAN filters which match the
3040  * given VID. It does not schedule the service event and does not take the
3041  * mac_filter_hash_lock so it may be combined with other operations under
3042  * a single invocation of the mac_filter_hash_lock.
3043  *
3044  * NOTE: this function expects to be called while under the
3045  * mac_filter_hash_lock
3046  */
3047 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3048 {
3049 	struct i40e_mac_filter *f;
3050 	struct hlist_node *h;
3051 	int bkt;
3052 
3053 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3054 		if (f->vlan == vid)
3055 			__i40e_del_filter(vsi, f);
3056 	}
3057 }
3058 
3059 /**
3060  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3061  * @vsi: the VSI being configured
3062  * @vid: VLAN id to be removed
3063  **/
3064 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3065 {
3066 	if (!vid || vsi->info.pvid)
3067 		return;
3068 
3069 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3070 	i40e_rm_vlan_all_mac(vsi, vid);
3071 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3072 
3073 	/* schedule our worker thread which will take care of
3074 	 * applying the new filter changes
3075 	 */
3076 	i40e_service_event_schedule(vsi->back);
3077 }
3078 
3079 /**
3080  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3081  * @netdev: network interface to be adjusted
3082  * @proto: unused protocol value
3083  * @vid: vlan id to be added
3084  *
3085  * net_device_ops implementation for adding vlan ids
3086  **/
3087 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3088 				__always_unused __be16 proto, u16 vid)
3089 {
3090 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3091 	struct i40e_vsi *vsi = np->vsi;
3092 	int ret = 0;
3093 
3094 	if (vid >= VLAN_N_VID)
3095 		return -EINVAL;
3096 
3097 	ret = i40e_vsi_add_vlan(vsi, vid);
3098 	if (!ret)
3099 		set_bit(vid, vsi->active_vlans);
3100 
3101 	return ret;
3102 }
3103 
3104 /**
3105  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3106  * @netdev: network interface to be adjusted
3107  * @proto: unused protocol value
3108  * @vid: vlan id to be added
3109  **/
3110 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3111 				    __always_unused __be16 proto, u16 vid)
3112 {
3113 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3114 	struct i40e_vsi *vsi = np->vsi;
3115 
3116 	if (vid >= VLAN_N_VID)
3117 		return;
3118 	set_bit(vid, vsi->active_vlans);
3119 }
3120 
3121 /**
3122  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3123  * @netdev: network interface to be adjusted
3124  * @proto: unused protocol value
3125  * @vid: vlan id to be removed
3126  *
3127  * net_device_ops implementation for removing vlan ids
3128  **/
3129 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3130 				 __always_unused __be16 proto, u16 vid)
3131 {
3132 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3133 	struct i40e_vsi *vsi = np->vsi;
3134 
3135 	/* return code is ignored as there is nothing a user
3136 	 * can do about failure to remove and a log message was
3137 	 * already printed from the other function
3138 	 */
3139 	i40e_vsi_kill_vlan(vsi, vid);
3140 
3141 	clear_bit(vid, vsi->active_vlans);
3142 
3143 	return 0;
3144 }
3145 
3146 /**
3147  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3148  * @vsi: the vsi being brought back up
3149  **/
3150 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3151 {
3152 	u16 vid;
3153 
3154 	if (!vsi->netdev)
3155 		return;
3156 
3157 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3158 		i40e_vlan_stripping_enable(vsi);
3159 	else
3160 		i40e_vlan_stripping_disable(vsi);
3161 
3162 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3163 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3164 					vid);
3165 }
3166 
3167 /**
3168  * i40e_vsi_add_pvid - Add pvid for the VSI
3169  * @vsi: the vsi being adjusted
3170  * @vid: the vlan id to set as a PVID
3171  **/
3172 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3173 {
3174 	struct i40e_vsi_context ctxt;
3175 	i40e_status ret;
3176 
3177 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3178 	vsi->info.pvid = cpu_to_le16(vid);
3179 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3180 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3181 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3182 
3183 	ctxt.seid = vsi->seid;
3184 	ctxt.info = vsi->info;
3185 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3186 	if (ret) {
3187 		dev_info(&vsi->back->pdev->dev,
3188 			 "add pvid failed, err %s aq_err %s\n",
3189 			 i40e_stat_str(&vsi->back->hw, ret),
3190 			 i40e_aq_str(&vsi->back->hw,
3191 				     vsi->back->hw.aq.asq_last_status));
3192 		return -ENOENT;
3193 	}
3194 
3195 	return 0;
3196 }
3197 
3198 /**
3199  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3200  * @vsi: the vsi being adjusted
3201  *
3202  * Just use the vlan_rx_register() service to put it back to normal
3203  **/
3204 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3205 {
3206 	vsi->info.pvid = 0;
3207 
3208 	i40e_vlan_stripping_disable(vsi);
3209 }
3210 
3211 /**
3212  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3213  * @vsi: ptr to the VSI
3214  *
3215  * If this function returns with an error, then it's possible one or
3216  * more of the rings is populated (while the rest are not).  It is the
3217  * callers duty to clean those orphaned rings.
3218  *
3219  * Return 0 on success, negative on failure
3220  **/
3221 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3222 {
3223 	int i, err = 0;
3224 
3225 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3226 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3227 
3228 	if (!i40e_enabled_xdp_vsi(vsi))
3229 		return err;
3230 
3231 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3232 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3233 
3234 	return err;
3235 }
3236 
3237 /**
3238  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3239  * @vsi: ptr to the VSI
3240  *
3241  * Free VSI's transmit software resources
3242  **/
3243 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3244 {
3245 	int i;
3246 
3247 	if (vsi->tx_rings) {
3248 		for (i = 0; i < vsi->num_queue_pairs; i++)
3249 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3250 				i40e_free_tx_resources(vsi->tx_rings[i]);
3251 	}
3252 
3253 	if (vsi->xdp_rings) {
3254 		for (i = 0; i < vsi->num_queue_pairs; i++)
3255 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3256 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3257 	}
3258 }
3259 
3260 /**
3261  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3262  * @vsi: ptr to the VSI
3263  *
3264  * If this function returns with an error, then it's possible one or
3265  * more of the rings is populated (while the rest are not).  It is the
3266  * callers duty to clean those orphaned rings.
3267  *
3268  * Return 0 on success, negative on failure
3269  **/
3270 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3271 {
3272 	int i, err = 0;
3273 
3274 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3275 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3276 	return err;
3277 }
3278 
3279 /**
3280  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3281  * @vsi: ptr to the VSI
3282  *
3283  * Free all receive software resources
3284  **/
3285 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3286 {
3287 	int i;
3288 
3289 	if (!vsi->rx_rings)
3290 		return;
3291 
3292 	for (i = 0; i < vsi->num_queue_pairs; i++)
3293 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3294 			i40e_free_rx_resources(vsi->rx_rings[i]);
3295 }
3296 
3297 /**
3298  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3299  * @ring: The Tx ring to configure
3300  *
3301  * This enables/disables XPS for a given Tx descriptor ring
3302  * based on the TCs enabled for the VSI that ring belongs to.
3303  **/
3304 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3305 {
3306 	int cpu;
3307 
3308 	if (!ring->q_vector || !ring->netdev || ring->ch)
3309 		return;
3310 
3311 	/* We only initialize XPS once, so as not to overwrite user settings */
3312 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3313 		return;
3314 
3315 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3316 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3317 			    ring->queue_index);
3318 }
3319 
3320 /**
3321  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3322  * @ring: The Tx or Rx ring
3323  *
3324  * Returns the AF_XDP buffer pool or NULL.
3325  **/
3326 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3327 {
3328 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3329 	int qid = ring->queue_index;
3330 
3331 	if (ring_is_xdp(ring))
3332 		qid -= ring->vsi->alloc_queue_pairs;
3333 
3334 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3335 		return NULL;
3336 
3337 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3338 }
3339 
3340 /**
3341  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3342  * @ring: The Tx ring to configure
3343  *
3344  * Configure the Tx descriptor ring in the HMC context.
3345  **/
3346 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3347 {
3348 	struct i40e_vsi *vsi = ring->vsi;
3349 	u16 pf_q = vsi->base_queue + ring->queue_index;
3350 	struct i40e_hw *hw = &vsi->back->hw;
3351 	struct i40e_hmc_obj_txq tx_ctx;
3352 	i40e_status err = 0;
3353 	u32 qtx_ctl = 0;
3354 
3355 	if (ring_is_xdp(ring))
3356 		ring->xsk_pool = i40e_xsk_pool(ring);
3357 
3358 	/* some ATR related tx ring init */
3359 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3360 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3361 		ring->atr_count = 0;
3362 	} else {
3363 		ring->atr_sample_rate = 0;
3364 	}
3365 
3366 	/* configure XPS */
3367 	i40e_config_xps_tx_ring(ring);
3368 
3369 	/* clear the context structure first */
3370 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3371 
3372 	tx_ctx.new_context = 1;
3373 	tx_ctx.base = (ring->dma / 128);
3374 	tx_ctx.qlen = ring->count;
3375 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3376 					       I40E_FLAG_FD_ATR_ENABLED));
3377 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3378 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3379 	if (vsi->type != I40E_VSI_FDIR)
3380 		tx_ctx.head_wb_ena = 1;
3381 	tx_ctx.head_wb_addr = ring->dma +
3382 			      (ring->count * sizeof(struct i40e_tx_desc));
3383 
3384 	/* As part of VSI creation/update, FW allocates certain
3385 	 * Tx arbitration queue sets for each TC enabled for
3386 	 * the VSI. The FW returns the handles to these queue
3387 	 * sets as part of the response buffer to Add VSI,
3388 	 * Update VSI, etc. AQ commands. It is expected that
3389 	 * these queue set handles be associated with the Tx
3390 	 * queues by the driver as part of the TX queue context
3391 	 * initialization. This has to be done regardless of
3392 	 * DCB as by default everything is mapped to TC0.
3393 	 */
3394 
3395 	if (ring->ch)
3396 		tx_ctx.rdylist =
3397 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3398 
3399 	else
3400 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3401 
3402 	tx_ctx.rdylist_act = 0;
3403 
3404 	/* clear the context in the HMC */
3405 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3406 	if (err) {
3407 		dev_info(&vsi->back->pdev->dev,
3408 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3409 			 ring->queue_index, pf_q, err);
3410 		return -ENOMEM;
3411 	}
3412 
3413 	/* set the context in the HMC */
3414 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3415 	if (err) {
3416 		dev_info(&vsi->back->pdev->dev,
3417 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3418 			 ring->queue_index, pf_q, err);
3419 		return -ENOMEM;
3420 	}
3421 
3422 	/* Now associate this queue with this PCI function */
3423 	if (ring->ch) {
3424 		if (ring->ch->type == I40E_VSI_VMDQ2)
3425 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3426 		else
3427 			return -EINVAL;
3428 
3429 		qtx_ctl |= (ring->ch->vsi_number <<
3430 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3431 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3432 	} else {
3433 		if (vsi->type == I40E_VSI_VMDQ2) {
3434 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3435 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3436 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3437 		} else {
3438 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3439 		}
3440 	}
3441 
3442 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3443 		    I40E_QTX_CTL_PF_INDX_MASK);
3444 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3445 	i40e_flush(hw);
3446 
3447 	/* cache tail off for easier writes later */
3448 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3449 
3450 	return 0;
3451 }
3452 
3453 /**
3454  * i40e_rx_offset - Return expected offset into page to access data
3455  * @rx_ring: Ring we are requesting offset of
3456  *
3457  * Returns the offset value for ring into the data buffer.
3458  */
3459 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3460 {
3461 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3462 }
3463 
3464 /**
3465  * i40e_configure_rx_ring - Configure a receive ring context
3466  * @ring: The Rx ring to configure
3467  *
3468  * Configure the Rx descriptor ring in the HMC context.
3469  **/
3470 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3471 {
3472 	struct i40e_vsi *vsi = ring->vsi;
3473 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3474 	u16 pf_q = vsi->base_queue + ring->queue_index;
3475 	struct i40e_hw *hw = &vsi->back->hw;
3476 	struct i40e_hmc_obj_rxq rx_ctx;
3477 	i40e_status err = 0;
3478 	bool ok;
3479 	int ret;
3480 
3481 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3482 
3483 	/* clear the context structure first */
3484 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3485 
3486 	if (ring->vsi->type == I40E_VSI_MAIN)
3487 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3488 
3489 	kfree(ring->rx_bi);
3490 	ring->xsk_pool = i40e_xsk_pool(ring);
3491 	if (ring->xsk_pool) {
3492 		ret = i40e_alloc_rx_bi_zc(ring);
3493 		if (ret)
3494 			return ret;
3495 		ring->rx_buf_len =
3496 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3497 		/* For AF_XDP ZC, we disallow packets to span on
3498 		 * multiple buffers, thus letting us skip that
3499 		 * handling in the fast-path.
3500 		 */
3501 		chain_len = 1;
3502 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3503 						 MEM_TYPE_XSK_BUFF_POOL,
3504 						 NULL);
3505 		if (ret)
3506 			return ret;
3507 		dev_info(&vsi->back->pdev->dev,
3508 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3509 			 ring->queue_index);
3510 
3511 	} else {
3512 		ret = i40e_alloc_rx_bi(ring);
3513 		if (ret)
3514 			return ret;
3515 		ring->rx_buf_len = vsi->rx_buf_len;
3516 		if (ring->vsi->type == I40E_VSI_MAIN) {
3517 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3518 							 MEM_TYPE_PAGE_SHARED,
3519 							 NULL);
3520 			if (ret)
3521 				return ret;
3522 		}
3523 	}
3524 
3525 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3526 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3527 
3528 	rx_ctx.base = (ring->dma / 128);
3529 	rx_ctx.qlen = ring->count;
3530 
3531 	/* use 16 byte descriptors */
3532 	rx_ctx.dsize = 0;
3533 
3534 	/* descriptor type is always zero
3535 	 * rx_ctx.dtype = 0;
3536 	 */
3537 	rx_ctx.hsplit_0 = 0;
3538 
3539 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3540 	if (hw->revision_id == 0)
3541 		rx_ctx.lrxqthresh = 0;
3542 	else
3543 		rx_ctx.lrxqthresh = 1;
3544 	rx_ctx.crcstrip = 1;
3545 	rx_ctx.l2tsel = 1;
3546 	/* this controls whether VLAN is stripped from inner headers */
3547 	rx_ctx.showiv = 0;
3548 	/* set the prefena field to 1 because the manual says to */
3549 	rx_ctx.prefena = 1;
3550 
3551 	/* clear the context in the HMC */
3552 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3553 	if (err) {
3554 		dev_info(&vsi->back->pdev->dev,
3555 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3556 			 ring->queue_index, pf_q, err);
3557 		return -ENOMEM;
3558 	}
3559 
3560 	/* set the context in the HMC */
3561 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3562 	if (err) {
3563 		dev_info(&vsi->back->pdev->dev,
3564 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3565 			 ring->queue_index, pf_q, err);
3566 		return -ENOMEM;
3567 	}
3568 
3569 	/* configure Rx buffer alignment */
3570 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3571 		clear_ring_build_skb_enabled(ring);
3572 	else
3573 		set_ring_build_skb_enabled(ring);
3574 
3575 	ring->rx_offset = i40e_rx_offset(ring);
3576 
3577 	/* cache tail for quicker writes, and clear the reg before use */
3578 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3579 	writel(0, ring->tail);
3580 
3581 	if (ring->xsk_pool) {
3582 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3583 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3584 	} else {
3585 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3586 	}
3587 	if (!ok) {
3588 		/* Log this in case the user has forgotten to give the kernel
3589 		 * any buffers, even later in the application.
3590 		 */
3591 		dev_info(&vsi->back->pdev->dev,
3592 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3593 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3594 			 ring->queue_index, pf_q);
3595 	}
3596 
3597 	return 0;
3598 }
3599 
3600 /**
3601  * i40e_vsi_configure_tx - Configure the VSI for Tx
3602  * @vsi: VSI structure describing this set of rings and resources
3603  *
3604  * Configure the Tx VSI for operation.
3605  **/
3606 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3607 {
3608 	int err = 0;
3609 	u16 i;
3610 
3611 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3612 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3613 
3614 	if (err || !i40e_enabled_xdp_vsi(vsi))
3615 		return err;
3616 
3617 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3618 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3619 
3620 	return err;
3621 }
3622 
3623 /**
3624  * i40e_vsi_configure_rx - Configure the VSI for Rx
3625  * @vsi: the VSI being configured
3626  *
3627  * Configure the Rx VSI for operation.
3628  **/
3629 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3630 {
3631 	int err = 0;
3632 	u16 i;
3633 
3634 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3635 		vsi->max_frame = I40E_MAX_RXBUFFER;
3636 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3637 #if (PAGE_SIZE < 8192)
3638 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3639 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3640 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3641 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3642 #endif
3643 	} else {
3644 		vsi->max_frame = I40E_MAX_RXBUFFER;
3645 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3646 						       I40E_RXBUFFER_2048;
3647 	}
3648 
3649 	/* set up individual rings */
3650 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3651 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3652 
3653 	return err;
3654 }
3655 
3656 /**
3657  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3658  * @vsi: ptr to the VSI
3659  **/
3660 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3661 {
3662 	struct i40e_ring *tx_ring, *rx_ring;
3663 	u16 qoffset, qcount;
3664 	int i, n;
3665 
3666 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3667 		/* Reset the TC information */
3668 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3669 			rx_ring = vsi->rx_rings[i];
3670 			tx_ring = vsi->tx_rings[i];
3671 			rx_ring->dcb_tc = 0;
3672 			tx_ring->dcb_tc = 0;
3673 		}
3674 		return;
3675 	}
3676 
3677 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3678 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3679 			continue;
3680 
3681 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3682 		qcount = vsi->tc_config.tc_info[n].qcount;
3683 		for (i = qoffset; i < (qoffset + qcount); i++) {
3684 			rx_ring = vsi->rx_rings[i];
3685 			tx_ring = vsi->tx_rings[i];
3686 			rx_ring->dcb_tc = n;
3687 			tx_ring->dcb_tc = n;
3688 		}
3689 	}
3690 }
3691 
3692 /**
3693  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3694  * @vsi: ptr to the VSI
3695  **/
3696 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3697 {
3698 	if (vsi->netdev)
3699 		i40e_set_rx_mode(vsi->netdev);
3700 }
3701 
3702 /**
3703  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3704  * @pf: Pointer to the targeted PF
3705  *
3706  * Set all flow director counters to 0.
3707  */
3708 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3709 {
3710 	pf->fd_tcp4_filter_cnt = 0;
3711 	pf->fd_udp4_filter_cnt = 0;
3712 	pf->fd_sctp4_filter_cnt = 0;
3713 	pf->fd_ip4_filter_cnt = 0;
3714 	pf->fd_tcp6_filter_cnt = 0;
3715 	pf->fd_udp6_filter_cnt = 0;
3716 	pf->fd_sctp6_filter_cnt = 0;
3717 	pf->fd_ip6_filter_cnt = 0;
3718 }
3719 
3720 /**
3721  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3722  * @vsi: Pointer to the targeted VSI
3723  *
3724  * This function replays the hlist on the hw where all the SB Flow Director
3725  * filters were saved.
3726  **/
3727 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3728 {
3729 	struct i40e_fdir_filter *filter;
3730 	struct i40e_pf *pf = vsi->back;
3731 	struct hlist_node *node;
3732 
3733 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3734 		return;
3735 
3736 	/* Reset FDir counters as we're replaying all existing filters */
3737 	i40e_reset_fdir_filter_cnt(pf);
3738 
3739 	hlist_for_each_entry_safe(filter, node,
3740 				  &pf->fdir_filter_list, fdir_node) {
3741 		i40e_add_del_fdir(vsi, filter, true);
3742 	}
3743 }
3744 
3745 /**
3746  * i40e_vsi_configure - Set up the VSI for action
3747  * @vsi: the VSI being configured
3748  **/
3749 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3750 {
3751 	int err;
3752 
3753 	i40e_set_vsi_rx_mode(vsi);
3754 	i40e_restore_vlan(vsi);
3755 	i40e_vsi_config_dcb_rings(vsi);
3756 	err = i40e_vsi_configure_tx(vsi);
3757 	if (!err)
3758 		err = i40e_vsi_configure_rx(vsi);
3759 
3760 	return err;
3761 }
3762 
3763 /**
3764  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3765  * @vsi: the VSI being configured
3766  **/
3767 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3768 {
3769 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3770 	struct i40e_pf *pf = vsi->back;
3771 	struct i40e_hw *hw = &pf->hw;
3772 	u16 vector;
3773 	int i, q;
3774 	u32 qp;
3775 
3776 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3777 	 * and PFINT_LNKLSTn registers, e.g.:
3778 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3779 	 */
3780 	qp = vsi->base_queue;
3781 	vector = vsi->base_vector;
3782 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3783 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3784 
3785 		q_vector->rx.next_update = jiffies + 1;
3786 		q_vector->rx.target_itr =
3787 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3788 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3789 		     q_vector->rx.target_itr >> 1);
3790 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3791 
3792 		q_vector->tx.next_update = jiffies + 1;
3793 		q_vector->tx.target_itr =
3794 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3795 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3796 		     q_vector->tx.target_itr >> 1);
3797 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3798 
3799 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3800 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3801 
3802 		/* Linked list for the queuepairs assigned to this vector */
3803 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3804 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3805 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3806 			u32 val;
3807 
3808 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3809 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3810 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3811 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3812 			      (I40E_QUEUE_TYPE_TX <<
3813 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3814 
3815 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3816 
3817 			if (has_xdp) {
3818 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3819 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3820 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3821 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3822 				      (I40E_QUEUE_TYPE_TX <<
3823 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3824 
3825 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3826 			}
3827 
3828 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3829 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3830 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3831 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3832 			      (I40E_QUEUE_TYPE_RX <<
3833 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3834 
3835 			/* Terminate the linked list */
3836 			if (q == (q_vector->num_ringpairs - 1))
3837 				val |= (I40E_QUEUE_END_OF_LIST <<
3838 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3839 
3840 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3841 			qp++;
3842 		}
3843 	}
3844 
3845 	i40e_flush(hw);
3846 }
3847 
3848 /**
3849  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3850  * @pf: pointer to private device data structure
3851  **/
3852 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3853 {
3854 	struct i40e_hw *hw = &pf->hw;
3855 	u32 val;
3856 
3857 	/* clear things first */
3858 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3859 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3860 
3861 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3862 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3863 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3864 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3865 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3866 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3867 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3868 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3869 
3870 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3871 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3872 
3873 	if (pf->flags & I40E_FLAG_PTP)
3874 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3875 
3876 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3877 
3878 	/* SW_ITR_IDX = 0, but don't change INTENA */
3879 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3880 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3881 
3882 	/* OTHER_ITR_IDX = 0 */
3883 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3884 }
3885 
3886 /**
3887  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3888  * @vsi: the VSI being configured
3889  **/
3890 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3891 {
3892 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3893 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3894 	struct i40e_pf *pf = vsi->back;
3895 	struct i40e_hw *hw = &pf->hw;
3896 	u32 val;
3897 
3898 	/* set the ITR configuration */
3899 	q_vector->rx.next_update = jiffies + 1;
3900 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3901 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3902 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3903 	q_vector->tx.next_update = jiffies + 1;
3904 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3905 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3906 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3907 
3908 	i40e_enable_misc_int_causes(pf);
3909 
3910 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3911 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3912 
3913 	/* Associate the queue pair to the vector and enable the queue int */
3914 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3915 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3916 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3917 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3918 
3919 	wr32(hw, I40E_QINT_RQCTL(0), val);
3920 
3921 	if (i40e_enabled_xdp_vsi(vsi)) {
3922 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
3923 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3924 		      (I40E_QUEUE_TYPE_TX
3925 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3926 
3927 		wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3928 	}
3929 
3930 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
3931 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3932 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3933 
3934 	wr32(hw, I40E_QINT_TQCTL(0), val);
3935 	i40e_flush(hw);
3936 }
3937 
3938 /**
3939  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3940  * @pf: board private structure
3941  **/
3942 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3943 {
3944 	struct i40e_hw *hw = &pf->hw;
3945 
3946 	wr32(hw, I40E_PFINT_DYN_CTL0,
3947 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3948 	i40e_flush(hw);
3949 }
3950 
3951 /**
3952  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3953  * @pf: board private structure
3954  **/
3955 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3956 {
3957 	struct i40e_hw *hw = &pf->hw;
3958 	u32 val;
3959 
3960 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
3961 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3962 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3963 
3964 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
3965 	i40e_flush(hw);
3966 }
3967 
3968 /**
3969  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3970  * @irq: interrupt number
3971  * @data: pointer to a q_vector
3972  **/
3973 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3974 {
3975 	struct i40e_q_vector *q_vector = data;
3976 
3977 	if (!q_vector->tx.ring && !q_vector->rx.ring)
3978 		return IRQ_HANDLED;
3979 
3980 	napi_schedule_irqoff(&q_vector->napi);
3981 
3982 	return IRQ_HANDLED;
3983 }
3984 
3985 /**
3986  * i40e_irq_affinity_notify - Callback for affinity changes
3987  * @notify: context as to what irq was changed
3988  * @mask: the new affinity mask
3989  *
3990  * This is a callback function used by the irq_set_affinity_notifier function
3991  * so that we may register to receive changes to the irq affinity masks.
3992  **/
3993 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3994 				     const cpumask_t *mask)
3995 {
3996 	struct i40e_q_vector *q_vector =
3997 		container_of(notify, struct i40e_q_vector, affinity_notify);
3998 
3999 	cpumask_copy(&q_vector->affinity_mask, mask);
4000 }
4001 
4002 /**
4003  * i40e_irq_affinity_release - Callback for affinity notifier release
4004  * @ref: internal core kernel usage
4005  *
4006  * This is a callback function used by the irq_set_affinity_notifier function
4007  * to inform the current notification subscriber that they will no longer
4008  * receive notifications.
4009  **/
4010 static void i40e_irq_affinity_release(struct kref *ref) {}
4011 
4012 /**
4013  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4014  * @vsi: the VSI being configured
4015  * @basename: name for the vector
4016  *
4017  * Allocates MSI-X vectors and requests interrupts from the kernel.
4018  **/
4019 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4020 {
4021 	int q_vectors = vsi->num_q_vectors;
4022 	struct i40e_pf *pf = vsi->back;
4023 	int base = vsi->base_vector;
4024 	int rx_int_idx = 0;
4025 	int tx_int_idx = 0;
4026 	int vector, err;
4027 	int irq_num;
4028 	int cpu;
4029 
4030 	for (vector = 0; vector < q_vectors; vector++) {
4031 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4032 
4033 		irq_num = pf->msix_entries[base + vector].vector;
4034 
4035 		if (q_vector->tx.ring && q_vector->rx.ring) {
4036 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4037 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4038 			tx_int_idx++;
4039 		} else if (q_vector->rx.ring) {
4040 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4041 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4042 		} else if (q_vector->tx.ring) {
4043 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4044 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4045 		} else {
4046 			/* skip this unused q_vector */
4047 			continue;
4048 		}
4049 		err = request_irq(irq_num,
4050 				  vsi->irq_handler,
4051 				  0,
4052 				  q_vector->name,
4053 				  q_vector);
4054 		if (err) {
4055 			dev_info(&pf->pdev->dev,
4056 				 "MSIX request_irq failed, error: %d\n", err);
4057 			goto free_queue_irqs;
4058 		}
4059 
4060 		/* register for affinity change notifications */
4061 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4062 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4063 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4064 		/* Spread affinity hints out across online CPUs.
4065 		 *
4066 		 * get_cpu_mask returns a static constant mask with
4067 		 * a permanent lifetime so it's ok to pass to
4068 		 * irq_update_affinity_hint without making a copy.
4069 		 */
4070 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4071 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4072 	}
4073 
4074 	vsi->irqs_ready = true;
4075 	return 0;
4076 
4077 free_queue_irqs:
4078 	while (vector) {
4079 		vector--;
4080 		irq_num = pf->msix_entries[base + vector].vector;
4081 		irq_set_affinity_notifier(irq_num, NULL);
4082 		irq_update_affinity_hint(irq_num, NULL);
4083 		free_irq(irq_num, &vsi->q_vectors[vector]);
4084 	}
4085 	return err;
4086 }
4087 
4088 /**
4089  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4090  * @vsi: the VSI being un-configured
4091  **/
4092 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4093 {
4094 	struct i40e_pf *pf = vsi->back;
4095 	struct i40e_hw *hw = &pf->hw;
4096 	int base = vsi->base_vector;
4097 	int i;
4098 
4099 	/* disable interrupt causation from each queue */
4100 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4101 		u32 val;
4102 
4103 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4104 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4105 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4106 
4107 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4108 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4109 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4110 
4111 		if (!i40e_enabled_xdp_vsi(vsi))
4112 			continue;
4113 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4114 	}
4115 
4116 	/* disable each interrupt */
4117 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4118 		for (i = vsi->base_vector;
4119 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4120 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4121 
4122 		i40e_flush(hw);
4123 		for (i = 0; i < vsi->num_q_vectors; i++)
4124 			synchronize_irq(pf->msix_entries[i + base].vector);
4125 	} else {
4126 		/* Legacy and MSI mode - this stops all interrupt handling */
4127 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4128 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4129 		i40e_flush(hw);
4130 		synchronize_irq(pf->pdev->irq);
4131 	}
4132 }
4133 
4134 /**
4135  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4136  * @vsi: the VSI being configured
4137  **/
4138 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4139 {
4140 	struct i40e_pf *pf = vsi->back;
4141 	int i;
4142 
4143 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4144 		for (i = 0; i < vsi->num_q_vectors; i++)
4145 			i40e_irq_dynamic_enable(vsi, i);
4146 	} else {
4147 		i40e_irq_dynamic_enable_icr0(pf);
4148 	}
4149 
4150 	i40e_flush(&pf->hw);
4151 	return 0;
4152 }
4153 
4154 /**
4155  * i40e_free_misc_vector - Free the vector that handles non-queue events
4156  * @pf: board private structure
4157  **/
4158 static void i40e_free_misc_vector(struct i40e_pf *pf)
4159 {
4160 	/* Disable ICR 0 */
4161 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4162 	i40e_flush(&pf->hw);
4163 
4164 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4165 		synchronize_irq(pf->msix_entries[0].vector);
4166 		free_irq(pf->msix_entries[0].vector, pf);
4167 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4168 	}
4169 }
4170 
4171 /**
4172  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4173  * @irq: interrupt number
4174  * @data: pointer to a q_vector
4175  *
4176  * This is the handler used for all MSI/Legacy interrupts, and deals
4177  * with both queue and non-queue interrupts.  This is also used in
4178  * MSIX mode to handle the non-queue interrupts.
4179  **/
4180 static irqreturn_t i40e_intr(int irq, void *data)
4181 {
4182 	struct i40e_pf *pf = (struct i40e_pf *)data;
4183 	struct i40e_hw *hw = &pf->hw;
4184 	irqreturn_t ret = IRQ_NONE;
4185 	u32 icr0, icr0_remaining;
4186 	u32 val, ena_mask;
4187 
4188 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4189 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4190 
4191 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4192 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4193 		goto enable_intr;
4194 
4195 	/* if interrupt but no bits showing, must be SWINT */
4196 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4197 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4198 		pf->sw_int_count++;
4199 
4200 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4201 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4202 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4203 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4204 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4205 	}
4206 
4207 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4208 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4209 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4210 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4211 
4212 		/* We do not have a way to disarm Queue causes while leaving
4213 		 * interrupt enabled for all other causes, ideally
4214 		 * interrupt should be disabled while we are in NAPI but
4215 		 * this is not a performance path and napi_schedule()
4216 		 * can deal with rescheduling.
4217 		 */
4218 		if (!test_bit(__I40E_DOWN, pf->state))
4219 			napi_schedule_irqoff(&q_vector->napi);
4220 	}
4221 
4222 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4223 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4224 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4225 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4226 	}
4227 
4228 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4229 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4230 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4231 	}
4232 
4233 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4234 		/* disable any further VFLR event notifications */
4235 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4236 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4237 
4238 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4239 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4240 		} else {
4241 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4242 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4243 		}
4244 	}
4245 
4246 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4247 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4248 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4249 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4250 		val = rd32(hw, I40E_GLGEN_RSTAT);
4251 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4252 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4253 		if (val == I40E_RESET_CORER) {
4254 			pf->corer_count++;
4255 		} else if (val == I40E_RESET_GLOBR) {
4256 			pf->globr_count++;
4257 		} else if (val == I40E_RESET_EMPR) {
4258 			pf->empr_count++;
4259 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4260 		}
4261 	}
4262 
4263 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4264 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4265 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4266 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4267 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4268 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4269 	}
4270 
4271 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4272 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4273 
4274 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4275 			schedule_work(&pf->ptp_extts0_work);
4276 
4277 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4278 			i40e_ptp_tx_hwtstamp(pf);
4279 
4280 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4281 	}
4282 
4283 	/* If a critical error is pending we have no choice but to reset the
4284 	 * device.
4285 	 * Report and mask out any remaining unexpected interrupts.
4286 	 */
4287 	icr0_remaining = icr0 & ena_mask;
4288 	if (icr0_remaining) {
4289 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4290 			 icr0_remaining);
4291 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4292 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4293 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4294 			dev_info(&pf->pdev->dev, "device will be reset\n");
4295 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4296 			i40e_service_event_schedule(pf);
4297 		}
4298 		ena_mask &= ~icr0_remaining;
4299 	}
4300 	ret = IRQ_HANDLED;
4301 
4302 enable_intr:
4303 	/* re-enable interrupt causes */
4304 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4305 	if (!test_bit(__I40E_DOWN, pf->state) ||
4306 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4307 		i40e_service_event_schedule(pf);
4308 		i40e_irq_dynamic_enable_icr0(pf);
4309 	}
4310 
4311 	return ret;
4312 }
4313 
4314 /**
4315  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4316  * @tx_ring:  tx ring to clean
4317  * @budget:   how many cleans we're allowed
4318  *
4319  * Returns true if there's any budget left (e.g. the clean is finished)
4320  **/
4321 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4322 {
4323 	struct i40e_vsi *vsi = tx_ring->vsi;
4324 	u16 i = tx_ring->next_to_clean;
4325 	struct i40e_tx_buffer *tx_buf;
4326 	struct i40e_tx_desc *tx_desc;
4327 
4328 	tx_buf = &tx_ring->tx_bi[i];
4329 	tx_desc = I40E_TX_DESC(tx_ring, i);
4330 	i -= tx_ring->count;
4331 
4332 	do {
4333 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4334 
4335 		/* if next_to_watch is not set then there is no work pending */
4336 		if (!eop_desc)
4337 			break;
4338 
4339 		/* prevent any other reads prior to eop_desc */
4340 		smp_rmb();
4341 
4342 		/* if the descriptor isn't done, no work yet to do */
4343 		if (!(eop_desc->cmd_type_offset_bsz &
4344 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4345 			break;
4346 
4347 		/* clear next_to_watch to prevent false hangs */
4348 		tx_buf->next_to_watch = NULL;
4349 
4350 		tx_desc->buffer_addr = 0;
4351 		tx_desc->cmd_type_offset_bsz = 0;
4352 		/* move past filter desc */
4353 		tx_buf++;
4354 		tx_desc++;
4355 		i++;
4356 		if (unlikely(!i)) {
4357 			i -= tx_ring->count;
4358 			tx_buf = tx_ring->tx_bi;
4359 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4360 		}
4361 		/* unmap skb header data */
4362 		dma_unmap_single(tx_ring->dev,
4363 				 dma_unmap_addr(tx_buf, dma),
4364 				 dma_unmap_len(tx_buf, len),
4365 				 DMA_TO_DEVICE);
4366 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4367 			kfree(tx_buf->raw_buf);
4368 
4369 		tx_buf->raw_buf = NULL;
4370 		tx_buf->tx_flags = 0;
4371 		tx_buf->next_to_watch = NULL;
4372 		dma_unmap_len_set(tx_buf, len, 0);
4373 		tx_desc->buffer_addr = 0;
4374 		tx_desc->cmd_type_offset_bsz = 0;
4375 
4376 		/* move us past the eop_desc for start of next FD desc */
4377 		tx_buf++;
4378 		tx_desc++;
4379 		i++;
4380 		if (unlikely(!i)) {
4381 			i -= tx_ring->count;
4382 			tx_buf = tx_ring->tx_bi;
4383 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4384 		}
4385 
4386 		/* update budget accounting */
4387 		budget--;
4388 	} while (likely(budget));
4389 
4390 	i += tx_ring->count;
4391 	tx_ring->next_to_clean = i;
4392 
4393 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4394 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4395 
4396 	return budget > 0;
4397 }
4398 
4399 /**
4400  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4401  * @irq: interrupt number
4402  * @data: pointer to a q_vector
4403  **/
4404 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4405 {
4406 	struct i40e_q_vector *q_vector = data;
4407 	struct i40e_vsi *vsi;
4408 
4409 	if (!q_vector->tx.ring)
4410 		return IRQ_HANDLED;
4411 
4412 	vsi = q_vector->tx.ring->vsi;
4413 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4414 
4415 	return IRQ_HANDLED;
4416 }
4417 
4418 /**
4419  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4420  * @vsi: the VSI being configured
4421  * @v_idx: vector index
4422  * @qp_idx: queue pair index
4423  **/
4424 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4425 {
4426 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4427 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4428 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4429 
4430 	tx_ring->q_vector = q_vector;
4431 	tx_ring->next = q_vector->tx.ring;
4432 	q_vector->tx.ring = tx_ring;
4433 	q_vector->tx.count++;
4434 
4435 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4436 	if (i40e_enabled_xdp_vsi(vsi)) {
4437 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4438 
4439 		xdp_ring->q_vector = q_vector;
4440 		xdp_ring->next = q_vector->tx.ring;
4441 		q_vector->tx.ring = xdp_ring;
4442 		q_vector->tx.count++;
4443 	}
4444 
4445 	rx_ring->q_vector = q_vector;
4446 	rx_ring->next = q_vector->rx.ring;
4447 	q_vector->rx.ring = rx_ring;
4448 	q_vector->rx.count++;
4449 }
4450 
4451 /**
4452  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4453  * @vsi: the VSI being configured
4454  *
4455  * This function maps descriptor rings to the queue-specific vectors
4456  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4457  * one vector per queue pair, but on a constrained vector budget, we
4458  * group the queue pairs as "efficiently" as possible.
4459  **/
4460 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4461 {
4462 	int qp_remaining = vsi->num_queue_pairs;
4463 	int q_vectors = vsi->num_q_vectors;
4464 	int num_ringpairs;
4465 	int v_start = 0;
4466 	int qp_idx = 0;
4467 
4468 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4469 	 * group them so there are multiple queues per vector.
4470 	 * It is also important to go through all the vectors available to be
4471 	 * sure that if we don't use all the vectors, that the remaining vectors
4472 	 * are cleared. This is especially important when decreasing the
4473 	 * number of queues in use.
4474 	 */
4475 	for (; v_start < q_vectors; v_start++) {
4476 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4477 
4478 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4479 
4480 		q_vector->num_ringpairs = num_ringpairs;
4481 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4482 
4483 		q_vector->rx.count = 0;
4484 		q_vector->tx.count = 0;
4485 		q_vector->rx.ring = NULL;
4486 		q_vector->tx.ring = NULL;
4487 
4488 		while (num_ringpairs--) {
4489 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4490 			qp_idx++;
4491 			qp_remaining--;
4492 		}
4493 	}
4494 }
4495 
4496 /**
4497  * i40e_vsi_request_irq - Request IRQ from the OS
4498  * @vsi: the VSI being configured
4499  * @basename: name for the vector
4500  **/
4501 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4502 {
4503 	struct i40e_pf *pf = vsi->back;
4504 	int err;
4505 
4506 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4507 		err = i40e_vsi_request_irq_msix(vsi, basename);
4508 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4509 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4510 				  pf->int_name, pf);
4511 	else
4512 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4513 				  pf->int_name, pf);
4514 
4515 	if (err)
4516 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4517 
4518 	return err;
4519 }
4520 
4521 #ifdef CONFIG_NET_POLL_CONTROLLER
4522 /**
4523  * i40e_netpoll - A Polling 'interrupt' handler
4524  * @netdev: network interface device structure
4525  *
4526  * This is used by netconsole to send skbs without having to re-enable
4527  * interrupts.  It's not called while the normal interrupt routine is executing.
4528  **/
4529 static void i40e_netpoll(struct net_device *netdev)
4530 {
4531 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4532 	struct i40e_vsi *vsi = np->vsi;
4533 	struct i40e_pf *pf = vsi->back;
4534 	int i;
4535 
4536 	/* if interface is down do nothing */
4537 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4538 		return;
4539 
4540 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4541 		for (i = 0; i < vsi->num_q_vectors; i++)
4542 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4543 	} else {
4544 		i40e_intr(pf->pdev->irq, netdev);
4545 	}
4546 }
4547 #endif
4548 
4549 #define I40E_QTX_ENA_WAIT_COUNT 50
4550 
4551 /**
4552  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4553  * @pf: the PF being configured
4554  * @pf_q: the PF queue
4555  * @enable: enable or disable state of the queue
4556  *
4557  * This routine will wait for the given Tx queue of the PF to reach the
4558  * enabled or disabled state.
4559  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4560  * multiple retries; else will return 0 in case of success.
4561  **/
4562 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4563 {
4564 	int i;
4565 	u32 tx_reg;
4566 
4567 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4568 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4569 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4570 			break;
4571 
4572 		usleep_range(10, 20);
4573 	}
4574 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4575 		return -ETIMEDOUT;
4576 
4577 	return 0;
4578 }
4579 
4580 /**
4581  * i40e_control_tx_q - Start or stop a particular Tx queue
4582  * @pf: the PF structure
4583  * @pf_q: the PF queue to configure
4584  * @enable: start or stop the queue
4585  *
4586  * This function enables or disables a single queue. Note that any delay
4587  * required after the operation is expected to be handled by the caller of
4588  * this function.
4589  **/
4590 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4591 {
4592 	struct i40e_hw *hw = &pf->hw;
4593 	u32 tx_reg;
4594 	int i;
4595 
4596 	/* warn the TX unit of coming changes */
4597 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4598 	if (!enable)
4599 		usleep_range(10, 20);
4600 
4601 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4602 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4603 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4604 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4605 			break;
4606 		usleep_range(1000, 2000);
4607 	}
4608 
4609 	/* Skip if the queue is already in the requested state */
4610 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4611 		return;
4612 
4613 	/* turn on/off the queue */
4614 	if (enable) {
4615 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4616 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4617 	} else {
4618 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4619 	}
4620 
4621 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4622 }
4623 
4624 /**
4625  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4626  * @seid: VSI SEID
4627  * @pf: the PF structure
4628  * @pf_q: the PF queue to configure
4629  * @is_xdp: true if the queue is used for XDP
4630  * @enable: start or stop the queue
4631  **/
4632 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4633 			   bool is_xdp, bool enable)
4634 {
4635 	int ret;
4636 
4637 	i40e_control_tx_q(pf, pf_q, enable);
4638 
4639 	/* wait for the change to finish */
4640 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4641 	if (ret) {
4642 		dev_info(&pf->pdev->dev,
4643 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4644 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4645 			 (enable ? "en" : "dis"));
4646 	}
4647 
4648 	return ret;
4649 }
4650 
4651 /**
4652  * i40e_vsi_enable_tx - Start a VSI's rings
4653  * @vsi: the VSI being configured
4654  **/
4655 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4656 {
4657 	struct i40e_pf *pf = vsi->back;
4658 	int i, pf_q, ret = 0;
4659 
4660 	pf_q = vsi->base_queue;
4661 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4662 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4663 					     pf_q,
4664 					     false /*is xdp*/, true);
4665 		if (ret)
4666 			break;
4667 
4668 		if (!i40e_enabled_xdp_vsi(vsi))
4669 			continue;
4670 
4671 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4672 					     pf_q + vsi->alloc_queue_pairs,
4673 					     true /*is xdp*/, true);
4674 		if (ret)
4675 			break;
4676 	}
4677 	return ret;
4678 }
4679 
4680 /**
4681  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4682  * @pf: the PF being configured
4683  * @pf_q: the PF queue
4684  * @enable: enable or disable state of the queue
4685  *
4686  * This routine will wait for the given Rx queue of the PF to reach the
4687  * enabled or disabled state.
4688  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4689  * multiple retries; else will return 0 in case of success.
4690  **/
4691 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4692 {
4693 	int i;
4694 	u32 rx_reg;
4695 
4696 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4697 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4698 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4699 			break;
4700 
4701 		usleep_range(10, 20);
4702 	}
4703 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4704 		return -ETIMEDOUT;
4705 
4706 	return 0;
4707 }
4708 
4709 /**
4710  * i40e_control_rx_q - Start or stop a particular Rx queue
4711  * @pf: the PF structure
4712  * @pf_q: the PF queue to configure
4713  * @enable: start or stop the queue
4714  *
4715  * This function enables or disables a single queue. Note that
4716  * any delay required after the operation is expected to be
4717  * handled by the caller of this function.
4718  **/
4719 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4720 {
4721 	struct i40e_hw *hw = &pf->hw;
4722 	u32 rx_reg;
4723 	int i;
4724 
4725 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4726 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4727 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4728 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4729 			break;
4730 		usleep_range(1000, 2000);
4731 	}
4732 
4733 	/* Skip if the queue is already in the requested state */
4734 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4735 		return;
4736 
4737 	/* turn on/off the queue */
4738 	if (enable)
4739 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4740 	else
4741 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4742 
4743 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4744 }
4745 
4746 /**
4747  * i40e_control_wait_rx_q
4748  * @pf: the PF structure
4749  * @pf_q: queue being configured
4750  * @enable: start or stop the rings
4751  *
4752  * This function enables or disables a single queue along with waiting
4753  * for the change to finish. The caller of this function should handle
4754  * the delays needed in the case of disabling queues.
4755  **/
4756 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4757 {
4758 	int ret = 0;
4759 
4760 	i40e_control_rx_q(pf, pf_q, enable);
4761 
4762 	/* wait for the change to finish */
4763 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4764 	if (ret)
4765 		return ret;
4766 
4767 	return ret;
4768 }
4769 
4770 /**
4771  * i40e_vsi_enable_rx - Start a VSI's rings
4772  * @vsi: the VSI being configured
4773  **/
4774 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4775 {
4776 	struct i40e_pf *pf = vsi->back;
4777 	int i, pf_q, ret = 0;
4778 
4779 	pf_q = vsi->base_queue;
4780 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4781 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4782 		if (ret) {
4783 			dev_info(&pf->pdev->dev,
4784 				 "VSI seid %d Rx ring %d enable timeout\n",
4785 				 vsi->seid, pf_q);
4786 			break;
4787 		}
4788 	}
4789 
4790 	return ret;
4791 }
4792 
4793 /**
4794  * i40e_vsi_start_rings - Start a VSI's rings
4795  * @vsi: the VSI being configured
4796  **/
4797 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4798 {
4799 	int ret = 0;
4800 
4801 	/* do rx first for enable and last for disable */
4802 	ret = i40e_vsi_enable_rx(vsi);
4803 	if (ret)
4804 		return ret;
4805 	ret = i40e_vsi_enable_tx(vsi);
4806 
4807 	return ret;
4808 }
4809 
4810 #define I40E_DISABLE_TX_GAP_MSEC	50
4811 
4812 /**
4813  * i40e_vsi_stop_rings - Stop a VSI's rings
4814  * @vsi: the VSI being configured
4815  **/
4816 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4817 {
4818 	struct i40e_pf *pf = vsi->back;
4819 	int pf_q, err, q_end;
4820 
4821 	/* When port TX is suspended, don't wait */
4822 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4823 		return i40e_vsi_stop_rings_no_wait(vsi);
4824 
4825 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4826 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4827 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4828 
4829 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4830 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4831 		if (err)
4832 			dev_info(&pf->pdev->dev,
4833 				 "VSI seid %d Rx ring %d disable timeout\n",
4834 				 vsi->seid, pf_q);
4835 	}
4836 
4837 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4838 	pf_q = vsi->base_queue;
4839 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4840 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4841 
4842 	i40e_vsi_wait_queues_disabled(vsi);
4843 }
4844 
4845 /**
4846  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4847  * @vsi: the VSI being shutdown
4848  *
4849  * This function stops all the rings for a VSI but does not delay to verify
4850  * that rings have been disabled. It is expected that the caller is shutting
4851  * down multiple VSIs at once and will delay together for all the VSIs after
4852  * initiating the shutdown. This is particularly useful for shutting down lots
4853  * of VFs together. Otherwise, a large delay can be incurred while configuring
4854  * each VSI in serial.
4855  **/
4856 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4857 {
4858 	struct i40e_pf *pf = vsi->back;
4859 	int i, pf_q;
4860 
4861 	pf_q = vsi->base_queue;
4862 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4863 		i40e_control_tx_q(pf, pf_q, false);
4864 		i40e_control_rx_q(pf, pf_q, false);
4865 	}
4866 }
4867 
4868 /**
4869  * i40e_vsi_free_irq - Free the irq association with the OS
4870  * @vsi: the VSI being configured
4871  **/
4872 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4873 {
4874 	struct i40e_pf *pf = vsi->back;
4875 	struct i40e_hw *hw = &pf->hw;
4876 	int base = vsi->base_vector;
4877 	u32 val, qp;
4878 	int i;
4879 
4880 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4881 		if (!vsi->q_vectors)
4882 			return;
4883 
4884 		if (!vsi->irqs_ready)
4885 			return;
4886 
4887 		vsi->irqs_ready = false;
4888 		for (i = 0; i < vsi->num_q_vectors; i++) {
4889 			int irq_num;
4890 			u16 vector;
4891 
4892 			vector = i + base;
4893 			irq_num = pf->msix_entries[vector].vector;
4894 
4895 			/* free only the irqs that were actually requested */
4896 			if (!vsi->q_vectors[i] ||
4897 			    !vsi->q_vectors[i]->num_ringpairs)
4898 				continue;
4899 
4900 			/* clear the affinity notifier in the IRQ descriptor */
4901 			irq_set_affinity_notifier(irq_num, NULL);
4902 			/* remove our suggested affinity mask for this IRQ */
4903 			irq_update_affinity_hint(irq_num, NULL);
4904 			synchronize_irq(irq_num);
4905 			free_irq(irq_num, vsi->q_vectors[i]);
4906 
4907 			/* Tear down the interrupt queue link list
4908 			 *
4909 			 * We know that they come in pairs and always
4910 			 * the Rx first, then the Tx.  To clear the
4911 			 * link list, stick the EOL value into the
4912 			 * next_q field of the registers.
4913 			 */
4914 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4915 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4916 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4917 			val |= I40E_QUEUE_END_OF_LIST
4918 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4919 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4920 
4921 			while (qp != I40E_QUEUE_END_OF_LIST) {
4922 				u32 next;
4923 
4924 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4925 
4926 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4927 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4928 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4929 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4930 
4931 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4932 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4933 
4934 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4935 
4936 				val = rd32(hw, I40E_QINT_TQCTL(qp));
4937 
4938 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4939 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4940 
4941 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4942 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4943 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4944 					 I40E_QINT_TQCTL_INTEVENT_MASK);
4945 
4946 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4947 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4948 
4949 				wr32(hw, I40E_QINT_TQCTL(qp), val);
4950 				qp = next;
4951 			}
4952 		}
4953 	} else {
4954 		free_irq(pf->pdev->irq, pf);
4955 
4956 		val = rd32(hw, I40E_PFINT_LNKLST0);
4957 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4958 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4959 		val |= I40E_QUEUE_END_OF_LIST
4960 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4961 		wr32(hw, I40E_PFINT_LNKLST0, val);
4962 
4963 		val = rd32(hw, I40E_QINT_RQCTL(qp));
4964 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4965 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4966 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4967 			 I40E_QINT_RQCTL_INTEVENT_MASK);
4968 
4969 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4970 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4971 
4972 		wr32(hw, I40E_QINT_RQCTL(qp), val);
4973 
4974 		val = rd32(hw, I40E_QINT_TQCTL(qp));
4975 
4976 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4977 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4978 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4979 			 I40E_QINT_TQCTL_INTEVENT_MASK);
4980 
4981 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4982 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4983 
4984 		wr32(hw, I40E_QINT_TQCTL(qp), val);
4985 	}
4986 }
4987 
4988 /**
4989  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4990  * @vsi: the VSI being configured
4991  * @v_idx: Index of vector to be freed
4992  *
4993  * This function frees the memory allocated to the q_vector.  In addition if
4994  * NAPI is enabled it will delete any references to the NAPI struct prior
4995  * to freeing the q_vector.
4996  **/
4997 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4998 {
4999 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5000 	struct i40e_ring *ring;
5001 
5002 	if (!q_vector)
5003 		return;
5004 
5005 	/* disassociate q_vector from rings */
5006 	i40e_for_each_ring(ring, q_vector->tx)
5007 		ring->q_vector = NULL;
5008 
5009 	i40e_for_each_ring(ring, q_vector->rx)
5010 		ring->q_vector = NULL;
5011 
5012 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5013 	if (vsi->netdev)
5014 		netif_napi_del(&q_vector->napi);
5015 
5016 	vsi->q_vectors[v_idx] = NULL;
5017 
5018 	kfree_rcu(q_vector, rcu);
5019 }
5020 
5021 /**
5022  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5023  * @vsi: the VSI being un-configured
5024  *
5025  * This frees the memory allocated to the q_vectors and
5026  * deletes references to the NAPI struct.
5027  **/
5028 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5029 {
5030 	int v_idx;
5031 
5032 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5033 		i40e_free_q_vector(vsi, v_idx);
5034 }
5035 
5036 /**
5037  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5038  * @pf: board private structure
5039  **/
5040 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5041 {
5042 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5043 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5044 		pci_disable_msix(pf->pdev);
5045 		kfree(pf->msix_entries);
5046 		pf->msix_entries = NULL;
5047 		kfree(pf->irq_pile);
5048 		pf->irq_pile = NULL;
5049 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
5050 		pci_disable_msi(pf->pdev);
5051 	}
5052 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
5053 }
5054 
5055 /**
5056  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5057  * @pf: board private structure
5058  *
5059  * We go through and clear interrupt specific resources and reset the structure
5060  * to pre-load conditions
5061  **/
5062 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5063 {
5064 	int i;
5065 
5066 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5067 		i40e_free_misc_vector(pf);
5068 
5069 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5070 		      I40E_IWARP_IRQ_PILE_ID);
5071 
5072 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5073 	for (i = 0; i < pf->num_alloc_vsi; i++)
5074 		if (pf->vsi[i])
5075 			i40e_vsi_free_q_vectors(pf->vsi[i]);
5076 	i40e_reset_interrupt_capability(pf);
5077 }
5078 
5079 /**
5080  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5081  * @vsi: the VSI being configured
5082  **/
5083 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5084 {
5085 	int q_idx;
5086 
5087 	if (!vsi->netdev)
5088 		return;
5089 
5090 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5091 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5092 
5093 		if (q_vector->rx.ring || q_vector->tx.ring)
5094 			napi_enable(&q_vector->napi);
5095 	}
5096 }
5097 
5098 /**
5099  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5100  * @vsi: the VSI being configured
5101  **/
5102 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5103 {
5104 	int q_idx;
5105 
5106 	if (!vsi->netdev)
5107 		return;
5108 
5109 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5110 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5111 
5112 		if (q_vector->rx.ring || q_vector->tx.ring)
5113 			napi_disable(&q_vector->napi);
5114 	}
5115 }
5116 
5117 /**
5118  * i40e_vsi_close - Shut down a VSI
5119  * @vsi: the vsi to be quelled
5120  **/
5121 static void i40e_vsi_close(struct i40e_vsi *vsi)
5122 {
5123 	struct i40e_pf *pf = vsi->back;
5124 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5125 		i40e_down(vsi);
5126 	i40e_vsi_free_irq(vsi);
5127 	i40e_vsi_free_tx_resources(vsi);
5128 	i40e_vsi_free_rx_resources(vsi);
5129 	vsi->current_netdev_flags = 0;
5130 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5131 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5132 		set_bit(__I40E_CLIENT_RESET, pf->state);
5133 }
5134 
5135 /**
5136  * i40e_quiesce_vsi - Pause a given VSI
5137  * @vsi: the VSI being paused
5138  **/
5139 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5140 {
5141 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5142 		return;
5143 
5144 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5145 	if (vsi->netdev && netif_running(vsi->netdev))
5146 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5147 	else
5148 		i40e_vsi_close(vsi);
5149 }
5150 
5151 /**
5152  * i40e_unquiesce_vsi - Resume a given VSI
5153  * @vsi: the VSI being resumed
5154  **/
5155 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5156 {
5157 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5158 		return;
5159 
5160 	if (vsi->netdev && netif_running(vsi->netdev))
5161 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5162 	else
5163 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5164 }
5165 
5166 /**
5167  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5168  * @pf: the PF
5169  **/
5170 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5171 {
5172 	int v;
5173 
5174 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5175 		if (pf->vsi[v])
5176 			i40e_quiesce_vsi(pf->vsi[v]);
5177 	}
5178 }
5179 
5180 /**
5181  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5182  * @pf: the PF
5183  **/
5184 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5185 {
5186 	int v;
5187 
5188 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5189 		if (pf->vsi[v])
5190 			i40e_unquiesce_vsi(pf->vsi[v]);
5191 	}
5192 }
5193 
5194 /**
5195  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5196  * @vsi: the VSI being configured
5197  *
5198  * Wait until all queues on a given VSI have been disabled.
5199  **/
5200 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5201 {
5202 	struct i40e_pf *pf = vsi->back;
5203 	int i, pf_q, ret;
5204 
5205 	pf_q = vsi->base_queue;
5206 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5207 		/* Check and wait for the Tx queue */
5208 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5209 		if (ret) {
5210 			dev_info(&pf->pdev->dev,
5211 				 "VSI seid %d Tx ring %d disable timeout\n",
5212 				 vsi->seid, pf_q);
5213 			return ret;
5214 		}
5215 
5216 		if (!i40e_enabled_xdp_vsi(vsi))
5217 			goto wait_rx;
5218 
5219 		/* Check and wait for the XDP Tx queue */
5220 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5221 				       false);
5222 		if (ret) {
5223 			dev_info(&pf->pdev->dev,
5224 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5225 				 vsi->seid, pf_q);
5226 			return ret;
5227 		}
5228 wait_rx:
5229 		/* Check and wait for the Rx queue */
5230 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5231 		if (ret) {
5232 			dev_info(&pf->pdev->dev,
5233 				 "VSI seid %d Rx ring %d disable timeout\n",
5234 				 vsi->seid, pf_q);
5235 			return ret;
5236 		}
5237 	}
5238 
5239 	return 0;
5240 }
5241 
5242 #ifdef CONFIG_I40E_DCB
5243 /**
5244  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5245  * @pf: the PF
5246  *
5247  * This function waits for the queues to be in disabled state for all the
5248  * VSIs that are managed by this PF.
5249  **/
5250 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5251 {
5252 	int v, ret = 0;
5253 
5254 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5255 		if (pf->vsi[v]) {
5256 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5257 			if (ret)
5258 				break;
5259 		}
5260 	}
5261 
5262 	return ret;
5263 }
5264 
5265 #endif
5266 
5267 /**
5268  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5269  * @pf: pointer to PF
5270  *
5271  * Get TC map for ISCSI PF type that will include iSCSI TC
5272  * and LAN TC.
5273  **/
5274 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5275 {
5276 	struct i40e_dcb_app_priority_table app;
5277 	struct i40e_hw *hw = &pf->hw;
5278 	u8 enabled_tc = 1; /* TC0 is always enabled */
5279 	u8 tc, i;
5280 	/* Get the iSCSI APP TLV */
5281 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5282 
5283 	for (i = 0; i < dcbcfg->numapps; i++) {
5284 		app = dcbcfg->app[i];
5285 		if (app.selector == I40E_APP_SEL_TCPIP &&
5286 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5287 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5288 			enabled_tc |= BIT(tc);
5289 			break;
5290 		}
5291 	}
5292 
5293 	return enabled_tc;
5294 }
5295 
5296 /**
5297  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5298  * @dcbcfg: the corresponding DCBx configuration structure
5299  *
5300  * Return the number of TCs from given DCBx configuration
5301  **/
5302 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5303 {
5304 	int i, tc_unused = 0;
5305 	u8 num_tc = 0;
5306 	u8 ret = 0;
5307 
5308 	/* Scan the ETS Config Priority Table to find
5309 	 * traffic class enabled for a given priority
5310 	 * and create a bitmask of enabled TCs
5311 	 */
5312 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5313 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5314 
5315 	/* Now scan the bitmask to check for
5316 	 * contiguous TCs starting with TC0
5317 	 */
5318 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5319 		if (num_tc & BIT(i)) {
5320 			if (!tc_unused) {
5321 				ret++;
5322 			} else {
5323 				pr_err("Non-contiguous TC - Disabling DCB\n");
5324 				return 1;
5325 			}
5326 		} else {
5327 			tc_unused = 1;
5328 		}
5329 	}
5330 
5331 	/* There is always at least TC0 */
5332 	if (!ret)
5333 		ret = 1;
5334 
5335 	return ret;
5336 }
5337 
5338 /**
5339  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5340  * @dcbcfg: the corresponding DCBx configuration structure
5341  *
5342  * Query the current DCB configuration and return the number of
5343  * traffic classes enabled from the given DCBX config
5344  **/
5345 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5346 {
5347 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5348 	u8 enabled_tc = 1;
5349 	u8 i;
5350 
5351 	for (i = 0; i < num_tc; i++)
5352 		enabled_tc |= BIT(i);
5353 
5354 	return enabled_tc;
5355 }
5356 
5357 /**
5358  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5359  * @pf: PF being queried
5360  *
5361  * Query the current MQPRIO configuration and return the number of
5362  * traffic classes enabled.
5363  **/
5364 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5365 {
5366 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5367 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5368 	u8 enabled_tc = 1, i;
5369 
5370 	for (i = 1; i < num_tc; i++)
5371 		enabled_tc |= BIT(i);
5372 	return enabled_tc;
5373 }
5374 
5375 /**
5376  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5377  * @pf: PF being queried
5378  *
5379  * Return number of traffic classes enabled for the given PF
5380  **/
5381 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5382 {
5383 	struct i40e_hw *hw = &pf->hw;
5384 	u8 i, enabled_tc = 1;
5385 	u8 num_tc = 0;
5386 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5387 
5388 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5389 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5390 
5391 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5392 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5393 		return 1;
5394 
5395 	/* SFP mode will be enabled for all TCs on port */
5396 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5397 		return i40e_dcb_get_num_tc(dcbcfg);
5398 
5399 	/* MFP mode return count of enabled TCs for this PF */
5400 	if (pf->hw.func_caps.iscsi)
5401 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5402 	else
5403 		return 1; /* Only TC0 */
5404 
5405 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5406 		if (enabled_tc & BIT(i))
5407 			num_tc++;
5408 	}
5409 	return num_tc;
5410 }
5411 
5412 /**
5413  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5414  * @pf: PF being queried
5415  *
5416  * Return a bitmap for enabled traffic classes for this PF.
5417  **/
5418 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5419 {
5420 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5421 		return i40e_mqprio_get_enabled_tc(pf);
5422 
5423 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5424 	 * default TC
5425 	 */
5426 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5427 		return I40E_DEFAULT_TRAFFIC_CLASS;
5428 
5429 	/* SFP mode we want PF to be enabled for all TCs */
5430 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5431 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5432 
5433 	/* MFP enabled and iSCSI PF type */
5434 	if (pf->hw.func_caps.iscsi)
5435 		return i40e_get_iscsi_tc_map(pf);
5436 	else
5437 		return I40E_DEFAULT_TRAFFIC_CLASS;
5438 }
5439 
5440 /**
5441  * i40e_vsi_get_bw_info - Query VSI BW Information
5442  * @vsi: the VSI being queried
5443  *
5444  * Returns 0 on success, negative value on failure
5445  **/
5446 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5447 {
5448 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5449 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5450 	struct i40e_pf *pf = vsi->back;
5451 	struct i40e_hw *hw = &pf->hw;
5452 	i40e_status ret;
5453 	u32 tc_bw_max;
5454 	int i;
5455 
5456 	/* Get the VSI level BW configuration */
5457 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5458 	if (ret) {
5459 		dev_info(&pf->pdev->dev,
5460 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5461 			 i40e_stat_str(&pf->hw, ret),
5462 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5463 		return -EINVAL;
5464 	}
5465 
5466 	/* Get the VSI level BW configuration per TC */
5467 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5468 					       NULL);
5469 	if (ret) {
5470 		dev_info(&pf->pdev->dev,
5471 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5472 			 i40e_stat_str(&pf->hw, ret),
5473 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5474 		return -EINVAL;
5475 	}
5476 
5477 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5478 		dev_info(&pf->pdev->dev,
5479 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5480 			 bw_config.tc_valid_bits,
5481 			 bw_ets_config.tc_valid_bits);
5482 		/* Still continuing */
5483 	}
5484 
5485 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5486 	vsi->bw_max_quanta = bw_config.max_bw;
5487 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5488 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5489 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5490 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5491 		vsi->bw_ets_limit_credits[i] =
5492 					le16_to_cpu(bw_ets_config.credits[i]);
5493 		/* 3 bits out of 4 for each TC */
5494 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5495 	}
5496 
5497 	return 0;
5498 }
5499 
5500 /**
5501  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5502  * @vsi: the VSI being configured
5503  * @enabled_tc: TC bitmap
5504  * @bw_share: BW shared credits per TC
5505  *
5506  * Returns 0 on success, negative value on failure
5507  **/
5508 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5509 				       u8 *bw_share)
5510 {
5511 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5512 	struct i40e_pf *pf = vsi->back;
5513 	i40e_status ret;
5514 	int i;
5515 
5516 	/* There is no need to reset BW when mqprio mode is on.  */
5517 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5518 		return 0;
5519 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5520 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5521 		if (ret)
5522 			dev_info(&pf->pdev->dev,
5523 				 "Failed to reset tx rate for vsi->seid %u\n",
5524 				 vsi->seid);
5525 		return ret;
5526 	}
5527 	memset(&bw_data, 0, sizeof(bw_data));
5528 	bw_data.tc_valid_bits = enabled_tc;
5529 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5530 		bw_data.tc_bw_credits[i] = bw_share[i];
5531 
5532 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5533 	if (ret) {
5534 		dev_info(&pf->pdev->dev,
5535 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5536 			 pf->hw.aq.asq_last_status);
5537 		return -EINVAL;
5538 	}
5539 
5540 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5541 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5542 
5543 	return 0;
5544 }
5545 
5546 /**
5547  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5548  * @vsi: the VSI being configured
5549  * @enabled_tc: TC map to be enabled
5550  *
5551  **/
5552 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5553 {
5554 	struct net_device *netdev = vsi->netdev;
5555 	struct i40e_pf *pf = vsi->back;
5556 	struct i40e_hw *hw = &pf->hw;
5557 	u8 netdev_tc = 0;
5558 	int i;
5559 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5560 
5561 	if (!netdev)
5562 		return;
5563 
5564 	if (!enabled_tc) {
5565 		netdev_reset_tc(netdev);
5566 		return;
5567 	}
5568 
5569 	/* Set up actual enabled TCs on the VSI */
5570 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5571 		return;
5572 
5573 	/* set per TC queues for the VSI */
5574 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5575 		/* Only set TC queues for enabled tcs
5576 		 *
5577 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5578 		 * enabled_tc bitmap would be 0x00001001; the driver
5579 		 * will set the numtc for netdev as 2 that will be
5580 		 * referenced by the netdev layer as TC 0 and 1.
5581 		 */
5582 		if (vsi->tc_config.enabled_tc & BIT(i))
5583 			netdev_set_tc_queue(netdev,
5584 					vsi->tc_config.tc_info[i].netdev_tc,
5585 					vsi->tc_config.tc_info[i].qcount,
5586 					vsi->tc_config.tc_info[i].qoffset);
5587 	}
5588 
5589 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5590 		return;
5591 
5592 	/* Assign UP2TC map for the VSI */
5593 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5594 		/* Get the actual TC# for the UP */
5595 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5596 		/* Get the mapped netdev TC# for the UP */
5597 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5598 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5599 	}
5600 }
5601 
5602 /**
5603  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5604  * @vsi: the VSI being configured
5605  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5606  **/
5607 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5608 				      struct i40e_vsi_context *ctxt)
5609 {
5610 	/* copy just the sections touched not the entire info
5611 	 * since not all sections are valid as returned by
5612 	 * update vsi params
5613 	 */
5614 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5615 	memcpy(&vsi->info.queue_mapping,
5616 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5617 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5618 	       sizeof(vsi->info.tc_mapping));
5619 }
5620 
5621 /**
5622  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5623  * @vsi: the VSI being reconfigured
5624  * @vsi_offset: offset from main VF VSI
5625  */
5626 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5627 {
5628 	struct i40e_vsi_context ctxt = {};
5629 	struct i40e_pf *pf;
5630 	struct i40e_hw *hw;
5631 	int ret;
5632 
5633 	if (!vsi)
5634 		return I40E_ERR_PARAM;
5635 	pf = vsi->back;
5636 	hw = &pf->hw;
5637 
5638 	ctxt.seid = vsi->seid;
5639 	ctxt.pf_num = hw->pf_id;
5640 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5641 	ctxt.uplink_seid = vsi->uplink_seid;
5642 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5643 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5644 	ctxt.info = vsi->info;
5645 
5646 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5647 				 false);
5648 	if (vsi->reconfig_rss) {
5649 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5650 				      vsi->num_queue_pairs);
5651 		ret = i40e_vsi_config_rss(vsi);
5652 		if (ret) {
5653 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5654 			return ret;
5655 		}
5656 		vsi->reconfig_rss = false;
5657 	}
5658 
5659 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5660 	if (ret) {
5661 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %s aq_err %s\n",
5662 			 i40e_stat_str(hw, ret),
5663 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5664 		return ret;
5665 	}
5666 	/* update the local VSI info with updated queue map */
5667 	i40e_vsi_update_queue_map(vsi, &ctxt);
5668 	vsi->info.valid_sections = 0;
5669 
5670 	return ret;
5671 }
5672 
5673 /**
5674  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5675  * @vsi: VSI to be configured
5676  * @enabled_tc: TC bitmap
5677  *
5678  * This configures a particular VSI for TCs that are mapped to the
5679  * given TC bitmap. It uses default bandwidth share for TCs across
5680  * VSIs to configure TC for a particular VSI.
5681  *
5682  * NOTE:
5683  * It is expected that the VSI queues have been quisced before calling
5684  * this function.
5685  **/
5686 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5687 {
5688 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5689 	struct i40e_pf *pf = vsi->back;
5690 	struct i40e_hw *hw = &pf->hw;
5691 	struct i40e_vsi_context ctxt;
5692 	int ret = 0;
5693 	int i;
5694 
5695 	/* Check if enabled_tc is same as existing or new TCs */
5696 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5697 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5698 		return ret;
5699 
5700 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5701 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5702 		if (enabled_tc & BIT(i))
5703 			bw_share[i] = 1;
5704 	}
5705 
5706 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5707 	if (ret) {
5708 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5709 
5710 		dev_info(&pf->pdev->dev,
5711 			 "Failed configuring TC map %d for VSI %d\n",
5712 			 enabled_tc, vsi->seid);
5713 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5714 						  &bw_config, NULL);
5715 		if (ret) {
5716 			dev_info(&pf->pdev->dev,
5717 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5718 				 i40e_stat_str(hw, ret),
5719 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5720 			goto out;
5721 		}
5722 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5723 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5724 
5725 			if (!valid_tc)
5726 				valid_tc = bw_config.tc_valid_bits;
5727 			/* Always enable TC0, no matter what */
5728 			valid_tc |= 1;
5729 			dev_info(&pf->pdev->dev,
5730 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5731 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5732 			enabled_tc = valid_tc;
5733 		}
5734 
5735 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5736 		if (ret) {
5737 			dev_err(&pf->pdev->dev,
5738 				"Unable to  configure TC map %d for VSI %d\n",
5739 				enabled_tc, vsi->seid);
5740 			goto out;
5741 		}
5742 	}
5743 
5744 	/* Update Queue Pairs Mapping for currently enabled UPs */
5745 	ctxt.seid = vsi->seid;
5746 	ctxt.pf_num = vsi->back->hw.pf_id;
5747 	ctxt.vf_num = 0;
5748 	ctxt.uplink_seid = vsi->uplink_seid;
5749 	ctxt.info = vsi->info;
5750 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5751 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5752 		if (ret)
5753 			goto out;
5754 	} else {
5755 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5756 	}
5757 
5758 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5759 	 * queues changed.
5760 	 */
5761 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5762 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5763 				      vsi->num_queue_pairs);
5764 		ret = i40e_vsi_config_rss(vsi);
5765 		if (ret) {
5766 			dev_info(&vsi->back->pdev->dev,
5767 				 "Failed to reconfig rss for num_queues\n");
5768 			return ret;
5769 		}
5770 		vsi->reconfig_rss = false;
5771 	}
5772 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5773 		ctxt.info.valid_sections |=
5774 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5775 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5776 	}
5777 
5778 	/* Update the VSI after updating the VSI queue-mapping
5779 	 * information
5780 	 */
5781 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5782 	if (ret) {
5783 		dev_info(&pf->pdev->dev,
5784 			 "Update vsi tc config failed, err %s aq_err %s\n",
5785 			 i40e_stat_str(hw, ret),
5786 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5787 		goto out;
5788 	}
5789 	/* update the local VSI info with updated queue map */
5790 	i40e_vsi_update_queue_map(vsi, &ctxt);
5791 	vsi->info.valid_sections = 0;
5792 
5793 	/* Update current VSI BW information */
5794 	ret = i40e_vsi_get_bw_info(vsi);
5795 	if (ret) {
5796 		dev_info(&pf->pdev->dev,
5797 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5798 			 i40e_stat_str(hw, ret),
5799 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5800 		goto out;
5801 	}
5802 
5803 	/* Update the netdev TC setup */
5804 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5805 out:
5806 	return ret;
5807 }
5808 
5809 /**
5810  * i40e_get_link_speed - Returns link speed for the interface
5811  * @vsi: VSI to be configured
5812  *
5813  **/
5814 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5815 {
5816 	struct i40e_pf *pf = vsi->back;
5817 
5818 	switch (pf->hw.phy.link_info.link_speed) {
5819 	case I40E_LINK_SPEED_40GB:
5820 		return 40000;
5821 	case I40E_LINK_SPEED_25GB:
5822 		return 25000;
5823 	case I40E_LINK_SPEED_20GB:
5824 		return 20000;
5825 	case I40E_LINK_SPEED_10GB:
5826 		return 10000;
5827 	case I40E_LINK_SPEED_1GB:
5828 		return 1000;
5829 	default:
5830 		return -EINVAL;
5831 	}
5832 }
5833 
5834 /**
5835  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5836  * @vsi: VSI to be configured
5837  * @seid: seid of the channel/VSI
5838  * @max_tx_rate: max TX rate to be configured as BW limit
5839  *
5840  * Helper function to set BW limit for a given VSI
5841  **/
5842 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5843 {
5844 	struct i40e_pf *pf = vsi->back;
5845 	u64 credits = 0;
5846 	int speed = 0;
5847 	int ret = 0;
5848 
5849 	speed = i40e_get_link_speed(vsi);
5850 	if (max_tx_rate > speed) {
5851 		dev_err(&pf->pdev->dev,
5852 			"Invalid max tx rate %llu specified for VSI seid %d.",
5853 			max_tx_rate, seid);
5854 		return -EINVAL;
5855 	}
5856 	if (max_tx_rate && max_tx_rate < 50) {
5857 		dev_warn(&pf->pdev->dev,
5858 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5859 		max_tx_rate = 50;
5860 	}
5861 
5862 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5863 	credits = max_tx_rate;
5864 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5865 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5866 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5867 	if (ret)
5868 		dev_err(&pf->pdev->dev,
5869 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5870 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5871 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5872 	return ret;
5873 }
5874 
5875 /**
5876  * i40e_remove_queue_channels - Remove queue channels for the TCs
5877  * @vsi: VSI to be configured
5878  *
5879  * Remove queue channels for the TCs
5880  **/
5881 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5882 {
5883 	enum i40e_admin_queue_err last_aq_status;
5884 	struct i40e_cloud_filter *cfilter;
5885 	struct i40e_channel *ch, *ch_tmp;
5886 	struct i40e_pf *pf = vsi->back;
5887 	struct hlist_node *node;
5888 	int ret, i;
5889 
5890 	/* Reset rss size that was stored when reconfiguring rss for
5891 	 * channel VSIs with non-power-of-2 queue count.
5892 	 */
5893 	vsi->current_rss_size = 0;
5894 
5895 	/* perform cleanup for channels if they exist */
5896 	if (list_empty(&vsi->ch_list))
5897 		return;
5898 
5899 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5900 		struct i40e_vsi *p_vsi;
5901 
5902 		list_del(&ch->list);
5903 		p_vsi = ch->parent_vsi;
5904 		if (!p_vsi || !ch->initialized) {
5905 			kfree(ch);
5906 			continue;
5907 		}
5908 		/* Reset queue contexts */
5909 		for (i = 0; i < ch->num_queue_pairs; i++) {
5910 			struct i40e_ring *tx_ring, *rx_ring;
5911 			u16 pf_q;
5912 
5913 			pf_q = ch->base_queue + i;
5914 			tx_ring = vsi->tx_rings[pf_q];
5915 			tx_ring->ch = NULL;
5916 
5917 			rx_ring = vsi->rx_rings[pf_q];
5918 			rx_ring->ch = NULL;
5919 		}
5920 
5921 		/* Reset BW configured for this VSI via mqprio */
5922 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5923 		if (ret)
5924 			dev_info(&vsi->back->pdev->dev,
5925 				 "Failed to reset tx rate for ch->seid %u\n",
5926 				 ch->seid);
5927 
5928 		/* delete cloud filters associated with this channel */
5929 		hlist_for_each_entry_safe(cfilter, node,
5930 					  &pf->cloud_filter_list, cloud_node) {
5931 			if (cfilter->seid != ch->seid)
5932 				continue;
5933 
5934 			hash_del(&cfilter->cloud_node);
5935 			if (cfilter->dst_port)
5936 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
5937 									cfilter,
5938 									false);
5939 			else
5940 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
5941 								false);
5942 			last_aq_status = pf->hw.aq.asq_last_status;
5943 			if (ret)
5944 				dev_info(&pf->pdev->dev,
5945 					 "Failed to delete cloud filter, err %s aq_err %s\n",
5946 					 i40e_stat_str(&pf->hw, ret),
5947 					 i40e_aq_str(&pf->hw, last_aq_status));
5948 			kfree(cfilter);
5949 		}
5950 
5951 		/* delete VSI from FW */
5952 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5953 					     NULL);
5954 		if (ret)
5955 			dev_err(&vsi->back->pdev->dev,
5956 				"unable to remove channel (%d) for parent VSI(%d)\n",
5957 				ch->seid, p_vsi->seid);
5958 		kfree(ch);
5959 	}
5960 	INIT_LIST_HEAD(&vsi->ch_list);
5961 }
5962 
5963 /**
5964  * i40e_get_max_queues_for_channel
5965  * @vsi: ptr to VSI to which channels are associated with
5966  *
5967  * Helper function which returns max value among the queue counts set on the
5968  * channels/TCs created.
5969  **/
5970 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5971 {
5972 	struct i40e_channel *ch, *ch_tmp;
5973 	int max = 0;
5974 
5975 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5976 		if (!ch->initialized)
5977 			continue;
5978 		if (ch->num_queue_pairs > max)
5979 			max = ch->num_queue_pairs;
5980 	}
5981 
5982 	return max;
5983 }
5984 
5985 /**
5986  * i40e_validate_num_queues - validate num_queues w.r.t channel
5987  * @pf: ptr to PF device
5988  * @num_queues: number of queues
5989  * @vsi: the parent VSI
5990  * @reconfig_rss: indicates should the RSS be reconfigured or not
5991  *
5992  * This function validates number of queues in the context of new channel
5993  * which is being established and determines if RSS should be reconfigured
5994  * or not for parent VSI.
5995  **/
5996 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5997 				    struct i40e_vsi *vsi, bool *reconfig_rss)
5998 {
5999 	int max_ch_queues;
6000 
6001 	if (!reconfig_rss)
6002 		return -EINVAL;
6003 
6004 	*reconfig_rss = false;
6005 	if (vsi->current_rss_size) {
6006 		if (num_queues > vsi->current_rss_size) {
6007 			dev_dbg(&pf->pdev->dev,
6008 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6009 				num_queues, vsi->current_rss_size);
6010 			return -EINVAL;
6011 		} else if ((num_queues < vsi->current_rss_size) &&
6012 			   (!is_power_of_2(num_queues))) {
6013 			dev_dbg(&pf->pdev->dev,
6014 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6015 				num_queues, vsi->current_rss_size);
6016 			return -EINVAL;
6017 		}
6018 	}
6019 
6020 	if (!is_power_of_2(num_queues)) {
6021 		/* Find the max num_queues configured for channel if channel
6022 		 * exist.
6023 		 * if channel exist, then enforce 'num_queues' to be more than
6024 		 * max ever queues configured for channel.
6025 		 */
6026 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6027 		if (num_queues < max_ch_queues) {
6028 			dev_dbg(&pf->pdev->dev,
6029 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6030 				num_queues, max_ch_queues);
6031 			return -EINVAL;
6032 		}
6033 		*reconfig_rss = true;
6034 	}
6035 
6036 	return 0;
6037 }
6038 
6039 /**
6040  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6041  * @vsi: the VSI being setup
6042  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6043  *
6044  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6045  **/
6046 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6047 {
6048 	struct i40e_pf *pf = vsi->back;
6049 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6050 	struct i40e_hw *hw = &pf->hw;
6051 	int local_rss_size;
6052 	u8 *lut;
6053 	int ret;
6054 
6055 	if (!vsi->rss_size)
6056 		return -EINVAL;
6057 
6058 	if (rss_size > vsi->rss_size)
6059 		return -EINVAL;
6060 
6061 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6062 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6063 	if (!lut)
6064 		return -ENOMEM;
6065 
6066 	/* Ignoring user configured lut if there is one */
6067 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6068 
6069 	/* Use user configured hash key if there is one, otherwise
6070 	 * use default.
6071 	 */
6072 	if (vsi->rss_hkey_user)
6073 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6074 	else
6075 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6076 
6077 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6078 	if (ret) {
6079 		dev_info(&pf->pdev->dev,
6080 			 "Cannot set RSS lut, err %s aq_err %s\n",
6081 			 i40e_stat_str(hw, ret),
6082 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6083 		kfree(lut);
6084 		return ret;
6085 	}
6086 	kfree(lut);
6087 
6088 	/* Do the update w.r.t. storing rss_size */
6089 	if (!vsi->orig_rss_size)
6090 		vsi->orig_rss_size = vsi->rss_size;
6091 	vsi->current_rss_size = local_rss_size;
6092 
6093 	return ret;
6094 }
6095 
6096 /**
6097  * i40e_channel_setup_queue_map - Setup a channel queue map
6098  * @pf: ptr to PF device
6099  * @ctxt: VSI context structure
6100  * @ch: ptr to channel structure
6101  *
6102  * Setup queue map for a specific channel
6103  **/
6104 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6105 					 struct i40e_vsi_context *ctxt,
6106 					 struct i40e_channel *ch)
6107 {
6108 	u16 qcount, qmap, sections = 0;
6109 	u8 offset = 0;
6110 	int pow;
6111 
6112 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6113 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6114 
6115 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6116 	ch->num_queue_pairs = qcount;
6117 
6118 	/* find the next higher power-of-2 of num queue pairs */
6119 	pow = ilog2(qcount);
6120 	if (!is_power_of_2(qcount))
6121 		pow++;
6122 
6123 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6124 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6125 
6126 	/* Setup queue TC[0].qmap for given VSI context */
6127 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6128 
6129 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6130 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6131 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6132 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6133 }
6134 
6135 /**
6136  * i40e_add_channel - add a channel by adding VSI
6137  * @pf: ptr to PF device
6138  * @uplink_seid: underlying HW switching element (VEB) ID
6139  * @ch: ptr to channel structure
6140  *
6141  * Add a channel (VSI) using add_vsi and queue_map
6142  **/
6143 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6144 			    struct i40e_channel *ch)
6145 {
6146 	struct i40e_hw *hw = &pf->hw;
6147 	struct i40e_vsi_context ctxt;
6148 	u8 enabled_tc = 0x1; /* TC0 enabled */
6149 	int ret;
6150 
6151 	if (ch->type != I40E_VSI_VMDQ2) {
6152 		dev_info(&pf->pdev->dev,
6153 			 "add new vsi failed, ch->type %d\n", ch->type);
6154 		return -EINVAL;
6155 	}
6156 
6157 	memset(&ctxt, 0, sizeof(ctxt));
6158 	ctxt.pf_num = hw->pf_id;
6159 	ctxt.vf_num = 0;
6160 	ctxt.uplink_seid = uplink_seid;
6161 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6162 	if (ch->type == I40E_VSI_VMDQ2)
6163 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6164 
6165 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6166 		ctxt.info.valid_sections |=
6167 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6168 		ctxt.info.switch_id =
6169 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6170 	}
6171 
6172 	/* Set queue map for a given VSI context */
6173 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6174 
6175 	/* Now time to create VSI */
6176 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6177 	if (ret) {
6178 		dev_info(&pf->pdev->dev,
6179 			 "add new vsi failed, err %s aq_err %s\n",
6180 			 i40e_stat_str(&pf->hw, ret),
6181 			 i40e_aq_str(&pf->hw,
6182 				     pf->hw.aq.asq_last_status));
6183 		return -ENOENT;
6184 	}
6185 
6186 	/* Success, update channel, set enabled_tc only if the channel
6187 	 * is not a macvlan
6188 	 */
6189 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6190 	ch->seid = ctxt.seid;
6191 	ch->vsi_number = ctxt.vsi_number;
6192 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6193 
6194 	/* copy just the sections touched not the entire info
6195 	 * since not all sections are valid as returned by
6196 	 * update vsi params
6197 	 */
6198 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6199 	memcpy(&ch->info.queue_mapping,
6200 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6201 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6202 	       sizeof(ctxt.info.tc_mapping));
6203 
6204 	return 0;
6205 }
6206 
6207 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6208 				  u8 *bw_share)
6209 {
6210 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6211 	i40e_status ret;
6212 	int i;
6213 
6214 	memset(&bw_data, 0, sizeof(bw_data));
6215 	bw_data.tc_valid_bits = ch->enabled_tc;
6216 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6217 		bw_data.tc_bw_credits[i] = bw_share[i];
6218 
6219 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6220 				       &bw_data, NULL);
6221 	if (ret) {
6222 		dev_info(&vsi->back->pdev->dev,
6223 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6224 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6225 		return -EINVAL;
6226 	}
6227 
6228 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6229 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6230 
6231 	return 0;
6232 }
6233 
6234 /**
6235  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6236  * @pf: ptr to PF device
6237  * @vsi: the VSI being setup
6238  * @ch: ptr to channel structure
6239  *
6240  * Configure TX rings associated with channel (VSI) since queues are being
6241  * from parent VSI.
6242  **/
6243 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6244 				       struct i40e_vsi *vsi,
6245 				       struct i40e_channel *ch)
6246 {
6247 	i40e_status ret;
6248 	int i;
6249 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6250 
6251 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6252 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6253 		if (ch->enabled_tc & BIT(i))
6254 			bw_share[i] = 1;
6255 	}
6256 
6257 	/* configure BW for new VSI */
6258 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6259 	if (ret) {
6260 		dev_info(&vsi->back->pdev->dev,
6261 			 "Failed configuring TC map %d for channel (seid %u)\n",
6262 			 ch->enabled_tc, ch->seid);
6263 		return ret;
6264 	}
6265 
6266 	for (i = 0; i < ch->num_queue_pairs; i++) {
6267 		struct i40e_ring *tx_ring, *rx_ring;
6268 		u16 pf_q;
6269 
6270 		pf_q = ch->base_queue + i;
6271 
6272 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6273 		 * context
6274 		 */
6275 		tx_ring = vsi->tx_rings[pf_q];
6276 		tx_ring->ch = ch;
6277 
6278 		/* Get the RX ring ptr */
6279 		rx_ring = vsi->rx_rings[pf_q];
6280 		rx_ring->ch = ch;
6281 	}
6282 
6283 	return 0;
6284 }
6285 
6286 /**
6287  * i40e_setup_hw_channel - setup new channel
6288  * @pf: ptr to PF device
6289  * @vsi: the VSI being setup
6290  * @ch: ptr to channel structure
6291  * @uplink_seid: underlying HW switching element (VEB) ID
6292  * @type: type of channel to be created (VMDq2/VF)
6293  *
6294  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6295  * and configures TX rings accordingly
6296  **/
6297 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6298 					struct i40e_vsi *vsi,
6299 					struct i40e_channel *ch,
6300 					u16 uplink_seid, u8 type)
6301 {
6302 	int ret;
6303 
6304 	ch->initialized = false;
6305 	ch->base_queue = vsi->next_base_queue;
6306 	ch->type = type;
6307 
6308 	/* Proceed with creation of channel (VMDq2) VSI */
6309 	ret = i40e_add_channel(pf, uplink_seid, ch);
6310 	if (ret) {
6311 		dev_info(&pf->pdev->dev,
6312 			 "failed to add_channel using uplink_seid %u\n",
6313 			 uplink_seid);
6314 		return ret;
6315 	}
6316 
6317 	/* Mark the successful creation of channel */
6318 	ch->initialized = true;
6319 
6320 	/* Reconfigure TX queues using QTX_CTL register */
6321 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6322 	if (ret) {
6323 		dev_info(&pf->pdev->dev,
6324 			 "failed to configure TX rings for channel %u\n",
6325 			 ch->seid);
6326 		return ret;
6327 	}
6328 
6329 	/* update 'next_base_queue' */
6330 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6331 	dev_dbg(&pf->pdev->dev,
6332 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6333 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6334 		ch->num_queue_pairs,
6335 		vsi->next_base_queue);
6336 	return ret;
6337 }
6338 
6339 /**
6340  * i40e_setup_channel - setup new channel using uplink element
6341  * @pf: ptr to PF device
6342  * @vsi: pointer to the VSI to set up the channel within
6343  * @ch: ptr to channel structure
6344  *
6345  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6346  * and uplink switching element (uplink_seid)
6347  **/
6348 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6349 			       struct i40e_channel *ch)
6350 {
6351 	u8 vsi_type;
6352 	u16 seid;
6353 	int ret;
6354 
6355 	if (vsi->type == I40E_VSI_MAIN) {
6356 		vsi_type = I40E_VSI_VMDQ2;
6357 	} else {
6358 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6359 			vsi->type);
6360 		return false;
6361 	}
6362 
6363 	/* underlying switching element */
6364 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6365 
6366 	/* create channel (VSI), configure TX rings */
6367 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6368 	if (ret) {
6369 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6370 		return false;
6371 	}
6372 
6373 	return ch->initialized ? true : false;
6374 }
6375 
6376 /**
6377  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6378  * @vsi: ptr to VSI which has PF backing
6379  *
6380  * Sets up switch mode correctly if it needs to be changed and perform
6381  * what are allowed modes.
6382  **/
6383 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6384 {
6385 	u8 mode;
6386 	struct i40e_pf *pf = vsi->back;
6387 	struct i40e_hw *hw = &pf->hw;
6388 	int ret;
6389 
6390 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6391 	if (ret)
6392 		return -EINVAL;
6393 
6394 	if (hw->dev_caps.switch_mode) {
6395 		/* if switch mode is set, support mode2 (non-tunneled for
6396 		 * cloud filter) for now
6397 		 */
6398 		u32 switch_mode = hw->dev_caps.switch_mode &
6399 				  I40E_SWITCH_MODE_MASK;
6400 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6401 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6402 				return 0;
6403 			dev_err(&pf->pdev->dev,
6404 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6405 				hw->dev_caps.switch_mode);
6406 			return -EINVAL;
6407 		}
6408 	}
6409 
6410 	/* Set Bit 7 to be valid */
6411 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6412 
6413 	/* Set L4type for TCP support */
6414 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6415 
6416 	/* Set cloud filter mode */
6417 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6418 
6419 	/* Prep mode field for set_switch_config */
6420 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6421 					pf->last_sw_conf_valid_flags,
6422 					mode, NULL);
6423 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6424 		dev_err(&pf->pdev->dev,
6425 			"couldn't set switch config bits, err %s aq_err %s\n",
6426 			i40e_stat_str(hw, ret),
6427 			i40e_aq_str(hw,
6428 				    hw->aq.asq_last_status));
6429 
6430 	return ret;
6431 }
6432 
6433 /**
6434  * i40e_create_queue_channel - function to create channel
6435  * @vsi: VSI to be configured
6436  * @ch: ptr to channel (it contains channel specific params)
6437  *
6438  * This function creates channel (VSI) using num_queues specified by user,
6439  * reconfigs RSS if needed.
6440  **/
6441 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6442 			      struct i40e_channel *ch)
6443 {
6444 	struct i40e_pf *pf = vsi->back;
6445 	bool reconfig_rss;
6446 	int err;
6447 
6448 	if (!ch)
6449 		return -EINVAL;
6450 
6451 	if (!ch->num_queue_pairs) {
6452 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6453 			ch->num_queue_pairs);
6454 		return -EINVAL;
6455 	}
6456 
6457 	/* validate user requested num_queues for channel */
6458 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6459 				       &reconfig_rss);
6460 	if (err) {
6461 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6462 			 ch->num_queue_pairs);
6463 		return -EINVAL;
6464 	}
6465 
6466 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6467 	 * VSI to be added switch to VEB mode.
6468 	 */
6469 
6470 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6471 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6472 
6473 		if (vsi->type == I40E_VSI_MAIN) {
6474 			if (pf->flags & I40E_FLAG_TC_MQPRIO)
6475 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6476 			else
6477 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6478 		}
6479 		/* now onwards for main VSI, number of queues will be value
6480 		 * of TC0's queue count
6481 		 */
6482 	}
6483 
6484 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6485 	 * it should be more than num_queues
6486 	 */
6487 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6488 		dev_dbg(&pf->pdev->dev,
6489 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6490 			vsi->cnt_q_avail, ch->num_queue_pairs);
6491 		return -EINVAL;
6492 	}
6493 
6494 	/* reconfig_rss only if vsi type is MAIN_VSI */
6495 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6496 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6497 		if (err) {
6498 			dev_info(&pf->pdev->dev,
6499 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6500 				 ch->num_queue_pairs);
6501 			return -EINVAL;
6502 		}
6503 	}
6504 
6505 	if (!i40e_setup_channel(pf, vsi, ch)) {
6506 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6507 		return -EINVAL;
6508 	}
6509 
6510 	dev_info(&pf->pdev->dev,
6511 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6512 		 ch->seid, ch->num_queue_pairs);
6513 
6514 	/* configure VSI for BW limit */
6515 	if (ch->max_tx_rate) {
6516 		u64 credits = ch->max_tx_rate;
6517 
6518 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6519 			return -EINVAL;
6520 
6521 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6522 		dev_dbg(&pf->pdev->dev,
6523 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6524 			ch->max_tx_rate,
6525 			credits,
6526 			ch->seid);
6527 	}
6528 
6529 	/* in case of VF, this will be main SRIOV VSI */
6530 	ch->parent_vsi = vsi;
6531 
6532 	/* and update main_vsi's count for queue_available to use */
6533 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6534 
6535 	return 0;
6536 }
6537 
6538 /**
6539  * i40e_configure_queue_channels - Add queue channel for the given TCs
6540  * @vsi: VSI to be configured
6541  *
6542  * Configures queue channel mapping to the given TCs
6543  **/
6544 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6545 {
6546 	struct i40e_channel *ch;
6547 	u64 max_rate = 0;
6548 	int ret = 0, i;
6549 
6550 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6551 	vsi->tc_seid_map[0] = vsi->seid;
6552 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6553 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6554 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6555 			if (!ch) {
6556 				ret = -ENOMEM;
6557 				goto err_free;
6558 			}
6559 
6560 			INIT_LIST_HEAD(&ch->list);
6561 			ch->num_queue_pairs =
6562 				vsi->tc_config.tc_info[i].qcount;
6563 			ch->base_queue =
6564 				vsi->tc_config.tc_info[i].qoffset;
6565 
6566 			/* Bandwidth limit through tc interface is in bytes/s,
6567 			 * change to Mbit/s
6568 			 */
6569 			max_rate = vsi->mqprio_qopt.max_rate[i];
6570 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6571 			ch->max_tx_rate = max_rate;
6572 
6573 			list_add_tail(&ch->list, &vsi->ch_list);
6574 
6575 			ret = i40e_create_queue_channel(vsi, ch);
6576 			if (ret) {
6577 				dev_err(&vsi->back->pdev->dev,
6578 					"Failed creating queue channel with TC%d: queues %d\n",
6579 					i, ch->num_queue_pairs);
6580 				goto err_free;
6581 			}
6582 			vsi->tc_seid_map[i] = ch->seid;
6583 		}
6584 	}
6585 	return ret;
6586 
6587 err_free:
6588 	i40e_remove_queue_channels(vsi);
6589 	return ret;
6590 }
6591 
6592 /**
6593  * i40e_veb_config_tc - Configure TCs for given VEB
6594  * @veb: given VEB
6595  * @enabled_tc: TC bitmap
6596  *
6597  * Configures given TC bitmap for VEB (switching) element
6598  **/
6599 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6600 {
6601 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6602 	struct i40e_pf *pf = veb->pf;
6603 	int ret = 0;
6604 	int i;
6605 
6606 	/* No TCs or already enabled TCs just return */
6607 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6608 		return ret;
6609 
6610 	bw_data.tc_valid_bits = enabled_tc;
6611 	/* bw_data.absolute_credits is not set (relative) */
6612 
6613 	/* Enable ETS TCs with equal BW Share for now */
6614 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6615 		if (enabled_tc & BIT(i))
6616 			bw_data.tc_bw_share_credits[i] = 1;
6617 	}
6618 
6619 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6620 						   &bw_data, NULL);
6621 	if (ret) {
6622 		dev_info(&pf->pdev->dev,
6623 			 "VEB bw config failed, err %s aq_err %s\n",
6624 			 i40e_stat_str(&pf->hw, ret),
6625 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6626 		goto out;
6627 	}
6628 
6629 	/* Update the BW information */
6630 	ret = i40e_veb_get_bw_info(veb);
6631 	if (ret) {
6632 		dev_info(&pf->pdev->dev,
6633 			 "Failed getting veb bw config, err %s aq_err %s\n",
6634 			 i40e_stat_str(&pf->hw, ret),
6635 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6636 	}
6637 
6638 out:
6639 	return ret;
6640 }
6641 
6642 #ifdef CONFIG_I40E_DCB
6643 /**
6644  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6645  * @pf: PF struct
6646  *
6647  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6648  * the caller would've quiesce all the VSIs before calling
6649  * this function
6650  **/
6651 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6652 {
6653 	u8 tc_map = 0;
6654 	int ret;
6655 	u8 v;
6656 
6657 	/* Enable the TCs available on PF to all VEBs */
6658 	tc_map = i40e_pf_get_tc_map(pf);
6659 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6660 		return;
6661 
6662 	for (v = 0; v < I40E_MAX_VEB; v++) {
6663 		if (!pf->veb[v])
6664 			continue;
6665 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6666 		if (ret) {
6667 			dev_info(&pf->pdev->dev,
6668 				 "Failed configuring TC for VEB seid=%d\n",
6669 				 pf->veb[v]->seid);
6670 			/* Will try to configure as many components */
6671 		}
6672 	}
6673 
6674 	/* Update each VSI */
6675 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6676 		if (!pf->vsi[v])
6677 			continue;
6678 
6679 		/* - Enable all TCs for the LAN VSI
6680 		 * - For all others keep them at TC0 for now
6681 		 */
6682 		if (v == pf->lan_vsi)
6683 			tc_map = i40e_pf_get_tc_map(pf);
6684 		else
6685 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6686 
6687 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6688 		if (ret) {
6689 			dev_info(&pf->pdev->dev,
6690 				 "Failed configuring TC for VSI seid=%d\n",
6691 				 pf->vsi[v]->seid);
6692 			/* Will try to configure as many components */
6693 		} else {
6694 			/* Re-configure VSI vectors based on updated TC map */
6695 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6696 			if (pf->vsi[v]->netdev)
6697 				i40e_dcbnl_set_all(pf->vsi[v]);
6698 		}
6699 	}
6700 }
6701 
6702 /**
6703  * i40e_resume_port_tx - Resume port Tx
6704  * @pf: PF struct
6705  *
6706  * Resume a port's Tx and issue a PF reset in case of failure to
6707  * resume.
6708  **/
6709 static int i40e_resume_port_tx(struct i40e_pf *pf)
6710 {
6711 	struct i40e_hw *hw = &pf->hw;
6712 	int ret;
6713 
6714 	ret = i40e_aq_resume_port_tx(hw, NULL);
6715 	if (ret) {
6716 		dev_info(&pf->pdev->dev,
6717 			 "Resume Port Tx failed, err %s aq_err %s\n",
6718 			  i40e_stat_str(&pf->hw, ret),
6719 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6720 		/* Schedule PF reset to recover */
6721 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6722 		i40e_service_event_schedule(pf);
6723 	}
6724 
6725 	return ret;
6726 }
6727 
6728 /**
6729  * i40e_suspend_port_tx - Suspend port Tx
6730  * @pf: PF struct
6731  *
6732  * Suspend a port's Tx and issue a PF reset in case of failure.
6733  **/
6734 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6735 {
6736 	struct i40e_hw *hw = &pf->hw;
6737 	int ret;
6738 
6739 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6740 	if (ret) {
6741 		dev_info(&pf->pdev->dev,
6742 			 "Suspend Port Tx failed, err %s aq_err %s\n",
6743 			 i40e_stat_str(&pf->hw, ret),
6744 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6745 		/* Schedule PF reset to recover */
6746 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6747 		i40e_service_event_schedule(pf);
6748 	}
6749 
6750 	return ret;
6751 }
6752 
6753 /**
6754  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6755  * @pf: PF being configured
6756  * @new_cfg: New DCBX configuration
6757  *
6758  * Program DCB settings into HW and reconfigure VEB/VSIs on
6759  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6760  **/
6761 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6762 				  struct i40e_dcbx_config *new_cfg)
6763 {
6764 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6765 	int ret;
6766 
6767 	/* Check if need reconfiguration */
6768 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6769 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6770 		return 0;
6771 	}
6772 
6773 	/* Config change disable all VSIs */
6774 	i40e_pf_quiesce_all_vsi(pf);
6775 
6776 	/* Copy the new config to the current config */
6777 	*old_cfg = *new_cfg;
6778 	old_cfg->etsrec = old_cfg->etscfg;
6779 	ret = i40e_set_dcb_config(&pf->hw);
6780 	if (ret) {
6781 		dev_info(&pf->pdev->dev,
6782 			 "Set DCB Config failed, err %s aq_err %s\n",
6783 			 i40e_stat_str(&pf->hw, ret),
6784 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6785 		goto out;
6786 	}
6787 
6788 	/* Changes in configuration update VEB/VSI */
6789 	i40e_dcb_reconfigure(pf);
6790 out:
6791 	/* In case of reset do not try to resume anything */
6792 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6793 		/* Re-start the VSIs if disabled */
6794 		ret = i40e_resume_port_tx(pf);
6795 		/* In case of error no point in resuming VSIs */
6796 		if (ret)
6797 			goto err;
6798 		i40e_pf_unquiesce_all_vsi(pf);
6799 	}
6800 err:
6801 	return ret;
6802 }
6803 
6804 /**
6805  * i40e_hw_dcb_config - Program new DCBX settings into HW
6806  * @pf: PF being configured
6807  * @new_cfg: New DCBX configuration
6808  *
6809  * Program DCB settings into HW and reconfigure VEB/VSIs on
6810  * given PF
6811  **/
6812 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6813 {
6814 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6815 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6816 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6817 	struct i40e_dcbx_config *old_cfg;
6818 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6819 	struct i40e_rx_pb_config pb_cfg;
6820 	struct i40e_hw *hw = &pf->hw;
6821 	u8 num_ports = hw->num_ports;
6822 	bool need_reconfig;
6823 	int ret = -EINVAL;
6824 	u8 lltc_map = 0;
6825 	u8 tc_map = 0;
6826 	u8 new_numtc;
6827 	u8 i;
6828 
6829 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6830 	/* Un-pack information to Program ETS HW via shared API
6831 	 * numtc, tcmap
6832 	 * LLTC map
6833 	 * ETS/NON-ETS arbiter mode
6834 	 * max exponent (credit refills)
6835 	 * Total number of ports
6836 	 * PFC priority bit-map
6837 	 * Priority Table
6838 	 * BW % per TC
6839 	 * Arbiter mode between UPs sharing same TC
6840 	 * TSA table (ETS or non-ETS)
6841 	 * EEE enabled or not
6842 	 * MFS TC table
6843 	 */
6844 
6845 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6846 
6847 	memset(&ets_data, 0, sizeof(ets_data));
6848 	for (i = 0; i < new_numtc; i++) {
6849 		tc_map |= BIT(i);
6850 		switch (new_cfg->etscfg.tsatable[i]) {
6851 		case I40E_IEEE_TSA_ETS:
6852 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6853 			ets_data.tc_bw_share_credits[i] =
6854 					new_cfg->etscfg.tcbwtable[i];
6855 			break;
6856 		case I40E_IEEE_TSA_STRICT:
6857 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6858 			lltc_map |= BIT(i);
6859 			ets_data.tc_bw_share_credits[i] =
6860 					I40E_DCB_STRICT_PRIO_CREDITS;
6861 			break;
6862 		default:
6863 			/* Invalid TSA type */
6864 			need_reconfig = false;
6865 			goto out;
6866 		}
6867 	}
6868 
6869 	old_cfg = &hw->local_dcbx_config;
6870 	/* Check if need reconfiguration */
6871 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6872 
6873 	/* If needed, enable/disable frame tagging, disable all VSIs
6874 	 * and suspend port tx
6875 	 */
6876 	if (need_reconfig) {
6877 		/* Enable DCB tagging only when more than one TC */
6878 		if (new_numtc > 1)
6879 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6880 		else
6881 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6882 
6883 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6884 		/* Reconfiguration needed quiesce all VSIs */
6885 		i40e_pf_quiesce_all_vsi(pf);
6886 		ret = i40e_suspend_port_tx(pf);
6887 		if (ret)
6888 			goto err;
6889 	}
6890 
6891 	/* Configure Port ETS Tx Scheduler */
6892 	ets_data.tc_valid_bits = tc_map;
6893 	ets_data.tc_strict_priority_flags = lltc_map;
6894 	ret = i40e_aq_config_switch_comp_ets
6895 		(hw, pf->mac_seid, &ets_data,
6896 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
6897 	if (ret) {
6898 		dev_info(&pf->pdev->dev,
6899 			 "Modify Port ETS failed, err %s aq_err %s\n",
6900 			 i40e_stat_str(&pf->hw, ret),
6901 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6902 		goto out;
6903 	}
6904 
6905 	/* Configure Rx ETS HW */
6906 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
6907 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
6908 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
6909 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
6910 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
6911 				   lltc_map);
6912 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
6913 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
6914 				     prio_type);
6915 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
6916 			       new_cfg->etscfg.prioritytable);
6917 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
6918 
6919 	/* Configure Rx Packet Buffers in HW */
6920 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6921 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
6922 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
6923 	}
6924 
6925 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
6926 					 false, new_cfg->pfc.pfcenable,
6927 					 mfs_tc, &pb_cfg);
6928 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
6929 
6930 	/* Update the local Rx Packet buffer config */
6931 	pf->pb_cfg = pb_cfg;
6932 
6933 	/* Inform the FW about changes to DCB configuration */
6934 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
6935 	if (ret) {
6936 		dev_info(&pf->pdev->dev,
6937 			 "DCB Updated failed, err %s aq_err %s\n",
6938 			 i40e_stat_str(&pf->hw, ret),
6939 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6940 		goto out;
6941 	}
6942 
6943 	/* Update the port DCBx configuration */
6944 	*old_cfg = *new_cfg;
6945 
6946 	/* Changes in configuration update VEB/VSI */
6947 	i40e_dcb_reconfigure(pf);
6948 out:
6949 	/* Re-start the VSIs if disabled */
6950 	if (need_reconfig) {
6951 		ret = i40e_resume_port_tx(pf);
6952 
6953 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
6954 		/* In case of error no point in resuming VSIs */
6955 		if (ret)
6956 			goto err;
6957 
6958 		/* Wait for the PF's queues to be disabled */
6959 		ret = i40e_pf_wait_queues_disabled(pf);
6960 		if (ret) {
6961 			/* Schedule PF reset to recover */
6962 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6963 			i40e_service_event_schedule(pf);
6964 			goto err;
6965 		} else {
6966 			i40e_pf_unquiesce_all_vsi(pf);
6967 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
6968 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
6969 		}
6970 		/* registers are set, lets apply */
6971 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
6972 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
6973 	}
6974 
6975 err:
6976 	return ret;
6977 }
6978 
6979 /**
6980  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
6981  * @pf: PF being queried
6982  *
6983  * Set default DCB configuration in case DCB is to be done in SW.
6984  **/
6985 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
6986 {
6987 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
6988 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6989 	struct i40e_hw *hw = &pf->hw;
6990 	int err;
6991 
6992 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
6993 		/* Update the local cached instance with TC0 ETS */
6994 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
6995 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
6996 		pf->tmp_cfg.etscfg.maxtcs = 0;
6997 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
6998 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
6999 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7000 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7001 		/* FW needs one App to configure HW */
7002 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7003 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7004 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7005 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7006 
7007 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7008 	}
7009 
7010 	memset(&ets_data, 0, sizeof(ets_data));
7011 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7012 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7013 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7014 
7015 	/* Enable ETS on the Physical port */
7016 	err = i40e_aq_config_switch_comp_ets
7017 		(hw, pf->mac_seid, &ets_data,
7018 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7019 	if (err) {
7020 		dev_info(&pf->pdev->dev,
7021 			 "Enable Port ETS failed, err %s aq_err %s\n",
7022 			 i40e_stat_str(&pf->hw, err),
7023 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7024 		err = -ENOENT;
7025 		goto out;
7026 	}
7027 
7028 	/* Update the local cached instance with TC0 ETS */
7029 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7030 	dcb_cfg->etscfg.cbs = 0;
7031 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7032 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7033 
7034 out:
7035 	return err;
7036 }
7037 
7038 /**
7039  * i40e_init_pf_dcb - Initialize DCB configuration
7040  * @pf: PF being configured
7041  *
7042  * Query the current DCB configuration and cache it
7043  * in the hardware structure
7044  **/
7045 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7046 {
7047 	struct i40e_hw *hw = &pf->hw;
7048 	int err;
7049 
7050 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7051 	 * Also do not enable DCBx if FW LLDP agent is disabled
7052 	 */
7053 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
7054 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7055 		err = I40E_NOT_SUPPORTED;
7056 		goto out;
7057 	}
7058 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
7059 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7060 		err = i40e_dcb_sw_default_config(pf);
7061 		if (err) {
7062 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7063 			goto out;
7064 		}
7065 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7066 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7067 			       DCB_CAP_DCBX_VER_IEEE;
7068 		/* at init capable but disabled */
7069 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
7070 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7071 		goto out;
7072 	}
7073 	err = i40e_init_dcb(hw, true);
7074 	if (!err) {
7075 		/* Device/Function is not DCBX capable */
7076 		if ((!hw->func_caps.dcb) ||
7077 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7078 			dev_info(&pf->pdev->dev,
7079 				 "DCBX offload is not supported or is disabled for this PF.\n");
7080 		} else {
7081 			/* When status is not DISABLED then DCBX in FW */
7082 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7083 				       DCB_CAP_DCBX_VER_IEEE;
7084 
7085 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
7086 			/* Enable DCB tagging only when more than one TC
7087 			 * or explicitly disable if only one TC
7088 			 */
7089 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7090 				pf->flags |= I40E_FLAG_DCB_ENABLED;
7091 			else
7092 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7093 			dev_dbg(&pf->pdev->dev,
7094 				"DCBX offload is supported for this PF.\n");
7095 		}
7096 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7097 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7098 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
7099 	} else {
7100 		dev_info(&pf->pdev->dev,
7101 			 "Query for DCB configuration failed, err %s aq_err %s\n",
7102 			 i40e_stat_str(&pf->hw, err),
7103 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7104 	}
7105 
7106 out:
7107 	return err;
7108 }
7109 #endif /* CONFIG_I40E_DCB */
7110 
7111 /**
7112  * i40e_print_link_message - print link up or down
7113  * @vsi: the VSI for which link needs a message
7114  * @isup: true of link is up, false otherwise
7115  */
7116 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7117 {
7118 	enum i40e_aq_link_speed new_speed;
7119 	struct i40e_pf *pf = vsi->back;
7120 	char *speed = "Unknown";
7121 	char *fc = "Unknown";
7122 	char *fec = "";
7123 	char *req_fec = "";
7124 	char *an = "";
7125 
7126 	if (isup)
7127 		new_speed = pf->hw.phy.link_info.link_speed;
7128 	else
7129 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7130 
7131 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7132 		return;
7133 	vsi->current_isup = isup;
7134 	vsi->current_speed = new_speed;
7135 	if (!isup) {
7136 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7137 		return;
7138 	}
7139 
7140 	/* Warn user if link speed on NPAR enabled partition is not at
7141 	 * least 10GB
7142 	 */
7143 	if (pf->hw.func_caps.npar_enable &&
7144 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7145 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7146 		netdev_warn(vsi->netdev,
7147 			    "The partition detected link speed that is less than 10Gbps\n");
7148 
7149 	switch (pf->hw.phy.link_info.link_speed) {
7150 	case I40E_LINK_SPEED_40GB:
7151 		speed = "40 G";
7152 		break;
7153 	case I40E_LINK_SPEED_20GB:
7154 		speed = "20 G";
7155 		break;
7156 	case I40E_LINK_SPEED_25GB:
7157 		speed = "25 G";
7158 		break;
7159 	case I40E_LINK_SPEED_10GB:
7160 		speed = "10 G";
7161 		break;
7162 	case I40E_LINK_SPEED_5GB:
7163 		speed = "5 G";
7164 		break;
7165 	case I40E_LINK_SPEED_2_5GB:
7166 		speed = "2.5 G";
7167 		break;
7168 	case I40E_LINK_SPEED_1GB:
7169 		speed = "1000 M";
7170 		break;
7171 	case I40E_LINK_SPEED_100MB:
7172 		speed = "100 M";
7173 		break;
7174 	default:
7175 		break;
7176 	}
7177 
7178 	switch (pf->hw.fc.current_mode) {
7179 	case I40E_FC_FULL:
7180 		fc = "RX/TX";
7181 		break;
7182 	case I40E_FC_TX_PAUSE:
7183 		fc = "TX";
7184 		break;
7185 	case I40E_FC_RX_PAUSE:
7186 		fc = "RX";
7187 		break;
7188 	default:
7189 		fc = "None";
7190 		break;
7191 	}
7192 
7193 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7194 		req_fec = "None";
7195 		fec = "None";
7196 		an = "False";
7197 
7198 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7199 			an = "True";
7200 
7201 		if (pf->hw.phy.link_info.fec_info &
7202 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7203 			fec = "CL74 FC-FEC/BASE-R";
7204 		else if (pf->hw.phy.link_info.fec_info &
7205 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7206 			fec = "CL108 RS-FEC";
7207 
7208 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7209 		 * both RS and FC are requested
7210 		 */
7211 		if (vsi->back->hw.phy.link_info.req_fec_info &
7212 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7213 			if (vsi->back->hw.phy.link_info.req_fec_info &
7214 			    I40E_AQ_REQUEST_FEC_RS)
7215 				req_fec = "CL108 RS-FEC";
7216 			else
7217 				req_fec = "CL74 FC-FEC/BASE-R";
7218 		}
7219 		netdev_info(vsi->netdev,
7220 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7221 			    speed, req_fec, fec, an, fc);
7222 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7223 		req_fec = "None";
7224 		fec = "None";
7225 		an = "False";
7226 
7227 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7228 			an = "True";
7229 
7230 		if (pf->hw.phy.link_info.fec_info &
7231 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7232 			fec = "CL74 FC-FEC/BASE-R";
7233 
7234 		if (pf->hw.phy.link_info.req_fec_info &
7235 		    I40E_AQ_REQUEST_FEC_KR)
7236 			req_fec = "CL74 FC-FEC/BASE-R";
7237 
7238 		netdev_info(vsi->netdev,
7239 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7240 			    speed, req_fec, fec, an, fc);
7241 	} else {
7242 		netdev_info(vsi->netdev,
7243 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7244 			    speed, fc);
7245 	}
7246 
7247 }
7248 
7249 /**
7250  * i40e_up_complete - Finish the last steps of bringing up a connection
7251  * @vsi: the VSI being configured
7252  **/
7253 static int i40e_up_complete(struct i40e_vsi *vsi)
7254 {
7255 	struct i40e_pf *pf = vsi->back;
7256 	int err;
7257 
7258 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7259 		i40e_vsi_configure_msix(vsi);
7260 	else
7261 		i40e_configure_msi_and_legacy(vsi);
7262 
7263 	/* start rings */
7264 	err = i40e_vsi_start_rings(vsi);
7265 	if (err)
7266 		return err;
7267 
7268 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7269 	i40e_napi_enable_all(vsi);
7270 	i40e_vsi_enable_irq(vsi);
7271 
7272 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7273 	    (vsi->netdev)) {
7274 		i40e_print_link_message(vsi, true);
7275 		netif_tx_start_all_queues(vsi->netdev);
7276 		netif_carrier_on(vsi->netdev);
7277 	}
7278 
7279 	/* replay FDIR SB filters */
7280 	if (vsi->type == I40E_VSI_FDIR) {
7281 		/* reset fd counters */
7282 		pf->fd_add_err = 0;
7283 		pf->fd_atr_cnt = 0;
7284 		i40e_fdir_filter_restore(vsi);
7285 	}
7286 
7287 	/* On the next run of the service_task, notify any clients of the new
7288 	 * opened netdev
7289 	 */
7290 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7291 	i40e_service_event_schedule(pf);
7292 
7293 	return 0;
7294 }
7295 
7296 /**
7297  * i40e_vsi_reinit_locked - Reset the VSI
7298  * @vsi: the VSI being configured
7299  *
7300  * Rebuild the ring structs after some configuration
7301  * has changed, e.g. MTU size.
7302  **/
7303 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7304 {
7305 	struct i40e_pf *pf = vsi->back;
7306 
7307 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7308 		usleep_range(1000, 2000);
7309 	i40e_down(vsi);
7310 
7311 	i40e_up(vsi);
7312 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7313 }
7314 
7315 /**
7316  * i40e_force_link_state - Force the link status
7317  * @pf: board private structure
7318  * @is_up: whether the link state should be forced up or down
7319  **/
7320 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7321 {
7322 	struct i40e_aq_get_phy_abilities_resp abilities;
7323 	struct i40e_aq_set_phy_config config = {0};
7324 	bool non_zero_phy_type = is_up;
7325 	struct i40e_hw *hw = &pf->hw;
7326 	i40e_status err;
7327 	u64 mask;
7328 	u8 speed;
7329 
7330 	/* Card might've been put in an unstable state by other drivers
7331 	 * and applications, which causes incorrect speed values being
7332 	 * set on startup. In order to clear speed registers, we call
7333 	 * get_phy_capabilities twice, once to get initial state of
7334 	 * available speeds, and once to get current PHY config.
7335 	 */
7336 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7337 					   NULL);
7338 	if (err) {
7339 		dev_err(&pf->pdev->dev,
7340 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7341 			i40e_stat_str(hw, err),
7342 			i40e_aq_str(hw, hw->aq.asq_last_status));
7343 		return err;
7344 	}
7345 	speed = abilities.link_speed;
7346 
7347 	/* Get the current phy config */
7348 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7349 					   NULL);
7350 	if (err) {
7351 		dev_err(&pf->pdev->dev,
7352 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7353 			i40e_stat_str(hw, err),
7354 			i40e_aq_str(hw, hw->aq.asq_last_status));
7355 		return err;
7356 	}
7357 
7358 	/* If link needs to go up, but was not forced to go down,
7359 	 * and its speed values are OK, no need for a flap
7360 	 * if non_zero_phy_type was set, still need to force up
7361 	 */
7362 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7363 		non_zero_phy_type = true;
7364 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7365 		return I40E_SUCCESS;
7366 
7367 	/* To force link we need to set bits for all supported PHY types,
7368 	 * but there are now more than 32, so we need to split the bitmap
7369 	 * across two fields.
7370 	 */
7371 	mask = I40E_PHY_TYPES_BITMASK;
7372 	config.phy_type =
7373 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7374 	config.phy_type_ext =
7375 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7376 	/* Copy the old settings, except of phy_type */
7377 	config.abilities = abilities.abilities;
7378 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7379 		if (is_up)
7380 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7381 		else
7382 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7383 	}
7384 	if (abilities.link_speed != 0)
7385 		config.link_speed = abilities.link_speed;
7386 	else
7387 		config.link_speed = speed;
7388 	config.eee_capability = abilities.eee_capability;
7389 	config.eeer = abilities.eeer_val;
7390 	config.low_power_ctrl = abilities.d3_lpan;
7391 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7392 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7393 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7394 
7395 	if (err) {
7396 		dev_err(&pf->pdev->dev,
7397 			"set phy config ret =  %s last_status =  %s\n",
7398 			i40e_stat_str(&pf->hw, err),
7399 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7400 		return err;
7401 	}
7402 
7403 	/* Update the link info */
7404 	err = i40e_update_link_info(hw);
7405 	if (err) {
7406 		/* Wait a little bit (on 40G cards it sometimes takes a really
7407 		 * long time for link to come back from the atomic reset)
7408 		 * and try once more
7409 		 */
7410 		msleep(1000);
7411 		i40e_update_link_info(hw);
7412 	}
7413 
7414 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7415 
7416 	return I40E_SUCCESS;
7417 }
7418 
7419 /**
7420  * i40e_up - Bring the connection back up after being down
7421  * @vsi: the VSI being configured
7422  **/
7423 int i40e_up(struct i40e_vsi *vsi)
7424 {
7425 	int err;
7426 
7427 	if (vsi->type == I40E_VSI_MAIN &&
7428 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7429 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7430 		i40e_force_link_state(vsi->back, true);
7431 
7432 	err = i40e_vsi_configure(vsi);
7433 	if (!err)
7434 		err = i40e_up_complete(vsi);
7435 
7436 	return err;
7437 }
7438 
7439 /**
7440  * i40e_down - Shutdown the connection processing
7441  * @vsi: the VSI being stopped
7442  **/
7443 void i40e_down(struct i40e_vsi *vsi)
7444 {
7445 	int i;
7446 
7447 	/* It is assumed that the caller of this function
7448 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7449 	 */
7450 	if (vsi->netdev) {
7451 		netif_carrier_off(vsi->netdev);
7452 		netif_tx_disable(vsi->netdev);
7453 	}
7454 	i40e_vsi_disable_irq(vsi);
7455 	i40e_vsi_stop_rings(vsi);
7456 	if (vsi->type == I40E_VSI_MAIN &&
7457 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7458 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7459 		i40e_force_link_state(vsi->back, false);
7460 	i40e_napi_disable_all(vsi);
7461 
7462 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7463 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7464 		if (i40e_enabled_xdp_vsi(vsi)) {
7465 			/* Make sure that in-progress ndo_xdp_xmit and
7466 			 * ndo_xsk_wakeup calls are completed.
7467 			 */
7468 			synchronize_rcu();
7469 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7470 		}
7471 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7472 	}
7473 
7474 }
7475 
7476 /**
7477  * i40e_validate_mqprio_qopt- validate queue mapping info
7478  * @vsi: the VSI being configured
7479  * @mqprio_qopt: queue parametrs
7480  **/
7481 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7482 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7483 {
7484 	u64 sum_max_rate = 0;
7485 	u64 max_rate = 0;
7486 	int i;
7487 
7488 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7489 	    mqprio_qopt->qopt.num_tc < 1 ||
7490 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7491 		return -EINVAL;
7492 	for (i = 0; ; i++) {
7493 		if (!mqprio_qopt->qopt.count[i])
7494 			return -EINVAL;
7495 		if (mqprio_qopt->min_rate[i]) {
7496 			dev_err(&vsi->back->pdev->dev,
7497 				"Invalid min tx rate (greater than 0) specified\n");
7498 			return -EINVAL;
7499 		}
7500 		max_rate = mqprio_qopt->max_rate[i];
7501 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7502 		sum_max_rate += max_rate;
7503 
7504 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7505 			break;
7506 		if (mqprio_qopt->qopt.offset[i + 1] !=
7507 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7508 			return -EINVAL;
7509 	}
7510 	if (vsi->num_queue_pairs <
7511 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7512 		dev_err(&vsi->back->pdev->dev,
7513 			"Failed to create traffic channel, insufficient number of queues.\n");
7514 		return -EINVAL;
7515 	}
7516 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7517 		dev_err(&vsi->back->pdev->dev,
7518 			"Invalid max tx rate specified\n");
7519 		return -EINVAL;
7520 	}
7521 	return 0;
7522 }
7523 
7524 /**
7525  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7526  * @vsi: the VSI being configured
7527  **/
7528 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7529 {
7530 	u16 qcount;
7531 	int i;
7532 
7533 	/* Only TC0 is enabled */
7534 	vsi->tc_config.numtc = 1;
7535 	vsi->tc_config.enabled_tc = 1;
7536 	qcount = min_t(int, vsi->alloc_queue_pairs,
7537 		       i40e_pf_get_max_q_per_tc(vsi->back));
7538 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7539 		/* For the TC that is not enabled set the offset to default
7540 		 * queue and allocate one queue for the given TC.
7541 		 */
7542 		vsi->tc_config.tc_info[i].qoffset = 0;
7543 		if (i == 0)
7544 			vsi->tc_config.tc_info[i].qcount = qcount;
7545 		else
7546 			vsi->tc_config.tc_info[i].qcount = 1;
7547 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7548 	}
7549 }
7550 
7551 /**
7552  * i40e_del_macvlan_filter
7553  * @hw: pointer to the HW structure
7554  * @seid: seid of the channel VSI
7555  * @macaddr: the mac address to apply as a filter
7556  * @aq_err: store the admin Q error
7557  *
7558  * This function deletes a mac filter on the channel VSI which serves as the
7559  * macvlan. Returns 0 on success.
7560  **/
7561 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7562 					   const u8 *macaddr, int *aq_err)
7563 {
7564 	struct i40e_aqc_remove_macvlan_element_data element;
7565 	i40e_status status;
7566 
7567 	memset(&element, 0, sizeof(element));
7568 	ether_addr_copy(element.mac_addr, macaddr);
7569 	element.vlan_tag = 0;
7570 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7571 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7572 	*aq_err = hw->aq.asq_last_status;
7573 
7574 	return status;
7575 }
7576 
7577 /**
7578  * i40e_add_macvlan_filter
7579  * @hw: pointer to the HW structure
7580  * @seid: seid of the channel VSI
7581  * @macaddr: the mac address to apply as a filter
7582  * @aq_err: store the admin Q error
7583  *
7584  * This function adds a mac filter on the channel VSI which serves as the
7585  * macvlan. Returns 0 on success.
7586  **/
7587 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7588 					   const u8 *macaddr, int *aq_err)
7589 {
7590 	struct i40e_aqc_add_macvlan_element_data element;
7591 	i40e_status status;
7592 	u16 cmd_flags = 0;
7593 
7594 	ether_addr_copy(element.mac_addr, macaddr);
7595 	element.vlan_tag = 0;
7596 	element.queue_number = 0;
7597 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7598 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7599 	element.flags = cpu_to_le16(cmd_flags);
7600 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7601 	*aq_err = hw->aq.asq_last_status;
7602 
7603 	return status;
7604 }
7605 
7606 /**
7607  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7608  * @vsi: the VSI we want to access
7609  * @ch: the channel we want to access
7610  */
7611 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7612 {
7613 	struct i40e_ring *tx_ring, *rx_ring;
7614 	u16 pf_q;
7615 	int i;
7616 
7617 	for (i = 0; i < ch->num_queue_pairs; i++) {
7618 		pf_q = ch->base_queue + i;
7619 		tx_ring = vsi->tx_rings[pf_q];
7620 		tx_ring->ch = NULL;
7621 		rx_ring = vsi->rx_rings[pf_q];
7622 		rx_ring->ch = NULL;
7623 	}
7624 }
7625 
7626 /**
7627  * i40e_free_macvlan_channels
7628  * @vsi: the VSI we want to access
7629  *
7630  * This function frees the Qs of the channel VSI from
7631  * the stack and also deletes the channel VSIs which
7632  * serve as macvlans.
7633  */
7634 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7635 {
7636 	struct i40e_channel *ch, *ch_tmp;
7637 	int ret;
7638 
7639 	if (list_empty(&vsi->macvlan_list))
7640 		return;
7641 
7642 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7643 		struct i40e_vsi *parent_vsi;
7644 
7645 		if (i40e_is_channel_macvlan(ch)) {
7646 			i40e_reset_ch_rings(vsi, ch);
7647 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7648 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7649 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7650 			kfree(ch->fwd);
7651 			ch->fwd = NULL;
7652 		}
7653 
7654 		list_del(&ch->list);
7655 		parent_vsi = ch->parent_vsi;
7656 		if (!parent_vsi || !ch->initialized) {
7657 			kfree(ch);
7658 			continue;
7659 		}
7660 
7661 		/* remove the VSI */
7662 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7663 					     NULL);
7664 		if (ret)
7665 			dev_err(&vsi->back->pdev->dev,
7666 				"unable to remove channel (%d) for parent VSI(%d)\n",
7667 				ch->seid, parent_vsi->seid);
7668 		kfree(ch);
7669 	}
7670 	vsi->macvlan_cnt = 0;
7671 }
7672 
7673 /**
7674  * i40e_fwd_ring_up - bring the macvlan device up
7675  * @vsi: the VSI we want to access
7676  * @vdev: macvlan netdevice
7677  * @fwd: the private fwd structure
7678  */
7679 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7680 			    struct i40e_fwd_adapter *fwd)
7681 {
7682 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7683 	int ret = 0, num_tc = 1,  i, aq_err;
7684 	struct i40e_pf *pf = vsi->back;
7685 	struct i40e_hw *hw = &pf->hw;
7686 
7687 	/* Go through the list and find an available channel */
7688 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7689 		if (!i40e_is_channel_macvlan(iter)) {
7690 			iter->fwd = fwd;
7691 			/* record configuration for macvlan interface in vdev */
7692 			for (i = 0; i < num_tc; i++)
7693 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7694 							     i,
7695 							     iter->num_queue_pairs,
7696 							     iter->base_queue);
7697 			for (i = 0; i < iter->num_queue_pairs; i++) {
7698 				struct i40e_ring *tx_ring, *rx_ring;
7699 				u16 pf_q;
7700 
7701 				pf_q = iter->base_queue + i;
7702 
7703 				/* Get to TX ring ptr */
7704 				tx_ring = vsi->tx_rings[pf_q];
7705 				tx_ring->ch = iter;
7706 
7707 				/* Get the RX ring ptr */
7708 				rx_ring = vsi->rx_rings[pf_q];
7709 				rx_ring->ch = iter;
7710 			}
7711 			ch = iter;
7712 			break;
7713 		}
7714 	}
7715 
7716 	if (!ch)
7717 		return -EINVAL;
7718 
7719 	/* Guarantee all rings are updated before we update the
7720 	 * MAC address filter.
7721 	 */
7722 	wmb();
7723 
7724 	/* Add a mac filter */
7725 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7726 	if (ret) {
7727 		/* if we cannot add the MAC rule then disable the offload */
7728 		macvlan_release_l2fw_offload(vdev);
7729 		for (i = 0; i < ch->num_queue_pairs; i++) {
7730 			struct i40e_ring *rx_ring;
7731 			u16 pf_q;
7732 
7733 			pf_q = ch->base_queue + i;
7734 			rx_ring = vsi->rx_rings[pf_q];
7735 			rx_ring->netdev = NULL;
7736 		}
7737 		dev_info(&pf->pdev->dev,
7738 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7739 			  i40e_stat_str(hw, ret),
7740 			  i40e_aq_str(hw, aq_err));
7741 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7742 	}
7743 
7744 	return ret;
7745 }
7746 
7747 /**
7748  * i40e_setup_macvlans - create the channels which will be macvlans
7749  * @vsi: the VSI we want to access
7750  * @macvlan_cnt: no. of macvlans to be setup
7751  * @qcnt: no. of Qs per macvlan
7752  * @vdev: macvlan netdevice
7753  */
7754 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7755 			       struct net_device *vdev)
7756 {
7757 	struct i40e_pf *pf = vsi->back;
7758 	struct i40e_hw *hw = &pf->hw;
7759 	struct i40e_vsi_context ctxt;
7760 	u16 sections, qmap, num_qps;
7761 	struct i40e_channel *ch;
7762 	int i, pow, ret = 0;
7763 	u8 offset = 0;
7764 
7765 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7766 		return -EINVAL;
7767 
7768 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7769 
7770 	/* find the next higher power-of-2 of num queue pairs */
7771 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7772 
7773 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7774 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7775 
7776 	/* Setup context bits for the main VSI */
7777 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7778 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7779 	memset(&ctxt, 0, sizeof(ctxt));
7780 	ctxt.seid = vsi->seid;
7781 	ctxt.pf_num = vsi->back->hw.pf_id;
7782 	ctxt.vf_num = 0;
7783 	ctxt.uplink_seid = vsi->uplink_seid;
7784 	ctxt.info = vsi->info;
7785 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7786 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7787 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7788 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7789 
7790 	/* Reconfigure RSS for main VSI with new max queue count */
7791 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7792 	ret = i40e_vsi_config_rss(vsi);
7793 	if (ret) {
7794 		dev_info(&pf->pdev->dev,
7795 			 "Failed to reconfig RSS for num_queues (%u)\n",
7796 			 vsi->rss_size);
7797 		return ret;
7798 	}
7799 	vsi->reconfig_rss = true;
7800 	dev_dbg(&vsi->back->pdev->dev,
7801 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7802 	vsi->next_base_queue = num_qps;
7803 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7804 
7805 	/* Update the VSI after updating the VSI queue-mapping
7806 	 * information
7807 	 */
7808 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7809 	if (ret) {
7810 		dev_info(&pf->pdev->dev,
7811 			 "Update vsi tc config failed, err %s aq_err %s\n",
7812 			 i40e_stat_str(hw, ret),
7813 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7814 		return ret;
7815 	}
7816 	/* update the local VSI info with updated queue map */
7817 	i40e_vsi_update_queue_map(vsi, &ctxt);
7818 	vsi->info.valid_sections = 0;
7819 
7820 	/* Create channels for macvlans */
7821 	INIT_LIST_HEAD(&vsi->macvlan_list);
7822 	for (i = 0; i < macvlan_cnt; i++) {
7823 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7824 		if (!ch) {
7825 			ret = -ENOMEM;
7826 			goto err_free;
7827 		}
7828 		INIT_LIST_HEAD(&ch->list);
7829 		ch->num_queue_pairs = qcnt;
7830 		if (!i40e_setup_channel(pf, vsi, ch)) {
7831 			ret = -EINVAL;
7832 			kfree(ch);
7833 			goto err_free;
7834 		}
7835 		ch->parent_vsi = vsi;
7836 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7837 		vsi->macvlan_cnt++;
7838 		list_add_tail(&ch->list, &vsi->macvlan_list);
7839 	}
7840 
7841 	return ret;
7842 
7843 err_free:
7844 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7845 	i40e_free_macvlan_channels(vsi);
7846 
7847 	return ret;
7848 }
7849 
7850 /**
7851  * i40e_fwd_add - configure macvlans
7852  * @netdev: net device to configure
7853  * @vdev: macvlan netdevice
7854  **/
7855 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7856 {
7857 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7858 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7859 	struct i40e_vsi *vsi = np->vsi;
7860 	struct i40e_pf *pf = vsi->back;
7861 	struct i40e_fwd_adapter *fwd;
7862 	int avail_macvlan, ret;
7863 
7864 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7865 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7866 		return ERR_PTR(-EINVAL);
7867 	}
7868 	if ((pf->flags & I40E_FLAG_TC_MQPRIO)) {
7869 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7870 		return ERR_PTR(-EINVAL);
7871 	}
7872 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7873 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7874 		return ERR_PTR(-EINVAL);
7875 	}
7876 
7877 	/* The macvlan device has to be a single Q device so that the
7878 	 * tc_to_txq field can be reused to pick the tx queue.
7879 	 */
7880 	if (netif_is_multiqueue(vdev))
7881 		return ERR_PTR(-ERANGE);
7882 
7883 	if (!vsi->macvlan_cnt) {
7884 		/* reserve bit 0 for the pf device */
7885 		set_bit(0, vsi->fwd_bitmask);
7886 
7887 		/* Try to reserve as many queues as possible for macvlans. First
7888 		 * reserve 3/4th of max vectors, then half, then quarter and
7889 		 * calculate Qs per macvlan as you go
7890 		 */
7891 		vectors = pf->num_lan_msix;
7892 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7893 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7894 			q_per_macvlan = 4;
7895 			macvlan_cnt = (vectors - 32) / 4;
7896 		} else if (vectors <= 64 && vectors > 32) {
7897 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7898 			q_per_macvlan = 2;
7899 			macvlan_cnt = (vectors - 16) / 2;
7900 		} else if (vectors <= 32 && vectors > 16) {
7901 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7902 			q_per_macvlan = 1;
7903 			macvlan_cnt = vectors - 16;
7904 		} else if (vectors <= 16 && vectors > 8) {
7905 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7906 			q_per_macvlan = 1;
7907 			macvlan_cnt = vectors - 8;
7908 		} else {
7909 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7910 			q_per_macvlan = 1;
7911 			macvlan_cnt = vectors - 1;
7912 		}
7913 
7914 		if (macvlan_cnt == 0)
7915 			return ERR_PTR(-EBUSY);
7916 
7917 		/* Quiesce VSI queues */
7918 		i40e_quiesce_vsi(vsi);
7919 
7920 		/* sets up the macvlans but does not "enable" them */
7921 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
7922 					  vdev);
7923 		if (ret)
7924 			return ERR_PTR(ret);
7925 
7926 		/* Unquiesce VSI */
7927 		i40e_unquiesce_vsi(vsi);
7928 	}
7929 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
7930 					    vsi->macvlan_cnt);
7931 	if (avail_macvlan >= I40E_MAX_MACVLANS)
7932 		return ERR_PTR(-EBUSY);
7933 
7934 	/* create the fwd struct */
7935 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
7936 	if (!fwd)
7937 		return ERR_PTR(-ENOMEM);
7938 
7939 	set_bit(avail_macvlan, vsi->fwd_bitmask);
7940 	fwd->bit_no = avail_macvlan;
7941 	netdev_set_sb_channel(vdev, avail_macvlan);
7942 	fwd->netdev = vdev;
7943 
7944 	if (!netif_running(netdev))
7945 		return fwd;
7946 
7947 	/* Set fwd ring up */
7948 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
7949 	if (ret) {
7950 		/* unbind the queues and drop the subordinate channel config */
7951 		netdev_unbind_sb_channel(netdev, vdev);
7952 		netdev_set_sb_channel(vdev, 0);
7953 
7954 		kfree(fwd);
7955 		return ERR_PTR(-EINVAL);
7956 	}
7957 
7958 	return fwd;
7959 }
7960 
7961 /**
7962  * i40e_del_all_macvlans - Delete all the mac filters on the channels
7963  * @vsi: the VSI we want to access
7964  */
7965 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
7966 {
7967 	struct i40e_channel *ch, *ch_tmp;
7968 	struct i40e_pf *pf = vsi->back;
7969 	struct i40e_hw *hw = &pf->hw;
7970 	int aq_err, ret = 0;
7971 
7972 	if (list_empty(&vsi->macvlan_list))
7973 		return;
7974 
7975 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7976 		if (i40e_is_channel_macvlan(ch)) {
7977 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7978 						      i40e_channel_mac(ch),
7979 						      &aq_err);
7980 			if (!ret) {
7981 				/* Reset queue contexts */
7982 				i40e_reset_ch_rings(vsi, ch);
7983 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7984 				netdev_unbind_sb_channel(vsi->netdev,
7985 							 ch->fwd->netdev);
7986 				netdev_set_sb_channel(ch->fwd->netdev, 0);
7987 				kfree(ch->fwd);
7988 				ch->fwd = NULL;
7989 			}
7990 		}
7991 	}
7992 }
7993 
7994 /**
7995  * i40e_fwd_del - delete macvlan interfaces
7996  * @netdev: net device to configure
7997  * @vdev: macvlan netdevice
7998  */
7999 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8000 {
8001 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8002 	struct i40e_fwd_adapter *fwd = vdev;
8003 	struct i40e_channel *ch, *ch_tmp;
8004 	struct i40e_vsi *vsi = np->vsi;
8005 	struct i40e_pf *pf = vsi->back;
8006 	struct i40e_hw *hw = &pf->hw;
8007 	int aq_err, ret = 0;
8008 
8009 	/* Find the channel associated with the macvlan and del mac filter */
8010 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8011 		if (i40e_is_channel_macvlan(ch) &&
8012 		    ether_addr_equal(i40e_channel_mac(ch),
8013 				     fwd->netdev->dev_addr)) {
8014 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8015 						      i40e_channel_mac(ch),
8016 						      &aq_err);
8017 			if (!ret) {
8018 				/* Reset queue contexts */
8019 				i40e_reset_ch_rings(vsi, ch);
8020 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8021 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8022 				netdev_set_sb_channel(fwd->netdev, 0);
8023 				kfree(ch->fwd);
8024 				ch->fwd = NULL;
8025 			} else {
8026 				dev_info(&pf->pdev->dev,
8027 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
8028 					  i40e_stat_str(hw, ret),
8029 					  i40e_aq_str(hw, aq_err));
8030 			}
8031 			break;
8032 		}
8033 	}
8034 }
8035 
8036 /**
8037  * i40e_setup_tc - configure multiple traffic classes
8038  * @netdev: net device to configure
8039  * @type_data: tc offload data
8040  **/
8041 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8042 {
8043 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8044 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8045 	struct i40e_vsi *vsi = np->vsi;
8046 	struct i40e_pf *pf = vsi->back;
8047 	u8 enabled_tc = 0, num_tc, hw;
8048 	bool need_reset = false;
8049 	int old_queue_pairs;
8050 	int ret = -EINVAL;
8051 	u16 mode;
8052 	int i;
8053 
8054 	old_queue_pairs = vsi->num_queue_pairs;
8055 	num_tc = mqprio_qopt->qopt.num_tc;
8056 	hw = mqprio_qopt->qopt.hw;
8057 	mode = mqprio_qopt->mode;
8058 	if (!hw) {
8059 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8060 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8061 		goto config_tc;
8062 	}
8063 
8064 	/* Check if MFP enabled */
8065 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
8066 		netdev_info(netdev,
8067 			    "Configuring TC not supported in MFP mode\n");
8068 		return ret;
8069 	}
8070 	switch (mode) {
8071 	case TC_MQPRIO_MODE_DCB:
8072 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8073 
8074 		/* Check if DCB enabled to continue */
8075 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
8076 			netdev_info(netdev,
8077 				    "DCB is not enabled for adapter\n");
8078 			return ret;
8079 		}
8080 
8081 		/* Check whether tc count is within enabled limit */
8082 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8083 			netdev_info(netdev,
8084 				    "TC count greater than enabled on link for adapter\n");
8085 			return ret;
8086 		}
8087 		break;
8088 	case TC_MQPRIO_MODE_CHANNEL:
8089 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
8090 			netdev_info(netdev,
8091 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8092 			return ret;
8093 		}
8094 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8095 			return ret;
8096 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8097 		if (ret)
8098 			return ret;
8099 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8100 		       sizeof(*mqprio_qopt));
8101 		pf->flags |= I40E_FLAG_TC_MQPRIO;
8102 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8103 		break;
8104 	default:
8105 		return -EINVAL;
8106 	}
8107 
8108 config_tc:
8109 	/* Generate TC map for number of tc requested */
8110 	for (i = 0; i < num_tc; i++)
8111 		enabled_tc |= BIT(i);
8112 
8113 	/* Requesting same TC configuration as already enabled */
8114 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8115 	    mode != TC_MQPRIO_MODE_CHANNEL)
8116 		return 0;
8117 
8118 	/* Quiesce VSI queues */
8119 	i40e_quiesce_vsi(vsi);
8120 
8121 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
8122 		i40e_remove_queue_channels(vsi);
8123 
8124 	/* Configure VSI for enabled TCs */
8125 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8126 	if (ret) {
8127 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8128 			    vsi->seid);
8129 		need_reset = true;
8130 		goto exit;
8131 	} else if (enabled_tc &&
8132 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8133 		netdev_info(netdev,
8134 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8135 			    vsi->tc_config.tc_info[0].qcount);
8136 		ret = -EINVAL;
8137 		need_reset = true;
8138 		goto exit;
8139 	}
8140 
8141 	dev_info(&vsi->back->pdev->dev,
8142 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8143 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8144 
8145 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
8146 		if (vsi->mqprio_qopt.max_rate[0]) {
8147 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
8148 
8149 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
8150 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8151 			if (!ret) {
8152 				u64 credits = max_tx_rate;
8153 
8154 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8155 				dev_dbg(&vsi->back->pdev->dev,
8156 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8157 					max_tx_rate,
8158 					credits,
8159 					vsi->seid);
8160 			} else {
8161 				need_reset = true;
8162 				goto exit;
8163 			}
8164 		}
8165 		ret = i40e_configure_queue_channels(vsi);
8166 		if (ret) {
8167 			vsi->num_queue_pairs = old_queue_pairs;
8168 			netdev_info(netdev,
8169 				    "Failed configuring queue channels\n");
8170 			need_reset = true;
8171 			goto exit;
8172 		}
8173 	}
8174 
8175 exit:
8176 	/* Reset the configuration data to defaults, only TC0 is enabled */
8177 	if (need_reset) {
8178 		i40e_vsi_set_default_tc_config(vsi);
8179 		need_reset = false;
8180 	}
8181 
8182 	/* Unquiesce VSI */
8183 	i40e_unquiesce_vsi(vsi);
8184 	return ret;
8185 }
8186 
8187 /**
8188  * i40e_set_cld_element - sets cloud filter element data
8189  * @filter: cloud filter rule
8190  * @cld: ptr to cloud filter element data
8191  *
8192  * This is helper function to copy data into cloud filter element
8193  **/
8194 static inline void
8195 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8196 		     struct i40e_aqc_cloud_filters_element_data *cld)
8197 {
8198 	u32 ipa;
8199 	int i;
8200 
8201 	memset(cld, 0, sizeof(*cld));
8202 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8203 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8204 
8205 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8206 		return;
8207 
8208 	if (filter->n_proto == ETH_P_IPV6) {
8209 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8210 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8211 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8212 
8213 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8214 		}
8215 	} else {
8216 		ipa = be32_to_cpu(filter->dst_ipv4);
8217 
8218 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8219 	}
8220 
8221 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8222 
8223 	/* tenant_id is not supported by FW now, once the support is enabled
8224 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8225 	 */
8226 	if (filter->tenant_id)
8227 		return;
8228 }
8229 
8230 /**
8231  * i40e_add_del_cloud_filter - Add/del cloud filter
8232  * @vsi: pointer to VSI
8233  * @filter: cloud filter rule
8234  * @add: if true, add, if false, delete
8235  *
8236  * Add or delete a cloud filter for a specific flow spec.
8237  * Returns 0 if the filter were successfully added.
8238  **/
8239 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8240 			      struct i40e_cloud_filter *filter, bool add)
8241 {
8242 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8243 	struct i40e_pf *pf = vsi->back;
8244 	int ret;
8245 	static const u16 flag_table[128] = {
8246 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8247 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8248 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8249 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8250 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8251 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8252 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8253 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8254 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8255 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8256 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8257 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8258 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8259 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8260 	};
8261 
8262 	if (filter->flags >= ARRAY_SIZE(flag_table))
8263 		return I40E_ERR_CONFIG;
8264 
8265 	memset(&cld_filter, 0, sizeof(cld_filter));
8266 
8267 	/* copy element needed to add cloud filter from filter */
8268 	i40e_set_cld_element(filter, &cld_filter);
8269 
8270 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8271 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8272 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8273 
8274 	if (filter->n_proto == ETH_P_IPV6)
8275 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8276 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8277 	else
8278 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8279 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8280 
8281 	if (add)
8282 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8283 						&cld_filter, 1);
8284 	else
8285 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8286 						&cld_filter, 1);
8287 	if (ret)
8288 		dev_dbg(&pf->pdev->dev,
8289 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8290 			add ? "add" : "delete", filter->dst_port, ret,
8291 			pf->hw.aq.asq_last_status);
8292 	else
8293 		dev_info(&pf->pdev->dev,
8294 			 "%s cloud filter for VSI: %d\n",
8295 			 add ? "Added" : "Deleted", filter->seid);
8296 	return ret;
8297 }
8298 
8299 /**
8300  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8301  * @vsi: pointer to VSI
8302  * @filter: cloud filter rule
8303  * @add: if true, add, if false, delete
8304  *
8305  * Add or delete a cloud filter for a specific flow spec using big buffer.
8306  * Returns 0 if the filter were successfully added.
8307  **/
8308 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8309 				      struct i40e_cloud_filter *filter,
8310 				      bool add)
8311 {
8312 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8313 	struct i40e_pf *pf = vsi->back;
8314 	int ret;
8315 
8316 	/* Both (src/dst) valid mac_addr are not supported */
8317 	if ((is_valid_ether_addr(filter->dst_mac) &&
8318 	     is_valid_ether_addr(filter->src_mac)) ||
8319 	    (is_multicast_ether_addr(filter->dst_mac) &&
8320 	     is_multicast_ether_addr(filter->src_mac)))
8321 		return -EOPNOTSUPP;
8322 
8323 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8324 	 * ports are not supported via big buffer now.
8325 	 */
8326 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8327 		return -EOPNOTSUPP;
8328 
8329 	/* adding filter using src_port/src_ip is not supported at this stage */
8330 	if (filter->src_port ||
8331 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8332 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8333 		return -EOPNOTSUPP;
8334 
8335 	memset(&cld_filter, 0, sizeof(cld_filter));
8336 
8337 	/* copy element needed to add cloud filter from filter */
8338 	i40e_set_cld_element(filter, &cld_filter.element);
8339 
8340 	if (is_valid_ether_addr(filter->dst_mac) ||
8341 	    is_valid_ether_addr(filter->src_mac) ||
8342 	    is_multicast_ether_addr(filter->dst_mac) ||
8343 	    is_multicast_ether_addr(filter->src_mac)) {
8344 		/* MAC + IP : unsupported mode */
8345 		if (filter->dst_ipv4)
8346 			return -EOPNOTSUPP;
8347 
8348 		/* since we validated that L4 port must be valid before
8349 		 * we get here, start with respective "flags" value
8350 		 * and update if vlan is present or not
8351 		 */
8352 		cld_filter.element.flags =
8353 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8354 
8355 		if (filter->vlan_id) {
8356 			cld_filter.element.flags =
8357 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8358 		}
8359 
8360 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8361 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8362 		cld_filter.element.flags =
8363 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8364 		if (filter->n_proto == ETH_P_IPV6)
8365 			cld_filter.element.flags |=
8366 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8367 		else
8368 			cld_filter.element.flags |=
8369 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8370 	} else {
8371 		dev_err(&pf->pdev->dev,
8372 			"either mac or ip has to be valid for cloud filter\n");
8373 		return -EINVAL;
8374 	}
8375 
8376 	/* Now copy L4 port in Byte 6..7 in general fields */
8377 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8378 						be16_to_cpu(filter->dst_port);
8379 
8380 	if (add) {
8381 		/* Validate current device switch mode, change if necessary */
8382 		ret = i40e_validate_and_set_switch_mode(vsi);
8383 		if (ret) {
8384 			dev_err(&pf->pdev->dev,
8385 				"failed to set switch mode, ret %d\n",
8386 				ret);
8387 			return ret;
8388 		}
8389 
8390 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8391 						   &cld_filter, 1);
8392 	} else {
8393 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8394 						   &cld_filter, 1);
8395 	}
8396 
8397 	if (ret)
8398 		dev_dbg(&pf->pdev->dev,
8399 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8400 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8401 	else
8402 		dev_info(&pf->pdev->dev,
8403 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8404 			 add ? "add" : "delete", filter->seid,
8405 			 ntohs(filter->dst_port));
8406 	return ret;
8407 }
8408 
8409 /**
8410  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8411  * @vsi: Pointer to VSI
8412  * @f: Pointer to struct flow_cls_offload
8413  * @filter: Pointer to cloud filter structure
8414  *
8415  **/
8416 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8417 				 struct flow_cls_offload *f,
8418 				 struct i40e_cloud_filter *filter)
8419 {
8420 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8421 	struct flow_dissector *dissector = rule->match.dissector;
8422 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8423 	struct i40e_pf *pf = vsi->back;
8424 	u8 field_flags = 0;
8425 
8426 	if (dissector->used_keys &
8427 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8428 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8429 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8430 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8431 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8432 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8433 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8434 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8435 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8436 			dissector->used_keys);
8437 		return -EOPNOTSUPP;
8438 	}
8439 
8440 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8441 		struct flow_match_enc_keyid match;
8442 
8443 		flow_rule_match_enc_keyid(rule, &match);
8444 		if (match.mask->keyid != 0)
8445 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8446 
8447 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8448 	}
8449 
8450 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8451 		struct flow_match_basic match;
8452 
8453 		flow_rule_match_basic(rule, &match);
8454 		n_proto_key = ntohs(match.key->n_proto);
8455 		n_proto_mask = ntohs(match.mask->n_proto);
8456 
8457 		if (n_proto_key == ETH_P_ALL) {
8458 			n_proto_key = 0;
8459 			n_proto_mask = 0;
8460 		}
8461 		filter->n_proto = n_proto_key & n_proto_mask;
8462 		filter->ip_proto = match.key->ip_proto;
8463 	}
8464 
8465 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8466 		struct flow_match_eth_addrs match;
8467 
8468 		flow_rule_match_eth_addrs(rule, &match);
8469 
8470 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8471 		if (!is_zero_ether_addr(match.mask->dst)) {
8472 			if (is_broadcast_ether_addr(match.mask->dst)) {
8473 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8474 			} else {
8475 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8476 					match.mask->dst);
8477 				return I40E_ERR_CONFIG;
8478 			}
8479 		}
8480 
8481 		if (!is_zero_ether_addr(match.mask->src)) {
8482 			if (is_broadcast_ether_addr(match.mask->src)) {
8483 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8484 			} else {
8485 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8486 					match.mask->src);
8487 				return I40E_ERR_CONFIG;
8488 			}
8489 		}
8490 		ether_addr_copy(filter->dst_mac, match.key->dst);
8491 		ether_addr_copy(filter->src_mac, match.key->src);
8492 	}
8493 
8494 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8495 		struct flow_match_vlan match;
8496 
8497 		flow_rule_match_vlan(rule, &match);
8498 		if (match.mask->vlan_id) {
8499 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8500 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8501 
8502 			} else {
8503 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8504 					match.mask->vlan_id);
8505 				return I40E_ERR_CONFIG;
8506 			}
8507 		}
8508 
8509 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8510 	}
8511 
8512 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8513 		struct flow_match_control match;
8514 
8515 		flow_rule_match_control(rule, &match);
8516 		addr_type = match.key->addr_type;
8517 	}
8518 
8519 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8520 		struct flow_match_ipv4_addrs match;
8521 
8522 		flow_rule_match_ipv4_addrs(rule, &match);
8523 		if (match.mask->dst) {
8524 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8525 				field_flags |= I40E_CLOUD_FIELD_IIP;
8526 			} else {
8527 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8528 					&match.mask->dst);
8529 				return I40E_ERR_CONFIG;
8530 			}
8531 		}
8532 
8533 		if (match.mask->src) {
8534 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8535 				field_flags |= I40E_CLOUD_FIELD_IIP;
8536 			} else {
8537 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8538 					&match.mask->src);
8539 				return I40E_ERR_CONFIG;
8540 			}
8541 		}
8542 
8543 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8544 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8545 			return I40E_ERR_CONFIG;
8546 		}
8547 		filter->dst_ipv4 = match.key->dst;
8548 		filter->src_ipv4 = match.key->src;
8549 	}
8550 
8551 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8552 		struct flow_match_ipv6_addrs match;
8553 
8554 		flow_rule_match_ipv6_addrs(rule, &match);
8555 
8556 		/* src and dest IPV6 address should not be LOOPBACK
8557 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8558 		 */
8559 		if (ipv6_addr_loopback(&match.key->dst) ||
8560 		    ipv6_addr_loopback(&match.key->src)) {
8561 			dev_err(&pf->pdev->dev,
8562 				"Bad ipv6, addr is LOOPBACK\n");
8563 			return I40E_ERR_CONFIG;
8564 		}
8565 		if (!ipv6_addr_any(&match.mask->dst) ||
8566 		    !ipv6_addr_any(&match.mask->src))
8567 			field_flags |= I40E_CLOUD_FIELD_IIP;
8568 
8569 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8570 		       sizeof(filter->src_ipv6));
8571 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8572 		       sizeof(filter->dst_ipv6));
8573 	}
8574 
8575 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8576 		struct flow_match_ports match;
8577 
8578 		flow_rule_match_ports(rule, &match);
8579 		if (match.mask->src) {
8580 			if (match.mask->src == cpu_to_be16(0xffff)) {
8581 				field_flags |= I40E_CLOUD_FIELD_IIP;
8582 			} else {
8583 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8584 					be16_to_cpu(match.mask->src));
8585 				return I40E_ERR_CONFIG;
8586 			}
8587 		}
8588 
8589 		if (match.mask->dst) {
8590 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8591 				field_flags |= I40E_CLOUD_FIELD_IIP;
8592 			} else {
8593 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8594 					be16_to_cpu(match.mask->dst));
8595 				return I40E_ERR_CONFIG;
8596 			}
8597 		}
8598 
8599 		filter->dst_port = match.key->dst;
8600 		filter->src_port = match.key->src;
8601 
8602 		switch (filter->ip_proto) {
8603 		case IPPROTO_TCP:
8604 		case IPPROTO_UDP:
8605 			break;
8606 		default:
8607 			dev_err(&pf->pdev->dev,
8608 				"Only UDP and TCP transport are supported\n");
8609 			return -EINVAL;
8610 		}
8611 	}
8612 	filter->flags = field_flags;
8613 	return 0;
8614 }
8615 
8616 /**
8617  * i40e_handle_tclass: Forward to a traffic class on the device
8618  * @vsi: Pointer to VSI
8619  * @tc: traffic class index on the device
8620  * @filter: Pointer to cloud filter structure
8621  *
8622  **/
8623 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8624 			      struct i40e_cloud_filter *filter)
8625 {
8626 	struct i40e_channel *ch, *ch_tmp;
8627 
8628 	/* direct to a traffic class on the same device */
8629 	if (tc == 0) {
8630 		filter->seid = vsi->seid;
8631 		return 0;
8632 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8633 		if (!filter->dst_port) {
8634 			dev_err(&vsi->back->pdev->dev,
8635 				"Specify destination port to direct to traffic class that is not default\n");
8636 			return -EINVAL;
8637 		}
8638 		if (list_empty(&vsi->ch_list))
8639 			return -EINVAL;
8640 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8641 					 list) {
8642 			if (ch->seid == vsi->tc_seid_map[tc])
8643 				filter->seid = ch->seid;
8644 		}
8645 		return 0;
8646 	}
8647 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8648 	return -EINVAL;
8649 }
8650 
8651 /**
8652  * i40e_configure_clsflower - Configure tc flower filters
8653  * @vsi: Pointer to VSI
8654  * @cls_flower: Pointer to struct flow_cls_offload
8655  *
8656  **/
8657 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8658 				    struct flow_cls_offload *cls_flower)
8659 {
8660 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8661 	struct i40e_cloud_filter *filter = NULL;
8662 	struct i40e_pf *pf = vsi->back;
8663 	int err = 0;
8664 
8665 	if (tc < 0) {
8666 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8667 		return -EOPNOTSUPP;
8668 	}
8669 
8670 	if (!tc) {
8671 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8672 		return -EINVAL;
8673 	}
8674 
8675 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8676 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8677 		return -EBUSY;
8678 
8679 	if (pf->fdir_pf_active_filters ||
8680 	    (!hlist_empty(&pf->fdir_filter_list))) {
8681 		dev_err(&vsi->back->pdev->dev,
8682 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8683 		return -EINVAL;
8684 	}
8685 
8686 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8687 		dev_err(&vsi->back->pdev->dev,
8688 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8689 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8690 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8691 	}
8692 
8693 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8694 	if (!filter)
8695 		return -ENOMEM;
8696 
8697 	filter->cookie = cls_flower->cookie;
8698 
8699 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8700 	if (err < 0)
8701 		goto err;
8702 
8703 	err = i40e_handle_tclass(vsi, tc, filter);
8704 	if (err < 0)
8705 		goto err;
8706 
8707 	/* Add cloud filter */
8708 	if (filter->dst_port)
8709 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8710 	else
8711 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8712 
8713 	if (err) {
8714 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8715 			err);
8716 		goto err;
8717 	}
8718 
8719 	/* add filter to the ordered list */
8720 	INIT_HLIST_NODE(&filter->cloud_node);
8721 
8722 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8723 
8724 	pf->num_cloud_filters++;
8725 
8726 	return err;
8727 err:
8728 	kfree(filter);
8729 	return err;
8730 }
8731 
8732 /**
8733  * i40e_find_cloud_filter - Find the could filter in the list
8734  * @vsi: Pointer to VSI
8735  * @cookie: filter specific cookie
8736  *
8737  **/
8738 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8739 							unsigned long *cookie)
8740 {
8741 	struct i40e_cloud_filter *filter = NULL;
8742 	struct hlist_node *node2;
8743 
8744 	hlist_for_each_entry_safe(filter, node2,
8745 				  &vsi->back->cloud_filter_list, cloud_node)
8746 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8747 			return filter;
8748 	return NULL;
8749 }
8750 
8751 /**
8752  * i40e_delete_clsflower - Remove tc flower filters
8753  * @vsi: Pointer to VSI
8754  * @cls_flower: Pointer to struct flow_cls_offload
8755  *
8756  **/
8757 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8758 				 struct flow_cls_offload *cls_flower)
8759 {
8760 	struct i40e_cloud_filter *filter = NULL;
8761 	struct i40e_pf *pf = vsi->back;
8762 	int err = 0;
8763 
8764 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8765 
8766 	if (!filter)
8767 		return -EINVAL;
8768 
8769 	hash_del(&filter->cloud_node);
8770 
8771 	if (filter->dst_port)
8772 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8773 	else
8774 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8775 
8776 	kfree(filter);
8777 	if (err) {
8778 		dev_err(&pf->pdev->dev,
8779 			"Failed to delete cloud filter, err %s\n",
8780 			i40e_stat_str(&pf->hw, err));
8781 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8782 	}
8783 
8784 	pf->num_cloud_filters--;
8785 	if (!pf->num_cloud_filters)
8786 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8787 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8788 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8789 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8790 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8791 		}
8792 	return 0;
8793 }
8794 
8795 /**
8796  * i40e_setup_tc_cls_flower - flower classifier offloads
8797  * @np: net device to configure
8798  * @cls_flower: offload data
8799  **/
8800 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8801 				    struct flow_cls_offload *cls_flower)
8802 {
8803 	struct i40e_vsi *vsi = np->vsi;
8804 
8805 	switch (cls_flower->command) {
8806 	case FLOW_CLS_REPLACE:
8807 		return i40e_configure_clsflower(vsi, cls_flower);
8808 	case FLOW_CLS_DESTROY:
8809 		return i40e_delete_clsflower(vsi, cls_flower);
8810 	case FLOW_CLS_STATS:
8811 		return -EOPNOTSUPP;
8812 	default:
8813 		return -EOPNOTSUPP;
8814 	}
8815 }
8816 
8817 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8818 				  void *cb_priv)
8819 {
8820 	struct i40e_netdev_priv *np = cb_priv;
8821 
8822 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8823 		return -EOPNOTSUPP;
8824 
8825 	switch (type) {
8826 	case TC_SETUP_CLSFLOWER:
8827 		return i40e_setup_tc_cls_flower(np, type_data);
8828 
8829 	default:
8830 		return -EOPNOTSUPP;
8831 	}
8832 }
8833 
8834 static LIST_HEAD(i40e_block_cb_list);
8835 
8836 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8837 			   void *type_data)
8838 {
8839 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8840 
8841 	switch (type) {
8842 	case TC_SETUP_QDISC_MQPRIO:
8843 		return i40e_setup_tc(netdev, type_data);
8844 	case TC_SETUP_BLOCK:
8845 		return flow_block_cb_setup_simple(type_data,
8846 						  &i40e_block_cb_list,
8847 						  i40e_setup_tc_block_cb,
8848 						  np, np, true);
8849 	default:
8850 		return -EOPNOTSUPP;
8851 	}
8852 }
8853 
8854 /**
8855  * i40e_open - Called when a network interface is made active
8856  * @netdev: network interface device structure
8857  *
8858  * The open entry point is called when a network interface is made
8859  * active by the system (IFF_UP).  At this point all resources needed
8860  * for transmit and receive operations are allocated, the interrupt
8861  * handler is registered with the OS, the netdev watchdog subtask is
8862  * enabled, and the stack is notified that the interface is ready.
8863  *
8864  * Returns 0 on success, negative value on failure
8865  **/
8866 int i40e_open(struct net_device *netdev)
8867 {
8868 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8869 	struct i40e_vsi *vsi = np->vsi;
8870 	struct i40e_pf *pf = vsi->back;
8871 	int err;
8872 
8873 	/* disallow open during test or if eeprom is broken */
8874 	if (test_bit(__I40E_TESTING, pf->state) ||
8875 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8876 		return -EBUSY;
8877 
8878 	netif_carrier_off(netdev);
8879 
8880 	if (i40e_force_link_state(pf, true))
8881 		return -EAGAIN;
8882 
8883 	err = i40e_vsi_open(vsi);
8884 	if (err)
8885 		return err;
8886 
8887 	/* configure global TSO hardware offload settings */
8888 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8889 						       TCP_FLAG_FIN) >> 16);
8890 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8891 						       TCP_FLAG_FIN |
8892 						       TCP_FLAG_CWR) >> 16);
8893 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8894 	udp_tunnel_get_rx_info(netdev);
8895 
8896 	return 0;
8897 }
8898 
8899 /**
8900  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
8901  * @vsi: vsi structure
8902  *
8903  * This updates netdev's number of tx/rx queues
8904  *
8905  * Returns status of setting tx/rx queues
8906  **/
8907 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
8908 {
8909 	int ret;
8910 
8911 	ret = netif_set_real_num_rx_queues(vsi->netdev,
8912 					   vsi->num_queue_pairs);
8913 	if (ret)
8914 		return ret;
8915 
8916 	return netif_set_real_num_tx_queues(vsi->netdev,
8917 					    vsi->num_queue_pairs);
8918 }
8919 
8920 /**
8921  * i40e_vsi_open -
8922  * @vsi: the VSI to open
8923  *
8924  * Finish initialization of the VSI.
8925  *
8926  * Returns 0 on success, negative value on failure
8927  *
8928  * Note: expects to be called while under rtnl_lock()
8929  **/
8930 int i40e_vsi_open(struct i40e_vsi *vsi)
8931 {
8932 	struct i40e_pf *pf = vsi->back;
8933 	char int_name[I40E_INT_NAME_STR_LEN];
8934 	int err;
8935 
8936 	/* allocate descriptors */
8937 	err = i40e_vsi_setup_tx_resources(vsi);
8938 	if (err)
8939 		goto err_setup_tx;
8940 	err = i40e_vsi_setup_rx_resources(vsi);
8941 	if (err)
8942 		goto err_setup_rx;
8943 
8944 	err = i40e_vsi_configure(vsi);
8945 	if (err)
8946 		goto err_setup_rx;
8947 
8948 	if (vsi->netdev) {
8949 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
8950 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
8951 		err = i40e_vsi_request_irq(vsi, int_name);
8952 		if (err)
8953 			goto err_setup_rx;
8954 
8955 		/* Notify the stack of the actual queue counts. */
8956 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
8957 		if (err)
8958 			goto err_set_queues;
8959 
8960 	} else if (vsi->type == I40E_VSI_FDIR) {
8961 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
8962 			 dev_driver_string(&pf->pdev->dev),
8963 			 dev_name(&pf->pdev->dev));
8964 		err = i40e_vsi_request_irq(vsi, int_name);
8965 		if (err)
8966 			goto err_setup_rx;
8967 
8968 	} else {
8969 		err = -EINVAL;
8970 		goto err_setup_rx;
8971 	}
8972 
8973 	err = i40e_up_complete(vsi);
8974 	if (err)
8975 		goto err_up_complete;
8976 
8977 	return 0;
8978 
8979 err_up_complete:
8980 	i40e_down(vsi);
8981 err_set_queues:
8982 	i40e_vsi_free_irq(vsi);
8983 err_setup_rx:
8984 	i40e_vsi_free_rx_resources(vsi);
8985 err_setup_tx:
8986 	i40e_vsi_free_tx_resources(vsi);
8987 	if (vsi == pf->vsi[pf->lan_vsi])
8988 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
8989 
8990 	return err;
8991 }
8992 
8993 /**
8994  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
8995  * @pf: Pointer to PF
8996  *
8997  * This function destroys the hlist where all the Flow Director
8998  * filters were saved.
8999  **/
9000 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9001 {
9002 	struct i40e_fdir_filter *filter;
9003 	struct i40e_flex_pit *pit_entry, *tmp;
9004 	struct hlist_node *node2;
9005 
9006 	hlist_for_each_entry_safe(filter, node2,
9007 				  &pf->fdir_filter_list, fdir_node) {
9008 		hlist_del(&filter->fdir_node);
9009 		kfree(filter);
9010 	}
9011 
9012 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9013 		list_del(&pit_entry->list);
9014 		kfree(pit_entry);
9015 	}
9016 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9017 
9018 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9019 		list_del(&pit_entry->list);
9020 		kfree(pit_entry);
9021 	}
9022 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9023 
9024 	pf->fdir_pf_active_filters = 0;
9025 	i40e_reset_fdir_filter_cnt(pf);
9026 
9027 	/* Reprogram the default input set for TCP/IPv4 */
9028 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9029 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9030 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9031 
9032 	/* Reprogram the default input set for TCP/IPv6 */
9033 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9034 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9035 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9036 
9037 	/* Reprogram the default input set for UDP/IPv4 */
9038 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9039 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9040 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9041 
9042 	/* Reprogram the default input set for UDP/IPv6 */
9043 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9044 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9045 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9046 
9047 	/* Reprogram the default input set for SCTP/IPv4 */
9048 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9049 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9050 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9051 
9052 	/* Reprogram the default input set for SCTP/IPv6 */
9053 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9054 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9055 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9056 
9057 	/* Reprogram the default input set for Other/IPv4 */
9058 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9059 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9060 
9061 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9062 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9063 
9064 	/* Reprogram the default input set for Other/IPv6 */
9065 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9066 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9067 
9068 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9069 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9070 }
9071 
9072 /**
9073  * i40e_cloud_filter_exit - Cleans up the cloud filters
9074  * @pf: Pointer to PF
9075  *
9076  * This function destroys the hlist where all the cloud filters
9077  * were saved.
9078  **/
9079 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9080 {
9081 	struct i40e_cloud_filter *cfilter;
9082 	struct hlist_node *node;
9083 
9084 	hlist_for_each_entry_safe(cfilter, node,
9085 				  &pf->cloud_filter_list, cloud_node) {
9086 		hlist_del(&cfilter->cloud_node);
9087 		kfree(cfilter);
9088 	}
9089 	pf->num_cloud_filters = 0;
9090 
9091 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
9092 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
9093 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
9094 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
9095 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
9096 	}
9097 }
9098 
9099 /**
9100  * i40e_close - Disables a network interface
9101  * @netdev: network interface device structure
9102  *
9103  * The close entry point is called when an interface is de-activated
9104  * by the OS.  The hardware is still under the driver's control, but
9105  * this netdev interface is disabled.
9106  *
9107  * Returns 0, this is not allowed to fail
9108  **/
9109 int i40e_close(struct net_device *netdev)
9110 {
9111 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9112 	struct i40e_vsi *vsi = np->vsi;
9113 
9114 	i40e_vsi_close(vsi);
9115 
9116 	return 0;
9117 }
9118 
9119 /**
9120  * i40e_do_reset - Start a PF or Core Reset sequence
9121  * @pf: board private structure
9122  * @reset_flags: which reset is requested
9123  * @lock_acquired: indicates whether or not the lock has been acquired
9124  * before this function was called.
9125  *
9126  * The essential difference in resets is that the PF Reset
9127  * doesn't clear the packet buffers, doesn't reset the PE
9128  * firmware, and doesn't bother the other PFs on the chip.
9129  **/
9130 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9131 {
9132 	u32 val;
9133 
9134 	/* do the biggest reset indicated */
9135 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9136 
9137 		/* Request a Global Reset
9138 		 *
9139 		 * This will start the chip's countdown to the actual full
9140 		 * chip reset event, and a warning interrupt to be sent
9141 		 * to all PFs, including the requestor.  Our handler
9142 		 * for the warning interrupt will deal with the shutdown
9143 		 * and recovery of the switch setup.
9144 		 */
9145 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9146 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9147 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9148 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9149 
9150 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9151 
9152 		/* Request a Core Reset
9153 		 *
9154 		 * Same as Global Reset, except does *not* include the MAC/PHY
9155 		 */
9156 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9157 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9158 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9159 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9160 		i40e_flush(&pf->hw);
9161 
9162 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9163 
9164 		/* Request a PF Reset
9165 		 *
9166 		 * Resets only the PF-specific registers
9167 		 *
9168 		 * This goes directly to the tear-down and rebuild of
9169 		 * the switch, since we need to do all the recovery as
9170 		 * for the Core Reset.
9171 		 */
9172 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9173 		i40e_handle_reset_warning(pf, lock_acquired);
9174 
9175 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9176 		/* Request a PF Reset
9177 		 *
9178 		 * Resets PF and reinitializes PFs VSI.
9179 		 */
9180 		i40e_prep_for_reset(pf);
9181 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9182 		dev_info(&pf->pdev->dev,
9183 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9184 			 "FW LLDP is disabled\n" :
9185 			 "FW LLDP is enabled\n");
9186 
9187 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9188 		int v;
9189 
9190 		/* Find the VSI(s) that requested a re-init */
9191 		dev_info(&pf->pdev->dev,
9192 			 "VSI reinit requested\n");
9193 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9194 			struct i40e_vsi *vsi = pf->vsi[v];
9195 
9196 			if (vsi != NULL &&
9197 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9198 					       vsi->state))
9199 				i40e_vsi_reinit_locked(pf->vsi[v]);
9200 		}
9201 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9202 		int v;
9203 
9204 		/* Find the VSI(s) that needs to be brought down */
9205 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9206 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9207 			struct i40e_vsi *vsi = pf->vsi[v];
9208 
9209 			if (vsi != NULL &&
9210 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9211 					       vsi->state)) {
9212 				set_bit(__I40E_VSI_DOWN, vsi->state);
9213 				i40e_down(vsi);
9214 			}
9215 		}
9216 	} else {
9217 		dev_info(&pf->pdev->dev,
9218 			 "bad reset request 0x%08x\n", reset_flags);
9219 	}
9220 }
9221 
9222 #ifdef CONFIG_I40E_DCB
9223 /**
9224  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9225  * @pf: board private structure
9226  * @old_cfg: current DCB config
9227  * @new_cfg: new DCB config
9228  **/
9229 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9230 			    struct i40e_dcbx_config *old_cfg,
9231 			    struct i40e_dcbx_config *new_cfg)
9232 {
9233 	bool need_reconfig = false;
9234 
9235 	/* Check if ETS configuration has changed */
9236 	if (memcmp(&new_cfg->etscfg,
9237 		   &old_cfg->etscfg,
9238 		   sizeof(new_cfg->etscfg))) {
9239 		/* If Priority Table has changed reconfig is needed */
9240 		if (memcmp(&new_cfg->etscfg.prioritytable,
9241 			   &old_cfg->etscfg.prioritytable,
9242 			   sizeof(new_cfg->etscfg.prioritytable))) {
9243 			need_reconfig = true;
9244 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9245 		}
9246 
9247 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9248 			   &old_cfg->etscfg.tcbwtable,
9249 			   sizeof(new_cfg->etscfg.tcbwtable)))
9250 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9251 
9252 		if (memcmp(&new_cfg->etscfg.tsatable,
9253 			   &old_cfg->etscfg.tsatable,
9254 			   sizeof(new_cfg->etscfg.tsatable)))
9255 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9256 	}
9257 
9258 	/* Check if PFC configuration has changed */
9259 	if (memcmp(&new_cfg->pfc,
9260 		   &old_cfg->pfc,
9261 		   sizeof(new_cfg->pfc))) {
9262 		need_reconfig = true;
9263 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9264 	}
9265 
9266 	/* Check if APP Table has changed */
9267 	if (memcmp(&new_cfg->app,
9268 		   &old_cfg->app,
9269 		   sizeof(new_cfg->app))) {
9270 		need_reconfig = true;
9271 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9272 	}
9273 
9274 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9275 	return need_reconfig;
9276 }
9277 
9278 /**
9279  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9280  * @pf: board private structure
9281  * @e: event info posted on ARQ
9282  **/
9283 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9284 				  struct i40e_arq_event_info *e)
9285 {
9286 	struct i40e_aqc_lldp_get_mib *mib =
9287 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9288 	struct i40e_hw *hw = &pf->hw;
9289 	struct i40e_dcbx_config tmp_dcbx_cfg;
9290 	bool need_reconfig = false;
9291 	int ret = 0;
9292 	u8 type;
9293 
9294 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9295 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9296 	    (hw->phy.link_info.link_speed &
9297 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9298 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9299 		/* let firmware decide if the DCB should be disabled */
9300 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9301 
9302 	/* Not DCB capable or capability disabled */
9303 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9304 		return ret;
9305 
9306 	/* Ignore if event is not for Nearest Bridge */
9307 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9308 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9309 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9310 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9311 		return ret;
9312 
9313 	/* Check MIB Type and return if event for Remote MIB update */
9314 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9315 	dev_dbg(&pf->pdev->dev,
9316 		"LLDP event mib type %s\n", type ? "remote" : "local");
9317 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9318 		/* Update the remote cached instance and return */
9319 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9320 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9321 				&hw->remote_dcbx_config);
9322 		goto exit;
9323 	}
9324 
9325 	/* Store the old configuration */
9326 	tmp_dcbx_cfg = hw->local_dcbx_config;
9327 
9328 	/* Reset the old DCBx configuration data */
9329 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9330 	/* Get updated DCBX data from firmware */
9331 	ret = i40e_get_dcb_config(&pf->hw);
9332 	if (ret) {
9333 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9334 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9335 		    (hw->phy.link_info.link_speed &
9336 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9337 			dev_warn(&pf->pdev->dev,
9338 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9339 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9340 		} else {
9341 			dev_info(&pf->pdev->dev,
9342 				 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
9343 				 i40e_stat_str(&pf->hw, ret),
9344 				 i40e_aq_str(&pf->hw,
9345 					     pf->hw.aq.asq_last_status));
9346 		}
9347 		goto exit;
9348 	}
9349 
9350 	/* No change detected in DCBX configs */
9351 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9352 		    sizeof(tmp_dcbx_cfg))) {
9353 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9354 		goto exit;
9355 	}
9356 
9357 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9358 					       &hw->local_dcbx_config);
9359 
9360 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9361 
9362 	if (!need_reconfig)
9363 		goto exit;
9364 
9365 	/* Enable DCB tagging only when more than one TC */
9366 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9367 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9368 	else
9369 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9370 
9371 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9372 	/* Reconfiguration needed quiesce all VSIs */
9373 	i40e_pf_quiesce_all_vsi(pf);
9374 
9375 	/* Changes in configuration update VEB/VSI */
9376 	i40e_dcb_reconfigure(pf);
9377 
9378 	ret = i40e_resume_port_tx(pf);
9379 
9380 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9381 	/* In case of error no point in resuming VSIs */
9382 	if (ret)
9383 		goto exit;
9384 
9385 	/* Wait for the PF's queues to be disabled */
9386 	ret = i40e_pf_wait_queues_disabled(pf);
9387 	if (ret) {
9388 		/* Schedule PF reset to recover */
9389 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9390 		i40e_service_event_schedule(pf);
9391 	} else {
9392 		i40e_pf_unquiesce_all_vsi(pf);
9393 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9394 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9395 	}
9396 
9397 exit:
9398 	return ret;
9399 }
9400 #endif /* CONFIG_I40E_DCB */
9401 
9402 /**
9403  * i40e_do_reset_safe - Protected reset path for userland calls.
9404  * @pf: board private structure
9405  * @reset_flags: which reset is requested
9406  *
9407  **/
9408 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9409 {
9410 	rtnl_lock();
9411 	i40e_do_reset(pf, reset_flags, true);
9412 	rtnl_unlock();
9413 }
9414 
9415 /**
9416  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9417  * @pf: board private structure
9418  * @e: event info posted on ARQ
9419  *
9420  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9421  * and VF queues
9422  **/
9423 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9424 					   struct i40e_arq_event_info *e)
9425 {
9426 	struct i40e_aqc_lan_overflow *data =
9427 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9428 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9429 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9430 	struct i40e_hw *hw = &pf->hw;
9431 	struct i40e_vf *vf;
9432 	u16 vf_id;
9433 
9434 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9435 		queue, qtx_ctl);
9436 
9437 	/* Queue belongs to VF, find the VF and issue VF reset */
9438 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9439 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9440 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9441 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9442 		vf_id -= hw->func_caps.vf_base_id;
9443 		vf = &pf->vf[vf_id];
9444 		i40e_vc_notify_vf_reset(vf);
9445 		/* Allow VF to process pending reset notification */
9446 		msleep(20);
9447 		i40e_reset_vf(vf, false);
9448 	}
9449 }
9450 
9451 /**
9452  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9453  * @pf: board private structure
9454  **/
9455 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9456 {
9457 	u32 val, fcnt_prog;
9458 
9459 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9460 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9461 	return fcnt_prog;
9462 }
9463 
9464 /**
9465  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9466  * @pf: board private structure
9467  **/
9468 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9469 {
9470 	u32 val, fcnt_prog;
9471 
9472 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9473 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9474 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9475 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9476 	return fcnt_prog;
9477 }
9478 
9479 /**
9480  * i40e_get_global_fd_count - Get total FD filters programmed on device
9481  * @pf: board private structure
9482  **/
9483 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9484 {
9485 	u32 val, fcnt_prog;
9486 
9487 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9488 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9489 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9490 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9491 	return fcnt_prog;
9492 }
9493 
9494 /**
9495  * i40e_reenable_fdir_sb - Restore FDir SB capability
9496  * @pf: board private structure
9497  **/
9498 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9499 {
9500 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9501 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9502 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9503 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9504 }
9505 
9506 /**
9507  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9508  * @pf: board private structure
9509  **/
9510 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9511 {
9512 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9513 		/* ATR uses the same filtering logic as SB rules. It only
9514 		 * functions properly if the input set mask is at the default
9515 		 * settings. It is safe to restore the default input set
9516 		 * because there are no active TCPv4 filter rules.
9517 		 */
9518 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9519 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9520 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9521 
9522 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9523 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9524 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9525 	}
9526 }
9527 
9528 /**
9529  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9530  * @pf: board private structure
9531  * @filter: FDir filter to remove
9532  */
9533 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9534 				       struct i40e_fdir_filter *filter)
9535 {
9536 	/* Update counters */
9537 	pf->fdir_pf_active_filters--;
9538 	pf->fd_inv = 0;
9539 
9540 	switch (filter->flow_type) {
9541 	case TCP_V4_FLOW:
9542 		pf->fd_tcp4_filter_cnt--;
9543 		break;
9544 	case UDP_V4_FLOW:
9545 		pf->fd_udp4_filter_cnt--;
9546 		break;
9547 	case SCTP_V4_FLOW:
9548 		pf->fd_sctp4_filter_cnt--;
9549 		break;
9550 	case TCP_V6_FLOW:
9551 		pf->fd_tcp6_filter_cnt--;
9552 		break;
9553 	case UDP_V6_FLOW:
9554 		pf->fd_udp6_filter_cnt--;
9555 		break;
9556 	case SCTP_V6_FLOW:
9557 		pf->fd_udp6_filter_cnt--;
9558 		break;
9559 	case IP_USER_FLOW:
9560 		switch (filter->ipl4_proto) {
9561 		case IPPROTO_TCP:
9562 			pf->fd_tcp4_filter_cnt--;
9563 			break;
9564 		case IPPROTO_UDP:
9565 			pf->fd_udp4_filter_cnt--;
9566 			break;
9567 		case IPPROTO_SCTP:
9568 			pf->fd_sctp4_filter_cnt--;
9569 			break;
9570 		case IPPROTO_IP:
9571 			pf->fd_ip4_filter_cnt--;
9572 			break;
9573 		}
9574 		break;
9575 	case IPV6_USER_FLOW:
9576 		switch (filter->ipl4_proto) {
9577 		case IPPROTO_TCP:
9578 			pf->fd_tcp6_filter_cnt--;
9579 			break;
9580 		case IPPROTO_UDP:
9581 			pf->fd_udp6_filter_cnt--;
9582 			break;
9583 		case IPPROTO_SCTP:
9584 			pf->fd_sctp6_filter_cnt--;
9585 			break;
9586 		case IPPROTO_IP:
9587 			pf->fd_ip6_filter_cnt--;
9588 			break;
9589 		}
9590 		break;
9591 	}
9592 
9593 	/* Remove the filter from the list and free memory */
9594 	hlist_del(&filter->fdir_node);
9595 	kfree(filter);
9596 }
9597 
9598 /**
9599  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9600  * @pf: board private structure
9601  **/
9602 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9603 {
9604 	struct i40e_fdir_filter *filter;
9605 	u32 fcnt_prog, fcnt_avail;
9606 	struct hlist_node *node;
9607 
9608 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9609 		return;
9610 
9611 	/* Check if we have enough room to re-enable FDir SB capability. */
9612 	fcnt_prog = i40e_get_global_fd_count(pf);
9613 	fcnt_avail = pf->fdir_pf_filter_count;
9614 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9615 	    (pf->fd_add_err == 0) ||
9616 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9617 		i40e_reenable_fdir_sb(pf);
9618 
9619 	/* We should wait for even more space before re-enabling ATR.
9620 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9621 	 * rules active.
9622 	 */
9623 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9624 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9625 		i40e_reenable_fdir_atr(pf);
9626 
9627 	/* if hw had a problem adding a filter, delete it */
9628 	if (pf->fd_inv > 0) {
9629 		hlist_for_each_entry_safe(filter, node,
9630 					  &pf->fdir_filter_list, fdir_node)
9631 			if (filter->fd_id == pf->fd_inv)
9632 				i40e_delete_invalid_filter(pf, filter);
9633 	}
9634 }
9635 
9636 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9637 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9638 /**
9639  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9640  * @pf: board private structure
9641  **/
9642 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9643 {
9644 	unsigned long min_flush_time;
9645 	int flush_wait_retry = 50;
9646 	bool disable_atr = false;
9647 	int fd_room;
9648 	int reg;
9649 
9650 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9651 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9652 		return;
9653 
9654 	/* If the flush is happening too quick and we have mostly SB rules we
9655 	 * should not re-enable ATR for some time.
9656 	 */
9657 	min_flush_time = pf->fd_flush_timestamp +
9658 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9659 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9660 
9661 	if (!(time_after(jiffies, min_flush_time)) &&
9662 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9663 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9664 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9665 		disable_atr = true;
9666 	}
9667 
9668 	pf->fd_flush_timestamp = jiffies;
9669 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9670 	/* flush all filters */
9671 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9672 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9673 	i40e_flush(&pf->hw);
9674 	pf->fd_flush_cnt++;
9675 	pf->fd_add_err = 0;
9676 	do {
9677 		/* Check FD flush status every 5-6msec */
9678 		usleep_range(5000, 6000);
9679 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9680 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9681 			break;
9682 	} while (flush_wait_retry--);
9683 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9684 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9685 	} else {
9686 		/* replay sideband filters */
9687 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9688 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9689 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9690 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9691 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9692 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9693 	}
9694 }
9695 
9696 /**
9697  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9698  * @pf: board private structure
9699  **/
9700 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9701 {
9702 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9703 }
9704 
9705 /**
9706  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9707  * @pf: board private structure
9708  **/
9709 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9710 {
9711 
9712 	/* if interface is down do nothing */
9713 	if (test_bit(__I40E_DOWN, pf->state))
9714 		return;
9715 
9716 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9717 		i40e_fdir_flush_and_replay(pf);
9718 
9719 	i40e_fdir_check_and_reenable(pf);
9720 
9721 }
9722 
9723 /**
9724  * i40e_vsi_link_event - notify VSI of a link event
9725  * @vsi: vsi to be notified
9726  * @link_up: link up or down
9727  **/
9728 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9729 {
9730 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9731 		return;
9732 
9733 	switch (vsi->type) {
9734 	case I40E_VSI_MAIN:
9735 		if (!vsi->netdev || !vsi->netdev_registered)
9736 			break;
9737 
9738 		if (link_up) {
9739 			netif_carrier_on(vsi->netdev);
9740 			netif_tx_wake_all_queues(vsi->netdev);
9741 		} else {
9742 			netif_carrier_off(vsi->netdev);
9743 			netif_tx_stop_all_queues(vsi->netdev);
9744 		}
9745 		break;
9746 
9747 	case I40E_VSI_SRIOV:
9748 	case I40E_VSI_VMDQ2:
9749 	case I40E_VSI_CTRL:
9750 	case I40E_VSI_IWARP:
9751 	case I40E_VSI_MIRROR:
9752 	default:
9753 		/* there is no notification for other VSIs */
9754 		break;
9755 	}
9756 }
9757 
9758 /**
9759  * i40e_veb_link_event - notify elements on the veb of a link event
9760  * @veb: veb to be notified
9761  * @link_up: link up or down
9762  **/
9763 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9764 {
9765 	struct i40e_pf *pf;
9766 	int i;
9767 
9768 	if (!veb || !veb->pf)
9769 		return;
9770 	pf = veb->pf;
9771 
9772 	/* depth first... */
9773 	for (i = 0; i < I40E_MAX_VEB; i++)
9774 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9775 			i40e_veb_link_event(pf->veb[i], link_up);
9776 
9777 	/* ... now the local VSIs */
9778 	for (i = 0; i < pf->num_alloc_vsi; i++)
9779 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9780 			i40e_vsi_link_event(pf->vsi[i], link_up);
9781 }
9782 
9783 /**
9784  * i40e_link_event - Update netif_carrier status
9785  * @pf: board private structure
9786  **/
9787 static void i40e_link_event(struct i40e_pf *pf)
9788 {
9789 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9790 	u8 new_link_speed, old_link_speed;
9791 	i40e_status status;
9792 	bool new_link, old_link;
9793 #ifdef CONFIG_I40E_DCB
9794 	int err;
9795 #endif /* CONFIG_I40E_DCB */
9796 
9797 	/* set this to force the get_link_status call to refresh state */
9798 	pf->hw.phy.get_link_info = true;
9799 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9800 	status = i40e_get_link_status(&pf->hw, &new_link);
9801 
9802 	/* On success, disable temp link polling */
9803 	if (status == I40E_SUCCESS) {
9804 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9805 	} else {
9806 		/* Enable link polling temporarily until i40e_get_link_status
9807 		 * returns I40E_SUCCESS
9808 		 */
9809 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9810 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9811 			status);
9812 		return;
9813 	}
9814 
9815 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9816 	new_link_speed = pf->hw.phy.link_info.link_speed;
9817 
9818 	if (new_link == old_link &&
9819 	    new_link_speed == old_link_speed &&
9820 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9821 	     new_link == netif_carrier_ok(vsi->netdev)))
9822 		return;
9823 
9824 	i40e_print_link_message(vsi, new_link);
9825 
9826 	/* Notify the base of the switch tree connected to
9827 	 * the link.  Floating VEBs are not notified.
9828 	 */
9829 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9830 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9831 	else
9832 		i40e_vsi_link_event(vsi, new_link);
9833 
9834 	if (pf->vf)
9835 		i40e_vc_notify_link_state(pf);
9836 
9837 	if (pf->flags & I40E_FLAG_PTP)
9838 		i40e_ptp_set_increment(pf);
9839 #ifdef CONFIG_I40E_DCB
9840 	if (new_link == old_link)
9841 		return;
9842 	/* Not SW DCB so firmware will take care of default settings */
9843 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9844 		return;
9845 
9846 	/* We cover here only link down, as after link up in case of SW DCB
9847 	 * SW LLDP agent will take care of setting it up
9848 	 */
9849 	if (!new_link) {
9850 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9851 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9852 		err = i40e_dcb_sw_default_config(pf);
9853 		if (err) {
9854 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9855 				       I40E_FLAG_DCB_ENABLED);
9856 		} else {
9857 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9858 				       DCB_CAP_DCBX_VER_IEEE;
9859 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9860 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9861 		}
9862 	}
9863 #endif /* CONFIG_I40E_DCB */
9864 }
9865 
9866 /**
9867  * i40e_watchdog_subtask - periodic checks not using event driven response
9868  * @pf: board private structure
9869  **/
9870 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9871 {
9872 	int i;
9873 
9874 	/* if interface is down do nothing */
9875 	if (test_bit(__I40E_DOWN, pf->state) ||
9876 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9877 		return;
9878 
9879 	/* make sure we don't do these things too often */
9880 	if (time_before(jiffies, (pf->service_timer_previous +
9881 				  pf->service_timer_period)))
9882 		return;
9883 	pf->service_timer_previous = jiffies;
9884 
9885 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9886 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9887 		i40e_link_event(pf);
9888 
9889 	/* Update the stats for active netdevs so the network stack
9890 	 * can look at updated numbers whenever it cares to
9891 	 */
9892 	for (i = 0; i < pf->num_alloc_vsi; i++)
9893 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9894 			i40e_update_stats(pf->vsi[i]);
9895 
9896 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9897 		/* Update the stats for the active switching components */
9898 		for (i = 0; i < I40E_MAX_VEB; i++)
9899 			if (pf->veb[i])
9900 				i40e_update_veb_stats(pf->veb[i]);
9901 	}
9902 
9903 	i40e_ptp_rx_hang(pf);
9904 	i40e_ptp_tx_hang(pf);
9905 }
9906 
9907 /**
9908  * i40e_reset_subtask - Set up for resetting the device and driver
9909  * @pf: board private structure
9910  **/
9911 static void i40e_reset_subtask(struct i40e_pf *pf)
9912 {
9913 	u32 reset_flags = 0;
9914 
9915 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
9916 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
9917 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
9918 	}
9919 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
9920 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
9921 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9922 	}
9923 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
9924 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
9925 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
9926 	}
9927 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
9928 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
9929 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
9930 	}
9931 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
9932 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
9933 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
9934 	}
9935 
9936 	/* If there's a recovery already waiting, it takes
9937 	 * precedence before starting a new reset sequence.
9938 	 */
9939 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
9940 		i40e_prep_for_reset(pf);
9941 		i40e_reset(pf);
9942 		i40e_rebuild(pf, false, false);
9943 	}
9944 
9945 	/* If we're already down or resetting, just bail */
9946 	if (reset_flags &&
9947 	    !test_bit(__I40E_DOWN, pf->state) &&
9948 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
9949 		i40e_do_reset(pf, reset_flags, false);
9950 	}
9951 }
9952 
9953 /**
9954  * i40e_handle_link_event - Handle link event
9955  * @pf: board private structure
9956  * @e: event info posted on ARQ
9957  **/
9958 static void i40e_handle_link_event(struct i40e_pf *pf,
9959 				   struct i40e_arq_event_info *e)
9960 {
9961 	struct i40e_aqc_get_link_status *status =
9962 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
9963 
9964 	/* Do a new status request to re-enable LSE reporting
9965 	 * and load new status information into the hw struct
9966 	 * This completely ignores any state information
9967 	 * in the ARQ event info, instead choosing to always
9968 	 * issue the AQ update link status command.
9969 	 */
9970 	i40e_link_event(pf);
9971 
9972 	/* Check if module meets thermal requirements */
9973 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
9974 		dev_err(&pf->pdev->dev,
9975 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
9976 		dev_err(&pf->pdev->dev,
9977 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9978 	} else {
9979 		/* check for unqualified module, if link is down, suppress
9980 		 * the message if link was forced to be down.
9981 		 */
9982 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
9983 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
9984 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
9985 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
9986 			dev_err(&pf->pdev->dev,
9987 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
9988 			dev_err(&pf->pdev->dev,
9989 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9990 		}
9991 	}
9992 }
9993 
9994 /**
9995  * i40e_clean_adminq_subtask - Clean the AdminQ rings
9996  * @pf: board private structure
9997  **/
9998 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
9999 {
10000 	struct i40e_arq_event_info event;
10001 	struct i40e_hw *hw = &pf->hw;
10002 	u16 pending, i = 0;
10003 	i40e_status ret;
10004 	u16 opcode;
10005 	u32 oldval;
10006 	u32 val;
10007 
10008 	/* Do not run clean AQ when PF reset fails */
10009 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10010 		return;
10011 
10012 	/* check for error indications */
10013 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
10014 	oldval = val;
10015 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10016 		if (hw->debug_mask & I40E_DEBUG_AQ)
10017 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10018 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10019 	}
10020 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10021 		if (hw->debug_mask & I40E_DEBUG_AQ)
10022 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10023 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10024 		pf->arq_overflows++;
10025 	}
10026 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10027 		if (hw->debug_mask & I40E_DEBUG_AQ)
10028 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10029 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10030 	}
10031 	if (oldval != val)
10032 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
10033 
10034 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
10035 	oldval = val;
10036 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10037 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10038 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10039 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10040 	}
10041 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10042 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10043 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10044 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10045 	}
10046 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10047 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10048 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10049 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10050 	}
10051 	if (oldval != val)
10052 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
10053 
10054 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10055 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10056 	if (!event.msg_buf)
10057 		return;
10058 
10059 	do {
10060 		ret = i40e_clean_arq_element(hw, &event, &pending);
10061 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
10062 			break;
10063 		else if (ret) {
10064 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10065 			break;
10066 		}
10067 
10068 		opcode = le16_to_cpu(event.desc.opcode);
10069 		switch (opcode) {
10070 
10071 		case i40e_aqc_opc_get_link_status:
10072 			rtnl_lock();
10073 			i40e_handle_link_event(pf, &event);
10074 			rtnl_unlock();
10075 			break;
10076 		case i40e_aqc_opc_send_msg_to_pf:
10077 			ret = i40e_vc_process_vf_msg(pf,
10078 					le16_to_cpu(event.desc.retval),
10079 					le32_to_cpu(event.desc.cookie_high),
10080 					le32_to_cpu(event.desc.cookie_low),
10081 					event.msg_buf,
10082 					event.msg_len);
10083 			break;
10084 		case i40e_aqc_opc_lldp_update_mib:
10085 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10086 #ifdef CONFIG_I40E_DCB
10087 			rtnl_lock();
10088 			i40e_handle_lldp_event(pf, &event);
10089 			rtnl_unlock();
10090 #endif /* CONFIG_I40E_DCB */
10091 			break;
10092 		case i40e_aqc_opc_event_lan_overflow:
10093 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10094 			i40e_handle_lan_overflow_event(pf, &event);
10095 			break;
10096 		case i40e_aqc_opc_send_msg_to_peer:
10097 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10098 			break;
10099 		case i40e_aqc_opc_nvm_erase:
10100 		case i40e_aqc_opc_nvm_update:
10101 		case i40e_aqc_opc_oem_post_update:
10102 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10103 				   "ARQ NVM operation 0x%04x completed\n",
10104 				   opcode);
10105 			break;
10106 		default:
10107 			dev_info(&pf->pdev->dev,
10108 				 "ARQ: Unknown event 0x%04x ignored\n",
10109 				 opcode);
10110 			break;
10111 		}
10112 	} while (i++ < pf->adminq_work_limit);
10113 
10114 	if (i < pf->adminq_work_limit)
10115 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10116 
10117 	/* re-enable Admin queue interrupt cause */
10118 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10119 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10120 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10121 	i40e_flush(hw);
10122 
10123 	kfree(event.msg_buf);
10124 }
10125 
10126 /**
10127  * i40e_verify_eeprom - make sure eeprom is good to use
10128  * @pf: board private structure
10129  **/
10130 static void i40e_verify_eeprom(struct i40e_pf *pf)
10131 {
10132 	int err;
10133 
10134 	err = i40e_diag_eeprom_test(&pf->hw);
10135 	if (err) {
10136 		/* retry in case of garbage read */
10137 		err = i40e_diag_eeprom_test(&pf->hw);
10138 		if (err) {
10139 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10140 				 err);
10141 			set_bit(__I40E_BAD_EEPROM, pf->state);
10142 		}
10143 	}
10144 
10145 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10146 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10147 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10148 	}
10149 }
10150 
10151 /**
10152  * i40e_enable_pf_switch_lb
10153  * @pf: pointer to the PF structure
10154  *
10155  * enable switch loop back or die - no point in a return value
10156  **/
10157 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10158 {
10159 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10160 	struct i40e_vsi_context ctxt;
10161 	int ret;
10162 
10163 	ctxt.seid = pf->main_vsi_seid;
10164 	ctxt.pf_num = pf->hw.pf_id;
10165 	ctxt.vf_num = 0;
10166 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10167 	if (ret) {
10168 		dev_info(&pf->pdev->dev,
10169 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10170 			 i40e_stat_str(&pf->hw, ret),
10171 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10172 		return;
10173 	}
10174 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10175 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10176 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10177 
10178 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10179 	if (ret) {
10180 		dev_info(&pf->pdev->dev,
10181 			 "update vsi switch failed, err %s aq_err %s\n",
10182 			 i40e_stat_str(&pf->hw, ret),
10183 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10184 	}
10185 }
10186 
10187 /**
10188  * i40e_disable_pf_switch_lb
10189  * @pf: pointer to the PF structure
10190  *
10191  * disable switch loop back or die - no point in a return value
10192  **/
10193 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10194 {
10195 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10196 	struct i40e_vsi_context ctxt;
10197 	int ret;
10198 
10199 	ctxt.seid = pf->main_vsi_seid;
10200 	ctxt.pf_num = pf->hw.pf_id;
10201 	ctxt.vf_num = 0;
10202 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10203 	if (ret) {
10204 		dev_info(&pf->pdev->dev,
10205 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10206 			 i40e_stat_str(&pf->hw, ret),
10207 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10208 		return;
10209 	}
10210 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10211 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10212 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10213 
10214 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10215 	if (ret) {
10216 		dev_info(&pf->pdev->dev,
10217 			 "update vsi switch failed, err %s aq_err %s\n",
10218 			 i40e_stat_str(&pf->hw, ret),
10219 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10220 	}
10221 }
10222 
10223 /**
10224  * i40e_config_bridge_mode - Configure the HW bridge mode
10225  * @veb: pointer to the bridge instance
10226  *
10227  * Configure the loop back mode for the LAN VSI that is downlink to the
10228  * specified HW bridge instance. It is expected this function is called
10229  * when a new HW bridge is instantiated.
10230  **/
10231 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10232 {
10233 	struct i40e_pf *pf = veb->pf;
10234 
10235 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10236 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10237 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10238 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10239 		i40e_disable_pf_switch_lb(pf);
10240 	else
10241 		i40e_enable_pf_switch_lb(pf);
10242 }
10243 
10244 /**
10245  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10246  * @veb: pointer to the VEB instance
10247  *
10248  * This is a recursive function that first builds the attached VSIs then
10249  * recurses in to build the next layer of VEB.  We track the connections
10250  * through our own index numbers because the seid's from the HW could
10251  * change across the reset.
10252  **/
10253 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10254 {
10255 	struct i40e_vsi *ctl_vsi = NULL;
10256 	struct i40e_pf *pf = veb->pf;
10257 	int v, veb_idx;
10258 	int ret;
10259 
10260 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10261 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10262 		if (pf->vsi[v] &&
10263 		    pf->vsi[v]->veb_idx == veb->idx &&
10264 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10265 			ctl_vsi = pf->vsi[v];
10266 			break;
10267 		}
10268 	}
10269 	if (!ctl_vsi) {
10270 		dev_info(&pf->pdev->dev,
10271 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10272 		ret = -ENOENT;
10273 		goto end_reconstitute;
10274 	}
10275 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10276 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10277 	ret = i40e_add_vsi(ctl_vsi);
10278 	if (ret) {
10279 		dev_info(&pf->pdev->dev,
10280 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10281 			 veb->idx, ret);
10282 		goto end_reconstitute;
10283 	}
10284 	i40e_vsi_reset_stats(ctl_vsi);
10285 
10286 	/* create the VEB in the switch and move the VSI onto the VEB */
10287 	ret = i40e_add_veb(veb, ctl_vsi);
10288 	if (ret)
10289 		goto end_reconstitute;
10290 
10291 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10292 		veb->bridge_mode = BRIDGE_MODE_VEB;
10293 	else
10294 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10295 	i40e_config_bridge_mode(veb);
10296 
10297 	/* create the remaining VSIs attached to this VEB */
10298 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10299 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10300 			continue;
10301 
10302 		if (pf->vsi[v]->veb_idx == veb->idx) {
10303 			struct i40e_vsi *vsi = pf->vsi[v];
10304 
10305 			vsi->uplink_seid = veb->seid;
10306 			ret = i40e_add_vsi(vsi);
10307 			if (ret) {
10308 				dev_info(&pf->pdev->dev,
10309 					 "rebuild of vsi_idx %d failed: %d\n",
10310 					 v, ret);
10311 				goto end_reconstitute;
10312 			}
10313 			i40e_vsi_reset_stats(vsi);
10314 		}
10315 	}
10316 
10317 	/* create any VEBs attached to this VEB - RECURSION */
10318 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10319 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10320 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10321 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10322 			if (ret)
10323 				break;
10324 		}
10325 	}
10326 
10327 end_reconstitute:
10328 	return ret;
10329 }
10330 
10331 /**
10332  * i40e_get_capabilities - get info about the HW
10333  * @pf: the PF struct
10334  * @list_type: AQ capability to be queried
10335  **/
10336 static int i40e_get_capabilities(struct i40e_pf *pf,
10337 				 enum i40e_admin_queue_opc list_type)
10338 {
10339 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10340 	u16 data_size;
10341 	int buf_len;
10342 	int err;
10343 
10344 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10345 	do {
10346 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10347 		if (!cap_buf)
10348 			return -ENOMEM;
10349 
10350 		/* this loads the data into the hw struct for us */
10351 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10352 						    &data_size, list_type,
10353 						    NULL);
10354 		/* data loaded, buffer no longer needed */
10355 		kfree(cap_buf);
10356 
10357 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10358 			/* retry with a larger buffer */
10359 			buf_len = data_size;
10360 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10361 			dev_info(&pf->pdev->dev,
10362 				 "capability discovery failed, err %s aq_err %s\n",
10363 				 i40e_stat_str(&pf->hw, err),
10364 				 i40e_aq_str(&pf->hw,
10365 					     pf->hw.aq.asq_last_status));
10366 			return -ENODEV;
10367 		}
10368 	} while (err);
10369 
10370 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10371 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10372 			dev_info(&pf->pdev->dev,
10373 				 "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",
10374 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10375 				 pf->hw.func_caps.num_msix_vectors,
10376 				 pf->hw.func_caps.num_msix_vectors_vf,
10377 				 pf->hw.func_caps.fd_filters_guaranteed,
10378 				 pf->hw.func_caps.fd_filters_best_effort,
10379 				 pf->hw.func_caps.num_tx_qp,
10380 				 pf->hw.func_caps.num_vsis);
10381 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10382 			dev_info(&pf->pdev->dev,
10383 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10384 				 pf->hw.dev_caps.switch_mode,
10385 				 pf->hw.dev_caps.valid_functions);
10386 			dev_info(&pf->pdev->dev,
10387 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10388 				 pf->hw.dev_caps.sr_iov_1_1,
10389 				 pf->hw.dev_caps.num_vfs);
10390 			dev_info(&pf->pdev->dev,
10391 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10392 				 pf->hw.dev_caps.num_vsis,
10393 				 pf->hw.dev_caps.num_rx_qp,
10394 				 pf->hw.dev_caps.num_tx_qp);
10395 		}
10396 	}
10397 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10398 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10399 		       + pf->hw.func_caps.num_vfs)
10400 		if (pf->hw.revision_id == 0 &&
10401 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10402 			dev_info(&pf->pdev->dev,
10403 				 "got num_vsis %d, setting num_vsis to %d\n",
10404 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10405 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10406 		}
10407 	}
10408 	return 0;
10409 }
10410 
10411 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10412 
10413 /**
10414  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10415  * @pf: board private structure
10416  **/
10417 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10418 {
10419 	struct i40e_vsi *vsi;
10420 
10421 	/* quick workaround for an NVM issue that leaves a critical register
10422 	 * uninitialized
10423 	 */
10424 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10425 		static const u32 hkey[] = {
10426 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10427 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10428 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10429 			0x95b3a76d};
10430 		int i;
10431 
10432 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10433 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10434 	}
10435 
10436 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10437 		return;
10438 
10439 	/* find existing VSI and see if it needs configuring */
10440 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10441 
10442 	/* create a new VSI if none exists */
10443 	if (!vsi) {
10444 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10445 				     pf->vsi[pf->lan_vsi]->seid, 0);
10446 		if (!vsi) {
10447 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10448 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10449 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10450 			return;
10451 		}
10452 	}
10453 
10454 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10455 }
10456 
10457 /**
10458  * i40e_fdir_teardown - release the Flow Director resources
10459  * @pf: board private structure
10460  **/
10461 static void i40e_fdir_teardown(struct i40e_pf *pf)
10462 {
10463 	struct i40e_vsi *vsi;
10464 
10465 	i40e_fdir_filter_exit(pf);
10466 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10467 	if (vsi)
10468 		i40e_vsi_release(vsi);
10469 }
10470 
10471 /**
10472  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10473  * @vsi: PF main vsi
10474  * @seid: seid of main or channel VSIs
10475  *
10476  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10477  * existed before reset
10478  **/
10479 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10480 {
10481 	struct i40e_cloud_filter *cfilter;
10482 	struct i40e_pf *pf = vsi->back;
10483 	struct hlist_node *node;
10484 	i40e_status ret;
10485 
10486 	/* Add cloud filters back if they exist */
10487 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10488 				  cloud_node) {
10489 		if (cfilter->seid != seid)
10490 			continue;
10491 
10492 		if (cfilter->dst_port)
10493 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10494 								true);
10495 		else
10496 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10497 
10498 		if (ret) {
10499 			dev_dbg(&pf->pdev->dev,
10500 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
10501 				i40e_stat_str(&pf->hw, ret),
10502 				i40e_aq_str(&pf->hw,
10503 					    pf->hw.aq.asq_last_status));
10504 			return ret;
10505 		}
10506 	}
10507 	return 0;
10508 }
10509 
10510 /**
10511  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10512  * @vsi: PF main vsi
10513  *
10514  * Rebuilds channel VSIs if they existed before reset
10515  **/
10516 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10517 {
10518 	struct i40e_channel *ch, *ch_tmp;
10519 	i40e_status ret;
10520 
10521 	if (list_empty(&vsi->ch_list))
10522 		return 0;
10523 
10524 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10525 		if (!ch->initialized)
10526 			break;
10527 		/* Proceed with creation of channel (VMDq2) VSI */
10528 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10529 		if (ret) {
10530 			dev_info(&vsi->back->pdev->dev,
10531 				 "failed to rebuild channels using uplink_seid %u\n",
10532 				 vsi->uplink_seid);
10533 			return ret;
10534 		}
10535 		/* Reconfigure TX queues using QTX_CTL register */
10536 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10537 		if (ret) {
10538 			dev_info(&vsi->back->pdev->dev,
10539 				 "failed to configure TX rings for channel %u\n",
10540 				 ch->seid);
10541 			return ret;
10542 		}
10543 		/* update 'next_base_queue' */
10544 		vsi->next_base_queue = vsi->next_base_queue +
10545 							ch->num_queue_pairs;
10546 		if (ch->max_tx_rate) {
10547 			u64 credits = ch->max_tx_rate;
10548 
10549 			if (i40e_set_bw_limit(vsi, ch->seid,
10550 					      ch->max_tx_rate))
10551 				return -EINVAL;
10552 
10553 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10554 			dev_dbg(&vsi->back->pdev->dev,
10555 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10556 				ch->max_tx_rate,
10557 				credits,
10558 				ch->seid);
10559 		}
10560 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10561 		if (ret) {
10562 			dev_dbg(&vsi->back->pdev->dev,
10563 				"Failed to rebuild cloud filters for channel VSI %u\n",
10564 				ch->seid);
10565 			return ret;
10566 		}
10567 	}
10568 	return 0;
10569 }
10570 
10571 /**
10572  * i40e_prep_for_reset - prep for the core to reset
10573  * @pf: board private structure
10574  *
10575  * Close up the VFs and other things in prep for PF Reset.
10576   **/
10577 static void i40e_prep_for_reset(struct i40e_pf *pf)
10578 {
10579 	struct i40e_hw *hw = &pf->hw;
10580 	i40e_status ret = 0;
10581 	u32 v;
10582 
10583 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10584 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10585 		return;
10586 	if (i40e_check_asq_alive(&pf->hw))
10587 		i40e_vc_notify_reset(pf);
10588 
10589 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10590 
10591 	/* quiesce the VSIs and their queues that are not already DOWN */
10592 	i40e_pf_quiesce_all_vsi(pf);
10593 
10594 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10595 		if (pf->vsi[v])
10596 			pf->vsi[v]->seid = 0;
10597 	}
10598 
10599 	i40e_shutdown_adminq(&pf->hw);
10600 
10601 	/* call shutdown HMC */
10602 	if (hw->hmc.hmc_obj) {
10603 		ret = i40e_shutdown_lan_hmc(hw);
10604 		if (ret)
10605 			dev_warn(&pf->pdev->dev,
10606 				 "shutdown_lan_hmc failed: %d\n", ret);
10607 	}
10608 
10609 	/* Save the current PTP time so that we can restore the time after the
10610 	 * reset completes.
10611 	 */
10612 	i40e_ptp_save_hw_time(pf);
10613 }
10614 
10615 /**
10616  * i40e_send_version - update firmware with driver version
10617  * @pf: PF struct
10618  */
10619 static void i40e_send_version(struct i40e_pf *pf)
10620 {
10621 	struct i40e_driver_version dv;
10622 
10623 	dv.major_version = 0xff;
10624 	dv.minor_version = 0xff;
10625 	dv.build_version = 0xff;
10626 	dv.subbuild_version = 0;
10627 	strlcpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10628 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10629 }
10630 
10631 /**
10632  * i40e_get_oem_version - get OEM specific version information
10633  * @hw: pointer to the hardware structure
10634  **/
10635 static void i40e_get_oem_version(struct i40e_hw *hw)
10636 {
10637 	u16 block_offset = 0xffff;
10638 	u16 block_length = 0;
10639 	u16 capabilities = 0;
10640 	u16 gen_snap = 0;
10641 	u16 release = 0;
10642 
10643 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10644 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10645 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10646 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10647 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10648 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10649 #define I40E_NVM_OEM_LENGTH			3
10650 
10651 	/* Check if pointer to OEM version block is valid. */
10652 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10653 	if (block_offset == 0xffff)
10654 		return;
10655 
10656 	/* Check if OEM version block has correct length. */
10657 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10658 			   &block_length);
10659 	if (block_length < I40E_NVM_OEM_LENGTH)
10660 		return;
10661 
10662 	/* Check if OEM version format is as expected. */
10663 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10664 			   &capabilities);
10665 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10666 		return;
10667 
10668 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10669 			   &gen_snap);
10670 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10671 			   &release);
10672 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10673 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10674 }
10675 
10676 /**
10677  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10678  * @pf: board private structure
10679  **/
10680 static int i40e_reset(struct i40e_pf *pf)
10681 {
10682 	struct i40e_hw *hw = &pf->hw;
10683 	i40e_status ret;
10684 
10685 	ret = i40e_pf_reset(hw);
10686 	if (ret) {
10687 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10688 		set_bit(__I40E_RESET_FAILED, pf->state);
10689 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10690 	} else {
10691 		pf->pfr_count++;
10692 	}
10693 	return ret;
10694 }
10695 
10696 /**
10697  * i40e_rebuild - rebuild using a saved config
10698  * @pf: board private structure
10699  * @reinit: if the Main VSI needs to re-initialized.
10700  * @lock_acquired: indicates whether or not the lock has been acquired
10701  * before this function was called.
10702  **/
10703 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10704 {
10705 	int old_recovery_mode_bit = test_bit(__I40E_RECOVERY_MODE, pf->state);
10706 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10707 	struct i40e_hw *hw = &pf->hw;
10708 	i40e_status ret;
10709 	u32 val;
10710 	int v;
10711 
10712 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10713 	    i40e_check_recovery_mode(pf)) {
10714 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10715 	}
10716 
10717 	if (test_bit(__I40E_DOWN, pf->state) &&
10718 	    !test_bit(__I40E_RECOVERY_MODE, pf->state) &&
10719 	    !old_recovery_mode_bit)
10720 		goto clear_recovery;
10721 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10722 
10723 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10724 	ret = i40e_init_adminq(&pf->hw);
10725 	if (ret) {
10726 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10727 			 i40e_stat_str(&pf->hw, ret),
10728 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10729 		goto clear_recovery;
10730 	}
10731 	i40e_get_oem_version(&pf->hw);
10732 
10733 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10734 		/* The following delay is necessary for firmware update. */
10735 		mdelay(1000);
10736 	}
10737 
10738 	/* re-verify the eeprom if we just had an EMP reset */
10739 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10740 		i40e_verify_eeprom(pf);
10741 
10742 	/* if we are going out of or into recovery mode we have to act
10743 	 * accordingly with regard to resources initialization
10744 	 * and deinitialization
10745 	 */
10746 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) ||
10747 	    old_recovery_mode_bit) {
10748 		if (i40e_get_capabilities(pf,
10749 					  i40e_aqc_opc_list_func_capabilities))
10750 			goto end_unlock;
10751 
10752 		if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10753 			/* we're staying in recovery mode so we'll reinitialize
10754 			 * misc vector here
10755 			 */
10756 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10757 				goto end_unlock;
10758 		} else {
10759 			if (!lock_acquired)
10760 				rtnl_lock();
10761 			/* we're going out of recovery mode so we'll free
10762 			 * the IRQ allocated specifically for recovery mode
10763 			 * and restore the interrupt scheme
10764 			 */
10765 			free_irq(pf->pdev->irq, pf);
10766 			i40e_clear_interrupt_scheme(pf);
10767 			if (i40e_restore_interrupt_scheme(pf))
10768 				goto end_unlock;
10769 		}
10770 
10771 		/* tell the firmware that we're starting */
10772 		i40e_send_version(pf);
10773 
10774 		/* bail out in case recovery mode was detected, as there is
10775 		 * no need for further configuration.
10776 		 */
10777 		goto end_unlock;
10778 	}
10779 
10780 	i40e_clear_pxe_mode(hw);
10781 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10782 	if (ret)
10783 		goto end_core_reset;
10784 
10785 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10786 				hw->func_caps.num_rx_qp, 0, 0);
10787 	if (ret) {
10788 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10789 		goto end_core_reset;
10790 	}
10791 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10792 	if (ret) {
10793 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10794 		goto end_core_reset;
10795 	}
10796 
10797 #ifdef CONFIG_I40E_DCB
10798 	/* Enable FW to write a default DCB config on link-up
10799 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10800 	 * is not supported with new link speed
10801 	 */
10802 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
10803 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10804 	} else {
10805 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10806 		    (hw->phy.link_info.link_speed &
10807 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10808 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10809 			dev_warn(&pf->pdev->dev,
10810 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10811 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10812 		} else {
10813 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10814 			ret = i40e_init_pf_dcb(pf);
10815 			if (ret) {
10816 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10817 					 ret);
10818 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10819 				/* Continue without DCB enabled */
10820 			}
10821 		}
10822 	}
10823 
10824 #endif /* CONFIG_I40E_DCB */
10825 	if (!lock_acquired)
10826 		rtnl_lock();
10827 	ret = i40e_setup_pf_switch(pf, reinit, true);
10828 	if (ret)
10829 		goto end_unlock;
10830 
10831 	/* The driver only wants link up/down and module qualification
10832 	 * reports from firmware.  Note the negative logic.
10833 	 */
10834 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10835 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10836 					 I40E_AQ_EVENT_MEDIA_NA |
10837 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10838 	if (ret)
10839 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10840 			 i40e_stat_str(&pf->hw, ret),
10841 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10842 
10843 	/* Rebuild the VSIs and VEBs that existed before reset.
10844 	 * They are still in our local switch element arrays, so only
10845 	 * need to rebuild the switch model in the HW.
10846 	 *
10847 	 * If there were VEBs but the reconstitution failed, we'll try
10848 	 * to recover minimal use by getting the basic PF VSI working.
10849 	 */
10850 	if (vsi->uplink_seid != pf->mac_seid) {
10851 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10852 		/* find the one VEB connected to the MAC, and find orphans */
10853 		for (v = 0; v < I40E_MAX_VEB; v++) {
10854 			if (!pf->veb[v])
10855 				continue;
10856 
10857 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10858 			    pf->veb[v]->uplink_seid == 0) {
10859 				ret = i40e_reconstitute_veb(pf->veb[v]);
10860 
10861 				if (!ret)
10862 					continue;
10863 
10864 				/* If Main VEB failed, we're in deep doodoo,
10865 				 * so give up rebuilding the switch and set up
10866 				 * for minimal rebuild of PF VSI.
10867 				 * If orphan failed, we'll report the error
10868 				 * but try to keep going.
10869 				 */
10870 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10871 					dev_info(&pf->pdev->dev,
10872 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10873 						 ret);
10874 					vsi->uplink_seid = pf->mac_seid;
10875 					break;
10876 				} else if (pf->veb[v]->uplink_seid == 0) {
10877 					dev_info(&pf->pdev->dev,
10878 						 "rebuild of orphan VEB failed: %d\n",
10879 						 ret);
10880 				}
10881 			}
10882 		}
10883 	}
10884 
10885 	if (vsi->uplink_seid == pf->mac_seid) {
10886 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10887 		/* no VEB, so rebuild only the Main VSI */
10888 		ret = i40e_add_vsi(vsi);
10889 		if (ret) {
10890 			dev_info(&pf->pdev->dev,
10891 				 "rebuild of Main VSI failed: %d\n", ret);
10892 			goto end_unlock;
10893 		}
10894 	}
10895 
10896 	if (vsi->mqprio_qopt.max_rate[0]) {
10897 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
10898 		u64 credits = 0;
10899 
10900 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
10901 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10902 		if (ret)
10903 			goto end_unlock;
10904 
10905 		credits = max_tx_rate;
10906 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10907 		dev_dbg(&vsi->back->pdev->dev,
10908 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10909 			max_tx_rate,
10910 			credits,
10911 			vsi->seid);
10912 	}
10913 
10914 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10915 	if (ret)
10916 		goto end_unlock;
10917 
10918 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
10919 	 * for this main VSI if they exist
10920 	 */
10921 	ret = i40e_rebuild_channels(vsi);
10922 	if (ret)
10923 		goto end_unlock;
10924 
10925 	/* Reconfigure hardware for allowing smaller MSS in the case
10926 	 * of TSO, so that we avoid the MDD being fired and causing
10927 	 * a reset in the case of small MSS+TSO.
10928 	 */
10929 #define I40E_REG_MSS          0x000E64DC
10930 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
10931 #define I40E_64BYTE_MSS       0x400000
10932 	val = rd32(hw, I40E_REG_MSS);
10933 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10934 		val &= ~I40E_REG_MSS_MIN_MASK;
10935 		val |= I40E_64BYTE_MSS;
10936 		wr32(hw, I40E_REG_MSS, val);
10937 	}
10938 
10939 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
10940 		msleep(75);
10941 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10942 		if (ret)
10943 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10944 				 i40e_stat_str(&pf->hw, ret),
10945 				 i40e_aq_str(&pf->hw,
10946 					     pf->hw.aq.asq_last_status));
10947 	}
10948 	/* reinit the misc interrupt */
10949 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10950 		ret = i40e_setup_misc_vector(pf);
10951 
10952 	/* Add a filter to drop all Flow control frames from any VSI from being
10953 	 * transmitted. By doing so we stop a malicious VF from sending out
10954 	 * PAUSE or PFC frames and potentially controlling traffic for other
10955 	 * PF/VF VSIs.
10956 	 * The FW can still send Flow control frames if enabled.
10957 	 */
10958 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
10959 						       pf->main_vsi_seid);
10960 
10961 	/* restart the VSIs that were rebuilt and running before the reset */
10962 	i40e_pf_unquiesce_all_vsi(pf);
10963 
10964 	/* Release the RTNL lock before we start resetting VFs */
10965 	if (!lock_acquired)
10966 		rtnl_unlock();
10967 
10968 	/* Restore promiscuous settings */
10969 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
10970 	if (ret)
10971 		dev_warn(&pf->pdev->dev,
10972 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
10973 			 pf->cur_promisc ? "on" : "off",
10974 			 i40e_stat_str(&pf->hw, ret),
10975 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10976 
10977 	i40e_reset_all_vfs(pf, true);
10978 
10979 	/* tell the firmware that we're starting */
10980 	i40e_send_version(pf);
10981 
10982 	/* We've already released the lock, so don't do it again */
10983 	goto end_core_reset;
10984 
10985 end_unlock:
10986 	if (!lock_acquired)
10987 		rtnl_unlock();
10988 end_core_reset:
10989 	clear_bit(__I40E_RESET_FAILED, pf->state);
10990 clear_recovery:
10991 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10992 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
10993 }
10994 
10995 /**
10996  * i40e_reset_and_rebuild - reset and rebuild using a saved config
10997  * @pf: board private structure
10998  * @reinit: if the Main VSI needs to re-initialized.
10999  * @lock_acquired: indicates whether or not the lock has been acquired
11000  * before this function was called.
11001  **/
11002 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11003 				   bool lock_acquired)
11004 {
11005 	int ret;
11006 
11007 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11008 		return;
11009 	/* Now we wait for GRST to settle out.
11010 	 * We don't have to delete the VEBs or VSIs from the hw switch
11011 	 * because the reset will make them disappear.
11012 	 */
11013 	ret = i40e_reset(pf);
11014 	if (!ret)
11015 		i40e_rebuild(pf, reinit, lock_acquired);
11016 }
11017 
11018 /**
11019  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11020  * @pf: board private structure
11021  *
11022  * Close up the VFs and other things in prep for a Core Reset,
11023  * then get ready to rebuild the world.
11024  * @lock_acquired: indicates whether or not the lock has been acquired
11025  * before this function was called.
11026  **/
11027 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11028 {
11029 	i40e_prep_for_reset(pf);
11030 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11031 }
11032 
11033 /**
11034  * i40e_handle_mdd_event
11035  * @pf: pointer to the PF structure
11036  *
11037  * Called from the MDD irq handler to identify possibly malicious vfs
11038  **/
11039 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11040 {
11041 	struct i40e_hw *hw = &pf->hw;
11042 	bool mdd_detected = false;
11043 	struct i40e_vf *vf;
11044 	u32 reg;
11045 	int i;
11046 
11047 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11048 		return;
11049 
11050 	/* find what triggered the MDD event */
11051 	reg = rd32(hw, I40E_GL_MDET_TX);
11052 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11053 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
11054 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
11055 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
11056 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
11057 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
11058 				I40E_GL_MDET_TX_EVENT_SHIFT;
11059 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
11060 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
11061 				pf->hw.func_caps.base_queue;
11062 		if (netif_msg_tx_err(pf))
11063 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11064 				 event, queue, pf_num, vf_num);
11065 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11066 		mdd_detected = true;
11067 	}
11068 	reg = rd32(hw, I40E_GL_MDET_RX);
11069 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11070 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
11071 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
11072 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
11073 				I40E_GL_MDET_RX_EVENT_SHIFT;
11074 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
11075 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
11076 				pf->hw.func_caps.base_queue;
11077 		if (netif_msg_rx_err(pf))
11078 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11079 				 event, queue, func);
11080 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11081 		mdd_detected = true;
11082 	}
11083 
11084 	if (mdd_detected) {
11085 		reg = rd32(hw, I40E_PF_MDET_TX);
11086 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11087 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11088 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11089 		}
11090 		reg = rd32(hw, I40E_PF_MDET_RX);
11091 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11092 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11093 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11094 		}
11095 	}
11096 
11097 	/* see if one of the VFs needs its hand slapped */
11098 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11099 		vf = &(pf->vf[i]);
11100 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11101 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11102 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11103 			vf->num_mdd_events++;
11104 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11105 				 i);
11106 			dev_info(&pf->pdev->dev,
11107 				 "Use PF Control I/F to re-enable the VF\n");
11108 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11109 		}
11110 
11111 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11112 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11113 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11114 			vf->num_mdd_events++;
11115 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11116 				 i);
11117 			dev_info(&pf->pdev->dev,
11118 				 "Use PF Control I/F to re-enable the VF\n");
11119 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11120 		}
11121 	}
11122 
11123 	/* re-enable mdd interrupt cause */
11124 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11125 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11126 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11127 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11128 	i40e_flush(hw);
11129 }
11130 
11131 /**
11132  * i40e_service_task - Run the driver's async subtasks
11133  * @work: pointer to work_struct containing our data
11134  **/
11135 static void i40e_service_task(struct work_struct *work)
11136 {
11137 	struct i40e_pf *pf = container_of(work,
11138 					  struct i40e_pf,
11139 					  service_task);
11140 	unsigned long start_time = jiffies;
11141 
11142 	/* don't bother with service tasks if a reset is in progress */
11143 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11144 	    test_bit(__I40E_SUSPENDED, pf->state))
11145 		return;
11146 
11147 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11148 		return;
11149 
11150 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11151 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11152 		i40e_sync_filters_subtask(pf);
11153 		i40e_reset_subtask(pf);
11154 		i40e_handle_mdd_event(pf);
11155 		i40e_vc_process_vflr_event(pf);
11156 		i40e_watchdog_subtask(pf);
11157 		i40e_fdir_reinit_subtask(pf);
11158 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11159 			/* Client subtask will reopen next time through. */
11160 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11161 							   true);
11162 		} else {
11163 			i40e_client_subtask(pf);
11164 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11165 					       pf->state))
11166 				i40e_notify_client_of_l2_param_changes(
11167 								pf->vsi[pf->lan_vsi]);
11168 		}
11169 		i40e_sync_filters_subtask(pf);
11170 	} else {
11171 		i40e_reset_subtask(pf);
11172 	}
11173 
11174 	i40e_clean_adminq_subtask(pf);
11175 
11176 	/* flush memory to make sure state is correct before next watchdog */
11177 	smp_mb__before_atomic();
11178 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11179 
11180 	/* If the tasks have taken longer than one timer cycle or there
11181 	 * is more work to be done, reschedule the service task now
11182 	 * rather than wait for the timer to tick again.
11183 	 */
11184 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11185 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11186 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11187 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11188 		i40e_service_event_schedule(pf);
11189 }
11190 
11191 /**
11192  * i40e_service_timer - timer callback
11193  * @t: timer list pointer
11194  **/
11195 static void i40e_service_timer(struct timer_list *t)
11196 {
11197 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11198 
11199 	mod_timer(&pf->service_timer,
11200 		  round_jiffies(jiffies + pf->service_timer_period));
11201 	i40e_service_event_schedule(pf);
11202 }
11203 
11204 /**
11205  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11206  * @vsi: the VSI being configured
11207  **/
11208 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11209 {
11210 	struct i40e_pf *pf = vsi->back;
11211 
11212 	switch (vsi->type) {
11213 	case I40E_VSI_MAIN:
11214 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11215 		if (!vsi->num_tx_desc)
11216 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11217 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11218 		if (!vsi->num_rx_desc)
11219 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11220 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11221 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11222 			vsi->num_q_vectors = pf->num_lan_msix;
11223 		else
11224 			vsi->num_q_vectors = 1;
11225 
11226 		break;
11227 
11228 	case I40E_VSI_FDIR:
11229 		vsi->alloc_queue_pairs = 1;
11230 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11231 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11232 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11233 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11234 		vsi->num_q_vectors = pf->num_fdsb_msix;
11235 		break;
11236 
11237 	case I40E_VSI_VMDQ2:
11238 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11239 		if (!vsi->num_tx_desc)
11240 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11241 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11242 		if (!vsi->num_rx_desc)
11243 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11244 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11245 		vsi->num_q_vectors = pf->num_vmdq_msix;
11246 		break;
11247 
11248 	case I40E_VSI_SRIOV:
11249 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11250 		if (!vsi->num_tx_desc)
11251 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11252 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11253 		if (!vsi->num_rx_desc)
11254 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11255 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11256 		break;
11257 
11258 	default:
11259 		WARN_ON(1);
11260 		return -ENODATA;
11261 	}
11262 
11263 	if (is_kdump_kernel()) {
11264 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11265 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11266 	}
11267 
11268 	return 0;
11269 }
11270 
11271 /**
11272  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11273  * @vsi: VSI pointer
11274  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11275  *
11276  * On error: returns error code (negative)
11277  * On success: returns 0
11278  **/
11279 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11280 {
11281 	struct i40e_ring **next_rings;
11282 	int size;
11283 	int ret = 0;
11284 
11285 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11286 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11287 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11288 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11289 	if (!vsi->tx_rings)
11290 		return -ENOMEM;
11291 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11292 	if (i40e_enabled_xdp_vsi(vsi)) {
11293 		vsi->xdp_rings = next_rings;
11294 		next_rings += vsi->alloc_queue_pairs;
11295 	}
11296 	vsi->rx_rings = next_rings;
11297 
11298 	if (alloc_qvectors) {
11299 		/* allocate memory for q_vector pointers */
11300 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11301 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11302 		if (!vsi->q_vectors) {
11303 			ret = -ENOMEM;
11304 			goto err_vectors;
11305 		}
11306 	}
11307 	return ret;
11308 
11309 err_vectors:
11310 	kfree(vsi->tx_rings);
11311 	return ret;
11312 }
11313 
11314 /**
11315  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11316  * @pf: board private structure
11317  * @type: type of VSI
11318  *
11319  * On error: returns error code (negative)
11320  * On success: returns vsi index in PF (positive)
11321  **/
11322 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11323 {
11324 	int ret = -ENODEV;
11325 	struct i40e_vsi *vsi;
11326 	int vsi_idx;
11327 	int i;
11328 
11329 	/* Need to protect the allocation of the VSIs at the PF level */
11330 	mutex_lock(&pf->switch_mutex);
11331 
11332 	/* VSI list may be fragmented if VSI creation/destruction has
11333 	 * been happening.  We can afford to do a quick scan to look
11334 	 * for any free VSIs in the list.
11335 	 *
11336 	 * find next empty vsi slot, looping back around if necessary
11337 	 */
11338 	i = pf->next_vsi;
11339 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11340 		i++;
11341 	if (i >= pf->num_alloc_vsi) {
11342 		i = 0;
11343 		while (i < pf->next_vsi && pf->vsi[i])
11344 			i++;
11345 	}
11346 
11347 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11348 		vsi_idx = i;             /* Found one! */
11349 	} else {
11350 		ret = -ENODEV;
11351 		goto unlock_pf;  /* out of VSI slots! */
11352 	}
11353 	pf->next_vsi = ++i;
11354 
11355 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11356 	if (!vsi) {
11357 		ret = -ENOMEM;
11358 		goto unlock_pf;
11359 	}
11360 	vsi->type = type;
11361 	vsi->back = pf;
11362 	set_bit(__I40E_VSI_DOWN, vsi->state);
11363 	vsi->flags = 0;
11364 	vsi->idx = vsi_idx;
11365 	vsi->int_rate_limit = 0;
11366 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11367 				pf->rss_table_size : 64;
11368 	vsi->netdev_registered = false;
11369 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11370 	hash_init(vsi->mac_filter_hash);
11371 	vsi->irqs_ready = false;
11372 
11373 	if (type == I40E_VSI_MAIN) {
11374 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11375 		if (!vsi->af_xdp_zc_qps)
11376 			goto err_rings;
11377 	}
11378 
11379 	ret = i40e_set_num_rings_in_vsi(vsi);
11380 	if (ret)
11381 		goto err_rings;
11382 
11383 	ret = i40e_vsi_alloc_arrays(vsi, true);
11384 	if (ret)
11385 		goto err_rings;
11386 
11387 	/* Setup default MSIX irq handler for VSI */
11388 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11389 
11390 	/* Initialize VSI lock */
11391 	spin_lock_init(&vsi->mac_filter_hash_lock);
11392 	pf->vsi[vsi_idx] = vsi;
11393 	ret = vsi_idx;
11394 	goto unlock_pf;
11395 
11396 err_rings:
11397 	bitmap_free(vsi->af_xdp_zc_qps);
11398 	pf->next_vsi = i - 1;
11399 	kfree(vsi);
11400 unlock_pf:
11401 	mutex_unlock(&pf->switch_mutex);
11402 	return ret;
11403 }
11404 
11405 /**
11406  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11407  * @vsi: VSI pointer
11408  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11409  *
11410  * On error: returns error code (negative)
11411  * On success: returns 0
11412  **/
11413 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11414 {
11415 	/* free the ring and vector containers */
11416 	if (free_qvectors) {
11417 		kfree(vsi->q_vectors);
11418 		vsi->q_vectors = NULL;
11419 	}
11420 	kfree(vsi->tx_rings);
11421 	vsi->tx_rings = NULL;
11422 	vsi->rx_rings = NULL;
11423 	vsi->xdp_rings = NULL;
11424 }
11425 
11426 /**
11427  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11428  * and lookup table
11429  * @vsi: Pointer to VSI structure
11430  */
11431 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11432 {
11433 	if (!vsi)
11434 		return;
11435 
11436 	kfree(vsi->rss_hkey_user);
11437 	vsi->rss_hkey_user = NULL;
11438 
11439 	kfree(vsi->rss_lut_user);
11440 	vsi->rss_lut_user = NULL;
11441 }
11442 
11443 /**
11444  * i40e_vsi_clear - Deallocate the VSI provided
11445  * @vsi: the VSI being un-configured
11446  **/
11447 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11448 {
11449 	struct i40e_pf *pf;
11450 
11451 	if (!vsi)
11452 		return 0;
11453 
11454 	if (!vsi->back)
11455 		goto free_vsi;
11456 	pf = vsi->back;
11457 
11458 	mutex_lock(&pf->switch_mutex);
11459 	if (!pf->vsi[vsi->idx]) {
11460 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11461 			vsi->idx, vsi->idx, vsi->type);
11462 		goto unlock_vsi;
11463 	}
11464 
11465 	if (pf->vsi[vsi->idx] != vsi) {
11466 		dev_err(&pf->pdev->dev,
11467 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11468 			pf->vsi[vsi->idx]->idx,
11469 			pf->vsi[vsi->idx]->type,
11470 			vsi->idx, vsi->type);
11471 		goto unlock_vsi;
11472 	}
11473 
11474 	/* updates the PF for this cleared vsi */
11475 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11476 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11477 
11478 	bitmap_free(vsi->af_xdp_zc_qps);
11479 	i40e_vsi_free_arrays(vsi, true);
11480 	i40e_clear_rss_config_user(vsi);
11481 
11482 	pf->vsi[vsi->idx] = NULL;
11483 	if (vsi->idx < pf->next_vsi)
11484 		pf->next_vsi = vsi->idx;
11485 
11486 unlock_vsi:
11487 	mutex_unlock(&pf->switch_mutex);
11488 free_vsi:
11489 	kfree(vsi);
11490 
11491 	return 0;
11492 }
11493 
11494 /**
11495  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11496  * @vsi: the VSI being cleaned
11497  **/
11498 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11499 {
11500 	int i;
11501 
11502 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11503 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11504 			kfree_rcu(vsi->tx_rings[i], rcu);
11505 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11506 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11507 			if (vsi->xdp_rings)
11508 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11509 		}
11510 	}
11511 }
11512 
11513 /**
11514  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11515  * @vsi: the VSI being configured
11516  **/
11517 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11518 {
11519 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11520 	struct i40e_pf *pf = vsi->back;
11521 	struct i40e_ring *ring;
11522 
11523 	/* Set basic values in the rings to be used later during open() */
11524 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11525 		/* allocate space for both Tx and Rx in one shot */
11526 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11527 		if (!ring)
11528 			goto err_out;
11529 
11530 		ring->queue_index = i;
11531 		ring->reg_idx = vsi->base_queue + i;
11532 		ring->ring_active = false;
11533 		ring->vsi = vsi;
11534 		ring->netdev = vsi->netdev;
11535 		ring->dev = &pf->pdev->dev;
11536 		ring->count = vsi->num_tx_desc;
11537 		ring->size = 0;
11538 		ring->dcb_tc = 0;
11539 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11540 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11541 		ring->itr_setting = pf->tx_itr_default;
11542 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11543 
11544 		if (!i40e_enabled_xdp_vsi(vsi))
11545 			goto setup_rx;
11546 
11547 		ring->queue_index = vsi->alloc_queue_pairs + i;
11548 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11549 		ring->ring_active = false;
11550 		ring->vsi = vsi;
11551 		ring->netdev = NULL;
11552 		ring->dev = &pf->pdev->dev;
11553 		ring->count = vsi->num_tx_desc;
11554 		ring->size = 0;
11555 		ring->dcb_tc = 0;
11556 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11557 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11558 		set_ring_xdp(ring);
11559 		ring->itr_setting = pf->tx_itr_default;
11560 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11561 
11562 setup_rx:
11563 		ring->queue_index = i;
11564 		ring->reg_idx = vsi->base_queue + i;
11565 		ring->ring_active = false;
11566 		ring->vsi = vsi;
11567 		ring->netdev = vsi->netdev;
11568 		ring->dev = &pf->pdev->dev;
11569 		ring->count = vsi->num_rx_desc;
11570 		ring->size = 0;
11571 		ring->dcb_tc = 0;
11572 		ring->itr_setting = pf->rx_itr_default;
11573 		WRITE_ONCE(vsi->rx_rings[i], ring);
11574 	}
11575 
11576 	return 0;
11577 
11578 err_out:
11579 	i40e_vsi_clear_rings(vsi);
11580 	return -ENOMEM;
11581 }
11582 
11583 /**
11584  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11585  * @pf: board private structure
11586  * @vectors: the number of MSI-X vectors to request
11587  *
11588  * Returns the number of vectors reserved, or error
11589  **/
11590 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11591 {
11592 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11593 					I40E_MIN_MSIX, vectors);
11594 	if (vectors < 0) {
11595 		dev_info(&pf->pdev->dev,
11596 			 "MSI-X vector reservation failed: %d\n", vectors);
11597 		vectors = 0;
11598 	}
11599 
11600 	return vectors;
11601 }
11602 
11603 /**
11604  * i40e_init_msix - Setup the MSIX capability
11605  * @pf: board private structure
11606  *
11607  * Work with the OS to set up the MSIX vectors needed.
11608  *
11609  * Returns the number of vectors reserved or negative on failure
11610  **/
11611 static int i40e_init_msix(struct i40e_pf *pf)
11612 {
11613 	struct i40e_hw *hw = &pf->hw;
11614 	int cpus, extra_vectors;
11615 	int vectors_left;
11616 	int v_budget, i;
11617 	int v_actual;
11618 	int iwarp_requested = 0;
11619 
11620 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11621 		return -ENODEV;
11622 
11623 	/* The number of vectors we'll request will be comprised of:
11624 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11625 	 *   - The number of LAN queue pairs
11626 	 *	- Queues being used for RSS.
11627 	 *		We don't need as many as max_rss_size vectors.
11628 	 *		use rss_size instead in the calculation since that
11629 	 *		is governed by number of cpus in the system.
11630 	 *	- assumes symmetric Tx/Rx pairing
11631 	 *   - The number of VMDq pairs
11632 	 *   - The CPU count within the NUMA node if iWARP is enabled
11633 	 * Once we count this up, try the request.
11634 	 *
11635 	 * If we can't get what we want, we'll simplify to nearly nothing
11636 	 * and try again.  If that still fails, we punt.
11637 	 */
11638 	vectors_left = hw->func_caps.num_msix_vectors;
11639 	v_budget = 0;
11640 
11641 	/* reserve one vector for miscellaneous handler */
11642 	if (vectors_left) {
11643 		v_budget++;
11644 		vectors_left--;
11645 	}
11646 
11647 	/* reserve some vectors for the main PF traffic queues. Initially we
11648 	 * only reserve at most 50% of the available vectors, in the case that
11649 	 * the number of online CPUs is large. This ensures that we can enable
11650 	 * extra features as well. Once we've enabled the other features, we
11651 	 * will use any remaining vectors to reach as close as we can to the
11652 	 * number of online CPUs.
11653 	 */
11654 	cpus = num_online_cpus();
11655 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11656 	vectors_left -= pf->num_lan_msix;
11657 
11658 	/* reserve one vector for sideband flow director */
11659 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11660 		if (vectors_left) {
11661 			pf->num_fdsb_msix = 1;
11662 			v_budget++;
11663 			vectors_left--;
11664 		} else {
11665 			pf->num_fdsb_msix = 0;
11666 		}
11667 	}
11668 
11669 	/* can we reserve enough for iWARP? */
11670 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11671 		iwarp_requested = pf->num_iwarp_msix;
11672 
11673 		if (!vectors_left)
11674 			pf->num_iwarp_msix = 0;
11675 		else if (vectors_left < pf->num_iwarp_msix)
11676 			pf->num_iwarp_msix = 1;
11677 		v_budget += pf->num_iwarp_msix;
11678 		vectors_left -= pf->num_iwarp_msix;
11679 	}
11680 
11681 	/* any vectors left over go for VMDq support */
11682 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11683 		if (!vectors_left) {
11684 			pf->num_vmdq_msix = 0;
11685 			pf->num_vmdq_qps = 0;
11686 		} else {
11687 			int vmdq_vecs_wanted =
11688 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11689 			int vmdq_vecs =
11690 				min_t(int, vectors_left, vmdq_vecs_wanted);
11691 
11692 			/* if we're short on vectors for what's desired, we limit
11693 			 * the queues per vmdq.  If this is still more than are
11694 			 * available, the user will need to change the number of
11695 			 * queues/vectors used by the PF later with the ethtool
11696 			 * channels command
11697 			 */
11698 			if (vectors_left < vmdq_vecs_wanted) {
11699 				pf->num_vmdq_qps = 1;
11700 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11701 				vmdq_vecs = min_t(int,
11702 						  vectors_left,
11703 						  vmdq_vecs_wanted);
11704 			}
11705 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11706 
11707 			v_budget += vmdq_vecs;
11708 			vectors_left -= vmdq_vecs;
11709 		}
11710 	}
11711 
11712 	/* On systems with a large number of SMP cores, we previously limited
11713 	 * the number of vectors for num_lan_msix to be at most 50% of the
11714 	 * available vectors, to allow for other features. Now, we add back
11715 	 * the remaining vectors. However, we ensure that the total
11716 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11717 	 * calculate the number of vectors we can add without going over the
11718 	 * cap of CPUs. For systems with a small number of CPUs this will be
11719 	 * zero.
11720 	 */
11721 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11722 	pf->num_lan_msix += extra_vectors;
11723 	vectors_left -= extra_vectors;
11724 
11725 	WARN(vectors_left < 0,
11726 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11727 
11728 	v_budget += pf->num_lan_msix;
11729 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11730 				   GFP_KERNEL);
11731 	if (!pf->msix_entries)
11732 		return -ENOMEM;
11733 
11734 	for (i = 0; i < v_budget; i++)
11735 		pf->msix_entries[i].entry = i;
11736 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11737 
11738 	if (v_actual < I40E_MIN_MSIX) {
11739 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11740 		kfree(pf->msix_entries);
11741 		pf->msix_entries = NULL;
11742 		pci_disable_msix(pf->pdev);
11743 		return -ENODEV;
11744 
11745 	} else if (v_actual == I40E_MIN_MSIX) {
11746 		/* Adjust for minimal MSIX use */
11747 		pf->num_vmdq_vsis = 0;
11748 		pf->num_vmdq_qps = 0;
11749 		pf->num_lan_qps = 1;
11750 		pf->num_lan_msix = 1;
11751 
11752 	} else if (v_actual != v_budget) {
11753 		/* If we have limited resources, we will start with no vectors
11754 		 * for the special features and then allocate vectors to some
11755 		 * of these features based on the policy and at the end disable
11756 		 * the features that did not get any vectors.
11757 		 */
11758 		int vec;
11759 
11760 		dev_info(&pf->pdev->dev,
11761 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11762 			 v_actual, v_budget);
11763 		/* reserve the misc vector */
11764 		vec = v_actual - 1;
11765 
11766 		/* Scale vector usage down */
11767 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11768 		pf->num_vmdq_vsis = 1;
11769 		pf->num_vmdq_qps = 1;
11770 
11771 		/* partition out the remaining vectors */
11772 		switch (vec) {
11773 		case 2:
11774 			pf->num_lan_msix = 1;
11775 			break;
11776 		case 3:
11777 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11778 				pf->num_lan_msix = 1;
11779 				pf->num_iwarp_msix = 1;
11780 			} else {
11781 				pf->num_lan_msix = 2;
11782 			}
11783 			break;
11784 		default:
11785 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11786 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11787 						 iwarp_requested);
11788 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11789 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11790 			} else {
11791 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11792 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11793 			}
11794 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11795 				pf->num_fdsb_msix = 1;
11796 				vec--;
11797 			}
11798 			pf->num_lan_msix = min_t(int,
11799 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11800 							      pf->num_lan_msix);
11801 			pf->num_lan_qps = pf->num_lan_msix;
11802 			break;
11803 		}
11804 	}
11805 
11806 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11807 	    (pf->num_fdsb_msix == 0)) {
11808 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11809 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11810 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11811 	}
11812 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11813 	    (pf->num_vmdq_msix == 0)) {
11814 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11815 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11816 	}
11817 
11818 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11819 	    (pf->num_iwarp_msix == 0)) {
11820 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11821 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11822 	}
11823 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11824 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11825 		   pf->num_lan_msix,
11826 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11827 		   pf->num_fdsb_msix,
11828 		   pf->num_iwarp_msix);
11829 
11830 	return v_actual;
11831 }
11832 
11833 /**
11834  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11835  * @vsi: the VSI being configured
11836  * @v_idx: index of the vector in the vsi struct
11837  *
11838  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11839  **/
11840 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11841 {
11842 	struct i40e_q_vector *q_vector;
11843 
11844 	/* allocate q_vector */
11845 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11846 	if (!q_vector)
11847 		return -ENOMEM;
11848 
11849 	q_vector->vsi = vsi;
11850 	q_vector->v_idx = v_idx;
11851 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11852 
11853 	if (vsi->netdev)
11854 		netif_napi_add(vsi->netdev, &q_vector->napi,
11855 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
11856 
11857 	/* tie q_vector and vsi together */
11858 	vsi->q_vectors[v_idx] = q_vector;
11859 
11860 	return 0;
11861 }
11862 
11863 /**
11864  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11865  * @vsi: the VSI being configured
11866  *
11867  * We allocate one q_vector per queue interrupt.  If allocation fails we
11868  * return -ENOMEM.
11869  **/
11870 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11871 {
11872 	struct i40e_pf *pf = vsi->back;
11873 	int err, v_idx, num_q_vectors;
11874 
11875 	/* if not MSIX, give the one vector only to the LAN VSI */
11876 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11877 		num_q_vectors = vsi->num_q_vectors;
11878 	else if (vsi == pf->vsi[pf->lan_vsi])
11879 		num_q_vectors = 1;
11880 	else
11881 		return -EINVAL;
11882 
11883 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11884 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11885 		if (err)
11886 			goto err_out;
11887 	}
11888 
11889 	return 0;
11890 
11891 err_out:
11892 	while (v_idx--)
11893 		i40e_free_q_vector(vsi, v_idx);
11894 
11895 	return err;
11896 }
11897 
11898 /**
11899  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11900  * @pf: board private structure to initialize
11901  **/
11902 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11903 {
11904 	int vectors = 0;
11905 	ssize_t size;
11906 
11907 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11908 		vectors = i40e_init_msix(pf);
11909 		if (vectors < 0) {
11910 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11911 				       I40E_FLAG_IWARP_ENABLED	|
11912 				       I40E_FLAG_RSS_ENABLED	|
11913 				       I40E_FLAG_DCB_CAPABLE	|
11914 				       I40E_FLAG_DCB_ENABLED	|
11915 				       I40E_FLAG_SRIOV_ENABLED	|
11916 				       I40E_FLAG_FD_SB_ENABLED	|
11917 				       I40E_FLAG_FD_ATR_ENABLED	|
11918 				       I40E_FLAG_VMDQ_ENABLED);
11919 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11920 
11921 			/* rework the queue expectations without MSIX */
11922 			i40e_determine_queue_usage(pf);
11923 		}
11924 	}
11925 
11926 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11927 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
11928 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
11929 		vectors = pci_enable_msi(pf->pdev);
11930 		if (vectors < 0) {
11931 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
11932 				 vectors);
11933 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
11934 		}
11935 		vectors = 1;  /* one MSI or Legacy vector */
11936 	}
11937 
11938 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
11939 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
11940 
11941 	/* set up vector assignment tracking */
11942 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
11943 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
11944 	if (!pf->irq_pile)
11945 		return -ENOMEM;
11946 
11947 	pf->irq_pile->num_entries = vectors;
11948 
11949 	/* track first vector for misc interrupts, ignore return */
11950 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
11951 
11952 	return 0;
11953 }
11954 
11955 /**
11956  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
11957  * @pf: private board data structure
11958  *
11959  * Restore the interrupt scheme that was cleared when we suspended the
11960  * device. This should be called during resume to re-allocate the q_vectors
11961  * and reacquire IRQs.
11962  */
11963 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
11964 {
11965 	int err, i;
11966 
11967 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
11968 	 * scheme. We need to re-enabled them here in order to attempt to
11969 	 * re-acquire the MSI or MSI-X vectors
11970 	 */
11971 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
11972 
11973 	err = i40e_init_interrupt_scheme(pf);
11974 	if (err)
11975 		return err;
11976 
11977 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
11978 	 * rings together again.
11979 	 */
11980 	for (i = 0; i < pf->num_alloc_vsi; i++) {
11981 		if (pf->vsi[i]) {
11982 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
11983 			if (err)
11984 				goto err_unwind;
11985 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
11986 		}
11987 	}
11988 
11989 	err = i40e_setup_misc_vector(pf);
11990 	if (err)
11991 		goto err_unwind;
11992 
11993 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
11994 		i40e_client_update_msix_info(pf);
11995 
11996 	return 0;
11997 
11998 err_unwind:
11999 	while (i--) {
12000 		if (pf->vsi[i])
12001 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12002 	}
12003 
12004 	return err;
12005 }
12006 
12007 /**
12008  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12009  * non queue events in recovery mode
12010  * @pf: board private structure
12011  *
12012  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12013  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12014  * This is handled differently than in recovery mode since no Tx/Rx resources
12015  * are being allocated.
12016  **/
12017 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12018 {
12019 	int err;
12020 
12021 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12022 		err = i40e_setup_misc_vector(pf);
12023 
12024 		if (err) {
12025 			dev_info(&pf->pdev->dev,
12026 				 "MSI-X misc vector request failed, error %d\n",
12027 				 err);
12028 			return err;
12029 		}
12030 	} else {
12031 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
12032 
12033 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12034 				  pf->int_name, pf);
12035 
12036 		if (err) {
12037 			dev_info(&pf->pdev->dev,
12038 				 "MSI/legacy misc vector request failed, error %d\n",
12039 				 err);
12040 			return err;
12041 		}
12042 		i40e_enable_misc_int_causes(pf);
12043 		i40e_irq_dynamic_enable_icr0(pf);
12044 	}
12045 
12046 	return 0;
12047 }
12048 
12049 /**
12050  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12051  * @pf: board private structure
12052  *
12053  * This sets up the handler for MSIX 0, which is used to manage the
12054  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12055  * when in MSI or Legacy interrupt mode.
12056  **/
12057 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12058 {
12059 	struct i40e_hw *hw = &pf->hw;
12060 	int err = 0;
12061 
12062 	/* Only request the IRQ once, the first time through. */
12063 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12064 		err = request_irq(pf->msix_entries[0].vector,
12065 				  i40e_intr, 0, pf->int_name, pf);
12066 		if (err) {
12067 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12068 			dev_info(&pf->pdev->dev,
12069 				 "request_irq for %s failed: %d\n",
12070 				 pf->int_name, err);
12071 			return -EFAULT;
12072 		}
12073 	}
12074 
12075 	i40e_enable_misc_int_causes(pf);
12076 
12077 	/* associate no queues to the misc vector */
12078 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12079 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12080 
12081 	i40e_flush(hw);
12082 
12083 	i40e_irq_dynamic_enable_icr0(pf);
12084 
12085 	return err;
12086 }
12087 
12088 /**
12089  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12090  * @vsi: Pointer to vsi structure
12091  * @seed: Buffter to store the hash keys
12092  * @lut: Buffer to store the lookup table entries
12093  * @lut_size: Size of buffer to store the lookup table entries
12094  *
12095  * Return 0 on success, negative on failure
12096  */
12097 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12098 			   u8 *lut, u16 lut_size)
12099 {
12100 	struct i40e_pf *pf = vsi->back;
12101 	struct i40e_hw *hw = &pf->hw;
12102 	int ret = 0;
12103 
12104 	if (seed) {
12105 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12106 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12107 		if (ret) {
12108 			dev_info(&pf->pdev->dev,
12109 				 "Cannot get RSS key, err %s aq_err %s\n",
12110 				 i40e_stat_str(&pf->hw, ret),
12111 				 i40e_aq_str(&pf->hw,
12112 					     pf->hw.aq.asq_last_status));
12113 			return ret;
12114 		}
12115 	}
12116 
12117 	if (lut) {
12118 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12119 
12120 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12121 		if (ret) {
12122 			dev_info(&pf->pdev->dev,
12123 				 "Cannot get RSS lut, err %s aq_err %s\n",
12124 				 i40e_stat_str(&pf->hw, ret),
12125 				 i40e_aq_str(&pf->hw,
12126 					     pf->hw.aq.asq_last_status));
12127 			return ret;
12128 		}
12129 	}
12130 
12131 	return ret;
12132 }
12133 
12134 /**
12135  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12136  * @vsi: Pointer to vsi structure
12137  * @seed: RSS hash seed
12138  * @lut: Lookup table
12139  * @lut_size: Lookup table size
12140  *
12141  * Returns 0 on success, negative on failure
12142  **/
12143 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12144 			       const u8 *lut, u16 lut_size)
12145 {
12146 	struct i40e_pf *pf = vsi->back;
12147 	struct i40e_hw *hw = &pf->hw;
12148 	u16 vf_id = vsi->vf_id;
12149 	u8 i;
12150 
12151 	/* Fill out hash function seed */
12152 	if (seed) {
12153 		u32 *seed_dw = (u32 *)seed;
12154 
12155 		if (vsi->type == I40E_VSI_MAIN) {
12156 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12157 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12158 		} else if (vsi->type == I40E_VSI_SRIOV) {
12159 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12160 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12161 		} else {
12162 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12163 		}
12164 	}
12165 
12166 	if (lut) {
12167 		u32 *lut_dw = (u32 *)lut;
12168 
12169 		if (vsi->type == I40E_VSI_MAIN) {
12170 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12171 				return -EINVAL;
12172 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12173 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12174 		} else if (vsi->type == I40E_VSI_SRIOV) {
12175 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12176 				return -EINVAL;
12177 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12178 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12179 		} else {
12180 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12181 		}
12182 	}
12183 	i40e_flush(hw);
12184 
12185 	return 0;
12186 }
12187 
12188 /**
12189  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12190  * @vsi: Pointer to VSI structure
12191  * @seed: Buffer to store the keys
12192  * @lut: Buffer to store the lookup table entries
12193  * @lut_size: Size of buffer to store the lookup table entries
12194  *
12195  * Returns 0 on success, negative on failure
12196  */
12197 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12198 			    u8 *lut, u16 lut_size)
12199 {
12200 	struct i40e_pf *pf = vsi->back;
12201 	struct i40e_hw *hw = &pf->hw;
12202 	u16 i;
12203 
12204 	if (seed) {
12205 		u32 *seed_dw = (u32 *)seed;
12206 
12207 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12208 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12209 	}
12210 	if (lut) {
12211 		u32 *lut_dw = (u32 *)lut;
12212 
12213 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12214 			return -EINVAL;
12215 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12216 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12217 	}
12218 
12219 	return 0;
12220 }
12221 
12222 /**
12223  * i40e_config_rss - Configure RSS keys and lut
12224  * @vsi: Pointer to VSI structure
12225  * @seed: RSS hash seed
12226  * @lut: Lookup table
12227  * @lut_size: Lookup table size
12228  *
12229  * Returns 0 on success, negative on failure
12230  */
12231 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12232 {
12233 	struct i40e_pf *pf = vsi->back;
12234 
12235 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12236 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12237 	else
12238 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12239 }
12240 
12241 /**
12242  * i40e_get_rss - Get RSS keys and lut
12243  * @vsi: Pointer to VSI structure
12244  * @seed: Buffer to store the keys
12245  * @lut: Buffer to store the lookup table entries
12246  * @lut_size: Size of buffer to store the lookup table entries
12247  *
12248  * Returns 0 on success, negative on failure
12249  */
12250 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12251 {
12252 	struct i40e_pf *pf = vsi->back;
12253 
12254 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12255 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12256 	else
12257 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12258 }
12259 
12260 /**
12261  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12262  * @pf: Pointer to board private structure
12263  * @lut: Lookup table
12264  * @rss_table_size: Lookup table size
12265  * @rss_size: Range of queue number for hashing
12266  */
12267 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12268 		       u16 rss_table_size, u16 rss_size)
12269 {
12270 	u16 i;
12271 
12272 	for (i = 0; i < rss_table_size; i++)
12273 		lut[i] = i % rss_size;
12274 }
12275 
12276 /**
12277  * i40e_pf_config_rss - Prepare for RSS if used
12278  * @pf: board private structure
12279  **/
12280 static int i40e_pf_config_rss(struct i40e_pf *pf)
12281 {
12282 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12283 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12284 	u8 *lut;
12285 	struct i40e_hw *hw = &pf->hw;
12286 	u32 reg_val;
12287 	u64 hena;
12288 	int ret;
12289 
12290 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12291 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12292 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12293 	hena |= i40e_pf_get_default_rss_hena(pf);
12294 
12295 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12296 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12297 
12298 	/* Determine the RSS table size based on the hardware capabilities */
12299 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12300 	reg_val = (pf->rss_table_size == 512) ?
12301 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12302 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12303 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12304 
12305 	/* Determine the RSS size of the VSI */
12306 	if (!vsi->rss_size) {
12307 		u16 qcount;
12308 		/* If the firmware does something weird during VSI init, we
12309 		 * could end up with zero TCs. Check for that to avoid
12310 		 * divide-by-zero. It probably won't pass traffic, but it also
12311 		 * won't panic.
12312 		 */
12313 		qcount = vsi->num_queue_pairs /
12314 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12315 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12316 	}
12317 	if (!vsi->rss_size)
12318 		return -EINVAL;
12319 
12320 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12321 	if (!lut)
12322 		return -ENOMEM;
12323 
12324 	/* Use user configured lut if there is one, otherwise use default */
12325 	if (vsi->rss_lut_user)
12326 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12327 	else
12328 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12329 
12330 	/* Use user configured hash key if there is one, otherwise
12331 	 * use default.
12332 	 */
12333 	if (vsi->rss_hkey_user)
12334 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12335 	else
12336 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12337 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12338 	kfree(lut);
12339 
12340 	return ret;
12341 }
12342 
12343 /**
12344  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12345  * @pf: board private structure
12346  * @queue_count: the requested queue count for rss.
12347  *
12348  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12349  * count which may be different from the requested queue count.
12350  * Note: expects to be called while under rtnl_lock()
12351  **/
12352 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12353 {
12354 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12355 	int new_rss_size;
12356 
12357 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12358 		return 0;
12359 
12360 	queue_count = min_t(int, queue_count, num_online_cpus());
12361 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12362 
12363 	if (queue_count != vsi->num_queue_pairs) {
12364 		u16 qcount;
12365 
12366 		vsi->req_queue_pairs = queue_count;
12367 		i40e_prep_for_reset(pf);
12368 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12369 			return pf->alloc_rss_size;
12370 
12371 		pf->alloc_rss_size = new_rss_size;
12372 
12373 		i40e_reset_and_rebuild(pf, true, true);
12374 
12375 		/* Discard the user configured hash keys and lut, if less
12376 		 * queues are enabled.
12377 		 */
12378 		if (queue_count < vsi->rss_size) {
12379 			i40e_clear_rss_config_user(vsi);
12380 			dev_dbg(&pf->pdev->dev,
12381 				"discard user configured hash keys and lut\n");
12382 		}
12383 
12384 		/* Reset vsi->rss_size, as number of enabled queues changed */
12385 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12386 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12387 
12388 		i40e_pf_config_rss(pf);
12389 	}
12390 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12391 		 vsi->req_queue_pairs, pf->rss_size_max);
12392 	return pf->alloc_rss_size;
12393 }
12394 
12395 /**
12396  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12397  * @pf: board private structure
12398  **/
12399 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
12400 {
12401 	i40e_status status;
12402 	bool min_valid, max_valid;
12403 	u32 max_bw, min_bw;
12404 
12405 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12406 					   &min_valid, &max_valid);
12407 
12408 	if (!status) {
12409 		if (min_valid)
12410 			pf->min_bw = min_bw;
12411 		if (max_valid)
12412 			pf->max_bw = max_bw;
12413 	}
12414 
12415 	return status;
12416 }
12417 
12418 /**
12419  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12420  * @pf: board private structure
12421  **/
12422 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
12423 {
12424 	struct i40e_aqc_configure_partition_bw_data bw_data;
12425 	i40e_status status;
12426 
12427 	memset(&bw_data, 0, sizeof(bw_data));
12428 
12429 	/* Set the valid bit for this PF */
12430 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12431 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12432 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12433 
12434 	/* Set the new bandwidths */
12435 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12436 
12437 	return status;
12438 }
12439 
12440 /**
12441  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12442  * @pf: board private structure
12443  **/
12444 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12445 {
12446 	/* Commit temporary BW setting to permanent NVM image */
12447 	enum i40e_admin_queue_err last_aq_status;
12448 	i40e_status ret;
12449 	u16 nvm_word;
12450 
12451 	if (pf->hw.partition_id != 1) {
12452 		dev_info(&pf->pdev->dev,
12453 			 "Commit BW only works on partition 1! This is partition %d",
12454 			 pf->hw.partition_id);
12455 		ret = I40E_NOT_SUPPORTED;
12456 		goto bw_commit_out;
12457 	}
12458 
12459 	/* Acquire NVM for read access */
12460 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12461 	last_aq_status = pf->hw.aq.asq_last_status;
12462 	if (ret) {
12463 		dev_info(&pf->pdev->dev,
12464 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
12465 			 i40e_stat_str(&pf->hw, ret),
12466 			 i40e_aq_str(&pf->hw, last_aq_status));
12467 		goto bw_commit_out;
12468 	}
12469 
12470 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12471 	ret = i40e_aq_read_nvm(&pf->hw,
12472 			       I40E_SR_NVM_CONTROL_WORD,
12473 			       0x10, sizeof(nvm_word), &nvm_word,
12474 			       false, NULL);
12475 	/* Save off last admin queue command status before releasing
12476 	 * the NVM
12477 	 */
12478 	last_aq_status = pf->hw.aq.asq_last_status;
12479 	i40e_release_nvm(&pf->hw);
12480 	if (ret) {
12481 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
12482 			 i40e_stat_str(&pf->hw, ret),
12483 			 i40e_aq_str(&pf->hw, last_aq_status));
12484 		goto bw_commit_out;
12485 	}
12486 
12487 	/* Wait a bit for NVM release to complete */
12488 	msleep(50);
12489 
12490 	/* Acquire NVM for write access */
12491 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12492 	last_aq_status = pf->hw.aq.asq_last_status;
12493 	if (ret) {
12494 		dev_info(&pf->pdev->dev,
12495 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
12496 			 i40e_stat_str(&pf->hw, ret),
12497 			 i40e_aq_str(&pf->hw, last_aq_status));
12498 		goto bw_commit_out;
12499 	}
12500 	/* Write it back out unchanged to initiate update NVM,
12501 	 * which will force a write of the shadow (alt) RAM to
12502 	 * the NVM - thus storing the bandwidth values permanently.
12503 	 */
12504 	ret = i40e_aq_update_nvm(&pf->hw,
12505 				 I40E_SR_NVM_CONTROL_WORD,
12506 				 0x10, sizeof(nvm_word),
12507 				 &nvm_word, true, 0, NULL);
12508 	/* Save off last admin queue command status before releasing
12509 	 * the NVM
12510 	 */
12511 	last_aq_status = pf->hw.aq.asq_last_status;
12512 	i40e_release_nvm(&pf->hw);
12513 	if (ret)
12514 		dev_info(&pf->pdev->dev,
12515 			 "BW settings NOT SAVED, err %s aq_err %s\n",
12516 			 i40e_stat_str(&pf->hw, ret),
12517 			 i40e_aq_str(&pf->hw, last_aq_status));
12518 bw_commit_out:
12519 
12520 	return ret;
12521 }
12522 
12523 /**
12524  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12525  * if total port shutdown feature is enabled for this PF
12526  * @pf: board private structure
12527  **/
12528 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12529 {
12530 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12531 #define I40E_FEATURES_ENABLE_PTR		0x2A
12532 #define I40E_CURRENT_SETTING_PTR		0x2B
12533 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12534 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12535 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12536 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12537 	i40e_status read_status = I40E_SUCCESS;
12538 	u16 sr_emp_sr_settings_ptr = 0;
12539 	u16 features_enable = 0;
12540 	u16 link_behavior = 0;
12541 	bool ret = false;
12542 
12543 	read_status = i40e_read_nvm_word(&pf->hw,
12544 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12545 					 &sr_emp_sr_settings_ptr);
12546 	if (read_status)
12547 		goto err_nvm;
12548 	read_status = i40e_read_nvm_word(&pf->hw,
12549 					 sr_emp_sr_settings_ptr +
12550 					 I40E_FEATURES_ENABLE_PTR,
12551 					 &features_enable);
12552 	if (read_status)
12553 		goto err_nvm;
12554 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12555 		read_status = i40e_read_nvm_module_data(&pf->hw,
12556 							I40E_SR_EMP_SR_SETTINGS_PTR,
12557 							I40E_CURRENT_SETTING_PTR,
12558 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12559 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12560 							&link_behavior);
12561 		if (read_status)
12562 			goto err_nvm;
12563 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12564 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12565 	}
12566 	return ret;
12567 
12568 err_nvm:
12569 	dev_warn(&pf->pdev->dev,
12570 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
12571 		 i40e_stat_str(&pf->hw, read_status));
12572 	return ret;
12573 }
12574 
12575 /**
12576  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12577  * @pf: board private structure to initialize
12578  *
12579  * i40e_sw_init initializes the Adapter private data structure.
12580  * Fields are initialized based on PCI device information and
12581  * OS network device settings (MTU size).
12582  **/
12583 static int i40e_sw_init(struct i40e_pf *pf)
12584 {
12585 	int err = 0;
12586 	int size;
12587 	u16 pow;
12588 
12589 	/* Set default capability flags */
12590 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12591 		    I40E_FLAG_MSI_ENABLED     |
12592 		    I40E_FLAG_MSIX_ENABLED;
12593 
12594 	/* Set default ITR */
12595 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12596 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12597 
12598 	/* Depending on PF configurations, it is possible that the RSS
12599 	 * maximum might end up larger than the available queues
12600 	 */
12601 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12602 	pf->alloc_rss_size = 1;
12603 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12604 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12605 				 pf->hw.func_caps.num_tx_qp);
12606 
12607 	/* find the next higher power-of-2 of num cpus */
12608 	pow = roundup_pow_of_two(num_online_cpus());
12609 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12610 
12611 	if (pf->hw.func_caps.rss) {
12612 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12613 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12614 					   num_online_cpus());
12615 	}
12616 
12617 	/* MFP mode enabled */
12618 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12619 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12620 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12621 		if (i40e_get_partition_bw_setting(pf)) {
12622 			dev_warn(&pf->pdev->dev,
12623 				 "Could not get partition bw settings\n");
12624 		} else {
12625 			dev_info(&pf->pdev->dev,
12626 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12627 				 pf->min_bw, pf->max_bw);
12628 
12629 			/* nudge the Tx scheduler */
12630 			i40e_set_partition_bw_setting(pf);
12631 		}
12632 	}
12633 
12634 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12635 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12636 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12637 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12638 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12639 		    pf->hw.num_partitions > 1)
12640 			dev_info(&pf->pdev->dev,
12641 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12642 		else
12643 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12644 		pf->fdir_pf_filter_count =
12645 				 pf->hw.func_caps.fd_filters_guaranteed;
12646 		pf->hw.fdir_shared_filter_count =
12647 				 pf->hw.func_caps.fd_filters_best_effort;
12648 	}
12649 
12650 	if (pf->hw.mac.type == I40E_MAC_X722) {
12651 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12652 				    I40E_HW_128_QP_RSS_CAPABLE |
12653 				    I40E_HW_ATR_EVICT_CAPABLE |
12654 				    I40E_HW_WB_ON_ITR_CAPABLE |
12655 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12656 				    I40E_HW_NO_PCI_LINK_CHECK |
12657 				    I40E_HW_USE_SET_LLDP_MIB |
12658 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12659 				    I40E_HW_PTP_L4_CAPABLE |
12660 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12661 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12662 
12663 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12664 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12665 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12666 			dev_warn(&pf->pdev->dev,
12667 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12668 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12669 		}
12670 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12671 		   ((pf->hw.aq.api_maj_ver == 1) &&
12672 		    (pf->hw.aq.api_min_ver > 4))) {
12673 		/* Supported in FW API version higher than 1.4 */
12674 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12675 	}
12676 
12677 	/* Enable HW ATR eviction if possible */
12678 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12679 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12680 
12681 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12682 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12683 	    (pf->hw.aq.fw_maj_ver < 4))) {
12684 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12685 		/* No DCB support  for FW < v4.33 */
12686 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12687 	}
12688 
12689 	/* Disable FW LLDP if FW < v4.3 */
12690 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12691 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12692 	    (pf->hw.aq.fw_maj_ver < 4)))
12693 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12694 
12695 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12696 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12697 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12698 	    (pf->hw.aq.fw_maj_ver >= 5)))
12699 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12700 
12701 	/* Enable PTP L4 if FW > v6.0 */
12702 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12703 	    pf->hw.aq.fw_maj_ver >= 6)
12704 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12705 
12706 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12707 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12708 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12709 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12710 	}
12711 
12712 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12713 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12714 		/* IWARP needs one extra vector for CQP just like MISC.*/
12715 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12716 	}
12717 	/* Stopping FW LLDP engine is supported on XL710 and X722
12718 	 * starting from FW versions determined in i40e_init_adminq.
12719 	 * Stopping the FW LLDP engine is not supported on XL710
12720 	 * if NPAR is functioning so unset this hw flag in this case.
12721 	 */
12722 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12723 	    pf->hw.func_caps.npar_enable &&
12724 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12725 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12726 
12727 #ifdef CONFIG_PCI_IOV
12728 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12729 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12730 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12731 		pf->num_req_vfs = min_t(int,
12732 					pf->hw.func_caps.num_vfs,
12733 					I40E_MAX_VF_COUNT);
12734 	}
12735 #endif /* CONFIG_PCI_IOV */
12736 	pf->eeprom_version = 0xDEAD;
12737 	pf->lan_veb = I40E_NO_VEB;
12738 	pf->lan_vsi = I40E_NO_VSI;
12739 
12740 	/* By default FW has this off for performance reasons */
12741 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12742 
12743 	/* set up queue assignment tracking */
12744 	size = sizeof(struct i40e_lump_tracking)
12745 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12746 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12747 	if (!pf->qp_pile) {
12748 		err = -ENOMEM;
12749 		goto sw_init_done;
12750 	}
12751 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12752 
12753 	pf->tx_timeout_recovery_level = 1;
12754 
12755 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12756 	    i40e_is_total_port_shutdown_enabled(pf)) {
12757 		/* Link down on close must be on when total port shutdown
12758 		 * is enabled for a given port
12759 		 */
12760 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12761 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12762 		dev_info(&pf->pdev->dev,
12763 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12764 	}
12765 	mutex_init(&pf->switch_mutex);
12766 
12767 sw_init_done:
12768 	return err;
12769 }
12770 
12771 /**
12772  * i40e_set_ntuple - set the ntuple feature flag and take action
12773  * @pf: board private structure to initialize
12774  * @features: the feature set that the stack is suggesting
12775  *
12776  * returns a bool to indicate if reset needs to happen
12777  **/
12778 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12779 {
12780 	bool need_reset = false;
12781 
12782 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12783 	 * the state changed, we need to reset.
12784 	 */
12785 	if (features & NETIF_F_NTUPLE) {
12786 		/* Enable filters and mark for reset */
12787 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12788 			need_reset = true;
12789 		/* enable FD_SB only if there is MSI-X vector and no cloud
12790 		 * filters exist
12791 		 */
12792 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12793 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12794 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12795 		}
12796 	} else {
12797 		/* turn off filters, mark for reset and clear SW filter list */
12798 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12799 			need_reset = true;
12800 			i40e_fdir_filter_exit(pf);
12801 		}
12802 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12803 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12804 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12805 
12806 		/* reset fd counters */
12807 		pf->fd_add_err = 0;
12808 		pf->fd_atr_cnt = 0;
12809 		/* if ATR was auto disabled it can be re-enabled. */
12810 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12811 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12812 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12813 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12814 	}
12815 	return need_reset;
12816 }
12817 
12818 /**
12819  * i40e_clear_rss_lut - clear the rx hash lookup table
12820  * @vsi: the VSI being configured
12821  **/
12822 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12823 {
12824 	struct i40e_pf *pf = vsi->back;
12825 	struct i40e_hw *hw = &pf->hw;
12826 	u16 vf_id = vsi->vf_id;
12827 	u8 i;
12828 
12829 	if (vsi->type == I40E_VSI_MAIN) {
12830 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12831 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12832 	} else if (vsi->type == I40E_VSI_SRIOV) {
12833 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12834 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12835 	} else {
12836 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12837 	}
12838 }
12839 
12840 /**
12841  * i40e_set_features - set the netdev feature flags
12842  * @netdev: ptr to the netdev being adjusted
12843  * @features: the feature set that the stack is suggesting
12844  * Note: expects to be called while under rtnl_lock()
12845  **/
12846 static int i40e_set_features(struct net_device *netdev,
12847 			     netdev_features_t features)
12848 {
12849 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12850 	struct i40e_vsi *vsi = np->vsi;
12851 	struct i40e_pf *pf = vsi->back;
12852 	bool need_reset;
12853 
12854 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12855 		i40e_pf_config_rss(pf);
12856 	else if (!(features & NETIF_F_RXHASH) &&
12857 		 netdev->features & NETIF_F_RXHASH)
12858 		i40e_clear_rss_lut(vsi);
12859 
12860 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12861 		i40e_vlan_stripping_enable(vsi);
12862 	else
12863 		i40e_vlan_stripping_disable(vsi);
12864 
12865 	if (!(features & NETIF_F_HW_TC) &&
12866 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12867 		dev_err(&pf->pdev->dev,
12868 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12869 		return -EINVAL;
12870 	}
12871 
12872 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12873 		i40e_del_all_macvlans(vsi);
12874 
12875 	need_reset = i40e_set_ntuple(pf, features);
12876 
12877 	if (need_reset)
12878 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12879 
12880 	return 0;
12881 }
12882 
12883 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12884 				    unsigned int table, unsigned int idx,
12885 				    struct udp_tunnel_info *ti)
12886 {
12887 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12888 	struct i40e_hw *hw = &np->vsi->back->hw;
12889 	u8 type, filter_index;
12890 	i40e_status ret;
12891 
12892 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12893 						   I40E_AQC_TUNNEL_TYPE_NGE;
12894 
12895 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12896 				     NULL);
12897 	if (ret) {
12898 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12899 			    i40e_stat_str(hw, ret),
12900 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12901 		return -EIO;
12902 	}
12903 
12904 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12905 	return 0;
12906 }
12907 
12908 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12909 				      unsigned int table, unsigned int idx,
12910 				      struct udp_tunnel_info *ti)
12911 {
12912 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12913 	struct i40e_hw *hw = &np->vsi->back->hw;
12914 	i40e_status ret;
12915 
12916 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12917 	if (ret) {
12918 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
12919 			    i40e_stat_str(hw, ret),
12920 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12921 		return -EIO;
12922 	}
12923 
12924 	return 0;
12925 }
12926 
12927 static int i40e_get_phys_port_id(struct net_device *netdev,
12928 				 struct netdev_phys_item_id *ppid)
12929 {
12930 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12931 	struct i40e_pf *pf = np->vsi->back;
12932 	struct i40e_hw *hw = &pf->hw;
12933 
12934 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
12935 		return -EOPNOTSUPP;
12936 
12937 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
12938 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
12939 
12940 	return 0;
12941 }
12942 
12943 /**
12944  * i40e_ndo_fdb_add - add an entry to the hardware database
12945  * @ndm: the input from the stack
12946  * @tb: pointer to array of nladdr (unused)
12947  * @dev: the net device pointer
12948  * @addr: the MAC address entry being added
12949  * @vid: VLAN ID
12950  * @flags: instructions from stack about fdb operation
12951  * @extack: netlink extended ack, unused currently
12952  */
12953 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
12954 			    struct net_device *dev,
12955 			    const unsigned char *addr, u16 vid,
12956 			    u16 flags,
12957 			    struct netlink_ext_ack *extack)
12958 {
12959 	struct i40e_netdev_priv *np = netdev_priv(dev);
12960 	struct i40e_pf *pf = np->vsi->back;
12961 	int err = 0;
12962 
12963 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
12964 		return -EOPNOTSUPP;
12965 
12966 	if (vid) {
12967 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
12968 		return -EINVAL;
12969 	}
12970 
12971 	/* Hardware does not support aging addresses so if a
12972 	 * ndm_state is given only allow permanent addresses
12973 	 */
12974 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
12975 		netdev_info(dev, "FDB only supports static addresses\n");
12976 		return -EINVAL;
12977 	}
12978 
12979 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
12980 		err = dev_uc_add_excl(dev, addr);
12981 	else if (is_multicast_ether_addr(addr))
12982 		err = dev_mc_add_excl(dev, addr);
12983 	else
12984 		err = -EINVAL;
12985 
12986 	/* Only return duplicate errors if NLM_F_EXCL is set */
12987 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
12988 		err = 0;
12989 
12990 	return err;
12991 }
12992 
12993 /**
12994  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
12995  * @dev: the netdev being configured
12996  * @nlh: RTNL message
12997  * @flags: bridge flags
12998  * @extack: netlink extended ack
12999  *
13000  * Inserts a new hardware bridge if not already created and
13001  * enables the bridging mode requested (VEB or VEPA). If the
13002  * hardware bridge has already been inserted and the request
13003  * is to change the mode then that requires a PF reset to
13004  * allow rebuild of the components with required hardware
13005  * bridge mode enabled.
13006  *
13007  * Note: expects to be called while under rtnl_lock()
13008  **/
13009 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13010 				   struct nlmsghdr *nlh,
13011 				   u16 flags,
13012 				   struct netlink_ext_ack *extack)
13013 {
13014 	struct i40e_netdev_priv *np = netdev_priv(dev);
13015 	struct i40e_vsi *vsi = np->vsi;
13016 	struct i40e_pf *pf = vsi->back;
13017 	struct i40e_veb *veb = NULL;
13018 	struct nlattr *attr, *br_spec;
13019 	int i, rem;
13020 
13021 	/* Only for PF VSI for now */
13022 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13023 		return -EOPNOTSUPP;
13024 
13025 	/* Find the HW bridge for PF VSI */
13026 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13027 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13028 			veb = pf->veb[i];
13029 	}
13030 
13031 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13032 
13033 	nla_for_each_nested(attr, br_spec, rem) {
13034 		__u16 mode;
13035 
13036 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
13037 			continue;
13038 
13039 		mode = nla_get_u16(attr);
13040 		if ((mode != BRIDGE_MODE_VEPA) &&
13041 		    (mode != BRIDGE_MODE_VEB))
13042 			return -EINVAL;
13043 
13044 		/* Insert a new HW bridge */
13045 		if (!veb) {
13046 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13047 					     vsi->tc_config.enabled_tc);
13048 			if (veb) {
13049 				veb->bridge_mode = mode;
13050 				i40e_config_bridge_mode(veb);
13051 			} else {
13052 				/* No Bridge HW offload available */
13053 				return -ENOENT;
13054 			}
13055 			break;
13056 		} else if (mode != veb->bridge_mode) {
13057 			/* Existing HW bridge but different mode needs reset */
13058 			veb->bridge_mode = mode;
13059 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13060 			if (mode == BRIDGE_MODE_VEB)
13061 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13062 			else
13063 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13064 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13065 			break;
13066 		}
13067 	}
13068 
13069 	return 0;
13070 }
13071 
13072 /**
13073  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13074  * @skb: skb buff
13075  * @pid: process id
13076  * @seq: RTNL message seq #
13077  * @dev: the netdev being configured
13078  * @filter_mask: unused
13079  * @nlflags: netlink flags passed in
13080  *
13081  * Return the mode in which the hardware bridge is operating in
13082  * i.e VEB or VEPA.
13083  **/
13084 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13085 				   struct net_device *dev,
13086 				   u32 __always_unused filter_mask,
13087 				   int nlflags)
13088 {
13089 	struct i40e_netdev_priv *np = netdev_priv(dev);
13090 	struct i40e_vsi *vsi = np->vsi;
13091 	struct i40e_pf *pf = vsi->back;
13092 	struct i40e_veb *veb = NULL;
13093 	int i;
13094 
13095 	/* Only for PF VSI for now */
13096 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13097 		return -EOPNOTSUPP;
13098 
13099 	/* Find the HW bridge for the PF VSI */
13100 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13101 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13102 			veb = pf->veb[i];
13103 	}
13104 
13105 	if (!veb)
13106 		return 0;
13107 
13108 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13109 				       0, 0, nlflags, filter_mask, NULL);
13110 }
13111 
13112 /**
13113  * i40e_features_check - Validate encapsulated packet conforms to limits
13114  * @skb: skb buff
13115  * @dev: This physical port's netdev
13116  * @features: Offload features that the stack believes apply
13117  **/
13118 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13119 					     struct net_device *dev,
13120 					     netdev_features_t features)
13121 {
13122 	size_t len;
13123 
13124 	/* No point in doing any of this if neither checksum nor GSO are
13125 	 * being requested for this frame.  We can rule out both by just
13126 	 * checking for CHECKSUM_PARTIAL
13127 	 */
13128 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13129 		return features;
13130 
13131 	/* We cannot support GSO if the MSS is going to be less than
13132 	 * 64 bytes.  If it is then we need to drop support for GSO.
13133 	 */
13134 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13135 		features &= ~NETIF_F_GSO_MASK;
13136 
13137 	/* MACLEN can support at most 63 words */
13138 	len = skb_network_header(skb) - skb->data;
13139 	if (len & ~(63 * 2))
13140 		goto out_err;
13141 
13142 	/* IPLEN and EIPLEN can support at most 127 dwords */
13143 	len = skb_transport_header(skb) - skb_network_header(skb);
13144 	if (len & ~(127 * 4))
13145 		goto out_err;
13146 
13147 	if (skb->encapsulation) {
13148 		/* L4TUNLEN can support 127 words */
13149 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13150 		if (len & ~(127 * 2))
13151 			goto out_err;
13152 
13153 		/* IPLEN can support at most 127 dwords */
13154 		len = skb_inner_transport_header(skb) -
13155 		      skb_inner_network_header(skb);
13156 		if (len & ~(127 * 4))
13157 			goto out_err;
13158 	}
13159 
13160 	/* No need to validate L4LEN as TCP is the only protocol with a
13161 	 * a flexible value and we support all possible values supported
13162 	 * by TCP, which is at most 15 dwords
13163 	 */
13164 
13165 	return features;
13166 out_err:
13167 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13168 }
13169 
13170 /**
13171  * i40e_xdp_setup - add/remove an XDP program
13172  * @vsi: VSI to changed
13173  * @prog: XDP program
13174  * @extack: netlink extended ack
13175  **/
13176 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13177 			  struct netlink_ext_ack *extack)
13178 {
13179 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
13180 	struct i40e_pf *pf = vsi->back;
13181 	struct bpf_prog *old_prog;
13182 	bool need_reset;
13183 	int i;
13184 
13185 	/* Don't allow frames that span over multiple buffers */
13186 	if (frame_size > vsi->rx_buf_len) {
13187 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
13188 		return -EINVAL;
13189 	}
13190 
13191 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13192 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13193 
13194 	if (need_reset)
13195 		i40e_prep_for_reset(pf);
13196 
13197 	/* VSI shall be deleted in a moment, just return EINVAL */
13198 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13199 		return -EINVAL;
13200 
13201 	old_prog = xchg(&vsi->xdp_prog, prog);
13202 
13203 	if (need_reset) {
13204 		if (!prog)
13205 			/* Wait until ndo_xsk_wakeup completes. */
13206 			synchronize_rcu();
13207 		i40e_reset_and_rebuild(pf, true, true);
13208 	}
13209 
13210 	for (i = 0; i < vsi->num_queue_pairs; i++)
13211 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13212 
13213 	if (old_prog)
13214 		bpf_prog_put(old_prog);
13215 
13216 	/* Kick start the NAPI context if there is an AF_XDP socket open
13217 	 * on that queue id. This so that receiving will start.
13218 	 */
13219 	if (need_reset && prog)
13220 		for (i = 0; i < vsi->num_queue_pairs; i++)
13221 			if (vsi->xdp_rings[i]->xsk_pool)
13222 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13223 						      XDP_WAKEUP_RX);
13224 
13225 	return 0;
13226 }
13227 
13228 /**
13229  * i40e_enter_busy_conf - Enters busy config state
13230  * @vsi: vsi
13231  *
13232  * Returns 0 on success, <0 for failure.
13233  **/
13234 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13235 {
13236 	struct i40e_pf *pf = vsi->back;
13237 	int timeout = 50;
13238 
13239 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13240 		timeout--;
13241 		if (!timeout)
13242 			return -EBUSY;
13243 		usleep_range(1000, 2000);
13244 	}
13245 
13246 	return 0;
13247 }
13248 
13249 /**
13250  * i40e_exit_busy_conf - Exits busy config state
13251  * @vsi: vsi
13252  **/
13253 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13254 {
13255 	struct i40e_pf *pf = vsi->back;
13256 
13257 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13258 }
13259 
13260 /**
13261  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13262  * @vsi: vsi
13263  * @queue_pair: queue pair
13264  **/
13265 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13266 {
13267 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13268 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13269 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13270 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13271 	if (i40e_enabled_xdp_vsi(vsi)) {
13272 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13273 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13274 	}
13275 }
13276 
13277 /**
13278  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13279  * @vsi: vsi
13280  * @queue_pair: queue pair
13281  **/
13282 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13283 {
13284 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13285 	if (i40e_enabled_xdp_vsi(vsi)) {
13286 		/* Make sure that in-progress ndo_xdp_xmit calls are
13287 		 * completed.
13288 		 */
13289 		synchronize_rcu();
13290 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13291 	}
13292 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13293 }
13294 
13295 /**
13296  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13297  * @vsi: vsi
13298  * @queue_pair: queue pair
13299  * @enable: true for enable, false for disable
13300  **/
13301 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13302 					bool enable)
13303 {
13304 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13305 	struct i40e_q_vector *q_vector = rxr->q_vector;
13306 
13307 	if (!vsi->netdev)
13308 		return;
13309 
13310 	/* All rings in a qp belong to the same qvector. */
13311 	if (q_vector->rx.ring || q_vector->tx.ring) {
13312 		if (enable)
13313 			napi_enable(&q_vector->napi);
13314 		else
13315 			napi_disable(&q_vector->napi);
13316 	}
13317 }
13318 
13319 /**
13320  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13321  * @vsi: vsi
13322  * @queue_pair: queue pair
13323  * @enable: true for enable, false for disable
13324  *
13325  * Returns 0 on success, <0 on failure.
13326  **/
13327 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13328 					bool enable)
13329 {
13330 	struct i40e_pf *pf = vsi->back;
13331 	int pf_q, ret = 0;
13332 
13333 	pf_q = vsi->base_queue + queue_pair;
13334 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13335 				     false /*is xdp*/, enable);
13336 	if (ret) {
13337 		dev_info(&pf->pdev->dev,
13338 			 "VSI seid %d Tx ring %d %sable timeout\n",
13339 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13340 		return ret;
13341 	}
13342 
13343 	i40e_control_rx_q(pf, pf_q, enable);
13344 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13345 	if (ret) {
13346 		dev_info(&pf->pdev->dev,
13347 			 "VSI seid %d Rx ring %d %sable timeout\n",
13348 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13349 		return ret;
13350 	}
13351 
13352 	/* Due to HW errata, on Rx disable only, the register can
13353 	 * indicate done before it really is. Needs 50ms to be sure
13354 	 */
13355 	if (!enable)
13356 		mdelay(50);
13357 
13358 	if (!i40e_enabled_xdp_vsi(vsi))
13359 		return ret;
13360 
13361 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13362 				     pf_q + vsi->alloc_queue_pairs,
13363 				     true /*is xdp*/, enable);
13364 	if (ret) {
13365 		dev_info(&pf->pdev->dev,
13366 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13367 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13368 	}
13369 
13370 	return ret;
13371 }
13372 
13373 /**
13374  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13375  * @vsi: vsi
13376  * @queue_pair: queue_pair
13377  **/
13378 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13379 {
13380 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13381 	struct i40e_pf *pf = vsi->back;
13382 	struct i40e_hw *hw = &pf->hw;
13383 
13384 	/* All rings in a qp belong to the same qvector. */
13385 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13386 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13387 	else
13388 		i40e_irq_dynamic_enable_icr0(pf);
13389 
13390 	i40e_flush(hw);
13391 }
13392 
13393 /**
13394  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13395  * @vsi: vsi
13396  * @queue_pair: queue_pair
13397  **/
13398 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13399 {
13400 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13401 	struct i40e_pf *pf = vsi->back;
13402 	struct i40e_hw *hw = &pf->hw;
13403 
13404 	/* For simplicity, instead of removing the qp interrupt causes
13405 	 * from the interrupt linked list, we simply disable the interrupt, and
13406 	 * leave the list intact.
13407 	 *
13408 	 * All rings in a qp belong to the same qvector.
13409 	 */
13410 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13411 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13412 
13413 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13414 		i40e_flush(hw);
13415 		synchronize_irq(pf->msix_entries[intpf].vector);
13416 	} else {
13417 		/* Legacy and MSI mode - this stops all interrupt handling */
13418 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13419 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13420 		i40e_flush(hw);
13421 		synchronize_irq(pf->pdev->irq);
13422 	}
13423 }
13424 
13425 /**
13426  * i40e_queue_pair_disable - Disables a queue pair
13427  * @vsi: vsi
13428  * @queue_pair: queue pair
13429  *
13430  * Returns 0 on success, <0 on failure.
13431  **/
13432 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13433 {
13434 	int err;
13435 
13436 	err = i40e_enter_busy_conf(vsi);
13437 	if (err)
13438 		return err;
13439 
13440 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13441 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13442 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13443 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13444 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13445 
13446 	return err;
13447 }
13448 
13449 /**
13450  * i40e_queue_pair_enable - Enables a queue pair
13451  * @vsi: vsi
13452  * @queue_pair: queue pair
13453  *
13454  * Returns 0 on success, <0 on failure.
13455  **/
13456 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13457 {
13458 	int err;
13459 
13460 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13461 	if (err)
13462 		return err;
13463 
13464 	if (i40e_enabled_xdp_vsi(vsi)) {
13465 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13466 		if (err)
13467 			return err;
13468 	}
13469 
13470 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13471 	if (err)
13472 		return err;
13473 
13474 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13475 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13476 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13477 
13478 	i40e_exit_busy_conf(vsi);
13479 
13480 	return err;
13481 }
13482 
13483 /**
13484  * i40e_xdp - implements ndo_bpf for i40e
13485  * @dev: netdevice
13486  * @xdp: XDP command
13487  **/
13488 static int i40e_xdp(struct net_device *dev,
13489 		    struct netdev_bpf *xdp)
13490 {
13491 	struct i40e_netdev_priv *np = netdev_priv(dev);
13492 	struct i40e_vsi *vsi = np->vsi;
13493 
13494 	if (vsi->type != I40E_VSI_MAIN)
13495 		return -EINVAL;
13496 
13497 	switch (xdp->command) {
13498 	case XDP_SETUP_PROG:
13499 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13500 	case XDP_SETUP_XSK_POOL:
13501 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13502 					   xdp->xsk.queue_id);
13503 	default:
13504 		return -EINVAL;
13505 	}
13506 }
13507 
13508 static const struct net_device_ops i40e_netdev_ops = {
13509 	.ndo_open		= i40e_open,
13510 	.ndo_stop		= i40e_close,
13511 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13512 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13513 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13514 	.ndo_validate_addr	= eth_validate_addr,
13515 	.ndo_set_mac_address	= i40e_set_mac,
13516 	.ndo_change_mtu		= i40e_change_mtu,
13517 	.ndo_eth_ioctl		= i40e_ioctl,
13518 	.ndo_tx_timeout		= i40e_tx_timeout,
13519 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13520 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13521 #ifdef CONFIG_NET_POLL_CONTROLLER
13522 	.ndo_poll_controller	= i40e_netpoll,
13523 #endif
13524 	.ndo_setup_tc		= __i40e_setup_tc,
13525 	.ndo_select_queue	= i40e_lan_select_queue,
13526 	.ndo_set_features	= i40e_set_features,
13527 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13528 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13529 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13530 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13531 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13532 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13533 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13534 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13535 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13536 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13537 	.ndo_features_check	= i40e_features_check,
13538 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13539 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13540 	.ndo_bpf		= i40e_xdp,
13541 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13542 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13543 	.ndo_dfwd_add_station	= i40e_fwd_add,
13544 	.ndo_dfwd_del_station	= i40e_fwd_del,
13545 };
13546 
13547 /**
13548  * i40e_config_netdev - Setup the netdev flags
13549  * @vsi: the VSI being configured
13550  *
13551  * Returns 0 on success, negative value on failure
13552  **/
13553 static int i40e_config_netdev(struct i40e_vsi *vsi)
13554 {
13555 	struct i40e_pf *pf = vsi->back;
13556 	struct i40e_hw *hw = &pf->hw;
13557 	struct i40e_netdev_priv *np;
13558 	struct net_device *netdev;
13559 	u8 broadcast[ETH_ALEN];
13560 	u8 mac_addr[ETH_ALEN];
13561 	int etherdev_size;
13562 	netdev_features_t hw_enc_features;
13563 	netdev_features_t hw_features;
13564 
13565 	etherdev_size = sizeof(struct i40e_netdev_priv);
13566 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13567 	if (!netdev)
13568 		return -ENOMEM;
13569 
13570 	vsi->netdev = netdev;
13571 	np = netdev_priv(netdev);
13572 	np->vsi = vsi;
13573 
13574 	hw_enc_features = NETIF_F_SG			|
13575 			  NETIF_F_HW_CSUM		|
13576 			  NETIF_F_HIGHDMA		|
13577 			  NETIF_F_SOFT_FEATURES		|
13578 			  NETIF_F_TSO			|
13579 			  NETIF_F_TSO_ECN		|
13580 			  NETIF_F_TSO6			|
13581 			  NETIF_F_GSO_GRE		|
13582 			  NETIF_F_GSO_GRE_CSUM		|
13583 			  NETIF_F_GSO_PARTIAL		|
13584 			  NETIF_F_GSO_IPXIP4		|
13585 			  NETIF_F_GSO_IPXIP6		|
13586 			  NETIF_F_GSO_UDP_TUNNEL	|
13587 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13588 			  NETIF_F_GSO_UDP_L4		|
13589 			  NETIF_F_SCTP_CRC		|
13590 			  NETIF_F_RXHASH		|
13591 			  NETIF_F_RXCSUM		|
13592 			  0;
13593 
13594 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13595 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13596 
13597 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13598 
13599 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13600 
13601 	netdev->hw_enc_features |= hw_enc_features;
13602 
13603 	/* record features VLANs can make use of */
13604 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13605 
13606 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13607 				   NETIF_F_GSO_GRE_CSUM |	\
13608 				   NETIF_F_GSO_IPXIP4 |		\
13609 				   NETIF_F_GSO_IPXIP6 |		\
13610 				   NETIF_F_GSO_UDP_TUNNEL |	\
13611 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13612 
13613 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13614 	netdev->features |= NETIF_F_GSO_PARTIAL |
13615 			    I40E_GSO_PARTIAL_FEATURES;
13616 
13617 	netdev->mpls_features |= NETIF_F_SG;
13618 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13619 	netdev->mpls_features |= NETIF_F_TSO;
13620 	netdev->mpls_features |= NETIF_F_TSO6;
13621 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13622 
13623 	/* enable macvlan offloads */
13624 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13625 
13626 	hw_features = hw_enc_features		|
13627 		      NETIF_F_HW_VLAN_CTAG_TX	|
13628 		      NETIF_F_HW_VLAN_CTAG_RX;
13629 
13630 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13631 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13632 
13633 	netdev->hw_features |= hw_features;
13634 
13635 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13636 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13637 
13638 	netdev->features &= ~NETIF_F_HW_TC;
13639 
13640 	if (vsi->type == I40E_VSI_MAIN) {
13641 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13642 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13643 		/* The following steps are necessary for two reasons. First,
13644 		 * some older NVM configurations load a default MAC-VLAN
13645 		 * filter that will accept any tagged packet, and we want to
13646 		 * replace this with a normal filter. Additionally, it is
13647 		 * possible our MAC address was provided by the platform using
13648 		 * Open Firmware or similar.
13649 		 *
13650 		 * Thus, we need to remove the default filter and install one
13651 		 * specific to the MAC address.
13652 		 */
13653 		i40e_rm_default_mac_filter(vsi, mac_addr);
13654 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13655 		i40e_add_mac_filter(vsi, mac_addr);
13656 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13657 	} else {
13658 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13659 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13660 		 * the end, which is 4 bytes long, so force truncation of the
13661 		 * original name by IFNAMSIZ - 4
13662 		 */
13663 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13664 			 IFNAMSIZ - 4,
13665 			 pf->vsi[pf->lan_vsi]->netdev->name);
13666 		eth_random_addr(mac_addr);
13667 
13668 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13669 		i40e_add_mac_filter(vsi, mac_addr);
13670 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13671 	}
13672 
13673 	/* Add the broadcast filter so that we initially will receive
13674 	 * broadcast packets. Note that when a new VLAN is first added the
13675 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13676 	 * specific filters as part of transitioning into "vlan" operation.
13677 	 * When more VLANs are added, the driver will copy each existing MAC
13678 	 * filter and add it for the new VLAN.
13679 	 *
13680 	 * Broadcast filters are handled specially by
13681 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13682 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13683 	 * filter. The subtask will update the correct broadcast promiscuous
13684 	 * bits as VLANs become active or inactive.
13685 	 */
13686 	eth_broadcast_addr(broadcast);
13687 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13688 	i40e_add_mac_filter(vsi, broadcast);
13689 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13690 
13691 	eth_hw_addr_set(netdev, mac_addr);
13692 	ether_addr_copy(netdev->perm_addr, mac_addr);
13693 
13694 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13695 	netdev->neigh_priv_len = sizeof(u32) * 4;
13696 
13697 	netdev->priv_flags |= IFF_UNICAST_FLT;
13698 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13699 	/* Setup netdev TC information */
13700 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13701 
13702 	netdev->netdev_ops = &i40e_netdev_ops;
13703 	netdev->watchdog_timeo = 5 * HZ;
13704 	i40e_set_ethtool_ops(netdev);
13705 
13706 	/* MTU range: 68 - 9706 */
13707 	netdev->min_mtu = ETH_MIN_MTU;
13708 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13709 
13710 	return 0;
13711 }
13712 
13713 /**
13714  * i40e_vsi_delete - Delete a VSI from the switch
13715  * @vsi: the VSI being removed
13716  *
13717  * Returns 0 on success, negative value on failure
13718  **/
13719 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13720 {
13721 	/* remove default VSI is not allowed */
13722 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13723 		return;
13724 
13725 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13726 }
13727 
13728 /**
13729  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13730  * @vsi: the VSI being queried
13731  *
13732  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13733  **/
13734 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13735 {
13736 	struct i40e_veb *veb;
13737 	struct i40e_pf *pf = vsi->back;
13738 
13739 	/* Uplink is not a bridge so default to VEB */
13740 	if (vsi->veb_idx >= I40E_MAX_VEB)
13741 		return 1;
13742 
13743 	veb = pf->veb[vsi->veb_idx];
13744 	if (!veb) {
13745 		dev_info(&pf->pdev->dev,
13746 			 "There is no veb associated with the bridge\n");
13747 		return -ENOENT;
13748 	}
13749 
13750 	/* Uplink is a bridge in VEPA mode */
13751 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13752 		return 0;
13753 	} else {
13754 		/* Uplink is a bridge in VEB mode */
13755 		return 1;
13756 	}
13757 
13758 	/* VEPA is now default bridge, so return 0 */
13759 	return 0;
13760 }
13761 
13762 /**
13763  * i40e_add_vsi - Add a VSI to the switch
13764  * @vsi: the VSI being configured
13765  *
13766  * This initializes a VSI context depending on the VSI type to be added and
13767  * passes it down to the add_vsi aq command.
13768  **/
13769 static int i40e_add_vsi(struct i40e_vsi *vsi)
13770 {
13771 	int ret = -ENODEV;
13772 	struct i40e_pf *pf = vsi->back;
13773 	struct i40e_hw *hw = &pf->hw;
13774 	struct i40e_vsi_context ctxt;
13775 	struct i40e_mac_filter *f;
13776 	struct hlist_node *h;
13777 	int bkt;
13778 
13779 	u8 enabled_tc = 0x1; /* TC0 enabled */
13780 	int f_count = 0;
13781 
13782 	memset(&ctxt, 0, sizeof(ctxt));
13783 	switch (vsi->type) {
13784 	case I40E_VSI_MAIN:
13785 		/* The PF's main VSI is already setup as part of the
13786 		 * device initialization, so we'll not bother with
13787 		 * the add_vsi call, but we will retrieve the current
13788 		 * VSI context.
13789 		 */
13790 		ctxt.seid = pf->main_vsi_seid;
13791 		ctxt.pf_num = pf->hw.pf_id;
13792 		ctxt.vf_num = 0;
13793 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13794 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13795 		if (ret) {
13796 			dev_info(&pf->pdev->dev,
13797 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13798 				 i40e_stat_str(&pf->hw, ret),
13799 				 i40e_aq_str(&pf->hw,
13800 					     pf->hw.aq.asq_last_status));
13801 			return -ENOENT;
13802 		}
13803 		vsi->info = ctxt.info;
13804 		vsi->info.valid_sections = 0;
13805 
13806 		vsi->seid = ctxt.seid;
13807 		vsi->id = ctxt.vsi_number;
13808 
13809 		enabled_tc = i40e_pf_get_tc_map(pf);
13810 
13811 		/* Source pruning is enabled by default, so the flag is
13812 		 * negative logic - if it's set, we need to fiddle with
13813 		 * the VSI to disable source pruning.
13814 		 */
13815 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13816 			memset(&ctxt, 0, sizeof(ctxt));
13817 			ctxt.seid = pf->main_vsi_seid;
13818 			ctxt.pf_num = pf->hw.pf_id;
13819 			ctxt.vf_num = 0;
13820 			ctxt.info.valid_sections |=
13821 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13822 			ctxt.info.switch_id =
13823 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13824 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13825 			if (ret) {
13826 				dev_info(&pf->pdev->dev,
13827 					 "update vsi failed, err %s aq_err %s\n",
13828 					 i40e_stat_str(&pf->hw, ret),
13829 					 i40e_aq_str(&pf->hw,
13830 						     pf->hw.aq.asq_last_status));
13831 				ret = -ENOENT;
13832 				goto err;
13833 			}
13834 		}
13835 
13836 		/* MFP mode setup queue map and update VSI */
13837 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13838 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13839 			memset(&ctxt, 0, sizeof(ctxt));
13840 			ctxt.seid = pf->main_vsi_seid;
13841 			ctxt.pf_num = pf->hw.pf_id;
13842 			ctxt.vf_num = 0;
13843 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13844 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13845 			if (ret) {
13846 				dev_info(&pf->pdev->dev,
13847 					 "update vsi failed, err %s aq_err %s\n",
13848 					 i40e_stat_str(&pf->hw, ret),
13849 					 i40e_aq_str(&pf->hw,
13850 						    pf->hw.aq.asq_last_status));
13851 				ret = -ENOENT;
13852 				goto err;
13853 			}
13854 			/* update the local VSI info queue map */
13855 			i40e_vsi_update_queue_map(vsi, &ctxt);
13856 			vsi->info.valid_sections = 0;
13857 		} else {
13858 			/* Default/Main VSI is only enabled for TC0
13859 			 * reconfigure it to enable all TCs that are
13860 			 * available on the port in SFP mode.
13861 			 * For MFP case the iSCSI PF would use this
13862 			 * flow to enable LAN+iSCSI TC.
13863 			 */
13864 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13865 			if (ret) {
13866 				/* Single TC condition is not fatal,
13867 				 * message and continue
13868 				 */
13869 				dev_info(&pf->pdev->dev,
13870 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13871 					 enabled_tc,
13872 					 i40e_stat_str(&pf->hw, ret),
13873 					 i40e_aq_str(&pf->hw,
13874 						    pf->hw.aq.asq_last_status));
13875 			}
13876 		}
13877 		break;
13878 
13879 	case I40E_VSI_FDIR:
13880 		ctxt.pf_num = hw->pf_id;
13881 		ctxt.vf_num = 0;
13882 		ctxt.uplink_seid = vsi->uplink_seid;
13883 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13884 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13885 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13886 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13887 			ctxt.info.valid_sections |=
13888 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13889 			ctxt.info.switch_id =
13890 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13891 		}
13892 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13893 		break;
13894 
13895 	case I40E_VSI_VMDQ2:
13896 		ctxt.pf_num = hw->pf_id;
13897 		ctxt.vf_num = 0;
13898 		ctxt.uplink_seid = vsi->uplink_seid;
13899 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13900 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13901 
13902 		/* This VSI is connected to VEB so the switch_id
13903 		 * should be set to zero by default.
13904 		 */
13905 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13906 			ctxt.info.valid_sections |=
13907 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13908 			ctxt.info.switch_id =
13909 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13910 		}
13911 
13912 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13913 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13914 		break;
13915 
13916 	case I40E_VSI_SRIOV:
13917 		ctxt.pf_num = hw->pf_id;
13918 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13919 		ctxt.uplink_seid = vsi->uplink_seid;
13920 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13921 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13922 
13923 		/* This VSI is connected to VEB so the switch_id
13924 		 * should be set to zero by default.
13925 		 */
13926 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13927 			ctxt.info.valid_sections |=
13928 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13929 			ctxt.info.switch_id =
13930 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13931 		}
13932 
13933 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
13934 			ctxt.info.valid_sections |=
13935 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
13936 			ctxt.info.queueing_opt_flags |=
13937 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
13938 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
13939 		}
13940 
13941 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
13942 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
13943 		if (pf->vf[vsi->vf_id].spoofchk) {
13944 			ctxt.info.valid_sections |=
13945 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
13946 			ctxt.info.sec_flags |=
13947 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
13948 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
13949 		}
13950 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13951 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13952 		break;
13953 
13954 	case I40E_VSI_IWARP:
13955 		/* send down message to iWARP */
13956 		break;
13957 
13958 	default:
13959 		return -ENODEV;
13960 	}
13961 
13962 	if (vsi->type != I40E_VSI_MAIN) {
13963 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
13964 		if (ret) {
13965 			dev_info(&vsi->back->pdev->dev,
13966 				 "add vsi failed, err %s aq_err %s\n",
13967 				 i40e_stat_str(&pf->hw, ret),
13968 				 i40e_aq_str(&pf->hw,
13969 					     pf->hw.aq.asq_last_status));
13970 			ret = -ENOENT;
13971 			goto err;
13972 		}
13973 		vsi->info = ctxt.info;
13974 		vsi->info.valid_sections = 0;
13975 		vsi->seid = ctxt.seid;
13976 		vsi->id = ctxt.vsi_number;
13977 	}
13978 
13979 	vsi->active_filters = 0;
13980 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
13981 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13982 	/* If macvlan filters already exist, force them to get loaded */
13983 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
13984 		f->state = I40E_FILTER_NEW;
13985 		f_count++;
13986 	}
13987 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13988 
13989 	if (f_count) {
13990 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
13991 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
13992 	}
13993 
13994 	/* Update VSI BW information */
13995 	ret = i40e_vsi_get_bw_info(vsi);
13996 	if (ret) {
13997 		dev_info(&pf->pdev->dev,
13998 			 "couldn't get vsi bw info, err %s aq_err %s\n",
13999 			 i40e_stat_str(&pf->hw, ret),
14000 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14001 		/* VSI is already added so not tearing that up */
14002 		ret = 0;
14003 	}
14004 
14005 err:
14006 	return ret;
14007 }
14008 
14009 /**
14010  * i40e_vsi_release - Delete a VSI and free its resources
14011  * @vsi: the VSI being removed
14012  *
14013  * Returns 0 on success or < 0 on error
14014  **/
14015 int i40e_vsi_release(struct i40e_vsi *vsi)
14016 {
14017 	struct i40e_mac_filter *f;
14018 	struct hlist_node *h;
14019 	struct i40e_veb *veb = NULL;
14020 	struct i40e_pf *pf;
14021 	u16 uplink_seid;
14022 	int i, n, bkt;
14023 
14024 	pf = vsi->back;
14025 
14026 	/* release of a VEB-owner or last VSI is not allowed */
14027 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14028 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14029 			 vsi->seid, vsi->uplink_seid);
14030 		return -ENODEV;
14031 	}
14032 	if (vsi == pf->vsi[pf->lan_vsi] &&
14033 	    !test_bit(__I40E_DOWN, pf->state)) {
14034 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14035 		return -ENODEV;
14036 	}
14037 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14038 	uplink_seid = vsi->uplink_seid;
14039 	if (vsi->type != I40E_VSI_SRIOV) {
14040 		if (vsi->netdev_registered) {
14041 			vsi->netdev_registered = false;
14042 			if (vsi->netdev) {
14043 				/* results in a call to i40e_close() */
14044 				unregister_netdev(vsi->netdev);
14045 			}
14046 		} else {
14047 			i40e_vsi_close(vsi);
14048 		}
14049 		i40e_vsi_disable_irq(vsi);
14050 	}
14051 
14052 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14053 
14054 	/* clear the sync flag on all filters */
14055 	if (vsi->netdev) {
14056 		__dev_uc_unsync(vsi->netdev, NULL);
14057 		__dev_mc_unsync(vsi->netdev, NULL);
14058 	}
14059 
14060 	/* make sure any remaining filters are marked for deletion */
14061 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14062 		__i40e_del_filter(vsi, f);
14063 
14064 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14065 
14066 	i40e_sync_vsi_filters(vsi);
14067 
14068 	i40e_vsi_delete(vsi);
14069 	i40e_vsi_free_q_vectors(vsi);
14070 	if (vsi->netdev) {
14071 		free_netdev(vsi->netdev);
14072 		vsi->netdev = NULL;
14073 	}
14074 	i40e_vsi_clear_rings(vsi);
14075 	i40e_vsi_clear(vsi);
14076 
14077 	/* If this was the last thing on the VEB, except for the
14078 	 * controlling VSI, remove the VEB, which puts the controlling
14079 	 * VSI onto the next level down in the switch.
14080 	 *
14081 	 * Well, okay, there's one more exception here: don't remove
14082 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
14083 	 * from up the network stack.
14084 	 */
14085 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
14086 		if (pf->vsi[i] &&
14087 		    pf->vsi[i]->uplink_seid == uplink_seid &&
14088 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14089 			n++;      /* count the VSIs */
14090 		}
14091 	}
14092 	for (i = 0; i < I40E_MAX_VEB; i++) {
14093 		if (!pf->veb[i])
14094 			continue;
14095 		if (pf->veb[i]->uplink_seid == uplink_seid)
14096 			n++;     /* count the VEBs */
14097 		if (pf->veb[i]->seid == uplink_seid)
14098 			veb = pf->veb[i];
14099 	}
14100 	if (n == 0 && veb && veb->uplink_seid != 0)
14101 		i40e_veb_release(veb);
14102 
14103 	return 0;
14104 }
14105 
14106 /**
14107  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14108  * @vsi: ptr to the VSI
14109  *
14110  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14111  * corresponding SW VSI structure and initializes num_queue_pairs for the
14112  * newly allocated VSI.
14113  *
14114  * Returns 0 on success or negative on failure
14115  **/
14116 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14117 {
14118 	int ret = -ENOENT;
14119 	struct i40e_pf *pf = vsi->back;
14120 
14121 	if (vsi->q_vectors[0]) {
14122 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14123 			 vsi->seid);
14124 		return -EEXIST;
14125 	}
14126 
14127 	if (vsi->base_vector) {
14128 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14129 			 vsi->seid, vsi->base_vector);
14130 		return -EEXIST;
14131 	}
14132 
14133 	ret = i40e_vsi_alloc_q_vectors(vsi);
14134 	if (ret) {
14135 		dev_info(&pf->pdev->dev,
14136 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14137 			 vsi->num_q_vectors, vsi->seid, ret);
14138 		vsi->num_q_vectors = 0;
14139 		goto vector_setup_out;
14140 	}
14141 
14142 	/* In Legacy mode, we do not have to get any other vector since we
14143 	 * piggyback on the misc/ICR0 for queue interrupts.
14144 	*/
14145 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
14146 		return ret;
14147 	if (vsi->num_q_vectors)
14148 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14149 						 vsi->num_q_vectors, vsi->idx);
14150 	if (vsi->base_vector < 0) {
14151 		dev_info(&pf->pdev->dev,
14152 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14153 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14154 		i40e_vsi_free_q_vectors(vsi);
14155 		ret = -ENOENT;
14156 		goto vector_setup_out;
14157 	}
14158 
14159 vector_setup_out:
14160 	return ret;
14161 }
14162 
14163 /**
14164  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14165  * @vsi: pointer to the vsi.
14166  *
14167  * This re-allocates a vsi's queue resources.
14168  *
14169  * Returns pointer to the successfully allocated and configured VSI sw struct
14170  * on success, otherwise returns NULL on failure.
14171  **/
14172 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14173 {
14174 	u16 alloc_queue_pairs;
14175 	struct i40e_pf *pf;
14176 	u8 enabled_tc;
14177 	int ret;
14178 
14179 	if (!vsi)
14180 		return NULL;
14181 
14182 	pf = vsi->back;
14183 
14184 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14185 	i40e_vsi_clear_rings(vsi);
14186 
14187 	i40e_vsi_free_arrays(vsi, false);
14188 	i40e_set_num_rings_in_vsi(vsi);
14189 	ret = i40e_vsi_alloc_arrays(vsi, false);
14190 	if (ret)
14191 		goto err_vsi;
14192 
14193 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14194 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14195 
14196 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14197 	if (ret < 0) {
14198 		dev_info(&pf->pdev->dev,
14199 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14200 			 alloc_queue_pairs, vsi->seid, ret);
14201 		goto err_vsi;
14202 	}
14203 	vsi->base_queue = ret;
14204 
14205 	/* Update the FW view of the VSI. Force a reset of TC and queue
14206 	 * layout configurations.
14207 	 */
14208 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14209 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14210 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14211 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14212 	if (vsi->type == I40E_VSI_MAIN)
14213 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14214 
14215 	/* assign it some queues */
14216 	ret = i40e_alloc_rings(vsi);
14217 	if (ret)
14218 		goto err_rings;
14219 
14220 	/* map all of the rings to the q_vectors */
14221 	i40e_vsi_map_rings_to_vectors(vsi);
14222 	return vsi;
14223 
14224 err_rings:
14225 	i40e_vsi_free_q_vectors(vsi);
14226 	if (vsi->netdev_registered) {
14227 		vsi->netdev_registered = false;
14228 		unregister_netdev(vsi->netdev);
14229 		free_netdev(vsi->netdev);
14230 		vsi->netdev = NULL;
14231 	}
14232 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14233 err_vsi:
14234 	i40e_vsi_clear(vsi);
14235 	return NULL;
14236 }
14237 
14238 /**
14239  * i40e_vsi_setup - Set up a VSI by a given type
14240  * @pf: board private structure
14241  * @type: VSI type
14242  * @uplink_seid: the switch element to link to
14243  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14244  *
14245  * This allocates the sw VSI structure and its queue resources, then add a VSI
14246  * to the identified VEB.
14247  *
14248  * Returns pointer to the successfully allocated and configure VSI sw struct on
14249  * success, otherwise returns NULL on failure.
14250  **/
14251 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14252 				u16 uplink_seid, u32 param1)
14253 {
14254 	struct i40e_vsi *vsi = NULL;
14255 	struct i40e_veb *veb = NULL;
14256 	u16 alloc_queue_pairs;
14257 	int ret, i;
14258 	int v_idx;
14259 
14260 	/* The requested uplink_seid must be either
14261 	 *     - the PF's port seid
14262 	 *              no VEB is needed because this is the PF
14263 	 *              or this is a Flow Director special case VSI
14264 	 *     - seid of an existing VEB
14265 	 *     - seid of a VSI that owns an existing VEB
14266 	 *     - seid of a VSI that doesn't own a VEB
14267 	 *              a new VEB is created and the VSI becomes the owner
14268 	 *     - seid of the PF VSI, which is what creates the first VEB
14269 	 *              this is a special case of the previous
14270 	 *
14271 	 * Find which uplink_seid we were given and create a new VEB if needed
14272 	 */
14273 	for (i = 0; i < I40E_MAX_VEB; i++) {
14274 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14275 			veb = pf->veb[i];
14276 			break;
14277 		}
14278 	}
14279 
14280 	if (!veb && uplink_seid != pf->mac_seid) {
14281 
14282 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14283 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14284 				vsi = pf->vsi[i];
14285 				break;
14286 			}
14287 		}
14288 		if (!vsi) {
14289 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14290 				 uplink_seid);
14291 			return NULL;
14292 		}
14293 
14294 		if (vsi->uplink_seid == pf->mac_seid)
14295 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14296 					     vsi->tc_config.enabled_tc);
14297 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14298 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14299 					     vsi->tc_config.enabled_tc);
14300 		if (veb) {
14301 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14302 				dev_info(&vsi->back->pdev->dev,
14303 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14304 				return NULL;
14305 			}
14306 			/* We come up by default in VEPA mode if SRIOV is not
14307 			 * already enabled, in which case we can't force VEPA
14308 			 * mode.
14309 			 */
14310 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14311 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14312 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14313 			}
14314 			i40e_config_bridge_mode(veb);
14315 		}
14316 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14317 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14318 				veb = pf->veb[i];
14319 		}
14320 		if (!veb) {
14321 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14322 			return NULL;
14323 		}
14324 
14325 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14326 		uplink_seid = veb->seid;
14327 	}
14328 
14329 	/* get vsi sw struct */
14330 	v_idx = i40e_vsi_mem_alloc(pf, type);
14331 	if (v_idx < 0)
14332 		goto err_alloc;
14333 	vsi = pf->vsi[v_idx];
14334 	if (!vsi)
14335 		goto err_alloc;
14336 	vsi->type = type;
14337 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14338 
14339 	if (type == I40E_VSI_MAIN)
14340 		pf->lan_vsi = v_idx;
14341 	else if (type == I40E_VSI_SRIOV)
14342 		vsi->vf_id = param1;
14343 	/* assign it some queues */
14344 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14345 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14346 
14347 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14348 	if (ret < 0) {
14349 		dev_info(&pf->pdev->dev,
14350 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14351 			 alloc_queue_pairs, vsi->seid, ret);
14352 		goto err_vsi;
14353 	}
14354 	vsi->base_queue = ret;
14355 
14356 	/* get a VSI from the hardware */
14357 	vsi->uplink_seid = uplink_seid;
14358 	ret = i40e_add_vsi(vsi);
14359 	if (ret)
14360 		goto err_vsi;
14361 
14362 	switch (vsi->type) {
14363 	/* setup the netdev if needed */
14364 	case I40E_VSI_MAIN:
14365 	case I40E_VSI_VMDQ2:
14366 		ret = i40e_config_netdev(vsi);
14367 		if (ret)
14368 			goto err_netdev;
14369 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14370 		if (ret)
14371 			goto err_netdev;
14372 		ret = register_netdev(vsi->netdev);
14373 		if (ret)
14374 			goto err_netdev;
14375 		vsi->netdev_registered = true;
14376 		netif_carrier_off(vsi->netdev);
14377 #ifdef CONFIG_I40E_DCB
14378 		/* Setup DCB netlink interface */
14379 		i40e_dcbnl_setup(vsi);
14380 #endif /* CONFIG_I40E_DCB */
14381 		fallthrough;
14382 	case I40E_VSI_FDIR:
14383 		/* set up vectors and rings if needed */
14384 		ret = i40e_vsi_setup_vectors(vsi);
14385 		if (ret)
14386 			goto err_msix;
14387 
14388 		ret = i40e_alloc_rings(vsi);
14389 		if (ret)
14390 			goto err_rings;
14391 
14392 		/* map all of the rings to the q_vectors */
14393 		i40e_vsi_map_rings_to_vectors(vsi);
14394 
14395 		i40e_vsi_reset_stats(vsi);
14396 		break;
14397 	default:
14398 		/* no netdev or rings for the other VSI types */
14399 		break;
14400 	}
14401 
14402 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14403 	    (vsi->type == I40E_VSI_VMDQ2)) {
14404 		ret = i40e_vsi_config_rss(vsi);
14405 	}
14406 	return vsi;
14407 
14408 err_rings:
14409 	i40e_vsi_free_q_vectors(vsi);
14410 err_msix:
14411 	if (vsi->netdev_registered) {
14412 		vsi->netdev_registered = false;
14413 		unregister_netdev(vsi->netdev);
14414 		free_netdev(vsi->netdev);
14415 		vsi->netdev = NULL;
14416 	}
14417 err_netdev:
14418 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14419 err_vsi:
14420 	i40e_vsi_clear(vsi);
14421 err_alloc:
14422 	return NULL;
14423 }
14424 
14425 /**
14426  * i40e_veb_get_bw_info - Query VEB BW information
14427  * @veb: the veb to query
14428  *
14429  * Query the Tx scheduler BW configuration data for given VEB
14430  **/
14431 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14432 {
14433 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14434 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14435 	struct i40e_pf *pf = veb->pf;
14436 	struct i40e_hw *hw = &pf->hw;
14437 	u32 tc_bw_max;
14438 	int ret = 0;
14439 	int i;
14440 
14441 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14442 						  &bw_data, NULL);
14443 	if (ret) {
14444 		dev_info(&pf->pdev->dev,
14445 			 "query veb bw config failed, err %s aq_err %s\n",
14446 			 i40e_stat_str(&pf->hw, ret),
14447 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14448 		goto out;
14449 	}
14450 
14451 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14452 						   &ets_data, NULL);
14453 	if (ret) {
14454 		dev_info(&pf->pdev->dev,
14455 			 "query veb bw ets config failed, err %s aq_err %s\n",
14456 			 i40e_stat_str(&pf->hw, ret),
14457 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14458 		goto out;
14459 	}
14460 
14461 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14462 	veb->bw_max_quanta = ets_data.tc_bw_max;
14463 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14464 	veb->enabled_tc = ets_data.tc_valid_bits;
14465 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14466 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14467 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14468 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14469 		veb->bw_tc_limit_credits[i] =
14470 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14471 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14472 	}
14473 
14474 out:
14475 	return ret;
14476 }
14477 
14478 /**
14479  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14480  * @pf: board private structure
14481  *
14482  * On error: returns error code (negative)
14483  * On success: returns vsi index in PF (positive)
14484  **/
14485 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14486 {
14487 	int ret = -ENOENT;
14488 	struct i40e_veb *veb;
14489 	int i;
14490 
14491 	/* Need to protect the allocation of switch elements at the PF level */
14492 	mutex_lock(&pf->switch_mutex);
14493 
14494 	/* VEB list may be fragmented if VEB creation/destruction has
14495 	 * been happening.  We can afford to do a quick scan to look
14496 	 * for any free slots in the list.
14497 	 *
14498 	 * find next empty veb slot, looping back around if necessary
14499 	 */
14500 	i = 0;
14501 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14502 		i++;
14503 	if (i >= I40E_MAX_VEB) {
14504 		ret = -ENOMEM;
14505 		goto err_alloc_veb;  /* out of VEB slots! */
14506 	}
14507 
14508 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14509 	if (!veb) {
14510 		ret = -ENOMEM;
14511 		goto err_alloc_veb;
14512 	}
14513 	veb->pf = pf;
14514 	veb->idx = i;
14515 	veb->enabled_tc = 1;
14516 
14517 	pf->veb[i] = veb;
14518 	ret = i;
14519 err_alloc_veb:
14520 	mutex_unlock(&pf->switch_mutex);
14521 	return ret;
14522 }
14523 
14524 /**
14525  * i40e_switch_branch_release - Delete a branch of the switch tree
14526  * @branch: where to start deleting
14527  *
14528  * This uses recursion to find the tips of the branch to be
14529  * removed, deleting until we get back to and can delete this VEB.
14530  **/
14531 static void i40e_switch_branch_release(struct i40e_veb *branch)
14532 {
14533 	struct i40e_pf *pf = branch->pf;
14534 	u16 branch_seid = branch->seid;
14535 	u16 veb_idx = branch->idx;
14536 	int i;
14537 
14538 	/* release any VEBs on this VEB - RECURSION */
14539 	for (i = 0; i < I40E_MAX_VEB; i++) {
14540 		if (!pf->veb[i])
14541 			continue;
14542 		if (pf->veb[i]->uplink_seid == branch->seid)
14543 			i40e_switch_branch_release(pf->veb[i]);
14544 	}
14545 
14546 	/* Release the VSIs on this VEB, but not the owner VSI.
14547 	 *
14548 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14549 	 *       the VEB itself, so don't use (*branch) after this loop.
14550 	 */
14551 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14552 		if (!pf->vsi[i])
14553 			continue;
14554 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14555 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14556 			i40e_vsi_release(pf->vsi[i]);
14557 		}
14558 	}
14559 
14560 	/* There's one corner case where the VEB might not have been
14561 	 * removed, so double check it here and remove it if needed.
14562 	 * This case happens if the veb was created from the debugfs
14563 	 * commands and no VSIs were added to it.
14564 	 */
14565 	if (pf->veb[veb_idx])
14566 		i40e_veb_release(pf->veb[veb_idx]);
14567 }
14568 
14569 /**
14570  * i40e_veb_clear - remove veb struct
14571  * @veb: the veb to remove
14572  **/
14573 static void i40e_veb_clear(struct i40e_veb *veb)
14574 {
14575 	if (!veb)
14576 		return;
14577 
14578 	if (veb->pf) {
14579 		struct i40e_pf *pf = veb->pf;
14580 
14581 		mutex_lock(&pf->switch_mutex);
14582 		if (pf->veb[veb->idx] == veb)
14583 			pf->veb[veb->idx] = NULL;
14584 		mutex_unlock(&pf->switch_mutex);
14585 	}
14586 
14587 	kfree(veb);
14588 }
14589 
14590 /**
14591  * i40e_veb_release - Delete a VEB and free its resources
14592  * @veb: the VEB being removed
14593  **/
14594 void i40e_veb_release(struct i40e_veb *veb)
14595 {
14596 	struct i40e_vsi *vsi = NULL;
14597 	struct i40e_pf *pf;
14598 	int i, n = 0;
14599 
14600 	pf = veb->pf;
14601 
14602 	/* find the remaining VSI and check for extras */
14603 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14604 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14605 			n++;
14606 			vsi = pf->vsi[i];
14607 		}
14608 	}
14609 	if (n != 1) {
14610 		dev_info(&pf->pdev->dev,
14611 			 "can't remove VEB %d with %d VSIs left\n",
14612 			 veb->seid, n);
14613 		return;
14614 	}
14615 
14616 	/* move the remaining VSI to uplink veb */
14617 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14618 	if (veb->uplink_seid) {
14619 		vsi->uplink_seid = veb->uplink_seid;
14620 		if (veb->uplink_seid == pf->mac_seid)
14621 			vsi->veb_idx = I40E_NO_VEB;
14622 		else
14623 			vsi->veb_idx = veb->veb_idx;
14624 	} else {
14625 		/* floating VEB */
14626 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14627 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14628 	}
14629 
14630 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14631 	i40e_veb_clear(veb);
14632 }
14633 
14634 /**
14635  * i40e_add_veb - create the VEB in the switch
14636  * @veb: the VEB to be instantiated
14637  * @vsi: the controlling VSI
14638  **/
14639 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14640 {
14641 	struct i40e_pf *pf = veb->pf;
14642 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14643 	int ret;
14644 
14645 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14646 			      veb->enabled_tc, false,
14647 			      &veb->seid, enable_stats, NULL);
14648 
14649 	/* get a VEB from the hardware */
14650 	if (ret) {
14651 		dev_info(&pf->pdev->dev,
14652 			 "couldn't add VEB, err %s aq_err %s\n",
14653 			 i40e_stat_str(&pf->hw, ret),
14654 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14655 		return -EPERM;
14656 	}
14657 
14658 	/* get statistics counter */
14659 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14660 					 &veb->stats_idx, NULL, NULL, NULL);
14661 	if (ret) {
14662 		dev_info(&pf->pdev->dev,
14663 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
14664 			 i40e_stat_str(&pf->hw, ret),
14665 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14666 		return -EPERM;
14667 	}
14668 	ret = i40e_veb_get_bw_info(veb);
14669 	if (ret) {
14670 		dev_info(&pf->pdev->dev,
14671 			 "couldn't get VEB bw info, err %s aq_err %s\n",
14672 			 i40e_stat_str(&pf->hw, ret),
14673 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14674 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14675 		return -ENOENT;
14676 	}
14677 
14678 	vsi->uplink_seid = veb->seid;
14679 	vsi->veb_idx = veb->idx;
14680 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14681 
14682 	return 0;
14683 }
14684 
14685 /**
14686  * i40e_veb_setup - Set up a VEB
14687  * @pf: board private structure
14688  * @flags: VEB setup flags
14689  * @uplink_seid: the switch element to link to
14690  * @vsi_seid: the initial VSI seid
14691  * @enabled_tc: Enabled TC bit-map
14692  *
14693  * This allocates the sw VEB structure and links it into the switch
14694  * It is possible and legal for this to be a duplicate of an already
14695  * existing VEB.  It is also possible for both uplink and vsi seids
14696  * to be zero, in order to create a floating VEB.
14697  *
14698  * Returns pointer to the successfully allocated VEB sw struct on
14699  * success, otherwise returns NULL on failure.
14700  **/
14701 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14702 				u16 uplink_seid, u16 vsi_seid,
14703 				u8 enabled_tc)
14704 {
14705 	struct i40e_veb *veb, *uplink_veb = NULL;
14706 	int vsi_idx, veb_idx;
14707 	int ret;
14708 
14709 	/* if one seid is 0, the other must be 0 to create a floating relay */
14710 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14711 	    (uplink_seid + vsi_seid != 0)) {
14712 		dev_info(&pf->pdev->dev,
14713 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14714 			 uplink_seid, vsi_seid);
14715 		return NULL;
14716 	}
14717 
14718 	/* make sure there is such a vsi and uplink */
14719 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14720 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14721 			break;
14722 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14723 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14724 			 vsi_seid);
14725 		return NULL;
14726 	}
14727 
14728 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14729 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14730 			if (pf->veb[veb_idx] &&
14731 			    pf->veb[veb_idx]->seid == uplink_seid) {
14732 				uplink_veb = pf->veb[veb_idx];
14733 				break;
14734 			}
14735 		}
14736 		if (!uplink_veb) {
14737 			dev_info(&pf->pdev->dev,
14738 				 "uplink seid %d not found\n", uplink_seid);
14739 			return NULL;
14740 		}
14741 	}
14742 
14743 	/* get veb sw struct */
14744 	veb_idx = i40e_veb_mem_alloc(pf);
14745 	if (veb_idx < 0)
14746 		goto err_alloc;
14747 	veb = pf->veb[veb_idx];
14748 	veb->flags = flags;
14749 	veb->uplink_seid = uplink_seid;
14750 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14751 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14752 
14753 	/* create the VEB in the switch */
14754 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14755 	if (ret)
14756 		goto err_veb;
14757 	if (vsi_idx == pf->lan_vsi)
14758 		pf->lan_veb = veb->idx;
14759 
14760 	return veb;
14761 
14762 err_veb:
14763 	i40e_veb_clear(veb);
14764 err_alloc:
14765 	return NULL;
14766 }
14767 
14768 /**
14769  * i40e_setup_pf_switch_element - set PF vars based on switch type
14770  * @pf: board private structure
14771  * @ele: element we are building info from
14772  * @num_reported: total number of elements
14773  * @printconfig: should we print the contents
14774  *
14775  * helper function to assist in extracting a few useful SEID values.
14776  **/
14777 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14778 				struct i40e_aqc_switch_config_element_resp *ele,
14779 				u16 num_reported, bool printconfig)
14780 {
14781 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14782 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14783 	u8 element_type = ele->element_type;
14784 	u16 seid = le16_to_cpu(ele->seid);
14785 
14786 	if (printconfig)
14787 		dev_info(&pf->pdev->dev,
14788 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14789 			 element_type, seid, uplink_seid, downlink_seid);
14790 
14791 	switch (element_type) {
14792 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14793 		pf->mac_seid = seid;
14794 		break;
14795 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14796 		/* Main VEB? */
14797 		if (uplink_seid != pf->mac_seid)
14798 			break;
14799 		if (pf->lan_veb >= I40E_MAX_VEB) {
14800 			int v;
14801 
14802 			/* find existing or else empty VEB */
14803 			for (v = 0; v < I40E_MAX_VEB; v++) {
14804 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14805 					pf->lan_veb = v;
14806 					break;
14807 				}
14808 			}
14809 			if (pf->lan_veb >= I40E_MAX_VEB) {
14810 				v = i40e_veb_mem_alloc(pf);
14811 				if (v < 0)
14812 					break;
14813 				pf->lan_veb = v;
14814 			}
14815 		}
14816 		if (pf->lan_veb >= I40E_MAX_VEB)
14817 			break;
14818 
14819 		pf->veb[pf->lan_veb]->seid = seid;
14820 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14821 		pf->veb[pf->lan_veb]->pf = pf;
14822 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14823 		break;
14824 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14825 		if (num_reported != 1)
14826 			break;
14827 		/* This is immediately after a reset so we can assume this is
14828 		 * the PF's VSI
14829 		 */
14830 		pf->mac_seid = uplink_seid;
14831 		pf->pf_seid = downlink_seid;
14832 		pf->main_vsi_seid = seid;
14833 		if (printconfig)
14834 			dev_info(&pf->pdev->dev,
14835 				 "pf_seid=%d main_vsi_seid=%d\n",
14836 				 pf->pf_seid, pf->main_vsi_seid);
14837 		break;
14838 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14839 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14840 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14841 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14842 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14843 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14844 		/* ignore these for now */
14845 		break;
14846 	default:
14847 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14848 			 element_type, seid);
14849 		break;
14850 	}
14851 }
14852 
14853 /**
14854  * i40e_fetch_switch_configuration - Get switch config from firmware
14855  * @pf: board private structure
14856  * @printconfig: should we print the contents
14857  *
14858  * Get the current switch configuration from the device and
14859  * extract a few useful SEID values.
14860  **/
14861 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14862 {
14863 	struct i40e_aqc_get_switch_config_resp *sw_config;
14864 	u16 next_seid = 0;
14865 	int ret = 0;
14866 	u8 *aq_buf;
14867 	int i;
14868 
14869 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14870 	if (!aq_buf)
14871 		return -ENOMEM;
14872 
14873 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14874 	do {
14875 		u16 num_reported, num_total;
14876 
14877 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14878 						I40E_AQ_LARGE_BUF,
14879 						&next_seid, NULL);
14880 		if (ret) {
14881 			dev_info(&pf->pdev->dev,
14882 				 "get switch config failed err %s aq_err %s\n",
14883 				 i40e_stat_str(&pf->hw, ret),
14884 				 i40e_aq_str(&pf->hw,
14885 					     pf->hw.aq.asq_last_status));
14886 			kfree(aq_buf);
14887 			return -ENOENT;
14888 		}
14889 
14890 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14891 		num_total = le16_to_cpu(sw_config->header.num_total);
14892 
14893 		if (printconfig)
14894 			dev_info(&pf->pdev->dev,
14895 				 "header: %d reported %d total\n",
14896 				 num_reported, num_total);
14897 
14898 		for (i = 0; i < num_reported; i++) {
14899 			struct i40e_aqc_switch_config_element_resp *ele =
14900 				&sw_config->element[i];
14901 
14902 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14903 						     printconfig);
14904 		}
14905 	} while (next_seid != 0);
14906 
14907 	kfree(aq_buf);
14908 	return ret;
14909 }
14910 
14911 /**
14912  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14913  * @pf: board private structure
14914  * @reinit: if the Main VSI needs to re-initialized.
14915  * @lock_acquired: indicates whether or not the lock has been acquired
14916  *
14917  * Returns 0 on success, negative value on failure
14918  **/
14919 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14920 {
14921 	u16 flags = 0;
14922 	int ret;
14923 
14924 	/* find out what's out there already */
14925 	ret = i40e_fetch_switch_configuration(pf, false);
14926 	if (ret) {
14927 		dev_info(&pf->pdev->dev,
14928 			 "couldn't fetch switch config, err %s aq_err %s\n",
14929 			 i40e_stat_str(&pf->hw, ret),
14930 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14931 		return ret;
14932 	}
14933 	i40e_pf_reset_stats(pf);
14934 
14935 	/* set the switch config bit for the whole device to
14936 	 * support limited promisc or true promisc
14937 	 * when user requests promisc. The default is limited
14938 	 * promisc.
14939 	*/
14940 
14941 	if ((pf->hw.pf_id == 0) &&
14942 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
14943 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14944 		pf->last_sw_conf_flags = flags;
14945 	}
14946 
14947 	if (pf->hw.pf_id == 0) {
14948 		u16 valid_flags;
14949 
14950 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14951 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
14952 						NULL);
14953 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
14954 			dev_info(&pf->pdev->dev,
14955 				 "couldn't set switch config bits, err %s aq_err %s\n",
14956 				 i40e_stat_str(&pf->hw, ret),
14957 				 i40e_aq_str(&pf->hw,
14958 					     pf->hw.aq.asq_last_status));
14959 			/* not a fatal problem, just keep going */
14960 		}
14961 		pf->last_sw_conf_valid_flags = valid_flags;
14962 	}
14963 
14964 	/* first time setup */
14965 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
14966 		struct i40e_vsi *vsi = NULL;
14967 		u16 uplink_seid;
14968 
14969 		/* Set up the PF VSI associated with the PF's main VSI
14970 		 * that is already in the HW switch
14971 		 */
14972 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
14973 			uplink_seid = pf->veb[pf->lan_veb]->seid;
14974 		else
14975 			uplink_seid = pf->mac_seid;
14976 		if (pf->lan_vsi == I40E_NO_VSI)
14977 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
14978 		else if (reinit)
14979 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
14980 		if (!vsi) {
14981 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
14982 			i40e_cloud_filter_exit(pf);
14983 			i40e_fdir_teardown(pf);
14984 			return -EAGAIN;
14985 		}
14986 	} else {
14987 		/* force a reset of TC and queue layout configurations */
14988 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14989 
14990 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14991 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14992 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14993 	}
14994 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
14995 
14996 	i40e_fdir_sb_setup(pf);
14997 
14998 	/* Setup static PF queue filter control settings */
14999 	ret = i40e_setup_pf_filter_control(pf);
15000 	if (ret) {
15001 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15002 			 ret);
15003 		/* Failure here should not stop continuing other steps */
15004 	}
15005 
15006 	/* enable RSS in the HW, even for only one queue, as the stack can use
15007 	 * the hash
15008 	 */
15009 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
15010 		i40e_pf_config_rss(pf);
15011 
15012 	/* fill in link information and enable LSE reporting */
15013 	i40e_link_event(pf);
15014 
15015 	/* Initialize user-specific link properties */
15016 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
15017 				  I40E_AQ_AN_COMPLETED) ? true : false);
15018 
15019 	i40e_ptp_init(pf);
15020 
15021 	if (!lock_acquired)
15022 		rtnl_lock();
15023 
15024 	/* repopulate tunnel port filters */
15025 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
15026 
15027 	if (!lock_acquired)
15028 		rtnl_unlock();
15029 
15030 	return ret;
15031 }
15032 
15033 /**
15034  * i40e_determine_queue_usage - Work out queue distribution
15035  * @pf: board private structure
15036  **/
15037 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15038 {
15039 	int queues_left;
15040 	int q_max;
15041 
15042 	pf->num_lan_qps = 0;
15043 
15044 	/* Find the max queues to be put into basic use.  We'll always be
15045 	 * using TC0, whether or not DCB is running, and TC0 will get the
15046 	 * big RSS set.
15047 	 */
15048 	queues_left = pf->hw.func_caps.num_tx_qp;
15049 
15050 	if ((queues_left == 1) ||
15051 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
15052 		/* one qp for PF, no queues for anything else */
15053 		queues_left = 0;
15054 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15055 
15056 		/* make sure all the fancies are disabled */
15057 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15058 			       I40E_FLAG_IWARP_ENABLED	|
15059 			       I40E_FLAG_FD_SB_ENABLED	|
15060 			       I40E_FLAG_FD_ATR_ENABLED	|
15061 			       I40E_FLAG_DCB_CAPABLE	|
15062 			       I40E_FLAG_DCB_ENABLED	|
15063 			       I40E_FLAG_SRIOV_ENABLED	|
15064 			       I40E_FLAG_VMDQ_ENABLED);
15065 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15066 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
15067 				  I40E_FLAG_FD_SB_ENABLED |
15068 				  I40E_FLAG_FD_ATR_ENABLED |
15069 				  I40E_FLAG_DCB_CAPABLE))) {
15070 		/* one qp for PF */
15071 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15072 		queues_left -= pf->num_lan_qps;
15073 
15074 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15075 			       I40E_FLAG_IWARP_ENABLED	|
15076 			       I40E_FLAG_FD_SB_ENABLED	|
15077 			       I40E_FLAG_FD_ATR_ENABLED	|
15078 			       I40E_FLAG_DCB_ENABLED	|
15079 			       I40E_FLAG_VMDQ_ENABLED);
15080 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15081 	} else {
15082 		/* Not enough queues for all TCs */
15083 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
15084 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
15085 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
15086 					I40E_FLAG_DCB_ENABLED);
15087 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15088 		}
15089 
15090 		/* limit lan qps to the smaller of qps, cpus or msix */
15091 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15092 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15093 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15094 		pf->num_lan_qps = q_max;
15095 
15096 		queues_left -= pf->num_lan_qps;
15097 	}
15098 
15099 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15100 		if (queues_left > 1) {
15101 			queues_left -= 1; /* save 1 queue for FD */
15102 		} else {
15103 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
15104 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15105 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15106 		}
15107 	}
15108 
15109 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15110 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15111 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15112 					(queues_left / pf->num_vf_qps));
15113 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15114 	}
15115 
15116 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
15117 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15118 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15119 					  (queues_left / pf->num_vmdq_qps));
15120 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15121 	}
15122 
15123 	pf->queues_left = queues_left;
15124 	dev_dbg(&pf->pdev->dev,
15125 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15126 		pf->hw.func_caps.num_tx_qp,
15127 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
15128 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15129 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15130 		queues_left);
15131 }
15132 
15133 /**
15134  * i40e_setup_pf_filter_control - Setup PF static filter control
15135  * @pf: PF to be setup
15136  *
15137  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15138  * settings. If PE/FCoE are enabled then it will also set the per PF
15139  * based filter sizes required for them. It also enables Flow director,
15140  * ethertype and macvlan type filter settings for the pf.
15141  *
15142  * Returns 0 on success, negative on failure
15143  **/
15144 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15145 {
15146 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15147 
15148 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15149 
15150 	/* Flow Director is enabled */
15151 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
15152 		settings->enable_fdir = true;
15153 
15154 	/* Ethtype and MACVLAN filters enabled for PF */
15155 	settings->enable_ethtype = true;
15156 	settings->enable_macvlan = true;
15157 
15158 	if (i40e_set_filter_control(&pf->hw, settings))
15159 		return -ENOENT;
15160 
15161 	return 0;
15162 }
15163 
15164 #define INFO_STRING_LEN 255
15165 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15166 static void i40e_print_features(struct i40e_pf *pf)
15167 {
15168 	struct i40e_hw *hw = &pf->hw;
15169 	char *buf;
15170 	int i;
15171 
15172 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15173 	if (!buf)
15174 		return;
15175 
15176 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15177 #ifdef CONFIG_PCI_IOV
15178 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15179 #endif
15180 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15181 		      pf->hw.func_caps.num_vsis,
15182 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15183 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15184 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15185 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15186 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15187 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15188 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15189 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15190 	}
15191 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15192 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15193 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15194 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15195 	if (pf->flags & I40E_FLAG_PTP)
15196 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15197 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15198 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15199 	else
15200 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15201 
15202 	dev_info(&pf->pdev->dev, "%s\n", buf);
15203 	kfree(buf);
15204 	WARN_ON(i > INFO_STRING_LEN);
15205 }
15206 
15207 /**
15208  * i40e_get_platform_mac_addr - get platform-specific MAC address
15209  * @pdev: PCI device information struct
15210  * @pf: board private structure
15211  *
15212  * Look up the MAC address for the device. First we'll try
15213  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15214  * specific fallback. Otherwise, we'll default to the stored value in
15215  * firmware.
15216  **/
15217 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15218 {
15219 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15220 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15221 }
15222 
15223 /**
15224  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15225  * @fec_cfg: FEC option to set in flags
15226  * @flags: ptr to flags in which we set FEC option
15227  **/
15228 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15229 {
15230 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15231 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15232 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15233 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15234 		*flags |= I40E_FLAG_RS_FEC;
15235 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15236 	}
15237 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15238 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15239 		*flags |= I40E_FLAG_BASE_R_FEC;
15240 		*flags &= ~I40E_FLAG_RS_FEC;
15241 	}
15242 	if (fec_cfg == 0)
15243 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15244 }
15245 
15246 /**
15247  * i40e_check_recovery_mode - check if we are running transition firmware
15248  * @pf: board private structure
15249  *
15250  * Check registers indicating the firmware runs in recovery mode. Sets the
15251  * appropriate driver state.
15252  *
15253  * Returns true if the recovery mode was detected, false otherwise
15254  **/
15255 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15256 {
15257 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15258 
15259 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15260 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15261 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15262 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15263 
15264 		return true;
15265 	}
15266 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15267 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15268 
15269 	return false;
15270 }
15271 
15272 /**
15273  * i40e_pf_loop_reset - perform reset in a loop.
15274  * @pf: board private structure
15275  *
15276  * This function is useful when a NIC is about to enter recovery mode.
15277  * When a NIC's internal data structures are corrupted the NIC's
15278  * firmware is going to enter recovery mode.
15279  * Right after a POR it takes about 7 minutes for firmware to enter
15280  * recovery mode. Until that time a NIC is in some kind of intermediate
15281  * state. After that time period the NIC almost surely enters
15282  * recovery mode. The only way for a driver to detect intermediate
15283  * state is to issue a series of pf-resets and check a return value.
15284  * If a PF reset returns success then the firmware could be in recovery
15285  * mode so the caller of this code needs to check for recovery mode
15286  * if this function returns success. There is a little chance that
15287  * firmware will hang in intermediate state forever.
15288  * Since waiting 7 minutes is quite a lot of time this function waits
15289  * 10 seconds and then gives up by returning an error.
15290  *
15291  * Return 0 on success, negative on failure.
15292  **/
15293 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
15294 {
15295 	/* wait max 10 seconds for PF reset to succeed */
15296 	const unsigned long time_end = jiffies + 10 * HZ;
15297 
15298 	struct i40e_hw *hw = &pf->hw;
15299 	i40e_status ret;
15300 
15301 	ret = i40e_pf_reset(hw);
15302 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15303 		usleep_range(10000, 20000);
15304 		ret = i40e_pf_reset(hw);
15305 	}
15306 
15307 	if (ret == I40E_SUCCESS)
15308 		pf->pfr_count++;
15309 	else
15310 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15311 
15312 	return ret;
15313 }
15314 
15315 /**
15316  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15317  * @pf: board private structure
15318  *
15319  * Check FW registers to determine if FW issued unexpected EMP Reset.
15320  * Every time when unexpected EMP Reset occurs the FW increments
15321  * a counter of unexpected EMP Resets. When the counter reaches 10
15322  * the FW should enter the Recovery mode
15323  *
15324  * Returns true if FW issued unexpected EMP Reset
15325  **/
15326 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15327 {
15328 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15329 			   I40E_GL_FWSTS_FWS1B_MASK;
15330 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15331 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15332 }
15333 
15334 /**
15335  * i40e_handle_resets - handle EMP resets and PF resets
15336  * @pf: board private structure
15337  *
15338  * Handle both EMP resets and PF resets and conclude whether there are
15339  * any issues regarding these resets. If there are any issues then
15340  * generate log entry.
15341  *
15342  * Return 0 if NIC is healthy or negative value when there are issues
15343  * with resets
15344  **/
15345 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
15346 {
15347 	const i40e_status pfr = i40e_pf_loop_reset(pf);
15348 	const bool is_empr = i40e_check_fw_empr(pf);
15349 
15350 	if (is_empr || pfr != I40E_SUCCESS)
15351 		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");
15352 
15353 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15354 }
15355 
15356 /**
15357  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15358  * @pf: board private structure
15359  * @hw: ptr to the hardware info
15360  *
15361  * This function does a minimal setup of all subsystems needed for running
15362  * recovery mode.
15363  *
15364  * Returns 0 on success, negative on failure
15365  **/
15366 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15367 {
15368 	struct i40e_vsi *vsi;
15369 	int err;
15370 	int v_idx;
15371 
15372 	pci_save_state(pf->pdev);
15373 
15374 	/* set up periodic task facility */
15375 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15376 	pf->service_timer_period = HZ;
15377 
15378 	INIT_WORK(&pf->service_task, i40e_service_task);
15379 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15380 
15381 	err = i40e_init_interrupt_scheme(pf);
15382 	if (err)
15383 		goto err_switch_setup;
15384 
15385 	/* The number of VSIs reported by the FW is the minimum guaranteed
15386 	 * to us; HW supports far more and we share the remaining pool with
15387 	 * the other PFs. We allocate space for more than the guarantee with
15388 	 * the understanding that we might not get them all later.
15389 	 */
15390 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15391 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15392 	else
15393 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15394 
15395 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15396 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15397 			  GFP_KERNEL);
15398 	if (!pf->vsi) {
15399 		err = -ENOMEM;
15400 		goto err_switch_setup;
15401 	}
15402 
15403 	/* We allocate one VSI which is needed as absolute minimum
15404 	 * in order to register the netdev
15405 	 */
15406 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15407 	if (v_idx < 0) {
15408 		err = v_idx;
15409 		goto err_switch_setup;
15410 	}
15411 	pf->lan_vsi = v_idx;
15412 	vsi = pf->vsi[v_idx];
15413 	if (!vsi) {
15414 		err = -EFAULT;
15415 		goto err_switch_setup;
15416 	}
15417 	vsi->alloc_queue_pairs = 1;
15418 	err = i40e_config_netdev(vsi);
15419 	if (err)
15420 		goto err_switch_setup;
15421 	err = register_netdev(vsi->netdev);
15422 	if (err)
15423 		goto err_switch_setup;
15424 	vsi->netdev_registered = true;
15425 	i40e_dbg_pf_init(pf);
15426 
15427 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15428 	if (err)
15429 		goto err_switch_setup;
15430 
15431 	/* tell the firmware that we're starting */
15432 	i40e_send_version(pf);
15433 
15434 	/* since everything's happy, start the service_task timer */
15435 	mod_timer(&pf->service_timer,
15436 		  round_jiffies(jiffies + pf->service_timer_period));
15437 
15438 	return 0;
15439 
15440 err_switch_setup:
15441 	i40e_reset_interrupt_capability(pf);
15442 	del_timer_sync(&pf->service_timer);
15443 	i40e_shutdown_adminq(hw);
15444 	iounmap(hw->hw_addr);
15445 	pci_disable_pcie_error_reporting(pf->pdev);
15446 	pci_release_mem_regions(pf->pdev);
15447 	pci_disable_device(pf->pdev);
15448 	kfree(pf);
15449 
15450 	return err;
15451 }
15452 
15453 /**
15454  * i40e_set_subsystem_device_id - set subsystem device id
15455  * @hw: pointer to the hardware info
15456  *
15457  * Set PCI subsystem device id either from a pci_dev structure or
15458  * a specific FW register.
15459  **/
15460 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15461 {
15462 	struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15463 
15464 	hw->subsystem_device_id = pdev->subsystem_device ?
15465 		pdev->subsystem_device :
15466 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15467 }
15468 
15469 /**
15470  * i40e_probe - Device initialization routine
15471  * @pdev: PCI device information struct
15472  * @ent: entry in i40e_pci_tbl
15473  *
15474  * i40e_probe initializes a PF identified by a pci_dev structure.
15475  * The OS initialization, configuring of the PF private structure,
15476  * and a hardware reset occur.
15477  *
15478  * Returns 0 on success, negative on failure
15479  **/
15480 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15481 {
15482 	struct i40e_aq_get_phy_abilities_resp abilities;
15483 #ifdef CONFIG_I40E_DCB
15484 	enum i40e_get_fw_lldp_status_resp lldp_status;
15485 	i40e_status status;
15486 #endif /* CONFIG_I40E_DCB */
15487 	struct i40e_pf *pf;
15488 	struct i40e_hw *hw;
15489 	static u16 pfs_found;
15490 	u16 wol_nvm_bits;
15491 	u16 link_status;
15492 	int err;
15493 	u32 val;
15494 	u32 i;
15495 
15496 	err = pci_enable_device_mem(pdev);
15497 	if (err)
15498 		return err;
15499 
15500 	/* set up for high or low dma */
15501 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15502 	if (err) {
15503 		dev_err(&pdev->dev,
15504 			"DMA configuration failed: 0x%x\n", err);
15505 		goto err_dma;
15506 	}
15507 
15508 	/* set up pci connections */
15509 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15510 	if (err) {
15511 		dev_info(&pdev->dev,
15512 			 "pci_request_selected_regions failed %d\n", err);
15513 		goto err_pci_reg;
15514 	}
15515 
15516 	pci_enable_pcie_error_reporting(pdev);
15517 	pci_set_master(pdev);
15518 
15519 	/* Now that we have a PCI connection, we need to do the
15520 	 * low level device setup.  This is primarily setting up
15521 	 * the Admin Queue structures and then querying for the
15522 	 * device's current profile information.
15523 	 */
15524 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15525 	if (!pf) {
15526 		err = -ENOMEM;
15527 		goto err_pf_alloc;
15528 	}
15529 	pf->next_vsi = 0;
15530 	pf->pdev = pdev;
15531 	set_bit(__I40E_DOWN, pf->state);
15532 
15533 	hw = &pf->hw;
15534 	hw->back = pf;
15535 
15536 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15537 				I40E_MAX_CSR_SPACE);
15538 	/* We believe that the highest register to read is
15539 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15540 	 * is not less than that before mapping to prevent a
15541 	 * kernel panic.
15542 	 */
15543 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15544 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15545 			pf->ioremap_len);
15546 		err = -ENOMEM;
15547 		goto err_ioremap;
15548 	}
15549 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15550 	if (!hw->hw_addr) {
15551 		err = -EIO;
15552 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15553 			 (unsigned int)pci_resource_start(pdev, 0),
15554 			 pf->ioremap_len, err);
15555 		goto err_ioremap;
15556 	}
15557 	hw->vendor_id = pdev->vendor;
15558 	hw->device_id = pdev->device;
15559 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15560 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15561 	i40e_set_subsystem_device_id(hw);
15562 	hw->bus.device = PCI_SLOT(pdev->devfn);
15563 	hw->bus.func = PCI_FUNC(pdev->devfn);
15564 	hw->bus.bus_id = pdev->bus->number;
15565 	pf->instance = pfs_found;
15566 
15567 	/* Select something other than the 802.1ad ethertype for the
15568 	 * switch to use internally and drop on ingress.
15569 	 */
15570 	hw->switch_tag = 0xffff;
15571 	hw->first_tag = ETH_P_8021AD;
15572 	hw->second_tag = ETH_P_8021Q;
15573 
15574 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15575 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15576 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15577 
15578 	/* set up the locks for the AQ, do this only once in probe
15579 	 * and destroy them only once in remove
15580 	 */
15581 	mutex_init(&hw->aq.asq_mutex);
15582 	mutex_init(&hw->aq.arq_mutex);
15583 
15584 	pf->msg_enable = netif_msg_init(debug,
15585 					NETIF_MSG_DRV |
15586 					NETIF_MSG_PROBE |
15587 					NETIF_MSG_LINK);
15588 	if (debug < -1)
15589 		pf->hw.debug_mask = debug;
15590 
15591 	/* do a special CORER for clearing PXE mode once at init */
15592 	if (hw->revision_id == 0 &&
15593 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15594 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15595 		i40e_flush(hw);
15596 		msleep(200);
15597 		pf->corer_count++;
15598 
15599 		i40e_clear_pxe_mode(hw);
15600 	}
15601 
15602 	/* Reset here to make sure all is clean and to define PF 'n' */
15603 	i40e_clear_hw(hw);
15604 
15605 	err = i40e_set_mac_type(hw);
15606 	if (err) {
15607 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15608 			 err);
15609 		goto err_pf_reset;
15610 	}
15611 
15612 	err = i40e_handle_resets(pf);
15613 	if (err)
15614 		goto err_pf_reset;
15615 
15616 	i40e_check_recovery_mode(pf);
15617 
15618 	if (is_kdump_kernel()) {
15619 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15620 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15621 	} else {
15622 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15623 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15624 	}
15625 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15626 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15627 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15628 
15629 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15630 		 "%s-%s:misc",
15631 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15632 
15633 	err = i40e_init_shared_code(hw);
15634 	if (err) {
15635 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15636 			 err);
15637 		goto err_pf_reset;
15638 	}
15639 
15640 	/* set up a default setting for link flow control */
15641 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15642 
15643 	err = i40e_init_adminq(hw);
15644 	if (err) {
15645 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15646 			dev_info(&pdev->dev,
15647 				 "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",
15648 				 hw->aq.api_maj_ver,
15649 				 hw->aq.api_min_ver,
15650 				 I40E_FW_API_VERSION_MAJOR,
15651 				 I40E_FW_MINOR_VERSION(hw));
15652 		else
15653 			dev_info(&pdev->dev,
15654 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15655 
15656 		goto err_pf_reset;
15657 	}
15658 	i40e_get_oem_version(hw);
15659 
15660 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15661 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15662 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15663 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15664 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15665 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15666 
15667 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15668 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15669 		dev_dbg(&pdev->dev,
15670 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15671 			 hw->aq.api_maj_ver,
15672 			 hw->aq.api_min_ver,
15673 			 I40E_FW_API_VERSION_MAJOR,
15674 			 I40E_FW_MINOR_VERSION(hw));
15675 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15676 		dev_info(&pdev->dev,
15677 			 "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",
15678 			 hw->aq.api_maj_ver,
15679 			 hw->aq.api_min_ver,
15680 			 I40E_FW_API_VERSION_MAJOR,
15681 			 I40E_FW_MINOR_VERSION(hw));
15682 
15683 	i40e_verify_eeprom(pf);
15684 
15685 	/* Rev 0 hardware was never productized */
15686 	if (hw->revision_id < 1)
15687 		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");
15688 
15689 	i40e_clear_pxe_mode(hw);
15690 
15691 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15692 	if (err)
15693 		goto err_adminq_setup;
15694 
15695 	err = i40e_sw_init(pf);
15696 	if (err) {
15697 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15698 		goto err_sw_init;
15699 	}
15700 
15701 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15702 		return i40e_init_recovery_mode(pf, hw);
15703 
15704 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15705 				hw->func_caps.num_rx_qp, 0, 0);
15706 	if (err) {
15707 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15708 		goto err_init_lan_hmc;
15709 	}
15710 
15711 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15712 	if (err) {
15713 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15714 		err = -ENOENT;
15715 		goto err_configure_lan_hmc;
15716 	}
15717 
15718 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15719 	 * Ignore error return codes because if it was already disabled via
15720 	 * hardware settings this will fail
15721 	 */
15722 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15723 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15724 		i40e_aq_stop_lldp(hw, true, false, NULL);
15725 	}
15726 
15727 	/* allow a platform config to override the HW addr */
15728 	i40e_get_platform_mac_addr(pdev, pf);
15729 
15730 	if (!is_valid_ether_addr(hw->mac.addr)) {
15731 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15732 		err = -EIO;
15733 		goto err_mac_addr;
15734 	}
15735 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15736 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15737 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15738 	if (is_valid_ether_addr(hw->mac.port_addr))
15739 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15740 
15741 	i40e_ptp_alloc_pins(pf);
15742 	pci_set_drvdata(pdev, pf);
15743 	pci_save_state(pdev);
15744 
15745 #ifdef CONFIG_I40E_DCB
15746 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15747 	(!status &&
15748 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15749 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15750 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15751 	dev_info(&pdev->dev,
15752 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15753 			"FW LLDP is disabled\n" :
15754 			"FW LLDP is enabled\n");
15755 
15756 	/* Enable FW to write default DCB config on link-up */
15757 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15758 
15759 	err = i40e_init_pf_dcb(pf);
15760 	if (err) {
15761 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15762 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15763 		/* Continue without DCB enabled */
15764 	}
15765 #endif /* CONFIG_I40E_DCB */
15766 
15767 	/* set up periodic task facility */
15768 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15769 	pf->service_timer_period = HZ;
15770 
15771 	INIT_WORK(&pf->service_task, i40e_service_task);
15772 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15773 
15774 	/* NVM bit on means WoL disabled for the port */
15775 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15776 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15777 		pf->wol_en = false;
15778 	else
15779 		pf->wol_en = true;
15780 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15781 
15782 	/* set up the main switch operations */
15783 	i40e_determine_queue_usage(pf);
15784 	err = i40e_init_interrupt_scheme(pf);
15785 	if (err)
15786 		goto err_switch_setup;
15787 
15788 	/* Reduce Tx and Rx pairs for kdump
15789 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15790 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15791 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15792 	 */
15793 	if (is_kdump_kernel())
15794 		pf->num_lan_msix = 1;
15795 
15796 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15797 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15798 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15799 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15800 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15801 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15802 						    UDP_TUNNEL_TYPE_GENEVE;
15803 
15804 	/* The number of VSIs reported by the FW is the minimum guaranteed
15805 	 * to us; HW supports far more and we share the remaining pool with
15806 	 * the other PFs. We allocate space for more than the guarantee with
15807 	 * the understanding that we might not get them all later.
15808 	 */
15809 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15810 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15811 	else
15812 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15813 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15814 		dev_warn(&pf->pdev->dev,
15815 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15816 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15817 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15818 	}
15819 
15820 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15821 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15822 			  GFP_KERNEL);
15823 	if (!pf->vsi) {
15824 		err = -ENOMEM;
15825 		goto err_switch_setup;
15826 	}
15827 
15828 #ifdef CONFIG_PCI_IOV
15829 	/* prep for VF support */
15830 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15831 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15832 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15833 		if (pci_num_vf(pdev))
15834 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15835 	}
15836 #endif
15837 	err = i40e_setup_pf_switch(pf, false, false);
15838 	if (err) {
15839 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15840 		goto err_vsis;
15841 	}
15842 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15843 
15844 	/* if FDIR VSI was set up, start it now */
15845 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15846 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15847 			i40e_vsi_open(pf->vsi[i]);
15848 			break;
15849 		}
15850 	}
15851 
15852 	/* The driver only wants link up/down and module qualification
15853 	 * reports from firmware.  Note the negative logic.
15854 	 */
15855 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15856 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15857 					 I40E_AQ_EVENT_MEDIA_NA |
15858 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15859 	if (err)
15860 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15861 			 i40e_stat_str(&pf->hw, err),
15862 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15863 
15864 	/* Reconfigure hardware for allowing smaller MSS in the case
15865 	 * of TSO, so that we avoid the MDD being fired and causing
15866 	 * a reset in the case of small MSS+TSO.
15867 	 */
15868 	val = rd32(hw, I40E_REG_MSS);
15869 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15870 		val &= ~I40E_REG_MSS_MIN_MASK;
15871 		val |= I40E_64BYTE_MSS;
15872 		wr32(hw, I40E_REG_MSS, val);
15873 	}
15874 
15875 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15876 		msleep(75);
15877 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15878 		if (err)
15879 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15880 				 i40e_stat_str(&pf->hw, err),
15881 				 i40e_aq_str(&pf->hw,
15882 					     pf->hw.aq.asq_last_status));
15883 	}
15884 	/* The main driver is (mostly) up and happy. We need to set this state
15885 	 * before setting up the misc vector or we get a race and the vector
15886 	 * ends up disabled forever.
15887 	 */
15888 	clear_bit(__I40E_DOWN, pf->state);
15889 
15890 	/* In case of MSIX we are going to setup the misc vector right here
15891 	 * to handle admin queue events etc. In case of legacy and MSI
15892 	 * the misc functionality and queue processing is combined in
15893 	 * the same vector and that gets setup at open.
15894 	 */
15895 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15896 		err = i40e_setup_misc_vector(pf);
15897 		if (err) {
15898 			dev_info(&pdev->dev,
15899 				 "setup of misc vector failed: %d\n", err);
15900 			i40e_cloud_filter_exit(pf);
15901 			i40e_fdir_teardown(pf);
15902 			goto err_vsis;
15903 		}
15904 	}
15905 
15906 #ifdef CONFIG_PCI_IOV
15907 	/* prep for VF support */
15908 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15909 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15910 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15911 		/* disable link interrupts for VFs */
15912 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15913 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15914 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15915 		i40e_flush(hw);
15916 
15917 		if (pci_num_vf(pdev)) {
15918 			dev_info(&pdev->dev,
15919 				 "Active VFs found, allocating resources.\n");
15920 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15921 			if (err)
15922 				dev_info(&pdev->dev,
15923 					 "Error %d allocating resources for existing VFs\n",
15924 					 err);
15925 		}
15926 	}
15927 #endif /* CONFIG_PCI_IOV */
15928 
15929 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15930 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
15931 						      pf->num_iwarp_msix,
15932 						      I40E_IWARP_IRQ_PILE_ID);
15933 		if (pf->iwarp_base_vector < 0) {
15934 			dev_info(&pdev->dev,
15935 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
15936 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
15937 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
15938 		}
15939 	}
15940 
15941 	i40e_dbg_pf_init(pf);
15942 
15943 	/* tell the firmware that we're starting */
15944 	i40e_send_version(pf);
15945 
15946 	/* since everything's happy, start the service_task timer */
15947 	mod_timer(&pf->service_timer,
15948 		  round_jiffies(jiffies + pf->service_timer_period));
15949 
15950 	/* add this PF to client device list and launch a client service task */
15951 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15952 		err = i40e_lan_add_device(pf);
15953 		if (err)
15954 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
15955 				 err);
15956 	}
15957 
15958 #define PCI_SPEED_SIZE 8
15959 #define PCI_WIDTH_SIZE 8
15960 	/* Devices on the IOSF bus do not have this information
15961 	 * and will report PCI Gen 1 x 1 by default so don't bother
15962 	 * checking them.
15963 	 */
15964 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
15965 		char speed[PCI_SPEED_SIZE] = "Unknown";
15966 		char width[PCI_WIDTH_SIZE] = "Unknown";
15967 
15968 		/* Get the negotiated link width and speed from PCI config
15969 		 * space
15970 		 */
15971 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
15972 					  &link_status);
15973 
15974 		i40e_set_pci_config_data(hw, link_status);
15975 
15976 		switch (hw->bus.speed) {
15977 		case i40e_bus_speed_8000:
15978 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
15979 		case i40e_bus_speed_5000:
15980 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
15981 		case i40e_bus_speed_2500:
15982 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
15983 		default:
15984 			break;
15985 		}
15986 		switch (hw->bus.width) {
15987 		case i40e_bus_width_pcie_x8:
15988 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
15989 		case i40e_bus_width_pcie_x4:
15990 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
15991 		case i40e_bus_width_pcie_x2:
15992 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
15993 		case i40e_bus_width_pcie_x1:
15994 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
15995 		default:
15996 			break;
15997 		}
15998 
15999 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16000 			 speed, width);
16001 
16002 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16003 		    hw->bus.speed < i40e_bus_speed_8000) {
16004 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16005 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16006 		}
16007 	}
16008 
16009 	/* get the requested speeds from the fw */
16010 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16011 	if (err)
16012 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
16013 			i40e_stat_str(&pf->hw, err),
16014 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16015 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16016 
16017 	/* set the FEC config due to the board capabilities */
16018 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
16019 
16020 	/* get the supported phy types from the fw */
16021 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16022 	if (err)
16023 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
16024 			i40e_stat_str(&pf->hw, err),
16025 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16026 
16027 	/* make sure the MFS hasn't been set lower than the default */
16028 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16029 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
16030 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
16031 	if (val < MAX_FRAME_SIZE_DEFAULT)
16032 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
16033 			 i, val);
16034 
16035 	/* Add a filter to drop all Flow control frames from any VSI from being
16036 	 * transmitted. By doing so we stop a malicious VF from sending out
16037 	 * PAUSE or PFC frames and potentially controlling traffic for other
16038 	 * PF/VF VSIs.
16039 	 * The FW can still send Flow control frames if enabled.
16040 	 */
16041 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16042 						       pf->main_vsi_seid);
16043 
16044 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16045 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16046 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
16047 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16048 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
16049 	/* print a string summarizing features */
16050 	i40e_print_features(pf);
16051 
16052 	return 0;
16053 
16054 	/* Unwind what we've done if something failed in the setup */
16055 err_vsis:
16056 	set_bit(__I40E_DOWN, pf->state);
16057 	i40e_clear_interrupt_scheme(pf);
16058 	kfree(pf->vsi);
16059 err_switch_setup:
16060 	i40e_reset_interrupt_capability(pf);
16061 	del_timer_sync(&pf->service_timer);
16062 err_mac_addr:
16063 err_configure_lan_hmc:
16064 	(void)i40e_shutdown_lan_hmc(hw);
16065 err_init_lan_hmc:
16066 	kfree(pf->qp_pile);
16067 err_sw_init:
16068 err_adminq_setup:
16069 err_pf_reset:
16070 	iounmap(hw->hw_addr);
16071 err_ioremap:
16072 	kfree(pf);
16073 err_pf_alloc:
16074 	pci_disable_pcie_error_reporting(pdev);
16075 	pci_release_mem_regions(pdev);
16076 err_pci_reg:
16077 err_dma:
16078 	pci_disable_device(pdev);
16079 	return err;
16080 }
16081 
16082 /**
16083  * i40e_remove - Device removal routine
16084  * @pdev: PCI device information struct
16085  *
16086  * i40e_remove is called by the PCI subsystem to alert the driver
16087  * that is should release a PCI device.  This could be caused by a
16088  * Hot-Plug event, or because the driver is going to be removed from
16089  * memory.
16090  **/
16091 static void i40e_remove(struct pci_dev *pdev)
16092 {
16093 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16094 	struct i40e_hw *hw = &pf->hw;
16095 	i40e_status ret_code;
16096 	int i;
16097 
16098 	i40e_dbg_pf_exit(pf);
16099 
16100 	i40e_ptp_stop(pf);
16101 
16102 	/* Disable RSS in hw */
16103 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16104 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16105 
16106 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16107 	 * flags, once they are set, i40e_rebuild should not be called as
16108 	 * i40e_prep_for_reset always returns early.
16109 	 */
16110 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16111 		usleep_range(1000, 2000);
16112 	set_bit(__I40E_IN_REMOVE, pf->state);
16113 
16114 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
16115 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16116 		i40e_free_vfs(pf);
16117 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
16118 	}
16119 	/* no more scheduling of any task */
16120 	set_bit(__I40E_SUSPENDED, pf->state);
16121 	set_bit(__I40E_DOWN, pf->state);
16122 	if (pf->service_timer.function)
16123 		del_timer_sync(&pf->service_timer);
16124 	if (pf->service_task.func)
16125 		cancel_work_sync(&pf->service_task);
16126 
16127 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16128 		struct i40e_vsi *vsi = pf->vsi[0];
16129 
16130 		/* We know that we have allocated only one vsi for this PF,
16131 		 * it was just for registering netdevice, so the interface
16132 		 * could be visible in the 'ifconfig' output
16133 		 */
16134 		unregister_netdev(vsi->netdev);
16135 		free_netdev(vsi->netdev);
16136 
16137 		goto unmap;
16138 	}
16139 
16140 	/* Client close must be called explicitly here because the timer
16141 	 * has been stopped.
16142 	 */
16143 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16144 
16145 	i40e_fdir_teardown(pf);
16146 
16147 	/* If there is a switch structure or any orphans, remove them.
16148 	 * This will leave only the PF's VSI remaining.
16149 	 */
16150 	for (i = 0; i < I40E_MAX_VEB; i++) {
16151 		if (!pf->veb[i])
16152 			continue;
16153 
16154 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16155 		    pf->veb[i]->uplink_seid == 0)
16156 			i40e_switch_branch_release(pf->veb[i]);
16157 	}
16158 
16159 	/* Now we can shutdown the PF's VSI, just before we kill
16160 	 * adminq and hmc.
16161 	 */
16162 	if (pf->vsi[pf->lan_vsi])
16163 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
16164 
16165 	i40e_cloud_filter_exit(pf);
16166 
16167 	/* remove attached clients */
16168 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16169 		ret_code = i40e_lan_del_device(pf);
16170 		if (ret_code)
16171 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16172 				 ret_code);
16173 	}
16174 
16175 	/* shutdown and destroy the HMC */
16176 	if (hw->hmc.hmc_obj) {
16177 		ret_code = i40e_shutdown_lan_hmc(hw);
16178 		if (ret_code)
16179 			dev_warn(&pdev->dev,
16180 				 "Failed to destroy the HMC resources: %d\n",
16181 				 ret_code);
16182 	}
16183 
16184 unmap:
16185 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16186 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16187 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16188 		free_irq(pf->pdev->irq, pf);
16189 
16190 	/* shutdown the adminq */
16191 	i40e_shutdown_adminq(hw);
16192 
16193 	/* destroy the locks only once, here */
16194 	mutex_destroy(&hw->aq.arq_mutex);
16195 	mutex_destroy(&hw->aq.asq_mutex);
16196 
16197 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16198 	rtnl_lock();
16199 	i40e_clear_interrupt_scheme(pf);
16200 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16201 		if (pf->vsi[i]) {
16202 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16203 				i40e_vsi_clear_rings(pf->vsi[i]);
16204 			i40e_vsi_clear(pf->vsi[i]);
16205 			pf->vsi[i] = NULL;
16206 		}
16207 	}
16208 	rtnl_unlock();
16209 
16210 	for (i = 0; i < I40E_MAX_VEB; i++) {
16211 		kfree(pf->veb[i]);
16212 		pf->veb[i] = NULL;
16213 	}
16214 
16215 	kfree(pf->qp_pile);
16216 	kfree(pf->vsi);
16217 
16218 	iounmap(hw->hw_addr);
16219 	kfree(pf);
16220 	pci_release_mem_regions(pdev);
16221 
16222 	pci_disable_pcie_error_reporting(pdev);
16223 	pci_disable_device(pdev);
16224 }
16225 
16226 /**
16227  * i40e_pci_error_detected - warning that something funky happened in PCI land
16228  * @pdev: PCI device information struct
16229  * @error: the type of PCI error
16230  *
16231  * Called to warn that something happened and the error handling steps
16232  * are in progress.  Allows the driver to quiesce things, be ready for
16233  * remediation.
16234  **/
16235 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16236 						pci_channel_state_t error)
16237 {
16238 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16239 
16240 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16241 
16242 	if (!pf) {
16243 		dev_info(&pdev->dev,
16244 			 "Cannot recover - error happened during device probe\n");
16245 		return PCI_ERS_RESULT_DISCONNECT;
16246 	}
16247 
16248 	/* shutdown all operations */
16249 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16250 		i40e_prep_for_reset(pf);
16251 
16252 	/* Request a slot reset */
16253 	return PCI_ERS_RESULT_NEED_RESET;
16254 }
16255 
16256 /**
16257  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16258  * @pdev: PCI device information struct
16259  *
16260  * Called to find if the driver can work with the device now that
16261  * the pci slot has been reset.  If a basic connection seems good
16262  * (registers are readable and have sane content) then return a
16263  * happy little PCI_ERS_RESULT_xxx.
16264  **/
16265 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16266 {
16267 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16268 	pci_ers_result_t result;
16269 	u32 reg;
16270 
16271 	dev_dbg(&pdev->dev, "%s\n", __func__);
16272 	if (pci_enable_device_mem(pdev)) {
16273 		dev_info(&pdev->dev,
16274 			 "Cannot re-enable PCI device after reset.\n");
16275 		result = PCI_ERS_RESULT_DISCONNECT;
16276 	} else {
16277 		pci_set_master(pdev);
16278 		pci_restore_state(pdev);
16279 		pci_save_state(pdev);
16280 		pci_wake_from_d3(pdev, false);
16281 
16282 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16283 		if (reg == 0)
16284 			result = PCI_ERS_RESULT_RECOVERED;
16285 		else
16286 			result = PCI_ERS_RESULT_DISCONNECT;
16287 	}
16288 
16289 	return result;
16290 }
16291 
16292 /**
16293  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16294  * @pdev: PCI device information struct
16295  */
16296 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16297 {
16298 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16299 
16300 	i40e_prep_for_reset(pf);
16301 }
16302 
16303 /**
16304  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16305  * @pdev: PCI device information struct
16306  */
16307 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16308 {
16309 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16310 
16311 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16312 		return;
16313 
16314 	i40e_reset_and_rebuild(pf, false, false);
16315 }
16316 
16317 /**
16318  * i40e_pci_error_resume - restart operations after PCI error recovery
16319  * @pdev: PCI device information struct
16320  *
16321  * Called to allow the driver to bring things back up after PCI error
16322  * and/or reset recovery has finished.
16323  **/
16324 static void i40e_pci_error_resume(struct pci_dev *pdev)
16325 {
16326 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16327 
16328 	dev_dbg(&pdev->dev, "%s\n", __func__);
16329 	if (test_bit(__I40E_SUSPENDED, pf->state))
16330 		return;
16331 
16332 	i40e_handle_reset_warning(pf, false);
16333 }
16334 
16335 /**
16336  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16337  * using the mac_address_write admin q function
16338  * @pf: pointer to i40e_pf struct
16339  **/
16340 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16341 {
16342 	struct i40e_hw *hw = &pf->hw;
16343 	i40e_status ret;
16344 	u8 mac_addr[6];
16345 	u16 flags = 0;
16346 
16347 	/* Get current MAC address in case it's an LAA */
16348 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16349 		ether_addr_copy(mac_addr,
16350 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16351 	} else {
16352 		dev_err(&pf->pdev->dev,
16353 			"Failed to retrieve MAC address; using default\n");
16354 		ether_addr_copy(mac_addr, hw->mac.addr);
16355 	}
16356 
16357 	/* The FW expects the mac address write cmd to first be called with
16358 	 * one of these flags before calling it again with the multicast
16359 	 * enable flags.
16360 	 */
16361 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16362 
16363 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16364 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16365 
16366 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16367 	if (ret) {
16368 		dev_err(&pf->pdev->dev,
16369 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16370 		return;
16371 	}
16372 
16373 	flags = I40E_AQC_MC_MAG_EN
16374 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16375 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16376 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16377 	if (ret)
16378 		dev_err(&pf->pdev->dev,
16379 			"Failed to enable Multicast Magic Packet wake up\n");
16380 }
16381 
16382 /**
16383  * i40e_shutdown - PCI callback for shutting down
16384  * @pdev: PCI device information struct
16385  **/
16386 static void i40e_shutdown(struct pci_dev *pdev)
16387 {
16388 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16389 	struct i40e_hw *hw = &pf->hw;
16390 
16391 	set_bit(__I40E_SUSPENDED, pf->state);
16392 	set_bit(__I40E_DOWN, pf->state);
16393 
16394 	del_timer_sync(&pf->service_timer);
16395 	cancel_work_sync(&pf->service_task);
16396 	i40e_cloud_filter_exit(pf);
16397 	i40e_fdir_teardown(pf);
16398 
16399 	/* Client close must be called explicitly here because the timer
16400 	 * has been stopped.
16401 	 */
16402 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16403 
16404 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16405 		i40e_enable_mc_magic_wake(pf);
16406 
16407 	i40e_prep_for_reset(pf);
16408 
16409 	wr32(hw, I40E_PFPM_APM,
16410 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16411 	wr32(hw, I40E_PFPM_WUFC,
16412 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16413 
16414 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16415 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16416 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16417 		free_irq(pf->pdev->irq, pf);
16418 
16419 	/* Since we're going to destroy queues during the
16420 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16421 	 * whole section
16422 	 */
16423 	rtnl_lock();
16424 	i40e_clear_interrupt_scheme(pf);
16425 	rtnl_unlock();
16426 
16427 	if (system_state == SYSTEM_POWER_OFF) {
16428 		pci_wake_from_d3(pdev, pf->wol_en);
16429 		pci_set_power_state(pdev, PCI_D3hot);
16430 	}
16431 }
16432 
16433 /**
16434  * i40e_suspend - PM callback for moving to D3
16435  * @dev: generic device information structure
16436  **/
16437 static int __maybe_unused i40e_suspend(struct device *dev)
16438 {
16439 	struct i40e_pf *pf = dev_get_drvdata(dev);
16440 	struct i40e_hw *hw = &pf->hw;
16441 
16442 	/* If we're already suspended, then there is nothing to do */
16443 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16444 		return 0;
16445 
16446 	set_bit(__I40E_DOWN, pf->state);
16447 
16448 	/* Ensure service task will not be running */
16449 	del_timer_sync(&pf->service_timer);
16450 	cancel_work_sync(&pf->service_task);
16451 
16452 	/* Client close must be called explicitly here because the timer
16453 	 * has been stopped.
16454 	 */
16455 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16456 
16457 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16458 		i40e_enable_mc_magic_wake(pf);
16459 
16460 	/* Since we're going to destroy queues during the
16461 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16462 	 * whole section
16463 	 */
16464 	rtnl_lock();
16465 
16466 	i40e_prep_for_reset(pf);
16467 
16468 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16469 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16470 
16471 	/* Clear the interrupt scheme and release our IRQs so that the system
16472 	 * can safely hibernate even when there are a large number of CPUs.
16473 	 * Otherwise hibernation might fail when mapping all the vectors back
16474 	 * to CPU0.
16475 	 */
16476 	i40e_clear_interrupt_scheme(pf);
16477 
16478 	rtnl_unlock();
16479 
16480 	return 0;
16481 }
16482 
16483 /**
16484  * i40e_resume - PM callback for waking up from D3
16485  * @dev: generic device information structure
16486  **/
16487 static int __maybe_unused i40e_resume(struct device *dev)
16488 {
16489 	struct i40e_pf *pf = dev_get_drvdata(dev);
16490 	int err;
16491 
16492 	/* If we're not suspended, then there is nothing to do */
16493 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16494 		return 0;
16495 
16496 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16497 	 * since we're going to be restoring queues
16498 	 */
16499 	rtnl_lock();
16500 
16501 	/* We cleared the interrupt scheme when we suspended, so we need to
16502 	 * restore it now to resume device functionality.
16503 	 */
16504 	err = i40e_restore_interrupt_scheme(pf);
16505 	if (err) {
16506 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16507 			err);
16508 	}
16509 
16510 	clear_bit(__I40E_DOWN, pf->state);
16511 	i40e_reset_and_rebuild(pf, false, true);
16512 
16513 	rtnl_unlock();
16514 
16515 	/* Clear suspended state last after everything is recovered */
16516 	clear_bit(__I40E_SUSPENDED, pf->state);
16517 
16518 	/* Restart the service task */
16519 	mod_timer(&pf->service_timer,
16520 		  round_jiffies(jiffies + pf->service_timer_period));
16521 
16522 	return 0;
16523 }
16524 
16525 static const struct pci_error_handlers i40e_err_handler = {
16526 	.error_detected = i40e_pci_error_detected,
16527 	.slot_reset = i40e_pci_error_slot_reset,
16528 	.reset_prepare = i40e_pci_error_reset_prepare,
16529 	.reset_done = i40e_pci_error_reset_done,
16530 	.resume = i40e_pci_error_resume,
16531 };
16532 
16533 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16534 
16535 static struct pci_driver i40e_driver = {
16536 	.name     = i40e_driver_name,
16537 	.id_table = i40e_pci_tbl,
16538 	.probe    = i40e_probe,
16539 	.remove   = i40e_remove,
16540 	.driver   = {
16541 		.pm = &i40e_pm_ops,
16542 	},
16543 	.shutdown = i40e_shutdown,
16544 	.err_handler = &i40e_err_handler,
16545 	.sriov_configure = i40e_pci_sriov_configure,
16546 };
16547 
16548 /**
16549  * i40e_init_module - Driver registration routine
16550  *
16551  * i40e_init_module is the first routine called when the driver is
16552  * loaded. All it does is register with the PCI subsystem.
16553  **/
16554 static int __init i40e_init_module(void)
16555 {
16556 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16557 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16558 
16559 	/* There is no need to throttle the number of active tasks because
16560 	 * each device limits its own task using a state bit for scheduling
16561 	 * the service task, and the device tasks do not interfere with each
16562 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16563 	 * since we need to be able to guarantee forward progress even under
16564 	 * memory pressure.
16565 	 */
16566 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16567 	if (!i40e_wq) {
16568 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16569 		return -ENOMEM;
16570 	}
16571 
16572 	i40e_dbg_init();
16573 	return pci_register_driver(&i40e_driver);
16574 }
16575 module_init(i40e_init_module);
16576 
16577 /**
16578  * i40e_exit_module - Driver exit cleanup routine
16579  *
16580  * i40e_exit_module is called just before the driver is removed
16581  * from memory.
16582  **/
16583 static void __exit i40e_exit_module(void)
16584 {
16585 	pci_unregister_driver(&i40e_driver);
16586 	destroy_workqueue(i40e_wq);
16587 	ida_destroy(&i40e_client_ida);
16588 	i40e_dbg_exit();
16589 }
16590 module_exit(i40e_exit_module);
16591