xref: /openbmc/linux/drivers/net/ethernet/intel/i40e/i40e_main.c (revision fa5d824ce5dd8306c66f45c34fd78536e6ce2488)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2021 Intel Corporation. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/of_net.h>
6 #include <linux/pci.h>
7 #include <linux/bpf.h>
8 #include <generated/utsrelease.h>
9 #include <linux/crash_dump.h>
10 
11 /* Local includes */
12 #include "i40e.h"
13 #include "i40e_diag.h"
14 #include "i40e_xsk.h"
15 #include <net/udp_tunnel.h>
16 #include <net/xdp_sock_drv.h>
17 /* All i40e tracepoints are defined by the include below, which
18  * must be included exactly once across the whole kernel with
19  * CREATE_TRACE_POINTS defined
20  */
21 #define CREATE_TRACE_POINTS
22 #include "i40e_trace.h"
23 
24 const char i40e_driver_name[] = "i40e";
25 static const char i40e_driver_string[] =
26 			"Intel(R) Ethernet Connection XL710 Network Driver";
27 
28 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
29 
30 /* a bit of forward declarations */
31 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
32 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
33 static int i40e_add_vsi(struct i40e_vsi *vsi);
34 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
35 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
36 static int i40e_setup_misc_vector(struct i40e_pf *pf);
37 static void i40e_determine_queue_usage(struct i40e_pf *pf);
38 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
39 static void i40e_prep_for_reset(struct i40e_pf *pf);
40 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
41 				   bool lock_acquired);
42 static int i40e_reset(struct i40e_pf *pf);
43 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
44 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
45 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
46 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
47 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
48 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
49 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
50 static int i40e_get_capabilities(struct i40e_pf *pf,
51 				 enum i40e_admin_queue_opc list_type);
52 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
53 
54 /* i40e_pci_tbl - PCI Device ID Table
55  *
56  * Last entry must be all 0s
57  *
58  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
59  *   Class, Class Mask, private data (not used) }
60  */
61 static const struct pci_device_id i40e_pci_tbl[] = {
62 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
63 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
64 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
86 	/* required last entry */
87 	{0, }
88 };
89 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
90 
91 #define I40E_MAX_VF_COUNT 128
92 static int debug = -1;
93 module_param(debug, uint, 0);
94 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
95 
96 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
97 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
98 MODULE_LICENSE("GPL v2");
99 
100 static struct workqueue_struct *i40e_wq;
101 
102 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
103 				  struct net_device *netdev, int delta)
104 {
105 	struct netdev_hw_addr *ha;
106 
107 	if (!f || !netdev)
108 		return;
109 
110 	netdev_for_each_mc_addr(ha, netdev) {
111 		if (ether_addr_equal(ha->addr, f->macaddr)) {
112 			ha->refcount += delta;
113 			if (ha->refcount <= 0)
114 				ha->refcount = 1;
115 			break;
116 		}
117 	}
118 }
119 
120 /**
121  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
122  * @hw:   pointer to the HW structure
123  * @mem:  ptr to mem struct to fill out
124  * @size: size of memory requested
125  * @alignment: what to align the allocation to
126  **/
127 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
128 			    u64 size, u32 alignment)
129 {
130 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
131 
132 	mem->size = ALIGN(size, alignment);
133 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
134 				     GFP_KERNEL);
135 	if (!mem->va)
136 		return -ENOMEM;
137 
138 	return 0;
139 }
140 
141 /**
142  * i40e_free_dma_mem_d - OS specific memory free for shared code
143  * @hw:   pointer to the HW structure
144  * @mem:  ptr to mem struct to free
145  **/
146 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
147 {
148 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
149 
150 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
151 	mem->va = NULL;
152 	mem->pa = 0;
153 	mem->size = 0;
154 
155 	return 0;
156 }
157 
158 /**
159  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
160  * @hw:   pointer to the HW structure
161  * @mem:  ptr to mem struct to fill out
162  * @size: size of memory requested
163  **/
164 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
165 			     u32 size)
166 {
167 	mem->size = size;
168 	mem->va = kzalloc(size, GFP_KERNEL);
169 
170 	if (!mem->va)
171 		return -ENOMEM;
172 
173 	return 0;
174 }
175 
176 /**
177  * i40e_free_virt_mem_d - OS specific memory free for shared code
178  * @hw:   pointer to the HW structure
179  * @mem:  ptr to mem struct to free
180  **/
181 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
182 {
183 	/* it's ok to kfree a NULL pointer */
184 	kfree(mem->va);
185 	mem->va = NULL;
186 	mem->size = 0;
187 
188 	return 0;
189 }
190 
191 /**
192  * i40e_get_lump - find a lump of free generic resource
193  * @pf: board private structure
194  * @pile: the pile of resource to search
195  * @needed: the number of items needed
196  * @id: an owner id to stick on the items assigned
197  *
198  * Returns the base item index of the lump, or negative for error
199  **/
200 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
201 			 u16 needed, u16 id)
202 {
203 	int ret = -ENOMEM;
204 	int i, j;
205 
206 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
207 		dev_info(&pf->pdev->dev,
208 			 "param err: pile=%s needed=%d id=0x%04x\n",
209 			 pile ? "<valid>" : "<null>", needed, id);
210 		return -EINVAL;
211 	}
212 
213 	/* Allocate last queue in the pile for FDIR VSI queue
214 	 * so it doesn't fragment the qp_pile
215 	 */
216 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
217 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
218 			dev_err(&pf->pdev->dev,
219 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
220 				pile->num_entries - 1);
221 			return -ENOMEM;
222 		}
223 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
224 		return pile->num_entries - 1;
225 	}
226 
227 	i = 0;
228 	while (i < pile->num_entries) {
229 		/* skip already allocated entries */
230 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
231 			i++;
232 			continue;
233 		}
234 
235 		/* do we have enough in this lump? */
236 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
237 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
238 				break;
239 		}
240 
241 		if (j == needed) {
242 			/* there was enough, so assign it to the requestor */
243 			for (j = 0; j < needed; j++)
244 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
245 			ret = i;
246 			break;
247 		}
248 
249 		/* not enough, so skip over it and continue looking */
250 		i += j;
251 	}
252 
253 	return ret;
254 }
255 
256 /**
257  * i40e_put_lump - return a lump of generic resource
258  * @pile: the pile of resource to search
259  * @index: the base item index
260  * @id: the owner id of the items assigned
261  *
262  * Returns the count of items in the lump
263  **/
264 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
265 {
266 	int valid_id = (id | I40E_PILE_VALID_BIT);
267 	int count = 0;
268 	u16 i;
269 
270 	if (!pile || index >= pile->num_entries)
271 		return -EINVAL;
272 
273 	for (i = index;
274 	     i < pile->num_entries && pile->list[i] == valid_id;
275 	     i++) {
276 		pile->list[i] = 0;
277 		count++;
278 	}
279 
280 
281 	return count;
282 }
283 
284 /**
285  * i40e_find_vsi_from_id - searches for the vsi with the given id
286  * @pf: the pf structure to search for the vsi
287  * @id: id of the vsi it is searching for
288  **/
289 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
290 {
291 	int i;
292 
293 	for (i = 0; i < pf->num_alloc_vsi; i++)
294 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
295 			return pf->vsi[i];
296 
297 	return NULL;
298 }
299 
300 /**
301  * i40e_service_event_schedule - Schedule the service task to wake up
302  * @pf: board private structure
303  *
304  * If not already scheduled, this puts the task into the work queue
305  **/
306 void i40e_service_event_schedule(struct i40e_pf *pf)
307 {
308 	if ((!test_bit(__I40E_DOWN, pf->state) &&
309 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
310 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
311 		queue_work(i40e_wq, &pf->service_task);
312 }
313 
314 /**
315  * i40e_tx_timeout - Respond to a Tx Hang
316  * @netdev: network interface device structure
317  * @txqueue: queue number timing out
318  *
319  * If any port has noticed a Tx timeout, it is likely that the whole
320  * device is munged, not just the one netdev port, so go for the full
321  * reset.
322  **/
323 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
324 {
325 	struct i40e_netdev_priv *np = netdev_priv(netdev);
326 	struct i40e_vsi *vsi = np->vsi;
327 	struct i40e_pf *pf = vsi->back;
328 	struct i40e_ring *tx_ring = NULL;
329 	unsigned int i;
330 	u32 head, val;
331 
332 	pf->tx_timeout_count++;
333 
334 	/* with txqueue index, find the tx_ring struct */
335 	for (i = 0; i < vsi->num_queue_pairs; i++) {
336 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
337 			if (txqueue ==
338 			    vsi->tx_rings[i]->queue_index) {
339 				tx_ring = vsi->tx_rings[i];
340 				break;
341 			}
342 		}
343 	}
344 
345 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
346 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
347 	else if (time_before(jiffies,
348 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
349 		return;   /* don't do any new action before the next timeout */
350 
351 	/* don't kick off another recovery if one is already pending */
352 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
353 		return;
354 
355 	if (tx_ring) {
356 		head = i40e_get_head(tx_ring);
357 		/* Read interrupt register */
358 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
359 			val = rd32(&pf->hw,
360 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
361 						tx_ring->vsi->base_vector - 1));
362 		else
363 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
364 
365 		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",
366 			    vsi->seid, txqueue, tx_ring->next_to_clean,
367 			    head, tx_ring->next_to_use,
368 			    readl(tx_ring->tail), val);
369 	}
370 
371 	pf->tx_timeout_last_recovery = jiffies;
372 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
373 		    pf->tx_timeout_recovery_level, txqueue);
374 
375 	switch (pf->tx_timeout_recovery_level) {
376 	case 1:
377 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
378 		break;
379 	case 2:
380 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
381 		break;
382 	case 3:
383 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
384 		break;
385 	default:
386 		netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
387 		break;
388 	}
389 
390 	i40e_service_event_schedule(pf);
391 	pf->tx_timeout_recovery_level++;
392 }
393 
394 /**
395  * i40e_get_vsi_stats_struct - Get System Network Statistics
396  * @vsi: the VSI we care about
397  *
398  * Returns the address of the device statistics structure.
399  * The statistics are actually updated from the service task.
400  **/
401 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
402 {
403 	return &vsi->net_stats;
404 }
405 
406 /**
407  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
408  * @ring: Tx ring to get statistics from
409  * @stats: statistics entry to be updated
410  **/
411 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
412 					    struct rtnl_link_stats64 *stats)
413 {
414 	u64 bytes, packets;
415 	unsigned int start;
416 
417 	do {
418 		start = u64_stats_fetch_begin_irq(&ring->syncp);
419 		packets = ring->stats.packets;
420 		bytes   = ring->stats.bytes;
421 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
422 
423 	stats->tx_packets += packets;
424 	stats->tx_bytes   += bytes;
425 }
426 
427 /**
428  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
429  * @netdev: network interface device structure
430  * @stats: data structure to store statistics
431  *
432  * Returns the address of the device statistics structure.
433  * The statistics are actually updated from the service task.
434  **/
435 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
436 				  struct rtnl_link_stats64 *stats)
437 {
438 	struct i40e_netdev_priv *np = netdev_priv(netdev);
439 	struct i40e_vsi *vsi = np->vsi;
440 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
441 	struct i40e_ring *ring;
442 	int i;
443 
444 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
445 		return;
446 
447 	if (!vsi->tx_rings)
448 		return;
449 
450 	rcu_read_lock();
451 	for (i = 0; i < vsi->num_queue_pairs; i++) {
452 		u64 bytes, packets;
453 		unsigned int start;
454 
455 		ring = READ_ONCE(vsi->tx_rings[i]);
456 		if (!ring)
457 			continue;
458 		i40e_get_netdev_stats_struct_tx(ring, stats);
459 
460 		if (i40e_enabled_xdp_vsi(vsi)) {
461 			ring = READ_ONCE(vsi->xdp_rings[i]);
462 			if (!ring)
463 				continue;
464 			i40e_get_netdev_stats_struct_tx(ring, stats);
465 		}
466 
467 		ring = READ_ONCE(vsi->rx_rings[i]);
468 		if (!ring)
469 			continue;
470 		do {
471 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
472 			packets = ring->stats.packets;
473 			bytes   = ring->stats.bytes;
474 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
475 
476 		stats->rx_packets += packets;
477 		stats->rx_bytes   += bytes;
478 
479 	}
480 	rcu_read_unlock();
481 
482 	/* following stats updated by i40e_watchdog_subtask() */
483 	stats->multicast	= vsi_stats->multicast;
484 	stats->tx_errors	= vsi_stats->tx_errors;
485 	stats->tx_dropped	= vsi_stats->tx_dropped;
486 	stats->rx_errors	= vsi_stats->rx_errors;
487 	stats->rx_dropped	= vsi_stats->rx_dropped;
488 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
489 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
490 }
491 
492 /**
493  * i40e_vsi_reset_stats - Resets all stats of the given vsi
494  * @vsi: the VSI to have its stats reset
495  **/
496 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
497 {
498 	struct rtnl_link_stats64 *ns;
499 	int i;
500 
501 	if (!vsi)
502 		return;
503 
504 	ns = i40e_get_vsi_stats_struct(vsi);
505 	memset(ns, 0, sizeof(*ns));
506 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
507 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
508 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
509 	if (vsi->rx_rings && vsi->rx_rings[0]) {
510 		for (i = 0; i < vsi->num_queue_pairs; i++) {
511 			memset(&vsi->rx_rings[i]->stats, 0,
512 			       sizeof(vsi->rx_rings[i]->stats));
513 			memset(&vsi->rx_rings[i]->rx_stats, 0,
514 			       sizeof(vsi->rx_rings[i]->rx_stats));
515 			memset(&vsi->tx_rings[i]->stats, 0,
516 			       sizeof(vsi->tx_rings[i]->stats));
517 			memset(&vsi->tx_rings[i]->tx_stats, 0,
518 			       sizeof(vsi->tx_rings[i]->tx_stats));
519 		}
520 	}
521 	vsi->stat_offsets_loaded = false;
522 }
523 
524 /**
525  * i40e_pf_reset_stats - Reset all of the stats for the given PF
526  * @pf: the PF to be reset
527  **/
528 void i40e_pf_reset_stats(struct i40e_pf *pf)
529 {
530 	int i;
531 
532 	memset(&pf->stats, 0, sizeof(pf->stats));
533 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
534 	pf->stat_offsets_loaded = false;
535 
536 	for (i = 0; i < I40E_MAX_VEB; i++) {
537 		if (pf->veb[i]) {
538 			memset(&pf->veb[i]->stats, 0,
539 			       sizeof(pf->veb[i]->stats));
540 			memset(&pf->veb[i]->stats_offsets, 0,
541 			       sizeof(pf->veb[i]->stats_offsets));
542 			memset(&pf->veb[i]->tc_stats, 0,
543 			       sizeof(pf->veb[i]->tc_stats));
544 			memset(&pf->veb[i]->tc_stats_offsets, 0,
545 			       sizeof(pf->veb[i]->tc_stats_offsets));
546 			pf->veb[i]->stat_offsets_loaded = false;
547 		}
548 	}
549 	pf->hw_csum_rx_error = 0;
550 }
551 
552 /**
553  * i40e_stat_update48 - read and update a 48 bit stat from the chip
554  * @hw: ptr to the hardware info
555  * @hireg: the high 32 bit reg to read
556  * @loreg: the low 32 bit reg to read
557  * @offset_loaded: has the initial offset been loaded yet
558  * @offset: ptr to current offset value
559  * @stat: ptr to the stat
560  *
561  * Since the device stats are not reset at PFReset, they likely will not
562  * be zeroed when the driver starts.  We'll save the first values read
563  * and use them as offsets to be subtracted from the raw values in order
564  * to report stats that count from zero.  In the process, we also manage
565  * the potential roll-over.
566  **/
567 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
568 			       bool offset_loaded, u64 *offset, u64 *stat)
569 {
570 	u64 new_data;
571 
572 	if (hw->device_id == I40E_DEV_ID_QEMU) {
573 		new_data = rd32(hw, loreg);
574 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
575 	} else {
576 		new_data = rd64(hw, loreg);
577 	}
578 	if (!offset_loaded)
579 		*offset = new_data;
580 	if (likely(new_data >= *offset))
581 		*stat = new_data - *offset;
582 	else
583 		*stat = (new_data + BIT_ULL(48)) - *offset;
584 	*stat &= 0xFFFFFFFFFFFFULL;
585 }
586 
587 /**
588  * i40e_stat_update32 - read and update a 32 bit stat from the chip
589  * @hw: ptr to the hardware info
590  * @reg: the hw reg to read
591  * @offset_loaded: has the initial offset been loaded yet
592  * @offset: ptr to current offset value
593  * @stat: ptr to the stat
594  **/
595 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
596 			       bool offset_loaded, u64 *offset, u64 *stat)
597 {
598 	u32 new_data;
599 
600 	new_data = rd32(hw, reg);
601 	if (!offset_loaded)
602 		*offset = new_data;
603 	if (likely(new_data >= *offset))
604 		*stat = (u32)(new_data - *offset);
605 	else
606 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
607 }
608 
609 /**
610  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
611  * @hw: ptr to the hardware info
612  * @reg: the hw reg to read and clear
613  * @stat: ptr to the stat
614  **/
615 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
616 {
617 	u32 new_data = rd32(hw, reg);
618 
619 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
620 	*stat += new_data;
621 }
622 
623 /**
624  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
625  * @vsi: the VSI to be updated
626  **/
627 void i40e_update_eth_stats(struct i40e_vsi *vsi)
628 {
629 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
630 	struct i40e_pf *pf = vsi->back;
631 	struct i40e_hw *hw = &pf->hw;
632 	struct i40e_eth_stats *oes;
633 	struct i40e_eth_stats *es;     /* device's eth stats */
634 
635 	es = &vsi->eth_stats;
636 	oes = &vsi->eth_stats_offsets;
637 
638 	/* Gather up the stats that the hw collects */
639 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
640 			   vsi->stat_offsets_loaded,
641 			   &oes->tx_errors, &es->tx_errors);
642 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
643 			   vsi->stat_offsets_loaded,
644 			   &oes->rx_discards, &es->rx_discards);
645 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
646 			   vsi->stat_offsets_loaded,
647 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
648 
649 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
650 			   I40E_GLV_GORCL(stat_idx),
651 			   vsi->stat_offsets_loaded,
652 			   &oes->rx_bytes, &es->rx_bytes);
653 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
654 			   I40E_GLV_UPRCL(stat_idx),
655 			   vsi->stat_offsets_loaded,
656 			   &oes->rx_unicast, &es->rx_unicast);
657 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
658 			   I40E_GLV_MPRCL(stat_idx),
659 			   vsi->stat_offsets_loaded,
660 			   &oes->rx_multicast, &es->rx_multicast);
661 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
662 			   I40E_GLV_BPRCL(stat_idx),
663 			   vsi->stat_offsets_loaded,
664 			   &oes->rx_broadcast, &es->rx_broadcast);
665 
666 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
667 			   I40E_GLV_GOTCL(stat_idx),
668 			   vsi->stat_offsets_loaded,
669 			   &oes->tx_bytes, &es->tx_bytes);
670 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
671 			   I40E_GLV_UPTCL(stat_idx),
672 			   vsi->stat_offsets_loaded,
673 			   &oes->tx_unicast, &es->tx_unicast);
674 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
675 			   I40E_GLV_MPTCL(stat_idx),
676 			   vsi->stat_offsets_loaded,
677 			   &oes->tx_multicast, &es->tx_multicast);
678 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
679 			   I40E_GLV_BPTCL(stat_idx),
680 			   vsi->stat_offsets_loaded,
681 			   &oes->tx_broadcast, &es->tx_broadcast);
682 	vsi->stat_offsets_loaded = true;
683 }
684 
685 /**
686  * i40e_update_veb_stats - Update Switch component statistics
687  * @veb: the VEB being updated
688  **/
689 void i40e_update_veb_stats(struct i40e_veb *veb)
690 {
691 	struct i40e_pf *pf = veb->pf;
692 	struct i40e_hw *hw = &pf->hw;
693 	struct i40e_eth_stats *oes;
694 	struct i40e_eth_stats *es;     /* device's eth stats */
695 	struct i40e_veb_tc_stats *veb_oes;
696 	struct i40e_veb_tc_stats *veb_es;
697 	int i, idx = 0;
698 
699 	idx = veb->stats_idx;
700 	es = &veb->stats;
701 	oes = &veb->stats_offsets;
702 	veb_es = &veb->tc_stats;
703 	veb_oes = &veb->tc_stats_offsets;
704 
705 	/* Gather up the stats that the hw collects */
706 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
707 			   veb->stat_offsets_loaded,
708 			   &oes->tx_discards, &es->tx_discards);
709 	if (hw->revision_id > 0)
710 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
711 				   veb->stat_offsets_loaded,
712 				   &oes->rx_unknown_protocol,
713 				   &es->rx_unknown_protocol);
714 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
715 			   veb->stat_offsets_loaded,
716 			   &oes->rx_bytes, &es->rx_bytes);
717 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
718 			   veb->stat_offsets_loaded,
719 			   &oes->rx_unicast, &es->rx_unicast);
720 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
721 			   veb->stat_offsets_loaded,
722 			   &oes->rx_multicast, &es->rx_multicast);
723 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
724 			   veb->stat_offsets_loaded,
725 			   &oes->rx_broadcast, &es->rx_broadcast);
726 
727 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
728 			   veb->stat_offsets_loaded,
729 			   &oes->tx_bytes, &es->tx_bytes);
730 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
731 			   veb->stat_offsets_loaded,
732 			   &oes->tx_unicast, &es->tx_unicast);
733 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
734 			   veb->stat_offsets_loaded,
735 			   &oes->tx_multicast, &es->tx_multicast);
736 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
737 			   veb->stat_offsets_loaded,
738 			   &oes->tx_broadcast, &es->tx_broadcast);
739 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
740 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
741 				   I40E_GLVEBTC_RPCL(i, idx),
742 				   veb->stat_offsets_loaded,
743 				   &veb_oes->tc_rx_packets[i],
744 				   &veb_es->tc_rx_packets[i]);
745 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
746 				   I40E_GLVEBTC_RBCL(i, idx),
747 				   veb->stat_offsets_loaded,
748 				   &veb_oes->tc_rx_bytes[i],
749 				   &veb_es->tc_rx_bytes[i]);
750 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
751 				   I40E_GLVEBTC_TPCL(i, idx),
752 				   veb->stat_offsets_loaded,
753 				   &veb_oes->tc_tx_packets[i],
754 				   &veb_es->tc_tx_packets[i]);
755 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
756 				   I40E_GLVEBTC_TBCL(i, idx),
757 				   veb->stat_offsets_loaded,
758 				   &veb_oes->tc_tx_bytes[i],
759 				   &veb_es->tc_tx_bytes[i]);
760 	}
761 	veb->stat_offsets_loaded = true;
762 }
763 
764 /**
765  * i40e_update_vsi_stats - Update the vsi statistics counters.
766  * @vsi: the VSI to be updated
767  *
768  * There are a few instances where we store the same stat in a
769  * couple of different structs.  This is partly because we have
770  * the netdev stats that need to be filled out, which is slightly
771  * different from the "eth_stats" defined by the chip and used in
772  * VF communications.  We sort it out here.
773  **/
774 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
775 {
776 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
777 	struct i40e_pf *pf = vsi->back;
778 	struct rtnl_link_stats64 *ons;
779 	struct rtnl_link_stats64 *ns;   /* netdev stats */
780 	struct i40e_eth_stats *oes;
781 	struct i40e_eth_stats *es;     /* device's eth stats */
782 	u64 tx_restart, tx_busy;
783 	struct i40e_ring *p;
784 	u64 bytes, packets;
785 	unsigned int start;
786 	u64 tx_linearize;
787 	u64 tx_force_wb;
788 	u64 rx_p, rx_b;
789 	u64 tx_p, tx_b;
790 	u16 q;
791 
792 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
793 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
794 		return;
795 
796 	ns = i40e_get_vsi_stats_struct(vsi);
797 	ons = &vsi->net_stats_offsets;
798 	es = &vsi->eth_stats;
799 	oes = &vsi->eth_stats_offsets;
800 
801 	/* Gather up the netdev and vsi stats that the driver collects
802 	 * on the fly during packet processing
803 	 */
804 	rx_b = rx_p = 0;
805 	tx_b = tx_p = 0;
806 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
807 	rx_page = 0;
808 	rx_buf = 0;
809 	rx_reuse = 0;
810 	rx_alloc = 0;
811 	rx_waive = 0;
812 	rx_busy = 0;
813 	rcu_read_lock();
814 	for (q = 0; q < vsi->num_queue_pairs; q++) {
815 		/* locate Tx ring */
816 		p = READ_ONCE(vsi->tx_rings[q]);
817 		if (!p)
818 			continue;
819 
820 		do {
821 			start = u64_stats_fetch_begin_irq(&p->syncp);
822 			packets = p->stats.packets;
823 			bytes = p->stats.bytes;
824 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
825 		tx_b += bytes;
826 		tx_p += packets;
827 		tx_restart += p->tx_stats.restart_queue;
828 		tx_busy += p->tx_stats.tx_busy;
829 		tx_linearize += p->tx_stats.tx_linearize;
830 		tx_force_wb += p->tx_stats.tx_force_wb;
831 
832 		/* locate Rx ring */
833 		p = READ_ONCE(vsi->rx_rings[q]);
834 		if (!p)
835 			continue;
836 
837 		do {
838 			start = u64_stats_fetch_begin_irq(&p->syncp);
839 			packets = p->stats.packets;
840 			bytes = p->stats.bytes;
841 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
842 		rx_b += bytes;
843 		rx_p += packets;
844 		rx_buf += p->rx_stats.alloc_buff_failed;
845 		rx_page += p->rx_stats.alloc_page_failed;
846 		rx_reuse += p->rx_stats.page_reuse_count;
847 		rx_alloc += p->rx_stats.page_alloc_count;
848 		rx_waive += p->rx_stats.page_waive_count;
849 		rx_busy += p->rx_stats.page_busy_count;
850 
851 		if (i40e_enabled_xdp_vsi(vsi)) {
852 			/* locate XDP ring */
853 			p = READ_ONCE(vsi->xdp_rings[q]);
854 			if (!p)
855 				continue;
856 
857 			do {
858 				start = u64_stats_fetch_begin_irq(&p->syncp);
859 				packets = p->stats.packets;
860 				bytes = p->stats.bytes;
861 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
862 			tx_b += bytes;
863 			tx_p += packets;
864 			tx_restart += p->tx_stats.restart_queue;
865 			tx_busy += p->tx_stats.tx_busy;
866 			tx_linearize += p->tx_stats.tx_linearize;
867 			tx_force_wb += p->tx_stats.tx_force_wb;
868 		}
869 	}
870 	rcu_read_unlock();
871 	vsi->tx_restart = tx_restart;
872 	vsi->tx_busy = tx_busy;
873 	vsi->tx_linearize = tx_linearize;
874 	vsi->tx_force_wb = tx_force_wb;
875 	vsi->rx_page_failed = rx_page;
876 	vsi->rx_buf_failed = rx_buf;
877 	vsi->rx_page_reuse = rx_reuse;
878 	vsi->rx_page_alloc = rx_alloc;
879 	vsi->rx_page_waive = rx_waive;
880 	vsi->rx_page_busy = rx_busy;
881 
882 	ns->rx_packets = rx_p;
883 	ns->rx_bytes = rx_b;
884 	ns->tx_packets = tx_p;
885 	ns->tx_bytes = tx_b;
886 
887 	/* update netdev stats from eth stats */
888 	i40e_update_eth_stats(vsi);
889 	ons->tx_errors = oes->tx_errors;
890 	ns->tx_errors = es->tx_errors;
891 	ons->multicast = oes->rx_multicast;
892 	ns->multicast = es->rx_multicast;
893 	ons->rx_dropped = oes->rx_discards;
894 	ns->rx_dropped = es->rx_discards;
895 	ons->tx_dropped = oes->tx_discards;
896 	ns->tx_dropped = es->tx_discards;
897 
898 	/* pull in a couple PF stats if this is the main vsi */
899 	if (vsi == pf->vsi[pf->lan_vsi]) {
900 		ns->rx_crc_errors = pf->stats.crc_errors;
901 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
902 		ns->rx_length_errors = pf->stats.rx_length_errors;
903 	}
904 }
905 
906 /**
907  * i40e_update_pf_stats - Update the PF statistics counters.
908  * @pf: the PF to be updated
909  **/
910 static void i40e_update_pf_stats(struct i40e_pf *pf)
911 {
912 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
913 	struct i40e_hw_port_stats *nsd = &pf->stats;
914 	struct i40e_hw *hw = &pf->hw;
915 	u32 val;
916 	int i;
917 
918 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
919 			   I40E_GLPRT_GORCL(hw->port),
920 			   pf->stat_offsets_loaded,
921 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
922 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
923 			   I40E_GLPRT_GOTCL(hw->port),
924 			   pf->stat_offsets_loaded,
925 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
926 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
927 			   pf->stat_offsets_loaded,
928 			   &osd->eth.rx_discards,
929 			   &nsd->eth.rx_discards);
930 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
931 			   I40E_GLPRT_UPRCL(hw->port),
932 			   pf->stat_offsets_loaded,
933 			   &osd->eth.rx_unicast,
934 			   &nsd->eth.rx_unicast);
935 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
936 			   I40E_GLPRT_MPRCL(hw->port),
937 			   pf->stat_offsets_loaded,
938 			   &osd->eth.rx_multicast,
939 			   &nsd->eth.rx_multicast);
940 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
941 			   I40E_GLPRT_BPRCL(hw->port),
942 			   pf->stat_offsets_loaded,
943 			   &osd->eth.rx_broadcast,
944 			   &nsd->eth.rx_broadcast);
945 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
946 			   I40E_GLPRT_UPTCL(hw->port),
947 			   pf->stat_offsets_loaded,
948 			   &osd->eth.tx_unicast,
949 			   &nsd->eth.tx_unicast);
950 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
951 			   I40E_GLPRT_MPTCL(hw->port),
952 			   pf->stat_offsets_loaded,
953 			   &osd->eth.tx_multicast,
954 			   &nsd->eth.tx_multicast);
955 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
956 			   I40E_GLPRT_BPTCL(hw->port),
957 			   pf->stat_offsets_loaded,
958 			   &osd->eth.tx_broadcast,
959 			   &nsd->eth.tx_broadcast);
960 
961 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
962 			   pf->stat_offsets_loaded,
963 			   &osd->tx_dropped_link_down,
964 			   &nsd->tx_dropped_link_down);
965 
966 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
967 			   pf->stat_offsets_loaded,
968 			   &osd->crc_errors, &nsd->crc_errors);
969 
970 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
971 			   pf->stat_offsets_loaded,
972 			   &osd->illegal_bytes, &nsd->illegal_bytes);
973 
974 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
975 			   pf->stat_offsets_loaded,
976 			   &osd->mac_local_faults,
977 			   &nsd->mac_local_faults);
978 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
979 			   pf->stat_offsets_loaded,
980 			   &osd->mac_remote_faults,
981 			   &nsd->mac_remote_faults);
982 
983 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
984 			   pf->stat_offsets_loaded,
985 			   &osd->rx_length_errors,
986 			   &nsd->rx_length_errors);
987 
988 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
989 			   pf->stat_offsets_loaded,
990 			   &osd->link_xon_rx, &nsd->link_xon_rx);
991 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
992 			   pf->stat_offsets_loaded,
993 			   &osd->link_xon_tx, &nsd->link_xon_tx);
994 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
995 			   pf->stat_offsets_loaded,
996 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
997 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
998 			   pf->stat_offsets_loaded,
999 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1000 
1001 	for (i = 0; i < 8; i++) {
1002 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1003 				   pf->stat_offsets_loaded,
1004 				   &osd->priority_xoff_rx[i],
1005 				   &nsd->priority_xoff_rx[i]);
1006 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1007 				   pf->stat_offsets_loaded,
1008 				   &osd->priority_xon_rx[i],
1009 				   &nsd->priority_xon_rx[i]);
1010 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1011 				   pf->stat_offsets_loaded,
1012 				   &osd->priority_xon_tx[i],
1013 				   &nsd->priority_xon_tx[i]);
1014 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1015 				   pf->stat_offsets_loaded,
1016 				   &osd->priority_xoff_tx[i],
1017 				   &nsd->priority_xoff_tx[i]);
1018 		i40e_stat_update32(hw,
1019 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1020 				   pf->stat_offsets_loaded,
1021 				   &osd->priority_xon_2_xoff[i],
1022 				   &nsd->priority_xon_2_xoff[i]);
1023 	}
1024 
1025 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1026 			   I40E_GLPRT_PRC64L(hw->port),
1027 			   pf->stat_offsets_loaded,
1028 			   &osd->rx_size_64, &nsd->rx_size_64);
1029 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1030 			   I40E_GLPRT_PRC127L(hw->port),
1031 			   pf->stat_offsets_loaded,
1032 			   &osd->rx_size_127, &nsd->rx_size_127);
1033 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1034 			   I40E_GLPRT_PRC255L(hw->port),
1035 			   pf->stat_offsets_loaded,
1036 			   &osd->rx_size_255, &nsd->rx_size_255);
1037 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1038 			   I40E_GLPRT_PRC511L(hw->port),
1039 			   pf->stat_offsets_loaded,
1040 			   &osd->rx_size_511, &nsd->rx_size_511);
1041 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1042 			   I40E_GLPRT_PRC1023L(hw->port),
1043 			   pf->stat_offsets_loaded,
1044 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1045 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1046 			   I40E_GLPRT_PRC1522L(hw->port),
1047 			   pf->stat_offsets_loaded,
1048 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1049 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1050 			   I40E_GLPRT_PRC9522L(hw->port),
1051 			   pf->stat_offsets_loaded,
1052 			   &osd->rx_size_big, &nsd->rx_size_big);
1053 
1054 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1055 			   I40E_GLPRT_PTC64L(hw->port),
1056 			   pf->stat_offsets_loaded,
1057 			   &osd->tx_size_64, &nsd->tx_size_64);
1058 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1059 			   I40E_GLPRT_PTC127L(hw->port),
1060 			   pf->stat_offsets_loaded,
1061 			   &osd->tx_size_127, &nsd->tx_size_127);
1062 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1063 			   I40E_GLPRT_PTC255L(hw->port),
1064 			   pf->stat_offsets_loaded,
1065 			   &osd->tx_size_255, &nsd->tx_size_255);
1066 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1067 			   I40E_GLPRT_PTC511L(hw->port),
1068 			   pf->stat_offsets_loaded,
1069 			   &osd->tx_size_511, &nsd->tx_size_511);
1070 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1071 			   I40E_GLPRT_PTC1023L(hw->port),
1072 			   pf->stat_offsets_loaded,
1073 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1074 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1075 			   I40E_GLPRT_PTC1522L(hw->port),
1076 			   pf->stat_offsets_loaded,
1077 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1078 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1079 			   I40E_GLPRT_PTC9522L(hw->port),
1080 			   pf->stat_offsets_loaded,
1081 			   &osd->tx_size_big, &nsd->tx_size_big);
1082 
1083 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1084 			   pf->stat_offsets_loaded,
1085 			   &osd->rx_undersize, &nsd->rx_undersize);
1086 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1087 			   pf->stat_offsets_loaded,
1088 			   &osd->rx_fragments, &nsd->rx_fragments);
1089 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1090 			   pf->stat_offsets_loaded,
1091 			   &osd->rx_oversize, &nsd->rx_oversize);
1092 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1093 			   pf->stat_offsets_loaded,
1094 			   &osd->rx_jabber, &nsd->rx_jabber);
1095 
1096 	/* FDIR stats */
1097 	i40e_stat_update_and_clear32(hw,
1098 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1099 			&nsd->fd_atr_match);
1100 	i40e_stat_update_and_clear32(hw,
1101 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1102 			&nsd->fd_sb_match);
1103 	i40e_stat_update_and_clear32(hw,
1104 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1105 			&nsd->fd_atr_tunnel_match);
1106 
1107 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1108 	nsd->tx_lpi_status =
1109 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1110 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1111 	nsd->rx_lpi_status =
1112 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1113 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1114 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1115 			   pf->stat_offsets_loaded,
1116 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1117 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1118 			   pf->stat_offsets_loaded,
1119 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1120 
1121 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1122 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1123 		nsd->fd_sb_status = true;
1124 	else
1125 		nsd->fd_sb_status = false;
1126 
1127 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1128 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1129 		nsd->fd_atr_status = true;
1130 	else
1131 		nsd->fd_atr_status = false;
1132 
1133 	pf->stat_offsets_loaded = true;
1134 }
1135 
1136 /**
1137  * i40e_update_stats - Update the various statistics counters.
1138  * @vsi: the VSI to be updated
1139  *
1140  * Update the various stats for this VSI and its related entities.
1141  **/
1142 void i40e_update_stats(struct i40e_vsi *vsi)
1143 {
1144 	struct i40e_pf *pf = vsi->back;
1145 
1146 	if (vsi == pf->vsi[pf->lan_vsi])
1147 		i40e_update_pf_stats(pf);
1148 
1149 	i40e_update_vsi_stats(vsi);
1150 }
1151 
1152 /**
1153  * i40e_count_filters - counts VSI mac filters
1154  * @vsi: the VSI to be searched
1155  *
1156  * Returns count of mac filters
1157  **/
1158 int i40e_count_filters(struct i40e_vsi *vsi)
1159 {
1160 	struct i40e_mac_filter *f;
1161 	struct hlist_node *h;
1162 	int bkt;
1163 	int cnt = 0;
1164 
1165 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1166 		++cnt;
1167 
1168 	return cnt;
1169 }
1170 
1171 /**
1172  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1173  * @vsi: the VSI to be searched
1174  * @macaddr: the MAC address
1175  * @vlan: the vlan
1176  *
1177  * Returns ptr to the filter object or NULL
1178  **/
1179 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1180 						const u8 *macaddr, s16 vlan)
1181 {
1182 	struct i40e_mac_filter *f;
1183 	u64 key;
1184 
1185 	if (!vsi || !macaddr)
1186 		return NULL;
1187 
1188 	key = i40e_addr_to_hkey(macaddr);
1189 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1190 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1191 		    (vlan == f->vlan))
1192 			return f;
1193 	}
1194 	return NULL;
1195 }
1196 
1197 /**
1198  * i40e_find_mac - Find a mac addr in the macvlan filters list
1199  * @vsi: the VSI to be searched
1200  * @macaddr: the MAC address we are searching for
1201  *
1202  * Returns the first filter with the provided MAC address or NULL if
1203  * MAC address was not found
1204  **/
1205 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1206 {
1207 	struct i40e_mac_filter *f;
1208 	u64 key;
1209 
1210 	if (!vsi || !macaddr)
1211 		return NULL;
1212 
1213 	key = i40e_addr_to_hkey(macaddr);
1214 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1215 		if ((ether_addr_equal(macaddr, f->macaddr)))
1216 			return f;
1217 	}
1218 	return NULL;
1219 }
1220 
1221 /**
1222  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1223  * @vsi: the VSI to be searched
1224  *
1225  * Returns true if VSI is in vlan mode or false otherwise
1226  **/
1227 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1228 {
1229 	/* If we have a PVID, always operate in VLAN mode */
1230 	if (vsi->info.pvid)
1231 		return true;
1232 
1233 	/* We need to operate in VLAN mode whenever we have any filters with
1234 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1235 	 * time, incurring search cost repeatedly. However, we can notice two
1236 	 * things:
1237 	 *
1238 	 * 1) the only place where we can gain a VLAN filter is in
1239 	 *    i40e_add_filter.
1240 	 *
1241 	 * 2) the only place where filters are actually removed is in
1242 	 *    i40e_sync_filters_subtask.
1243 	 *
1244 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1245 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1246 	 * we have to perform the full search after deleting filters in
1247 	 * i40e_sync_filters_subtask, but we already have to search
1248 	 * filters here and can perform the check at the same time. This
1249 	 * results in avoiding embedding a loop for VLAN mode inside another
1250 	 * loop over all the filters, and should maintain correctness as noted
1251 	 * above.
1252 	 */
1253 	return vsi->has_vlan_filter;
1254 }
1255 
1256 /**
1257  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1258  * @vsi: the VSI to configure
1259  * @tmp_add_list: list of filters ready to be added
1260  * @tmp_del_list: list of filters ready to be deleted
1261  * @vlan_filters: the number of active VLAN filters
1262  *
1263  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1264  * behave as expected. If we have any active VLAN filters remaining or about
1265  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1266  * so that they only match against untagged traffic. If we no longer have any
1267  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1268  * so that they match against both tagged and untagged traffic. In this way,
1269  * we ensure that we correctly receive the desired traffic. This ensures that
1270  * when we have an active VLAN we will receive only untagged traffic and
1271  * traffic matching active VLANs. If we have no active VLANs then we will
1272  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1273  *
1274  * Finally, in a similar fashion, this function also corrects filters when
1275  * there is an active PVID assigned to this VSI.
1276  *
1277  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1278  *
1279  * This function is only expected to be called from within
1280  * i40e_sync_vsi_filters.
1281  *
1282  * NOTE: This function expects to be called while under the
1283  * mac_filter_hash_lock
1284  */
1285 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1286 					 struct hlist_head *tmp_add_list,
1287 					 struct hlist_head *tmp_del_list,
1288 					 int vlan_filters)
1289 {
1290 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1291 	struct i40e_mac_filter *f, *add_head;
1292 	struct i40e_new_mac_filter *new;
1293 	struct hlist_node *h;
1294 	int bkt, new_vlan;
1295 
1296 	/* To determine if a particular filter needs to be replaced we
1297 	 * have the three following conditions:
1298 	 *
1299 	 * a) if we have a PVID assigned, then all filters which are
1300 	 *    not marked as VLAN=PVID must be replaced with filters that
1301 	 *    are.
1302 	 * b) otherwise, if we have any active VLANS, all filters
1303 	 *    which are marked as VLAN=-1 must be replaced with
1304 	 *    filters marked as VLAN=0
1305 	 * c) finally, if we do not have any active VLANS, all filters
1306 	 *    which are marked as VLAN=0 must be replaced with filters
1307 	 *    marked as VLAN=-1
1308 	 */
1309 
1310 	/* Update the filters about to be added in place */
1311 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1312 		if (pvid && new->f->vlan != pvid)
1313 			new->f->vlan = pvid;
1314 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1315 			new->f->vlan = 0;
1316 		else if (!vlan_filters && new->f->vlan == 0)
1317 			new->f->vlan = I40E_VLAN_ANY;
1318 	}
1319 
1320 	/* Update the remaining active filters */
1321 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1322 		/* Combine the checks for whether a filter needs to be changed
1323 		 * and then determine the new VLAN inside the if block, in
1324 		 * order to avoid duplicating code for adding the new filter
1325 		 * then deleting the old filter.
1326 		 */
1327 		if ((pvid && f->vlan != pvid) ||
1328 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1329 		    (!vlan_filters && f->vlan == 0)) {
1330 			/* Determine the new vlan we will be adding */
1331 			if (pvid)
1332 				new_vlan = pvid;
1333 			else if (vlan_filters)
1334 				new_vlan = 0;
1335 			else
1336 				new_vlan = I40E_VLAN_ANY;
1337 
1338 			/* Create the new filter */
1339 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1340 			if (!add_head)
1341 				return -ENOMEM;
1342 
1343 			/* Create a temporary i40e_new_mac_filter */
1344 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1345 			if (!new)
1346 				return -ENOMEM;
1347 
1348 			new->f = add_head;
1349 			new->state = add_head->state;
1350 
1351 			/* Add the new filter to the tmp list */
1352 			hlist_add_head(&new->hlist, tmp_add_list);
1353 
1354 			/* Put the original filter into the delete list */
1355 			f->state = I40E_FILTER_REMOVE;
1356 			hash_del(&f->hlist);
1357 			hlist_add_head(&f->hlist, tmp_del_list);
1358 		}
1359 	}
1360 
1361 	vsi->has_vlan_filter = !!vlan_filters;
1362 
1363 	return 0;
1364 }
1365 
1366 /**
1367  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1368  * @vsi: the PF Main VSI - inappropriate for any other VSI
1369  * @macaddr: the MAC address
1370  *
1371  * Remove whatever filter the firmware set up so the driver can manage
1372  * its own filtering intelligently.
1373  **/
1374 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1375 {
1376 	struct i40e_aqc_remove_macvlan_element_data element;
1377 	struct i40e_pf *pf = vsi->back;
1378 
1379 	/* Only appropriate for the PF main VSI */
1380 	if (vsi->type != I40E_VSI_MAIN)
1381 		return;
1382 
1383 	memset(&element, 0, sizeof(element));
1384 	ether_addr_copy(element.mac_addr, macaddr);
1385 	element.vlan_tag = 0;
1386 	/* Ignore error returns, some firmware does it this way... */
1387 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1388 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1389 
1390 	memset(&element, 0, sizeof(element));
1391 	ether_addr_copy(element.mac_addr, macaddr);
1392 	element.vlan_tag = 0;
1393 	/* ...and some firmware does it this way. */
1394 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1395 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1396 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1397 }
1398 
1399 /**
1400  * i40e_add_filter - Add a mac/vlan filter to the VSI
1401  * @vsi: the VSI to be searched
1402  * @macaddr: the MAC address
1403  * @vlan: the vlan
1404  *
1405  * Returns ptr to the filter object or NULL when no memory available.
1406  *
1407  * NOTE: This function is expected to be called with mac_filter_hash_lock
1408  * being held.
1409  **/
1410 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1411 					const u8 *macaddr, s16 vlan)
1412 {
1413 	struct i40e_mac_filter *f;
1414 	u64 key;
1415 
1416 	if (!vsi || !macaddr)
1417 		return NULL;
1418 
1419 	f = i40e_find_filter(vsi, macaddr, vlan);
1420 	if (!f) {
1421 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1422 		if (!f)
1423 			return NULL;
1424 
1425 		/* Update the boolean indicating if we need to function in
1426 		 * VLAN mode.
1427 		 */
1428 		if (vlan >= 0)
1429 			vsi->has_vlan_filter = true;
1430 
1431 		ether_addr_copy(f->macaddr, macaddr);
1432 		f->vlan = vlan;
1433 		f->state = I40E_FILTER_NEW;
1434 		INIT_HLIST_NODE(&f->hlist);
1435 
1436 		key = i40e_addr_to_hkey(macaddr);
1437 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1438 
1439 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1440 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1441 	}
1442 
1443 	/* If we're asked to add a filter that has been marked for removal, it
1444 	 * is safe to simply restore it to active state. __i40e_del_filter
1445 	 * will have simply deleted any filters which were previously marked
1446 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1447 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1448 	 * task, just restore this filter to the ACTIVE state so that the
1449 	 * sync task leaves it in place
1450 	 */
1451 	if (f->state == I40E_FILTER_REMOVE)
1452 		f->state = I40E_FILTER_ACTIVE;
1453 
1454 	return f;
1455 }
1456 
1457 /**
1458  * __i40e_del_filter - Remove a specific filter from the VSI
1459  * @vsi: VSI to remove from
1460  * @f: the filter to remove from the list
1461  *
1462  * This function should be called instead of i40e_del_filter only if you know
1463  * the exact filter you will remove already, such as via i40e_find_filter or
1464  * i40e_find_mac.
1465  *
1466  * NOTE: This function is expected to be called with mac_filter_hash_lock
1467  * being held.
1468  * ANOTHER NOTE: This function MUST be called from within the context of
1469  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1470  * instead of list_for_each_entry().
1471  **/
1472 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1473 {
1474 	if (!f)
1475 		return;
1476 
1477 	/* If the filter was never added to firmware then we can just delete it
1478 	 * directly and we don't want to set the status to remove or else an
1479 	 * admin queue command will unnecessarily fire.
1480 	 */
1481 	if ((f->state == I40E_FILTER_FAILED) ||
1482 	    (f->state == I40E_FILTER_NEW)) {
1483 		hash_del(&f->hlist);
1484 		kfree(f);
1485 	} else {
1486 		f->state = I40E_FILTER_REMOVE;
1487 	}
1488 
1489 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1490 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1491 }
1492 
1493 /**
1494  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1495  * @vsi: the VSI to be searched
1496  * @macaddr: the MAC address
1497  * @vlan: the VLAN
1498  *
1499  * NOTE: This function is expected to be called with mac_filter_hash_lock
1500  * being held.
1501  * ANOTHER NOTE: This function MUST be called from within the context of
1502  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1503  * instead of list_for_each_entry().
1504  **/
1505 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1506 {
1507 	struct i40e_mac_filter *f;
1508 
1509 	if (!vsi || !macaddr)
1510 		return;
1511 
1512 	f = i40e_find_filter(vsi, macaddr, vlan);
1513 	__i40e_del_filter(vsi, f);
1514 }
1515 
1516 /**
1517  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1518  * @vsi: the VSI to be searched
1519  * @macaddr: the mac address to be filtered
1520  *
1521  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1522  * go through all the macvlan filters and add a macvlan filter for each
1523  * unique vlan that already exists. If a PVID has been assigned, instead only
1524  * add the macaddr to that VLAN.
1525  *
1526  * Returns last filter added on success, else NULL
1527  **/
1528 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1529 					    const u8 *macaddr)
1530 {
1531 	struct i40e_mac_filter *f, *add = NULL;
1532 	struct hlist_node *h;
1533 	int bkt;
1534 
1535 	if (vsi->info.pvid)
1536 		return i40e_add_filter(vsi, macaddr,
1537 				       le16_to_cpu(vsi->info.pvid));
1538 
1539 	if (!i40e_is_vsi_in_vlan(vsi))
1540 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1541 
1542 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1543 		if (f->state == I40E_FILTER_REMOVE)
1544 			continue;
1545 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1546 		if (!add)
1547 			return NULL;
1548 	}
1549 
1550 	return add;
1551 }
1552 
1553 /**
1554  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1555  * @vsi: the VSI to be searched
1556  * @macaddr: the mac address to be removed
1557  *
1558  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1559  * associated with.
1560  *
1561  * Returns 0 for success, or error
1562  **/
1563 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1564 {
1565 	struct i40e_mac_filter *f;
1566 	struct hlist_node *h;
1567 	bool found = false;
1568 	int bkt;
1569 
1570 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1571 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1572 		if (ether_addr_equal(macaddr, f->macaddr)) {
1573 			__i40e_del_filter(vsi, f);
1574 			found = true;
1575 		}
1576 	}
1577 
1578 	if (found)
1579 		return 0;
1580 	else
1581 		return -ENOENT;
1582 }
1583 
1584 /**
1585  * i40e_set_mac - NDO callback to set mac address
1586  * @netdev: network interface device structure
1587  * @p: pointer to an address structure
1588  *
1589  * Returns 0 on success, negative on failure
1590  **/
1591 static int i40e_set_mac(struct net_device *netdev, void *p)
1592 {
1593 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1594 	struct i40e_vsi *vsi = np->vsi;
1595 	struct i40e_pf *pf = vsi->back;
1596 	struct i40e_hw *hw = &pf->hw;
1597 	struct sockaddr *addr = p;
1598 
1599 	if (!is_valid_ether_addr(addr->sa_data))
1600 		return -EADDRNOTAVAIL;
1601 
1602 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1603 		netdev_info(netdev, "already using mac address %pM\n",
1604 			    addr->sa_data);
1605 		return 0;
1606 	}
1607 
1608 	if (test_bit(__I40E_DOWN, pf->state) ||
1609 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1610 		return -EADDRNOTAVAIL;
1611 
1612 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1613 		netdev_info(netdev, "returning to hw mac address %pM\n",
1614 			    hw->mac.addr);
1615 	else
1616 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1617 
1618 	/* Copy the address first, so that we avoid a possible race with
1619 	 * .set_rx_mode().
1620 	 * - Remove old address from MAC filter
1621 	 * - Copy new address
1622 	 * - Add new address to MAC filter
1623 	 */
1624 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1625 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1626 	eth_hw_addr_set(netdev, addr->sa_data);
1627 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1628 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1629 
1630 	if (vsi->type == I40E_VSI_MAIN) {
1631 		i40e_status ret;
1632 
1633 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1634 						addr->sa_data, NULL);
1635 		if (ret)
1636 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1637 				    i40e_stat_str(hw, ret),
1638 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1639 	}
1640 
1641 	/* schedule our worker thread which will take care of
1642 	 * applying the new filter changes
1643 	 */
1644 	i40e_service_event_schedule(pf);
1645 	return 0;
1646 }
1647 
1648 /**
1649  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1650  * @vsi: vsi structure
1651  * @seed: RSS hash seed
1652  * @lut: pointer to lookup table of lut_size
1653  * @lut_size: size of the lookup table
1654  **/
1655 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1656 			      u8 *lut, u16 lut_size)
1657 {
1658 	struct i40e_pf *pf = vsi->back;
1659 	struct i40e_hw *hw = &pf->hw;
1660 	int ret = 0;
1661 
1662 	if (seed) {
1663 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1664 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1665 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1666 		if (ret) {
1667 			dev_info(&pf->pdev->dev,
1668 				 "Cannot set RSS key, err %s aq_err %s\n",
1669 				 i40e_stat_str(hw, ret),
1670 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1671 			return ret;
1672 		}
1673 	}
1674 	if (lut) {
1675 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1676 
1677 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1678 		if (ret) {
1679 			dev_info(&pf->pdev->dev,
1680 				 "Cannot set RSS lut, err %s aq_err %s\n",
1681 				 i40e_stat_str(hw, ret),
1682 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1683 			return ret;
1684 		}
1685 	}
1686 	return ret;
1687 }
1688 
1689 /**
1690  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1691  * @vsi: VSI structure
1692  **/
1693 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1694 {
1695 	struct i40e_pf *pf = vsi->back;
1696 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1697 	u8 *lut;
1698 	int ret;
1699 
1700 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1701 		return 0;
1702 	if (!vsi->rss_size)
1703 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1704 				      vsi->num_queue_pairs);
1705 	if (!vsi->rss_size)
1706 		return -EINVAL;
1707 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1708 	if (!lut)
1709 		return -ENOMEM;
1710 
1711 	/* Use the user configured hash keys and lookup table if there is one,
1712 	 * otherwise use default
1713 	 */
1714 	if (vsi->rss_lut_user)
1715 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1716 	else
1717 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1718 	if (vsi->rss_hkey_user)
1719 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1720 	else
1721 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1722 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1723 	kfree(lut);
1724 	return ret;
1725 }
1726 
1727 /**
1728  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1729  * @vsi: the VSI being configured,
1730  * @ctxt: VSI context structure
1731  * @enabled_tc: number of traffic classes to enable
1732  *
1733  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1734  **/
1735 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1736 					   struct i40e_vsi_context *ctxt,
1737 					   u8 enabled_tc)
1738 {
1739 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1740 	int i, override_q, pow, num_qps, ret;
1741 	u8 netdev_tc = 0, offset = 0;
1742 
1743 	if (vsi->type != I40E_VSI_MAIN)
1744 		return -EINVAL;
1745 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1746 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1747 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1748 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1749 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1750 
1751 	/* find the next higher power-of-2 of num queue pairs */
1752 	pow = ilog2(num_qps);
1753 	if (!is_power_of_2(num_qps))
1754 		pow++;
1755 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1756 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1757 
1758 	/* Setup queue offset/count for all TCs for given VSI */
1759 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1760 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1761 		/* See if the given TC is enabled for the given VSI */
1762 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1763 			offset = vsi->mqprio_qopt.qopt.offset[i];
1764 			qcount = vsi->mqprio_qopt.qopt.count[i];
1765 			if (qcount > max_qcount)
1766 				max_qcount = qcount;
1767 			vsi->tc_config.tc_info[i].qoffset = offset;
1768 			vsi->tc_config.tc_info[i].qcount = qcount;
1769 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1770 		} else {
1771 			/* TC is not enabled so set the offset to
1772 			 * default queue and allocate one queue
1773 			 * for the given TC.
1774 			 */
1775 			vsi->tc_config.tc_info[i].qoffset = 0;
1776 			vsi->tc_config.tc_info[i].qcount = 1;
1777 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1778 		}
1779 	}
1780 
1781 	/* Set actual Tx/Rx queue pairs */
1782 	vsi->num_queue_pairs = offset + qcount;
1783 
1784 	/* Setup queue TC[0].qmap for given VSI context */
1785 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1786 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1787 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1788 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1789 
1790 	/* Reconfigure RSS for main VSI with max queue count */
1791 	vsi->rss_size = max_qcount;
1792 	ret = i40e_vsi_config_rss(vsi);
1793 	if (ret) {
1794 		dev_info(&vsi->back->pdev->dev,
1795 			 "Failed to reconfig rss for num_queues (%u)\n",
1796 			 max_qcount);
1797 		return ret;
1798 	}
1799 	vsi->reconfig_rss = true;
1800 	dev_dbg(&vsi->back->pdev->dev,
1801 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1802 
1803 	/* Find queue count available for channel VSIs and starting offset
1804 	 * for channel VSIs
1805 	 */
1806 	override_q = vsi->mqprio_qopt.qopt.count[0];
1807 	if (override_q && override_q < vsi->num_queue_pairs) {
1808 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1809 		vsi->next_base_queue = override_q;
1810 	}
1811 	return 0;
1812 }
1813 
1814 /**
1815  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1816  * @vsi: the VSI being setup
1817  * @ctxt: VSI context structure
1818  * @enabled_tc: Enabled TCs bitmap
1819  * @is_add: True if called before Add VSI
1820  *
1821  * Setup VSI queue mapping for enabled traffic classes.
1822  **/
1823 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1824 				     struct i40e_vsi_context *ctxt,
1825 				     u8 enabled_tc,
1826 				     bool is_add)
1827 {
1828 	struct i40e_pf *pf = vsi->back;
1829 	u16 num_tc_qps = 0;
1830 	u16 sections = 0;
1831 	u8 netdev_tc = 0;
1832 	u16 numtc = 1;
1833 	u16 qcount;
1834 	u8 offset;
1835 	u16 qmap;
1836 	int i;
1837 
1838 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1839 	offset = 0;
1840 	/* zero out queue mapping, it will get updated on the end of the function */
1841 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
1842 
1843 	if (vsi->type == I40E_VSI_MAIN) {
1844 		/* This code helps add more queue to the VSI if we have
1845 		 * more cores than RSS can support, the higher cores will
1846 		 * be served by ATR or other filters. Furthermore, the
1847 		 * non-zero req_queue_pairs says that user requested a new
1848 		 * queue count via ethtool's set_channels, so use this
1849 		 * value for queues distribution across traffic classes
1850 		 */
1851 		if (vsi->req_queue_pairs > 0)
1852 			vsi->num_queue_pairs = vsi->req_queue_pairs;
1853 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1854 			vsi->num_queue_pairs = pf->num_lan_msix;
1855 	}
1856 
1857 	/* Number of queues per enabled TC */
1858 	if (vsi->type == I40E_VSI_MAIN ||
1859 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
1860 		num_tc_qps = vsi->num_queue_pairs;
1861 	else
1862 		num_tc_qps = vsi->alloc_queue_pairs;
1863 
1864 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1865 		/* Find numtc from enabled TC bitmap */
1866 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1867 			if (enabled_tc & BIT(i)) /* TC is enabled */
1868 				numtc++;
1869 		}
1870 		if (!numtc) {
1871 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1872 			numtc = 1;
1873 		}
1874 		num_tc_qps = num_tc_qps / numtc;
1875 		num_tc_qps = min_t(int, num_tc_qps,
1876 				   i40e_pf_get_max_q_per_tc(pf));
1877 	}
1878 
1879 	vsi->tc_config.numtc = numtc;
1880 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1881 
1882 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
1883 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1884 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
1885 
1886 	/* Setup queue offset/count for all TCs for given VSI */
1887 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1888 		/* See if the given TC is enabled for the given VSI */
1889 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1890 			/* TC is enabled */
1891 			int pow, num_qps;
1892 
1893 			switch (vsi->type) {
1894 			case I40E_VSI_MAIN:
1895 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
1896 				    I40E_FLAG_FD_ATR_ENABLED)) ||
1897 				    vsi->tc_config.enabled_tc != 1) {
1898 					qcount = min_t(int, pf->alloc_rss_size,
1899 						       num_tc_qps);
1900 					break;
1901 				}
1902 				fallthrough;
1903 			case I40E_VSI_FDIR:
1904 			case I40E_VSI_SRIOV:
1905 			case I40E_VSI_VMDQ2:
1906 			default:
1907 				qcount = num_tc_qps;
1908 				WARN_ON(i != 0);
1909 				break;
1910 			}
1911 			vsi->tc_config.tc_info[i].qoffset = offset;
1912 			vsi->tc_config.tc_info[i].qcount = qcount;
1913 
1914 			/* find the next higher power-of-2 of num queue pairs */
1915 			num_qps = qcount;
1916 			pow = 0;
1917 			while (num_qps && (BIT_ULL(pow) < qcount)) {
1918 				pow++;
1919 				num_qps >>= 1;
1920 			}
1921 
1922 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1923 			qmap =
1924 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1925 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1926 
1927 			offset += qcount;
1928 		} else {
1929 			/* TC is not enabled so set the offset to
1930 			 * default queue and allocate one queue
1931 			 * for the given TC.
1932 			 */
1933 			vsi->tc_config.tc_info[i].qoffset = 0;
1934 			vsi->tc_config.tc_info[i].qcount = 1;
1935 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1936 
1937 			qmap = 0;
1938 		}
1939 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1940 	}
1941 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
1942 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
1943 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
1944 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
1945 		vsi->num_queue_pairs = offset;
1946 
1947 	/* Scheduler section valid can only be set for ADD VSI */
1948 	if (is_add) {
1949 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1950 
1951 		ctxt->info.up_enable_bits = enabled_tc;
1952 	}
1953 	if (vsi->type == I40E_VSI_SRIOV) {
1954 		ctxt->info.mapping_flags |=
1955 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1956 		for (i = 0; i < vsi->num_queue_pairs; i++)
1957 			ctxt->info.queue_mapping[i] =
1958 					       cpu_to_le16(vsi->base_queue + i);
1959 	} else {
1960 		ctxt->info.mapping_flags |=
1961 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1962 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1963 	}
1964 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1965 }
1966 
1967 /**
1968  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1969  * @netdev: the netdevice
1970  * @addr: address to add
1971  *
1972  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1973  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1974  */
1975 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1976 {
1977 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1978 	struct i40e_vsi *vsi = np->vsi;
1979 
1980 	if (i40e_add_mac_filter(vsi, addr))
1981 		return 0;
1982 	else
1983 		return -ENOMEM;
1984 }
1985 
1986 /**
1987  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1988  * @netdev: the netdevice
1989  * @addr: address to add
1990  *
1991  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1992  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1993  */
1994 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1995 {
1996 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1997 	struct i40e_vsi *vsi = np->vsi;
1998 
1999 	/* Under some circumstances, we might receive a request to delete
2000 	 * our own device address from our uc list. Because we store the
2001 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2002 	 * such requests and not delete our device address from this list.
2003 	 */
2004 	if (ether_addr_equal(addr, netdev->dev_addr))
2005 		return 0;
2006 
2007 	i40e_del_mac_filter(vsi, addr);
2008 
2009 	return 0;
2010 }
2011 
2012 /**
2013  * i40e_set_rx_mode - NDO callback to set the netdev filters
2014  * @netdev: network interface device structure
2015  **/
2016 static void i40e_set_rx_mode(struct net_device *netdev)
2017 {
2018 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2019 	struct i40e_vsi *vsi = np->vsi;
2020 
2021 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2022 
2023 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2024 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2025 
2026 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2027 
2028 	/* check for other flag changes */
2029 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2030 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2031 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2032 	}
2033 }
2034 
2035 /**
2036  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2037  * @vsi: Pointer to VSI struct
2038  * @from: Pointer to list which contains MAC filter entries - changes to
2039  *        those entries needs to be undone.
2040  *
2041  * MAC filter entries from this list were slated for deletion.
2042  **/
2043 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2044 					 struct hlist_head *from)
2045 {
2046 	struct i40e_mac_filter *f;
2047 	struct hlist_node *h;
2048 
2049 	hlist_for_each_entry_safe(f, h, from, hlist) {
2050 		u64 key = i40e_addr_to_hkey(f->macaddr);
2051 
2052 		/* Move the element back into MAC filter list*/
2053 		hlist_del(&f->hlist);
2054 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2055 	}
2056 }
2057 
2058 /**
2059  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2060  * @vsi: Pointer to vsi struct
2061  * @from: Pointer to list which contains MAC filter entries - changes to
2062  *        those entries needs to be undone.
2063  *
2064  * MAC filter entries from this list were slated for addition.
2065  **/
2066 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2067 					 struct hlist_head *from)
2068 {
2069 	struct i40e_new_mac_filter *new;
2070 	struct hlist_node *h;
2071 
2072 	hlist_for_each_entry_safe(new, h, from, hlist) {
2073 		/* We can simply free the wrapper structure */
2074 		hlist_del(&new->hlist);
2075 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2076 		kfree(new);
2077 	}
2078 }
2079 
2080 /**
2081  * i40e_next_filter - Get the next non-broadcast filter from a list
2082  * @next: pointer to filter in list
2083  *
2084  * Returns the next non-broadcast filter in the list. Required so that we
2085  * ignore broadcast filters within the list, since these are not handled via
2086  * the normal firmware update path.
2087  */
2088 static
2089 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2090 {
2091 	hlist_for_each_entry_continue(next, hlist) {
2092 		if (!is_broadcast_ether_addr(next->f->macaddr))
2093 			return next;
2094 	}
2095 
2096 	return NULL;
2097 }
2098 
2099 /**
2100  * i40e_update_filter_state - Update filter state based on return data
2101  * from firmware
2102  * @count: Number of filters added
2103  * @add_list: return data from fw
2104  * @add_head: pointer to first filter in current batch
2105  *
2106  * MAC filter entries from list were slated to be added to device. Returns
2107  * number of successful filters. Note that 0 does NOT mean success!
2108  **/
2109 static int
2110 i40e_update_filter_state(int count,
2111 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2112 			 struct i40e_new_mac_filter *add_head)
2113 {
2114 	int retval = 0;
2115 	int i;
2116 
2117 	for (i = 0; i < count; i++) {
2118 		/* Always check status of each filter. We don't need to check
2119 		 * the firmware return status because we pre-set the filter
2120 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2121 		 * request to the adminq. Thus, if it no longer matches then
2122 		 * we know the filter is active.
2123 		 */
2124 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2125 			add_head->state = I40E_FILTER_FAILED;
2126 		} else {
2127 			add_head->state = I40E_FILTER_ACTIVE;
2128 			retval++;
2129 		}
2130 
2131 		add_head = i40e_next_filter(add_head);
2132 		if (!add_head)
2133 			break;
2134 	}
2135 
2136 	return retval;
2137 }
2138 
2139 /**
2140  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2141  * @vsi: ptr to the VSI
2142  * @vsi_name: name to display in messages
2143  * @list: the list of filters to send to firmware
2144  * @num_del: the number of filters to delete
2145  * @retval: Set to -EIO on failure to delete
2146  *
2147  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2148  * *retval instead of a return value so that success does not force ret_val to
2149  * be set to 0. This ensures that a sequence of calls to this function
2150  * preserve the previous value of *retval on successful delete.
2151  */
2152 static
2153 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2154 			  struct i40e_aqc_remove_macvlan_element_data *list,
2155 			  int num_del, int *retval)
2156 {
2157 	struct i40e_hw *hw = &vsi->back->hw;
2158 	enum i40e_admin_queue_err aq_status;
2159 	i40e_status aq_ret;
2160 
2161 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2162 					   &aq_status);
2163 
2164 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2165 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2166 		*retval = -EIO;
2167 		dev_info(&vsi->back->pdev->dev,
2168 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2169 			 vsi_name, i40e_stat_str(hw, aq_ret),
2170 			 i40e_aq_str(hw, aq_status));
2171 	}
2172 }
2173 
2174 /**
2175  * i40e_aqc_add_filters - Request firmware to add a set of filters
2176  * @vsi: ptr to the VSI
2177  * @vsi_name: name to display in messages
2178  * @list: the list of filters to send to firmware
2179  * @add_head: Position in the add hlist
2180  * @num_add: the number of filters to add
2181  *
2182  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2183  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2184  * space for more filters.
2185  */
2186 static
2187 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2188 			  struct i40e_aqc_add_macvlan_element_data *list,
2189 			  struct i40e_new_mac_filter *add_head,
2190 			  int num_add)
2191 {
2192 	struct i40e_hw *hw = &vsi->back->hw;
2193 	enum i40e_admin_queue_err aq_status;
2194 	int fcnt;
2195 
2196 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2197 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2198 
2199 	if (fcnt != num_add) {
2200 		if (vsi->type == I40E_VSI_MAIN) {
2201 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2202 			dev_warn(&vsi->back->pdev->dev,
2203 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2204 				 i40e_aq_str(hw, aq_status), vsi_name);
2205 		} else if (vsi->type == I40E_VSI_SRIOV ||
2206 			   vsi->type == I40E_VSI_VMDQ1 ||
2207 			   vsi->type == I40E_VSI_VMDQ2) {
2208 			dev_warn(&vsi->back->pdev->dev,
2209 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2210 				 i40e_aq_str(hw, aq_status), vsi_name,
2211 					     vsi_name);
2212 		} else {
2213 			dev_warn(&vsi->back->pdev->dev,
2214 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2215 				 i40e_aq_str(hw, aq_status), vsi_name,
2216 					     vsi->type);
2217 		}
2218 	}
2219 }
2220 
2221 /**
2222  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2223  * @vsi: pointer to the VSI
2224  * @vsi_name: the VSI name
2225  * @f: filter data
2226  *
2227  * This function sets or clears the promiscuous broadcast flags for VLAN
2228  * filters in order to properly receive broadcast frames. Assumes that only
2229  * broadcast filters are passed.
2230  *
2231  * Returns status indicating success or failure;
2232  **/
2233 static i40e_status
2234 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2235 			  struct i40e_mac_filter *f)
2236 {
2237 	bool enable = f->state == I40E_FILTER_NEW;
2238 	struct i40e_hw *hw = &vsi->back->hw;
2239 	i40e_status aq_ret;
2240 
2241 	if (f->vlan == I40E_VLAN_ANY) {
2242 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2243 						   vsi->seid,
2244 						   enable,
2245 						   NULL);
2246 	} else {
2247 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2248 							    vsi->seid,
2249 							    enable,
2250 							    f->vlan,
2251 							    NULL);
2252 	}
2253 
2254 	if (aq_ret) {
2255 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2256 		dev_warn(&vsi->back->pdev->dev,
2257 			 "Error %s, forcing overflow promiscuous on %s\n",
2258 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2259 			 vsi_name);
2260 	}
2261 
2262 	return aq_ret;
2263 }
2264 
2265 /**
2266  * i40e_set_promiscuous - set promiscuous mode
2267  * @pf: board private structure
2268  * @promisc: promisc on or off
2269  *
2270  * There are different ways of setting promiscuous mode on a PF depending on
2271  * what state/environment we're in.  This identifies and sets it appropriately.
2272  * Returns 0 on success.
2273  **/
2274 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2275 {
2276 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2277 	struct i40e_hw *hw = &pf->hw;
2278 	i40e_status aq_ret;
2279 
2280 	if (vsi->type == I40E_VSI_MAIN &&
2281 	    pf->lan_veb != I40E_NO_VEB &&
2282 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2283 		/* set defport ON for Main VSI instead of true promisc
2284 		 * this way we will get all unicast/multicast and VLAN
2285 		 * promisc behavior but will not get VF or VMDq traffic
2286 		 * replicated on the Main VSI.
2287 		 */
2288 		if (promisc)
2289 			aq_ret = i40e_aq_set_default_vsi(hw,
2290 							 vsi->seid,
2291 							 NULL);
2292 		else
2293 			aq_ret = i40e_aq_clear_default_vsi(hw,
2294 							   vsi->seid,
2295 							   NULL);
2296 		if (aq_ret) {
2297 			dev_info(&pf->pdev->dev,
2298 				 "Set default VSI failed, err %s, aq_err %s\n",
2299 				 i40e_stat_str(hw, aq_ret),
2300 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2301 		}
2302 	} else {
2303 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2304 						  hw,
2305 						  vsi->seid,
2306 						  promisc, NULL,
2307 						  true);
2308 		if (aq_ret) {
2309 			dev_info(&pf->pdev->dev,
2310 				 "set unicast promisc failed, err %s, aq_err %s\n",
2311 				 i40e_stat_str(hw, aq_ret),
2312 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2313 		}
2314 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2315 						  hw,
2316 						  vsi->seid,
2317 						  promisc, NULL);
2318 		if (aq_ret) {
2319 			dev_info(&pf->pdev->dev,
2320 				 "set multicast promisc failed, err %s, aq_err %s\n",
2321 				 i40e_stat_str(hw, aq_ret),
2322 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2323 		}
2324 	}
2325 
2326 	if (!aq_ret)
2327 		pf->cur_promisc = promisc;
2328 
2329 	return aq_ret;
2330 }
2331 
2332 /**
2333  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2334  * @vsi: ptr to the VSI
2335  *
2336  * Push any outstanding VSI filter changes through the AdminQ.
2337  *
2338  * Returns 0 or error value
2339  **/
2340 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2341 {
2342 	struct hlist_head tmp_add_list, tmp_del_list;
2343 	struct i40e_mac_filter *f;
2344 	struct i40e_new_mac_filter *new, *add_head = NULL;
2345 	struct i40e_hw *hw = &vsi->back->hw;
2346 	bool old_overflow, new_overflow;
2347 	unsigned int failed_filters = 0;
2348 	unsigned int vlan_filters = 0;
2349 	char vsi_name[16] = "PF";
2350 	int filter_list_len = 0;
2351 	i40e_status aq_ret = 0;
2352 	u32 changed_flags = 0;
2353 	struct hlist_node *h;
2354 	struct i40e_pf *pf;
2355 	int num_add = 0;
2356 	int num_del = 0;
2357 	int retval = 0;
2358 	u16 cmd_flags;
2359 	int list_size;
2360 	int bkt;
2361 
2362 	/* empty array typed pointers, kcalloc later */
2363 	struct i40e_aqc_add_macvlan_element_data *add_list;
2364 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2365 
2366 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2367 		usleep_range(1000, 2000);
2368 	pf = vsi->back;
2369 
2370 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2371 
2372 	if (vsi->netdev) {
2373 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2374 		vsi->current_netdev_flags = vsi->netdev->flags;
2375 	}
2376 
2377 	INIT_HLIST_HEAD(&tmp_add_list);
2378 	INIT_HLIST_HEAD(&tmp_del_list);
2379 
2380 	if (vsi->type == I40E_VSI_SRIOV)
2381 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2382 	else if (vsi->type != I40E_VSI_MAIN)
2383 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2384 
2385 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2386 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2387 
2388 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2389 		/* Create a list of filters to delete. */
2390 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2391 			if (f->state == I40E_FILTER_REMOVE) {
2392 				/* Move the element into temporary del_list */
2393 				hash_del(&f->hlist);
2394 				hlist_add_head(&f->hlist, &tmp_del_list);
2395 
2396 				/* Avoid counting removed filters */
2397 				continue;
2398 			}
2399 			if (f->state == I40E_FILTER_NEW) {
2400 				/* Create a temporary i40e_new_mac_filter */
2401 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2402 				if (!new)
2403 					goto err_no_memory_locked;
2404 
2405 				/* Store pointer to the real filter */
2406 				new->f = f;
2407 				new->state = f->state;
2408 
2409 				/* Add it to the hash list */
2410 				hlist_add_head(&new->hlist, &tmp_add_list);
2411 			}
2412 
2413 			/* Count the number of active (current and new) VLAN
2414 			 * filters we have now. Does not count filters which
2415 			 * are marked for deletion.
2416 			 */
2417 			if (f->vlan > 0)
2418 				vlan_filters++;
2419 		}
2420 
2421 		retval = i40e_correct_mac_vlan_filters(vsi,
2422 						       &tmp_add_list,
2423 						       &tmp_del_list,
2424 						       vlan_filters);
2425 
2426 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2427 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2428 
2429 		if (retval)
2430 			goto err_no_memory_locked;
2431 
2432 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2433 	}
2434 
2435 	/* Now process 'del_list' outside the lock */
2436 	if (!hlist_empty(&tmp_del_list)) {
2437 		filter_list_len = hw->aq.asq_buf_size /
2438 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2439 		list_size = filter_list_len *
2440 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2441 		del_list = kzalloc(list_size, GFP_ATOMIC);
2442 		if (!del_list)
2443 			goto err_no_memory;
2444 
2445 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2446 			cmd_flags = 0;
2447 
2448 			/* handle broadcast filters by updating the broadcast
2449 			 * promiscuous flag and release filter list.
2450 			 */
2451 			if (is_broadcast_ether_addr(f->macaddr)) {
2452 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2453 
2454 				hlist_del(&f->hlist);
2455 				kfree(f);
2456 				continue;
2457 			}
2458 
2459 			/* add to delete list */
2460 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2461 			if (f->vlan == I40E_VLAN_ANY) {
2462 				del_list[num_del].vlan_tag = 0;
2463 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2464 			} else {
2465 				del_list[num_del].vlan_tag =
2466 					cpu_to_le16((u16)(f->vlan));
2467 			}
2468 
2469 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2470 			del_list[num_del].flags = cmd_flags;
2471 			num_del++;
2472 
2473 			/* flush a full buffer */
2474 			if (num_del == filter_list_len) {
2475 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2476 						     num_del, &retval);
2477 				memset(del_list, 0, list_size);
2478 				num_del = 0;
2479 			}
2480 			/* Release memory for MAC filter entries which were
2481 			 * synced up with HW.
2482 			 */
2483 			hlist_del(&f->hlist);
2484 			kfree(f);
2485 		}
2486 
2487 		if (num_del) {
2488 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2489 					     num_del, &retval);
2490 		}
2491 
2492 		kfree(del_list);
2493 		del_list = NULL;
2494 	}
2495 
2496 	if (!hlist_empty(&tmp_add_list)) {
2497 		/* Do all the adds now. */
2498 		filter_list_len = hw->aq.asq_buf_size /
2499 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2500 		list_size = filter_list_len *
2501 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2502 		add_list = kzalloc(list_size, GFP_ATOMIC);
2503 		if (!add_list)
2504 			goto err_no_memory;
2505 
2506 		num_add = 0;
2507 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2508 			/* handle broadcast filters by updating the broadcast
2509 			 * promiscuous flag instead of adding a MAC filter.
2510 			 */
2511 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2512 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2513 							      new->f))
2514 					new->state = I40E_FILTER_FAILED;
2515 				else
2516 					new->state = I40E_FILTER_ACTIVE;
2517 				continue;
2518 			}
2519 
2520 			/* add to add array */
2521 			if (num_add == 0)
2522 				add_head = new;
2523 			cmd_flags = 0;
2524 			ether_addr_copy(add_list[num_add].mac_addr,
2525 					new->f->macaddr);
2526 			if (new->f->vlan == I40E_VLAN_ANY) {
2527 				add_list[num_add].vlan_tag = 0;
2528 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2529 			} else {
2530 				add_list[num_add].vlan_tag =
2531 					cpu_to_le16((u16)(new->f->vlan));
2532 			}
2533 			add_list[num_add].queue_number = 0;
2534 			/* set invalid match method for later detection */
2535 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2536 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2537 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2538 			num_add++;
2539 
2540 			/* flush a full buffer */
2541 			if (num_add == filter_list_len) {
2542 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2543 						     add_head, num_add);
2544 				memset(add_list, 0, list_size);
2545 				num_add = 0;
2546 			}
2547 		}
2548 		if (num_add) {
2549 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2550 					     num_add);
2551 		}
2552 		/* Now move all of the filters from the temp add list back to
2553 		 * the VSI's list.
2554 		 */
2555 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2556 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2557 			/* Only update the state if we're still NEW */
2558 			if (new->f->state == I40E_FILTER_NEW)
2559 				new->f->state = new->state;
2560 			hlist_del(&new->hlist);
2561 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2562 			kfree(new);
2563 		}
2564 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2565 		kfree(add_list);
2566 		add_list = NULL;
2567 	}
2568 
2569 	/* Determine the number of active and failed filters. */
2570 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2571 	vsi->active_filters = 0;
2572 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2573 		if (f->state == I40E_FILTER_ACTIVE)
2574 			vsi->active_filters++;
2575 		else if (f->state == I40E_FILTER_FAILED)
2576 			failed_filters++;
2577 	}
2578 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2579 
2580 	/* Check if we are able to exit overflow promiscuous mode. We can
2581 	 * safely exit if we didn't just enter, we no longer have any failed
2582 	 * filters, and we have reduced filters below the threshold value.
2583 	 */
2584 	if (old_overflow && !failed_filters &&
2585 	    vsi->active_filters < vsi->promisc_threshold) {
2586 		dev_info(&pf->pdev->dev,
2587 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2588 			 vsi_name);
2589 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2590 		vsi->promisc_threshold = 0;
2591 	}
2592 
2593 	/* if the VF is not trusted do not do promisc */
2594 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2595 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2596 		goto out;
2597 	}
2598 
2599 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2600 
2601 	/* If we are entering overflow promiscuous, we need to calculate a new
2602 	 * threshold for when we are safe to exit
2603 	 */
2604 	if (!old_overflow && new_overflow)
2605 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2606 
2607 	/* check for changes in promiscuous modes */
2608 	if (changed_flags & IFF_ALLMULTI) {
2609 		bool cur_multipromisc;
2610 
2611 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2612 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2613 							       vsi->seid,
2614 							       cur_multipromisc,
2615 							       NULL);
2616 		if (aq_ret) {
2617 			retval = i40e_aq_rc_to_posix(aq_ret,
2618 						     hw->aq.asq_last_status);
2619 			dev_info(&pf->pdev->dev,
2620 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2621 				 vsi_name,
2622 				 i40e_stat_str(hw, aq_ret),
2623 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2624 		} else {
2625 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2626 				 cur_multipromisc ? "entering" : "leaving");
2627 		}
2628 	}
2629 
2630 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2631 		bool cur_promisc;
2632 
2633 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2634 			       new_overflow);
2635 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2636 		if (aq_ret) {
2637 			retval = i40e_aq_rc_to_posix(aq_ret,
2638 						     hw->aq.asq_last_status);
2639 			dev_info(&pf->pdev->dev,
2640 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2641 				 cur_promisc ? "on" : "off",
2642 				 vsi_name,
2643 				 i40e_stat_str(hw, aq_ret),
2644 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2645 		}
2646 	}
2647 out:
2648 	/* if something went wrong then set the changed flag so we try again */
2649 	if (retval)
2650 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2651 
2652 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2653 	return retval;
2654 
2655 err_no_memory:
2656 	/* Restore elements on the temporary add and delete lists */
2657 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2658 err_no_memory_locked:
2659 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2660 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2661 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2662 
2663 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2664 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2665 	return -ENOMEM;
2666 }
2667 
2668 /**
2669  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2670  * @pf: board private structure
2671  **/
2672 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2673 {
2674 	int v;
2675 
2676 	if (!pf)
2677 		return;
2678 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2679 		return;
2680 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2681 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2682 		return;
2683 	}
2684 
2685 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2686 		if (pf->vsi[v] &&
2687 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2688 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2689 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2690 
2691 			if (ret) {
2692 				/* come back and try again later */
2693 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2694 					pf->state);
2695 				break;
2696 			}
2697 		}
2698 	}
2699 }
2700 
2701 /**
2702  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2703  * @vsi: the vsi
2704  **/
2705 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2706 {
2707 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2708 		return I40E_RXBUFFER_2048;
2709 	else
2710 		return I40E_RXBUFFER_3072;
2711 }
2712 
2713 /**
2714  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2715  * @netdev: network interface device structure
2716  * @new_mtu: new value for maximum frame size
2717  *
2718  * Returns 0 on success, negative on failure
2719  **/
2720 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2721 {
2722 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2723 	struct i40e_vsi *vsi = np->vsi;
2724 	struct i40e_pf *pf = vsi->back;
2725 
2726 	if (i40e_enabled_xdp_vsi(vsi)) {
2727 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2728 
2729 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2730 			return -EINVAL;
2731 	}
2732 
2733 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2734 		   netdev->mtu, new_mtu);
2735 	netdev->mtu = new_mtu;
2736 	if (netif_running(netdev))
2737 		i40e_vsi_reinit_locked(vsi);
2738 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2739 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2740 	return 0;
2741 }
2742 
2743 /**
2744  * i40e_ioctl - Access the hwtstamp interface
2745  * @netdev: network interface device structure
2746  * @ifr: interface request data
2747  * @cmd: ioctl command
2748  **/
2749 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2750 {
2751 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2752 	struct i40e_pf *pf = np->vsi->back;
2753 
2754 	switch (cmd) {
2755 	case SIOCGHWTSTAMP:
2756 		return i40e_ptp_get_ts_config(pf, ifr);
2757 	case SIOCSHWTSTAMP:
2758 		return i40e_ptp_set_ts_config(pf, ifr);
2759 	default:
2760 		return -EOPNOTSUPP;
2761 	}
2762 }
2763 
2764 /**
2765  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2766  * @vsi: the vsi being adjusted
2767  **/
2768 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2769 {
2770 	struct i40e_vsi_context ctxt;
2771 	i40e_status ret;
2772 
2773 	/* Don't modify stripping options if a port VLAN is active */
2774 	if (vsi->info.pvid)
2775 		return;
2776 
2777 	if ((vsi->info.valid_sections &
2778 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2779 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2780 		return;  /* already enabled */
2781 
2782 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2783 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2784 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2785 
2786 	ctxt.seid = vsi->seid;
2787 	ctxt.info = vsi->info;
2788 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2789 	if (ret) {
2790 		dev_info(&vsi->back->pdev->dev,
2791 			 "update vlan stripping failed, err %s aq_err %s\n",
2792 			 i40e_stat_str(&vsi->back->hw, ret),
2793 			 i40e_aq_str(&vsi->back->hw,
2794 				     vsi->back->hw.aq.asq_last_status));
2795 	}
2796 }
2797 
2798 /**
2799  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2800  * @vsi: the vsi being adjusted
2801  **/
2802 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2803 {
2804 	struct i40e_vsi_context ctxt;
2805 	i40e_status ret;
2806 
2807 	/* Don't modify stripping options if a port VLAN is active */
2808 	if (vsi->info.pvid)
2809 		return;
2810 
2811 	if ((vsi->info.valid_sections &
2812 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2813 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2814 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
2815 		return;  /* already disabled */
2816 
2817 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2818 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2819 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2820 
2821 	ctxt.seid = vsi->seid;
2822 	ctxt.info = vsi->info;
2823 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2824 	if (ret) {
2825 		dev_info(&vsi->back->pdev->dev,
2826 			 "update vlan stripping failed, err %s aq_err %s\n",
2827 			 i40e_stat_str(&vsi->back->hw, ret),
2828 			 i40e_aq_str(&vsi->back->hw,
2829 				     vsi->back->hw.aq.asq_last_status));
2830 	}
2831 }
2832 
2833 /**
2834  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2835  * @vsi: the vsi being configured
2836  * @vid: vlan id to be added (0 = untagged only , -1 = any)
2837  *
2838  * This is a helper function for adding a new MAC/VLAN filter with the
2839  * specified VLAN for each existing MAC address already in the hash table.
2840  * This function does *not* perform any accounting to update filters based on
2841  * VLAN mode.
2842  *
2843  * NOTE: this function expects to be called while under the
2844  * mac_filter_hash_lock
2845  **/
2846 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2847 {
2848 	struct i40e_mac_filter *f, *add_f;
2849 	struct hlist_node *h;
2850 	int bkt;
2851 
2852 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2853 		if (f->state == I40E_FILTER_REMOVE)
2854 			continue;
2855 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
2856 		if (!add_f) {
2857 			dev_info(&vsi->back->pdev->dev,
2858 				 "Could not add vlan filter %d for %pM\n",
2859 				 vid, f->macaddr);
2860 			return -ENOMEM;
2861 		}
2862 	}
2863 
2864 	return 0;
2865 }
2866 
2867 /**
2868  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2869  * @vsi: the VSI being configured
2870  * @vid: VLAN id to be added
2871  **/
2872 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
2873 {
2874 	int err;
2875 
2876 	if (vsi->info.pvid)
2877 		return -EINVAL;
2878 
2879 	/* The network stack will attempt to add VID=0, with the intention to
2880 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
2881 	 * these packets by default when configured to receive untagged
2882 	 * packets, so we don't need to add a filter for this case.
2883 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
2884 	 * receive *only* tagged traffic and stops receiving untagged traffic.
2885 	 * Thus, we do not want to actually add a filter for VID=0
2886 	 */
2887 	if (!vid)
2888 		return 0;
2889 
2890 	/* Locked once because all functions invoked below iterates list*/
2891 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2892 	err = i40e_add_vlan_all_mac(vsi, vid);
2893 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2894 	if (err)
2895 		return err;
2896 
2897 	/* schedule our worker thread which will take care of
2898 	 * applying the new filter changes
2899 	 */
2900 	i40e_service_event_schedule(vsi->back);
2901 	return 0;
2902 }
2903 
2904 /**
2905  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
2906  * @vsi: the vsi being configured
2907  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2908  *
2909  * This function should be used to remove all VLAN filters which match the
2910  * given VID. It does not schedule the service event and does not take the
2911  * mac_filter_hash_lock so it may be combined with other operations under
2912  * a single invocation of the mac_filter_hash_lock.
2913  *
2914  * NOTE: this function expects to be called while under the
2915  * mac_filter_hash_lock
2916  */
2917 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2918 {
2919 	struct i40e_mac_filter *f;
2920 	struct hlist_node *h;
2921 	int bkt;
2922 
2923 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2924 		if (f->vlan == vid)
2925 			__i40e_del_filter(vsi, f);
2926 	}
2927 }
2928 
2929 /**
2930  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2931  * @vsi: the VSI being configured
2932  * @vid: VLAN id to be removed
2933  **/
2934 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
2935 {
2936 	if (!vid || vsi->info.pvid)
2937 		return;
2938 
2939 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2940 	i40e_rm_vlan_all_mac(vsi, vid);
2941 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2942 
2943 	/* schedule our worker thread which will take care of
2944 	 * applying the new filter changes
2945 	 */
2946 	i40e_service_event_schedule(vsi->back);
2947 }
2948 
2949 /**
2950  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2951  * @netdev: network interface to be adjusted
2952  * @proto: unused protocol value
2953  * @vid: vlan id to be added
2954  *
2955  * net_device_ops implementation for adding vlan ids
2956  **/
2957 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2958 				__always_unused __be16 proto, u16 vid)
2959 {
2960 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2961 	struct i40e_vsi *vsi = np->vsi;
2962 	int ret = 0;
2963 
2964 	if (vid >= VLAN_N_VID)
2965 		return -EINVAL;
2966 
2967 	ret = i40e_vsi_add_vlan(vsi, vid);
2968 	if (!ret)
2969 		set_bit(vid, vsi->active_vlans);
2970 
2971 	return ret;
2972 }
2973 
2974 /**
2975  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
2976  * @netdev: network interface to be adjusted
2977  * @proto: unused protocol value
2978  * @vid: vlan id to be added
2979  **/
2980 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
2981 				    __always_unused __be16 proto, u16 vid)
2982 {
2983 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2984 	struct i40e_vsi *vsi = np->vsi;
2985 
2986 	if (vid >= VLAN_N_VID)
2987 		return;
2988 	set_bit(vid, vsi->active_vlans);
2989 }
2990 
2991 /**
2992  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2993  * @netdev: network interface to be adjusted
2994  * @proto: unused protocol value
2995  * @vid: vlan id to be removed
2996  *
2997  * net_device_ops implementation for removing vlan ids
2998  **/
2999 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3000 				 __always_unused __be16 proto, u16 vid)
3001 {
3002 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3003 	struct i40e_vsi *vsi = np->vsi;
3004 
3005 	/* return code is ignored as there is nothing a user
3006 	 * can do about failure to remove and a log message was
3007 	 * already printed from the other function
3008 	 */
3009 	i40e_vsi_kill_vlan(vsi, vid);
3010 
3011 	clear_bit(vid, vsi->active_vlans);
3012 
3013 	return 0;
3014 }
3015 
3016 /**
3017  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3018  * @vsi: the vsi being brought back up
3019  **/
3020 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3021 {
3022 	u16 vid;
3023 
3024 	if (!vsi->netdev)
3025 		return;
3026 
3027 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3028 		i40e_vlan_stripping_enable(vsi);
3029 	else
3030 		i40e_vlan_stripping_disable(vsi);
3031 
3032 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3033 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3034 					vid);
3035 }
3036 
3037 /**
3038  * i40e_vsi_add_pvid - Add pvid for the VSI
3039  * @vsi: the vsi being adjusted
3040  * @vid: the vlan id to set as a PVID
3041  **/
3042 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3043 {
3044 	struct i40e_vsi_context ctxt;
3045 	i40e_status ret;
3046 
3047 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3048 	vsi->info.pvid = cpu_to_le16(vid);
3049 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3050 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3051 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3052 
3053 	ctxt.seid = vsi->seid;
3054 	ctxt.info = vsi->info;
3055 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3056 	if (ret) {
3057 		dev_info(&vsi->back->pdev->dev,
3058 			 "add pvid failed, err %s aq_err %s\n",
3059 			 i40e_stat_str(&vsi->back->hw, ret),
3060 			 i40e_aq_str(&vsi->back->hw,
3061 				     vsi->back->hw.aq.asq_last_status));
3062 		return -ENOENT;
3063 	}
3064 
3065 	return 0;
3066 }
3067 
3068 /**
3069  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3070  * @vsi: the vsi being adjusted
3071  *
3072  * Just use the vlan_rx_register() service to put it back to normal
3073  **/
3074 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3075 {
3076 	vsi->info.pvid = 0;
3077 
3078 	i40e_vlan_stripping_disable(vsi);
3079 }
3080 
3081 /**
3082  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3083  * @vsi: ptr to the VSI
3084  *
3085  * If this function returns with an error, then it's possible one or
3086  * more of the rings is populated (while the rest are not).  It is the
3087  * callers duty to clean those orphaned rings.
3088  *
3089  * Return 0 on success, negative on failure
3090  **/
3091 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3092 {
3093 	int i, err = 0;
3094 
3095 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3096 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3097 
3098 	if (!i40e_enabled_xdp_vsi(vsi))
3099 		return err;
3100 
3101 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3102 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3103 
3104 	return err;
3105 }
3106 
3107 /**
3108  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3109  * @vsi: ptr to the VSI
3110  *
3111  * Free VSI's transmit software resources
3112  **/
3113 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3114 {
3115 	int i;
3116 
3117 	if (vsi->tx_rings) {
3118 		for (i = 0; i < vsi->num_queue_pairs; i++)
3119 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3120 				i40e_free_tx_resources(vsi->tx_rings[i]);
3121 	}
3122 
3123 	if (vsi->xdp_rings) {
3124 		for (i = 0; i < vsi->num_queue_pairs; i++)
3125 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3126 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3127 	}
3128 }
3129 
3130 /**
3131  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3132  * @vsi: ptr to the VSI
3133  *
3134  * If this function returns with an error, then it's possible one or
3135  * more of the rings is populated (while the rest are not).  It is the
3136  * callers duty to clean those orphaned rings.
3137  *
3138  * Return 0 on success, negative on failure
3139  **/
3140 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3141 {
3142 	int i, err = 0;
3143 
3144 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3145 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3146 	return err;
3147 }
3148 
3149 /**
3150  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3151  * @vsi: ptr to the VSI
3152  *
3153  * Free all receive software resources
3154  **/
3155 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3156 {
3157 	int i;
3158 
3159 	if (!vsi->rx_rings)
3160 		return;
3161 
3162 	for (i = 0; i < vsi->num_queue_pairs; i++)
3163 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3164 			i40e_free_rx_resources(vsi->rx_rings[i]);
3165 }
3166 
3167 /**
3168  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3169  * @ring: The Tx ring to configure
3170  *
3171  * This enables/disables XPS for a given Tx descriptor ring
3172  * based on the TCs enabled for the VSI that ring belongs to.
3173  **/
3174 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3175 {
3176 	int cpu;
3177 
3178 	if (!ring->q_vector || !ring->netdev || ring->ch)
3179 		return;
3180 
3181 	/* We only initialize XPS once, so as not to overwrite user settings */
3182 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3183 		return;
3184 
3185 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3186 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3187 			    ring->queue_index);
3188 }
3189 
3190 /**
3191  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3192  * @ring: The Tx or Rx ring
3193  *
3194  * Returns the AF_XDP buffer pool or NULL.
3195  **/
3196 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3197 {
3198 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3199 	int qid = ring->queue_index;
3200 
3201 	if (ring_is_xdp(ring))
3202 		qid -= ring->vsi->alloc_queue_pairs;
3203 
3204 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3205 		return NULL;
3206 
3207 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3208 }
3209 
3210 /**
3211  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3212  * @ring: The Tx ring to configure
3213  *
3214  * Configure the Tx descriptor ring in the HMC context.
3215  **/
3216 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3217 {
3218 	struct i40e_vsi *vsi = ring->vsi;
3219 	u16 pf_q = vsi->base_queue + ring->queue_index;
3220 	struct i40e_hw *hw = &vsi->back->hw;
3221 	struct i40e_hmc_obj_txq tx_ctx;
3222 	i40e_status err = 0;
3223 	u32 qtx_ctl = 0;
3224 
3225 	if (ring_is_xdp(ring))
3226 		ring->xsk_pool = i40e_xsk_pool(ring);
3227 
3228 	/* some ATR related tx ring init */
3229 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3230 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3231 		ring->atr_count = 0;
3232 	} else {
3233 		ring->atr_sample_rate = 0;
3234 	}
3235 
3236 	/* configure XPS */
3237 	i40e_config_xps_tx_ring(ring);
3238 
3239 	/* clear the context structure first */
3240 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3241 
3242 	tx_ctx.new_context = 1;
3243 	tx_ctx.base = (ring->dma / 128);
3244 	tx_ctx.qlen = ring->count;
3245 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3246 					       I40E_FLAG_FD_ATR_ENABLED));
3247 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3248 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3249 	if (vsi->type != I40E_VSI_FDIR)
3250 		tx_ctx.head_wb_ena = 1;
3251 	tx_ctx.head_wb_addr = ring->dma +
3252 			      (ring->count * sizeof(struct i40e_tx_desc));
3253 
3254 	/* As part of VSI creation/update, FW allocates certain
3255 	 * Tx arbitration queue sets for each TC enabled for
3256 	 * the VSI. The FW returns the handles to these queue
3257 	 * sets as part of the response buffer to Add VSI,
3258 	 * Update VSI, etc. AQ commands. It is expected that
3259 	 * these queue set handles be associated with the Tx
3260 	 * queues by the driver as part of the TX queue context
3261 	 * initialization. This has to be done regardless of
3262 	 * DCB as by default everything is mapped to TC0.
3263 	 */
3264 
3265 	if (ring->ch)
3266 		tx_ctx.rdylist =
3267 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3268 
3269 	else
3270 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3271 
3272 	tx_ctx.rdylist_act = 0;
3273 
3274 	/* clear the context in the HMC */
3275 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3276 	if (err) {
3277 		dev_info(&vsi->back->pdev->dev,
3278 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3279 			 ring->queue_index, pf_q, err);
3280 		return -ENOMEM;
3281 	}
3282 
3283 	/* set the context in the HMC */
3284 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3285 	if (err) {
3286 		dev_info(&vsi->back->pdev->dev,
3287 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3288 			 ring->queue_index, pf_q, err);
3289 		return -ENOMEM;
3290 	}
3291 
3292 	/* Now associate this queue with this PCI function */
3293 	if (ring->ch) {
3294 		if (ring->ch->type == I40E_VSI_VMDQ2)
3295 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3296 		else
3297 			return -EINVAL;
3298 
3299 		qtx_ctl |= (ring->ch->vsi_number <<
3300 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3301 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3302 	} else {
3303 		if (vsi->type == I40E_VSI_VMDQ2) {
3304 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3305 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3306 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3307 		} else {
3308 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3309 		}
3310 	}
3311 
3312 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3313 		    I40E_QTX_CTL_PF_INDX_MASK);
3314 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3315 	i40e_flush(hw);
3316 
3317 	/* cache tail off for easier writes later */
3318 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3319 
3320 	return 0;
3321 }
3322 
3323 /**
3324  * i40e_rx_offset - Return expected offset into page to access data
3325  * @rx_ring: Ring we are requesting offset of
3326  *
3327  * Returns the offset value for ring into the data buffer.
3328  */
3329 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3330 {
3331 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3332 }
3333 
3334 /**
3335  * i40e_configure_rx_ring - Configure a receive ring context
3336  * @ring: The Rx ring to configure
3337  *
3338  * Configure the Rx descriptor ring in the HMC context.
3339  **/
3340 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3341 {
3342 	struct i40e_vsi *vsi = ring->vsi;
3343 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3344 	u16 pf_q = vsi->base_queue + ring->queue_index;
3345 	struct i40e_hw *hw = &vsi->back->hw;
3346 	struct i40e_hmc_obj_rxq rx_ctx;
3347 	i40e_status err = 0;
3348 	bool ok;
3349 	int ret;
3350 
3351 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3352 
3353 	/* clear the context structure first */
3354 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3355 
3356 	if (ring->vsi->type == I40E_VSI_MAIN)
3357 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3358 
3359 	kfree(ring->rx_bi);
3360 	ring->xsk_pool = i40e_xsk_pool(ring);
3361 	if (ring->xsk_pool) {
3362 		ret = i40e_alloc_rx_bi_zc(ring);
3363 		if (ret)
3364 			return ret;
3365 		ring->rx_buf_len =
3366 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3367 		/* For AF_XDP ZC, we disallow packets to span on
3368 		 * multiple buffers, thus letting us skip that
3369 		 * handling in the fast-path.
3370 		 */
3371 		chain_len = 1;
3372 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3373 						 MEM_TYPE_XSK_BUFF_POOL,
3374 						 NULL);
3375 		if (ret)
3376 			return ret;
3377 		dev_info(&vsi->back->pdev->dev,
3378 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3379 			 ring->queue_index);
3380 
3381 	} else {
3382 		ret = i40e_alloc_rx_bi(ring);
3383 		if (ret)
3384 			return ret;
3385 		ring->rx_buf_len = vsi->rx_buf_len;
3386 		if (ring->vsi->type == I40E_VSI_MAIN) {
3387 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3388 							 MEM_TYPE_PAGE_SHARED,
3389 							 NULL);
3390 			if (ret)
3391 				return ret;
3392 		}
3393 	}
3394 
3395 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3396 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3397 
3398 	rx_ctx.base = (ring->dma / 128);
3399 	rx_ctx.qlen = ring->count;
3400 
3401 	/* use 16 byte descriptors */
3402 	rx_ctx.dsize = 0;
3403 
3404 	/* descriptor type is always zero
3405 	 * rx_ctx.dtype = 0;
3406 	 */
3407 	rx_ctx.hsplit_0 = 0;
3408 
3409 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3410 	if (hw->revision_id == 0)
3411 		rx_ctx.lrxqthresh = 0;
3412 	else
3413 		rx_ctx.lrxqthresh = 1;
3414 	rx_ctx.crcstrip = 1;
3415 	rx_ctx.l2tsel = 1;
3416 	/* this controls whether VLAN is stripped from inner headers */
3417 	rx_ctx.showiv = 0;
3418 	/* set the prefena field to 1 because the manual says to */
3419 	rx_ctx.prefena = 1;
3420 
3421 	/* clear the context in the HMC */
3422 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3423 	if (err) {
3424 		dev_info(&vsi->back->pdev->dev,
3425 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3426 			 ring->queue_index, pf_q, err);
3427 		return -ENOMEM;
3428 	}
3429 
3430 	/* set the context in the HMC */
3431 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3432 	if (err) {
3433 		dev_info(&vsi->back->pdev->dev,
3434 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3435 			 ring->queue_index, pf_q, err);
3436 		return -ENOMEM;
3437 	}
3438 
3439 	/* configure Rx buffer alignment */
3440 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3441 		clear_ring_build_skb_enabled(ring);
3442 	else
3443 		set_ring_build_skb_enabled(ring);
3444 
3445 	ring->rx_offset = i40e_rx_offset(ring);
3446 
3447 	/* cache tail for quicker writes, and clear the reg before use */
3448 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3449 	writel(0, ring->tail);
3450 
3451 	if (ring->xsk_pool) {
3452 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3453 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3454 	} else {
3455 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3456 	}
3457 	if (!ok) {
3458 		/* Log this in case the user has forgotten to give the kernel
3459 		 * any buffers, even later in the application.
3460 		 */
3461 		dev_info(&vsi->back->pdev->dev,
3462 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3463 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3464 			 ring->queue_index, pf_q);
3465 	}
3466 
3467 	return 0;
3468 }
3469 
3470 /**
3471  * i40e_vsi_configure_tx - Configure the VSI for Tx
3472  * @vsi: VSI structure describing this set of rings and resources
3473  *
3474  * Configure the Tx VSI for operation.
3475  **/
3476 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3477 {
3478 	int err = 0;
3479 	u16 i;
3480 
3481 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3482 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3483 
3484 	if (err || !i40e_enabled_xdp_vsi(vsi))
3485 		return err;
3486 
3487 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3488 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3489 
3490 	return err;
3491 }
3492 
3493 /**
3494  * i40e_vsi_configure_rx - Configure the VSI for Rx
3495  * @vsi: the VSI being configured
3496  *
3497  * Configure the Rx VSI for operation.
3498  **/
3499 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3500 {
3501 	int err = 0;
3502 	u16 i;
3503 
3504 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3505 		vsi->max_frame = I40E_MAX_RXBUFFER;
3506 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3507 #if (PAGE_SIZE < 8192)
3508 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3509 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3510 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3511 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3512 #endif
3513 	} else {
3514 		vsi->max_frame = I40E_MAX_RXBUFFER;
3515 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3516 						       I40E_RXBUFFER_2048;
3517 	}
3518 
3519 	/* set up individual rings */
3520 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3521 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3522 
3523 	return err;
3524 }
3525 
3526 /**
3527  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3528  * @vsi: ptr to the VSI
3529  **/
3530 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3531 {
3532 	struct i40e_ring *tx_ring, *rx_ring;
3533 	u16 qoffset, qcount;
3534 	int i, n;
3535 
3536 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3537 		/* Reset the TC information */
3538 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3539 			rx_ring = vsi->rx_rings[i];
3540 			tx_ring = vsi->tx_rings[i];
3541 			rx_ring->dcb_tc = 0;
3542 			tx_ring->dcb_tc = 0;
3543 		}
3544 		return;
3545 	}
3546 
3547 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3548 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3549 			continue;
3550 
3551 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3552 		qcount = vsi->tc_config.tc_info[n].qcount;
3553 		for (i = qoffset; i < (qoffset + qcount); i++) {
3554 			rx_ring = vsi->rx_rings[i];
3555 			tx_ring = vsi->tx_rings[i];
3556 			rx_ring->dcb_tc = n;
3557 			tx_ring->dcb_tc = n;
3558 		}
3559 	}
3560 }
3561 
3562 /**
3563  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3564  * @vsi: ptr to the VSI
3565  **/
3566 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3567 {
3568 	if (vsi->netdev)
3569 		i40e_set_rx_mode(vsi->netdev);
3570 }
3571 
3572 /**
3573  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3574  * @pf: Pointer to the targeted PF
3575  *
3576  * Set all flow director counters to 0.
3577  */
3578 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3579 {
3580 	pf->fd_tcp4_filter_cnt = 0;
3581 	pf->fd_udp4_filter_cnt = 0;
3582 	pf->fd_sctp4_filter_cnt = 0;
3583 	pf->fd_ip4_filter_cnt = 0;
3584 	pf->fd_tcp6_filter_cnt = 0;
3585 	pf->fd_udp6_filter_cnt = 0;
3586 	pf->fd_sctp6_filter_cnt = 0;
3587 	pf->fd_ip6_filter_cnt = 0;
3588 }
3589 
3590 /**
3591  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3592  * @vsi: Pointer to the targeted VSI
3593  *
3594  * This function replays the hlist on the hw where all the SB Flow Director
3595  * filters were saved.
3596  **/
3597 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3598 {
3599 	struct i40e_fdir_filter *filter;
3600 	struct i40e_pf *pf = vsi->back;
3601 	struct hlist_node *node;
3602 
3603 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3604 		return;
3605 
3606 	/* Reset FDir counters as we're replaying all existing filters */
3607 	i40e_reset_fdir_filter_cnt(pf);
3608 
3609 	hlist_for_each_entry_safe(filter, node,
3610 				  &pf->fdir_filter_list, fdir_node) {
3611 		i40e_add_del_fdir(vsi, filter, true);
3612 	}
3613 }
3614 
3615 /**
3616  * i40e_vsi_configure - Set up the VSI for action
3617  * @vsi: the VSI being configured
3618  **/
3619 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3620 {
3621 	int err;
3622 
3623 	i40e_set_vsi_rx_mode(vsi);
3624 	i40e_restore_vlan(vsi);
3625 	i40e_vsi_config_dcb_rings(vsi);
3626 	err = i40e_vsi_configure_tx(vsi);
3627 	if (!err)
3628 		err = i40e_vsi_configure_rx(vsi);
3629 
3630 	return err;
3631 }
3632 
3633 /**
3634  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3635  * @vsi: the VSI being configured
3636  **/
3637 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3638 {
3639 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3640 	struct i40e_pf *pf = vsi->back;
3641 	struct i40e_hw *hw = &pf->hw;
3642 	u16 vector;
3643 	int i, q;
3644 	u32 qp;
3645 
3646 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3647 	 * and PFINT_LNKLSTn registers, e.g.:
3648 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3649 	 */
3650 	qp = vsi->base_queue;
3651 	vector = vsi->base_vector;
3652 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3653 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3654 
3655 		q_vector->rx.next_update = jiffies + 1;
3656 		q_vector->rx.target_itr =
3657 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3658 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3659 		     q_vector->rx.target_itr >> 1);
3660 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3661 
3662 		q_vector->tx.next_update = jiffies + 1;
3663 		q_vector->tx.target_itr =
3664 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3665 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3666 		     q_vector->tx.target_itr >> 1);
3667 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3668 
3669 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3670 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3671 
3672 		/* Linked list for the queuepairs assigned to this vector */
3673 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3674 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3675 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3676 			u32 val;
3677 
3678 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3679 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3680 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3681 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3682 			      (I40E_QUEUE_TYPE_TX <<
3683 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3684 
3685 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3686 
3687 			if (has_xdp) {
3688 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3689 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3690 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3691 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3692 				      (I40E_QUEUE_TYPE_TX <<
3693 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3694 
3695 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3696 			}
3697 
3698 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3699 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3700 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3701 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3702 			      (I40E_QUEUE_TYPE_RX <<
3703 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3704 
3705 			/* Terminate the linked list */
3706 			if (q == (q_vector->num_ringpairs - 1))
3707 				val |= (I40E_QUEUE_END_OF_LIST <<
3708 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3709 
3710 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3711 			qp++;
3712 		}
3713 	}
3714 
3715 	i40e_flush(hw);
3716 }
3717 
3718 /**
3719  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3720  * @pf: pointer to private device data structure
3721  **/
3722 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3723 {
3724 	struct i40e_hw *hw = &pf->hw;
3725 	u32 val;
3726 
3727 	/* clear things first */
3728 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3729 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3730 
3731 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3732 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3733 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3734 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3735 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3736 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3737 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3738 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3739 
3740 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3741 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3742 
3743 	if (pf->flags & I40E_FLAG_PTP)
3744 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3745 
3746 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3747 
3748 	/* SW_ITR_IDX = 0, but don't change INTENA */
3749 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3750 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3751 
3752 	/* OTHER_ITR_IDX = 0 */
3753 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3754 }
3755 
3756 /**
3757  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3758  * @vsi: the VSI being configured
3759  **/
3760 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3761 {
3762 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3763 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3764 	struct i40e_pf *pf = vsi->back;
3765 	struct i40e_hw *hw = &pf->hw;
3766 	u32 val;
3767 
3768 	/* set the ITR configuration */
3769 	q_vector->rx.next_update = jiffies + 1;
3770 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3771 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3772 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3773 	q_vector->tx.next_update = jiffies + 1;
3774 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3775 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3776 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3777 
3778 	i40e_enable_misc_int_causes(pf);
3779 
3780 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3781 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3782 
3783 	/* Associate the queue pair to the vector and enable the queue int */
3784 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3785 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3786 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3787 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3788 
3789 	wr32(hw, I40E_QINT_RQCTL(0), val);
3790 
3791 	if (i40e_enabled_xdp_vsi(vsi)) {
3792 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
3793 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3794 		      (I40E_QUEUE_TYPE_TX
3795 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3796 
3797 		wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3798 	}
3799 
3800 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
3801 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3802 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3803 
3804 	wr32(hw, I40E_QINT_TQCTL(0), val);
3805 	i40e_flush(hw);
3806 }
3807 
3808 /**
3809  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3810  * @pf: board private structure
3811  **/
3812 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3813 {
3814 	struct i40e_hw *hw = &pf->hw;
3815 
3816 	wr32(hw, I40E_PFINT_DYN_CTL0,
3817 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3818 	i40e_flush(hw);
3819 }
3820 
3821 /**
3822  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3823  * @pf: board private structure
3824  **/
3825 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3826 {
3827 	struct i40e_hw *hw = &pf->hw;
3828 	u32 val;
3829 
3830 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
3831 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3832 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3833 
3834 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
3835 	i40e_flush(hw);
3836 }
3837 
3838 /**
3839  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3840  * @irq: interrupt number
3841  * @data: pointer to a q_vector
3842  **/
3843 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3844 {
3845 	struct i40e_q_vector *q_vector = data;
3846 
3847 	if (!q_vector->tx.ring && !q_vector->rx.ring)
3848 		return IRQ_HANDLED;
3849 
3850 	napi_schedule_irqoff(&q_vector->napi);
3851 
3852 	return IRQ_HANDLED;
3853 }
3854 
3855 /**
3856  * i40e_irq_affinity_notify - Callback for affinity changes
3857  * @notify: context as to what irq was changed
3858  * @mask: the new affinity mask
3859  *
3860  * This is a callback function used by the irq_set_affinity_notifier function
3861  * so that we may register to receive changes to the irq affinity masks.
3862  **/
3863 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3864 				     const cpumask_t *mask)
3865 {
3866 	struct i40e_q_vector *q_vector =
3867 		container_of(notify, struct i40e_q_vector, affinity_notify);
3868 
3869 	cpumask_copy(&q_vector->affinity_mask, mask);
3870 }
3871 
3872 /**
3873  * i40e_irq_affinity_release - Callback for affinity notifier release
3874  * @ref: internal core kernel usage
3875  *
3876  * This is a callback function used by the irq_set_affinity_notifier function
3877  * to inform the current notification subscriber that they will no longer
3878  * receive notifications.
3879  **/
3880 static void i40e_irq_affinity_release(struct kref *ref) {}
3881 
3882 /**
3883  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3884  * @vsi: the VSI being configured
3885  * @basename: name for the vector
3886  *
3887  * Allocates MSI-X vectors and requests interrupts from the kernel.
3888  **/
3889 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3890 {
3891 	int q_vectors = vsi->num_q_vectors;
3892 	struct i40e_pf *pf = vsi->back;
3893 	int base = vsi->base_vector;
3894 	int rx_int_idx = 0;
3895 	int tx_int_idx = 0;
3896 	int vector, err;
3897 	int irq_num;
3898 	int cpu;
3899 
3900 	for (vector = 0; vector < q_vectors; vector++) {
3901 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
3902 
3903 		irq_num = pf->msix_entries[base + vector].vector;
3904 
3905 		if (q_vector->tx.ring && q_vector->rx.ring) {
3906 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3907 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3908 			tx_int_idx++;
3909 		} else if (q_vector->rx.ring) {
3910 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3911 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
3912 		} else if (q_vector->tx.ring) {
3913 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3914 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
3915 		} else {
3916 			/* skip this unused q_vector */
3917 			continue;
3918 		}
3919 		err = request_irq(irq_num,
3920 				  vsi->irq_handler,
3921 				  0,
3922 				  q_vector->name,
3923 				  q_vector);
3924 		if (err) {
3925 			dev_info(&pf->pdev->dev,
3926 				 "MSIX request_irq failed, error: %d\n", err);
3927 			goto free_queue_irqs;
3928 		}
3929 
3930 		/* register for affinity change notifications */
3931 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3932 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
3933 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
3934 		/* Spread affinity hints out across online CPUs.
3935 		 *
3936 		 * get_cpu_mask returns a static constant mask with
3937 		 * a permanent lifetime so it's ok to pass to
3938 		 * irq_update_affinity_hint without making a copy.
3939 		 */
3940 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
3941 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
3942 	}
3943 
3944 	vsi->irqs_ready = true;
3945 	return 0;
3946 
3947 free_queue_irqs:
3948 	while (vector) {
3949 		vector--;
3950 		irq_num = pf->msix_entries[base + vector].vector;
3951 		irq_set_affinity_notifier(irq_num, NULL);
3952 		irq_update_affinity_hint(irq_num, NULL);
3953 		free_irq(irq_num, &vsi->q_vectors[vector]);
3954 	}
3955 	return err;
3956 }
3957 
3958 /**
3959  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3960  * @vsi: the VSI being un-configured
3961  **/
3962 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3963 {
3964 	struct i40e_pf *pf = vsi->back;
3965 	struct i40e_hw *hw = &pf->hw;
3966 	int base = vsi->base_vector;
3967 	int i;
3968 
3969 	/* disable interrupt causation from each queue */
3970 	for (i = 0; i < vsi->num_queue_pairs; i++) {
3971 		u32 val;
3972 
3973 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3974 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3975 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3976 
3977 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3978 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3979 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3980 
3981 		if (!i40e_enabled_xdp_vsi(vsi))
3982 			continue;
3983 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
3984 	}
3985 
3986 	/* disable each interrupt */
3987 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3988 		for (i = vsi->base_vector;
3989 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
3990 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3991 
3992 		i40e_flush(hw);
3993 		for (i = 0; i < vsi->num_q_vectors; i++)
3994 			synchronize_irq(pf->msix_entries[i + base].vector);
3995 	} else {
3996 		/* Legacy and MSI mode - this stops all interrupt handling */
3997 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3998 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3999 		i40e_flush(hw);
4000 		synchronize_irq(pf->pdev->irq);
4001 	}
4002 }
4003 
4004 /**
4005  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4006  * @vsi: the VSI being configured
4007  **/
4008 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4009 {
4010 	struct i40e_pf *pf = vsi->back;
4011 	int i;
4012 
4013 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4014 		for (i = 0; i < vsi->num_q_vectors; i++)
4015 			i40e_irq_dynamic_enable(vsi, i);
4016 	} else {
4017 		i40e_irq_dynamic_enable_icr0(pf);
4018 	}
4019 
4020 	i40e_flush(&pf->hw);
4021 	return 0;
4022 }
4023 
4024 /**
4025  * i40e_free_misc_vector - Free the vector that handles non-queue events
4026  * @pf: board private structure
4027  **/
4028 static void i40e_free_misc_vector(struct i40e_pf *pf)
4029 {
4030 	/* Disable ICR 0 */
4031 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4032 	i40e_flush(&pf->hw);
4033 
4034 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4035 		synchronize_irq(pf->msix_entries[0].vector);
4036 		free_irq(pf->msix_entries[0].vector, pf);
4037 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4038 	}
4039 }
4040 
4041 /**
4042  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4043  * @irq: interrupt number
4044  * @data: pointer to a q_vector
4045  *
4046  * This is the handler used for all MSI/Legacy interrupts, and deals
4047  * with both queue and non-queue interrupts.  This is also used in
4048  * MSIX mode to handle the non-queue interrupts.
4049  **/
4050 static irqreturn_t i40e_intr(int irq, void *data)
4051 {
4052 	struct i40e_pf *pf = (struct i40e_pf *)data;
4053 	struct i40e_hw *hw = &pf->hw;
4054 	irqreturn_t ret = IRQ_NONE;
4055 	u32 icr0, icr0_remaining;
4056 	u32 val, ena_mask;
4057 
4058 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4059 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4060 
4061 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4062 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4063 		goto enable_intr;
4064 
4065 	/* if interrupt but no bits showing, must be SWINT */
4066 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4067 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4068 		pf->sw_int_count++;
4069 
4070 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4071 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4072 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4073 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4074 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4075 	}
4076 
4077 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4078 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4079 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4080 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4081 
4082 		/* We do not have a way to disarm Queue causes while leaving
4083 		 * interrupt enabled for all other causes, ideally
4084 		 * interrupt should be disabled while we are in NAPI but
4085 		 * this is not a performance path and napi_schedule()
4086 		 * can deal with rescheduling.
4087 		 */
4088 		if (!test_bit(__I40E_DOWN, pf->state))
4089 			napi_schedule_irqoff(&q_vector->napi);
4090 	}
4091 
4092 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4093 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4094 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4095 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4096 	}
4097 
4098 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4099 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4100 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4101 	}
4102 
4103 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4104 		/* disable any further VFLR event notifications */
4105 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4106 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4107 
4108 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4109 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4110 		} else {
4111 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4112 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4113 		}
4114 	}
4115 
4116 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4117 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4118 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4119 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4120 		val = rd32(hw, I40E_GLGEN_RSTAT);
4121 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4122 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4123 		if (val == I40E_RESET_CORER) {
4124 			pf->corer_count++;
4125 		} else if (val == I40E_RESET_GLOBR) {
4126 			pf->globr_count++;
4127 		} else if (val == I40E_RESET_EMPR) {
4128 			pf->empr_count++;
4129 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4130 		}
4131 	}
4132 
4133 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4134 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4135 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4136 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4137 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4138 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4139 	}
4140 
4141 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4142 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4143 
4144 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4145 			schedule_work(&pf->ptp_extts0_work);
4146 
4147 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4148 			i40e_ptp_tx_hwtstamp(pf);
4149 
4150 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4151 	}
4152 
4153 	/* If a critical error is pending we have no choice but to reset the
4154 	 * device.
4155 	 * Report and mask out any remaining unexpected interrupts.
4156 	 */
4157 	icr0_remaining = icr0 & ena_mask;
4158 	if (icr0_remaining) {
4159 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4160 			 icr0_remaining);
4161 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4162 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4163 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4164 			dev_info(&pf->pdev->dev, "device will be reset\n");
4165 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4166 			i40e_service_event_schedule(pf);
4167 		}
4168 		ena_mask &= ~icr0_remaining;
4169 	}
4170 	ret = IRQ_HANDLED;
4171 
4172 enable_intr:
4173 	/* re-enable interrupt causes */
4174 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4175 	if (!test_bit(__I40E_DOWN, pf->state) ||
4176 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4177 		i40e_service_event_schedule(pf);
4178 		i40e_irq_dynamic_enable_icr0(pf);
4179 	}
4180 
4181 	return ret;
4182 }
4183 
4184 /**
4185  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4186  * @tx_ring:  tx ring to clean
4187  * @budget:   how many cleans we're allowed
4188  *
4189  * Returns true if there's any budget left (e.g. the clean is finished)
4190  **/
4191 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4192 {
4193 	struct i40e_vsi *vsi = tx_ring->vsi;
4194 	u16 i = tx_ring->next_to_clean;
4195 	struct i40e_tx_buffer *tx_buf;
4196 	struct i40e_tx_desc *tx_desc;
4197 
4198 	tx_buf = &tx_ring->tx_bi[i];
4199 	tx_desc = I40E_TX_DESC(tx_ring, i);
4200 	i -= tx_ring->count;
4201 
4202 	do {
4203 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4204 
4205 		/* if next_to_watch is not set then there is no work pending */
4206 		if (!eop_desc)
4207 			break;
4208 
4209 		/* prevent any other reads prior to eop_desc */
4210 		smp_rmb();
4211 
4212 		/* if the descriptor isn't done, no work yet to do */
4213 		if (!(eop_desc->cmd_type_offset_bsz &
4214 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4215 			break;
4216 
4217 		/* clear next_to_watch to prevent false hangs */
4218 		tx_buf->next_to_watch = NULL;
4219 
4220 		tx_desc->buffer_addr = 0;
4221 		tx_desc->cmd_type_offset_bsz = 0;
4222 		/* move past filter desc */
4223 		tx_buf++;
4224 		tx_desc++;
4225 		i++;
4226 		if (unlikely(!i)) {
4227 			i -= tx_ring->count;
4228 			tx_buf = tx_ring->tx_bi;
4229 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4230 		}
4231 		/* unmap skb header data */
4232 		dma_unmap_single(tx_ring->dev,
4233 				 dma_unmap_addr(tx_buf, dma),
4234 				 dma_unmap_len(tx_buf, len),
4235 				 DMA_TO_DEVICE);
4236 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4237 			kfree(tx_buf->raw_buf);
4238 
4239 		tx_buf->raw_buf = NULL;
4240 		tx_buf->tx_flags = 0;
4241 		tx_buf->next_to_watch = NULL;
4242 		dma_unmap_len_set(tx_buf, len, 0);
4243 		tx_desc->buffer_addr = 0;
4244 		tx_desc->cmd_type_offset_bsz = 0;
4245 
4246 		/* move us past the eop_desc for start of next FD desc */
4247 		tx_buf++;
4248 		tx_desc++;
4249 		i++;
4250 		if (unlikely(!i)) {
4251 			i -= tx_ring->count;
4252 			tx_buf = tx_ring->tx_bi;
4253 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4254 		}
4255 
4256 		/* update budget accounting */
4257 		budget--;
4258 	} while (likely(budget));
4259 
4260 	i += tx_ring->count;
4261 	tx_ring->next_to_clean = i;
4262 
4263 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4264 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4265 
4266 	return budget > 0;
4267 }
4268 
4269 /**
4270  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4271  * @irq: interrupt number
4272  * @data: pointer to a q_vector
4273  **/
4274 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4275 {
4276 	struct i40e_q_vector *q_vector = data;
4277 	struct i40e_vsi *vsi;
4278 
4279 	if (!q_vector->tx.ring)
4280 		return IRQ_HANDLED;
4281 
4282 	vsi = q_vector->tx.ring->vsi;
4283 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4284 
4285 	return IRQ_HANDLED;
4286 }
4287 
4288 /**
4289  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4290  * @vsi: the VSI being configured
4291  * @v_idx: vector index
4292  * @qp_idx: queue pair index
4293  **/
4294 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4295 {
4296 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4297 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4298 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4299 
4300 	tx_ring->q_vector = q_vector;
4301 	tx_ring->next = q_vector->tx.ring;
4302 	q_vector->tx.ring = tx_ring;
4303 	q_vector->tx.count++;
4304 
4305 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4306 	if (i40e_enabled_xdp_vsi(vsi)) {
4307 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4308 
4309 		xdp_ring->q_vector = q_vector;
4310 		xdp_ring->next = q_vector->tx.ring;
4311 		q_vector->tx.ring = xdp_ring;
4312 		q_vector->tx.count++;
4313 	}
4314 
4315 	rx_ring->q_vector = q_vector;
4316 	rx_ring->next = q_vector->rx.ring;
4317 	q_vector->rx.ring = rx_ring;
4318 	q_vector->rx.count++;
4319 }
4320 
4321 /**
4322  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4323  * @vsi: the VSI being configured
4324  *
4325  * This function maps descriptor rings to the queue-specific vectors
4326  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4327  * one vector per queue pair, but on a constrained vector budget, we
4328  * group the queue pairs as "efficiently" as possible.
4329  **/
4330 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4331 {
4332 	int qp_remaining = vsi->num_queue_pairs;
4333 	int q_vectors = vsi->num_q_vectors;
4334 	int num_ringpairs;
4335 	int v_start = 0;
4336 	int qp_idx = 0;
4337 
4338 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4339 	 * group them so there are multiple queues per vector.
4340 	 * It is also important to go through all the vectors available to be
4341 	 * sure that if we don't use all the vectors, that the remaining vectors
4342 	 * are cleared. This is especially important when decreasing the
4343 	 * number of queues in use.
4344 	 */
4345 	for (; v_start < q_vectors; v_start++) {
4346 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4347 
4348 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4349 
4350 		q_vector->num_ringpairs = num_ringpairs;
4351 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4352 
4353 		q_vector->rx.count = 0;
4354 		q_vector->tx.count = 0;
4355 		q_vector->rx.ring = NULL;
4356 		q_vector->tx.ring = NULL;
4357 
4358 		while (num_ringpairs--) {
4359 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4360 			qp_idx++;
4361 			qp_remaining--;
4362 		}
4363 	}
4364 }
4365 
4366 /**
4367  * i40e_vsi_request_irq - Request IRQ from the OS
4368  * @vsi: the VSI being configured
4369  * @basename: name for the vector
4370  **/
4371 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4372 {
4373 	struct i40e_pf *pf = vsi->back;
4374 	int err;
4375 
4376 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4377 		err = i40e_vsi_request_irq_msix(vsi, basename);
4378 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4379 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4380 				  pf->int_name, pf);
4381 	else
4382 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4383 				  pf->int_name, pf);
4384 
4385 	if (err)
4386 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4387 
4388 	return err;
4389 }
4390 
4391 #ifdef CONFIG_NET_POLL_CONTROLLER
4392 /**
4393  * i40e_netpoll - A Polling 'interrupt' handler
4394  * @netdev: network interface device structure
4395  *
4396  * This is used by netconsole to send skbs without having to re-enable
4397  * interrupts.  It's not called while the normal interrupt routine is executing.
4398  **/
4399 static void i40e_netpoll(struct net_device *netdev)
4400 {
4401 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4402 	struct i40e_vsi *vsi = np->vsi;
4403 	struct i40e_pf *pf = vsi->back;
4404 	int i;
4405 
4406 	/* if interface is down do nothing */
4407 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4408 		return;
4409 
4410 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4411 		for (i = 0; i < vsi->num_q_vectors; i++)
4412 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4413 	} else {
4414 		i40e_intr(pf->pdev->irq, netdev);
4415 	}
4416 }
4417 #endif
4418 
4419 #define I40E_QTX_ENA_WAIT_COUNT 50
4420 
4421 /**
4422  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4423  * @pf: the PF being configured
4424  * @pf_q: the PF queue
4425  * @enable: enable or disable state of the queue
4426  *
4427  * This routine will wait for the given Tx queue of the PF to reach the
4428  * enabled or disabled state.
4429  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4430  * multiple retries; else will return 0 in case of success.
4431  **/
4432 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4433 {
4434 	int i;
4435 	u32 tx_reg;
4436 
4437 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4438 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4439 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4440 			break;
4441 
4442 		usleep_range(10, 20);
4443 	}
4444 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4445 		return -ETIMEDOUT;
4446 
4447 	return 0;
4448 }
4449 
4450 /**
4451  * i40e_control_tx_q - Start or stop a particular Tx queue
4452  * @pf: the PF structure
4453  * @pf_q: the PF queue to configure
4454  * @enable: start or stop the queue
4455  *
4456  * This function enables or disables a single queue. Note that any delay
4457  * required after the operation is expected to be handled by the caller of
4458  * this function.
4459  **/
4460 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4461 {
4462 	struct i40e_hw *hw = &pf->hw;
4463 	u32 tx_reg;
4464 	int i;
4465 
4466 	/* warn the TX unit of coming changes */
4467 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4468 	if (!enable)
4469 		usleep_range(10, 20);
4470 
4471 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4472 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4473 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4474 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4475 			break;
4476 		usleep_range(1000, 2000);
4477 	}
4478 
4479 	/* Skip if the queue is already in the requested state */
4480 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4481 		return;
4482 
4483 	/* turn on/off the queue */
4484 	if (enable) {
4485 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4486 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4487 	} else {
4488 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4489 	}
4490 
4491 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4492 }
4493 
4494 /**
4495  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4496  * @seid: VSI SEID
4497  * @pf: the PF structure
4498  * @pf_q: the PF queue to configure
4499  * @is_xdp: true if the queue is used for XDP
4500  * @enable: start or stop the queue
4501  **/
4502 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4503 			   bool is_xdp, bool enable)
4504 {
4505 	int ret;
4506 
4507 	i40e_control_tx_q(pf, pf_q, enable);
4508 
4509 	/* wait for the change to finish */
4510 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4511 	if (ret) {
4512 		dev_info(&pf->pdev->dev,
4513 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4514 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4515 			 (enable ? "en" : "dis"));
4516 	}
4517 
4518 	return ret;
4519 }
4520 
4521 /**
4522  * i40e_vsi_enable_tx - Start a VSI's rings
4523  * @vsi: the VSI being configured
4524  **/
4525 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4526 {
4527 	struct i40e_pf *pf = vsi->back;
4528 	int i, pf_q, ret = 0;
4529 
4530 	pf_q = vsi->base_queue;
4531 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4532 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4533 					     pf_q,
4534 					     false /*is xdp*/, true);
4535 		if (ret)
4536 			break;
4537 
4538 		if (!i40e_enabled_xdp_vsi(vsi))
4539 			continue;
4540 
4541 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4542 					     pf_q + vsi->alloc_queue_pairs,
4543 					     true /*is xdp*/, true);
4544 		if (ret)
4545 			break;
4546 	}
4547 	return ret;
4548 }
4549 
4550 /**
4551  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4552  * @pf: the PF being configured
4553  * @pf_q: the PF queue
4554  * @enable: enable or disable state of the queue
4555  *
4556  * This routine will wait for the given Rx queue of the PF to reach the
4557  * enabled or disabled state.
4558  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4559  * multiple retries; else will return 0 in case of success.
4560  **/
4561 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4562 {
4563 	int i;
4564 	u32 rx_reg;
4565 
4566 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4567 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4568 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4569 			break;
4570 
4571 		usleep_range(10, 20);
4572 	}
4573 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4574 		return -ETIMEDOUT;
4575 
4576 	return 0;
4577 }
4578 
4579 /**
4580  * i40e_control_rx_q - Start or stop a particular Rx queue
4581  * @pf: the PF structure
4582  * @pf_q: the PF queue to configure
4583  * @enable: start or stop the queue
4584  *
4585  * This function enables or disables a single queue. Note that
4586  * any delay required after the operation is expected to be
4587  * handled by the caller of this function.
4588  **/
4589 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4590 {
4591 	struct i40e_hw *hw = &pf->hw;
4592 	u32 rx_reg;
4593 	int i;
4594 
4595 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4596 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4597 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4598 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4599 			break;
4600 		usleep_range(1000, 2000);
4601 	}
4602 
4603 	/* Skip if the queue is already in the requested state */
4604 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4605 		return;
4606 
4607 	/* turn on/off the queue */
4608 	if (enable)
4609 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4610 	else
4611 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4612 
4613 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4614 }
4615 
4616 /**
4617  * i40e_control_wait_rx_q
4618  * @pf: the PF structure
4619  * @pf_q: queue being configured
4620  * @enable: start or stop the rings
4621  *
4622  * This function enables or disables a single queue along with waiting
4623  * for the change to finish. The caller of this function should handle
4624  * the delays needed in the case of disabling queues.
4625  **/
4626 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4627 {
4628 	int ret = 0;
4629 
4630 	i40e_control_rx_q(pf, pf_q, enable);
4631 
4632 	/* wait for the change to finish */
4633 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4634 	if (ret)
4635 		return ret;
4636 
4637 	return ret;
4638 }
4639 
4640 /**
4641  * i40e_vsi_enable_rx - Start a VSI's rings
4642  * @vsi: the VSI being configured
4643  **/
4644 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4645 {
4646 	struct i40e_pf *pf = vsi->back;
4647 	int i, pf_q, ret = 0;
4648 
4649 	pf_q = vsi->base_queue;
4650 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4651 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4652 		if (ret) {
4653 			dev_info(&pf->pdev->dev,
4654 				 "VSI seid %d Rx ring %d enable timeout\n",
4655 				 vsi->seid, pf_q);
4656 			break;
4657 		}
4658 	}
4659 
4660 	return ret;
4661 }
4662 
4663 /**
4664  * i40e_vsi_start_rings - Start a VSI's rings
4665  * @vsi: the VSI being configured
4666  **/
4667 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4668 {
4669 	int ret = 0;
4670 
4671 	/* do rx first for enable and last for disable */
4672 	ret = i40e_vsi_enable_rx(vsi);
4673 	if (ret)
4674 		return ret;
4675 	ret = i40e_vsi_enable_tx(vsi);
4676 
4677 	return ret;
4678 }
4679 
4680 #define I40E_DISABLE_TX_GAP_MSEC	50
4681 
4682 /**
4683  * i40e_vsi_stop_rings - Stop a VSI's rings
4684  * @vsi: the VSI being configured
4685  **/
4686 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4687 {
4688 	struct i40e_pf *pf = vsi->back;
4689 	int pf_q, err, q_end;
4690 
4691 	/* When port TX is suspended, don't wait */
4692 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4693 		return i40e_vsi_stop_rings_no_wait(vsi);
4694 
4695 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4696 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4697 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4698 
4699 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4700 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4701 		if (err)
4702 			dev_info(&pf->pdev->dev,
4703 				 "VSI seid %d Rx ring %d disable timeout\n",
4704 				 vsi->seid, pf_q);
4705 	}
4706 
4707 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4708 	pf_q = vsi->base_queue;
4709 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4710 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4711 
4712 	i40e_vsi_wait_queues_disabled(vsi);
4713 }
4714 
4715 /**
4716  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4717  * @vsi: the VSI being shutdown
4718  *
4719  * This function stops all the rings for a VSI but does not delay to verify
4720  * that rings have been disabled. It is expected that the caller is shutting
4721  * down multiple VSIs at once and will delay together for all the VSIs after
4722  * initiating the shutdown. This is particularly useful for shutting down lots
4723  * of VFs together. Otherwise, a large delay can be incurred while configuring
4724  * each VSI in serial.
4725  **/
4726 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4727 {
4728 	struct i40e_pf *pf = vsi->back;
4729 	int i, pf_q;
4730 
4731 	pf_q = vsi->base_queue;
4732 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4733 		i40e_control_tx_q(pf, pf_q, false);
4734 		i40e_control_rx_q(pf, pf_q, false);
4735 	}
4736 }
4737 
4738 /**
4739  * i40e_vsi_free_irq - Free the irq association with the OS
4740  * @vsi: the VSI being configured
4741  **/
4742 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4743 {
4744 	struct i40e_pf *pf = vsi->back;
4745 	struct i40e_hw *hw = &pf->hw;
4746 	int base = vsi->base_vector;
4747 	u32 val, qp;
4748 	int i;
4749 
4750 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4751 		if (!vsi->q_vectors)
4752 			return;
4753 
4754 		if (!vsi->irqs_ready)
4755 			return;
4756 
4757 		vsi->irqs_ready = false;
4758 		for (i = 0; i < vsi->num_q_vectors; i++) {
4759 			int irq_num;
4760 			u16 vector;
4761 
4762 			vector = i + base;
4763 			irq_num = pf->msix_entries[vector].vector;
4764 
4765 			/* free only the irqs that were actually requested */
4766 			if (!vsi->q_vectors[i] ||
4767 			    !vsi->q_vectors[i]->num_ringpairs)
4768 				continue;
4769 
4770 			/* clear the affinity notifier in the IRQ descriptor */
4771 			irq_set_affinity_notifier(irq_num, NULL);
4772 			/* remove our suggested affinity mask for this IRQ */
4773 			irq_update_affinity_hint(irq_num, NULL);
4774 			synchronize_irq(irq_num);
4775 			free_irq(irq_num, vsi->q_vectors[i]);
4776 
4777 			/* Tear down the interrupt queue link list
4778 			 *
4779 			 * We know that they come in pairs and always
4780 			 * the Rx first, then the Tx.  To clear the
4781 			 * link list, stick the EOL value into the
4782 			 * next_q field of the registers.
4783 			 */
4784 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4785 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4786 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4787 			val |= I40E_QUEUE_END_OF_LIST
4788 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4789 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4790 
4791 			while (qp != I40E_QUEUE_END_OF_LIST) {
4792 				u32 next;
4793 
4794 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4795 
4796 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4797 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4798 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4799 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4800 
4801 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4802 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4803 
4804 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4805 
4806 				val = rd32(hw, I40E_QINT_TQCTL(qp));
4807 
4808 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4809 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4810 
4811 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4812 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4813 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4814 					 I40E_QINT_TQCTL_INTEVENT_MASK);
4815 
4816 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4817 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4818 
4819 				wr32(hw, I40E_QINT_TQCTL(qp), val);
4820 				qp = next;
4821 			}
4822 		}
4823 	} else {
4824 		free_irq(pf->pdev->irq, pf);
4825 
4826 		val = rd32(hw, I40E_PFINT_LNKLST0);
4827 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4828 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4829 		val |= I40E_QUEUE_END_OF_LIST
4830 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4831 		wr32(hw, I40E_PFINT_LNKLST0, val);
4832 
4833 		val = rd32(hw, I40E_QINT_RQCTL(qp));
4834 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4835 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4836 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4837 			 I40E_QINT_RQCTL_INTEVENT_MASK);
4838 
4839 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4840 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4841 
4842 		wr32(hw, I40E_QINT_RQCTL(qp), val);
4843 
4844 		val = rd32(hw, I40E_QINT_TQCTL(qp));
4845 
4846 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4847 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4848 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4849 			 I40E_QINT_TQCTL_INTEVENT_MASK);
4850 
4851 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4852 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4853 
4854 		wr32(hw, I40E_QINT_TQCTL(qp), val);
4855 	}
4856 }
4857 
4858 /**
4859  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4860  * @vsi: the VSI being configured
4861  * @v_idx: Index of vector to be freed
4862  *
4863  * This function frees the memory allocated to the q_vector.  In addition if
4864  * NAPI is enabled it will delete any references to the NAPI struct prior
4865  * to freeing the q_vector.
4866  **/
4867 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4868 {
4869 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4870 	struct i40e_ring *ring;
4871 
4872 	if (!q_vector)
4873 		return;
4874 
4875 	/* disassociate q_vector from rings */
4876 	i40e_for_each_ring(ring, q_vector->tx)
4877 		ring->q_vector = NULL;
4878 
4879 	i40e_for_each_ring(ring, q_vector->rx)
4880 		ring->q_vector = NULL;
4881 
4882 	/* only VSI w/ an associated netdev is set up w/ NAPI */
4883 	if (vsi->netdev)
4884 		netif_napi_del(&q_vector->napi);
4885 
4886 	vsi->q_vectors[v_idx] = NULL;
4887 
4888 	kfree_rcu(q_vector, rcu);
4889 }
4890 
4891 /**
4892  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4893  * @vsi: the VSI being un-configured
4894  *
4895  * This frees the memory allocated to the q_vectors and
4896  * deletes references to the NAPI struct.
4897  **/
4898 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4899 {
4900 	int v_idx;
4901 
4902 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4903 		i40e_free_q_vector(vsi, v_idx);
4904 }
4905 
4906 /**
4907  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4908  * @pf: board private structure
4909  **/
4910 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4911 {
4912 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4913 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4914 		pci_disable_msix(pf->pdev);
4915 		kfree(pf->msix_entries);
4916 		pf->msix_entries = NULL;
4917 		kfree(pf->irq_pile);
4918 		pf->irq_pile = NULL;
4919 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4920 		pci_disable_msi(pf->pdev);
4921 	}
4922 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4923 }
4924 
4925 /**
4926  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4927  * @pf: board private structure
4928  *
4929  * We go through and clear interrupt specific resources and reset the structure
4930  * to pre-load conditions
4931  **/
4932 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4933 {
4934 	int i;
4935 
4936 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
4937 		i40e_free_misc_vector(pf);
4938 
4939 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4940 		      I40E_IWARP_IRQ_PILE_ID);
4941 
4942 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
4943 	for (i = 0; i < pf->num_alloc_vsi; i++)
4944 		if (pf->vsi[i])
4945 			i40e_vsi_free_q_vectors(pf->vsi[i]);
4946 	i40e_reset_interrupt_capability(pf);
4947 }
4948 
4949 /**
4950  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4951  * @vsi: the VSI being configured
4952  **/
4953 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4954 {
4955 	int q_idx;
4956 
4957 	if (!vsi->netdev)
4958 		return;
4959 
4960 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4961 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4962 
4963 		if (q_vector->rx.ring || q_vector->tx.ring)
4964 			napi_enable(&q_vector->napi);
4965 	}
4966 }
4967 
4968 /**
4969  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4970  * @vsi: the VSI being configured
4971  **/
4972 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4973 {
4974 	int q_idx;
4975 
4976 	if (!vsi->netdev)
4977 		return;
4978 
4979 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4980 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4981 
4982 		if (q_vector->rx.ring || q_vector->tx.ring)
4983 			napi_disable(&q_vector->napi);
4984 	}
4985 }
4986 
4987 /**
4988  * i40e_vsi_close - Shut down a VSI
4989  * @vsi: the vsi to be quelled
4990  **/
4991 static void i40e_vsi_close(struct i40e_vsi *vsi)
4992 {
4993 	struct i40e_pf *pf = vsi->back;
4994 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
4995 		i40e_down(vsi);
4996 	i40e_vsi_free_irq(vsi);
4997 	i40e_vsi_free_tx_resources(vsi);
4998 	i40e_vsi_free_rx_resources(vsi);
4999 	vsi->current_netdev_flags = 0;
5000 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5001 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5002 		set_bit(__I40E_CLIENT_RESET, pf->state);
5003 }
5004 
5005 /**
5006  * i40e_quiesce_vsi - Pause a given VSI
5007  * @vsi: the VSI being paused
5008  **/
5009 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5010 {
5011 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5012 		return;
5013 
5014 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5015 	if (vsi->netdev && netif_running(vsi->netdev))
5016 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5017 	else
5018 		i40e_vsi_close(vsi);
5019 }
5020 
5021 /**
5022  * i40e_unquiesce_vsi - Resume a given VSI
5023  * @vsi: the VSI being resumed
5024  **/
5025 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5026 {
5027 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5028 		return;
5029 
5030 	if (vsi->netdev && netif_running(vsi->netdev))
5031 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5032 	else
5033 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5034 }
5035 
5036 /**
5037  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5038  * @pf: the PF
5039  **/
5040 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5041 {
5042 	int v;
5043 
5044 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5045 		if (pf->vsi[v])
5046 			i40e_quiesce_vsi(pf->vsi[v]);
5047 	}
5048 }
5049 
5050 /**
5051  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5052  * @pf: the PF
5053  **/
5054 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5055 {
5056 	int v;
5057 
5058 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5059 		if (pf->vsi[v])
5060 			i40e_unquiesce_vsi(pf->vsi[v]);
5061 	}
5062 }
5063 
5064 /**
5065  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5066  * @vsi: the VSI being configured
5067  *
5068  * Wait until all queues on a given VSI have been disabled.
5069  **/
5070 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5071 {
5072 	struct i40e_pf *pf = vsi->back;
5073 	int i, pf_q, ret;
5074 
5075 	pf_q = vsi->base_queue;
5076 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5077 		/* Check and wait for the Tx queue */
5078 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5079 		if (ret) {
5080 			dev_info(&pf->pdev->dev,
5081 				 "VSI seid %d Tx ring %d disable timeout\n",
5082 				 vsi->seid, pf_q);
5083 			return ret;
5084 		}
5085 
5086 		if (!i40e_enabled_xdp_vsi(vsi))
5087 			goto wait_rx;
5088 
5089 		/* Check and wait for the XDP Tx queue */
5090 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5091 				       false);
5092 		if (ret) {
5093 			dev_info(&pf->pdev->dev,
5094 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5095 				 vsi->seid, pf_q);
5096 			return ret;
5097 		}
5098 wait_rx:
5099 		/* Check and wait for the Rx queue */
5100 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5101 		if (ret) {
5102 			dev_info(&pf->pdev->dev,
5103 				 "VSI seid %d Rx ring %d disable timeout\n",
5104 				 vsi->seid, pf_q);
5105 			return ret;
5106 		}
5107 	}
5108 
5109 	return 0;
5110 }
5111 
5112 #ifdef CONFIG_I40E_DCB
5113 /**
5114  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5115  * @pf: the PF
5116  *
5117  * This function waits for the queues to be in disabled state for all the
5118  * VSIs that are managed by this PF.
5119  **/
5120 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5121 {
5122 	int v, ret = 0;
5123 
5124 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5125 		if (pf->vsi[v]) {
5126 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5127 			if (ret)
5128 				break;
5129 		}
5130 	}
5131 
5132 	return ret;
5133 }
5134 
5135 #endif
5136 
5137 /**
5138  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5139  * @pf: pointer to PF
5140  *
5141  * Get TC map for ISCSI PF type that will include iSCSI TC
5142  * and LAN TC.
5143  **/
5144 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5145 {
5146 	struct i40e_dcb_app_priority_table app;
5147 	struct i40e_hw *hw = &pf->hw;
5148 	u8 enabled_tc = 1; /* TC0 is always enabled */
5149 	u8 tc, i;
5150 	/* Get the iSCSI APP TLV */
5151 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5152 
5153 	for (i = 0; i < dcbcfg->numapps; i++) {
5154 		app = dcbcfg->app[i];
5155 		if (app.selector == I40E_APP_SEL_TCPIP &&
5156 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5157 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5158 			enabled_tc |= BIT(tc);
5159 			break;
5160 		}
5161 	}
5162 
5163 	return enabled_tc;
5164 }
5165 
5166 /**
5167  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5168  * @dcbcfg: the corresponding DCBx configuration structure
5169  *
5170  * Return the number of TCs from given DCBx configuration
5171  **/
5172 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5173 {
5174 	int i, tc_unused = 0;
5175 	u8 num_tc = 0;
5176 	u8 ret = 0;
5177 
5178 	/* Scan the ETS Config Priority Table to find
5179 	 * traffic class enabled for a given priority
5180 	 * and create a bitmask of enabled TCs
5181 	 */
5182 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5183 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5184 
5185 	/* Now scan the bitmask to check for
5186 	 * contiguous TCs starting with TC0
5187 	 */
5188 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5189 		if (num_tc & BIT(i)) {
5190 			if (!tc_unused) {
5191 				ret++;
5192 			} else {
5193 				pr_err("Non-contiguous TC - Disabling DCB\n");
5194 				return 1;
5195 			}
5196 		} else {
5197 			tc_unused = 1;
5198 		}
5199 	}
5200 
5201 	/* There is always at least TC0 */
5202 	if (!ret)
5203 		ret = 1;
5204 
5205 	return ret;
5206 }
5207 
5208 /**
5209  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5210  * @dcbcfg: the corresponding DCBx configuration structure
5211  *
5212  * Query the current DCB configuration and return the number of
5213  * traffic classes enabled from the given DCBX config
5214  **/
5215 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5216 {
5217 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5218 	u8 enabled_tc = 1;
5219 	u8 i;
5220 
5221 	for (i = 0; i < num_tc; i++)
5222 		enabled_tc |= BIT(i);
5223 
5224 	return enabled_tc;
5225 }
5226 
5227 /**
5228  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5229  * @pf: PF being queried
5230  *
5231  * Query the current MQPRIO configuration and return the number of
5232  * traffic classes enabled.
5233  **/
5234 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5235 {
5236 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5237 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5238 	u8 enabled_tc = 1, i;
5239 
5240 	for (i = 1; i < num_tc; i++)
5241 		enabled_tc |= BIT(i);
5242 	return enabled_tc;
5243 }
5244 
5245 /**
5246  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5247  * @pf: PF being queried
5248  *
5249  * Return number of traffic classes enabled for the given PF
5250  **/
5251 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5252 {
5253 	struct i40e_hw *hw = &pf->hw;
5254 	u8 i, enabled_tc = 1;
5255 	u8 num_tc = 0;
5256 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5257 
5258 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5259 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5260 
5261 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5262 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5263 		return 1;
5264 
5265 	/* SFP mode will be enabled for all TCs on port */
5266 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5267 		return i40e_dcb_get_num_tc(dcbcfg);
5268 
5269 	/* MFP mode return count of enabled TCs for this PF */
5270 	if (pf->hw.func_caps.iscsi)
5271 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5272 	else
5273 		return 1; /* Only TC0 */
5274 
5275 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5276 		if (enabled_tc & BIT(i))
5277 			num_tc++;
5278 	}
5279 	return num_tc;
5280 }
5281 
5282 /**
5283  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5284  * @pf: PF being queried
5285  *
5286  * Return a bitmap for enabled traffic classes for this PF.
5287  **/
5288 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5289 {
5290 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5291 		return i40e_mqprio_get_enabled_tc(pf);
5292 
5293 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5294 	 * default TC
5295 	 */
5296 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5297 		return I40E_DEFAULT_TRAFFIC_CLASS;
5298 
5299 	/* SFP mode we want PF to be enabled for all TCs */
5300 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5301 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5302 
5303 	/* MFP enabled and iSCSI PF type */
5304 	if (pf->hw.func_caps.iscsi)
5305 		return i40e_get_iscsi_tc_map(pf);
5306 	else
5307 		return I40E_DEFAULT_TRAFFIC_CLASS;
5308 }
5309 
5310 /**
5311  * i40e_vsi_get_bw_info - Query VSI BW Information
5312  * @vsi: the VSI being queried
5313  *
5314  * Returns 0 on success, negative value on failure
5315  **/
5316 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5317 {
5318 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5319 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5320 	struct i40e_pf *pf = vsi->back;
5321 	struct i40e_hw *hw = &pf->hw;
5322 	i40e_status ret;
5323 	u32 tc_bw_max;
5324 	int i;
5325 
5326 	/* Get the VSI level BW configuration */
5327 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5328 	if (ret) {
5329 		dev_info(&pf->pdev->dev,
5330 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5331 			 i40e_stat_str(&pf->hw, ret),
5332 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5333 		return -EINVAL;
5334 	}
5335 
5336 	/* Get the VSI level BW configuration per TC */
5337 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5338 					       NULL);
5339 	if (ret) {
5340 		dev_info(&pf->pdev->dev,
5341 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5342 			 i40e_stat_str(&pf->hw, ret),
5343 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5344 		return -EINVAL;
5345 	}
5346 
5347 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5348 		dev_info(&pf->pdev->dev,
5349 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5350 			 bw_config.tc_valid_bits,
5351 			 bw_ets_config.tc_valid_bits);
5352 		/* Still continuing */
5353 	}
5354 
5355 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5356 	vsi->bw_max_quanta = bw_config.max_bw;
5357 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5358 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5359 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5360 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5361 		vsi->bw_ets_limit_credits[i] =
5362 					le16_to_cpu(bw_ets_config.credits[i]);
5363 		/* 3 bits out of 4 for each TC */
5364 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5365 	}
5366 
5367 	return 0;
5368 }
5369 
5370 /**
5371  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5372  * @vsi: the VSI being configured
5373  * @enabled_tc: TC bitmap
5374  * @bw_share: BW shared credits per TC
5375  *
5376  * Returns 0 on success, negative value on failure
5377  **/
5378 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5379 				       u8 *bw_share)
5380 {
5381 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5382 	struct i40e_pf *pf = vsi->back;
5383 	i40e_status ret;
5384 	int i;
5385 
5386 	/* There is no need to reset BW when mqprio mode is on.  */
5387 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5388 		return 0;
5389 
5390 	if (!vsi->mqprio_qopt.qopt.hw) {
5391 		if (pf->flags & I40E_FLAG_DCB_ENABLED)
5392 			goto skip_reset;
5393 
5394 		if (IS_ENABLED(CONFIG_I40E_DCB) &&
5395 		    i40e_dcb_hw_get_num_tc(&pf->hw) == 1)
5396 			goto skip_reset;
5397 
5398 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5399 		if (ret)
5400 			dev_info(&pf->pdev->dev,
5401 				 "Failed to reset tx rate for vsi->seid %u\n",
5402 				 vsi->seid);
5403 		return ret;
5404 	}
5405 
5406 skip_reset:
5407 	memset(&bw_data, 0, sizeof(bw_data));
5408 	bw_data.tc_valid_bits = enabled_tc;
5409 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5410 		bw_data.tc_bw_credits[i] = bw_share[i];
5411 
5412 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5413 	if (ret) {
5414 		dev_info(&pf->pdev->dev,
5415 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5416 			 pf->hw.aq.asq_last_status);
5417 		return -EINVAL;
5418 	}
5419 
5420 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5421 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5422 
5423 	return 0;
5424 }
5425 
5426 /**
5427  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5428  * @vsi: the VSI being configured
5429  * @enabled_tc: TC map to be enabled
5430  *
5431  **/
5432 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5433 {
5434 	struct net_device *netdev = vsi->netdev;
5435 	struct i40e_pf *pf = vsi->back;
5436 	struct i40e_hw *hw = &pf->hw;
5437 	u8 netdev_tc = 0;
5438 	int i;
5439 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5440 
5441 	if (!netdev)
5442 		return;
5443 
5444 	if (!enabled_tc) {
5445 		netdev_reset_tc(netdev);
5446 		return;
5447 	}
5448 
5449 	/* Set up actual enabled TCs on the VSI */
5450 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5451 		return;
5452 
5453 	/* set per TC queues for the VSI */
5454 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5455 		/* Only set TC queues for enabled tcs
5456 		 *
5457 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5458 		 * enabled_tc bitmap would be 0x00001001; the driver
5459 		 * will set the numtc for netdev as 2 that will be
5460 		 * referenced by the netdev layer as TC 0 and 1.
5461 		 */
5462 		if (vsi->tc_config.enabled_tc & BIT(i))
5463 			netdev_set_tc_queue(netdev,
5464 					vsi->tc_config.tc_info[i].netdev_tc,
5465 					vsi->tc_config.tc_info[i].qcount,
5466 					vsi->tc_config.tc_info[i].qoffset);
5467 	}
5468 
5469 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5470 		return;
5471 
5472 	/* Assign UP2TC map for the VSI */
5473 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5474 		/* Get the actual TC# for the UP */
5475 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5476 		/* Get the mapped netdev TC# for the UP */
5477 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5478 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5479 	}
5480 }
5481 
5482 /**
5483  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5484  * @vsi: the VSI being configured
5485  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5486  **/
5487 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5488 				      struct i40e_vsi_context *ctxt)
5489 {
5490 	/* copy just the sections touched not the entire info
5491 	 * since not all sections are valid as returned by
5492 	 * update vsi params
5493 	 */
5494 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5495 	memcpy(&vsi->info.queue_mapping,
5496 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5497 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5498 	       sizeof(vsi->info.tc_mapping));
5499 }
5500 
5501 /**
5502  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5503  * @vsi: the VSI being reconfigured
5504  * @vsi_offset: offset from main VF VSI
5505  */
5506 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5507 {
5508 	struct i40e_vsi_context ctxt = {};
5509 	struct i40e_pf *pf;
5510 	struct i40e_hw *hw;
5511 	int ret;
5512 
5513 	if (!vsi)
5514 		return I40E_ERR_PARAM;
5515 	pf = vsi->back;
5516 	hw = &pf->hw;
5517 
5518 	ctxt.seid = vsi->seid;
5519 	ctxt.pf_num = hw->pf_id;
5520 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5521 	ctxt.uplink_seid = vsi->uplink_seid;
5522 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5523 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5524 	ctxt.info = vsi->info;
5525 
5526 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5527 				 false);
5528 	if (vsi->reconfig_rss) {
5529 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5530 				      vsi->num_queue_pairs);
5531 		ret = i40e_vsi_config_rss(vsi);
5532 		if (ret) {
5533 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5534 			return ret;
5535 		}
5536 		vsi->reconfig_rss = false;
5537 	}
5538 
5539 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5540 	if (ret) {
5541 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %s aq_err %s\n",
5542 			 i40e_stat_str(hw, ret),
5543 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5544 		return ret;
5545 	}
5546 	/* update the local VSI info with updated queue map */
5547 	i40e_vsi_update_queue_map(vsi, &ctxt);
5548 	vsi->info.valid_sections = 0;
5549 
5550 	return ret;
5551 }
5552 
5553 /**
5554  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5555  * @vsi: VSI to be configured
5556  * @enabled_tc: TC bitmap
5557  *
5558  * This configures a particular VSI for TCs that are mapped to the
5559  * given TC bitmap. It uses default bandwidth share for TCs across
5560  * VSIs to configure TC for a particular VSI.
5561  *
5562  * NOTE:
5563  * It is expected that the VSI queues have been quisced before calling
5564  * this function.
5565  **/
5566 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5567 {
5568 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5569 	struct i40e_pf *pf = vsi->back;
5570 	struct i40e_hw *hw = &pf->hw;
5571 	struct i40e_vsi_context ctxt;
5572 	int ret = 0;
5573 	int i;
5574 
5575 	/* Check if enabled_tc is same as existing or new TCs */
5576 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5577 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5578 		return ret;
5579 
5580 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5581 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5582 		if (enabled_tc & BIT(i))
5583 			bw_share[i] = 1;
5584 	}
5585 
5586 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5587 	if (ret) {
5588 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5589 
5590 		dev_info(&pf->pdev->dev,
5591 			 "Failed configuring TC map %d for VSI %d\n",
5592 			 enabled_tc, vsi->seid);
5593 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5594 						  &bw_config, NULL);
5595 		if (ret) {
5596 			dev_info(&pf->pdev->dev,
5597 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5598 				 i40e_stat_str(hw, ret),
5599 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5600 			goto out;
5601 		}
5602 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5603 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5604 
5605 			if (!valid_tc)
5606 				valid_tc = bw_config.tc_valid_bits;
5607 			/* Always enable TC0, no matter what */
5608 			valid_tc |= 1;
5609 			dev_info(&pf->pdev->dev,
5610 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5611 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5612 			enabled_tc = valid_tc;
5613 		}
5614 
5615 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5616 		if (ret) {
5617 			dev_err(&pf->pdev->dev,
5618 				"Unable to  configure TC map %d for VSI %d\n",
5619 				enabled_tc, vsi->seid);
5620 			goto out;
5621 		}
5622 	}
5623 
5624 	/* Update Queue Pairs Mapping for currently enabled UPs */
5625 	ctxt.seid = vsi->seid;
5626 	ctxt.pf_num = vsi->back->hw.pf_id;
5627 	ctxt.vf_num = 0;
5628 	ctxt.uplink_seid = vsi->uplink_seid;
5629 	ctxt.info = vsi->info;
5630 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5631 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5632 		if (ret)
5633 			goto out;
5634 	} else {
5635 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5636 	}
5637 
5638 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5639 	 * queues changed.
5640 	 */
5641 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5642 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5643 				      vsi->num_queue_pairs);
5644 		ret = i40e_vsi_config_rss(vsi);
5645 		if (ret) {
5646 			dev_info(&vsi->back->pdev->dev,
5647 				 "Failed to reconfig rss for num_queues\n");
5648 			return ret;
5649 		}
5650 		vsi->reconfig_rss = false;
5651 	}
5652 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5653 		ctxt.info.valid_sections |=
5654 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5655 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5656 	}
5657 
5658 	/* Update the VSI after updating the VSI queue-mapping
5659 	 * information
5660 	 */
5661 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5662 	if (ret) {
5663 		dev_info(&pf->pdev->dev,
5664 			 "Update vsi tc config failed, err %s aq_err %s\n",
5665 			 i40e_stat_str(hw, ret),
5666 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5667 		goto out;
5668 	}
5669 	/* update the local VSI info with updated queue map */
5670 	i40e_vsi_update_queue_map(vsi, &ctxt);
5671 	vsi->info.valid_sections = 0;
5672 
5673 	/* Update current VSI BW information */
5674 	ret = i40e_vsi_get_bw_info(vsi);
5675 	if (ret) {
5676 		dev_info(&pf->pdev->dev,
5677 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5678 			 i40e_stat_str(hw, ret),
5679 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5680 		goto out;
5681 	}
5682 
5683 	/* Update the netdev TC setup */
5684 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5685 out:
5686 	return ret;
5687 }
5688 
5689 /**
5690  * i40e_get_link_speed - Returns link speed for the interface
5691  * @vsi: VSI to be configured
5692  *
5693  **/
5694 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5695 {
5696 	struct i40e_pf *pf = vsi->back;
5697 
5698 	switch (pf->hw.phy.link_info.link_speed) {
5699 	case I40E_LINK_SPEED_40GB:
5700 		return 40000;
5701 	case I40E_LINK_SPEED_25GB:
5702 		return 25000;
5703 	case I40E_LINK_SPEED_20GB:
5704 		return 20000;
5705 	case I40E_LINK_SPEED_10GB:
5706 		return 10000;
5707 	case I40E_LINK_SPEED_1GB:
5708 		return 1000;
5709 	default:
5710 		return -EINVAL;
5711 	}
5712 }
5713 
5714 /**
5715  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5716  * @vsi: VSI to be configured
5717  * @seid: seid of the channel/VSI
5718  * @max_tx_rate: max TX rate to be configured as BW limit
5719  *
5720  * Helper function to set BW limit for a given VSI
5721  **/
5722 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5723 {
5724 	struct i40e_pf *pf = vsi->back;
5725 	u64 credits = 0;
5726 	int speed = 0;
5727 	int ret = 0;
5728 
5729 	speed = i40e_get_link_speed(vsi);
5730 	if (max_tx_rate > speed) {
5731 		dev_err(&pf->pdev->dev,
5732 			"Invalid max tx rate %llu specified for VSI seid %d.",
5733 			max_tx_rate, seid);
5734 		return -EINVAL;
5735 	}
5736 	if (max_tx_rate && max_tx_rate < 50) {
5737 		dev_warn(&pf->pdev->dev,
5738 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5739 		max_tx_rate = 50;
5740 	}
5741 
5742 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5743 	credits = max_tx_rate;
5744 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5745 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5746 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5747 	if (ret)
5748 		dev_err(&pf->pdev->dev,
5749 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5750 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5751 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5752 	return ret;
5753 }
5754 
5755 /**
5756  * i40e_remove_queue_channels - Remove queue channels for the TCs
5757  * @vsi: VSI to be configured
5758  *
5759  * Remove queue channels for the TCs
5760  **/
5761 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5762 {
5763 	enum i40e_admin_queue_err last_aq_status;
5764 	struct i40e_cloud_filter *cfilter;
5765 	struct i40e_channel *ch, *ch_tmp;
5766 	struct i40e_pf *pf = vsi->back;
5767 	struct hlist_node *node;
5768 	int ret, i;
5769 
5770 	/* Reset rss size that was stored when reconfiguring rss for
5771 	 * channel VSIs with non-power-of-2 queue count.
5772 	 */
5773 	vsi->current_rss_size = 0;
5774 
5775 	/* perform cleanup for channels if they exist */
5776 	if (list_empty(&vsi->ch_list))
5777 		return;
5778 
5779 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5780 		struct i40e_vsi *p_vsi;
5781 
5782 		list_del(&ch->list);
5783 		p_vsi = ch->parent_vsi;
5784 		if (!p_vsi || !ch->initialized) {
5785 			kfree(ch);
5786 			continue;
5787 		}
5788 		/* Reset queue contexts */
5789 		for (i = 0; i < ch->num_queue_pairs; i++) {
5790 			struct i40e_ring *tx_ring, *rx_ring;
5791 			u16 pf_q;
5792 
5793 			pf_q = ch->base_queue + i;
5794 			tx_ring = vsi->tx_rings[pf_q];
5795 			tx_ring->ch = NULL;
5796 
5797 			rx_ring = vsi->rx_rings[pf_q];
5798 			rx_ring->ch = NULL;
5799 		}
5800 
5801 		/* Reset BW configured for this VSI via mqprio */
5802 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5803 		if (ret)
5804 			dev_info(&vsi->back->pdev->dev,
5805 				 "Failed to reset tx rate for ch->seid %u\n",
5806 				 ch->seid);
5807 
5808 		/* delete cloud filters associated with this channel */
5809 		hlist_for_each_entry_safe(cfilter, node,
5810 					  &pf->cloud_filter_list, cloud_node) {
5811 			if (cfilter->seid != ch->seid)
5812 				continue;
5813 
5814 			hash_del(&cfilter->cloud_node);
5815 			if (cfilter->dst_port)
5816 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
5817 									cfilter,
5818 									false);
5819 			else
5820 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
5821 								false);
5822 			last_aq_status = pf->hw.aq.asq_last_status;
5823 			if (ret)
5824 				dev_info(&pf->pdev->dev,
5825 					 "Failed to delete cloud filter, err %s aq_err %s\n",
5826 					 i40e_stat_str(&pf->hw, ret),
5827 					 i40e_aq_str(&pf->hw, last_aq_status));
5828 			kfree(cfilter);
5829 		}
5830 
5831 		/* delete VSI from FW */
5832 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5833 					     NULL);
5834 		if (ret)
5835 			dev_err(&vsi->back->pdev->dev,
5836 				"unable to remove channel (%d) for parent VSI(%d)\n",
5837 				ch->seid, p_vsi->seid);
5838 		kfree(ch);
5839 	}
5840 	INIT_LIST_HEAD(&vsi->ch_list);
5841 }
5842 
5843 /**
5844  * i40e_get_max_queues_for_channel
5845  * @vsi: ptr to VSI to which channels are associated with
5846  *
5847  * Helper function which returns max value among the queue counts set on the
5848  * channels/TCs created.
5849  **/
5850 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5851 {
5852 	struct i40e_channel *ch, *ch_tmp;
5853 	int max = 0;
5854 
5855 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5856 		if (!ch->initialized)
5857 			continue;
5858 		if (ch->num_queue_pairs > max)
5859 			max = ch->num_queue_pairs;
5860 	}
5861 
5862 	return max;
5863 }
5864 
5865 /**
5866  * i40e_validate_num_queues - validate num_queues w.r.t channel
5867  * @pf: ptr to PF device
5868  * @num_queues: number of queues
5869  * @vsi: the parent VSI
5870  * @reconfig_rss: indicates should the RSS be reconfigured or not
5871  *
5872  * This function validates number of queues in the context of new channel
5873  * which is being established and determines if RSS should be reconfigured
5874  * or not for parent VSI.
5875  **/
5876 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5877 				    struct i40e_vsi *vsi, bool *reconfig_rss)
5878 {
5879 	int max_ch_queues;
5880 
5881 	if (!reconfig_rss)
5882 		return -EINVAL;
5883 
5884 	*reconfig_rss = false;
5885 	if (vsi->current_rss_size) {
5886 		if (num_queues > vsi->current_rss_size) {
5887 			dev_dbg(&pf->pdev->dev,
5888 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
5889 				num_queues, vsi->current_rss_size);
5890 			return -EINVAL;
5891 		} else if ((num_queues < vsi->current_rss_size) &&
5892 			   (!is_power_of_2(num_queues))) {
5893 			dev_dbg(&pf->pdev->dev,
5894 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5895 				num_queues, vsi->current_rss_size);
5896 			return -EINVAL;
5897 		}
5898 	}
5899 
5900 	if (!is_power_of_2(num_queues)) {
5901 		/* Find the max num_queues configured for channel if channel
5902 		 * exist.
5903 		 * if channel exist, then enforce 'num_queues' to be more than
5904 		 * max ever queues configured for channel.
5905 		 */
5906 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5907 		if (num_queues < max_ch_queues) {
5908 			dev_dbg(&pf->pdev->dev,
5909 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
5910 				num_queues, max_ch_queues);
5911 			return -EINVAL;
5912 		}
5913 		*reconfig_rss = true;
5914 	}
5915 
5916 	return 0;
5917 }
5918 
5919 /**
5920  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5921  * @vsi: the VSI being setup
5922  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5923  *
5924  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5925  **/
5926 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5927 {
5928 	struct i40e_pf *pf = vsi->back;
5929 	u8 seed[I40E_HKEY_ARRAY_SIZE];
5930 	struct i40e_hw *hw = &pf->hw;
5931 	int local_rss_size;
5932 	u8 *lut;
5933 	int ret;
5934 
5935 	if (!vsi->rss_size)
5936 		return -EINVAL;
5937 
5938 	if (rss_size > vsi->rss_size)
5939 		return -EINVAL;
5940 
5941 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
5942 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5943 	if (!lut)
5944 		return -ENOMEM;
5945 
5946 	/* Ignoring user configured lut if there is one */
5947 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5948 
5949 	/* Use user configured hash key if there is one, otherwise
5950 	 * use default.
5951 	 */
5952 	if (vsi->rss_hkey_user)
5953 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5954 	else
5955 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5956 
5957 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5958 	if (ret) {
5959 		dev_info(&pf->pdev->dev,
5960 			 "Cannot set RSS lut, err %s aq_err %s\n",
5961 			 i40e_stat_str(hw, ret),
5962 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5963 		kfree(lut);
5964 		return ret;
5965 	}
5966 	kfree(lut);
5967 
5968 	/* Do the update w.r.t. storing rss_size */
5969 	if (!vsi->orig_rss_size)
5970 		vsi->orig_rss_size = vsi->rss_size;
5971 	vsi->current_rss_size = local_rss_size;
5972 
5973 	return ret;
5974 }
5975 
5976 /**
5977  * i40e_channel_setup_queue_map - Setup a channel queue map
5978  * @pf: ptr to PF device
5979  * @ctxt: VSI context structure
5980  * @ch: ptr to channel structure
5981  *
5982  * Setup queue map for a specific channel
5983  **/
5984 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5985 					 struct i40e_vsi_context *ctxt,
5986 					 struct i40e_channel *ch)
5987 {
5988 	u16 qcount, qmap, sections = 0;
5989 	u8 offset = 0;
5990 	int pow;
5991 
5992 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5993 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5994 
5995 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5996 	ch->num_queue_pairs = qcount;
5997 
5998 	/* find the next higher power-of-2 of num queue pairs */
5999 	pow = ilog2(qcount);
6000 	if (!is_power_of_2(qcount))
6001 		pow++;
6002 
6003 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6004 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6005 
6006 	/* Setup queue TC[0].qmap for given VSI context */
6007 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6008 
6009 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6010 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6011 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6012 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6013 }
6014 
6015 /**
6016  * i40e_add_channel - add a channel by adding VSI
6017  * @pf: ptr to PF device
6018  * @uplink_seid: underlying HW switching element (VEB) ID
6019  * @ch: ptr to channel structure
6020  *
6021  * Add a channel (VSI) using add_vsi and queue_map
6022  **/
6023 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6024 			    struct i40e_channel *ch)
6025 {
6026 	struct i40e_hw *hw = &pf->hw;
6027 	struct i40e_vsi_context ctxt;
6028 	u8 enabled_tc = 0x1; /* TC0 enabled */
6029 	int ret;
6030 
6031 	if (ch->type != I40E_VSI_VMDQ2) {
6032 		dev_info(&pf->pdev->dev,
6033 			 "add new vsi failed, ch->type %d\n", ch->type);
6034 		return -EINVAL;
6035 	}
6036 
6037 	memset(&ctxt, 0, sizeof(ctxt));
6038 	ctxt.pf_num = hw->pf_id;
6039 	ctxt.vf_num = 0;
6040 	ctxt.uplink_seid = uplink_seid;
6041 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6042 	if (ch->type == I40E_VSI_VMDQ2)
6043 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6044 
6045 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6046 		ctxt.info.valid_sections |=
6047 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6048 		ctxt.info.switch_id =
6049 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6050 	}
6051 
6052 	/* Set queue map for a given VSI context */
6053 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6054 
6055 	/* Now time to create VSI */
6056 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6057 	if (ret) {
6058 		dev_info(&pf->pdev->dev,
6059 			 "add new vsi failed, err %s aq_err %s\n",
6060 			 i40e_stat_str(&pf->hw, ret),
6061 			 i40e_aq_str(&pf->hw,
6062 				     pf->hw.aq.asq_last_status));
6063 		return -ENOENT;
6064 	}
6065 
6066 	/* Success, update channel, set enabled_tc only if the channel
6067 	 * is not a macvlan
6068 	 */
6069 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6070 	ch->seid = ctxt.seid;
6071 	ch->vsi_number = ctxt.vsi_number;
6072 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6073 
6074 	/* copy just the sections touched not the entire info
6075 	 * since not all sections are valid as returned by
6076 	 * update vsi params
6077 	 */
6078 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6079 	memcpy(&ch->info.queue_mapping,
6080 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6081 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6082 	       sizeof(ctxt.info.tc_mapping));
6083 
6084 	return 0;
6085 }
6086 
6087 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6088 				  u8 *bw_share)
6089 {
6090 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6091 	i40e_status ret;
6092 	int i;
6093 
6094 	memset(&bw_data, 0, sizeof(bw_data));
6095 	bw_data.tc_valid_bits = ch->enabled_tc;
6096 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6097 		bw_data.tc_bw_credits[i] = bw_share[i];
6098 
6099 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6100 				       &bw_data, NULL);
6101 	if (ret) {
6102 		dev_info(&vsi->back->pdev->dev,
6103 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6104 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6105 		return -EINVAL;
6106 	}
6107 
6108 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6109 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6110 
6111 	return 0;
6112 }
6113 
6114 /**
6115  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6116  * @pf: ptr to PF device
6117  * @vsi: the VSI being setup
6118  * @ch: ptr to channel structure
6119  *
6120  * Configure TX rings associated with channel (VSI) since queues are being
6121  * from parent VSI.
6122  **/
6123 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6124 				       struct i40e_vsi *vsi,
6125 				       struct i40e_channel *ch)
6126 {
6127 	i40e_status ret;
6128 	int i;
6129 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6130 
6131 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6132 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6133 		if (ch->enabled_tc & BIT(i))
6134 			bw_share[i] = 1;
6135 	}
6136 
6137 	/* configure BW for new VSI */
6138 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6139 	if (ret) {
6140 		dev_info(&vsi->back->pdev->dev,
6141 			 "Failed configuring TC map %d for channel (seid %u)\n",
6142 			 ch->enabled_tc, ch->seid);
6143 		return ret;
6144 	}
6145 
6146 	for (i = 0; i < ch->num_queue_pairs; i++) {
6147 		struct i40e_ring *tx_ring, *rx_ring;
6148 		u16 pf_q;
6149 
6150 		pf_q = ch->base_queue + i;
6151 
6152 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6153 		 * context
6154 		 */
6155 		tx_ring = vsi->tx_rings[pf_q];
6156 		tx_ring->ch = ch;
6157 
6158 		/* Get the RX ring ptr */
6159 		rx_ring = vsi->rx_rings[pf_q];
6160 		rx_ring->ch = ch;
6161 	}
6162 
6163 	return 0;
6164 }
6165 
6166 /**
6167  * i40e_setup_hw_channel - setup new channel
6168  * @pf: ptr to PF device
6169  * @vsi: the VSI being setup
6170  * @ch: ptr to channel structure
6171  * @uplink_seid: underlying HW switching element (VEB) ID
6172  * @type: type of channel to be created (VMDq2/VF)
6173  *
6174  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6175  * and configures TX rings accordingly
6176  **/
6177 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6178 					struct i40e_vsi *vsi,
6179 					struct i40e_channel *ch,
6180 					u16 uplink_seid, u8 type)
6181 {
6182 	int ret;
6183 
6184 	ch->initialized = false;
6185 	ch->base_queue = vsi->next_base_queue;
6186 	ch->type = type;
6187 
6188 	/* Proceed with creation of channel (VMDq2) VSI */
6189 	ret = i40e_add_channel(pf, uplink_seid, ch);
6190 	if (ret) {
6191 		dev_info(&pf->pdev->dev,
6192 			 "failed to add_channel using uplink_seid %u\n",
6193 			 uplink_seid);
6194 		return ret;
6195 	}
6196 
6197 	/* Mark the successful creation of channel */
6198 	ch->initialized = true;
6199 
6200 	/* Reconfigure TX queues using QTX_CTL register */
6201 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6202 	if (ret) {
6203 		dev_info(&pf->pdev->dev,
6204 			 "failed to configure TX rings for channel %u\n",
6205 			 ch->seid);
6206 		return ret;
6207 	}
6208 
6209 	/* update 'next_base_queue' */
6210 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6211 	dev_dbg(&pf->pdev->dev,
6212 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6213 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6214 		ch->num_queue_pairs,
6215 		vsi->next_base_queue);
6216 	return ret;
6217 }
6218 
6219 /**
6220  * i40e_setup_channel - setup new channel using uplink element
6221  * @pf: ptr to PF device
6222  * @vsi: pointer to the VSI to set up the channel within
6223  * @ch: ptr to channel structure
6224  *
6225  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6226  * and uplink switching element (uplink_seid)
6227  **/
6228 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6229 			       struct i40e_channel *ch)
6230 {
6231 	u8 vsi_type;
6232 	u16 seid;
6233 	int ret;
6234 
6235 	if (vsi->type == I40E_VSI_MAIN) {
6236 		vsi_type = I40E_VSI_VMDQ2;
6237 	} else {
6238 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6239 			vsi->type);
6240 		return false;
6241 	}
6242 
6243 	/* underlying switching element */
6244 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6245 
6246 	/* create channel (VSI), configure TX rings */
6247 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6248 	if (ret) {
6249 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6250 		return false;
6251 	}
6252 
6253 	return ch->initialized ? true : false;
6254 }
6255 
6256 /**
6257  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6258  * @vsi: ptr to VSI which has PF backing
6259  *
6260  * Sets up switch mode correctly if it needs to be changed and perform
6261  * what are allowed modes.
6262  **/
6263 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6264 {
6265 	u8 mode;
6266 	struct i40e_pf *pf = vsi->back;
6267 	struct i40e_hw *hw = &pf->hw;
6268 	int ret;
6269 
6270 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6271 	if (ret)
6272 		return -EINVAL;
6273 
6274 	if (hw->dev_caps.switch_mode) {
6275 		/* if switch mode is set, support mode2 (non-tunneled for
6276 		 * cloud filter) for now
6277 		 */
6278 		u32 switch_mode = hw->dev_caps.switch_mode &
6279 				  I40E_SWITCH_MODE_MASK;
6280 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6281 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6282 				return 0;
6283 			dev_err(&pf->pdev->dev,
6284 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6285 				hw->dev_caps.switch_mode);
6286 			return -EINVAL;
6287 		}
6288 	}
6289 
6290 	/* Set Bit 7 to be valid */
6291 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6292 
6293 	/* Set L4type for TCP support */
6294 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6295 
6296 	/* Set cloud filter mode */
6297 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6298 
6299 	/* Prep mode field for set_switch_config */
6300 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6301 					pf->last_sw_conf_valid_flags,
6302 					mode, NULL);
6303 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6304 		dev_err(&pf->pdev->dev,
6305 			"couldn't set switch config bits, err %s aq_err %s\n",
6306 			i40e_stat_str(hw, ret),
6307 			i40e_aq_str(hw,
6308 				    hw->aq.asq_last_status));
6309 
6310 	return ret;
6311 }
6312 
6313 /**
6314  * i40e_create_queue_channel - function to create channel
6315  * @vsi: VSI to be configured
6316  * @ch: ptr to channel (it contains channel specific params)
6317  *
6318  * This function creates channel (VSI) using num_queues specified by user,
6319  * reconfigs RSS if needed.
6320  **/
6321 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6322 			      struct i40e_channel *ch)
6323 {
6324 	struct i40e_pf *pf = vsi->back;
6325 	bool reconfig_rss;
6326 	int err;
6327 
6328 	if (!ch)
6329 		return -EINVAL;
6330 
6331 	if (!ch->num_queue_pairs) {
6332 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6333 			ch->num_queue_pairs);
6334 		return -EINVAL;
6335 	}
6336 
6337 	/* validate user requested num_queues for channel */
6338 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6339 				       &reconfig_rss);
6340 	if (err) {
6341 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6342 			 ch->num_queue_pairs);
6343 		return -EINVAL;
6344 	}
6345 
6346 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6347 	 * VSI to be added switch to VEB mode.
6348 	 */
6349 
6350 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6351 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6352 
6353 		if (vsi->type == I40E_VSI_MAIN) {
6354 			if (pf->flags & I40E_FLAG_TC_MQPRIO)
6355 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6356 			else
6357 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6358 		}
6359 		/* now onwards for main VSI, number of queues will be value
6360 		 * of TC0's queue count
6361 		 */
6362 	}
6363 
6364 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6365 	 * it should be more than num_queues
6366 	 */
6367 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6368 		dev_dbg(&pf->pdev->dev,
6369 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6370 			vsi->cnt_q_avail, ch->num_queue_pairs);
6371 		return -EINVAL;
6372 	}
6373 
6374 	/* reconfig_rss only if vsi type is MAIN_VSI */
6375 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6376 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6377 		if (err) {
6378 			dev_info(&pf->pdev->dev,
6379 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6380 				 ch->num_queue_pairs);
6381 			return -EINVAL;
6382 		}
6383 	}
6384 
6385 	if (!i40e_setup_channel(pf, vsi, ch)) {
6386 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6387 		return -EINVAL;
6388 	}
6389 
6390 	dev_info(&pf->pdev->dev,
6391 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6392 		 ch->seid, ch->num_queue_pairs);
6393 
6394 	/* configure VSI for BW limit */
6395 	if (ch->max_tx_rate) {
6396 		u64 credits = ch->max_tx_rate;
6397 
6398 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6399 			return -EINVAL;
6400 
6401 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6402 		dev_dbg(&pf->pdev->dev,
6403 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6404 			ch->max_tx_rate,
6405 			credits,
6406 			ch->seid);
6407 	}
6408 
6409 	/* in case of VF, this will be main SRIOV VSI */
6410 	ch->parent_vsi = vsi;
6411 
6412 	/* and update main_vsi's count for queue_available to use */
6413 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6414 
6415 	return 0;
6416 }
6417 
6418 /**
6419  * i40e_configure_queue_channels - Add queue channel for the given TCs
6420  * @vsi: VSI to be configured
6421  *
6422  * Configures queue channel mapping to the given TCs
6423  **/
6424 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6425 {
6426 	struct i40e_channel *ch;
6427 	u64 max_rate = 0;
6428 	int ret = 0, i;
6429 
6430 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6431 	vsi->tc_seid_map[0] = vsi->seid;
6432 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6433 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6434 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6435 			if (!ch) {
6436 				ret = -ENOMEM;
6437 				goto err_free;
6438 			}
6439 
6440 			INIT_LIST_HEAD(&ch->list);
6441 			ch->num_queue_pairs =
6442 				vsi->tc_config.tc_info[i].qcount;
6443 			ch->base_queue =
6444 				vsi->tc_config.tc_info[i].qoffset;
6445 
6446 			/* Bandwidth limit through tc interface is in bytes/s,
6447 			 * change to Mbit/s
6448 			 */
6449 			max_rate = vsi->mqprio_qopt.max_rate[i];
6450 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6451 			ch->max_tx_rate = max_rate;
6452 
6453 			list_add_tail(&ch->list, &vsi->ch_list);
6454 
6455 			ret = i40e_create_queue_channel(vsi, ch);
6456 			if (ret) {
6457 				dev_err(&vsi->back->pdev->dev,
6458 					"Failed creating queue channel with TC%d: queues %d\n",
6459 					i, ch->num_queue_pairs);
6460 				goto err_free;
6461 			}
6462 			vsi->tc_seid_map[i] = ch->seid;
6463 		}
6464 	}
6465 	return ret;
6466 
6467 err_free:
6468 	i40e_remove_queue_channels(vsi);
6469 	return ret;
6470 }
6471 
6472 /**
6473  * i40e_veb_config_tc - Configure TCs for given VEB
6474  * @veb: given VEB
6475  * @enabled_tc: TC bitmap
6476  *
6477  * Configures given TC bitmap for VEB (switching) element
6478  **/
6479 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6480 {
6481 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6482 	struct i40e_pf *pf = veb->pf;
6483 	int ret = 0;
6484 	int i;
6485 
6486 	/* No TCs or already enabled TCs just return */
6487 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6488 		return ret;
6489 
6490 	bw_data.tc_valid_bits = enabled_tc;
6491 	/* bw_data.absolute_credits is not set (relative) */
6492 
6493 	/* Enable ETS TCs with equal BW Share for now */
6494 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6495 		if (enabled_tc & BIT(i))
6496 			bw_data.tc_bw_share_credits[i] = 1;
6497 	}
6498 
6499 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6500 						   &bw_data, NULL);
6501 	if (ret) {
6502 		dev_info(&pf->pdev->dev,
6503 			 "VEB bw config failed, err %s aq_err %s\n",
6504 			 i40e_stat_str(&pf->hw, ret),
6505 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6506 		goto out;
6507 	}
6508 
6509 	/* Update the BW information */
6510 	ret = i40e_veb_get_bw_info(veb);
6511 	if (ret) {
6512 		dev_info(&pf->pdev->dev,
6513 			 "Failed getting veb bw config, err %s aq_err %s\n",
6514 			 i40e_stat_str(&pf->hw, ret),
6515 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6516 	}
6517 
6518 out:
6519 	return ret;
6520 }
6521 
6522 #ifdef CONFIG_I40E_DCB
6523 /**
6524  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6525  * @pf: PF struct
6526  *
6527  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6528  * the caller would've quiesce all the VSIs before calling
6529  * this function
6530  **/
6531 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6532 {
6533 	u8 tc_map = 0;
6534 	int ret;
6535 	u8 v;
6536 
6537 	/* Enable the TCs available on PF to all VEBs */
6538 	tc_map = i40e_pf_get_tc_map(pf);
6539 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6540 		return;
6541 
6542 	for (v = 0; v < I40E_MAX_VEB; v++) {
6543 		if (!pf->veb[v])
6544 			continue;
6545 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6546 		if (ret) {
6547 			dev_info(&pf->pdev->dev,
6548 				 "Failed configuring TC for VEB seid=%d\n",
6549 				 pf->veb[v]->seid);
6550 			/* Will try to configure as many components */
6551 		}
6552 	}
6553 
6554 	/* Update each VSI */
6555 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6556 		if (!pf->vsi[v])
6557 			continue;
6558 
6559 		/* - Enable all TCs for the LAN VSI
6560 		 * - For all others keep them at TC0 for now
6561 		 */
6562 		if (v == pf->lan_vsi)
6563 			tc_map = i40e_pf_get_tc_map(pf);
6564 		else
6565 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6566 
6567 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6568 		if (ret) {
6569 			dev_info(&pf->pdev->dev,
6570 				 "Failed configuring TC for VSI seid=%d\n",
6571 				 pf->vsi[v]->seid);
6572 			/* Will try to configure as many components */
6573 		} else {
6574 			/* Re-configure VSI vectors based on updated TC map */
6575 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6576 			if (pf->vsi[v]->netdev)
6577 				i40e_dcbnl_set_all(pf->vsi[v]);
6578 		}
6579 	}
6580 }
6581 
6582 /**
6583  * i40e_resume_port_tx - Resume port Tx
6584  * @pf: PF struct
6585  *
6586  * Resume a port's Tx and issue a PF reset in case of failure to
6587  * resume.
6588  **/
6589 static int i40e_resume_port_tx(struct i40e_pf *pf)
6590 {
6591 	struct i40e_hw *hw = &pf->hw;
6592 	int ret;
6593 
6594 	ret = i40e_aq_resume_port_tx(hw, NULL);
6595 	if (ret) {
6596 		dev_info(&pf->pdev->dev,
6597 			 "Resume Port Tx failed, err %s aq_err %s\n",
6598 			  i40e_stat_str(&pf->hw, ret),
6599 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6600 		/* Schedule PF reset to recover */
6601 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6602 		i40e_service_event_schedule(pf);
6603 	}
6604 
6605 	return ret;
6606 }
6607 
6608 /**
6609  * i40e_suspend_port_tx - Suspend port Tx
6610  * @pf: PF struct
6611  *
6612  * Suspend a port's Tx and issue a PF reset in case of failure.
6613  **/
6614 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6615 {
6616 	struct i40e_hw *hw = &pf->hw;
6617 	int ret;
6618 
6619 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6620 	if (ret) {
6621 		dev_info(&pf->pdev->dev,
6622 			 "Suspend Port Tx failed, err %s aq_err %s\n",
6623 			 i40e_stat_str(&pf->hw, ret),
6624 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6625 		/* Schedule PF reset to recover */
6626 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6627 		i40e_service_event_schedule(pf);
6628 	}
6629 
6630 	return ret;
6631 }
6632 
6633 /**
6634  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6635  * @pf: PF being configured
6636  * @new_cfg: New DCBX configuration
6637  *
6638  * Program DCB settings into HW and reconfigure VEB/VSIs on
6639  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6640  **/
6641 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6642 				  struct i40e_dcbx_config *new_cfg)
6643 {
6644 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6645 	int ret;
6646 
6647 	/* Check if need reconfiguration */
6648 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6649 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6650 		return 0;
6651 	}
6652 
6653 	/* Config change disable all VSIs */
6654 	i40e_pf_quiesce_all_vsi(pf);
6655 
6656 	/* Copy the new config to the current config */
6657 	*old_cfg = *new_cfg;
6658 	old_cfg->etsrec = old_cfg->etscfg;
6659 	ret = i40e_set_dcb_config(&pf->hw);
6660 	if (ret) {
6661 		dev_info(&pf->pdev->dev,
6662 			 "Set DCB Config failed, err %s aq_err %s\n",
6663 			 i40e_stat_str(&pf->hw, ret),
6664 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6665 		goto out;
6666 	}
6667 
6668 	/* Changes in configuration update VEB/VSI */
6669 	i40e_dcb_reconfigure(pf);
6670 out:
6671 	/* In case of reset do not try to resume anything */
6672 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6673 		/* Re-start the VSIs if disabled */
6674 		ret = i40e_resume_port_tx(pf);
6675 		/* In case of error no point in resuming VSIs */
6676 		if (ret)
6677 			goto err;
6678 		i40e_pf_unquiesce_all_vsi(pf);
6679 	}
6680 err:
6681 	return ret;
6682 }
6683 
6684 /**
6685  * i40e_hw_dcb_config - Program new DCBX settings into HW
6686  * @pf: PF being configured
6687  * @new_cfg: New DCBX configuration
6688  *
6689  * Program DCB settings into HW and reconfigure VEB/VSIs on
6690  * given PF
6691  **/
6692 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6693 {
6694 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6695 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6696 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6697 	struct i40e_dcbx_config *old_cfg;
6698 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6699 	struct i40e_rx_pb_config pb_cfg;
6700 	struct i40e_hw *hw = &pf->hw;
6701 	u8 num_ports = hw->num_ports;
6702 	bool need_reconfig;
6703 	int ret = -EINVAL;
6704 	u8 lltc_map = 0;
6705 	u8 tc_map = 0;
6706 	u8 new_numtc;
6707 	u8 i;
6708 
6709 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6710 	/* Un-pack information to Program ETS HW via shared API
6711 	 * numtc, tcmap
6712 	 * LLTC map
6713 	 * ETS/NON-ETS arbiter mode
6714 	 * max exponent (credit refills)
6715 	 * Total number of ports
6716 	 * PFC priority bit-map
6717 	 * Priority Table
6718 	 * BW % per TC
6719 	 * Arbiter mode between UPs sharing same TC
6720 	 * TSA table (ETS or non-ETS)
6721 	 * EEE enabled or not
6722 	 * MFS TC table
6723 	 */
6724 
6725 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6726 
6727 	memset(&ets_data, 0, sizeof(ets_data));
6728 	for (i = 0; i < new_numtc; i++) {
6729 		tc_map |= BIT(i);
6730 		switch (new_cfg->etscfg.tsatable[i]) {
6731 		case I40E_IEEE_TSA_ETS:
6732 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6733 			ets_data.tc_bw_share_credits[i] =
6734 					new_cfg->etscfg.tcbwtable[i];
6735 			break;
6736 		case I40E_IEEE_TSA_STRICT:
6737 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6738 			lltc_map |= BIT(i);
6739 			ets_data.tc_bw_share_credits[i] =
6740 					I40E_DCB_STRICT_PRIO_CREDITS;
6741 			break;
6742 		default:
6743 			/* Invalid TSA type */
6744 			need_reconfig = false;
6745 			goto out;
6746 		}
6747 	}
6748 
6749 	old_cfg = &hw->local_dcbx_config;
6750 	/* Check if need reconfiguration */
6751 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6752 
6753 	/* If needed, enable/disable frame tagging, disable all VSIs
6754 	 * and suspend port tx
6755 	 */
6756 	if (need_reconfig) {
6757 		/* Enable DCB tagging only when more than one TC */
6758 		if (new_numtc > 1)
6759 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6760 		else
6761 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6762 
6763 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6764 		/* Reconfiguration needed quiesce all VSIs */
6765 		i40e_pf_quiesce_all_vsi(pf);
6766 		ret = i40e_suspend_port_tx(pf);
6767 		if (ret)
6768 			goto err;
6769 	}
6770 
6771 	/* Configure Port ETS Tx Scheduler */
6772 	ets_data.tc_valid_bits = tc_map;
6773 	ets_data.tc_strict_priority_flags = lltc_map;
6774 	ret = i40e_aq_config_switch_comp_ets
6775 		(hw, pf->mac_seid, &ets_data,
6776 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
6777 	if (ret) {
6778 		dev_info(&pf->pdev->dev,
6779 			 "Modify Port ETS failed, err %s aq_err %s\n",
6780 			 i40e_stat_str(&pf->hw, ret),
6781 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6782 		goto out;
6783 	}
6784 
6785 	/* Configure Rx ETS HW */
6786 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
6787 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
6788 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
6789 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
6790 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
6791 				   lltc_map);
6792 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
6793 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
6794 				     prio_type);
6795 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
6796 			       new_cfg->etscfg.prioritytable);
6797 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
6798 
6799 	/* Configure Rx Packet Buffers in HW */
6800 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6801 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
6802 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
6803 	}
6804 
6805 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
6806 					 false, new_cfg->pfc.pfcenable,
6807 					 mfs_tc, &pb_cfg);
6808 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
6809 
6810 	/* Update the local Rx Packet buffer config */
6811 	pf->pb_cfg = pb_cfg;
6812 
6813 	/* Inform the FW about changes to DCB configuration */
6814 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
6815 	if (ret) {
6816 		dev_info(&pf->pdev->dev,
6817 			 "DCB Updated failed, err %s aq_err %s\n",
6818 			 i40e_stat_str(&pf->hw, ret),
6819 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6820 		goto out;
6821 	}
6822 
6823 	/* Update the port DCBx configuration */
6824 	*old_cfg = *new_cfg;
6825 
6826 	/* Changes in configuration update VEB/VSI */
6827 	i40e_dcb_reconfigure(pf);
6828 out:
6829 	/* Re-start the VSIs if disabled */
6830 	if (need_reconfig) {
6831 		ret = i40e_resume_port_tx(pf);
6832 
6833 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
6834 		/* In case of error no point in resuming VSIs */
6835 		if (ret)
6836 			goto err;
6837 
6838 		/* Wait for the PF's queues to be disabled */
6839 		ret = i40e_pf_wait_queues_disabled(pf);
6840 		if (ret) {
6841 			/* Schedule PF reset to recover */
6842 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6843 			i40e_service_event_schedule(pf);
6844 			goto err;
6845 		} else {
6846 			i40e_pf_unquiesce_all_vsi(pf);
6847 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
6848 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
6849 		}
6850 		/* registers are set, lets apply */
6851 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
6852 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
6853 	}
6854 
6855 err:
6856 	return ret;
6857 }
6858 
6859 /**
6860  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
6861  * @pf: PF being queried
6862  *
6863  * Set default DCB configuration in case DCB is to be done in SW.
6864  **/
6865 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
6866 {
6867 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
6868 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6869 	struct i40e_hw *hw = &pf->hw;
6870 	int err;
6871 
6872 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
6873 		/* Update the local cached instance with TC0 ETS */
6874 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
6875 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
6876 		pf->tmp_cfg.etscfg.maxtcs = 0;
6877 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
6878 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
6879 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
6880 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
6881 		/* FW needs one App to configure HW */
6882 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
6883 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
6884 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
6885 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
6886 
6887 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
6888 	}
6889 
6890 	memset(&ets_data, 0, sizeof(ets_data));
6891 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
6892 	ets_data.tc_strict_priority_flags = 0; /* ETS */
6893 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
6894 
6895 	/* Enable ETS on the Physical port */
6896 	err = i40e_aq_config_switch_comp_ets
6897 		(hw, pf->mac_seid, &ets_data,
6898 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
6899 	if (err) {
6900 		dev_info(&pf->pdev->dev,
6901 			 "Enable Port ETS failed, err %s aq_err %s\n",
6902 			 i40e_stat_str(&pf->hw, err),
6903 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6904 		err = -ENOENT;
6905 		goto out;
6906 	}
6907 
6908 	/* Update the local cached instance with TC0 ETS */
6909 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
6910 	dcb_cfg->etscfg.cbs = 0;
6911 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
6912 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
6913 
6914 out:
6915 	return err;
6916 }
6917 
6918 /**
6919  * i40e_init_pf_dcb - Initialize DCB configuration
6920  * @pf: PF being configured
6921  *
6922  * Query the current DCB configuration and cache it
6923  * in the hardware structure
6924  **/
6925 static int i40e_init_pf_dcb(struct i40e_pf *pf)
6926 {
6927 	struct i40e_hw *hw = &pf->hw;
6928 	int err;
6929 
6930 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
6931 	 * Also do not enable DCBx if FW LLDP agent is disabled
6932 	 */
6933 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
6934 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
6935 		err = I40E_NOT_SUPPORTED;
6936 		goto out;
6937 	}
6938 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
6939 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
6940 		err = i40e_dcb_sw_default_config(pf);
6941 		if (err) {
6942 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
6943 			goto out;
6944 		}
6945 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
6946 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
6947 			       DCB_CAP_DCBX_VER_IEEE;
6948 		/* at init capable but disabled */
6949 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
6950 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6951 		goto out;
6952 	}
6953 	err = i40e_init_dcb(hw, true);
6954 	if (!err) {
6955 		/* Device/Function is not DCBX capable */
6956 		if ((!hw->func_caps.dcb) ||
6957 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6958 			dev_info(&pf->pdev->dev,
6959 				 "DCBX offload is not supported or is disabled for this PF.\n");
6960 		} else {
6961 			/* When status is not DISABLED then DCBX in FW */
6962 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6963 				       DCB_CAP_DCBX_VER_IEEE;
6964 
6965 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
6966 			/* Enable DCB tagging only when more than one TC
6967 			 * or explicitly disable if only one TC
6968 			 */
6969 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6970 				pf->flags |= I40E_FLAG_DCB_ENABLED;
6971 			else
6972 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6973 			dev_dbg(&pf->pdev->dev,
6974 				"DCBX offload is supported for this PF.\n");
6975 		}
6976 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
6977 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
6978 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
6979 	} else {
6980 		dev_info(&pf->pdev->dev,
6981 			 "Query for DCB configuration failed, err %s aq_err %s\n",
6982 			 i40e_stat_str(&pf->hw, err),
6983 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6984 	}
6985 
6986 out:
6987 	return err;
6988 }
6989 #endif /* CONFIG_I40E_DCB */
6990 
6991 /**
6992  * i40e_print_link_message - print link up or down
6993  * @vsi: the VSI for which link needs a message
6994  * @isup: true of link is up, false otherwise
6995  */
6996 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
6997 {
6998 	enum i40e_aq_link_speed new_speed;
6999 	struct i40e_pf *pf = vsi->back;
7000 	char *speed = "Unknown";
7001 	char *fc = "Unknown";
7002 	char *fec = "";
7003 	char *req_fec = "";
7004 	char *an = "";
7005 
7006 	if (isup)
7007 		new_speed = pf->hw.phy.link_info.link_speed;
7008 	else
7009 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7010 
7011 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7012 		return;
7013 	vsi->current_isup = isup;
7014 	vsi->current_speed = new_speed;
7015 	if (!isup) {
7016 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7017 		return;
7018 	}
7019 
7020 	/* Warn user if link speed on NPAR enabled partition is not at
7021 	 * least 10GB
7022 	 */
7023 	if (pf->hw.func_caps.npar_enable &&
7024 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7025 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7026 		netdev_warn(vsi->netdev,
7027 			    "The partition detected link speed that is less than 10Gbps\n");
7028 
7029 	switch (pf->hw.phy.link_info.link_speed) {
7030 	case I40E_LINK_SPEED_40GB:
7031 		speed = "40 G";
7032 		break;
7033 	case I40E_LINK_SPEED_20GB:
7034 		speed = "20 G";
7035 		break;
7036 	case I40E_LINK_SPEED_25GB:
7037 		speed = "25 G";
7038 		break;
7039 	case I40E_LINK_SPEED_10GB:
7040 		speed = "10 G";
7041 		break;
7042 	case I40E_LINK_SPEED_5GB:
7043 		speed = "5 G";
7044 		break;
7045 	case I40E_LINK_SPEED_2_5GB:
7046 		speed = "2.5 G";
7047 		break;
7048 	case I40E_LINK_SPEED_1GB:
7049 		speed = "1000 M";
7050 		break;
7051 	case I40E_LINK_SPEED_100MB:
7052 		speed = "100 M";
7053 		break;
7054 	default:
7055 		break;
7056 	}
7057 
7058 	switch (pf->hw.fc.current_mode) {
7059 	case I40E_FC_FULL:
7060 		fc = "RX/TX";
7061 		break;
7062 	case I40E_FC_TX_PAUSE:
7063 		fc = "TX";
7064 		break;
7065 	case I40E_FC_RX_PAUSE:
7066 		fc = "RX";
7067 		break;
7068 	default:
7069 		fc = "None";
7070 		break;
7071 	}
7072 
7073 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7074 		req_fec = "None";
7075 		fec = "None";
7076 		an = "False";
7077 
7078 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7079 			an = "True";
7080 
7081 		if (pf->hw.phy.link_info.fec_info &
7082 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7083 			fec = "CL74 FC-FEC/BASE-R";
7084 		else if (pf->hw.phy.link_info.fec_info &
7085 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7086 			fec = "CL108 RS-FEC";
7087 
7088 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7089 		 * both RS and FC are requested
7090 		 */
7091 		if (vsi->back->hw.phy.link_info.req_fec_info &
7092 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7093 			if (vsi->back->hw.phy.link_info.req_fec_info &
7094 			    I40E_AQ_REQUEST_FEC_RS)
7095 				req_fec = "CL108 RS-FEC";
7096 			else
7097 				req_fec = "CL74 FC-FEC/BASE-R";
7098 		}
7099 		netdev_info(vsi->netdev,
7100 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7101 			    speed, req_fec, fec, an, fc);
7102 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7103 		req_fec = "None";
7104 		fec = "None";
7105 		an = "False";
7106 
7107 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7108 			an = "True";
7109 
7110 		if (pf->hw.phy.link_info.fec_info &
7111 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7112 			fec = "CL74 FC-FEC/BASE-R";
7113 
7114 		if (pf->hw.phy.link_info.req_fec_info &
7115 		    I40E_AQ_REQUEST_FEC_KR)
7116 			req_fec = "CL74 FC-FEC/BASE-R";
7117 
7118 		netdev_info(vsi->netdev,
7119 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7120 			    speed, req_fec, fec, an, fc);
7121 	} else {
7122 		netdev_info(vsi->netdev,
7123 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7124 			    speed, fc);
7125 	}
7126 
7127 }
7128 
7129 /**
7130  * i40e_up_complete - Finish the last steps of bringing up a connection
7131  * @vsi: the VSI being configured
7132  **/
7133 static int i40e_up_complete(struct i40e_vsi *vsi)
7134 {
7135 	struct i40e_pf *pf = vsi->back;
7136 	int err;
7137 
7138 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7139 		i40e_vsi_configure_msix(vsi);
7140 	else
7141 		i40e_configure_msi_and_legacy(vsi);
7142 
7143 	/* start rings */
7144 	err = i40e_vsi_start_rings(vsi);
7145 	if (err)
7146 		return err;
7147 
7148 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7149 	i40e_napi_enable_all(vsi);
7150 	i40e_vsi_enable_irq(vsi);
7151 
7152 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7153 	    (vsi->netdev)) {
7154 		i40e_print_link_message(vsi, true);
7155 		netif_tx_start_all_queues(vsi->netdev);
7156 		netif_carrier_on(vsi->netdev);
7157 	}
7158 
7159 	/* replay FDIR SB filters */
7160 	if (vsi->type == I40E_VSI_FDIR) {
7161 		/* reset fd counters */
7162 		pf->fd_add_err = 0;
7163 		pf->fd_atr_cnt = 0;
7164 		i40e_fdir_filter_restore(vsi);
7165 	}
7166 
7167 	/* On the next run of the service_task, notify any clients of the new
7168 	 * opened netdev
7169 	 */
7170 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7171 	i40e_service_event_schedule(pf);
7172 
7173 	return 0;
7174 }
7175 
7176 /**
7177  * i40e_vsi_reinit_locked - Reset the VSI
7178  * @vsi: the VSI being configured
7179  *
7180  * Rebuild the ring structs after some configuration
7181  * has changed, e.g. MTU size.
7182  **/
7183 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7184 {
7185 	struct i40e_pf *pf = vsi->back;
7186 
7187 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7188 		usleep_range(1000, 2000);
7189 	i40e_down(vsi);
7190 
7191 	i40e_up(vsi);
7192 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7193 }
7194 
7195 /**
7196  * i40e_force_link_state - Force the link status
7197  * @pf: board private structure
7198  * @is_up: whether the link state should be forced up or down
7199  **/
7200 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7201 {
7202 	struct i40e_aq_get_phy_abilities_resp abilities;
7203 	struct i40e_aq_set_phy_config config = {0};
7204 	bool non_zero_phy_type = is_up;
7205 	struct i40e_hw *hw = &pf->hw;
7206 	i40e_status err;
7207 	u64 mask;
7208 	u8 speed;
7209 
7210 	/* Card might've been put in an unstable state by other drivers
7211 	 * and applications, which causes incorrect speed values being
7212 	 * set on startup. In order to clear speed registers, we call
7213 	 * get_phy_capabilities twice, once to get initial state of
7214 	 * available speeds, and once to get current PHY config.
7215 	 */
7216 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7217 					   NULL);
7218 	if (err) {
7219 		dev_err(&pf->pdev->dev,
7220 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7221 			i40e_stat_str(hw, err),
7222 			i40e_aq_str(hw, hw->aq.asq_last_status));
7223 		return err;
7224 	}
7225 	speed = abilities.link_speed;
7226 
7227 	/* Get the current phy config */
7228 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7229 					   NULL);
7230 	if (err) {
7231 		dev_err(&pf->pdev->dev,
7232 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7233 			i40e_stat_str(hw, err),
7234 			i40e_aq_str(hw, hw->aq.asq_last_status));
7235 		return err;
7236 	}
7237 
7238 	/* If link needs to go up, but was not forced to go down,
7239 	 * and its speed values are OK, no need for a flap
7240 	 * if non_zero_phy_type was set, still need to force up
7241 	 */
7242 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7243 		non_zero_phy_type = true;
7244 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7245 		return I40E_SUCCESS;
7246 
7247 	/* To force link we need to set bits for all supported PHY types,
7248 	 * but there are now more than 32, so we need to split the bitmap
7249 	 * across two fields.
7250 	 */
7251 	mask = I40E_PHY_TYPES_BITMASK;
7252 	config.phy_type =
7253 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7254 	config.phy_type_ext =
7255 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7256 	/* Copy the old settings, except of phy_type */
7257 	config.abilities = abilities.abilities;
7258 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7259 		if (is_up)
7260 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7261 		else
7262 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7263 	}
7264 	if (abilities.link_speed != 0)
7265 		config.link_speed = abilities.link_speed;
7266 	else
7267 		config.link_speed = speed;
7268 	config.eee_capability = abilities.eee_capability;
7269 	config.eeer = abilities.eeer_val;
7270 	config.low_power_ctrl = abilities.d3_lpan;
7271 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7272 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7273 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7274 
7275 	if (err) {
7276 		dev_err(&pf->pdev->dev,
7277 			"set phy config ret =  %s last_status =  %s\n",
7278 			i40e_stat_str(&pf->hw, err),
7279 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7280 		return err;
7281 	}
7282 
7283 	/* Update the link info */
7284 	err = i40e_update_link_info(hw);
7285 	if (err) {
7286 		/* Wait a little bit (on 40G cards it sometimes takes a really
7287 		 * long time for link to come back from the atomic reset)
7288 		 * and try once more
7289 		 */
7290 		msleep(1000);
7291 		i40e_update_link_info(hw);
7292 	}
7293 
7294 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7295 
7296 	return I40E_SUCCESS;
7297 }
7298 
7299 /**
7300  * i40e_up - Bring the connection back up after being down
7301  * @vsi: the VSI being configured
7302  **/
7303 int i40e_up(struct i40e_vsi *vsi)
7304 {
7305 	int err;
7306 
7307 	if (vsi->type == I40E_VSI_MAIN &&
7308 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7309 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7310 		i40e_force_link_state(vsi->back, true);
7311 
7312 	err = i40e_vsi_configure(vsi);
7313 	if (!err)
7314 		err = i40e_up_complete(vsi);
7315 
7316 	return err;
7317 }
7318 
7319 /**
7320  * i40e_down - Shutdown the connection processing
7321  * @vsi: the VSI being stopped
7322  **/
7323 void i40e_down(struct i40e_vsi *vsi)
7324 {
7325 	int i;
7326 
7327 	/* It is assumed that the caller of this function
7328 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7329 	 */
7330 	if (vsi->netdev) {
7331 		netif_carrier_off(vsi->netdev);
7332 		netif_tx_disable(vsi->netdev);
7333 	}
7334 	i40e_vsi_disable_irq(vsi);
7335 	i40e_vsi_stop_rings(vsi);
7336 	if (vsi->type == I40E_VSI_MAIN &&
7337 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7338 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7339 		i40e_force_link_state(vsi->back, false);
7340 	i40e_napi_disable_all(vsi);
7341 
7342 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7343 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7344 		if (i40e_enabled_xdp_vsi(vsi)) {
7345 			/* Make sure that in-progress ndo_xdp_xmit and
7346 			 * ndo_xsk_wakeup calls are completed.
7347 			 */
7348 			synchronize_rcu();
7349 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7350 		}
7351 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7352 	}
7353 
7354 }
7355 
7356 /**
7357  * i40e_validate_mqprio_qopt- validate queue mapping info
7358  * @vsi: the VSI being configured
7359  * @mqprio_qopt: queue parametrs
7360  **/
7361 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7362 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7363 {
7364 	u64 sum_max_rate = 0;
7365 	u64 max_rate = 0;
7366 	int i;
7367 
7368 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7369 	    mqprio_qopt->qopt.num_tc < 1 ||
7370 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7371 		return -EINVAL;
7372 	for (i = 0; ; i++) {
7373 		if (!mqprio_qopt->qopt.count[i])
7374 			return -EINVAL;
7375 		if (mqprio_qopt->min_rate[i]) {
7376 			dev_err(&vsi->back->pdev->dev,
7377 				"Invalid min tx rate (greater than 0) specified\n");
7378 			return -EINVAL;
7379 		}
7380 		max_rate = mqprio_qopt->max_rate[i];
7381 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7382 		sum_max_rate += max_rate;
7383 
7384 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7385 			break;
7386 		if (mqprio_qopt->qopt.offset[i + 1] !=
7387 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7388 			return -EINVAL;
7389 	}
7390 	if (vsi->num_queue_pairs <
7391 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7392 		dev_err(&vsi->back->pdev->dev,
7393 			"Failed to create traffic channel, insufficient number of queues.\n");
7394 		return -EINVAL;
7395 	}
7396 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7397 		dev_err(&vsi->back->pdev->dev,
7398 			"Invalid max tx rate specified\n");
7399 		return -EINVAL;
7400 	}
7401 	return 0;
7402 }
7403 
7404 /**
7405  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7406  * @vsi: the VSI being configured
7407  **/
7408 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7409 {
7410 	u16 qcount;
7411 	int i;
7412 
7413 	/* Only TC0 is enabled */
7414 	vsi->tc_config.numtc = 1;
7415 	vsi->tc_config.enabled_tc = 1;
7416 	qcount = min_t(int, vsi->alloc_queue_pairs,
7417 		       i40e_pf_get_max_q_per_tc(vsi->back));
7418 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7419 		/* For the TC that is not enabled set the offset to default
7420 		 * queue and allocate one queue for the given TC.
7421 		 */
7422 		vsi->tc_config.tc_info[i].qoffset = 0;
7423 		if (i == 0)
7424 			vsi->tc_config.tc_info[i].qcount = qcount;
7425 		else
7426 			vsi->tc_config.tc_info[i].qcount = 1;
7427 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7428 	}
7429 }
7430 
7431 /**
7432  * i40e_del_macvlan_filter
7433  * @hw: pointer to the HW structure
7434  * @seid: seid of the channel VSI
7435  * @macaddr: the mac address to apply as a filter
7436  * @aq_err: store the admin Q error
7437  *
7438  * This function deletes a mac filter on the channel VSI which serves as the
7439  * macvlan. Returns 0 on success.
7440  **/
7441 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7442 					   const u8 *macaddr, int *aq_err)
7443 {
7444 	struct i40e_aqc_remove_macvlan_element_data element;
7445 	i40e_status status;
7446 
7447 	memset(&element, 0, sizeof(element));
7448 	ether_addr_copy(element.mac_addr, macaddr);
7449 	element.vlan_tag = 0;
7450 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7451 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7452 	*aq_err = hw->aq.asq_last_status;
7453 
7454 	return status;
7455 }
7456 
7457 /**
7458  * i40e_add_macvlan_filter
7459  * @hw: pointer to the HW structure
7460  * @seid: seid of the channel VSI
7461  * @macaddr: the mac address to apply as a filter
7462  * @aq_err: store the admin Q error
7463  *
7464  * This function adds a mac filter on the channel VSI which serves as the
7465  * macvlan. Returns 0 on success.
7466  **/
7467 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7468 					   const u8 *macaddr, int *aq_err)
7469 {
7470 	struct i40e_aqc_add_macvlan_element_data element;
7471 	i40e_status status;
7472 	u16 cmd_flags = 0;
7473 
7474 	ether_addr_copy(element.mac_addr, macaddr);
7475 	element.vlan_tag = 0;
7476 	element.queue_number = 0;
7477 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7478 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7479 	element.flags = cpu_to_le16(cmd_flags);
7480 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7481 	*aq_err = hw->aq.asq_last_status;
7482 
7483 	return status;
7484 }
7485 
7486 /**
7487  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7488  * @vsi: the VSI we want to access
7489  * @ch: the channel we want to access
7490  */
7491 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7492 {
7493 	struct i40e_ring *tx_ring, *rx_ring;
7494 	u16 pf_q;
7495 	int i;
7496 
7497 	for (i = 0; i < ch->num_queue_pairs; i++) {
7498 		pf_q = ch->base_queue + i;
7499 		tx_ring = vsi->tx_rings[pf_q];
7500 		tx_ring->ch = NULL;
7501 		rx_ring = vsi->rx_rings[pf_q];
7502 		rx_ring->ch = NULL;
7503 	}
7504 }
7505 
7506 /**
7507  * i40e_free_macvlan_channels
7508  * @vsi: the VSI we want to access
7509  *
7510  * This function frees the Qs of the channel VSI from
7511  * the stack and also deletes the channel VSIs which
7512  * serve as macvlans.
7513  */
7514 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7515 {
7516 	struct i40e_channel *ch, *ch_tmp;
7517 	int ret;
7518 
7519 	if (list_empty(&vsi->macvlan_list))
7520 		return;
7521 
7522 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7523 		struct i40e_vsi *parent_vsi;
7524 
7525 		if (i40e_is_channel_macvlan(ch)) {
7526 			i40e_reset_ch_rings(vsi, ch);
7527 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7528 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7529 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7530 			kfree(ch->fwd);
7531 			ch->fwd = NULL;
7532 		}
7533 
7534 		list_del(&ch->list);
7535 		parent_vsi = ch->parent_vsi;
7536 		if (!parent_vsi || !ch->initialized) {
7537 			kfree(ch);
7538 			continue;
7539 		}
7540 
7541 		/* remove the VSI */
7542 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7543 					     NULL);
7544 		if (ret)
7545 			dev_err(&vsi->back->pdev->dev,
7546 				"unable to remove channel (%d) for parent VSI(%d)\n",
7547 				ch->seid, parent_vsi->seid);
7548 		kfree(ch);
7549 	}
7550 	vsi->macvlan_cnt = 0;
7551 }
7552 
7553 /**
7554  * i40e_fwd_ring_up - bring the macvlan device up
7555  * @vsi: the VSI we want to access
7556  * @vdev: macvlan netdevice
7557  * @fwd: the private fwd structure
7558  */
7559 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7560 			    struct i40e_fwd_adapter *fwd)
7561 {
7562 	int ret = 0, num_tc = 1,  i, aq_err;
7563 	struct i40e_channel *ch, *ch_tmp;
7564 	struct i40e_pf *pf = vsi->back;
7565 	struct i40e_hw *hw = &pf->hw;
7566 
7567 	if (list_empty(&vsi->macvlan_list))
7568 		return -EINVAL;
7569 
7570 	/* Go through the list and find an available channel */
7571 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7572 		if (!i40e_is_channel_macvlan(ch)) {
7573 			ch->fwd = fwd;
7574 			/* record configuration for macvlan interface in vdev */
7575 			for (i = 0; i < num_tc; i++)
7576 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7577 							     i,
7578 							     ch->num_queue_pairs,
7579 							     ch->base_queue);
7580 			for (i = 0; i < ch->num_queue_pairs; i++) {
7581 				struct i40e_ring *tx_ring, *rx_ring;
7582 				u16 pf_q;
7583 
7584 				pf_q = ch->base_queue + i;
7585 
7586 				/* Get to TX ring ptr */
7587 				tx_ring = vsi->tx_rings[pf_q];
7588 				tx_ring->ch = ch;
7589 
7590 				/* Get the RX ring ptr */
7591 				rx_ring = vsi->rx_rings[pf_q];
7592 				rx_ring->ch = ch;
7593 			}
7594 			break;
7595 		}
7596 	}
7597 
7598 	/* Guarantee all rings are updated before we update the
7599 	 * MAC address filter.
7600 	 */
7601 	wmb();
7602 
7603 	/* Add a mac filter */
7604 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7605 	if (ret) {
7606 		/* if we cannot add the MAC rule then disable the offload */
7607 		macvlan_release_l2fw_offload(vdev);
7608 		for (i = 0; i < ch->num_queue_pairs; i++) {
7609 			struct i40e_ring *rx_ring;
7610 			u16 pf_q;
7611 
7612 			pf_q = ch->base_queue + i;
7613 			rx_ring = vsi->rx_rings[pf_q];
7614 			rx_ring->netdev = NULL;
7615 		}
7616 		dev_info(&pf->pdev->dev,
7617 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7618 			  i40e_stat_str(hw, ret),
7619 			  i40e_aq_str(hw, aq_err));
7620 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7621 	}
7622 
7623 	return ret;
7624 }
7625 
7626 /**
7627  * i40e_setup_macvlans - create the channels which will be macvlans
7628  * @vsi: the VSI we want to access
7629  * @macvlan_cnt: no. of macvlans to be setup
7630  * @qcnt: no. of Qs per macvlan
7631  * @vdev: macvlan netdevice
7632  */
7633 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7634 			       struct net_device *vdev)
7635 {
7636 	struct i40e_pf *pf = vsi->back;
7637 	struct i40e_hw *hw = &pf->hw;
7638 	struct i40e_vsi_context ctxt;
7639 	u16 sections, qmap, num_qps;
7640 	struct i40e_channel *ch;
7641 	int i, pow, ret = 0;
7642 	u8 offset = 0;
7643 
7644 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7645 		return -EINVAL;
7646 
7647 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7648 
7649 	/* find the next higher power-of-2 of num queue pairs */
7650 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7651 
7652 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7653 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7654 
7655 	/* Setup context bits for the main VSI */
7656 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7657 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7658 	memset(&ctxt, 0, sizeof(ctxt));
7659 	ctxt.seid = vsi->seid;
7660 	ctxt.pf_num = vsi->back->hw.pf_id;
7661 	ctxt.vf_num = 0;
7662 	ctxt.uplink_seid = vsi->uplink_seid;
7663 	ctxt.info = vsi->info;
7664 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7665 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7666 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7667 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7668 
7669 	/* Reconfigure RSS for main VSI with new max queue count */
7670 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7671 	ret = i40e_vsi_config_rss(vsi);
7672 	if (ret) {
7673 		dev_info(&pf->pdev->dev,
7674 			 "Failed to reconfig RSS for num_queues (%u)\n",
7675 			 vsi->rss_size);
7676 		return ret;
7677 	}
7678 	vsi->reconfig_rss = true;
7679 	dev_dbg(&vsi->back->pdev->dev,
7680 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7681 	vsi->next_base_queue = num_qps;
7682 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7683 
7684 	/* Update the VSI after updating the VSI queue-mapping
7685 	 * information
7686 	 */
7687 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7688 	if (ret) {
7689 		dev_info(&pf->pdev->dev,
7690 			 "Update vsi tc config failed, err %s aq_err %s\n",
7691 			 i40e_stat_str(hw, ret),
7692 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7693 		return ret;
7694 	}
7695 	/* update the local VSI info with updated queue map */
7696 	i40e_vsi_update_queue_map(vsi, &ctxt);
7697 	vsi->info.valid_sections = 0;
7698 
7699 	/* Create channels for macvlans */
7700 	INIT_LIST_HEAD(&vsi->macvlan_list);
7701 	for (i = 0; i < macvlan_cnt; i++) {
7702 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7703 		if (!ch) {
7704 			ret = -ENOMEM;
7705 			goto err_free;
7706 		}
7707 		INIT_LIST_HEAD(&ch->list);
7708 		ch->num_queue_pairs = qcnt;
7709 		if (!i40e_setup_channel(pf, vsi, ch)) {
7710 			ret = -EINVAL;
7711 			kfree(ch);
7712 			goto err_free;
7713 		}
7714 		ch->parent_vsi = vsi;
7715 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7716 		vsi->macvlan_cnt++;
7717 		list_add_tail(&ch->list, &vsi->macvlan_list);
7718 	}
7719 
7720 	return ret;
7721 
7722 err_free:
7723 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7724 	i40e_free_macvlan_channels(vsi);
7725 
7726 	return ret;
7727 }
7728 
7729 /**
7730  * i40e_fwd_add - configure macvlans
7731  * @netdev: net device to configure
7732  * @vdev: macvlan netdevice
7733  **/
7734 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7735 {
7736 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7737 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7738 	struct i40e_vsi *vsi = np->vsi;
7739 	struct i40e_pf *pf = vsi->back;
7740 	struct i40e_fwd_adapter *fwd;
7741 	int avail_macvlan, ret;
7742 
7743 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7744 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7745 		return ERR_PTR(-EINVAL);
7746 	}
7747 	if ((pf->flags & I40E_FLAG_TC_MQPRIO)) {
7748 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7749 		return ERR_PTR(-EINVAL);
7750 	}
7751 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7752 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7753 		return ERR_PTR(-EINVAL);
7754 	}
7755 
7756 	/* The macvlan device has to be a single Q device so that the
7757 	 * tc_to_txq field can be reused to pick the tx queue.
7758 	 */
7759 	if (netif_is_multiqueue(vdev))
7760 		return ERR_PTR(-ERANGE);
7761 
7762 	if (!vsi->macvlan_cnt) {
7763 		/* reserve bit 0 for the pf device */
7764 		set_bit(0, vsi->fwd_bitmask);
7765 
7766 		/* Try to reserve as many queues as possible for macvlans. First
7767 		 * reserve 3/4th of max vectors, then half, then quarter and
7768 		 * calculate Qs per macvlan as you go
7769 		 */
7770 		vectors = pf->num_lan_msix;
7771 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7772 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7773 			q_per_macvlan = 4;
7774 			macvlan_cnt = (vectors - 32) / 4;
7775 		} else if (vectors <= 64 && vectors > 32) {
7776 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7777 			q_per_macvlan = 2;
7778 			macvlan_cnt = (vectors - 16) / 2;
7779 		} else if (vectors <= 32 && vectors > 16) {
7780 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7781 			q_per_macvlan = 1;
7782 			macvlan_cnt = vectors - 16;
7783 		} else if (vectors <= 16 && vectors > 8) {
7784 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7785 			q_per_macvlan = 1;
7786 			macvlan_cnt = vectors - 8;
7787 		} else {
7788 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7789 			q_per_macvlan = 1;
7790 			macvlan_cnt = vectors - 1;
7791 		}
7792 
7793 		if (macvlan_cnt == 0)
7794 			return ERR_PTR(-EBUSY);
7795 
7796 		/* Quiesce VSI queues */
7797 		i40e_quiesce_vsi(vsi);
7798 
7799 		/* sets up the macvlans but does not "enable" them */
7800 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
7801 					  vdev);
7802 		if (ret)
7803 			return ERR_PTR(ret);
7804 
7805 		/* Unquiesce VSI */
7806 		i40e_unquiesce_vsi(vsi);
7807 	}
7808 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
7809 					    vsi->macvlan_cnt);
7810 	if (avail_macvlan >= I40E_MAX_MACVLANS)
7811 		return ERR_PTR(-EBUSY);
7812 
7813 	/* create the fwd struct */
7814 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
7815 	if (!fwd)
7816 		return ERR_PTR(-ENOMEM);
7817 
7818 	set_bit(avail_macvlan, vsi->fwd_bitmask);
7819 	fwd->bit_no = avail_macvlan;
7820 	netdev_set_sb_channel(vdev, avail_macvlan);
7821 	fwd->netdev = vdev;
7822 
7823 	if (!netif_running(netdev))
7824 		return fwd;
7825 
7826 	/* Set fwd ring up */
7827 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
7828 	if (ret) {
7829 		/* unbind the queues and drop the subordinate channel config */
7830 		netdev_unbind_sb_channel(netdev, vdev);
7831 		netdev_set_sb_channel(vdev, 0);
7832 
7833 		kfree(fwd);
7834 		return ERR_PTR(-EINVAL);
7835 	}
7836 
7837 	return fwd;
7838 }
7839 
7840 /**
7841  * i40e_del_all_macvlans - Delete all the mac filters on the channels
7842  * @vsi: the VSI we want to access
7843  */
7844 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
7845 {
7846 	struct i40e_channel *ch, *ch_tmp;
7847 	struct i40e_pf *pf = vsi->back;
7848 	struct i40e_hw *hw = &pf->hw;
7849 	int aq_err, ret = 0;
7850 
7851 	if (list_empty(&vsi->macvlan_list))
7852 		return;
7853 
7854 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7855 		if (i40e_is_channel_macvlan(ch)) {
7856 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7857 						      i40e_channel_mac(ch),
7858 						      &aq_err);
7859 			if (!ret) {
7860 				/* Reset queue contexts */
7861 				i40e_reset_ch_rings(vsi, ch);
7862 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7863 				netdev_unbind_sb_channel(vsi->netdev,
7864 							 ch->fwd->netdev);
7865 				netdev_set_sb_channel(ch->fwd->netdev, 0);
7866 				kfree(ch->fwd);
7867 				ch->fwd = NULL;
7868 			}
7869 		}
7870 	}
7871 }
7872 
7873 /**
7874  * i40e_fwd_del - delete macvlan interfaces
7875  * @netdev: net device to configure
7876  * @vdev: macvlan netdevice
7877  */
7878 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
7879 {
7880 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7881 	struct i40e_fwd_adapter *fwd = vdev;
7882 	struct i40e_channel *ch, *ch_tmp;
7883 	struct i40e_vsi *vsi = np->vsi;
7884 	struct i40e_pf *pf = vsi->back;
7885 	struct i40e_hw *hw = &pf->hw;
7886 	int aq_err, ret = 0;
7887 
7888 	/* Find the channel associated with the macvlan and del mac filter */
7889 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7890 		if (i40e_is_channel_macvlan(ch) &&
7891 		    ether_addr_equal(i40e_channel_mac(ch),
7892 				     fwd->netdev->dev_addr)) {
7893 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7894 						      i40e_channel_mac(ch),
7895 						      &aq_err);
7896 			if (!ret) {
7897 				/* Reset queue contexts */
7898 				i40e_reset_ch_rings(vsi, ch);
7899 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7900 				netdev_unbind_sb_channel(netdev, fwd->netdev);
7901 				netdev_set_sb_channel(fwd->netdev, 0);
7902 				kfree(ch->fwd);
7903 				ch->fwd = NULL;
7904 			} else {
7905 				dev_info(&pf->pdev->dev,
7906 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
7907 					  i40e_stat_str(hw, ret),
7908 					  i40e_aq_str(hw, aq_err));
7909 			}
7910 			break;
7911 		}
7912 	}
7913 }
7914 
7915 /**
7916  * i40e_setup_tc - configure multiple traffic classes
7917  * @netdev: net device to configure
7918  * @type_data: tc offload data
7919  **/
7920 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
7921 {
7922 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
7923 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7924 	struct i40e_vsi *vsi = np->vsi;
7925 	struct i40e_pf *pf = vsi->back;
7926 	u8 enabled_tc = 0, num_tc, hw;
7927 	bool need_reset = false;
7928 	int old_queue_pairs;
7929 	int ret = -EINVAL;
7930 	u16 mode;
7931 	int i;
7932 
7933 	old_queue_pairs = vsi->num_queue_pairs;
7934 	num_tc = mqprio_qopt->qopt.num_tc;
7935 	hw = mqprio_qopt->qopt.hw;
7936 	mode = mqprio_qopt->mode;
7937 	if (!hw) {
7938 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7939 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
7940 		goto config_tc;
7941 	}
7942 
7943 	/* Check if MFP enabled */
7944 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
7945 		netdev_info(netdev,
7946 			    "Configuring TC not supported in MFP mode\n");
7947 		return ret;
7948 	}
7949 	switch (mode) {
7950 	case TC_MQPRIO_MODE_DCB:
7951 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7952 
7953 		/* Check if DCB enabled to continue */
7954 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7955 			netdev_info(netdev,
7956 				    "DCB is not enabled for adapter\n");
7957 			return ret;
7958 		}
7959 
7960 		/* Check whether tc count is within enabled limit */
7961 		if (num_tc > i40e_pf_get_num_tc(pf)) {
7962 			netdev_info(netdev,
7963 				    "TC count greater than enabled on link for adapter\n");
7964 			return ret;
7965 		}
7966 		break;
7967 	case TC_MQPRIO_MODE_CHANNEL:
7968 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
7969 			netdev_info(netdev,
7970 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
7971 			return ret;
7972 		}
7973 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7974 			return ret;
7975 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
7976 		if (ret)
7977 			return ret;
7978 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
7979 		       sizeof(*mqprio_qopt));
7980 		pf->flags |= I40E_FLAG_TC_MQPRIO;
7981 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7982 		break;
7983 	default:
7984 		return -EINVAL;
7985 	}
7986 
7987 config_tc:
7988 	/* Generate TC map for number of tc requested */
7989 	for (i = 0; i < num_tc; i++)
7990 		enabled_tc |= BIT(i);
7991 
7992 	/* Requesting same TC configuration as already enabled */
7993 	if (enabled_tc == vsi->tc_config.enabled_tc &&
7994 	    mode != TC_MQPRIO_MODE_CHANNEL)
7995 		return 0;
7996 
7997 	/* Quiesce VSI queues */
7998 	i40e_quiesce_vsi(vsi);
7999 
8000 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
8001 		i40e_remove_queue_channels(vsi);
8002 
8003 	/* Configure VSI for enabled TCs */
8004 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8005 	if (ret) {
8006 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8007 			    vsi->seid);
8008 		need_reset = true;
8009 		goto exit;
8010 	} else if (enabled_tc &&
8011 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8012 		netdev_info(netdev,
8013 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8014 			    vsi->tc_config.tc_info[0].qcount);
8015 		ret = -EINVAL;
8016 		need_reset = true;
8017 		goto exit;
8018 	}
8019 
8020 	dev_info(&vsi->back->pdev->dev,
8021 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8022 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8023 
8024 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
8025 		if (vsi->mqprio_qopt.max_rate[0]) {
8026 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
8027 
8028 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
8029 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8030 			if (!ret) {
8031 				u64 credits = max_tx_rate;
8032 
8033 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8034 				dev_dbg(&vsi->back->pdev->dev,
8035 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8036 					max_tx_rate,
8037 					credits,
8038 					vsi->seid);
8039 			} else {
8040 				need_reset = true;
8041 				goto exit;
8042 			}
8043 		}
8044 		ret = i40e_configure_queue_channels(vsi);
8045 		if (ret) {
8046 			vsi->num_queue_pairs = old_queue_pairs;
8047 			netdev_info(netdev,
8048 				    "Failed configuring queue channels\n");
8049 			need_reset = true;
8050 			goto exit;
8051 		}
8052 	}
8053 
8054 exit:
8055 	/* Reset the configuration data to defaults, only TC0 is enabled */
8056 	if (need_reset) {
8057 		i40e_vsi_set_default_tc_config(vsi);
8058 		need_reset = false;
8059 	}
8060 
8061 	/* Unquiesce VSI */
8062 	i40e_unquiesce_vsi(vsi);
8063 	return ret;
8064 }
8065 
8066 /**
8067  * i40e_set_cld_element - sets cloud filter element data
8068  * @filter: cloud filter rule
8069  * @cld: ptr to cloud filter element data
8070  *
8071  * This is helper function to copy data into cloud filter element
8072  **/
8073 static inline void
8074 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8075 		     struct i40e_aqc_cloud_filters_element_data *cld)
8076 {
8077 	u32 ipa;
8078 	int i;
8079 
8080 	memset(cld, 0, sizeof(*cld));
8081 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8082 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8083 
8084 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8085 		return;
8086 
8087 	if (filter->n_proto == ETH_P_IPV6) {
8088 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8089 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8090 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8091 
8092 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8093 		}
8094 	} else {
8095 		ipa = be32_to_cpu(filter->dst_ipv4);
8096 
8097 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8098 	}
8099 
8100 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8101 
8102 	/* tenant_id is not supported by FW now, once the support is enabled
8103 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8104 	 */
8105 	if (filter->tenant_id)
8106 		return;
8107 }
8108 
8109 /**
8110  * i40e_add_del_cloud_filter - Add/del cloud filter
8111  * @vsi: pointer to VSI
8112  * @filter: cloud filter rule
8113  * @add: if true, add, if false, delete
8114  *
8115  * Add or delete a cloud filter for a specific flow spec.
8116  * Returns 0 if the filter were successfully added.
8117  **/
8118 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8119 			      struct i40e_cloud_filter *filter, bool add)
8120 {
8121 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8122 	struct i40e_pf *pf = vsi->back;
8123 	int ret;
8124 	static const u16 flag_table[128] = {
8125 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8126 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8127 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8128 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8129 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8130 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8131 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8132 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8133 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8134 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8135 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8136 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8137 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8138 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8139 	};
8140 
8141 	if (filter->flags >= ARRAY_SIZE(flag_table))
8142 		return I40E_ERR_CONFIG;
8143 
8144 	memset(&cld_filter, 0, sizeof(cld_filter));
8145 
8146 	/* copy element needed to add cloud filter from filter */
8147 	i40e_set_cld_element(filter, &cld_filter);
8148 
8149 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8150 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8151 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8152 
8153 	if (filter->n_proto == ETH_P_IPV6)
8154 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8155 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8156 	else
8157 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8158 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8159 
8160 	if (add)
8161 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8162 						&cld_filter, 1);
8163 	else
8164 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8165 						&cld_filter, 1);
8166 	if (ret)
8167 		dev_dbg(&pf->pdev->dev,
8168 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8169 			add ? "add" : "delete", filter->dst_port, ret,
8170 			pf->hw.aq.asq_last_status);
8171 	else
8172 		dev_info(&pf->pdev->dev,
8173 			 "%s cloud filter for VSI: %d\n",
8174 			 add ? "Added" : "Deleted", filter->seid);
8175 	return ret;
8176 }
8177 
8178 /**
8179  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8180  * @vsi: pointer to VSI
8181  * @filter: cloud filter rule
8182  * @add: if true, add, if false, delete
8183  *
8184  * Add or delete a cloud filter for a specific flow spec using big buffer.
8185  * Returns 0 if the filter were successfully added.
8186  **/
8187 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8188 				      struct i40e_cloud_filter *filter,
8189 				      bool add)
8190 {
8191 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8192 	struct i40e_pf *pf = vsi->back;
8193 	int ret;
8194 
8195 	/* Both (src/dst) valid mac_addr are not supported */
8196 	if ((is_valid_ether_addr(filter->dst_mac) &&
8197 	     is_valid_ether_addr(filter->src_mac)) ||
8198 	    (is_multicast_ether_addr(filter->dst_mac) &&
8199 	     is_multicast_ether_addr(filter->src_mac)))
8200 		return -EOPNOTSUPP;
8201 
8202 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8203 	 * ports are not supported via big buffer now.
8204 	 */
8205 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8206 		return -EOPNOTSUPP;
8207 
8208 	/* adding filter using src_port/src_ip is not supported at this stage */
8209 	if (filter->src_port ||
8210 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8211 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8212 		return -EOPNOTSUPP;
8213 
8214 	memset(&cld_filter, 0, sizeof(cld_filter));
8215 
8216 	/* copy element needed to add cloud filter from filter */
8217 	i40e_set_cld_element(filter, &cld_filter.element);
8218 
8219 	if (is_valid_ether_addr(filter->dst_mac) ||
8220 	    is_valid_ether_addr(filter->src_mac) ||
8221 	    is_multicast_ether_addr(filter->dst_mac) ||
8222 	    is_multicast_ether_addr(filter->src_mac)) {
8223 		/* MAC + IP : unsupported mode */
8224 		if (filter->dst_ipv4)
8225 			return -EOPNOTSUPP;
8226 
8227 		/* since we validated that L4 port must be valid before
8228 		 * we get here, start with respective "flags" value
8229 		 * and update if vlan is present or not
8230 		 */
8231 		cld_filter.element.flags =
8232 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8233 
8234 		if (filter->vlan_id) {
8235 			cld_filter.element.flags =
8236 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8237 		}
8238 
8239 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8240 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8241 		cld_filter.element.flags =
8242 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8243 		if (filter->n_proto == ETH_P_IPV6)
8244 			cld_filter.element.flags |=
8245 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8246 		else
8247 			cld_filter.element.flags |=
8248 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8249 	} else {
8250 		dev_err(&pf->pdev->dev,
8251 			"either mac or ip has to be valid for cloud filter\n");
8252 		return -EINVAL;
8253 	}
8254 
8255 	/* Now copy L4 port in Byte 6..7 in general fields */
8256 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8257 						be16_to_cpu(filter->dst_port);
8258 
8259 	if (add) {
8260 		/* Validate current device switch mode, change if necessary */
8261 		ret = i40e_validate_and_set_switch_mode(vsi);
8262 		if (ret) {
8263 			dev_err(&pf->pdev->dev,
8264 				"failed to set switch mode, ret %d\n",
8265 				ret);
8266 			return ret;
8267 		}
8268 
8269 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8270 						   &cld_filter, 1);
8271 	} else {
8272 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8273 						   &cld_filter, 1);
8274 	}
8275 
8276 	if (ret)
8277 		dev_dbg(&pf->pdev->dev,
8278 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8279 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8280 	else
8281 		dev_info(&pf->pdev->dev,
8282 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8283 			 add ? "add" : "delete", filter->seid,
8284 			 ntohs(filter->dst_port));
8285 	return ret;
8286 }
8287 
8288 /**
8289  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8290  * @vsi: Pointer to VSI
8291  * @f: Pointer to struct flow_cls_offload
8292  * @filter: Pointer to cloud filter structure
8293  *
8294  **/
8295 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8296 				 struct flow_cls_offload *f,
8297 				 struct i40e_cloud_filter *filter)
8298 {
8299 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8300 	struct flow_dissector *dissector = rule->match.dissector;
8301 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8302 	struct i40e_pf *pf = vsi->back;
8303 	u8 field_flags = 0;
8304 
8305 	if (dissector->used_keys &
8306 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8307 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8308 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8309 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8310 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8311 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8312 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8313 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8314 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8315 			dissector->used_keys);
8316 		return -EOPNOTSUPP;
8317 	}
8318 
8319 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8320 		struct flow_match_enc_keyid match;
8321 
8322 		flow_rule_match_enc_keyid(rule, &match);
8323 		if (match.mask->keyid != 0)
8324 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8325 
8326 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8327 	}
8328 
8329 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8330 		struct flow_match_basic match;
8331 
8332 		flow_rule_match_basic(rule, &match);
8333 		n_proto_key = ntohs(match.key->n_proto);
8334 		n_proto_mask = ntohs(match.mask->n_proto);
8335 
8336 		if (n_proto_key == ETH_P_ALL) {
8337 			n_proto_key = 0;
8338 			n_proto_mask = 0;
8339 		}
8340 		filter->n_proto = n_proto_key & n_proto_mask;
8341 		filter->ip_proto = match.key->ip_proto;
8342 	}
8343 
8344 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8345 		struct flow_match_eth_addrs match;
8346 
8347 		flow_rule_match_eth_addrs(rule, &match);
8348 
8349 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8350 		if (!is_zero_ether_addr(match.mask->dst)) {
8351 			if (is_broadcast_ether_addr(match.mask->dst)) {
8352 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8353 			} else {
8354 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8355 					match.mask->dst);
8356 				return I40E_ERR_CONFIG;
8357 			}
8358 		}
8359 
8360 		if (!is_zero_ether_addr(match.mask->src)) {
8361 			if (is_broadcast_ether_addr(match.mask->src)) {
8362 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8363 			} else {
8364 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8365 					match.mask->src);
8366 				return I40E_ERR_CONFIG;
8367 			}
8368 		}
8369 		ether_addr_copy(filter->dst_mac, match.key->dst);
8370 		ether_addr_copy(filter->src_mac, match.key->src);
8371 	}
8372 
8373 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8374 		struct flow_match_vlan match;
8375 
8376 		flow_rule_match_vlan(rule, &match);
8377 		if (match.mask->vlan_id) {
8378 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8379 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8380 
8381 			} else {
8382 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8383 					match.mask->vlan_id);
8384 				return I40E_ERR_CONFIG;
8385 			}
8386 		}
8387 
8388 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8389 	}
8390 
8391 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8392 		struct flow_match_control match;
8393 
8394 		flow_rule_match_control(rule, &match);
8395 		addr_type = match.key->addr_type;
8396 	}
8397 
8398 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8399 		struct flow_match_ipv4_addrs match;
8400 
8401 		flow_rule_match_ipv4_addrs(rule, &match);
8402 		if (match.mask->dst) {
8403 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8404 				field_flags |= I40E_CLOUD_FIELD_IIP;
8405 			} else {
8406 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8407 					&match.mask->dst);
8408 				return I40E_ERR_CONFIG;
8409 			}
8410 		}
8411 
8412 		if (match.mask->src) {
8413 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8414 				field_flags |= I40E_CLOUD_FIELD_IIP;
8415 			} else {
8416 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8417 					&match.mask->src);
8418 				return I40E_ERR_CONFIG;
8419 			}
8420 		}
8421 
8422 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8423 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8424 			return I40E_ERR_CONFIG;
8425 		}
8426 		filter->dst_ipv4 = match.key->dst;
8427 		filter->src_ipv4 = match.key->src;
8428 	}
8429 
8430 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8431 		struct flow_match_ipv6_addrs match;
8432 
8433 		flow_rule_match_ipv6_addrs(rule, &match);
8434 
8435 		/* src and dest IPV6 address should not be LOOPBACK
8436 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8437 		 */
8438 		if (ipv6_addr_loopback(&match.key->dst) ||
8439 		    ipv6_addr_loopback(&match.key->src)) {
8440 			dev_err(&pf->pdev->dev,
8441 				"Bad ipv6, addr is LOOPBACK\n");
8442 			return I40E_ERR_CONFIG;
8443 		}
8444 		if (!ipv6_addr_any(&match.mask->dst) ||
8445 		    !ipv6_addr_any(&match.mask->src))
8446 			field_flags |= I40E_CLOUD_FIELD_IIP;
8447 
8448 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8449 		       sizeof(filter->src_ipv6));
8450 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8451 		       sizeof(filter->dst_ipv6));
8452 	}
8453 
8454 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8455 		struct flow_match_ports match;
8456 
8457 		flow_rule_match_ports(rule, &match);
8458 		if (match.mask->src) {
8459 			if (match.mask->src == cpu_to_be16(0xffff)) {
8460 				field_flags |= I40E_CLOUD_FIELD_IIP;
8461 			} else {
8462 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8463 					be16_to_cpu(match.mask->src));
8464 				return I40E_ERR_CONFIG;
8465 			}
8466 		}
8467 
8468 		if (match.mask->dst) {
8469 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8470 				field_flags |= I40E_CLOUD_FIELD_IIP;
8471 			} else {
8472 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8473 					be16_to_cpu(match.mask->dst));
8474 				return I40E_ERR_CONFIG;
8475 			}
8476 		}
8477 
8478 		filter->dst_port = match.key->dst;
8479 		filter->src_port = match.key->src;
8480 
8481 		switch (filter->ip_proto) {
8482 		case IPPROTO_TCP:
8483 		case IPPROTO_UDP:
8484 			break;
8485 		default:
8486 			dev_err(&pf->pdev->dev,
8487 				"Only UDP and TCP transport are supported\n");
8488 			return -EINVAL;
8489 		}
8490 	}
8491 	filter->flags = field_flags;
8492 	return 0;
8493 }
8494 
8495 /**
8496  * i40e_handle_tclass: Forward to a traffic class on the device
8497  * @vsi: Pointer to VSI
8498  * @tc: traffic class index on the device
8499  * @filter: Pointer to cloud filter structure
8500  *
8501  **/
8502 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8503 			      struct i40e_cloud_filter *filter)
8504 {
8505 	struct i40e_channel *ch, *ch_tmp;
8506 
8507 	/* direct to a traffic class on the same device */
8508 	if (tc == 0) {
8509 		filter->seid = vsi->seid;
8510 		return 0;
8511 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8512 		if (!filter->dst_port) {
8513 			dev_err(&vsi->back->pdev->dev,
8514 				"Specify destination port to direct to traffic class that is not default\n");
8515 			return -EINVAL;
8516 		}
8517 		if (list_empty(&vsi->ch_list))
8518 			return -EINVAL;
8519 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8520 					 list) {
8521 			if (ch->seid == vsi->tc_seid_map[tc])
8522 				filter->seid = ch->seid;
8523 		}
8524 		return 0;
8525 	}
8526 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8527 	return -EINVAL;
8528 }
8529 
8530 /**
8531  * i40e_configure_clsflower - Configure tc flower filters
8532  * @vsi: Pointer to VSI
8533  * @cls_flower: Pointer to struct flow_cls_offload
8534  *
8535  **/
8536 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8537 				    struct flow_cls_offload *cls_flower)
8538 {
8539 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8540 	struct i40e_cloud_filter *filter = NULL;
8541 	struct i40e_pf *pf = vsi->back;
8542 	int err = 0;
8543 
8544 	if (tc < 0) {
8545 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8546 		return -EOPNOTSUPP;
8547 	}
8548 
8549 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8550 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8551 		return -EBUSY;
8552 
8553 	if (pf->fdir_pf_active_filters ||
8554 	    (!hlist_empty(&pf->fdir_filter_list))) {
8555 		dev_err(&vsi->back->pdev->dev,
8556 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8557 		return -EINVAL;
8558 	}
8559 
8560 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8561 		dev_err(&vsi->back->pdev->dev,
8562 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8563 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8564 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8565 	}
8566 
8567 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8568 	if (!filter)
8569 		return -ENOMEM;
8570 
8571 	filter->cookie = cls_flower->cookie;
8572 
8573 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8574 	if (err < 0)
8575 		goto err;
8576 
8577 	err = i40e_handle_tclass(vsi, tc, filter);
8578 	if (err < 0)
8579 		goto err;
8580 
8581 	/* Add cloud filter */
8582 	if (filter->dst_port)
8583 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8584 	else
8585 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8586 
8587 	if (err) {
8588 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8589 			err);
8590 		goto err;
8591 	}
8592 
8593 	/* add filter to the ordered list */
8594 	INIT_HLIST_NODE(&filter->cloud_node);
8595 
8596 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8597 
8598 	pf->num_cloud_filters++;
8599 
8600 	return err;
8601 err:
8602 	kfree(filter);
8603 	return err;
8604 }
8605 
8606 /**
8607  * i40e_find_cloud_filter - Find the could filter in the list
8608  * @vsi: Pointer to VSI
8609  * @cookie: filter specific cookie
8610  *
8611  **/
8612 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8613 							unsigned long *cookie)
8614 {
8615 	struct i40e_cloud_filter *filter = NULL;
8616 	struct hlist_node *node2;
8617 
8618 	hlist_for_each_entry_safe(filter, node2,
8619 				  &vsi->back->cloud_filter_list, cloud_node)
8620 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8621 			return filter;
8622 	return NULL;
8623 }
8624 
8625 /**
8626  * i40e_delete_clsflower - Remove tc flower filters
8627  * @vsi: Pointer to VSI
8628  * @cls_flower: Pointer to struct flow_cls_offload
8629  *
8630  **/
8631 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8632 				 struct flow_cls_offload *cls_flower)
8633 {
8634 	struct i40e_cloud_filter *filter = NULL;
8635 	struct i40e_pf *pf = vsi->back;
8636 	int err = 0;
8637 
8638 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8639 
8640 	if (!filter)
8641 		return -EINVAL;
8642 
8643 	hash_del(&filter->cloud_node);
8644 
8645 	if (filter->dst_port)
8646 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8647 	else
8648 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8649 
8650 	kfree(filter);
8651 	if (err) {
8652 		dev_err(&pf->pdev->dev,
8653 			"Failed to delete cloud filter, err %s\n",
8654 			i40e_stat_str(&pf->hw, err));
8655 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8656 	}
8657 
8658 	pf->num_cloud_filters--;
8659 	if (!pf->num_cloud_filters)
8660 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8661 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8662 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8663 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8664 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8665 		}
8666 	return 0;
8667 }
8668 
8669 /**
8670  * i40e_setup_tc_cls_flower - flower classifier offloads
8671  * @np: net device to configure
8672  * @cls_flower: offload data
8673  **/
8674 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8675 				    struct flow_cls_offload *cls_flower)
8676 {
8677 	struct i40e_vsi *vsi = np->vsi;
8678 
8679 	switch (cls_flower->command) {
8680 	case FLOW_CLS_REPLACE:
8681 		return i40e_configure_clsflower(vsi, cls_flower);
8682 	case FLOW_CLS_DESTROY:
8683 		return i40e_delete_clsflower(vsi, cls_flower);
8684 	case FLOW_CLS_STATS:
8685 		return -EOPNOTSUPP;
8686 	default:
8687 		return -EOPNOTSUPP;
8688 	}
8689 }
8690 
8691 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8692 				  void *cb_priv)
8693 {
8694 	struct i40e_netdev_priv *np = cb_priv;
8695 
8696 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8697 		return -EOPNOTSUPP;
8698 
8699 	switch (type) {
8700 	case TC_SETUP_CLSFLOWER:
8701 		return i40e_setup_tc_cls_flower(np, type_data);
8702 
8703 	default:
8704 		return -EOPNOTSUPP;
8705 	}
8706 }
8707 
8708 static LIST_HEAD(i40e_block_cb_list);
8709 
8710 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8711 			   void *type_data)
8712 {
8713 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8714 
8715 	switch (type) {
8716 	case TC_SETUP_QDISC_MQPRIO:
8717 		return i40e_setup_tc(netdev, type_data);
8718 	case TC_SETUP_BLOCK:
8719 		return flow_block_cb_setup_simple(type_data,
8720 						  &i40e_block_cb_list,
8721 						  i40e_setup_tc_block_cb,
8722 						  np, np, true);
8723 	default:
8724 		return -EOPNOTSUPP;
8725 	}
8726 }
8727 
8728 /**
8729  * i40e_open - Called when a network interface is made active
8730  * @netdev: network interface device structure
8731  *
8732  * The open entry point is called when a network interface is made
8733  * active by the system (IFF_UP).  At this point all resources needed
8734  * for transmit and receive operations are allocated, the interrupt
8735  * handler is registered with the OS, the netdev watchdog subtask is
8736  * enabled, and the stack is notified that the interface is ready.
8737  *
8738  * Returns 0 on success, negative value on failure
8739  **/
8740 int i40e_open(struct net_device *netdev)
8741 {
8742 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8743 	struct i40e_vsi *vsi = np->vsi;
8744 	struct i40e_pf *pf = vsi->back;
8745 	int err;
8746 
8747 	/* disallow open during test or if eeprom is broken */
8748 	if (test_bit(__I40E_TESTING, pf->state) ||
8749 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8750 		return -EBUSY;
8751 
8752 	netif_carrier_off(netdev);
8753 
8754 	if (i40e_force_link_state(pf, true))
8755 		return -EAGAIN;
8756 
8757 	err = i40e_vsi_open(vsi);
8758 	if (err)
8759 		return err;
8760 
8761 	/* configure global TSO hardware offload settings */
8762 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8763 						       TCP_FLAG_FIN) >> 16);
8764 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8765 						       TCP_FLAG_FIN |
8766 						       TCP_FLAG_CWR) >> 16);
8767 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8768 	udp_tunnel_get_rx_info(netdev);
8769 
8770 	return 0;
8771 }
8772 
8773 /**
8774  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
8775  * @vsi: vsi structure
8776  *
8777  * This updates netdev's number of tx/rx queues
8778  *
8779  * Returns status of setting tx/rx queues
8780  **/
8781 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
8782 {
8783 	int ret;
8784 
8785 	ret = netif_set_real_num_rx_queues(vsi->netdev,
8786 					   vsi->num_queue_pairs);
8787 	if (ret)
8788 		return ret;
8789 
8790 	return netif_set_real_num_tx_queues(vsi->netdev,
8791 					    vsi->num_queue_pairs);
8792 }
8793 
8794 /**
8795  * i40e_vsi_open -
8796  * @vsi: the VSI to open
8797  *
8798  * Finish initialization of the VSI.
8799  *
8800  * Returns 0 on success, negative value on failure
8801  *
8802  * Note: expects to be called while under rtnl_lock()
8803  **/
8804 int i40e_vsi_open(struct i40e_vsi *vsi)
8805 {
8806 	struct i40e_pf *pf = vsi->back;
8807 	char int_name[I40E_INT_NAME_STR_LEN];
8808 	int err;
8809 
8810 	/* allocate descriptors */
8811 	err = i40e_vsi_setup_tx_resources(vsi);
8812 	if (err)
8813 		goto err_setup_tx;
8814 	err = i40e_vsi_setup_rx_resources(vsi);
8815 	if (err)
8816 		goto err_setup_rx;
8817 
8818 	err = i40e_vsi_configure(vsi);
8819 	if (err)
8820 		goto err_setup_rx;
8821 
8822 	if (vsi->netdev) {
8823 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
8824 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
8825 		err = i40e_vsi_request_irq(vsi, int_name);
8826 		if (err)
8827 			goto err_setup_rx;
8828 
8829 		/* Notify the stack of the actual queue counts. */
8830 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
8831 		if (err)
8832 			goto err_set_queues;
8833 
8834 	} else if (vsi->type == I40E_VSI_FDIR) {
8835 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
8836 			 dev_driver_string(&pf->pdev->dev),
8837 			 dev_name(&pf->pdev->dev));
8838 		err = i40e_vsi_request_irq(vsi, int_name);
8839 		if (err)
8840 			goto err_setup_rx;
8841 
8842 	} else {
8843 		err = -EINVAL;
8844 		goto err_setup_rx;
8845 	}
8846 
8847 	err = i40e_up_complete(vsi);
8848 	if (err)
8849 		goto err_up_complete;
8850 
8851 	return 0;
8852 
8853 err_up_complete:
8854 	i40e_down(vsi);
8855 err_set_queues:
8856 	i40e_vsi_free_irq(vsi);
8857 err_setup_rx:
8858 	i40e_vsi_free_rx_resources(vsi);
8859 err_setup_tx:
8860 	i40e_vsi_free_tx_resources(vsi);
8861 	if (vsi == pf->vsi[pf->lan_vsi])
8862 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
8863 
8864 	return err;
8865 }
8866 
8867 /**
8868  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
8869  * @pf: Pointer to PF
8870  *
8871  * This function destroys the hlist where all the Flow Director
8872  * filters were saved.
8873  **/
8874 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
8875 {
8876 	struct i40e_fdir_filter *filter;
8877 	struct i40e_flex_pit *pit_entry, *tmp;
8878 	struct hlist_node *node2;
8879 
8880 	hlist_for_each_entry_safe(filter, node2,
8881 				  &pf->fdir_filter_list, fdir_node) {
8882 		hlist_del(&filter->fdir_node);
8883 		kfree(filter);
8884 	}
8885 
8886 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
8887 		list_del(&pit_entry->list);
8888 		kfree(pit_entry);
8889 	}
8890 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
8891 
8892 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
8893 		list_del(&pit_entry->list);
8894 		kfree(pit_entry);
8895 	}
8896 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
8897 
8898 	pf->fdir_pf_active_filters = 0;
8899 	i40e_reset_fdir_filter_cnt(pf);
8900 
8901 	/* Reprogram the default input set for TCP/IPv4 */
8902 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8903 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8904 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8905 
8906 	/* Reprogram the default input set for TCP/IPv6 */
8907 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
8908 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8909 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8910 
8911 	/* Reprogram the default input set for UDP/IPv4 */
8912 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
8913 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8914 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8915 
8916 	/* Reprogram the default input set for UDP/IPv6 */
8917 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
8918 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8919 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8920 
8921 	/* Reprogram the default input set for SCTP/IPv4 */
8922 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
8923 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8924 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8925 
8926 	/* Reprogram the default input set for SCTP/IPv6 */
8927 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
8928 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8929 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8930 
8931 	/* Reprogram the default input set for Other/IPv4 */
8932 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
8933 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8934 
8935 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
8936 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8937 
8938 	/* Reprogram the default input set for Other/IPv6 */
8939 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
8940 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8941 
8942 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
8943 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8944 }
8945 
8946 /**
8947  * i40e_cloud_filter_exit - Cleans up the cloud filters
8948  * @pf: Pointer to PF
8949  *
8950  * This function destroys the hlist where all the cloud filters
8951  * were saved.
8952  **/
8953 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
8954 {
8955 	struct i40e_cloud_filter *cfilter;
8956 	struct hlist_node *node;
8957 
8958 	hlist_for_each_entry_safe(cfilter, node,
8959 				  &pf->cloud_filter_list, cloud_node) {
8960 		hlist_del(&cfilter->cloud_node);
8961 		kfree(cfilter);
8962 	}
8963 	pf->num_cloud_filters = 0;
8964 
8965 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8966 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8967 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8968 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8969 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8970 	}
8971 }
8972 
8973 /**
8974  * i40e_close - Disables a network interface
8975  * @netdev: network interface device structure
8976  *
8977  * The close entry point is called when an interface is de-activated
8978  * by the OS.  The hardware is still under the driver's control, but
8979  * this netdev interface is disabled.
8980  *
8981  * Returns 0, this is not allowed to fail
8982  **/
8983 int i40e_close(struct net_device *netdev)
8984 {
8985 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8986 	struct i40e_vsi *vsi = np->vsi;
8987 
8988 	i40e_vsi_close(vsi);
8989 
8990 	return 0;
8991 }
8992 
8993 /**
8994  * i40e_do_reset - Start a PF or Core Reset sequence
8995  * @pf: board private structure
8996  * @reset_flags: which reset is requested
8997  * @lock_acquired: indicates whether or not the lock has been acquired
8998  * before this function was called.
8999  *
9000  * The essential difference in resets is that the PF Reset
9001  * doesn't clear the packet buffers, doesn't reset the PE
9002  * firmware, and doesn't bother the other PFs on the chip.
9003  **/
9004 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9005 {
9006 	u32 val;
9007 
9008 	/* do the biggest reset indicated */
9009 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9010 
9011 		/* Request a Global Reset
9012 		 *
9013 		 * This will start the chip's countdown to the actual full
9014 		 * chip reset event, and a warning interrupt to be sent
9015 		 * to all PFs, including the requestor.  Our handler
9016 		 * for the warning interrupt will deal with the shutdown
9017 		 * and recovery of the switch setup.
9018 		 */
9019 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9020 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9021 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9022 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9023 
9024 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9025 
9026 		/* Request a Core Reset
9027 		 *
9028 		 * Same as Global Reset, except does *not* include the MAC/PHY
9029 		 */
9030 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9031 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9032 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9033 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9034 		i40e_flush(&pf->hw);
9035 
9036 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9037 
9038 		/* Request a PF Reset
9039 		 *
9040 		 * Resets only the PF-specific registers
9041 		 *
9042 		 * This goes directly to the tear-down and rebuild of
9043 		 * the switch, since we need to do all the recovery as
9044 		 * for the Core Reset.
9045 		 */
9046 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9047 		i40e_handle_reset_warning(pf, lock_acquired);
9048 
9049 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9050 		/* Request a PF Reset
9051 		 *
9052 		 * Resets PF and reinitializes PFs VSI.
9053 		 */
9054 		i40e_prep_for_reset(pf);
9055 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9056 		dev_info(&pf->pdev->dev,
9057 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9058 			 "FW LLDP is disabled\n" :
9059 			 "FW LLDP is enabled\n");
9060 
9061 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9062 		int v;
9063 
9064 		/* Find the VSI(s) that requested a re-init */
9065 		dev_info(&pf->pdev->dev,
9066 			 "VSI reinit requested\n");
9067 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9068 			struct i40e_vsi *vsi = pf->vsi[v];
9069 
9070 			if (vsi != NULL &&
9071 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9072 					       vsi->state))
9073 				i40e_vsi_reinit_locked(pf->vsi[v]);
9074 		}
9075 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9076 		int v;
9077 
9078 		/* Find the VSI(s) that needs to be brought down */
9079 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9080 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9081 			struct i40e_vsi *vsi = pf->vsi[v];
9082 
9083 			if (vsi != NULL &&
9084 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9085 					       vsi->state)) {
9086 				set_bit(__I40E_VSI_DOWN, vsi->state);
9087 				i40e_down(vsi);
9088 			}
9089 		}
9090 	} else {
9091 		dev_info(&pf->pdev->dev,
9092 			 "bad reset request 0x%08x\n", reset_flags);
9093 	}
9094 }
9095 
9096 #ifdef CONFIG_I40E_DCB
9097 /**
9098  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9099  * @pf: board private structure
9100  * @old_cfg: current DCB config
9101  * @new_cfg: new DCB config
9102  **/
9103 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9104 			    struct i40e_dcbx_config *old_cfg,
9105 			    struct i40e_dcbx_config *new_cfg)
9106 {
9107 	bool need_reconfig = false;
9108 
9109 	/* Check if ETS configuration has changed */
9110 	if (memcmp(&new_cfg->etscfg,
9111 		   &old_cfg->etscfg,
9112 		   sizeof(new_cfg->etscfg))) {
9113 		/* If Priority Table has changed reconfig is needed */
9114 		if (memcmp(&new_cfg->etscfg.prioritytable,
9115 			   &old_cfg->etscfg.prioritytable,
9116 			   sizeof(new_cfg->etscfg.prioritytable))) {
9117 			need_reconfig = true;
9118 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9119 		}
9120 
9121 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9122 			   &old_cfg->etscfg.tcbwtable,
9123 			   sizeof(new_cfg->etscfg.tcbwtable)))
9124 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9125 
9126 		if (memcmp(&new_cfg->etscfg.tsatable,
9127 			   &old_cfg->etscfg.tsatable,
9128 			   sizeof(new_cfg->etscfg.tsatable)))
9129 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9130 	}
9131 
9132 	/* Check if PFC configuration has changed */
9133 	if (memcmp(&new_cfg->pfc,
9134 		   &old_cfg->pfc,
9135 		   sizeof(new_cfg->pfc))) {
9136 		need_reconfig = true;
9137 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9138 	}
9139 
9140 	/* Check if APP Table has changed */
9141 	if (memcmp(&new_cfg->app,
9142 		   &old_cfg->app,
9143 		   sizeof(new_cfg->app))) {
9144 		need_reconfig = true;
9145 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9146 	}
9147 
9148 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9149 	return need_reconfig;
9150 }
9151 
9152 /**
9153  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9154  * @pf: board private structure
9155  * @e: event info posted on ARQ
9156  **/
9157 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9158 				  struct i40e_arq_event_info *e)
9159 {
9160 	struct i40e_aqc_lldp_get_mib *mib =
9161 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9162 	struct i40e_hw *hw = &pf->hw;
9163 	struct i40e_dcbx_config tmp_dcbx_cfg;
9164 	bool need_reconfig = false;
9165 	int ret = 0;
9166 	u8 type;
9167 
9168 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9169 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9170 	    (hw->phy.link_info.link_speed &
9171 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9172 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9173 		/* let firmware decide if the DCB should be disabled */
9174 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9175 
9176 	/* Not DCB capable or capability disabled */
9177 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9178 		return ret;
9179 
9180 	/* Ignore if event is not for Nearest Bridge */
9181 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9182 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9183 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9184 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9185 		return ret;
9186 
9187 	/* Check MIB Type and return if event for Remote MIB update */
9188 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9189 	dev_dbg(&pf->pdev->dev,
9190 		"LLDP event mib type %s\n", type ? "remote" : "local");
9191 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9192 		/* Update the remote cached instance and return */
9193 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9194 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9195 				&hw->remote_dcbx_config);
9196 		goto exit;
9197 	}
9198 
9199 	/* Store the old configuration */
9200 	tmp_dcbx_cfg = hw->local_dcbx_config;
9201 
9202 	/* Reset the old DCBx configuration data */
9203 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9204 	/* Get updated DCBX data from firmware */
9205 	ret = i40e_get_dcb_config(&pf->hw);
9206 	if (ret) {
9207 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9208 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9209 		    (hw->phy.link_info.link_speed &
9210 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9211 			dev_warn(&pf->pdev->dev,
9212 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9213 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9214 		} else {
9215 			dev_info(&pf->pdev->dev,
9216 				 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
9217 				 i40e_stat_str(&pf->hw, ret),
9218 				 i40e_aq_str(&pf->hw,
9219 					     pf->hw.aq.asq_last_status));
9220 		}
9221 		goto exit;
9222 	}
9223 
9224 	/* No change detected in DCBX configs */
9225 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9226 		    sizeof(tmp_dcbx_cfg))) {
9227 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9228 		goto exit;
9229 	}
9230 
9231 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9232 					       &hw->local_dcbx_config);
9233 
9234 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9235 
9236 	if (!need_reconfig)
9237 		goto exit;
9238 
9239 	/* Enable DCB tagging only when more than one TC */
9240 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9241 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9242 	else
9243 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9244 
9245 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9246 	/* Reconfiguration needed quiesce all VSIs */
9247 	i40e_pf_quiesce_all_vsi(pf);
9248 
9249 	/* Changes in configuration update VEB/VSI */
9250 	i40e_dcb_reconfigure(pf);
9251 
9252 	ret = i40e_resume_port_tx(pf);
9253 
9254 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9255 	/* In case of error no point in resuming VSIs */
9256 	if (ret)
9257 		goto exit;
9258 
9259 	/* Wait for the PF's queues to be disabled */
9260 	ret = i40e_pf_wait_queues_disabled(pf);
9261 	if (ret) {
9262 		/* Schedule PF reset to recover */
9263 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9264 		i40e_service_event_schedule(pf);
9265 	} else {
9266 		i40e_pf_unquiesce_all_vsi(pf);
9267 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9268 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9269 	}
9270 
9271 exit:
9272 	return ret;
9273 }
9274 #endif /* CONFIG_I40E_DCB */
9275 
9276 /**
9277  * i40e_do_reset_safe - Protected reset path for userland calls.
9278  * @pf: board private structure
9279  * @reset_flags: which reset is requested
9280  *
9281  **/
9282 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9283 {
9284 	rtnl_lock();
9285 	i40e_do_reset(pf, reset_flags, true);
9286 	rtnl_unlock();
9287 }
9288 
9289 /**
9290  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9291  * @pf: board private structure
9292  * @e: event info posted on ARQ
9293  *
9294  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9295  * and VF queues
9296  **/
9297 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9298 					   struct i40e_arq_event_info *e)
9299 {
9300 	struct i40e_aqc_lan_overflow *data =
9301 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9302 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9303 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9304 	struct i40e_hw *hw = &pf->hw;
9305 	struct i40e_vf *vf;
9306 	u16 vf_id;
9307 
9308 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9309 		queue, qtx_ctl);
9310 
9311 	/* Queue belongs to VF, find the VF and issue VF reset */
9312 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9313 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9314 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9315 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9316 		vf_id -= hw->func_caps.vf_base_id;
9317 		vf = &pf->vf[vf_id];
9318 		i40e_vc_notify_vf_reset(vf);
9319 		/* Allow VF to process pending reset notification */
9320 		msleep(20);
9321 		i40e_reset_vf(vf, false);
9322 	}
9323 }
9324 
9325 /**
9326  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9327  * @pf: board private structure
9328  **/
9329 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9330 {
9331 	u32 val, fcnt_prog;
9332 
9333 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9334 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9335 	return fcnt_prog;
9336 }
9337 
9338 /**
9339  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9340  * @pf: board private structure
9341  **/
9342 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9343 {
9344 	u32 val, fcnt_prog;
9345 
9346 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9347 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9348 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9349 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9350 	return fcnt_prog;
9351 }
9352 
9353 /**
9354  * i40e_get_global_fd_count - Get total FD filters programmed on device
9355  * @pf: board private structure
9356  **/
9357 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9358 {
9359 	u32 val, fcnt_prog;
9360 
9361 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9362 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9363 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9364 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9365 	return fcnt_prog;
9366 }
9367 
9368 /**
9369  * i40e_reenable_fdir_sb - Restore FDir SB capability
9370  * @pf: board private structure
9371  **/
9372 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9373 {
9374 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9375 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9376 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9377 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9378 }
9379 
9380 /**
9381  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9382  * @pf: board private structure
9383  **/
9384 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9385 {
9386 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9387 		/* ATR uses the same filtering logic as SB rules. It only
9388 		 * functions properly if the input set mask is at the default
9389 		 * settings. It is safe to restore the default input set
9390 		 * because there are no active TCPv4 filter rules.
9391 		 */
9392 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9393 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9394 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9395 
9396 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9397 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9398 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9399 	}
9400 }
9401 
9402 /**
9403  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9404  * @pf: board private structure
9405  * @filter: FDir filter to remove
9406  */
9407 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9408 				       struct i40e_fdir_filter *filter)
9409 {
9410 	/* Update counters */
9411 	pf->fdir_pf_active_filters--;
9412 	pf->fd_inv = 0;
9413 
9414 	switch (filter->flow_type) {
9415 	case TCP_V4_FLOW:
9416 		pf->fd_tcp4_filter_cnt--;
9417 		break;
9418 	case UDP_V4_FLOW:
9419 		pf->fd_udp4_filter_cnt--;
9420 		break;
9421 	case SCTP_V4_FLOW:
9422 		pf->fd_sctp4_filter_cnt--;
9423 		break;
9424 	case TCP_V6_FLOW:
9425 		pf->fd_tcp6_filter_cnt--;
9426 		break;
9427 	case UDP_V6_FLOW:
9428 		pf->fd_udp6_filter_cnt--;
9429 		break;
9430 	case SCTP_V6_FLOW:
9431 		pf->fd_udp6_filter_cnt--;
9432 		break;
9433 	case IP_USER_FLOW:
9434 		switch (filter->ipl4_proto) {
9435 		case IPPROTO_TCP:
9436 			pf->fd_tcp4_filter_cnt--;
9437 			break;
9438 		case IPPROTO_UDP:
9439 			pf->fd_udp4_filter_cnt--;
9440 			break;
9441 		case IPPROTO_SCTP:
9442 			pf->fd_sctp4_filter_cnt--;
9443 			break;
9444 		case IPPROTO_IP:
9445 			pf->fd_ip4_filter_cnt--;
9446 			break;
9447 		}
9448 		break;
9449 	case IPV6_USER_FLOW:
9450 		switch (filter->ipl4_proto) {
9451 		case IPPROTO_TCP:
9452 			pf->fd_tcp6_filter_cnt--;
9453 			break;
9454 		case IPPROTO_UDP:
9455 			pf->fd_udp6_filter_cnt--;
9456 			break;
9457 		case IPPROTO_SCTP:
9458 			pf->fd_sctp6_filter_cnt--;
9459 			break;
9460 		case IPPROTO_IP:
9461 			pf->fd_ip6_filter_cnt--;
9462 			break;
9463 		}
9464 		break;
9465 	}
9466 
9467 	/* Remove the filter from the list and free memory */
9468 	hlist_del(&filter->fdir_node);
9469 	kfree(filter);
9470 }
9471 
9472 /**
9473  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9474  * @pf: board private structure
9475  **/
9476 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9477 {
9478 	struct i40e_fdir_filter *filter;
9479 	u32 fcnt_prog, fcnt_avail;
9480 	struct hlist_node *node;
9481 
9482 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9483 		return;
9484 
9485 	/* Check if we have enough room to re-enable FDir SB capability. */
9486 	fcnt_prog = i40e_get_global_fd_count(pf);
9487 	fcnt_avail = pf->fdir_pf_filter_count;
9488 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9489 	    (pf->fd_add_err == 0) ||
9490 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9491 		i40e_reenable_fdir_sb(pf);
9492 
9493 	/* We should wait for even more space before re-enabling ATR.
9494 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9495 	 * rules active.
9496 	 */
9497 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9498 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9499 		i40e_reenable_fdir_atr(pf);
9500 
9501 	/* if hw had a problem adding a filter, delete it */
9502 	if (pf->fd_inv > 0) {
9503 		hlist_for_each_entry_safe(filter, node,
9504 					  &pf->fdir_filter_list, fdir_node)
9505 			if (filter->fd_id == pf->fd_inv)
9506 				i40e_delete_invalid_filter(pf, filter);
9507 	}
9508 }
9509 
9510 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9511 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9512 /**
9513  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9514  * @pf: board private structure
9515  **/
9516 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9517 {
9518 	unsigned long min_flush_time;
9519 	int flush_wait_retry = 50;
9520 	bool disable_atr = false;
9521 	int fd_room;
9522 	int reg;
9523 
9524 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9525 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9526 		return;
9527 
9528 	/* If the flush is happening too quick and we have mostly SB rules we
9529 	 * should not re-enable ATR for some time.
9530 	 */
9531 	min_flush_time = pf->fd_flush_timestamp +
9532 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9533 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9534 
9535 	if (!(time_after(jiffies, min_flush_time)) &&
9536 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9537 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9538 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9539 		disable_atr = true;
9540 	}
9541 
9542 	pf->fd_flush_timestamp = jiffies;
9543 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9544 	/* flush all filters */
9545 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9546 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9547 	i40e_flush(&pf->hw);
9548 	pf->fd_flush_cnt++;
9549 	pf->fd_add_err = 0;
9550 	do {
9551 		/* Check FD flush status every 5-6msec */
9552 		usleep_range(5000, 6000);
9553 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9554 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9555 			break;
9556 	} while (flush_wait_retry--);
9557 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9558 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9559 	} else {
9560 		/* replay sideband filters */
9561 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9562 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9563 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9564 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9565 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9566 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9567 	}
9568 }
9569 
9570 /**
9571  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9572  * @pf: board private structure
9573  **/
9574 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9575 {
9576 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9577 }
9578 
9579 /**
9580  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9581  * @pf: board private structure
9582  **/
9583 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9584 {
9585 
9586 	/* if interface is down do nothing */
9587 	if (test_bit(__I40E_DOWN, pf->state))
9588 		return;
9589 
9590 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9591 		i40e_fdir_flush_and_replay(pf);
9592 
9593 	i40e_fdir_check_and_reenable(pf);
9594 
9595 }
9596 
9597 /**
9598  * i40e_vsi_link_event - notify VSI of a link event
9599  * @vsi: vsi to be notified
9600  * @link_up: link up or down
9601  **/
9602 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9603 {
9604 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9605 		return;
9606 
9607 	switch (vsi->type) {
9608 	case I40E_VSI_MAIN:
9609 		if (!vsi->netdev || !vsi->netdev_registered)
9610 			break;
9611 
9612 		if (link_up) {
9613 			netif_carrier_on(vsi->netdev);
9614 			netif_tx_wake_all_queues(vsi->netdev);
9615 		} else {
9616 			netif_carrier_off(vsi->netdev);
9617 			netif_tx_stop_all_queues(vsi->netdev);
9618 		}
9619 		break;
9620 
9621 	case I40E_VSI_SRIOV:
9622 	case I40E_VSI_VMDQ2:
9623 	case I40E_VSI_CTRL:
9624 	case I40E_VSI_IWARP:
9625 	case I40E_VSI_MIRROR:
9626 	default:
9627 		/* there is no notification for other VSIs */
9628 		break;
9629 	}
9630 }
9631 
9632 /**
9633  * i40e_veb_link_event - notify elements on the veb of a link event
9634  * @veb: veb to be notified
9635  * @link_up: link up or down
9636  **/
9637 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9638 {
9639 	struct i40e_pf *pf;
9640 	int i;
9641 
9642 	if (!veb || !veb->pf)
9643 		return;
9644 	pf = veb->pf;
9645 
9646 	/* depth first... */
9647 	for (i = 0; i < I40E_MAX_VEB; i++)
9648 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9649 			i40e_veb_link_event(pf->veb[i], link_up);
9650 
9651 	/* ... now the local VSIs */
9652 	for (i = 0; i < pf->num_alloc_vsi; i++)
9653 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9654 			i40e_vsi_link_event(pf->vsi[i], link_up);
9655 }
9656 
9657 /**
9658  * i40e_link_event - Update netif_carrier status
9659  * @pf: board private structure
9660  **/
9661 static void i40e_link_event(struct i40e_pf *pf)
9662 {
9663 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9664 	u8 new_link_speed, old_link_speed;
9665 	i40e_status status;
9666 	bool new_link, old_link;
9667 #ifdef CONFIG_I40E_DCB
9668 	int err;
9669 #endif /* CONFIG_I40E_DCB */
9670 
9671 	/* set this to force the get_link_status call to refresh state */
9672 	pf->hw.phy.get_link_info = true;
9673 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9674 	status = i40e_get_link_status(&pf->hw, &new_link);
9675 
9676 	/* On success, disable temp link polling */
9677 	if (status == I40E_SUCCESS) {
9678 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9679 	} else {
9680 		/* Enable link polling temporarily until i40e_get_link_status
9681 		 * returns I40E_SUCCESS
9682 		 */
9683 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9684 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9685 			status);
9686 		return;
9687 	}
9688 
9689 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9690 	new_link_speed = pf->hw.phy.link_info.link_speed;
9691 
9692 	if (new_link == old_link &&
9693 	    new_link_speed == old_link_speed &&
9694 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9695 	     new_link == netif_carrier_ok(vsi->netdev)))
9696 		return;
9697 
9698 	i40e_print_link_message(vsi, new_link);
9699 
9700 	/* Notify the base of the switch tree connected to
9701 	 * the link.  Floating VEBs are not notified.
9702 	 */
9703 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9704 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9705 	else
9706 		i40e_vsi_link_event(vsi, new_link);
9707 
9708 	if (pf->vf)
9709 		i40e_vc_notify_link_state(pf);
9710 
9711 	if (pf->flags & I40E_FLAG_PTP)
9712 		i40e_ptp_set_increment(pf);
9713 #ifdef CONFIG_I40E_DCB
9714 	if (new_link == old_link)
9715 		return;
9716 	/* Not SW DCB so firmware will take care of default settings */
9717 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9718 		return;
9719 
9720 	/* We cover here only link down, as after link up in case of SW DCB
9721 	 * SW LLDP agent will take care of setting it up
9722 	 */
9723 	if (!new_link) {
9724 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9725 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9726 		err = i40e_dcb_sw_default_config(pf);
9727 		if (err) {
9728 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9729 				       I40E_FLAG_DCB_ENABLED);
9730 		} else {
9731 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9732 				       DCB_CAP_DCBX_VER_IEEE;
9733 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9734 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9735 		}
9736 	}
9737 #endif /* CONFIG_I40E_DCB */
9738 }
9739 
9740 /**
9741  * i40e_watchdog_subtask - periodic checks not using event driven response
9742  * @pf: board private structure
9743  **/
9744 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9745 {
9746 	int i;
9747 
9748 	/* if interface is down do nothing */
9749 	if (test_bit(__I40E_DOWN, pf->state) ||
9750 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9751 		return;
9752 
9753 	/* make sure we don't do these things too often */
9754 	if (time_before(jiffies, (pf->service_timer_previous +
9755 				  pf->service_timer_period)))
9756 		return;
9757 	pf->service_timer_previous = jiffies;
9758 
9759 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9760 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9761 		i40e_link_event(pf);
9762 
9763 	/* Update the stats for active netdevs so the network stack
9764 	 * can look at updated numbers whenever it cares to
9765 	 */
9766 	for (i = 0; i < pf->num_alloc_vsi; i++)
9767 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9768 			i40e_update_stats(pf->vsi[i]);
9769 
9770 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9771 		/* Update the stats for the active switching components */
9772 		for (i = 0; i < I40E_MAX_VEB; i++)
9773 			if (pf->veb[i])
9774 				i40e_update_veb_stats(pf->veb[i]);
9775 	}
9776 
9777 	i40e_ptp_rx_hang(pf);
9778 	i40e_ptp_tx_hang(pf);
9779 }
9780 
9781 /**
9782  * i40e_reset_subtask - Set up for resetting the device and driver
9783  * @pf: board private structure
9784  **/
9785 static void i40e_reset_subtask(struct i40e_pf *pf)
9786 {
9787 	u32 reset_flags = 0;
9788 
9789 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
9790 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
9791 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
9792 	}
9793 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
9794 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
9795 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9796 	}
9797 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
9798 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
9799 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
9800 	}
9801 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
9802 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
9803 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
9804 	}
9805 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
9806 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
9807 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
9808 	}
9809 
9810 	/* If there's a recovery already waiting, it takes
9811 	 * precedence before starting a new reset sequence.
9812 	 */
9813 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
9814 		i40e_prep_for_reset(pf);
9815 		i40e_reset(pf);
9816 		i40e_rebuild(pf, false, false);
9817 	}
9818 
9819 	/* If we're already down or resetting, just bail */
9820 	if (reset_flags &&
9821 	    !test_bit(__I40E_DOWN, pf->state) &&
9822 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
9823 		i40e_do_reset(pf, reset_flags, false);
9824 	}
9825 }
9826 
9827 /**
9828  * i40e_handle_link_event - Handle link event
9829  * @pf: board private structure
9830  * @e: event info posted on ARQ
9831  **/
9832 static void i40e_handle_link_event(struct i40e_pf *pf,
9833 				   struct i40e_arq_event_info *e)
9834 {
9835 	struct i40e_aqc_get_link_status *status =
9836 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
9837 
9838 	/* Do a new status request to re-enable LSE reporting
9839 	 * and load new status information into the hw struct
9840 	 * This completely ignores any state information
9841 	 * in the ARQ event info, instead choosing to always
9842 	 * issue the AQ update link status command.
9843 	 */
9844 	i40e_link_event(pf);
9845 
9846 	/* Check if module meets thermal requirements */
9847 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
9848 		dev_err(&pf->pdev->dev,
9849 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
9850 		dev_err(&pf->pdev->dev,
9851 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9852 	} else {
9853 		/* check for unqualified module, if link is down, suppress
9854 		 * the message if link was forced to be down.
9855 		 */
9856 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
9857 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
9858 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
9859 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
9860 			dev_err(&pf->pdev->dev,
9861 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
9862 			dev_err(&pf->pdev->dev,
9863 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9864 		}
9865 	}
9866 }
9867 
9868 /**
9869  * i40e_clean_adminq_subtask - Clean the AdminQ rings
9870  * @pf: board private structure
9871  **/
9872 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
9873 {
9874 	struct i40e_arq_event_info event;
9875 	struct i40e_hw *hw = &pf->hw;
9876 	u16 pending, i = 0;
9877 	i40e_status ret;
9878 	u16 opcode;
9879 	u32 oldval;
9880 	u32 val;
9881 
9882 	/* Do not run clean AQ when PF reset fails */
9883 	if (test_bit(__I40E_RESET_FAILED, pf->state))
9884 		return;
9885 
9886 	/* check for error indications */
9887 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
9888 	oldval = val;
9889 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
9890 		if (hw->debug_mask & I40E_DEBUG_AQ)
9891 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
9892 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
9893 	}
9894 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
9895 		if (hw->debug_mask & I40E_DEBUG_AQ)
9896 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
9897 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
9898 		pf->arq_overflows++;
9899 	}
9900 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
9901 		if (hw->debug_mask & I40E_DEBUG_AQ)
9902 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
9903 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
9904 	}
9905 	if (oldval != val)
9906 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
9907 
9908 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
9909 	oldval = val;
9910 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
9911 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9912 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
9913 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
9914 	}
9915 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
9916 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9917 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
9918 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
9919 	}
9920 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
9921 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9922 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
9923 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
9924 	}
9925 	if (oldval != val)
9926 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
9927 
9928 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
9929 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
9930 	if (!event.msg_buf)
9931 		return;
9932 
9933 	do {
9934 		ret = i40e_clean_arq_element(hw, &event, &pending);
9935 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
9936 			break;
9937 		else if (ret) {
9938 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
9939 			break;
9940 		}
9941 
9942 		opcode = le16_to_cpu(event.desc.opcode);
9943 		switch (opcode) {
9944 
9945 		case i40e_aqc_opc_get_link_status:
9946 			rtnl_lock();
9947 			i40e_handle_link_event(pf, &event);
9948 			rtnl_unlock();
9949 			break;
9950 		case i40e_aqc_opc_send_msg_to_pf:
9951 			ret = i40e_vc_process_vf_msg(pf,
9952 					le16_to_cpu(event.desc.retval),
9953 					le32_to_cpu(event.desc.cookie_high),
9954 					le32_to_cpu(event.desc.cookie_low),
9955 					event.msg_buf,
9956 					event.msg_len);
9957 			break;
9958 		case i40e_aqc_opc_lldp_update_mib:
9959 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
9960 #ifdef CONFIG_I40E_DCB
9961 			rtnl_lock();
9962 			i40e_handle_lldp_event(pf, &event);
9963 			rtnl_unlock();
9964 #endif /* CONFIG_I40E_DCB */
9965 			break;
9966 		case i40e_aqc_opc_event_lan_overflow:
9967 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
9968 			i40e_handle_lan_overflow_event(pf, &event);
9969 			break;
9970 		case i40e_aqc_opc_send_msg_to_peer:
9971 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
9972 			break;
9973 		case i40e_aqc_opc_nvm_erase:
9974 		case i40e_aqc_opc_nvm_update:
9975 		case i40e_aqc_opc_oem_post_update:
9976 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
9977 				   "ARQ NVM operation 0x%04x completed\n",
9978 				   opcode);
9979 			break;
9980 		default:
9981 			dev_info(&pf->pdev->dev,
9982 				 "ARQ: Unknown event 0x%04x ignored\n",
9983 				 opcode);
9984 			break;
9985 		}
9986 	} while (i++ < pf->adminq_work_limit);
9987 
9988 	if (i < pf->adminq_work_limit)
9989 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
9990 
9991 	/* re-enable Admin queue interrupt cause */
9992 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
9993 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
9994 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
9995 	i40e_flush(hw);
9996 
9997 	kfree(event.msg_buf);
9998 }
9999 
10000 /**
10001  * i40e_verify_eeprom - make sure eeprom is good to use
10002  * @pf: board private structure
10003  **/
10004 static void i40e_verify_eeprom(struct i40e_pf *pf)
10005 {
10006 	int err;
10007 
10008 	err = i40e_diag_eeprom_test(&pf->hw);
10009 	if (err) {
10010 		/* retry in case of garbage read */
10011 		err = i40e_diag_eeprom_test(&pf->hw);
10012 		if (err) {
10013 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10014 				 err);
10015 			set_bit(__I40E_BAD_EEPROM, pf->state);
10016 		}
10017 	}
10018 
10019 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10020 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10021 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10022 	}
10023 }
10024 
10025 /**
10026  * i40e_enable_pf_switch_lb
10027  * @pf: pointer to the PF structure
10028  *
10029  * enable switch loop back or die - no point in a return value
10030  **/
10031 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10032 {
10033 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10034 	struct i40e_vsi_context ctxt;
10035 	int ret;
10036 
10037 	ctxt.seid = pf->main_vsi_seid;
10038 	ctxt.pf_num = pf->hw.pf_id;
10039 	ctxt.vf_num = 0;
10040 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10041 	if (ret) {
10042 		dev_info(&pf->pdev->dev,
10043 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10044 			 i40e_stat_str(&pf->hw, ret),
10045 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10046 		return;
10047 	}
10048 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10049 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10050 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10051 
10052 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10053 	if (ret) {
10054 		dev_info(&pf->pdev->dev,
10055 			 "update vsi switch failed, err %s aq_err %s\n",
10056 			 i40e_stat_str(&pf->hw, ret),
10057 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10058 	}
10059 }
10060 
10061 /**
10062  * i40e_disable_pf_switch_lb
10063  * @pf: pointer to the PF structure
10064  *
10065  * disable switch loop back or die - no point in a return value
10066  **/
10067 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10068 {
10069 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10070 	struct i40e_vsi_context ctxt;
10071 	int ret;
10072 
10073 	ctxt.seid = pf->main_vsi_seid;
10074 	ctxt.pf_num = pf->hw.pf_id;
10075 	ctxt.vf_num = 0;
10076 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10077 	if (ret) {
10078 		dev_info(&pf->pdev->dev,
10079 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10080 			 i40e_stat_str(&pf->hw, ret),
10081 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10082 		return;
10083 	}
10084 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10085 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10086 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10087 
10088 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10089 	if (ret) {
10090 		dev_info(&pf->pdev->dev,
10091 			 "update vsi switch failed, err %s aq_err %s\n",
10092 			 i40e_stat_str(&pf->hw, ret),
10093 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10094 	}
10095 }
10096 
10097 /**
10098  * i40e_config_bridge_mode - Configure the HW bridge mode
10099  * @veb: pointer to the bridge instance
10100  *
10101  * Configure the loop back mode for the LAN VSI that is downlink to the
10102  * specified HW bridge instance. It is expected this function is called
10103  * when a new HW bridge is instantiated.
10104  **/
10105 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10106 {
10107 	struct i40e_pf *pf = veb->pf;
10108 
10109 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10110 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10111 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10112 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10113 		i40e_disable_pf_switch_lb(pf);
10114 	else
10115 		i40e_enable_pf_switch_lb(pf);
10116 }
10117 
10118 /**
10119  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10120  * @veb: pointer to the VEB instance
10121  *
10122  * This is a recursive function that first builds the attached VSIs then
10123  * recurses in to build the next layer of VEB.  We track the connections
10124  * through our own index numbers because the seid's from the HW could
10125  * change across the reset.
10126  **/
10127 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10128 {
10129 	struct i40e_vsi *ctl_vsi = NULL;
10130 	struct i40e_pf *pf = veb->pf;
10131 	int v, veb_idx;
10132 	int ret;
10133 
10134 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10135 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10136 		if (pf->vsi[v] &&
10137 		    pf->vsi[v]->veb_idx == veb->idx &&
10138 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10139 			ctl_vsi = pf->vsi[v];
10140 			break;
10141 		}
10142 	}
10143 	if (!ctl_vsi) {
10144 		dev_info(&pf->pdev->dev,
10145 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10146 		ret = -ENOENT;
10147 		goto end_reconstitute;
10148 	}
10149 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10150 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10151 	ret = i40e_add_vsi(ctl_vsi);
10152 	if (ret) {
10153 		dev_info(&pf->pdev->dev,
10154 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10155 			 veb->idx, ret);
10156 		goto end_reconstitute;
10157 	}
10158 	i40e_vsi_reset_stats(ctl_vsi);
10159 
10160 	/* create the VEB in the switch and move the VSI onto the VEB */
10161 	ret = i40e_add_veb(veb, ctl_vsi);
10162 	if (ret)
10163 		goto end_reconstitute;
10164 
10165 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10166 		veb->bridge_mode = BRIDGE_MODE_VEB;
10167 	else
10168 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10169 	i40e_config_bridge_mode(veb);
10170 
10171 	/* create the remaining VSIs attached to this VEB */
10172 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10173 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10174 			continue;
10175 
10176 		if (pf->vsi[v]->veb_idx == veb->idx) {
10177 			struct i40e_vsi *vsi = pf->vsi[v];
10178 
10179 			vsi->uplink_seid = veb->seid;
10180 			ret = i40e_add_vsi(vsi);
10181 			if (ret) {
10182 				dev_info(&pf->pdev->dev,
10183 					 "rebuild of vsi_idx %d failed: %d\n",
10184 					 v, ret);
10185 				goto end_reconstitute;
10186 			}
10187 			i40e_vsi_reset_stats(vsi);
10188 		}
10189 	}
10190 
10191 	/* create any VEBs attached to this VEB - RECURSION */
10192 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10193 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10194 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10195 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10196 			if (ret)
10197 				break;
10198 		}
10199 	}
10200 
10201 end_reconstitute:
10202 	return ret;
10203 }
10204 
10205 /**
10206  * i40e_get_capabilities - get info about the HW
10207  * @pf: the PF struct
10208  * @list_type: AQ capability to be queried
10209  **/
10210 static int i40e_get_capabilities(struct i40e_pf *pf,
10211 				 enum i40e_admin_queue_opc list_type)
10212 {
10213 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10214 	u16 data_size;
10215 	int buf_len;
10216 	int err;
10217 
10218 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10219 	do {
10220 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10221 		if (!cap_buf)
10222 			return -ENOMEM;
10223 
10224 		/* this loads the data into the hw struct for us */
10225 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10226 						    &data_size, list_type,
10227 						    NULL);
10228 		/* data loaded, buffer no longer needed */
10229 		kfree(cap_buf);
10230 
10231 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10232 			/* retry with a larger buffer */
10233 			buf_len = data_size;
10234 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10235 			dev_info(&pf->pdev->dev,
10236 				 "capability discovery failed, err %s aq_err %s\n",
10237 				 i40e_stat_str(&pf->hw, err),
10238 				 i40e_aq_str(&pf->hw,
10239 					     pf->hw.aq.asq_last_status));
10240 			return -ENODEV;
10241 		}
10242 	} while (err);
10243 
10244 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10245 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10246 			dev_info(&pf->pdev->dev,
10247 				 "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",
10248 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10249 				 pf->hw.func_caps.num_msix_vectors,
10250 				 pf->hw.func_caps.num_msix_vectors_vf,
10251 				 pf->hw.func_caps.fd_filters_guaranteed,
10252 				 pf->hw.func_caps.fd_filters_best_effort,
10253 				 pf->hw.func_caps.num_tx_qp,
10254 				 pf->hw.func_caps.num_vsis);
10255 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10256 			dev_info(&pf->pdev->dev,
10257 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10258 				 pf->hw.dev_caps.switch_mode,
10259 				 pf->hw.dev_caps.valid_functions);
10260 			dev_info(&pf->pdev->dev,
10261 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10262 				 pf->hw.dev_caps.sr_iov_1_1,
10263 				 pf->hw.dev_caps.num_vfs);
10264 			dev_info(&pf->pdev->dev,
10265 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10266 				 pf->hw.dev_caps.num_vsis,
10267 				 pf->hw.dev_caps.num_rx_qp,
10268 				 pf->hw.dev_caps.num_tx_qp);
10269 		}
10270 	}
10271 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10272 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10273 		       + pf->hw.func_caps.num_vfs)
10274 		if (pf->hw.revision_id == 0 &&
10275 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10276 			dev_info(&pf->pdev->dev,
10277 				 "got num_vsis %d, setting num_vsis to %d\n",
10278 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10279 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10280 		}
10281 	}
10282 	return 0;
10283 }
10284 
10285 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10286 
10287 /**
10288  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10289  * @pf: board private structure
10290  **/
10291 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10292 {
10293 	struct i40e_vsi *vsi;
10294 
10295 	/* quick workaround for an NVM issue that leaves a critical register
10296 	 * uninitialized
10297 	 */
10298 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10299 		static const u32 hkey[] = {
10300 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10301 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10302 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10303 			0x95b3a76d};
10304 		int i;
10305 
10306 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10307 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10308 	}
10309 
10310 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10311 		return;
10312 
10313 	/* find existing VSI and see if it needs configuring */
10314 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10315 
10316 	/* create a new VSI if none exists */
10317 	if (!vsi) {
10318 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10319 				     pf->vsi[pf->lan_vsi]->seid, 0);
10320 		if (!vsi) {
10321 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10322 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10323 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10324 			return;
10325 		}
10326 	}
10327 
10328 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10329 }
10330 
10331 /**
10332  * i40e_fdir_teardown - release the Flow Director resources
10333  * @pf: board private structure
10334  **/
10335 static void i40e_fdir_teardown(struct i40e_pf *pf)
10336 {
10337 	struct i40e_vsi *vsi;
10338 
10339 	i40e_fdir_filter_exit(pf);
10340 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10341 	if (vsi)
10342 		i40e_vsi_release(vsi);
10343 }
10344 
10345 /**
10346  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10347  * @vsi: PF main vsi
10348  * @seid: seid of main or channel VSIs
10349  *
10350  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10351  * existed before reset
10352  **/
10353 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10354 {
10355 	struct i40e_cloud_filter *cfilter;
10356 	struct i40e_pf *pf = vsi->back;
10357 	struct hlist_node *node;
10358 	i40e_status ret;
10359 
10360 	/* Add cloud filters back if they exist */
10361 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10362 				  cloud_node) {
10363 		if (cfilter->seid != seid)
10364 			continue;
10365 
10366 		if (cfilter->dst_port)
10367 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10368 								true);
10369 		else
10370 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10371 
10372 		if (ret) {
10373 			dev_dbg(&pf->pdev->dev,
10374 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
10375 				i40e_stat_str(&pf->hw, ret),
10376 				i40e_aq_str(&pf->hw,
10377 					    pf->hw.aq.asq_last_status));
10378 			return ret;
10379 		}
10380 	}
10381 	return 0;
10382 }
10383 
10384 /**
10385  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10386  * @vsi: PF main vsi
10387  *
10388  * Rebuilds channel VSIs if they existed before reset
10389  **/
10390 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10391 {
10392 	struct i40e_channel *ch, *ch_tmp;
10393 	i40e_status ret;
10394 
10395 	if (list_empty(&vsi->ch_list))
10396 		return 0;
10397 
10398 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10399 		if (!ch->initialized)
10400 			break;
10401 		/* Proceed with creation of channel (VMDq2) VSI */
10402 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10403 		if (ret) {
10404 			dev_info(&vsi->back->pdev->dev,
10405 				 "failed to rebuild channels using uplink_seid %u\n",
10406 				 vsi->uplink_seid);
10407 			return ret;
10408 		}
10409 		/* Reconfigure TX queues using QTX_CTL register */
10410 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10411 		if (ret) {
10412 			dev_info(&vsi->back->pdev->dev,
10413 				 "failed to configure TX rings for channel %u\n",
10414 				 ch->seid);
10415 			return ret;
10416 		}
10417 		/* update 'next_base_queue' */
10418 		vsi->next_base_queue = vsi->next_base_queue +
10419 							ch->num_queue_pairs;
10420 		if (ch->max_tx_rate) {
10421 			u64 credits = ch->max_tx_rate;
10422 
10423 			if (i40e_set_bw_limit(vsi, ch->seid,
10424 					      ch->max_tx_rate))
10425 				return -EINVAL;
10426 
10427 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10428 			dev_dbg(&vsi->back->pdev->dev,
10429 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10430 				ch->max_tx_rate,
10431 				credits,
10432 				ch->seid);
10433 		}
10434 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10435 		if (ret) {
10436 			dev_dbg(&vsi->back->pdev->dev,
10437 				"Failed to rebuild cloud filters for channel VSI %u\n",
10438 				ch->seid);
10439 			return ret;
10440 		}
10441 	}
10442 	return 0;
10443 }
10444 
10445 /**
10446  * i40e_prep_for_reset - prep for the core to reset
10447  * @pf: board private structure
10448  *
10449  * Close up the VFs and other things in prep for PF Reset.
10450   **/
10451 static void i40e_prep_for_reset(struct i40e_pf *pf)
10452 {
10453 	struct i40e_hw *hw = &pf->hw;
10454 	i40e_status ret = 0;
10455 	u32 v;
10456 
10457 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10458 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10459 		return;
10460 	if (i40e_check_asq_alive(&pf->hw))
10461 		i40e_vc_notify_reset(pf);
10462 
10463 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10464 
10465 	/* quiesce the VSIs and their queues that are not already DOWN */
10466 	i40e_pf_quiesce_all_vsi(pf);
10467 
10468 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10469 		if (pf->vsi[v])
10470 			pf->vsi[v]->seid = 0;
10471 	}
10472 
10473 	i40e_shutdown_adminq(&pf->hw);
10474 
10475 	/* call shutdown HMC */
10476 	if (hw->hmc.hmc_obj) {
10477 		ret = i40e_shutdown_lan_hmc(hw);
10478 		if (ret)
10479 			dev_warn(&pf->pdev->dev,
10480 				 "shutdown_lan_hmc failed: %d\n", ret);
10481 	}
10482 
10483 	/* Save the current PTP time so that we can restore the time after the
10484 	 * reset completes.
10485 	 */
10486 	i40e_ptp_save_hw_time(pf);
10487 }
10488 
10489 /**
10490  * i40e_send_version - update firmware with driver version
10491  * @pf: PF struct
10492  */
10493 static void i40e_send_version(struct i40e_pf *pf)
10494 {
10495 	struct i40e_driver_version dv;
10496 
10497 	dv.major_version = 0xff;
10498 	dv.minor_version = 0xff;
10499 	dv.build_version = 0xff;
10500 	dv.subbuild_version = 0;
10501 	strlcpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10502 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10503 }
10504 
10505 /**
10506  * i40e_get_oem_version - get OEM specific version information
10507  * @hw: pointer to the hardware structure
10508  **/
10509 static void i40e_get_oem_version(struct i40e_hw *hw)
10510 {
10511 	u16 block_offset = 0xffff;
10512 	u16 block_length = 0;
10513 	u16 capabilities = 0;
10514 	u16 gen_snap = 0;
10515 	u16 release = 0;
10516 
10517 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10518 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10519 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10520 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10521 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10522 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10523 #define I40E_NVM_OEM_LENGTH			3
10524 
10525 	/* Check if pointer to OEM version block is valid. */
10526 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10527 	if (block_offset == 0xffff)
10528 		return;
10529 
10530 	/* Check if OEM version block has correct length. */
10531 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10532 			   &block_length);
10533 	if (block_length < I40E_NVM_OEM_LENGTH)
10534 		return;
10535 
10536 	/* Check if OEM version format is as expected. */
10537 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10538 			   &capabilities);
10539 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10540 		return;
10541 
10542 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10543 			   &gen_snap);
10544 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10545 			   &release);
10546 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10547 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10548 }
10549 
10550 /**
10551  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10552  * @pf: board private structure
10553  **/
10554 static int i40e_reset(struct i40e_pf *pf)
10555 {
10556 	struct i40e_hw *hw = &pf->hw;
10557 	i40e_status ret;
10558 
10559 	ret = i40e_pf_reset(hw);
10560 	if (ret) {
10561 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10562 		set_bit(__I40E_RESET_FAILED, pf->state);
10563 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10564 	} else {
10565 		pf->pfr_count++;
10566 	}
10567 	return ret;
10568 }
10569 
10570 /**
10571  * i40e_rebuild - rebuild using a saved config
10572  * @pf: board private structure
10573  * @reinit: if the Main VSI needs to re-initialized.
10574  * @lock_acquired: indicates whether or not the lock has been acquired
10575  * before this function was called.
10576  **/
10577 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10578 {
10579 	int old_recovery_mode_bit = test_bit(__I40E_RECOVERY_MODE, pf->state);
10580 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10581 	struct i40e_hw *hw = &pf->hw;
10582 	i40e_status ret;
10583 	u32 val;
10584 	int v;
10585 
10586 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10587 	    i40e_check_recovery_mode(pf)) {
10588 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10589 	}
10590 
10591 	if (test_bit(__I40E_DOWN, pf->state) &&
10592 	    !test_bit(__I40E_RECOVERY_MODE, pf->state) &&
10593 	    !old_recovery_mode_bit)
10594 		goto clear_recovery;
10595 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10596 
10597 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10598 	ret = i40e_init_adminq(&pf->hw);
10599 	if (ret) {
10600 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10601 			 i40e_stat_str(&pf->hw, ret),
10602 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10603 		goto clear_recovery;
10604 	}
10605 	i40e_get_oem_version(&pf->hw);
10606 
10607 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10608 		/* The following delay is necessary for firmware update. */
10609 		mdelay(1000);
10610 	}
10611 
10612 	/* re-verify the eeprom if we just had an EMP reset */
10613 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10614 		i40e_verify_eeprom(pf);
10615 
10616 	/* if we are going out of or into recovery mode we have to act
10617 	 * accordingly with regard to resources initialization
10618 	 * and deinitialization
10619 	 */
10620 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) ||
10621 	    old_recovery_mode_bit) {
10622 		if (i40e_get_capabilities(pf,
10623 					  i40e_aqc_opc_list_func_capabilities))
10624 			goto end_unlock;
10625 
10626 		if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10627 			/* we're staying in recovery mode so we'll reinitialize
10628 			 * misc vector here
10629 			 */
10630 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10631 				goto end_unlock;
10632 		} else {
10633 			if (!lock_acquired)
10634 				rtnl_lock();
10635 			/* we're going out of recovery mode so we'll free
10636 			 * the IRQ allocated specifically for recovery mode
10637 			 * and restore the interrupt scheme
10638 			 */
10639 			free_irq(pf->pdev->irq, pf);
10640 			i40e_clear_interrupt_scheme(pf);
10641 			if (i40e_restore_interrupt_scheme(pf))
10642 				goto end_unlock;
10643 		}
10644 
10645 		/* tell the firmware that we're starting */
10646 		i40e_send_version(pf);
10647 
10648 		/* bail out in case recovery mode was detected, as there is
10649 		 * no need for further configuration.
10650 		 */
10651 		goto end_unlock;
10652 	}
10653 
10654 	i40e_clear_pxe_mode(hw);
10655 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10656 	if (ret)
10657 		goto end_core_reset;
10658 
10659 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10660 				hw->func_caps.num_rx_qp, 0, 0);
10661 	if (ret) {
10662 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10663 		goto end_core_reset;
10664 	}
10665 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10666 	if (ret) {
10667 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10668 		goto end_core_reset;
10669 	}
10670 
10671 #ifdef CONFIG_I40E_DCB
10672 	/* Enable FW to write a default DCB config on link-up
10673 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10674 	 * is not supported with new link speed
10675 	 */
10676 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
10677 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10678 	} else {
10679 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10680 		    (hw->phy.link_info.link_speed &
10681 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10682 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10683 			dev_warn(&pf->pdev->dev,
10684 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10685 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10686 		} else {
10687 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10688 			ret = i40e_init_pf_dcb(pf);
10689 			if (ret) {
10690 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10691 					 ret);
10692 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10693 				/* Continue without DCB enabled */
10694 			}
10695 		}
10696 	}
10697 
10698 #endif /* CONFIG_I40E_DCB */
10699 	if (!lock_acquired)
10700 		rtnl_lock();
10701 	ret = i40e_setup_pf_switch(pf, reinit, true);
10702 	if (ret)
10703 		goto end_unlock;
10704 
10705 	/* The driver only wants link up/down and module qualification
10706 	 * reports from firmware.  Note the negative logic.
10707 	 */
10708 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10709 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10710 					 I40E_AQ_EVENT_MEDIA_NA |
10711 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10712 	if (ret)
10713 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10714 			 i40e_stat_str(&pf->hw, ret),
10715 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10716 
10717 	/* Rebuild the VSIs and VEBs that existed before reset.
10718 	 * They are still in our local switch element arrays, so only
10719 	 * need to rebuild the switch model in the HW.
10720 	 *
10721 	 * If there were VEBs but the reconstitution failed, we'll try
10722 	 * to recover minimal use by getting the basic PF VSI working.
10723 	 */
10724 	if (vsi->uplink_seid != pf->mac_seid) {
10725 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10726 		/* find the one VEB connected to the MAC, and find orphans */
10727 		for (v = 0; v < I40E_MAX_VEB; v++) {
10728 			if (!pf->veb[v])
10729 				continue;
10730 
10731 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10732 			    pf->veb[v]->uplink_seid == 0) {
10733 				ret = i40e_reconstitute_veb(pf->veb[v]);
10734 
10735 				if (!ret)
10736 					continue;
10737 
10738 				/* If Main VEB failed, we're in deep doodoo,
10739 				 * so give up rebuilding the switch and set up
10740 				 * for minimal rebuild of PF VSI.
10741 				 * If orphan failed, we'll report the error
10742 				 * but try to keep going.
10743 				 */
10744 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10745 					dev_info(&pf->pdev->dev,
10746 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10747 						 ret);
10748 					vsi->uplink_seid = pf->mac_seid;
10749 					break;
10750 				} else if (pf->veb[v]->uplink_seid == 0) {
10751 					dev_info(&pf->pdev->dev,
10752 						 "rebuild of orphan VEB failed: %d\n",
10753 						 ret);
10754 				}
10755 			}
10756 		}
10757 	}
10758 
10759 	if (vsi->uplink_seid == pf->mac_seid) {
10760 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10761 		/* no VEB, so rebuild only the Main VSI */
10762 		ret = i40e_add_vsi(vsi);
10763 		if (ret) {
10764 			dev_info(&pf->pdev->dev,
10765 				 "rebuild of Main VSI failed: %d\n", ret);
10766 			goto end_unlock;
10767 		}
10768 	}
10769 
10770 	if (vsi->mqprio_qopt.max_rate[0]) {
10771 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
10772 		u64 credits = 0;
10773 
10774 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
10775 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10776 		if (ret)
10777 			goto end_unlock;
10778 
10779 		credits = max_tx_rate;
10780 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10781 		dev_dbg(&vsi->back->pdev->dev,
10782 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10783 			max_tx_rate,
10784 			credits,
10785 			vsi->seid);
10786 	}
10787 
10788 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10789 	if (ret)
10790 		goto end_unlock;
10791 
10792 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
10793 	 * for this main VSI if they exist
10794 	 */
10795 	ret = i40e_rebuild_channels(vsi);
10796 	if (ret)
10797 		goto end_unlock;
10798 
10799 	/* Reconfigure hardware for allowing smaller MSS in the case
10800 	 * of TSO, so that we avoid the MDD being fired and causing
10801 	 * a reset in the case of small MSS+TSO.
10802 	 */
10803 #define I40E_REG_MSS          0x000E64DC
10804 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
10805 #define I40E_64BYTE_MSS       0x400000
10806 	val = rd32(hw, I40E_REG_MSS);
10807 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10808 		val &= ~I40E_REG_MSS_MIN_MASK;
10809 		val |= I40E_64BYTE_MSS;
10810 		wr32(hw, I40E_REG_MSS, val);
10811 	}
10812 
10813 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
10814 		msleep(75);
10815 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10816 		if (ret)
10817 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10818 				 i40e_stat_str(&pf->hw, ret),
10819 				 i40e_aq_str(&pf->hw,
10820 					     pf->hw.aq.asq_last_status));
10821 	}
10822 	/* reinit the misc interrupt */
10823 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10824 		ret = i40e_setup_misc_vector(pf);
10825 
10826 	/* Add a filter to drop all Flow control frames from any VSI from being
10827 	 * transmitted. By doing so we stop a malicious VF from sending out
10828 	 * PAUSE or PFC frames and potentially controlling traffic for other
10829 	 * PF/VF VSIs.
10830 	 * The FW can still send Flow control frames if enabled.
10831 	 */
10832 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
10833 						       pf->main_vsi_seid);
10834 
10835 	/* restart the VSIs that were rebuilt and running before the reset */
10836 	i40e_pf_unquiesce_all_vsi(pf);
10837 
10838 	/* Release the RTNL lock before we start resetting VFs */
10839 	if (!lock_acquired)
10840 		rtnl_unlock();
10841 
10842 	/* Restore promiscuous settings */
10843 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
10844 	if (ret)
10845 		dev_warn(&pf->pdev->dev,
10846 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
10847 			 pf->cur_promisc ? "on" : "off",
10848 			 i40e_stat_str(&pf->hw, ret),
10849 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10850 
10851 	i40e_reset_all_vfs(pf, true);
10852 
10853 	/* tell the firmware that we're starting */
10854 	i40e_send_version(pf);
10855 
10856 	/* We've already released the lock, so don't do it again */
10857 	goto end_core_reset;
10858 
10859 end_unlock:
10860 	if (!lock_acquired)
10861 		rtnl_unlock();
10862 end_core_reset:
10863 	clear_bit(__I40E_RESET_FAILED, pf->state);
10864 clear_recovery:
10865 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10866 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
10867 }
10868 
10869 /**
10870  * i40e_reset_and_rebuild - reset and rebuild using a saved config
10871  * @pf: board private structure
10872  * @reinit: if the Main VSI needs to re-initialized.
10873  * @lock_acquired: indicates whether or not the lock has been acquired
10874  * before this function was called.
10875  **/
10876 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
10877 				   bool lock_acquired)
10878 {
10879 	int ret;
10880 
10881 	if (test_bit(__I40E_IN_REMOVE, pf->state))
10882 		return;
10883 	/* Now we wait for GRST to settle out.
10884 	 * We don't have to delete the VEBs or VSIs from the hw switch
10885 	 * because the reset will make them disappear.
10886 	 */
10887 	ret = i40e_reset(pf);
10888 	if (!ret)
10889 		i40e_rebuild(pf, reinit, lock_acquired);
10890 }
10891 
10892 /**
10893  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
10894  * @pf: board private structure
10895  *
10896  * Close up the VFs and other things in prep for a Core Reset,
10897  * then get ready to rebuild the world.
10898  * @lock_acquired: indicates whether or not the lock has been acquired
10899  * before this function was called.
10900  **/
10901 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
10902 {
10903 	i40e_prep_for_reset(pf);
10904 	i40e_reset_and_rebuild(pf, false, lock_acquired);
10905 }
10906 
10907 /**
10908  * i40e_handle_mdd_event
10909  * @pf: pointer to the PF structure
10910  *
10911  * Called from the MDD irq handler to identify possibly malicious vfs
10912  **/
10913 static void i40e_handle_mdd_event(struct i40e_pf *pf)
10914 {
10915 	struct i40e_hw *hw = &pf->hw;
10916 	bool mdd_detected = false;
10917 	struct i40e_vf *vf;
10918 	u32 reg;
10919 	int i;
10920 
10921 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
10922 		return;
10923 
10924 	/* find what triggered the MDD event */
10925 	reg = rd32(hw, I40E_GL_MDET_TX);
10926 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
10927 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
10928 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
10929 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
10930 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
10931 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
10932 				I40E_GL_MDET_TX_EVENT_SHIFT;
10933 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
10934 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
10935 				pf->hw.func_caps.base_queue;
10936 		if (netif_msg_tx_err(pf))
10937 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
10938 				 event, queue, pf_num, vf_num);
10939 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
10940 		mdd_detected = true;
10941 	}
10942 	reg = rd32(hw, I40E_GL_MDET_RX);
10943 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
10944 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
10945 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
10946 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
10947 				I40E_GL_MDET_RX_EVENT_SHIFT;
10948 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
10949 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
10950 				pf->hw.func_caps.base_queue;
10951 		if (netif_msg_rx_err(pf))
10952 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
10953 				 event, queue, func);
10954 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
10955 		mdd_detected = true;
10956 	}
10957 
10958 	if (mdd_detected) {
10959 		reg = rd32(hw, I40E_PF_MDET_TX);
10960 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
10961 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
10962 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
10963 		}
10964 		reg = rd32(hw, I40E_PF_MDET_RX);
10965 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
10966 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
10967 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
10968 		}
10969 	}
10970 
10971 	/* see if one of the VFs needs its hand slapped */
10972 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
10973 		vf = &(pf->vf[i]);
10974 		reg = rd32(hw, I40E_VP_MDET_TX(i));
10975 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
10976 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
10977 			vf->num_mdd_events++;
10978 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
10979 				 i);
10980 			dev_info(&pf->pdev->dev,
10981 				 "Use PF Control I/F to re-enable the VF\n");
10982 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10983 		}
10984 
10985 		reg = rd32(hw, I40E_VP_MDET_RX(i));
10986 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
10987 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
10988 			vf->num_mdd_events++;
10989 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
10990 				 i);
10991 			dev_info(&pf->pdev->dev,
10992 				 "Use PF Control I/F to re-enable the VF\n");
10993 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10994 		}
10995 	}
10996 
10997 	/* re-enable mdd interrupt cause */
10998 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
10999 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11000 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11001 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11002 	i40e_flush(hw);
11003 }
11004 
11005 /**
11006  * i40e_service_task - Run the driver's async subtasks
11007  * @work: pointer to work_struct containing our data
11008  **/
11009 static void i40e_service_task(struct work_struct *work)
11010 {
11011 	struct i40e_pf *pf = container_of(work,
11012 					  struct i40e_pf,
11013 					  service_task);
11014 	unsigned long start_time = jiffies;
11015 
11016 	/* don't bother with service tasks if a reset is in progress */
11017 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11018 	    test_bit(__I40E_SUSPENDED, pf->state))
11019 		return;
11020 
11021 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11022 		return;
11023 
11024 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11025 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11026 		i40e_sync_filters_subtask(pf);
11027 		i40e_reset_subtask(pf);
11028 		i40e_handle_mdd_event(pf);
11029 		i40e_vc_process_vflr_event(pf);
11030 		i40e_watchdog_subtask(pf);
11031 		i40e_fdir_reinit_subtask(pf);
11032 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11033 			/* Client subtask will reopen next time through. */
11034 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11035 							   true);
11036 		} else {
11037 			i40e_client_subtask(pf);
11038 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11039 					       pf->state))
11040 				i40e_notify_client_of_l2_param_changes(
11041 								pf->vsi[pf->lan_vsi]);
11042 		}
11043 		i40e_sync_filters_subtask(pf);
11044 	} else {
11045 		i40e_reset_subtask(pf);
11046 	}
11047 
11048 	i40e_clean_adminq_subtask(pf);
11049 
11050 	/* flush memory to make sure state is correct before next watchdog */
11051 	smp_mb__before_atomic();
11052 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11053 
11054 	/* If the tasks have taken longer than one timer cycle or there
11055 	 * is more work to be done, reschedule the service task now
11056 	 * rather than wait for the timer to tick again.
11057 	 */
11058 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11059 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11060 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11061 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11062 		i40e_service_event_schedule(pf);
11063 }
11064 
11065 /**
11066  * i40e_service_timer - timer callback
11067  * @t: timer list pointer
11068  **/
11069 static void i40e_service_timer(struct timer_list *t)
11070 {
11071 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11072 
11073 	mod_timer(&pf->service_timer,
11074 		  round_jiffies(jiffies + pf->service_timer_period));
11075 	i40e_service_event_schedule(pf);
11076 }
11077 
11078 /**
11079  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11080  * @vsi: the VSI being configured
11081  **/
11082 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11083 {
11084 	struct i40e_pf *pf = vsi->back;
11085 
11086 	switch (vsi->type) {
11087 	case I40E_VSI_MAIN:
11088 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11089 		if (!vsi->num_tx_desc)
11090 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11091 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11092 		if (!vsi->num_rx_desc)
11093 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11094 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11095 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11096 			vsi->num_q_vectors = pf->num_lan_msix;
11097 		else
11098 			vsi->num_q_vectors = 1;
11099 
11100 		break;
11101 
11102 	case I40E_VSI_FDIR:
11103 		vsi->alloc_queue_pairs = 1;
11104 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11105 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11106 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11107 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11108 		vsi->num_q_vectors = pf->num_fdsb_msix;
11109 		break;
11110 
11111 	case I40E_VSI_VMDQ2:
11112 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11113 		if (!vsi->num_tx_desc)
11114 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11115 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11116 		if (!vsi->num_rx_desc)
11117 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11118 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11119 		vsi->num_q_vectors = pf->num_vmdq_msix;
11120 		break;
11121 
11122 	case I40E_VSI_SRIOV:
11123 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11124 		if (!vsi->num_tx_desc)
11125 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11126 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11127 		if (!vsi->num_rx_desc)
11128 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11129 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11130 		break;
11131 
11132 	default:
11133 		WARN_ON(1);
11134 		return -ENODATA;
11135 	}
11136 
11137 	if (is_kdump_kernel()) {
11138 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11139 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11140 	}
11141 
11142 	return 0;
11143 }
11144 
11145 /**
11146  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11147  * @vsi: VSI pointer
11148  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11149  *
11150  * On error: returns error code (negative)
11151  * On success: returns 0
11152  **/
11153 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11154 {
11155 	struct i40e_ring **next_rings;
11156 	int size;
11157 	int ret = 0;
11158 
11159 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11160 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11161 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11162 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11163 	if (!vsi->tx_rings)
11164 		return -ENOMEM;
11165 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11166 	if (i40e_enabled_xdp_vsi(vsi)) {
11167 		vsi->xdp_rings = next_rings;
11168 		next_rings += vsi->alloc_queue_pairs;
11169 	}
11170 	vsi->rx_rings = next_rings;
11171 
11172 	if (alloc_qvectors) {
11173 		/* allocate memory for q_vector pointers */
11174 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11175 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11176 		if (!vsi->q_vectors) {
11177 			ret = -ENOMEM;
11178 			goto err_vectors;
11179 		}
11180 	}
11181 	return ret;
11182 
11183 err_vectors:
11184 	kfree(vsi->tx_rings);
11185 	return ret;
11186 }
11187 
11188 /**
11189  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11190  * @pf: board private structure
11191  * @type: type of VSI
11192  *
11193  * On error: returns error code (negative)
11194  * On success: returns vsi index in PF (positive)
11195  **/
11196 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11197 {
11198 	int ret = -ENODEV;
11199 	struct i40e_vsi *vsi;
11200 	int vsi_idx;
11201 	int i;
11202 
11203 	/* Need to protect the allocation of the VSIs at the PF level */
11204 	mutex_lock(&pf->switch_mutex);
11205 
11206 	/* VSI list may be fragmented if VSI creation/destruction has
11207 	 * been happening.  We can afford to do a quick scan to look
11208 	 * for any free VSIs in the list.
11209 	 *
11210 	 * find next empty vsi slot, looping back around if necessary
11211 	 */
11212 	i = pf->next_vsi;
11213 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11214 		i++;
11215 	if (i >= pf->num_alloc_vsi) {
11216 		i = 0;
11217 		while (i < pf->next_vsi && pf->vsi[i])
11218 			i++;
11219 	}
11220 
11221 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11222 		vsi_idx = i;             /* Found one! */
11223 	} else {
11224 		ret = -ENODEV;
11225 		goto unlock_pf;  /* out of VSI slots! */
11226 	}
11227 	pf->next_vsi = ++i;
11228 
11229 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11230 	if (!vsi) {
11231 		ret = -ENOMEM;
11232 		goto unlock_pf;
11233 	}
11234 	vsi->type = type;
11235 	vsi->back = pf;
11236 	set_bit(__I40E_VSI_DOWN, vsi->state);
11237 	vsi->flags = 0;
11238 	vsi->idx = vsi_idx;
11239 	vsi->int_rate_limit = 0;
11240 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11241 				pf->rss_table_size : 64;
11242 	vsi->netdev_registered = false;
11243 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11244 	hash_init(vsi->mac_filter_hash);
11245 	vsi->irqs_ready = false;
11246 
11247 	if (type == I40E_VSI_MAIN) {
11248 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11249 		if (!vsi->af_xdp_zc_qps)
11250 			goto err_rings;
11251 	}
11252 
11253 	ret = i40e_set_num_rings_in_vsi(vsi);
11254 	if (ret)
11255 		goto err_rings;
11256 
11257 	ret = i40e_vsi_alloc_arrays(vsi, true);
11258 	if (ret)
11259 		goto err_rings;
11260 
11261 	/* Setup default MSIX irq handler for VSI */
11262 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11263 
11264 	/* Initialize VSI lock */
11265 	spin_lock_init(&vsi->mac_filter_hash_lock);
11266 	pf->vsi[vsi_idx] = vsi;
11267 	ret = vsi_idx;
11268 	goto unlock_pf;
11269 
11270 err_rings:
11271 	bitmap_free(vsi->af_xdp_zc_qps);
11272 	pf->next_vsi = i - 1;
11273 	kfree(vsi);
11274 unlock_pf:
11275 	mutex_unlock(&pf->switch_mutex);
11276 	return ret;
11277 }
11278 
11279 /**
11280  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11281  * @vsi: VSI pointer
11282  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11283  *
11284  * On error: returns error code (negative)
11285  * On success: returns 0
11286  **/
11287 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11288 {
11289 	/* free the ring and vector containers */
11290 	if (free_qvectors) {
11291 		kfree(vsi->q_vectors);
11292 		vsi->q_vectors = NULL;
11293 	}
11294 	kfree(vsi->tx_rings);
11295 	vsi->tx_rings = NULL;
11296 	vsi->rx_rings = NULL;
11297 	vsi->xdp_rings = NULL;
11298 }
11299 
11300 /**
11301  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11302  * and lookup table
11303  * @vsi: Pointer to VSI structure
11304  */
11305 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11306 {
11307 	if (!vsi)
11308 		return;
11309 
11310 	kfree(vsi->rss_hkey_user);
11311 	vsi->rss_hkey_user = NULL;
11312 
11313 	kfree(vsi->rss_lut_user);
11314 	vsi->rss_lut_user = NULL;
11315 }
11316 
11317 /**
11318  * i40e_vsi_clear - Deallocate the VSI provided
11319  * @vsi: the VSI being un-configured
11320  **/
11321 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11322 {
11323 	struct i40e_pf *pf;
11324 
11325 	if (!vsi)
11326 		return 0;
11327 
11328 	if (!vsi->back)
11329 		goto free_vsi;
11330 	pf = vsi->back;
11331 
11332 	mutex_lock(&pf->switch_mutex);
11333 	if (!pf->vsi[vsi->idx]) {
11334 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11335 			vsi->idx, vsi->idx, vsi->type);
11336 		goto unlock_vsi;
11337 	}
11338 
11339 	if (pf->vsi[vsi->idx] != vsi) {
11340 		dev_err(&pf->pdev->dev,
11341 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11342 			pf->vsi[vsi->idx]->idx,
11343 			pf->vsi[vsi->idx]->type,
11344 			vsi->idx, vsi->type);
11345 		goto unlock_vsi;
11346 	}
11347 
11348 	/* updates the PF for this cleared vsi */
11349 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11350 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11351 
11352 	bitmap_free(vsi->af_xdp_zc_qps);
11353 	i40e_vsi_free_arrays(vsi, true);
11354 	i40e_clear_rss_config_user(vsi);
11355 
11356 	pf->vsi[vsi->idx] = NULL;
11357 	if (vsi->idx < pf->next_vsi)
11358 		pf->next_vsi = vsi->idx;
11359 
11360 unlock_vsi:
11361 	mutex_unlock(&pf->switch_mutex);
11362 free_vsi:
11363 	kfree(vsi);
11364 
11365 	return 0;
11366 }
11367 
11368 /**
11369  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11370  * @vsi: the VSI being cleaned
11371  **/
11372 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11373 {
11374 	int i;
11375 
11376 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11377 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11378 			kfree_rcu(vsi->tx_rings[i], rcu);
11379 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11380 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11381 			if (vsi->xdp_rings)
11382 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11383 		}
11384 	}
11385 }
11386 
11387 /**
11388  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11389  * @vsi: the VSI being configured
11390  **/
11391 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11392 {
11393 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11394 	struct i40e_pf *pf = vsi->back;
11395 	struct i40e_ring *ring;
11396 
11397 	/* Set basic values in the rings to be used later during open() */
11398 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11399 		/* allocate space for both Tx and Rx in one shot */
11400 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11401 		if (!ring)
11402 			goto err_out;
11403 
11404 		ring->queue_index = i;
11405 		ring->reg_idx = vsi->base_queue + i;
11406 		ring->ring_active = false;
11407 		ring->vsi = vsi;
11408 		ring->netdev = vsi->netdev;
11409 		ring->dev = &pf->pdev->dev;
11410 		ring->count = vsi->num_tx_desc;
11411 		ring->size = 0;
11412 		ring->dcb_tc = 0;
11413 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11414 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11415 		ring->itr_setting = pf->tx_itr_default;
11416 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11417 
11418 		if (!i40e_enabled_xdp_vsi(vsi))
11419 			goto setup_rx;
11420 
11421 		ring->queue_index = vsi->alloc_queue_pairs + i;
11422 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11423 		ring->ring_active = false;
11424 		ring->vsi = vsi;
11425 		ring->netdev = NULL;
11426 		ring->dev = &pf->pdev->dev;
11427 		ring->count = vsi->num_tx_desc;
11428 		ring->size = 0;
11429 		ring->dcb_tc = 0;
11430 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11431 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11432 		set_ring_xdp(ring);
11433 		ring->itr_setting = pf->tx_itr_default;
11434 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11435 
11436 setup_rx:
11437 		ring->queue_index = i;
11438 		ring->reg_idx = vsi->base_queue + i;
11439 		ring->ring_active = false;
11440 		ring->vsi = vsi;
11441 		ring->netdev = vsi->netdev;
11442 		ring->dev = &pf->pdev->dev;
11443 		ring->count = vsi->num_rx_desc;
11444 		ring->size = 0;
11445 		ring->dcb_tc = 0;
11446 		ring->itr_setting = pf->rx_itr_default;
11447 		WRITE_ONCE(vsi->rx_rings[i], ring);
11448 	}
11449 
11450 	return 0;
11451 
11452 err_out:
11453 	i40e_vsi_clear_rings(vsi);
11454 	return -ENOMEM;
11455 }
11456 
11457 /**
11458  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11459  * @pf: board private structure
11460  * @vectors: the number of MSI-X vectors to request
11461  *
11462  * Returns the number of vectors reserved, or error
11463  **/
11464 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11465 {
11466 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11467 					I40E_MIN_MSIX, vectors);
11468 	if (vectors < 0) {
11469 		dev_info(&pf->pdev->dev,
11470 			 "MSI-X vector reservation failed: %d\n", vectors);
11471 		vectors = 0;
11472 	}
11473 
11474 	return vectors;
11475 }
11476 
11477 /**
11478  * i40e_init_msix - Setup the MSIX capability
11479  * @pf: board private structure
11480  *
11481  * Work with the OS to set up the MSIX vectors needed.
11482  *
11483  * Returns the number of vectors reserved or negative on failure
11484  **/
11485 static int i40e_init_msix(struct i40e_pf *pf)
11486 {
11487 	struct i40e_hw *hw = &pf->hw;
11488 	int cpus, extra_vectors;
11489 	int vectors_left;
11490 	int v_budget, i;
11491 	int v_actual;
11492 	int iwarp_requested = 0;
11493 
11494 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11495 		return -ENODEV;
11496 
11497 	/* The number of vectors we'll request will be comprised of:
11498 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11499 	 *   - The number of LAN queue pairs
11500 	 *	- Queues being used for RSS.
11501 	 *		We don't need as many as max_rss_size vectors.
11502 	 *		use rss_size instead in the calculation since that
11503 	 *		is governed by number of cpus in the system.
11504 	 *	- assumes symmetric Tx/Rx pairing
11505 	 *   - The number of VMDq pairs
11506 	 *   - The CPU count within the NUMA node if iWARP is enabled
11507 	 * Once we count this up, try the request.
11508 	 *
11509 	 * If we can't get what we want, we'll simplify to nearly nothing
11510 	 * and try again.  If that still fails, we punt.
11511 	 */
11512 	vectors_left = hw->func_caps.num_msix_vectors;
11513 	v_budget = 0;
11514 
11515 	/* reserve one vector for miscellaneous handler */
11516 	if (vectors_left) {
11517 		v_budget++;
11518 		vectors_left--;
11519 	}
11520 
11521 	/* reserve some vectors for the main PF traffic queues. Initially we
11522 	 * only reserve at most 50% of the available vectors, in the case that
11523 	 * the number of online CPUs is large. This ensures that we can enable
11524 	 * extra features as well. Once we've enabled the other features, we
11525 	 * will use any remaining vectors to reach as close as we can to the
11526 	 * number of online CPUs.
11527 	 */
11528 	cpus = num_online_cpus();
11529 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11530 	vectors_left -= pf->num_lan_msix;
11531 
11532 	/* reserve one vector for sideband flow director */
11533 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11534 		if (vectors_left) {
11535 			pf->num_fdsb_msix = 1;
11536 			v_budget++;
11537 			vectors_left--;
11538 		} else {
11539 			pf->num_fdsb_msix = 0;
11540 		}
11541 	}
11542 
11543 	/* can we reserve enough for iWARP? */
11544 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11545 		iwarp_requested = pf->num_iwarp_msix;
11546 
11547 		if (!vectors_left)
11548 			pf->num_iwarp_msix = 0;
11549 		else if (vectors_left < pf->num_iwarp_msix)
11550 			pf->num_iwarp_msix = 1;
11551 		v_budget += pf->num_iwarp_msix;
11552 		vectors_left -= pf->num_iwarp_msix;
11553 	}
11554 
11555 	/* any vectors left over go for VMDq support */
11556 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11557 		if (!vectors_left) {
11558 			pf->num_vmdq_msix = 0;
11559 			pf->num_vmdq_qps = 0;
11560 		} else {
11561 			int vmdq_vecs_wanted =
11562 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11563 			int vmdq_vecs =
11564 				min_t(int, vectors_left, vmdq_vecs_wanted);
11565 
11566 			/* if we're short on vectors for what's desired, we limit
11567 			 * the queues per vmdq.  If this is still more than are
11568 			 * available, the user will need to change the number of
11569 			 * queues/vectors used by the PF later with the ethtool
11570 			 * channels command
11571 			 */
11572 			if (vectors_left < vmdq_vecs_wanted) {
11573 				pf->num_vmdq_qps = 1;
11574 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11575 				vmdq_vecs = min_t(int,
11576 						  vectors_left,
11577 						  vmdq_vecs_wanted);
11578 			}
11579 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11580 
11581 			v_budget += vmdq_vecs;
11582 			vectors_left -= vmdq_vecs;
11583 		}
11584 	}
11585 
11586 	/* On systems with a large number of SMP cores, we previously limited
11587 	 * the number of vectors for num_lan_msix to be at most 50% of the
11588 	 * available vectors, to allow for other features. Now, we add back
11589 	 * the remaining vectors. However, we ensure that the total
11590 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11591 	 * calculate the number of vectors we can add without going over the
11592 	 * cap of CPUs. For systems with a small number of CPUs this will be
11593 	 * zero.
11594 	 */
11595 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11596 	pf->num_lan_msix += extra_vectors;
11597 	vectors_left -= extra_vectors;
11598 
11599 	WARN(vectors_left < 0,
11600 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11601 
11602 	v_budget += pf->num_lan_msix;
11603 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11604 				   GFP_KERNEL);
11605 	if (!pf->msix_entries)
11606 		return -ENOMEM;
11607 
11608 	for (i = 0; i < v_budget; i++)
11609 		pf->msix_entries[i].entry = i;
11610 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11611 
11612 	if (v_actual < I40E_MIN_MSIX) {
11613 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11614 		kfree(pf->msix_entries);
11615 		pf->msix_entries = NULL;
11616 		pci_disable_msix(pf->pdev);
11617 		return -ENODEV;
11618 
11619 	} else if (v_actual == I40E_MIN_MSIX) {
11620 		/* Adjust for minimal MSIX use */
11621 		pf->num_vmdq_vsis = 0;
11622 		pf->num_vmdq_qps = 0;
11623 		pf->num_lan_qps = 1;
11624 		pf->num_lan_msix = 1;
11625 
11626 	} else if (v_actual != v_budget) {
11627 		/* If we have limited resources, we will start with no vectors
11628 		 * for the special features and then allocate vectors to some
11629 		 * of these features based on the policy and at the end disable
11630 		 * the features that did not get any vectors.
11631 		 */
11632 		int vec;
11633 
11634 		dev_info(&pf->pdev->dev,
11635 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11636 			 v_actual, v_budget);
11637 		/* reserve the misc vector */
11638 		vec = v_actual - 1;
11639 
11640 		/* Scale vector usage down */
11641 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11642 		pf->num_vmdq_vsis = 1;
11643 		pf->num_vmdq_qps = 1;
11644 
11645 		/* partition out the remaining vectors */
11646 		switch (vec) {
11647 		case 2:
11648 			pf->num_lan_msix = 1;
11649 			break;
11650 		case 3:
11651 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11652 				pf->num_lan_msix = 1;
11653 				pf->num_iwarp_msix = 1;
11654 			} else {
11655 				pf->num_lan_msix = 2;
11656 			}
11657 			break;
11658 		default:
11659 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11660 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11661 						 iwarp_requested);
11662 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11663 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11664 			} else {
11665 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11666 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11667 			}
11668 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11669 				pf->num_fdsb_msix = 1;
11670 				vec--;
11671 			}
11672 			pf->num_lan_msix = min_t(int,
11673 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11674 							      pf->num_lan_msix);
11675 			pf->num_lan_qps = pf->num_lan_msix;
11676 			break;
11677 		}
11678 	}
11679 
11680 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11681 	    (pf->num_fdsb_msix == 0)) {
11682 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11683 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11684 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11685 	}
11686 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11687 	    (pf->num_vmdq_msix == 0)) {
11688 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11689 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11690 	}
11691 
11692 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11693 	    (pf->num_iwarp_msix == 0)) {
11694 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11695 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11696 	}
11697 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11698 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11699 		   pf->num_lan_msix,
11700 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11701 		   pf->num_fdsb_msix,
11702 		   pf->num_iwarp_msix);
11703 
11704 	return v_actual;
11705 }
11706 
11707 /**
11708  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11709  * @vsi: the VSI being configured
11710  * @v_idx: index of the vector in the vsi struct
11711  *
11712  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11713  **/
11714 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11715 {
11716 	struct i40e_q_vector *q_vector;
11717 
11718 	/* allocate q_vector */
11719 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11720 	if (!q_vector)
11721 		return -ENOMEM;
11722 
11723 	q_vector->vsi = vsi;
11724 	q_vector->v_idx = v_idx;
11725 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11726 
11727 	if (vsi->netdev)
11728 		netif_napi_add(vsi->netdev, &q_vector->napi,
11729 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
11730 
11731 	/* tie q_vector and vsi together */
11732 	vsi->q_vectors[v_idx] = q_vector;
11733 
11734 	return 0;
11735 }
11736 
11737 /**
11738  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11739  * @vsi: the VSI being configured
11740  *
11741  * We allocate one q_vector per queue interrupt.  If allocation fails we
11742  * return -ENOMEM.
11743  **/
11744 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11745 {
11746 	struct i40e_pf *pf = vsi->back;
11747 	int err, v_idx, num_q_vectors;
11748 
11749 	/* if not MSIX, give the one vector only to the LAN VSI */
11750 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11751 		num_q_vectors = vsi->num_q_vectors;
11752 	else if (vsi == pf->vsi[pf->lan_vsi])
11753 		num_q_vectors = 1;
11754 	else
11755 		return -EINVAL;
11756 
11757 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11758 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11759 		if (err)
11760 			goto err_out;
11761 	}
11762 
11763 	return 0;
11764 
11765 err_out:
11766 	while (v_idx--)
11767 		i40e_free_q_vector(vsi, v_idx);
11768 
11769 	return err;
11770 }
11771 
11772 /**
11773  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11774  * @pf: board private structure to initialize
11775  **/
11776 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11777 {
11778 	int vectors = 0;
11779 	ssize_t size;
11780 
11781 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11782 		vectors = i40e_init_msix(pf);
11783 		if (vectors < 0) {
11784 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11785 				       I40E_FLAG_IWARP_ENABLED	|
11786 				       I40E_FLAG_RSS_ENABLED	|
11787 				       I40E_FLAG_DCB_CAPABLE	|
11788 				       I40E_FLAG_DCB_ENABLED	|
11789 				       I40E_FLAG_SRIOV_ENABLED	|
11790 				       I40E_FLAG_FD_SB_ENABLED	|
11791 				       I40E_FLAG_FD_ATR_ENABLED	|
11792 				       I40E_FLAG_VMDQ_ENABLED);
11793 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11794 
11795 			/* rework the queue expectations without MSIX */
11796 			i40e_determine_queue_usage(pf);
11797 		}
11798 	}
11799 
11800 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11801 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
11802 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
11803 		vectors = pci_enable_msi(pf->pdev);
11804 		if (vectors < 0) {
11805 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
11806 				 vectors);
11807 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
11808 		}
11809 		vectors = 1;  /* one MSI or Legacy vector */
11810 	}
11811 
11812 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
11813 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
11814 
11815 	/* set up vector assignment tracking */
11816 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
11817 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
11818 	if (!pf->irq_pile)
11819 		return -ENOMEM;
11820 
11821 	pf->irq_pile->num_entries = vectors;
11822 
11823 	/* track first vector for misc interrupts, ignore return */
11824 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
11825 
11826 	return 0;
11827 }
11828 
11829 /**
11830  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
11831  * @pf: private board data structure
11832  *
11833  * Restore the interrupt scheme that was cleared when we suspended the
11834  * device. This should be called during resume to re-allocate the q_vectors
11835  * and reacquire IRQs.
11836  */
11837 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
11838 {
11839 	int err, i;
11840 
11841 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
11842 	 * scheme. We need to re-enabled them here in order to attempt to
11843 	 * re-acquire the MSI or MSI-X vectors
11844 	 */
11845 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
11846 
11847 	err = i40e_init_interrupt_scheme(pf);
11848 	if (err)
11849 		return err;
11850 
11851 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
11852 	 * rings together again.
11853 	 */
11854 	for (i = 0; i < pf->num_alloc_vsi; i++) {
11855 		if (pf->vsi[i]) {
11856 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
11857 			if (err)
11858 				goto err_unwind;
11859 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
11860 		}
11861 	}
11862 
11863 	err = i40e_setup_misc_vector(pf);
11864 	if (err)
11865 		goto err_unwind;
11866 
11867 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
11868 		i40e_client_update_msix_info(pf);
11869 
11870 	return 0;
11871 
11872 err_unwind:
11873 	while (i--) {
11874 		if (pf->vsi[i])
11875 			i40e_vsi_free_q_vectors(pf->vsi[i]);
11876 	}
11877 
11878 	return err;
11879 }
11880 
11881 /**
11882  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
11883  * non queue events in recovery mode
11884  * @pf: board private structure
11885  *
11886  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
11887  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
11888  * This is handled differently than in recovery mode since no Tx/Rx resources
11889  * are being allocated.
11890  **/
11891 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
11892 {
11893 	int err;
11894 
11895 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11896 		err = i40e_setup_misc_vector(pf);
11897 
11898 		if (err) {
11899 			dev_info(&pf->pdev->dev,
11900 				 "MSI-X misc vector request failed, error %d\n",
11901 				 err);
11902 			return err;
11903 		}
11904 	} else {
11905 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
11906 
11907 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
11908 				  pf->int_name, pf);
11909 
11910 		if (err) {
11911 			dev_info(&pf->pdev->dev,
11912 				 "MSI/legacy misc vector request failed, error %d\n",
11913 				 err);
11914 			return err;
11915 		}
11916 		i40e_enable_misc_int_causes(pf);
11917 		i40e_irq_dynamic_enable_icr0(pf);
11918 	}
11919 
11920 	return 0;
11921 }
11922 
11923 /**
11924  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
11925  * @pf: board private structure
11926  *
11927  * This sets up the handler for MSIX 0, which is used to manage the
11928  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
11929  * when in MSI or Legacy interrupt mode.
11930  **/
11931 static int i40e_setup_misc_vector(struct i40e_pf *pf)
11932 {
11933 	struct i40e_hw *hw = &pf->hw;
11934 	int err = 0;
11935 
11936 	/* Only request the IRQ once, the first time through. */
11937 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
11938 		err = request_irq(pf->msix_entries[0].vector,
11939 				  i40e_intr, 0, pf->int_name, pf);
11940 		if (err) {
11941 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
11942 			dev_info(&pf->pdev->dev,
11943 				 "request_irq for %s failed: %d\n",
11944 				 pf->int_name, err);
11945 			return -EFAULT;
11946 		}
11947 	}
11948 
11949 	i40e_enable_misc_int_causes(pf);
11950 
11951 	/* associate no queues to the misc vector */
11952 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
11953 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
11954 
11955 	i40e_flush(hw);
11956 
11957 	i40e_irq_dynamic_enable_icr0(pf);
11958 
11959 	return err;
11960 }
11961 
11962 /**
11963  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
11964  * @vsi: Pointer to vsi structure
11965  * @seed: Buffter to store the hash keys
11966  * @lut: Buffer to store the lookup table entries
11967  * @lut_size: Size of buffer to store the lookup table entries
11968  *
11969  * Return 0 on success, negative on failure
11970  */
11971 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
11972 			   u8 *lut, u16 lut_size)
11973 {
11974 	struct i40e_pf *pf = vsi->back;
11975 	struct i40e_hw *hw = &pf->hw;
11976 	int ret = 0;
11977 
11978 	if (seed) {
11979 		ret = i40e_aq_get_rss_key(hw, vsi->id,
11980 			(struct i40e_aqc_get_set_rss_key_data *)seed);
11981 		if (ret) {
11982 			dev_info(&pf->pdev->dev,
11983 				 "Cannot get RSS key, err %s aq_err %s\n",
11984 				 i40e_stat_str(&pf->hw, ret),
11985 				 i40e_aq_str(&pf->hw,
11986 					     pf->hw.aq.asq_last_status));
11987 			return ret;
11988 		}
11989 	}
11990 
11991 	if (lut) {
11992 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
11993 
11994 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
11995 		if (ret) {
11996 			dev_info(&pf->pdev->dev,
11997 				 "Cannot get RSS lut, err %s aq_err %s\n",
11998 				 i40e_stat_str(&pf->hw, ret),
11999 				 i40e_aq_str(&pf->hw,
12000 					     pf->hw.aq.asq_last_status));
12001 			return ret;
12002 		}
12003 	}
12004 
12005 	return ret;
12006 }
12007 
12008 /**
12009  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12010  * @vsi: Pointer to vsi structure
12011  * @seed: RSS hash seed
12012  * @lut: Lookup table
12013  * @lut_size: Lookup table size
12014  *
12015  * Returns 0 on success, negative on failure
12016  **/
12017 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12018 			       const u8 *lut, u16 lut_size)
12019 {
12020 	struct i40e_pf *pf = vsi->back;
12021 	struct i40e_hw *hw = &pf->hw;
12022 	u16 vf_id = vsi->vf_id;
12023 	u8 i;
12024 
12025 	/* Fill out hash function seed */
12026 	if (seed) {
12027 		u32 *seed_dw = (u32 *)seed;
12028 
12029 		if (vsi->type == I40E_VSI_MAIN) {
12030 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12031 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12032 		} else if (vsi->type == I40E_VSI_SRIOV) {
12033 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12034 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12035 		} else {
12036 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12037 		}
12038 	}
12039 
12040 	if (lut) {
12041 		u32 *lut_dw = (u32 *)lut;
12042 
12043 		if (vsi->type == I40E_VSI_MAIN) {
12044 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12045 				return -EINVAL;
12046 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12047 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12048 		} else if (vsi->type == I40E_VSI_SRIOV) {
12049 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12050 				return -EINVAL;
12051 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12052 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12053 		} else {
12054 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12055 		}
12056 	}
12057 	i40e_flush(hw);
12058 
12059 	return 0;
12060 }
12061 
12062 /**
12063  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12064  * @vsi: Pointer to VSI structure
12065  * @seed: Buffer to store the keys
12066  * @lut: Buffer to store the lookup table entries
12067  * @lut_size: Size of buffer to store the lookup table entries
12068  *
12069  * Returns 0 on success, negative on failure
12070  */
12071 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12072 			    u8 *lut, u16 lut_size)
12073 {
12074 	struct i40e_pf *pf = vsi->back;
12075 	struct i40e_hw *hw = &pf->hw;
12076 	u16 i;
12077 
12078 	if (seed) {
12079 		u32 *seed_dw = (u32 *)seed;
12080 
12081 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12082 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12083 	}
12084 	if (lut) {
12085 		u32 *lut_dw = (u32 *)lut;
12086 
12087 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12088 			return -EINVAL;
12089 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12090 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12091 	}
12092 
12093 	return 0;
12094 }
12095 
12096 /**
12097  * i40e_config_rss - Configure RSS keys and lut
12098  * @vsi: Pointer to VSI structure
12099  * @seed: RSS hash seed
12100  * @lut: Lookup table
12101  * @lut_size: Lookup table size
12102  *
12103  * Returns 0 on success, negative on failure
12104  */
12105 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12106 {
12107 	struct i40e_pf *pf = vsi->back;
12108 
12109 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12110 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12111 	else
12112 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12113 }
12114 
12115 /**
12116  * i40e_get_rss - Get RSS keys and lut
12117  * @vsi: Pointer to VSI structure
12118  * @seed: Buffer to store the keys
12119  * @lut: Buffer to store the lookup table entries
12120  * @lut_size: Size of buffer to store the lookup table entries
12121  *
12122  * Returns 0 on success, negative on failure
12123  */
12124 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12125 {
12126 	struct i40e_pf *pf = vsi->back;
12127 
12128 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12129 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12130 	else
12131 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12132 }
12133 
12134 /**
12135  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12136  * @pf: Pointer to board private structure
12137  * @lut: Lookup table
12138  * @rss_table_size: Lookup table size
12139  * @rss_size: Range of queue number for hashing
12140  */
12141 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12142 		       u16 rss_table_size, u16 rss_size)
12143 {
12144 	u16 i;
12145 
12146 	for (i = 0; i < rss_table_size; i++)
12147 		lut[i] = i % rss_size;
12148 }
12149 
12150 /**
12151  * i40e_pf_config_rss - Prepare for RSS if used
12152  * @pf: board private structure
12153  **/
12154 static int i40e_pf_config_rss(struct i40e_pf *pf)
12155 {
12156 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12157 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12158 	u8 *lut;
12159 	struct i40e_hw *hw = &pf->hw;
12160 	u32 reg_val;
12161 	u64 hena;
12162 	int ret;
12163 
12164 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12165 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12166 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12167 	hena |= i40e_pf_get_default_rss_hena(pf);
12168 
12169 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12170 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12171 
12172 	/* Determine the RSS table size based on the hardware capabilities */
12173 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12174 	reg_val = (pf->rss_table_size == 512) ?
12175 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12176 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12177 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12178 
12179 	/* Determine the RSS size of the VSI */
12180 	if (!vsi->rss_size) {
12181 		u16 qcount;
12182 		/* If the firmware does something weird during VSI init, we
12183 		 * could end up with zero TCs. Check for that to avoid
12184 		 * divide-by-zero. It probably won't pass traffic, but it also
12185 		 * won't panic.
12186 		 */
12187 		qcount = vsi->num_queue_pairs /
12188 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12189 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12190 	}
12191 	if (!vsi->rss_size)
12192 		return -EINVAL;
12193 
12194 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12195 	if (!lut)
12196 		return -ENOMEM;
12197 
12198 	/* Use user configured lut if there is one, otherwise use default */
12199 	if (vsi->rss_lut_user)
12200 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12201 	else
12202 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12203 
12204 	/* Use user configured hash key if there is one, otherwise
12205 	 * use default.
12206 	 */
12207 	if (vsi->rss_hkey_user)
12208 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12209 	else
12210 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12211 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12212 	kfree(lut);
12213 
12214 	return ret;
12215 }
12216 
12217 /**
12218  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12219  * @pf: board private structure
12220  * @queue_count: the requested queue count for rss.
12221  *
12222  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12223  * count which may be different from the requested queue count.
12224  * Note: expects to be called while under rtnl_lock()
12225  **/
12226 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12227 {
12228 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12229 	int new_rss_size;
12230 
12231 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12232 		return 0;
12233 
12234 	queue_count = min_t(int, queue_count, num_online_cpus());
12235 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12236 
12237 	if (queue_count != vsi->num_queue_pairs) {
12238 		u16 qcount;
12239 
12240 		vsi->req_queue_pairs = queue_count;
12241 		i40e_prep_for_reset(pf);
12242 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12243 			return pf->alloc_rss_size;
12244 
12245 		pf->alloc_rss_size = new_rss_size;
12246 
12247 		i40e_reset_and_rebuild(pf, true, true);
12248 
12249 		/* Discard the user configured hash keys and lut, if less
12250 		 * queues are enabled.
12251 		 */
12252 		if (queue_count < vsi->rss_size) {
12253 			i40e_clear_rss_config_user(vsi);
12254 			dev_dbg(&pf->pdev->dev,
12255 				"discard user configured hash keys and lut\n");
12256 		}
12257 
12258 		/* Reset vsi->rss_size, as number of enabled queues changed */
12259 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12260 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12261 
12262 		i40e_pf_config_rss(pf);
12263 	}
12264 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12265 		 vsi->req_queue_pairs, pf->rss_size_max);
12266 	return pf->alloc_rss_size;
12267 }
12268 
12269 /**
12270  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12271  * @pf: board private structure
12272  **/
12273 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
12274 {
12275 	i40e_status status;
12276 	bool min_valid, max_valid;
12277 	u32 max_bw, min_bw;
12278 
12279 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12280 					   &min_valid, &max_valid);
12281 
12282 	if (!status) {
12283 		if (min_valid)
12284 			pf->min_bw = min_bw;
12285 		if (max_valid)
12286 			pf->max_bw = max_bw;
12287 	}
12288 
12289 	return status;
12290 }
12291 
12292 /**
12293  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12294  * @pf: board private structure
12295  **/
12296 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
12297 {
12298 	struct i40e_aqc_configure_partition_bw_data bw_data;
12299 	i40e_status status;
12300 
12301 	memset(&bw_data, 0, sizeof(bw_data));
12302 
12303 	/* Set the valid bit for this PF */
12304 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12305 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12306 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12307 
12308 	/* Set the new bandwidths */
12309 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12310 
12311 	return status;
12312 }
12313 
12314 /**
12315  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12316  * @pf: board private structure
12317  **/
12318 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12319 {
12320 	/* Commit temporary BW setting to permanent NVM image */
12321 	enum i40e_admin_queue_err last_aq_status;
12322 	i40e_status ret;
12323 	u16 nvm_word;
12324 
12325 	if (pf->hw.partition_id != 1) {
12326 		dev_info(&pf->pdev->dev,
12327 			 "Commit BW only works on partition 1! This is partition %d",
12328 			 pf->hw.partition_id);
12329 		ret = I40E_NOT_SUPPORTED;
12330 		goto bw_commit_out;
12331 	}
12332 
12333 	/* Acquire NVM for read access */
12334 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12335 	last_aq_status = pf->hw.aq.asq_last_status;
12336 	if (ret) {
12337 		dev_info(&pf->pdev->dev,
12338 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
12339 			 i40e_stat_str(&pf->hw, ret),
12340 			 i40e_aq_str(&pf->hw, last_aq_status));
12341 		goto bw_commit_out;
12342 	}
12343 
12344 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12345 	ret = i40e_aq_read_nvm(&pf->hw,
12346 			       I40E_SR_NVM_CONTROL_WORD,
12347 			       0x10, sizeof(nvm_word), &nvm_word,
12348 			       false, NULL);
12349 	/* Save off last admin queue command status before releasing
12350 	 * the NVM
12351 	 */
12352 	last_aq_status = pf->hw.aq.asq_last_status;
12353 	i40e_release_nvm(&pf->hw);
12354 	if (ret) {
12355 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
12356 			 i40e_stat_str(&pf->hw, ret),
12357 			 i40e_aq_str(&pf->hw, last_aq_status));
12358 		goto bw_commit_out;
12359 	}
12360 
12361 	/* Wait a bit for NVM release to complete */
12362 	msleep(50);
12363 
12364 	/* Acquire NVM for write access */
12365 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12366 	last_aq_status = pf->hw.aq.asq_last_status;
12367 	if (ret) {
12368 		dev_info(&pf->pdev->dev,
12369 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
12370 			 i40e_stat_str(&pf->hw, ret),
12371 			 i40e_aq_str(&pf->hw, last_aq_status));
12372 		goto bw_commit_out;
12373 	}
12374 	/* Write it back out unchanged to initiate update NVM,
12375 	 * which will force a write of the shadow (alt) RAM to
12376 	 * the NVM - thus storing the bandwidth values permanently.
12377 	 */
12378 	ret = i40e_aq_update_nvm(&pf->hw,
12379 				 I40E_SR_NVM_CONTROL_WORD,
12380 				 0x10, sizeof(nvm_word),
12381 				 &nvm_word, true, 0, NULL);
12382 	/* Save off last admin queue command status before releasing
12383 	 * the NVM
12384 	 */
12385 	last_aq_status = pf->hw.aq.asq_last_status;
12386 	i40e_release_nvm(&pf->hw);
12387 	if (ret)
12388 		dev_info(&pf->pdev->dev,
12389 			 "BW settings NOT SAVED, err %s aq_err %s\n",
12390 			 i40e_stat_str(&pf->hw, ret),
12391 			 i40e_aq_str(&pf->hw, last_aq_status));
12392 bw_commit_out:
12393 
12394 	return ret;
12395 }
12396 
12397 /**
12398  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12399  * if total port shutdown feature is enabled for this PF
12400  * @pf: board private structure
12401  **/
12402 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12403 {
12404 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12405 #define I40E_FEATURES_ENABLE_PTR		0x2A
12406 #define I40E_CURRENT_SETTING_PTR		0x2B
12407 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12408 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12409 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12410 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12411 	i40e_status read_status = I40E_SUCCESS;
12412 	u16 sr_emp_sr_settings_ptr = 0;
12413 	u16 features_enable = 0;
12414 	u16 link_behavior = 0;
12415 	bool ret = false;
12416 
12417 	read_status = i40e_read_nvm_word(&pf->hw,
12418 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12419 					 &sr_emp_sr_settings_ptr);
12420 	if (read_status)
12421 		goto err_nvm;
12422 	read_status = i40e_read_nvm_word(&pf->hw,
12423 					 sr_emp_sr_settings_ptr +
12424 					 I40E_FEATURES_ENABLE_PTR,
12425 					 &features_enable);
12426 	if (read_status)
12427 		goto err_nvm;
12428 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12429 		read_status = i40e_read_nvm_module_data(&pf->hw,
12430 							I40E_SR_EMP_SR_SETTINGS_PTR,
12431 							I40E_CURRENT_SETTING_PTR,
12432 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12433 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12434 							&link_behavior);
12435 		if (read_status)
12436 			goto err_nvm;
12437 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12438 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12439 	}
12440 	return ret;
12441 
12442 err_nvm:
12443 	dev_warn(&pf->pdev->dev,
12444 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
12445 		 i40e_stat_str(&pf->hw, read_status));
12446 	return ret;
12447 }
12448 
12449 /**
12450  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12451  * @pf: board private structure to initialize
12452  *
12453  * i40e_sw_init initializes the Adapter private data structure.
12454  * Fields are initialized based on PCI device information and
12455  * OS network device settings (MTU size).
12456  **/
12457 static int i40e_sw_init(struct i40e_pf *pf)
12458 {
12459 	int err = 0;
12460 	int size;
12461 	u16 pow;
12462 
12463 	/* Set default capability flags */
12464 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12465 		    I40E_FLAG_MSI_ENABLED     |
12466 		    I40E_FLAG_MSIX_ENABLED;
12467 
12468 	/* Set default ITR */
12469 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12470 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12471 
12472 	/* Depending on PF configurations, it is possible that the RSS
12473 	 * maximum might end up larger than the available queues
12474 	 */
12475 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12476 	pf->alloc_rss_size = 1;
12477 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12478 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12479 				 pf->hw.func_caps.num_tx_qp);
12480 
12481 	/* find the next higher power-of-2 of num cpus */
12482 	pow = roundup_pow_of_two(num_online_cpus());
12483 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12484 
12485 	if (pf->hw.func_caps.rss) {
12486 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12487 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12488 					   num_online_cpus());
12489 	}
12490 
12491 	/* MFP mode enabled */
12492 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12493 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12494 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12495 		if (i40e_get_partition_bw_setting(pf)) {
12496 			dev_warn(&pf->pdev->dev,
12497 				 "Could not get partition bw settings\n");
12498 		} else {
12499 			dev_info(&pf->pdev->dev,
12500 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12501 				 pf->min_bw, pf->max_bw);
12502 
12503 			/* nudge the Tx scheduler */
12504 			i40e_set_partition_bw_setting(pf);
12505 		}
12506 	}
12507 
12508 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12509 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12510 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12511 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12512 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12513 		    pf->hw.num_partitions > 1)
12514 			dev_info(&pf->pdev->dev,
12515 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12516 		else
12517 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12518 		pf->fdir_pf_filter_count =
12519 				 pf->hw.func_caps.fd_filters_guaranteed;
12520 		pf->hw.fdir_shared_filter_count =
12521 				 pf->hw.func_caps.fd_filters_best_effort;
12522 	}
12523 
12524 	if (pf->hw.mac.type == I40E_MAC_X722) {
12525 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12526 				    I40E_HW_128_QP_RSS_CAPABLE |
12527 				    I40E_HW_ATR_EVICT_CAPABLE |
12528 				    I40E_HW_WB_ON_ITR_CAPABLE |
12529 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12530 				    I40E_HW_NO_PCI_LINK_CHECK |
12531 				    I40E_HW_USE_SET_LLDP_MIB |
12532 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12533 				    I40E_HW_PTP_L4_CAPABLE |
12534 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12535 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12536 
12537 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12538 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12539 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12540 			dev_warn(&pf->pdev->dev,
12541 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12542 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12543 		}
12544 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12545 		   ((pf->hw.aq.api_maj_ver == 1) &&
12546 		    (pf->hw.aq.api_min_ver > 4))) {
12547 		/* Supported in FW API version higher than 1.4 */
12548 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12549 	}
12550 
12551 	/* Enable HW ATR eviction if possible */
12552 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12553 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12554 
12555 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12556 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12557 	    (pf->hw.aq.fw_maj_ver < 4))) {
12558 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12559 		/* No DCB support  for FW < v4.33 */
12560 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12561 	}
12562 
12563 	/* Disable FW LLDP if FW < v4.3 */
12564 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12565 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12566 	    (pf->hw.aq.fw_maj_ver < 4)))
12567 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12568 
12569 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12570 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12571 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12572 	    (pf->hw.aq.fw_maj_ver >= 5)))
12573 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12574 
12575 	/* Enable PTP L4 if FW > v6.0 */
12576 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12577 	    pf->hw.aq.fw_maj_ver >= 6)
12578 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12579 
12580 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12581 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12582 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12583 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12584 	}
12585 
12586 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12587 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12588 		/* IWARP needs one extra vector for CQP just like MISC.*/
12589 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12590 	}
12591 	/* Stopping FW LLDP engine is supported on XL710 and X722
12592 	 * starting from FW versions determined in i40e_init_adminq.
12593 	 * Stopping the FW LLDP engine is not supported on XL710
12594 	 * if NPAR is functioning so unset this hw flag in this case.
12595 	 */
12596 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12597 	    pf->hw.func_caps.npar_enable &&
12598 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12599 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12600 
12601 #ifdef CONFIG_PCI_IOV
12602 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12603 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12604 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12605 		pf->num_req_vfs = min_t(int,
12606 					pf->hw.func_caps.num_vfs,
12607 					I40E_MAX_VF_COUNT);
12608 	}
12609 #endif /* CONFIG_PCI_IOV */
12610 	pf->eeprom_version = 0xDEAD;
12611 	pf->lan_veb = I40E_NO_VEB;
12612 	pf->lan_vsi = I40E_NO_VSI;
12613 
12614 	/* By default FW has this off for performance reasons */
12615 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12616 
12617 	/* set up queue assignment tracking */
12618 	size = sizeof(struct i40e_lump_tracking)
12619 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12620 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12621 	if (!pf->qp_pile) {
12622 		err = -ENOMEM;
12623 		goto sw_init_done;
12624 	}
12625 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12626 
12627 	pf->tx_timeout_recovery_level = 1;
12628 
12629 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12630 	    i40e_is_total_port_shutdown_enabled(pf)) {
12631 		/* Link down on close must be on when total port shutdown
12632 		 * is enabled for a given port
12633 		 */
12634 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12635 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12636 		dev_info(&pf->pdev->dev,
12637 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12638 	}
12639 	mutex_init(&pf->switch_mutex);
12640 
12641 sw_init_done:
12642 	return err;
12643 }
12644 
12645 /**
12646  * i40e_set_ntuple - set the ntuple feature flag and take action
12647  * @pf: board private structure to initialize
12648  * @features: the feature set that the stack is suggesting
12649  *
12650  * returns a bool to indicate if reset needs to happen
12651  **/
12652 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12653 {
12654 	bool need_reset = false;
12655 
12656 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12657 	 * the state changed, we need to reset.
12658 	 */
12659 	if (features & NETIF_F_NTUPLE) {
12660 		/* Enable filters and mark for reset */
12661 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12662 			need_reset = true;
12663 		/* enable FD_SB only if there is MSI-X vector and no cloud
12664 		 * filters exist
12665 		 */
12666 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12667 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12668 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12669 		}
12670 	} else {
12671 		/* turn off filters, mark for reset and clear SW filter list */
12672 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12673 			need_reset = true;
12674 			i40e_fdir_filter_exit(pf);
12675 		}
12676 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12677 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12678 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12679 
12680 		/* reset fd counters */
12681 		pf->fd_add_err = 0;
12682 		pf->fd_atr_cnt = 0;
12683 		/* if ATR was auto disabled it can be re-enabled. */
12684 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12685 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12686 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12687 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12688 	}
12689 	return need_reset;
12690 }
12691 
12692 /**
12693  * i40e_clear_rss_lut - clear the rx hash lookup table
12694  * @vsi: the VSI being configured
12695  **/
12696 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12697 {
12698 	struct i40e_pf *pf = vsi->back;
12699 	struct i40e_hw *hw = &pf->hw;
12700 	u16 vf_id = vsi->vf_id;
12701 	u8 i;
12702 
12703 	if (vsi->type == I40E_VSI_MAIN) {
12704 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12705 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12706 	} else if (vsi->type == I40E_VSI_SRIOV) {
12707 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12708 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12709 	} else {
12710 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12711 	}
12712 }
12713 
12714 /**
12715  * i40e_set_features - set the netdev feature flags
12716  * @netdev: ptr to the netdev being adjusted
12717  * @features: the feature set that the stack is suggesting
12718  * Note: expects to be called while under rtnl_lock()
12719  **/
12720 static int i40e_set_features(struct net_device *netdev,
12721 			     netdev_features_t features)
12722 {
12723 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12724 	struct i40e_vsi *vsi = np->vsi;
12725 	struct i40e_pf *pf = vsi->back;
12726 	bool need_reset;
12727 
12728 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12729 		i40e_pf_config_rss(pf);
12730 	else if (!(features & NETIF_F_RXHASH) &&
12731 		 netdev->features & NETIF_F_RXHASH)
12732 		i40e_clear_rss_lut(vsi);
12733 
12734 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12735 		i40e_vlan_stripping_enable(vsi);
12736 	else
12737 		i40e_vlan_stripping_disable(vsi);
12738 
12739 	if (!(features & NETIF_F_HW_TC) &&
12740 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12741 		dev_err(&pf->pdev->dev,
12742 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12743 		return -EINVAL;
12744 	}
12745 
12746 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12747 		i40e_del_all_macvlans(vsi);
12748 
12749 	need_reset = i40e_set_ntuple(pf, features);
12750 
12751 	if (need_reset)
12752 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12753 
12754 	return 0;
12755 }
12756 
12757 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12758 				    unsigned int table, unsigned int idx,
12759 				    struct udp_tunnel_info *ti)
12760 {
12761 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12762 	struct i40e_hw *hw = &np->vsi->back->hw;
12763 	u8 type, filter_index;
12764 	i40e_status ret;
12765 
12766 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12767 						   I40E_AQC_TUNNEL_TYPE_NGE;
12768 
12769 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12770 				     NULL);
12771 	if (ret) {
12772 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12773 			    i40e_stat_str(hw, ret),
12774 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12775 		return -EIO;
12776 	}
12777 
12778 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12779 	return 0;
12780 }
12781 
12782 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12783 				      unsigned int table, unsigned int idx,
12784 				      struct udp_tunnel_info *ti)
12785 {
12786 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12787 	struct i40e_hw *hw = &np->vsi->back->hw;
12788 	i40e_status ret;
12789 
12790 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12791 	if (ret) {
12792 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
12793 			    i40e_stat_str(hw, ret),
12794 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12795 		return -EIO;
12796 	}
12797 
12798 	return 0;
12799 }
12800 
12801 static int i40e_get_phys_port_id(struct net_device *netdev,
12802 				 struct netdev_phys_item_id *ppid)
12803 {
12804 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12805 	struct i40e_pf *pf = np->vsi->back;
12806 	struct i40e_hw *hw = &pf->hw;
12807 
12808 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
12809 		return -EOPNOTSUPP;
12810 
12811 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
12812 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
12813 
12814 	return 0;
12815 }
12816 
12817 /**
12818  * i40e_ndo_fdb_add - add an entry to the hardware database
12819  * @ndm: the input from the stack
12820  * @tb: pointer to array of nladdr (unused)
12821  * @dev: the net device pointer
12822  * @addr: the MAC address entry being added
12823  * @vid: VLAN ID
12824  * @flags: instructions from stack about fdb operation
12825  * @extack: netlink extended ack, unused currently
12826  */
12827 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
12828 			    struct net_device *dev,
12829 			    const unsigned char *addr, u16 vid,
12830 			    u16 flags,
12831 			    struct netlink_ext_ack *extack)
12832 {
12833 	struct i40e_netdev_priv *np = netdev_priv(dev);
12834 	struct i40e_pf *pf = np->vsi->back;
12835 	int err = 0;
12836 
12837 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
12838 		return -EOPNOTSUPP;
12839 
12840 	if (vid) {
12841 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
12842 		return -EINVAL;
12843 	}
12844 
12845 	/* Hardware does not support aging addresses so if a
12846 	 * ndm_state is given only allow permanent addresses
12847 	 */
12848 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
12849 		netdev_info(dev, "FDB only supports static addresses\n");
12850 		return -EINVAL;
12851 	}
12852 
12853 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
12854 		err = dev_uc_add_excl(dev, addr);
12855 	else if (is_multicast_ether_addr(addr))
12856 		err = dev_mc_add_excl(dev, addr);
12857 	else
12858 		err = -EINVAL;
12859 
12860 	/* Only return duplicate errors if NLM_F_EXCL is set */
12861 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
12862 		err = 0;
12863 
12864 	return err;
12865 }
12866 
12867 /**
12868  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
12869  * @dev: the netdev being configured
12870  * @nlh: RTNL message
12871  * @flags: bridge flags
12872  * @extack: netlink extended ack
12873  *
12874  * Inserts a new hardware bridge if not already created and
12875  * enables the bridging mode requested (VEB or VEPA). If the
12876  * hardware bridge has already been inserted and the request
12877  * is to change the mode then that requires a PF reset to
12878  * allow rebuild of the components with required hardware
12879  * bridge mode enabled.
12880  *
12881  * Note: expects to be called while under rtnl_lock()
12882  **/
12883 static int i40e_ndo_bridge_setlink(struct net_device *dev,
12884 				   struct nlmsghdr *nlh,
12885 				   u16 flags,
12886 				   struct netlink_ext_ack *extack)
12887 {
12888 	struct i40e_netdev_priv *np = netdev_priv(dev);
12889 	struct i40e_vsi *vsi = np->vsi;
12890 	struct i40e_pf *pf = vsi->back;
12891 	struct i40e_veb *veb = NULL;
12892 	struct nlattr *attr, *br_spec;
12893 	int i, rem;
12894 
12895 	/* Only for PF VSI for now */
12896 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12897 		return -EOPNOTSUPP;
12898 
12899 	/* Find the HW bridge for PF VSI */
12900 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12901 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12902 			veb = pf->veb[i];
12903 	}
12904 
12905 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
12906 
12907 	nla_for_each_nested(attr, br_spec, rem) {
12908 		__u16 mode;
12909 
12910 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
12911 			continue;
12912 
12913 		mode = nla_get_u16(attr);
12914 		if ((mode != BRIDGE_MODE_VEPA) &&
12915 		    (mode != BRIDGE_MODE_VEB))
12916 			return -EINVAL;
12917 
12918 		/* Insert a new HW bridge */
12919 		if (!veb) {
12920 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12921 					     vsi->tc_config.enabled_tc);
12922 			if (veb) {
12923 				veb->bridge_mode = mode;
12924 				i40e_config_bridge_mode(veb);
12925 			} else {
12926 				/* No Bridge HW offload available */
12927 				return -ENOENT;
12928 			}
12929 			break;
12930 		} else if (mode != veb->bridge_mode) {
12931 			/* Existing HW bridge but different mode needs reset */
12932 			veb->bridge_mode = mode;
12933 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
12934 			if (mode == BRIDGE_MODE_VEB)
12935 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
12936 			else
12937 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12938 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12939 			break;
12940 		}
12941 	}
12942 
12943 	return 0;
12944 }
12945 
12946 /**
12947  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
12948  * @skb: skb buff
12949  * @pid: process id
12950  * @seq: RTNL message seq #
12951  * @dev: the netdev being configured
12952  * @filter_mask: unused
12953  * @nlflags: netlink flags passed in
12954  *
12955  * Return the mode in which the hardware bridge is operating in
12956  * i.e VEB or VEPA.
12957  **/
12958 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
12959 				   struct net_device *dev,
12960 				   u32 __always_unused filter_mask,
12961 				   int nlflags)
12962 {
12963 	struct i40e_netdev_priv *np = netdev_priv(dev);
12964 	struct i40e_vsi *vsi = np->vsi;
12965 	struct i40e_pf *pf = vsi->back;
12966 	struct i40e_veb *veb = NULL;
12967 	int i;
12968 
12969 	/* Only for PF VSI for now */
12970 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12971 		return -EOPNOTSUPP;
12972 
12973 	/* Find the HW bridge for the PF VSI */
12974 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12975 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12976 			veb = pf->veb[i];
12977 	}
12978 
12979 	if (!veb)
12980 		return 0;
12981 
12982 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
12983 				       0, 0, nlflags, filter_mask, NULL);
12984 }
12985 
12986 /**
12987  * i40e_features_check - Validate encapsulated packet conforms to limits
12988  * @skb: skb buff
12989  * @dev: This physical port's netdev
12990  * @features: Offload features that the stack believes apply
12991  **/
12992 static netdev_features_t i40e_features_check(struct sk_buff *skb,
12993 					     struct net_device *dev,
12994 					     netdev_features_t features)
12995 {
12996 	size_t len;
12997 
12998 	/* No point in doing any of this if neither checksum nor GSO are
12999 	 * being requested for this frame.  We can rule out both by just
13000 	 * checking for CHECKSUM_PARTIAL
13001 	 */
13002 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13003 		return features;
13004 
13005 	/* We cannot support GSO if the MSS is going to be less than
13006 	 * 64 bytes.  If it is then we need to drop support for GSO.
13007 	 */
13008 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13009 		features &= ~NETIF_F_GSO_MASK;
13010 
13011 	/* MACLEN can support at most 63 words */
13012 	len = skb_network_header(skb) - skb->data;
13013 	if (len & ~(63 * 2))
13014 		goto out_err;
13015 
13016 	/* IPLEN and EIPLEN can support at most 127 dwords */
13017 	len = skb_transport_header(skb) - skb_network_header(skb);
13018 	if (len & ~(127 * 4))
13019 		goto out_err;
13020 
13021 	if (skb->encapsulation) {
13022 		/* L4TUNLEN can support 127 words */
13023 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13024 		if (len & ~(127 * 2))
13025 			goto out_err;
13026 
13027 		/* IPLEN can support at most 127 dwords */
13028 		len = skb_inner_transport_header(skb) -
13029 		      skb_inner_network_header(skb);
13030 		if (len & ~(127 * 4))
13031 			goto out_err;
13032 	}
13033 
13034 	/* No need to validate L4LEN as TCP is the only protocol with a
13035 	 * a flexible value and we support all possible values supported
13036 	 * by TCP, which is at most 15 dwords
13037 	 */
13038 
13039 	return features;
13040 out_err:
13041 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13042 }
13043 
13044 /**
13045  * i40e_xdp_setup - add/remove an XDP program
13046  * @vsi: VSI to changed
13047  * @prog: XDP program
13048  * @extack: netlink extended ack
13049  **/
13050 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13051 			  struct netlink_ext_ack *extack)
13052 {
13053 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
13054 	struct i40e_pf *pf = vsi->back;
13055 	struct bpf_prog *old_prog;
13056 	bool need_reset;
13057 	int i;
13058 
13059 	/* Don't allow frames that span over multiple buffers */
13060 	if (frame_size > vsi->rx_buf_len) {
13061 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
13062 		return -EINVAL;
13063 	}
13064 
13065 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13066 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13067 
13068 	if (need_reset)
13069 		i40e_prep_for_reset(pf);
13070 
13071 	/* VSI shall be deleted in a moment, just return EINVAL */
13072 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13073 		return -EINVAL;
13074 
13075 	old_prog = xchg(&vsi->xdp_prog, prog);
13076 
13077 	if (need_reset) {
13078 		if (!prog)
13079 			/* Wait until ndo_xsk_wakeup completes. */
13080 			synchronize_rcu();
13081 		i40e_reset_and_rebuild(pf, true, true);
13082 	}
13083 
13084 	for (i = 0; i < vsi->num_queue_pairs; i++)
13085 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13086 
13087 	if (old_prog)
13088 		bpf_prog_put(old_prog);
13089 
13090 	/* Kick start the NAPI context if there is an AF_XDP socket open
13091 	 * on that queue id. This so that receiving will start.
13092 	 */
13093 	if (need_reset && prog)
13094 		for (i = 0; i < vsi->num_queue_pairs; i++)
13095 			if (vsi->xdp_rings[i]->xsk_pool)
13096 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13097 						      XDP_WAKEUP_RX);
13098 
13099 	return 0;
13100 }
13101 
13102 /**
13103  * i40e_enter_busy_conf - Enters busy config state
13104  * @vsi: vsi
13105  *
13106  * Returns 0 on success, <0 for failure.
13107  **/
13108 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13109 {
13110 	struct i40e_pf *pf = vsi->back;
13111 	int timeout = 50;
13112 
13113 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13114 		timeout--;
13115 		if (!timeout)
13116 			return -EBUSY;
13117 		usleep_range(1000, 2000);
13118 	}
13119 
13120 	return 0;
13121 }
13122 
13123 /**
13124  * i40e_exit_busy_conf - Exits busy config state
13125  * @vsi: vsi
13126  **/
13127 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13128 {
13129 	struct i40e_pf *pf = vsi->back;
13130 
13131 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13132 }
13133 
13134 /**
13135  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13136  * @vsi: vsi
13137  * @queue_pair: queue pair
13138  **/
13139 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13140 {
13141 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13142 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13143 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13144 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13145 	if (i40e_enabled_xdp_vsi(vsi)) {
13146 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13147 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13148 	}
13149 }
13150 
13151 /**
13152  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13153  * @vsi: vsi
13154  * @queue_pair: queue pair
13155  **/
13156 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13157 {
13158 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13159 	if (i40e_enabled_xdp_vsi(vsi)) {
13160 		/* Make sure that in-progress ndo_xdp_xmit calls are
13161 		 * completed.
13162 		 */
13163 		synchronize_rcu();
13164 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13165 	}
13166 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13167 }
13168 
13169 /**
13170  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13171  * @vsi: vsi
13172  * @queue_pair: queue pair
13173  * @enable: true for enable, false for disable
13174  **/
13175 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13176 					bool enable)
13177 {
13178 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13179 	struct i40e_q_vector *q_vector = rxr->q_vector;
13180 
13181 	if (!vsi->netdev)
13182 		return;
13183 
13184 	/* All rings in a qp belong to the same qvector. */
13185 	if (q_vector->rx.ring || q_vector->tx.ring) {
13186 		if (enable)
13187 			napi_enable(&q_vector->napi);
13188 		else
13189 			napi_disable(&q_vector->napi);
13190 	}
13191 }
13192 
13193 /**
13194  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13195  * @vsi: vsi
13196  * @queue_pair: queue pair
13197  * @enable: true for enable, false for disable
13198  *
13199  * Returns 0 on success, <0 on failure.
13200  **/
13201 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13202 					bool enable)
13203 {
13204 	struct i40e_pf *pf = vsi->back;
13205 	int pf_q, ret = 0;
13206 
13207 	pf_q = vsi->base_queue + queue_pair;
13208 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13209 				     false /*is xdp*/, enable);
13210 	if (ret) {
13211 		dev_info(&pf->pdev->dev,
13212 			 "VSI seid %d Tx ring %d %sable timeout\n",
13213 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13214 		return ret;
13215 	}
13216 
13217 	i40e_control_rx_q(pf, pf_q, enable);
13218 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13219 	if (ret) {
13220 		dev_info(&pf->pdev->dev,
13221 			 "VSI seid %d Rx ring %d %sable timeout\n",
13222 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13223 		return ret;
13224 	}
13225 
13226 	/* Due to HW errata, on Rx disable only, the register can
13227 	 * indicate done before it really is. Needs 50ms to be sure
13228 	 */
13229 	if (!enable)
13230 		mdelay(50);
13231 
13232 	if (!i40e_enabled_xdp_vsi(vsi))
13233 		return ret;
13234 
13235 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13236 				     pf_q + vsi->alloc_queue_pairs,
13237 				     true /*is xdp*/, enable);
13238 	if (ret) {
13239 		dev_info(&pf->pdev->dev,
13240 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13241 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13242 	}
13243 
13244 	return ret;
13245 }
13246 
13247 /**
13248  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13249  * @vsi: vsi
13250  * @queue_pair: queue_pair
13251  **/
13252 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13253 {
13254 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13255 	struct i40e_pf *pf = vsi->back;
13256 	struct i40e_hw *hw = &pf->hw;
13257 
13258 	/* All rings in a qp belong to the same qvector. */
13259 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13260 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13261 	else
13262 		i40e_irq_dynamic_enable_icr0(pf);
13263 
13264 	i40e_flush(hw);
13265 }
13266 
13267 /**
13268  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13269  * @vsi: vsi
13270  * @queue_pair: queue_pair
13271  **/
13272 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13273 {
13274 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13275 	struct i40e_pf *pf = vsi->back;
13276 	struct i40e_hw *hw = &pf->hw;
13277 
13278 	/* For simplicity, instead of removing the qp interrupt causes
13279 	 * from the interrupt linked list, we simply disable the interrupt, and
13280 	 * leave the list intact.
13281 	 *
13282 	 * All rings in a qp belong to the same qvector.
13283 	 */
13284 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13285 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13286 
13287 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13288 		i40e_flush(hw);
13289 		synchronize_irq(pf->msix_entries[intpf].vector);
13290 	} else {
13291 		/* Legacy and MSI mode - this stops all interrupt handling */
13292 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13293 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13294 		i40e_flush(hw);
13295 		synchronize_irq(pf->pdev->irq);
13296 	}
13297 }
13298 
13299 /**
13300  * i40e_queue_pair_disable - Disables a queue pair
13301  * @vsi: vsi
13302  * @queue_pair: queue pair
13303  *
13304  * Returns 0 on success, <0 on failure.
13305  **/
13306 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13307 {
13308 	int err;
13309 
13310 	err = i40e_enter_busy_conf(vsi);
13311 	if (err)
13312 		return err;
13313 
13314 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13315 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13316 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13317 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13318 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13319 
13320 	return err;
13321 }
13322 
13323 /**
13324  * i40e_queue_pair_enable - Enables a queue pair
13325  * @vsi: vsi
13326  * @queue_pair: queue pair
13327  *
13328  * Returns 0 on success, <0 on failure.
13329  **/
13330 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13331 {
13332 	int err;
13333 
13334 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13335 	if (err)
13336 		return err;
13337 
13338 	if (i40e_enabled_xdp_vsi(vsi)) {
13339 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13340 		if (err)
13341 			return err;
13342 	}
13343 
13344 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13345 	if (err)
13346 		return err;
13347 
13348 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13349 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13350 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13351 
13352 	i40e_exit_busy_conf(vsi);
13353 
13354 	return err;
13355 }
13356 
13357 /**
13358  * i40e_xdp - implements ndo_bpf for i40e
13359  * @dev: netdevice
13360  * @xdp: XDP command
13361  **/
13362 static int i40e_xdp(struct net_device *dev,
13363 		    struct netdev_bpf *xdp)
13364 {
13365 	struct i40e_netdev_priv *np = netdev_priv(dev);
13366 	struct i40e_vsi *vsi = np->vsi;
13367 
13368 	if (vsi->type != I40E_VSI_MAIN)
13369 		return -EINVAL;
13370 
13371 	switch (xdp->command) {
13372 	case XDP_SETUP_PROG:
13373 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13374 	case XDP_SETUP_XSK_POOL:
13375 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13376 					   xdp->xsk.queue_id);
13377 	default:
13378 		return -EINVAL;
13379 	}
13380 }
13381 
13382 static const struct net_device_ops i40e_netdev_ops = {
13383 	.ndo_open		= i40e_open,
13384 	.ndo_stop		= i40e_close,
13385 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13386 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13387 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13388 	.ndo_validate_addr	= eth_validate_addr,
13389 	.ndo_set_mac_address	= i40e_set_mac,
13390 	.ndo_change_mtu		= i40e_change_mtu,
13391 	.ndo_eth_ioctl		= i40e_ioctl,
13392 	.ndo_tx_timeout		= i40e_tx_timeout,
13393 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13394 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13395 #ifdef CONFIG_NET_POLL_CONTROLLER
13396 	.ndo_poll_controller	= i40e_netpoll,
13397 #endif
13398 	.ndo_setup_tc		= __i40e_setup_tc,
13399 	.ndo_select_queue	= i40e_lan_select_queue,
13400 	.ndo_set_features	= i40e_set_features,
13401 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13402 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13403 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13404 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13405 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13406 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13407 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13408 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13409 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13410 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13411 	.ndo_features_check	= i40e_features_check,
13412 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13413 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13414 	.ndo_bpf		= i40e_xdp,
13415 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13416 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13417 	.ndo_dfwd_add_station	= i40e_fwd_add,
13418 	.ndo_dfwd_del_station	= i40e_fwd_del,
13419 };
13420 
13421 /**
13422  * i40e_config_netdev - Setup the netdev flags
13423  * @vsi: the VSI being configured
13424  *
13425  * Returns 0 on success, negative value on failure
13426  **/
13427 static int i40e_config_netdev(struct i40e_vsi *vsi)
13428 {
13429 	struct i40e_pf *pf = vsi->back;
13430 	struct i40e_hw *hw = &pf->hw;
13431 	struct i40e_netdev_priv *np;
13432 	struct net_device *netdev;
13433 	u8 broadcast[ETH_ALEN];
13434 	u8 mac_addr[ETH_ALEN];
13435 	int etherdev_size;
13436 	netdev_features_t hw_enc_features;
13437 	netdev_features_t hw_features;
13438 
13439 	etherdev_size = sizeof(struct i40e_netdev_priv);
13440 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13441 	if (!netdev)
13442 		return -ENOMEM;
13443 
13444 	vsi->netdev = netdev;
13445 	np = netdev_priv(netdev);
13446 	np->vsi = vsi;
13447 
13448 	hw_enc_features = NETIF_F_SG			|
13449 			  NETIF_F_IP_CSUM		|
13450 			  NETIF_F_IPV6_CSUM		|
13451 			  NETIF_F_HIGHDMA		|
13452 			  NETIF_F_SOFT_FEATURES		|
13453 			  NETIF_F_TSO			|
13454 			  NETIF_F_TSO_ECN		|
13455 			  NETIF_F_TSO6			|
13456 			  NETIF_F_GSO_GRE		|
13457 			  NETIF_F_GSO_GRE_CSUM		|
13458 			  NETIF_F_GSO_PARTIAL		|
13459 			  NETIF_F_GSO_IPXIP4		|
13460 			  NETIF_F_GSO_IPXIP6		|
13461 			  NETIF_F_GSO_UDP_TUNNEL	|
13462 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13463 			  NETIF_F_GSO_UDP_L4		|
13464 			  NETIF_F_SCTP_CRC		|
13465 			  NETIF_F_RXHASH		|
13466 			  NETIF_F_RXCSUM		|
13467 			  0;
13468 
13469 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13470 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13471 
13472 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13473 
13474 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13475 
13476 	netdev->hw_enc_features |= hw_enc_features;
13477 
13478 	/* record features VLANs can make use of */
13479 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13480 
13481 	/* enable macvlan offloads */
13482 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13483 
13484 	hw_features = hw_enc_features		|
13485 		      NETIF_F_HW_VLAN_CTAG_TX	|
13486 		      NETIF_F_HW_VLAN_CTAG_RX;
13487 
13488 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13489 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13490 
13491 	netdev->hw_features |= hw_features;
13492 
13493 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13494 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13495 
13496 	netdev->features &= ~NETIF_F_HW_TC;
13497 
13498 	if (vsi->type == I40E_VSI_MAIN) {
13499 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13500 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13501 		/* The following steps are necessary for two reasons. First,
13502 		 * some older NVM configurations load a default MAC-VLAN
13503 		 * filter that will accept any tagged packet, and we want to
13504 		 * replace this with a normal filter. Additionally, it is
13505 		 * possible our MAC address was provided by the platform using
13506 		 * Open Firmware or similar.
13507 		 *
13508 		 * Thus, we need to remove the default filter and install one
13509 		 * specific to the MAC address.
13510 		 */
13511 		i40e_rm_default_mac_filter(vsi, mac_addr);
13512 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13513 		i40e_add_mac_filter(vsi, mac_addr);
13514 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13515 	} else {
13516 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13517 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13518 		 * the end, which is 4 bytes long, so force truncation of the
13519 		 * original name by IFNAMSIZ - 4
13520 		 */
13521 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13522 			 IFNAMSIZ - 4,
13523 			 pf->vsi[pf->lan_vsi]->netdev->name);
13524 		eth_random_addr(mac_addr);
13525 
13526 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13527 		i40e_add_mac_filter(vsi, mac_addr);
13528 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13529 	}
13530 
13531 	/* Add the broadcast filter so that we initially will receive
13532 	 * broadcast packets. Note that when a new VLAN is first added the
13533 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13534 	 * specific filters as part of transitioning into "vlan" operation.
13535 	 * When more VLANs are added, the driver will copy each existing MAC
13536 	 * filter and add it for the new VLAN.
13537 	 *
13538 	 * Broadcast filters are handled specially by
13539 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13540 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13541 	 * filter. The subtask will update the correct broadcast promiscuous
13542 	 * bits as VLANs become active or inactive.
13543 	 */
13544 	eth_broadcast_addr(broadcast);
13545 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13546 	i40e_add_mac_filter(vsi, broadcast);
13547 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13548 
13549 	eth_hw_addr_set(netdev, mac_addr);
13550 	ether_addr_copy(netdev->perm_addr, mac_addr);
13551 
13552 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13553 	netdev->neigh_priv_len = sizeof(u32) * 4;
13554 
13555 	netdev->priv_flags |= IFF_UNICAST_FLT;
13556 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13557 	/* Setup netdev TC information */
13558 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13559 
13560 	netdev->netdev_ops = &i40e_netdev_ops;
13561 	netdev->watchdog_timeo = 5 * HZ;
13562 	i40e_set_ethtool_ops(netdev);
13563 
13564 	/* MTU range: 68 - 9706 */
13565 	netdev->min_mtu = ETH_MIN_MTU;
13566 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13567 
13568 	return 0;
13569 }
13570 
13571 /**
13572  * i40e_vsi_delete - Delete a VSI from the switch
13573  * @vsi: the VSI being removed
13574  *
13575  * Returns 0 on success, negative value on failure
13576  **/
13577 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13578 {
13579 	/* remove default VSI is not allowed */
13580 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13581 		return;
13582 
13583 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13584 }
13585 
13586 /**
13587  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13588  * @vsi: the VSI being queried
13589  *
13590  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13591  **/
13592 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13593 {
13594 	struct i40e_veb *veb;
13595 	struct i40e_pf *pf = vsi->back;
13596 
13597 	/* Uplink is not a bridge so default to VEB */
13598 	if (vsi->veb_idx >= I40E_MAX_VEB)
13599 		return 1;
13600 
13601 	veb = pf->veb[vsi->veb_idx];
13602 	if (!veb) {
13603 		dev_info(&pf->pdev->dev,
13604 			 "There is no veb associated with the bridge\n");
13605 		return -ENOENT;
13606 	}
13607 
13608 	/* Uplink is a bridge in VEPA mode */
13609 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13610 		return 0;
13611 	} else {
13612 		/* Uplink is a bridge in VEB mode */
13613 		return 1;
13614 	}
13615 
13616 	/* VEPA is now default bridge, so return 0 */
13617 	return 0;
13618 }
13619 
13620 /**
13621  * i40e_add_vsi - Add a VSI to the switch
13622  * @vsi: the VSI being configured
13623  *
13624  * This initializes a VSI context depending on the VSI type to be added and
13625  * passes it down to the add_vsi aq command.
13626  **/
13627 static int i40e_add_vsi(struct i40e_vsi *vsi)
13628 {
13629 	int ret = -ENODEV;
13630 	struct i40e_pf *pf = vsi->back;
13631 	struct i40e_hw *hw = &pf->hw;
13632 	struct i40e_vsi_context ctxt;
13633 	struct i40e_mac_filter *f;
13634 	struct hlist_node *h;
13635 	int bkt;
13636 
13637 	u8 enabled_tc = 0x1; /* TC0 enabled */
13638 	int f_count = 0;
13639 
13640 	memset(&ctxt, 0, sizeof(ctxt));
13641 	switch (vsi->type) {
13642 	case I40E_VSI_MAIN:
13643 		/* The PF's main VSI is already setup as part of the
13644 		 * device initialization, so we'll not bother with
13645 		 * the add_vsi call, but we will retrieve the current
13646 		 * VSI context.
13647 		 */
13648 		ctxt.seid = pf->main_vsi_seid;
13649 		ctxt.pf_num = pf->hw.pf_id;
13650 		ctxt.vf_num = 0;
13651 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13652 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13653 		if (ret) {
13654 			dev_info(&pf->pdev->dev,
13655 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13656 				 i40e_stat_str(&pf->hw, ret),
13657 				 i40e_aq_str(&pf->hw,
13658 					     pf->hw.aq.asq_last_status));
13659 			return -ENOENT;
13660 		}
13661 		vsi->info = ctxt.info;
13662 		vsi->info.valid_sections = 0;
13663 
13664 		vsi->seid = ctxt.seid;
13665 		vsi->id = ctxt.vsi_number;
13666 
13667 		enabled_tc = i40e_pf_get_tc_map(pf);
13668 
13669 		/* Source pruning is enabled by default, so the flag is
13670 		 * negative logic - if it's set, we need to fiddle with
13671 		 * the VSI to disable source pruning.
13672 		 */
13673 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13674 			memset(&ctxt, 0, sizeof(ctxt));
13675 			ctxt.seid = pf->main_vsi_seid;
13676 			ctxt.pf_num = pf->hw.pf_id;
13677 			ctxt.vf_num = 0;
13678 			ctxt.info.valid_sections |=
13679 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13680 			ctxt.info.switch_id =
13681 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13682 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13683 			if (ret) {
13684 				dev_info(&pf->pdev->dev,
13685 					 "update vsi failed, err %s aq_err %s\n",
13686 					 i40e_stat_str(&pf->hw, ret),
13687 					 i40e_aq_str(&pf->hw,
13688 						     pf->hw.aq.asq_last_status));
13689 				ret = -ENOENT;
13690 				goto err;
13691 			}
13692 		}
13693 
13694 		/* MFP mode setup queue map and update VSI */
13695 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13696 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13697 			memset(&ctxt, 0, sizeof(ctxt));
13698 			ctxt.seid = pf->main_vsi_seid;
13699 			ctxt.pf_num = pf->hw.pf_id;
13700 			ctxt.vf_num = 0;
13701 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13702 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13703 			if (ret) {
13704 				dev_info(&pf->pdev->dev,
13705 					 "update vsi failed, err %s aq_err %s\n",
13706 					 i40e_stat_str(&pf->hw, ret),
13707 					 i40e_aq_str(&pf->hw,
13708 						    pf->hw.aq.asq_last_status));
13709 				ret = -ENOENT;
13710 				goto err;
13711 			}
13712 			/* update the local VSI info queue map */
13713 			i40e_vsi_update_queue_map(vsi, &ctxt);
13714 			vsi->info.valid_sections = 0;
13715 		} else {
13716 			/* Default/Main VSI is only enabled for TC0
13717 			 * reconfigure it to enable all TCs that are
13718 			 * available on the port in SFP mode.
13719 			 * For MFP case the iSCSI PF would use this
13720 			 * flow to enable LAN+iSCSI TC.
13721 			 */
13722 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13723 			if (ret) {
13724 				/* Single TC condition is not fatal,
13725 				 * message and continue
13726 				 */
13727 				dev_info(&pf->pdev->dev,
13728 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13729 					 enabled_tc,
13730 					 i40e_stat_str(&pf->hw, ret),
13731 					 i40e_aq_str(&pf->hw,
13732 						    pf->hw.aq.asq_last_status));
13733 			}
13734 		}
13735 		break;
13736 
13737 	case I40E_VSI_FDIR:
13738 		ctxt.pf_num = hw->pf_id;
13739 		ctxt.vf_num = 0;
13740 		ctxt.uplink_seid = vsi->uplink_seid;
13741 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13742 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13743 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13744 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13745 			ctxt.info.valid_sections |=
13746 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13747 			ctxt.info.switch_id =
13748 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13749 		}
13750 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13751 		break;
13752 
13753 	case I40E_VSI_VMDQ2:
13754 		ctxt.pf_num = hw->pf_id;
13755 		ctxt.vf_num = 0;
13756 		ctxt.uplink_seid = vsi->uplink_seid;
13757 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13758 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13759 
13760 		/* This VSI is connected to VEB so the switch_id
13761 		 * should be set to zero by default.
13762 		 */
13763 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13764 			ctxt.info.valid_sections |=
13765 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13766 			ctxt.info.switch_id =
13767 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13768 		}
13769 
13770 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13771 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13772 		break;
13773 
13774 	case I40E_VSI_SRIOV:
13775 		ctxt.pf_num = hw->pf_id;
13776 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13777 		ctxt.uplink_seid = vsi->uplink_seid;
13778 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13779 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13780 
13781 		/* This VSI is connected to VEB so the switch_id
13782 		 * should be set to zero by default.
13783 		 */
13784 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13785 			ctxt.info.valid_sections |=
13786 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13787 			ctxt.info.switch_id =
13788 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13789 		}
13790 
13791 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
13792 			ctxt.info.valid_sections |=
13793 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
13794 			ctxt.info.queueing_opt_flags |=
13795 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
13796 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
13797 		}
13798 
13799 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
13800 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
13801 		if (pf->vf[vsi->vf_id].spoofchk) {
13802 			ctxt.info.valid_sections |=
13803 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
13804 			ctxt.info.sec_flags |=
13805 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
13806 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
13807 		}
13808 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13809 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13810 		break;
13811 
13812 	case I40E_VSI_IWARP:
13813 		/* send down message to iWARP */
13814 		break;
13815 
13816 	default:
13817 		return -ENODEV;
13818 	}
13819 
13820 	if (vsi->type != I40E_VSI_MAIN) {
13821 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
13822 		if (ret) {
13823 			dev_info(&vsi->back->pdev->dev,
13824 				 "add vsi failed, err %s aq_err %s\n",
13825 				 i40e_stat_str(&pf->hw, ret),
13826 				 i40e_aq_str(&pf->hw,
13827 					     pf->hw.aq.asq_last_status));
13828 			ret = -ENOENT;
13829 			goto err;
13830 		}
13831 		vsi->info = ctxt.info;
13832 		vsi->info.valid_sections = 0;
13833 		vsi->seid = ctxt.seid;
13834 		vsi->id = ctxt.vsi_number;
13835 	}
13836 
13837 	vsi->active_filters = 0;
13838 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
13839 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13840 	/* If macvlan filters already exist, force them to get loaded */
13841 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
13842 		f->state = I40E_FILTER_NEW;
13843 		f_count++;
13844 	}
13845 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13846 
13847 	if (f_count) {
13848 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
13849 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
13850 	}
13851 
13852 	/* Update VSI BW information */
13853 	ret = i40e_vsi_get_bw_info(vsi);
13854 	if (ret) {
13855 		dev_info(&pf->pdev->dev,
13856 			 "couldn't get vsi bw info, err %s aq_err %s\n",
13857 			 i40e_stat_str(&pf->hw, ret),
13858 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13859 		/* VSI is already added so not tearing that up */
13860 		ret = 0;
13861 	}
13862 
13863 err:
13864 	return ret;
13865 }
13866 
13867 /**
13868  * i40e_vsi_release - Delete a VSI and free its resources
13869  * @vsi: the VSI being removed
13870  *
13871  * Returns 0 on success or < 0 on error
13872  **/
13873 int i40e_vsi_release(struct i40e_vsi *vsi)
13874 {
13875 	struct i40e_mac_filter *f;
13876 	struct hlist_node *h;
13877 	struct i40e_veb *veb = NULL;
13878 	struct i40e_pf *pf;
13879 	u16 uplink_seid;
13880 	int i, n, bkt;
13881 
13882 	pf = vsi->back;
13883 
13884 	/* release of a VEB-owner or last VSI is not allowed */
13885 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
13886 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
13887 			 vsi->seid, vsi->uplink_seid);
13888 		return -ENODEV;
13889 	}
13890 	if (vsi == pf->vsi[pf->lan_vsi] &&
13891 	    !test_bit(__I40E_DOWN, pf->state)) {
13892 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
13893 		return -ENODEV;
13894 	}
13895 	set_bit(__I40E_VSI_RELEASING, vsi->state);
13896 	uplink_seid = vsi->uplink_seid;
13897 	if (vsi->type != I40E_VSI_SRIOV) {
13898 		if (vsi->netdev_registered) {
13899 			vsi->netdev_registered = false;
13900 			if (vsi->netdev) {
13901 				/* results in a call to i40e_close() */
13902 				unregister_netdev(vsi->netdev);
13903 			}
13904 		} else {
13905 			i40e_vsi_close(vsi);
13906 		}
13907 		i40e_vsi_disable_irq(vsi);
13908 	}
13909 
13910 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13911 
13912 	/* clear the sync flag on all filters */
13913 	if (vsi->netdev) {
13914 		__dev_uc_unsync(vsi->netdev, NULL);
13915 		__dev_mc_unsync(vsi->netdev, NULL);
13916 	}
13917 
13918 	/* make sure any remaining filters are marked for deletion */
13919 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
13920 		__i40e_del_filter(vsi, f);
13921 
13922 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13923 
13924 	i40e_sync_vsi_filters(vsi);
13925 
13926 	i40e_vsi_delete(vsi);
13927 	i40e_vsi_free_q_vectors(vsi);
13928 	if (vsi->netdev) {
13929 		free_netdev(vsi->netdev);
13930 		vsi->netdev = NULL;
13931 	}
13932 	i40e_vsi_clear_rings(vsi);
13933 	i40e_vsi_clear(vsi);
13934 
13935 	/* If this was the last thing on the VEB, except for the
13936 	 * controlling VSI, remove the VEB, which puts the controlling
13937 	 * VSI onto the next level down in the switch.
13938 	 *
13939 	 * Well, okay, there's one more exception here: don't remove
13940 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
13941 	 * from up the network stack.
13942 	 */
13943 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
13944 		if (pf->vsi[i] &&
13945 		    pf->vsi[i]->uplink_seid == uplink_seid &&
13946 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13947 			n++;      /* count the VSIs */
13948 		}
13949 	}
13950 	for (i = 0; i < I40E_MAX_VEB; i++) {
13951 		if (!pf->veb[i])
13952 			continue;
13953 		if (pf->veb[i]->uplink_seid == uplink_seid)
13954 			n++;     /* count the VEBs */
13955 		if (pf->veb[i]->seid == uplink_seid)
13956 			veb = pf->veb[i];
13957 	}
13958 	if (n == 0 && veb && veb->uplink_seid != 0)
13959 		i40e_veb_release(veb);
13960 
13961 	return 0;
13962 }
13963 
13964 /**
13965  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
13966  * @vsi: ptr to the VSI
13967  *
13968  * This should only be called after i40e_vsi_mem_alloc() which allocates the
13969  * corresponding SW VSI structure and initializes num_queue_pairs for the
13970  * newly allocated VSI.
13971  *
13972  * Returns 0 on success or negative on failure
13973  **/
13974 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
13975 {
13976 	int ret = -ENOENT;
13977 	struct i40e_pf *pf = vsi->back;
13978 
13979 	if (vsi->q_vectors[0]) {
13980 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
13981 			 vsi->seid);
13982 		return -EEXIST;
13983 	}
13984 
13985 	if (vsi->base_vector) {
13986 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
13987 			 vsi->seid, vsi->base_vector);
13988 		return -EEXIST;
13989 	}
13990 
13991 	ret = i40e_vsi_alloc_q_vectors(vsi);
13992 	if (ret) {
13993 		dev_info(&pf->pdev->dev,
13994 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
13995 			 vsi->num_q_vectors, vsi->seid, ret);
13996 		vsi->num_q_vectors = 0;
13997 		goto vector_setup_out;
13998 	}
13999 
14000 	/* In Legacy mode, we do not have to get any other vector since we
14001 	 * piggyback on the misc/ICR0 for queue interrupts.
14002 	*/
14003 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
14004 		return ret;
14005 	if (vsi->num_q_vectors)
14006 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14007 						 vsi->num_q_vectors, vsi->idx);
14008 	if (vsi->base_vector < 0) {
14009 		dev_info(&pf->pdev->dev,
14010 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14011 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14012 		i40e_vsi_free_q_vectors(vsi);
14013 		ret = -ENOENT;
14014 		goto vector_setup_out;
14015 	}
14016 
14017 vector_setup_out:
14018 	return ret;
14019 }
14020 
14021 /**
14022  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14023  * @vsi: pointer to the vsi.
14024  *
14025  * This re-allocates a vsi's queue resources.
14026  *
14027  * Returns pointer to the successfully allocated and configured VSI sw struct
14028  * on success, otherwise returns NULL on failure.
14029  **/
14030 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14031 {
14032 	u16 alloc_queue_pairs;
14033 	struct i40e_pf *pf;
14034 	u8 enabled_tc;
14035 	int ret;
14036 
14037 	if (!vsi)
14038 		return NULL;
14039 
14040 	pf = vsi->back;
14041 
14042 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14043 	i40e_vsi_clear_rings(vsi);
14044 
14045 	i40e_vsi_free_arrays(vsi, false);
14046 	i40e_set_num_rings_in_vsi(vsi);
14047 	ret = i40e_vsi_alloc_arrays(vsi, false);
14048 	if (ret)
14049 		goto err_vsi;
14050 
14051 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14052 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14053 
14054 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14055 	if (ret < 0) {
14056 		dev_info(&pf->pdev->dev,
14057 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14058 			 alloc_queue_pairs, vsi->seid, ret);
14059 		goto err_vsi;
14060 	}
14061 	vsi->base_queue = ret;
14062 
14063 	/* Update the FW view of the VSI. Force a reset of TC and queue
14064 	 * layout configurations.
14065 	 */
14066 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14067 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14068 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14069 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14070 	if (vsi->type == I40E_VSI_MAIN)
14071 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14072 
14073 	/* assign it some queues */
14074 	ret = i40e_alloc_rings(vsi);
14075 	if (ret)
14076 		goto err_rings;
14077 
14078 	/* map all of the rings to the q_vectors */
14079 	i40e_vsi_map_rings_to_vectors(vsi);
14080 	return vsi;
14081 
14082 err_rings:
14083 	i40e_vsi_free_q_vectors(vsi);
14084 	if (vsi->netdev_registered) {
14085 		vsi->netdev_registered = false;
14086 		unregister_netdev(vsi->netdev);
14087 		free_netdev(vsi->netdev);
14088 		vsi->netdev = NULL;
14089 	}
14090 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14091 err_vsi:
14092 	i40e_vsi_clear(vsi);
14093 	return NULL;
14094 }
14095 
14096 /**
14097  * i40e_vsi_setup - Set up a VSI by a given type
14098  * @pf: board private structure
14099  * @type: VSI type
14100  * @uplink_seid: the switch element to link to
14101  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14102  *
14103  * This allocates the sw VSI structure and its queue resources, then add a VSI
14104  * to the identified VEB.
14105  *
14106  * Returns pointer to the successfully allocated and configure VSI sw struct on
14107  * success, otherwise returns NULL on failure.
14108  **/
14109 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14110 				u16 uplink_seid, u32 param1)
14111 {
14112 	struct i40e_vsi *vsi = NULL;
14113 	struct i40e_veb *veb = NULL;
14114 	u16 alloc_queue_pairs;
14115 	int ret, i;
14116 	int v_idx;
14117 
14118 	/* The requested uplink_seid must be either
14119 	 *     - the PF's port seid
14120 	 *              no VEB is needed because this is the PF
14121 	 *              or this is a Flow Director special case VSI
14122 	 *     - seid of an existing VEB
14123 	 *     - seid of a VSI that owns an existing VEB
14124 	 *     - seid of a VSI that doesn't own a VEB
14125 	 *              a new VEB is created and the VSI becomes the owner
14126 	 *     - seid of the PF VSI, which is what creates the first VEB
14127 	 *              this is a special case of the previous
14128 	 *
14129 	 * Find which uplink_seid we were given and create a new VEB if needed
14130 	 */
14131 	for (i = 0; i < I40E_MAX_VEB; i++) {
14132 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14133 			veb = pf->veb[i];
14134 			break;
14135 		}
14136 	}
14137 
14138 	if (!veb && uplink_seid != pf->mac_seid) {
14139 
14140 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14141 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14142 				vsi = pf->vsi[i];
14143 				break;
14144 			}
14145 		}
14146 		if (!vsi) {
14147 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14148 				 uplink_seid);
14149 			return NULL;
14150 		}
14151 
14152 		if (vsi->uplink_seid == pf->mac_seid)
14153 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14154 					     vsi->tc_config.enabled_tc);
14155 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14156 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14157 					     vsi->tc_config.enabled_tc);
14158 		if (veb) {
14159 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14160 				dev_info(&vsi->back->pdev->dev,
14161 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14162 				return NULL;
14163 			}
14164 			/* We come up by default in VEPA mode if SRIOV is not
14165 			 * already enabled, in which case we can't force VEPA
14166 			 * mode.
14167 			 */
14168 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14169 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14170 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14171 			}
14172 			i40e_config_bridge_mode(veb);
14173 		}
14174 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14175 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14176 				veb = pf->veb[i];
14177 		}
14178 		if (!veb) {
14179 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14180 			return NULL;
14181 		}
14182 
14183 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14184 		uplink_seid = veb->seid;
14185 	}
14186 
14187 	/* get vsi sw struct */
14188 	v_idx = i40e_vsi_mem_alloc(pf, type);
14189 	if (v_idx < 0)
14190 		goto err_alloc;
14191 	vsi = pf->vsi[v_idx];
14192 	if (!vsi)
14193 		goto err_alloc;
14194 	vsi->type = type;
14195 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14196 
14197 	if (type == I40E_VSI_MAIN)
14198 		pf->lan_vsi = v_idx;
14199 	else if (type == I40E_VSI_SRIOV)
14200 		vsi->vf_id = param1;
14201 	/* assign it some queues */
14202 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14203 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14204 
14205 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14206 	if (ret < 0) {
14207 		dev_info(&pf->pdev->dev,
14208 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14209 			 alloc_queue_pairs, vsi->seid, ret);
14210 		goto err_vsi;
14211 	}
14212 	vsi->base_queue = ret;
14213 
14214 	/* get a VSI from the hardware */
14215 	vsi->uplink_seid = uplink_seid;
14216 	ret = i40e_add_vsi(vsi);
14217 	if (ret)
14218 		goto err_vsi;
14219 
14220 	switch (vsi->type) {
14221 	/* setup the netdev if needed */
14222 	case I40E_VSI_MAIN:
14223 	case I40E_VSI_VMDQ2:
14224 		ret = i40e_config_netdev(vsi);
14225 		if (ret)
14226 			goto err_netdev;
14227 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14228 		if (ret)
14229 			goto err_netdev;
14230 		ret = register_netdev(vsi->netdev);
14231 		if (ret)
14232 			goto err_netdev;
14233 		vsi->netdev_registered = true;
14234 		netif_carrier_off(vsi->netdev);
14235 #ifdef CONFIG_I40E_DCB
14236 		/* Setup DCB netlink interface */
14237 		i40e_dcbnl_setup(vsi);
14238 #endif /* CONFIG_I40E_DCB */
14239 		fallthrough;
14240 	case I40E_VSI_FDIR:
14241 		/* set up vectors and rings if needed */
14242 		ret = i40e_vsi_setup_vectors(vsi);
14243 		if (ret)
14244 			goto err_msix;
14245 
14246 		ret = i40e_alloc_rings(vsi);
14247 		if (ret)
14248 			goto err_rings;
14249 
14250 		/* map all of the rings to the q_vectors */
14251 		i40e_vsi_map_rings_to_vectors(vsi);
14252 
14253 		i40e_vsi_reset_stats(vsi);
14254 		break;
14255 	default:
14256 		/* no netdev or rings for the other VSI types */
14257 		break;
14258 	}
14259 
14260 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14261 	    (vsi->type == I40E_VSI_VMDQ2)) {
14262 		ret = i40e_vsi_config_rss(vsi);
14263 	}
14264 	return vsi;
14265 
14266 err_rings:
14267 	i40e_vsi_free_q_vectors(vsi);
14268 err_msix:
14269 	if (vsi->netdev_registered) {
14270 		vsi->netdev_registered = false;
14271 		unregister_netdev(vsi->netdev);
14272 		free_netdev(vsi->netdev);
14273 		vsi->netdev = NULL;
14274 	}
14275 err_netdev:
14276 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14277 err_vsi:
14278 	i40e_vsi_clear(vsi);
14279 err_alloc:
14280 	return NULL;
14281 }
14282 
14283 /**
14284  * i40e_veb_get_bw_info - Query VEB BW information
14285  * @veb: the veb to query
14286  *
14287  * Query the Tx scheduler BW configuration data for given VEB
14288  **/
14289 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14290 {
14291 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14292 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14293 	struct i40e_pf *pf = veb->pf;
14294 	struct i40e_hw *hw = &pf->hw;
14295 	u32 tc_bw_max;
14296 	int ret = 0;
14297 	int i;
14298 
14299 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14300 						  &bw_data, NULL);
14301 	if (ret) {
14302 		dev_info(&pf->pdev->dev,
14303 			 "query veb bw config failed, err %s aq_err %s\n",
14304 			 i40e_stat_str(&pf->hw, ret),
14305 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14306 		goto out;
14307 	}
14308 
14309 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14310 						   &ets_data, NULL);
14311 	if (ret) {
14312 		dev_info(&pf->pdev->dev,
14313 			 "query veb bw ets config failed, err %s aq_err %s\n",
14314 			 i40e_stat_str(&pf->hw, ret),
14315 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14316 		goto out;
14317 	}
14318 
14319 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14320 	veb->bw_max_quanta = ets_data.tc_bw_max;
14321 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14322 	veb->enabled_tc = ets_data.tc_valid_bits;
14323 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14324 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14325 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14326 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14327 		veb->bw_tc_limit_credits[i] =
14328 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14329 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14330 	}
14331 
14332 out:
14333 	return ret;
14334 }
14335 
14336 /**
14337  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14338  * @pf: board private structure
14339  *
14340  * On error: returns error code (negative)
14341  * On success: returns vsi index in PF (positive)
14342  **/
14343 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14344 {
14345 	int ret = -ENOENT;
14346 	struct i40e_veb *veb;
14347 	int i;
14348 
14349 	/* Need to protect the allocation of switch elements at the PF level */
14350 	mutex_lock(&pf->switch_mutex);
14351 
14352 	/* VEB list may be fragmented if VEB creation/destruction has
14353 	 * been happening.  We can afford to do a quick scan to look
14354 	 * for any free slots in the list.
14355 	 *
14356 	 * find next empty veb slot, looping back around if necessary
14357 	 */
14358 	i = 0;
14359 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14360 		i++;
14361 	if (i >= I40E_MAX_VEB) {
14362 		ret = -ENOMEM;
14363 		goto err_alloc_veb;  /* out of VEB slots! */
14364 	}
14365 
14366 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14367 	if (!veb) {
14368 		ret = -ENOMEM;
14369 		goto err_alloc_veb;
14370 	}
14371 	veb->pf = pf;
14372 	veb->idx = i;
14373 	veb->enabled_tc = 1;
14374 
14375 	pf->veb[i] = veb;
14376 	ret = i;
14377 err_alloc_veb:
14378 	mutex_unlock(&pf->switch_mutex);
14379 	return ret;
14380 }
14381 
14382 /**
14383  * i40e_switch_branch_release - Delete a branch of the switch tree
14384  * @branch: where to start deleting
14385  *
14386  * This uses recursion to find the tips of the branch to be
14387  * removed, deleting until we get back to and can delete this VEB.
14388  **/
14389 static void i40e_switch_branch_release(struct i40e_veb *branch)
14390 {
14391 	struct i40e_pf *pf = branch->pf;
14392 	u16 branch_seid = branch->seid;
14393 	u16 veb_idx = branch->idx;
14394 	int i;
14395 
14396 	/* release any VEBs on this VEB - RECURSION */
14397 	for (i = 0; i < I40E_MAX_VEB; i++) {
14398 		if (!pf->veb[i])
14399 			continue;
14400 		if (pf->veb[i]->uplink_seid == branch->seid)
14401 			i40e_switch_branch_release(pf->veb[i]);
14402 	}
14403 
14404 	/* Release the VSIs on this VEB, but not the owner VSI.
14405 	 *
14406 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14407 	 *       the VEB itself, so don't use (*branch) after this loop.
14408 	 */
14409 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14410 		if (!pf->vsi[i])
14411 			continue;
14412 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14413 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14414 			i40e_vsi_release(pf->vsi[i]);
14415 		}
14416 	}
14417 
14418 	/* There's one corner case where the VEB might not have been
14419 	 * removed, so double check it here and remove it if needed.
14420 	 * This case happens if the veb was created from the debugfs
14421 	 * commands and no VSIs were added to it.
14422 	 */
14423 	if (pf->veb[veb_idx])
14424 		i40e_veb_release(pf->veb[veb_idx]);
14425 }
14426 
14427 /**
14428  * i40e_veb_clear - remove veb struct
14429  * @veb: the veb to remove
14430  **/
14431 static void i40e_veb_clear(struct i40e_veb *veb)
14432 {
14433 	if (!veb)
14434 		return;
14435 
14436 	if (veb->pf) {
14437 		struct i40e_pf *pf = veb->pf;
14438 
14439 		mutex_lock(&pf->switch_mutex);
14440 		if (pf->veb[veb->idx] == veb)
14441 			pf->veb[veb->idx] = NULL;
14442 		mutex_unlock(&pf->switch_mutex);
14443 	}
14444 
14445 	kfree(veb);
14446 }
14447 
14448 /**
14449  * i40e_veb_release - Delete a VEB and free its resources
14450  * @veb: the VEB being removed
14451  **/
14452 void i40e_veb_release(struct i40e_veb *veb)
14453 {
14454 	struct i40e_vsi *vsi = NULL;
14455 	struct i40e_pf *pf;
14456 	int i, n = 0;
14457 
14458 	pf = veb->pf;
14459 
14460 	/* find the remaining VSI and check for extras */
14461 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14462 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14463 			n++;
14464 			vsi = pf->vsi[i];
14465 		}
14466 	}
14467 	if (n != 1) {
14468 		dev_info(&pf->pdev->dev,
14469 			 "can't remove VEB %d with %d VSIs left\n",
14470 			 veb->seid, n);
14471 		return;
14472 	}
14473 
14474 	/* move the remaining VSI to uplink veb */
14475 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14476 	if (veb->uplink_seid) {
14477 		vsi->uplink_seid = veb->uplink_seid;
14478 		if (veb->uplink_seid == pf->mac_seid)
14479 			vsi->veb_idx = I40E_NO_VEB;
14480 		else
14481 			vsi->veb_idx = veb->veb_idx;
14482 	} else {
14483 		/* floating VEB */
14484 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14485 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14486 	}
14487 
14488 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14489 	i40e_veb_clear(veb);
14490 }
14491 
14492 /**
14493  * i40e_add_veb - create the VEB in the switch
14494  * @veb: the VEB to be instantiated
14495  * @vsi: the controlling VSI
14496  **/
14497 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14498 {
14499 	struct i40e_pf *pf = veb->pf;
14500 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14501 	int ret;
14502 
14503 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14504 			      veb->enabled_tc, false,
14505 			      &veb->seid, enable_stats, NULL);
14506 
14507 	/* get a VEB from the hardware */
14508 	if (ret) {
14509 		dev_info(&pf->pdev->dev,
14510 			 "couldn't add VEB, err %s aq_err %s\n",
14511 			 i40e_stat_str(&pf->hw, ret),
14512 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14513 		return -EPERM;
14514 	}
14515 
14516 	/* get statistics counter */
14517 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14518 					 &veb->stats_idx, NULL, NULL, NULL);
14519 	if (ret) {
14520 		dev_info(&pf->pdev->dev,
14521 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
14522 			 i40e_stat_str(&pf->hw, ret),
14523 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14524 		return -EPERM;
14525 	}
14526 	ret = i40e_veb_get_bw_info(veb);
14527 	if (ret) {
14528 		dev_info(&pf->pdev->dev,
14529 			 "couldn't get VEB bw info, err %s aq_err %s\n",
14530 			 i40e_stat_str(&pf->hw, ret),
14531 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14532 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14533 		return -ENOENT;
14534 	}
14535 
14536 	vsi->uplink_seid = veb->seid;
14537 	vsi->veb_idx = veb->idx;
14538 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14539 
14540 	return 0;
14541 }
14542 
14543 /**
14544  * i40e_veb_setup - Set up a VEB
14545  * @pf: board private structure
14546  * @flags: VEB setup flags
14547  * @uplink_seid: the switch element to link to
14548  * @vsi_seid: the initial VSI seid
14549  * @enabled_tc: Enabled TC bit-map
14550  *
14551  * This allocates the sw VEB structure and links it into the switch
14552  * It is possible and legal for this to be a duplicate of an already
14553  * existing VEB.  It is also possible for both uplink and vsi seids
14554  * to be zero, in order to create a floating VEB.
14555  *
14556  * Returns pointer to the successfully allocated VEB sw struct on
14557  * success, otherwise returns NULL on failure.
14558  **/
14559 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14560 				u16 uplink_seid, u16 vsi_seid,
14561 				u8 enabled_tc)
14562 {
14563 	struct i40e_veb *veb, *uplink_veb = NULL;
14564 	int vsi_idx, veb_idx;
14565 	int ret;
14566 
14567 	/* if one seid is 0, the other must be 0 to create a floating relay */
14568 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14569 	    (uplink_seid + vsi_seid != 0)) {
14570 		dev_info(&pf->pdev->dev,
14571 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14572 			 uplink_seid, vsi_seid);
14573 		return NULL;
14574 	}
14575 
14576 	/* make sure there is such a vsi and uplink */
14577 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14578 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14579 			break;
14580 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14581 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14582 			 vsi_seid);
14583 		return NULL;
14584 	}
14585 
14586 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14587 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14588 			if (pf->veb[veb_idx] &&
14589 			    pf->veb[veb_idx]->seid == uplink_seid) {
14590 				uplink_veb = pf->veb[veb_idx];
14591 				break;
14592 			}
14593 		}
14594 		if (!uplink_veb) {
14595 			dev_info(&pf->pdev->dev,
14596 				 "uplink seid %d not found\n", uplink_seid);
14597 			return NULL;
14598 		}
14599 	}
14600 
14601 	/* get veb sw struct */
14602 	veb_idx = i40e_veb_mem_alloc(pf);
14603 	if (veb_idx < 0)
14604 		goto err_alloc;
14605 	veb = pf->veb[veb_idx];
14606 	veb->flags = flags;
14607 	veb->uplink_seid = uplink_seid;
14608 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14609 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14610 
14611 	/* create the VEB in the switch */
14612 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14613 	if (ret)
14614 		goto err_veb;
14615 	if (vsi_idx == pf->lan_vsi)
14616 		pf->lan_veb = veb->idx;
14617 
14618 	return veb;
14619 
14620 err_veb:
14621 	i40e_veb_clear(veb);
14622 err_alloc:
14623 	return NULL;
14624 }
14625 
14626 /**
14627  * i40e_setup_pf_switch_element - set PF vars based on switch type
14628  * @pf: board private structure
14629  * @ele: element we are building info from
14630  * @num_reported: total number of elements
14631  * @printconfig: should we print the contents
14632  *
14633  * helper function to assist in extracting a few useful SEID values.
14634  **/
14635 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14636 				struct i40e_aqc_switch_config_element_resp *ele,
14637 				u16 num_reported, bool printconfig)
14638 {
14639 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14640 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14641 	u8 element_type = ele->element_type;
14642 	u16 seid = le16_to_cpu(ele->seid);
14643 
14644 	if (printconfig)
14645 		dev_info(&pf->pdev->dev,
14646 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14647 			 element_type, seid, uplink_seid, downlink_seid);
14648 
14649 	switch (element_type) {
14650 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14651 		pf->mac_seid = seid;
14652 		break;
14653 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14654 		/* Main VEB? */
14655 		if (uplink_seid != pf->mac_seid)
14656 			break;
14657 		if (pf->lan_veb >= I40E_MAX_VEB) {
14658 			int v;
14659 
14660 			/* find existing or else empty VEB */
14661 			for (v = 0; v < I40E_MAX_VEB; v++) {
14662 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14663 					pf->lan_veb = v;
14664 					break;
14665 				}
14666 			}
14667 			if (pf->lan_veb >= I40E_MAX_VEB) {
14668 				v = i40e_veb_mem_alloc(pf);
14669 				if (v < 0)
14670 					break;
14671 				pf->lan_veb = v;
14672 			}
14673 		}
14674 		if (pf->lan_veb >= I40E_MAX_VEB)
14675 			break;
14676 
14677 		pf->veb[pf->lan_veb]->seid = seid;
14678 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14679 		pf->veb[pf->lan_veb]->pf = pf;
14680 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14681 		break;
14682 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14683 		if (num_reported != 1)
14684 			break;
14685 		/* This is immediately after a reset so we can assume this is
14686 		 * the PF's VSI
14687 		 */
14688 		pf->mac_seid = uplink_seid;
14689 		pf->pf_seid = downlink_seid;
14690 		pf->main_vsi_seid = seid;
14691 		if (printconfig)
14692 			dev_info(&pf->pdev->dev,
14693 				 "pf_seid=%d main_vsi_seid=%d\n",
14694 				 pf->pf_seid, pf->main_vsi_seid);
14695 		break;
14696 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14697 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14698 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14699 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14700 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14701 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14702 		/* ignore these for now */
14703 		break;
14704 	default:
14705 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14706 			 element_type, seid);
14707 		break;
14708 	}
14709 }
14710 
14711 /**
14712  * i40e_fetch_switch_configuration - Get switch config from firmware
14713  * @pf: board private structure
14714  * @printconfig: should we print the contents
14715  *
14716  * Get the current switch configuration from the device and
14717  * extract a few useful SEID values.
14718  **/
14719 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14720 {
14721 	struct i40e_aqc_get_switch_config_resp *sw_config;
14722 	u16 next_seid = 0;
14723 	int ret = 0;
14724 	u8 *aq_buf;
14725 	int i;
14726 
14727 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14728 	if (!aq_buf)
14729 		return -ENOMEM;
14730 
14731 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14732 	do {
14733 		u16 num_reported, num_total;
14734 
14735 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14736 						I40E_AQ_LARGE_BUF,
14737 						&next_seid, NULL);
14738 		if (ret) {
14739 			dev_info(&pf->pdev->dev,
14740 				 "get switch config failed err %s aq_err %s\n",
14741 				 i40e_stat_str(&pf->hw, ret),
14742 				 i40e_aq_str(&pf->hw,
14743 					     pf->hw.aq.asq_last_status));
14744 			kfree(aq_buf);
14745 			return -ENOENT;
14746 		}
14747 
14748 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14749 		num_total = le16_to_cpu(sw_config->header.num_total);
14750 
14751 		if (printconfig)
14752 			dev_info(&pf->pdev->dev,
14753 				 "header: %d reported %d total\n",
14754 				 num_reported, num_total);
14755 
14756 		for (i = 0; i < num_reported; i++) {
14757 			struct i40e_aqc_switch_config_element_resp *ele =
14758 				&sw_config->element[i];
14759 
14760 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14761 						     printconfig);
14762 		}
14763 	} while (next_seid != 0);
14764 
14765 	kfree(aq_buf);
14766 	return ret;
14767 }
14768 
14769 /**
14770  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14771  * @pf: board private structure
14772  * @reinit: if the Main VSI needs to re-initialized.
14773  * @lock_acquired: indicates whether or not the lock has been acquired
14774  *
14775  * Returns 0 on success, negative value on failure
14776  **/
14777 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14778 {
14779 	u16 flags = 0;
14780 	int ret;
14781 
14782 	/* find out what's out there already */
14783 	ret = i40e_fetch_switch_configuration(pf, false);
14784 	if (ret) {
14785 		dev_info(&pf->pdev->dev,
14786 			 "couldn't fetch switch config, err %s aq_err %s\n",
14787 			 i40e_stat_str(&pf->hw, ret),
14788 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14789 		return ret;
14790 	}
14791 	i40e_pf_reset_stats(pf);
14792 
14793 	/* set the switch config bit for the whole device to
14794 	 * support limited promisc or true promisc
14795 	 * when user requests promisc. The default is limited
14796 	 * promisc.
14797 	*/
14798 
14799 	if ((pf->hw.pf_id == 0) &&
14800 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
14801 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14802 		pf->last_sw_conf_flags = flags;
14803 	}
14804 
14805 	if (pf->hw.pf_id == 0) {
14806 		u16 valid_flags;
14807 
14808 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14809 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
14810 						NULL);
14811 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
14812 			dev_info(&pf->pdev->dev,
14813 				 "couldn't set switch config bits, err %s aq_err %s\n",
14814 				 i40e_stat_str(&pf->hw, ret),
14815 				 i40e_aq_str(&pf->hw,
14816 					     pf->hw.aq.asq_last_status));
14817 			/* not a fatal problem, just keep going */
14818 		}
14819 		pf->last_sw_conf_valid_flags = valid_flags;
14820 	}
14821 
14822 	/* first time setup */
14823 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
14824 		struct i40e_vsi *vsi = NULL;
14825 		u16 uplink_seid;
14826 
14827 		/* Set up the PF VSI associated with the PF's main VSI
14828 		 * that is already in the HW switch
14829 		 */
14830 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
14831 			uplink_seid = pf->veb[pf->lan_veb]->seid;
14832 		else
14833 			uplink_seid = pf->mac_seid;
14834 		if (pf->lan_vsi == I40E_NO_VSI)
14835 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
14836 		else if (reinit)
14837 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
14838 		if (!vsi) {
14839 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
14840 			i40e_cloud_filter_exit(pf);
14841 			i40e_fdir_teardown(pf);
14842 			return -EAGAIN;
14843 		}
14844 	} else {
14845 		/* force a reset of TC and queue layout configurations */
14846 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14847 
14848 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14849 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14850 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14851 	}
14852 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
14853 
14854 	i40e_fdir_sb_setup(pf);
14855 
14856 	/* Setup static PF queue filter control settings */
14857 	ret = i40e_setup_pf_filter_control(pf);
14858 	if (ret) {
14859 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
14860 			 ret);
14861 		/* Failure here should not stop continuing other steps */
14862 	}
14863 
14864 	/* enable RSS in the HW, even for only one queue, as the stack can use
14865 	 * the hash
14866 	 */
14867 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
14868 		i40e_pf_config_rss(pf);
14869 
14870 	/* fill in link information and enable LSE reporting */
14871 	i40e_link_event(pf);
14872 
14873 	/* Initialize user-specific link properties */
14874 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
14875 				  I40E_AQ_AN_COMPLETED) ? true : false);
14876 
14877 	i40e_ptp_init(pf);
14878 
14879 	if (!lock_acquired)
14880 		rtnl_lock();
14881 
14882 	/* repopulate tunnel port filters */
14883 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
14884 
14885 	if (!lock_acquired)
14886 		rtnl_unlock();
14887 
14888 	return ret;
14889 }
14890 
14891 /**
14892  * i40e_determine_queue_usage - Work out queue distribution
14893  * @pf: board private structure
14894  **/
14895 static void i40e_determine_queue_usage(struct i40e_pf *pf)
14896 {
14897 	int queues_left;
14898 	int q_max;
14899 
14900 	pf->num_lan_qps = 0;
14901 
14902 	/* Find the max queues to be put into basic use.  We'll always be
14903 	 * using TC0, whether or not DCB is running, and TC0 will get the
14904 	 * big RSS set.
14905 	 */
14906 	queues_left = pf->hw.func_caps.num_tx_qp;
14907 
14908 	if ((queues_left == 1) ||
14909 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
14910 		/* one qp for PF, no queues for anything else */
14911 		queues_left = 0;
14912 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14913 
14914 		/* make sure all the fancies are disabled */
14915 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14916 			       I40E_FLAG_IWARP_ENABLED	|
14917 			       I40E_FLAG_FD_SB_ENABLED	|
14918 			       I40E_FLAG_FD_ATR_ENABLED	|
14919 			       I40E_FLAG_DCB_CAPABLE	|
14920 			       I40E_FLAG_DCB_ENABLED	|
14921 			       I40E_FLAG_SRIOV_ENABLED	|
14922 			       I40E_FLAG_VMDQ_ENABLED);
14923 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14924 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
14925 				  I40E_FLAG_FD_SB_ENABLED |
14926 				  I40E_FLAG_FD_ATR_ENABLED |
14927 				  I40E_FLAG_DCB_CAPABLE))) {
14928 		/* one qp for PF */
14929 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14930 		queues_left -= pf->num_lan_qps;
14931 
14932 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14933 			       I40E_FLAG_IWARP_ENABLED	|
14934 			       I40E_FLAG_FD_SB_ENABLED	|
14935 			       I40E_FLAG_FD_ATR_ENABLED	|
14936 			       I40E_FLAG_DCB_ENABLED	|
14937 			       I40E_FLAG_VMDQ_ENABLED);
14938 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14939 	} else {
14940 		/* Not enough queues for all TCs */
14941 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
14942 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
14943 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
14944 					I40E_FLAG_DCB_ENABLED);
14945 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
14946 		}
14947 
14948 		/* limit lan qps to the smaller of qps, cpus or msix */
14949 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
14950 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
14951 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
14952 		pf->num_lan_qps = q_max;
14953 
14954 		queues_left -= pf->num_lan_qps;
14955 	}
14956 
14957 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14958 		if (queues_left > 1) {
14959 			queues_left -= 1; /* save 1 queue for FD */
14960 		} else {
14961 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
14962 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14963 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
14964 		}
14965 	}
14966 
14967 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
14968 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
14969 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
14970 					(queues_left / pf->num_vf_qps));
14971 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
14972 	}
14973 
14974 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
14975 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
14976 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
14977 					  (queues_left / pf->num_vmdq_qps));
14978 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
14979 	}
14980 
14981 	pf->queues_left = queues_left;
14982 	dev_dbg(&pf->pdev->dev,
14983 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
14984 		pf->hw.func_caps.num_tx_qp,
14985 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
14986 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
14987 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
14988 		queues_left);
14989 }
14990 
14991 /**
14992  * i40e_setup_pf_filter_control - Setup PF static filter control
14993  * @pf: PF to be setup
14994  *
14995  * i40e_setup_pf_filter_control sets up a PF's initial filter control
14996  * settings. If PE/FCoE are enabled then it will also set the per PF
14997  * based filter sizes required for them. It also enables Flow director,
14998  * ethertype and macvlan type filter settings for the pf.
14999  *
15000  * Returns 0 on success, negative on failure
15001  **/
15002 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15003 {
15004 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15005 
15006 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15007 
15008 	/* Flow Director is enabled */
15009 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
15010 		settings->enable_fdir = true;
15011 
15012 	/* Ethtype and MACVLAN filters enabled for PF */
15013 	settings->enable_ethtype = true;
15014 	settings->enable_macvlan = true;
15015 
15016 	if (i40e_set_filter_control(&pf->hw, settings))
15017 		return -ENOENT;
15018 
15019 	return 0;
15020 }
15021 
15022 #define INFO_STRING_LEN 255
15023 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15024 static void i40e_print_features(struct i40e_pf *pf)
15025 {
15026 	struct i40e_hw *hw = &pf->hw;
15027 	char *buf;
15028 	int i;
15029 
15030 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15031 	if (!buf)
15032 		return;
15033 
15034 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15035 #ifdef CONFIG_PCI_IOV
15036 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15037 #endif
15038 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15039 		      pf->hw.func_caps.num_vsis,
15040 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15041 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15042 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15043 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15044 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15045 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15046 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15047 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15048 	}
15049 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15050 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15051 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15052 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15053 	if (pf->flags & I40E_FLAG_PTP)
15054 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15055 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15056 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15057 	else
15058 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15059 
15060 	dev_info(&pf->pdev->dev, "%s\n", buf);
15061 	kfree(buf);
15062 	WARN_ON(i > INFO_STRING_LEN);
15063 }
15064 
15065 /**
15066  * i40e_get_platform_mac_addr - get platform-specific MAC address
15067  * @pdev: PCI device information struct
15068  * @pf: board private structure
15069  *
15070  * Look up the MAC address for the device. First we'll try
15071  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15072  * specific fallback. Otherwise, we'll default to the stored value in
15073  * firmware.
15074  **/
15075 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15076 {
15077 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15078 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15079 }
15080 
15081 /**
15082  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15083  * @fec_cfg: FEC option to set in flags
15084  * @flags: ptr to flags in which we set FEC option
15085  **/
15086 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15087 {
15088 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15089 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15090 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15091 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15092 		*flags |= I40E_FLAG_RS_FEC;
15093 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15094 	}
15095 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15096 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15097 		*flags |= I40E_FLAG_BASE_R_FEC;
15098 		*flags &= ~I40E_FLAG_RS_FEC;
15099 	}
15100 	if (fec_cfg == 0)
15101 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15102 }
15103 
15104 /**
15105  * i40e_check_recovery_mode - check if we are running transition firmware
15106  * @pf: board private structure
15107  *
15108  * Check registers indicating the firmware runs in recovery mode. Sets the
15109  * appropriate driver state.
15110  *
15111  * Returns true if the recovery mode was detected, false otherwise
15112  **/
15113 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15114 {
15115 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15116 
15117 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15118 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15119 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15120 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15121 
15122 		return true;
15123 	}
15124 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15125 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15126 
15127 	return false;
15128 }
15129 
15130 /**
15131  * i40e_pf_loop_reset - perform reset in a loop.
15132  * @pf: board private structure
15133  *
15134  * This function is useful when a NIC is about to enter recovery mode.
15135  * When a NIC's internal data structures are corrupted the NIC's
15136  * firmware is going to enter recovery mode.
15137  * Right after a POR it takes about 7 minutes for firmware to enter
15138  * recovery mode. Until that time a NIC is in some kind of intermediate
15139  * state. After that time period the NIC almost surely enters
15140  * recovery mode. The only way for a driver to detect intermediate
15141  * state is to issue a series of pf-resets and check a return value.
15142  * If a PF reset returns success then the firmware could be in recovery
15143  * mode so the caller of this code needs to check for recovery mode
15144  * if this function returns success. There is a little chance that
15145  * firmware will hang in intermediate state forever.
15146  * Since waiting 7 minutes is quite a lot of time this function waits
15147  * 10 seconds and then gives up by returning an error.
15148  *
15149  * Return 0 on success, negative on failure.
15150  **/
15151 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
15152 {
15153 	/* wait max 10 seconds for PF reset to succeed */
15154 	const unsigned long time_end = jiffies + 10 * HZ;
15155 
15156 	struct i40e_hw *hw = &pf->hw;
15157 	i40e_status ret;
15158 
15159 	ret = i40e_pf_reset(hw);
15160 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15161 		usleep_range(10000, 20000);
15162 		ret = i40e_pf_reset(hw);
15163 	}
15164 
15165 	if (ret == I40E_SUCCESS)
15166 		pf->pfr_count++;
15167 	else
15168 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15169 
15170 	return ret;
15171 }
15172 
15173 /**
15174  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15175  * @pf: board private structure
15176  *
15177  * Check FW registers to determine if FW issued unexpected EMP Reset.
15178  * Every time when unexpected EMP Reset occurs the FW increments
15179  * a counter of unexpected EMP Resets. When the counter reaches 10
15180  * the FW should enter the Recovery mode
15181  *
15182  * Returns true if FW issued unexpected EMP Reset
15183  **/
15184 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15185 {
15186 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15187 			   I40E_GL_FWSTS_FWS1B_MASK;
15188 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15189 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15190 }
15191 
15192 /**
15193  * i40e_handle_resets - handle EMP resets and PF resets
15194  * @pf: board private structure
15195  *
15196  * Handle both EMP resets and PF resets and conclude whether there are
15197  * any issues regarding these resets. If there are any issues then
15198  * generate log entry.
15199  *
15200  * Return 0 if NIC is healthy or negative value when there are issues
15201  * with resets
15202  **/
15203 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
15204 {
15205 	const i40e_status pfr = i40e_pf_loop_reset(pf);
15206 	const bool is_empr = i40e_check_fw_empr(pf);
15207 
15208 	if (is_empr || pfr != I40E_SUCCESS)
15209 		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");
15210 
15211 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15212 }
15213 
15214 /**
15215  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15216  * @pf: board private structure
15217  * @hw: ptr to the hardware info
15218  *
15219  * This function does a minimal setup of all subsystems needed for running
15220  * recovery mode.
15221  *
15222  * Returns 0 on success, negative on failure
15223  **/
15224 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15225 {
15226 	struct i40e_vsi *vsi;
15227 	int err;
15228 	int v_idx;
15229 
15230 	pci_save_state(pf->pdev);
15231 
15232 	/* set up periodic task facility */
15233 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15234 	pf->service_timer_period = HZ;
15235 
15236 	INIT_WORK(&pf->service_task, i40e_service_task);
15237 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15238 
15239 	err = i40e_init_interrupt_scheme(pf);
15240 	if (err)
15241 		goto err_switch_setup;
15242 
15243 	/* The number of VSIs reported by the FW is the minimum guaranteed
15244 	 * to us; HW supports far more and we share the remaining pool with
15245 	 * the other PFs. We allocate space for more than the guarantee with
15246 	 * the understanding that we might not get them all later.
15247 	 */
15248 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15249 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15250 	else
15251 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15252 
15253 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15254 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15255 			  GFP_KERNEL);
15256 	if (!pf->vsi) {
15257 		err = -ENOMEM;
15258 		goto err_switch_setup;
15259 	}
15260 
15261 	/* We allocate one VSI which is needed as absolute minimum
15262 	 * in order to register the netdev
15263 	 */
15264 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15265 	if (v_idx < 0) {
15266 		err = v_idx;
15267 		goto err_switch_setup;
15268 	}
15269 	pf->lan_vsi = v_idx;
15270 	vsi = pf->vsi[v_idx];
15271 	if (!vsi) {
15272 		err = -EFAULT;
15273 		goto err_switch_setup;
15274 	}
15275 	vsi->alloc_queue_pairs = 1;
15276 	err = i40e_config_netdev(vsi);
15277 	if (err)
15278 		goto err_switch_setup;
15279 	err = register_netdev(vsi->netdev);
15280 	if (err)
15281 		goto err_switch_setup;
15282 	vsi->netdev_registered = true;
15283 	i40e_dbg_pf_init(pf);
15284 
15285 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15286 	if (err)
15287 		goto err_switch_setup;
15288 
15289 	/* tell the firmware that we're starting */
15290 	i40e_send_version(pf);
15291 
15292 	/* since everything's happy, start the service_task timer */
15293 	mod_timer(&pf->service_timer,
15294 		  round_jiffies(jiffies + pf->service_timer_period));
15295 
15296 	return 0;
15297 
15298 err_switch_setup:
15299 	i40e_reset_interrupt_capability(pf);
15300 	del_timer_sync(&pf->service_timer);
15301 	i40e_shutdown_adminq(hw);
15302 	iounmap(hw->hw_addr);
15303 	pci_disable_pcie_error_reporting(pf->pdev);
15304 	pci_release_mem_regions(pf->pdev);
15305 	pci_disable_device(pf->pdev);
15306 	kfree(pf);
15307 
15308 	return err;
15309 }
15310 
15311 /**
15312  * i40e_set_subsystem_device_id - set subsystem device id
15313  * @hw: pointer to the hardware info
15314  *
15315  * Set PCI subsystem device id either from a pci_dev structure or
15316  * a specific FW register.
15317  **/
15318 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15319 {
15320 	struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15321 
15322 	hw->subsystem_device_id = pdev->subsystem_device ?
15323 		pdev->subsystem_device :
15324 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15325 }
15326 
15327 /**
15328  * i40e_probe - Device initialization routine
15329  * @pdev: PCI device information struct
15330  * @ent: entry in i40e_pci_tbl
15331  *
15332  * i40e_probe initializes a PF identified by a pci_dev structure.
15333  * The OS initialization, configuring of the PF private structure,
15334  * and a hardware reset occur.
15335  *
15336  * Returns 0 on success, negative on failure
15337  **/
15338 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15339 {
15340 	struct i40e_aq_get_phy_abilities_resp abilities;
15341 #ifdef CONFIG_I40E_DCB
15342 	enum i40e_get_fw_lldp_status_resp lldp_status;
15343 	i40e_status status;
15344 #endif /* CONFIG_I40E_DCB */
15345 	struct i40e_pf *pf;
15346 	struct i40e_hw *hw;
15347 	static u16 pfs_found;
15348 	u16 wol_nvm_bits;
15349 	u16 link_status;
15350 	int err;
15351 	u32 val;
15352 	u32 i;
15353 
15354 	err = pci_enable_device_mem(pdev);
15355 	if (err)
15356 		return err;
15357 
15358 	/* set up for high or low dma */
15359 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15360 	if (err) {
15361 		dev_err(&pdev->dev,
15362 			"DMA configuration failed: 0x%x\n", err);
15363 		goto err_dma;
15364 	}
15365 
15366 	/* set up pci connections */
15367 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15368 	if (err) {
15369 		dev_info(&pdev->dev,
15370 			 "pci_request_selected_regions failed %d\n", err);
15371 		goto err_pci_reg;
15372 	}
15373 
15374 	pci_enable_pcie_error_reporting(pdev);
15375 	pci_set_master(pdev);
15376 
15377 	/* Now that we have a PCI connection, we need to do the
15378 	 * low level device setup.  This is primarily setting up
15379 	 * the Admin Queue structures and then querying for the
15380 	 * device's current profile information.
15381 	 */
15382 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15383 	if (!pf) {
15384 		err = -ENOMEM;
15385 		goto err_pf_alloc;
15386 	}
15387 	pf->next_vsi = 0;
15388 	pf->pdev = pdev;
15389 	set_bit(__I40E_DOWN, pf->state);
15390 
15391 	hw = &pf->hw;
15392 	hw->back = pf;
15393 
15394 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15395 				I40E_MAX_CSR_SPACE);
15396 	/* We believe that the highest register to read is
15397 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15398 	 * is not less than that before mapping to prevent a
15399 	 * kernel panic.
15400 	 */
15401 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15402 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15403 			pf->ioremap_len);
15404 		err = -ENOMEM;
15405 		goto err_ioremap;
15406 	}
15407 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15408 	if (!hw->hw_addr) {
15409 		err = -EIO;
15410 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15411 			 (unsigned int)pci_resource_start(pdev, 0),
15412 			 pf->ioremap_len, err);
15413 		goto err_ioremap;
15414 	}
15415 	hw->vendor_id = pdev->vendor;
15416 	hw->device_id = pdev->device;
15417 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15418 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15419 	i40e_set_subsystem_device_id(hw);
15420 	hw->bus.device = PCI_SLOT(pdev->devfn);
15421 	hw->bus.func = PCI_FUNC(pdev->devfn);
15422 	hw->bus.bus_id = pdev->bus->number;
15423 	pf->instance = pfs_found;
15424 
15425 	/* Select something other than the 802.1ad ethertype for the
15426 	 * switch to use internally and drop on ingress.
15427 	 */
15428 	hw->switch_tag = 0xffff;
15429 	hw->first_tag = ETH_P_8021AD;
15430 	hw->second_tag = ETH_P_8021Q;
15431 
15432 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15433 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15434 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15435 
15436 	/* set up the locks for the AQ, do this only once in probe
15437 	 * and destroy them only once in remove
15438 	 */
15439 	mutex_init(&hw->aq.asq_mutex);
15440 	mutex_init(&hw->aq.arq_mutex);
15441 
15442 	pf->msg_enable = netif_msg_init(debug,
15443 					NETIF_MSG_DRV |
15444 					NETIF_MSG_PROBE |
15445 					NETIF_MSG_LINK);
15446 	if (debug < -1)
15447 		pf->hw.debug_mask = debug;
15448 
15449 	/* do a special CORER for clearing PXE mode once at init */
15450 	if (hw->revision_id == 0 &&
15451 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15452 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15453 		i40e_flush(hw);
15454 		msleep(200);
15455 		pf->corer_count++;
15456 
15457 		i40e_clear_pxe_mode(hw);
15458 	}
15459 
15460 	/* Reset here to make sure all is clean and to define PF 'n' */
15461 	i40e_clear_hw(hw);
15462 
15463 	err = i40e_set_mac_type(hw);
15464 	if (err) {
15465 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15466 			 err);
15467 		goto err_pf_reset;
15468 	}
15469 
15470 	err = i40e_handle_resets(pf);
15471 	if (err)
15472 		goto err_pf_reset;
15473 
15474 	i40e_check_recovery_mode(pf);
15475 
15476 	if (is_kdump_kernel()) {
15477 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15478 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15479 	} else {
15480 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15481 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15482 	}
15483 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15484 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15485 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15486 
15487 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15488 		 "%s-%s:misc",
15489 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15490 
15491 	err = i40e_init_shared_code(hw);
15492 	if (err) {
15493 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15494 			 err);
15495 		goto err_pf_reset;
15496 	}
15497 
15498 	/* set up a default setting for link flow control */
15499 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15500 
15501 	err = i40e_init_adminq(hw);
15502 	if (err) {
15503 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15504 			dev_info(&pdev->dev,
15505 				 "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",
15506 				 hw->aq.api_maj_ver,
15507 				 hw->aq.api_min_ver,
15508 				 I40E_FW_API_VERSION_MAJOR,
15509 				 I40E_FW_MINOR_VERSION(hw));
15510 		else
15511 			dev_info(&pdev->dev,
15512 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15513 
15514 		goto err_pf_reset;
15515 	}
15516 	i40e_get_oem_version(hw);
15517 
15518 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15519 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15520 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15521 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15522 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15523 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15524 
15525 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15526 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15527 		dev_dbg(&pdev->dev,
15528 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15529 			 hw->aq.api_maj_ver,
15530 			 hw->aq.api_min_ver,
15531 			 I40E_FW_API_VERSION_MAJOR,
15532 			 I40E_FW_MINOR_VERSION(hw));
15533 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15534 		dev_info(&pdev->dev,
15535 			 "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",
15536 			 hw->aq.api_maj_ver,
15537 			 hw->aq.api_min_ver,
15538 			 I40E_FW_API_VERSION_MAJOR,
15539 			 I40E_FW_MINOR_VERSION(hw));
15540 
15541 	i40e_verify_eeprom(pf);
15542 
15543 	/* Rev 0 hardware was never productized */
15544 	if (hw->revision_id < 1)
15545 		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");
15546 
15547 	i40e_clear_pxe_mode(hw);
15548 
15549 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15550 	if (err)
15551 		goto err_adminq_setup;
15552 
15553 	err = i40e_sw_init(pf);
15554 	if (err) {
15555 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15556 		goto err_sw_init;
15557 	}
15558 
15559 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15560 		return i40e_init_recovery_mode(pf, hw);
15561 
15562 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15563 				hw->func_caps.num_rx_qp, 0, 0);
15564 	if (err) {
15565 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15566 		goto err_init_lan_hmc;
15567 	}
15568 
15569 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15570 	if (err) {
15571 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15572 		err = -ENOENT;
15573 		goto err_configure_lan_hmc;
15574 	}
15575 
15576 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15577 	 * Ignore error return codes because if it was already disabled via
15578 	 * hardware settings this will fail
15579 	 */
15580 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15581 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15582 		i40e_aq_stop_lldp(hw, true, false, NULL);
15583 	}
15584 
15585 	/* allow a platform config to override the HW addr */
15586 	i40e_get_platform_mac_addr(pdev, pf);
15587 
15588 	if (!is_valid_ether_addr(hw->mac.addr)) {
15589 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15590 		err = -EIO;
15591 		goto err_mac_addr;
15592 	}
15593 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15594 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15595 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15596 	if (is_valid_ether_addr(hw->mac.port_addr))
15597 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15598 
15599 	i40e_ptp_alloc_pins(pf);
15600 	pci_set_drvdata(pdev, pf);
15601 	pci_save_state(pdev);
15602 
15603 #ifdef CONFIG_I40E_DCB
15604 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15605 	(!status &&
15606 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15607 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15608 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15609 	dev_info(&pdev->dev,
15610 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15611 			"FW LLDP is disabled\n" :
15612 			"FW LLDP is enabled\n");
15613 
15614 	/* Enable FW to write default DCB config on link-up */
15615 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15616 
15617 	err = i40e_init_pf_dcb(pf);
15618 	if (err) {
15619 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15620 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15621 		/* Continue without DCB enabled */
15622 	}
15623 #endif /* CONFIG_I40E_DCB */
15624 
15625 	/* set up periodic task facility */
15626 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15627 	pf->service_timer_period = HZ;
15628 
15629 	INIT_WORK(&pf->service_task, i40e_service_task);
15630 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15631 
15632 	/* NVM bit on means WoL disabled for the port */
15633 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15634 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15635 		pf->wol_en = false;
15636 	else
15637 		pf->wol_en = true;
15638 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15639 
15640 	/* set up the main switch operations */
15641 	i40e_determine_queue_usage(pf);
15642 	err = i40e_init_interrupt_scheme(pf);
15643 	if (err)
15644 		goto err_switch_setup;
15645 
15646 	/* Reduce Tx and Rx pairs for kdump
15647 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15648 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15649 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15650 	 */
15651 	if (is_kdump_kernel())
15652 		pf->num_lan_msix = 1;
15653 
15654 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15655 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15656 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15657 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15658 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15659 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15660 						    UDP_TUNNEL_TYPE_GENEVE;
15661 
15662 	/* The number of VSIs reported by the FW is the minimum guaranteed
15663 	 * to us; HW supports far more and we share the remaining pool with
15664 	 * the other PFs. We allocate space for more than the guarantee with
15665 	 * the understanding that we might not get them all later.
15666 	 */
15667 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15668 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15669 	else
15670 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15671 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15672 		dev_warn(&pf->pdev->dev,
15673 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15674 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15675 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15676 	}
15677 
15678 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15679 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15680 			  GFP_KERNEL);
15681 	if (!pf->vsi) {
15682 		err = -ENOMEM;
15683 		goto err_switch_setup;
15684 	}
15685 
15686 #ifdef CONFIG_PCI_IOV
15687 	/* prep for VF support */
15688 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15689 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15690 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15691 		if (pci_num_vf(pdev))
15692 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15693 	}
15694 #endif
15695 	err = i40e_setup_pf_switch(pf, false, false);
15696 	if (err) {
15697 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15698 		goto err_vsis;
15699 	}
15700 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15701 
15702 	/* if FDIR VSI was set up, start it now */
15703 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15704 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15705 			i40e_vsi_open(pf->vsi[i]);
15706 			break;
15707 		}
15708 	}
15709 
15710 	/* The driver only wants link up/down and module qualification
15711 	 * reports from firmware.  Note the negative logic.
15712 	 */
15713 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15714 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15715 					 I40E_AQ_EVENT_MEDIA_NA |
15716 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15717 	if (err)
15718 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15719 			 i40e_stat_str(&pf->hw, err),
15720 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15721 
15722 	/* Reconfigure hardware for allowing smaller MSS in the case
15723 	 * of TSO, so that we avoid the MDD being fired and causing
15724 	 * a reset in the case of small MSS+TSO.
15725 	 */
15726 	val = rd32(hw, I40E_REG_MSS);
15727 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15728 		val &= ~I40E_REG_MSS_MIN_MASK;
15729 		val |= I40E_64BYTE_MSS;
15730 		wr32(hw, I40E_REG_MSS, val);
15731 	}
15732 
15733 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15734 		msleep(75);
15735 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15736 		if (err)
15737 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15738 				 i40e_stat_str(&pf->hw, err),
15739 				 i40e_aq_str(&pf->hw,
15740 					     pf->hw.aq.asq_last_status));
15741 	}
15742 	/* The main driver is (mostly) up and happy. We need to set this state
15743 	 * before setting up the misc vector or we get a race and the vector
15744 	 * ends up disabled forever.
15745 	 */
15746 	clear_bit(__I40E_DOWN, pf->state);
15747 
15748 	/* In case of MSIX we are going to setup the misc vector right here
15749 	 * to handle admin queue events etc. In case of legacy and MSI
15750 	 * the misc functionality and queue processing is combined in
15751 	 * the same vector and that gets setup at open.
15752 	 */
15753 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15754 		err = i40e_setup_misc_vector(pf);
15755 		if (err) {
15756 			dev_info(&pdev->dev,
15757 				 "setup of misc vector failed: %d\n", err);
15758 			i40e_cloud_filter_exit(pf);
15759 			i40e_fdir_teardown(pf);
15760 			goto err_vsis;
15761 		}
15762 	}
15763 
15764 #ifdef CONFIG_PCI_IOV
15765 	/* prep for VF support */
15766 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15767 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15768 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15769 		/* disable link interrupts for VFs */
15770 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15771 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15772 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15773 		i40e_flush(hw);
15774 
15775 		if (pci_num_vf(pdev)) {
15776 			dev_info(&pdev->dev,
15777 				 "Active VFs found, allocating resources.\n");
15778 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15779 			if (err)
15780 				dev_info(&pdev->dev,
15781 					 "Error %d allocating resources for existing VFs\n",
15782 					 err);
15783 		}
15784 	}
15785 #endif /* CONFIG_PCI_IOV */
15786 
15787 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15788 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
15789 						      pf->num_iwarp_msix,
15790 						      I40E_IWARP_IRQ_PILE_ID);
15791 		if (pf->iwarp_base_vector < 0) {
15792 			dev_info(&pdev->dev,
15793 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
15794 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
15795 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
15796 		}
15797 	}
15798 
15799 	i40e_dbg_pf_init(pf);
15800 
15801 	/* tell the firmware that we're starting */
15802 	i40e_send_version(pf);
15803 
15804 	/* since everything's happy, start the service_task timer */
15805 	mod_timer(&pf->service_timer,
15806 		  round_jiffies(jiffies + pf->service_timer_period));
15807 
15808 	/* add this PF to client device list and launch a client service task */
15809 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15810 		err = i40e_lan_add_device(pf);
15811 		if (err)
15812 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
15813 				 err);
15814 	}
15815 
15816 #define PCI_SPEED_SIZE 8
15817 #define PCI_WIDTH_SIZE 8
15818 	/* Devices on the IOSF bus do not have this information
15819 	 * and will report PCI Gen 1 x 1 by default so don't bother
15820 	 * checking them.
15821 	 */
15822 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
15823 		char speed[PCI_SPEED_SIZE] = "Unknown";
15824 		char width[PCI_WIDTH_SIZE] = "Unknown";
15825 
15826 		/* Get the negotiated link width and speed from PCI config
15827 		 * space
15828 		 */
15829 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
15830 					  &link_status);
15831 
15832 		i40e_set_pci_config_data(hw, link_status);
15833 
15834 		switch (hw->bus.speed) {
15835 		case i40e_bus_speed_8000:
15836 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
15837 		case i40e_bus_speed_5000:
15838 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
15839 		case i40e_bus_speed_2500:
15840 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
15841 		default:
15842 			break;
15843 		}
15844 		switch (hw->bus.width) {
15845 		case i40e_bus_width_pcie_x8:
15846 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
15847 		case i40e_bus_width_pcie_x4:
15848 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
15849 		case i40e_bus_width_pcie_x2:
15850 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
15851 		case i40e_bus_width_pcie_x1:
15852 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
15853 		default:
15854 			break;
15855 		}
15856 
15857 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
15858 			 speed, width);
15859 
15860 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
15861 		    hw->bus.speed < i40e_bus_speed_8000) {
15862 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
15863 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
15864 		}
15865 	}
15866 
15867 	/* get the requested speeds from the fw */
15868 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
15869 	if (err)
15870 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
15871 			i40e_stat_str(&pf->hw, err),
15872 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15873 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
15874 
15875 	/* set the FEC config due to the board capabilities */
15876 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
15877 
15878 	/* get the supported phy types from the fw */
15879 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
15880 	if (err)
15881 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
15882 			i40e_stat_str(&pf->hw, err),
15883 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15884 
15885 	/* make sure the MFS hasn't been set lower than the default */
15886 #define MAX_FRAME_SIZE_DEFAULT 0x2600
15887 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
15888 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
15889 	if (val < MAX_FRAME_SIZE_DEFAULT)
15890 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
15891 			 i, val);
15892 
15893 	/* Add a filter to drop all Flow control frames from any VSI from being
15894 	 * transmitted. By doing so we stop a malicious VF from sending out
15895 	 * PAUSE or PFC frames and potentially controlling traffic for other
15896 	 * PF/VF VSIs.
15897 	 * The FW can still send Flow control frames if enabled.
15898 	 */
15899 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
15900 						       pf->main_vsi_seid);
15901 
15902 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
15903 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
15904 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
15905 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
15906 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
15907 	/* print a string summarizing features */
15908 	i40e_print_features(pf);
15909 
15910 	return 0;
15911 
15912 	/* Unwind what we've done if something failed in the setup */
15913 err_vsis:
15914 	set_bit(__I40E_DOWN, pf->state);
15915 	i40e_clear_interrupt_scheme(pf);
15916 	kfree(pf->vsi);
15917 err_switch_setup:
15918 	i40e_reset_interrupt_capability(pf);
15919 	del_timer_sync(&pf->service_timer);
15920 err_mac_addr:
15921 err_configure_lan_hmc:
15922 	(void)i40e_shutdown_lan_hmc(hw);
15923 err_init_lan_hmc:
15924 	kfree(pf->qp_pile);
15925 err_sw_init:
15926 err_adminq_setup:
15927 err_pf_reset:
15928 	iounmap(hw->hw_addr);
15929 err_ioremap:
15930 	kfree(pf);
15931 err_pf_alloc:
15932 	pci_disable_pcie_error_reporting(pdev);
15933 	pci_release_mem_regions(pdev);
15934 err_pci_reg:
15935 err_dma:
15936 	pci_disable_device(pdev);
15937 	return err;
15938 }
15939 
15940 /**
15941  * i40e_remove - Device removal routine
15942  * @pdev: PCI device information struct
15943  *
15944  * i40e_remove is called by the PCI subsystem to alert the driver
15945  * that is should release a PCI device.  This could be caused by a
15946  * Hot-Plug event, or because the driver is going to be removed from
15947  * memory.
15948  **/
15949 static void i40e_remove(struct pci_dev *pdev)
15950 {
15951 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15952 	struct i40e_hw *hw = &pf->hw;
15953 	i40e_status ret_code;
15954 	int i;
15955 
15956 	i40e_dbg_pf_exit(pf);
15957 
15958 	i40e_ptp_stop(pf);
15959 
15960 	/* Disable RSS in hw */
15961 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
15962 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
15963 
15964 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
15965 	 * flags, once they are set, i40e_rebuild should not be called as
15966 	 * i40e_prep_for_reset always returns early.
15967 	 */
15968 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
15969 		usleep_range(1000, 2000);
15970 	set_bit(__I40E_IN_REMOVE, pf->state);
15971 
15972 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
15973 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
15974 		i40e_free_vfs(pf);
15975 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
15976 	}
15977 	/* no more scheduling of any task */
15978 	set_bit(__I40E_SUSPENDED, pf->state);
15979 	set_bit(__I40E_DOWN, pf->state);
15980 	if (pf->service_timer.function)
15981 		del_timer_sync(&pf->service_timer);
15982 	if (pf->service_task.func)
15983 		cancel_work_sync(&pf->service_task);
15984 
15985 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
15986 		struct i40e_vsi *vsi = pf->vsi[0];
15987 
15988 		/* We know that we have allocated only one vsi for this PF,
15989 		 * it was just for registering netdevice, so the interface
15990 		 * could be visible in the 'ifconfig' output
15991 		 */
15992 		unregister_netdev(vsi->netdev);
15993 		free_netdev(vsi->netdev);
15994 
15995 		goto unmap;
15996 	}
15997 
15998 	/* Client close must be called explicitly here because the timer
15999 	 * has been stopped.
16000 	 */
16001 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16002 
16003 	i40e_fdir_teardown(pf);
16004 
16005 	/* If there is a switch structure or any orphans, remove them.
16006 	 * This will leave only the PF's VSI remaining.
16007 	 */
16008 	for (i = 0; i < I40E_MAX_VEB; i++) {
16009 		if (!pf->veb[i])
16010 			continue;
16011 
16012 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16013 		    pf->veb[i]->uplink_seid == 0)
16014 			i40e_switch_branch_release(pf->veb[i]);
16015 	}
16016 
16017 	/* Now we can shutdown the PF's VSI, just before we kill
16018 	 * adminq and hmc.
16019 	 */
16020 	if (pf->vsi[pf->lan_vsi])
16021 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
16022 
16023 	i40e_cloud_filter_exit(pf);
16024 
16025 	/* remove attached clients */
16026 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16027 		ret_code = i40e_lan_del_device(pf);
16028 		if (ret_code)
16029 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16030 				 ret_code);
16031 	}
16032 
16033 	/* shutdown and destroy the HMC */
16034 	if (hw->hmc.hmc_obj) {
16035 		ret_code = i40e_shutdown_lan_hmc(hw);
16036 		if (ret_code)
16037 			dev_warn(&pdev->dev,
16038 				 "Failed to destroy the HMC resources: %d\n",
16039 				 ret_code);
16040 	}
16041 
16042 unmap:
16043 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16044 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16045 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16046 		free_irq(pf->pdev->irq, pf);
16047 
16048 	/* shutdown the adminq */
16049 	i40e_shutdown_adminq(hw);
16050 
16051 	/* destroy the locks only once, here */
16052 	mutex_destroy(&hw->aq.arq_mutex);
16053 	mutex_destroy(&hw->aq.asq_mutex);
16054 
16055 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16056 	rtnl_lock();
16057 	i40e_clear_interrupt_scheme(pf);
16058 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16059 		if (pf->vsi[i]) {
16060 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16061 				i40e_vsi_clear_rings(pf->vsi[i]);
16062 			i40e_vsi_clear(pf->vsi[i]);
16063 			pf->vsi[i] = NULL;
16064 		}
16065 	}
16066 	rtnl_unlock();
16067 
16068 	for (i = 0; i < I40E_MAX_VEB; i++) {
16069 		kfree(pf->veb[i]);
16070 		pf->veb[i] = NULL;
16071 	}
16072 
16073 	kfree(pf->qp_pile);
16074 	kfree(pf->vsi);
16075 
16076 	iounmap(hw->hw_addr);
16077 	kfree(pf);
16078 	pci_release_mem_regions(pdev);
16079 
16080 	pci_disable_pcie_error_reporting(pdev);
16081 	pci_disable_device(pdev);
16082 }
16083 
16084 /**
16085  * i40e_pci_error_detected - warning that something funky happened in PCI land
16086  * @pdev: PCI device information struct
16087  * @error: the type of PCI error
16088  *
16089  * Called to warn that something happened and the error handling steps
16090  * are in progress.  Allows the driver to quiesce things, be ready for
16091  * remediation.
16092  **/
16093 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16094 						pci_channel_state_t error)
16095 {
16096 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16097 
16098 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16099 
16100 	if (!pf) {
16101 		dev_info(&pdev->dev,
16102 			 "Cannot recover - error happened during device probe\n");
16103 		return PCI_ERS_RESULT_DISCONNECT;
16104 	}
16105 
16106 	/* shutdown all operations */
16107 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16108 		i40e_prep_for_reset(pf);
16109 
16110 	/* Request a slot reset */
16111 	return PCI_ERS_RESULT_NEED_RESET;
16112 }
16113 
16114 /**
16115  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16116  * @pdev: PCI device information struct
16117  *
16118  * Called to find if the driver can work with the device now that
16119  * the pci slot has been reset.  If a basic connection seems good
16120  * (registers are readable and have sane content) then return a
16121  * happy little PCI_ERS_RESULT_xxx.
16122  **/
16123 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16124 {
16125 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16126 	pci_ers_result_t result;
16127 	u32 reg;
16128 
16129 	dev_dbg(&pdev->dev, "%s\n", __func__);
16130 	if (pci_enable_device_mem(pdev)) {
16131 		dev_info(&pdev->dev,
16132 			 "Cannot re-enable PCI device after reset.\n");
16133 		result = PCI_ERS_RESULT_DISCONNECT;
16134 	} else {
16135 		pci_set_master(pdev);
16136 		pci_restore_state(pdev);
16137 		pci_save_state(pdev);
16138 		pci_wake_from_d3(pdev, false);
16139 
16140 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16141 		if (reg == 0)
16142 			result = PCI_ERS_RESULT_RECOVERED;
16143 		else
16144 			result = PCI_ERS_RESULT_DISCONNECT;
16145 	}
16146 
16147 	return result;
16148 }
16149 
16150 /**
16151  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16152  * @pdev: PCI device information struct
16153  */
16154 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16155 {
16156 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16157 
16158 	i40e_prep_for_reset(pf);
16159 }
16160 
16161 /**
16162  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16163  * @pdev: PCI device information struct
16164  */
16165 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16166 {
16167 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16168 
16169 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16170 		return;
16171 
16172 	i40e_reset_and_rebuild(pf, false, false);
16173 }
16174 
16175 /**
16176  * i40e_pci_error_resume - restart operations after PCI error recovery
16177  * @pdev: PCI device information struct
16178  *
16179  * Called to allow the driver to bring things back up after PCI error
16180  * and/or reset recovery has finished.
16181  **/
16182 static void i40e_pci_error_resume(struct pci_dev *pdev)
16183 {
16184 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16185 
16186 	dev_dbg(&pdev->dev, "%s\n", __func__);
16187 	if (test_bit(__I40E_SUSPENDED, pf->state))
16188 		return;
16189 
16190 	i40e_handle_reset_warning(pf, false);
16191 }
16192 
16193 /**
16194  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16195  * using the mac_address_write admin q function
16196  * @pf: pointer to i40e_pf struct
16197  **/
16198 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16199 {
16200 	struct i40e_hw *hw = &pf->hw;
16201 	i40e_status ret;
16202 	u8 mac_addr[6];
16203 	u16 flags = 0;
16204 
16205 	/* Get current MAC address in case it's an LAA */
16206 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16207 		ether_addr_copy(mac_addr,
16208 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16209 	} else {
16210 		dev_err(&pf->pdev->dev,
16211 			"Failed to retrieve MAC address; using default\n");
16212 		ether_addr_copy(mac_addr, hw->mac.addr);
16213 	}
16214 
16215 	/* The FW expects the mac address write cmd to first be called with
16216 	 * one of these flags before calling it again with the multicast
16217 	 * enable flags.
16218 	 */
16219 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16220 
16221 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16222 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16223 
16224 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16225 	if (ret) {
16226 		dev_err(&pf->pdev->dev,
16227 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16228 		return;
16229 	}
16230 
16231 	flags = I40E_AQC_MC_MAG_EN
16232 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16233 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16234 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16235 	if (ret)
16236 		dev_err(&pf->pdev->dev,
16237 			"Failed to enable Multicast Magic Packet wake up\n");
16238 }
16239 
16240 /**
16241  * i40e_shutdown - PCI callback for shutting down
16242  * @pdev: PCI device information struct
16243  **/
16244 static void i40e_shutdown(struct pci_dev *pdev)
16245 {
16246 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16247 	struct i40e_hw *hw = &pf->hw;
16248 
16249 	set_bit(__I40E_SUSPENDED, pf->state);
16250 	set_bit(__I40E_DOWN, pf->state);
16251 
16252 	del_timer_sync(&pf->service_timer);
16253 	cancel_work_sync(&pf->service_task);
16254 	i40e_cloud_filter_exit(pf);
16255 	i40e_fdir_teardown(pf);
16256 
16257 	/* Client close must be called explicitly here because the timer
16258 	 * has been stopped.
16259 	 */
16260 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16261 
16262 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16263 		i40e_enable_mc_magic_wake(pf);
16264 
16265 	i40e_prep_for_reset(pf);
16266 
16267 	wr32(hw, I40E_PFPM_APM,
16268 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16269 	wr32(hw, I40E_PFPM_WUFC,
16270 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16271 
16272 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16273 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16274 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16275 		free_irq(pf->pdev->irq, pf);
16276 
16277 	/* Since we're going to destroy queues during the
16278 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16279 	 * whole section
16280 	 */
16281 	rtnl_lock();
16282 	i40e_clear_interrupt_scheme(pf);
16283 	rtnl_unlock();
16284 
16285 	if (system_state == SYSTEM_POWER_OFF) {
16286 		pci_wake_from_d3(pdev, pf->wol_en);
16287 		pci_set_power_state(pdev, PCI_D3hot);
16288 	}
16289 }
16290 
16291 /**
16292  * i40e_suspend - PM callback for moving to D3
16293  * @dev: generic device information structure
16294  **/
16295 static int __maybe_unused i40e_suspend(struct device *dev)
16296 {
16297 	struct i40e_pf *pf = dev_get_drvdata(dev);
16298 	struct i40e_hw *hw = &pf->hw;
16299 
16300 	/* If we're already suspended, then there is nothing to do */
16301 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16302 		return 0;
16303 
16304 	set_bit(__I40E_DOWN, pf->state);
16305 
16306 	/* Ensure service task will not be running */
16307 	del_timer_sync(&pf->service_timer);
16308 	cancel_work_sync(&pf->service_task);
16309 
16310 	/* Client close must be called explicitly here because the timer
16311 	 * has been stopped.
16312 	 */
16313 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16314 
16315 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16316 		i40e_enable_mc_magic_wake(pf);
16317 
16318 	/* Since we're going to destroy queues during the
16319 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16320 	 * whole section
16321 	 */
16322 	rtnl_lock();
16323 
16324 	i40e_prep_for_reset(pf);
16325 
16326 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16327 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16328 
16329 	/* Clear the interrupt scheme and release our IRQs so that the system
16330 	 * can safely hibernate even when there are a large number of CPUs.
16331 	 * Otherwise hibernation might fail when mapping all the vectors back
16332 	 * to CPU0.
16333 	 */
16334 	i40e_clear_interrupt_scheme(pf);
16335 
16336 	rtnl_unlock();
16337 
16338 	return 0;
16339 }
16340 
16341 /**
16342  * i40e_resume - PM callback for waking up from D3
16343  * @dev: generic device information structure
16344  **/
16345 static int __maybe_unused i40e_resume(struct device *dev)
16346 {
16347 	struct i40e_pf *pf = dev_get_drvdata(dev);
16348 	int err;
16349 
16350 	/* If we're not suspended, then there is nothing to do */
16351 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16352 		return 0;
16353 
16354 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16355 	 * since we're going to be restoring queues
16356 	 */
16357 	rtnl_lock();
16358 
16359 	/* We cleared the interrupt scheme when we suspended, so we need to
16360 	 * restore it now to resume device functionality.
16361 	 */
16362 	err = i40e_restore_interrupt_scheme(pf);
16363 	if (err) {
16364 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16365 			err);
16366 	}
16367 
16368 	clear_bit(__I40E_DOWN, pf->state);
16369 	i40e_reset_and_rebuild(pf, false, true);
16370 
16371 	rtnl_unlock();
16372 
16373 	/* Clear suspended state last after everything is recovered */
16374 	clear_bit(__I40E_SUSPENDED, pf->state);
16375 
16376 	/* Restart the service task */
16377 	mod_timer(&pf->service_timer,
16378 		  round_jiffies(jiffies + pf->service_timer_period));
16379 
16380 	return 0;
16381 }
16382 
16383 static const struct pci_error_handlers i40e_err_handler = {
16384 	.error_detected = i40e_pci_error_detected,
16385 	.slot_reset = i40e_pci_error_slot_reset,
16386 	.reset_prepare = i40e_pci_error_reset_prepare,
16387 	.reset_done = i40e_pci_error_reset_done,
16388 	.resume = i40e_pci_error_resume,
16389 };
16390 
16391 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16392 
16393 static struct pci_driver i40e_driver = {
16394 	.name     = i40e_driver_name,
16395 	.id_table = i40e_pci_tbl,
16396 	.probe    = i40e_probe,
16397 	.remove   = i40e_remove,
16398 	.driver   = {
16399 		.pm = &i40e_pm_ops,
16400 	},
16401 	.shutdown = i40e_shutdown,
16402 	.err_handler = &i40e_err_handler,
16403 	.sriov_configure = i40e_pci_sriov_configure,
16404 };
16405 
16406 /**
16407  * i40e_init_module - Driver registration routine
16408  *
16409  * i40e_init_module is the first routine called when the driver is
16410  * loaded. All it does is register with the PCI subsystem.
16411  **/
16412 static int __init i40e_init_module(void)
16413 {
16414 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16415 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16416 
16417 	/* There is no need to throttle the number of active tasks because
16418 	 * each device limits its own task using a state bit for scheduling
16419 	 * the service task, and the device tasks do not interfere with each
16420 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16421 	 * since we need to be able to guarantee forward progress even under
16422 	 * memory pressure.
16423 	 */
16424 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16425 	if (!i40e_wq) {
16426 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16427 		return -ENOMEM;
16428 	}
16429 
16430 	i40e_dbg_init();
16431 	return pci_register_driver(&i40e_driver);
16432 }
16433 module_init(i40e_init_module);
16434 
16435 /**
16436  * i40e_exit_module - Driver exit cleanup routine
16437  *
16438  * i40e_exit_module is called just before the driver is removed
16439  * from memory.
16440  **/
16441 static void __exit i40e_exit_module(void)
16442 {
16443 	pci_unregister_driver(&i40e_driver);
16444 	destroy_workqueue(i40e_wq);
16445 	ida_destroy(&i40e_client_ida);
16446 	i40e_dbg_exit();
16447 }
16448 module_exit(i40e_exit_module);
16449