1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2021 Intel Corporation. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/of_net.h>
6 #include <linux/pci.h>
7 #include <linux/bpf.h>
8 #include <generated/utsrelease.h>
9 #include <linux/crash_dump.h>
10 
11 /* Local includes */
12 #include "i40e.h"
13 #include "i40e_diag.h"
14 #include "i40e_xsk.h"
15 #include <net/udp_tunnel.h>
16 #include <net/xdp_sock_drv.h>
17 /* All i40e tracepoints are defined by the include below, which
18  * must be included exactly once across the whole kernel with
19  * CREATE_TRACE_POINTS defined
20  */
21 #define CREATE_TRACE_POINTS
22 #include "i40e_trace.h"
23 
24 const char i40e_driver_name[] = "i40e";
25 static const char i40e_driver_string[] =
26 			"Intel(R) Ethernet Connection XL710 Network Driver";
27 
28 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
29 
30 /* a bit of forward declarations */
31 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
32 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
33 static int i40e_add_vsi(struct i40e_vsi *vsi);
34 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
35 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
36 static int i40e_setup_misc_vector(struct i40e_pf *pf);
37 static void i40e_determine_queue_usage(struct i40e_pf *pf);
38 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
39 static void i40e_prep_for_reset(struct i40e_pf *pf);
40 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
41 				   bool lock_acquired);
42 static int i40e_reset(struct i40e_pf *pf);
43 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
44 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
45 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
46 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
47 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
48 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
49 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
50 static int i40e_get_capabilities(struct i40e_pf *pf,
51 				 enum i40e_admin_queue_opc list_type);
52 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
53 
54 /* i40e_pci_tbl - PCI Device ID Table
55  *
56  * Last entry must be all 0s
57  *
58  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
59  *   Class, Class Mask, private data (not used) }
60  */
61 static const struct pci_device_id i40e_pci_tbl[] = {
62 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
63 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
64 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722_A), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
87 	/* required last entry */
88 	{0, }
89 };
90 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
91 
92 #define I40E_MAX_VF_COUNT 128
93 static int debug = -1;
94 module_param(debug, uint, 0);
95 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
96 
97 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
98 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
99 MODULE_LICENSE("GPL v2");
100 
101 static struct workqueue_struct *i40e_wq;
102 
103 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
104 				  struct net_device *netdev, int delta)
105 {
106 	struct netdev_hw_addr *ha;
107 
108 	if (!f || !netdev)
109 		return;
110 
111 	netdev_for_each_mc_addr(ha, netdev) {
112 		if (ether_addr_equal(ha->addr, f->macaddr)) {
113 			ha->refcount += delta;
114 			if (ha->refcount <= 0)
115 				ha->refcount = 1;
116 			break;
117 		}
118 	}
119 }
120 
121 /**
122  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
123  * @hw:   pointer to the HW structure
124  * @mem:  ptr to mem struct to fill out
125  * @size: size of memory requested
126  * @alignment: what to align the allocation to
127  **/
128 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
129 			    u64 size, u32 alignment)
130 {
131 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
132 
133 	mem->size = ALIGN(size, alignment);
134 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
135 				     GFP_KERNEL);
136 	if (!mem->va)
137 		return -ENOMEM;
138 
139 	return 0;
140 }
141 
142 /**
143  * i40e_free_dma_mem_d - OS specific memory free for shared code
144  * @hw:   pointer to the HW structure
145  * @mem:  ptr to mem struct to free
146  **/
147 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
148 {
149 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
150 
151 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
152 	mem->va = NULL;
153 	mem->pa = 0;
154 	mem->size = 0;
155 
156 	return 0;
157 }
158 
159 /**
160  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
161  * @hw:   pointer to the HW structure
162  * @mem:  ptr to mem struct to fill out
163  * @size: size of memory requested
164  **/
165 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
166 			     u32 size)
167 {
168 	mem->size = size;
169 	mem->va = kzalloc(size, GFP_KERNEL);
170 
171 	if (!mem->va)
172 		return -ENOMEM;
173 
174 	return 0;
175 }
176 
177 /**
178  * i40e_free_virt_mem_d - OS specific memory free for shared code
179  * @hw:   pointer to the HW structure
180  * @mem:  ptr to mem struct to free
181  **/
182 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
183 {
184 	/* it's ok to kfree a NULL pointer */
185 	kfree(mem->va);
186 	mem->va = NULL;
187 	mem->size = 0;
188 
189 	return 0;
190 }
191 
192 /**
193  * i40e_get_lump - find a lump of free generic resource
194  * @pf: board private structure
195  * @pile: the pile of resource to search
196  * @needed: the number of items needed
197  * @id: an owner id to stick on the items assigned
198  *
199  * Returns the base item index of the lump, or negative for error
200  **/
201 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
202 			 u16 needed, u16 id)
203 {
204 	int ret = -ENOMEM;
205 	int i, j;
206 
207 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
208 		dev_info(&pf->pdev->dev,
209 			 "param err: pile=%s needed=%d id=0x%04x\n",
210 			 pile ? "<valid>" : "<null>", needed, id);
211 		return -EINVAL;
212 	}
213 
214 	/* Allocate last queue in the pile for FDIR VSI queue
215 	 * so it doesn't fragment the qp_pile
216 	 */
217 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
218 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
219 			dev_err(&pf->pdev->dev,
220 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
221 				pile->num_entries - 1);
222 			return -ENOMEM;
223 		}
224 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
225 		return pile->num_entries - 1;
226 	}
227 
228 	i = 0;
229 	while (i < pile->num_entries) {
230 		/* skip already allocated entries */
231 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
232 			i++;
233 			continue;
234 		}
235 
236 		/* do we have enough in this lump? */
237 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
238 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
239 				break;
240 		}
241 
242 		if (j == needed) {
243 			/* there was enough, so assign it to the requestor */
244 			for (j = 0; j < needed; j++)
245 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
246 			ret = i;
247 			break;
248 		}
249 
250 		/* not enough, so skip over it and continue looking */
251 		i += j;
252 	}
253 
254 	return ret;
255 }
256 
257 /**
258  * i40e_put_lump - return a lump of generic resource
259  * @pile: the pile of resource to search
260  * @index: the base item index
261  * @id: the owner id of the items assigned
262  *
263  * Returns the count of items in the lump
264  **/
265 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
266 {
267 	int valid_id = (id | I40E_PILE_VALID_BIT);
268 	int count = 0;
269 	u16 i;
270 
271 	if (!pile || index >= pile->num_entries)
272 		return -EINVAL;
273 
274 	for (i = index;
275 	     i < pile->num_entries && pile->list[i] == valid_id;
276 	     i++) {
277 		pile->list[i] = 0;
278 		count++;
279 	}
280 
281 
282 	return count;
283 }
284 
285 /**
286  * i40e_find_vsi_from_id - searches for the vsi with the given id
287  * @pf: the pf structure to search for the vsi
288  * @id: id of the vsi it is searching for
289  **/
290 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
291 {
292 	int i;
293 
294 	for (i = 0; i < pf->num_alloc_vsi; i++)
295 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
296 			return pf->vsi[i];
297 
298 	return NULL;
299 }
300 
301 /**
302  * i40e_service_event_schedule - Schedule the service task to wake up
303  * @pf: board private structure
304  *
305  * If not already scheduled, this puts the task into the work queue
306  **/
307 void i40e_service_event_schedule(struct i40e_pf *pf)
308 {
309 	if ((!test_bit(__I40E_DOWN, pf->state) &&
310 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
311 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
312 		queue_work(i40e_wq, &pf->service_task);
313 }
314 
315 /**
316  * i40e_tx_timeout - Respond to a Tx Hang
317  * @netdev: network interface device structure
318  * @txqueue: queue number timing out
319  *
320  * If any port has noticed a Tx timeout, it is likely that the whole
321  * device is munged, not just the one netdev port, so go for the full
322  * reset.
323  **/
324 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
325 {
326 	struct i40e_netdev_priv *np = netdev_priv(netdev);
327 	struct i40e_vsi *vsi = np->vsi;
328 	struct i40e_pf *pf = vsi->back;
329 	struct i40e_ring *tx_ring = NULL;
330 	unsigned int i;
331 	u32 head, val;
332 
333 	pf->tx_timeout_count++;
334 
335 	/* with txqueue index, find the tx_ring struct */
336 	for (i = 0; i < vsi->num_queue_pairs; i++) {
337 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
338 			if (txqueue ==
339 			    vsi->tx_rings[i]->queue_index) {
340 				tx_ring = vsi->tx_rings[i];
341 				break;
342 			}
343 		}
344 	}
345 
346 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
347 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
348 	else if (time_before(jiffies,
349 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
350 		return;   /* don't do any new action before the next timeout */
351 
352 	/* don't kick off another recovery if one is already pending */
353 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
354 		return;
355 
356 	if (tx_ring) {
357 		head = i40e_get_head(tx_ring);
358 		/* Read interrupt register */
359 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
360 			val = rd32(&pf->hw,
361 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
362 						tx_ring->vsi->base_vector - 1));
363 		else
364 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
365 
366 		netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
367 			    vsi->seid, txqueue, tx_ring->next_to_clean,
368 			    head, tx_ring->next_to_use,
369 			    readl(tx_ring->tail), val);
370 	}
371 
372 	pf->tx_timeout_last_recovery = jiffies;
373 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
374 		    pf->tx_timeout_recovery_level, txqueue);
375 
376 	switch (pf->tx_timeout_recovery_level) {
377 	case 1:
378 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
379 		break;
380 	case 2:
381 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
382 		break;
383 	case 3:
384 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
385 		break;
386 	default:
387 		netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
388 		break;
389 	}
390 
391 	i40e_service_event_schedule(pf);
392 	pf->tx_timeout_recovery_level++;
393 }
394 
395 /**
396  * i40e_get_vsi_stats_struct - Get System Network Statistics
397  * @vsi: the VSI we care about
398  *
399  * Returns the address of the device statistics structure.
400  * The statistics are actually updated from the service task.
401  **/
402 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
403 {
404 	return &vsi->net_stats;
405 }
406 
407 /**
408  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
409  * @ring: Tx ring to get statistics from
410  * @stats: statistics entry to be updated
411  **/
412 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
413 					    struct rtnl_link_stats64 *stats)
414 {
415 	u64 bytes, packets;
416 	unsigned int start;
417 
418 	do {
419 		start = u64_stats_fetch_begin_irq(&ring->syncp);
420 		packets = ring->stats.packets;
421 		bytes   = ring->stats.bytes;
422 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
423 
424 	stats->tx_packets += packets;
425 	stats->tx_bytes   += bytes;
426 }
427 
428 /**
429  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
430  * @netdev: network interface device structure
431  * @stats: data structure to store statistics
432  *
433  * Returns the address of the device statistics structure.
434  * The statistics are actually updated from the service task.
435  **/
436 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
437 				  struct rtnl_link_stats64 *stats)
438 {
439 	struct i40e_netdev_priv *np = netdev_priv(netdev);
440 	struct i40e_vsi *vsi = np->vsi;
441 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
442 	struct i40e_ring *ring;
443 	int i;
444 
445 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
446 		return;
447 
448 	if (!vsi->tx_rings)
449 		return;
450 
451 	rcu_read_lock();
452 	for (i = 0; i < vsi->num_queue_pairs; i++) {
453 		u64 bytes, packets;
454 		unsigned int start;
455 
456 		ring = READ_ONCE(vsi->tx_rings[i]);
457 		if (!ring)
458 			continue;
459 		i40e_get_netdev_stats_struct_tx(ring, stats);
460 
461 		if (i40e_enabled_xdp_vsi(vsi)) {
462 			ring = READ_ONCE(vsi->xdp_rings[i]);
463 			if (!ring)
464 				continue;
465 			i40e_get_netdev_stats_struct_tx(ring, stats);
466 		}
467 
468 		ring = READ_ONCE(vsi->rx_rings[i]);
469 		if (!ring)
470 			continue;
471 		do {
472 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
473 			packets = ring->stats.packets;
474 			bytes   = ring->stats.bytes;
475 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
476 
477 		stats->rx_packets += packets;
478 		stats->rx_bytes   += bytes;
479 
480 	}
481 	rcu_read_unlock();
482 
483 	/* following stats updated by i40e_watchdog_subtask() */
484 	stats->multicast	= vsi_stats->multicast;
485 	stats->tx_errors	= vsi_stats->tx_errors;
486 	stats->tx_dropped	= vsi_stats->tx_dropped;
487 	stats->rx_errors	= vsi_stats->rx_errors;
488 	stats->rx_dropped	= vsi_stats->rx_dropped;
489 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
490 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
491 }
492 
493 /**
494  * i40e_vsi_reset_stats - Resets all stats of the given vsi
495  * @vsi: the VSI to have its stats reset
496  **/
497 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
498 {
499 	struct rtnl_link_stats64 *ns;
500 	int i;
501 
502 	if (!vsi)
503 		return;
504 
505 	ns = i40e_get_vsi_stats_struct(vsi);
506 	memset(ns, 0, sizeof(*ns));
507 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
508 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
509 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
510 	if (vsi->rx_rings && vsi->rx_rings[0]) {
511 		for (i = 0; i < vsi->num_queue_pairs; i++) {
512 			memset(&vsi->rx_rings[i]->stats, 0,
513 			       sizeof(vsi->rx_rings[i]->stats));
514 			memset(&vsi->rx_rings[i]->rx_stats, 0,
515 			       sizeof(vsi->rx_rings[i]->rx_stats));
516 			memset(&vsi->tx_rings[i]->stats, 0,
517 			       sizeof(vsi->tx_rings[i]->stats));
518 			memset(&vsi->tx_rings[i]->tx_stats, 0,
519 			       sizeof(vsi->tx_rings[i]->tx_stats));
520 		}
521 	}
522 	vsi->stat_offsets_loaded = false;
523 }
524 
525 /**
526  * i40e_pf_reset_stats - Reset all of the stats for the given PF
527  * @pf: the PF to be reset
528  **/
529 void i40e_pf_reset_stats(struct i40e_pf *pf)
530 {
531 	int i;
532 
533 	memset(&pf->stats, 0, sizeof(pf->stats));
534 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
535 	pf->stat_offsets_loaded = false;
536 
537 	for (i = 0; i < I40E_MAX_VEB; i++) {
538 		if (pf->veb[i]) {
539 			memset(&pf->veb[i]->stats, 0,
540 			       sizeof(pf->veb[i]->stats));
541 			memset(&pf->veb[i]->stats_offsets, 0,
542 			       sizeof(pf->veb[i]->stats_offsets));
543 			memset(&pf->veb[i]->tc_stats, 0,
544 			       sizeof(pf->veb[i]->tc_stats));
545 			memset(&pf->veb[i]->tc_stats_offsets, 0,
546 			       sizeof(pf->veb[i]->tc_stats_offsets));
547 			pf->veb[i]->stat_offsets_loaded = false;
548 		}
549 	}
550 	pf->hw_csum_rx_error = 0;
551 }
552 
553 /**
554  * i40e_stat_update48 - read and update a 48 bit stat from the chip
555  * @hw: ptr to the hardware info
556  * @hireg: the high 32 bit reg to read
557  * @loreg: the low 32 bit reg to read
558  * @offset_loaded: has the initial offset been loaded yet
559  * @offset: ptr to current offset value
560  * @stat: ptr to the stat
561  *
562  * Since the device stats are not reset at PFReset, they likely will not
563  * be zeroed when the driver starts.  We'll save the first values read
564  * and use them as offsets to be subtracted from the raw values in order
565  * to report stats that count from zero.  In the process, we also manage
566  * the potential roll-over.
567  **/
568 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
569 			       bool offset_loaded, u64 *offset, u64 *stat)
570 {
571 	u64 new_data;
572 
573 	if (hw->device_id == I40E_DEV_ID_QEMU) {
574 		new_data = rd32(hw, loreg);
575 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
576 	} else {
577 		new_data = rd64(hw, loreg);
578 	}
579 	if (!offset_loaded)
580 		*offset = new_data;
581 	if (likely(new_data >= *offset))
582 		*stat = new_data - *offset;
583 	else
584 		*stat = (new_data + BIT_ULL(48)) - *offset;
585 	*stat &= 0xFFFFFFFFFFFFULL;
586 }
587 
588 /**
589  * i40e_stat_update32 - read and update a 32 bit stat from the chip
590  * @hw: ptr to the hardware info
591  * @reg: the hw reg to read
592  * @offset_loaded: has the initial offset been loaded yet
593  * @offset: ptr to current offset value
594  * @stat: ptr to the stat
595  **/
596 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
597 			       bool offset_loaded, u64 *offset, u64 *stat)
598 {
599 	u32 new_data;
600 
601 	new_data = rd32(hw, reg);
602 	if (!offset_loaded)
603 		*offset = new_data;
604 	if (likely(new_data >= *offset))
605 		*stat = (u32)(new_data - *offset);
606 	else
607 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
608 }
609 
610 /**
611  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
612  * @hw: ptr to the hardware info
613  * @reg: the hw reg to read and clear
614  * @stat: ptr to the stat
615  **/
616 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
617 {
618 	u32 new_data = rd32(hw, reg);
619 
620 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
621 	*stat += new_data;
622 }
623 
624 /**
625  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
626  * @vsi: the VSI to be updated
627  **/
628 void i40e_update_eth_stats(struct i40e_vsi *vsi)
629 {
630 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
631 	struct i40e_pf *pf = vsi->back;
632 	struct i40e_hw *hw = &pf->hw;
633 	struct i40e_eth_stats *oes;
634 	struct i40e_eth_stats *es;     /* device's eth stats */
635 
636 	es = &vsi->eth_stats;
637 	oes = &vsi->eth_stats_offsets;
638 
639 	/* Gather up the stats that the hw collects */
640 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
641 			   vsi->stat_offsets_loaded,
642 			   &oes->tx_errors, &es->tx_errors);
643 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
644 			   vsi->stat_offsets_loaded,
645 			   &oes->rx_discards, &es->rx_discards);
646 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
647 			   vsi->stat_offsets_loaded,
648 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
649 
650 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
651 			   I40E_GLV_GORCL(stat_idx),
652 			   vsi->stat_offsets_loaded,
653 			   &oes->rx_bytes, &es->rx_bytes);
654 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
655 			   I40E_GLV_UPRCL(stat_idx),
656 			   vsi->stat_offsets_loaded,
657 			   &oes->rx_unicast, &es->rx_unicast);
658 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
659 			   I40E_GLV_MPRCL(stat_idx),
660 			   vsi->stat_offsets_loaded,
661 			   &oes->rx_multicast, &es->rx_multicast);
662 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
663 			   I40E_GLV_BPRCL(stat_idx),
664 			   vsi->stat_offsets_loaded,
665 			   &oes->rx_broadcast, &es->rx_broadcast);
666 
667 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
668 			   I40E_GLV_GOTCL(stat_idx),
669 			   vsi->stat_offsets_loaded,
670 			   &oes->tx_bytes, &es->tx_bytes);
671 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
672 			   I40E_GLV_UPTCL(stat_idx),
673 			   vsi->stat_offsets_loaded,
674 			   &oes->tx_unicast, &es->tx_unicast);
675 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
676 			   I40E_GLV_MPTCL(stat_idx),
677 			   vsi->stat_offsets_loaded,
678 			   &oes->tx_multicast, &es->tx_multicast);
679 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
680 			   I40E_GLV_BPTCL(stat_idx),
681 			   vsi->stat_offsets_loaded,
682 			   &oes->tx_broadcast, &es->tx_broadcast);
683 	vsi->stat_offsets_loaded = true;
684 }
685 
686 /**
687  * i40e_update_veb_stats - Update Switch component statistics
688  * @veb: the VEB being updated
689  **/
690 void i40e_update_veb_stats(struct i40e_veb *veb)
691 {
692 	struct i40e_pf *pf = veb->pf;
693 	struct i40e_hw *hw = &pf->hw;
694 	struct i40e_eth_stats *oes;
695 	struct i40e_eth_stats *es;     /* device's eth stats */
696 	struct i40e_veb_tc_stats *veb_oes;
697 	struct i40e_veb_tc_stats *veb_es;
698 	int i, idx = 0;
699 
700 	idx = veb->stats_idx;
701 	es = &veb->stats;
702 	oes = &veb->stats_offsets;
703 	veb_es = &veb->tc_stats;
704 	veb_oes = &veb->tc_stats_offsets;
705 
706 	/* Gather up the stats that the hw collects */
707 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
708 			   veb->stat_offsets_loaded,
709 			   &oes->tx_discards, &es->tx_discards);
710 	if (hw->revision_id > 0)
711 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
712 				   veb->stat_offsets_loaded,
713 				   &oes->rx_unknown_protocol,
714 				   &es->rx_unknown_protocol);
715 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
716 			   veb->stat_offsets_loaded,
717 			   &oes->rx_bytes, &es->rx_bytes);
718 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
719 			   veb->stat_offsets_loaded,
720 			   &oes->rx_unicast, &es->rx_unicast);
721 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
722 			   veb->stat_offsets_loaded,
723 			   &oes->rx_multicast, &es->rx_multicast);
724 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
725 			   veb->stat_offsets_loaded,
726 			   &oes->rx_broadcast, &es->rx_broadcast);
727 
728 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
729 			   veb->stat_offsets_loaded,
730 			   &oes->tx_bytes, &es->tx_bytes);
731 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
732 			   veb->stat_offsets_loaded,
733 			   &oes->tx_unicast, &es->tx_unicast);
734 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
735 			   veb->stat_offsets_loaded,
736 			   &oes->tx_multicast, &es->tx_multicast);
737 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
738 			   veb->stat_offsets_loaded,
739 			   &oes->tx_broadcast, &es->tx_broadcast);
740 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
741 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
742 				   I40E_GLVEBTC_RPCL(i, idx),
743 				   veb->stat_offsets_loaded,
744 				   &veb_oes->tc_rx_packets[i],
745 				   &veb_es->tc_rx_packets[i]);
746 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
747 				   I40E_GLVEBTC_RBCL(i, idx),
748 				   veb->stat_offsets_loaded,
749 				   &veb_oes->tc_rx_bytes[i],
750 				   &veb_es->tc_rx_bytes[i]);
751 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
752 				   I40E_GLVEBTC_TPCL(i, idx),
753 				   veb->stat_offsets_loaded,
754 				   &veb_oes->tc_tx_packets[i],
755 				   &veb_es->tc_tx_packets[i]);
756 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
757 				   I40E_GLVEBTC_TBCL(i, idx),
758 				   veb->stat_offsets_loaded,
759 				   &veb_oes->tc_tx_bytes[i],
760 				   &veb_es->tc_tx_bytes[i]);
761 	}
762 	veb->stat_offsets_loaded = true;
763 }
764 
765 /**
766  * i40e_update_vsi_stats - Update the vsi statistics counters.
767  * @vsi: the VSI to be updated
768  *
769  * There are a few instances where we store the same stat in a
770  * couple of different structs.  This is partly because we have
771  * the netdev stats that need to be filled out, which is slightly
772  * different from the "eth_stats" defined by the chip and used in
773  * VF communications.  We sort it out here.
774  **/
775 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
776 {
777 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
778 	struct i40e_pf *pf = vsi->back;
779 	struct rtnl_link_stats64 *ons;
780 	struct rtnl_link_stats64 *ns;   /* netdev stats */
781 	struct i40e_eth_stats *oes;
782 	struct i40e_eth_stats *es;     /* device's eth stats */
783 	u64 tx_restart, tx_busy;
784 	struct i40e_ring *p;
785 	u64 bytes, packets;
786 	unsigned int start;
787 	u64 tx_linearize;
788 	u64 tx_force_wb;
789 	u64 tx_stopped;
790 	u64 rx_p, rx_b;
791 	u64 tx_p, tx_b;
792 	u16 q;
793 
794 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
795 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
796 		return;
797 
798 	ns = i40e_get_vsi_stats_struct(vsi);
799 	ons = &vsi->net_stats_offsets;
800 	es = &vsi->eth_stats;
801 	oes = &vsi->eth_stats_offsets;
802 
803 	/* Gather up the netdev and vsi stats that the driver collects
804 	 * on the fly during packet processing
805 	 */
806 	rx_b = rx_p = 0;
807 	tx_b = tx_p = 0;
808 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
809 	tx_stopped = 0;
810 	rx_page = 0;
811 	rx_buf = 0;
812 	rx_reuse = 0;
813 	rx_alloc = 0;
814 	rx_waive = 0;
815 	rx_busy = 0;
816 	rcu_read_lock();
817 	for (q = 0; q < vsi->num_queue_pairs; q++) {
818 		/* locate Tx ring */
819 		p = READ_ONCE(vsi->tx_rings[q]);
820 		if (!p)
821 			continue;
822 
823 		do {
824 			start = u64_stats_fetch_begin_irq(&p->syncp);
825 			packets = p->stats.packets;
826 			bytes = p->stats.bytes;
827 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
828 		tx_b += bytes;
829 		tx_p += packets;
830 		tx_restart += p->tx_stats.restart_queue;
831 		tx_busy += p->tx_stats.tx_busy;
832 		tx_linearize += p->tx_stats.tx_linearize;
833 		tx_force_wb += p->tx_stats.tx_force_wb;
834 		tx_stopped += p->tx_stats.tx_stopped;
835 
836 		/* locate Rx ring */
837 		p = READ_ONCE(vsi->rx_rings[q]);
838 		if (!p)
839 			continue;
840 
841 		do {
842 			start = u64_stats_fetch_begin_irq(&p->syncp);
843 			packets = p->stats.packets;
844 			bytes = p->stats.bytes;
845 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
846 		rx_b += bytes;
847 		rx_p += packets;
848 		rx_buf += p->rx_stats.alloc_buff_failed;
849 		rx_page += p->rx_stats.alloc_page_failed;
850 		rx_reuse += p->rx_stats.page_reuse_count;
851 		rx_alloc += p->rx_stats.page_alloc_count;
852 		rx_waive += p->rx_stats.page_waive_count;
853 		rx_busy += p->rx_stats.page_busy_count;
854 
855 		if (i40e_enabled_xdp_vsi(vsi)) {
856 			/* locate XDP ring */
857 			p = READ_ONCE(vsi->xdp_rings[q]);
858 			if (!p)
859 				continue;
860 
861 			do {
862 				start = u64_stats_fetch_begin_irq(&p->syncp);
863 				packets = p->stats.packets;
864 				bytes = p->stats.bytes;
865 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
866 			tx_b += bytes;
867 			tx_p += packets;
868 			tx_restart += p->tx_stats.restart_queue;
869 			tx_busy += p->tx_stats.tx_busy;
870 			tx_linearize += p->tx_stats.tx_linearize;
871 			tx_force_wb += p->tx_stats.tx_force_wb;
872 		}
873 	}
874 	rcu_read_unlock();
875 	vsi->tx_restart = tx_restart;
876 	vsi->tx_busy = tx_busy;
877 	vsi->tx_linearize = tx_linearize;
878 	vsi->tx_force_wb = tx_force_wb;
879 	vsi->tx_stopped = tx_stopped;
880 	vsi->rx_page_failed = rx_page;
881 	vsi->rx_buf_failed = rx_buf;
882 	vsi->rx_page_reuse = rx_reuse;
883 	vsi->rx_page_alloc = rx_alloc;
884 	vsi->rx_page_waive = rx_waive;
885 	vsi->rx_page_busy = rx_busy;
886 
887 	ns->rx_packets = rx_p;
888 	ns->rx_bytes = rx_b;
889 	ns->tx_packets = tx_p;
890 	ns->tx_bytes = tx_b;
891 
892 	/* update netdev stats from eth stats */
893 	i40e_update_eth_stats(vsi);
894 	ons->tx_errors = oes->tx_errors;
895 	ns->tx_errors = es->tx_errors;
896 	ons->multicast = oes->rx_multicast;
897 	ns->multicast = es->rx_multicast;
898 	ons->rx_dropped = oes->rx_discards;
899 	ns->rx_dropped = es->rx_discards;
900 	ons->tx_dropped = oes->tx_discards;
901 	ns->tx_dropped = es->tx_discards;
902 
903 	/* pull in a couple PF stats if this is the main vsi */
904 	if (vsi == pf->vsi[pf->lan_vsi]) {
905 		ns->rx_crc_errors = pf->stats.crc_errors;
906 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
907 		ns->rx_length_errors = pf->stats.rx_length_errors;
908 	}
909 }
910 
911 /**
912  * i40e_update_pf_stats - Update the PF statistics counters.
913  * @pf: the PF to be updated
914  **/
915 static void i40e_update_pf_stats(struct i40e_pf *pf)
916 {
917 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
918 	struct i40e_hw_port_stats *nsd = &pf->stats;
919 	struct i40e_hw *hw = &pf->hw;
920 	u32 val;
921 	int i;
922 
923 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
924 			   I40E_GLPRT_GORCL(hw->port),
925 			   pf->stat_offsets_loaded,
926 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
927 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
928 			   I40E_GLPRT_GOTCL(hw->port),
929 			   pf->stat_offsets_loaded,
930 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
931 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
932 			   pf->stat_offsets_loaded,
933 			   &osd->eth.rx_discards,
934 			   &nsd->eth.rx_discards);
935 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
936 			   I40E_GLPRT_UPRCL(hw->port),
937 			   pf->stat_offsets_loaded,
938 			   &osd->eth.rx_unicast,
939 			   &nsd->eth.rx_unicast);
940 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
941 			   I40E_GLPRT_MPRCL(hw->port),
942 			   pf->stat_offsets_loaded,
943 			   &osd->eth.rx_multicast,
944 			   &nsd->eth.rx_multicast);
945 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
946 			   I40E_GLPRT_BPRCL(hw->port),
947 			   pf->stat_offsets_loaded,
948 			   &osd->eth.rx_broadcast,
949 			   &nsd->eth.rx_broadcast);
950 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
951 			   I40E_GLPRT_UPTCL(hw->port),
952 			   pf->stat_offsets_loaded,
953 			   &osd->eth.tx_unicast,
954 			   &nsd->eth.tx_unicast);
955 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
956 			   I40E_GLPRT_MPTCL(hw->port),
957 			   pf->stat_offsets_loaded,
958 			   &osd->eth.tx_multicast,
959 			   &nsd->eth.tx_multicast);
960 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
961 			   I40E_GLPRT_BPTCL(hw->port),
962 			   pf->stat_offsets_loaded,
963 			   &osd->eth.tx_broadcast,
964 			   &nsd->eth.tx_broadcast);
965 
966 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
967 			   pf->stat_offsets_loaded,
968 			   &osd->tx_dropped_link_down,
969 			   &nsd->tx_dropped_link_down);
970 
971 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
972 			   pf->stat_offsets_loaded,
973 			   &osd->crc_errors, &nsd->crc_errors);
974 
975 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
976 			   pf->stat_offsets_loaded,
977 			   &osd->illegal_bytes, &nsd->illegal_bytes);
978 
979 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
980 			   pf->stat_offsets_loaded,
981 			   &osd->mac_local_faults,
982 			   &nsd->mac_local_faults);
983 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
984 			   pf->stat_offsets_loaded,
985 			   &osd->mac_remote_faults,
986 			   &nsd->mac_remote_faults);
987 
988 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
989 			   pf->stat_offsets_loaded,
990 			   &osd->rx_length_errors,
991 			   &nsd->rx_length_errors);
992 
993 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
994 			   pf->stat_offsets_loaded,
995 			   &osd->link_xon_rx, &nsd->link_xon_rx);
996 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
997 			   pf->stat_offsets_loaded,
998 			   &osd->link_xon_tx, &nsd->link_xon_tx);
999 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1000 			   pf->stat_offsets_loaded,
1001 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1002 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1003 			   pf->stat_offsets_loaded,
1004 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1005 
1006 	for (i = 0; i < 8; i++) {
1007 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1008 				   pf->stat_offsets_loaded,
1009 				   &osd->priority_xoff_rx[i],
1010 				   &nsd->priority_xoff_rx[i]);
1011 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1012 				   pf->stat_offsets_loaded,
1013 				   &osd->priority_xon_rx[i],
1014 				   &nsd->priority_xon_rx[i]);
1015 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1016 				   pf->stat_offsets_loaded,
1017 				   &osd->priority_xon_tx[i],
1018 				   &nsd->priority_xon_tx[i]);
1019 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1020 				   pf->stat_offsets_loaded,
1021 				   &osd->priority_xoff_tx[i],
1022 				   &nsd->priority_xoff_tx[i]);
1023 		i40e_stat_update32(hw,
1024 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1025 				   pf->stat_offsets_loaded,
1026 				   &osd->priority_xon_2_xoff[i],
1027 				   &nsd->priority_xon_2_xoff[i]);
1028 	}
1029 
1030 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1031 			   I40E_GLPRT_PRC64L(hw->port),
1032 			   pf->stat_offsets_loaded,
1033 			   &osd->rx_size_64, &nsd->rx_size_64);
1034 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1035 			   I40E_GLPRT_PRC127L(hw->port),
1036 			   pf->stat_offsets_loaded,
1037 			   &osd->rx_size_127, &nsd->rx_size_127);
1038 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1039 			   I40E_GLPRT_PRC255L(hw->port),
1040 			   pf->stat_offsets_loaded,
1041 			   &osd->rx_size_255, &nsd->rx_size_255);
1042 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1043 			   I40E_GLPRT_PRC511L(hw->port),
1044 			   pf->stat_offsets_loaded,
1045 			   &osd->rx_size_511, &nsd->rx_size_511);
1046 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1047 			   I40E_GLPRT_PRC1023L(hw->port),
1048 			   pf->stat_offsets_loaded,
1049 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1050 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1051 			   I40E_GLPRT_PRC1522L(hw->port),
1052 			   pf->stat_offsets_loaded,
1053 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1054 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1055 			   I40E_GLPRT_PRC9522L(hw->port),
1056 			   pf->stat_offsets_loaded,
1057 			   &osd->rx_size_big, &nsd->rx_size_big);
1058 
1059 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1060 			   I40E_GLPRT_PTC64L(hw->port),
1061 			   pf->stat_offsets_loaded,
1062 			   &osd->tx_size_64, &nsd->tx_size_64);
1063 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1064 			   I40E_GLPRT_PTC127L(hw->port),
1065 			   pf->stat_offsets_loaded,
1066 			   &osd->tx_size_127, &nsd->tx_size_127);
1067 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1068 			   I40E_GLPRT_PTC255L(hw->port),
1069 			   pf->stat_offsets_loaded,
1070 			   &osd->tx_size_255, &nsd->tx_size_255);
1071 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1072 			   I40E_GLPRT_PTC511L(hw->port),
1073 			   pf->stat_offsets_loaded,
1074 			   &osd->tx_size_511, &nsd->tx_size_511);
1075 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1076 			   I40E_GLPRT_PTC1023L(hw->port),
1077 			   pf->stat_offsets_loaded,
1078 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1079 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1080 			   I40E_GLPRT_PTC1522L(hw->port),
1081 			   pf->stat_offsets_loaded,
1082 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1083 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1084 			   I40E_GLPRT_PTC9522L(hw->port),
1085 			   pf->stat_offsets_loaded,
1086 			   &osd->tx_size_big, &nsd->tx_size_big);
1087 
1088 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1089 			   pf->stat_offsets_loaded,
1090 			   &osd->rx_undersize, &nsd->rx_undersize);
1091 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1092 			   pf->stat_offsets_loaded,
1093 			   &osd->rx_fragments, &nsd->rx_fragments);
1094 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1095 			   pf->stat_offsets_loaded,
1096 			   &osd->rx_oversize, &nsd->rx_oversize);
1097 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1098 			   pf->stat_offsets_loaded,
1099 			   &osd->rx_jabber, &nsd->rx_jabber);
1100 
1101 	/* FDIR stats */
1102 	i40e_stat_update_and_clear32(hw,
1103 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1104 			&nsd->fd_atr_match);
1105 	i40e_stat_update_and_clear32(hw,
1106 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1107 			&nsd->fd_sb_match);
1108 	i40e_stat_update_and_clear32(hw,
1109 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1110 			&nsd->fd_atr_tunnel_match);
1111 
1112 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1113 	nsd->tx_lpi_status =
1114 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1115 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1116 	nsd->rx_lpi_status =
1117 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1118 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1119 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1120 			   pf->stat_offsets_loaded,
1121 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1122 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1123 			   pf->stat_offsets_loaded,
1124 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1125 
1126 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1127 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1128 		nsd->fd_sb_status = true;
1129 	else
1130 		nsd->fd_sb_status = false;
1131 
1132 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1133 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1134 		nsd->fd_atr_status = true;
1135 	else
1136 		nsd->fd_atr_status = false;
1137 
1138 	pf->stat_offsets_loaded = true;
1139 }
1140 
1141 /**
1142  * i40e_update_stats - Update the various statistics counters.
1143  * @vsi: the VSI to be updated
1144  *
1145  * Update the various stats for this VSI and its related entities.
1146  **/
1147 void i40e_update_stats(struct i40e_vsi *vsi)
1148 {
1149 	struct i40e_pf *pf = vsi->back;
1150 
1151 	if (vsi == pf->vsi[pf->lan_vsi])
1152 		i40e_update_pf_stats(pf);
1153 
1154 	i40e_update_vsi_stats(vsi);
1155 }
1156 
1157 /**
1158  * i40e_count_filters - counts VSI mac filters
1159  * @vsi: the VSI to be searched
1160  *
1161  * Returns count of mac filters
1162  **/
1163 int i40e_count_filters(struct i40e_vsi *vsi)
1164 {
1165 	struct i40e_mac_filter *f;
1166 	struct hlist_node *h;
1167 	int bkt;
1168 	int cnt = 0;
1169 
1170 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1171 		++cnt;
1172 
1173 	return cnt;
1174 }
1175 
1176 /**
1177  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1178  * @vsi: the VSI to be searched
1179  * @macaddr: the MAC address
1180  * @vlan: the vlan
1181  *
1182  * Returns ptr to the filter object or NULL
1183  **/
1184 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1185 						const u8 *macaddr, s16 vlan)
1186 {
1187 	struct i40e_mac_filter *f;
1188 	u64 key;
1189 
1190 	if (!vsi || !macaddr)
1191 		return NULL;
1192 
1193 	key = i40e_addr_to_hkey(macaddr);
1194 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1195 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1196 		    (vlan == f->vlan))
1197 			return f;
1198 	}
1199 	return NULL;
1200 }
1201 
1202 /**
1203  * i40e_find_mac - Find a mac addr in the macvlan filters list
1204  * @vsi: the VSI to be searched
1205  * @macaddr: the MAC address we are searching for
1206  *
1207  * Returns the first filter with the provided MAC address or NULL if
1208  * MAC address was not found
1209  **/
1210 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1211 {
1212 	struct i40e_mac_filter *f;
1213 	u64 key;
1214 
1215 	if (!vsi || !macaddr)
1216 		return NULL;
1217 
1218 	key = i40e_addr_to_hkey(macaddr);
1219 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1220 		if ((ether_addr_equal(macaddr, f->macaddr)))
1221 			return f;
1222 	}
1223 	return NULL;
1224 }
1225 
1226 /**
1227  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1228  * @vsi: the VSI to be searched
1229  *
1230  * Returns true if VSI is in vlan mode or false otherwise
1231  **/
1232 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1233 {
1234 	/* If we have a PVID, always operate in VLAN mode */
1235 	if (vsi->info.pvid)
1236 		return true;
1237 
1238 	/* We need to operate in VLAN mode whenever we have any filters with
1239 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1240 	 * time, incurring search cost repeatedly. However, we can notice two
1241 	 * things:
1242 	 *
1243 	 * 1) the only place where we can gain a VLAN filter is in
1244 	 *    i40e_add_filter.
1245 	 *
1246 	 * 2) the only place where filters are actually removed is in
1247 	 *    i40e_sync_filters_subtask.
1248 	 *
1249 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1250 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1251 	 * we have to perform the full search after deleting filters in
1252 	 * i40e_sync_filters_subtask, but we already have to search
1253 	 * filters here and can perform the check at the same time. This
1254 	 * results in avoiding embedding a loop for VLAN mode inside another
1255 	 * loop over all the filters, and should maintain correctness as noted
1256 	 * above.
1257 	 */
1258 	return vsi->has_vlan_filter;
1259 }
1260 
1261 /**
1262  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1263  * @vsi: the VSI to configure
1264  * @tmp_add_list: list of filters ready to be added
1265  * @tmp_del_list: list of filters ready to be deleted
1266  * @vlan_filters: the number of active VLAN filters
1267  *
1268  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1269  * behave as expected. If we have any active VLAN filters remaining or about
1270  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1271  * so that they only match against untagged traffic. If we no longer have any
1272  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1273  * so that they match against both tagged and untagged traffic. In this way,
1274  * we ensure that we correctly receive the desired traffic. This ensures that
1275  * when we have an active VLAN we will receive only untagged traffic and
1276  * traffic matching active VLANs. If we have no active VLANs then we will
1277  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1278  *
1279  * Finally, in a similar fashion, this function also corrects filters when
1280  * there is an active PVID assigned to this VSI.
1281  *
1282  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1283  *
1284  * This function is only expected to be called from within
1285  * i40e_sync_vsi_filters.
1286  *
1287  * NOTE: This function expects to be called while under the
1288  * mac_filter_hash_lock
1289  */
1290 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1291 					 struct hlist_head *tmp_add_list,
1292 					 struct hlist_head *tmp_del_list,
1293 					 int vlan_filters)
1294 {
1295 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1296 	struct i40e_mac_filter *f, *add_head;
1297 	struct i40e_new_mac_filter *new;
1298 	struct hlist_node *h;
1299 	int bkt, new_vlan;
1300 
1301 	/* To determine if a particular filter needs to be replaced we
1302 	 * have the three following conditions:
1303 	 *
1304 	 * a) if we have a PVID assigned, then all filters which are
1305 	 *    not marked as VLAN=PVID must be replaced with filters that
1306 	 *    are.
1307 	 * b) otherwise, if we have any active VLANS, all filters
1308 	 *    which are marked as VLAN=-1 must be replaced with
1309 	 *    filters marked as VLAN=0
1310 	 * c) finally, if we do not have any active VLANS, all filters
1311 	 *    which are marked as VLAN=0 must be replaced with filters
1312 	 *    marked as VLAN=-1
1313 	 */
1314 
1315 	/* Update the filters about to be added in place */
1316 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1317 		if (pvid && new->f->vlan != pvid)
1318 			new->f->vlan = pvid;
1319 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1320 			new->f->vlan = 0;
1321 		else if (!vlan_filters && new->f->vlan == 0)
1322 			new->f->vlan = I40E_VLAN_ANY;
1323 	}
1324 
1325 	/* Update the remaining active filters */
1326 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1327 		/* Combine the checks for whether a filter needs to be changed
1328 		 * and then determine the new VLAN inside the if block, in
1329 		 * order to avoid duplicating code for adding the new filter
1330 		 * then deleting the old filter.
1331 		 */
1332 		if ((pvid && f->vlan != pvid) ||
1333 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1334 		    (!vlan_filters && f->vlan == 0)) {
1335 			/* Determine the new vlan we will be adding */
1336 			if (pvid)
1337 				new_vlan = pvid;
1338 			else if (vlan_filters)
1339 				new_vlan = 0;
1340 			else
1341 				new_vlan = I40E_VLAN_ANY;
1342 
1343 			/* Create the new filter */
1344 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1345 			if (!add_head)
1346 				return -ENOMEM;
1347 
1348 			/* Create a temporary i40e_new_mac_filter */
1349 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1350 			if (!new)
1351 				return -ENOMEM;
1352 
1353 			new->f = add_head;
1354 			new->state = add_head->state;
1355 
1356 			/* Add the new filter to the tmp list */
1357 			hlist_add_head(&new->hlist, tmp_add_list);
1358 
1359 			/* Put the original filter into the delete list */
1360 			f->state = I40E_FILTER_REMOVE;
1361 			hash_del(&f->hlist);
1362 			hlist_add_head(&f->hlist, tmp_del_list);
1363 		}
1364 	}
1365 
1366 	vsi->has_vlan_filter = !!vlan_filters;
1367 
1368 	return 0;
1369 }
1370 
1371 /**
1372  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1373  * @vsi: the PF Main VSI - inappropriate for any other VSI
1374  * @macaddr: the MAC address
1375  *
1376  * Remove whatever filter the firmware set up so the driver can manage
1377  * its own filtering intelligently.
1378  **/
1379 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1380 {
1381 	struct i40e_aqc_remove_macvlan_element_data element;
1382 	struct i40e_pf *pf = vsi->back;
1383 
1384 	/* Only appropriate for the PF main VSI */
1385 	if (vsi->type != I40E_VSI_MAIN)
1386 		return;
1387 
1388 	memset(&element, 0, sizeof(element));
1389 	ether_addr_copy(element.mac_addr, macaddr);
1390 	element.vlan_tag = 0;
1391 	/* Ignore error returns, some firmware does it this way... */
1392 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1393 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1394 
1395 	memset(&element, 0, sizeof(element));
1396 	ether_addr_copy(element.mac_addr, macaddr);
1397 	element.vlan_tag = 0;
1398 	/* ...and some firmware does it this way. */
1399 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1400 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1401 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1402 }
1403 
1404 /**
1405  * i40e_add_filter - Add a mac/vlan filter to the VSI
1406  * @vsi: the VSI to be searched
1407  * @macaddr: the MAC address
1408  * @vlan: the vlan
1409  *
1410  * Returns ptr to the filter object or NULL when no memory available.
1411  *
1412  * NOTE: This function is expected to be called with mac_filter_hash_lock
1413  * being held.
1414  **/
1415 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1416 					const u8 *macaddr, s16 vlan)
1417 {
1418 	struct i40e_mac_filter *f;
1419 	u64 key;
1420 
1421 	if (!vsi || !macaddr)
1422 		return NULL;
1423 
1424 	f = i40e_find_filter(vsi, macaddr, vlan);
1425 	if (!f) {
1426 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1427 		if (!f)
1428 			return NULL;
1429 
1430 		/* Update the boolean indicating if we need to function in
1431 		 * VLAN mode.
1432 		 */
1433 		if (vlan >= 0)
1434 			vsi->has_vlan_filter = true;
1435 
1436 		ether_addr_copy(f->macaddr, macaddr);
1437 		f->vlan = vlan;
1438 		f->state = I40E_FILTER_NEW;
1439 		INIT_HLIST_NODE(&f->hlist);
1440 
1441 		key = i40e_addr_to_hkey(macaddr);
1442 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1443 
1444 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1445 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1446 	}
1447 
1448 	/* If we're asked to add a filter that has been marked for removal, it
1449 	 * is safe to simply restore it to active state. __i40e_del_filter
1450 	 * will have simply deleted any filters which were previously marked
1451 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1452 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1453 	 * task, just restore this filter to the ACTIVE state so that the
1454 	 * sync task leaves it in place
1455 	 */
1456 	if (f->state == I40E_FILTER_REMOVE)
1457 		f->state = I40E_FILTER_ACTIVE;
1458 
1459 	return f;
1460 }
1461 
1462 /**
1463  * __i40e_del_filter - Remove a specific filter from the VSI
1464  * @vsi: VSI to remove from
1465  * @f: the filter to remove from the list
1466  *
1467  * This function should be called instead of i40e_del_filter only if you know
1468  * the exact filter you will remove already, such as via i40e_find_filter or
1469  * i40e_find_mac.
1470  *
1471  * NOTE: This function is expected to be called with mac_filter_hash_lock
1472  * being held.
1473  * ANOTHER NOTE: This function MUST be called from within the context of
1474  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1475  * instead of list_for_each_entry().
1476  **/
1477 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1478 {
1479 	if (!f)
1480 		return;
1481 
1482 	/* If the filter was never added to firmware then we can just delete it
1483 	 * directly and we don't want to set the status to remove or else an
1484 	 * admin queue command will unnecessarily fire.
1485 	 */
1486 	if ((f->state == I40E_FILTER_FAILED) ||
1487 	    (f->state == I40E_FILTER_NEW)) {
1488 		hash_del(&f->hlist);
1489 		kfree(f);
1490 	} else {
1491 		f->state = I40E_FILTER_REMOVE;
1492 	}
1493 
1494 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1495 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1496 }
1497 
1498 /**
1499  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1500  * @vsi: the VSI to be searched
1501  * @macaddr: the MAC address
1502  * @vlan: the VLAN
1503  *
1504  * NOTE: This function is expected to be called with mac_filter_hash_lock
1505  * being held.
1506  * ANOTHER NOTE: This function MUST be called from within the context of
1507  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1508  * instead of list_for_each_entry().
1509  **/
1510 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1511 {
1512 	struct i40e_mac_filter *f;
1513 
1514 	if (!vsi || !macaddr)
1515 		return;
1516 
1517 	f = i40e_find_filter(vsi, macaddr, vlan);
1518 	__i40e_del_filter(vsi, f);
1519 }
1520 
1521 /**
1522  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1523  * @vsi: the VSI to be searched
1524  * @macaddr: the mac address to be filtered
1525  *
1526  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1527  * go through all the macvlan filters and add a macvlan filter for each
1528  * unique vlan that already exists. If a PVID has been assigned, instead only
1529  * add the macaddr to that VLAN.
1530  *
1531  * Returns last filter added on success, else NULL
1532  **/
1533 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1534 					    const u8 *macaddr)
1535 {
1536 	struct i40e_mac_filter *f, *add = NULL;
1537 	struct hlist_node *h;
1538 	int bkt;
1539 
1540 	if (vsi->info.pvid)
1541 		return i40e_add_filter(vsi, macaddr,
1542 				       le16_to_cpu(vsi->info.pvid));
1543 
1544 	if (!i40e_is_vsi_in_vlan(vsi))
1545 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1546 
1547 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1548 		if (f->state == I40E_FILTER_REMOVE)
1549 			continue;
1550 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1551 		if (!add)
1552 			return NULL;
1553 	}
1554 
1555 	return add;
1556 }
1557 
1558 /**
1559  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1560  * @vsi: the VSI to be searched
1561  * @macaddr: the mac address to be removed
1562  *
1563  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1564  * associated with.
1565  *
1566  * Returns 0 for success, or error
1567  **/
1568 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1569 {
1570 	struct i40e_mac_filter *f;
1571 	struct hlist_node *h;
1572 	bool found = false;
1573 	int bkt;
1574 
1575 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1576 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1577 		if (ether_addr_equal(macaddr, f->macaddr)) {
1578 			__i40e_del_filter(vsi, f);
1579 			found = true;
1580 		}
1581 	}
1582 
1583 	if (found)
1584 		return 0;
1585 	else
1586 		return -ENOENT;
1587 }
1588 
1589 /**
1590  * i40e_set_mac - NDO callback to set mac address
1591  * @netdev: network interface device structure
1592  * @p: pointer to an address structure
1593  *
1594  * Returns 0 on success, negative on failure
1595  **/
1596 static int i40e_set_mac(struct net_device *netdev, void *p)
1597 {
1598 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1599 	struct i40e_vsi *vsi = np->vsi;
1600 	struct i40e_pf *pf = vsi->back;
1601 	struct i40e_hw *hw = &pf->hw;
1602 	struct sockaddr *addr = p;
1603 
1604 	if (!is_valid_ether_addr(addr->sa_data))
1605 		return -EADDRNOTAVAIL;
1606 
1607 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1608 		netdev_info(netdev, "already using mac address %pM\n",
1609 			    addr->sa_data);
1610 		return 0;
1611 	}
1612 
1613 	if (test_bit(__I40E_DOWN, pf->state) ||
1614 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1615 		return -EADDRNOTAVAIL;
1616 
1617 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1618 		netdev_info(netdev, "returning to hw mac address %pM\n",
1619 			    hw->mac.addr);
1620 	else
1621 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1622 
1623 	/* Copy the address first, so that we avoid a possible race with
1624 	 * .set_rx_mode().
1625 	 * - Remove old address from MAC filter
1626 	 * - Copy new address
1627 	 * - Add new address to MAC filter
1628 	 */
1629 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1630 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1631 	eth_hw_addr_set(netdev, addr->sa_data);
1632 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1633 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1634 
1635 	if (vsi->type == I40E_VSI_MAIN) {
1636 		i40e_status ret;
1637 
1638 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1639 						addr->sa_data, NULL);
1640 		if (ret)
1641 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1642 				    i40e_stat_str(hw, ret),
1643 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1644 	}
1645 
1646 	/* schedule our worker thread which will take care of
1647 	 * applying the new filter changes
1648 	 */
1649 	i40e_service_event_schedule(pf);
1650 	return 0;
1651 }
1652 
1653 /**
1654  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1655  * @vsi: vsi structure
1656  * @seed: RSS hash seed
1657  * @lut: pointer to lookup table of lut_size
1658  * @lut_size: size of the lookup table
1659  **/
1660 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1661 			      u8 *lut, u16 lut_size)
1662 {
1663 	struct i40e_pf *pf = vsi->back;
1664 	struct i40e_hw *hw = &pf->hw;
1665 	int ret = 0;
1666 
1667 	if (seed) {
1668 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1669 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1670 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1671 		if (ret) {
1672 			dev_info(&pf->pdev->dev,
1673 				 "Cannot set RSS key, err %s aq_err %s\n",
1674 				 i40e_stat_str(hw, ret),
1675 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1676 			return ret;
1677 		}
1678 	}
1679 	if (lut) {
1680 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1681 
1682 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1683 		if (ret) {
1684 			dev_info(&pf->pdev->dev,
1685 				 "Cannot set RSS lut, err %s aq_err %s\n",
1686 				 i40e_stat_str(hw, ret),
1687 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1688 			return ret;
1689 		}
1690 	}
1691 	return ret;
1692 }
1693 
1694 /**
1695  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1696  * @vsi: VSI structure
1697  **/
1698 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1699 {
1700 	struct i40e_pf *pf = vsi->back;
1701 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1702 	u8 *lut;
1703 	int ret;
1704 
1705 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1706 		return 0;
1707 	if (!vsi->rss_size)
1708 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1709 				      vsi->num_queue_pairs);
1710 	if (!vsi->rss_size)
1711 		return -EINVAL;
1712 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1713 	if (!lut)
1714 		return -ENOMEM;
1715 
1716 	/* Use the user configured hash keys and lookup table if there is one,
1717 	 * otherwise use default
1718 	 */
1719 	if (vsi->rss_lut_user)
1720 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1721 	else
1722 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1723 	if (vsi->rss_hkey_user)
1724 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1725 	else
1726 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1727 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1728 	kfree(lut);
1729 	return ret;
1730 }
1731 
1732 /**
1733  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1734  * @vsi: the VSI being configured,
1735  * @ctxt: VSI context structure
1736  * @enabled_tc: number of traffic classes to enable
1737  *
1738  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1739  **/
1740 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1741 					   struct i40e_vsi_context *ctxt,
1742 					   u8 enabled_tc)
1743 {
1744 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1745 	int i, override_q, pow, num_qps, ret;
1746 	u8 netdev_tc = 0, offset = 0;
1747 
1748 	if (vsi->type != I40E_VSI_MAIN)
1749 		return -EINVAL;
1750 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1751 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1752 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1753 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1754 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1755 
1756 	/* find the next higher power-of-2 of num queue pairs */
1757 	pow = ilog2(num_qps);
1758 	if (!is_power_of_2(num_qps))
1759 		pow++;
1760 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1761 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1762 
1763 	/* Setup queue offset/count for all TCs for given VSI */
1764 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1765 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1766 		/* See if the given TC is enabled for the given VSI */
1767 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1768 			offset = vsi->mqprio_qopt.qopt.offset[i];
1769 			qcount = vsi->mqprio_qopt.qopt.count[i];
1770 			if (qcount > max_qcount)
1771 				max_qcount = qcount;
1772 			vsi->tc_config.tc_info[i].qoffset = offset;
1773 			vsi->tc_config.tc_info[i].qcount = qcount;
1774 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1775 		} else {
1776 			/* TC is not enabled so set the offset to
1777 			 * default queue and allocate one queue
1778 			 * for the given TC.
1779 			 */
1780 			vsi->tc_config.tc_info[i].qoffset = 0;
1781 			vsi->tc_config.tc_info[i].qcount = 1;
1782 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1783 		}
1784 	}
1785 
1786 	/* Set actual Tx/Rx queue pairs */
1787 	vsi->num_queue_pairs = offset + qcount;
1788 
1789 	/* Setup queue TC[0].qmap for given VSI context */
1790 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1791 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1792 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1793 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1794 
1795 	/* Reconfigure RSS for main VSI with max queue count */
1796 	vsi->rss_size = max_qcount;
1797 	ret = i40e_vsi_config_rss(vsi);
1798 	if (ret) {
1799 		dev_info(&vsi->back->pdev->dev,
1800 			 "Failed to reconfig rss for num_queues (%u)\n",
1801 			 max_qcount);
1802 		return ret;
1803 	}
1804 	vsi->reconfig_rss = true;
1805 	dev_dbg(&vsi->back->pdev->dev,
1806 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1807 
1808 	/* Find queue count available for channel VSIs and starting offset
1809 	 * for channel VSIs
1810 	 */
1811 	override_q = vsi->mqprio_qopt.qopt.count[0];
1812 	if (override_q && override_q < vsi->num_queue_pairs) {
1813 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1814 		vsi->next_base_queue = override_q;
1815 	}
1816 	return 0;
1817 }
1818 
1819 /**
1820  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1821  * @vsi: the VSI being setup
1822  * @ctxt: VSI context structure
1823  * @enabled_tc: Enabled TCs bitmap
1824  * @is_add: True if called before Add VSI
1825  *
1826  * Setup VSI queue mapping for enabled traffic classes.
1827  **/
1828 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1829 				     struct i40e_vsi_context *ctxt,
1830 				     u8 enabled_tc,
1831 				     bool is_add)
1832 {
1833 	struct i40e_pf *pf = vsi->back;
1834 	u16 num_tc_qps = 0;
1835 	u16 sections = 0;
1836 	u8 netdev_tc = 0;
1837 	u16 numtc = 1;
1838 	u16 qcount;
1839 	u8 offset;
1840 	u16 qmap;
1841 	int i;
1842 
1843 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1844 	offset = 0;
1845 	/* zero out queue mapping, it will get updated on the end of the function */
1846 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
1847 
1848 	if (vsi->type == I40E_VSI_MAIN) {
1849 		/* This code helps add more queue to the VSI if we have
1850 		 * more cores than RSS can support, the higher cores will
1851 		 * be served by ATR or other filters. Furthermore, the
1852 		 * non-zero req_queue_pairs says that user requested a new
1853 		 * queue count via ethtool's set_channels, so use this
1854 		 * value for queues distribution across traffic classes
1855 		 */
1856 		if (vsi->req_queue_pairs > 0)
1857 			vsi->num_queue_pairs = vsi->req_queue_pairs;
1858 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1859 			vsi->num_queue_pairs = pf->num_lan_msix;
1860 	}
1861 
1862 	/* Number of queues per enabled TC */
1863 	if (vsi->type == I40E_VSI_MAIN ||
1864 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
1865 		num_tc_qps = vsi->num_queue_pairs;
1866 	else
1867 		num_tc_qps = vsi->alloc_queue_pairs;
1868 
1869 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1870 		/* Find numtc from enabled TC bitmap */
1871 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1872 			if (enabled_tc & BIT(i)) /* TC is enabled */
1873 				numtc++;
1874 		}
1875 		if (!numtc) {
1876 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1877 			numtc = 1;
1878 		}
1879 		num_tc_qps = num_tc_qps / numtc;
1880 		num_tc_qps = min_t(int, num_tc_qps,
1881 				   i40e_pf_get_max_q_per_tc(pf));
1882 	}
1883 
1884 	vsi->tc_config.numtc = numtc;
1885 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1886 
1887 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
1888 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1889 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
1890 
1891 	/* Setup queue offset/count for all TCs for given VSI */
1892 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1893 		/* See if the given TC is enabled for the given VSI */
1894 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1895 			/* TC is enabled */
1896 			int pow, num_qps;
1897 
1898 			switch (vsi->type) {
1899 			case I40E_VSI_MAIN:
1900 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
1901 				    I40E_FLAG_FD_ATR_ENABLED)) ||
1902 				    vsi->tc_config.enabled_tc != 1) {
1903 					qcount = min_t(int, pf->alloc_rss_size,
1904 						       num_tc_qps);
1905 					break;
1906 				}
1907 				fallthrough;
1908 			case I40E_VSI_FDIR:
1909 			case I40E_VSI_SRIOV:
1910 			case I40E_VSI_VMDQ2:
1911 			default:
1912 				qcount = num_tc_qps;
1913 				WARN_ON(i != 0);
1914 				break;
1915 			}
1916 			vsi->tc_config.tc_info[i].qoffset = offset;
1917 			vsi->tc_config.tc_info[i].qcount = qcount;
1918 
1919 			/* find the next higher power-of-2 of num queue pairs */
1920 			num_qps = qcount;
1921 			pow = 0;
1922 			while (num_qps && (BIT_ULL(pow) < qcount)) {
1923 				pow++;
1924 				num_qps >>= 1;
1925 			}
1926 
1927 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1928 			qmap =
1929 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1930 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1931 
1932 			offset += qcount;
1933 		} else {
1934 			/* TC is not enabled so set the offset to
1935 			 * default queue and allocate one queue
1936 			 * for the given TC.
1937 			 */
1938 			vsi->tc_config.tc_info[i].qoffset = 0;
1939 			vsi->tc_config.tc_info[i].qcount = 1;
1940 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1941 
1942 			qmap = 0;
1943 		}
1944 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1945 	}
1946 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
1947 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
1948 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
1949 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
1950 		vsi->num_queue_pairs = offset;
1951 
1952 	/* Scheduler section valid can only be set for ADD VSI */
1953 	if (is_add) {
1954 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1955 
1956 		ctxt->info.up_enable_bits = enabled_tc;
1957 	}
1958 	if (vsi->type == I40E_VSI_SRIOV) {
1959 		ctxt->info.mapping_flags |=
1960 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1961 		for (i = 0; i < vsi->num_queue_pairs; i++)
1962 			ctxt->info.queue_mapping[i] =
1963 					       cpu_to_le16(vsi->base_queue + i);
1964 	} else {
1965 		ctxt->info.mapping_flags |=
1966 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1967 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1968 	}
1969 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1970 }
1971 
1972 /**
1973  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1974  * @netdev: the netdevice
1975  * @addr: address to add
1976  *
1977  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1978  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1979  */
1980 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1981 {
1982 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1983 	struct i40e_vsi *vsi = np->vsi;
1984 
1985 	if (i40e_add_mac_filter(vsi, addr))
1986 		return 0;
1987 	else
1988 		return -ENOMEM;
1989 }
1990 
1991 /**
1992  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1993  * @netdev: the netdevice
1994  * @addr: address to add
1995  *
1996  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1997  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1998  */
1999 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2000 {
2001 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2002 	struct i40e_vsi *vsi = np->vsi;
2003 
2004 	/* Under some circumstances, we might receive a request to delete
2005 	 * our own device address from our uc list. Because we store the
2006 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2007 	 * such requests and not delete our device address from this list.
2008 	 */
2009 	if (ether_addr_equal(addr, netdev->dev_addr))
2010 		return 0;
2011 
2012 	i40e_del_mac_filter(vsi, addr);
2013 
2014 	return 0;
2015 }
2016 
2017 /**
2018  * i40e_set_rx_mode - NDO callback to set the netdev filters
2019  * @netdev: network interface device structure
2020  **/
2021 static void i40e_set_rx_mode(struct net_device *netdev)
2022 {
2023 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2024 	struct i40e_vsi *vsi = np->vsi;
2025 
2026 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2027 
2028 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2029 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2030 
2031 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2032 
2033 	/* check for other flag changes */
2034 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2035 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2036 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2037 	}
2038 }
2039 
2040 /**
2041  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2042  * @vsi: Pointer to VSI struct
2043  * @from: Pointer to list which contains MAC filter entries - changes to
2044  *        those entries needs to be undone.
2045  *
2046  * MAC filter entries from this list were slated for deletion.
2047  **/
2048 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2049 					 struct hlist_head *from)
2050 {
2051 	struct i40e_mac_filter *f;
2052 	struct hlist_node *h;
2053 
2054 	hlist_for_each_entry_safe(f, h, from, hlist) {
2055 		u64 key = i40e_addr_to_hkey(f->macaddr);
2056 
2057 		/* Move the element back into MAC filter list*/
2058 		hlist_del(&f->hlist);
2059 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2060 	}
2061 }
2062 
2063 /**
2064  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2065  * @vsi: Pointer to vsi struct
2066  * @from: Pointer to list which contains MAC filter entries - changes to
2067  *        those entries needs to be undone.
2068  *
2069  * MAC filter entries from this list were slated for addition.
2070  **/
2071 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2072 					 struct hlist_head *from)
2073 {
2074 	struct i40e_new_mac_filter *new;
2075 	struct hlist_node *h;
2076 
2077 	hlist_for_each_entry_safe(new, h, from, hlist) {
2078 		/* We can simply free the wrapper structure */
2079 		hlist_del(&new->hlist);
2080 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2081 		kfree(new);
2082 	}
2083 }
2084 
2085 /**
2086  * i40e_next_filter - Get the next non-broadcast filter from a list
2087  * @next: pointer to filter in list
2088  *
2089  * Returns the next non-broadcast filter in the list. Required so that we
2090  * ignore broadcast filters within the list, since these are not handled via
2091  * the normal firmware update path.
2092  */
2093 static
2094 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2095 {
2096 	hlist_for_each_entry_continue(next, hlist) {
2097 		if (!is_broadcast_ether_addr(next->f->macaddr))
2098 			return next;
2099 	}
2100 
2101 	return NULL;
2102 }
2103 
2104 /**
2105  * i40e_update_filter_state - Update filter state based on return data
2106  * from firmware
2107  * @count: Number of filters added
2108  * @add_list: return data from fw
2109  * @add_head: pointer to first filter in current batch
2110  *
2111  * MAC filter entries from list were slated to be added to device. Returns
2112  * number of successful filters. Note that 0 does NOT mean success!
2113  **/
2114 static int
2115 i40e_update_filter_state(int count,
2116 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2117 			 struct i40e_new_mac_filter *add_head)
2118 {
2119 	int retval = 0;
2120 	int i;
2121 
2122 	for (i = 0; i < count; i++) {
2123 		/* Always check status of each filter. We don't need to check
2124 		 * the firmware return status because we pre-set the filter
2125 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2126 		 * request to the adminq. Thus, if it no longer matches then
2127 		 * we know the filter is active.
2128 		 */
2129 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2130 			add_head->state = I40E_FILTER_FAILED;
2131 		} else {
2132 			add_head->state = I40E_FILTER_ACTIVE;
2133 			retval++;
2134 		}
2135 
2136 		add_head = i40e_next_filter(add_head);
2137 		if (!add_head)
2138 			break;
2139 	}
2140 
2141 	return retval;
2142 }
2143 
2144 /**
2145  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2146  * @vsi: ptr to the VSI
2147  * @vsi_name: name to display in messages
2148  * @list: the list of filters to send to firmware
2149  * @num_del: the number of filters to delete
2150  * @retval: Set to -EIO on failure to delete
2151  *
2152  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2153  * *retval instead of a return value so that success does not force ret_val to
2154  * be set to 0. This ensures that a sequence of calls to this function
2155  * preserve the previous value of *retval on successful delete.
2156  */
2157 static
2158 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2159 			  struct i40e_aqc_remove_macvlan_element_data *list,
2160 			  int num_del, int *retval)
2161 {
2162 	struct i40e_hw *hw = &vsi->back->hw;
2163 	enum i40e_admin_queue_err aq_status;
2164 	i40e_status aq_ret;
2165 
2166 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2167 					   &aq_status);
2168 
2169 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2170 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2171 		*retval = -EIO;
2172 		dev_info(&vsi->back->pdev->dev,
2173 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2174 			 vsi_name, i40e_stat_str(hw, aq_ret),
2175 			 i40e_aq_str(hw, aq_status));
2176 	}
2177 }
2178 
2179 /**
2180  * i40e_aqc_add_filters - Request firmware to add a set of filters
2181  * @vsi: ptr to the VSI
2182  * @vsi_name: name to display in messages
2183  * @list: the list of filters to send to firmware
2184  * @add_head: Position in the add hlist
2185  * @num_add: the number of filters to add
2186  *
2187  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2188  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2189  * space for more filters.
2190  */
2191 static
2192 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2193 			  struct i40e_aqc_add_macvlan_element_data *list,
2194 			  struct i40e_new_mac_filter *add_head,
2195 			  int num_add)
2196 {
2197 	struct i40e_hw *hw = &vsi->back->hw;
2198 	enum i40e_admin_queue_err aq_status;
2199 	int fcnt;
2200 
2201 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2202 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2203 
2204 	if (fcnt != num_add) {
2205 		if (vsi->type == I40E_VSI_MAIN) {
2206 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2207 			dev_warn(&vsi->back->pdev->dev,
2208 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2209 				 i40e_aq_str(hw, aq_status), vsi_name);
2210 		} else if (vsi->type == I40E_VSI_SRIOV ||
2211 			   vsi->type == I40E_VSI_VMDQ1 ||
2212 			   vsi->type == I40E_VSI_VMDQ2) {
2213 			dev_warn(&vsi->back->pdev->dev,
2214 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2215 				 i40e_aq_str(hw, aq_status), vsi_name,
2216 					     vsi_name);
2217 		} else {
2218 			dev_warn(&vsi->back->pdev->dev,
2219 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2220 				 i40e_aq_str(hw, aq_status), vsi_name,
2221 					     vsi->type);
2222 		}
2223 	}
2224 }
2225 
2226 /**
2227  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2228  * @vsi: pointer to the VSI
2229  * @vsi_name: the VSI name
2230  * @f: filter data
2231  *
2232  * This function sets or clears the promiscuous broadcast flags for VLAN
2233  * filters in order to properly receive broadcast frames. Assumes that only
2234  * broadcast filters are passed.
2235  *
2236  * Returns status indicating success or failure;
2237  **/
2238 static i40e_status
2239 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2240 			  struct i40e_mac_filter *f)
2241 {
2242 	bool enable = f->state == I40E_FILTER_NEW;
2243 	struct i40e_hw *hw = &vsi->back->hw;
2244 	i40e_status aq_ret;
2245 
2246 	if (f->vlan == I40E_VLAN_ANY) {
2247 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2248 						   vsi->seid,
2249 						   enable,
2250 						   NULL);
2251 	} else {
2252 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2253 							    vsi->seid,
2254 							    enable,
2255 							    f->vlan,
2256 							    NULL);
2257 	}
2258 
2259 	if (aq_ret) {
2260 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2261 		dev_warn(&vsi->back->pdev->dev,
2262 			 "Error %s, forcing overflow promiscuous on %s\n",
2263 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2264 			 vsi_name);
2265 	}
2266 
2267 	return aq_ret;
2268 }
2269 
2270 /**
2271  * i40e_set_promiscuous - set promiscuous mode
2272  * @pf: board private structure
2273  * @promisc: promisc on or off
2274  *
2275  * There are different ways of setting promiscuous mode on a PF depending on
2276  * what state/environment we're in.  This identifies and sets it appropriately.
2277  * Returns 0 on success.
2278  **/
2279 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2280 {
2281 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2282 	struct i40e_hw *hw = &pf->hw;
2283 	i40e_status aq_ret;
2284 
2285 	if (vsi->type == I40E_VSI_MAIN &&
2286 	    pf->lan_veb != I40E_NO_VEB &&
2287 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2288 		/* set defport ON for Main VSI instead of true promisc
2289 		 * this way we will get all unicast/multicast and VLAN
2290 		 * promisc behavior but will not get VF or VMDq traffic
2291 		 * replicated on the Main VSI.
2292 		 */
2293 		if (promisc)
2294 			aq_ret = i40e_aq_set_default_vsi(hw,
2295 							 vsi->seid,
2296 							 NULL);
2297 		else
2298 			aq_ret = i40e_aq_clear_default_vsi(hw,
2299 							   vsi->seid,
2300 							   NULL);
2301 		if (aq_ret) {
2302 			dev_info(&pf->pdev->dev,
2303 				 "Set default VSI failed, err %s, aq_err %s\n",
2304 				 i40e_stat_str(hw, aq_ret),
2305 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2306 		}
2307 	} else {
2308 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2309 						  hw,
2310 						  vsi->seid,
2311 						  promisc, NULL,
2312 						  true);
2313 		if (aq_ret) {
2314 			dev_info(&pf->pdev->dev,
2315 				 "set unicast promisc failed, err %s, aq_err %s\n",
2316 				 i40e_stat_str(hw, aq_ret),
2317 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2318 		}
2319 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2320 						  hw,
2321 						  vsi->seid,
2322 						  promisc, NULL);
2323 		if (aq_ret) {
2324 			dev_info(&pf->pdev->dev,
2325 				 "set multicast promisc failed, err %s, aq_err %s\n",
2326 				 i40e_stat_str(hw, aq_ret),
2327 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2328 		}
2329 	}
2330 
2331 	if (!aq_ret)
2332 		pf->cur_promisc = promisc;
2333 
2334 	return aq_ret;
2335 }
2336 
2337 /**
2338  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2339  * @vsi: ptr to the VSI
2340  *
2341  * Push any outstanding VSI filter changes through the AdminQ.
2342  *
2343  * Returns 0 or error value
2344  **/
2345 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2346 {
2347 	struct hlist_head tmp_add_list, tmp_del_list;
2348 	struct i40e_mac_filter *f;
2349 	struct i40e_new_mac_filter *new, *add_head = NULL;
2350 	struct i40e_hw *hw = &vsi->back->hw;
2351 	bool old_overflow, new_overflow;
2352 	unsigned int failed_filters = 0;
2353 	unsigned int vlan_filters = 0;
2354 	char vsi_name[16] = "PF";
2355 	int filter_list_len = 0;
2356 	i40e_status aq_ret = 0;
2357 	u32 changed_flags = 0;
2358 	struct hlist_node *h;
2359 	struct i40e_pf *pf;
2360 	int num_add = 0;
2361 	int num_del = 0;
2362 	int retval = 0;
2363 	u16 cmd_flags;
2364 	int list_size;
2365 	int bkt;
2366 
2367 	/* empty array typed pointers, kcalloc later */
2368 	struct i40e_aqc_add_macvlan_element_data *add_list;
2369 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2370 
2371 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2372 		usleep_range(1000, 2000);
2373 	pf = vsi->back;
2374 
2375 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2376 
2377 	if (vsi->netdev) {
2378 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2379 		vsi->current_netdev_flags = vsi->netdev->flags;
2380 	}
2381 
2382 	INIT_HLIST_HEAD(&tmp_add_list);
2383 	INIT_HLIST_HEAD(&tmp_del_list);
2384 
2385 	if (vsi->type == I40E_VSI_SRIOV)
2386 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2387 	else if (vsi->type != I40E_VSI_MAIN)
2388 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2389 
2390 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2391 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2392 
2393 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2394 		/* Create a list of filters to delete. */
2395 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2396 			if (f->state == I40E_FILTER_REMOVE) {
2397 				/* Move the element into temporary del_list */
2398 				hash_del(&f->hlist);
2399 				hlist_add_head(&f->hlist, &tmp_del_list);
2400 
2401 				/* Avoid counting removed filters */
2402 				continue;
2403 			}
2404 			if (f->state == I40E_FILTER_NEW) {
2405 				/* Create a temporary i40e_new_mac_filter */
2406 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2407 				if (!new)
2408 					goto err_no_memory_locked;
2409 
2410 				/* Store pointer to the real filter */
2411 				new->f = f;
2412 				new->state = f->state;
2413 
2414 				/* Add it to the hash list */
2415 				hlist_add_head(&new->hlist, &tmp_add_list);
2416 			}
2417 
2418 			/* Count the number of active (current and new) VLAN
2419 			 * filters we have now. Does not count filters which
2420 			 * are marked for deletion.
2421 			 */
2422 			if (f->vlan > 0)
2423 				vlan_filters++;
2424 		}
2425 
2426 		retval = i40e_correct_mac_vlan_filters(vsi,
2427 						       &tmp_add_list,
2428 						       &tmp_del_list,
2429 						       vlan_filters);
2430 
2431 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2432 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2433 
2434 		if (retval)
2435 			goto err_no_memory_locked;
2436 
2437 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2438 	}
2439 
2440 	/* Now process 'del_list' outside the lock */
2441 	if (!hlist_empty(&tmp_del_list)) {
2442 		filter_list_len = hw->aq.asq_buf_size /
2443 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2444 		list_size = filter_list_len *
2445 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2446 		del_list = kzalloc(list_size, GFP_ATOMIC);
2447 		if (!del_list)
2448 			goto err_no_memory;
2449 
2450 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2451 			cmd_flags = 0;
2452 
2453 			/* handle broadcast filters by updating the broadcast
2454 			 * promiscuous flag and release filter list.
2455 			 */
2456 			if (is_broadcast_ether_addr(f->macaddr)) {
2457 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2458 
2459 				hlist_del(&f->hlist);
2460 				kfree(f);
2461 				continue;
2462 			}
2463 
2464 			/* add to delete list */
2465 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2466 			if (f->vlan == I40E_VLAN_ANY) {
2467 				del_list[num_del].vlan_tag = 0;
2468 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2469 			} else {
2470 				del_list[num_del].vlan_tag =
2471 					cpu_to_le16((u16)(f->vlan));
2472 			}
2473 
2474 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2475 			del_list[num_del].flags = cmd_flags;
2476 			num_del++;
2477 
2478 			/* flush a full buffer */
2479 			if (num_del == filter_list_len) {
2480 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2481 						     num_del, &retval);
2482 				memset(del_list, 0, list_size);
2483 				num_del = 0;
2484 			}
2485 			/* Release memory for MAC filter entries which were
2486 			 * synced up with HW.
2487 			 */
2488 			hlist_del(&f->hlist);
2489 			kfree(f);
2490 		}
2491 
2492 		if (num_del) {
2493 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2494 					     num_del, &retval);
2495 		}
2496 
2497 		kfree(del_list);
2498 		del_list = NULL;
2499 	}
2500 
2501 	if (!hlist_empty(&tmp_add_list)) {
2502 		/* Do all the adds now. */
2503 		filter_list_len = hw->aq.asq_buf_size /
2504 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2505 		list_size = filter_list_len *
2506 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2507 		add_list = kzalloc(list_size, GFP_ATOMIC);
2508 		if (!add_list)
2509 			goto err_no_memory;
2510 
2511 		num_add = 0;
2512 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2513 			/* handle broadcast filters by updating the broadcast
2514 			 * promiscuous flag instead of adding a MAC filter.
2515 			 */
2516 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2517 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2518 							      new->f))
2519 					new->state = I40E_FILTER_FAILED;
2520 				else
2521 					new->state = I40E_FILTER_ACTIVE;
2522 				continue;
2523 			}
2524 
2525 			/* add to add array */
2526 			if (num_add == 0)
2527 				add_head = new;
2528 			cmd_flags = 0;
2529 			ether_addr_copy(add_list[num_add].mac_addr,
2530 					new->f->macaddr);
2531 			if (new->f->vlan == I40E_VLAN_ANY) {
2532 				add_list[num_add].vlan_tag = 0;
2533 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2534 			} else {
2535 				add_list[num_add].vlan_tag =
2536 					cpu_to_le16((u16)(new->f->vlan));
2537 			}
2538 			add_list[num_add].queue_number = 0;
2539 			/* set invalid match method for later detection */
2540 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2541 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2542 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2543 			num_add++;
2544 
2545 			/* flush a full buffer */
2546 			if (num_add == filter_list_len) {
2547 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2548 						     add_head, num_add);
2549 				memset(add_list, 0, list_size);
2550 				num_add = 0;
2551 			}
2552 		}
2553 		if (num_add) {
2554 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2555 					     num_add);
2556 		}
2557 		/* Now move all of the filters from the temp add list back to
2558 		 * the VSI's list.
2559 		 */
2560 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2561 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2562 			/* Only update the state if we're still NEW */
2563 			if (new->f->state == I40E_FILTER_NEW)
2564 				new->f->state = new->state;
2565 			hlist_del(&new->hlist);
2566 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2567 			kfree(new);
2568 		}
2569 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2570 		kfree(add_list);
2571 		add_list = NULL;
2572 	}
2573 
2574 	/* Determine the number of active and failed filters. */
2575 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2576 	vsi->active_filters = 0;
2577 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2578 		if (f->state == I40E_FILTER_ACTIVE)
2579 			vsi->active_filters++;
2580 		else if (f->state == I40E_FILTER_FAILED)
2581 			failed_filters++;
2582 	}
2583 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2584 
2585 	/* Check if we are able to exit overflow promiscuous mode. We can
2586 	 * safely exit if we didn't just enter, we no longer have any failed
2587 	 * filters, and we have reduced filters below the threshold value.
2588 	 */
2589 	if (old_overflow && !failed_filters &&
2590 	    vsi->active_filters < vsi->promisc_threshold) {
2591 		dev_info(&pf->pdev->dev,
2592 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2593 			 vsi_name);
2594 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2595 		vsi->promisc_threshold = 0;
2596 	}
2597 
2598 	/* if the VF is not trusted do not do promisc */
2599 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2600 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2601 		goto out;
2602 	}
2603 
2604 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2605 
2606 	/* If we are entering overflow promiscuous, we need to calculate a new
2607 	 * threshold for when we are safe to exit
2608 	 */
2609 	if (!old_overflow && new_overflow)
2610 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2611 
2612 	/* check for changes in promiscuous modes */
2613 	if (changed_flags & IFF_ALLMULTI) {
2614 		bool cur_multipromisc;
2615 
2616 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2617 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2618 							       vsi->seid,
2619 							       cur_multipromisc,
2620 							       NULL);
2621 		if (aq_ret) {
2622 			retval = i40e_aq_rc_to_posix(aq_ret,
2623 						     hw->aq.asq_last_status);
2624 			dev_info(&pf->pdev->dev,
2625 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2626 				 vsi_name,
2627 				 i40e_stat_str(hw, aq_ret),
2628 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2629 		} else {
2630 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2631 				 cur_multipromisc ? "entering" : "leaving");
2632 		}
2633 	}
2634 
2635 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2636 		bool cur_promisc;
2637 
2638 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2639 			       new_overflow);
2640 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2641 		if (aq_ret) {
2642 			retval = i40e_aq_rc_to_posix(aq_ret,
2643 						     hw->aq.asq_last_status);
2644 			dev_info(&pf->pdev->dev,
2645 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2646 				 cur_promisc ? "on" : "off",
2647 				 vsi_name,
2648 				 i40e_stat_str(hw, aq_ret),
2649 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2650 		}
2651 	}
2652 out:
2653 	/* if something went wrong then set the changed flag so we try again */
2654 	if (retval)
2655 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2656 
2657 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2658 	return retval;
2659 
2660 err_no_memory:
2661 	/* Restore elements on the temporary add and delete lists */
2662 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2663 err_no_memory_locked:
2664 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2665 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2666 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2667 
2668 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2669 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2670 	return -ENOMEM;
2671 }
2672 
2673 /**
2674  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2675  * @pf: board private structure
2676  **/
2677 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2678 {
2679 	int v;
2680 
2681 	if (!pf)
2682 		return;
2683 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2684 		return;
2685 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2686 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2687 		return;
2688 	}
2689 
2690 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2691 		if (pf->vsi[v] &&
2692 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2693 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2694 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2695 
2696 			if (ret) {
2697 				/* come back and try again later */
2698 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2699 					pf->state);
2700 				break;
2701 			}
2702 		}
2703 	}
2704 }
2705 
2706 /**
2707  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2708  * @vsi: the vsi
2709  **/
2710 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2711 {
2712 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2713 		return I40E_RXBUFFER_2048;
2714 	else
2715 		return I40E_RXBUFFER_3072;
2716 }
2717 
2718 /**
2719  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2720  * @netdev: network interface device structure
2721  * @new_mtu: new value for maximum frame size
2722  *
2723  * Returns 0 on success, negative on failure
2724  **/
2725 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2726 {
2727 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2728 	struct i40e_vsi *vsi = np->vsi;
2729 	struct i40e_pf *pf = vsi->back;
2730 
2731 	if (i40e_enabled_xdp_vsi(vsi)) {
2732 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2733 
2734 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2735 			return -EINVAL;
2736 	}
2737 
2738 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2739 		   netdev->mtu, new_mtu);
2740 	netdev->mtu = new_mtu;
2741 	if (netif_running(netdev))
2742 		i40e_vsi_reinit_locked(vsi);
2743 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2744 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2745 	return 0;
2746 }
2747 
2748 /**
2749  * i40e_ioctl - Access the hwtstamp interface
2750  * @netdev: network interface device structure
2751  * @ifr: interface request data
2752  * @cmd: ioctl command
2753  **/
2754 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2755 {
2756 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2757 	struct i40e_pf *pf = np->vsi->back;
2758 
2759 	switch (cmd) {
2760 	case SIOCGHWTSTAMP:
2761 		return i40e_ptp_get_ts_config(pf, ifr);
2762 	case SIOCSHWTSTAMP:
2763 		return i40e_ptp_set_ts_config(pf, ifr);
2764 	default:
2765 		return -EOPNOTSUPP;
2766 	}
2767 }
2768 
2769 /**
2770  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2771  * @vsi: the vsi being adjusted
2772  **/
2773 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2774 {
2775 	struct i40e_vsi_context ctxt;
2776 	i40e_status ret;
2777 
2778 	/* Don't modify stripping options if a port VLAN is active */
2779 	if (vsi->info.pvid)
2780 		return;
2781 
2782 	if ((vsi->info.valid_sections &
2783 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2784 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2785 		return;  /* already enabled */
2786 
2787 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2788 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2789 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2790 
2791 	ctxt.seid = vsi->seid;
2792 	ctxt.info = vsi->info;
2793 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2794 	if (ret) {
2795 		dev_info(&vsi->back->pdev->dev,
2796 			 "update vlan stripping failed, err %s aq_err %s\n",
2797 			 i40e_stat_str(&vsi->back->hw, ret),
2798 			 i40e_aq_str(&vsi->back->hw,
2799 				     vsi->back->hw.aq.asq_last_status));
2800 	}
2801 }
2802 
2803 /**
2804  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2805  * @vsi: the vsi being adjusted
2806  **/
2807 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2808 {
2809 	struct i40e_vsi_context ctxt;
2810 	i40e_status ret;
2811 
2812 	/* Don't modify stripping options if a port VLAN is active */
2813 	if (vsi->info.pvid)
2814 		return;
2815 
2816 	if ((vsi->info.valid_sections &
2817 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2818 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2819 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
2820 		return;  /* already disabled */
2821 
2822 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2823 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2824 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2825 
2826 	ctxt.seid = vsi->seid;
2827 	ctxt.info = vsi->info;
2828 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2829 	if (ret) {
2830 		dev_info(&vsi->back->pdev->dev,
2831 			 "update vlan stripping failed, err %s aq_err %s\n",
2832 			 i40e_stat_str(&vsi->back->hw, ret),
2833 			 i40e_aq_str(&vsi->back->hw,
2834 				     vsi->back->hw.aq.asq_last_status));
2835 	}
2836 }
2837 
2838 /**
2839  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2840  * @vsi: the vsi being configured
2841  * @vid: vlan id to be added (0 = untagged only , -1 = any)
2842  *
2843  * This is a helper function for adding a new MAC/VLAN filter with the
2844  * specified VLAN for each existing MAC address already in the hash table.
2845  * This function does *not* perform any accounting to update filters based on
2846  * VLAN mode.
2847  *
2848  * NOTE: this function expects to be called while under the
2849  * mac_filter_hash_lock
2850  **/
2851 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2852 {
2853 	struct i40e_mac_filter *f, *add_f;
2854 	struct hlist_node *h;
2855 	int bkt;
2856 
2857 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2858 		if (f->state == I40E_FILTER_REMOVE)
2859 			continue;
2860 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
2861 		if (!add_f) {
2862 			dev_info(&vsi->back->pdev->dev,
2863 				 "Could not add vlan filter %d for %pM\n",
2864 				 vid, f->macaddr);
2865 			return -ENOMEM;
2866 		}
2867 	}
2868 
2869 	return 0;
2870 }
2871 
2872 /**
2873  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2874  * @vsi: the VSI being configured
2875  * @vid: VLAN id to be added
2876  **/
2877 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
2878 {
2879 	int err;
2880 
2881 	if (vsi->info.pvid)
2882 		return -EINVAL;
2883 
2884 	/* The network stack will attempt to add VID=0, with the intention to
2885 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
2886 	 * these packets by default when configured to receive untagged
2887 	 * packets, so we don't need to add a filter for this case.
2888 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
2889 	 * receive *only* tagged traffic and stops receiving untagged traffic.
2890 	 * Thus, we do not want to actually add a filter for VID=0
2891 	 */
2892 	if (!vid)
2893 		return 0;
2894 
2895 	/* Locked once because all functions invoked below iterates list*/
2896 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2897 	err = i40e_add_vlan_all_mac(vsi, vid);
2898 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2899 	if (err)
2900 		return err;
2901 
2902 	/* schedule our worker thread which will take care of
2903 	 * applying the new filter changes
2904 	 */
2905 	i40e_service_event_schedule(vsi->back);
2906 	return 0;
2907 }
2908 
2909 /**
2910  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
2911  * @vsi: the vsi being configured
2912  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2913  *
2914  * This function should be used to remove all VLAN filters which match the
2915  * given VID. It does not schedule the service event and does not take the
2916  * mac_filter_hash_lock so it may be combined with other operations under
2917  * a single invocation of the mac_filter_hash_lock.
2918  *
2919  * NOTE: this function expects to be called while under the
2920  * mac_filter_hash_lock
2921  */
2922 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2923 {
2924 	struct i40e_mac_filter *f;
2925 	struct hlist_node *h;
2926 	int bkt;
2927 
2928 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2929 		if (f->vlan == vid)
2930 			__i40e_del_filter(vsi, f);
2931 	}
2932 }
2933 
2934 /**
2935  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2936  * @vsi: the VSI being configured
2937  * @vid: VLAN id to be removed
2938  **/
2939 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
2940 {
2941 	if (!vid || vsi->info.pvid)
2942 		return;
2943 
2944 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2945 	i40e_rm_vlan_all_mac(vsi, vid);
2946 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2947 
2948 	/* schedule our worker thread which will take care of
2949 	 * applying the new filter changes
2950 	 */
2951 	i40e_service_event_schedule(vsi->back);
2952 }
2953 
2954 /**
2955  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2956  * @netdev: network interface to be adjusted
2957  * @proto: unused protocol value
2958  * @vid: vlan id to be added
2959  *
2960  * net_device_ops implementation for adding vlan ids
2961  **/
2962 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2963 				__always_unused __be16 proto, u16 vid)
2964 {
2965 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2966 	struct i40e_vsi *vsi = np->vsi;
2967 	int ret = 0;
2968 
2969 	if (vid >= VLAN_N_VID)
2970 		return -EINVAL;
2971 
2972 	ret = i40e_vsi_add_vlan(vsi, vid);
2973 	if (!ret)
2974 		set_bit(vid, vsi->active_vlans);
2975 
2976 	return ret;
2977 }
2978 
2979 /**
2980  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
2981  * @netdev: network interface to be adjusted
2982  * @proto: unused protocol value
2983  * @vid: vlan id to be added
2984  **/
2985 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
2986 				    __always_unused __be16 proto, u16 vid)
2987 {
2988 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2989 	struct i40e_vsi *vsi = np->vsi;
2990 
2991 	if (vid >= VLAN_N_VID)
2992 		return;
2993 	set_bit(vid, vsi->active_vlans);
2994 }
2995 
2996 /**
2997  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2998  * @netdev: network interface to be adjusted
2999  * @proto: unused protocol value
3000  * @vid: vlan id to be removed
3001  *
3002  * net_device_ops implementation for removing vlan ids
3003  **/
3004 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3005 				 __always_unused __be16 proto, u16 vid)
3006 {
3007 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3008 	struct i40e_vsi *vsi = np->vsi;
3009 
3010 	/* return code is ignored as there is nothing a user
3011 	 * can do about failure to remove and a log message was
3012 	 * already printed from the other function
3013 	 */
3014 	i40e_vsi_kill_vlan(vsi, vid);
3015 
3016 	clear_bit(vid, vsi->active_vlans);
3017 
3018 	return 0;
3019 }
3020 
3021 /**
3022  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3023  * @vsi: the vsi being brought back up
3024  **/
3025 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3026 {
3027 	u16 vid;
3028 
3029 	if (!vsi->netdev)
3030 		return;
3031 
3032 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3033 		i40e_vlan_stripping_enable(vsi);
3034 	else
3035 		i40e_vlan_stripping_disable(vsi);
3036 
3037 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3038 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3039 					vid);
3040 }
3041 
3042 /**
3043  * i40e_vsi_add_pvid - Add pvid for the VSI
3044  * @vsi: the vsi being adjusted
3045  * @vid: the vlan id to set as a PVID
3046  **/
3047 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3048 {
3049 	struct i40e_vsi_context ctxt;
3050 	i40e_status ret;
3051 
3052 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3053 	vsi->info.pvid = cpu_to_le16(vid);
3054 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3055 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3056 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3057 
3058 	ctxt.seid = vsi->seid;
3059 	ctxt.info = vsi->info;
3060 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3061 	if (ret) {
3062 		dev_info(&vsi->back->pdev->dev,
3063 			 "add pvid failed, err %s aq_err %s\n",
3064 			 i40e_stat_str(&vsi->back->hw, ret),
3065 			 i40e_aq_str(&vsi->back->hw,
3066 				     vsi->back->hw.aq.asq_last_status));
3067 		return -ENOENT;
3068 	}
3069 
3070 	return 0;
3071 }
3072 
3073 /**
3074  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3075  * @vsi: the vsi being adjusted
3076  *
3077  * Just use the vlan_rx_register() service to put it back to normal
3078  **/
3079 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3080 {
3081 	vsi->info.pvid = 0;
3082 
3083 	i40e_vlan_stripping_disable(vsi);
3084 }
3085 
3086 /**
3087  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3088  * @vsi: ptr to the VSI
3089  *
3090  * If this function returns with an error, then it's possible one or
3091  * more of the rings is populated (while the rest are not).  It is the
3092  * callers duty to clean those orphaned rings.
3093  *
3094  * Return 0 on success, negative on failure
3095  **/
3096 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3097 {
3098 	int i, err = 0;
3099 
3100 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3101 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3102 
3103 	if (!i40e_enabled_xdp_vsi(vsi))
3104 		return err;
3105 
3106 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3107 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3108 
3109 	return err;
3110 }
3111 
3112 /**
3113  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3114  * @vsi: ptr to the VSI
3115  *
3116  * Free VSI's transmit software resources
3117  **/
3118 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3119 {
3120 	int i;
3121 
3122 	if (vsi->tx_rings) {
3123 		for (i = 0; i < vsi->num_queue_pairs; i++)
3124 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3125 				i40e_free_tx_resources(vsi->tx_rings[i]);
3126 	}
3127 
3128 	if (vsi->xdp_rings) {
3129 		for (i = 0; i < vsi->num_queue_pairs; i++)
3130 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3131 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3132 	}
3133 }
3134 
3135 /**
3136  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3137  * @vsi: ptr to the VSI
3138  *
3139  * If this function returns with an error, then it's possible one or
3140  * more of the rings is populated (while the rest are not).  It is the
3141  * callers duty to clean those orphaned rings.
3142  *
3143  * Return 0 on success, negative on failure
3144  **/
3145 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3146 {
3147 	int i, err = 0;
3148 
3149 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3150 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3151 	return err;
3152 }
3153 
3154 /**
3155  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3156  * @vsi: ptr to the VSI
3157  *
3158  * Free all receive software resources
3159  **/
3160 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3161 {
3162 	int i;
3163 
3164 	if (!vsi->rx_rings)
3165 		return;
3166 
3167 	for (i = 0; i < vsi->num_queue_pairs; i++)
3168 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3169 			i40e_free_rx_resources(vsi->rx_rings[i]);
3170 }
3171 
3172 /**
3173  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3174  * @ring: The Tx ring to configure
3175  *
3176  * This enables/disables XPS for a given Tx descriptor ring
3177  * based on the TCs enabled for the VSI that ring belongs to.
3178  **/
3179 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3180 {
3181 	int cpu;
3182 
3183 	if (!ring->q_vector || !ring->netdev || ring->ch)
3184 		return;
3185 
3186 	/* We only initialize XPS once, so as not to overwrite user settings */
3187 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3188 		return;
3189 
3190 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3191 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3192 			    ring->queue_index);
3193 }
3194 
3195 /**
3196  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3197  * @ring: The Tx or Rx ring
3198  *
3199  * Returns the AF_XDP buffer pool or NULL.
3200  **/
3201 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3202 {
3203 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3204 	int qid = ring->queue_index;
3205 
3206 	if (ring_is_xdp(ring))
3207 		qid -= ring->vsi->alloc_queue_pairs;
3208 
3209 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3210 		return NULL;
3211 
3212 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3213 }
3214 
3215 /**
3216  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3217  * @ring: The Tx ring to configure
3218  *
3219  * Configure the Tx descriptor ring in the HMC context.
3220  **/
3221 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3222 {
3223 	struct i40e_vsi *vsi = ring->vsi;
3224 	u16 pf_q = vsi->base_queue + ring->queue_index;
3225 	struct i40e_hw *hw = &vsi->back->hw;
3226 	struct i40e_hmc_obj_txq tx_ctx;
3227 	i40e_status err = 0;
3228 	u32 qtx_ctl = 0;
3229 
3230 	if (ring_is_xdp(ring))
3231 		ring->xsk_pool = i40e_xsk_pool(ring);
3232 
3233 	/* some ATR related tx ring init */
3234 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3235 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3236 		ring->atr_count = 0;
3237 	} else {
3238 		ring->atr_sample_rate = 0;
3239 	}
3240 
3241 	/* configure XPS */
3242 	i40e_config_xps_tx_ring(ring);
3243 
3244 	/* clear the context structure first */
3245 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3246 
3247 	tx_ctx.new_context = 1;
3248 	tx_ctx.base = (ring->dma / 128);
3249 	tx_ctx.qlen = ring->count;
3250 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3251 					       I40E_FLAG_FD_ATR_ENABLED));
3252 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3253 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3254 	if (vsi->type != I40E_VSI_FDIR)
3255 		tx_ctx.head_wb_ena = 1;
3256 	tx_ctx.head_wb_addr = ring->dma +
3257 			      (ring->count * sizeof(struct i40e_tx_desc));
3258 
3259 	/* As part of VSI creation/update, FW allocates certain
3260 	 * Tx arbitration queue sets for each TC enabled for
3261 	 * the VSI. The FW returns the handles to these queue
3262 	 * sets as part of the response buffer to Add VSI,
3263 	 * Update VSI, etc. AQ commands. It is expected that
3264 	 * these queue set handles be associated with the Tx
3265 	 * queues by the driver as part of the TX queue context
3266 	 * initialization. This has to be done regardless of
3267 	 * DCB as by default everything is mapped to TC0.
3268 	 */
3269 
3270 	if (ring->ch)
3271 		tx_ctx.rdylist =
3272 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3273 
3274 	else
3275 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3276 
3277 	tx_ctx.rdylist_act = 0;
3278 
3279 	/* clear the context in the HMC */
3280 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3281 	if (err) {
3282 		dev_info(&vsi->back->pdev->dev,
3283 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3284 			 ring->queue_index, pf_q, err);
3285 		return -ENOMEM;
3286 	}
3287 
3288 	/* set the context in the HMC */
3289 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3290 	if (err) {
3291 		dev_info(&vsi->back->pdev->dev,
3292 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3293 			 ring->queue_index, pf_q, err);
3294 		return -ENOMEM;
3295 	}
3296 
3297 	/* Now associate this queue with this PCI function */
3298 	if (ring->ch) {
3299 		if (ring->ch->type == I40E_VSI_VMDQ2)
3300 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3301 		else
3302 			return -EINVAL;
3303 
3304 		qtx_ctl |= (ring->ch->vsi_number <<
3305 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3306 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3307 	} else {
3308 		if (vsi->type == I40E_VSI_VMDQ2) {
3309 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3310 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3311 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3312 		} else {
3313 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3314 		}
3315 	}
3316 
3317 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3318 		    I40E_QTX_CTL_PF_INDX_MASK);
3319 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3320 	i40e_flush(hw);
3321 
3322 	/* cache tail off for easier writes later */
3323 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3324 
3325 	return 0;
3326 }
3327 
3328 /**
3329  * i40e_rx_offset - Return expected offset into page to access data
3330  * @rx_ring: Ring we are requesting offset of
3331  *
3332  * Returns the offset value for ring into the data buffer.
3333  */
3334 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3335 {
3336 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3337 }
3338 
3339 /**
3340  * i40e_configure_rx_ring - Configure a receive ring context
3341  * @ring: The Rx ring to configure
3342  *
3343  * Configure the Rx descriptor ring in the HMC context.
3344  **/
3345 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3346 {
3347 	struct i40e_vsi *vsi = ring->vsi;
3348 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3349 	u16 pf_q = vsi->base_queue + ring->queue_index;
3350 	struct i40e_hw *hw = &vsi->back->hw;
3351 	struct i40e_hmc_obj_rxq rx_ctx;
3352 	i40e_status err = 0;
3353 	bool ok;
3354 	int ret;
3355 
3356 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3357 
3358 	/* clear the context structure first */
3359 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3360 
3361 	if (ring->vsi->type == I40E_VSI_MAIN)
3362 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3363 
3364 	kfree(ring->rx_bi);
3365 	ring->xsk_pool = i40e_xsk_pool(ring);
3366 	if (ring->xsk_pool) {
3367 		ret = i40e_alloc_rx_bi_zc(ring);
3368 		if (ret)
3369 			return ret;
3370 		ring->rx_buf_len =
3371 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3372 		/* For AF_XDP ZC, we disallow packets to span on
3373 		 * multiple buffers, thus letting us skip that
3374 		 * handling in the fast-path.
3375 		 */
3376 		chain_len = 1;
3377 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3378 						 MEM_TYPE_XSK_BUFF_POOL,
3379 						 NULL);
3380 		if (ret)
3381 			return ret;
3382 		dev_info(&vsi->back->pdev->dev,
3383 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3384 			 ring->queue_index);
3385 
3386 	} else {
3387 		ret = i40e_alloc_rx_bi(ring);
3388 		if (ret)
3389 			return ret;
3390 		ring->rx_buf_len = vsi->rx_buf_len;
3391 		if (ring->vsi->type == I40E_VSI_MAIN) {
3392 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3393 							 MEM_TYPE_PAGE_SHARED,
3394 							 NULL);
3395 			if (ret)
3396 				return ret;
3397 		}
3398 	}
3399 
3400 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3401 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3402 
3403 	rx_ctx.base = (ring->dma / 128);
3404 	rx_ctx.qlen = ring->count;
3405 
3406 	/* use 16 byte descriptors */
3407 	rx_ctx.dsize = 0;
3408 
3409 	/* descriptor type is always zero
3410 	 * rx_ctx.dtype = 0;
3411 	 */
3412 	rx_ctx.hsplit_0 = 0;
3413 
3414 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3415 	if (hw->revision_id == 0)
3416 		rx_ctx.lrxqthresh = 0;
3417 	else
3418 		rx_ctx.lrxqthresh = 1;
3419 	rx_ctx.crcstrip = 1;
3420 	rx_ctx.l2tsel = 1;
3421 	/* this controls whether VLAN is stripped from inner headers */
3422 	rx_ctx.showiv = 0;
3423 	/* set the prefena field to 1 because the manual says to */
3424 	rx_ctx.prefena = 1;
3425 
3426 	/* clear the context in the HMC */
3427 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3428 	if (err) {
3429 		dev_info(&vsi->back->pdev->dev,
3430 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3431 			 ring->queue_index, pf_q, err);
3432 		return -ENOMEM;
3433 	}
3434 
3435 	/* set the context in the HMC */
3436 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3437 	if (err) {
3438 		dev_info(&vsi->back->pdev->dev,
3439 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3440 			 ring->queue_index, pf_q, err);
3441 		return -ENOMEM;
3442 	}
3443 
3444 	/* configure Rx buffer alignment */
3445 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3446 		clear_ring_build_skb_enabled(ring);
3447 	else
3448 		set_ring_build_skb_enabled(ring);
3449 
3450 	ring->rx_offset = i40e_rx_offset(ring);
3451 
3452 	/* cache tail for quicker writes, and clear the reg before use */
3453 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3454 	writel(0, ring->tail);
3455 
3456 	if (ring->xsk_pool) {
3457 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3458 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3459 	} else {
3460 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3461 	}
3462 	if (!ok) {
3463 		/* Log this in case the user has forgotten to give the kernel
3464 		 * any buffers, even later in the application.
3465 		 */
3466 		dev_info(&vsi->back->pdev->dev,
3467 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3468 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3469 			 ring->queue_index, pf_q);
3470 	}
3471 
3472 	return 0;
3473 }
3474 
3475 /**
3476  * i40e_vsi_configure_tx - Configure the VSI for Tx
3477  * @vsi: VSI structure describing this set of rings and resources
3478  *
3479  * Configure the Tx VSI for operation.
3480  **/
3481 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3482 {
3483 	int err = 0;
3484 	u16 i;
3485 
3486 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3487 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3488 
3489 	if (err || !i40e_enabled_xdp_vsi(vsi))
3490 		return err;
3491 
3492 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3493 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3494 
3495 	return err;
3496 }
3497 
3498 /**
3499  * i40e_vsi_configure_rx - Configure the VSI for Rx
3500  * @vsi: the VSI being configured
3501  *
3502  * Configure the Rx VSI for operation.
3503  **/
3504 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3505 {
3506 	int err = 0;
3507 	u16 i;
3508 
3509 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3510 		vsi->max_frame = I40E_MAX_RXBUFFER;
3511 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3512 #if (PAGE_SIZE < 8192)
3513 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3514 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3515 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3516 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3517 #endif
3518 	} else {
3519 		vsi->max_frame = I40E_MAX_RXBUFFER;
3520 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3521 						       I40E_RXBUFFER_2048;
3522 	}
3523 
3524 	/* set up individual rings */
3525 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3526 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3527 
3528 	return err;
3529 }
3530 
3531 /**
3532  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3533  * @vsi: ptr to the VSI
3534  **/
3535 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3536 {
3537 	struct i40e_ring *tx_ring, *rx_ring;
3538 	u16 qoffset, qcount;
3539 	int i, n;
3540 
3541 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3542 		/* Reset the TC information */
3543 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3544 			rx_ring = vsi->rx_rings[i];
3545 			tx_ring = vsi->tx_rings[i];
3546 			rx_ring->dcb_tc = 0;
3547 			tx_ring->dcb_tc = 0;
3548 		}
3549 		return;
3550 	}
3551 
3552 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3553 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3554 			continue;
3555 
3556 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3557 		qcount = vsi->tc_config.tc_info[n].qcount;
3558 		for (i = qoffset; i < (qoffset + qcount); i++) {
3559 			rx_ring = vsi->rx_rings[i];
3560 			tx_ring = vsi->tx_rings[i];
3561 			rx_ring->dcb_tc = n;
3562 			tx_ring->dcb_tc = n;
3563 		}
3564 	}
3565 }
3566 
3567 /**
3568  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3569  * @vsi: ptr to the VSI
3570  **/
3571 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3572 {
3573 	if (vsi->netdev)
3574 		i40e_set_rx_mode(vsi->netdev);
3575 }
3576 
3577 /**
3578  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3579  * @pf: Pointer to the targeted PF
3580  *
3581  * Set all flow director counters to 0.
3582  */
3583 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3584 {
3585 	pf->fd_tcp4_filter_cnt = 0;
3586 	pf->fd_udp4_filter_cnt = 0;
3587 	pf->fd_sctp4_filter_cnt = 0;
3588 	pf->fd_ip4_filter_cnt = 0;
3589 	pf->fd_tcp6_filter_cnt = 0;
3590 	pf->fd_udp6_filter_cnt = 0;
3591 	pf->fd_sctp6_filter_cnt = 0;
3592 	pf->fd_ip6_filter_cnt = 0;
3593 }
3594 
3595 /**
3596  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3597  * @vsi: Pointer to the targeted VSI
3598  *
3599  * This function replays the hlist on the hw where all the SB Flow Director
3600  * filters were saved.
3601  **/
3602 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3603 {
3604 	struct i40e_fdir_filter *filter;
3605 	struct i40e_pf *pf = vsi->back;
3606 	struct hlist_node *node;
3607 
3608 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3609 		return;
3610 
3611 	/* Reset FDir counters as we're replaying all existing filters */
3612 	i40e_reset_fdir_filter_cnt(pf);
3613 
3614 	hlist_for_each_entry_safe(filter, node,
3615 				  &pf->fdir_filter_list, fdir_node) {
3616 		i40e_add_del_fdir(vsi, filter, true);
3617 	}
3618 }
3619 
3620 /**
3621  * i40e_vsi_configure - Set up the VSI for action
3622  * @vsi: the VSI being configured
3623  **/
3624 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3625 {
3626 	int err;
3627 
3628 	i40e_set_vsi_rx_mode(vsi);
3629 	i40e_restore_vlan(vsi);
3630 	i40e_vsi_config_dcb_rings(vsi);
3631 	err = i40e_vsi_configure_tx(vsi);
3632 	if (!err)
3633 		err = i40e_vsi_configure_rx(vsi);
3634 
3635 	return err;
3636 }
3637 
3638 /**
3639  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3640  * @vsi: the VSI being configured
3641  **/
3642 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3643 {
3644 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3645 	struct i40e_pf *pf = vsi->back;
3646 	struct i40e_hw *hw = &pf->hw;
3647 	u16 vector;
3648 	int i, q;
3649 	u32 qp;
3650 
3651 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3652 	 * and PFINT_LNKLSTn registers, e.g.:
3653 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3654 	 */
3655 	qp = vsi->base_queue;
3656 	vector = vsi->base_vector;
3657 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3658 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3659 
3660 		q_vector->rx.next_update = jiffies + 1;
3661 		q_vector->rx.target_itr =
3662 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3663 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3664 		     q_vector->rx.target_itr >> 1);
3665 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3666 
3667 		q_vector->tx.next_update = jiffies + 1;
3668 		q_vector->tx.target_itr =
3669 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3670 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3671 		     q_vector->tx.target_itr >> 1);
3672 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3673 
3674 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3675 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3676 
3677 		/* Linked list for the queuepairs assigned to this vector */
3678 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3679 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3680 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3681 			u32 val;
3682 
3683 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3684 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3685 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3686 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3687 			      (I40E_QUEUE_TYPE_TX <<
3688 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3689 
3690 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3691 
3692 			if (has_xdp) {
3693 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3694 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3695 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3696 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3697 				      (I40E_QUEUE_TYPE_TX <<
3698 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3699 
3700 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3701 			}
3702 
3703 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3704 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3705 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3706 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3707 			      (I40E_QUEUE_TYPE_RX <<
3708 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3709 
3710 			/* Terminate the linked list */
3711 			if (q == (q_vector->num_ringpairs - 1))
3712 				val |= (I40E_QUEUE_END_OF_LIST <<
3713 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3714 
3715 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3716 			qp++;
3717 		}
3718 	}
3719 
3720 	i40e_flush(hw);
3721 }
3722 
3723 /**
3724  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3725  * @pf: pointer to private device data structure
3726  **/
3727 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3728 {
3729 	struct i40e_hw *hw = &pf->hw;
3730 	u32 val;
3731 
3732 	/* clear things first */
3733 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3734 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3735 
3736 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3737 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3738 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3739 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3740 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3741 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3742 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3743 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3744 
3745 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3746 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3747 
3748 	if (pf->flags & I40E_FLAG_PTP)
3749 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3750 
3751 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3752 
3753 	/* SW_ITR_IDX = 0, but don't change INTENA */
3754 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3755 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3756 
3757 	/* OTHER_ITR_IDX = 0 */
3758 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3759 }
3760 
3761 /**
3762  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3763  * @vsi: the VSI being configured
3764  **/
3765 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3766 {
3767 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3768 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3769 	struct i40e_pf *pf = vsi->back;
3770 	struct i40e_hw *hw = &pf->hw;
3771 	u32 val;
3772 
3773 	/* set the ITR configuration */
3774 	q_vector->rx.next_update = jiffies + 1;
3775 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3776 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3777 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3778 	q_vector->tx.next_update = jiffies + 1;
3779 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3780 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3781 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3782 
3783 	i40e_enable_misc_int_causes(pf);
3784 
3785 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3786 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3787 
3788 	/* Associate the queue pair to the vector and enable the queue int */
3789 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3790 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3791 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3792 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3793 
3794 	wr32(hw, I40E_QINT_RQCTL(0), val);
3795 
3796 	if (i40e_enabled_xdp_vsi(vsi)) {
3797 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
3798 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3799 		      (I40E_QUEUE_TYPE_TX
3800 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3801 
3802 		wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3803 	}
3804 
3805 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
3806 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3807 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3808 
3809 	wr32(hw, I40E_QINT_TQCTL(0), val);
3810 	i40e_flush(hw);
3811 }
3812 
3813 /**
3814  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3815  * @pf: board private structure
3816  **/
3817 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3818 {
3819 	struct i40e_hw *hw = &pf->hw;
3820 
3821 	wr32(hw, I40E_PFINT_DYN_CTL0,
3822 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3823 	i40e_flush(hw);
3824 }
3825 
3826 /**
3827  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3828  * @pf: board private structure
3829  **/
3830 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3831 {
3832 	struct i40e_hw *hw = &pf->hw;
3833 	u32 val;
3834 
3835 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
3836 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3837 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3838 
3839 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
3840 	i40e_flush(hw);
3841 }
3842 
3843 /**
3844  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3845  * @irq: interrupt number
3846  * @data: pointer to a q_vector
3847  **/
3848 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3849 {
3850 	struct i40e_q_vector *q_vector = data;
3851 
3852 	if (!q_vector->tx.ring && !q_vector->rx.ring)
3853 		return IRQ_HANDLED;
3854 
3855 	napi_schedule_irqoff(&q_vector->napi);
3856 
3857 	return IRQ_HANDLED;
3858 }
3859 
3860 /**
3861  * i40e_irq_affinity_notify - Callback for affinity changes
3862  * @notify: context as to what irq was changed
3863  * @mask: the new affinity mask
3864  *
3865  * This is a callback function used by the irq_set_affinity_notifier function
3866  * so that we may register to receive changes to the irq affinity masks.
3867  **/
3868 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3869 				     const cpumask_t *mask)
3870 {
3871 	struct i40e_q_vector *q_vector =
3872 		container_of(notify, struct i40e_q_vector, affinity_notify);
3873 
3874 	cpumask_copy(&q_vector->affinity_mask, mask);
3875 }
3876 
3877 /**
3878  * i40e_irq_affinity_release - Callback for affinity notifier release
3879  * @ref: internal core kernel usage
3880  *
3881  * This is a callback function used by the irq_set_affinity_notifier function
3882  * to inform the current notification subscriber that they will no longer
3883  * receive notifications.
3884  **/
3885 static void i40e_irq_affinity_release(struct kref *ref) {}
3886 
3887 /**
3888  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3889  * @vsi: the VSI being configured
3890  * @basename: name for the vector
3891  *
3892  * Allocates MSI-X vectors and requests interrupts from the kernel.
3893  **/
3894 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3895 {
3896 	int q_vectors = vsi->num_q_vectors;
3897 	struct i40e_pf *pf = vsi->back;
3898 	int base = vsi->base_vector;
3899 	int rx_int_idx = 0;
3900 	int tx_int_idx = 0;
3901 	int vector, err;
3902 	int irq_num;
3903 	int cpu;
3904 
3905 	for (vector = 0; vector < q_vectors; vector++) {
3906 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
3907 
3908 		irq_num = pf->msix_entries[base + vector].vector;
3909 
3910 		if (q_vector->tx.ring && q_vector->rx.ring) {
3911 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3912 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3913 			tx_int_idx++;
3914 		} else if (q_vector->rx.ring) {
3915 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3916 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
3917 		} else if (q_vector->tx.ring) {
3918 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3919 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
3920 		} else {
3921 			/* skip this unused q_vector */
3922 			continue;
3923 		}
3924 		err = request_irq(irq_num,
3925 				  vsi->irq_handler,
3926 				  0,
3927 				  q_vector->name,
3928 				  q_vector);
3929 		if (err) {
3930 			dev_info(&pf->pdev->dev,
3931 				 "MSIX request_irq failed, error: %d\n", err);
3932 			goto free_queue_irqs;
3933 		}
3934 
3935 		/* register for affinity change notifications */
3936 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3937 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
3938 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
3939 		/* Spread affinity hints out across online CPUs.
3940 		 *
3941 		 * get_cpu_mask returns a static constant mask with
3942 		 * a permanent lifetime so it's ok to pass to
3943 		 * irq_update_affinity_hint without making a copy.
3944 		 */
3945 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
3946 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
3947 	}
3948 
3949 	vsi->irqs_ready = true;
3950 	return 0;
3951 
3952 free_queue_irqs:
3953 	while (vector) {
3954 		vector--;
3955 		irq_num = pf->msix_entries[base + vector].vector;
3956 		irq_set_affinity_notifier(irq_num, NULL);
3957 		irq_update_affinity_hint(irq_num, NULL);
3958 		free_irq(irq_num, &vsi->q_vectors[vector]);
3959 	}
3960 	return err;
3961 }
3962 
3963 /**
3964  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3965  * @vsi: the VSI being un-configured
3966  **/
3967 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3968 {
3969 	struct i40e_pf *pf = vsi->back;
3970 	struct i40e_hw *hw = &pf->hw;
3971 	int base = vsi->base_vector;
3972 	int i;
3973 
3974 	/* disable interrupt causation from each queue */
3975 	for (i = 0; i < vsi->num_queue_pairs; i++) {
3976 		u32 val;
3977 
3978 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3979 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3980 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3981 
3982 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3983 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3984 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3985 
3986 		if (!i40e_enabled_xdp_vsi(vsi))
3987 			continue;
3988 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
3989 	}
3990 
3991 	/* disable each interrupt */
3992 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3993 		for (i = vsi->base_vector;
3994 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
3995 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3996 
3997 		i40e_flush(hw);
3998 		for (i = 0; i < vsi->num_q_vectors; i++)
3999 			synchronize_irq(pf->msix_entries[i + base].vector);
4000 	} else {
4001 		/* Legacy and MSI mode - this stops all interrupt handling */
4002 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4003 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4004 		i40e_flush(hw);
4005 		synchronize_irq(pf->pdev->irq);
4006 	}
4007 }
4008 
4009 /**
4010  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4011  * @vsi: the VSI being configured
4012  **/
4013 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4014 {
4015 	struct i40e_pf *pf = vsi->back;
4016 	int i;
4017 
4018 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4019 		for (i = 0; i < vsi->num_q_vectors; i++)
4020 			i40e_irq_dynamic_enable(vsi, i);
4021 	} else {
4022 		i40e_irq_dynamic_enable_icr0(pf);
4023 	}
4024 
4025 	i40e_flush(&pf->hw);
4026 	return 0;
4027 }
4028 
4029 /**
4030  * i40e_free_misc_vector - Free the vector that handles non-queue events
4031  * @pf: board private structure
4032  **/
4033 static void i40e_free_misc_vector(struct i40e_pf *pf)
4034 {
4035 	/* Disable ICR 0 */
4036 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4037 	i40e_flush(&pf->hw);
4038 
4039 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4040 		synchronize_irq(pf->msix_entries[0].vector);
4041 		free_irq(pf->msix_entries[0].vector, pf);
4042 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4043 	}
4044 }
4045 
4046 /**
4047  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4048  * @irq: interrupt number
4049  * @data: pointer to a q_vector
4050  *
4051  * This is the handler used for all MSI/Legacy interrupts, and deals
4052  * with both queue and non-queue interrupts.  This is also used in
4053  * MSIX mode to handle the non-queue interrupts.
4054  **/
4055 static irqreturn_t i40e_intr(int irq, void *data)
4056 {
4057 	struct i40e_pf *pf = (struct i40e_pf *)data;
4058 	struct i40e_hw *hw = &pf->hw;
4059 	irqreturn_t ret = IRQ_NONE;
4060 	u32 icr0, icr0_remaining;
4061 	u32 val, ena_mask;
4062 
4063 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4064 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4065 
4066 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4067 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4068 		goto enable_intr;
4069 
4070 	/* if interrupt but no bits showing, must be SWINT */
4071 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4072 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4073 		pf->sw_int_count++;
4074 
4075 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4076 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4077 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4078 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4079 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4080 	}
4081 
4082 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4083 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4084 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4085 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4086 
4087 		/* We do not have a way to disarm Queue causes while leaving
4088 		 * interrupt enabled for all other causes, ideally
4089 		 * interrupt should be disabled while we are in NAPI but
4090 		 * this is not a performance path and napi_schedule()
4091 		 * can deal with rescheduling.
4092 		 */
4093 		if (!test_bit(__I40E_DOWN, pf->state))
4094 			napi_schedule_irqoff(&q_vector->napi);
4095 	}
4096 
4097 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4098 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4099 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4100 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4101 	}
4102 
4103 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4104 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4105 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4106 	}
4107 
4108 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4109 		/* disable any further VFLR event notifications */
4110 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4111 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4112 
4113 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4114 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4115 		} else {
4116 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4117 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4118 		}
4119 	}
4120 
4121 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4122 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4123 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4124 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4125 		val = rd32(hw, I40E_GLGEN_RSTAT);
4126 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4127 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4128 		if (val == I40E_RESET_CORER) {
4129 			pf->corer_count++;
4130 		} else if (val == I40E_RESET_GLOBR) {
4131 			pf->globr_count++;
4132 		} else if (val == I40E_RESET_EMPR) {
4133 			pf->empr_count++;
4134 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4135 		}
4136 	}
4137 
4138 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4139 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4140 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4141 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4142 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4143 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4144 	}
4145 
4146 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4147 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4148 
4149 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4150 			schedule_work(&pf->ptp_extts0_work);
4151 
4152 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4153 			i40e_ptp_tx_hwtstamp(pf);
4154 
4155 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4156 	}
4157 
4158 	/* If a critical error is pending we have no choice but to reset the
4159 	 * device.
4160 	 * Report and mask out any remaining unexpected interrupts.
4161 	 */
4162 	icr0_remaining = icr0 & ena_mask;
4163 	if (icr0_remaining) {
4164 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4165 			 icr0_remaining);
4166 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4167 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4168 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4169 			dev_info(&pf->pdev->dev, "device will be reset\n");
4170 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4171 			i40e_service_event_schedule(pf);
4172 		}
4173 		ena_mask &= ~icr0_remaining;
4174 	}
4175 	ret = IRQ_HANDLED;
4176 
4177 enable_intr:
4178 	/* re-enable interrupt causes */
4179 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4180 	if (!test_bit(__I40E_DOWN, pf->state) ||
4181 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4182 		i40e_service_event_schedule(pf);
4183 		i40e_irq_dynamic_enable_icr0(pf);
4184 	}
4185 
4186 	return ret;
4187 }
4188 
4189 /**
4190  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4191  * @tx_ring:  tx ring to clean
4192  * @budget:   how many cleans we're allowed
4193  *
4194  * Returns true if there's any budget left (e.g. the clean is finished)
4195  **/
4196 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4197 {
4198 	struct i40e_vsi *vsi = tx_ring->vsi;
4199 	u16 i = tx_ring->next_to_clean;
4200 	struct i40e_tx_buffer *tx_buf;
4201 	struct i40e_tx_desc *tx_desc;
4202 
4203 	tx_buf = &tx_ring->tx_bi[i];
4204 	tx_desc = I40E_TX_DESC(tx_ring, i);
4205 	i -= tx_ring->count;
4206 
4207 	do {
4208 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4209 
4210 		/* if next_to_watch is not set then there is no work pending */
4211 		if (!eop_desc)
4212 			break;
4213 
4214 		/* prevent any other reads prior to eop_desc */
4215 		smp_rmb();
4216 
4217 		/* if the descriptor isn't done, no work yet to do */
4218 		if (!(eop_desc->cmd_type_offset_bsz &
4219 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4220 			break;
4221 
4222 		/* clear next_to_watch to prevent false hangs */
4223 		tx_buf->next_to_watch = NULL;
4224 
4225 		tx_desc->buffer_addr = 0;
4226 		tx_desc->cmd_type_offset_bsz = 0;
4227 		/* move past filter desc */
4228 		tx_buf++;
4229 		tx_desc++;
4230 		i++;
4231 		if (unlikely(!i)) {
4232 			i -= tx_ring->count;
4233 			tx_buf = tx_ring->tx_bi;
4234 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4235 		}
4236 		/* unmap skb header data */
4237 		dma_unmap_single(tx_ring->dev,
4238 				 dma_unmap_addr(tx_buf, dma),
4239 				 dma_unmap_len(tx_buf, len),
4240 				 DMA_TO_DEVICE);
4241 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4242 			kfree(tx_buf->raw_buf);
4243 
4244 		tx_buf->raw_buf = NULL;
4245 		tx_buf->tx_flags = 0;
4246 		tx_buf->next_to_watch = NULL;
4247 		dma_unmap_len_set(tx_buf, len, 0);
4248 		tx_desc->buffer_addr = 0;
4249 		tx_desc->cmd_type_offset_bsz = 0;
4250 
4251 		/* move us past the eop_desc for start of next FD desc */
4252 		tx_buf++;
4253 		tx_desc++;
4254 		i++;
4255 		if (unlikely(!i)) {
4256 			i -= tx_ring->count;
4257 			tx_buf = tx_ring->tx_bi;
4258 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4259 		}
4260 
4261 		/* update budget accounting */
4262 		budget--;
4263 	} while (likely(budget));
4264 
4265 	i += tx_ring->count;
4266 	tx_ring->next_to_clean = i;
4267 
4268 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4269 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4270 
4271 	return budget > 0;
4272 }
4273 
4274 /**
4275  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4276  * @irq: interrupt number
4277  * @data: pointer to a q_vector
4278  **/
4279 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4280 {
4281 	struct i40e_q_vector *q_vector = data;
4282 	struct i40e_vsi *vsi;
4283 
4284 	if (!q_vector->tx.ring)
4285 		return IRQ_HANDLED;
4286 
4287 	vsi = q_vector->tx.ring->vsi;
4288 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4289 
4290 	return IRQ_HANDLED;
4291 }
4292 
4293 /**
4294  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4295  * @vsi: the VSI being configured
4296  * @v_idx: vector index
4297  * @qp_idx: queue pair index
4298  **/
4299 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4300 {
4301 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4302 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4303 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4304 
4305 	tx_ring->q_vector = q_vector;
4306 	tx_ring->next = q_vector->tx.ring;
4307 	q_vector->tx.ring = tx_ring;
4308 	q_vector->tx.count++;
4309 
4310 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4311 	if (i40e_enabled_xdp_vsi(vsi)) {
4312 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4313 
4314 		xdp_ring->q_vector = q_vector;
4315 		xdp_ring->next = q_vector->tx.ring;
4316 		q_vector->tx.ring = xdp_ring;
4317 		q_vector->tx.count++;
4318 	}
4319 
4320 	rx_ring->q_vector = q_vector;
4321 	rx_ring->next = q_vector->rx.ring;
4322 	q_vector->rx.ring = rx_ring;
4323 	q_vector->rx.count++;
4324 }
4325 
4326 /**
4327  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4328  * @vsi: the VSI being configured
4329  *
4330  * This function maps descriptor rings to the queue-specific vectors
4331  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4332  * one vector per queue pair, but on a constrained vector budget, we
4333  * group the queue pairs as "efficiently" as possible.
4334  **/
4335 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4336 {
4337 	int qp_remaining = vsi->num_queue_pairs;
4338 	int q_vectors = vsi->num_q_vectors;
4339 	int num_ringpairs;
4340 	int v_start = 0;
4341 	int qp_idx = 0;
4342 
4343 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4344 	 * group them so there are multiple queues per vector.
4345 	 * It is also important to go through all the vectors available to be
4346 	 * sure that if we don't use all the vectors, that the remaining vectors
4347 	 * are cleared. This is especially important when decreasing the
4348 	 * number of queues in use.
4349 	 */
4350 	for (; v_start < q_vectors; v_start++) {
4351 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4352 
4353 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4354 
4355 		q_vector->num_ringpairs = num_ringpairs;
4356 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4357 
4358 		q_vector->rx.count = 0;
4359 		q_vector->tx.count = 0;
4360 		q_vector->rx.ring = NULL;
4361 		q_vector->tx.ring = NULL;
4362 
4363 		while (num_ringpairs--) {
4364 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4365 			qp_idx++;
4366 			qp_remaining--;
4367 		}
4368 	}
4369 }
4370 
4371 /**
4372  * i40e_vsi_request_irq - Request IRQ from the OS
4373  * @vsi: the VSI being configured
4374  * @basename: name for the vector
4375  **/
4376 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4377 {
4378 	struct i40e_pf *pf = vsi->back;
4379 	int err;
4380 
4381 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4382 		err = i40e_vsi_request_irq_msix(vsi, basename);
4383 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4384 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4385 				  pf->int_name, pf);
4386 	else
4387 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4388 				  pf->int_name, pf);
4389 
4390 	if (err)
4391 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4392 
4393 	return err;
4394 }
4395 
4396 #ifdef CONFIG_NET_POLL_CONTROLLER
4397 /**
4398  * i40e_netpoll - A Polling 'interrupt' handler
4399  * @netdev: network interface device structure
4400  *
4401  * This is used by netconsole to send skbs without having to re-enable
4402  * interrupts.  It's not called while the normal interrupt routine is executing.
4403  **/
4404 static void i40e_netpoll(struct net_device *netdev)
4405 {
4406 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4407 	struct i40e_vsi *vsi = np->vsi;
4408 	struct i40e_pf *pf = vsi->back;
4409 	int i;
4410 
4411 	/* if interface is down do nothing */
4412 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4413 		return;
4414 
4415 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4416 		for (i = 0; i < vsi->num_q_vectors; i++)
4417 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4418 	} else {
4419 		i40e_intr(pf->pdev->irq, netdev);
4420 	}
4421 }
4422 #endif
4423 
4424 #define I40E_QTX_ENA_WAIT_COUNT 50
4425 
4426 /**
4427  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4428  * @pf: the PF being configured
4429  * @pf_q: the PF queue
4430  * @enable: enable or disable state of the queue
4431  *
4432  * This routine will wait for the given Tx queue of the PF to reach the
4433  * enabled or disabled state.
4434  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4435  * multiple retries; else will return 0 in case of success.
4436  **/
4437 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4438 {
4439 	int i;
4440 	u32 tx_reg;
4441 
4442 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4443 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4444 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4445 			break;
4446 
4447 		usleep_range(10, 20);
4448 	}
4449 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4450 		return -ETIMEDOUT;
4451 
4452 	return 0;
4453 }
4454 
4455 /**
4456  * i40e_control_tx_q - Start or stop a particular Tx queue
4457  * @pf: the PF structure
4458  * @pf_q: the PF queue to configure
4459  * @enable: start or stop the queue
4460  *
4461  * This function enables or disables a single queue. Note that any delay
4462  * required after the operation is expected to be handled by the caller of
4463  * this function.
4464  **/
4465 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4466 {
4467 	struct i40e_hw *hw = &pf->hw;
4468 	u32 tx_reg;
4469 	int i;
4470 
4471 	/* warn the TX unit of coming changes */
4472 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4473 	if (!enable)
4474 		usleep_range(10, 20);
4475 
4476 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4477 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4478 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4479 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4480 			break;
4481 		usleep_range(1000, 2000);
4482 	}
4483 
4484 	/* Skip if the queue is already in the requested state */
4485 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4486 		return;
4487 
4488 	/* turn on/off the queue */
4489 	if (enable) {
4490 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4491 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4492 	} else {
4493 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4494 	}
4495 
4496 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4497 }
4498 
4499 /**
4500  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4501  * @seid: VSI SEID
4502  * @pf: the PF structure
4503  * @pf_q: the PF queue to configure
4504  * @is_xdp: true if the queue is used for XDP
4505  * @enable: start or stop the queue
4506  **/
4507 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4508 			   bool is_xdp, bool enable)
4509 {
4510 	int ret;
4511 
4512 	i40e_control_tx_q(pf, pf_q, enable);
4513 
4514 	/* wait for the change to finish */
4515 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4516 	if (ret) {
4517 		dev_info(&pf->pdev->dev,
4518 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4519 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4520 			 (enable ? "en" : "dis"));
4521 	}
4522 
4523 	return ret;
4524 }
4525 
4526 /**
4527  * i40e_vsi_enable_tx - Start a VSI's rings
4528  * @vsi: the VSI being configured
4529  **/
4530 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4531 {
4532 	struct i40e_pf *pf = vsi->back;
4533 	int i, pf_q, ret = 0;
4534 
4535 	pf_q = vsi->base_queue;
4536 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4537 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4538 					     pf_q,
4539 					     false /*is xdp*/, true);
4540 		if (ret)
4541 			break;
4542 
4543 		if (!i40e_enabled_xdp_vsi(vsi))
4544 			continue;
4545 
4546 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4547 					     pf_q + vsi->alloc_queue_pairs,
4548 					     true /*is xdp*/, true);
4549 		if (ret)
4550 			break;
4551 	}
4552 	return ret;
4553 }
4554 
4555 /**
4556  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4557  * @pf: the PF being configured
4558  * @pf_q: the PF queue
4559  * @enable: enable or disable state of the queue
4560  *
4561  * This routine will wait for the given Rx queue of the PF to reach the
4562  * enabled or disabled state.
4563  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4564  * multiple retries; else will return 0 in case of success.
4565  **/
4566 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4567 {
4568 	int i;
4569 	u32 rx_reg;
4570 
4571 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4572 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4573 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4574 			break;
4575 
4576 		usleep_range(10, 20);
4577 	}
4578 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4579 		return -ETIMEDOUT;
4580 
4581 	return 0;
4582 }
4583 
4584 /**
4585  * i40e_control_rx_q - Start or stop a particular Rx queue
4586  * @pf: the PF structure
4587  * @pf_q: the PF queue to configure
4588  * @enable: start or stop the queue
4589  *
4590  * This function enables or disables a single queue. Note that
4591  * any delay required after the operation is expected to be
4592  * handled by the caller of this function.
4593  **/
4594 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4595 {
4596 	struct i40e_hw *hw = &pf->hw;
4597 	u32 rx_reg;
4598 	int i;
4599 
4600 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4601 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4602 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4603 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4604 			break;
4605 		usleep_range(1000, 2000);
4606 	}
4607 
4608 	/* Skip if the queue is already in the requested state */
4609 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4610 		return;
4611 
4612 	/* turn on/off the queue */
4613 	if (enable)
4614 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4615 	else
4616 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4617 
4618 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4619 }
4620 
4621 /**
4622  * i40e_control_wait_rx_q
4623  * @pf: the PF structure
4624  * @pf_q: queue being configured
4625  * @enable: start or stop the rings
4626  *
4627  * This function enables or disables a single queue along with waiting
4628  * for the change to finish. The caller of this function should handle
4629  * the delays needed in the case of disabling queues.
4630  **/
4631 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4632 {
4633 	int ret = 0;
4634 
4635 	i40e_control_rx_q(pf, pf_q, enable);
4636 
4637 	/* wait for the change to finish */
4638 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4639 	if (ret)
4640 		return ret;
4641 
4642 	return ret;
4643 }
4644 
4645 /**
4646  * i40e_vsi_enable_rx - Start a VSI's rings
4647  * @vsi: the VSI being configured
4648  **/
4649 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4650 {
4651 	struct i40e_pf *pf = vsi->back;
4652 	int i, pf_q, ret = 0;
4653 
4654 	pf_q = vsi->base_queue;
4655 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4656 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4657 		if (ret) {
4658 			dev_info(&pf->pdev->dev,
4659 				 "VSI seid %d Rx ring %d enable timeout\n",
4660 				 vsi->seid, pf_q);
4661 			break;
4662 		}
4663 	}
4664 
4665 	return ret;
4666 }
4667 
4668 /**
4669  * i40e_vsi_start_rings - Start a VSI's rings
4670  * @vsi: the VSI being configured
4671  **/
4672 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4673 {
4674 	int ret = 0;
4675 
4676 	/* do rx first for enable and last for disable */
4677 	ret = i40e_vsi_enable_rx(vsi);
4678 	if (ret)
4679 		return ret;
4680 	ret = i40e_vsi_enable_tx(vsi);
4681 
4682 	return ret;
4683 }
4684 
4685 #define I40E_DISABLE_TX_GAP_MSEC	50
4686 
4687 /**
4688  * i40e_vsi_stop_rings - Stop a VSI's rings
4689  * @vsi: the VSI being configured
4690  **/
4691 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4692 {
4693 	struct i40e_pf *pf = vsi->back;
4694 	int pf_q, err, q_end;
4695 
4696 	/* When port TX is suspended, don't wait */
4697 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4698 		return i40e_vsi_stop_rings_no_wait(vsi);
4699 
4700 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4701 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4702 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4703 
4704 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4705 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4706 		if (err)
4707 			dev_info(&pf->pdev->dev,
4708 				 "VSI seid %d Rx ring %d disable timeout\n",
4709 				 vsi->seid, pf_q);
4710 	}
4711 
4712 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4713 	pf_q = vsi->base_queue;
4714 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4715 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4716 
4717 	i40e_vsi_wait_queues_disabled(vsi);
4718 }
4719 
4720 /**
4721  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4722  * @vsi: the VSI being shutdown
4723  *
4724  * This function stops all the rings for a VSI but does not delay to verify
4725  * that rings have been disabled. It is expected that the caller is shutting
4726  * down multiple VSIs at once and will delay together for all the VSIs after
4727  * initiating the shutdown. This is particularly useful for shutting down lots
4728  * of VFs together. Otherwise, a large delay can be incurred while configuring
4729  * each VSI in serial.
4730  **/
4731 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4732 {
4733 	struct i40e_pf *pf = vsi->back;
4734 	int i, pf_q;
4735 
4736 	pf_q = vsi->base_queue;
4737 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4738 		i40e_control_tx_q(pf, pf_q, false);
4739 		i40e_control_rx_q(pf, pf_q, false);
4740 	}
4741 }
4742 
4743 /**
4744  * i40e_vsi_free_irq - Free the irq association with the OS
4745  * @vsi: the VSI being configured
4746  **/
4747 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4748 {
4749 	struct i40e_pf *pf = vsi->back;
4750 	struct i40e_hw *hw = &pf->hw;
4751 	int base = vsi->base_vector;
4752 	u32 val, qp;
4753 	int i;
4754 
4755 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4756 		if (!vsi->q_vectors)
4757 			return;
4758 
4759 		if (!vsi->irqs_ready)
4760 			return;
4761 
4762 		vsi->irqs_ready = false;
4763 		for (i = 0; i < vsi->num_q_vectors; i++) {
4764 			int irq_num;
4765 			u16 vector;
4766 
4767 			vector = i + base;
4768 			irq_num = pf->msix_entries[vector].vector;
4769 
4770 			/* free only the irqs that were actually requested */
4771 			if (!vsi->q_vectors[i] ||
4772 			    !vsi->q_vectors[i]->num_ringpairs)
4773 				continue;
4774 
4775 			/* clear the affinity notifier in the IRQ descriptor */
4776 			irq_set_affinity_notifier(irq_num, NULL);
4777 			/* remove our suggested affinity mask for this IRQ */
4778 			irq_update_affinity_hint(irq_num, NULL);
4779 			synchronize_irq(irq_num);
4780 			free_irq(irq_num, vsi->q_vectors[i]);
4781 
4782 			/* Tear down the interrupt queue link list
4783 			 *
4784 			 * We know that they come in pairs and always
4785 			 * the Rx first, then the Tx.  To clear the
4786 			 * link list, stick the EOL value into the
4787 			 * next_q field of the registers.
4788 			 */
4789 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4790 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4791 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4792 			val |= I40E_QUEUE_END_OF_LIST
4793 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4794 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4795 
4796 			while (qp != I40E_QUEUE_END_OF_LIST) {
4797 				u32 next;
4798 
4799 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4800 
4801 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4802 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4803 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4804 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4805 
4806 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4807 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4808 
4809 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4810 
4811 				val = rd32(hw, I40E_QINT_TQCTL(qp));
4812 
4813 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4814 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4815 
4816 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4817 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4818 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4819 					 I40E_QINT_TQCTL_INTEVENT_MASK);
4820 
4821 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4822 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4823 
4824 				wr32(hw, I40E_QINT_TQCTL(qp), val);
4825 				qp = next;
4826 			}
4827 		}
4828 	} else {
4829 		free_irq(pf->pdev->irq, pf);
4830 
4831 		val = rd32(hw, I40E_PFINT_LNKLST0);
4832 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4833 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4834 		val |= I40E_QUEUE_END_OF_LIST
4835 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4836 		wr32(hw, I40E_PFINT_LNKLST0, val);
4837 
4838 		val = rd32(hw, I40E_QINT_RQCTL(qp));
4839 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4840 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4841 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4842 			 I40E_QINT_RQCTL_INTEVENT_MASK);
4843 
4844 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4845 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4846 
4847 		wr32(hw, I40E_QINT_RQCTL(qp), val);
4848 
4849 		val = rd32(hw, I40E_QINT_TQCTL(qp));
4850 
4851 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4852 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4853 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4854 			 I40E_QINT_TQCTL_INTEVENT_MASK);
4855 
4856 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4857 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4858 
4859 		wr32(hw, I40E_QINT_TQCTL(qp), val);
4860 	}
4861 }
4862 
4863 /**
4864  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4865  * @vsi: the VSI being configured
4866  * @v_idx: Index of vector to be freed
4867  *
4868  * This function frees the memory allocated to the q_vector.  In addition if
4869  * NAPI is enabled it will delete any references to the NAPI struct prior
4870  * to freeing the q_vector.
4871  **/
4872 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4873 {
4874 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4875 	struct i40e_ring *ring;
4876 
4877 	if (!q_vector)
4878 		return;
4879 
4880 	/* disassociate q_vector from rings */
4881 	i40e_for_each_ring(ring, q_vector->tx)
4882 		ring->q_vector = NULL;
4883 
4884 	i40e_for_each_ring(ring, q_vector->rx)
4885 		ring->q_vector = NULL;
4886 
4887 	/* only VSI w/ an associated netdev is set up w/ NAPI */
4888 	if (vsi->netdev)
4889 		netif_napi_del(&q_vector->napi);
4890 
4891 	vsi->q_vectors[v_idx] = NULL;
4892 
4893 	kfree_rcu(q_vector, rcu);
4894 }
4895 
4896 /**
4897  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4898  * @vsi: the VSI being un-configured
4899  *
4900  * This frees the memory allocated to the q_vectors and
4901  * deletes references to the NAPI struct.
4902  **/
4903 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4904 {
4905 	int v_idx;
4906 
4907 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4908 		i40e_free_q_vector(vsi, v_idx);
4909 }
4910 
4911 /**
4912  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4913  * @pf: board private structure
4914  **/
4915 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4916 {
4917 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4918 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4919 		pci_disable_msix(pf->pdev);
4920 		kfree(pf->msix_entries);
4921 		pf->msix_entries = NULL;
4922 		kfree(pf->irq_pile);
4923 		pf->irq_pile = NULL;
4924 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4925 		pci_disable_msi(pf->pdev);
4926 	}
4927 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4928 }
4929 
4930 /**
4931  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4932  * @pf: board private structure
4933  *
4934  * We go through and clear interrupt specific resources and reset the structure
4935  * to pre-load conditions
4936  **/
4937 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4938 {
4939 	int i;
4940 
4941 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
4942 		i40e_free_misc_vector(pf);
4943 
4944 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4945 		      I40E_IWARP_IRQ_PILE_ID);
4946 
4947 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
4948 	for (i = 0; i < pf->num_alloc_vsi; i++)
4949 		if (pf->vsi[i])
4950 			i40e_vsi_free_q_vectors(pf->vsi[i]);
4951 	i40e_reset_interrupt_capability(pf);
4952 }
4953 
4954 /**
4955  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4956  * @vsi: the VSI being configured
4957  **/
4958 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4959 {
4960 	int q_idx;
4961 
4962 	if (!vsi->netdev)
4963 		return;
4964 
4965 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4966 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4967 
4968 		if (q_vector->rx.ring || q_vector->tx.ring)
4969 			napi_enable(&q_vector->napi);
4970 	}
4971 }
4972 
4973 /**
4974  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4975  * @vsi: the VSI being configured
4976  **/
4977 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4978 {
4979 	int q_idx;
4980 
4981 	if (!vsi->netdev)
4982 		return;
4983 
4984 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4985 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4986 
4987 		if (q_vector->rx.ring || q_vector->tx.ring)
4988 			napi_disable(&q_vector->napi);
4989 	}
4990 }
4991 
4992 /**
4993  * i40e_vsi_close - Shut down a VSI
4994  * @vsi: the vsi to be quelled
4995  **/
4996 static void i40e_vsi_close(struct i40e_vsi *vsi)
4997 {
4998 	struct i40e_pf *pf = vsi->back;
4999 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5000 		i40e_down(vsi);
5001 	i40e_vsi_free_irq(vsi);
5002 	i40e_vsi_free_tx_resources(vsi);
5003 	i40e_vsi_free_rx_resources(vsi);
5004 	vsi->current_netdev_flags = 0;
5005 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5006 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5007 		set_bit(__I40E_CLIENT_RESET, pf->state);
5008 }
5009 
5010 /**
5011  * i40e_quiesce_vsi - Pause a given VSI
5012  * @vsi: the VSI being paused
5013  **/
5014 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5015 {
5016 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5017 		return;
5018 
5019 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5020 	if (vsi->netdev && netif_running(vsi->netdev))
5021 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5022 	else
5023 		i40e_vsi_close(vsi);
5024 }
5025 
5026 /**
5027  * i40e_unquiesce_vsi - Resume a given VSI
5028  * @vsi: the VSI being resumed
5029  **/
5030 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5031 {
5032 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5033 		return;
5034 
5035 	if (vsi->netdev && netif_running(vsi->netdev))
5036 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5037 	else
5038 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5039 }
5040 
5041 /**
5042  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5043  * @pf: the PF
5044  **/
5045 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5046 {
5047 	int v;
5048 
5049 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5050 		if (pf->vsi[v])
5051 			i40e_quiesce_vsi(pf->vsi[v]);
5052 	}
5053 }
5054 
5055 /**
5056  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5057  * @pf: the PF
5058  **/
5059 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5060 {
5061 	int v;
5062 
5063 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5064 		if (pf->vsi[v])
5065 			i40e_unquiesce_vsi(pf->vsi[v]);
5066 	}
5067 }
5068 
5069 /**
5070  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5071  * @vsi: the VSI being configured
5072  *
5073  * Wait until all queues on a given VSI have been disabled.
5074  **/
5075 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5076 {
5077 	struct i40e_pf *pf = vsi->back;
5078 	int i, pf_q, ret;
5079 
5080 	pf_q = vsi->base_queue;
5081 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5082 		/* Check and wait for the Tx queue */
5083 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5084 		if (ret) {
5085 			dev_info(&pf->pdev->dev,
5086 				 "VSI seid %d Tx ring %d disable timeout\n",
5087 				 vsi->seid, pf_q);
5088 			return ret;
5089 		}
5090 
5091 		if (!i40e_enabled_xdp_vsi(vsi))
5092 			goto wait_rx;
5093 
5094 		/* Check and wait for the XDP Tx queue */
5095 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5096 				       false);
5097 		if (ret) {
5098 			dev_info(&pf->pdev->dev,
5099 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5100 				 vsi->seid, pf_q);
5101 			return ret;
5102 		}
5103 wait_rx:
5104 		/* Check and wait for the Rx queue */
5105 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5106 		if (ret) {
5107 			dev_info(&pf->pdev->dev,
5108 				 "VSI seid %d Rx ring %d disable timeout\n",
5109 				 vsi->seid, pf_q);
5110 			return ret;
5111 		}
5112 	}
5113 
5114 	return 0;
5115 }
5116 
5117 #ifdef CONFIG_I40E_DCB
5118 /**
5119  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5120  * @pf: the PF
5121  *
5122  * This function waits for the queues to be in disabled state for all the
5123  * VSIs that are managed by this PF.
5124  **/
5125 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5126 {
5127 	int v, ret = 0;
5128 
5129 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5130 		if (pf->vsi[v]) {
5131 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5132 			if (ret)
5133 				break;
5134 		}
5135 	}
5136 
5137 	return ret;
5138 }
5139 
5140 #endif
5141 
5142 /**
5143  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5144  * @pf: pointer to PF
5145  *
5146  * Get TC map for ISCSI PF type that will include iSCSI TC
5147  * and LAN TC.
5148  **/
5149 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5150 {
5151 	struct i40e_dcb_app_priority_table app;
5152 	struct i40e_hw *hw = &pf->hw;
5153 	u8 enabled_tc = 1; /* TC0 is always enabled */
5154 	u8 tc, i;
5155 	/* Get the iSCSI APP TLV */
5156 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5157 
5158 	for (i = 0; i < dcbcfg->numapps; i++) {
5159 		app = dcbcfg->app[i];
5160 		if (app.selector == I40E_APP_SEL_TCPIP &&
5161 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5162 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5163 			enabled_tc |= BIT(tc);
5164 			break;
5165 		}
5166 	}
5167 
5168 	return enabled_tc;
5169 }
5170 
5171 /**
5172  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5173  * @dcbcfg: the corresponding DCBx configuration structure
5174  *
5175  * Return the number of TCs from given DCBx configuration
5176  **/
5177 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5178 {
5179 	int i, tc_unused = 0;
5180 	u8 num_tc = 0;
5181 	u8 ret = 0;
5182 
5183 	/* Scan the ETS Config Priority Table to find
5184 	 * traffic class enabled for a given priority
5185 	 * and create a bitmask of enabled TCs
5186 	 */
5187 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5188 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5189 
5190 	/* Now scan the bitmask to check for
5191 	 * contiguous TCs starting with TC0
5192 	 */
5193 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5194 		if (num_tc & BIT(i)) {
5195 			if (!tc_unused) {
5196 				ret++;
5197 			} else {
5198 				pr_err("Non-contiguous TC - Disabling DCB\n");
5199 				return 1;
5200 			}
5201 		} else {
5202 			tc_unused = 1;
5203 		}
5204 	}
5205 
5206 	/* There is always at least TC0 */
5207 	if (!ret)
5208 		ret = 1;
5209 
5210 	return ret;
5211 }
5212 
5213 /**
5214  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5215  * @dcbcfg: the corresponding DCBx configuration structure
5216  *
5217  * Query the current DCB configuration and return the number of
5218  * traffic classes enabled from the given DCBX config
5219  **/
5220 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5221 {
5222 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5223 	u8 enabled_tc = 1;
5224 	u8 i;
5225 
5226 	for (i = 0; i < num_tc; i++)
5227 		enabled_tc |= BIT(i);
5228 
5229 	return enabled_tc;
5230 }
5231 
5232 /**
5233  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5234  * @pf: PF being queried
5235  *
5236  * Query the current MQPRIO configuration and return the number of
5237  * traffic classes enabled.
5238  **/
5239 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5240 {
5241 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5242 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5243 	u8 enabled_tc = 1, i;
5244 
5245 	for (i = 1; i < num_tc; i++)
5246 		enabled_tc |= BIT(i);
5247 	return enabled_tc;
5248 }
5249 
5250 /**
5251  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5252  * @pf: PF being queried
5253  *
5254  * Return number of traffic classes enabled for the given PF
5255  **/
5256 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5257 {
5258 	struct i40e_hw *hw = &pf->hw;
5259 	u8 i, enabled_tc = 1;
5260 	u8 num_tc = 0;
5261 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5262 
5263 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5264 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5265 
5266 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5267 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5268 		return 1;
5269 
5270 	/* SFP mode will be enabled for all TCs on port */
5271 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5272 		return i40e_dcb_get_num_tc(dcbcfg);
5273 
5274 	/* MFP mode return count of enabled TCs for this PF */
5275 	if (pf->hw.func_caps.iscsi)
5276 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5277 	else
5278 		return 1; /* Only TC0 */
5279 
5280 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5281 		if (enabled_tc & BIT(i))
5282 			num_tc++;
5283 	}
5284 	return num_tc;
5285 }
5286 
5287 /**
5288  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5289  * @pf: PF being queried
5290  *
5291  * Return a bitmap for enabled traffic classes for this PF.
5292  **/
5293 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5294 {
5295 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5296 		return i40e_mqprio_get_enabled_tc(pf);
5297 
5298 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5299 	 * default TC
5300 	 */
5301 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5302 		return I40E_DEFAULT_TRAFFIC_CLASS;
5303 
5304 	/* SFP mode we want PF to be enabled for all TCs */
5305 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5306 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5307 
5308 	/* MFP enabled and iSCSI PF type */
5309 	if (pf->hw.func_caps.iscsi)
5310 		return i40e_get_iscsi_tc_map(pf);
5311 	else
5312 		return I40E_DEFAULT_TRAFFIC_CLASS;
5313 }
5314 
5315 /**
5316  * i40e_vsi_get_bw_info - Query VSI BW Information
5317  * @vsi: the VSI being queried
5318  *
5319  * Returns 0 on success, negative value on failure
5320  **/
5321 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5322 {
5323 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5324 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5325 	struct i40e_pf *pf = vsi->back;
5326 	struct i40e_hw *hw = &pf->hw;
5327 	i40e_status ret;
5328 	u32 tc_bw_max;
5329 	int i;
5330 
5331 	/* Get the VSI level BW configuration */
5332 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5333 	if (ret) {
5334 		dev_info(&pf->pdev->dev,
5335 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5336 			 i40e_stat_str(&pf->hw, ret),
5337 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5338 		return -EINVAL;
5339 	}
5340 
5341 	/* Get the VSI level BW configuration per TC */
5342 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5343 					       NULL);
5344 	if (ret) {
5345 		dev_info(&pf->pdev->dev,
5346 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5347 			 i40e_stat_str(&pf->hw, ret),
5348 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5349 		return -EINVAL;
5350 	}
5351 
5352 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5353 		dev_info(&pf->pdev->dev,
5354 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5355 			 bw_config.tc_valid_bits,
5356 			 bw_ets_config.tc_valid_bits);
5357 		/* Still continuing */
5358 	}
5359 
5360 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5361 	vsi->bw_max_quanta = bw_config.max_bw;
5362 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5363 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5364 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5365 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5366 		vsi->bw_ets_limit_credits[i] =
5367 					le16_to_cpu(bw_ets_config.credits[i]);
5368 		/* 3 bits out of 4 for each TC */
5369 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5370 	}
5371 
5372 	return 0;
5373 }
5374 
5375 /**
5376  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5377  * @vsi: the VSI being configured
5378  * @enabled_tc: TC bitmap
5379  * @bw_share: BW shared credits per TC
5380  *
5381  * Returns 0 on success, negative value on failure
5382  **/
5383 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5384 				       u8 *bw_share)
5385 {
5386 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5387 	struct i40e_pf *pf = vsi->back;
5388 	i40e_status ret;
5389 	int i;
5390 
5391 	/* There is no need to reset BW when mqprio mode is on.  */
5392 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5393 		return 0;
5394 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5395 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5396 		if (ret)
5397 			dev_info(&pf->pdev->dev,
5398 				 "Failed to reset tx rate for vsi->seid %u\n",
5399 				 vsi->seid);
5400 		return ret;
5401 	}
5402 	memset(&bw_data, 0, sizeof(bw_data));
5403 	bw_data.tc_valid_bits = enabled_tc;
5404 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5405 		bw_data.tc_bw_credits[i] = bw_share[i];
5406 
5407 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5408 	if (ret) {
5409 		dev_info(&pf->pdev->dev,
5410 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5411 			 pf->hw.aq.asq_last_status);
5412 		return -EINVAL;
5413 	}
5414 
5415 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5416 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5417 
5418 	return 0;
5419 }
5420 
5421 /**
5422  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5423  * @vsi: the VSI being configured
5424  * @enabled_tc: TC map to be enabled
5425  *
5426  **/
5427 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5428 {
5429 	struct net_device *netdev = vsi->netdev;
5430 	struct i40e_pf *pf = vsi->back;
5431 	struct i40e_hw *hw = &pf->hw;
5432 	u8 netdev_tc = 0;
5433 	int i;
5434 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5435 
5436 	if (!netdev)
5437 		return;
5438 
5439 	if (!enabled_tc) {
5440 		netdev_reset_tc(netdev);
5441 		return;
5442 	}
5443 
5444 	/* Set up actual enabled TCs on the VSI */
5445 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5446 		return;
5447 
5448 	/* set per TC queues for the VSI */
5449 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5450 		/* Only set TC queues for enabled tcs
5451 		 *
5452 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5453 		 * enabled_tc bitmap would be 0x00001001; the driver
5454 		 * will set the numtc for netdev as 2 that will be
5455 		 * referenced by the netdev layer as TC 0 and 1.
5456 		 */
5457 		if (vsi->tc_config.enabled_tc & BIT(i))
5458 			netdev_set_tc_queue(netdev,
5459 					vsi->tc_config.tc_info[i].netdev_tc,
5460 					vsi->tc_config.tc_info[i].qcount,
5461 					vsi->tc_config.tc_info[i].qoffset);
5462 	}
5463 
5464 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5465 		return;
5466 
5467 	/* Assign UP2TC map for the VSI */
5468 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5469 		/* Get the actual TC# for the UP */
5470 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5471 		/* Get the mapped netdev TC# for the UP */
5472 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5473 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5474 	}
5475 }
5476 
5477 /**
5478  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5479  * @vsi: the VSI being configured
5480  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5481  **/
5482 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5483 				      struct i40e_vsi_context *ctxt)
5484 {
5485 	/* copy just the sections touched not the entire info
5486 	 * since not all sections are valid as returned by
5487 	 * update vsi params
5488 	 */
5489 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5490 	memcpy(&vsi->info.queue_mapping,
5491 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5492 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5493 	       sizeof(vsi->info.tc_mapping));
5494 }
5495 
5496 /**
5497  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5498  * @vsi: the VSI being reconfigured
5499  * @vsi_offset: offset from main VF VSI
5500  */
5501 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5502 {
5503 	struct i40e_vsi_context ctxt = {};
5504 	struct i40e_pf *pf;
5505 	struct i40e_hw *hw;
5506 	int ret;
5507 
5508 	if (!vsi)
5509 		return I40E_ERR_PARAM;
5510 	pf = vsi->back;
5511 	hw = &pf->hw;
5512 
5513 	ctxt.seid = vsi->seid;
5514 	ctxt.pf_num = hw->pf_id;
5515 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5516 	ctxt.uplink_seid = vsi->uplink_seid;
5517 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5518 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5519 	ctxt.info = vsi->info;
5520 
5521 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5522 				 false);
5523 	if (vsi->reconfig_rss) {
5524 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5525 				      vsi->num_queue_pairs);
5526 		ret = i40e_vsi_config_rss(vsi);
5527 		if (ret) {
5528 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5529 			return ret;
5530 		}
5531 		vsi->reconfig_rss = false;
5532 	}
5533 
5534 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5535 	if (ret) {
5536 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %s aq_err %s\n",
5537 			 i40e_stat_str(hw, ret),
5538 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5539 		return ret;
5540 	}
5541 	/* update the local VSI info with updated queue map */
5542 	i40e_vsi_update_queue_map(vsi, &ctxt);
5543 	vsi->info.valid_sections = 0;
5544 
5545 	return ret;
5546 }
5547 
5548 /**
5549  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5550  * @vsi: VSI to be configured
5551  * @enabled_tc: TC bitmap
5552  *
5553  * This configures a particular VSI for TCs that are mapped to the
5554  * given TC bitmap. It uses default bandwidth share for TCs across
5555  * VSIs to configure TC for a particular VSI.
5556  *
5557  * NOTE:
5558  * It is expected that the VSI queues have been quisced before calling
5559  * this function.
5560  **/
5561 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5562 {
5563 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5564 	struct i40e_pf *pf = vsi->back;
5565 	struct i40e_hw *hw = &pf->hw;
5566 	struct i40e_vsi_context ctxt;
5567 	int ret = 0;
5568 	int i;
5569 
5570 	/* Check if enabled_tc is same as existing or new TCs */
5571 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5572 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5573 		return ret;
5574 
5575 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5576 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5577 		if (enabled_tc & BIT(i))
5578 			bw_share[i] = 1;
5579 	}
5580 
5581 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5582 	if (ret) {
5583 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5584 
5585 		dev_info(&pf->pdev->dev,
5586 			 "Failed configuring TC map %d for VSI %d\n",
5587 			 enabled_tc, vsi->seid);
5588 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5589 						  &bw_config, NULL);
5590 		if (ret) {
5591 			dev_info(&pf->pdev->dev,
5592 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5593 				 i40e_stat_str(hw, ret),
5594 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5595 			goto out;
5596 		}
5597 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5598 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5599 
5600 			if (!valid_tc)
5601 				valid_tc = bw_config.tc_valid_bits;
5602 			/* Always enable TC0, no matter what */
5603 			valid_tc |= 1;
5604 			dev_info(&pf->pdev->dev,
5605 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5606 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5607 			enabled_tc = valid_tc;
5608 		}
5609 
5610 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5611 		if (ret) {
5612 			dev_err(&pf->pdev->dev,
5613 				"Unable to  configure TC map %d for VSI %d\n",
5614 				enabled_tc, vsi->seid);
5615 			goto out;
5616 		}
5617 	}
5618 
5619 	/* Update Queue Pairs Mapping for currently enabled UPs */
5620 	ctxt.seid = vsi->seid;
5621 	ctxt.pf_num = vsi->back->hw.pf_id;
5622 	ctxt.vf_num = 0;
5623 	ctxt.uplink_seid = vsi->uplink_seid;
5624 	ctxt.info = vsi->info;
5625 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5626 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5627 		if (ret)
5628 			goto out;
5629 	} else {
5630 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5631 	}
5632 
5633 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5634 	 * queues changed.
5635 	 */
5636 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5637 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5638 				      vsi->num_queue_pairs);
5639 		ret = i40e_vsi_config_rss(vsi);
5640 		if (ret) {
5641 			dev_info(&vsi->back->pdev->dev,
5642 				 "Failed to reconfig rss for num_queues\n");
5643 			return ret;
5644 		}
5645 		vsi->reconfig_rss = false;
5646 	}
5647 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5648 		ctxt.info.valid_sections |=
5649 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5650 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5651 	}
5652 
5653 	/* Update the VSI after updating the VSI queue-mapping
5654 	 * information
5655 	 */
5656 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5657 	if (ret) {
5658 		dev_info(&pf->pdev->dev,
5659 			 "Update vsi tc config failed, err %s aq_err %s\n",
5660 			 i40e_stat_str(hw, ret),
5661 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5662 		goto out;
5663 	}
5664 	/* update the local VSI info with updated queue map */
5665 	i40e_vsi_update_queue_map(vsi, &ctxt);
5666 	vsi->info.valid_sections = 0;
5667 
5668 	/* Update current VSI BW information */
5669 	ret = i40e_vsi_get_bw_info(vsi);
5670 	if (ret) {
5671 		dev_info(&pf->pdev->dev,
5672 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5673 			 i40e_stat_str(hw, ret),
5674 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5675 		goto out;
5676 	}
5677 
5678 	/* Update the netdev TC setup */
5679 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5680 out:
5681 	return ret;
5682 }
5683 
5684 /**
5685  * i40e_get_link_speed - Returns link speed for the interface
5686  * @vsi: VSI to be configured
5687  *
5688  **/
5689 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5690 {
5691 	struct i40e_pf *pf = vsi->back;
5692 
5693 	switch (pf->hw.phy.link_info.link_speed) {
5694 	case I40E_LINK_SPEED_40GB:
5695 		return 40000;
5696 	case I40E_LINK_SPEED_25GB:
5697 		return 25000;
5698 	case I40E_LINK_SPEED_20GB:
5699 		return 20000;
5700 	case I40E_LINK_SPEED_10GB:
5701 		return 10000;
5702 	case I40E_LINK_SPEED_1GB:
5703 		return 1000;
5704 	default:
5705 		return -EINVAL;
5706 	}
5707 }
5708 
5709 /**
5710  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5711  * @vsi: VSI to be configured
5712  * @seid: seid of the channel/VSI
5713  * @max_tx_rate: max TX rate to be configured as BW limit
5714  *
5715  * Helper function to set BW limit for a given VSI
5716  **/
5717 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5718 {
5719 	struct i40e_pf *pf = vsi->back;
5720 	u64 credits = 0;
5721 	int speed = 0;
5722 	int ret = 0;
5723 
5724 	speed = i40e_get_link_speed(vsi);
5725 	if (max_tx_rate > speed) {
5726 		dev_err(&pf->pdev->dev,
5727 			"Invalid max tx rate %llu specified for VSI seid %d.",
5728 			max_tx_rate, seid);
5729 		return -EINVAL;
5730 	}
5731 	if (max_tx_rate && max_tx_rate < 50) {
5732 		dev_warn(&pf->pdev->dev,
5733 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5734 		max_tx_rate = 50;
5735 	}
5736 
5737 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5738 	credits = max_tx_rate;
5739 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5740 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5741 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5742 	if (ret)
5743 		dev_err(&pf->pdev->dev,
5744 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5745 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5746 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5747 	return ret;
5748 }
5749 
5750 /**
5751  * i40e_remove_queue_channels - Remove queue channels for the TCs
5752  * @vsi: VSI to be configured
5753  *
5754  * Remove queue channels for the TCs
5755  **/
5756 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5757 {
5758 	enum i40e_admin_queue_err last_aq_status;
5759 	struct i40e_cloud_filter *cfilter;
5760 	struct i40e_channel *ch, *ch_tmp;
5761 	struct i40e_pf *pf = vsi->back;
5762 	struct hlist_node *node;
5763 	int ret, i;
5764 
5765 	/* Reset rss size that was stored when reconfiguring rss for
5766 	 * channel VSIs with non-power-of-2 queue count.
5767 	 */
5768 	vsi->current_rss_size = 0;
5769 
5770 	/* perform cleanup for channels if they exist */
5771 	if (list_empty(&vsi->ch_list))
5772 		return;
5773 
5774 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5775 		struct i40e_vsi *p_vsi;
5776 
5777 		list_del(&ch->list);
5778 		p_vsi = ch->parent_vsi;
5779 		if (!p_vsi || !ch->initialized) {
5780 			kfree(ch);
5781 			continue;
5782 		}
5783 		/* Reset queue contexts */
5784 		for (i = 0; i < ch->num_queue_pairs; i++) {
5785 			struct i40e_ring *tx_ring, *rx_ring;
5786 			u16 pf_q;
5787 
5788 			pf_q = ch->base_queue + i;
5789 			tx_ring = vsi->tx_rings[pf_q];
5790 			tx_ring->ch = NULL;
5791 
5792 			rx_ring = vsi->rx_rings[pf_q];
5793 			rx_ring->ch = NULL;
5794 		}
5795 
5796 		/* Reset BW configured for this VSI via mqprio */
5797 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5798 		if (ret)
5799 			dev_info(&vsi->back->pdev->dev,
5800 				 "Failed to reset tx rate for ch->seid %u\n",
5801 				 ch->seid);
5802 
5803 		/* delete cloud filters associated with this channel */
5804 		hlist_for_each_entry_safe(cfilter, node,
5805 					  &pf->cloud_filter_list, cloud_node) {
5806 			if (cfilter->seid != ch->seid)
5807 				continue;
5808 
5809 			hash_del(&cfilter->cloud_node);
5810 			if (cfilter->dst_port)
5811 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
5812 									cfilter,
5813 									false);
5814 			else
5815 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
5816 								false);
5817 			last_aq_status = pf->hw.aq.asq_last_status;
5818 			if (ret)
5819 				dev_info(&pf->pdev->dev,
5820 					 "Failed to delete cloud filter, err %s aq_err %s\n",
5821 					 i40e_stat_str(&pf->hw, ret),
5822 					 i40e_aq_str(&pf->hw, last_aq_status));
5823 			kfree(cfilter);
5824 		}
5825 
5826 		/* delete VSI from FW */
5827 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5828 					     NULL);
5829 		if (ret)
5830 			dev_err(&vsi->back->pdev->dev,
5831 				"unable to remove channel (%d) for parent VSI(%d)\n",
5832 				ch->seid, p_vsi->seid);
5833 		kfree(ch);
5834 	}
5835 	INIT_LIST_HEAD(&vsi->ch_list);
5836 }
5837 
5838 /**
5839  * i40e_get_max_queues_for_channel
5840  * @vsi: ptr to VSI to which channels are associated with
5841  *
5842  * Helper function which returns max value among the queue counts set on the
5843  * channels/TCs created.
5844  **/
5845 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5846 {
5847 	struct i40e_channel *ch, *ch_tmp;
5848 	int max = 0;
5849 
5850 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5851 		if (!ch->initialized)
5852 			continue;
5853 		if (ch->num_queue_pairs > max)
5854 			max = ch->num_queue_pairs;
5855 	}
5856 
5857 	return max;
5858 }
5859 
5860 /**
5861  * i40e_validate_num_queues - validate num_queues w.r.t channel
5862  * @pf: ptr to PF device
5863  * @num_queues: number of queues
5864  * @vsi: the parent VSI
5865  * @reconfig_rss: indicates should the RSS be reconfigured or not
5866  *
5867  * This function validates number of queues in the context of new channel
5868  * which is being established and determines if RSS should be reconfigured
5869  * or not for parent VSI.
5870  **/
5871 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5872 				    struct i40e_vsi *vsi, bool *reconfig_rss)
5873 {
5874 	int max_ch_queues;
5875 
5876 	if (!reconfig_rss)
5877 		return -EINVAL;
5878 
5879 	*reconfig_rss = false;
5880 	if (vsi->current_rss_size) {
5881 		if (num_queues > vsi->current_rss_size) {
5882 			dev_dbg(&pf->pdev->dev,
5883 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
5884 				num_queues, vsi->current_rss_size);
5885 			return -EINVAL;
5886 		} else if ((num_queues < vsi->current_rss_size) &&
5887 			   (!is_power_of_2(num_queues))) {
5888 			dev_dbg(&pf->pdev->dev,
5889 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5890 				num_queues, vsi->current_rss_size);
5891 			return -EINVAL;
5892 		}
5893 	}
5894 
5895 	if (!is_power_of_2(num_queues)) {
5896 		/* Find the max num_queues configured for channel if channel
5897 		 * exist.
5898 		 * if channel exist, then enforce 'num_queues' to be more than
5899 		 * max ever queues configured for channel.
5900 		 */
5901 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5902 		if (num_queues < max_ch_queues) {
5903 			dev_dbg(&pf->pdev->dev,
5904 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
5905 				num_queues, max_ch_queues);
5906 			return -EINVAL;
5907 		}
5908 		*reconfig_rss = true;
5909 	}
5910 
5911 	return 0;
5912 }
5913 
5914 /**
5915  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5916  * @vsi: the VSI being setup
5917  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5918  *
5919  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5920  **/
5921 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5922 {
5923 	struct i40e_pf *pf = vsi->back;
5924 	u8 seed[I40E_HKEY_ARRAY_SIZE];
5925 	struct i40e_hw *hw = &pf->hw;
5926 	int local_rss_size;
5927 	u8 *lut;
5928 	int ret;
5929 
5930 	if (!vsi->rss_size)
5931 		return -EINVAL;
5932 
5933 	if (rss_size > vsi->rss_size)
5934 		return -EINVAL;
5935 
5936 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
5937 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5938 	if (!lut)
5939 		return -ENOMEM;
5940 
5941 	/* Ignoring user configured lut if there is one */
5942 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5943 
5944 	/* Use user configured hash key if there is one, otherwise
5945 	 * use default.
5946 	 */
5947 	if (vsi->rss_hkey_user)
5948 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5949 	else
5950 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5951 
5952 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5953 	if (ret) {
5954 		dev_info(&pf->pdev->dev,
5955 			 "Cannot set RSS lut, err %s aq_err %s\n",
5956 			 i40e_stat_str(hw, ret),
5957 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5958 		kfree(lut);
5959 		return ret;
5960 	}
5961 	kfree(lut);
5962 
5963 	/* Do the update w.r.t. storing rss_size */
5964 	if (!vsi->orig_rss_size)
5965 		vsi->orig_rss_size = vsi->rss_size;
5966 	vsi->current_rss_size = local_rss_size;
5967 
5968 	return ret;
5969 }
5970 
5971 /**
5972  * i40e_channel_setup_queue_map - Setup a channel queue map
5973  * @pf: ptr to PF device
5974  * @ctxt: VSI context structure
5975  * @ch: ptr to channel structure
5976  *
5977  * Setup queue map for a specific channel
5978  **/
5979 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5980 					 struct i40e_vsi_context *ctxt,
5981 					 struct i40e_channel *ch)
5982 {
5983 	u16 qcount, qmap, sections = 0;
5984 	u8 offset = 0;
5985 	int pow;
5986 
5987 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5988 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5989 
5990 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5991 	ch->num_queue_pairs = qcount;
5992 
5993 	/* find the next higher power-of-2 of num queue pairs */
5994 	pow = ilog2(qcount);
5995 	if (!is_power_of_2(qcount))
5996 		pow++;
5997 
5998 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5999 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6000 
6001 	/* Setup queue TC[0].qmap for given VSI context */
6002 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6003 
6004 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6005 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6006 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6007 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6008 }
6009 
6010 /**
6011  * i40e_add_channel - add a channel by adding VSI
6012  * @pf: ptr to PF device
6013  * @uplink_seid: underlying HW switching element (VEB) ID
6014  * @ch: ptr to channel structure
6015  *
6016  * Add a channel (VSI) using add_vsi and queue_map
6017  **/
6018 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6019 			    struct i40e_channel *ch)
6020 {
6021 	struct i40e_hw *hw = &pf->hw;
6022 	struct i40e_vsi_context ctxt;
6023 	u8 enabled_tc = 0x1; /* TC0 enabled */
6024 	int ret;
6025 
6026 	if (ch->type != I40E_VSI_VMDQ2) {
6027 		dev_info(&pf->pdev->dev,
6028 			 "add new vsi failed, ch->type %d\n", ch->type);
6029 		return -EINVAL;
6030 	}
6031 
6032 	memset(&ctxt, 0, sizeof(ctxt));
6033 	ctxt.pf_num = hw->pf_id;
6034 	ctxt.vf_num = 0;
6035 	ctxt.uplink_seid = uplink_seid;
6036 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6037 	if (ch->type == I40E_VSI_VMDQ2)
6038 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6039 
6040 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6041 		ctxt.info.valid_sections |=
6042 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6043 		ctxt.info.switch_id =
6044 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6045 	}
6046 
6047 	/* Set queue map for a given VSI context */
6048 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6049 
6050 	/* Now time to create VSI */
6051 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6052 	if (ret) {
6053 		dev_info(&pf->pdev->dev,
6054 			 "add new vsi failed, err %s aq_err %s\n",
6055 			 i40e_stat_str(&pf->hw, ret),
6056 			 i40e_aq_str(&pf->hw,
6057 				     pf->hw.aq.asq_last_status));
6058 		return -ENOENT;
6059 	}
6060 
6061 	/* Success, update channel, set enabled_tc only if the channel
6062 	 * is not a macvlan
6063 	 */
6064 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6065 	ch->seid = ctxt.seid;
6066 	ch->vsi_number = ctxt.vsi_number;
6067 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6068 
6069 	/* copy just the sections touched not the entire info
6070 	 * since not all sections are valid as returned by
6071 	 * update vsi params
6072 	 */
6073 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6074 	memcpy(&ch->info.queue_mapping,
6075 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6076 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6077 	       sizeof(ctxt.info.tc_mapping));
6078 
6079 	return 0;
6080 }
6081 
6082 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6083 				  u8 *bw_share)
6084 {
6085 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6086 	i40e_status ret;
6087 	int i;
6088 
6089 	memset(&bw_data, 0, sizeof(bw_data));
6090 	bw_data.tc_valid_bits = ch->enabled_tc;
6091 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6092 		bw_data.tc_bw_credits[i] = bw_share[i];
6093 
6094 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6095 				       &bw_data, NULL);
6096 	if (ret) {
6097 		dev_info(&vsi->back->pdev->dev,
6098 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6099 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6100 		return -EINVAL;
6101 	}
6102 
6103 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6104 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6105 
6106 	return 0;
6107 }
6108 
6109 /**
6110  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6111  * @pf: ptr to PF device
6112  * @vsi: the VSI being setup
6113  * @ch: ptr to channel structure
6114  *
6115  * Configure TX rings associated with channel (VSI) since queues are being
6116  * from parent VSI.
6117  **/
6118 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6119 				       struct i40e_vsi *vsi,
6120 				       struct i40e_channel *ch)
6121 {
6122 	i40e_status ret;
6123 	int i;
6124 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6125 
6126 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6127 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6128 		if (ch->enabled_tc & BIT(i))
6129 			bw_share[i] = 1;
6130 	}
6131 
6132 	/* configure BW for new VSI */
6133 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6134 	if (ret) {
6135 		dev_info(&vsi->back->pdev->dev,
6136 			 "Failed configuring TC map %d for channel (seid %u)\n",
6137 			 ch->enabled_tc, ch->seid);
6138 		return ret;
6139 	}
6140 
6141 	for (i = 0; i < ch->num_queue_pairs; i++) {
6142 		struct i40e_ring *tx_ring, *rx_ring;
6143 		u16 pf_q;
6144 
6145 		pf_q = ch->base_queue + i;
6146 
6147 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6148 		 * context
6149 		 */
6150 		tx_ring = vsi->tx_rings[pf_q];
6151 		tx_ring->ch = ch;
6152 
6153 		/* Get the RX ring ptr */
6154 		rx_ring = vsi->rx_rings[pf_q];
6155 		rx_ring->ch = ch;
6156 	}
6157 
6158 	return 0;
6159 }
6160 
6161 /**
6162  * i40e_setup_hw_channel - setup new channel
6163  * @pf: ptr to PF device
6164  * @vsi: the VSI being setup
6165  * @ch: ptr to channel structure
6166  * @uplink_seid: underlying HW switching element (VEB) ID
6167  * @type: type of channel to be created (VMDq2/VF)
6168  *
6169  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6170  * and configures TX rings accordingly
6171  **/
6172 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6173 					struct i40e_vsi *vsi,
6174 					struct i40e_channel *ch,
6175 					u16 uplink_seid, u8 type)
6176 {
6177 	int ret;
6178 
6179 	ch->initialized = false;
6180 	ch->base_queue = vsi->next_base_queue;
6181 	ch->type = type;
6182 
6183 	/* Proceed with creation of channel (VMDq2) VSI */
6184 	ret = i40e_add_channel(pf, uplink_seid, ch);
6185 	if (ret) {
6186 		dev_info(&pf->pdev->dev,
6187 			 "failed to add_channel using uplink_seid %u\n",
6188 			 uplink_seid);
6189 		return ret;
6190 	}
6191 
6192 	/* Mark the successful creation of channel */
6193 	ch->initialized = true;
6194 
6195 	/* Reconfigure TX queues using QTX_CTL register */
6196 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6197 	if (ret) {
6198 		dev_info(&pf->pdev->dev,
6199 			 "failed to configure TX rings for channel %u\n",
6200 			 ch->seid);
6201 		return ret;
6202 	}
6203 
6204 	/* update 'next_base_queue' */
6205 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6206 	dev_dbg(&pf->pdev->dev,
6207 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6208 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6209 		ch->num_queue_pairs,
6210 		vsi->next_base_queue);
6211 	return ret;
6212 }
6213 
6214 /**
6215  * i40e_setup_channel - setup new channel using uplink element
6216  * @pf: ptr to PF device
6217  * @vsi: pointer to the VSI to set up the channel within
6218  * @ch: ptr to channel structure
6219  *
6220  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6221  * and uplink switching element (uplink_seid)
6222  **/
6223 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6224 			       struct i40e_channel *ch)
6225 {
6226 	u8 vsi_type;
6227 	u16 seid;
6228 	int ret;
6229 
6230 	if (vsi->type == I40E_VSI_MAIN) {
6231 		vsi_type = I40E_VSI_VMDQ2;
6232 	} else {
6233 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6234 			vsi->type);
6235 		return false;
6236 	}
6237 
6238 	/* underlying switching element */
6239 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6240 
6241 	/* create channel (VSI), configure TX rings */
6242 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6243 	if (ret) {
6244 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6245 		return false;
6246 	}
6247 
6248 	return ch->initialized ? true : false;
6249 }
6250 
6251 /**
6252  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6253  * @vsi: ptr to VSI which has PF backing
6254  *
6255  * Sets up switch mode correctly if it needs to be changed and perform
6256  * what are allowed modes.
6257  **/
6258 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6259 {
6260 	u8 mode;
6261 	struct i40e_pf *pf = vsi->back;
6262 	struct i40e_hw *hw = &pf->hw;
6263 	int ret;
6264 
6265 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6266 	if (ret)
6267 		return -EINVAL;
6268 
6269 	if (hw->dev_caps.switch_mode) {
6270 		/* if switch mode is set, support mode2 (non-tunneled for
6271 		 * cloud filter) for now
6272 		 */
6273 		u32 switch_mode = hw->dev_caps.switch_mode &
6274 				  I40E_SWITCH_MODE_MASK;
6275 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6276 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6277 				return 0;
6278 			dev_err(&pf->pdev->dev,
6279 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6280 				hw->dev_caps.switch_mode);
6281 			return -EINVAL;
6282 		}
6283 	}
6284 
6285 	/* Set Bit 7 to be valid */
6286 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6287 
6288 	/* Set L4type for TCP support */
6289 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6290 
6291 	/* Set cloud filter mode */
6292 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6293 
6294 	/* Prep mode field for set_switch_config */
6295 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6296 					pf->last_sw_conf_valid_flags,
6297 					mode, NULL);
6298 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6299 		dev_err(&pf->pdev->dev,
6300 			"couldn't set switch config bits, err %s aq_err %s\n",
6301 			i40e_stat_str(hw, ret),
6302 			i40e_aq_str(hw,
6303 				    hw->aq.asq_last_status));
6304 
6305 	return ret;
6306 }
6307 
6308 /**
6309  * i40e_create_queue_channel - function to create channel
6310  * @vsi: VSI to be configured
6311  * @ch: ptr to channel (it contains channel specific params)
6312  *
6313  * This function creates channel (VSI) using num_queues specified by user,
6314  * reconfigs RSS if needed.
6315  **/
6316 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6317 			      struct i40e_channel *ch)
6318 {
6319 	struct i40e_pf *pf = vsi->back;
6320 	bool reconfig_rss;
6321 	int err;
6322 
6323 	if (!ch)
6324 		return -EINVAL;
6325 
6326 	if (!ch->num_queue_pairs) {
6327 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6328 			ch->num_queue_pairs);
6329 		return -EINVAL;
6330 	}
6331 
6332 	/* validate user requested num_queues for channel */
6333 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6334 				       &reconfig_rss);
6335 	if (err) {
6336 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6337 			 ch->num_queue_pairs);
6338 		return -EINVAL;
6339 	}
6340 
6341 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6342 	 * VSI to be added switch to VEB mode.
6343 	 */
6344 
6345 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6346 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6347 
6348 		if (vsi->type == I40E_VSI_MAIN) {
6349 			if (pf->flags & I40E_FLAG_TC_MQPRIO)
6350 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6351 			else
6352 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6353 		}
6354 		/* now onwards for main VSI, number of queues will be value
6355 		 * of TC0's queue count
6356 		 */
6357 	}
6358 
6359 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6360 	 * it should be more than num_queues
6361 	 */
6362 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6363 		dev_dbg(&pf->pdev->dev,
6364 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6365 			vsi->cnt_q_avail, ch->num_queue_pairs);
6366 		return -EINVAL;
6367 	}
6368 
6369 	/* reconfig_rss only if vsi type is MAIN_VSI */
6370 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6371 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6372 		if (err) {
6373 			dev_info(&pf->pdev->dev,
6374 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6375 				 ch->num_queue_pairs);
6376 			return -EINVAL;
6377 		}
6378 	}
6379 
6380 	if (!i40e_setup_channel(pf, vsi, ch)) {
6381 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6382 		return -EINVAL;
6383 	}
6384 
6385 	dev_info(&pf->pdev->dev,
6386 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6387 		 ch->seid, ch->num_queue_pairs);
6388 
6389 	/* configure VSI for BW limit */
6390 	if (ch->max_tx_rate) {
6391 		u64 credits = ch->max_tx_rate;
6392 
6393 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6394 			return -EINVAL;
6395 
6396 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6397 		dev_dbg(&pf->pdev->dev,
6398 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6399 			ch->max_tx_rate,
6400 			credits,
6401 			ch->seid);
6402 	}
6403 
6404 	/* in case of VF, this will be main SRIOV VSI */
6405 	ch->parent_vsi = vsi;
6406 
6407 	/* and update main_vsi's count for queue_available to use */
6408 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6409 
6410 	return 0;
6411 }
6412 
6413 /**
6414  * i40e_configure_queue_channels - Add queue channel for the given TCs
6415  * @vsi: VSI to be configured
6416  *
6417  * Configures queue channel mapping to the given TCs
6418  **/
6419 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6420 {
6421 	struct i40e_channel *ch;
6422 	u64 max_rate = 0;
6423 	int ret = 0, i;
6424 
6425 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6426 	vsi->tc_seid_map[0] = vsi->seid;
6427 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6428 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6429 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6430 			if (!ch) {
6431 				ret = -ENOMEM;
6432 				goto err_free;
6433 			}
6434 
6435 			INIT_LIST_HEAD(&ch->list);
6436 			ch->num_queue_pairs =
6437 				vsi->tc_config.tc_info[i].qcount;
6438 			ch->base_queue =
6439 				vsi->tc_config.tc_info[i].qoffset;
6440 
6441 			/* Bandwidth limit through tc interface is in bytes/s,
6442 			 * change to Mbit/s
6443 			 */
6444 			max_rate = vsi->mqprio_qopt.max_rate[i];
6445 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6446 			ch->max_tx_rate = max_rate;
6447 
6448 			list_add_tail(&ch->list, &vsi->ch_list);
6449 
6450 			ret = i40e_create_queue_channel(vsi, ch);
6451 			if (ret) {
6452 				dev_err(&vsi->back->pdev->dev,
6453 					"Failed creating queue channel with TC%d: queues %d\n",
6454 					i, ch->num_queue_pairs);
6455 				goto err_free;
6456 			}
6457 			vsi->tc_seid_map[i] = ch->seid;
6458 		}
6459 	}
6460 	return ret;
6461 
6462 err_free:
6463 	i40e_remove_queue_channels(vsi);
6464 	return ret;
6465 }
6466 
6467 /**
6468  * i40e_veb_config_tc - Configure TCs for given VEB
6469  * @veb: given VEB
6470  * @enabled_tc: TC bitmap
6471  *
6472  * Configures given TC bitmap for VEB (switching) element
6473  **/
6474 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6475 {
6476 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6477 	struct i40e_pf *pf = veb->pf;
6478 	int ret = 0;
6479 	int i;
6480 
6481 	/* No TCs or already enabled TCs just return */
6482 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6483 		return ret;
6484 
6485 	bw_data.tc_valid_bits = enabled_tc;
6486 	/* bw_data.absolute_credits is not set (relative) */
6487 
6488 	/* Enable ETS TCs with equal BW Share for now */
6489 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6490 		if (enabled_tc & BIT(i))
6491 			bw_data.tc_bw_share_credits[i] = 1;
6492 	}
6493 
6494 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6495 						   &bw_data, NULL);
6496 	if (ret) {
6497 		dev_info(&pf->pdev->dev,
6498 			 "VEB bw config failed, err %s aq_err %s\n",
6499 			 i40e_stat_str(&pf->hw, ret),
6500 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6501 		goto out;
6502 	}
6503 
6504 	/* Update the BW information */
6505 	ret = i40e_veb_get_bw_info(veb);
6506 	if (ret) {
6507 		dev_info(&pf->pdev->dev,
6508 			 "Failed getting veb bw config, err %s aq_err %s\n",
6509 			 i40e_stat_str(&pf->hw, ret),
6510 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6511 	}
6512 
6513 out:
6514 	return ret;
6515 }
6516 
6517 #ifdef CONFIG_I40E_DCB
6518 /**
6519  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6520  * @pf: PF struct
6521  *
6522  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6523  * the caller would've quiesce all the VSIs before calling
6524  * this function
6525  **/
6526 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6527 {
6528 	u8 tc_map = 0;
6529 	int ret;
6530 	u8 v;
6531 
6532 	/* Enable the TCs available on PF to all VEBs */
6533 	tc_map = i40e_pf_get_tc_map(pf);
6534 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6535 		return;
6536 
6537 	for (v = 0; v < I40E_MAX_VEB; v++) {
6538 		if (!pf->veb[v])
6539 			continue;
6540 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6541 		if (ret) {
6542 			dev_info(&pf->pdev->dev,
6543 				 "Failed configuring TC for VEB seid=%d\n",
6544 				 pf->veb[v]->seid);
6545 			/* Will try to configure as many components */
6546 		}
6547 	}
6548 
6549 	/* Update each VSI */
6550 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6551 		if (!pf->vsi[v])
6552 			continue;
6553 
6554 		/* - Enable all TCs for the LAN VSI
6555 		 * - For all others keep them at TC0 for now
6556 		 */
6557 		if (v == pf->lan_vsi)
6558 			tc_map = i40e_pf_get_tc_map(pf);
6559 		else
6560 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6561 
6562 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6563 		if (ret) {
6564 			dev_info(&pf->pdev->dev,
6565 				 "Failed configuring TC for VSI seid=%d\n",
6566 				 pf->vsi[v]->seid);
6567 			/* Will try to configure as many components */
6568 		} else {
6569 			/* Re-configure VSI vectors based on updated TC map */
6570 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6571 			if (pf->vsi[v]->netdev)
6572 				i40e_dcbnl_set_all(pf->vsi[v]);
6573 		}
6574 	}
6575 }
6576 
6577 /**
6578  * i40e_resume_port_tx - Resume port Tx
6579  * @pf: PF struct
6580  *
6581  * Resume a port's Tx and issue a PF reset in case of failure to
6582  * resume.
6583  **/
6584 static int i40e_resume_port_tx(struct i40e_pf *pf)
6585 {
6586 	struct i40e_hw *hw = &pf->hw;
6587 	int ret;
6588 
6589 	ret = i40e_aq_resume_port_tx(hw, NULL);
6590 	if (ret) {
6591 		dev_info(&pf->pdev->dev,
6592 			 "Resume Port Tx failed, err %s aq_err %s\n",
6593 			  i40e_stat_str(&pf->hw, ret),
6594 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6595 		/* Schedule PF reset to recover */
6596 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6597 		i40e_service_event_schedule(pf);
6598 	}
6599 
6600 	return ret;
6601 }
6602 
6603 /**
6604  * i40e_suspend_port_tx - Suspend port Tx
6605  * @pf: PF struct
6606  *
6607  * Suspend a port's Tx and issue a PF reset in case of failure.
6608  **/
6609 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6610 {
6611 	struct i40e_hw *hw = &pf->hw;
6612 	int ret;
6613 
6614 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6615 	if (ret) {
6616 		dev_info(&pf->pdev->dev,
6617 			 "Suspend Port Tx failed, err %s aq_err %s\n",
6618 			 i40e_stat_str(&pf->hw, ret),
6619 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6620 		/* Schedule PF reset to recover */
6621 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6622 		i40e_service_event_schedule(pf);
6623 	}
6624 
6625 	return ret;
6626 }
6627 
6628 /**
6629  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6630  * @pf: PF being configured
6631  * @new_cfg: New DCBX configuration
6632  *
6633  * Program DCB settings into HW and reconfigure VEB/VSIs on
6634  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6635  **/
6636 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6637 				  struct i40e_dcbx_config *new_cfg)
6638 {
6639 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6640 	int ret;
6641 
6642 	/* Check if need reconfiguration */
6643 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6644 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6645 		return 0;
6646 	}
6647 
6648 	/* Config change disable all VSIs */
6649 	i40e_pf_quiesce_all_vsi(pf);
6650 
6651 	/* Copy the new config to the current config */
6652 	*old_cfg = *new_cfg;
6653 	old_cfg->etsrec = old_cfg->etscfg;
6654 	ret = i40e_set_dcb_config(&pf->hw);
6655 	if (ret) {
6656 		dev_info(&pf->pdev->dev,
6657 			 "Set DCB Config failed, err %s aq_err %s\n",
6658 			 i40e_stat_str(&pf->hw, ret),
6659 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6660 		goto out;
6661 	}
6662 
6663 	/* Changes in configuration update VEB/VSI */
6664 	i40e_dcb_reconfigure(pf);
6665 out:
6666 	/* In case of reset do not try to resume anything */
6667 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6668 		/* Re-start the VSIs if disabled */
6669 		ret = i40e_resume_port_tx(pf);
6670 		/* In case of error no point in resuming VSIs */
6671 		if (ret)
6672 			goto err;
6673 		i40e_pf_unquiesce_all_vsi(pf);
6674 	}
6675 err:
6676 	return ret;
6677 }
6678 
6679 /**
6680  * i40e_hw_dcb_config - Program new DCBX settings into HW
6681  * @pf: PF being configured
6682  * @new_cfg: New DCBX configuration
6683  *
6684  * Program DCB settings into HW and reconfigure VEB/VSIs on
6685  * given PF
6686  **/
6687 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6688 {
6689 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6690 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6691 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6692 	struct i40e_dcbx_config *old_cfg;
6693 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6694 	struct i40e_rx_pb_config pb_cfg;
6695 	struct i40e_hw *hw = &pf->hw;
6696 	u8 num_ports = hw->num_ports;
6697 	bool need_reconfig;
6698 	int ret = -EINVAL;
6699 	u8 lltc_map = 0;
6700 	u8 tc_map = 0;
6701 	u8 new_numtc;
6702 	u8 i;
6703 
6704 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6705 	/* Un-pack information to Program ETS HW via shared API
6706 	 * numtc, tcmap
6707 	 * LLTC map
6708 	 * ETS/NON-ETS arbiter mode
6709 	 * max exponent (credit refills)
6710 	 * Total number of ports
6711 	 * PFC priority bit-map
6712 	 * Priority Table
6713 	 * BW % per TC
6714 	 * Arbiter mode between UPs sharing same TC
6715 	 * TSA table (ETS or non-ETS)
6716 	 * EEE enabled or not
6717 	 * MFS TC table
6718 	 */
6719 
6720 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6721 
6722 	memset(&ets_data, 0, sizeof(ets_data));
6723 	for (i = 0; i < new_numtc; i++) {
6724 		tc_map |= BIT(i);
6725 		switch (new_cfg->etscfg.tsatable[i]) {
6726 		case I40E_IEEE_TSA_ETS:
6727 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6728 			ets_data.tc_bw_share_credits[i] =
6729 					new_cfg->etscfg.tcbwtable[i];
6730 			break;
6731 		case I40E_IEEE_TSA_STRICT:
6732 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6733 			lltc_map |= BIT(i);
6734 			ets_data.tc_bw_share_credits[i] =
6735 					I40E_DCB_STRICT_PRIO_CREDITS;
6736 			break;
6737 		default:
6738 			/* Invalid TSA type */
6739 			need_reconfig = false;
6740 			goto out;
6741 		}
6742 	}
6743 
6744 	old_cfg = &hw->local_dcbx_config;
6745 	/* Check if need reconfiguration */
6746 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6747 
6748 	/* If needed, enable/disable frame tagging, disable all VSIs
6749 	 * and suspend port tx
6750 	 */
6751 	if (need_reconfig) {
6752 		/* Enable DCB tagging only when more than one TC */
6753 		if (new_numtc > 1)
6754 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6755 		else
6756 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6757 
6758 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6759 		/* Reconfiguration needed quiesce all VSIs */
6760 		i40e_pf_quiesce_all_vsi(pf);
6761 		ret = i40e_suspend_port_tx(pf);
6762 		if (ret)
6763 			goto err;
6764 	}
6765 
6766 	/* Configure Port ETS Tx Scheduler */
6767 	ets_data.tc_valid_bits = tc_map;
6768 	ets_data.tc_strict_priority_flags = lltc_map;
6769 	ret = i40e_aq_config_switch_comp_ets
6770 		(hw, pf->mac_seid, &ets_data,
6771 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
6772 	if (ret) {
6773 		dev_info(&pf->pdev->dev,
6774 			 "Modify Port ETS failed, err %s aq_err %s\n",
6775 			 i40e_stat_str(&pf->hw, ret),
6776 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6777 		goto out;
6778 	}
6779 
6780 	/* Configure Rx ETS HW */
6781 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
6782 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
6783 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
6784 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
6785 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
6786 				   lltc_map);
6787 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
6788 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
6789 				     prio_type);
6790 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
6791 			       new_cfg->etscfg.prioritytable);
6792 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
6793 
6794 	/* Configure Rx Packet Buffers in HW */
6795 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6796 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
6797 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
6798 	}
6799 
6800 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
6801 					 false, new_cfg->pfc.pfcenable,
6802 					 mfs_tc, &pb_cfg);
6803 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
6804 
6805 	/* Update the local Rx Packet buffer config */
6806 	pf->pb_cfg = pb_cfg;
6807 
6808 	/* Inform the FW about changes to DCB configuration */
6809 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
6810 	if (ret) {
6811 		dev_info(&pf->pdev->dev,
6812 			 "DCB Updated failed, err %s aq_err %s\n",
6813 			 i40e_stat_str(&pf->hw, ret),
6814 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6815 		goto out;
6816 	}
6817 
6818 	/* Update the port DCBx configuration */
6819 	*old_cfg = *new_cfg;
6820 
6821 	/* Changes in configuration update VEB/VSI */
6822 	i40e_dcb_reconfigure(pf);
6823 out:
6824 	/* Re-start the VSIs if disabled */
6825 	if (need_reconfig) {
6826 		ret = i40e_resume_port_tx(pf);
6827 
6828 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
6829 		/* In case of error no point in resuming VSIs */
6830 		if (ret)
6831 			goto err;
6832 
6833 		/* Wait for the PF's queues to be disabled */
6834 		ret = i40e_pf_wait_queues_disabled(pf);
6835 		if (ret) {
6836 			/* Schedule PF reset to recover */
6837 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6838 			i40e_service_event_schedule(pf);
6839 			goto err;
6840 		} else {
6841 			i40e_pf_unquiesce_all_vsi(pf);
6842 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
6843 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
6844 		}
6845 		/* registers are set, lets apply */
6846 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
6847 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
6848 	}
6849 
6850 err:
6851 	return ret;
6852 }
6853 
6854 /**
6855  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
6856  * @pf: PF being queried
6857  *
6858  * Set default DCB configuration in case DCB is to be done in SW.
6859  **/
6860 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
6861 {
6862 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
6863 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6864 	struct i40e_hw *hw = &pf->hw;
6865 	int err;
6866 
6867 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
6868 		/* Update the local cached instance with TC0 ETS */
6869 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
6870 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
6871 		pf->tmp_cfg.etscfg.maxtcs = 0;
6872 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
6873 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
6874 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
6875 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
6876 		/* FW needs one App to configure HW */
6877 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
6878 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
6879 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
6880 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
6881 
6882 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
6883 	}
6884 
6885 	memset(&ets_data, 0, sizeof(ets_data));
6886 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
6887 	ets_data.tc_strict_priority_flags = 0; /* ETS */
6888 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
6889 
6890 	/* Enable ETS on the Physical port */
6891 	err = i40e_aq_config_switch_comp_ets
6892 		(hw, pf->mac_seid, &ets_data,
6893 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
6894 	if (err) {
6895 		dev_info(&pf->pdev->dev,
6896 			 "Enable Port ETS failed, err %s aq_err %s\n",
6897 			 i40e_stat_str(&pf->hw, err),
6898 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6899 		err = -ENOENT;
6900 		goto out;
6901 	}
6902 
6903 	/* Update the local cached instance with TC0 ETS */
6904 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
6905 	dcb_cfg->etscfg.cbs = 0;
6906 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
6907 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
6908 
6909 out:
6910 	return err;
6911 }
6912 
6913 /**
6914  * i40e_init_pf_dcb - Initialize DCB configuration
6915  * @pf: PF being configured
6916  *
6917  * Query the current DCB configuration and cache it
6918  * in the hardware structure
6919  **/
6920 static int i40e_init_pf_dcb(struct i40e_pf *pf)
6921 {
6922 	struct i40e_hw *hw = &pf->hw;
6923 	int err;
6924 
6925 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
6926 	 * Also do not enable DCBx if FW LLDP agent is disabled
6927 	 */
6928 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
6929 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
6930 		err = I40E_NOT_SUPPORTED;
6931 		goto out;
6932 	}
6933 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
6934 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
6935 		err = i40e_dcb_sw_default_config(pf);
6936 		if (err) {
6937 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
6938 			goto out;
6939 		}
6940 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
6941 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
6942 			       DCB_CAP_DCBX_VER_IEEE;
6943 		/* at init capable but disabled */
6944 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
6945 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6946 		goto out;
6947 	}
6948 	err = i40e_init_dcb(hw, true);
6949 	if (!err) {
6950 		/* Device/Function is not DCBX capable */
6951 		if ((!hw->func_caps.dcb) ||
6952 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6953 			dev_info(&pf->pdev->dev,
6954 				 "DCBX offload is not supported or is disabled for this PF.\n");
6955 		} else {
6956 			/* When status is not DISABLED then DCBX in FW */
6957 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6958 				       DCB_CAP_DCBX_VER_IEEE;
6959 
6960 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
6961 			/* Enable DCB tagging only when more than one TC
6962 			 * or explicitly disable if only one TC
6963 			 */
6964 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6965 				pf->flags |= I40E_FLAG_DCB_ENABLED;
6966 			else
6967 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6968 			dev_dbg(&pf->pdev->dev,
6969 				"DCBX offload is supported for this PF.\n");
6970 		}
6971 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
6972 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
6973 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
6974 	} else {
6975 		dev_info(&pf->pdev->dev,
6976 			 "Query for DCB configuration failed, err %s aq_err %s\n",
6977 			 i40e_stat_str(&pf->hw, err),
6978 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6979 	}
6980 
6981 out:
6982 	return err;
6983 }
6984 #endif /* CONFIG_I40E_DCB */
6985 
6986 /**
6987  * i40e_print_link_message - print link up or down
6988  * @vsi: the VSI for which link needs a message
6989  * @isup: true of link is up, false otherwise
6990  */
6991 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
6992 {
6993 	enum i40e_aq_link_speed new_speed;
6994 	struct i40e_pf *pf = vsi->back;
6995 	char *speed = "Unknown";
6996 	char *fc = "Unknown";
6997 	char *fec = "";
6998 	char *req_fec = "";
6999 	char *an = "";
7000 
7001 	if (isup)
7002 		new_speed = pf->hw.phy.link_info.link_speed;
7003 	else
7004 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7005 
7006 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7007 		return;
7008 	vsi->current_isup = isup;
7009 	vsi->current_speed = new_speed;
7010 	if (!isup) {
7011 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7012 		return;
7013 	}
7014 
7015 	/* Warn user if link speed on NPAR enabled partition is not at
7016 	 * least 10GB
7017 	 */
7018 	if (pf->hw.func_caps.npar_enable &&
7019 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7020 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7021 		netdev_warn(vsi->netdev,
7022 			    "The partition detected link speed that is less than 10Gbps\n");
7023 
7024 	switch (pf->hw.phy.link_info.link_speed) {
7025 	case I40E_LINK_SPEED_40GB:
7026 		speed = "40 G";
7027 		break;
7028 	case I40E_LINK_SPEED_20GB:
7029 		speed = "20 G";
7030 		break;
7031 	case I40E_LINK_SPEED_25GB:
7032 		speed = "25 G";
7033 		break;
7034 	case I40E_LINK_SPEED_10GB:
7035 		speed = "10 G";
7036 		break;
7037 	case I40E_LINK_SPEED_5GB:
7038 		speed = "5 G";
7039 		break;
7040 	case I40E_LINK_SPEED_2_5GB:
7041 		speed = "2.5 G";
7042 		break;
7043 	case I40E_LINK_SPEED_1GB:
7044 		speed = "1000 M";
7045 		break;
7046 	case I40E_LINK_SPEED_100MB:
7047 		speed = "100 M";
7048 		break;
7049 	default:
7050 		break;
7051 	}
7052 
7053 	switch (pf->hw.fc.current_mode) {
7054 	case I40E_FC_FULL:
7055 		fc = "RX/TX";
7056 		break;
7057 	case I40E_FC_TX_PAUSE:
7058 		fc = "TX";
7059 		break;
7060 	case I40E_FC_RX_PAUSE:
7061 		fc = "RX";
7062 		break;
7063 	default:
7064 		fc = "None";
7065 		break;
7066 	}
7067 
7068 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7069 		req_fec = "None";
7070 		fec = "None";
7071 		an = "False";
7072 
7073 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7074 			an = "True";
7075 
7076 		if (pf->hw.phy.link_info.fec_info &
7077 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7078 			fec = "CL74 FC-FEC/BASE-R";
7079 		else if (pf->hw.phy.link_info.fec_info &
7080 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7081 			fec = "CL108 RS-FEC";
7082 
7083 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7084 		 * both RS and FC are requested
7085 		 */
7086 		if (vsi->back->hw.phy.link_info.req_fec_info &
7087 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7088 			if (vsi->back->hw.phy.link_info.req_fec_info &
7089 			    I40E_AQ_REQUEST_FEC_RS)
7090 				req_fec = "CL108 RS-FEC";
7091 			else
7092 				req_fec = "CL74 FC-FEC/BASE-R";
7093 		}
7094 		netdev_info(vsi->netdev,
7095 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7096 			    speed, req_fec, fec, an, fc);
7097 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7098 		req_fec = "None";
7099 		fec = "None";
7100 		an = "False";
7101 
7102 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7103 			an = "True";
7104 
7105 		if (pf->hw.phy.link_info.fec_info &
7106 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7107 			fec = "CL74 FC-FEC/BASE-R";
7108 
7109 		if (pf->hw.phy.link_info.req_fec_info &
7110 		    I40E_AQ_REQUEST_FEC_KR)
7111 			req_fec = "CL74 FC-FEC/BASE-R";
7112 
7113 		netdev_info(vsi->netdev,
7114 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7115 			    speed, req_fec, fec, an, fc);
7116 	} else {
7117 		netdev_info(vsi->netdev,
7118 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7119 			    speed, fc);
7120 	}
7121 
7122 }
7123 
7124 /**
7125  * i40e_up_complete - Finish the last steps of bringing up a connection
7126  * @vsi: the VSI being configured
7127  **/
7128 static int i40e_up_complete(struct i40e_vsi *vsi)
7129 {
7130 	struct i40e_pf *pf = vsi->back;
7131 	int err;
7132 
7133 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7134 		i40e_vsi_configure_msix(vsi);
7135 	else
7136 		i40e_configure_msi_and_legacy(vsi);
7137 
7138 	/* start rings */
7139 	err = i40e_vsi_start_rings(vsi);
7140 	if (err)
7141 		return err;
7142 
7143 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7144 	i40e_napi_enable_all(vsi);
7145 	i40e_vsi_enable_irq(vsi);
7146 
7147 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7148 	    (vsi->netdev)) {
7149 		i40e_print_link_message(vsi, true);
7150 		netif_tx_start_all_queues(vsi->netdev);
7151 		netif_carrier_on(vsi->netdev);
7152 	}
7153 
7154 	/* replay FDIR SB filters */
7155 	if (vsi->type == I40E_VSI_FDIR) {
7156 		/* reset fd counters */
7157 		pf->fd_add_err = 0;
7158 		pf->fd_atr_cnt = 0;
7159 		i40e_fdir_filter_restore(vsi);
7160 	}
7161 
7162 	/* On the next run of the service_task, notify any clients of the new
7163 	 * opened netdev
7164 	 */
7165 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7166 	i40e_service_event_schedule(pf);
7167 
7168 	return 0;
7169 }
7170 
7171 /**
7172  * i40e_vsi_reinit_locked - Reset the VSI
7173  * @vsi: the VSI being configured
7174  *
7175  * Rebuild the ring structs after some configuration
7176  * has changed, e.g. MTU size.
7177  **/
7178 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7179 {
7180 	struct i40e_pf *pf = vsi->back;
7181 
7182 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7183 		usleep_range(1000, 2000);
7184 	i40e_down(vsi);
7185 
7186 	i40e_up(vsi);
7187 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7188 }
7189 
7190 /**
7191  * i40e_force_link_state - Force the link status
7192  * @pf: board private structure
7193  * @is_up: whether the link state should be forced up or down
7194  **/
7195 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7196 {
7197 	struct i40e_aq_get_phy_abilities_resp abilities;
7198 	struct i40e_aq_set_phy_config config = {0};
7199 	bool non_zero_phy_type = is_up;
7200 	struct i40e_hw *hw = &pf->hw;
7201 	i40e_status err;
7202 	u64 mask;
7203 	u8 speed;
7204 
7205 	/* Card might've been put in an unstable state by other drivers
7206 	 * and applications, which causes incorrect speed values being
7207 	 * set on startup. In order to clear speed registers, we call
7208 	 * get_phy_capabilities twice, once to get initial state of
7209 	 * available speeds, and once to get current PHY config.
7210 	 */
7211 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7212 					   NULL);
7213 	if (err) {
7214 		dev_err(&pf->pdev->dev,
7215 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7216 			i40e_stat_str(hw, err),
7217 			i40e_aq_str(hw, hw->aq.asq_last_status));
7218 		return err;
7219 	}
7220 	speed = abilities.link_speed;
7221 
7222 	/* Get the current phy config */
7223 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7224 					   NULL);
7225 	if (err) {
7226 		dev_err(&pf->pdev->dev,
7227 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7228 			i40e_stat_str(hw, err),
7229 			i40e_aq_str(hw, hw->aq.asq_last_status));
7230 		return err;
7231 	}
7232 
7233 	/* If link needs to go up, but was not forced to go down,
7234 	 * and its speed values are OK, no need for a flap
7235 	 * if non_zero_phy_type was set, still need to force up
7236 	 */
7237 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7238 		non_zero_phy_type = true;
7239 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7240 		return I40E_SUCCESS;
7241 
7242 	/* To force link we need to set bits for all supported PHY types,
7243 	 * but there are now more than 32, so we need to split the bitmap
7244 	 * across two fields.
7245 	 */
7246 	mask = I40E_PHY_TYPES_BITMASK;
7247 	config.phy_type =
7248 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7249 	config.phy_type_ext =
7250 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7251 	/* Copy the old settings, except of phy_type */
7252 	config.abilities = abilities.abilities;
7253 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7254 		if (is_up)
7255 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7256 		else
7257 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7258 	}
7259 	if (abilities.link_speed != 0)
7260 		config.link_speed = abilities.link_speed;
7261 	else
7262 		config.link_speed = speed;
7263 	config.eee_capability = abilities.eee_capability;
7264 	config.eeer = abilities.eeer_val;
7265 	config.low_power_ctrl = abilities.d3_lpan;
7266 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7267 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7268 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7269 
7270 	if (err) {
7271 		dev_err(&pf->pdev->dev,
7272 			"set phy config ret =  %s last_status =  %s\n",
7273 			i40e_stat_str(&pf->hw, err),
7274 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7275 		return err;
7276 	}
7277 
7278 	/* Update the link info */
7279 	err = i40e_update_link_info(hw);
7280 	if (err) {
7281 		/* Wait a little bit (on 40G cards it sometimes takes a really
7282 		 * long time for link to come back from the atomic reset)
7283 		 * and try once more
7284 		 */
7285 		msleep(1000);
7286 		i40e_update_link_info(hw);
7287 	}
7288 
7289 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7290 
7291 	return I40E_SUCCESS;
7292 }
7293 
7294 /**
7295  * i40e_up - Bring the connection back up after being down
7296  * @vsi: the VSI being configured
7297  **/
7298 int i40e_up(struct i40e_vsi *vsi)
7299 {
7300 	int err;
7301 
7302 	if (vsi->type == I40E_VSI_MAIN &&
7303 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7304 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7305 		i40e_force_link_state(vsi->back, true);
7306 
7307 	err = i40e_vsi_configure(vsi);
7308 	if (!err)
7309 		err = i40e_up_complete(vsi);
7310 
7311 	return err;
7312 }
7313 
7314 /**
7315  * i40e_down - Shutdown the connection processing
7316  * @vsi: the VSI being stopped
7317  **/
7318 void i40e_down(struct i40e_vsi *vsi)
7319 {
7320 	int i;
7321 
7322 	/* It is assumed that the caller of this function
7323 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7324 	 */
7325 	if (vsi->netdev) {
7326 		netif_carrier_off(vsi->netdev);
7327 		netif_tx_disable(vsi->netdev);
7328 	}
7329 	i40e_vsi_disable_irq(vsi);
7330 	i40e_vsi_stop_rings(vsi);
7331 	if (vsi->type == I40E_VSI_MAIN &&
7332 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7333 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7334 		i40e_force_link_state(vsi->back, false);
7335 	i40e_napi_disable_all(vsi);
7336 
7337 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7338 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7339 		if (i40e_enabled_xdp_vsi(vsi)) {
7340 			/* Make sure that in-progress ndo_xdp_xmit and
7341 			 * ndo_xsk_wakeup calls are completed.
7342 			 */
7343 			synchronize_rcu();
7344 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7345 		}
7346 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7347 	}
7348 
7349 }
7350 
7351 /**
7352  * i40e_validate_mqprio_qopt- validate queue mapping info
7353  * @vsi: the VSI being configured
7354  * @mqprio_qopt: queue parametrs
7355  **/
7356 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7357 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7358 {
7359 	u64 sum_max_rate = 0;
7360 	u64 max_rate = 0;
7361 	int i;
7362 
7363 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7364 	    mqprio_qopt->qopt.num_tc < 1 ||
7365 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7366 		return -EINVAL;
7367 	for (i = 0; ; i++) {
7368 		if (!mqprio_qopt->qopt.count[i])
7369 			return -EINVAL;
7370 		if (mqprio_qopt->min_rate[i]) {
7371 			dev_err(&vsi->back->pdev->dev,
7372 				"Invalid min tx rate (greater than 0) specified\n");
7373 			return -EINVAL;
7374 		}
7375 		max_rate = mqprio_qopt->max_rate[i];
7376 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7377 		sum_max_rate += max_rate;
7378 
7379 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7380 			break;
7381 		if (mqprio_qopt->qopt.offset[i + 1] !=
7382 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7383 			return -EINVAL;
7384 	}
7385 	if (vsi->num_queue_pairs <
7386 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7387 		dev_err(&vsi->back->pdev->dev,
7388 			"Failed to create traffic channel, insufficient number of queues.\n");
7389 		return -EINVAL;
7390 	}
7391 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7392 		dev_err(&vsi->back->pdev->dev,
7393 			"Invalid max tx rate specified\n");
7394 		return -EINVAL;
7395 	}
7396 	return 0;
7397 }
7398 
7399 /**
7400  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7401  * @vsi: the VSI being configured
7402  **/
7403 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7404 {
7405 	u16 qcount;
7406 	int i;
7407 
7408 	/* Only TC0 is enabled */
7409 	vsi->tc_config.numtc = 1;
7410 	vsi->tc_config.enabled_tc = 1;
7411 	qcount = min_t(int, vsi->alloc_queue_pairs,
7412 		       i40e_pf_get_max_q_per_tc(vsi->back));
7413 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7414 		/* For the TC that is not enabled set the offset to default
7415 		 * queue and allocate one queue for the given TC.
7416 		 */
7417 		vsi->tc_config.tc_info[i].qoffset = 0;
7418 		if (i == 0)
7419 			vsi->tc_config.tc_info[i].qcount = qcount;
7420 		else
7421 			vsi->tc_config.tc_info[i].qcount = 1;
7422 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7423 	}
7424 }
7425 
7426 /**
7427  * i40e_del_macvlan_filter
7428  * @hw: pointer to the HW structure
7429  * @seid: seid of the channel VSI
7430  * @macaddr: the mac address to apply as a filter
7431  * @aq_err: store the admin Q error
7432  *
7433  * This function deletes a mac filter on the channel VSI which serves as the
7434  * macvlan. Returns 0 on success.
7435  **/
7436 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7437 					   const u8 *macaddr, int *aq_err)
7438 {
7439 	struct i40e_aqc_remove_macvlan_element_data element;
7440 	i40e_status status;
7441 
7442 	memset(&element, 0, sizeof(element));
7443 	ether_addr_copy(element.mac_addr, macaddr);
7444 	element.vlan_tag = 0;
7445 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7446 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7447 	*aq_err = hw->aq.asq_last_status;
7448 
7449 	return status;
7450 }
7451 
7452 /**
7453  * i40e_add_macvlan_filter
7454  * @hw: pointer to the HW structure
7455  * @seid: seid of the channel VSI
7456  * @macaddr: the mac address to apply as a filter
7457  * @aq_err: store the admin Q error
7458  *
7459  * This function adds a mac filter on the channel VSI which serves as the
7460  * macvlan. Returns 0 on success.
7461  **/
7462 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7463 					   const u8 *macaddr, int *aq_err)
7464 {
7465 	struct i40e_aqc_add_macvlan_element_data element;
7466 	i40e_status status;
7467 	u16 cmd_flags = 0;
7468 
7469 	ether_addr_copy(element.mac_addr, macaddr);
7470 	element.vlan_tag = 0;
7471 	element.queue_number = 0;
7472 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7473 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7474 	element.flags = cpu_to_le16(cmd_flags);
7475 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7476 	*aq_err = hw->aq.asq_last_status;
7477 
7478 	return status;
7479 }
7480 
7481 /**
7482  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7483  * @vsi: the VSI we want to access
7484  * @ch: the channel we want to access
7485  */
7486 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7487 {
7488 	struct i40e_ring *tx_ring, *rx_ring;
7489 	u16 pf_q;
7490 	int i;
7491 
7492 	for (i = 0; i < ch->num_queue_pairs; i++) {
7493 		pf_q = ch->base_queue + i;
7494 		tx_ring = vsi->tx_rings[pf_q];
7495 		tx_ring->ch = NULL;
7496 		rx_ring = vsi->rx_rings[pf_q];
7497 		rx_ring->ch = NULL;
7498 	}
7499 }
7500 
7501 /**
7502  * i40e_free_macvlan_channels
7503  * @vsi: the VSI we want to access
7504  *
7505  * This function frees the Qs of the channel VSI from
7506  * the stack and also deletes the channel VSIs which
7507  * serve as macvlans.
7508  */
7509 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7510 {
7511 	struct i40e_channel *ch, *ch_tmp;
7512 	int ret;
7513 
7514 	if (list_empty(&vsi->macvlan_list))
7515 		return;
7516 
7517 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7518 		struct i40e_vsi *parent_vsi;
7519 
7520 		if (i40e_is_channel_macvlan(ch)) {
7521 			i40e_reset_ch_rings(vsi, ch);
7522 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7523 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7524 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7525 			kfree(ch->fwd);
7526 			ch->fwd = NULL;
7527 		}
7528 
7529 		list_del(&ch->list);
7530 		parent_vsi = ch->parent_vsi;
7531 		if (!parent_vsi || !ch->initialized) {
7532 			kfree(ch);
7533 			continue;
7534 		}
7535 
7536 		/* remove the VSI */
7537 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7538 					     NULL);
7539 		if (ret)
7540 			dev_err(&vsi->back->pdev->dev,
7541 				"unable to remove channel (%d) for parent VSI(%d)\n",
7542 				ch->seid, parent_vsi->seid);
7543 		kfree(ch);
7544 	}
7545 	vsi->macvlan_cnt = 0;
7546 }
7547 
7548 /**
7549  * i40e_fwd_ring_up - bring the macvlan device up
7550  * @vsi: the VSI we want to access
7551  * @vdev: macvlan netdevice
7552  * @fwd: the private fwd structure
7553  */
7554 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7555 			    struct i40e_fwd_adapter *fwd)
7556 {
7557 	int ret = 0, num_tc = 1,  i, aq_err;
7558 	struct i40e_channel *ch, *ch_tmp;
7559 	struct i40e_pf *pf = vsi->back;
7560 	struct i40e_hw *hw = &pf->hw;
7561 
7562 	if (list_empty(&vsi->macvlan_list))
7563 		return -EINVAL;
7564 
7565 	/* Go through the list and find an available channel */
7566 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7567 		if (!i40e_is_channel_macvlan(ch)) {
7568 			ch->fwd = fwd;
7569 			/* record configuration for macvlan interface in vdev */
7570 			for (i = 0; i < num_tc; i++)
7571 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7572 							     i,
7573 							     ch->num_queue_pairs,
7574 							     ch->base_queue);
7575 			for (i = 0; i < ch->num_queue_pairs; i++) {
7576 				struct i40e_ring *tx_ring, *rx_ring;
7577 				u16 pf_q;
7578 
7579 				pf_q = ch->base_queue + i;
7580 
7581 				/* Get to TX ring ptr */
7582 				tx_ring = vsi->tx_rings[pf_q];
7583 				tx_ring->ch = ch;
7584 
7585 				/* Get the RX ring ptr */
7586 				rx_ring = vsi->rx_rings[pf_q];
7587 				rx_ring->ch = ch;
7588 			}
7589 			break;
7590 		}
7591 	}
7592 
7593 	/* Guarantee all rings are updated before we update the
7594 	 * MAC address filter.
7595 	 */
7596 	wmb();
7597 
7598 	/* Add a mac filter */
7599 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7600 	if (ret) {
7601 		/* if we cannot add the MAC rule then disable the offload */
7602 		macvlan_release_l2fw_offload(vdev);
7603 		for (i = 0; i < ch->num_queue_pairs; i++) {
7604 			struct i40e_ring *rx_ring;
7605 			u16 pf_q;
7606 
7607 			pf_q = ch->base_queue + i;
7608 			rx_ring = vsi->rx_rings[pf_q];
7609 			rx_ring->netdev = NULL;
7610 		}
7611 		dev_info(&pf->pdev->dev,
7612 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7613 			  i40e_stat_str(hw, ret),
7614 			  i40e_aq_str(hw, aq_err));
7615 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7616 	}
7617 
7618 	return ret;
7619 }
7620 
7621 /**
7622  * i40e_setup_macvlans - create the channels which will be macvlans
7623  * @vsi: the VSI we want to access
7624  * @macvlan_cnt: no. of macvlans to be setup
7625  * @qcnt: no. of Qs per macvlan
7626  * @vdev: macvlan netdevice
7627  */
7628 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7629 			       struct net_device *vdev)
7630 {
7631 	struct i40e_pf *pf = vsi->back;
7632 	struct i40e_hw *hw = &pf->hw;
7633 	struct i40e_vsi_context ctxt;
7634 	u16 sections, qmap, num_qps;
7635 	struct i40e_channel *ch;
7636 	int i, pow, ret = 0;
7637 	u8 offset = 0;
7638 
7639 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7640 		return -EINVAL;
7641 
7642 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7643 
7644 	/* find the next higher power-of-2 of num queue pairs */
7645 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7646 
7647 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7648 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7649 
7650 	/* Setup context bits for the main VSI */
7651 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7652 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7653 	memset(&ctxt, 0, sizeof(ctxt));
7654 	ctxt.seid = vsi->seid;
7655 	ctxt.pf_num = vsi->back->hw.pf_id;
7656 	ctxt.vf_num = 0;
7657 	ctxt.uplink_seid = vsi->uplink_seid;
7658 	ctxt.info = vsi->info;
7659 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7660 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7661 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7662 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7663 
7664 	/* Reconfigure RSS for main VSI with new max queue count */
7665 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7666 	ret = i40e_vsi_config_rss(vsi);
7667 	if (ret) {
7668 		dev_info(&pf->pdev->dev,
7669 			 "Failed to reconfig RSS for num_queues (%u)\n",
7670 			 vsi->rss_size);
7671 		return ret;
7672 	}
7673 	vsi->reconfig_rss = true;
7674 	dev_dbg(&vsi->back->pdev->dev,
7675 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7676 	vsi->next_base_queue = num_qps;
7677 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7678 
7679 	/* Update the VSI after updating the VSI queue-mapping
7680 	 * information
7681 	 */
7682 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7683 	if (ret) {
7684 		dev_info(&pf->pdev->dev,
7685 			 "Update vsi tc config failed, err %s aq_err %s\n",
7686 			 i40e_stat_str(hw, ret),
7687 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7688 		return ret;
7689 	}
7690 	/* update the local VSI info with updated queue map */
7691 	i40e_vsi_update_queue_map(vsi, &ctxt);
7692 	vsi->info.valid_sections = 0;
7693 
7694 	/* Create channels for macvlans */
7695 	INIT_LIST_HEAD(&vsi->macvlan_list);
7696 	for (i = 0; i < macvlan_cnt; i++) {
7697 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7698 		if (!ch) {
7699 			ret = -ENOMEM;
7700 			goto err_free;
7701 		}
7702 		INIT_LIST_HEAD(&ch->list);
7703 		ch->num_queue_pairs = qcnt;
7704 		if (!i40e_setup_channel(pf, vsi, ch)) {
7705 			ret = -EINVAL;
7706 			kfree(ch);
7707 			goto err_free;
7708 		}
7709 		ch->parent_vsi = vsi;
7710 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7711 		vsi->macvlan_cnt++;
7712 		list_add_tail(&ch->list, &vsi->macvlan_list);
7713 	}
7714 
7715 	return ret;
7716 
7717 err_free:
7718 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7719 	i40e_free_macvlan_channels(vsi);
7720 
7721 	return ret;
7722 }
7723 
7724 /**
7725  * i40e_fwd_add - configure macvlans
7726  * @netdev: net device to configure
7727  * @vdev: macvlan netdevice
7728  **/
7729 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7730 {
7731 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7732 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7733 	struct i40e_vsi *vsi = np->vsi;
7734 	struct i40e_pf *pf = vsi->back;
7735 	struct i40e_fwd_adapter *fwd;
7736 	int avail_macvlan, ret;
7737 
7738 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7739 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7740 		return ERR_PTR(-EINVAL);
7741 	}
7742 	if ((pf->flags & I40E_FLAG_TC_MQPRIO)) {
7743 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7744 		return ERR_PTR(-EINVAL);
7745 	}
7746 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7747 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7748 		return ERR_PTR(-EINVAL);
7749 	}
7750 
7751 	/* The macvlan device has to be a single Q device so that the
7752 	 * tc_to_txq field can be reused to pick the tx queue.
7753 	 */
7754 	if (netif_is_multiqueue(vdev))
7755 		return ERR_PTR(-ERANGE);
7756 
7757 	if (!vsi->macvlan_cnt) {
7758 		/* reserve bit 0 for the pf device */
7759 		set_bit(0, vsi->fwd_bitmask);
7760 
7761 		/* Try to reserve as many queues as possible for macvlans. First
7762 		 * reserve 3/4th of max vectors, then half, then quarter and
7763 		 * calculate Qs per macvlan as you go
7764 		 */
7765 		vectors = pf->num_lan_msix;
7766 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7767 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7768 			q_per_macvlan = 4;
7769 			macvlan_cnt = (vectors - 32) / 4;
7770 		} else if (vectors <= 64 && vectors > 32) {
7771 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7772 			q_per_macvlan = 2;
7773 			macvlan_cnt = (vectors - 16) / 2;
7774 		} else if (vectors <= 32 && vectors > 16) {
7775 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7776 			q_per_macvlan = 1;
7777 			macvlan_cnt = vectors - 16;
7778 		} else if (vectors <= 16 && vectors > 8) {
7779 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7780 			q_per_macvlan = 1;
7781 			macvlan_cnt = vectors - 8;
7782 		} else {
7783 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7784 			q_per_macvlan = 1;
7785 			macvlan_cnt = vectors - 1;
7786 		}
7787 
7788 		if (macvlan_cnt == 0)
7789 			return ERR_PTR(-EBUSY);
7790 
7791 		/* Quiesce VSI queues */
7792 		i40e_quiesce_vsi(vsi);
7793 
7794 		/* sets up the macvlans but does not "enable" them */
7795 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
7796 					  vdev);
7797 		if (ret)
7798 			return ERR_PTR(ret);
7799 
7800 		/* Unquiesce VSI */
7801 		i40e_unquiesce_vsi(vsi);
7802 	}
7803 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
7804 					    vsi->macvlan_cnt);
7805 	if (avail_macvlan >= I40E_MAX_MACVLANS)
7806 		return ERR_PTR(-EBUSY);
7807 
7808 	/* create the fwd struct */
7809 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
7810 	if (!fwd)
7811 		return ERR_PTR(-ENOMEM);
7812 
7813 	set_bit(avail_macvlan, vsi->fwd_bitmask);
7814 	fwd->bit_no = avail_macvlan;
7815 	netdev_set_sb_channel(vdev, avail_macvlan);
7816 	fwd->netdev = vdev;
7817 
7818 	if (!netif_running(netdev))
7819 		return fwd;
7820 
7821 	/* Set fwd ring up */
7822 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
7823 	if (ret) {
7824 		/* unbind the queues and drop the subordinate channel config */
7825 		netdev_unbind_sb_channel(netdev, vdev);
7826 		netdev_set_sb_channel(vdev, 0);
7827 
7828 		kfree(fwd);
7829 		return ERR_PTR(-EINVAL);
7830 	}
7831 
7832 	return fwd;
7833 }
7834 
7835 /**
7836  * i40e_del_all_macvlans - Delete all the mac filters on the channels
7837  * @vsi: the VSI we want to access
7838  */
7839 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
7840 {
7841 	struct i40e_channel *ch, *ch_tmp;
7842 	struct i40e_pf *pf = vsi->back;
7843 	struct i40e_hw *hw = &pf->hw;
7844 	int aq_err, ret = 0;
7845 
7846 	if (list_empty(&vsi->macvlan_list))
7847 		return;
7848 
7849 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7850 		if (i40e_is_channel_macvlan(ch)) {
7851 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7852 						      i40e_channel_mac(ch),
7853 						      &aq_err);
7854 			if (!ret) {
7855 				/* Reset queue contexts */
7856 				i40e_reset_ch_rings(vsi, ch);
7857 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7858 				netdev_unbind_sb_channel(vsi->netdev,
7859 							 ch->fwd->netdev);
7860 				netdev_set_sb_channel(ch->fwd->netdev, 0);
7861 				kfree(ch->fwd);
7862 				ch->fwd = NULL;
7863 			}
7864 		}
7865 	}
7866 }
7867 
7868 /**
7869  * i40e_fwd_del - delete macvlan interfaces
7870  * @netdev: net device to configure
7871  * @vdev: macvlan netdevice
7872  */
7873 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
7874 {
7875 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7876 	struct i40e_fwd_adapter *fwd = vdev;
7877 	struct i40e_channel *ch, *ch_tmp;
7878 	struct i40e_vsi *vsi = np->vsi;
7879 	struct i40e_pf *pf = vsi->back;
7880 	struct i40e_hw *hw = &pf->hw;
7881 	int aq_err, ret = 0;
7882 
7883 	/* Find the channel associated with the macvlan and del mac filter */
7884 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7885 		if (i40e_is_channel_macvlan(ch) &&
7886 		    ether_addr_equal(i40e_channel_mac(ch),
7887 				     fwd->netdev->dev_addr)) {
7888 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7889 						      i40e_channel_mac(ch),
7890 						      &aq_err);
7891 			if (!ret) {
7892 				/* Reset queue contexts */
7893 				i40e_reset_ch_rings(vsi, ch);
7894 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7895 				netdev_unbind_sb_channel(netdev, fwd->netdev);
7896 				netdev_set_sb_channel(fwd->netdev, 0);
7897 				kfree(ch->fwd);
7898 				ch->fwd = NULL;
7899 			} else {
7900 				dev_info(&pf->pdev->dev,
7901 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
7902 					  i40e_stat_str(hw, ret),
7903 					  i40e_aq_str(hw, aq_err));
7904 			}
7905 			break;
7906 		}
7907 	}
7908 }
7909 
7910 /**
7911  * i40e_setup_tc - configure multiple traffic classes
7912  * @netdev: net device to configure
7913  * @type_data: tc offload data
7914  **/
7915 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
7916 {
7917 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
7918 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7919 	struct i40e_vsi *vsi = np->vsi;
7920 	struct i40e_pf *pf = vsi->back;
7921 	u8 enabled_tc = 0, num_tc, hw;
7922 	bool need_reset = false;
7923 	int old_queue_pairs;
7924 	int ret = -EINVAL;
7925 	u16 mode;
7926 	int i;
7927 
7928 	old_queue_pairs = vsi->num_queue_pairs;
7929 	num_tc = mqprio_qopt->qopt.num_tc;
7930 	hw = mqprio_qopt->qopt.hw;
7931 	mode = mqprio_qopt->mode;
7932 	if (!hw) {
7933 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7934 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
7935 		goto config_tc;
7936 	}
7937 
7938 	/* Check if MFP enabled */
7939 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
7940 		netdev_info(netdev,
7941 			    "Configuring TC not supported in MFP mode\n");
7942 		return ret;
7943 	}
7944 	switch (mode) {
7945 	case TC_MQPRIO_MODE_DCB:
7946 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7947 
7948 		/* Check if DCB enabled to continue */
7949 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7950 			netdev_info(netdev,
7951 				    "DCB is not enabled for adapter\n");
7952 			return ret;
7953 		}
7954 
7955 		/* Check whether tc count is within enabled limit */
7956 		if (num_tc > i40e_pf_get_num_tc(pf)) {
7957 			netdev_info(netdev,
7958 				    "TC count greater than enabled on link for adapter\n");
7959 			return ret;
7960 		}
7961 		break;
7962 	case TC_MQPRIO_MODE_CHANNEL:
7963 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
7964 			netdev_info(netdev,
7965 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
7966 			return ret;
7967 		}
7968 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7969 			return ret;
7970 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
7971 		if (ret)
7972 			return ret;
7973 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
7974 		       sizeof(*mqprio_qopt));
7975 		pf->flags |= I40E_FLAG_TC_MQPRIO;
7976 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7977 		break;
7978 	default:
7979 		return -EINVAL;
7980 	}
7981 
7982 config_tc:
7983 	/* Generate TC map for number of tc requested */
7984 	for (i = 0; i < num_tc; i++)
7985 		enabled_tc |= BIT(i);
7986 
7987 	/* Requesting same TC configuration as already enabled */
7988 	if (enabled_tc == vsi->tc_config.enabled_tc &&
7989 	    mode != TC_MQPRIO_MODE_CHANNEL)
7990 		return 0;
7991 
7992 	/* Quiesce VSI queues */
7993 	i40e_quiesce_vsi(vsi);
7994 
7995 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
7996 		i40e_remove_queue_channels(vsi);
7997 
7998 	/* Configure VSI for enabled TCs */
7999 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8000 	if (ret) {
8001 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8002 			    vsi->seid);
8003 		need_reset = true;
8004 		goto exit;
8005 	} else if (enabled_tc &&
8006 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8007 		netdev_info(netdev,
8008 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8009 			    vsi->tc_config.tc_info[0].qcount);
8010 		ret = -EINVAL;
8011 		need_reset = true;
8012 		goto exit;
8013 	}
8014 
8015 	dev_info(&vsi->back->pdev->dev,
8016 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8017 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8018 
8019 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
8020 		if (vsi->mqprio_qopt.max_rate[0]) {
8021 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
8022 
8023 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
8024 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8025 			if (!ret) {
8026 				u64 credits = max_tx_rate;
8027 
8028 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8029 				dev_dbg(&vsi->back->pdev->dev,
8030 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8031 					max_tx_rate,
8032 					credits,
8033 					vsi->seid);
8034 			} else {
8035 				need_reset = true;
8036 				goto exit;
8037 			}
8038 		}
8039 		ret = i40e_configure_queue_channels(vsi);
8040 		if (ret) {
8041 			vsi->num_queue_pairs = old_queue_pairs;
8042 			netdev_info(netdev,
8043 				    "Failed configuring queue channels\n");
8044 			need_reset = true;
8045 			goto exit;
8046 		}
8047 	}
8048 
8049 exit:
8050 	/* Reset the configuration data to defaults, only TC0 is enabled */
8051 	if (need_reset) {
8052 		i40e_vsi_set_default_tc_config(vsi);
8053 		need_reset = false;
8054 	}
8055 
8056 	/* Unquiesce VSI */
8057 	i40e_unquiesce_vsi(vsi);
8058 	return ret;
8059 }
8060 
8061 /**
8062  * i40e_set_cld_element - sets cloud filter element data
8063  * @filter: cloud filter rule
8064  * @cld: ptr to cloud filter element data
8065  *
8066  * This is helper function to copy data into cloud filter element
8067  **/
8068 static inline void
8069 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8070 		     struct i40e_aqc_cloud_filters_element_data *cld)
8071 {
8072 	u32 ipa;
8073 	int i;
8074 
8075 	memset(cld, 0, sizeof(*cld));
8076 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8077 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8078 
8079 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8080 		return;
8081 
8082 	if (filter->n_proto == ETH_P_IPV6) {
8083 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8084 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8085 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8086 
8087 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8088 		}
8089 	} else {
8090 		ipa = be32_to_cpu(filter->dst_ipv4);
8091 
8092 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8093 	}
8094 
8095 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8096 
8097 	/* tenant_id is not supported by FW now, once the support is enabled
8098 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8099 	 */
8100 	if (filter->tenant_id)
8101 		return;
8102 }
8103 
8104 /**
8105  * i40e_add_del_cloud_filter - Add/del cloud filter
8106  * @vsi: pointer to VSI
8107  * @filter: cloud filter rule
8108  * @add: if true, add, if false, delete
8109  *
8110  * Add or delete a cloud filter for a specific flow spec.
8111  * Returns 0 if the filter were successfully added.
8112  **/
8113 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8114 			      struct i40e_cloud_filter *filter, bool add)
8115 {
8116 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8117 	struct i40e_pf *pf = vsi->back;
8118 	int ret;
8119 	static const u16 flag_table[128] = {
8120 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8121 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8122 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8123 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8124 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8125 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8126 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8127 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8128 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8129 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8130 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8131 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8132 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8133 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8134 	};
8135 
8136 	if (filter->flags >= ARRAY_SIZE(flag_table))
8137 		return I40E_ERR_CONFIG;
8138 
8139 	memset(&cld_filter, 0, sizeof(cld_filter));
8140 
8141 	/* copy element needed to add cloud filter from filter */
8142 	i40e_set_cld_element(filter, &cld_filter);
8143 
8144 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8145 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8146 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8147 
8148 	if (filter->n_proto == ETH_P_IPV6)
8149 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8150 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8151 	else
8152 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8153 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8154 
8155 	if (add)
8156 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8157 						&cld_filter, 1);
8158 	else
8159 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8160 						&cld_filter, 1);
8161 	if (ret)
8162 		dev_dbg(&pf->pdev->dev,
8163 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8164 			add ? "add" : "delete", filter->dst_port, ret,
8165 			pf->hw.aq.asq_last_status);
8166 	else
8167 		dev_info(&pf->pdev->dev,
8168 			 "%s cloud filter for VSI: %d\n",
8169 			 add ? "Added" : "Deleted", filter->seid);
8170 	return ret;
8171 }
8172 
8173 /**
8174  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8175  * @vsi: pointer to VSI
8176  * @filter: cloud filter rule
8177  * @add: if true, add, if false, delete
8178  *
8179  * Add or delete a cloud filter for a specific flow spec using big buffer.
8180  * Returns 0 if the filter were successfully added.
8181  **/
8182 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8183 				      struct i40e_cloud_filter *filter,
8184 				      bool add)
8185 {
8186 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8187 	struct i40e_pf *pf = vsi->back;
8188 	int ret;
8189 
8190 	/* Both (src/dst) valid mac_addr are not supported */
8191 	if ((is_valid_ether_addr(filter->dst_mac) &&
8192 	     is_valid_ether_addr(filter->src_mac)) ||
8193 	    (is_multicast_ether_addr(filter->dst_mac) &&
8194 	     is_multicast_ether_addr(filter->src_mac)))
8195 		return -EOPNOTSUPP;
8196 
8197 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8198 	 * ports are not supported via big buffer now.
8199 	 */
8200 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8201 		return -EOPNOTSUPP;
8202 
8203 	/* adding filter using src_port/src_ip is not supported at this stage */
8204 	if (filter->src_port ||
8205 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8206 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8207 		return -EOPNOTSUPP;
8208 
8209 	memset(&cld_filter, 0, sizeof(cld_filter));
8210 
8211 	/* copy element needed to add cloud filter from filter */
8212 	i40e_set_cld_element(filter, &cld_filter.element);
8213 
8214 	if (is_valid_ether_addr(filter->dst_mac) ||
8215 	    is_valid_ether_addr(filter->src_mac) ||
8216 	    is_multicast_ether_addr(filter->dst_mac) ||
8217 	    is_multicast_ether_addr(filter->src_mac)) {
8218 		/* MAC + IP : unsupported mode */
8219 		if (filter->dst_ipv4)
8220 			return -EOPNOTSUPP;
8221 
8222 		/* since we validated that L4 port must be valid before
8223 		 * we get here, start with respective "flags" value
8224 		 * and update if vlan is present or not
8225 		 */
8226 		cld_filter.element.flags =
8227 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8228 
8229 		if (filter->vlan_id) {
8230 			cld_filter.element.flags =
8231 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8232 		}
8233 
8234 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8235 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8236 		cld_filter.element.flags =
8237 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8238 		if (filter->n_proto == ETH_P_IPV6)
8239 			cld_filter.element.flags |=
8240 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8241 		else
8242 			cld_filter.element.flags |=
8243 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8244 	} else {
8245 		dev_err(&pf->pdev->dev,
8246 			"either mac or ip has to be valid for cloud filter\n");
8247 		return -EINVAL;
8248 	}
8249 
8250 	/* Now copy L4 port in Byte 6..7 in general fields */
8251 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8252 						be16_to_cpu(filter->dst_port);
8253 
8254 	if (add) {
8255 		/* Validate current device switch mode, change if necessary */
8256 		ret = i40e_validate_and_set_switch_mode(vsi);
8257 		if (ret) {
8258 			dev_err(&pf->pdev->dev,
8259 				"failed to set switch mode, ret %d\n",
8260 				ret);
8261 			return ret;
8262 		}
8263 
8264 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8265 						   &cld_filter, 1);
8266 	} else {
8267 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8268 						   &cld_filter, 1);
8269 	}
8270 
8271 	if (ret)
8272 		dev_dbg(&pf->pdev->dev,
8273 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8274 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8275 	else
8276 		dev_info(&pf->pdev->dev,
8277 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8278 			 add ? "add" : "delete", filter->seid,
8279 			 ntohs(filter->dst_port));
8280 	return ret;
8281 }
8282 
8283 /**
8284  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8285  * @vsi: Pointer to VSI
8286  * @f: Pointer to struct flow_cls_offload
8287  * @filter: Pointer to cloud filter structure
8288  *
8289  **/
8290 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8291 				 struct flow_cls_offload *f,
8292 				 struct i40e_cloud_filter *filter)
8293 {
8294 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8295 	struct flow_dissector *dissector = rule->match.dissector;
8296 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8297 	struct i40e_pf *pf = vsi->back;
8298 	u8 field_flags = 0;
8299 
8300 	if (dissector->used_keys &
8301 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8302 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8303 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8304 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8305 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8306 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8307 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8308 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8309 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8310 			dissector->used_keys);
8311 		return -EOPNOTSUPP;
8312 	}
8313 
8314 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8315 		struct flow_match_enc_keyid match;
8316 
8317 		flow_rule_match_enc_keyid(rule, &match);
8318 		if (match.mask->keyid != 0)
8319 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8320 
8321 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8322 	}
8323 
8324 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8325 		struct flow_match_basic match;
8326 
8327 		flow_rule_match_basic(rule, &match);
8328 		n_proto_key = ntohs(match.key->n_proto);
8329 		n_proto_mask = ntohs(match.mask->n_proto);
8330 
8331 		if (n_proto_key == ETH_P_ALL) {
8332 			n_proto_key = 0;
8333 			n_proto_mask = 0;
8334 		}
8335 		filter->n_proto = n_proto_key & n_proto_mask;
8336 		filter->ip_proto = match.key->ip_proto;
8337 	}
8338 
8339 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8340 		struct flow_match_eth_addrs match;
8341 
8342 		flow_rule_match_eth_addrs(rule, &match);
8343 
8344 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8345 		if (!is_zero_ether_addr(match.mask->dst)) {
8346 			if (is_broadcast_ether_addr(match.mask->dst)) {
8347 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8348 			} else {
8349 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8350 					match.mask->dst);
8351 				return I40E_ERR_CONFIG;
8352 			}
8353 		}
8354 
8355 		if (!is_zero_ether_addr(match.mask->src)) {
8356 			if (is_broadcast_ether_addr(match.mask->src)) {
8357 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8358 			} else {
8359 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8360 					match.mask->src);
8361 				return I40E_ERR_CONFIG;
8362 			}
8363 		}
8364 		ether_addr_copy(filter->dst_mac, match.key->dst);
8365 		ether_addr_copy(filter->src_mac, match.key->src);
8366 	}
8367 
8368 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8369 		struct flow_match_vlan match;
8370 
8371 		flow_rule_match_vlan(rule, &match);
8372 		if (match.mask->vlan_id) {
8373 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8374 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8375 
8376 			} else {
8377 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8378 					match.mask->vlan_id);
8379 				return I40E_ERR_CONFIG;
8380 			}
8381 		}
8382 
8383 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8384 	}
8385 
8386 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8387 		struct flow_match_control match;
8388 
8389 		flow_rule_match_control(rule, &match);
8390 		addr_type = match.key->addr_type;
8391 	}
8392 
8393 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8394 		struct flow_match_ipv4_addrs match;
8395 
8396 		flow_rule_match_ipv4_addrs(rule, &match);
8397 		if (match.mask->dst) {
8398 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8399 				field_flags |= I40E_CLOUD_FIELD_IIP;
8400 			} else {
8401 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8402 					&match.mask->dst);
8403 				return I40E_ERR_CONFIG;
8404 			}
8405 		}
8406 
8407 		if (match.mask->src) {
8408 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8409 				field_flags |= I40E_CLOUD_FIELD_IIP;
8410 			} else {
8411 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8412 					&match.mask->src);
8413 				return I40E_ERR_CONFIG;
8414 			}
8415 		}
8416 
8417 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8418 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8419 			return I40E_ERR_CONFIG;
8420 		}
8421 		filter->dst_ipv4 = match.key->dst;
8422 		filter->src_ipv4 = match.key->src;
8423 	}
8424 
8425 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8426 		struct flow_match_ipv6_addrs match;
8427 
8428 		flow_rule_match_ipv6_addrs(rule, &match);
8429 
8430 		/* src and dest IPV6 address should not be LOOPBACK
8431 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8432 		 */
8433 		if (ipv6_addr_loopback(&match.key->dst) ||
8434 		    ipv6_addr_loopback(&match.key->src)) {
8435 			dev_err(&pf->pdev->dev,
8436 				"Bad ipv6, addr is LOOPBACK\n");
8437 			return I40E_ERR_CONFIG;
8438 		}
8439 		if (!ipv6_addr_any(&match.mask->dst) ||
8440 		    !ipv6_addr_any(&match.mask->src))
8441 			field_flags |= I40E_CLOUD_FIELD_IIP;
8442 
8443 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8444 		       sizeof(filter->src_ipv6));
8445 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8446 		       sizeof(filter->dst_ipv6));
8447 	}
8448 
8449 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8450 		struct flow_match_ports match;
8451 
8452 		flow_rule_match_ports(rule, &match);
8453 		if (match.mask->src) {
8454 			if (match.mask->src == cpu_to_be16(0xffff)) {
8455 				field_flags |= I40E_CLOUD_FIELD_IIP;
8456 			} else {
8457 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8458 					be16_to_cpu(match.mask->src));
8459 				return I40E_ERR_CONFIG;
8460 			}
8461 		}
8462 
8463 		if (match.mask->dst) {
8464 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8465 				field_flags |= I40E_CLOUD_FIELD_IIP;
8466 			} else {
8467 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8468 					be16_to_cpu(match.mask->dst));
8469 				return I40E_ERR_CONFIG;
8470 			}
8471 		}
8472 
8473 		filter->dst_port = match.key->dst;
8474 		filter->src_port = match.key->src;
8475 
8476 		switch (filter->ip_proto) {
8477 		case IPPROTO_TCP:
8478 		case IPPROTO_UDP:
8479 			break;
8480 		default:
8481 			dev_err(&pf->pdev->dev,
8482 				"Only UDP and TCP transport are supported\n");
8483 			return -EINVAL;
8484 		}
8485 	}
8486 	filter->flags = field_flags;
8487 	return 0;
8488 }
8489 
8490 /**
8491  * i40e_handle_tclass: Forward to a traffic class on the device
8492  * @vsi: Pointer to VSI
8493  * @tc: traffic class index on the device
8494  * @filter: Pointer to cloud filter structure
8495  *
8496  **/
8497 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8498 			      struct i40e_cloud_filter *filter)
8499 {
8500 	struct i40e_channel *ch, *ch_tmp;
8501 
8502 	/* direct to a traffic class on the same device */
8503 	if (tc == 0) {
8504 		filter->seid = vsi->seid;
8505 		return 0;
8506 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8507 		if (!filter->dst_port) {
8508 			dev_err(&vsi->back->pdev->dev,
8509 				"Specify destination port to direct to traffic class that is not default\n");
8510 			return -EINVAL;
8511 		}
8512 		if (list_empty(&vsi->ch_list))
8513 			return -EINVAL;
8514 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8515 					 list) {
8516 			if (ch->seid == vsi->tc_seid_map[tc])
8517 				filter->seid = ch->seid;
8518 		}
8519 		return 0;
8520 	}
8521 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8522 	return -EINVAL;
8523 }
8524 
8525 /**
8526  * i40e_configure_clsflower - Configure tc flower filters
8527  * @vsi: Pointer to VSI
8528  * @cls_flower: Pointer to struct flow_cls_offload
8529  *
8530  **/
8531 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8532 				    struct flow_cls_offload *cls_flower)
8533 {
8534 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8535 	struct i40e_cloud_filter *filter = NULL;
8536 	struct i40e_pf *pf = vsi->back;
8537 	int err = 0;
8538 
8539 	if (tc < 0) {
8540 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8541 		return -EOPNOTSUPP;
8542 	}
8543 
8544 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8545 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8546 		return -EBUSY;
8547 
8548 	if (pf->fdir_pf_active_filters ||
8549 	    (!hlist_empty(&pf->fdir_filter_list))) {
8550 		dev_err(&vsi->back->pdev->dev,
8551 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8552 		return -EINVAL;
8553 	}
8554 
8555 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8556 		dev_err(&vsi->back->pdev->dev,
8557 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8558 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8559 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8560 	}
8561 
8562 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8563 	if (!filter)
8564 		return -ENOMEM;
8565 
8566 	filter->cookie = cls_flower->cookie;
8567 
8568 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8569 	if (err < 0)
8570 		goto err;
8571 
8572 	err = i40e_handle_tclass(vsi, tc, filter);
8573 	if (err < 0)
8574 		goto err;
8575 
8576 	/* Add cloud filter */
8577 	if (filter->dst_port)
8578 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8579 	else
8580 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8581 
8582 	if (err) {
8583 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8584 			err);
8585 		goto err;
8586 	}
8587 
8588 	/* add filter to the ordered list */
8589 	INIT_HLIST_NODE(&filter->cloud_node);
8590 
8591 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8592 
8593 	pf->num_cloud_filters++;
8594 
8595 	return err;
8596 err:
8597 	kfree(filter);
8598 	return err;
8599 }
8600 
8601 /**
8602  * i40e_find_cloud_filter - Find the could filter in the list
8603  * @vsi: Pointer to VSI
8604  * @cookie: filter specific cookie
8605  *
8606  **/
8607 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8608 							unsigned long *cookie)
8609 {
8610 	struct i40e_cloud_filter *filter = NULL;
8611 	struct hlist_node *node2;
8612 
8613 	hlist_for_each_entry_safe(filter, node2,
8614 				  &vsi->back->cloud_filter_list, cloud_node)
8615 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8616 			return filter;
8617 	return NULL;
8618 }
8619 
8620 /**
8621  * i40e_delete_clsflower - Remove tc flower filters
8622  * @vsi: Pointer to VSI
8623  * @cls_flower: Pointer to struct flow_cls_offload
8624  *
8625  **/
8626 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8627 				 struct flow_cls_offload *cls_flower)
8628 {
8629 	struct i40e_cloud_filter *filter = NULL;
8630 	struct i40e_pf *pf = vsi->back;
8631 	int err = 0;
8632 
8633 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8634 
8635 	if (!filter)
8636 		return -EINVAL;
8637 
8638 	hash_del(&filter->cloud_node);
8639 
8640 	if (filter->dst_port)
8641 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8642 	else
8643 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8644 
8645 	kfree(filter);
8646 	if (err) {
8647 		dev_err(&pf->pdev->dev,
8648 			"Failed to delete cloud filter, err %s\n",
8649 			i40e_stat_str(&pf->hw, err));
8650 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8651 	}
8652 
8653 	pf->num_cloud_filters--;
8654 	if (!pf->num_cloud_filters)
8655 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8656 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8657 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8658 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8659 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8660 		}
8661 	return 0;
8662 }
8663 
8664 /**
8665  * i40e_setup_tc_cls_flower - flower classifier offloads
8666  * @np: net device to configure
8667  * @cls_flower: offload data
8668  **/
8669 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8670 				    struct flow_cls_offload *cls_flower)
8671 {
8672 	struct i40e_vsi *vsi = np->vsi;
8673 
8674 	switch (cls_flower->command) {
8675 	case FLOW_CLS_REPLACE:
8676 		return i40e_configure_clsflower(vsi, cls_flower);
8677 	case FLOW_CLS_DESTROY:
8678 		return i40e_delete_clsflower(vsi, cls_flower);
8679 	case FLOW_CLS_STATS:
8680 		return -EOPNOTSUPP;
8681 	default:
8682 		return -EOPNOTSUPP;
8683 	}
8684 }
8685 
8686 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8687 				  void *cb_priv)
8688 {
8689 	struct i40e_netdev_priv *np = cb_priv;
8690 
8691 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8692 		return -EOPNOTSUPP;
8693 
8694 	switch (type) {
8695 	case TC_SETUP_CLSFLOWER:
8696 		return i40e_setup_tc_cls_flower(np, type_data);
8697 
8698 	default:
8699 		return -EOPNOTSUPP;
8700 	}
8701 }
8702 
8703 static LIST_HEAD(i40e_block_cb_list);
8704 
8705 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8706 			   void *type_data)
8707 {
8708 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8709 
8710 	switch (type) {
8711 	case TC_SETUP_QDISC_MQPRIO:
8712 		return i40e_setup_tc(netdev, type_data);
8713 	case TC_SETUP_BLOCK:
8714 		return flow_block_cb_setup_simple(type_data,
8715 						  &i40e_block_cb_list,
8716 						  i40e_setup_tc_block_cb,
8717 						  np, np, true);
8718 	default:
8719 		return -EOPNOTSUPP;
8720 	}
8721 }
8722 
8723 /**
8724  * i40e_open - Called when a network interface is made active
8725  * @netdev: network interface device structure
8726  *
8727  * The open entry point is called when a network interface is made
8728  * active by the system (IFF_UP).  At this point all resources needed
8729  * for transmit and receive operations are allocated, the interrupt
8730  * handler is registered with the OS, the netdev watchdog subtask is
8731  * enabled, and the stack is notified that the interface is ready.
8732  *
8733  * Returns 0 on success, negative value on failure
8734  **/
8735 int i40e_open(struct net_device *netdev)
8736 {
8737 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8738 	struct i40e_vsi *vsi = np->vsi;
8739 	struct i40e_pf *pf = vsi->back;
8740 	int err;
8741 
8742 	/* disallow open during test or if eeprom is broken */
8743 	if (test_bit(__I40E_TESTING, pf->state) ||
8744 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8745 		return -EBUSY;
8746 
8747 	netif_carrier_off(netdev);
8748 
8749 	if (i40e_force_link_state(pf, true))
8750 		return -EAGAIN;
8751 
8752 	err = i40e_vsi_open(vsi);
8753 	if (err)
8754 		return err;
8755 
8756 	/* configure global TSO hardware offload settings */
8757 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8758 						       TCP_FLAG_FIN) >> 16);
8759 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8760 						       TCP_FLAG_FIN |
8761 						       TCP_FLAG_CWR) >> 16);
8762 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8763 	udp_tunnel_get_rx_info(netdev);
8764 
8765 	return 0;
8766 }
8767 
8768 /**
8769  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
8770  * @vsi: vsi structure
8771  *
8772  * This updates netdev's number of tx/rx queues
8773  *
8774  * Returns status of setting tx/rx queues
8775  **/
8776 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
8777 {
8778 	int ret;
8779 
8780 	ret = netif_set_real_num_rx_queues(vsi->netdev,
8781 					   vsi->num_queue_pairs);
8782 	if (ret)
8783 		return ret;
8784 
8785 	return netif_set_real_num_tx_queues(vsi->netdev,
8786 					    vsi->num_queue_pairs);
8787 }
8788 
8789 /**
8790  * i40e_vsi_open -
8791  * @vsi: the VSI to open
8792  *
8793  * Finish initialization of the VSI.
8794  *
8795  * Returns 0 on success, negative value on failure
8796  *
8797  * Note: expects to be called while under rtnl_lock()
8798  **/
8799 int i40e_vsi_open(struct i40e_vsi *vsi)
8800 {
8801 	struct i40e_pf *pf = vsi->back;
8802 	char int_name[I40E_INT_NAME_STR_LEN];
8803 	int err;
8804 
8805 	/* allocate descriptors */
8806 	err = i40e_vsi_setup_tx_resources(vsi);
8807 	if (err)
8808 		goto err_setup_tx;
8809 	err = i40e_vsi_setup_rx_resources(vsi);
8810 	if (err)
8811 		goto err_setup_rx;
8812 
8813 	err = i40e_vsi_configure(vsi);
8814 	if (err)
8815 		goto err_setup_rx;
8816 
8817 	if (vsi->netdev) {
8818 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
8819 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
8820 		err = i40e_vsi_request_irq(vsi, int_name);
8821 		if (err)
8822 			goto err_setup_rx;
8823 
8824 		/* Notify the stack of the actual queue counts. */
8825 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
8826 		if (err)
8827 			goto err_set_queues;
8828 
8829 	} else if (vsi->type == I40E_VSI_FDIR) {
8830 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
8831 			 dev_driver_string(&pf->pdev->dev),
8832 			 dev_name(&pf->pdev->dev));
8833 		err = i40e_vsi_request_irq(vsi, int_name);
8834 		if (err)
8835 			goto err_setup_rx;
8836 
8837 	} else {
8838 		err = -EINVAL;
8839 		goto err_setup_rx;
8840 	}
8841 
8842 	err = i40e_up_complete(vsi);
8843 	if (err)
8844 		goto err_up_complete;
8845 
8846 	return 0;
8847 
8848 err_up_complete:
8849 	i40e_down(vsi);
8850 err_set_queues:
8851 	i40e_vsi_free_irq(vsi);
8852 err_setup_rx:
8853 	i40e_vsi_free_rx_resources(vsi);
8854 err_setup_tx:
8855 	i40e_vsi_free_tx_resources(vsi);
8856 	if (vsi == pf->vsi[pf->lan_vsi])
8857 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
8858 
8859 	return err;
8860 }
8861 
8862 /**
8863  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
8864  * @pf: Pointer to PF
8865  *
8866  * This function destroys the hlist where all the Flow Director
8867  * filters were saved.
8868  **/
8869 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
8870 {
8871 	struct i40e_fdir_filter *filter;
8872 	struct i40e_flex_pit *pit_entry, *tmp;
8873 	struct hlist_node *node2;
8874 
8875 	hlist_for_each_entry_safe(filter, node2,
8876 				  &pf->fdir_filter_list, fdir_node) {
8877 		hlist_del(&filter->fdir_node);
8878 		kfree(filter);
8879 	}
8880 
8881 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
8882 		list_del(&pit_entry->list);
8883 		kfree(pit_entry);
8884 	}
8885 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
8886 
8887 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
8888 		list_del(&pit_entry->list);
8889 		kfree(pit_entry);
8890 	}
8891 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
8892 
8893 	pf->fdir_pf_active_filters = 0;
8894 	i40e_reset_fdir_filter_cnt(pf);
8895 
8896 	/* Reprogram the default input set for TCP/IPv4 */
8897 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8898 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8899 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8900 
8901 	/* Reprogram the default input set for TCP/IPv6 */
8902 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
8903 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8904 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8905 
8906 	/* Reprogram the default input set for UDP/IPv4 */
8907 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
8908 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8909 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8910 
8911 	/* Reprogram the default input set for UDP/IPv6 */
8912 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
8913 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8914 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8915 
8916 	/* Reprogram the default input set for SCTP/IPv4 */
8917 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
8918 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8919 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8920 
8921 	/* Reprogram the default input set for SCTP/IPv6 */
8922 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
8923 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8924 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8925 
8926 	/* Reprogram the default input set for Other/IPv4 */
8927 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
8928 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8929 
8930 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
8931 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8932 
8933 	/* Reprogram the default input set for Other/IPv6 */
8934 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
8935 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8936 
8937 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
8938 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8939 }
8940 
8941 /**
8942  * i40e_cloud_filter_exit - Cleans up the cloud filters
8943  * @pf: Pointer to PF
8944  *
8945  * This function destroys the hlist where all the cloud filters
8946  * were saved.
8947  **/
8948 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
8949 {
8950 	struct i40e_cloud_filter *cfilter;
8951 	struct hlist_node *node;
8952 
8953 	hlist_for_each_entry_safe(cfilter, node,
8954 				  &pf->cloud_filter_list, cloud_node) {
8955 		hlist_del(&cfilter->cloud_node);
8956 		kfree(cfilter);
8957 	}
8958 	pf->num_cloud_filters = 0;
8959 
8960 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8961 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8962 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8963 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8964 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8965 	}
8966 }
8967 
8968 /**
8969  * i40e_close - Disables a network interface
8970  * @netdev: network interface device structure
8971  *
8972  * The close entry point is called when an interface is de-activated
8973  * by the OS.  The hardware is still under the driver's control, but
8974  * this netdev interface is disabled.
8975  *
8976  * Returns 0, this is not allowed to fail
8977  **/
8978 int i40e_close(struct net_device *netdev)
8979 {
8980 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8981 	struct i40e_vsi *vsi = np->vsi;
8982 
8983 	i40e_vsi_close(vsi);
8984 
8985 	return 0;
8986 }
8987 
8988 /**
8989  * i40e_do_reset - Start a PF or Core Reset sequence
8990  * @pf: board private structure
8991  * @reset_flags: which reset is requested
8992  * @lock_acquired: indicates whether or not the lock has been acquired
8993  * before this function was called.
8994  *
8995  * The essential difference in resets is that the PF Reset
8996  * doesn't clear the packet buffers, doesn't reset the PE
8997  * firmware, and doesn't bother the other PFs on the chip.
8998  **/
8999 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9000 {
9001 	u32 val;
9002 
9003 	/* do the biggest reset indicated */
9004 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9005 
9006 		/* Request a Global Reset
9007 		 *
9008 		 * This will start the chip's countdown to the actual full
9009 		 * chip reset event, and a warning interrupt to be sent
9010 		 * to all PFs, including the requestor.  Our handler
9011 		 * for the warning interrupt will deal with the shutdown
9012 		 * and recovery of the switch setup.
9013 		 */
9014 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9015 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9016 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9017 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9018 
9019 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9020 
9021 		/* Request a Core Reset
9022 		 *
9023 		 * Same as Global Reset, except does *not* include the MAC/PHY
9024 		 */
9025 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9026 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9027 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9028 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9029 		i40e_flush(&pf->hw);
9030 
9031 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9032 
9033 		/* Request a PF Reset
9034 		 *
9035 		 * Resets only the PF-specific registers
9036 		 *
9037 		 * This goes directly to the tear-down and rebuild of
9038 		 * the switch, since we need to do all the recovery as
9039 		 * for the Core Reset.
9040 		 */
9041 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9042 		i40e_handle_reset_warning(pf, lock_acquired);
9043 
9044 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9045 		/* Request a PF Reset
9046 		 *
9047 		 * Resets PF and reinitializes PFs VSI.
9048 		 */
9049 		i40e_prep_for_reset(pf);
9050 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9051 		dev_info(&pf->pdev->dev,
9052 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9053 			 "FW LLDP is disabled\n" :
9054 			 "FW LLDP is enabled\n");
9055 
9056 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9057 		int v;
9058 
9059 		/* Find the VSI(s) that requested a re-init */
9060 		dev_info(&pf->pdev->dev,
9061 			 "VSI reinit requested\n");
9062 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9063 			struct i40e_vsi *vsi = pf->vsi[v];
9064 
9065 			if (vsi != NULL &&
9066 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9067 					       vsi->state))
9068 				i40e_vsi_reinit_locked(pf->vsi[v]);
9069 		}
9070 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9071 		int v;
9072 
9073 		/* Find the VSI(s) that needs to be brought down */
9074 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9075 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9076 			struct i40e_vsi *vsi = pf->vsi[v];
9077 
9078 			if (vsi != NULL &&
9079 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9080 					       vsi->state)) {
9081 				set_bit(__I40E_VSI_DOWN, vsi->state);
9082 				i40e_down(vsi);
9083 			}
9084 		}
9085 	} else {
9086 		dev_info(&pf->pdev->dev,
9087 			 "bad reset request 0x%08x\n", reset_flags);
9088 	}
9089 }
9090 
9091 #ifdef CONFIG_I40E_DCB
9092 /**
9093  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9094  * @pf: board private structure
9095  * @old_cfg: current DCB config
9096  * @new_cfg: new DCB config
9097  **/
9098 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9099 			    struct i40e_dcbx_config *old_cfg,
9100 			    struct i40e_dcbx_config *new_cfg)
9101 {
9102 	bool need_reconfig = false;
9103 
9104 	/* Check if ETS configuration has changed */
9105 	if (memcmp(&new_cfg->etscfg,
9106 		   &old_cfg->etscfg,
9107 		   sizeof(new_cfg->etscfg))) {
9108 		/* If Priority Table has changed reconfig is needed */
9109 		if (memcmp(&new_cfg->etscfg.prioritytable,
9110 			   &old_cfg->etscfg.prioritytable,
9111 			   sizeof(new_cfg->etscfg.prioritytable))) {
9112 			need_reconfig = true;
9113 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9114 		}
9115 
9116 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9117 			   &old_cfg->etscfg.tcbwtable,
9118 			   sizeof(new_cfg->etscfg.tcbwtable)))
9119 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9120 
9121 		if (memcmp(&new_cfg->etscfg.tsatable,
9122 			   &old_cfg->etscfg.tsatable,
9123 			   sizeof(new_cfg->etscfg.tsatable)))
9124 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9125 	}
9126 
9127 	/* Check if PFC configuration has changed */
9128 	if (memcmp(&new_cfg->pfc,
9129 		   &old_cfg->pfc,
9130 		   sizeof(new_cfg->pfc))) {
9131 		need_reconfig = true;
9132 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9133 	}
9134 
9135 	/* Check if APP Table has changed */
9136 	if (memcmp(&new_cfg->app,
9137 		   &old_cfg->app,
9138 		   sizeof(new_cfg->app))) {
9139 		need_reconfig = true;
9140 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9141 	}
9142 
9143 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9144 	return need_reconfig;
9145 }
9146 
9147 /**
9148  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9149  * @pf: board private structure
9150  * @e: event info posted on ARQ
9151  **/
9152 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9153 				  struct i40e_arq_event_info *e)
9154 {
9155 	struct i40e_aqc_lldp_get_mib *mib =
9156 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9157 	struct i40e_hw *hw = &pf->hw;
9158 	struct i40e_dcbx_config tmp_dcbx_cfg;
9159 	bool need_reconfig = false;
9160 	int ret = 0;
9161 	u8 type;
9162 
9163 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9164 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9165 	    (hw->phy.link_info.link_speed &
9166 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9167 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9168 		/* let firmware decide if the DCB should be disabled */
9169 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9170 
9171 	/* Not DCB capable or capability disabled */
9172 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9173 		return ret;
9174 
9175 	/* Ignore if event is not for Nearest Bridge */
9176 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9177 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9178 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9179 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9180 		return ret;
9181 
9182 	/* Check MIB Type and return if event for Remote MIB update */
9183 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9184 	dev_dbg(&pf->pdev->dev,
9185 		"LLDP event mib type %s\n", type ? "remote" : "local");
9186 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9187 		/* Update the remote cached instance and return */
9188 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9189 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9190 				&hw->remote_dcbx_config);
9191 		goto exit;
9192 	}
9193 
9194 	/* Store the old configuration */
9195 	tmp_dcbx_cfg = hw->local_dcbx_config;
9196 
9197 	/* Reset the old DCBx configuration data */
9198 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9199 	/* Get updated DCBX data from firmware */
9200 	ret = i40e_get_dcb_config(&pf->hw);
9201 	if (ret) {
9202 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9203 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9204 		    (hw->phy.link_info.link_speed &
9205 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9206 			dev_warn(&pf->pdev->dev,
9207 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9208 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9209 		} else {
9210 			dev_info(&pf->pdev->dev,
9211 				 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
9212 				 i40e_stat_str(&pf->hw, ret),
9213 				 i40e_aq_str(&pf->hw,
9214 					     pf->hw.aq.asq_last_status));
9215 		}
9216 		goto exit;
9217 	}
9218 
9219 	/* No change detected in DCBX configs */
9220 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9221 		    sizeof(tmp_dcbx_cfg))) {
9222 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9223 		goto exit;
9224 	}
9225 
9226 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9227 					       &hw->local_dcbx_config);
9228 
9229 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9230 
9231 	if (!need_reconfig)
9232 		goto exit;
9233 
9234 	/* Enable DCB tagging only when more than one TC */
9235 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9236 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9237 	else
9238 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9239 
9240 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9241 	/* Reconfiguration needed quiesce all VSIs */
9242 	i40e_pf_quiesce_all_vsi(pf);
9243 
9244 	/* Changes in configuration update VEB/VSI */
9245 	i40e_dcb_reconfigure(pf);
9246 
9247 	ret = i40e_resume_port_tx(pf);
9248 
9249 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9250 	/* In case of error no point in resuming VSIs */
9251 	if (ret)
9252 		goto exit;
9253 
9254 	/* Wait for the PF's queues to be disabled */
9255 	ret = i40e_pf_wait_queues_disabled(pf);
9256 	if (ret) {
9257 		/* Schedule PF reset to recover */
9258 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9259 		i40e_service_event_schedule(pf);
9260 	} else {
9261 		i40e_pf_unquiesce_all_vsi(pf);
9262 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9263 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9264 	}
9265 
9266 exit:
9267 	return ret;
9268 }
9269 #endif /* CONFIG_I40E_DCB */
9270 
9271 /**
9272  * i40e_do_reset_safe - Protected reset path for userland calls.
9273  * @pf: board private structure
9274  * @reset_flags: which reset is requested
9275  *
9276  **/
9277 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9278 {
9279 	rtnl_lock();
9280 	i40e_do_reset(pf, reset_flags, true);
9281 	rtnl_unlock();
9282 }
9283 
9284 /**
9285  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9286  * @pf: board private structure
9287  * @e: event info posted on ARQ
9288  *
9289  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9290  * and VF queues
9291  **/
9292 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9293 					   struct i40e_arq_event_info *e)
9294 {
9295 	struct i40e_aqc_lan_overflow *data =
9296 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9297 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9298 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9299 	struct i40e_hw *hw = &pf->hw;
9300 	struct i40e_vf *vf;
9301 	u16 vf_id;
9302 
9303 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9304 		queue, qtx_ctl);
9305 
9306 	/* Queue belongs to VF, find the VF and issue VF reset */
9307 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9308 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9309 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9310 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9311 		vf_id -= hw->func_caps.vf_base_id;
9312 		vf = &pf->vf[vf_id];
9313 		i40e_vc_notify_vf_reset(vf);
9314 		/* Allow VF to process pending reset notification */
9315 		msleep(20);
9316 		i40e_reset_vf(vf, false);
9317 	}
9318 }
9319 
9320 /**
9321  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9322  * @pf: board private structure
9323  **/
9324 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9325 {
9326 	u32 val, fcnt_prog;
9327 
9328 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9329 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9330 	return fcnt_prog;
9331 }
9332 
9333 /**
9334  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9335  * @pf: board private structure
9336  **/
9337 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9338 {
9339 	u32 val, fcnt_prog;
9340 
9341 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9342 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9343 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9344 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9345 	return fcnt_prog;
9346 }
9347 
9348 /**
9349  * i40e_get_global_fd_count - Get total FD filters programmed on device
9350  * @pf: board private structure
9351  **/
9352 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9353 {
9354 	u32 val, fcnt_prog;
9355 
9356 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9357 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9358 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9359 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9360 	return fcnt_prog;
9361 }
9362 
9363 /**
9364  * i40e_reenable_fdir_sb - Restore FDir SB capability
9365  * @pf: board private structure
9366  **/
9367 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9368 {
9369 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9370 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9371 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9372 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9373 }
9374 
9375 /**
9376  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9377  * @pf: board private structure
9378  **/
9379 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9380 {
9381 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9382 		/* ATR uses the same filtering logic as SB rules. It only
9383 		 * functions properly if the input set mask is at the default
9384 		 * settings. It is safe to restore the default input set
9385 		 * because there are no active TCPv4 filter rules.
9386 		 */
9387 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9388 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9389 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9390 
9391 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9392 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9393 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9394 	}
9395 }
9396 
9397 /**
9398  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9399  * @pf: board private structure
9400  * @filter: FDir filter to remove
9401  */
9402 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9403 				       struct i40e_fdir_filter *filter)
9404 {
9405 	/* Update counters */
9406 	pf->fdir_pf_active_filters--;
9407 	pf->fd_inv = 0;
9408 
9409 	switch (filter->flow_type) {
9410 	case TCP_V4_FLOW:
9411 		pf->fd_tcp4_filter_cnt--;
9412 		break;
9413 	case UDP_V4_FLOW:
9414 		pf->fd_udp4_filter_cnt--;
9415 		break;
9416 	case SCTP_V4_FLOW:
9417 		pf->fd_sctp4_filter_cnt--;
9418 		break;
9419 	case TCP_V6_FLOW:
9420 		pf->fd_tcp6_filter_cnt--;
9421 		break;
9422 	case UDP_V6_FLOW:
9423 		pf->fd_udp6_filter_cnt--;
9424 		break;
9425 	case SCTP_V6_FLOW:
9426 		pf->fd_udp6_filter_cnt--;
9427 		break;
9428 	case IP_USER_FLOW:
9429 		switch (filter->ipl4_proto) {
9430 		case IPPROTO_TCP:
9431 			pf->fd_tcp4_filter_cnt--;
9432 			break;
9433 		case IPPROTO_UDP:
9434 			pf->fd_udp4_filter_cnt--;
9435 			break;
9436 		case IPPROTO_SCTP:
9437 			pf->fd_sctp4_filter_cnt--;
9438 			break;
9439 		case IPPROTO_IP:
9440 			pf->fd_ip4_filter_cnt--;
9441 			break;
9442 		}
9443 		break;
9444 	case IPV6_USER_FLOW:
9445 		switch (filter->ipl4_proto) {
9446 		case IPPROTO_TCP:
9447 			pf->fd_tcp6_filter_cnt--;
9448 			break;
9449 		case IPPROTO_UDP:
9450 			pf->fd_udp6_filter_cnt--;
9451 			break;
9452 		case IPPROTO_SCTP:
9453 			pf->fd_sctp6_filter_cnt--;
9454 			break;
9455 		case IPPROTO_IP:
9456 			pf->fd_ip6_filter_cnt--;
9457 			break;
9458 		}
9459 		break;
9460 	}
9461 
9462 	/* Remove the filter from the list and free memory */
9463 	hlist_del(&filter->fdir_node);
9464 	kfree(filter);
9465 }
9466 
9467 /**
9468  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9469  * @pf: board private structure
9470  **/
9471 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9472 {
9473 	struct i40e_fdir_filter *filter;
9474 	u32 fcnt_prog, fcnt_avail;
9475 	struct hlist_node *node;
9476 
9477 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9478 		return;
9479 
9480 	/* Check if we have enough room to re-enable FDir SB capability. */
9481 	fcnt_prog = i40e_get_global_fd_count(pf);
9482 	fcnt_avail = pf->fdir_pf_filter_count;
9483 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9484 	    (pf->fd_add_err == 0) ||
9485 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9486 		i40e_reenable_fdir_sb(pf);
9487 
9488 	/* We should wait for even more space before re-enabling ATR.
9489 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9490 	 * rules active.
9491 	 */
9492 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9493 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9494 		i40e_reenable_fdir_atr(pf);
9495 
9496 	/* if hw had a problem adding a filter, delete it */
9497 	if (pf->fd_inv > 0) {
9498 		hlist_for_each_entry_safe(filter, node,
9499 					  &pf->fdir_filter_list, fdir_node)
9500 			if (filter->fd_id == pf->fd_inv)
9501 				i40e_delete_invalid_filter(pf, filter);
9502 	}
9503 }
9504 
9505 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9506 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9507 /**
9508  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9509  * @pf: board private structure
9510  **/
9511 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9512 {
9513 	unsigned long min_flush_time;
9514 	int flush_wait_retry = 50;
9515 	bool disable_atr = false;
9516 	int fd_room;
9517 	int reg;
9518 
9519 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9520 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9521 		return;
9522 
9523 	/* If the flush is happening too quick and we have mostly SB rules we
9524 	 * should not re-enable ATR for some time.
9525 	 */
9526 	min_flush_time = pf->fd_flush_timestamp +
9527 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9528 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9529 
9530 	if (!(time_after(jiffies, min_flush_time)) &&
9531 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9532 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9533 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9534 		disable_atr = true;
9535 	}
9536 
9537 	pf->fd_flush_timestamp = jiffies;
9538 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9539 	/* flush all filters */
9540 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9541 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9542 	i40e_flush(&pf->hw);
9543 	pf->fd_flush_cnt++;
9544 	pf->fd_add_err = 0;
9545 	do {
9546 		/* Check FD flush status every 5-6msec */
9547 		usleep_range(5000, 6000);
9548 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9549 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9550 			break;
9551 	} while (flush_wait_retry--);
9552 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9553 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9554 	} else {
9555 		/* replay sideband filters */
9556 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9557 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9558 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9559 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9560 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9561 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9562 	}
9563 }
9564 
9565 /**
9566  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9567  * @pf: board private structure
9568  **/
9569 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9570 {
9571 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9572 }
9573 
9574 /**
9575  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9576  * @pf: board private structure
9577  **/
9578 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9579 {
9580 
9581 	/* if interface is down do nothing */
9582 	if (test_bit(__I40E_DOWN, pf->state))
9583 		return;
9584 
9585 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9586 		i40e_fdir_flush_and_replay(pf);
9587 
9588 	i40e_fdir_check_and_reenable(pf);
9589 
9590 }
9591 
9592 /**
9593  * i40e_vsi_link_event - notify VSI of a link event
9594  * @vsi: vsi to be notified
9595  * @link_up: link up or down
9596  **/
9597 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9598 {
9599 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9600 		return;
9601 
9602 	switch (vsi->type) {
9603 	case I40E_VSI_MAIN:
9604 		if (!vsi->netdev || !vsi->netdev_registered)
9605 			break;
9606 
9607 		if (link_up) {
9608 			netif_carrier_on(vsi->netdev);
9609 			netif_tx_wake_all_queues(vsi->netdev);
9610 		} else {
9611 			netif_carrier_off(vsi->netdev);
9612 			netif_tx_stop_all_queues(vsi->netdev);
9613 		}
9614 		break;
9615 
9616 	case I40E_VSI_SRIOV:
9617 	case I40E_VSI_VMDQ2:
9618 	case I40E_VSI_CTRL:
9619 	case I40E_VSI_IWARP:
9620 	case I40E_VSI_MIRROR:
9621 	default:
9622 		/* there is no notification for other VSIs */
9623 		break;
9624 	}
9625 }
9626 
9627 /**
9628  * i40e_veb_link_event - notify elements on the veb of a link event
9629  * @veb: veb to be notified
9630  * @link_up: link up or down
9631  **/
9632 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9633 {
9634 	struct i40e_pf *pf;
9635 	int i;
9636 
9637 	if (!veb || !veb->pf)
9638 		return;
9639 	pf = veb->pf;
9640 
9641 	/* depth first... */
9642 	for (i = 0; i < I40E_MAX_VEB; i++)
9643 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9644 			i40e_veb_link_event(pf->veb[i], link_up);
9645 
9646 	/* ... now the local VSIs */
9647 	for (i = 0; i < pf->num_alloc_vsi; i++)
9648 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9649 			i40e_vsi_link_event(pf->vsi[i], link_up);
9650 }
9651 
9652 /**
9653  * i40e_link_event - Update netif_carrier status
9654  * @pf: board private structure
9655  **/
9656 static void i40e_link_event(struct i40e_pf *pf)
9657 {
9658 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9659 	u8 new_link_speed, old_link_speed;
9660 	i40e_status status;
9661 	bool new_link, old_link;
9662 #ifdef CONFIG_I40E_DCB
9663 	int err;
9664 #endif /* CONFIG_I40E_DCB */
9665 
9666 	/* set this to force the get_link_status call to refresh state */
9667 	pf->hw.phy.get_link_info = true;
9668 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9669 	status = i40e_get_link_status(&pf->hw, &new_link);
9670 
9671 	/* On success, disable temp link polling */
9672 	if (status == I40E_SUCCESS) {
9673 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9674 	} else {
9675 		/* Enable link polling temporarily until i40e_get_link_status
9676 		 * returns I40E_SUCCESS
9677 		 */
9678 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9679 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9680 			status);
9681 		return;
9682 	}
9683 
9684 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9685 	new_link_speed = pf->hw.phy.link_info.link_speed;
9686 
9687 	if (new_link == old_link &&
9688 	    new_link_speed == old_link_speed &&
9689 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9690 	     new_link == netif_carrier_ok(vsi->netdev)))
9691 		return;
9692 
9693 	i40e_print_link_message(vsi, new_link);
9694 
9695 	/* Notify the base of the switch tree connected to
9696 	 * the link.  Floating VEBs are not notified.
9697 	 */
9698 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9699 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9700 	else
9701 		i40e_vsi_link_event(vsi, new_link);
9702 
9703 	if (pf->vf)
9704 		i40e_vc_notify_link_state(pf);
9705 
9706 	if (pf->flags & I40E_FLAG_PTP)
9707 		i40e_ptp_set_increment(pf);
9708 #ifdef CONFIG_I40E_DCB
9709 	if (new_link == old_link)
9710 		return;
9711 	/* Not SW DCB so firmware will take care of default settings */
9712 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9713 		return;
9714 
9715 	/* We cover here only link down, as after link up in case of SW DCB
9716 	 * SW LLDP agent will take care of setting it up
9717 	 */
9718 	if (!new_link) {
9719 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9720 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9721 		err = i40e_dcb_sw_default_config(pf);
9722 		if (err) {
9723 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9724 				       I40E_FLAG_DCB_ENABLED);
9725 		} else {
9726 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9727 				       DCB_CAP_DCBX_VER_IEEE;
9728 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9729 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9730 		}
9731 	}
9732 #endif /* CONFIG_I40E_DCB */
9733 }
9734 
9735 /**
9736  * i40e_watchdog_subtask - periodic checks not using event driven response
9737  * @pf: board private structure
9738  **/
9739 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9740 {
9741 	int i;
9742 
9743 	/* if interface is down do nothing */
9744 	if (test_bit(__I40E_DOWN, pf->state) ||
9745 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9746 		return;
9747 
9748 	/* make sure we don't do these things too often */
9749 	if (time_before(jiffies, (pf->service_timer_previous +
9750 				  pf->service_timer_period)))
9751 		return;
9752 	pf->service_timer_previous = jiffies;
9753 
9754 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9755 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9756 		i40e_link_event(pf);
9757 
9758 	/* Update the stats for active netdevs so the network stack
9759 	 * can look at updated numbers whenever it cares to
9760 	 */
9761 	for (i = 0; i < pf->num_alloc_vsi; i++)
9762 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9763 			i40e_update_stats(pf->vsi[i]);
9764 
9765 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9766 		/* Update the stats for the active switching components */
9767 		for (i = 0; i < I40E_MAX_VEB; i++)
9768 			if (pf->veb[i])
9769 				i40e_update_veb_stats(pf->veb[i]);
9770 	}
9771 
9772 	i40e_ptp_rx_hang(pf);
9773 	i40e_ptp_tx_hang(pf);
9774 }
9775 
9776 /**
9777  * i40e_reset_subtask - Set up for resetting the device and driver
9778  * @pf: board private structure
9779  **/
9780 static void i40e_reset_subtask(struct i40e_pf *pf)
9781 {
9782 	u32 reset_flags = 0;
9783 
9784 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
9785 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
9786 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
9787 	}
9788 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
9789 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
9790 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9791 	}
9792 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
9793 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
9794 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
9795 	}
9796 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
9797 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
9798 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
9799 	}
9800 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
9801 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
9802 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
9803 	}
9804 
9805 	/* If there's a recovery already waiting, it takes
9806 	 * precedence before starting a new reset sequence.
9807 	 */
9808 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
9809 		i40e_prep_for_reset(pf);
9810 		i40e_reset(pf);
9811 		i40e_rebuild(pf, false, false);
9812 	}
9813 
9814 	/* If we're already down or resetting, just bail */
9815 	if (reset_flags &&
9816 	    !test_bit(__I40E_DOWN, pf->state) &&
9817 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
9818 		i40e_do_reset(pf, reset_flags, false);
9819 	}
9820 }
9821 
9822 /**
9823  * i40e_handle_link_event - Handle link event
9824  * @pf: board private structure
9825  * @e: event info posted on ARQ
9826  **/
9827 static void i40e_handle_link_event(struct i40e_pf *pf,
9828 				   struct i40e_arq_event_info *e)
9829 {
9830 	struct i40e_aqc_get_link_status *status =
9831 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
9832 
9833 	/* Do a new status request to re-enable LSE reporting
9834 	 * and load new status information into the hw struct
9835 	 * This completely ignores any state information
9836 	 * in the ARQ event info, instead choosing to always
9837 	 * issue the AQ update link status command.
9838 	 */
9839 	i40e_link_event(pf);
9840 
9841 	/* Check if module meets thermal requirements */
9842 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
9843 		dev_err(&pf->pdev->dev,
9844 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
9845 		dev_err(&pf->pdev->dev,
9846 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9847 	} else {
9848 		/* check for unqualified module, if link is down, suppress
9849 		 * the message if link was forced to be down.
9850 		 */
9851 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
9852 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
9853 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
9854 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
9855 			dev_err(&pf->pdev->dev,
9856 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
9857 			dev_err(&pf->pdev->dev,
9858 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9859 		}
9860 	}
9861 }
9862 
9863 /**
9864  * i40e_clean_adminq_subtask - Clean the AdminQ rings
9865  * @pf: board private structure
9866  **/
9867 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
9868 {
9869 	struct i40e_arq_event_info event;
9870 	struct i40e_hw *hw = &pf->hw;
9871 	u16 pending, i = 0;
9872 	i40e_status ret;
9873 	u16 opcode;
9874 	u32 oldval;
9875 	u32 val;
9876 
9877 	/* Do not run clean AQ when PF reset fails */
9878 	if (test_bit(__I40E_RESET_FAILED, pf->state))
9879 		return;
9880 
9881 	/* check for error indications */
9882 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
9883 	oldval = val;
9884 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
9885 		if (hw->debug_mask & I40E_DEBUG_AQ)
9886 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
9887 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
9888 	}
9889 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
9890 		if (hw->debug_mask & I40E_DEBUG_AQ)
9891 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
9892 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
9893 		pf->arq_overflows++;
9894 	}
9895 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
9896 		if (hw->debug_mask & I40E_DEBUG_AQ)
9897 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
9898 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
9899 	}
9900 	if (oldval != val)
9901 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
9902 
9903 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
9904 	oldval = val;
9905 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
9906 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9907 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
9908 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
9909 	}
9910 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
9911 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9912 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
9913 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
9914 	}
9915 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
9916 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9917 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
9918 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
9919 	}
9920 	if (oldval != val)
9921 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
9922 
9923 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
9924 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
9925 	if (!event.msg_buf)
9926 		return;
9927 
9928 	do {
9929 		ret = i40e_clean_arq_element(hw, &event, &pending);
9930 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
9931 			break;
9932 		else if (ret) {
9933 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
9934 			break;
9935 		}
9936 
9937 		opcode = le16_to_cpu(event.desc.opcode);
9938 		switch (opcode) {
9939 
9940 		case i40e_aqc_opc_get_link_status:
9941 			rtnl_lock();
9942 			i40e_handle_link_event(pf, &event);
9943 			rtnl_unlock();
9944 			break;
9945 		case i40e_aqc_opc_send_msg_to_pf:
9946 			ret = i40e_vc_process_vf_msg(pf,
9947 					le16_to_cpu(event.desc.retval),
9948 					le32_to_cpu(event.desc.cookie_high),
9949 					le32_to_cpu(event.desc.cookie_low),
9950 					event.msg_buf,
9951 					event.msg_len);
9952 			break;
9953 		case i40e_aqc_opc_lldp_update_mib:
9954 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
9955 #ifdef CONFIG_I40E_DCB
9956 			rtnl_lock();
9957 			i40e_handle_lldp_event(pf, &event);
9958 			rtnl_unlock();
9959 #endif /* CONFIG_I40E_DCB */
9960 			break;
9961 		case i40e_aqc_opc_event_lan_overflow:
9962 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
9963 			i40e_handle_lan_overflow_event(pf, &event);
9964 			break;
9965 		case i40e_aqc_opc_send_msg_to_peer:
9966 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
9967 			break;
9968 		case i40e_aqc_opc_nvm_erase:
9969 		case i40e_aqc_opc_nvm_update:
9970 		case i40e_aqc_opc_oem_post_update:
9971 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
9972 				   "ARQ NVM operation 0x%04x completed\n",
9973 				   opcode);
9974 			break;
9975 		default:
9976 			dev_info(&pf->pdev->dev,
9977 				 "ARQ: Unknown event 0x%04x ignored\n",
9978 				 opcode);
9979 			break;
9980 		}
9981 	} while (i++ < pf->adminq_work_limit);
9982 
9983 	if (i < pf->adminq_work_limit)
9984 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
9985 
9986 	/* re-enable Admin queue interrupt cause */
9987 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
9988 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
9989 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
9990 	i40e_flush(hw);
9991 
9992 	kfree(event.msg_buf);
9993 }
9994 
9995 /**
9996  * i40e_verify_eeprom - make sure eeprom is good to use
9997  * @pf: board private structure
9998  **/
9999 static void i40e_verify_eeprom(struct i40e_pf *pf)
10000 {
10001 	int err;
10002 
10003 	err = i40e_diag_eeprom_test(&pf->hw);
10004 	if (err) {
10005 		/* retry in case of garbage read */
10006 		err = i40e_diag_eeprom_test(&pf->hw);
10007 		if (err) {
10008 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10009 				 err);
10010 			set_bit(__I40E_BAD_EEPROM, pf->state);
10011 		}
10012 	}
10013 
10014 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10015 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10016 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10017 	}
10018 }
10019 
10020 /**
10021  * i40e_enable_pf_switch_lb
10022  * @pf: pointer to the PF structure
10023  *
10024  * enable switch loop back or die - no point in a return value
10025  **/
10026 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10027 {
10028 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10029 	struct i40e_vsi_context ctxt;
10030 	int ret;
10031 
10032 	ctxt.seid = pf->main_vsi_seid;
10033 	ctxt.pf_num = pf->hw.pf_id;
10034 	ctxt.vf_num = 0;
10035 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10036 	if (ret) {
10037 		dev_info(&pf->pdev->dev,
10038 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10039 			 i40e_stat_str(&pf->hw, ret),
10040 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10041 		return;
10042 	}
10043 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10044 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10045 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10046 
10047 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10048 	if (ret) {
10049 		dev_info(&pf->pdev->dev,
10050 			 "update vsi switch failed, err %s aq_err %s\n",
10051 			 i40e_stat_str(&pf->hw, ret),
10052 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10053 	}
10054 }
10055 
10056 /**
10057  * i40e_disable_pf_switch_lb
10058  * @pf: pointer to the PF structure
10059  *
10060  * disable switch loop back or die - no point in a return value
10061  **/
10062 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10063 {
10064 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10065 	struct i40e_vsi_context ctxt;
10066 	int ret;
10067 
10068 	ctxt.seid = pf->main_vsi_seid;
10069 	ctxt.pf_num = pf->hw.pf_id;
10070 	ctxt.vf_num = 0;
10071 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10072 	if (ret) {
10073 		dev_info(&pf->pdev->dev,
10074 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10075 			 i40e_stat_str(&pf->hw, ret),
10076 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10077 		return;
10078 	}
10079 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10080 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10081 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10082 
10083 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10084 	if (ret) {
10085 		dev_info(&pf->pdev->dev,
10086 			 "update vsi switch failed, err %s aq_err %s\n",
10087 			 i40e_stat_str(&pf->hw, ret),
10088 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10089 	}
10090 }
10091 
10092 /**
10093  * i40e_config_bridge_mode - Configure the HW bridge mode
10094  * @veb: pointer to the bridge instance
10095  *
10096  * Configure the loop back mode for the LAN VSI that is downlink to the
10097  * specified HW bridge instance. It is expected this function is called
10098  * when a new HW bridge is instantiated.
10099  **/
10100 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10101 {
10102 	struct i40e_pf *pf = veb->pf;
10103 
10104 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10105 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10106 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10107 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10108 		i40e_disable_pf_switch_lb(pf);
10109 	else
10110 		i40e_enable_pf_switch_lb(pf);
10111 }
10112 
10113 /**
10114  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10115  * @veb: pointer to the VEB instance
10116  *
10117  * This is a recursive function that first builds the attached VSIs then
10118  * recurses in to build the next layer of VEB.  We track the connections
10119  * through our own index numbers because the seid's from the HW could
10120  * change across the reset.
10121  **/
10122 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10123 {
10124 	struct i40e_vsi *ctl_vsi = NULL;
10125 	struct i40e_pf *pf = veb->pf;
10126 	int v, veb_idx;
10127 	int ret;
10128 
10129 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10130 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10131 		if (pf->vsi[v] &&
10132 		    pf->vsi[v]->veb_idx == veb->idx &&
10133 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10134 			ctl_vsi = pf->vsi[v];
10135 			break;
10136 		}
10137 	}
10138 	if (!ctl_vsi) {
10139 		dev_info(&pf->pdev->dev,
10140 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10141 		ret = -ENOENT;
10142 		goto end_reconstitute;
10143 	}
10144 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10145 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10146 	ret = i40e_add_vsi(ctl_vsi);
10147 	if (ret) {
10148 		dev_info(&pf->pdev->dev,
10149 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10150 			 veb->idx, ret);
10151 		goto end_reconstitute;
10152 	}
10153 	i40e_vsi_reset_stats(ctl_vsi);
10154 
10155 	/* create the VEB in the switch and move the VSI onto the VEB */
10156 	ret = i40e_add_veb(veb, ctl_vsi);
10157 	if (ret)
10158 		goto end_reconstitute;
10159 
10160 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10161 		veb->bridge_mode = BRIDGE_MODE_VEB;
10162 	else
10163 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10164 	i40e_config_bridge_mode(veb);
10165 
10166 	/* create the remaining VSIs attached to this VEB */
10167 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10168 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10169 			continue;
10170 
10171 		if (pf->vsi[v]->veb_idx == veb->idx) {
10172 			struct i40e_vsi *vsi = pf->vsi[v];
10173 
10174 			vsi->uplink_seid = veb->seid;
10175 			ret = i40e_add_vsi(vsi);
10176 			if (ret) {
10177 				dev_info(&pf->pdev->dev,
10178 					 "rebuild of vsi_idx %d failed: %d\n",
10179 					 v, ret);
10180 				goto end_reconstitute;
10181 			}
10182 			i40e_vsi_reset_stats(vsi);
10183 		}
10184 	}
10185 
10186 	/* create any VEBs attached to this VEB - RECURSION */
10187 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10188 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10189 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10190 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10191 			if (ret)
10192 				break;
10193 		}
10194 	}
10195 
10196 end_reconstitute:
10197 	return ret;
10198 }
10199 
10200 /**
10201  * i40e_get_capabilities - get info about the HW
10202  * @pf: the PF struct
10203  * @list_type: AQ capability to be queried
10204  **/
10205 static int i40e_get_capabilities(struct i40e_pf *pf,
10206 				 enum i40e_admin_queue_opc list_type)
10207 {
10208 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10209 	u16 data_size;
10210 	int buf_len;
10211 	int err;
10212 
10213 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10214 	do {
10215 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10216 		if (!cap_buf)
10217 			return -ENOMEM;
10218 
10219 		/* this loads the data into the hw struct for us */
10220 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10221 						    &data_size, list_type,
10222 						    NULL);
10223 		/* data loaded, buffer no longer needed */
10224 		kfree(cap_buf);
10225 
10226 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10227 			/* retry with a larger buffer */
10228 			buf_len = data_size;
10229 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10230 			dev_info(&pf->pdev->dev,
10231 				 "capability discovery failed, err %s aq_err %s\n",
10232 				 i40e_stat_str(&pf->hw, err),
10233 				 i40e_aq_str(&pf->hw,
10234 					     pf->hw.aq.asq_last_status));
10235 			return -ENODEV;
10236 		}
10237 	} while (err);
10238 
10239 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10240 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10241 			dev_info(&pf->pdev->dev,
10242 				 "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",
10243 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10244 				 pf->hw.func_caps.num_msix_vectors,
10245 				 pf->hw.func_caps.num_msix_vectors_vf,
10246 				 pf->hw.func_caps.fd_filters_guaranteed,
10247 				 pf->hw.func_caps.fd_filters_best_effort,
10248 				 pf->hw.func_caps.num_tx_qp,
10249 				 pf->hw.func_caps.num_vsis);
10250 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10251 			dev_info(&pf->pdev->dev,
10252 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10253 				 pf->hw.dev_caps.switch_mode,
10254 				 pf->hw.dev_caps.valid_functions);
10255 			dev_info(&pf->pdev->dev,
10256 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10257 				 pf->hw.dev_caps.sr_iov_1_1,
10258 				 pf->hw.dev_caps.num_vfs);
10259 			dev_info(&pf->pdev->dev,
10260 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10261 				 pf->hw.dev_caps.num_vsis,
10262 				 pf->hw.dev_caps.num_rx_qp,
10263 				 pf->hw.dev_caps.num_tx_qp);
10264 		}
10265 	}
10266 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10267 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10268 		       + pf->hw.func_caps.num_vfs)
10269 		if (pf->hw.revision_id == 0 &&
10270 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10271 			dev_info(&pf->pdev->dev,
10272 				 "got num_vsis %d, setting num_vsis to %d\n",
10273 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10274 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10275 		}
10276 	}
10277 	return 0;
10278 }
10279 
10280 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10281 
10282 /**
10283  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10284  * @pf: board private structure
10285  **/
10286 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10287 {
10288 	struct i40e_vsi *vsi;
10289 
10290 	/* quick workaround for an NVM issue that leaves a critical register
10291 	 * uninitialized
10292 	 */
10293 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10294 		static const u32 hkey[] = {
10295 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10296 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10297 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10298 			0x95b3a76d};
10299 		int i;
10300 
10301 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10302 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10303 	}
10304 
10305 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10306 		return;
10307 
10308 	/* find existing VSI and see if it needs configuring */
10309 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10310 
10311 	/* create a new VSI if none exists */
10312 	if (!vsi) {
10313 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10314 				     pf->vsi[pf->lan_vsi]->seid, 0);
10315 		if (!vsi) {
10316 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10317 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10318 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10319 			return;
10320 		}
10321 	}
10322 
10323 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10324 }
10325 
10326 /**
10327  * i40e_fdir_teardown - release the Flow Director resources
10328  * @pf: board private structure
10329  **/
10330 static void i40e_fdir_teardown(struct i40e_pf *pf)
10331 {
10332 	struct i40e_vsi *vsi;
10333 
10334 	i40e_fdir_filter_exit(pf);
10335 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10336 	if (vsi)
10337 		i40e_vsi_release(vsi);
10338 }
10339 
10340 /**
10341  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10342  * @vsi: PF main vsi
10343  * @seid: seid of main or channel VSIs
10344  *
10345  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10346  * existed before reset
10347  **/
10348 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10349 {
10350 	struct i40e_cloud_filter *cfilter;
10351 	struct i40e_pf *pf = vsi->back;
10352 	struct hlist_node *node;
10353 	i40e_status ret;
10354 
10355 	/* Add cloud filters back if they exist */
10356 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10357 				  cloud_node) {
10358 		if (cfilter->seid != seid)
10359 			continue;
10360 
10361 		if (cfilter->dst_port)
10362 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10363 								true);
10364 		else
10365 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10366 
10367 		if (ret) {
10368 			dev_dbg(&pf->pdev->dev,
10369 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
10370 				i40e_stat_str(&pf->hw, ret),
10371 				i40e_aq_str(&pf->hw,
10372 					    pf->hw.aq.asq_last_status));
10373 			return ret;
10374 		}
10375 	}
10376 	return 0;
10377 }
10378 
10379 /**
10380  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10381  * @vsi: PF main vsi
10382  *
10383  * Rebuilds channel VSIs if they existed before reset
10384  **/
10385 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10386 {
10387 	struct i40e_channel *ch, *ch_tmp;
10388 	i40e_status ret;
10389 
10390 	if (list_empty(&vsi->ch_list))
10391 		return 0;
10392 
10393 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10394 		if (!ch->initialized)
10395 			break;
10396 		/* Proceed with creation of channel (VMDq2) VSI */
10397 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10398 		if (ret) {
10399 			dev_info(&vsi->back->pdev->dev,
10400 				 "failed to rebuild channels using uplink_seid %u\n",
10401 				 vsi->uplink_seid);
10402 			return ret;
10403 		}
10404 		/* Reconfigure TX queues using QTX_CTL register */
10405 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10406 		if (ret) {
10407 			dev_info(&vsi->back->pdev->dev,
10408 				 "failed to configure TX rings for channel %u\n",
10409 				 ch->seid);
10410 			return ret;
10411 		}
10412 		/* update 'next_base_queue' */
10413 		vsi->next_base_queue = vsi->next_base_queue +
10414 							ch->num_queue_pairs;
10415 		if (ch->max_tx_rate) {
10416 			u64 credits = ch->max_tx_rate;
10417 
10418 			if (i40e_set_bw_limit(vsi, ch->seid,
10419 					      ch->max_tx_rate))
10420 				return -EINVAL;
10421 
10422 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10423 			dev_dbg(&vsi->back->pdev->dev,
10424 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10425 				ch->max_tx_rate,
10426 				credits,
10427 				ch->seid);
10428 		}
10429 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10430 		if (ret) {
10431 			dev_dbg(&vsi->back->pdev->dev,
10432 				"Failed to rebuild cloud filters for channel VSI %u\n",
10433 				ch->seid);
10434 			return ret;
10435 		}
10436 	}
10437 	return 0;
10438 }
10439 
10440 /**
10441  * i40e_prep_for_reset - prep for the core to reset
10442  * @pf: board private structure
10443  *
10444  * Close up the VFs and other things in prep for PF Reset.
10445   **/
10446 static void i40e_prep_for_reset(struct i40e_pf *pf)
10447 {
10448 	struct i40e_hw *hw = &pf->hw;
10449 	i40e_status ret = 0;
10450 	u32 v;
10451 
10452 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10453 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10454 		return;
10455 	if (i40e_check_asq_alive(&pf->hw))
10456 		i40e_vc_notify_reset(pf);
10457 
10458 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10459 
10460 	/* quiesce the VSIs and their queues that are not already DOWN */
10461 	i40e_pf_quiesce_all_vsi(pf);
10462 
10463 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10464 		if (pf->vsi[v])
10465 			pf->vsi[v]->seid = 0;
10466 	}
10467 
10468 	i40e_shutdown_adminq(&pf->hw);
10469 
10470 	/* call shutdown HMC */
10471 	if (hw->hmc.hmc_obj) {
10472 		ret = i40e_shutdown_lan_hmc(hw);
10473 		if (ret)
10474 			dev_warn(&pf->pdev->dev,
10475 				 "shutdown_lan_hmc failed: %d\n", ret);
10476 	}
10477 
10478 	/* Save the current PTP time so that we can restore the time after the
10479 	 * reset completes.
10480 	 */
10481 	i40e_ptp_save_hw_time(pf);
10482 }
10483 
10484 /**
10485  * i40e_send_version - update firmware with driver version
10486  * @pf: PF struct
10487  */
10488 static void i40e_send_version(struct i40e_pf *pf)
10489 {
10490 	struct i40e_driver_version dv;
10491 
10492 	dv.major_version = 0xff;
10493 	dv.minor_version = 0xff;
10494 	dv.build_version = 0xff;
10495 	dv.subbuild_version = 0;
10496 	strlcpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10497 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10498 }
10499 
10500 /**
10501  * i40e_get_oem_version - get OEM specific version information
10502  * @hw: pointer to the hardware structure
10503  **/
10504 static void i40e_get_oem_version(struct i40e_hw *hw)
10505 {
10506 	u16 block_offset = 0xffff;
10507 	u16 block_length = 0;
10508 	u16 capabilities = 0;
10509 	u16 gen_snap = 0;
10510 	u16 release = 0;
10511 
10512 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10513 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10514 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10515 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10516 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10517 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10518 #define I40E_NVM_OEM_LENGTH			3
10519 
10520 	/* Check if pointer to OEM version block is valid. */
10521 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10522 	if (block_offset == 0xffff)
10523 		return;
10524 
10525 	/* Check if OEM version block has correct length. */
10526 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10527 			   &block_length);
10528 	if (block_length < I40E_NVM_OEM_LENGTH)
10529 		return;
10530 
10531 	/* Check if OEM version format is as expected. */
10532 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10533 			   &capabilities);
10534 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10535 		return;
10536 
10537 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10538 			   &gen_snap);
10539 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10540 			   &release);
10541 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10542 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10543 }
10544 
10545 /**
10546  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10547  * @pf: board private structure
10548  **/
10549 static int i40e_reset(struct i40e_pf *pf)
10550 {
10551 	struct i40e_hw *hw = &pf->hw;
10552 	i40e_status ret;
10553 
10554 	ret = i40e_pf_reset(hw);
10555 	if (ret) {
10556 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10557 		set_bit(__I40E_RESET_FAILED, pf->state);
10558 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10559 	} else {
10560 		pf->pfr_count++;
10561 	}
10562 	return ret;
10563 }
10564 
10565 /**
10566  * i40e_rebuild - rebuild using a saved config
10567  * @pf: board private structure
10568  * @reinit: if the Main VSI needs to re-initialized.
10569  * @lock_acquired: indicates whether or not the lock has been acquired
10570  * before this function was called.
10571  **/
10572 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10573 {
10574 	int old_recovery_mode_bit = test_bit(__I40E_RECOVERY_MODE, pf->state);
10575 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10576 	struct i40e_hw *hw = &pf->hw;
10577 	i40e_status ret;
10578 	u32 val;
10579 	int v;
10580 
10581 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10582 	    i40e_check_recovery_mode(pf)) {
10583 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10584 	}
10585 
10586 	if (test_bit(__I40E_DOWN, pf->state) &&
10587 	    !test_bit(__I40E_RECOVERY_MODE, pf->state) &&
10588 	    !old_recovery_mode_bit)
10589 		goto clear_recovery;
10590 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10591 
10592 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10593 	ret = i40e_init_adminq(&pf->hw);
10594 	if (ret) {
10595 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10596 			 i40e_stat_str(&pf->hw, ret),
10597 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10598 		goto clear_recovery;
10599 	}
10600 	i40e_get_oem_version(&pf->hw);
10601 
10602 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10603 		/* The following delay is necessary for firmware update. */
10604 		mdelay(1000);
10605 	}
10606 
10607 	/* re-verify the eeprom if we just had an EMP reset */
10608 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10609 		i40e_verify_eeprom(pf);
10610 
10611 	/* if we are going out of or into recovery mode we have to act
10612 	 * accordingly with regard to resources initialization
10613 	 * and deinitialization
10614 	 */
10615 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) ||
10616 	    old_recovery_mode_bit) {
10617 		if (i40e_get_capabilities(pf,
10618 					  i40e_aqc_opc_list_func_capabilities))
10619 			goto end_unlock;
10620 
10621 		if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10622 			/* we're staying in recovery mode so we'll reinitialize
10623 			 * misc vector here
10624 			 */
10625 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10626 				goto end_unlock;
10627 		} else {
10628 			if (!lock_acquired)
10629 				rtnl_lock();
10630 			/* we're going out of recovery mode so we'll free
10631 			 * the IRQ allocated specifically for recovery mode
10632 			 * and restore the interrupt scheme
10633 			 */
10634 			free_irq(pf->pdev->irq, pf);
10635 			i40e_clear_interrupt_scheme(pf);
10636 			if (i40e_restore_interrupt_scheme(pf))
10637 				goto end_unlock;
10638 		}
10639 
10640 		/* tell the firmware that we're starting */
10641 		i40e_send_version(pf);
10642 
10643 		/* bail out in case recovery mode was detected, as there is
10644 		 * no need for further configuration.
10645 		 */
10646 		goto end_unlock;
10647 	}
10648 
10649 	i40e_clear_pxe_mode(hw);
10650 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10651 	if (ret)
10652 		goto end_core_reset;
10653 
10654 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10655 				hw->func_caps.num_rx_qp, 0, 0);
10656 	if (ret) {
10657 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10658 		goto end_core_reset;
10659 	}
10660 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10661 	if (ret) {
10662 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10663 		goto end_core_reset;
10664 	}
10665 
10666 #ifdef CONFIG_I40E_DCB
10667 	/* Enable FW to write a default DCB config on link-up
10668 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10669 	 * is not supported with new link speed
10670 	 */
10671 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
10672 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10673 	} else {
10674 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10675 		    (hw->phy.link_info.link_speed &
10676 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10677 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10678 			dev_warn(&pf->pdev->dev,
10679 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10680 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10681 		} else {
10682 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10683 			ret = i40e_init_pf_dcb(pf);
10684 			if (ret) {
10685 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10686 					 ret);
10687 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10688 				/* Continue without DCB enabled */
10689 			}
10690 		}
10691 	}
10692 
10693 #endif /* CONFIG_I40E_DCB */
10694 	if (!lock_acquired)
10695 		rtnl_lock();
10696 	ret = i40e_setup_pf_switch(pf, reinit, true);
10697 	if (ret)
10698 		goto end_unlock;
10699 
10700 	/* The driver only wants link up/down and module qualification
10701 	 * reports from firmware.  Note the negative logic.
10702 	 */
10703 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10704 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10705 					 I40E_AQ_EVENT_MEDIA_NA |
10706 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10707 	if (ret)
10708 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10709 			 i40e_stat_str(&pf->hw, ret),
10710 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10711 
10712 	/* Rebuild the VSIs and VEBs that existed before reset.
10713 	 * They are still in our local switch element arrays, so only
10714 	 * need to rebuild the switch model in the HW.
10715 	 *
10716 	 * If there were VEBs but the reconstitution failed, we'll try
10717 	 * to recover minimal use by getting the basic PF VSI working.
10718 	 */
10719 	if (vsi->uplink_seid != pf->mac_seid) {
10720 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10721 		/* find the one VEB connected to the MAC, and find orphans */
10722 		for (v = 0; v < I40E_MAX_VEB; v++) {
10723 			if (!pf->veb[v])
10724 				continue;
10725 
10726 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10727 			    pf->veb[v]->uplink_seid == 0) {
10728 				ret = i40e_reconstitute_veb(pf->veb[v]);
10729 
10730 				if (!ret)
10731 					continue;
10732 
10733 				/* If Main VEB failed, we're in deep doodoo,
10734 				 * so give up rebuilding the switch and set up
10735 				 * for minimal rebuild of PF VSI.
10736 				 * If orphan failed, we'll report the error
10737 				 * but try to keep going.
10738 				 */
10739 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10740 					dev_info(&pf->pdev->dev,
10741 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10742 						 ret);
10743 					vsi->uplink_seid = pf->mac_seid;
10744 					break;
10745 				} else if (pf->veb[v]->uplink_seid == 0) {
10746 					dev_info(&pf->pdev->dev,
10747 						 "rebuild of orphan VEB failed: %d\n",
10748 						 ret);
10749 				}
10750 			}
10751 		}
10752 	}
10753 
10754 	if (vsi->uplink_seid == pf->mac_seid) {
10755 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10756 		/* no VEB, so rebuild only the Main VSI */
10757 		ret = i40e_add_vsi(vsi);
10758 		if (ret) {
10759 			dev_info(&pf->pdev->dev,
10760 				 "rebuild of Main VSI failed: %d\n", ret);
10761 			goto end_unlock;
10762 		}
10763 	}
10764 
10765 	if (vsi->mqprio_qopt.max_rate[0]) {
10766 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
10767 		u64 credits = 0;
10768 
10769 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
10770 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10771 		if (ret)
10772 			goto end_unlock;
10773 
10774 		credits = max_tx_rate;
10775 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10776 		dev_dbg(&vsi->back->pdev->dev,
10777 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10778 			max_tx_rate,
10779 			credits,
10780 			vsi->seid);
10781 	}
10782 
10783 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10784 	if (ret)
10785 		goto end_unlock;
10786 
10787 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
10788 	 * for this main VSI if they exist
10789 	 */
10790 	ret = i40e_rebuild_channels(vsi);
10791 	if (ret)
10792 		goto end_unlock;
10793 
10794 	/* Reconfigure hardware for allowing smaller MSS in the case
10795 	 * of TSO, so that we avoid the MDD being fired and causing
10796 	 * a reset in the case of small MSS+TSO.
10797 	 */
10798 #define I40E_REG_MSS          0x000E64DC
10799 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
10800 #define I40E_64BYTE_MSS       0x400000
10801 	val = rd32(hw, I40E_REG_MSS);
10802 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10803 		val &= ~I40E_REG_MSS_MIN_MASK;
10804 		val |= I40E_64BYTE_MSS;
10805 		wr32(hw, I40E_REG_MSS, val);
10806 	}
10807 
10808 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
10809 		msleep(75);
10810 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10811 		if (ret)
10812 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10813 				 i40e_stat_str(&pf->hw, ret),
10814 				 i40e_aq_str(&pf->hw,
10815 					     pf->hw.aq.asq_last_status));
10816 	}
10817 	/* reinit the misc interrupt */
10818 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10819 		ret = i40e_setup_misc_vector(pf);
10820 
10821 	/* Add a filter to drop all Flow control frames from any VSI from being
10822 	 * transmitted. By doing so we stop a malicious VF from sending out
10823 	 * PAUSE or PFC frames and potentially controlling traffic for other
10824 	 * PF/VF VSIs.
10825 	 * The FW can still send Flow control frames if enabled.
10826 	 */
10827 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
10828 						       pf->main_vsi_seid);
10829 
10830 	/* restart the VSIs that were rebuilt and running before the reset */
10831 	i40e_pf_unquiesce_all_vsi(pf);
10832 
10833 	/* Release the RTNL lock before we start resetting VFs */
10834 	if (!lock_acquired)
10835 		rtnl_unlock();
10836 
10837 	/* Restore promiscuous settings */
10838 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
10839 	if (ret)
10840 		dev_warn(&pf->pdev->dev,
10841 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
10842 			 pf->cur_promisc ? "on" : "off",
10843 			 i40e_stat_str(&pf->hw, ret),
10844 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10845 
10846 	i40e_reset_all_vfs(pf, true);
10847 
10848 	/* tell the firmware that we're starting */
10849 	i40e_send_version(pf);
10850 
10851 	/* We've already released the lock, so don't do it again */
10852 	goto end_core_reset;
10853 
10854 end_unlock:
10855 	if (!lock_acquired)
10856 		rtnl_unlock();
10857 end_core_reset:
10858 	clear_bit(__I40E_RESET_FAILED, pf->state);
10859 clear_recovery:
10860 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10861 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
10862 }
10863 
10864 /**
10865  * i40e_reset_and_rebuild - reset and rebuild using a saved config
10866  * @pf: board private structure
10867  * @reinit: if the Main VSI needs to re-initialized.
10868  * @lock_acquired: indicates whether or not the lock has been acquired
10869  * before this function was called.
10870  **/
10871 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
10872 				   bool lock_acquired)
10873 {
10874 	int ret;
10875 
10876 	if (test_bit(__I40E_IN_REMOVE, pf->state))
10877 		return;
10878 	/* Now we wait for GRST to settle out.
10879 	 * We don't have to delete the VEBs or VSIs from the hw switch
10880 	 * because the reset will make them disappear.
10881 	 */
10882 	ret = i40e_reset(pf);
10883 	if (!ret)
10884 		i40e_rebuild(pf, reinit, lock_acquired);
10885 }
10886 
10887 /**
10888  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
10889  * @pf: board private structure
10890  *
10891  * Close up the VFs and other things in prep for a Core Reset,
10892  * then get ready to rebuild the world.
10893  * @lock_acquired: indicates whether or not the lock has been acquired
10894  * before this function was called.
10895  **/
10896 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
10897 {
10898 	i40e_prep_for_reset(pf);
10899 	i40e_reset_and_rebuild(pf, false, lock_acquired);
10900 }
10901 
10902 /**
10903  * i40e_handle_mdd_event
10904  * @pf: pointer to the PF structure
10905  *
10906  * Called from the MDD irq handler to identify possibly malicious vfs
10907  **/
10908 static void i40e_handle_mdd_event(struct i40e_pf *pf)
10909 {
10910 	struct i40e_hw *hw = &pf->hw;
10911 	bool mdd_detected = false;
10912 	struct i40e_vf *vf;
10913 	u32 reg;
10914 	int i;
10915 
10916 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
10917 		return;
10918 
10919 	/* find what triggered the MDD event */
10920 	reg = rd32(hw, I40E_GL_MDET_TX);
10921 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
10922 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
10923 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
10924 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
10925 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
10926 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
10927 				I40E_GL_MDET_TX_EVENT_SHIFT;
10928 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
10929 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
10930 				pf->hw.func_caps.base_queue;
10931 		if (netif_msg_tx_err(pf))
10932 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
10933 				 event, queue, pf_num, vf_num);
10934 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
10935 		mdd_detected = true;
10936 	}
10937 	reg = rd32(hw, I40E_GL_MDET_RX);
10938 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
10939 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
10940 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
10941 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
10942 				I40E_GL_MDET_RX_EVENT_SHIFT;
10943 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
10944 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
10945 				pf->hw.func_caps.base_queue;
10946 		if (netif_msg_rx_err(pf))
10947 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
10948 				 event, queue, func);
10949 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
10950 		mdd_detected = true;
10951 	}
10952 
10953 	if (mdd_detected) {
10954 		reg = rd32(hw, I40E_PF_MDET_TX);
10955 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
10956 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
10957 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
10958 		}
10959 		reg = rd32(hw, I40E_PF_MDET_RX);
10960 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
10961 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
10962 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
10963 		}
10964 	}
10965 
10966 	/* see if one of the VFs needs its hand slapped */
10967 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
10968 		vf = &(pf->vf[i]);
10969 		reg = rd32(hw, I40E_VP_MDET_TX(i));
10970 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
10971 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
10972 			vf->num_mdd_events++;
10973 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
10974 				 i);
10975 			dev_info(&pf->pdev->dev,
10976 				 "Use PF Control I/F to re-enable the VF\n");
10977 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10978 		}
10979 
10980 		reg = rd32(hw, I40E_VP_MDET_RX(i));
10981 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
10982 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
10983 			vf->num_mdd_events++;
10984 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
10985 				 i);
10986 			dev_info(&pf->pdev->dev,
10987 				 "Use PF Control I/F to re-enable the VF\n");
10988 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10989 		}
10990 	}
10991 
10992 	/* re-enable mdd interrupt cause */
10993 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
10994 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
10995 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
10996 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
10997 	i40e_flush(hw);
10998 }
10999 
11000 /**
11001  * i40e_service_task - Run the driver's async subtasks
11002  * @work: pointer to work_struct containing our data
11003  **/
11004 static void i40e_service_task(struct work_struct *work)
11005 {
11006 	struct i40e_pf *pf = container_of(work,
11007 					  struct i40e_pf,
11008 					  service_task);
11009 	unsigned long start_time = jiffies;
11010 
11011 	/* don't bother with service tasks if a reset is in progress */
11012 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11013 	    test_bit(__I40E_SUSPENDED, pf->state))
11014 		return;
11015 
11016 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11017 		return;
11018 
11019 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11020 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11021 		i40e_sync_filters_subtask(pf);
11022 		i40e_reset_subtask(pf);
11023 		i40e_handle_mdd_event(pf);
11024 		i40e_vc_process_vflr_event(pf);
11025 		i40e_watchdog_subtask(pf);
11026 		i40e_fdir_reinit_subtask(pf);
11027 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11028 			/* Client subtask will reopen next time through. */
11029 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11030 							   true);
11031 		} else {
11032 			i40e_client_subtask(pf);
11033 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11034 					       pf->state))
11035 				i40e_notify_client_of_l2_param_changes(
11036 								pf->vsi[pf->lan_vsi]);
11037 		}
11038 		i40e_sync_filters_subtask(pf);
11039 	} else {
11040 		i40e_reset_subtask(pf);
11041 	}
11042 
11043 	i40e_clean_adminq_subtask(pf);
11044 
11045 	/* flush memory to make sure state is correct before next watchdog */
11046 	smp_mb__before_atomic();
11047 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11048 
11049 	/* If the tasks have taken longer than one timer cycle or there
11050 	 * is more work to be done, reschedule the service task now
11051 	 * rather than wait for the timer to tick again.
11052 	 */
11053 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11054 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11055 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11056 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11057 		i40e_service_event_schedule(pf);
11058 }
11059 
11060 /**
11061  * i40e_service_timer - timer callback
11062  * @t: timer list pointer
11063  **/
11064 static void i40e_service_timer(struct timer_list *t)
11065 {
11066 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11067 
11068 	mod_timer(&pf->service_timer,
11069 		  round_jiffies(jiffies + pf->service_timer_period));
11070 	i40e_service_event_schedule(pf);
11071 }
11072 
11073 /**
11074  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11075  * @vsi: the VSI being configured
11076  **/
11077 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11078 {
11079 	struct i40e_pf *pf = vsi->back;
11080 
11081 	switch (vsi->type) {
11082 	case I40E_VSI_MAIN:
11083 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11084 		if (!vsi->num_tx_desc)
11085 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11086 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11087 		if (!vsi->num_rx_desc)
11088 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11089 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11090 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11091 			vsi->num_q_vectors = pf->num_lan_msix;
11092 		else
11093 			vsi->num_q_vectors = 1;
11094 
11095 		break;
11096 
11097 	case I40E_VSI_FDIR:
11098 		vsi->alloc_queue_pairs = 1;
11099 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11100 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11101 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11102 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11103 		vsi->num_q_vectors = pf->num_fdsb_msix;
11104 		break;
11105 
11106 	case I40E_VSI_VMDQ2:
11107 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11108 		if (!vsi->num_tx_desc)
11109 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11110 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11111 		if (!vsi->num_rx_desc)
11112 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11113 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11114 		vsi->num_q_vectors = pf->num_vmdq_msix;
11115 		break;
11116 
11117 	case I40E_VSI_SRIOV:
11118 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11119 		if (!vsi->num_tx_desc)
11120 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11121 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11122 		if (!vsi->num_rx_desc)
11123 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11124 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11125 		break;
11126 
11127 	default:
11128 		WARN_ON(1);
11129 		return -ENODATA;
11130 	}
11131 
11132 	if (is_kdump_kernel()) {
11133 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11134 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11135 	}
11136 
11137 	return 0;
11138 }
11139 
11140 /**
11141  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11142  * @vsi: VSI pointer
11143  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11144  *
11145  * On error: returns error code (negative)
11146  * On success: returns 0
11147  **/
11148 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11149 {
11150 	struct i40e_ring **next_rings;
11151 	int size;
11152 	int ret = 0;
11153 
11154 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11155 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11156 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11157 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11158 	if (!vsi->tx_rings)
11159 		return -ENOMEM;
11160 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11161 	if (i40e_enabled_xdp_vsi(vsi)) {
11162 		vsi->xdp_rings = next_rings;
11163 		next_rings += vsi->alloc_queue_pairs;
11164 	}
11165 	vsi->rx_rings = next_rings;
11166 
11167 	if (alloc_qvectors) {
11168 		/* allocate memory for q_vector pointers */
11169 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11170 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11171 		if (!vsi->q_vectors) {
11172 			ret = -ENOMEM;
11173 			goto err_vectors;
11174 		}
11175 	}
11176 	return ret;
11177 
11178 err_vectors:
11179 	kfree(vsi->tx_rings);
11180 	return ret;
11181 }
11182 
11183 /**
11184  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11185  * @pf: board private structure
11186  * @type: type of VSI
11187  *
11188  * On error: returns error code (negative)
11189  * On success: returns vsi index in PF (positive)
11190  **/
11191 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11192 {
11193 	int ret = -ENODEV;
11194 	struct i40e_vsi *vsi;
11195 	int vsi_idx;
11196 	int i;
11197 
11198 	/* Need to protect the allocation of the VSIs at the PF level */
11199 	mutex_lock(&pf->switch_mutex);
11200 
11201 	/* VSI list may be fragmented if VSI creation/destruction has
11202 	 * been happening.  We can afford to do a quick scan to look
11203 	 * for any free VSIs in the list.
11204 	 *
11205 	 * find next empty vsi slot, looping back around if necessary
11206 	 */
11207 	i = pf->next_vsi;
11208 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11209 		i++;
11210 	if (i >= pf->num_alloc_vsi) {
11211 		i = 0;
11212 		while (i < pf->next_vsi && pf->vsi[i])
11213 			i++;
11214 	}
11215 
11216 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11217 		vsi_idx = i;             /* Found one! */
11218 	} else {
11219 		ret = -ENODEV;
11220 		goto unlock_pf;  /* out of VSI slots! */
11221 	}
11222 	pf->next_vsi = ++i;
11223 
11224 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11225 	if (!vsi) {
11226 		ret = -ENOMEM;
11227 		goto unlock_pf;
11228 	}
11229 	vsi->type = type;
11230 	vsi->back = pf;
11231 	set_bit(__I40E_VSI_DOWN, vsi->state);
11232 	vsi->flags = 0;
11233 	vsi->idx = vsi_idx;
11234 	vsi->int_rate_limit = 0;
11235 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11236 				pf->rss_table_size : 64;
11237 	vsi->netdev_registered = false;
11238 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11239 	hash_init(vsi->mac_filter_hash);
11240 	vsi->irqs_ready = false;
11241 
11242 	if (type == I40E_VSI_MAIN) {
11243 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11244 		if (!vsi->af_xdp_zc_qps)
11245 			goto err_rings;
11246 	}
11247 
11248 	ret = i40e_set_num_rings_in_vsi(vsi);
11249 	if (ret)
11250 		goto err_rings;
11251 
11252 	ret = i40e_vsi_alloc_arrays(vsi, true);
11253 	if (ret)
11254 		goto err_rings;
11255 
11256 	/* Setup default MSIX irq handler for VSI */
11257 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11258 
11259 	/* Initialize VSI lock */
11260 	spin_lock_init(&vsi->mac_filter_hash_lock);
11261 	pf->vsi[vsi_idx] = vsi;
11262 	ret = vsi_idx;
11263 	goto unlock_pf;
11264 
11265 err_rings:
11266 	bitmap_free(vsi->af_xdp_zc_qps);
11267 	pf->next_vsi = i - 1;
11268 	kfree(vsi);
11269 unlock_pf:
11270 	mutex_unlock(&pf->switch_mutex);
11271 	return ret;
11272 }
11273 
11274 /**
11275  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11276  * @vsi: VSI pointer
11277  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11278  *
11279  * On error: returns error code (negative)
11280  * On success: returns 0
11281  **/
11282 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11283 {
11284 	/* free the ring and vector containers */
11285 	if (free_qvectors) {
11286 		kfree(vsi->q_vectors);
11287 		vsi->q_vectors = NULL;
11288 	}
11289 	kfree(vsi->tx_rings);
11290 	vsi->tx_rings = NULL;
11291 	vsi->rx_rings = NULL;
11292 	vsi->xdp_rings = NULL;
11293 }
11294 
11295 /**
11296  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11297  * and lookup table
11298  * @vsi: Pointer to VSI structure
11299  */
11300 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11301 {
11302 	if (!vsi)
11303 		return;
11304 
11305 	kfree(vsi->rss_hkey_user);
11306 	vsi->rss_hkey_user = NULL;
11307 
11308 	kfree(vsi->rss_lut_user);
11309 	vsi->rss_lut_user = NULL;
11310 }
11311 
11312 /**
11313  * i40e_vsi_clear - Deallocate the VSI provided
11314  * @vsi: the VSI being un-configured
11315  **/
11316 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11317 {
11318 	struct i40e_pf *pf;
11319 
11320 	if (!vsi)
11321 		return 0;
11322 
11323 	if (!vsi->back)
11324 		goto free_vsi;
11325 	pf = vsi->back;
11326 
11327 	mutex_lock(&pf->switch_mutex);
11328 	if (!pf->vsi[vsi->idx]) {
11329 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11330 			vsi->idx, vsi->idx, vsi->type);
11331 		goto unlock_vsi;
11332 	}
11333 
11334 	if (pf->vsi[vsi->idx] != vsi) {
11335 		dev_err(&pf->pdev->dev,
11336 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11337 			pf->vsi[vsi->idx]->idx,
11338 			pf->vsi[vsi->idx]->type,
11339 			vsi->idx, vsi->type);
11340 		goto unlock_vsi;
11341 	}
11342 
11343 	/* updates the PF for this cleared vsi */
11344 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11345 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11346 
11347 	bitmap_free(vsi->af_xdp_zc_qps);
11348 	i40e_vsi_free_arrays(vsi, true);
11349 	i40e_clear_rss_config_user(vsi);
11350 
11351 	pf->vsi[vsi->idx] = NULL;
11352 	if (vsi->idx < pf->next_vsi)
11353 		pf->next_vsi = vsi->idx;
11354 
11355 unlock_vsi:
11356 	mutex_unlock(&pf->switch_mutex);
11357 free_vsi:
11358 	kfree(vsi);
11359 
11360 	return 0;
11361 }
11362 
11363 /**
11364  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11365  * @vsi: the VSI being cleaned
11366  **/
11367 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11368 {
11369 	int i;
11370 
11371 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11372 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11373 			kfree_rcu(vsi->tx_rings[i], rcu);
11374 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11375 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11376 			if (vsi->xdp_rings)
11377 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11378 		}
11379 	}
11380 }
11381 
11382 /**
11383  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11384  * @vsi: the VSI being configured
11385  **/
11386 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11387 {
11388 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11389 	struct i40e_pf *pf = vsi->back;
11390 	struct i40e_ring *ring;
11391 
11392 	/* Set basic values in the rings to be used later during open() */
11393 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11394 		/* allocate space for both Tx and Rx in one shot */
11395 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11396 		if (!ring)
11397 			goto err_out;
11398 
11399 		ring->queue_index = i;
11400 		ring->reg_idx = vsi->base_queue + i;
11401 		ring->ring_active = false;
11402 		ring->vsi = vsi;
11403 		ring->netdev = vsi->netdev;
11404 		ring->dev = &pf->pdev->dev;
11405 		ring->count = vsi->num_tx_desc;
11406 		ring->size = 0;
11407 		ring->dcb_tc = 0;
11408 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11409 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11410 		ring->itr_setting = pf->tx_itr_default;
11411 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11412 
11413 		if (!i40e_enabled_xdp_vsi(vsi))
11414 			goto setup_rx;
11415 
11416 		ring->queue_index = vsi->alloc_queue_pairs + i;
11417 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11418 		ring->ring_active = false;
11419 		ring->vsi = vsi;
11420 		ring->netdev = NULL;
11421 		ring->dev = &pf->pdev->dev;
11422 		ring->count = vsi->num_tx_desc;
11423 		ring->size = 0;
11424 		ring->dcb_tc = 0;
11425 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11426 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11427 		set_ring_xdp(ring);
11428 		ring->itr_setting = pf->tx_itr_default;
11429 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11430 
11431 setup_rx:
11432 		ring->queue_index = i;
11433 		ring->reg_idx = vsi->base_queue + i;
11434 		ring->ring_active = false;
11435 		ring->vsi = vsi;
11436 		ring->netdev = vsi->netdev;
11437 		ring->dev = &pf->pdev->dev;
11438 		ring->count = vsi->num_rx_desc;
11439 		ring->size = 0;
11440 		ring->dcb_tc = 0;
11441 		ring->itr_setting = pf->rx_itr_default;
11442 		WRITE_ONCE(vsi->rx_rings[i], ring);
11443 	}
11444 
11445 	return 0;
11446 
11447 err_out:
11448 	i40e_vsi_clear_rings(vsi);
11449 	return -ENOMEM;
11450 }
11451 
11452 /**
11453  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11454  * @pf: board private structure
11455  * @vectors: the number of MSI-X vectors to request
11456  *
11457  * Returns the number of vectors reserved, or error
11458  **/
11459 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11460 {
11461 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11462 					I40E_MIN_MSIX, vectors);
11463 	if (vectors < 0) {
11464 		dev_info(&pf->pdev->dev,
11465 			 "MSI-X vector reservation failed: %d\n", vectors);
11466 		vectors = 0;
11467 	}
11468 
11469 	return vectors;
11470 }
11471 
11472 /**
11473  * i40e_init_msix - Setup the MSIX capability
11474  * @pf: board private structure
11475  *
11476  * Work with the OS to set up the MSIX vectors needed.
11477  *
11478  * Returns the number of vectors reserved or negative on failure
11479  **/
11480 static int i40e_init_msix(struct i40e_pf *pf)
11481 {
11482 	struct i40e_hw *hw = &pf->hw;
11483 	int cpus, extra_vectors;
11484 	int vectors_left;
11485 	int v_budget, i;
11486 	int v_actual;
11487 	int iwarp_requested = 0;
11488 
11489 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11490 		return -ENODEV;
11491 
11492 	/* The number of vectors we'll request will be comprised of:
11493 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11494 	 *   - The number of LAN queue pairs
11495 	 *	- Queues being used for RSS.
11496 	 *		We don't need as many as max_rss_size vectors.
11497 	 *		use rss_size instead in the calculation since that
11498 	 *		is governed by number of cpus in the system.
11499 	 *	- assumes symmetric Tx/Rx pairing
11500 	 *   - The number of VMDq pairs
11501 	 *   - The CPU count within the NUMA node if iWARP is enabled
11502 	 * Once we count this up, try the request.
11503 	 *
11504 	 * If we can't get what we want, we'll simplify to nearly nothing
11505 	 * and try again.  If that still fails, we punt.
11506 	 */
11507 	vectors_left = hw->func_caps.num_msix_vectors;
11508 	v_budget = 0;
11509 
11510 	/* reserve one vector for miscellaneous handler */
11511 	if (vectors_left) {
11512 		v_budget++;
11513 		vectors_left--;
11514 	}
11515 
11516 	/* reserve some vectors for the main PF traffic queues. Initially we
11517 	 * only reserve at most 50% of the available vectors, in the case that
11518 	 * the number of online CPUs is large. This ensures that we can enable
11519 	 * extra features as well. Once we've enabled the other features, we
11520 	 * will use any remaining vectors to reach as close as we can to the
11521 	 * number of online CPUs.
11522 	 */
11523 	cpus = num_online_cpus();
11524 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11525 	vectors_left -= pf->num_lan_msix;
11526 
11527 	/* reserve one vector for sideband flow director */
11528 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11529 		if (vectors_left) {
11530 			pf->num_fdsb_msix = 1;
11531 			v_budget++;
11532 			vectors_left--;
11533 		} else {
11534 			pf->num_fdsb_msix = 0;
11535 		}
11536 	}
11537 
11538 	/* can we reserve enough for iWARP? */
11539 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11540 		iwarp_requested = pf->num_iwarp_msix;
11541 
11542 		if (!vectors_left)
11543 			pf->num_iwarp_msix = 0;
11544 		else if (vectors_left < pf->num_iwarp_msix)
11545 			pf->num_iwarp_msix = 1;
11546 		v_budget += pf->num_iwarp_msix;
11547 		vectors_left -= pf->num_iwarp_msix;
11548 	}
11549 
11550 	/* any vectors left over go for VMDq support */
11551 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11552 		if (!vectors_left) {
11553 			pf->num_vmdq_msix = 0;
11554 			pf->num_vmdq_qps = 0;
11555 		} else {
11556 			int vmdq_vecs_wanted =
11557 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11558 			int vmdq_vecs =
11559 				min_t(int, vectors_left, vmdq_vecs_wanted);
11560 
11561 			/* if we're short on vectors for what's desired, we limit
11562 			 * the queues per vmdq.  If this is still more than are
11563 			 * available, the user will need to change the number of
11564 			 * queues/vectors used by the PF later with the ethtool
11565 			 * channels command
11566 			 */
11567 			if (vectors_left < vmdq_vecs_wanted) {
11568 				pf->num_vmdq_qps = 1;
11569 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11570 				vmdq_vecs = min_t(int,
11571 						  vectors_left,
11572 						  vmdq_vecs_wanted);
11573 			}
11574 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11575 
11576 			v_budget += vmdq_vecs;
11577 			vectors_left -= vmdq_vecs;
11578 		}
11579 	}
11580 
11581 	/* On systems with a large number of SMP cores, we previously limited
11582 	 * the number of vectors for num_lan_msix to be at most 50% of the
11583 	 * available vectors, to allow for other features. Now, we add back
11584 	 * the remaining vectors. However, we ensure that the total
11585 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11586 	 * calculate the number of vectors we can add without going over the
11587 	 * cap of CPUs. For systems with a small number of CPUs this will be
11588 	 * zero.
11589 	 */
11590 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11591 	pf->num_lan_msix += extra_vectors;
11592 	vectors_left -= extra_vectors;
11593 
11594 	WARN(vectors_left < 0,
11595 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11596 
11597 	v_budget += pf->num_lan_msix;
11598 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11599 				   GFP_KERNEL);
11600 	if (!pf->msix_entries)
11601 		return -ENOMEM;
11602 
11603 	for (i = 0; i < v_budget; i++)
11604 		pf->msix_entries[i].entry = i;
11605 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11606 
11607 	if (v_actual < I40E_MIN_MSIX) {
11608 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11609 		kfree(pf->msix_entries);
11610 		pf->msix_entries = NULL;
11611 		pci_disable_msix(pf->pdev);
11612 		return -ENODEV;
11613 
11614 	} else if (v_actual == I40E_MIN_MSIX) {
11615 		/* Adjust for minimal MSIX use */
11616 		pf->num_vmdq_vsis = 0;
11617 		pf->num_vmdq_qps = 0;
11618 		pf->num_lan_qps = 1;
11619 		pf->num_lan_msix = 1;
11620 
11621 	} else if (v_actual != v_budget) {
11622 		/* If we have limited resources, we will start with no vectors
11623 		 * for the special features and then allocate vectors to some
11624 		 * of these features based on the policy and at the end disable
11625 		 * the features that did not get any vectors.
11626 		 */
11627 		int vec;
11628 
11629 		dev_info(&pf->pdev->dev,
11630 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11631 			 v_actual, v_budget);
11632 		/* reserve the misc vector */
11633 		vec = v_actual - 1;
11634 
11635 		/* Scale vector usage down */
11636 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11637 		pf->num_vmdq_vsis = 1;
11638 		pf->num_vmdq_qps = 1;
11639 
11640 		/* partition out the remaining vectors */
11641 		switch (vec) {
11642 		case 2:
11643 			pf->num_lan_msix = 1;
11644 			break;
11645 		case 3:
11646 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11647 				pf->num_lan_msix = 1;
11648 				pf->num_iwarp_msix = 1;
11649 			} else {
11650 				pf->num_lan_msix = 2;
11651 			}
11652 			break;
11653 		default:
11654 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11655 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11656 						 iwarp_requested);
11657 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11658 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11659 			} else {
11660 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11661 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11662 			}
11663 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11664 				pf->num_fdsb_msix = 1;
11665 				vec--;
11666 			}
11667 			pf->num_lan_msix = min_t(int,
11668 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11669 							      pf->num_lan_msix);
11670 			pf->num_lan_qps = pf->num_lan_msix;
11671 			break;
11672 		}
11673 	}
11674 
11675 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11676 	    (pf->num_fdsb_msix == 0)) {
11677 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11678 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11679 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11680 	}
11681 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11682 	    (pf->num_vmdq_msix == 0)) {
11683 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11684 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11685 	}
11686 
11687 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11688 	    (pf->num_iwarp_msix == 0)) {
11689 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11690 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11691 	}
11692 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11693 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11694 		   pf->num_lan_msix,
11695 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11696 		   pf->num_fdsb_msix,
11697 		   pf->num_iwarp_msix);
11698 
11699 	return v_actual;
11700 }
11701 
11702 /**
11703  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11704  * @vsi: the VSI being configured
11705  * @v_idx: index of the vector in the vsi struct
11706  *
11707  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11708  **/
11709 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11710 {
11711 	struct i40e_q_vector *q_vector;
11712 
11713 	/* allocate q_vector */
11714 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11715 	if (!q_vector)
11716 		return -ENOMEM;
11717 
11718 	q_vector->vsi = vsi;
11719 	q_vector->v_idx = v_idx;
11720 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11721 
11722 	if (vsi->netdev)
11723 		netif_napi_add(vsi->netdev, &q_vector->napi,
11724 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
11725 
11726 	/* tie q_vector and vsi together */
11727 	vsi->q_vectors[v_idx] = q_vector;
11728 
11729 	return 0;
11730 }
11731 
11732 /**
11733  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11734  * @vsi: the VSI being configured
11735  *
11736  * We allocate one q_vector per queue interrupt.  If allocation fails we
11737  * return -ENOMEM.
11738  **/
11739 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11740 {
11741 	struct i40e_pf *pf = vsi->back;
11742 	int err, v_idx, num_q_vectors;
11743 
11744 	/* if not MSIX, give the one vector only to the LAN VSI */
11745 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11746 		num_q_vectors = vsi->num_q_vectors;
11747 	else if (vsi == pf->vsi[pf->lan_vsi])
11748 		num_q_vectors = 1;
11749 	else
11750 		return -EINVAL;
11751 
11752 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11753 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11754 		if (err)
11755 			goto err_out;
11756 	}
11757 
11758 	return 0;
11759 
11760 err_out:
11761 	while (v_idx--)
11762 		i40e_free_q_vector(vsi, v_idx);
11763 
11764 	return err;
11765 }
11766 
11767 /**
11768  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11769  * @pf: board private structure to initialize
11770  **/
11771 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11772 {
11773 	int vectors = 0;
11774 	ssize_t size;
11775 
11776 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11777 		vectors = i40e_init_msix(pf);
11778 		if (vectors < 0) {
11779 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11780 				       I40E_FLAG_IWARP_ENABLED	|
11781 				       I40E_FLAG_RSS_ENABLED	|
11782 				       I40E_FLAG_DCB_CAPABLE	|
11783 				       I40E_FLAG_DCB_ENABLED	|
11784 				       I40E_FLAG_SRIOV_ENABLED	|
11785 				       I40E_FLAG_FD_SB_ENABLED	|
11786 				       I40E_FLAG_FD_ATR_ENABLED	|
11787 				       I40E_FLAG_VMDQ_ENABLED);
11788 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11789 
11790 			/* rework the queue expectations without MSIX */
11791 			i40e_determine_queue_usage(pf);
11792 		}
11793 	}
11794 
11795 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11796 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
11797 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
11798 		vectors = pci_enable_msi(pf->pdev);
11799 		if (vectors < 0) {
11800 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
11801 				 vectors);
11802 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
11803 		}
11804 		vectors = 1;  /* one MSI or Legacy vector */
11805 	}
11806 
11807 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
11808 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
11809 
11810 	/* set up vector assignment tracking */
11811 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
11812 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
11813 	if (!pf->irq_pile)
11814 		return -ENOMEM;
11815 
11816 	pf->irq_pile->num_entries = vectors;
11817 
11818 	/* track first vector for misc interrupts, ignore return */
11819 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
11820 
11821 	return 0;
11822 }
11823 
11824 /**
11825  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
11826  * @pf: private board data structure
11827  *
11828  * Restore the interrupt scheme that was cleared when we suspended the
11829  * device. This should be called during resume to re-allocate the q_vectors
11830  * and reacquire IRQs.
11831  */
11832 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
11833 {
11834 	int err, i;
11835 
11836 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
11837 	 * scheme. We need to re-enabled them here in order to attempt to
11838 	 * re-acquire the MSI or MSI-X vectors
11839 	 */
11840 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
11841 
11842 	err = i40e_init_interrupt_scheme(pf);
11843 	if (err)
11844 		return err;
11845 
11846 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
11847 	 * rings together again.
11848 	 */
11849 	for (i = 0; i < pf->num_alloc_vsi; i++) {
11850 		if (pf->vsi[i]) {
11851 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
11852 			if (err)
11853 				goto err_unwind;
11854 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
11855 		}
11856 	}
11857 
11858 	err = i40e_setup_misc_vector(pf);
11859 	if (err)
11860 		goto err_unwind;
11861 
11862 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
11863 		i40e_client_update_msix_info(pf);
11864 
11865 	return 0;
11866 
11867 err_unwind:
11868 	while (i--) {
11869 		if (pf->vsi[i])
11870 			i40e_vsi_free_q_vectors(pf->vsi[i]);
11871 	}
11872 
11873 	return err;
11874 }
11875 
11876 /**
11877  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
11878  * non queue events in recovery mode
11879  * @pf: board private structure
11880  *
11881  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
11882  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
11883  * This is handled differently than in recovery mode since no Tx/Rx resources
11884  * are being allocated.
11885  **/
11886 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
11887 {
11888 	int err;
11889 
11890 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11891 		err = i40e_setup_misc_vector(pf);
11892 
11893 		if (err) {
11894 			dev_info(&pf->pdev->dev,
11895 				 "MSI-X misc vector request failed, error %d\n",
11896 				 err);
11897 			return err;
11898 		}
11899 	} else {
11900 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
11901 
11902 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
11903 				  pf->int_name, pf);
11904 
11905 		if (err) {
11906 			dev_info(&pf->pdev->dev,
11907 				 "MSI/legacy misc vector request failed, error %d\n",
11908 				 err);
11909 			return err;
11910 		}
11911 		i40e_enable_misc_int_causes(pf);
11912 		i40e_irq_dynamic_enable_icr0(pf);
11913 	}
11914 
11915 	return 0;
11916 }
11917 
11918 /**
11919  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
11920  * @pf: board private structure
11921  *
11922  * This sets up the handler for MSIX 0, which is used to manage the
11923  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
11924  * when in MSI or Legacy interrupt mode.
11925  **/
11926 static int i40e_setup_misc_vector(struct i40e_pf *pf)
11927 {
11928 	struct i40e_hw *hw = &pf->hw;
11929 	int err = 0;
11930 
11931 	/* Only request the IRQ once, the first time through. */
11932 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
11933 		err = request_irq(pf->msix_entries[0].vector,
11934 				  i40e_intr, 0, pf->int_name, pf);
11935 		if (err) {
11936 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
11937 			dev_info(&pf->pdev->dev,
11938 				 "request_irq for %s failed: %d\n",
11939 				 pf->int_name, err);
11940 			return -EFAULT;
11941 		}
11942 	}
11943 
11944 	i40e_enable_misc_int_causes(pf);
11945 
11946 	/* associate no queues to the misc vector */
11947 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
11948 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
11949 
11950 	i40e_flush(hw);
11951 
11952 	i40e_irq_dynamic_enable_icr0(pf);
11953 
11954 	return err;
11955 }
11956 
11957 /**
11958  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
11959  * @vsi: Pointer to vsi structure
11960  * @seed: Buffter to store the hash keys
11961  * @lut: Buffer to store the lookup table entries
11962  * @lut_size: Size of buffer to store the lookup table entries
11963  *
11964  * Return 0 on success, negative on failure
11965  */
11966 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
11967 			   u8 *lut, u16 lut_size)
11968 {
11969 	struct i40e_pf *pf = vsi->back;
11970 	struct i40e_hw *hw = &pf->hw;
11971 	int ret = 0;
11972 
11973 	if (seed) {
11974 		ret = i40e_aq_get_rss_key(hw, vsi->id,
11975 			(struct i40e_aqc_get_set_rss_key_data *)seed);
11976 		if (ret) {
11977 			dev_info(&pf->pdev->dev,
11978 				 "Cannot get RSS key, err %s aq_err %s\n",
11979 				 i40e_stat_str(&pf->hw, ret),
11980 				 i40e_aq_str(&pf->hw,
11981 					     pf->hw.aq.asq_last_status));
11982 			return ret;
11983 		}
11984 	}
11985 
11986 	if (lut) {
11987 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
11988 
11989 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
11990 		if (ret) {
11991 			dev_info(&pf->pdev->dev,
11992 				 "Cannot get RSS lut, err %s aq_err %s\n",
11993 				 i40e_stat_str(&pf->hw, ret),
11994 				 i40e_aq_str(&pf->hw,
11995 					     pf->hw.aq.asq_last_status));
11996 			return ret;
11997 		}
11998 	}
11999 
12000 	return ret;
12001 }
12002 
12003 /**
12004  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12005  * @vsi: Pointer to vsi structure
12006  * @seed: RSS hash seed
12007  * @lut: Lookup table
12008  * @lut_size: Lookup table size
12009  *
12010  * Returns 0 on success, negative on failure
12011  **/
12012 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12013 			       const u8 *lut, u16 lut_size)
12014 {
12015 	struct i40e_pf *pf = vsi->back;
12016 	struct i40e_hw *hw = &pf->hw;
12017 	u16 vf_id = vsi->vf_id;
12018 	u8 i;
12019 
12020 	/* Fill out hash function seed */
12021 	if (seed) {
12022 		u32 *seed_dw = (u32 *)seed;
12023 
12024 		if (vsi->type == I40E_VSI_MAIN) {
12025 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12026 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12027 		} else if (vsi->type == I40E_VSI_SRIOV) {
12028 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12029 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12030 		} else {
12031 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12032 		}
12033 	}
12034 
12035 	if (lut) {
12036 		u32 *lut_dw = (u32 *)lut;
12037 
12038 		if (vsi->type == I40E_VSI_MAIN) {
12039 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12040 				return -EINVAL;
12041 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12042 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12043 		} else if (vsi->type == I40E_VSI_SRIOV) {
12044 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12045 				return -EINVAL;
12046 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12047 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12048 		} else {
12049 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12050 		}
12051 	}
12052 	i40e_flush(hw);
12053 
12054 	return 0;
12055 }
12056 
12057 /**
12058  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12059  * @vsi: Pointer to VSI structure
12060  * @seed: Buffer to store the keys
12061  * @lut: Buffer to store the lookup table entries
12062  * @lut_size: Size of buffer to store the lookup table entries
12063  *
12064  * Returns 0 on success, negative on failure
12065  */
12066 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12067 			    u8 *lut, u16 lut_size)
12068 {
12069 	struct i40e_pf *pf = vsi->back;
12070 	struct i40e_hw *hw = &pf->hw;
12071 	u16 i;
12072 
12073 	if (seed) {
12074 		u32 *seed_dw = (u32 *)seed;
12075 
12076 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12077 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12078 	}
12079 	if (lut) {
12080 		u32 *lut_dw = (u32 *)lut;
12081 
12082 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12083 			return -EINVAL;
12084 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12085 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12086 	}
12087 
12088 	return 0;
12089 }
12090 
12091 /**
12092  * i40e_config_rss - Configure RSS keys and lut
12093  * @vsi: Pointer to VSI structure
12094  * @seed: RSS hash seed
12095  * @lut: Lookup table
12096  * @lut_size: Lookup table size
12097  *
12098  * Returns 0 on success, negative on failure
12099  */
12100 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12101 {
12102 	struct i40e_pf *pf = vsi->back;
12103 
12104 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12105 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12106 	else
12107 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12108 }
12109 
12110 /**
12111  * i40e_get_rss - Get RSS keys and lut
12112  * @vsi: Pointer to VSI structure
12113  * @seed: Buffer to store the keys
12114  * @lut: Buffer to store the lookup table entries
12115  * @lut_size: Size of buffer to store the lookup table entries
12116  *
12117  * Returns 0 on success, negative on failure
12118  */
12119 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12120 {
12121 	struct i40e_pf *pf = vsi->back;
12122 
12123 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12124 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12125 	else
12126 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12127 }
12128 
12129 /**
12130  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12131  * @pf: Pointer to board private structure
12132  * @lut: Lookup table
12133  * @rss_table_size: Lookup table size
12134  * @rss_size: Range of queue number for hashing
12135  */
12136 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12137 		       u16 rss_table_size, u16 rss_size)
12138 {
12139 	u16 i;
12140 
12141 	for (i = 0; i < rss_table_size; i++)
12142 		lut[i] = i % rss_size;
12143 }
12144 
12145 /**
12146  * i40e_pf_config_rss - Prepare for RSS if used
12147  * @pf: board private structure
12148  **/
12149 static int i40e_pf_config_rss(struct i40e_pf *pf)
12150 {
12151 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12152 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12153 	u8 *lut;
12154 	struct i40e_hw *hw = &pf->hw;
12155 	u32 reg_val;
12156 	u64 hena;
12157 	int ret;
12158 
12159 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12160 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12161 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12162 	hena |= i40e_pf_get_default_rss_hena(pf);
12163 
12164 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12165 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12166 
12167 	/* Determine the RSS table size based on the hardware capabilities */
12168 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12169 	reg_val = (pf->rss_table_size == 512) ?
12170 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12171 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12172 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12173 
12174 	/* Determine the RSS size of the VSI */
12175 	if (!vsi->rss_size) {
12176 		u16 qcount;
12177 		/* If the firmware does something weird during VSI init, we
12178 		 * could end up with zero TCs. Check for that to avoid
12179 		 * divide-by-zero. It probably won't pass traffic, but it also
12180 		 * won't panic.
12181 		 */
12182 		qcount = vsi->num_queue_pairs /
12183 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12184 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12185 	}
12186 	if (!vsi->rss_size)
12187 		return -EINVAL;
12188 
12189 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12190 	if (!lut)
12191 		return -ENOMEM;
12192 
12193 	/* Use user configured lut if there is one, otherwise use default */
12194 	if (vsi->rss_lut_user)
12195 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12196 	else
12197 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12198 
12199 	/* Use user configured hash key if there is one, otherwise
12200 	 * use default.
12201 	 */
12202 	if (vsi->rss_hkey_user)
12203 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12204 	else
12205 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12206 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12207 	kfree(lut);
12208 
12209 	return ret;
12210 }
12211 
12212 /**
12213  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12214  * @pf: board private structure
12215  * @queue_count: the requested queue count for rss.
12216  *
12217  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12218  * count which may be different from the requested queue count.
12219  * Note: expects to be called while under rtnl_lock()
12220  **/
12221 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12222 {
12223 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12224 	int new_rss_size;
12225 
12226 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12227 		return 0;
12228 
12229 	queue_count = min_t(int, queue_count, num_online_cpus());
12230 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12231 
12232 	if (queue_count != vsi->num_queue_pairs) {
12233 		u16 qcount;
12234 
12235 		vsi->req_queue_pairs = queue_count;
12236 		i40e_prep_for_reset(pf);
12237 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12238 			return pf->alloc_rss_size;
12239 
12240 		pf->alloc_rss_size = new_rss_size;
12241 
12242 		i40e_reset_and_rebuild(pf, true, true);
12243 
12244 		/* Discard the user configured hash keys and lut, if less
12245 		 * queues are enabled.
12246 		 */
12247 		if (queue_count < vsi->rss_size) {
12248 			i40e_clear_rss_config_user(vsi);
12249 			dev_dbg(&pf->pdev->dev,
12250 				"discard user configured hash keys and lut\n");
12251 		}
12252 
12253 		/* Reset vsi->rss_size, as number of enabled queues changed */
12254 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12255 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12256 
12257 		i40e_pf_config_rss(pf);
12258 	}
12259 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12260 		 vsi->req_queue_pairs, pf->rss_size_max);
12261 	return pf->alloc_rss_size;
12262 }
12263 
12264 /**
12265  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12266  * @pf: board private structure
12267  **/
12268 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
12269 {
12270 	i40e_status status;
12271 	bool min_valid, max_valid;
12272 	u32 max_bw, min_bw;
12273 
12274 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12275 					   &min_valid, &max_valid);
12276 
12277 	if (!status) {
12278 		if (min_valid)
12279 			pf->min_bw = min_bw;
12280 		if (max_valid)
12281 			pf->max_bw = max_bw;
12282 	}
12283 
12284 	return status;
12285 }
12286 
12287 /**
12288  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12289  * @pf: board private structure
12290  **/
12291 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
12292 {
12293 	struct i40e_aqc_configure_partition_bw_data bw_data;
12294 	i40e_status status;
12295 
12296 	memset(&bw_data, 0, sizeof(bw_data));
12297 
12298 	/* Set the valid bit for this PF */
12299 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12300 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12301 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12302 
12303 	/* Set the new bandwidths */
12304 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12305 
12306 	return status;
12307 }
12308 
12309 /**
12310  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12311  * @pf: board private structure
12312  **/
12313 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12314 {
12315 	/* Commit temporary BW setting to permanent NVM image */
12316 	enum i40e_admin_queue_err last_aq_status;
12317 	i40e_status ret;
12318 	u16 nvm_word;
12319 
12320 	if (pf->hw.partition_id != 1) {
12321 		dev_info(&pf->pdev->dev,
12322 			 "Commit BW only works on partition 1! This is partition %d",
12323 			 pf->hw.partition_id);
12324 		ret = I40E_NOT_SUPPORTED;
12325 		goto bw_commit_out;
12326 	}
12327 
12328 	/* Acquire NVM for read access */
12329 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12330 	last_aq_status = pf->hw.aq.asq_last_status;
12331 	if (ret) {
12332 		dev_info(&pf->pdev->dev,
12333 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
12334 			 i40e_stat_str(&pf->hw, ret),
12335 			 i40e_aq_str(&pf->hw, last_aq_status));
12336 		goto bw_commit_out;
12337 	}
12338 
12339 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12340 	ret = i40e_aq_read_nvm(&pf->hw,
12341 			       I40E_SR_NVM_CONTROL_WORD,
12342 			       0x10, sizeof(nvm_word), &nvm_word,
12343 			       false, NULL);
12344 	/* Save off last admin queue command status before releasing
12345 	 * the NVM
12346 	 */
12347 	last_aq_status = pf->hw.aq.asq_last_status;
12348 	i40e_release_nvm(&pf->hw);
12349 	if (ret) {
12350 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
12351 			 i40e_stat_str(&pf->hw, ret),
12352 			 i40e_aq_str(&pf->hw, last_aq_status));
12353 		goto bw_commit_out;
12354 	}
12355 
12356 	/* Wait a bit for NVM release to complete */
12357 	msleep(50);
12358 
12359 	/* Acquire NVM for write access */
12360 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12361 	last_aq_status = pf->hw.aq.asq_last_status;
12362 	if (ret) {
12363 		dev_info(&pf->pdev->dev,
12364 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
12365 			 i40e_stat_str(&pf->hw, ret),
12366 			 i40e_aq_str(&pf->hw, last_aq_status));
12367 		goto bw_commit_out;
12368 	}
12369 	/* Write it back out unchanged to initiate update NVM,
12370 	 * which will force a write of the shadow (alt) RAM to
12371 	 * the NVM - thus storing the bandwidth values permanently.
12372 	 */
12373 	ret = i40e_aq_update_nvm(&pf->hw,
12374 				 I40E_SR_NVM_CONTROL_WORD,
12375 				 0x10, sizeof(nvm_word),
12376 				 &nvm_word, true, 0, NULL);
12377 	/* Save off last admin queue command status before releasing
12378 	 * the NVM
12379 	 */
12380 	last_aq_status = pf->hw.aq.asq_last_status;
12381 	i40e_release_nvm(&pf->hw);
12382 	if (ret)
12383 		dev_info(&pf->pdev->dev,
12384 			 "BW settings NOT SAVED, err %s aq_err %s\n",
12385 			 i40e_stat_str(&pf->hw, ret),
12386 			 i40e_aq_str(&pf->hw, last_aq_status));
12387 bw_commit_out:
12388 
12389 	return ret;
12390 }
12391 
12392 /**
12393  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12394  * if total port shutdown feature is enabled for this PF
12395  * @pf: board private structure
12396  **/
12397 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12398 {
12399 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12400 #define I40E_FEATURES_ENABLE_PTR		0x2A
12401 #define I40E_CURRENT_SETTING_PTR		0x2B
12402 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12403 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12404 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12405 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12406 	i40e_status read_status = I40E_SUCCESS;
12407 	u16 sr_emp_sr_settings_ptr = 0;
12408 	u16 features_enable = 0;
12409 	u16 link_behavior = 0;
12410 	bool ret = false;
12411 
12412 	read_status = i40e_read_nvm_word(&pf->hw,
12413 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12414 					 &sr_emp_sr_settings_ptr);
12415 	if (read_status)
12416 		goto err_nvm;
12417 	read_status = i40e_read_nvm_word(&pf->hw,
12418 					 sr_emp_sr_settings_ptr +
12419 					 I40E_FEATURES_ENABLE_PTR,
12420 					 &features_enable);
12421 	if (read_status)
12422 		goto err_nvm;
12423 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12424 		read_status = i40e_read_nvm_module_data(&pf->hw,
12425 							I40E_SR_EMP_SR_SETTINGS_PTR,
12426 							I40E_CURRENT_SETTING_PTR,
12427 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12428 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12429 							&link_behavior);
12430 		if (read_status)
12431 			goto err_nvm;
12432 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12433 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12434 	}
12435 	return ret;
12436 
12437 err_nvm:
12438 	dev_warn(&pf->pdev->dev,
12439 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
12440 		 i40e_stat_str(&pf->hw, read_status));
12441 	return ret;
12442 }
12443 
12444 /**
12445  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12446  * @pf: board private structure to initialize
12447  *
12448  * i40e_sw_init initializes the Adapter private data structure.
12449  * Fields are initialized based on PCI device information and
12450  * OS network device settings (MTU size).
12451  **/
12452 static int i40e_sw_init(struct i40e_pf *pf)
12453 {
12454 	int err = 0;
12455 	int size;
12456 	u16 pow;
12457 
12458 	/* Set default capability flags */
12459 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12460 		    I40E_FLAG_MSI_ENABLED     |
12461 		    I40E_FLAG_MSIX_ENABLED;
12462 
12463 	/* Set default ITR */
12464 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12465 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12466 
12467 	/* Depending on PF configurations, it is possible that the RSS
12468 	 * maximum might end up larger than the available queues
12469 	 */
12470 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12471 	pf->alloc_rss_size = 1;
12472 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12473 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12474 				 pf->hw.func_caps.num_tx_qp);
12475 
12476 	/* find the next higher power-of-2 of num cpus */
12477 	pow = roundup_pow_of_two(num_online_cpus());
12478 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12479 
12480 	if (pf->hw.func_caps.rss) {
12481 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12482 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12483 					   num_online_cpus());
12484 	}
12485 
12486 	/* MFP mode enabled */
12487 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12488 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12489 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12490 		if (i40e_get_partition_bw_setting(pf)) {
12491 			dev_warn(&pf->pdev->dev,
12492 				 "Could not get partition bw settings\n");
12493 		} else {
12494 			dev_info(&pf->pdev->dev,
12495 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12496 				 pf->min_bw, pf->max_bw);
12497 
12498 			/* nudge the Tx scheduler */
12499 			i40e_set_partition_bw_setting(pf);
12500 		}
12501 	}
12502 
12503 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12504 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12505 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12506 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12507 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12508 		    pf->hw.num_partitions > 1)
12509 			dev_info(&pf->pdev->dev,
12510 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12511 		else
12512 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12513 		pf->fdir_pf_filter_count =
12514 				 pf->hw.func_caps.fd_filters_guaranteed;
12515 		pf->hw.fdir_shared_filter_count =
12516 				 pf->hw.func_caps.fd_filters_best_effort;
12517 	}
12518 
12519 	if (pf->hw.mac.type == I40E_MAC_X722) {
12520 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12521 				    I40E_HW_128_QP_RSS_CAPABLE |
12522 				    I40E_HW_ATR_EVICT_CAPABLE |
12523 				    I40E_HW_WB_ON_ITR_CAPABLE |
12524 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12525 				    I40E_HW_NO_PCI_LINK_CHECK |
12526 				    I40E_HW_USE_SET_LLDP_MIB |
12527 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12528 				    I40E_HW_PTP_L4_CAPABLE |
12529 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12530 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12531 
12532 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12533 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12534 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12535 			dev_warn(&pf->pdev->dev,
12536 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12537 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12538 		}
12539 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12540 		   ((pf->hw.aq.api_maj_ver == 1) &&
12541 		    (pf->hw.aq.api_min_ver > 4))) {
12542 		/* Supported in FW API version higher than 1.4 */
12543 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12544 	}
12545 
12546 	/* Enable HW ATR eviction if possible */
12547 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12548 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12549 
12550 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12551 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12552 	    (pf->hw.aq.fw_maj_ver < 4))) {
12553 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12554 		/* No DCB support  for FW < v4.33 */
12555 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12556 	}
12557 
12558 	/* Disable FW LLDP if FW < v4.3 */
12559 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12560 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12561 	    (pf->hw.aq.fw_maj_ver < 4)))
12562 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12563 
12564 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12565 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12566 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12567 	    (pf->hw.aq.fw_maj_ver >= 5)))
12568 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12569 
12570 	/* Enable PTP L4 if FW > v6.0 */
12571 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12572 	    pf->hw.aq.fw_maj_ver >= 6)
12573 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12574 
12575 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12576 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12577 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12578 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12579 	}
12580 
12581 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12582 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12583 		/* IWARP needs one extra vector for CQP just like MISC.*/
12584 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12585 	}
12586 	/* Stopping FW LLDP engine is supported on XL710 and X722
12587 	 * starting from FW versions determined in i40e_init_adminq.
12588 	 * Stopping the FW LLDP engine is not supported on XL710
12589 	 * if NPAR is functioning so unset this hw flag in this case.
12590 	 */
12591 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12592 	    pf->hw.func_caps.npar_enable &&
12593 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12594 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12595 
12596 #ifdef CONFIG_PCI_IOV
12597 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12598 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12599 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12600 		pf->num_req_vfs = min_t(int,
12601 					pf->hw.func_caps.num_vfs,
12602 					I40E_MAX_VF_COUNT);
12603 	}
12604 #endif /* CONFIG_PCI_IOV */
12605 	pf->eeprom_version = 0xDEAD;
12606 	pf->lan_veb = I40E_NO_VEB;
12607 	pf->lan_vsi = I40E_NO_VSI;
12608 
12609 	/* By default FW has this off for performance reasons */
12610 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12611 
12612 	/* set up queue assignment tracking */
12613 	size = sizeof(struct i40e_lump_tracking)
12614 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12615 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12616 	if (!pf->qp_pile) {
12617 		err = -ENOMEM;
12618 		goto sw_init_done;
12619 	}
12620 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12621 
12622 	pf->tx_timeout_recovery_level = 1;
12623 
12624 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12625 	    i40e_is_total_port_shutdown_enabled(pf)) {
12626 		/* Link down on close must be on when total port shutdown
12627 		 * is enabled for a given port
12628 		 */
12629 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12630 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12631 		dev_info(&pf->pdev->dev,
12632 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12633 	}
12634 	mutex_init(&pf->switch_mutex);
12635 
12636 sw_init_done:
12637 	return err;
12638 }
12639 
12640 /**
12641  * i40e_set_ntuple - set the ntuple feature flag and take action
12642  * @pf: board private structure to initialize
12643  * @features: the feature set that the stack is suggesting
12644  *
12645  * returns a bool to indicate if reset needs to happen
12646  **/
12647 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12648 {
12649 	bool need_reset = false;
12650 
12651 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12652 	 * the state changed, we need to reset.
12653 	 */
12654 	if (features & NETIF_F_NTUPLE) {
12655 		/* Enable filters and mark for reset */
12656 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12657 			need_reset = true;
12658 		/* enable FD_SB only if there is MSI-X vector and no cloud
12659 		 * filters exist
12660 		 */
12661 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12662 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12663 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12664 		}
12665 	} else {
12666 		/* turn off filters, mark for reset and clear SW filter list */
12667 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12668 			need_reset = true;
12669 			i40e_fdir_filter_exit(pf);
12670 		}
12671 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12672 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12673 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12674 
12675 		/* reset fd counters */
12676 		pf->fd_add_err = 0;
12677 		pf->fd_atr_cnt = 0;
12678 		/* if ATR was auto disabled it can be re-enabled. */
12679 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12680 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12681 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12682 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12683 	}
12684 	return need_reset;
12685 }
12686 
12687 /**
12688  * i40e_clear_rss_lut - clear the rx hash lookup table
12689  * @vsi: the VSI being configured
12690  **/
12691 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12692 {
12693 	struct i40e_pf *pf = vsi->back;
12694 	struct i40e_hw *hw = &pf->hw;
12695 	u16 vf_id = vsi->vf_id;
12696 	u8 i;
12697 
12698 	if (vsi->type == I40E_VSI_MAIN) {
12699 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12700 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12701 	} else if (vsi->type == I40E_VSI_SRIOV) {
12702 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12703 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12704 	} else {
12705 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12706 	}
12707 }
12708 
12709 /**
12710  * i40e_set_features - set the netdev feature flags
12711  * @netdev: ptr to the netdev being adjusted
12712  * @features: the feature set that the stack is suggesting
12713  * Note: expects to be called while under rtnl_lock()
12714  **/
12715 static int i40e_set_features(struct net_device *netdev,
12716 			     netdev_features_t features)
12717 {
12718 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12719 	struct i40e_vsi *vsi = np->vsi;
12720 	struct i40e_pf *pf = vsi->back;
12721 	bool need_reset;
12722 
12723 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12724 		i40e_pf_config_rss(pf);
12725 	else if (!(features & NETIF_F_RXHASH) &&
12726 		 netdev->features & NETIF_F_RXHASH)
12727 		i40e_clear_rss_lut(vsi);
12728 
12729 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12730 		i40e_vlan_stripping_enable(vsi);
12731 	else
12732 		i40e_vlan_stripping_disable(vsi);
12733 
12734 	if (!(features & NETIF_F_HW_TC) &&
12735 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12736 		dev_err(&pf->pdev->dev,
12737 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12738 		return -EINVAL;
12739 	}
12740 
12741 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12742 		i40e_del_all_macvlans(vsi);
12743 
12744 	need_reset = i40e_set_ntuple(pf, features);
12745 
12746 	if (need_reset)
12747 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12748 
12749 	return 0;
12750 }
12751 
12752 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12753 				    unsigned int table, unsigned int idx,
12754 				    struct udp_tunnel_info *ti)
12755 {
12756 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12757 	struct i40e_hw *hw = &np->vsi->back->hw;
12758 	u8 type, filter_index;
12759 	i40e_status ret;
12760 
12761 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12762 						   I40E_AQC_TUNNEL_TYPE_NGE;
12763 
12764 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12765 				     NULL);
12766 	if (ret) {
12767 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12768 			    i40e_stat_str(hw, ret),
12769 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12770 		return -EIO;
12771 	}
12772 
12773 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12774 	return 0;
12775 }
12776 
12777 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12778 				      unsigned int table, unsigned int idx,
12779 				      struct udp_tunnel_info *ti)
12780 {
12781 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12782 	struct i40e_hw *hw = &np->vsi->back->hw;
12783 	i40e_status ret;
12784 
12785 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12786 	if (ret) {
12787 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
12788 			    i40e_stat_str(hw, ret),
12789 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12790 		return -EIO;
12791 	}
12792 
12793 	return 0;
12794 }
12795 
12796 static int i40e_get_phys_port_id(struct net_device *netdev,
12797 				 struct netdev_phys_item_id *ppid)
12798 {
12799 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12800 	struct i40e_pf *pf = np->vsi->back;
12801 	struct i40e_hw *hw = &pf->hw;
12802 
12803 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
12804 		return -EOPNOTSUPP;
12805 
12806 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
12807 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
12808 
12809 	return 0;
12810 }
12811 
12812 /**
12813  * i40e_ndo_fdb_add - add an entry to the hardware database
12814  * @ndm: the input from the stack
12815  * @tb: pointer to array of nladdr (unused)
12816  * @dev: the net device pointer
12817  * @addr: the MAC address entry being added
12818  * @vid: VLAN ID
12819  * @flags: instructions from stack about fdb operation
12820  * @extack: netlink extended ack, unused currently
12821  */
12822 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
12823 			    struct net_device *dev,
12824 			    const unsigned char *addr, u16 vid,
12825 			    u16 flags,
12826 			    struct netlink_ext_ack *extack)
12827 {
12828 	struct i40e_netdev_priv *np = netdev_priv(dev);
12829 	struct i40e_pf *pf = np->vsi->back;
12830 	int err = 0;
12831 
12832 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
12833 		return -EOPNOTSUPP;
12834 
12835 	if (vid) {
12836 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
12837 		return -EINVAL;
12838 	}
12839 
12840 	/* Hardware does not support aging addresses so if a
12841 	 * ndm_state is given only allow permanent addresses
12842 	 */
12843 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
12844 		netdev_info(dev, "FDB only supports static addresses\n");
12845 		return -EINVAL;
12846 	}
12847 
12848 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
12849 		err = dev_uc_add_excl(dev, addr);
12850 	else if (is_multicast_ether_addr(addr))
12851 		err = dev_mc_add_excl(dev, addr);
12852 	else
12853 		err = -EINVAL;
12854 
12855 	/* Only return duplicate errors if NLM_F_EXCL is set */
12856 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
12857 		err = 0;
12858 
12859 	return err;
12860 }
12861 
12862 /**
12863  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
12864  * @dev: the netdev being configured
12865  * @nlh: RTNL message
12866  * @flags: bridge flags
12867  * @extack: netlink extended ack
12868  *
12869  * Inserts a new hardware bridge if not already created and
12870  * enables the bridging mode requested (VEB or VEPA). If the
12871  * hardware bridge has already been inserted and the request
12872  * is to change the mode then that requires a PF reset to
12873  * allow rebuild of the components with required hardware
12874  * bridge mode enabled.
12875  *
12876  * Note: expects to be called while under rtnl_lock()
12877  **/
12878 static int i40e_ndo_bridge_setlink(struct net_device *dev,
12879 				   struct nlmsghdr *nlh,
12880 				   u16 flags,
12881 				   struct netlink_ext_ack *extack)
12882 {
12883 	struct i40e_netdev_priv *np = netdev_priv(dev);
12884 	struct i40e_vsi *vsi = np->vsi;
12885 	struct i40e_pf *pf = vsi->back;
12886 	struct i40e_veb *veb = NULL;
12887 	struct nlattr *attr, *br_spec;
12888 	int i, rem;
12889 
12890 	/* Only for PF VSI for now */
12891 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12892 		return -EOPNOTSUPP;
12893 
12894 	/* Find the HW bridge for PF VSI */
12895 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12896 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12897 			veb = pf->veb[i];
12898 	}
12899 
12900 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
12901 
12902 	nla_for_each_nested(attr, br_spec, rem) {
12903 		__u16 mode;
12904 
12905 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
12906 			continue;
12907 
12908 		mode = nla_get_u16(attr);
12909 		if ((mode != BRIDGE_MODE_VEPA) &&
12910 		    (mode != BRIDGE_MODE_VEB))
12911 			return -EINVAL;
12912 
12913 		/* Insert a new HW bridge */
12914 		if (!veb) {
12915 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12916 					     vsi->tc_config.enabled_tc);
12917 			if (veb) {
12918 				veb->bridge_mode = mode;
12919 				i40e_config_bridge_mode(veb);
12920 			} else {
12921 				/* No Bridge HW offload available */
12922 				return -ENOENT;
12923 			}
12924 			break;
12925 		} else if (mode != veb->bridge_mode) {
12926 			/* Existing HW bridge but different mode needs reset */
12927 			veb->bridge_mode = mode;
12928 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
12929 			if (mode == BRIDGE_MODE_VEB)
12930 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
12931 			else
12932 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12933 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12934 			break;
12935 		}
12936 	}
12937 
12938 	return 0;
12939 }
12940 
12941 /**
12942  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
12943  * @skb: skb buff
12944  * @pid: process id
12945  * @seq: RTNL message seq #
12946  * @dev: the netdev being configured
12947  * @filter_mask: unused
12948  * @nlflags: netlink flags passed in
12949  *
12950  * Return the mode in which the hardware bridge is operating in
12951  * i.e VEB or VEPA.
12952  **/
12953 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
12954 				   struct net_device *dev,
12955 				   u32 __always_unused filter_mask,
12956 				   int nlflags)
12957 {
12958 	struct i40e_netdev_priv *np = netdev_priv(dev);
12959 	struct i40e_vsi *vsi = np->vsi;
12960 	struct i40e_pf *pf = vsi->back;
12961 	struct i40e_veb *veb = NULL;
12962 	int i;
12963 
12964 	/* Only for PF VSI for now */
12965 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12966 		return -EOPNOTSUPP;
12967 
12968 	/* Find the HW bridge for the PF VSI */
12969 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12970 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12971 			veb = pf->veb[i];
12972 	}
12973 
12974 	if (!veb)
12975 		return 0;
12976 
12977 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
12978 				       0, 0, nlflags, filter_mask, NULL);
12979 }
12980 
12981 /**
12982  * i40e_features_check - Validate encapsulated packet conforms to limits
12983  * @skb: skb buff
12984  * @dev: This physical port's netdev
12985  * @features: Offload features that the stack believes apply
12986  **/
12987 static netdev_features_t i40e_features_check(struct sk_buff *skb,
12988 					     struct net_device *dev,
12989 					     netdev_features_t features)
12990 {
12991 	size_t len;
12992 
12993 	/* No point in doing any of this if neither checksum nor GSO are
12994 	 * being requested for this frame.  We can rule out both by just
12995 	 * checking for CHECKSUM_PARTIAL
12996 	 */
12997 	if (skb->ip_summed != CHECKSUM_PARTIAL)
12998 		return features;
12999 
13000 	/* We cannot support GSO if the MSS is going to be less than
13001 	 * 64 bytes.  If it is then we need to drop support for GSO.
13002 	 */
13003 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13004 		features &= ~NETIF_F_GSO_MASK;
13005 
13006 	/* MACLEN can support at most 63 words */
13007 	len = skb_network_header(skb) - skb->data;
13008 	if (len & ~(63 * 2))
13009 		goto out_err;
13010 
13011 	/* IPLEN and EIPLEN can support at most 127 dwords */
13012 	len = skb_transport_header(skb) - skb_network_header(skb);
13013 	if (len & ~(127 * 4))
13014 		goto out_err;
13015 
13016 	if (skb->encapsulation) {
13017 		/* L4TUNLEN can support 127 words */
13018 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13019 		if (len & ~(127 * 2))
13020 			goto out_err;
13021 
13022 		/* IPLEN can support at most 127 dwords */
13023 		len = skb_inner_transport_header(skb) -
13024 		      skb_inner_network_header(skb);
13025 		if (len & ~(127 * 4))
13026 			goto out_err;
13027 	}
13028 
13029 	/* No need to validate L4LEN as TCP is the only protocol with a
13030 	 * a flexible value and we support all possible values supported
13031 	 * by TCP, which is at most 15 dwords
13032 	 */
13033 
13034 	return features;
13035 out_err:
13036 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13037 }
13038 
13039 /**
13040  * i40e_xdp_setup - add/remove an XDP program
13041  * @vsi: VSI to changed
13042  * @prog: XDP program
13043  * @extack: netlink extended ack
13044  **/
13045 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13046 			  struct netlink_ext_ack *extack)
13047 {
13048 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
13049 	struct i40e_pf *pf = vsi->back;
13050 	struct bpf_prog *old_prog;
13051 	bool need_reset;
13052 	int i;
13053 
13054 	/* Don't allow frames that span over multiple buffers */
13055 	if (frame_size > vsi->rx_buf_len) {
13056 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
13057 		return -EINVAL;
13058 	}
13059 
13060 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13061 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13062 
13063 	if (need_reset)
13064 		i40e_prep_for_reset(pf);
13065 
13066 	/* VSI shall be deleted in a moment, just return EINVAL */
13067 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13068 		return -EINVAL;
13069 
13070 	old_prog = xchg(&vsi->xdp_prog, prog);
13071 
13072 	if (need_reset) {
13073 		if (!prog)
13074 			/* Wait until ndo_xsk_wakeup completes. */
13075 			synchronize_rcu();
13076 		i40e_reset_and_rebuild(pf, true, true);
13077 	}
13078 
13079 	for (i = 0; i < vsi->num_queue_pairs; i++)
13080 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13081 
13082 	if (old_prog)
13083 		bpf_prog_put(old_prog);
13084 
13085 	/* Kick start the NAPI context if there is an AF_XDP socket open
13086 	 * on that queue id. This so that receiving will start.
13087 	 */
13088 	if (need_reset && prog)
13089 		for (i = 0; i < vsi->num_queue_pairs; i++)
13090 			if (vsi->xdp_rings[i]->xsk_pool)
13091 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13092 						      XDP_WAKEUP_RX);
13093 
13094 	return 0;
13095 }
13096 
13097 /**
13098  * i40e_enter_busy_conf - Enters busy config state
13099  * @vsi: vsi
13100  *
13101  * Returns 0 on success, <0 for failure.
13102  **/
13103 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13104 {
13105 	struct i40e_pf *pf = vsi->back;
13106 	int timeout = 50;
13107 
13108 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13109 		timeout--;
13110 		if (!timeout)
13111 			return -EBUSY;
13112 		usleep_range(1000, 2000);
13113 	}
13114 
13115 	return 0;
13116 }
13117 
13118 /**
13119  * i40e_exit_busy_conf - Exits busy config state
13120  * @vsi: vsi
13121  **/
13122 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13123 {
13124 	struct i40e_pf *pf = vsi->back;
13125 
13126 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13127 }
13128 
13129 /**
13130  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13131  * @vsi: vsi
13132  * @queue_pair: queue pair
13133  **/
13134 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13135 {
13136 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13137 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13138 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13139 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13140 	if (i40e_enabled_xdp_vsi(vsi)) {
13141 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13142 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13143 	}
13144 }
13145 
13146 /**
13147  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13148  * @vsi: vsi
13149  * @queue_pair: queue pair
13150  **/
13151 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13152 {
13153 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13154 	if (i40e_enabled_xdp_vsi(vsi)) {
13155 		/* Make sure that in-progress ndo_xdp_xmit calls are
13156 		 * completed.
13157 		 */
13158 		synchronize_rcu();
13159 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13160 	}
13161 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13162 }
13163 
13164 /**
13165  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13166  * @vsi: vsi
13167  * @queue_pair: queue pair
13168  * @enable: true for enable, false for disable
13169  **/
13170 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13171 					bool enable)
13172 {
13173 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13174 	struct i40e_q_vector *q_vector = rxr->q_vector;
13175 
13176 	if (!vsi->netdev)
13177 		return;
13178 
13179 	/* All rings in a qp belong to the same qvector. */
13180 	if (q_vector->rx.ring || q_vector->tx.ring) {
13181 		if (enable)
13182 			napi_enable(&q_vector->napi);
13183 		else
13184 			napi_disable(&q_vector->napi);
13185 	}
13186 }
13187 
13188 /**
13189  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13190  * @vsi: vsi
13191  * @queue_pair: queue pair
13192  * @enable: true for enable, false for disable
13193  *
13194  * Returns 0 on success, <0 on failure.
13195  **/
13196 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13197 					bool enable)
13198 {
13199 	struct i40e_pf *pf = vsi->back;
13200 	int pf_q, ret = 0;
13201 
13202 	pf_q = vsi->base_queue + queue_pair;
13203 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13204 				     false /*is xdp*/, enable);
13205 	if (ret) {
13206 		dev_info(&pf->pdev->dev,
13207 			 "VSI seid %d Tx ring %d %sable timeout\n",
13208 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13209 		return ret;
13210 	}
13211 
13212 	i40e_control_rx_q(pf, pf_q, enable);
13213 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13214 	if (ret) {
13215 		dev_info(&pf->pdev->dev,
13216 			 "VSI seid %d Rx ring %d %sable timeout\n",
13217 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13218 		return ret;
13219 	}
13220 
13221 	/* Due to HW errata, on Rx disable only, the register can
13222 	 * indicate done before it really is. Needs 50ms to be sure
13223 	 */
13224 	if (!enable)
13225 		mdelay(50);
13226 
13227 	if (!i40e_enabled_xdp_vsi(vsi))
13228 		return ret;
13229 
13230 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13231 				     pf_q + vsi->alloc_queue_pairs,
13232 				     true /*is xdp*/, enable);
13233 	if (ret) {
13234 		dev_info(&pf->pdev->dev,
13235 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13236 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13237 	}
13238 
13239 	return ret;
13240 }
13241 
13242 /**
13243  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13244  * @vsi: vsi
13245  * @queue_pair: queue_pair
13246  **/
13247 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13248 {
13249 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13250 	struct i40e_pf *pf = vsi->back;
13251 	struct i40e_hw *hw = &pf->hw;
13252 
13253 	/* All rings in a qp belong to the same qvector. */
13254 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13255 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13256 	else
13257 		i40e_irq_dynamic_enable_icr0(pf);
13258 
13259 	i40e_flush(hw);
13260 }
13261 
13262 /**
13263  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13264  * @vsi: vsi
13265  * @queue_pair: queue_pair
13266  **/
13267 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13268 {
13269 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13270 	struct i40e_pf *pf = vsi->back;
13271 	struct i40e_hw *hw = &pf->hw;
13272 
13273 	/* For simplicity, instead of removing the qp interrupt causes
13274 	 * from the interrupt linked list, we simply disable the interrupt, and
13275 	 * leave the list intact.
13276 	 *
13277 	 * All rings in a qp belong to the same qvector.
13278 	 */
13279 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13280 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13281 
13282 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13283 		i40e_flush(hw);
13284 		synchronize_irq(pf->msix_entries[intpf].vector);
13285 	} else {
13286 		/* Legacy and MSI mode - this stops all interrupt handling */
13287 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13288 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13289 		i40e_flush(hw);
13290 		synchronize_irq(pf->pdev->irq);
13291 	}
13292 }
13293 
13294 /**
13295  * i40e_queue_pair_disable - Disables a queue pair
13296  * @vsi: vsi
13297  * @queue_pair: queue pair
13298  *
13299  * Returns 0 on success, <0 on failure.
13300  **/
13301 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13302 {
13303 	int err;
13304 
13305 	err = i40e_enter_busy_conf(vsi);
13306 	if (err)
13307 		return err;
13308 
13309 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13310 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13311 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13312 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13313 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13314 
13315 	return err;
13316 }
13317 
13318 /**
13319  * i40e_queue_pair_enable - Enables a queue pair
13320  * @vsi: vsi
13321  * @queue_pair: queue pair
13322  *
13323  * Returns 0 on success, <0 on failure.
13324  **/
13325 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13326 {
13327 	int err;
13328 
13329 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13330 	if (err)
13331 		return err;
13332 
13333 	if (i40e_enabled_xdp_vsi(vsi)) {
13334 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13335 		if (err)
13336 			return err;
13337 	}
13338 
13339 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13340 	if (err)
13341 		return err;
13342 
13343 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13344 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13345 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13346 
13347 	i40e_exit_busy_conf(vsi);
13348 
13349 	return err;
13350 }
13351 
13352 /**
13353  * i40e_xdp - implements ndo_bpf for i40e
13354  * @dev: netdevice
13355  * @xdp: XDP command
13356  **/
13357 static int i40e_xdp(struct net_device *dev,
13358 		    struct netdev_bpf *xdp)
13359 {
13360 	struct i40e_netdev_priv *np = netdev_priv(dev);
13361 	struct i40e_vsi *vsi = np->vsi;
13362 
13363 	if (vsi->type != I40E_VSI_MAIN)
13364 		return -EINVAL;
13365 
13366 	switch (xdp->command) {
13367 	case XDP_SETUP_PROG:
13368 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13369 	case XDP_SETUP_XSK_POOL:
13370 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13371 					   xdp->xsk.queue_id);
13372 	default:
13373 		return -EINVAL;
13374 	}
13375 }
13376 
13377 static const struct net_device_ops i40e_netdev_ops = {
13378 	.ndo_open		= i40e_open,
13379 	.ndo_stop		= i40e_close,
13380 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13381 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13382 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13383 	.ndo_validate_addr	= eth_validate_addr,
13384 	.ndo_set_mac_address	= i40e_set_mac,
13385 	.ndo_change_mtu		= i40e_change_mtu,
13386 	.ndo_eth_ioctl		= i40e_ioctl,
13387 	.ndo_tx_timeout		= i40e_tx_timeout,
13388 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13389 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13390 #ifdef CONFIG_NET_POLL_CONTROLLER
13391 	.ndo_poll_controller	= i40e_netpoll,
13392 #endif
13393 	.ndo_setup_tc		= __i40e_setup_tc,
13394 	.ndo_select_queue	= i40e_lan_select_queue,
13395 	.ndo_set_features	= i40e_set_features,
13396 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13397 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13398 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13399 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13400 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13401 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13402 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13403 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13404 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13405 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13406 	.ndo_features_check	= i40e_features_check,
13407 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13408 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13409 	.ndo_bpf		= i40e_xdp,
13410 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13411 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13412 	.ndo_dfwd_add_station	= i40e_fwd_add,
13413 	.ndo_dfwd_del_station	= i40e_fwd_del,
13414 };
13415 
13416 /**
13417  * i40e_config_netdev - Setup the netdev flags
13418  * @vsi: the VSI being configured
13419  *
13420  * Returns 0 on success, negative value on failure
13421  **/
13422 static int i40e_config_netdev(struct i40e_vsi *vsi)
13423 {
13424 	struct i40e_pf *pf = vsi->back;
13425 	struct i40e_hw *hw = &pf->hw;
13426 	struct i40e_netdev_priv *np;
13427 	struct net_device *netdev;
13428 	u8 broadcast[ETH_ALEN];
13429 	u8 mac_addr[ETH_ALEN];
13430 	int etherdev_size;
13431 	netdev_features_t hw_enc_features;
13432 	netdev_features_t hw_features;
13433 
13434 	etherdev_size = sizeof(struct i40e_netdev_priv);
13435 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13436 	if (!netdev)
13437 		return -ENOMEM;
13438 
13439 	vsi->netdev = netdev;
13440 	np = netdev_priv(netdev);
13441 	np->vsi = vsi;
13442 
13443 	hw_enc_features = NETIF_F_SG			|
13444 			  NETIF_F_HW_CSUM		|
13445 			  NETIF_F_HIGHDMA		|
13446 			  NETIF_F_SOFT_FEATURES		|
13447 			  NETIF_F_TSO			|
13448 			  NETIF_F_TSO_ECN		|
13449 			  NETIF_F_TSO6			|
13450 			  NETIF_F_GSO_GRE		|
13451 			  NETIF_F_GSO_GRE_CSUM		|
13452 			  NETIF_F_GSO_PARTIAL		|
13453 			  NETIF_F_GSO_IPXIP4		|
13454 			  NETIF_F_GSO_IPXIP6		|
13455 			  NETIF_F_GSO_UDP_TUNNEL	|
13456 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13457 			  NETIF_F_GSO_UDP_L4		|
13458 			  NETIF_F_SCTP_CRC		|
13459 			  NETIF_F_RXHASH		|
13460 			  NETIF_F_RXCSUM		|
13461 			  0;
13462 
13463 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13464 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13465 
13466 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13467 
13468 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13469 
13470 	netdev->hw_enc_features |= hw_enc_features;
13471 
13472 	/* record features VLANs can make use of */
13473 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13474 
13475 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13476 				   NETIF_F_GSO_GRE_CSUM |	\
13477 				   NETIF_F_GSO_IPXIP4 |		\
13478 				   NETIF_F_GSO_IPXIP6 |		\
13479 				   NETIF_F_GSO_UDP_TUNNEL |	\
13480 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13481 
13482 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13483 	netdev->features |= NETIF_F_GSO_PARTIAL |
13484 			    I40E_GSO_PARTIAL_FEATURES;
13485 
13486 	netdev->mpls_features |= NETIF_F_SG;
13487 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13488 	netdev->mpls_features |= NETIF_F_TSO;
13489 	netdev->mpls_features |= NETIF_F_TSO6;
13490 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13491 
13492 	/* enable macvlan offloads */
13493 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13494 
13495 	hw_features = hw_enc_features		|
13496 		      NETIF_F_HW_VLAN_CTAG_TX	|
13497 		      NETIF_F_HW_VLAN_CTAG_RX;
13498 
13499 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13500 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13501 
13502 	netdev->hw_features |= hw_features;
13503 
13504 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13505 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13506 
13507 	netdev->features &= ~NETIF_F_HW_TC;
13508 
13509 	if (vsi->type == I40E_VSI_MAIN) {
13510 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13511 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13512 		/* The following steps are necessary for two reasons. First,
13513 		 * some older NVM configurations load a default MAC-VLAN
13514 		 * filter that will accept any tagged packet, and we want to
13515 		 * replace this with a normal filter. Additionally, it is
13516 		 * possible our MAC address was provided by the platform using
13517 		 * Open Firmware or similar.
13518 		 *
13519 		 * Thus, we need to remove the default filter and install one
13520 		 * specific to the MAC address.
13521 		 */
13522 		i40e_rm_default_mac_filter(vsi, mac_addr);
13523 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13524 		i40e_add_mac_filter(vsi, mac_addr);
13525 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13526 	} else {
13527 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13528 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13529 		 * the end, which is 4 bytes long, so force truncation of the
13530 		 * original name by IFNAMSIZ - 4
13531 		 */
13532 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13533 			 IFNAMSIZ - 4,
13534 			 pf->vsi[pf->lan_vsi]->netdev->name);
13535 		eth_random_addr(mac_addr);
13536 
13537 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13538 		i40e_add_mac_filter(vsi, mac_addr);
13539 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13540 	}
13541 
13542 	/* Add the broadcast filter so that we initially will receive
13543 	 * broadcast packets. Note that when a new VLAN is first added the
13544 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13545 	 * specific filters as part of transitioning into "vlan" operation.
13546 	 * When more VLANs are added, the driver will copy each existing MAC
13547 	 * filter and add it for the new VLAN.
13548 	 *
13549 	 * Broadcast filters are handled specially by
13550 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13551 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13552 	 * filter. The subtask will update the correct broadcast promiscuous
13553 	 * bits as VLANs become active or inactive.
13554 	 */
13555 	eth_broadcast_addr(broadcast);
13556 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13557 	i40e_add_mac_filter(vsi, broadcast);
13558 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13559 
13560 	eth_hw_addr_set(netdev, mac_addr);
13561 	ether_addr_copy(netdev->perm_addr, mac_addr);
13562 
13563 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13564 	netdev->neigh_priv_len = sizeof(u32) * 4;
13565 
13566 	netdev->priv_flags |= IFF_UNICAST_FLT;
13567 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13568 	/* Setup netdev TC information */
13569 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13570 
13571 	netdev->netdev_ops = &i40e_netdev_ops;
13572 	netdev->watchdog_timeo = 5 * HZ;
13573 	i40e_set_ethtool_ops(netdev);
13574 
13575 	/* MTU range: 68 - 9706 */
13576 	netdev->min_mtu = ETH_MIN_MTU;
13577 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13578 
13579 	return 0;
13580 }
13581 
13582 /**
13583  * i40e_vsi_delete - Delete a VSI from the switch
13584  * @vsi: the VSI being removed
13585  *
13586  * Returns 0 on success, negative value on failure
13587  **/
13588 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13589 {
13590 	/* remove default VSI is not allowed */
13591 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13592 		return;
13593 
13594 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13595 }
13596 
13597 /**
13598  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13599  * @vsi: the VSI being queried
13600  *
13601  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13602  **/
13603 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13604 {
13605 	struct i40e_veb *veb;
13606 	struct i40e_pf *pf = vsi->back;
13607 
13608 	/* Uplink is not a bridge so default to VEB */
13609 	if (vsi->veb_idx >= I40E_MAX_VEB)
13610 		return 1;
13611 
13612 	veb = pf->veb[vsi->veb_idx];
13613 	if (!veb) {
13614 		dev_info(&pf->pdev->dev,
13615 			 "There is no veb associated with the bridge\n");
13616 		return -ENOENT;
13617 	}
13618 
13619 	/* Uplink is a bridge in VEPA mode */
13620 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13621 		return 0;
13622 	} else {
13623 		/* Uplink is a bridge in VEB mode */
13624 		return 1;
13625 	}
13626 
13627 	/* VEPA is now default bridge, so return 0 */
13628 	return 0;
13629 }
13630 
13631 /**
13632  * i40e_add_vsi - Add a VSI to the switch
13633  * @vsi: the VSI being configured
13634  *
13635  * This initializes a VSI context depending on the VSI type to be added and
13636  * passes it down to the add_vsi aq command.
13637  **/
13638 static int i40e_add_vsi(struct i40e_vsi *vsi)
13639 {
13640 	int ret = -ENODEV;
13641 	struct i40e_pf *pf = vsi->back;
13642 	struct i40e_hw *hw = &pf->hw;
13643 	struct i40e_vsi_context ctxt;
13644 	struct i40e_mac_filter *f;
13645 	struct hlist_node *h;
13646 	int bkt;
13647 
13648 	u8 enabled_tc = 0x1; /* TC0 enabled */
13649 	int f_count = 0;
13650 
13651 	memset(&ctxt, 0, sizeof(ctxt));
13652 	switch (vsi->type) {
13653 	case I40E_VSI_MAIN:
13654 		/* The PF's main VSI is already setup as part of the
13655 		 * device initialization, so we'll not bother with
13656 		 * the add_vsi call, but we will retrieve the current
13657 		 * VSI context.
13658 		 */
13659 		ctxt.seid = pf->main_vsi_seid;
13660 		ctxt.pf_num = pf->hw.pf_id;
13661 		ctxt.vf_num = 0;
13662 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13663 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13664 		if (ret) {
13665 			dev_info(&pf->pdev->dev,
13666 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13667 				 i40e_stat_str(&pf->hw, ret),
13668 				 i40e_aq_str(&pf->hw,
13669 					     pf->hw.aq.asq_last_status));
13670 			return -ENOENT;
13671 		}
13672 		vsi->info = ctxt.info;
13673 		vsi->info.valid_sections = 0;
13674 
13675 		vsi->seid = ctxt.seid;
13676 		vsi->id = ctxt.vsi_number;
13677 
13678 		enabled_tc = i40e_pf_get_tc_map(pf);
13679 
13680 		/* Source pruning is enabled by default, so the flag is
13681 		 * negative logic - if it's set, we need to fiddle with
13682 		 * the VSI to disable source pruning.
13683 		 */
13684 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13685 			memset(&ctxt, 0, sizeof(ctxt));
13686 			ctxt.seid = pf->main_vsi_seid;
13687 			ctxt.pf_num = pf->hw.pf_id;
13688 			ctxt.vf_num = 0;
13689 			ctxt.info.valid_sections |=
13690 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13691 			ctxt.info.switch_id =
13692 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13693 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13694 			if (ret) {
13695 				dev_info(&pf->pdev->dev,
13696 					 "update vsi failed, err %s aq_err %s\n",
13697 					 i40e_stat_str(&pf->hw, ret),
13698 					 i40e_aq_str(&pf->hw,
13699 						     pf->hw.aq.asq_last_status));
13700 				ret = -ENOENT;
13701 				goto err;
13702 			}
13703 		}
13704 
13705 		/* MFP mode setup queue map and update VSI */
13706 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13707 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13708 			memset(&ctxt, 0, sizeof(ctxt));
13709 			ctxt.seid = pf->main_vsi_seid;
13710 			ctxt.pf_num = pf->hw.pf_id;
13711 			ctxt.vf_num = 0;
13712 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13713 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13714 			if (ret) {
13715 				dev_info(&pf->pdev->dev,
13716 					 "update vsi failed, err %s aq_err %s\n",
13717 					 i40e_stat_str(&pf->hw, ret),
13718 					 i40e_aq_str(&pf->hw,
13719 						    pf->hw.aq.asq_last_status));
13720 				ret = -ENOENT;
13721 				goto err;
13722 			}
13723 			/* update the local VSI info queue map */
13724 			i40e_vsi_update_queue_map(vsi, &ctxt);
13725 			vsi->info.valid_sections = 0;
13726 		} else {
13727 			/* Default/Main VSI is only enabled for TC0
13728 			 * reconfigure it to enable all TCs that are
13729 			 * available on the port in SFP mode.
13730 			 * For MFP case the iSCSI PF would use this
13731 			 * flow to enable LAN+iSCSI TC.
13732 			 */
13733 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13734 			if (ret) {
13735 				/* Single TC condition is not fatal,
13736 				 * message and continue
13737 				 */
13738 				dev_info(&pf->pdev->dev,
13739 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13740 					 enabled_tc,
13741 					 i40e_stat_str(&pf->hw, ret),
13742 					 i40e_aq_str(&pf->hw,
13743 						    pf->hw.aq.asq_last_status));
13744 			}
13745 		}
13746 		break;
13747 
13748 	case I40E_VSI_FDIR:
13749 		ctxt.pf_num = hw->pf_id;
13750 		ctxt.vf_num = 0;
13751 		ctxt.uplink_seid = vsi->uplink_seid;
13752 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13753 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13754 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13755 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13756 			ctxt.info.valid_sections |=
13757 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13758 			ctxt.info.switch_id =
13759 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13760 		}
13761 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13762 		break;
13763 
13764 	case I40E_VSI_VMDQ2:
13765 		ctxt.pf_num = hw->pf_id;
13766 		ctxt.vf_num = 0;
13767 		ctxt.uplink_seid = vsi->uplink_seid;
13768 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13769 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13770 
13771 		/* This VSI is connected to VEB so the switch_id
13772 		 * should be set to zero by default.
13773 		 */
13774 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13775 			ctxt.info.valid_sections |=
13776 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13777 			ctxt.info.switch_id =
13778 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13779 		}
13780 
13781 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13782 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13783 		break;
13784 
13785 	case I40E_VSI_SRIOV:
13786 		ctxt.pf_num = hw->pf_id;
13787 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13788 		ctxt.uplink_seid = vsi->uplink_seid;
13789 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13790 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13791 
13792 		/* This VSI is connected to VEB so the switch_id
13793 		 * should be set to zero by default.
13794 		 */
13795 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13796 			ctxt.info.valid_sections |=
13797 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13798 			ctxt.info.switch_id =
13799 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13800 		}
13801 
13802 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
13803 			ctxt.info.valid_sections |=
13804 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
13805 			ctxt.info.queueing_opt_flags |=
13806 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
13807 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
13808 		}
13809 
13810 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
13811 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
13812 		if (pf->vf[vsi->vf_id].spoofchk) {
13813 			ctxt.info.valid_sections |=
13814 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
13815 			ctxt.info.sec_flags |=
13816 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
13817 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
13818 		}
13819 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13820 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13821 		break;
13822 
13823 	case I40E_VSI_IWARP:
13824 		/* send down message to iWARP */
13825 		break;
13826 
13827 	default:
13828 		return -ENODEV;
13829 	}
13830 
13831 	if (vsi->type != I40E_VSI_MAIN) {
13832 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
13833 		if (ret) {
13834 			dev_info(&vsi->back->pdev->dev,
13835 				 "add vsi failed, err %s aq_err %s\n",
13836 				 i40e_stat_str(&pf->hw, ret),
13837 				 i40e_aq_str(&pf->hw,
13838 					     pf->hw.aq.asq_last_status));
13839 			ret = -ENOENT;
13840 			goto err;
13841 		}
13842 		vsi->info = ctxt.info;
13843 		vsi->info.valid_sections = 0;
13844 		vsi->seid = ctxt.seid;
13845 		vsi->id = ctxt.vsi_number;
13846 	}
13847 
13848 	vsi->active_filters = 0;
13849 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
13850 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13851 	/* If macvlan filters already exist, force them to get loaded */
13852 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
13853 		f->state = I40E_FILTER_NEW;
13854 		f_count++;
13855 	}
13856 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13857 
13858 	if (f_count) {
13859 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
13860 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
13861 	}
13862 
13863 	/* Update VSI BW information */
13864 	ret = i40e_vsi_get_bw_info(vsi);
13865 	if (ret) {
13866 		dev_info(&pf->pdev->dev,
13867 			 "couldn't get vsi bw info, err %s aq_err %s\n",
13868 			 i40e_stat_str(&pf->hw, ret),
13869 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13870 		/* VSI is already added so not tearing that up */
13871 		ret = 0;
13872 	}
13873 
13874 err:
13875 	return ret;
13876 }
13877 
13878 /**
13879  * i40e_vsi_release - Delete a VSI and free its resources
13880  * @vsi: the VSI being removed
13881  *
13882  * Returns 0 on success or < 0 on error
13883  **/
13884 int i40e_vsi_release(struct i40e_vsi *vsi)
13885 {
13886 	struct i40e_mac_filter *f;
13887 	struct hlist_node *h;
13888 	struct i40e_veb *veb = NULL;
13889 	struct i40e_pf *pf;
13890 	u16 uplink_seid;
13891 	int i, n, bkt;
13892 
13893 	pf = vsi->back;
13894 
13895 	/* release of a VEB-owner or last VSI is not allowed */
13896 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
13897 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
13898 			 vsi->seid, vsi->uplink_seid);
13899 		return -ENODEV;
13900 	}
13901 	if (vsi == pf->vsi[pf->lan_vsi] &&
13902 	    !test_bit(__I40E_DOWN, pf->state)) {
13903 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
13904 		return -ENODEV;
13905 	}
13906 	set_bit(__I40E_VSI_RELEASING, vsi->state);
13907 	uplink_seid = vsi->uplink_seid;
13908 	if (vsi->type != I40E_VSI_SRIOV) {
13909 		if (vsi->netdev_registered) {
13910 			vsi->netdev_registered = false;
13911 			if (vsi->netdev) {
13912 				/* results in a call to i40e_close() */
13913 				unregister_netdev(vsi->netdev);
13914 			}
13915 		} else {
13916 			i40e_vsi_close(vsi);
13917 		}
13918 		i40e_vsi_disable_irq(vsi);
13919 	}
13920 
13921 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13922 
13923 	/* clear the sync flag on all filters */
13924 	if (vsi->netdev) {
13925 		__dev_uc_unsync(vsi->netdev, NULL);
13926 		__dev_mc_unsync(vsi->netdev, NULL);
13927 	}
13928 
13929 	/* make sure any remaining filters are marked for deletion */
13930 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
13931 		__i40e_del_filter(vsi, f);
13932 
13933 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13934 
13935 	i40e_sync_vsi_filters(vsi);
13936 
13937 	i40e_vsi_delete(vsi);
13938 	i40e_vsi_free_q_vectors(vsi);
13939 	if (vsi->netdev) {
13940 		free_netdev(vsi->netdev);
13941 		vsi->netdev = NULL;
13942 	}
13943 	i40e_vsi_clear_rings(vsi);
13944 	i40e_vsi_clear(vsi);
13945 
13946 	/* If this was the last thing on the VEB, except for the
13947 	 * controlling VSI, remove the VEB, which puts the controlling
13948 	 * VSI onto the next level down in the switch.
13949 	 *
13950 	 * Well, okay, there's one more exception here: don't remove
13951 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
13952 	 * from up the network stack.
13953 	 */
13954 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
13955 		if (pf->vsi[i] &&
13956 		    pf->vsi[i]->uplink_seid == uplink_seid &&
13957 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13958 			n++;      /* count the VSIs */
13959 		}
13960 	}
13961 	for (i = 0; i < I40E_MAX_VEB; i++) {
13962 		if (!pf->veb[i])
13963 			continue;
13964 		if (pf->veb[i]->uplink_seid == uplink_seid)
13965 			n++;     /* count the VEBs */
13966 		if (pf->veb[i]->seid == uplink_seid)
13967 			veb = pf->veb[i];
13968 	}
13969 	if (n == 0 && veb && veb->uplink_seid != 0)
13970 		i40e_veb_release(veb);
13971 
13972 	return 0;
13973 }
13974 
13975 /**
13976  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
13977  * @vsi: ptr to the VSI
13978  *
13979  * This should only be called after i40e_vsi_mem_alloc() which allocates the
13980  * corresponding SW VSI structure and initializes num_queue_pairs for the
13981  * newly allocated VSI.
13982  *
13983  * Returns 0 on success or negative on failure
13984  **/
13985 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
13986 {
13987 	int ret = -ENOENT;
13988 	struct i40e_pf *pf = vsi->back;
13989 
13990 	if (vsi->q_vectors[0]) {
13991 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
13992 			 vsi->seid);
13993 		return -EEXIST;
13994 	}
13995 
13996 	if (vsi->base_vector) {
13997 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
13998 			 vsi->seid, vsi->base_vector);
13999 		return -EEXIST;
14000 	}
14001 
14002 	ret = i40e_vsi_alloc_q_vectors(vsi);
14003 	if (ret) {
14004 		dev_info(&pf->pdev->dev,
14005 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14006 			 vsi->num_q_vectors, vsi->seid, ret);
14007 		vsi->num_q_vectors = 0;
14008 		goto vector_setup_out;
14009 	}
14010 
14011 	/* In Legacy mode, we do not have to get any other vector since we
14012 	 * piggyback on the misc/ICR0 for queue interrupts.
14013 	*/
14014 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
14015 		return ret;
14016 	if (vsi->num_q_vectors)
14017 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14018 						 vsi->num_q_vectors, vsi->idx);
14019 	if (vsi->base_vector < 0) {
14020 		dev_info(&pf->pdev->dev,
14021 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14022 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14023 		i40e_vsi_free_q_vectors(vsi);
14024 		ret = -ENOENT;
14025 		goto vector_setup_out;
14026 	}
14027 
14028 vector_setup_out:
14029 	return ret;
14030 }
14031 
14032 /**
14033  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14034  * @vsi: pointer to the vsi.
14035  *
14036  * This re-allocates a vsi's queue resources.
14037  *
14038  * Returns pointer to the successfully allocated and configured VSI sw struct
14039  * on success, otherwise returns NULL on failure.
14040  **/
14041 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14042 {
14043 	u16 alloc_queue_pairs;
14044 	struct i40e_pf *pf;
14045 	u8 enabled_tc;
14046 	int ret;
14047 
14048 	if (!vsi)
14049 		return NULL;
14050 
14051 	pf = vsi->back;
14052 
14053 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14054 	i40e_vsi_clear_rings(vsi);
14055 
14056 	i40e_vsi_free_arrays(vsi, false);
14057 	i40e_set_num_rings_in_vsi(vsi);
14058 	ret = i40e_vsi_alloc_arrays(vsi, false);
14059 	if (ret)
14060 		goto err_vsi;
14061 
14062 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14063 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14064 
14065 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14066 	if (ret < 0) {
14067 		dev_info(&pf->pdev->dev,
14068 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14069 			 alloc_queue_pairs, vsi->seid, ret);
14070 		goto err_vsi;
14071 	}
14072 	vsi->base_queue = ret;
14073 
14074 	/* Update the FW view of the VSI. Force a reset of TC and queue
14075 	 * layout configurations.
14076 	 */
14077 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14078 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14079 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14080 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14081 	if (vsi->type == I40E_VSI_MAIN)
14082 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14083 
14084 	/* assign it some queues */
14085 	ret = i40e_alloc_rings(vsi);
14086 	if (ret)
14087 		goto err_rings;
14088 
14089 	/* map all of the rings to the q_vectors */
14090 	i40e_vsi_map_rings_to_vectors(vsi);
14091 	return vsi;
14092 
14093 err_rings:
14094 	i40e_vsi_free_q_vectors(vsi);
14095 	if (vsi->netdev_registered) {
14096 		vsi->netdev_registered = false;
14097 		unregister_netdev(vsi->netdev);
14098 		free_netdev(vsi->netdev);
14099 		vsi->netdev = NULL;
14100 	}
14101 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14102 err_vsi:
14103 	i40e_vsi_clear(vsi);
14104 	return NULL;
14105 }
14106 
14107 /**
14108  * i40e_vsi_setup - Set up a VSI by a given type
14109  * @pf: board private structure
14110  * @type: VSI type
14111  * @uplink_seid: the switch element to link to
14112  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14113  *
14114  * This allocates the sw VSI structure and its queue resources, then add a VSI
14115  * to the identified VEB.
14116  *
14117  * Returns pointer to the successfully allocated and configure VSI sw struct on
14118  * success, otherwise returns NULL on failure.
14119  **/
14120 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14121 				u16 uplink_seid, u32 param1)
14122 {
14123 	struct i40e_vsi *vsi = NULL;
14124 	struct i40e_veb *veb = NULL;
14125 	u16 alloc_queue_pairs;
14126 	int ret, i;
14127 	int v_idx;
14128 
14129 	/* The requested uplink_seid must be either
14130 	 *     - the PF's port seid
14131 	 *              no VEB is needed because this is the PF
14132 	 *              or this is a Flow Director special case VSI
14133 	 *     - seid of an existing VEB
14134 	 *     - seid of a VSI that owns an existing VEB
14135 	 *     - seid of a VSI that doesn't own a VEB
14136 	 *              a new VEB is created and the VSI becomes the owner
14137 	 *     - seid of the PF VSI, which is what creates the first VEB
14138 	 *              this is a special case of the previous
14139 	 *
14140 	 * Find which uplink_seid we were given and create a new VEB if needed
14141 	 */
14142 	for (i = 0; i < I40E_MAX_VEB; i++) {
14143 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14144 			veb = pf->veb[i];
14145 			break;
14146 		}
14147 	}
14148 
14149 	if (!veb && uplink_seid != pf->mac_seid) {
14150 
14151 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14152 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14153 				vsi = pf->vsi[i];
14154 				break;
14155 			}
14156 		}
14157 		if (!vsi) {
14158 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14159 				 uplink_seid);
14160 			return NULL;
14161 		}
14162 
14163 		if (vsi->uplink_seid == pf->mac_seid)
14164 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14165 					     vsi->tc_config.enabled_tc);
14166 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14167 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14168 					     vsi->tc_config.enabled_tc);
14169 		if (veb) {
14170 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14171 				dev_info(&vsi->back->pdev->dev,
14172 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14173 				return NULL;
14174 			}
14175 			/* We come up by default in VEPA mode if SRIOV is not
14176 			 * already enabled, in which case we can't force VEPA
14177 			 * mode.
14178 			 */
14179 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14180 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14181 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14182 			}
14183 			i40e_config_bridge_mode(veb);
14184 		}
14185 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14186 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14187 				veb = pf->veb[i];
14188 		}
14189 		if (!veb) {
14190 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14191 			return NULL;
14192 		}
14193 
14194 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14195 		uplink_seid = veb->seid;
14196 	}
14197 
14198 	/* get vsi sw struct */
14199 	v_idx = i40e_vsi_mem_alloc(pf, type);
14200 	if (v_idx < 0)
14201 		goto err_alloc;
14202 	vsi = pf->vsi[v_idx];
14203 	if (!vsi)
14204 		goto err_alloc;
14205 	vsi->type = type;
14206 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14207 
14208 	if (type == I40E_VSI_MAIN)
14209 		pf->lan_vsi = v_idx;
14210 	else if (type == I40E_VSI_SRIOV)
14211 		vsi->vf_id = param1;
14212 	/* assign it some queues */
14213 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14214 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14215 
14216 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14217 	if (ret < 0) {
14218 		dev_info(&pf->pdev->dev,
14219 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14220 			 alloc_queue_pairs, vsi->seid, ret);
14221 		goto err_vsi;
14222 	}
14223 	vsi->base_queue = ret;
14224 
14225 	/* get a VSI from the hardware */
14226 	vsi->uplink_seid = uplink_seid;
14227 	ret = i40e_add_vsi(vsi);
14228 	if (ret)
14229 		goto err_vsi;
14230 
14231 	switch (vsi->type) {
14232 	/* setup the netdev if needed */
14233 	case I40E_VSI_MAIN:
14234 	case I40E_VSI_VMDQ2:
14235 		ret = i40e_config_netdev(vsi);
14236 		if (ret)
14237 			goto err_netdev;
14238 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14239 		if (ret)
14240 			goto err_netdev;
14241 		ret = register_netdev(vsi->netdev);
14242 		if (ret)
14243 			goto err_netdev;
14244 		vsi->netdev_registered = true;
14245 		netif_carrier_off(vsi->netdev);
14246 #ifdef CONFIG_I40E_DCB
14247 		/* Setup DCB netlink interface */
14248 		i40e_dcbnl_setup(vsi);
14249 #endif /* CONFIG_I40E_DCB */
14250 		fallthrough;
14251 	case I40E_VSI_FDIR:
14252 		/* set up vectors and rings if needed */
14253 		ret = i40e_vsi_setup_vectors(vsi);
14254 		if (ret)
14255 			goto err_msix;
14256 
14257 		ret = i40e_alloc_rings(vsi);
14258 		if (ret)
14259 			goto err_rings;
14260 
14261 		/* map all of the rings to the q_vectors */
14262 		i40e_vsi_map_rings_to_vectors(vsi);
14263 
14264 		i40e_vsi_reset_stats(vsi);
14265 		break;
14266 	default:
14267 		/* no netdev or rings for the other VSI types */
14268 		break;
14269 	}
14270 
14271 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14272 	    (vsi->type == I40E_VSI_VMDQ2)) {
14273 		ret = i40e_vsi_config_rss(vsi);
14274 	}
14275 	return vsi;
14276 
14277 err_rings:
14278 	i40e_vsi_free_q_vectors(vsi);
14279 err_msix:
14280 	if (vsi->netdev_registered) {
14281 		vsi->netdev_registered = false;
14282 		unregister_netdev(vsi->netdev);
14283 		free_netdev(vsi->netdev);
14284 		vsi->netdev = NULL;
14285 	}
14286 err_netdev:
14287 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14288 err_vsi:
14289 	i40e_vsi_clear(vsi);
14290 err_alloc:
14291 	return NULL;
14292 }
14293 
14294 /**
14295  * i40e_veb_get_bw_info - Query VEB BW information
14296  * @veb: the veb to query
14297  *
14298  * Query the Tx scheduler BW configuration data for given VEB
14299  **/
14300 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14301 {
14302 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14303 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14304 	struct i40e_pf *pf = veb->pf;
14305 	struct i40e_hw *hw = &pf->hw;
14306 	u32 tc_bw_max;
14307 	int ret = 0;
14308 	int i;
14309 
14310 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14311 						  &bw_data, NULL);
14312 	if (ret) {
14313 		dev_info(&pf->pdev->dev,
14314 			 "query veb bw config failed, err %s aq_err %s\n",
14315 			 i40e_stat_str(&pf->hw, ret),
14316 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14317 		goto out;
14318 	}
14319 
14320 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14321 						   &ets_data, NULL);
14322 	if (ret) {
14323 		dev_info(&pf->pdev->dev,
14324 			 "query veb bw ets config failed, err %s aq_err %s\n",
14325 			 i40e_stat_str(&pf->hw, ret),
14326 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14327 		goto out;
14328 	}
14329 
14330 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14331 	veb->bw_max_quanta = ets_data.tc_bw_max;
14332 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14333 	veb->enabled_tc = ets_data.tc_valid_bits;
14334 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14335 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14336 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14337 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14338 		veb->bw_tc_limit_credits[i] =
14339 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14340 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14341 	}
14342 
14343 out:
14344 	return ret;
14345 }
14346 
14347 /**
14348  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14349  * @pf: board private structure
14350  *
14351  * On error: returns error code (negative)
14352  * On success: returns vsi index in PF (positive)
14353  **/
14354 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14355 {
14356 	int ret = -ENOENT;
14357 	struct i40e_veb *veb;
14358 	int i;
14359 
14360 	/* Need to protect the allocation of switch elements at the PF level */
14361 	mutex_lock(&pf->switch_mutex);
14362 
14363 	/* VEB list may be fragmented if VEB creation/destruction has
14364 	 * been happening.  We can afford to do a quick scan to look
14365 	 * for any free slots in the list.
14366 	 *
14367 	 * find next empty veb slot, looping back around if necessary
14368 	 */
14369 	i = 0;
14370 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14371 		i++;
14372 	if (i >= I40E_MAX_VEB) {
14373 		ret = -ENOMEM;
14374 		goto err_alloc_veb;  /* out of VEB slots! */
14375 	}
14376 
14377 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14378 	if (!veb) {
14379 		ret = -ENOMEM;
14380 		goto err_alloc_veb;
14381 	}
14382 	veb->pf = pf;
14383 	veb->idx = i;
14384 	veb->enabled_tc = 1;
14385 
14386 	pf->veb[i] = veb;
14387 	ret = i;
14388 err_alloc_veb:
14389 	mutex_unlock(&pf->switch_mutex);
14390 	return ret;
14391 }
14392 
14393 /**
14394  * i40e_switch_branch_release - Delete a branch of the switch tree
14395  * @branch: where to start deleting
14396  *
14397  * This uses recursion to find the tips of the branch to be
14398  * removed, deleting until we get back to and can delete this VEB.
14399  **/
14400 static void i40e_switch_branch_release(struct i40e_veb *branch)
14401 {
14402 	struct i40e_pf *pf = branch->pf;
14403 	u16 branch_seid = branch->seid;
14404 	u16 veb_idx = branch->idx;
14405 	int i;
14406 
14407 	/* release any VEBs on this VEB - RECURSION */
14408 	for (i = 0; i < I40E_MAX_VEB; i++) {
14409 		if (!pf->veb[i])
14410 			continue;
14411 		if (pf->veb[i]->uplink_seid == branch->seid)
14412 			i40e_switch_branch_release(pf->veb[i]);
14413 	}
14414 
14415 	/* Release the VSIs on this VEB, but not the owner VSI.
14416 	 *
14417 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14418 	 *       the VEB itself, so don't use (*branch) after this loop.
14419 	 */
14420 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14421 		if (!pf->vsi[i])
14422 			continue;
14423 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14424 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14425 			i40e_vsi_release(pf->vsi[i]);
14426 		}
14427 	}
14428 
14429 	/* There's one corner case where the VEB might not have been
14430 	 * removed, so double check it here and remove it if needed.
14431 	 * This case happens if the veb was created from the debugfs
14432 	 * commands and no VSIs were added to it.
14433 	 */
14434 	if (pf->veb[veb_idx])
14435 		i40e_veb_release(pf->veb[veb_idx]);
14436 }
14437 
14438 /**
14439  * i40e_veb_clear - remove veb struct
14440  * @veb: the veb to remove
14441  **/
14442 static void i40e_veb_clear(struct i40e_veb *veb)
14443 {
14444 	if (!veb)
14445 		return;
14446 
14447 	if (veb->pf) {
14448 		struct i40e_pf *pf = veb->pf;
14449 
14450 		mutex_lock(&pf->switch_mutex);
14451 		if (pf->veb[veb->idx] == veb)
14452 			pf->veb[veb->idx] = NULL;
14453 		mutex_unlock(&pf->switch_mutex);
14454 	}
14455 
14456 	kfree(veb);
14457 }
14458 
14459 /**
14460  * i40e_veb_release - Delete a VEB and free its resources
14461  * @veb: the VEB being removed
14462  **/
14463 void i40e_veb_release(struct i40e_veb *veb)
14464 {
14465 	struct i40e_vsi *vsi = NULL;
14466 	struct i40e_pf *pf;
14467 	int i, n = 0;
14468 
14469 	pf = veb->pf;
14470 
14471 	/* find the remaining VSI and check for extras */
14472 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14473 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14474 			n++;
14475 			vsi = pf->vsi[i];
14476 		}
14477 	}
14478 	if (n != 1) {
14479 		dev_info(&pf->pdev->dev,
14480 			 "can't remove VEB %d with %d VSIs left\n",
14481 			 veb->seid, n);
14482 		return;
14483 	}
14484 
14485 	/* move the remaining VSI to uplink veb */
14486 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14487 	if (veb->uplink_seid) {
14488 		vsi->uplink_seid = veb->uplink_seid;
14489 		if (veb->uplink_seid == pf->mac_seid)
14490 			vsi->veb_idx = I40E_NO_VEB;
14491 		else
14492 			vsi->veb_idx = veb->veb_idx;
14493 	} else {
14494 		/* floating VEB */
14495 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14496 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14497 	}
14498 
14499 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14500 	i40e_veb_clear(veb);
14501 }
14502 
14503 /**
14504  * i40e_add_veb - create the VEB in the switch
14505  * @veb: the VEB to be instantiated
14506  * @vsi: the controlling VSI
14507  **/
14508 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14509 {
14510 	struct i40e_pf *pf = veb->pf;
14511 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14512 	int ret;
14513 
14514 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14515 			      veb->enabled_tc, false,
14516 			      &veb->seid, enable_stats, NULL);
14517 
14518 	/* get a VEB from the hardware */
14519 	if (ret) {
14520 		dev_info(&pf->pdev->dev,
14521 			 "couldn't add VEB, 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 
14527 	/* get statistics counter */
14528 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14529 					 &veb->stats_idx, NULL, NULL, NULL);
14530 	if (ret) {
14531 		dev_info(&pf->pdev->dev,
14532 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
14533 			 i40e_stat_str(&pf->hw, ret),
14534 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14535 		return -EPERM;
14536 	}
14537 	ret = i40e_veb_get_bw_info(veb);
14538 	if (ret) {
14539 		dev_info(&pf->pdev->dev,
14540 			 "couldn't get VEB bw info, err %s aq_err %s\n",
14541 			 i40e_stat_str(&pf->hw, ret),
14542 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14543 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14544 		return -ENOENT;
14545 	}
14546 
14547 	vsi->uplink_seid = veb->seid;
14548 	vsi->veb_idx = veb->idx;
14549 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14550 
14551 	return 0;
14552 }
14553 
14554 /**
14555  * i40e_veb_setup - Set up a VEB
14556  * @pf: board private structure
14557  * @flags: VEB setup flags
14558  * @uplink_seid: the switch element to link to
14559  * @vsi_seid: the initial VSI seid
14560  * @enabled_tc: Enabled TC bit-map
14561  *
14562  * This allocates the sw VEB structure and links it into the switch
14563  * It is possible and legal for this to be a duplicate of an already
14564  * existing VEB.  It is also possible for both uplink and vsi seids
14565  * to be zero, in order to create a floating VEB.
14566  *
14567  * Returns pointer to the successfully allocated VEB sw struct on
14568  * success, otherwise returns NULL on failure.
14569  **/
14570 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14571 				u16 uplink_seid, u16 vsi_seid,
14572 				u8 enabled_tc)
14573 {
14574 	struct i40e_veb *veb, *uplink_veb = NULL;
14575 	int vsi_idx, veb_idx;
14576 	int ret;
14577 
14578 	/* if one seid is 0, the other must be 0 to create a floating relay */
14579 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14580 	    (uplink_seid + vsi_seid != 0)) {
14581 		dev_info(&pf->pdev->dev,
14582 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14583 			 uplink_seid, vsi_seid);
14584 		return NULL;
14585 	}
14586 
14587 	/* make sure there is such a vsi and uplink */
14588 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14589 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14590 			break;
14591 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14592 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14593 			 vsi_seid);
14594 		return NULL;
14595 	}
14596 
14597 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14598 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14599 			if (pf->veb[veb_idx] &&
14600 			    pf->veb[veb_idx]->seid == uplink_seid) {
14601 				uplink_veb = pf->veb[veb_idx];
14602 				break;
14603 			}
14604 		}
14605 		if (!uplink_veb) {
14606 			dev_info(&pf->pdev->dev,
14607 				 "uplink seid %d not found\n", uplink_seid);
14608 			return NULL;
14609 		}
14610 	}
14611 
14612 	/* get veb sw struct */
14613 	veb_idx = i40e_veb_mem_alloc(pf);
14614 	if (veb_idx < 0)
14615 		goto err_alloc;
14616 	veb = pf->veb[veb_idx];
14617 	veb->flags = flags;
14618 	veb->uplink_seid = uplink_seid;
14619 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14620 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14621 
14622 	/* create the VEB in the switch */
14623 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14624 	if (ret)
14625 		goto err_veb;
14626 	if (vsi_idx == pf->lan_vsi)
14627 		pf->lan_veb = veb->idx;
14628 
14629 	return veb;
14630 
14631 err_veb:
14632 	i40e_veb_clear(veb);
14633 err_alloc:
14634 	return NULL;
14635 }
14636 
14637 /**
14638  * i40e_setup_pf_switch_element - set PF vars based on switch type
14639  * @pf: board private structure
14640  * @ele: element we are building info from
14641  * @num_reported: total number of elements
14642  * @printconfig: should we print the contents
14643  *
14644  * helper function to assist in extracting a few useful SEID values.
14645  **/
14646 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14647 				struct i40e_aqc_switch_config_element_resp *ele,
14648 				u16 num_reported, bool printconfig)
14649 {
14650 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14651 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14652 	u8 element_type = ele->element_type;
14653 	u16 seid = le16_to_cpu(ele->seid);
14654 
14655 	if (printconfig)
14656 		dev_info(&pf->pdev->dev,
14657 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14658 			 element_type, seid, uplink_seid, downlink_seid);
14659 
14660 	switch (element_type) {
14661 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14662 		pf->mac_seid = seid;
14663 		break;
14664 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14665 		/* Main VEB? */
14666 		if (uplink_seid != pf->mac_seid)
14667 			break;
14668 		if (pf->lan_veb >= I40E_MAX_VEB) {
14669 			int v;
14670 
14671 			/* find existing or else empty VEB */
14672 			for (v = 0; v < I40E_MAX_VEB; v++) {
14673 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14674 					pf->lan_veb = v;
14675 					break;
14676 				}
14677 			}
14678 			if (pf->lan_veb >= I40E_MAX_VEB) {
14679 				v = i40e_veb_mem_alloc(pf);
14680 				if (v < 0)
14681 					break;
14682 				pf->lan_veb = v;
14683 			}
14684 		}
14685 		if (pf->lan_veb >= I40E_MAX_VEB)
14686 			break;
14687 
14688 		pf->veb[pf->lan_veb]->seid = seid;
14689 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14690 		pf->veb[pf->lan_veb]->pf = pf;
14691 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14692 		break;
14693 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14694 		if (num_reported != 1)
14695 			break;
14696 		/* This is immediately after a reset so we can assume this is
14697 		 * the PF's VSI
14698 		 */
14699 		pf->mac_seid = uplink_seid;
14700 		pf->pf_seid = downlink_seid;
14701 		pf->main_vsi_seid = seid;
14702 		if (printconfig)
14703 			dev_info(&pf->pdev->dev,
14704 				 "pf_seid=%d main_vsi_seid=%d\n",
14705 				 pf->pf_seid, pf->main_vsi_seid);
14706 		break;
14707 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14708 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14709 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14710 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14711 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14712 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14713 		/* ignore these for now */
14714 		break;
14715 	default:
14716 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14717 			 element_type, seid);
14718 		break;
14719 	}
14720 }
14721 
14722 /**
14723  * i40e_fetch_switch_configuration - Get switch config from firmware
14724  * @pf: board private structure
14725  * @printconfig: should we print the contents
14726  *
14727  * Get the current switch configuration from the device and
14728  * extract a few useful SEID values.
14729  **/
14730 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14731 {
14732 	struct i40e_aqc_get_switch_config_resp *sw_config;
14733 	u16 next_seid = 0;
14734 	int ret = 0;
14735 	u8 *aq_buf;
14736 	int i;
14737 
14738 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14739 	if (!aq_buf)
14740 		return -ENOMEM;
14741 
14742 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14743 	do {
14744 		u16 num_reported, num_total;
14745 
14746 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14747 						I40E_AQ_LARGE_BUF,
14748 						&next_seid, NULL);
14749 		if (ret) {
14750 			dev_info(&pf->pdev->dev,
14751 				 "get switch config failed err %s aq_err %s\n",
14752 				 i40e_stat_str(&pf->hw, ret),
14753 				 i40e_aq_str(&pf->hw,
14754 					     pf->hw.aq.asq_last_status));
14755 			kfree(aq_buf);
14756 			return -ENOENT;
14757 		}
14758 
14759 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14760 		num_total = le16_to_cpu(sw_config->header.num_total);
14761 
14762 		if (printconfig)
14763 			dev_info(&pf->pdev->dev,
14764 				 "header: %d reported %d total\n",
14765 				 num_reported, num_total);
14766 
14767 		for (i = 0; i < num_reported; i++) {
14768 			struct i40e_aqc_switch_config_element_resp *ele =
14769 				&sw_config->element[i];
14770 
14771 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14772 						     printconfig);
14773 		}
14774 	} while (next_seid != 0);
14775 
14776 	kfree(aq_buf);
14777 	return ret;
14778 }
14779 
14780 /**
14781  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14782  * @pf: board private structure
14783  * @reinit: if the Main VSI needs to re-initialized.
14784  * @lock_acquired: indicates whether or not the lock has been acquired
14785  *
14786  * Returns 0 on success, negative value on failure
14787  **/
14788 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14789 {
14790 	u16 flags = 0;
14791 	int ret;
14792 
14793 	/* find out what's out there already */
14794 	ret = i40e_fetch_switch_configuration(pf, false);
14795 	if (ret) {
14796 		dev_info(&pf->pdev->dev,
14797 			 "couldn't fetch switch config, err %s aq_err %s\n",
14798 			 i40e_stat_str(&pf->hw, ret),
14799 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14800 		return ret;
14801 	}
14802 	i40e_pf_reset_stats(pf);
14803 
14804 	/* set the switch config bit for the whole device to
14805 	 * support limited promisc or true promisc
14806 	 * when user requests promisc. The default is limited
14807 	 * promisc.
14808 	*/
14809 
14810 	if ((pf->hw.pf_id == 0) &&
14811 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
14812 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14813 		pf->last_sw_conf_flags = flags;
14814 	}
14815 
14816 	if (pf->hw.pf_id == 0) {
14817 		u16 valid_flags;
14818 
14819 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14820 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
14821 						NULL);
14822 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
14823 			dev_info(&pf->pdev->dev,
14824 				 "couldn't set switch config bits, err %s aq_err %s\n",
14825 				 i40e_stat_str(&pf->hw, ret),
14826 				 i40e_aq_str(&pf->hw,
14827 					     pf->hw.aq.asq_last_status));
14828 			/* not a fatal problem, just keep going */
14829 		}
14830 		pf->last_sw_conf_valid_flags = valid_flags;
14831 	}
14832 
14833 	/* first time setup */
14834 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
14835 		struct i40e_vsi *vsi = NULL;
14836 		u16 uplink_seid;
14837 
14838 		/* Set up the PF VSI associated with the PF's main VSI
14839 		 * that is already in the HW switch
14840 		 */
14841 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
14842 			uplink_seid = pf->veb[pf->lan_veb]->seid;
14843 		else
14844 			uplink_seid = pf->mac_seid;
14845 		if (pf->lan_vsi == I40E_NO_VSI)
14846 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
14847 		else if (reinit)
14848 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
14849 		if (!vsi) {
14850 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
14851 			i40e_cloud_filter_exit(pf);
14852 			i40e_fdir_teardown(pf);
14853 			return -EAGAIN;
14854 		}
14855 	} else {
14856 		/* force a reset of TC and queue layout configurations */
14857 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14858 
14859 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14860 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14861 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14862 	}
14863 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
14864 
14865 	i40e_fdir_sb_setup(pf);
14866 
14867 	/* Setup static PF queue filter control settings */
14868 	ret = i40e_setup_pf_filter_control(pf);
14869 	if (ret) {
14870 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
14871 			 ret);
14872 		/* Failure here should not stop continuing other steps */
14873 	}
14874 
14875 	/* enable RSS in the HW, even for only one queue, as the stack can use
14876 	 * the hash
14877 	 */
14878 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
14879 		i40e_pf_config_rss(pf);
14880 
14881 	/* fill in link information and enable LSE reporting */
14882 	i40e_link_event(pf);
14883 
14884 	/* Initialize user-specific link properties */
14885 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
14886 				  I40E_AQ_AN_COMPLETED) ? true : false);
14887 
14888 	i40e_ptp_init(pf);
14889 
14890 	if (!lock_acquired)
14891 		rtnl_lock();
14892 
14893 	/* repopulate tunnel port filters */
14894 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
14895 
14896 	if (!lock_acquired)
14897 		rtnl_unlock();
14898 
14899 	return ret;
14900 }
14901 
14902 /**
14903  * i40e_determine_queue_usage - Work out queue distribution
14904  * @pf: board private structure
14905  **/
14906 static void i40e_determine_queue_usage(struct i40e_pf *pf)
14907 {
14908 	int queues_left;
14909 	int q_max;
14910 
14911 	pf->num_lan_qps = 0;
14912 
14913 	/* Find the max queues to be put into basic use.  We'll always be
14914 	 * using TC0, whether or not DCB is running, and TC0 will get the
14915 	 * big RSS set.
14916 	 */
14917 	queues_left = pf->hw.func_caps.num_tx_qp;
14918 
14919 	if ((queues_left == 1) ||
14920 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
14921 		/* one qp for PF, no queues for anything else */
14922 		queues_left = 0;
14923 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14924 
14925 		/* make sure all the fancies are disabled */
14926 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14927 			       I40E_FLAG_IWARP_ENABLED	|
14928 			       I40E_FLAG_FD_SB_ENABLED	|
14929 			       I40E_FLAG_FD_ATR_ENABLED	|
14930 			       I40E_FLAG_DCB_CAPABLE	|
14931 			       I40E_FLAG_DCB_ENABLED	|
14932 			       I40E_FLAG_SRIOV_ENABLED	|
14933 			       I40E_FLAG_VMDQ_ENABLED);
14934 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14935 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
14936 				  I40E_FLAG_FD_SB_ENABLED |
14937 				  I40E_FLAG_FD_ATR_ENABLED |
14938 				  I40E_FLAG_DCB_CAPABLE))) {
14939 		/* one qp for PF */
14940 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14941 		queues_left -= pf->num_lan_qps;
14942 
14943 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14944 			       I40E_FLAG_IWARP_ENABLED	|
14945 			       I40E_FLAG_FD_SB_ENABLED	|
14946 			       I40E_FLAG_FD_ATR_ENABLED	|
14947 			       I40E_FLAG_DCB_ENABLED	|
14948 			       I40E_FLAG_VMDQ_ENABLED);
14949 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14950 	} else {
14951 		/* Not enough queues for all TCs */
14952 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
14953 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
14954 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
14955 					I40E_FLAG_DCB_ENABLED);
14956 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
14957 		}
14958 
14959 		/* limit lan qps to the smaller of qps, cpus or msix */
14960 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
14961 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
14962 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
14963 		pf->num_lan_qps = q_max;
14964 
14965 		queues_left -= pf->num_lan_qps;
14966 	}
14967 
14968 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14969 		if (queues_left > 1) {
14970 			queues_left -= 1; /* save 1 queue for FD */
14971 		} else {
14972 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
14973 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14974 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
14975 		}
14976 	}
14977 
14978 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
14979 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
14980 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
14981 					(queues_left / pf->num_vf_qps));
14982 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
14983 	}
14984 
14985 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
14986 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
14987 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
14988 					  (queues_left / pf->num_vmdq_qps));
14989 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
14990 	}
14991 
14992 	pf->queues_left = queues_left;
14993 	dev_dbg(&pf->pdev->dev,
14994 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
14995 		pf->hw.func_caps.num_tx_qp,
14996 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
14997 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
14998 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
14999 		queues_left);
15000 }
15001 
15002 /**
15003  * i40e_setup_pf_filter_control - Setup PF static filter control
15004  * @pf: PF to be setup
15005  *
15006  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15007  * settings. If PE/FCoE are enabled then it will also set the per PF
15008  * based filter sizes required for them. It also enables Flow director,
15009  * ethertype and macvlan type filter settings for the pf.
15010  *
15011  * Returns 0 on success, negative on failure
15012  **/
15013 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15014 {
15015 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15016 
15017 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15018 
15019 	/* Flow Director is enabled */
15020 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
15021 		settings->enable_fdir = true;
15022 
15023 	/* Ethtype and MACVLAN filters enabled for PF */
15024 	settings->enable_ethtype = true;
15025 	settings->enable_macvlan = true;
15026 
15027 	if (i40e_set_filter_control(&pf->hw, settings))
15028 		return -ENOENT;
15029 
15030 	return 0;
15031 }
15032 
15033 #define INFO_STRING_LEN 255
15034 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15035 static void i40e_print_features(struct i40e_pf *pf)
15036 {
15037 	struct i40e_hw *hw = &pf->hw;
15038 	char *buf;
15039 	int i;
15040 
15041 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15042 	if (!buf)
15043 		return;
15044 
15045 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15046 #ifdef CONFIG_PCI_IOV
15047 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15048 #endif
15049 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15050 		      pf->hw.func_caps.num_vsis,
15051 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15052 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15053 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15054 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15055 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15056 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15057 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15058 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15059 	}
15060 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15061 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15062 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15063 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15064 	if (pf->flags & I40E_FLAG_PTP)
15065 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15066 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15067 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15068 	else
15069 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15070 
15071 	dev_info(&pf->pdev->dev, "%s\n", buf);
15072 	kfree(buf);
15073 	WARN_ON(i > INFO_STRING_LEN);
15074 }
15075 
15076 /**
15077  * i40e_get_platform_mac_addr - get platform-specific MAC address
15078  * @pdev: PCI device information struct
15079  * @pf: board private structure
15080  *
15081  * Look up the MAC address for the device. First we'll try
15082  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15083  * specific fallback. Otherwise, we'll default to the stored value in
15084  * firmware.
15085  **/
15086 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15087 {
15088 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15089 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15090 }
15091 
15092 /**
15093  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15094  * @fec_cfg: FEC option to set in flags
15095  * @flags: ptr to flags in which we set FEC option
15096  **/
15097 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15098 {
15099 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15100 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15101 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15102 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15103 		*flags |= I40E_FLAG_RS_FEC;
15104 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15105 	}
15106 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15107 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15108 		*flags |= I40E_FLAG_BASE_R_FEC;
15109 		*flags &= ~I40E_FLAG_RS_FEC;
15110 	}
15111 	if (fec_cfg == 0)
15112 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15113 }
15114 
15115 /**
15116  * i40e_check_recovery_mode - check if we are running transition firmware
15117  * @pf: board private structure
15118  *
15119  * Check registers indicating the firmware runs in recovery mode. Sets the
15120  * appropriate driver state.
15121  *
15122  * Returns true if the recovery mode was detected, false otherwise
15123  **/
15124 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15125 {
15126 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15127 
15128 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15129 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15130 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15131 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15132 
15133 		return true;
15134 	}
15135 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15136 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15137 
15138 	return false;
15139 }
15140 
15141 /**
15142  * i40e_pf_loop_reset - perform reset in a loop.
15143  * @pf: board private structure
15144  *
15145  * This function is useful when a NIC is about to enter recovery mode.
15146  * When a NIC's internal data structures are corrupted the NIC's
15147  * firmware is going to enter recovery mode.
15148  * Right after a POR it takes about 7 minutes for firmware to enter
15149  * recovery mode. Until that time a NIC is in some kind of intermediate
15150  * state. After that time period the NIC almost surely enters
15151  * recovery mode. The only way for a driver to detect intermediate
15152  * state is to issue a series of pf-resets and check a return value.
15153  * If a PF reset returns success then the firmware could be in recovery
15154  * mode so the caller of this code needs to check for recovery mode
15155  * if this function returns success. There is a little chance that
15156  * firmware will hang in intermediate state forever.
15157  * Since waiting 7 minutes is quite a lot of time this function waits
15158  * 10 seconds and then gives up by returning an error.
15159  *
15160  * Return 0 on success, negative on failure.
15161  **/
15162 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
15163 {
15164 	/* wait max 10 seconds for PF reset to succeed */
15165 	const unsigned long time_end = jiffies + 10 * HZ;
15166 
15167 	struct i40e_hw *hw = &pf->hw;
15168 	i40e_status ret;
15169 
15170 	ret = i40e_pf_reset(hw);
15171 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15172 		usleep_range(10000, 20000);
15173 		ret = i40e_pf_reset(hw);
15174 	}
15175 
15176 	if (ret == I40E_SUCCESS)
15177 		pf->pfr_count++;
15178 	else
15179 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15180 
15181 	return ret;
15182 }
15183 
15184 /**
15185  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15186  * @pf: board private structure
15187  *
15188  * Check FW registers to determine if FW issued unexpected EMP Reset.
15189  * Every time when unexpected EMP Reset occurs the FW increments
15190  * a counter of unexpected EMP Resets. When the counter reaches 10
15191  * the FW should enter the Recovery mode
15192  *
15193  * Returns true if FW issued unexpected EMP Reset
15194  **/
15195 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15196 {
15197 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15198 			   I40E_GL_FWSTS_FWS1B_MASK;
15199 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15200 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15201 }
15202 
15203 /**
15204  * i40e_handle_resets - handle EMP resets and PF resets
15205  * @pf: board private structure
15206  *
15207  * Handle both EMP resets and PF resets and conclude whether there are
15208  * any issues regarding these resets. If there are any issues then
15209  * generate log entry.
15210  *
15211  * Return 0 if NIC is healthy or negative value when there are issues
15212  * with resets
15213  **/
15214 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
15215 {
15216 	const i40e_status pfr = i40e_pf_loop_reset(pf);
15217 	const bool is_empr = i40e_check_fw_empr(pf);
15218 
15219 	if (is_empr || pfr != I40E_SUCCESS)
15220 		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");
15221 
15222 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15223 }
15224 
15225 /**
15226  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15227  * @pf: board private structure
15228  * @hw: ptr to the hardware info
15229  *
15230  * This function does a minimal setup of all subsystems needed for running
15231  * recovery mode.
15232  *
15233  * Returns 0 on success, negative on failure
15234  **/
15235 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15236 {
15237 	struct i40e_vsi *vsi;
15238 	int err;
15239 	int v_idx;
15240 
15241 	pci_save_state(pf->pdev);
15242 
15243 	/* set up periodic task facility */
15244 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15245 	pf->service_timer_period = HZ;
15246 
15247 	INIT_WORK(&pf->service_task, i40e_service_task);
15248 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15249 
15250 	err = i40e_init_interrupt_scheme(pf);
15251 	if (err)
15252 		goto err_switch_setup;
15253 
15254 	/* The number of VSIs reported by the FW is the minimum guaranteed
15255 	 * to us; HW supports far more and we share the remaining pool with
15256 	 * the other PFs. We allocate space for more than the guarantee with
15257 	 * the understanding that we might not get them all later.
15258 	 */
15259 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15260 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15261 	else
15262 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15263 
15264 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15265 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15266 			  GFP_KERNEL);
15267 	if (!pf->vsi) {
15268 		err = -ENOMEM;
15269 		goto err_switch_setup;
15270 	}
15271 
15272 	/* We allocate one VSI which is needed as absolute minimum
15273 	 * in order to register the netdev
15274 	 */
15275 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15276 	if (v_idx < 0) {
15277 		err = v_idx;
15278 		goto err_switch_setup;
15279 	}
15280 	pf->lan_vsi = v_idx;
15281 	vsi = pf->vsi[v_idx];
15282 	if (!vsi) {
15283 		err = -EFAULT;
15284 		goto err_switch_setup;
15285 	}
15286 	vsi->alloc_queue_pairs = 1;
15287 	err = i40e_config_netdev(vsi);
15288 	if (err)
15289 		goto err_switch_setup;
15290 	err = register_netdev(vsi->netdev);
15291 	if (err)
15292 		goto err_switch_setup;
15293 	vsi->netdev_registered = true;
15294 	i40e_dbg_pf_init(pf);
15295 
15296 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15297 	if (err)
15298 		goto err_switch_setup;
15299 
15300 	/* tell the firmware that we're starting */
15301 	i40e_send_version(pf);
15302 
15303 	/* since everything's happy, start the service_task timer */
15304 	mod_timer(&pf->service_timer,
15305 		  round_jiffies(jiffies + pf->service_timer_period));
15306 
15307 	return 0;
15308 
15309 err_switch_setup:
15310 	i40e_reset_interrupt_capability(pf);
15311 	del_timer_sync(&pf->service_timer);
15312 	i40e_shutdown_adminq(hw);
15313 	iounmap(hw->hw_addr);
15314 	pci_disable_pcie_error_reporting(pf->pdev);
15315 	pci_release_mem_regions(pf->pdev);
15316 	pci_disable_device(pf->pdev);
15317 	kfree(pf);
15318 
15319 	return err;
15320 }
15321 
15322 /**
15323  * i40e_set_subsystem_device_id - set subsystem device id
15324  * @hw: pointer to the hardware info
15325  *
15326  * Set PCI subsystem device id either from a pci_dev structure or
15327  * a specific FW register.
15328  **/
15329 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15330 {
15331 	struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15332 
15333 	hw->subsystem_device_id = pdev->subsystem_device ?
15334 		pdev->subsystem_device :
15335 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15336 }
15337 
15338 /**
15339  * i40e_probe - Device initialization routine
15340  * @pdev: PCI device information struct
15341  * @ent: entry in i40e_pci_tbl
15342  *
15343  * i40e_probe initializes a PF identified by a pci_dev structure.
15344  * The OS initialization, configuring of the PF private structure,
15345  * and a hardware reset occur.
15346  *
15347  * Returns 0 on success, negative on failure
15348  **/
15349 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15350 {
15351 	struct i40e_aq_get_phy_abilities_resp abilities;
15352 #ifdef CONFIG_I40E_DCB
15353 	enum i40e_get_fw_lldp_status_resp lldp_status;
15354 	i40e_status status;
15355 #endif /* CONFIG_I40E_DCB */
15356 	struct i40e_pf *pf;
15357 	struct i40e_hw *hw;
15358 	static u16 pfs_found;
15359 	u16 wol_nvm_bits;
15360 	u16 link_status;
15361 	int err;
15362 	u32 val;
15363 	u32 i;
15364 
15365 	err = pci_enable_device_mem(pdev);
15366 	if (err)
15367 		return err;
15368 
15369 	/* set up for high or low dma */
15370 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15371 	if (err) {
15372 		dev_err(&pdev->dev,
15373 			"DMA configuration failed: 0x%x\n", err);
15374 		goto err_dma;
15375 	}
15376 
15377 	/* set up pci connections */
15378 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15379 	if (err) {
15380 		dev_info(&pdev->dev,
15381 			 "pci_request_selected_regions failed %d\n", err);
15382 		goto err_pci_reg;
15383 	}
15384 
15385 	pci_enable_pcie_error_reporting(pdev);
15386 	pci_set_master(pdev);
15387 
15388 	/* Now that we have a PCI connection, we need to do the
15389 	 * low level device setup.  This is primarily setting up
15390 	 * the Admin Queue structures and then querying for the
15391 	 * device's current profile information.
15392 	 */
15393 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15394 	if (!pf) {
15395 		err = -ENOMEM;
15396 		goto err_pf_alloc;
15397 	}
15398 	pf->next_vsi = 0;
15399 	pf->pdev = pdev;
15400 	set_bit(__I40E_DOWN, pf->state);
15401 
15402 	hw = &pf->hw;
15403 	hw->back = pf;
15404 
15405 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15406 				I40E_MAX_CSR_SPACE);
15407 	/* We believe that the highest register to read is
15408 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15409 	 * is not less than that before mapping to prevent a
15410 	 * kernel panic.
15411 	 */
15412 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15413 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15414 			pf->ioremap_len);
15415 		err = -ENOMEM;
15416 		goto err_ioremap;
15417 	}
15418 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15419 	if (!hw->hw_addr) {
15420 		err = -EIO;
15421 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15422 			 (unsigned int)pci_resource_start(pdev, 0),
15423 			 pf->ioremap_len, err);
15424 		goto err_ioremap;
15425 	}
15426 	hw->vendor_id = pdev->vendor;
15427 	hw->device_id = pdev->device;
15428 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15429 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15430 	i40e_set_subsystem_device_id(hw);
15431 	hw->bus.device = PCI_SLOT(pdev->devfn);
15432 	hw->bus.func = PCI_FUNC(pdev->devfn);
15433 	hw->bus.bus_id = pdev->bus->number;
15434 	pf->instance = pfs_found;
15435 
15436 	/* Select something other than the 802.1ad ethertype for the
15437 	 * switch to use internally and drop on ingress.
15438 	 */
15439 	hw->switch_tag = 0xffff;
15440 	hw->first_tag = ETH_P_8021AD;
15441 	hw->second_tag = ETH_P_8021Q;
15442 
15443 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15444 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15445 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15446 
15447 	/* set up the locks for the AQ, do this only once in probe
15448 	 * and destroy them only once in remove
15449 	 */
15450 	mutex_init(&hw->aq.asq_mutex);
15451 	mutex_init(&hw->aq.arq_mutex);
15452 
15453 	pf->msg_enable = netif_msg_init(debug,
15454 					NETIF_MSG_DRV |
15455 					NETIF_MSG_PROBE |
15456 					NETIF_MSG_LINK);
15457 	if (debug < -1)
15458 		pf->hw.debug_mask = debug;
15459 
15460 	/* do a special CORER for clearing PXE mode once at init */
15461 	if (hw->revision_id == 0 &&
15462 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15463 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15464 		i40e_flush(hw);
15465 		msleep(200);
15466 		pf->corer_count++;
15467 
15468 		i40e_clear_pxe_mode(hw);
15469 	}
15470 
15471 	/* Reset here to make sure all is clean and to define PF 'n' */
15472 	i40e_clear_hw(hw);
15473 
15474 	err = i40e_set_mac_type(hw);
15475 	if (err) {
15476 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15477 			 err);
15478 		goto err_pf_reset;
15479 	}
15480 
15481 	err = i40e_handle_resets(pf);
15482 	if (err)
15483 		goto err_pf_reset;
15484 
15485 	i40e_check_recovery_mode(pf);
15486 
15487 	if (is_kdump_kernel()) {
15488 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15489 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15490 	} else {
15491 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15492 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15493 	}
15494 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15495 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15496 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15497 
15498 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15499 		 "%s-%s:misc",
15500 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15501 
15502 	err = i40e_init_shared_code(hw);
15503 	if (err) {
15504 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15505 			 err);
15506 		goto err_pf_reset;
15507 	}
15508 
15509 	/* set up a default setting for link flow control */
15510 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15511 
15512 	err = i40e_init_adminq(hw);
15513 	if (err) {
15514 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15515 			dev_info(&pdev->dev,
15516 				 "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",
15517 				 hw->aq.api_maj_ver,
15518 				 hw->aq.api_min_ver,
15519 				 I40E_FW_API_VERSION_MAJOR,
15520 				 I40E_FW_MINOR_VERSION(hw));
15521 		else
15522 			dev_info(&pdev->dev,
15523 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15524 
15525 		goto err_pf_reset;
15526 	}
15527 	i40e_get_oem_version(hw);
15528 
15529 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15530 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15531 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15532 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15533 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15534 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15535 
15536 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15537 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15538 		dev_dbg(&pdev->dev,
15539 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15540 			 hw->aq.api_maj_ver,
15541 			 hw->aq.api_min_ver,
15542 			 I40E_FW_API_VERSION_MAJOR,
15543 			 I40E_FW_MINOR_VERSION(hw));
15544 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15545 		dev_info(&pdev->dev,
15546 			 "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",
15547 			 hw->aq.api_maj_ver,
15548 			 hw->aq.api_min_ver,
15549 			 I40E_FW_API_VERSION_MAJOR,
15550 			 I40E_FW_MINOR_VERSION(hw));
15551 
15552 	i40e_verify_eeprom(pf);
15553 
15554 	/* Rev 0 hardware was never productized */
15555 	if (hw->revision_id < 1)
15556 		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");
15557 
15558 	i40e_clear_pxe_mode(hw);
15559 
15560 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15561 	if (err)
15562 		goto err_adminq_setup;
15563 
15564 	err = i40e_sw_init(pf);
15565 	if (err) {
15566 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15567 		goto err_sw_init;
15568 	}
15569 
15570 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15571 		return i40e_init_recovery_mode(pf, hw);
15572 
15573 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15574 				hw->func_caps.num_rx_qp, 0, 0);
15575 	if (err) {
15576 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15577 		goto err_init_lan_hmc;
15578 	}
15579 
15580 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15581 	if (err) {
15582 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15583 		err = -ENOENT;
15584 		goto err_configure_lan_hmc;
15585 	}
15586 
15587 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15588 	 * Ignore error return codes because if it was already disabled via
15589 	 * hardware settings this will fail
15590 	 */
15591 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15592 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15593 		i40e_aq_stop_lldp(hw, true, false, NULL);
15594 	}
15595 
15596 	/* allow a platform config to override the HW addr */
15597 	i40e_get_platform_mac_addr(pdev, pf);
15598 
15599 	if (!is_valid_ether_addr(hw->mac.addr)) {
15600 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15601 		err = -EIO;
15602 		goto err_mac_addr;
15603 	}
15604 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15605 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15606 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15607 	if (is_valid_ether_addr(hw->mac.port_addr))
15608 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15609 
15610 	i40e_ptp_alloc_pins(pf);
15611 	pci_set_drvdata(pdev, pf);
15612 	pci_save_state(pdev);
15613 
15614 #ifdef CONFIG_I40E_DCB
15615 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15616 	(!status &&
15617 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15618 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15619 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15620 	dev_info(&pdev->dev,
15621 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15622 			"FW LLDP is disabled\n" :
15623 			"FW LLDP is enabled\n");
15624 
15625 	/* Enable FW to write default DCB config on link-up */
15626 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15627 
15628 	err = i40e_init_pf_dcb(pf);
15629 	if (err) {
15630 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15631 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15632 		/* Continue without DCB enabled */
15633 	}
15634 #endif /* CONFIG_I40E_DCB */
15635 
15636 	/* set up periodic task facility */
15637 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15638 	pf->service_timer_period = HZ;
15639 
15640 	INIT_WORK(&pf->service_task, i40e_service_task);
15641 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15642 
15643 	/* NVM bit on means WoL disabled for the port */
15644 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15645 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15646 		pf->wol_en = false;
15647 	else
15648 		pf->wol_en = true;
15649 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15650 
15651 	/* set up the main switch operations */
15652 	i40e_determine_queue_usage(pf);
15653 	err = i40e_init_interrupt_scheme(pf);
15654 	if (err)
15655 		goto err_switch_setup;
15656 
15657 	/* Reduce Tx and Rx pairs for kdump
15658 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15659 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15660 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15661 	 */
15662 	if (is_kdump_kernel())
15663 		pf->num_lan_msix = 1;
15664 
15665 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15666 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15667 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15668 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15669 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15670 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15671 						    UDP_TUNNEL_TYPE_GENEVE;
15672 
15673 	/* The number of VSIs reported by the FW is the minimum guaranteed
15674 	 * to us; HW supports far more and we share the remaining pool with
15675 	 * the other PFs. We allocate space for more than the guarantee with
15676 	 * the understanding that we might not get them all later.
15677 	 */
15678 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15679 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15680 	else
15681 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15682 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15683 		dev_warn(&pf->pdev->dev,
15684 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15685 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15686 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15687 	}
15688 
15689 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15690 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15691 			  GFP_KERNEL);
15692 	if (!pf->vsi) {
15693 		err = -ENOMEM;
15694 		goto err_switch_setup;
15695 	}
15696 
15697 #ifdef CONFIG_PCI_IOV
15698 	/* prep for VF support */
15699 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15700 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15701 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15702 		if (pci_num_vf(pdev))
15703 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15704 	}
15705 #endif
15706 	err = i40e_setup_pf_switch(pf, false, false);
15707 	if (err) {
15708 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15709 		goto err_vsis;
15710 	}
15711 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15712 
15713 	/* if FDIR VSI was set up, start it now */
15714 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15715 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15716 			i40e_vsi_open(pf->vsi[i]);
15717 			break;
15718 		}
15719 	}
15720 
15721 	/* The driver only wants link up/down and module qualification
15722 	 * reports from firmware.  Note the negative logic.
15723 	 */
15724 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15725 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15726 					 I40E_AQ_EVENT_MEDIA_NA |
15727 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15728 	if (err)
15729 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15730 			 i40e_stat_str(&pf->hw, err),
15731 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15732 
15733 	/* Reconfigure hardware for allowing smaller MSS in the case
15734 	 * of TSO, so that we avoid the MDD being fired and causing
15735 	 * a reset in the case of small MSS+TSO.
15736 	 */
15737 	val = rd32(hw, I40E_REG_MSS);
15738 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15739 		val &= ~I40E_REG_MSS_MIN_MASK;
15740 		val |= I40E_64BYTE_MSS;
15741 		wr32(hw, I40E_REG_MSS, val);
15742 	}
15743 
15744 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15745 		msleep(75);
15746 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15747 		if (err)
15748 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15749 				 i40e_stat_str(&pf->hw, err),
15750 				 i40e_aq_str(&pf->hw,
15751 					     pf->hw.aq.asq_last_status));
15752 	}
15753 	/* The main driver is (mostly) up and happy. We need to set this state
15754 	 * before setting up the misc vector or we get a race and the vector
15755 	 * ends up disabled forever.
15756 	 */
15757 	clear_bit(__I40E_DOWN, pf->state);
15758 
15759 	/* In case of MSIX we are going to setup the misc vector right here
15760 	 * to handle admin queue events etc. In case of legacy and MSI
15761 	 * the misc functionality and queue processing is combined in
15762 	 * the same vector and that gets setup at open.
15763 	 */
15764 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15765 		err = i40e_setup_misc_vector(pf);
15766 		if (err) {
15767 			dev_info(&pdev->dev,
15768 				 "setup of misc vector failed: %d\n", err);
15769 			i40e_cloud_filter_exit(pf);
15770 			i40e_fdir_teardown(pf);
15771 			goto err_vsis;
15772 		}
15773 	}
15774 
15775 #ifdef CONFIG_PCI_IOV
15776 	/* prep for VF support */
15777 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15778 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15779 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15780 		/* disable link interrupts for VFs */
15781 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15782 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15783 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15784 		i40e_flush(hw);
15785 
15786 		if (pci_num_vf(pdev)) {
15787 			dev_info(&pdev->dev,
15788 				 "Active VFs found, allocating resources.\n");
15789 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15790 			if (err)
15791 				dev_info(&pdev->dev,
15792 					 "Error %d allocating resources for existing VFs\n",
15793 					 err);
15794 		}
15795 	}
15796 #endif /* CONFIG_PCI_IOV */
15797 
15798 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15799 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
15800 						      pf->num_iwarp_msix,
15801 						      I40E_IWARP_IRQ_PILE_ID);
15802 		if (pf->iwarp_base_vector < 0) {
15803 			dev_info(&pdev->dev,
15804 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
15805 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
15806 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
15807 		}
15808 	}
15809 
15810 	i40e_dbg_pf_init(pf);
15811 
15812 	/* tell the firmware that we're starting */
15813 	i40e_send_version(pf);
15814 
15815 	/* since everything's happy, start the service_task timer */
15816 	mod_timer(&pf->service_timer,
15817 		  round_jiffies(jiffies + pf->service_timer_period));
15818 
15819 	/* add this PF to client device list and launch a client service task */
15820 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15821 		err = i40e_lan_add_device(pf);
15822 		if (err)
15823 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
15824 				 err);
15825 	}
15826 
15827 #define PCI_SPEED_SIZE 8
15828 #define PCI_WIDTH_SIZE 8
15829 	/* Devices on the IOSF bus do not have this information
15830 	 * and will report PCI Gen 1 x 1 by default so don't bother
15831 	 * checking them.
15832 	 */
15833 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
15834 		char speed[PCI_SPEED_SIZE] = "Unknown";
15835 		char width[PCI_WIDTH_SIZE] = "Unknown";
15836 
15837 		/* Get the negotiated link width and speed from PCI config
15838 		 * space
15839 		 */
15840 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
15841 					  &link_status);
15842 
15843 		i40e_set_pci_config_data(hw, link_status);
15844 
15845 		switch (hw->bus.speed) {
15846 		case i40e_bus_speed_8000:
15847 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
15848 		case i40e_bus_speed_5000:
15849 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
15850 		case i40e_bus_speed_2500:
15851 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
15852 		default:
15853 			break;
15854 		}
15855 		switch (hw->bus.width) {
15856 		case i40e_bus_width_pcie_x8:
15857 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
15858 		case i40e_bus_width_pcie_x4:
15859 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
15860 		case i40e_bus_width_pcie_x2:
15861 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
15862 		case i40e_bus_width_pcie_x1:
15863 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
15864 		default:
15865 			break;
15866 		}
15867 
15868 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
15869 			 speed, width);
15870 
15871 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
15872 		    hw->bus.speed < i40e_bus_speed_8000) {
15873 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
15874 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
15875 		}
15876 	}
15877 
15878 	/* get the requested speeds from the fw */
15879 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
15880 	if (err)
15881 		dev_dbg(&pf->pdev->dev, "get requested speeds 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 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
15885 
15886 	/* set the FEC config due to the board capabilities */
15887 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
15888 
15889 	/* get the supported phy types from the fw */
15890 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
15891 	if (err)
15892 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
15893 			i40e_stat_str(&pf->hw, err),
15894 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15895 
15896 	/* make sure the MFS hasn't been set lower than the default */
15897 #define MAX_FRAME_SIZE_DEFAULT 0x2600
15898 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
15899 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
15900 	if (val < MAX_FRAME_SIZE_DEFAULT)
15901 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
15902 			 i, val);
15903 
15904 	/* Add a filter to drop all Flow control frames from any VSI from being
15905 	 * transmitted. By doing so we stop a malicious VF from sending out
15906 	 * PAUSE or PFC frames and potentially controlling traffic for other
15907 	 * PF/VF VSIs.
15908 	 * The FW can still send Flow control frames if enabled.
15909 	 */
15910 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
15911 						       pf->main_vsi_seid);
15912 
15913 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
15914 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
15915 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
15916 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
15917 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
15918 	/* print a string summarizing features */
15919 	i40e_print_features(pf);
15920 
15921 	return 0;
15922 
15923 	/* Unwind what we've done if something failed in the setup */
15924 err_vsis:
15925 	set_bit(__I40E_DOWN, pf->state);
15926 	i40e_clear_interrupt_scheme(pf);
15927 	kfree(pf->vsi);
15928 err_switch_setup:
15929 	i40e_reset_interrupt_capability(pf);
15930 	del_timer_sync(&pf->service_timer);
15931 err_mac_addr:
15932 err_configure_lan_hmc:
15933 	(void)i40e_shutdown_lan_hmc(hw);
15934 err_init_lan_hmc:
15935 	kfree(pf->qp_pile);
15936 err_sw_init:
15937 err_adminq_setup:
15938 err_pf_reset:
15939 	iounmap(hw->hw_addr);
15940 err_ioremap:
15941 	kfree(pf);
15942 err_pf_alloc:
15943 	pci_disable_pcie_error_reporting(pdev);
15944 	pci_release_mem_regions(pdev);
15945 err_pci_reg:
15946 err_dma:
15947 	pci_disable_device(pdev);
15948 	return err;
15949 }
15950 
15951 /**
15952  * i40e_remove - Device removal routine
15953  * @pdev: PCI device information struct
15954  *
15955  * i40e_remove is called by the PCI subsystem to alert the driver
15956  * that is should release a PCI device.  This could be caused by a
15957  * Hot-Plug event, or because the driver is going to be removed from
15958  * memory.
15959  **/
15960 static void i40e_remove(struct pci_dev *pdev)
15961 {
15962 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15963 	struct i40e_hw *hw = &pf->hw;
15964 	i40e_status ret_code;
15965 	int i;
15966 
15967 	i40e_dbg_pf_exit(pf);
15968 
15969 	i40e_ptp_stop(pf);
15970 
15971 	/* Disable RSS in hw */
15972 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
15973 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
15974 
15975 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
15976 	 * flags, once they are set, i40e_rebuild should not be called as
15977 	 * i40e_prep_for_reset always returns early.
15978 	 */
15979 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
15980 		usleep_range(1000, 2000);
15981 	set_bit(__I40E_IN_REMOVE, pf->state);
15982 
15983 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
15984 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
15985 		i40e_free_vfs(pf);
15986 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
15987 	}
15988 	/* no more scheduling of any task */
15989 	set_bit(__I40E_SUSPENDED, pf->state);
15990 	set_bit(__I40E_DOWN, pf->state);
15991 	if (pf->service_timer.function)
15992 		del_timer_sync(&pf->service_timer);
15993 	if (pf->service_task.func)
15994 		cancel_work_sync(&pf->service_task);
15995 
15996 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
15997 		struct i40e_vsi *vsi = pf->vsi[0];
15998 
15999 		/* We know that we have allocated only one vsi for this PF,
16000 		 * it was just for registering netdevice, so the interface
16001 		 * could be visible in the 'ifconfig' output
16002 		 */
16003 		unregister_netdev(vsi->netdev);
16004 		free_netdev(vsi->netdev);
16005 
16006 		goto unmap;
16007 	}
16008 
16009 	/* Client close must be called explicitly here because the timer
16010 	 * has been stopped.
16011 	 */
16012 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16013 
16014 	i40e_fdir_teardown(pf);
16015 
16016 	/* If there is a switch structure or any orphans, remove them.
16017 	 * This will leave only the PF's VSI remaining.
16018 	 */
16019 	for (i = 0; i < I40E_MAX_VEB; i++) {
16020 		if (!pf->veb[i])
16021 			continue;
16022 
16023 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16024 		    pf->veb[i]->uplink_seid == 0)
16025 			i40e_switch_branch_release(pf->veb[i]);
16026 	}
16027 
16028 	/* Now we can shutdown the PF's VSI, just before we kill
16029 	 * adminq and hmc.
16030 	 */
16031 	if (pf->vsi[pf->lan_vsi])
16032 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
16033 
16034 	i40e_cloud_filter_exit(pf);
16035 
16036 	/* remove attached clients */
16037 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16038 		ret_code = i40e_lan_del_device(pf);
16039 		if (ret_code)
16040 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16041 				 ret_code);
16042 	}
16043 
16044 	/* shutdown and destroy the HMC */
16045 	if (hw->hmc.hmc_obj) {
16046 		ret_code = i40e_shutdown_lan_hmc(hw);
16047 		if (ret_code)
16048 			dev_warn(&pdev->dev,
16049 				 "Failed to destroy the HMC resources: %d\n",
16050 				 ret_code);
16051 	}
16052 
16053 unmap:
16054 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16055 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16056 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16057 		free_irq(pf->pdev->irq, pf);
16058 
16059 	/* shutdown the adminq */
16060 	i40e_shutdown_adminq(hw);
16061 
16062 	/* destroy the locks only once, here */
16063 	mutex_destroy(&hw->aq.arq_mutex);
16064 	mutex_destroy(&hw->aq.asq_mutex);
16065 
16066 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16067 	rtnl_lock();
16068 	i40e_clear_interrupt_scheme(pf);
16069 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16070 		if (pf->vsi[i]) {
16071 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16072 				i40e_vsi_clear_rings(pf->vsi[i]);
16073 			i40e_vsi_clear(pf->vsi[i]);
16074 			pf->vsi[i] = NULL;
16075 		}
16076 	}
16077 	rtnl_unlock();
16078 
16079 	for (i = 0; i < I40E_MAX_VEB; i++) {
16080 		kfree(pf->veb[i]);
16081 		pf->veb[i] = NULL;
16082 	}
16083 
16084 	kfree(pf->qp_pile);
16085 	kfree(pf->vsi);
16086 
16087 	iounmap(hw->hw_addr);
16088 	kfree(pf);
16089 	pci_release_mem_regions(pdev);
16090 
16091 	pci_disable_pcie_error_reporting(pdev);
16092 	pci_disable_device(pdev);
16093 }
16094 
16095 /**
16096  * i40e_pci_error_detected - warning that something funky happened in PCI land
16097  * @pdev: PCI device information struct
16098  * @error: the type of PCI error
16099  *
16100  * Called to warn that something happened and the error handling steps
16101  * are in progress.  Allows the driver to quiesce things, be ready for
16102  * remediation.
16103  **/
16104 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16105 						pci_channel_state_t error)
16106 {
16107 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16108 
16109 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16110 
16111 	if (!pf) {
16112 		dev_info(&pdev->dev,
16113 			 "Cannot recover - error happened during device probe\n");
16114 		return PCI_ERS_RESULT_DISCONNECT;
16115 	}
16116 
16117 	/* shutdown all operations */
16118 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16119 		i40e_prep_for_reset(pf);
16120 
16121 	/* Request a slot reset */
16122 	return PCI_ERS_RESULT_NEED_RESET;
16123 }
16124 
16125 /**
16126  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16127  * @pdev: PCI device information struct
16128  *
16129  * Called to find if the driver can work with the device now that
16130  * the pci slot has been reset.  If a basic connection seems good
16131  * (registers are readable and have sane content) then return a
16132  * happy little PCI_ERS_RESULT_xxx.
16133  **/
16134 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16135 {
16136 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16137 	pci_ers_result_t result;
16138 	u32 reg;
16139 
16140 	dev_dbg(&pdev->dev, "%s\n", __func__);
16141 	if (pci_enable_device_mem(pdev)) {
16142 		dev_info(&pdev->dev,
16143 			 "Cannot re-enable PCI device after reset.\n");
16144 		result = PCI_ERS_RESULT_DISCONNECT;
16145 	} else {
16146 		pci_set_master(pdev);
16147 		pci_restore_state(pdev);
16148 		pci_save_state(pdev);
16149 		pci_wake_from_d3(pdev, false);
16150 
16151 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16152 		if (reg == 0)
16153 			result = PCI_ERS_RESULT_RECOVERED;
16154 		else
16155 			result = PCI_ERS_RESULT_DISCONNECT;
16156 	}
16157 
16158 	return result;
16159 }
16160 
16161 /**
16162  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16163  * @pdev: PCI device information struct
16164  */
16165 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16166 {
16167 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16168 
16169 	i40e_prep_for_reset(pf);
16170 }
16171 
16172 /**
16173  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16174  * @pdev: PCI device information struct
16175  */
16176 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16177 {
16178 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16179 
16180 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16181 		return;
16182 
16183 	i40e_reset_and_rebuild(pf, false, false);
16184 }
16185 
16186 /**
16187  * i40e_pci_error_resume - restart operations after PCI error recovery
16188  * @pdev: PCI device information struct
16189  *
16190  * Called to allow the driver to bring things back up after PCI error
16191  * and/or reset recovery has finished.
16192  **/
16193 static void i40e_pci_error_resume(struct pci_dev *pdev)
16194 {
16195 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16196 
16197 	dev_dbg(&pdev->dev, "%s\n", __func__);
16198 	if (test_bit(__I40E_SUSPENDED, pf->state))
16199 		return;
16200 
16201 	i40e_handle_reset_warning(pf, false);
16202 }
16203 
16204 /**
16205  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16206  * using the mac_address_write admin q function
16207  * @pf: pointer to i40e_pf struct
16208  **/
16209 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16210 {
16211 	struct i40e_hw *hw = &pf->hw;
16212 	i40e_status ret;
16213 	u8 mac_addr[6];
16214 	u16 flags = 0;
16215 
16216 	/* Get current MAC address in case it's an LAA */
16217 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16218 		ether_addr_copy(mac_addr,
16219 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16220 	} else {
16221 		dev_err(&pf->pdev->dev,
16222 			"Failed to retrieve MAC address; using default\n");
16223 		ether_addr_copy(mac_addr, hw->mac.addr);
16224 	}
16225 
16226 	/* The FW expects the mac address write cmd to first be called with
16227 	 * one of these flags before calling it again with the multicast
16228 	 * enable flags.
16229 	 */
16230 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16231 
16232 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16233 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16234 
16235 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16236 	if (ret) {
16237 		dev_err(&pf->pdev->dev,
16238 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16239 		return;
16240 	}
16241 
16242 	flags = I40E_AQC_MC_MAG_EN
16243 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16244 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16245 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16246 	if (ret)
16247 		dev_err(&pf->pdev->dev,
16248 			"Failed to enable Multicast Magic Packet wake up\n");
16249 }
16250 
16251 /**
16252  * i40e_shutdown - PCI callback for shutting down
16253  * @pdev: PCI device information struct
16254  **/
16255 static void i40e_shutdown(struct pci_dev *pdev)
16256 {
16257 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16258 	struct i40e_hw *hw = &pf->hw;
16259 
16260 	set_bit(__I40E_SUSPENDED, pf->state);
16261 	set_bit(__I40E_DOWN, pf->state);
16262 
16263 	del_timer_sync(&pf->service_timer);
16264 	cancel_work_sync(&pf->service_task);
16265 	i40e_cloud_filter_exit(pf);
16266 	i40e_fdir_teardown(pf);
16267 
16268 	/* Client close must be called explicitly here because the timer
16269 	 * has been stopped.
16270 	 */
16271 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16272 
16273 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16274 		i40e_enable_mc_magic_wake(pf);
16275 
16276 	i40e_prep_for_reset(pf);
16277 
16278 	wr32(hw, I40E_PFPM_APM,
16279 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16280 	wr32(hw, I40E_PFPM_WUFC,
16281 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16282 
16283 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16284 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16285 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16286 		free_irq(pf->pdev->irq, pf);
16287 
16288 	/* Since we're going to destroy queues during the
16289 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16290 	 * whole section
16291 	 */
16292 	rtnl_lock();
16293 	i40e_clear_interrupt_scheme(pf);
16294 	rtnl_unlock();
16295 
16296 	if (system_state == SYSTEM_POWER_OFF) {
16297 		pci_wake_from_d3(pdev, pf->wol_en);
16298 		pci_set_power_state(pdev, PCI_D3hot);
16299 	}
16300 }
16301 
16302 /**
16303  * i40e_suspend - PM callback for moving to D3
16304  * @dev: generic device information structure
16305  **/
16306 static int __maybe_unused i40e_suspend(struct device *dev)
16307 {
16308 	struct i40e_pf *pf = dev_get_drvdata(dev);
16309 	struct i40e_hw *hw = &pf->hw;
16310 
16311 	/* If we're already suspended, then there is nothing to do */
16312 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16313 		return 0;
16314 
16315 	set_bit(__I40E_DOWN, pf->state);
16316 
16317 	/* Ensure service task will not be running */
16318 	del_timer_sync(&pf->service_timer);
16319 	cancel_work_sync(&pf->service_task);
16320 
16321 	/* Client close must be called explicitly here because the timer
16322 	 * has been stopped.
16323 	 */
16324 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16325 
16326 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16327 		i40e_enable_mc_magic_wake(pf);
16328 
16329 	/* Since we're going to destroy queues during the
16330 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16331 	 * whole section
16332 	 */
16333 	rtnl_lock();
16334 
16335 	i40e_prep_for_reset(pf);
16336 
16337 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16338 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16339 
16340 	/* Clear the interrupt scheme and release our IRQs so that the system
16341 	 * can safely hibernate even when there are a large number of CPUs.
16342 	 * Otherwise hibernation might fail when mapping all the vectors back
16343 	 * to CPU0.
16344 	 */
16345 	i40e_clear_interrupt_scheme(pf);
16346 
16347 	rtnl_unlock();
16348 
16349 	return 0;
16350 }
16351 
16352 /**
16353  * i40e_resume - PM callback for waking up from D3
16354  * @dev: generic device information structure
16355  **/
16356 static int __maybe_unused i40e_resume(struct device *dev)
16357 {
16358 	struct i40e_pf *pf = dev_get_drvdata(dev);
16359 	int err;
16360 
16361 	/* If we're not suspended, then there is nothing to do */
16362 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16363 		return 0;
16364 
16365 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16366 	 * since we're going to be restoring queues
16367 	 */
16368 	rtnl_lock();
16369 
16370 	/* We cleared the interrupt scheme when we suspended, so we need to
16371 	 * restore it now to resume device functionality.
16372 	 */
16373 	err = i40e_restore_interrupt_scheme(pf);
16374 	if (err) {
16375 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16376 			err);
16377 	}
16378 
16379 	clear_bit(__I40E_DOWN, pf->state);
16380 	i40e_reset_and_rebuild(pf, false, true);
16381 
16382 	rtnl_unlock();
16383 
16384 	/* Clear suspended state last after everything is recovered */
16385 	clear_bit(__I40E_SUSPENDED, pf->state);
16386 
16387 	/* Restart the service task */
16388 	mod_timer(&pf->service_timer,
16389 		  round_jiffies(jiffies + pf->service_timer_period));
16390 
16391 	return 0;
16392 }
16393 
16394 static const struct pci_error_handlers i40e_err_handler = {
16395 	.error_detected = i40e_pci_error_detected,
16396 	.slot_reset = i40e_pci_error_slot_reset,
16397 	.reset_prepare = i40e_pci_error_reset_prepare,
16398 	.reset_done = i40e_pci_error_reset_done,
16399 	.resume = i40e_pci_error_resume,
16400 };
16401 
16402 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16403 
16404 static struct pci_driver i40e_driver = {
16405 	.name     = i40e_driver_name,
16406 	.id_table = i40e_pci_tbl,
16407 	.probe    = i40e_probe,
16408 	.remove   = i40e_remove,
16409 	.driver   = {
16410 		.pm = &i40e_pm_ops,
16411 	},
16412 	.shutdown = i40e_shutdown,
16413 	.err_handler = &i40e_err_handler,
16414 	.sriov_configure = i40e_pci_sriov_configure,
16415 };
16416 
16417 /**
16418  * i40e_init_module - Driver registration routine
16419  *
16420  * i40e_init_module is the first routine called when the driver is
16421  * loaded. All it does is register with the PCI subsystem.
16422  **/
16423 static int __init i40e_init_module(void)
16424 {
16425 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16426 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16427 
16428 	/* There is no need to throttle the number of active tasks because
16429 	 * each device limits its own task using a state bit for scheduling
16430 	 * the service task, and the device tasks do not interfere with each
16431 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16432 	 * since we need to be able to guarantee forward progress even under
16433 	 * memory pressure.
16434 	 */
16435 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16436 	if (!i40e_wq) {
16437 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16438 		return -ENOMEM;
16439 	}
16440 
16441 	i40e_dbg_init();
16442 	return pci_register_driver(&i40e_driver);
16443 }
16444 module_init(i40e_init_module);
16445 
16446 /**
16447  * i40e_exit_module - Driver exit cleanup routine
16448  *
16449  * i40e_exit_module is called just before the driver is removed
16450  * from memory.
16451  **/
16452 static void __exit i40e_exit_module(void)
16453 {
16454 	pci_unregister_driver(&i40e_driver);
16455 	destroy_workqueue(i40e_wq);
16456 	ida_destroy(&i40e_client_ida);
16457 	i40e_dbg_exit();
16458 }
16459 module_exit(i40e_exit_module);
16460