1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2021 Intel Corporation. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/of_net.h>
6 #include <linux/pci.h>
7 #include <linux/bpf.h>
8 #include <generated/utsrelease.h>
9 #include <linux/crash_dump.h>
10 
11 /* Local includes */
12 #include "i40e.h"
13 #include "i40e_diag.h"
14 #include "i40e_xsk.h"
15 #include <net/udp_tunnel.h>
16 #include <net/xdp_sock_drv.h>
17 /* All i40e tracepoints are defined by the include below, which
18  * must be included exactly once across the whole kernel with
19  * CREATE_TRACE_POINTS defined
20  */
21 #define CREATE_TRACE_POINTS
22 #include "i40e_trace.h"
23 
24 const char i40e_driver_name[] = "i40e";
25 static const char i40e_driver_string[] =
26 			"Intel(R) Ethernet Connection XL710 Network Driver";
27 
28 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
29 
30 /* a bit of forward declarations */
31 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
32 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
33 static int i40e_add_vsi(struct i40e_vsi *vsi);
34 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
35 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
36 static int i40e_setup_misc_vector(struct i40e_pf *pf);
37 static void i40e_determine_queue_usage(struct i40e_pf *pf);
38 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
39 static void i40e_prep_for_reset(struct i40e_pf *pf);
40 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
41 				   bool lock_acquired);
42 static int i40e_reset(struct i40e_pf *pf);
43 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
44 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
45 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
46 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
47 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
48 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
49 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
50 static int i40e_get_capabilities(struct i40e_pf *pf,
51 				 enum i40e_admin_queue_opc list_type);
52 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
53 
54 /* i40e_pci_tbl - PCI Device ID Table
55  *
56  * Last entry must be all 0s
57  *
58  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
59  *   Class, Class Mask, private data (not used) }
60  */
61 static const struct pci_device_id i40e_pci_tbl[] = {
62 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
63 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
64 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
86 	/* required last entry */
87 	{0, }
88 };
89 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
90 
91 #define I40E_MAX_VF_COUNT 128
92 static int debug = -1;
93 module_param(debug, uint, 0);
94 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
95 
96 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
97 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
98 MODULE_LICENSE("GPL v2");
99 
100 static struct workqueue_struct *i40e_wq;
101 
102 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
103 				  struct net_device *netdev, int delta)
104 {
105 	struct netdev_hw_addr *ha;
106 
107 	if (!f || !netdev)
108 		return;
109 
110 	netdev_for_each_mc_addr(ha, netdev) {
111 		if (ether_addr_equal(ha->addr, f->macaddr)) {
112 			ha->refcount += delta;
113 			if (ha->refcount <= 0)
114 				ha->refcount = 1;
115 			break;
116 		}
117 	}
118 }
119 
120 /**
121  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
122  * @hw:   pointer to the HW structure
123  * @mem:  ptr to mem struct to fill out
124  * @size: size of memory requested
125  * @alignment: what to align the allocation to
126  **/
127 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
128 			    u64 size, u32 alignment)
129 {
130 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
131 
132 	mem->size = ALIGN(size, alignment);
133 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
134 				     GFP_KERNEL);
135 	if (!mem->va)
136 		return -ENOMEM;
137 
138 	return 0;
139 }
140 
141 /**
142  * i40e_free_dma_mem_d - OS specific memory free for shared code
143  * @hw:   pointer to the HW structure
144  * @mem:  ptr to mem struct to free
145  **/
146 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
147 {
148 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
149 
150 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
151 	mem->va = NULL;
152 	mem->pa = 0;
153 	mem->size = 0;
154 
155 	return 0;
156 }
157 
158 /**
159  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
160  * @hw:   pointer to the HW structure
161  * @mem:  ptr to mem struct to fill out
162  * @size: size of memory requested
163  **/
164 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
165 			     u32 size)
166 {
167 	mem->size = size;
168 	mem->va = kzalloc(size, GFP_KERNEL);
169 
170 	if (!mem->va)
171 		return -ENOMEM;
172 
173 	return 0;
174 }
175 
176 /**
177  * i40e_free_virt_mem_d - OS specific memory free for shared code
178  * @hw:   pointer to the HW structure
179  * @mem:  ptr to mem struct to free
180  **/
181 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
182 {
183 	/* it's ok to kfree a NULL pointer */
184 	kfree(mem->va);
185 	mem->va = NULL;
186 	mem->size = 0;
187 
188 	return 0;
189 }
190 
191 /**
192  * i40e_get_lump - find a lump of free generic resource
193  * @pf: board private structure
194  * @pile: the pile of resource to search
195  * @needed: the number of items needed
196  * @id: an owner id to stick on the items assigned
197  *
198  * Returns the base item index of the lump, or negative for error
199  **/
200 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
201 			 u16 needed, u16 id)
202 {
203 	int ret = -ENOMEM;
204 	int i, j;
205 
206 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
207 		dev_info(&pf->pdev->dev,
208 			 "param err: pile=%s needed=%d id=0x%04x\n",
209 			 pile ? "<valid>" : "<null>", needed, id);
210 		return -EINVAL;
211 	}
212 
213 	/* Allocate last queue in the pile for FDIR VSI queue
214 	 * so it doesn't fragment the qp_pile
215 	 */
216 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
217 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
218 			dev_err(&pf->pdev->dev,
219 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
220 				pile->num_entries - 1);
221 			return -ENOMEM;
222 		}
223 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
224 		return pile->num_entries - 1;
225 	}
226 
227 	i = 0;
228 	while (i < pile->num_entries) {
229 		/* skip already allocated entries */
230 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
231 			i++;
232 			continue;
233 		}
234 
235 		/* do we have enough in this lump? */
236 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
237 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
238 				break;
239 		}
240 
241 		if (j == needed) {
242 			/* there was enough, so assign it to the requestor */
243 			for (j = 0; j < needed; j++)
244 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
245 			ret = i;
246 			break;
247 		}
248 
249 		/* not enough, so skip over it and continue looking */
250 		i += j;
251 	}
252 
253 	return ret;
254 }
255 
256 /**
257  * i40e_put_lump - return a lump of generic resource
258  * @pile: the pile of resource to search
259  * @index: the base item index
260  * @id: the owner id of the items assigned
261  *
262  * Returns the count of items in the lump
263  **/
264 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
265 {
266 	int valid_id = (id | I40E_PILE_VALID_BIT);
267 	int count = 0;
268 	u16 i;
269 
270 	if (!pile || index >= pile->num_entries)
271 		return -EINVAL;
272 
273 	for (i = index;
274 	     i < pile->num_entries && pile->list[i] == valid_id;
275 	     i++) {
276 		pile->list[i] = 0;
277 		count++;
278 	}
279 
280 
281 	return count;
282 }
283 
284 /**
285  * i40e_find_vsi_from_id - searches for the vsi with the given id
286  * @pf: the pf structure to search for the vsi
287  * @id: id of the vsi it is searching for
288  **/
289 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
290 {
291 	int i;
292 
293 	for (i = 0; i < pf->num_alloc_vsi; i++)
294 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
295 			return pf->vsi[i];
296 
297 	return NULL;
298 }
299 
300 /**
301  * i40e_service_event_schedule - Schedule the service task to wake up
302  * @pf: board private structure
303  *
304  * If not already scheduled, this puts the task into the work queue
305  **/
306 void i40e_service_event_schedule(struct i40e_pf *pf)
307 {
308 	if ((!test_bit(__I40E_DOWN, pf->state) &&
309 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
310 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
311 		queue_work(i40e_wq, &pf->service_task);
312 }
313 
314 /**
315  * i40e_tx_timeout - Respond to a Tx Hang
316  * @netdev: network interface device structure
317  * @txqueue: queue number timing out
318  *
319  * If any port has noticed a Tx timeout, it is likely that the whole
320  * device is munged, not just the one netdev port, so go for the full
321  * reset.
322  **/
323 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
324 {
325 	struct i40e_netdev_priv *np = netdev_priv(netdev);
326 	struct i40e_vsi *vsi = np->vsi;
327 	struct i40e_pf *pf = vsi->back;
328 	struct i40e_ring *tx_ring = NULL;
329 	unsigned int i;
330 	u32 head, val;
331 
332 	pf->tx_timeout_count++;
333 
334 	/* with txqueue index, find the tx_ring struct */
335 	for (i = 0; i < vsi->num_queue_pairs; i++) {
336 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
337 			if (txqueue ==
338 			    vsi->tx_rings[i]->queue_index) {
339 				tx_ring = vsi->tx_rings[i];
340 				break;
341 			}
342 		}
343 	}
344 
345 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
346 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
347 	else if (time_before(jiffies,
348 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
349 		return;   /* don't do any new action before the next timeout */
350 
351 	/* don't kick off another recovery if one is already pending */
352 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
353 		return;
354 
355 	if (tx_ring) {
356 		head = i40e_get_head(tx_ring);
357 		/* Read interrupt register */
358 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
359 			val = rd32(&pf->hw,
360 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
361 						tx_ring->vsi->base_vector - 1));
362 		else
363 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
364 
365 		netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
366 			    vsi->seid, txqueue, tx_ring->next_to_clean,
367 			    head, tx_ring->next_to_use,
368 			    readl(tx_ring->tail), val);
369 	}
370 
371 	pf->tx_timeout_last_recovery = jiffies;
372 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
373 		    pf->tx_timeout_recovery_level, txqueue);
374 
375 	switch (pf->tx_timeout_recovery_level) {
376 	case 1:
377 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
378 		break;
379 	case 2:
380 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
381 		break;
382 	case 3:
383 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
384 		break;
385 	default:
386 		netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
387 		break;
388 	}
389 
390 	i40e_service_event_schedule(pf);
391 	pf->tx_timeout_recovery_level++;
392 }
393 
394 /**
395  * i40e_get_vsi_stats_struct - Get System Network Statistics
396  * @vsi: the VSI we care about
397  *
398  * Returns the address of the device statistics structure.
399  * The statistics are actually updated from the service task.
400  **/
401 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
402 {
403 	return &vsi->net_stats;
404 }
405 
406 /**
407  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
408  * @ring: Tx ring to get statistics from
409  * @stats: statistics entry to be updated
410  **/
411 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
412 					    struct rtnl_link_stats64 *stats)
413 {
414 	u64 bytes, packets;
415 	unsigned int start;
416 
417 	do {
418 		start = u64_stats_fetch_begin_irq(&ring->syncp);
419 		packets = ring->stats.packets;
420 		bytes   = ring->stats.bytes;
421 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
422 
423 	stats->tx_packets += packets;
424 	stats->tx_bytes   += bytes;
425 }
426 
427 /**
428  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
429  * @netdev: network interface device structure
430  * @stats: data structure to store statistics
431  *
432  * Returns the address of the device statistics structure.
433  * The statistics are actually updated from the service task.
434  **/
435 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
436 				  struct rtnl_link_stats64 *stats)
437 {
438 	struct i40e_netdev_priv *np = netdev_priv(netdev);
439 	struct i40e_vsi *vsi = np->vsi;
440 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
441 	struct i40e_ring *ring;
442 	int i;
443 
444 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
445 		return;
446 
447 	if (!vsi->tx_rings)
448 		return;
449 
450 	rcu_read_lock();
451 	for (i = 0; i < vsi->num_queue_pairs; i++) {
452 		u64 bytes, packets;
453 		unsigned int start;
454 
455 		ring = READ_ONCE(vsi->tx_rings[i]);
456 		if (!ring)
457 			continue;
458 		i40e_get_netdev_stats_struct_tx(ring, stats);
459 
460 		if (i40e_enabled_xdp_vsi(vsi)) {
461 			ring = READ_ONCE(vsi->xdp_rings[i]);
462 			if (!ring)
463 				continue;
464 			i40e_get_netdev_stats_struct_tx(ring, stats);
465 		}
466 
467 		ring = READ_ONCE(vsi->rx_rings[i]);
468 		if (!ring)
469 			continue;
470 		do {
471 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
472 			packets = ring->stats.packets;
473 			bytes   = ring->stats.bytes;
474 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
475 
476 		stats->rx_packets += packets;
477 		stats->rx_bytes   += bytes;
478 
479 	}
480 	rcu_read_unlock();
481 
482 	/* following stats updated by i40e_watchdog_subtask() */
483 	stats->multicast	= vsi_stats->multicast;
484 	stats->tx_errors	= vsi_stats->tx_errors;
485 	stats->tx_dropped	= vsi_stats->tx_dropped;
486 	stats->rx_errors	= vsi_stats->rx_errors;
487 	stats->rx_dropped	= vsi_stats->rx_dropped;
488 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
489 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
490 }
491 
492 /**
493  * i40e_vsi_reset_stats - Resets all stats of the given vsi
494  * @vsi: the VSI to have its stats reset
495  **/
496 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
497 {
498 	struct rtnl_link_stats64 *ns;
499 	int i;
500 
501 	if (!vsi)
502 		return;
503 
504 	ns = i40e_get_vsi_stats_struct(vsi);
505 	memset(ns, 0, sizeof(*ns));
506 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
507 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
508 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
509 	if (vsi->rx_rings && vsi->rx_rings[0]) {
510 		for (i = 0; i < vsi->num_queue_pairs; i++) {
511 			memset(&vsi->rx_rings[i]->stats, 0,
512 			       sizeof(vsi->rx_rings[i]->stats));
513 			memset(&vsi->rx_rings[i]->rx_stats, 0,
514 			       sizeof(vsi->rx_rings[i]->rx_stats));
515 			memset(&vsi->tx_rings[i]->stats, 0,
516 			       sizeof(vsi->tx_rings[i]->stats));
517 			memset(&vsi->tx_rings[i]->tx_stats, 0,
518 			       sizeof(vsi->tx_rings[i]->tx_stats));
519 		}
520 	}
521 	vsi->stat_offsets_loaded = false;
522 }
523 
524 /**
525  * i40e_pf_reset_stats - Reset all of the stats for the given PF
526  * @pf: the PF to be reset
527  **/
528 void i40e_pf_reset_stats(struct i40e_pf *pf)
529 {
530 	int i;
531 
532 	memset(&pf->stats, 0, sizeof(pf->stats));
533 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
534 	pf->stat_offsets_loaded = false;
535 
536 	for (i = 0; i < I40E_MAX_VEB; i++) {
537 		if (pf->veb[i]) {
538 			memset(&pf->veb[i]->stats, 0,
539 			       sizeof(pf->veb[i]->stats));
540 			memset(&pf->veb[i]->stats_offsets, 0,
541 			       sizeof(pf->veb[i]->stats_offsets));
542 			memset(&pf->veb[i]->tc_stats, 0,
543 			       sizeof(pf->veb[i]->tc_stats));
544 			memset(&pf->veb[i]->tc_stats_offsets, 0,
545 			       sizeof(pf->veb[i]->tc_stats_offsets));
546 			pf->veb[i]->stat_offsets_loaded = false;
547 		}
548 	}
549 	pf->hw_csum_rx_error = 0;
550 }
551 
552 /**
553  * i40e_stat_update48 - read and update a 48 bit stat from the chip
554  * @hw: ptr to the hardware info
555  * @hireg: the high 32 bit reg to read
556  * @loreg: the low 32 bit reg to read
557  * @offset_loaded: has the initial offset been loaded yet
558  * @offset: ptr to current offset value
559  * @stat: ptr to the stat
560  *
561  * Since the device stats are not reset at PFReset, they likely will not
562  * be zeroed when the driver starts.  We'll save the first values read
563  * and use them as offsets to be subtracted from the raw values in order
564  * to report stats that count from zero.  In the process, we also manage
565  * the potential roll-over.
566  **/
567 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
568 			       bool offset_loaded, u64 *offset, u64 *stat)
569 {
570 	u64 new_data;
571 
572 	if (hw->device_id == I40E_DEV_ID_QEMU) {
573 		new_data = rd32(hw, loreg);
574 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
575 	} else {
576 		new_data = rd64(hw, loreg);
577 	}
578 	if (!offset_loaded)
579 		*offset = new_data;
580 	if (likely(new_data >= *offset))
581 		*stat = new_data - *offset;
582 	else
583 		*stat = (new_data + BIT_ULL(48)) - *offset;
584 	*stat &= 0xFFFFFFFFFFFFULL;
585 }
586 
587 /**
588  * i40e_stat_update32 - read and update a 32 bit stat from the chip
589  * @hw: ptr to the hardware info
590  * @reg: the hw reg to read
591  * @offset_loaded: has the initial offset been loaded yet
592  * @offset: ptr to current offset value
593  * @stat: ptr to the stat
594  **/
595 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
596 			       bool offset_loaded, u64 *offset, u64 *stat)
597 {
598 	u32 new_data;
599 
600 	new_data = rd32(hw, reg);
601 	if (!offset_loaded)
602 		*offset = new_data;
603 	if (likely(new_data >= *offset))
604 		*stat = (u32)(new_data - *offset);
605 	else
606 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
607 }
608 
609 /**
610  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
611  * @hw: ptr to the hardware info
612  * @reg: the hw reg to read and clear
613  * @stat: ptr to the stat
614  **/
615 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
616 {
617 	u32 new_data = rd32(hw, reg);
618 
619 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
620 	*stat += new_data;
621 }
622 
623 /**
624  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
625  * @vsi: the VSI to be updated
626  **/
627 void i40e_update_eth_stats(struct i40e_vsi *vsi)
628 {
629 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
630 	struct i40e_pf *pf = vsi->back;
631 	struct i40e_hw *hw = &pf->hw;
632 	struct i40e_eth_stats *oes;
633 	struct i40e_eth_stats *es;     /* device's eth stats */
634 
635 	es = &vsi->eth_stats;
636 	oes = &vsi->eth_stats_offsets;
637 
638 	/* Gather up the stats that the hw collects */
639 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
640 			   vsi->stat_offsets_loaded,
641 			   &oes->tx_errors, &es->tx_errors);
642 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
643 			   vsi->stat_offsets_loaded,
644 			   &oes->rx_discards, &es->rx_discards);
645 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
646 			   vsi->stat_offsets_loaded,
647 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
648 
649 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
650 			   I40E_GLV_GORCL(stat_idx),
651 			   vsi->stat_offsets_loaded,
652 			   &oes->rx_bytes, &es->rx_bytes);
653 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
654 			   I40E_GLV_UPRCL(stat_idx),
655 			   vsi->stat_offsets_loaded,
656 			   &oes->rx_unicast, &es->rx_unicast);
657 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
658 			   I40E_GLV_MPRCL(stat_idx),
659 			   vsi->stat_offsets_loaded,
660 			   &oes->rx_multicast, &es->rx_multicast);
661 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
662 			   I40E_GLV_BPRCL(stat_idx),
663 			   vsi->stat_offsets_loaded,
664 			   &oes->rx_broadcast, &es->rx_broadcast);
665 
666 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
667 			   I40E_GLV_GOTCL(stat_idx),
668 			   vsi->stat_offsets_loaded,
669 			   &oes->tx_bytes, &es->tx_bytes);
670 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
671 			   I40E_GLV_UPTCL(stat_idx),
672 			   vsi->stat_offsets_loaded,
673 			   &oes->tx_unicast, &es->tx_unicast);
674 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
675 			   I40E_GLV_MPTCL(stat_idx),
676 			   vsi->stat_offsets_loaded,
677 			   &oes->tx_multicast, &es->tx_multicast);
678 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
679 			   I40E_GLV_BPTCL(stat_idx),
680 			   vsi->stat_offsets_loaded,
681 			   &oes->tx_broadcast, &es->tx_broadcast);
682 	vsi->stat_offsets_loaded = true;
683 }
684 
685 /**
686  * i40e_update_veb_stats - Update Switch component statistics
687  * @veb: the VEB being updated
688  **/
689 void i40e_update_veb_stats(struct i40e_veb *veb)
690 {
691 	struct i40e_pf *pf = veb->pf;
692 	struct i40e_hw *hw = &pf->hw;
693 	struct i40e_eth_stats *oes;
694 	struct i40e_eth_stats *es;     /* device's eth stats */
695 	struct i40e_veb_tc_stats *veb_oes;
696 	struct i40e_veb_tc_stats *veb_es;
697 	int i, idx = 0;
698 
699 	idx = veb->stats_idx;
700 	es = &veb->stats;
701 	oes = &veb->stats_offsets;
702 	veb_es = &veb->tc_stats;
703 	veb_oes = &veb->tc_stats_offsets;
704 
705 	/* Gather up the stats that the hw collects */
706 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
707 			   veb->stat_offsets_loaded,
708 			   &oes->tx_discards, &es->tx_discards);
709 	if (hw->revision_id > 0)
710 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
711 				   veb->stat_offsets_loaded,
712 				   &oes->rx_unknown_protocol,
713 				   &es->rx_unknown_protocol);
714 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
715 			   veb->stat_offsets_loaded,
716 			   &oes->rx_bytes, &es->rx_bytes);
717 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
718 			   veb->stat_offsets_loaded,
719 			   &oes->rx_unicast, &es->rx_unicast);
720 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
721 			   veb->stat_offsets_loaded,
722 			   &oes->rx_multicast, &es->rx_multicast);
723 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
724 			   veb->stat_offsets_loaded,
725 			   &oes->rx_broadcast, &es->rx_broadcast);
726 
727 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
728 			   veb->stat_offsets_loaded,
729 			   &oes->tx_bytes, &es->tx_bytes);
730 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
731 			   veb->stat_offsets_loaded,
732 			   &oes->tx_unicast, &es->tx_unicast);
733 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
734 			   veb->stat_offsets_loaded,
735 			   &oes->tx_multicast, &es->tx_multicast);
736 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
737 			   veb->stat_offsets_loaded,
738 			   &oes->tx_broadcast, &es->tx_broadcast);
739 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
740 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
741 				   I40E_GLVEBTC_RPCL(i, idx),
742 				   veb->stat_offsets_loaded,
743 				   &veb_oes->tc_rx_packets[i],
744 				   &veb_es->tc_rx_packets[i]);
745 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
746 				   I40E_GLVEBTC_RBCL(i, idx),
747 				   veb->stat_offsets_loaded,
748 				   &veb_oes->tc_rx_bytes[i],
749 				   &veb_es->tc_rx_bytes[i]);
750 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
751 				   I40E_GLVEBTC_TPCL(i, idx),
752 				   veb->stat_offsets_loaded,
753 				   &veb_oes->tc_tx_packets[i],
754 				   &veb_es->tc_tx_packets[i]);
755 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
756 				   I40E_GLVEBTC_TBCL(i, idx),
757 				   veb->stat_offsets_loaded,
758 				   &veb_oes->tc_tx_bytes[i],
759 				   &veb_es->tc_tx_bytes[i]);
760 	}
761 	veb->stat_offsets_loaded = true;
762 }
763 
764 /**
765  * i40e_update_vsi_stats - Update the vsi statistics counters.
766  * @vsi: the VSI to be updated
767  *
768  * There are a few instances where we store the same stat in a
769  * couple of different structs.  This is partly because we have
770  * the netdev stats that need to be filled out, which is slightly
771  * different from the "eth_stats" defined by the chip and used in
772  * VF communications.  We sort it out here.
773  **/
774 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
775 {
776 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
777 	struct i40e_pf *pf = vsi->back;
778 	struct rtnl_link_stats64 *ons;
779 	struct rtnl_link_stats64 *ns;   /* netdev stats */
780 	struct i40e_eth_stats *oes;
781 	struct i40e_eth_stats *es;     /* device's eth stats */
782 	u64 tx_restart, tx_busy;
783 	struct i40e_ring *p;
784 	u64 bytes, packets;
785 	unsigned int start;
786 	u64 tx_linearize;
787 	u64 tx_force_wb;
788 	u64 rx_p, rx_b;
789 	u64 tx_p, tx_b;
790 	u16 q;
791 
792 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
793 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
794 		return;
795 
796 	ns = i40e_get_vsi_stats_struct(vsi);
797 	ons = &vsi->net_stats_offsets;
798 	es = &vsi->eth_stats;
799 	oes = &vsi->eth_stats_offsets;
800 
801 	/* Gather up the netdev and vsi stats that the driver collects
802 	 * on the fly during packet processing
803 	 */
804 	rx_b = rx_p = 0;
805 	tx_b = tx_p = 0;
806 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
807 	rx_page = 0;
808 	rx_buf = 0;
809 	rx_reuse = 0;
810 	rx_alloc = 0;
811 	rx_waive = 0;
812 	rx_busy = 0;
813 	rcu_read_lock();
814 	for (q = 0; q < vsi->num_queue_pairs; q++) {
815 		/* locate Tx ring */
816 		p = READ_ONCE(vsi->tx_rings[q]);
817 		if (!p)
818 			continue;
819 
820 		do {
821 			start = u64_stats_fetch_begin_irq(&p->syncp);
822 			packets = p->stats.packets;
823 			bytes = p->stats.bytes;
824 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
825 		tx_b += bytes;
826 		tx_p += packets;
827 		tx_restart += p->tx_stats.restart_queue;
828 		tx_busy += p->tx_stats.tx_busy;
829 		tx_linearize += p->tx_stats.tx_linearize;
830 		tx_force_wb += p->tx_stats.tx_force_wb;
831 
832 		/* locate Rx ring */
833 		p = READ_ONCE(vsi->rx_rings[q]);
834 		if (!p)
835 			continue;
836 
837 		do {
838 			start = u64_stats_fetch_begin_irq(&p->syncp);
839 			packets = p->stats.packets;
840 			bytes = p->stats.bytes;
841 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
842 		rx_b += bytes;
843 		rx_p += packets;
844 		rx_buf += p->rx_stats.alloc_buff_failed;
845 		rx_page += p->rx_stats.alloc_page_failed;
846 		rx_reuse += p->rx_stats.page_reuse_count;
847 		rx_alloc += p->rx_stats.page_alloc_count;
848 		rx_waive += p->rx_stats.page_waive_count;
849 		rx_busy += p->rx_stats.page_busy_count;
850 
851 		if (i40e_enabled_xdp_vsi(vsi)) {
852 			/* locate XDP ring */
853 			p = READ_ONCE(vsi->xdp_rings[q]);
854 			if (!p)
855 				continue;
856 
857 			do {
858 				start = u64_stats_fetch_begin_irq(&p->syncp);
859 				packets = p->stats.packets;
860 				bytes = p->stats.bytes;
861 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
862 			tx_b += bytes;
863 			tx_p += packets;
864 			tx_restart += p->tx_stats.restart_queue;
865 			tx_busy += p->tx_stats.tx_busy;
866 			tx_linearize += p->tx_stats.tx_linearize;
867 			tx_force_wb += p->tx_stats.tx_force_wb;
868 		}
869 	}
870 	rcu_read_unlock();
871 	vsi->tx_restart = tx_restart;
872 	vsi->tx_busy = tx_busy;
873 	vsi->tx_linearize = tx_linearize;
874 	vsi->tx_force_wb = tx_force_wb;
875 	vsi->rx_page_failed = rx_page;
876 	vsi->rx_buf_failed = rx_buf;
877 	vsi->rx_page_reuse = rx_reuse;
878 	vsi->rx_page_alloc = rx_alloc;
879 	vsi->rx_page_waive = rx_waive;
880 	vsi->rx_page_busy = rx_busy;
881 
882 	ns->rx_packets = rx_p;
883 	ns->rx_bytes = rx_b;
884 	ns->tx_packets = tx_p;
885 	ns->tx_bytes = tx_b;
886 
887 	/* update netdev stats from eth stats */
888 	i40e_update_eth_stats(vsi);
889 	ons->tx_errors = oes->tx_errors;
890 	ns->tx_errors = es->tx_errors;
891 	ons->multicast = oes->rx_multicast;
892 	ns->multicast = es->rx_multicast;
893 	ons->rx_dropped = oes->rx_discards;
894 	ns->rx_dropped = es->rx_discards;
895 	ons->tx_dropped = oes->tx_discards;
896 	ns->tx_dropped = es->tx_discards;
897 
898 	/* pull in a couple PF stats if this is the main vsi */
899 	if (vsi == pf->vsi[pf->lan_vsi]) {
900 		ns->rx_crc_errors = pf->stats.crc_errors;
901 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
902 		ns->rx_length_errors = pf->stats.rx_length_errors;
903 	}
904 }
905 
906 /**
907  * i40e_update_pf_stats - Update the PF statistics counters.
908  * @pf: the PF to be updated
909  **/
910 static void i40e_update_pf_stats(struct i40e_pf *pf)
911 {
912 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
913 	struct i40e_hw_port_stats *nsd = &pf->stats;
914 	struct i40e_hw *hw = &pf->hw;
915 	u32 val;
916 	int i;
917 
918 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
919 			   I40E_GLPRT_GORCL(hw->port),
920 			   pf->stat_offsets_loaded,
921 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
922 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
923 			   I40E_GLPRT_GOTCL(hw->port),
924 			   pf->stat_offsets_loaded,
925 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
926 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
927 			   pf->stat_offsets_loaded,
928 			   &osd->eth.rx_discards,
929 			   &nsd->eth.rx_discards);
930 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
931 			   I40E_GLPRT_UPRCL(hw->port),
932 			   pf->stat_offsets_loaded,
933 			   &osd->eth.rx_unicast,
934 			   &nsd->eth.rx_unicast);
935 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
936 			   I40E_GLPRT_MPRCL(hw->port),
937 			   pf->stat_offsets_loaded,
938 			   &osd->eth.rx_multicast,
939 			   &nsd->eth.rx_multicast);
940 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
941 			   I40E_GLPRT_BPRCL(hw->port),
942 			   pf->stat_offsets_loaded,
943 			   &osd->eth.rx_broadcast,
944 			   &nsd->eth.rx_broadcast);
945 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
946 			   I40E_GLPRT_UPTCL(hw->port),
947 			   pf->stat_offsets_loaded,
948 			   &osd->eth.tx_unicast,
949 			   &nsd->eth.tx_unicast);
950 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
951 			   I40E_GLPRT_MPTCL(hw->port),
952 			   pf->stat_offsets_loaded,
953 			   &osd->eth.tx_multicast,
954 			   &nsd->eth.tx_multicast);
955 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
956 			   I40E_GLPRT_BPTCL(hw->port),
957 			   pf->stat_offsets_loaded,
958 			   &osd->eth.tx_broadcast,
959 			   &nsd->eth.tx_broadcast);
960 
961 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
962 			   pf->stat_offsets_loaded,
963 			   &osd->tx_dropped_link_down,
964 			   &nsd->tx_dropped_link_down);
965 
966 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
967 			   pf->stat_offsets_loaded,
968 			   &osd->crc_errors, &nsd->crc_errors);
969 
970 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
971 			   pf->stat_offsets_loaded,
972 			   &osd->illegal_bytes, &nsd->illegal_bytes);
973 
974 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
975 			   pf->stat_offsets_loaded,
976 			   &osd->mac_local_faults,
977 			   &nsd->mac_local_faults);
978 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
979 			   pf->stat_offsets_loaded,
980 			   &osd->mac_remote_faults,
981 			   &nsd->mac_remote_faults);
982 
983 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
984 			   pf->stat_offsets_loaded,
985 			   &osd->rx_length_errors,
986 			   &nsd->rx_length_errors);
987 
988 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
989 			   pf->stat_offsets_loaded,
990 			   &osd->link_xon_rx, &nsd->link_xon_rx);
991 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
992 			   pf->stat_offsets_loaded,
993 			   &osd->link_xon_tx, &nsd->link_xon_tx);
994 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
995 			   pf->stat_offsets_loaded,
996 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
997 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
998 			   pf->stat_offsets_loaded,
999 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1000 
1001 	for (i = 0; i < 8; i++) {
1002 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1003 				   pf->stat_offsets_loaded,
1004 				   &osd->priority_xoff_rx[i],
1005 				   &nsd->priority_xoff_rx[i]);
1006 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1007 				   pf->stat_offsets_loaded,
1008 				   &osd->priority_xon_rx[i],
1009 				   &nsd->priority_xon_rx[i]);
1010 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1011 				   pf->stat_offsets_loaded,
1012 				   &osd->priority_xon_tx[i],
1013 				   &nsd->priority_xon_tx[i]);
1014 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1015 				   pf->stat_offsets_loaded,
1016 				   &osd->priority_xoff_tx[i],
1017 				   &nsd->priority_xoff_tx[i]);
1018 		i40e_stat_update32(hw,
1019 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1020 				   pf->stat_offsets_loaded,
1021 				   &osd->priority_xon_2_xoff[i],
1022 				   &nsd->priority_xon_2_xoff[i]);
1023 	}
1024 
1025 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1026 			   I40E_GLPRT_PRC64L(hw->port),
1027 			   pf->stat_offsets_loaded,
1028 			   &osd->rx_size_64, &nsd->rx_size_64);
1029 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1030 			   I40E_GLPRT_PRC127L(hw->port),
1031 			   pf->stat_offsets_loaded,
1032 			   &osd->rx_size_127, &nsd->rx_size_127);
1033 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1034 			   I40E_GLPRT_PRC255L(hw->port),
1035 			   pf->stat_offsets_loaded,
1036 			   &osd->rx_size_255, &nsd->rx_size_255);
1037 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1038 			   I40E_GLPRT_PRC511L(hw->port),
1039 			   pf->stat_offsets_loaded,
1040 			   &osd->rx_size_511, &nsd->rx_size_511);
1041 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1042 			   I40E_GLPRT_PRC1023L(hw->port),
1043 			   pf->stat_offsets_loaded,
1044 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1045 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1046 			   I40E_GLPRT_PRC1522L(hw->port),
1047 			   pf->stat_offsets_loaded,
1048 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1049 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1050 			   I40E_GLPRT_PRC9522L(hw->port),
1051 			   pf->stat_offsets_loaded,
1052 			   &osd->rx_size_big, &nsd->rx_size_big);
1053 
1054 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1055 			   I40E_GLPRT_PTC64L(hw->port),
1056 			   pf->stat_offsets_loaded,
1057 			   &osd->tx_size_64, &nsd->tx_size_64);
1058 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1059 			   I40E_GLPRT_PTC127L(hw->port),
1060 			   pf->stat_offsets_loaded,
1061 			   &osd->tx_size_127, &nsd->tx_size_127);
1062 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1063 			   I40E_GLPRT_PTC255L(hw->port),
1064 			   pf->stat_offsets_loaded,
1065 			   &osd->tx_size_255, &nsd->tx_size_255);
1066 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1067 			   I40E_GLPRT_PTC511L(hw->port),
1068 			   pf->stat_offsets_loaded,
1069 			   &osd->tx_size_511, &nsd->tx_size_511);
1070 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1071 			   I40E_GLPRT_PTC1023L(hw->port),
1072 			   pf->stat_offsets_loaded,
1073 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1074 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1075 			   I40E_GLPRT_PTC1522L(hw->port),
1076 			   pf->stat_offsets_loaded,
1077 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1078 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1079 			   I40E_GLPRT_PTC9522L(hw->port),
1080 			   pf->stat_offsets_loaded,
1081 			   &osd->tx_size_big, &nsd->tx_size_big);
1082 
1083 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1084 			   pf->stat_offsets_loaded,
1085 			   &osd->rx_undersize, &nsd->rx_undersize);
1086 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1087 			   pf->stat_offsets_loaded,
1088 			   &osd->rx_fragments, &nsd->rx_fragments);
1089 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1090 			   pf->stat_offsets_loaded,
1091 			   &osd->rx_oversize, &nsd->rx_oversize);
1092 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1093 			   pf->stat_offsets_loaded,
1094 			   &osd->rx_jabber, &nsd->rx_jabber);
1095 
1096 	/* FDIR stats */
1097 	i40e_stat_update_and_clear32(hw,
1098 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1099 			&nsd->fd_atr_match);
1100 	i40e_stat_update_and_clear32(hw,
1101 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1102 			&nsd->fd_sb_match);
1103 	i40e_stat_update_and_clear32(hw,
1104 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1105 			&nsd->fd_atr_tunnel_match);
1106 
1107 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1108 	nsd->tx_lpi_status =
1109 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1110 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1111 	nsd->rx_lpi_status =
1112 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1113 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1114 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1115 			   pf->stat_offsets_loaded,
1116 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1117 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1118 			   pf->stat_offsets_loaded,
1119 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1120 
1121 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1122 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1123 		nsd->fd_sb_status = true;
1124 	else
1125 		nsd->fd_sb_status = false;
1126 
1127 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1128 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1129 		nsd->fd_atr_status = true;
1130 	else
1131 		nsd->fd_atr_status = false;
1132 
1133 	pf->stat_offsets_loaded = true;
1134 }
1135 
1136 /**
1137  * i40e_update_stats - Update the various statistics counters.
1138  * @vsi: the VSI to be updated
1139  *
1140  * Update the various stats for this VSI and its related entities.
1141  **/
1142 void i40e_update_stats(struct i40e_vsi *vsi)
1143 {
1144 	struct i40e_pf *pf = vsi->back;
1145 
1146 	if (vsi == pf->vsi[pf->lan_vsi])
1147 		i40e_update_pf_stats(pf);
1148 
1149 	i40e_update_vsi_stats(vsi);
1150 }
1151 
1152 /**
1153  * i40e_count_filters - counts VSI mac filters
1154  * @vsi: the VSI to be searched
1155  *
1156  * Returns count of mac filters
1157  **/
1158 int i40e_count_filters(struct i40e_vsi *vsi)
1159 {
1160 	struct i40e_mac_filter *f;
1161 	struct hlist_node *h;
1162 	int bkt;
1163 	int cnt = 0;
1164 
1165 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1166 		++cnt;
1167 
1168 	return cnt;
1169 }
1170 
1171 /**
1172  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1173  * @vsi: the VSI to be searched
1174  * @macaddr: the MAC address
1175  * @vlan: the vlan
1176  *
1177  * Returns ptr to the filter object or NULL
1178  **/
1179 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1180 						const u8 *macaddr, s16 vlan)
1181 {
1182 	struct i40e_mac_filter *f;
1183 	u64 key;
1184 
1185 	if (!vsi || !macaddr)
1186 		return NULL;
1187 
1188 	key = i40e_addr_to_hkey(macaddr);
1189 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1190 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1191 		    (vlan == f->vlan))
1192 			return f;
1193 	}
1194 	return NULL;
1195 }
1196 
1197 /**
1198  * i40e_find_mac - Find a mac addr in the macvlan filters list
1199  * @vsi: the VSI to be searched
1200  * @macaddr: the MAC address we are searching for
1201  *
1202  * Returns the first filter with the provided MAC address or NULL if
1203  * MAC address was not found
1204  **/
1205 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1206 {
1207 	struct i40e_mac_filter *f;
1208 	u64 key;
1209 
1210 	if (!vsi || !macaddr)
1211 		return NULL;
1212 
1213 	key = i40e_addr_to_hkey(macaddr);
1214 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1215 		if ((ether_addr_equal(macaddr, f->macaddr)))
1216 			return f;
1217 	}
1218 	return NULL;
1219 }
1220 
1221 /**
1222  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1223  * @vsi: the VSI to be searched
1224  *
1225  * Returns true if VSI is in vlan mode or false otherwise
1226  **/
1227 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1228 {
1229 	/* If we have a PVID, always operate in VLAN mode */
1230 	if (vsi->info.pvid)
1231 		return true;
1232 
1233 	/* We need to operate in VLAN mode whenever we have any filters with
1234 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1235 	 * time, incurring search cost repeatedly. However, we can notice two
1236 	 * things:
1237 	 *
1238 	 * 1) the only place where we can gain a VLAN filter is in
1239 	 *    i40e_add_filter.
1240 	 *
1241 	 * 2) the only place where filters are actually removed is in
1242 	 *    i40e_sync_filters_subtask.
1243 	 *
1244 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1245 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1246 	 * we have to perform the full search after deleting filters in
1247 	 * i40e_sync_filters_subtask, but we already have to search
1248 	 * filters here and can perform the check at the same time. This
1249 	 * results in avoiding embedding a loop for VLAN mode inside another
1250 	 * loop over all the filters, and should maintain correctness as noted
1251 	 * above.
1252 	 */
1253 	return vsi->has_vlan_filter;
1254 }
1255 
1256 /**
1257  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1258  * @vsi: the VSI to configure
1259  * @tmp_add_list: list of filters ready to be added
1260  * @tmp_del_list: list of filters ready to be deleted
1261  * @vlan_filters: the number of active VLAN filters
1262  *
1263  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1264  * behave as expected. If we have any active VLAN filters remaining or about
1265  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1266  * so that they only match against untagged traffic. If we no longer have any
1267  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1268  * so that they match against both tagged and untagged traffic. In this way,
1269  * we ensure that we correctly receive the desired traffic. This ensures that
1270  * when we have an active VLAN we will receive only untagged traffic and
1271  * traffic matching active VLANs. If we have no active VLANs then we will
1272  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1273  *
1274  * Finally, in a similar fashion, this function also corrects filters when
1275  * there is an active PVID assigned to this VSI.
1276  *
1277  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1278  *
1279  * This function is only expected to be called from within
1280  * i40e_sync_vsi_filters.
1281  *
1282  * NOTE: This function expects to be called while under the
1283  * mac_filter_hash_lock
1284  */
1285 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1286 					 struct hlist_head *tmp_add_list,
1287 					 struct hlist_head *tmp_del_list,
1288 					 int vlan_filters)
1289 {
1290 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1291 	struct i40e_mac_filter *f, *add_head;
1292 	struct i40e_new_mac_filter *new;
1293 	struct hlist_node *h;
1294 	int bkt, new_vlan;
1295 
1296 	/* To determine if a particular filter needs to be replaced we
1297 	 * have the three following conditions:
1298 	 *
1299 	 * a) if we have a PVID assigned, then all filters which are
1300 	 *    not marked as VLAN=PVID must be replaced with filters that
1301 	 *    are.
1302 	 * b) otherwise, if we have any active VLANS, all filters
1303 	 *    which are marked as VLAN=-1 must be replaced with
1304 	 *    filters marked as VLAN=0
1305 	 * c) finally, if we do not have any active VLANS, all filters
1306 	 *    which are marked as VLAN=0 must be replaced with filters
1307 	 *    marked as VLAN=-1
1308 	 */
1309 
1310 	/* Update the filters about to be added in place */
1311 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1312 		if (pvid && new->f->vlan != pvid)
1313 			new->f->vlan = pvid;
1314 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1315 			new->f->vlan = 0;
1316 		else if (!vlan_filters && new->f->vlan == 0)
1317 			new->f->vlan = I40E_VLAN_ANY;
1318 	}
1319 
1320 	/* Update the remaining active filters */
1321 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1322 		/* Combine the checks for whether a filter needs to be changed
1323 		 * and then determine the new VLAN inside the if block, in
1324 		 * order to avoid duplicating code for adding the new filter
1325 		 * then deleting the old filter.
1326 		 */
1327 		if ((pvid && f->vlan != pvid) ||
1328 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1329 		    (!vlan_filters && f->vlan == 0)) {
1330 			/* Determine the new vlan we will be adding */
1331 			if (pvid)
1332 				new_vlan = pvid;
1333 			else if (vlan_filters)
1334 				new_vlan = 0;
1335 			else
1336 				new_vlan = I40E_VLAN_ANY;
1337 
1338 			/* Create the new filter */
1339 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1340 			if (!add_head)
1341 				return -ENOMEM;
1342 
1343 			/* Create a temporary i40e_new_mac_filter */
1344 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1345 			if (!new)
1346 				return -ENOMEM;
1347 
1348 			new->f = add_head;
1349 			new->state = add_head->state;
1350 
1351 			/* Add the new filter to the tmp list */
1352 			hlist_add_head(&new->hlist, tmp_add_list);
1353 
1354 			/* Put the original filter into the delete list */
1355 			f->state = I40E_FILTER_REMOVE;
1356 			hash_del(&f->hlist);
1357 			hlist_add_head(&f->hlist, tmp_del_list);
1358 		}
1359 	}
1360 
1361 	vsi->has_vlan_filter = !!vlan_filters;
1362 
1363 	return 0;
1364 }
1365 
1366 /**
1367  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1368  * @vsi: the PF Main VSI - inappropriate for any other VSI
1369  * @macaddr: the MAC address
1370  *
1371  * Remove whatever filter the firmware set up so the driver can manage
1372  * its own filtering intelligently.
1373  **/
1374 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1375 {
1376 	struct i40e_aqc_remove_macvlan_element_data element;
1377 	struct i40e_pf *pf = vsi->back;
1378 
1379 	/* Only appropriate for the PF main VSI */
1380 	if (vsi->type != I40E_VSI_MAIN)
1381 		return;
1382 
1383 	memset(&element, 0, sizeof(element));
1384 	ether_addr_copy(element.mac_addr, macaddr);
1385 	element.vlan_tag = 0;
1386 	/* Ignore error returns, some firmware does it this way... */
1387 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1388 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1389 
1390 	memset(&element, 0, sizeof(element));
1391 	ether_addr_copy(element.mac_addr, macaddr);
1392 	element.vlan_tag = 0;
1393 	/* ...and some firmware does it this way. */
1394 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1395 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1396 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1397 }
1398 
1399 /**
1400  * i40e_add_filter - Add a mac/vlan filter to the VSI
1401  * @vsi: the VSI to be searched
1402  * @macaddr: the MAC address
1403  * @vlan: the vlan
1404  *
1405  * Returns ptr to the filter object or NULL when no memory available.
1406  *
1407  * NOTE: This function is expected to be called with mac_filter_hash_lock
1408  * being held.
1409  **/
1410 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1411 					const u8 *macaddr, s16 vlan)
1412 {
1413 	struct i40e_mac_filter *f;
1414 	u64 key;
1415 
1416 	if (!vsi || !macaddr)
1417 		return NULL;
1418 
1419 	f = i40e_find_filter(vsi, macaddr, vlan);
1420 	if (!f) {
1421 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1422 		if (!f)
1423 			return NULL;
1424 
1425 		/* Update the boolean indicating if we need to function in
1426 		 * VLAN mode.
1427 		 */
1428 		if (vlan >= 0)
1429 			vsi->has_vlan_filter = true;
1430 
1431 		ether_addr_copy(f->macaddr, macaddr);
1432 		f->vlan = vlan;
1433 		f->state = I40E_FILTER_NEW;
1434 		INIT_HLIST_NODE(&f->hlist);
1435 
1436 		key = i40e_addr_to_hkey(macaddr);
1437 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1438 
1439 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1440 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1441 	}
1442 
1443 	/* If we're asked to add a filter that has been marked for removal, it
1444 	 * is safe to simply restore it to active state. __i40e_del_filter
1445 	 * will have simply deleted any filters which were previously marked
1446 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1447 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1448 	 * task, just restore this filter to the ACTIVE state so that the
1449 	 * sync task leaves it in place
1450 	 */
1451 	if (f->state == I40E_FILTER_REMOVE)
1452 		f->state = I40E_FILTER_ACTIVE;
1453 
1454 	return f;
1455 }
1456 
1457 /**
1458  * __i40e_del_filter - Remove a specific filter from the VSI
1459  * @vsi: VSI to remove from
1460  * @f: the filter to remove from the list
1461  *
1462  * This function should be called instead of i40e_del_filter only if you know
1463  * the exact filter you will remove already, such as via i40e_find_filter or
1464  * i40e_find_mac.
1465  *
1466  * NOTE: This function is expected to be called with mac_filter_hash_lock
1467  * being held.
1468  * ANOTHER NOTE: This function MUST be called from within the context of
1469  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1470  * instead of list_for_each_entry().
1471  **/
1472 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1473 {
1474 	if (!f)
1475 		return;
1476 
1477 	/* If the filter was never added to firmware then we can just delete it
1478 	 * directly and we don't want to set the status to remove or else an
1479 	 * admin queue command will unnecessarily fire.
1480 	 */
1481 	if ((f->state == I40E_FILTER_FAILED) ||
1482 	    (f->state == I40E_FILTER_NEW)) {
1483 		hash_del(&f->hlist);
1484 		kfree(f);
1485 	} else {
1486 		f->state = I40E_FILTER_REMOVE;
1487 	}
1488 
1489 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1490 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1491 }
1492 
1493 /**
1494  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1495  * @vsi: the VSI to be searched
1496  * @macaddr: the MAC address
1497  * @vlan: the VLAN
1498  *
1499  * NOTE: This function is expected to be called with mac_filter_hash_lock
1500  * being held.
1501  * ANOTHER NOTE: This function MUST be called from within the context of
1502  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1503  * instead of list_for_each_entry().
1504  **/
1505 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1506 {
1507 	struct i40e_mac_filter *f;
1508 
1509 	if (!vsi || !macaddr)
1510 		return;
1511 
1512 	f = i40e_find_filter(vsi, macaddr, vlan);
1513 	__i40e_del_filter(vsi, f);
1514 }
1515 
1516 /**
1517  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1518  * @vsi: the VSI to be searched
1519  * @macaddr: the mac address to be filtered
1520  *
1521  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1522  * go through all the macvlan filters and add a macvlan filter for each
1523  * unique vlan that already exists. If a PVID has been assigned, instead only
1524  * add the macaddr to that VLAN.
1525  *
1526  * Returns last filter added on success, else NULL
1527  **/
1528 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1529 					    const u8 *macaddr)
1530 {
1531 	struct i40e_mac_filter *f, *add = NULL;
1532 	struct hlist_node *h;
1533 	int bkt;
1534 
1535 	if (vsi->info.pvid)
1536 		return i40e_add_filter(vsi, macaddr,
1537 				       le16_to_cpu(vsi->info.pvid));
1538 
1539 	if (!i40e_is_vsi_in_vlan(vsi))
1540 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1541 
1542 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1543 		if (f->state == I40E_FILTER_REMOVE)
1544 			continue;
1545 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1546 		if (!add)
1547 			return NULL;
1548 	}
1549 
1550 	return add;
1551 }
1552 
1553 /**
1554  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1555  * @vsi: the VSI to be searched
1556  * @macaddr: the mac address to be removed
1557  *
1558  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1559  * associated with.
1560  *
1561  * Returns 0 for success, or error
1562  **/
1563 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1564 {
1565 	struct i40e_mac_filter *f;
1566 	struct hlist_node *h;
1567 	bool found = false;
1568 	int bkt;
1569 
1570 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1571 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1572 		if (ether_addr_equal(macaddr, f->macaddr)) {
1573 			__i40e_del_filter(vsi, f);
1574 			found = true;
1575 		}
1576 	}
1577 
1578 	if (found)
1579 		return 0;
1580 	else
1581 		return -ENOENT;
1582 }
1583 
1584 /**
1585  * i40e_set_mac - NDO callback to set mac address
1586  * @netdev: network interface device structure
1587  * @p: pointer to an address structure
1588  *
1589  * Returns 0 on success, negative on failure
1590  **/
1591 static int i40e_set_mac(struct net_device *netdev, void *p)
1592 {
1593 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1594 	struct i40e_vsi *vsi = np->vsi;
1595 	struct i40e_pf *pf = vsi->back;
1596 	struct i40e_hw *hw = &pf->hw;
1597 	struct sockaddr *addr = p;
1598 
1599 	if (!is_valid_ether_addr(addr->sa_data))
1600 		return -EADDRNOTAVAIL;
1601 
1602 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1603 		netdev_info(netdev, "already using mac address %pM\n",
1604 			    addr->sa_data);
1605 		return 0;
1606 	}
1607 
1608 	if (test_bit(__I40E_DOWN, pf->state) ||
1609 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1610 		return -EADDRNOTAVAIL;
1611 
1612 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1613 		netdev_info(netdev, "returning to hw mac address %pM\n",
1614 			    hw->mac.addr);
1615 	else
1616 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1617 
1618 	/* Copy the address first, so that we avoid a possible race with
1619 	 * .set_rx_mode().
1620 	 * - Remove old address from MAC filter
1621 	 * - Copy new address
1622 	 * - Add new address to MAC filter
1623 	 */
1624 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1625 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1626 	eth_hw_addr_set(netdev, addr->sa_data);
1627 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1628 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1629 
1630 	if (vsi->type == I40E_VSI_MAIN) {
1631 		i40e_status ret;
1632 
1633 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1634 						addr->sa_data, NULL);
1635 		if (ret)
1636 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1637 				    i40e_stat_str(hw, ret),
1638 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1639 	}
1640 
1641 	/* schedule our worker thread which will take care of
1642 	 * applying the new filter changes
1643 	 */
1644 	i40e_service_event_schedule(pf);
1645 	return 0;
1646 }
1647 
1648 /**
1649  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1650  * @vsi: vsi structure
1651  * @seed: RSS hash seed
1652  * @lut: pointer to lookup table of lut_size
1653  * @lut_size: size of the lookup table
1654  **/
1655 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1656 			      u8 *lut, u16 lut_size)
1657 {
1658 	struct i40e_pf *pf = vsi->back;
1659 	struct i40e_hw *hw = &pf->hw;
1660 	int ret = 0;
1661 
1662 	if (seed) {
1663 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1664 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1665 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1666 		if (ret) {
1667 			dev_info(&pf->pdev->dev,
1668 				 "Cannot set RSS key, err %s aq_err %s\n",
1669 				 i40e_stat_str(hw, ret),
1670 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1671 			return ret;
1672 		}
1673 	}
1674 	if (lut) {
1675 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1676 
1677 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1678 		if (ret) {
1679 			dev_info(&pf->pdev->dev,
1680 				 "Cannot set RSS lut, err %s aq_err %s\n",
1681 				 i40e_stat_str(hw, ret),
1682 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1683 			return ret;
1684 		}
1685 	}
1686 	return ret;
1687 }
1688 
1689 /**
1690  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1691  * @vsi: VSI structure
1692  **/
1693 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1694 {
1695 	struct i40e_pf *pf = vsi->back;
1696 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1697 	u8 *lut;
1698 	int ret;
1699 
1700 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1701 		return 0;
1702 	if (!vsi->rss_size)
1703 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1704 				      vsi->num_queue_pairs);
1705 	if (!vsi->rss_size)
1706 		return -EINVAL;
1707 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1708 	if (!lut)
1709 		return -ENOMEM;
1710 
1711 	/* Use the user configured hash keys and lookup table if there is one,
1712 	 * otherwise use default
1713 	 */
1714 	if (vsi->rss_lut_user)
1715 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1716 	else
1717 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1718 	if (vsi->rss_hkey_user)
1719 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1720 	else
1721 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1722 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1723 	kfree(lut);
1724 	return ret;
1725 }
1726 
1727 /**
1728  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1729  * @vsi: the VSI being configured,
1730  * @ctxt: VSI context structure
1731  * @enabled_tc: number of traffic classes to enable
1732  *
1733  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1734  **/
1735 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1736 					   struct i40e_vsi_context *ctxt,
1737 					   u8 enabled_tc)
1738 {
1739 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1740 	int i, override_q, pow, num_qps, ret;
1741 	u8 netdev_tc = 0, offset = 0;
1742 
1743 	if (vsi->type != I40E_VSI_MAIN)
1744 		return -EINVAL;
1745 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1746 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1747 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1748 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1749 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1750 
1751 	/* find the next higher power-of-2 of num queue pairs */
1752 	pow = ilog2(num_qps);
1753 	if (!is_power_of_2(num_qps))
1754 		pow++;
1755 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1756 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1757 
1758 	/* Setup queue offset/count for all TCs for given VSI */
1759 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1760 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1761 		/* See if the given TC is enabled for the given VSI */
1762 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1763 			offset = vsi->mqprio_qopt.qopt.offset[i];
1764 			qcount = vsi->mqprio_qopt.qopt.count[i];
1765 			if (qcount > max_qcount)
1766 				max_qcount = qcount;
1767 			vsi->tc_config.tc_info[i].qoffset = offset;
1768 			vsi->tc_config.tc_info[i].qcount = qcount;
1769 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1770 		} else {
1771 			/* TC is not enabled so set the offset to
1772 			 * default queue and allocate one queue
1773 			 * for the given TC.
1774 			 */
1775 			vsi->tc_config.tc_info[i].qoffset = 0;
1776 			vsi->tc_config.tc_info[i].qcount = 1;
1777 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1778 		}
1779 	}
1780 
1781 	/* Set actual Tx/Rx queue pairs */
1782 	vsi->num_queue_pairs = offset + qcount;
1783 
1784 	/* Setup queue TC[0].qmap for given VSI context */
1785 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1786 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1787 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1788 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1789 
1790 	/* Reconfigure RSS for main VSI with max queue count */
1791 	vsi->rss_size = max_qcount;
1792 	ret = i40e_vsi_config_rss(vsi);
1793 	if (ret) {
1794 		dev_info(&vsi->back->pdev->dev,
1795 			 "Failed to reconfig rss for num_queues (%u)\n",
1796 			 max_qcount);
1797 		return ret;
1798 	}
1799 	vsi->reconfig_rss = true;
1800 	dev_dbg(&vsi->back->pdev->dev,
1801 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1802 
1803 	/* Find queue count available for channel VSIs and starting offset
1804 	 * for channel VSIs
1805 	 */
1806 	override_q = vsi->mqprio_qopt.qopt.count[0];
1807 	if (override_q && override_q < vsi->num_queue_pairs) {
1808 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1809 		vsi->next_base_queue = override_q;
1810 	}
1811 	return 0;
1812 }
1813 
1814 /**
1815  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1816  * @vsi: the VSI being setup
1817  * @ctxt: VSI context structure
1818  * @enabled_tc: Enabled TCs bitmap
1819  * @is_add: True if called before Add VSI
1820  *
1821  * Setup VSI queue mapping for enabled traffic classes.
1822  **/
1823 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1824 				     struct i40e_vsi_context *ctxt,
1825 				     u8 enabled_tc,
1826 				     bool is_add)
1827 {
1828 	struct i40e_pf *pf = vsi->back;
1829 	u16 num_tc_qps = 0;
1830 	u16 sections = 0;
1831 	u8 netdev_tc = 0;
1832 	u16 numtc = 1;
1833 	u16 qcount;
1834 	u8 offset;
1835 	u16 qmap;
1836 	int i;
1837 
1838 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1839 	offset = 0;
1840 	/* zero out queue mapping, it will get updated on the end of the function */
1841 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
1842 
1843 	if (vsi->type == I40E_VSI_MAIN) {
1844 		/* This code helps add more queue to the VSI if we have
1845 		 * more cores than RSS can support, the higher cores will
1846 		 * be served by ATR or other filters. Furthermore, the
1847 		 * non-zero req_queue_pairs says that user requested a new
1848 		 * queue count via ethtool's set_channels, so use this
1849 		 * value for queues distribution across traffic classes
1850 		 */
1851 		if (vsi->req_queue_pairs > 0)
1852 			vsi->num_queue_pairs = vsi->req_queue_pairs;
1853 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1854 			vsi->num_queue_pairs = pf->num_lan_msix;
1855 	}
1856 
1857 	/* Number of queues per enabled TC */
1858 	if (vsi->type == I40E_VSI_MAIN ||
1859 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
1860 		num_tc_qps = vsi->num_queue_pairs;
1861 	else
1862 		num_tc_qps = vsi->alloc_queue_pairs;
1863 
1864 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1865 		/* Find numtc from enabled TC bitmap */
1866 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1867 			if (enabled_tc & BIT(i)) /* TC is enabled */
1868 				numtc++;
1869 		}
1870 		if (!numtc) {
1871 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1872 			numtc = 1;
1873 		}
1874 		num_tc_qps = num_tc_qps / numtc;
1875 		num_tc_qps = min_t(int, num_tc_qps,
1876 				   i40e_pf_get_max_q_per_tc(pf));
1877 	}
1878 
1879 	vsi->tc_config.numtc = numtc;
1880 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1881 
1882 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
1883 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1884 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
1885 
1886 	/* Setup queue offset/count for all TCs for given VSI */
1887 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1888 		/* See if the given TC is enabled for the given VSI */
1889 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1890 			/* TC is enabled */
1891 			int pow, num_qps;
1892 
1893 			switch (vsi->type) {
1894 			case I40E_VSI_MAIN:
1895 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
1896 				    I40E_FLAG_FD_ATR_ENABLED)) ||
1897 				    vsi->tc_config.enabled_tc != 1) {
1898 					qcount = min_t(int, pf->alloc_rss_size,
1899 						       num_tc_qps);
1900 					break;
1901 				}
1902 				fallthrough;
1903 			case I40E_VSI_FDIR:
1904 			case I40E_VSI_SRIOV:
1905 			case I40E_VSI_VMDQ2:
1906 			default:
1907 				qcount = num_tc_qps;
1908 				WARN_ON(i != 0);
1909 				break;
1910 			}
1911 			vsi->tc_config.tc_info[i].qoffset = offset;
1912 			vsi->tc_config.tc_info[i].qcount = qcount;
1913 
1914 			/* find the next higher power-of-2 of num queue pairs */
1915 			num_qps = qcount;
1916 			pow = 0;
1917 			while (num_qps && (BIT_ULL(pow) < qcount)) {
1918 				pow++;
1919 				num_qps >>= 1;
1920 			}
1921 
1922 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1923 			qmap =
1924 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1925 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1926 
1927 			offset += qcount;
1928 		} else {
1929 			/* TC is not enabled so set the offset to
1930 			 * default queue and allocate one queue
1931 			 * for the given TC.
1932 			 */
1933 			vsi->tc_config.tc_info[i].qoffset = 0;
1934 			vsi->tc_config.tc_info[i].qcount = 1;
1935 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1936 
1937 			qmap = 0;
1938 		}
1939 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1940 	}
1941 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
1942 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
1943 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
1944 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
1945 		vsi->num_queue_pairs = offset;
1946 
1947 	/* Scheduler section valid can only be set for ADD VSI */
1948 	if (is_add) {
1949 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1950 
1951 		ctxt->info.up_enable_bits = enabled_tc;
1952 	}
1953 	if (vsi->type == I40E_VSI_SRIOV) {
1954 		ctxt->info.mapping_flags |=
1955 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1956 		for (i = 0; i < vsi->num_queue_pairs; i++)
1957 			ctxt->info.queue_mapping[i] =
1958 					       cpu_to_le16(vsi->base_queue + i);
1959 	} else {
1960 		ctxt->info.mapping_flags |=
1961 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1962 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1963 	}
1964 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1965 }
1966 
1967 /**
1968  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1969  * @netdev: the netdevice
1970  * @addr: address to add
1971  *
1972  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1973  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1974  */
1975 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1976 {
1977 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1978 	struct i40e_vsi *vsi = np->vsi;
1979 
1980 	if (i40e_add_mac_filter(vsi, addr))
1981 		return 0;
1982 	else
1983 		return -ENOMEM;
1984 }
1985 
1986 /**
1987  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1988  * @netdev: the netdevice
1989  * @addr: address to add
1990  *
1991  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1992  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1993  */
1994 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1995 {
1996 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1997 	struct i40e_vsi *vsi = np->vsi;
1998 
1999 	/* Under some circumstances, we might receive a request to delete
2000 	 * our own device address from our uc list. Because we store the
2001 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2002 	 * such requests and not delete our device address from this list.
2003 	 */
2004 	if (ether_addr_equal(addr, netdev->dev_addr))
2005 		return 0;
2006 
2007 	i40e_del_mac_filter(vsi, addr);
2008 
2009 	return 0;
2010 }
2011 
2012 /**
2013  * i40e_set_rx_mode - NDO callback to set the netdev filters
2014  * @netdev: network interface device structure
2015  **/
2016 static void i40e_set_rx_mode(struct net_device *netdev)
2017 {
2018 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2019 	struct i40e_vsi *vsi = np->vsi;
2020 
2021 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2022 
2023 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2024 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2025 
2026 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2027 
2028 	/* check for other flag changes */
2029 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2030 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2031 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2032 	}
2033 }
2034 
2035 /**
2036  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2037  * @vsi: Pointer to VSI struct
2038  * @from: Pointer to list which contains MAC filter entries - changes to
2039  *        those entries needs to be undone.
2040  *
2041  * MAC filter entries from this list were slated for deletion.
2042  **/
2043 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2044 					 struct hlist_head *from)
2045 {
2046 	struct i40e_mac_filter *f;
2047 	struct hlist_node *h;
2048 
2049 	hlist_for_each_entry_safe(f, h, from, hlist) {
2050 		u64 key = i40e_addr_to_hkey(f->macaddr);
2051 
2052 		/* Move the element back into MAC filter list*/
2053 		hlist_del(&f->hlist);
2054 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2055 	}
2056 }
2057 
2058 /**
2059  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2060  * @vsi: Pointer to vsi struct
2061  * @from: Pointer to list which contains MAC filter entries - changes to
2062  *        those entries needs to be undone.
2063  *
2064  * MAC filter entries from this list were slated for addition.
2065  **/
2066 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2067 					 struct hlist_head *from)
2068 {
2069 	struct i40e_new_mac_filter *new;
2070 	struct hlist_node *h;
2071 
2072 	hlist_for_each_entry_safe(new, h, from, hlist) {
2073 		/* We can simply free the wrapper structure */
2074 		hlist_del(&new->hlist);
2075 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2076 		kfree(new);
2077 	}
2078 }
2079 
2080 /**
2081  * i40e_next_filter - Get the next non-broadcast filter from a list
2082  * @next: pointer to filter in list
2083  *
2084  * Returns the next non-broadcast filter in the list. Required so that we
2085  * ignore broadcast filters within the list, since these are not handled via
2086  * the normal firmware update path.
2087  */
2088 static
2089 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2090 {
2091 	hlist_for_each_entry_continue(next, hlist) {
2092 		if (!is_broadcast_ether_addr(next->f->macaddr))
2093 			return next;
2094 	}
2095 
2096 	return NULL;
2097 }
2098 
2099 /**
2100  * i40e_update_filter_state - Update filter state based on return data
2101  * from firmware
2102  * @count: Number of filters added
2103  * @add_list: return data from fw
2104  * @add_head: pointer to first filter in current batch
2105  *
2106  * MAC filter entries from list were slated to be added to device. Returns
2107  * number of successful filters. Note that 0 does NOT mean success!
2108  **/
2109 static int
2110 i40e_update_filter_state(int count,
2111 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2112 			 struct i40e_new_mac_filter *add_head)
2113 {
2114 	int retval = 0;
2115 	int i;
2116 
2117 	for (i = 0; i < count; i++) {
2118 		/* Always check status of each filter. We don't need to check
2119 		 * the firmware return status because we pre-set the filter
2120 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2121 		 * request to the adminq. Thus, if it no longer matches then
2122 		 * we know the filter is active.
2123 		 */
2124 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2125 			add_head->state = I40E_FILTER_FAILED;
2126 		} else {
2127 			add_head->state = I40E_FILTER_ACTIVE;
2128 			retval++;
2129 		}
2130 
2131 		add_head = i40e_next_filter(add_head);
2132 		if (!add_head)
2133 			break;
2134 	}
2135 
2136 	return retval;
2137 }
2138 
2139 /**
2140  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2141  * @vsi: ptr to the VSI
2142  * @vsi_name: name to display in messages
2143  * @list: the list of filters to send to firmware
2144  * @num_del: the number of filters to delete
2145  * @retval: Set to -EIO on failure to delete
2146  *
2147  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2148  * *retval instead of a return value so that success does not force ret_val to
2149  * be set to 0. This ensures that a sequence of calls to this function
2150  * preserve the previous value of *retval on successful delete.
2151  */
2152 static
2153 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2154 			  struct i40e_aqc_remove_macvlan_element_data *list,
2155 			  int num_del, int *retval)
2156 {
2157 	struct i40e_hw *hw = &vsi->back->hw;
2158 	enum i40e_admin_queue_err aq_status;
2159 	i40e_status aq_ret;
2160 
2161 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2162 					   &aq_status);
2163 
2164 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2165 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2166 		*retval = -EIO;
2167 		dev_info(&vsi->back->pdev->dev,
2168 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2169 			 vsi_name, i40e_stat_str(hw, aq_ret),
2170 			 i40e_aq_str(hw, aq_status));
2171 	}
2172 }
2173 
2174 /**
2175  * i40e_aqc_add_filters - Request firmware to add a set of filters
2176  * @vsi: ptr to the VSI
2177  * @vsi_name: name to display in messages
2178  * @list: the list of filters to send to firmware
2179  * @add_head: Position in the add hlist
2180  * @num_add: the number of filters to add
2181  *
2182  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2183  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2184  * space for more filters.
2185  */
2186 static
2187 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2188 			  struct i40e_aqc_add_macvlan_element_data *list,
2189 			  struct i40e_new_mac_filter *add_head,
2190 			  int num_add)
2191 {
2192 	struct i40e_hw *hw = &vsi->back->hw;
2193 	enum i40e_admin_queue_err aq_status;
2194 	int fcnt;
2195 
2196 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2197 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2198 
2199 	if (fcnt != num_add) {
2200 		if (vsi->type == I40E_VSI_MAIN) {
2201 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2202 			dev_warn(&vsi->back->pdev->dev,
2203 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2204 				 i40e_aq_str(hw, aq_status), vsi_name);
2205 		} else if (vsi->type == I40E_VSI_SRIOV ||
2206 			   vsi->type == I40E_VSI_VMDQ1 ||
2207 			   vsi->type == I40E_VSI_VMDQ2) {
2208 			dev_warn(&vsi->back->pdev->dev,
2209 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2210 				 i40e_aq_str(hw, aq_status), vsi_name,
2211 					     vsi_name);
2212 		} else {
2213 			dev_warn(&vsi->back->pdev->dev,
2214 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2215 				 i40e_aq_str(hw, aq_status), vsi_name,
2216 					     vsi->type);
2217 		}
2218 	}
2219 }
2220 
2221 /**
2222  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2223  * @vsi: pointer to the VSI
2224  * @vsi_name: the VSI name
2225  * @f: filter data
2226  *
2227  * This function sets or clears the promiscuous broadcast flags for VLAN
2228  * filters in order to properly receive broadcast frames. Assumes that only
2229  * broadcast filters are passed.
2230  *
2231  * Returns status indicating success or failure;
2232  **/
2233 static i40e_status
2234 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2235 			  struct i40e_mac_filter *f)
2236 {
2237 	bool enable = f->state == I40E_FILTER_NEW;
2238 	struct i40e_hw *hw = &vsi->back->hw;
2239 	i40e_status aq_ret;
2240 
2241 	if (f->vlan == I40E_VLAN_ANY) {
2242 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2243 						   vsi->seid,
2244 						   enable,
2245 						   NULL);
2246 	} else {
2247 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2248 							    vsi->seid,
2249 							    enable,
2250 							    f->vlan,
2251 							    NULL);
2252 	}
2253 
2254 	if (aq_ret) {
2255 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2256 		dev_warn(&vsi->back->pdev->dev,
2257 			 "Error %s, forcing overflow promiscuous on %s\n",
2258 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2259 			 vsi_name);
2260 	}
2261 
2262 	return aq_ret;
2263 }
2264 
2265 /**
2266  * i40e_set_promiscuous - set promiscuous mode
2267  * @pf: board private structure
2268  * @promisc: promisc on or off
2269  *
2270  * There are different ways of setting promiscuous mode on a PF depending on
2271  * what state/environment we're in.  This identifies and sets it appropriately.
2272  * Returns 0 on success.
2273  **/
2274 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2275 {
2276 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2277 	struct i40e_hw *hw = &pf->hw;
2278 	i40e_status aq_ret;
2279 
2280 	if (vsi->type == I40E_VSI_MAIN &&
2281 	    pf->lan_veb != I40E_NO_VEB &&
2282 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2283 		/* set defport ON for Main VSI instead of true promisc
2284 		 * this way we will get all unicast/multicast and VLAN
2285 		 * promisc behavior but will not get VF or VMDq traffic
2286 		 * replicated on the Main VSI.
2287 		 */
2288 		if (promisc)
2289 			aq_ret = i40e_aq_set_default_vsi(hw,
2290 							 vsi->seid,
2291 							 NULL);
2292 		else
2293 			aq_ret = i40e_aq_clear_default_vsi(hw,
2294 							   vsi->seid,
2295 							   NULL);
2296 		if (aq_ret) {
2297 			dev_info(&pf->pdev->dev,
2298 				 "Set default VSI failed, err %s, aq_err %s\n",
2299 				 i40e_stat_str(hw, aq_ret),
2300 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2301 		}
2302 	} else {
2303 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2304 						  hw,
2305 						  vsi->seid,
2306 						  promisc, NULL,
2307 						  true);
2308 		if (aq_ret) {
2309 			dev_info(&pf->pdev->dev,
2310 				 "set unicast promisc failed, err %s, aq_err %s\n",
2311 				 i40e_stat_str(hw, aq_ret),
2312 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2313 		}
2314 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2315 						  hw,
2316 						  vsi->seid,
2317 						  promisc, NULL);
2318 		if (aq_ret) {
2319 			dev_info(&pf->pdev->dev,
2320 				 "set multicast promisc failed, err %s, aq_err %s\n",
2321 				 i40e_stat_str(hw, aq_ret),
2322 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2323 		}
2324 	}
2325 
2326 	if (!aq_ret)
2327 		pf->cur_promisc = promisc;
2328 
2329 	return aq_ret;
2330 }
2331 
2332 /**
2333  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2334  * @vsi: ptr to the VSI
2335  *
2336  * Push any outstanding VSI filter changes through the AdminQ.
2337  *
2338  * Returns 0 or error value
2339  **/
2340 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2341 {
2342 	struct hlist_head tmp_add_list, tmp_del_list;
2343 	struct i40e_mac_filter *f;
2344 	struct i40e_new_mac_filter *new, *add_head = NULL;
2345 	struct i40e_hw *hw = &vsi->back->hw;
2346 	bool old_overflow, new_overflow;
2347 	unsigned int failed_filters = 0;
2348 	unsigned int vlan_filters = 0;
2349 	char vsi_name[16] = "PF";
2350 	int filter_list_len = 0;
2351 	i40e_status aq_ret = 0;
2352 	u32 changed_flags = 0;
2353 	struct hlist_node *h;
2354 	struct i40e_pf *pf;
2355 	int num_add = 0;
2356 	int num_del = 0;
2357 	int retval = 0;
2358 	u16 cmd_flags;
2359 	int list_size;
2360 	int bkt;
2361 
2362 	/* empty array typed pointers, kcalloc later */
2363 	struct i40e_aqc_add_macvlan_element_data *add_list;
2364 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2365 
2366 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2367 		usleep_range(1000, 2000);
2368 	pf = vsi->back;
2369 
2370 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2371 
2372 	if (vsi->netdev) {
2373 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2374 		vsi->current_netdev_flags = vsi->netdev->flags;
2375 	}
2376 
2377 	INIT_HLIST_HEAD(&tmp_add_list);
2378 	INIT_HLIST_HEAD(&tmp_del_list);
2379 
2380 	if (vsi->type == I40E_VSI_SRIOV)
2381 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2382 	else if (vsi->type != I40E_VSI_MAIN)
2383 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2384 
2385 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2386 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2387 
2388 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2389 		/* Create a list of filters to delete. */
2390 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2391 			if (f->state == I40E_FILTER_REMOVE) {
2392 				/* Move the element into temporary del_list */
2393 				hash_del(&f->hlist);
2394 				hlist_add_head(&f->hlist, &tmp_del_list);
2395 
2396 				/* Avoid counting removed filters */
2397 				continue;
2398 			}
2399 			if (f->state == I40E_FILTER_NEW) {
2400 				/* Create a temporary i40e_new_mac_filter */
2401 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2402 				if (!new)
2403 					goto err_no_memory_locked;
2404 
2405 				/* Store pointer to the real filter */
2406 				new->f = f;
2407 				new->state = f->state;
2408 
2409 				/* Add it to the hash list */
2410 				hlist_add_head(&new->hlist, &tmp_add_list);
2411 			}
2412 
2413 			/* Count the number of active (current and new) VLAN
2414 			 * filters we have now. Does not count filters which
2415 			 * are marked for deletion.
2416 			 */
2417 			if (f->vlan > 0)
2418 				vlan_filters++;
2419 		}
2420 
2421 		retval = i40e_correct_mac_vlan_filters(vsi,
2422 						       &tmp_add_list,
2423 						       &tmp_del_list,
2424 						       vlan_filters);
2425 
2426 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2427 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2428 
2429 		if (retval)
2430 			goto err_no_memory_locked;
2431 
2432 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2433 	}
2434 
2435 	/* Now process 'del_list' outside the lock */
2436 	if (!hlist_empty(&tmp_del_list)) {
2437 		filter_list_len = hw->aq.asq_buf_size /
2438 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2439 		list_size = filter_list_len *
2440 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2441 		del_list = kzalloc(list_size, GFP_ATOMIC);
2442 		if (!del_list)
2443 			goto err_no_memory;
2444 
2445 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2446 			cmd_flags = 0;
2447 
2448 			/* handle broadcast filters by updating the broadcast
2449 			 * promiscuous flag and release filter list.
2450 			 */
2451 			if (is_broadcast_ether_addr(f->macaddr)) {
2452 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2453 
2454 				hlist_del(&f->hlist);
2455 				kfree(f);
2456 				continue;
2457 			}
2458 
2459 			/* add to delete list */
2460 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2461 			if (f->vlan == I40E_VLAN_ANY) {
2462 				del_list[num_del].vlan_tag = 0;
2463 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2464 			} else {
2465 				del_list[num_del].vlan_tag =
2466 					cpu_to_le16((u16)(f->vlan));
2467 			}
2468 
2469 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2470 			del_list[num_del].flags = cmd_flags;
2471 			num_del++;
2472 
2473 			/* flush a full buffer */
2474 			if (num_del == filter_list_len) {
2475 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2476 						     num_del, &retval);
2477 				memset(del_list, 0, list_size);
2478 				num_del = 0;
2479 			}
2480 			/* Release memory for MAC filter entries which were
2481 			 * synced up with HW.
2482 			 */
2483 			hlist_del(&f->hlist);
2484 			kfree(f);
2485 		}
2486 
2487 		if (num_del) {
2488 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2489 					     num_del, &retval);
2490 		}
2491 
2492 		kfree(del_list);
2493 		del_list = NULL;
2494 	}
2495 
2496 	if (!hlist_empty(&tmp_add_list)) {
2497 		/* Do all the adds now. */
2498 		filter_list_len = hw->aq.asq_buf_size /
2499 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2500 		list_size = filter_list_len *
2501 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2502 		add_list = kzalloc(list_size, GFP_ATOMIC);
2503 		if (!add_list)
2504 			goto err_no_memory;
2505 
2506 		num_add = 0;
2507 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2508 			/* handle broadcast filters by updating the broadcast
2509 			 * promiscuous flag instead of adding a MAC filter.
2510 			 */
2511 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2512 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2513 							      new->f))
2514 					new->state = I40E_FILTER_FAILED;
2515 				else
2516 					new->state = I40E_FILTER_ACTIVE;
2517 				continue;
2518 			}
2519 
2520 			/* add to add array */
2521 			if (num_add == 0)
2522 				add_head = new;
2523 			cmd_flags = 0;
2524 			ether_addr_copy(add_list[num_add].mac_addr,
2525 					new->f->macaddr);
2526 			if (new->f->vlan == I40E_VLAN_ANY) {
2527 				add_list[num_add].vlan_tag = 0;
2528 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2529 			} else {
2530 				add_list[num_add].vlan_tag =
2531 					cpu_to_le16((u16)(new->f->vlan));
2532 			}
2533 			add_list[num_add].queue_number = 0;
2534 			/* set invalid match method for later detection */
2535 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2536 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2537 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2538 			num_add++;
2539 
2540 			/* flush a full buffer */
2541 			if (num_add == filter_list_len) {
2542 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2543 						     add_head, num_add);
2544 				memset(add_list, 0, list_size);
2545 				num_add = 0;
2546 			}
2547 		}
2548 		if (num_add) {
2549 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2550 					     num_add);
2551 		}
2552 		/* Now move all of the filters from the temp add list back to
2553 		 * the VSI's list.
2554 		 */
2555 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2556 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2557 			/* Only update the state if we're still NEW */
2558 			if (new->f->state == I40E_FILTER_NEW)
2559 				new->f->state = new->state;
2560 			hlist_del(&new->hlist);
2561 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2562 			kfree(new);
2563 		}
2564 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2565 		kfree(add_list);
2566 		add_list = NULL;
2567 	}
2568 
2569 	/* Determine the number of active and failed filters. */
2570 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2571 	vsi->active_filters = 0;
2572 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2573 		if (f->state == I40E_FILTER_ACTIVE)
2574 			vsi->active_filters++;
2575 		else if (f->state == I40E_FILTER_FAILED)
2576 			failed_filters++;
2577 	}
2578 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2579 
2580 	/* Check if we are able to exit overflow promiscuous mode. We can
2581 	 * safely exit if we didn't just enter, we no longer have any failed
2582 	 * filters, and we have reduced filters below the threshold value.
2583 	 */
2584 	if (old_overflow && !failed_filters &&
2585 	    vsi->active_filters < vsi->promisc_threshold) {
2586 		dev_info(&pf->pdev->dev,
2587 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2588 			 vsi_name);
2589 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2590 		vsi->promisc_threshold = 0;
2591 	}
2592 
2593 	/* if the VF is not trusted do not do promisc */
2594 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2595 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2596 		goto out;
2597 	}
2598 
2599 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2600 
2601 	/* If we are entering overflow promiscuous, we need to calculate a new
2602 	 * threshold for when we are safe to exit
2603 	 */
2604 	if (!old_overflow && new_overflow)
2605 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2606 
2607 	/* check for changes in promiscuous modes */
2608 	if (changed_flags & IFF_ALLMULTI) {
2609 		bool cur_multipromisc;
2610 
2611 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2612 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2613 							       vsi->seid,
2614 							       cur_multipromisc,
2615 							       NULL);
2616 		if (aq_ret) {
2617 			retval = i40e_aq_rc_to_posix(aq_ret,
2618 						     hw->aq.asq_last_status);
2619 			dev_info(&pf->pdev->dev,
2620 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2621 				 vsi_name,
2622 				 i40e_stat_str(hw, aq_ret),
2623 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2624 		} else {
2625 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2626 				 cur_multipromisc ? "entering" : "leaving");
2627 		}
2628 	}
2629 
2630 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2631 		bool cur_promisc;
2632 
2633 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2634 			       new_overflow);
2635 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2636 		if (aq_ret) {
2637 			retval = i40e_aq_rc_to_posix(aq_ret,
2638 						     hw->aq.asq_last_status);
2639 			dev_info(&pf->pdev->dev,
2640 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2641 				 cur_promisc ? "on" : "off",
2642 				 vsi_name,
2643 				 i40e_stat_str(hw, aq_ret),
2644 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2645 		}
2646 	}
2647 out:
2648 	/* if something went wrong then set the changed flag so we try again */
2649 	if (retval)
2650 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2651 
2652 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2653 	return retval;
2654 
2655 err_no_memory:
2656 	/* Restore elements on the temporary add and delete lists */
2657 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2658 err_no_memory_locked:
2659 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2660 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2661 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2662 
2663 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2664 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2665 	return -ENOMEM;
2666 }
2667 
2668 /**
2669  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2670  * @pf: board private structure
2671  **/
2672 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2673 {
2674 	int v;
2675 
2676 	if (!pf)
2677 		return;
2678 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2679 		return;
2680 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2681 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2682 		return;
2683 	}
2684 
2685 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2686 		if (pf->vsi[v] &&
2687 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2688 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2689 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2690 
2691 			if (ret) {
2692 				/* come back and try again later */
2693 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2694 					pf->state);
2695 				break;
2696 			}
2697 		}
2698 	}
2699 }
2700 
2701 /**
2702  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2703  * @vsi: the vsi
2704  **/
2705 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2706 {
2707 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2708 		return I40E_RXBUFFER_2048;
2709 	else
2710 		return I40E_RXBUFFER_3072;
2711 }
2712 
2713 /**
2714  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2715  * @netdev: network interface device structure
2716  * @new_mtu: new value for maximum frame size
2717  *
2718  * Returns 0 on success, negative on failure
2719  **/
2720 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2721 {
2722 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2723 	struct i40e_vsi *vsi = np->vsi;
2724 	struct i40e_pf *pf = vsi->back;
2725 
2726 	if (i40e_enabled_xdp_vsi(vsi)) {
2727 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2728 
2729 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2730 			return -EINVAL;
2731 	}
2732 
2733 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2734 		   netdev->mtu, new_mtu);
2735 	netdev->mtu = new_mtu;
2736 	if (netif_running(netdev))
2737 		i40e_vsi_reinit_locked(vsi);
2738 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2739 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2740 	return 0;
2741 }
2742 
2743 /**
2744  * i40e_ioctl - Access the hwtstamp interface
2745  * @netdev: network interface device structure
2746  * @ifr: interface request data
2747  * @cmd: ioctl command
2748  **/
2749 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2750 {
2751 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2752 	struct i40e_pf *pf = np->vsi->back;
2753 
2754 	switch (cmd) {
2755 	case SIOCGHWTSTAMP:
2756 		return i40e_ptp_get_ts_config(pf, ifr);
2757 	case SIOCSHWTSTAMP:
2758 		return i40e_ptp_set_ts_config(pf, ifr);
2759 	default:
2760 		return -EOPNOTSUPP;
2761 	}
2762 }
2763 
2764 /**
2765  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2766  * @vsi: the vsi being adjusted
2767  **/
2768 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2769 {
2770 	struct i40e_vsi_context ctxt;
2771 	i40e_status ret;
2772 
2773 	/* Don't modify stripping options if a port VLAN is active */
2774 	if (vsi->info.pvid)
2775 		return;
2776 
2777 	if ((vsi->info.valid_sections &
2778 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2779 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2780 		return;  /* already enabled */
2781 
2782 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2783 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2784 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2785 
2786 	ctxt.seid = vsi->seid;
2787 	ctxt.info = vsi->info;
2788 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2789 	if (ret) {
2790 		dev_info(&vsi->back->pdev->dev,
2791 			 "update vlan stripping failed, err %s aq_err %s\n",
2792 			 i40e_stat_str(&vsi->back->hw, ret),
2793 			 i40e_aq_str(&vsi->back->hw,
2794 				     vsi->back->hw.aq.asq_last_status));
2795 	}
2796 }
2797 
2798 /**
2799  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2800  * @vsi: the vsi being adjusted
2801  **/
2802 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2803 {
2804 	struct i40e_vsi_context ctxt;
2805 	i40e_status ret;
2806 
2807 	/* Don't modify stripping options if a port VLAN is active */
2808 	if (vsi->info.pvid)
2809 		return;
2810 
2811 	if ((vsi->info.valid_sections &
2812 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2813 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2814 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
2815 		return;  /* already disabled */
2816 
2817 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2818 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2819 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2820 
2821 	ctxt.seid = vsi->seid;
2822 	ctxt.info = vsi->info;
2823 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2824 	if (ret) {
2825 		dev_info(&vsi->back->pdev->dev,
2826 			 "update vlan stripping failed, err %s aq_err %s\n",
2827 			 i40e_stat_str(&vsi->back->hw, ret),
2828 			 i40e_aq_str(&vsi->back->hw,
2829 				     vsi->back->hw.aq.asq_last_status));
2830 	}
2831 }
2832 
2833 /**
2834  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2835  * @vsi: the vsi being configured
2836  * @vid: vlan id to be added (0 = untagged only , -1 = any)
2837  *
2838  * This is a helper function for adding a new MAC/VLAN filter with the
2839  * specified VLAN for each existing MAC address already in the hash table.
2840  * This function does *not* perform any accounting to update filters based on
2841  * VLAN mode.
2842  *
2843  * NOTE: this function expects to be called while under the
2844  * mac_filter_hash_lock
2845  **/
2846 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2847 {
2848 	struct i40e_mac_filter *f, *add_f;
2849 	struct hlist_node *h;
2850 	int bkt;
2851 
2852 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2853 		if (f->state == I40E_FILTER_REMOVE)
2854 			continue;
2855 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
2856 		if (!add_f) {
2857 			dev_info(&vsi->back->pdev->dev,
2858 				 "Could not add vlan filter %d for %pM\n",
2859 				 vid, f->macaddr);
2860 			return -ENOMEM;
2861 		}
2862 	}
2863 
2864 	return 0;
2865 }
2866 
2867 /**
2868  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2869  * @vsi: the VSI being configured
2870  * @vid: VLAN id to be added
2871  **/
2872 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
2873 {
2874 	int err;
2875 
2876 	if (vsi->info.pvid)
2877 		return -EINVAL;
2878 
2879 	/* The network stack will attempt to add VID=0, with the intention to
2880 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
2881 	 * these packets by default when configured to receive untagged
2882 	 * packets, so we don't need to add a filter for this case.
2883 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
2884 	 * receive *only* tagged traffic and stops receiving untagged traffic.
2885 	 * Thus, we do not want to actually add a filter for VID=0
2886 	 */
2887 	if (!vid)
2888 		return 0;
2889 
2890 	/* Locked once because all functions invoked below iterates list*/
2891 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2892 	err = i40e_add_vlan_all_mac(vsi, vid);
2893 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2894 	if (err)
2895 		return err;
2896 
2897 	/* schedule our worker thread which will take care of
2898 	 * applying the new filter changes
2899 	 */
2900 	i40e_service_event_schedule(vsi->back);
2901 	return 0;
2902 }
2903 
2904 /**
2905  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
2906  * @vsi: the vsi being configured
2907  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2908  *
2909  * This function should be used to remove all VLAN filters which match the
2910  * given VID. It does not schedule the service event and does not take the
2911  * mac_filter_hash_lock so it may be combined with other operations under
2912  * a single invocation of the mac_filter_hash_lock.
2913  *
2914  * NOTE: this function expects to be called while under the
2915  * mac_filter_hash_lock
2916  */
2917 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2918 {
2919 	struct i40e_mac_filter *f;
2920 	struct hlist_node *h;
2921 	int bkt;
2922 
2923 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2924 		if (f->vlan == vid)
2925 			__i40e_del_filter(vsi, f);
2926 	}
2927 }
2928 
2929 /**
2930  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2931  * @vsi: the VSI being configured
2932  * @vid: VLAN id to be removed
2933  **/
2934 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
2935 {
2936 	if (!vid || vsi->info.pvid)
2937 		return;
2938 
2939 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2940 	i40e_rm_vlan_all_mac(vsi, vid);
2941 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2942 
2943 	/* schedule our worker thread which will take care of
2944 	 * applying the new filter changes
2945 	 */
2946 	i40e_service_event_schedule(vsi->back);
2947 }
2948 
2949 /**
2950  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2951  * @netdev: network interface to be adjusted
2952  * @proto: unused protocol value
2953  * @vid: vlan id to be added
2954  *
2955  * net_device_ops implementation for adding vlan ids
2956  **/
2957 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2958 				__always_unused __be16 proto, u16 vid)
2959 {
2960 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2961 	struct i40e_vsi *vsi = np->vsi;
2962 	int ret = 0;
2963 
2964 	if (vid >= VLAN_N_VID)
2965 		return -EINVAL;
2966 
2967 	ret = i40e_vsi_add_vlan(vsi, vid);
2968 	if (!ret)
2969 		set_bit(vid, vsi->active_vlans);
2970 
2971 	return ret;
2972 }
2973 
2974 /**
2975  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
2976  * @netdev: network interface to be adjusted
2977  * @proto: unused protocol value
2978  * @vid: vlan id to be added
2979  **/
2980 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
2981 				    __always_unused __be16 proto, u16 vid)
2982 {
2983 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2984 	struct i40e_vsi *vsi = np->vsi;
2985 
2986 	if (vid >= VLAN_N_VID)
2987 		return;
2988 	set_bit(vid, vsi->active_vlans);
2989 }
2990 
2991 /**
2992  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2993  * @netdev: network interface to be adjusted
2994  * @proto: unused protocol value
2995  * @vid: vlan id to be removed
2996  *
2997  * net_device_ops implementation for removing vlan ids
2998  **/
2999 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3000 				 __always_unused __be16 proto, u16 vid)
3001 {
3002 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3003 	struct i40e_vsi *vsi = np->vsi;
3004 
3005 	/* return code is ignored as there is nothing a user
3006 	 * can do about failure to remove and a log message was
3007 	 * already printed from the other function
3008 	 */
3009 	i40e_vsi_kill_vlan(vsi, vid);
3010 
3011 	clear_bit(vid, vsi->active_vlans);
3012 
3013 	return 0;
3014 }
3015 
3016 /**
3017  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3018  * @vsi: the vsi being brought back up
3019  **/
3020 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3021 {
3022 	u16 vid;
3023 
3024 	if (!vsi->netdev)
3025 		return;
3026 
3027 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3028 		i40e_vlan_stripping_enable(vsi);
3029 	else
3030 		i40e_vlan_stripping_disable(vsi);
3031 
3032 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3033 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3034 					vid);
3035 }
3036 
3037 /**
3038  * i40e_vsi_add_pvid - Add pvid for the VSI
3039  * @vsi: the vsi being adjusted
3040  * @vid: the vlan id to set as a PVID
3041  **/
3042 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3043 {
3044 	struct i40e_vsi_context ctxt;
3045 	i40e_status ret;
3046 
3047 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3048 	vsi->info.pvid = cpu_to_le16(vid);
3049 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3050 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3051 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3052 
3053 	ctxt.seid = vsi->seid;
3054 	ctxt.info = vsi->info;
3055 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3056 	if (ret) {
3057 		dev_info(&vsi->back->pdev->dev,
3058 			 "add pvid failed, err %s aq_err %s\n",
3059 			 i40e_stat_str(&vsi->back->hw, ret),
3060 			 i40e_aq_str(&vsi->back->hw,
3061 				     vsi->back->hw.aq.asq_last_status));
3062 		return -ENOENT;
3063 	}
3064 
3065 	return 0;
3066 }
3067 
3068 /**
3069  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3070  * @vsi: the vsi being adjusted
3071  *
3072  * Just use the vlan_rx_register() service to put it back to normal
3073  **/
3074 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3075 {
3076 	vsi->info.pvid = 0;
3077 
3078 	i40e_vlan_stripping_disable(vsi);
3079 }
3080 
3081 /**
3082  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3083  * @vsi: ptr to the VSI
3084  *
3085  * If this function returns with an error, then it's possible one or
3086  * more of the rings is populated (while the rest are not).  It is the
3087  * callers duty to clean those orphaned rings.
3088  *
3089  * Return 0 on success, negative on failure
3090  **/
3091 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3092 {
3093 	int i, err = 0;
3094 
3095 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3096 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3097 
3098 	if (!i40e_enabled_xdp_vsi(vsi))
3099 		return err;
3100 
3101 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3102 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3103 
3104 	return err;
3105 }
3106 
3107 /**
3108  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3109  * @vsi: ptr to the VSI
3110  *
3111  * Free VSI's transmit software resources
3112  **/
3113 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3114 {
3115 	int i;
3116 
3117 	if (vsi->tx_rings) {
3118 		for (i = 0; i < vsi->num_queue_pairs; i++)
3119 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3120 				i40e_free_tx_resources(vsi->tx_rings[i]);
3121 	}
3122 
3123 	if (vsi->xdp_rings) {
3124 		for (i = 0; i < vsi->num_queue_pairs; i++)
3125 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3126 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3127 	}
3128 }
3129 
3130 /**
3131  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3132  * @vsi: ptr to the VSI
3133  *
3134  * If this function returns with an error, then it's possible one or
3135  * more of the rings is populated (while the rest are not).  It is the
3136  * callers duty to clean those orphaned rings.
3137  *
3138  * Return 0 on success, negative on failure
3139  **/
3140 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3141 {
3142 	int i, err = 0;
3143 
3144 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3145 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3146 	return err;
3147 }
3148 
3149 /**
3150  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3151  * @vsi: ptr to the VSI
3152  *
3153  * Free all receive software resources
3154  **/
3155 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3156 {
3157 	int i;
3158 
3159 	if (!vsi->rx_rings)
3160 		return;
3161 
3162 	for (i = 0; i < vsi->num_queue_pairs; i++)
3163 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3164 			i40e_free_rx_resources(vsi->rx_rings[i]);
3165 }
3166 
3167 /**
3168  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3169  * @ring: The Tx ring to configure
3170  *
3171  * This enables/disables XPS for a given Tx descriptor ring
3172  * based on the TCs enabled for the VSI that ring belongs to.
3173  **/
3174 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3175 {
3176 	int cpu;
3177 
3178 	if (!ring->q_vector || !ring->netdev || ring->ch)
3179 		return;
3180 
3181 	/* We only initialize XPS once, so as not to overwrite user settings */
3182 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3183 		return;
3184 
3185 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3186 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3187 			    ring->queue_index);
3188 }
3189 
3190 /**
3191  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3192  * @ring: The Tx or Rx ring
3193  *
3194  * Returns the AF_XDP buffer pool or NULL.
3195  **/
3196 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3197 {
3198 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3199 	int qid = ring->queue_index;
3200 
3201 	if (ring_is_xdp(ring))
3202 		qid -= ring->vsi->alloc_queue_pairs;
3203 
3204 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3205 		return NULL;
3206 
3207 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3208 }
3209 
3210 /**
3211  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3212  * @ring: The Tx ring to configure
3213  *
3214  * Configure the Tx descriptor ring in the HMC context.
3215  **/
3216 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3217 {
3218 	struct i40e_vsi *vsi = ring->vsi;
3219 	u16 pf_q = vsi->base_queue + ring->queue_index;
3220 	struct i40e_hw *hw = &vsi->back->hw;
3221 	struct i40e_hmc_obj_txq tx_ctx;
3222 	i40e_status err = 0;
3223 	u32 qtx_ctl = 0;
3224 
3225 	if (ring_is_xdp(ring))
3226 		ring->xsk_pool = i40e_xsk_pool(ring);
3227 
3228 	/* some ATR related tx ring init */
3229 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3230 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3231 		ring->atr_count = 0;
3232 	} else {
3233 		ring->atr_sample_rate = 0;
3234 	}
3235 
3236 	/* configure XPS */
3237 	i40e_config_xps_tx_ring(ring);
3238 
3239 	/* clear the context structure first */
3240 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3241 
3242 	tx_ctx.new_context = 1;
3243 	tx_ctx.base = (ring->dma / 128);
3244 	tx_ctx.qlen = ring->count;
3245 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3246 					       I40E_FLAG_FD_ATR_ENABLED));
3247 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3248 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3249 	if (vsi->type != I40E_VSI_FDIR)
3250 		tx_ctx.head_wb_ena = 1;
3251 	tx_ctx.head_wb_addr = ring->dma +
3252 			      (ring->count * sizeof(struct i40e_tx_desc));
3253 
3254 	/* As part of VSI creation/update, FW allocates certain
3255 	 * Tx arbitration queue sets for each TC enabled for
3256 	 * the VSI. The FW returns the handles to these queue
3257 	 * sets as part of the response buffer to Add VSI,
3258 	 * Update VSI, etc. AQ commands. It is expected that
3259 	 * these queue set handles be associated with the Tx
3260 	 * queues by the driver as part of the TX queue context
3261 	 * initialization. This has to be done regardless of
3262 	 * DCB as by default everything is mapped to TC0.
3263 	 */
3264 
3265 	if (ring->ch)
3266 		tx_ctx.rdylist =
3267 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3268 
3269 	else
3270 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3271 
3272 	tx_ctx.rdylist_act = 0;
3273 
3274 	/* clear the context in the HMC */
3275 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3276 	if (err) {
3277 		dev_info(&vsi->back->pdev->dev,
3278 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3279 			 ring->queue_index, pf_q, err);
3280 		return -ENOMEM;
3281 	}
3282 
3283 	/* set the context in the HMC */
3284 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3285 	if (err) {
3286 		dev_info(&vsi->back->pdev->dev,
3287 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3288 			 ring->queue_index, pf_q, err);
3289 		return -ENOMEM;
3290 	}
3291 
3292 	/* Now associate this queue with this PCI function */
3293 	if (ring->ch) {
3294 		if (ring->ch->type == I40E_VSI_VMDQ2)
3295 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3296 		else
3297 			return -EINVAL;
3298 
3299 		qtx_ctl |= (ring->ch->vsi_number <<
3300 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3301 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3302 	} else {
3303 		if (vsi->type == I40E_VSI_VMDQ2) {
3304 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3305 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3306 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3307 		} else {
3308 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3309 		}
3310 	}
3311 
3312 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3313 		    I40E_QTX_CTL_PF_INDX_MASK);
3314 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3315 	i40e_flush(hw);
3316 
3317 	/* cache tail off for easier writes later */
3318 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3319 
3320 	return 0;
3321 }
3322 
3323 /**
3324  * i40e_rx_offset - Return expected offset into page to access data
3325  * @rx_ring: Ring we are requesting offset of
3326  *
3327  * Returns the offset value for ring into the data buffer.
3328  */
3329 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3330 {
3331 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3332 }
3333 
3334 /**
3335  * i40e_configure_rx_ring - Configure a receive ring context
3336  * @ring: The Rx ring to configure
3337  *
3338  * Configure the Rx descriptor ring in the HMC context.
3339  **/
3340 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3341 {
3342 	struct i40e_vsi *vsi = ring->vsi;
3343 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3344 	u16 pf_q = vsi->base_queue + ring->queue_index;
3345 	struct i40e_hw *hw = &vsi->back->hw;
3346 	struct i40e_hmc_obj_rxq rx_ctx;
3347 	i40e_status err = 0;
3348 	bool ok;
3349 	int ret;
3350 
3351 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3352 
3353 	/* clear the context structure first */
3354 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3355 
3356 	if (ring->vsi->type == I40E_VSI_MAIN)
3357 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3358 
3359 	kfree(ring->rx_bi);
3360 	ring->xsk_pool = i40e_xsk_pool(ring);
3361 	if (ring->xsk_pool) {
3362 		ret = i40e_alloc_rx_bi_zc(ring);
3363 		if (ret)
3364 			return ret;
3365 		ring->rx_buf_len =
3366 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3367 		/* For AF_XDP ZC, we disallow packets to span on
3368 		 * multiple buffers, thus letting us skip that
3369 		 * handling in the fast-path.
3370 		 */
3371 		chain_len = 1;
3372 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3373 						 MEM_TYPE_XSK_BUFF_POOL,
3374 						 NULL);
3375 		if (ret)
3376 			return ret;
3377 		dev_info(&vsi->back->pdev->dev,
3378 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3379 			 ring->queue_index);
3380 
3381 	} else {
3382 		ret = i40e_alloc_rx_bi(ring);
3383 		if (ret)
3384 			return ret;
3385 		ring->rx_buf_len = vsi->rx_buf_len;
3386 		if (ring->vsi->type == I40E_VSI_MAIN) {
3387 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3388 							 MEM_TYPE_PAGE_SHARED,
3389 							 NULL);
3390 			if (ret)
3391 				return ret;
3392 		}
3393 	}
3394 
3395 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3396 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3397 
3398 	rx_ctx.base = (ring->dma / 128);
3399 	rx_ctx.qlen = ring->count;
3400 
3401 	/* use 16 byte descriptors */
3402 	rx_ctx.dsize = 0;
3403 
3404 	/* descriptor type is always zero
3405 	 * rx_ctx.dtype = 0;
3406 	 */
3407 	rx_ctx.hsplit_0 = 0;
3408 
3409 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3410 	if (hw->revision_id == 0)
3411 		rx_ctx.lrxqthresh = 0;
3412 	else
3413 		rx_ctx.lrxqthresh = 1;
3414 	rx_ctx.crcstrip = 1;
3415 	rx_ctx.l2tsel = 1;
3416 	/* this controls whether VLAN is stripped from inner headers */
3417 	rx_ctx.showiv = 0;
3418 	/* set the prefena field to 1 because the manual says to */
3419 	rx_ctx.prefena = 1;
3420 
3421 	/* clear the context in the HMC */
3422 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3423 	if (err) {
3424 		dev_info(&vsi->back->pdev->dev,
3425 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3426 			 ring->queue_index, pf_q, err);
3427 		return -ENOMEM;
3428 	}
3429 
3430 	/* set the context in the HMC */
3431 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3432 	if (err) {
3433 		dev_info(&vsi->back->pdev->dev,
3434 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3435 			 ring->queue_index, pf_q, err);
3436 		return -ENOMEM;
3437 	}
3438 
3439 	/* configure Rx buffer alignment */
3440 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3441 		clear_ring_build_skb_enabled(ring);
3442 	else
3443 		set_ring_build_skb_enabled(ring);
3444 
3445 	ring->rx_offset = i40e_rx_offset(ring);
3446 
3447 	/* cache tail for quicker writes, and clear the reg before use */
3448 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3449 	writel(0, ring->tail);
3450 
3451 	if (ring->xsk_pool) {
3452 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3453 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3454 	} else {
3455 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3456 	}
3457 	if (!ok) {
3458 		/* Log this in case the user has forgotten to give the kernel
3459 		 * any buffers, even later in the application.
3460 		 */
3461 		dev_info(&vsi->back->pdev->dev,
3462 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3463 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3464 			 ring->queue_index, pf_q);
3465 	}
3466 
3467 	return 0;
3468 }
3469 
3470 /**
3471  * i40e_vsi_configure_tx - Configure the VSI for Tx
3472  * @vsi: VSI structure describing this set of rings and resources
3473  *
3474  * Configure the Tx VSI for operation.
3475  **/
3476 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3477 {
3478 	int err = 0;
3479 	u16 i;
3480 
3481 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3482 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3483 
3484 	if (err || !i40e_enabled_xdp_vsi(vsi))
3485 		return err;
3486 
3487 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3488 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3489 
3490 	return err;
3491 }
3492 
3493 /**
3494  * i40e_vsi_configure_rx - Configure the VSI for Rx
3495  * @vsi: the VSI being configured
3496  *
3497  * Configure the Rx VSI for operation.
3498  **/
3499 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3500 {
3501 	int err = 0;
3502 	u16 i;
3503 
3504 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3505 		vsi->max_frame = I40E_MAX_RXBUFFER;
3506 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3507 #if (PAGE_SIZE < 8192)
3508 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3509 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3510 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3511 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3512 #endif
3513 	} else {
3514 		vsi->max_frame = I40E_MAX_RXBUFFER;
3515 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3516 						       I40E_RXBUFFER_2048;
3517 	}
3518 
3519 	/* set up individual rings */
3520 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3521 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3522 
3523 	return err;
3524 }
3525 
3526 /**
3527  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3528  * @vsi: ptr to the VSI
3529  **/
3530 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3531 {
3532 	struct i40e_ring *tx_ring, *rx_ring;
3533 	u16 qoffset, qcount;
3534 	int i, n;
3535 
3536 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3537 		/* Reset the TC information */
3538 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3539 			rx_ring = vsi->rx_rings[i];
3540 			tx_ring = vsi->tx_rings[i];
3541 			rx_ring->dcb_tc = 0;
3542 			tx_ring->dcb_tc = 0;
3543 		}
3544 		return;
3545 	}
3546 
3547 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3548 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3549 			continue;
3550 
3551 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3552 		qcount = vsi->tc_config.tc_info[n].qcount;
3553 		for (i = qoffset; i < (qoffset + qcount); i++) {
3554 			rx_ring = vsi->rx_rings[i];
3555 			tx_ring = vsi->tx_rings[i];
3556 			rx_ring->dcb_tc = n;
3557 			tx_ring->dcb_tc = n;
3558 		}
3559 	}
3560 }
3561 
3562 /**
3563  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3564  * @vsi: ptr to the VSI
3565  **/
3566 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3567 {
3568 	if (vsi->netdev)
3569 		i40e_set_rx_mode(vsi->netdev);
3570 }
3571 
3572 /**
3573  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3574  * @pf: Pointer to the targeted PF
3575  *
3576  * Set all flow director counters to 0.
3577  */
3578 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3579 {
3580 	pf->fd_tcp4_filter_cnt = 0;
3581 	pf->fd_udp4_filter_cnt = 0;
3582 	pf->fd_sctp4_filter_cnt = 0;
3583 	pf->fd_ip4_filter_cnt = 0;
3584 	pf->fd_tcp6_filter_cnt = 0;
3585 	pf->fd_udp6_filter_cnt = 0;
3586 	pf->fd_sctp6_filter_cnt = 0;
3587 	pf->fd_ip6_filter_cnt = 0;
3588 }
3589 
3590 /**
3591  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3592  * @vsi: Pointer to the targeted VSI
3593  *
3594  * This function replays the hlist on the hw where all the SB Flow Director
3595  * filters were saved.
3596  **/
3597 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3598 {
3599 	struct i40e_fdir_filter *filter;
3600 	struct i40e_pf *pf = vsi->back;
3601 	struct hlist_node *node;
3602 
3603 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3604 		return;
3605 
3606 	/* Reset FDir counters as we're replaying all existing filters */
3607 	i40e_reset_fdir_filter_cnt(pf);
3608 
3609 	hlist_for_each_entry_safe(filter, node,
3610 				  &pf->fdir_filter_list, fdir_node) {
3611 		i40e_add_del_fdir(vsi, filter, true);
3612 	}
3613 }
3614 
3615 /**
3616  * i40e_vsi_configure - Set up the VSI for action
3617  * @vsi: the VSI being configured
3618  **/
3619 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3620 {
3621 	int err;
3622 
3623 	i40e_set_vsi_rx_mode(vsi);
3624 	i40e_restore_vlan(vsi);
3625 	i40e_vsi_config_dcb_rings(vsi);
3626 	err = i40e_vsi_configure_tx(vsi);
3627 	if (!err)
3628 		err = i40e_vsi_configure_rx(vsi);
3629 
3630 	return err;
3631 }
3632 
3633 /**
3634  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3635  * @vsi: the VSI being configured
3636  **/
3637 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3638 {
3639 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3640 	struct i40e_pf *pf = vsi->back;
3641 	struct i40e_hw *hw = &pf->hw;
3642 	u16 vector;
3643 	int i, q;
3644 	u32 qp;
3645 
3646 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3647 	 * and PFINT_LNKLSTn registers, e.g.:
3648 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3649 	 */
3650 	qp = vsi->base_queue;
3651 	vector = vsi->base_vector;
3652 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3653 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3654 
3655 		q_vector->rx.next_update = jiffies + 1;
3656 		q_vector->rx.target_itr =
3657 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3658 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3659 		     q_vector->rx.target_itr >> 1);
3660 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3661 
3662 		q_vector->tx.next_update = jiffies + 1;
3663 		q_vector->tx.target_itr =
3664 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3665 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3666 		     q_vector->tx.target_itr >> 1);
3667 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3668 
3669 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3670 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3671 
3672 		/* Linked list for the queuepairs assigned to this vector */
3673 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3674 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3675 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3676 			u32 val;
3677 
3678 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3679 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3680 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3681 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3682 			      (I40E_QUEUE_TYPE_TX <<
3683 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3684 
3685 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3686 
3687 			if (has_xdp) {
3688 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3689 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3690 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3691 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3692 				      (I40E_QUEUE_TYPE_TX <<
3693 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3694 
3695 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3696 			}
3697 
3698 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3699 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3700 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3701 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3702 			      (I40E_QUEUE_TYPE_RX <<
3703 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3704 
3705 			/* Terminate the linked list */
3706 			if (q == (q_vector->num_ringpairs - 1))
3707 				val |= (I40E_QUEUE_END_OF_LIST <<
3708 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3709 
3710 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3711 			qp++;
3712 		}
3713 	}
3714 
3715 	i40e_flush(hw);
3716 }
3717 
3718 /**
3719  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3720  * @pf: pointer to private device data structure
3721  **/
3722 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3723 {
3724 	struct i40e_hw *hw = &pf->hw;
3725 	u32 val;
3726 
3727 	/* clear things first */
3728 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3729 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3730 
3731 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3732 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3733 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3734 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3735 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3736 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3737 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3738 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3739 
3740 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3741 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3742 
3743 	if (pf->flags & I40E_FLAG_PTP)
3744 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3745 
3746 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3747 
3748 	/* SW_ITR_IDX = 0, but don't change INTENA */
3749 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3750 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3751 
3752 	/* OTHER_ITR_IDX = 0 */
3753 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3754 }
3755 
3756 /**
3757  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3758  * @vsi: the VSI being configured
3759  **/
3760 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3761 {
3762 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3763 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3764 	struct i40e_pf *pf = vsi->back;
3765 	struct i40e_hw *hw = &pf->hw;
3766 	u32 val;
3767 
3768 	/* set the ITR configuration */
3769 	q_vector->rx.next_update = jiffies + 1;
3770 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3771 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3772 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3773 	q_vector->tx.next_update = jiffies + 1;
3774 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3775 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3776 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3777 
3778 	i40e_enable_misc_int_causes(pf);
3779 
3780 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3781 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3782 
3783 	/* Associate the queue pair to the vector and enable the queue int */
3784 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3785 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3786 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3787 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3788 
3789 	wr32(hw, I40E_QINT_RQCTL(0), val);
3790 
3791 	if (i40e_enabled_xdp_vsi(vsi)) {
3792 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
3793 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3794 		      (I40E_QUEUE_TYPE_TX
3795 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3796 
3797 		wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3798 	}
3799 
3800 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
3801 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3802 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3803 
3804 	wr32(hw, I40E_QINT_TQCTL(0), val);
3805 	i40e_flush(hw);
3806 }
3807 
3808 /**
3809  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3810  * @pf: board private structure
3811  **/
3812 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3813 {
3814 	struct i40e_hw *hw = &pf->hw;
3815 
3816 	wr32(hw, I40E_PFINT_DYN_CTL0,
3817 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3818 	i40e_flush(hw);
3819 }
3820 
3821 /**
3822  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3823  * @pf: board private structure
3824  **/
3825 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3826 {
3827 	struct i40e_hw *hw = &pf->hw;
3828 	u32 val;
3829 
3830 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
3831 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3832 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3833 
3834 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
3835 	i40e_flush(hw);
3836 }
3837 
3838 /**
3839  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3840  * @irq: interrupt number
3841  * @data: pointer to a q_vector
3842  **/
3843 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3844 {
3845 	struct i40e_q_vector *q_vector = data;
3846 
3847 	if (!q_vector->tx.ring && !q_vector->rx.ring)
3848 		return IRQ_HANDLED;
3849 
3850 	napi_schedule_irqoff(&q_vector->napi);
3851 
3852 	return IRQ_HANDLED;
3853 }
3854 
3855 /**
3856  * i40e_irq_affinity_notify - Callback for affinity changes
3857  * @notify: context as to what irq was changed
3858  * @mask: the new affinity mask
3859  *
3860  * This is a callback function used by the irq_set_affinity_notifier function
3861  * so that we may register to receive changes to the irq affinity masks.
3862  **/
3863 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3864 				     const cpumask_t *mask)
3865 {
3866 	struct i40e_q_vector *q_vector =
3867 		container_of(notify, struct i40e_q_vector, affinity_notify);
3868 
3869 	cpumask_copy(&q_vector->affinity_mask, mask);
3870 }
3871 
3872 /**
3873  * i40e_irq_affinity_release - Callback for affinity notifier release
3874  * @ref: internal core kernel usage
3875  *
3876  * This is a callback function used by the irq_set_affinity_notifier function
3877  * to inform the current notification subscriber that they will no longer
3878  * receive notifications.
3879  **/
3880 static void i40e_irq_affinity_release(struct kref *ref) {}
3881 
3882 /**
3883  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3884  * @vsi: the VSI being configured
3885  * @basename: name for the vector
3886  *
3887  * Allocates MSI-X vectors and requests interrupts from the kernel.
3888  **/
3889 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3890 {
3891 	int q_vectors = vsi->num_q_vectors;
3892 	struct i40e_pf *pf = vsi->back;
3893 	int base = vsi->base_vector;
3894 	int rx_int_idx = 0;
3895 	int tx_int_idx = 0;
3896 	int vector, err;
3897 	int irq_num;
3898 	int cpu;
3899 
3900 	for (vector = 0; vector < q_vectors; vector++) {
3901 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
3902 
3903 		irq_num = pf->msix_entries[base + vector].vector;
3904 
3905 		if (q_vector->tx.ring && q_vector->rx.ring) {
3906 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3907 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3908 			tx_int_idx++;
3909 		} else if (q_vector->rx.ring) {
3910 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3911 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
3912 		} else if (q_vector->tx.ring) {
3913 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3914 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
3915 		} else {
3916 			/* skip this unused q_vector */
3917 			continue;
3918 		}
3919 		err = request_irq(irq_num,
3920 				  vsi->irq_handler,
3921 				  0,
3922 				  q_vector->name,
3923 				  q_vector);
3924 		if (err) {
3925 			dev_info(&pf->pdev->dev,
3926 				 "MSIX request_irq failed, error: %d\n", err);
3927 			goto free_queue_irqs;
3928 		}
3929 
3930 		/* register for affinity change notifications */
3931 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3932 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
3933 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
3934 		/* Spread affinity hints out across online CPUs.
3935 		 *
3936 		 * get_cpu_mask returns a static constant mask with
3937 		 * a permanent lifetime so it's ok to pass to
3938 		 * irq_update_affinity_hint without making a copy.
3939 		 */
3940 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
3941 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
3942 	}
3943 
3944 	vsi->irqs_ready = true;
3945 	return 0;
3946 
3947 free_queue_irqs:
3948 	while (vector) {
3949 		vector--;
3950 		irq_num = pf->msix_entries[base + vector].vector;
3951 		irq_set_affinity_notifier(irq_num, NULL);
3952 		irq_update_affinity_hint(irq_num, NULL);
3953 		free_irq(irq_num, &vsi->q_vectors[vector]);
3954 	}
3955 	return err;
3956 }
3957 
3958 /**
3959  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3960  * @vsi: the VSI being un-configured
3961  **/
3962 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3963 {
3964 	struct i40e_pf *pf = vsi->back;
3965 	struct i40e_hw *hw = &pf->hw;
3966 	int base = vsi->base_vector;
3967 	int i;
3968 
3969 	/* disable interrupt causation from each queue */
3970 	for (i = 0; i < vsi->num_queue_pairs; i++) {
3971 		u32 val;
3972 
3973 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3974 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3975 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3976 
3977 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3978 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3979 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3980 
3981 		if (!i40e_enabled_xdp_vsi(vsi))
3982 			continue;
3983 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
3984 	}
3985 
3986 	/* disable each interrupt */
3987 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3988 		for (i = vsi->base_vector;
3989 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
3990 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3991 
3992 		i40e_flush(hw);
3993 		for (i = 0; i < vsi->num_q_vectors; i++)
3994 			synchronize_irq(pf->msix_entries[i + base].vector);
3995 	} else {
3996 		/* Legacy and MSI mode - this stops all interrupt handling */
3997 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3998 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3999 		i40e_flush(hw);
4000 		synchronize_irq(pf->pdev->irq);
4001 	}
4002 }
4003 
4004 /**
4005  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4006  * @vsi: the VSI being configured
4007  **/
4008 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4009 {
4010 	struct i40e_pf *pf = vsi->back;
4011 	int i;
4012 
4013 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4014 		for (i = 0; i < vsi->num_q_vectors; i++)
4015 			i40e_irq_dynamic_enable(vsi, i);
4016 	} else {
4017 		i40e_irq_dynamic_enable_icr0(pf);
4018 	}
4019 
4020 	i40e_flush(&pf->hw);
4021 	return 0;
4022 }
4023 
4024 /**
4025  * i40e_free_misc_vector - Free the vector that handles non-queue events
4026  * @pf: board private structure
4027  **/
4028 static void i40e_free_misc_vector(struct i40e_pf *pf)
4029 {
4030 	/* Disable ICR 0 */
4031 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4032 	i40e_flush(&pf->hw);
4033 
4034 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4035 		synchronize_irq(pf->msix_entries[0].vector);
4036 		free_irq(pf->msix_entries[0].vector, pf);
4037 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4038 	}
4039 }
4040 
4041 /**
4042  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4043  * @irq: interrupt number
4044  * @data: pointer to a q_vector
4045  *
4046  * This is the handler used for all MSI/Legacy interrupts, and deals
4047  * with both queue and non-queue interrupts.  This is also used in
4048  * MSIX mode to handle the non-queue interrupts.
4049  **/
4050 static irqreturn_t i40e_intr(int irq, void *data)
4051 {
4052 	struct i40e_pf *pf = (struct i40e_pf *)data;
4053 	struct i40e_hw *hw = &pf->hw;
4054 	irqreturn_t ret = IRQ_NONE;
4055 	u32 icr0, icr0_remaining;
4056 	u32 val, ena_mask;
4057 
4058 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4059 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4060 
4061 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4062 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4063 		goto enable_intr;
4064 
4065 	/* if interrupt but no bits showing, must be SWINT */
4066 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4067 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4068 		pf->sw_int_count++;
4069 
4070 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4071 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4072 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4073 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4074 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4075 	}
4076 
4077 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4078 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4079 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4080 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4081 
4082 		/* We do not have a way to disarm Queue causes while leaving
4083 		 * interrupt enabled for all other causes, ideally
4084 		 * interrupt should be disabled while we are in NAPI but
4085 		 * this is not a performance path and napi_schedule()
4086 		 * can deal with rescheduling.
4087 		 */
4088 		if (!test_bit(__I40E_DOWN, pf->state))
4089 			napi_schedule_irqoff(&q_vector->napi);
4090 	}
4091 
4092 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4093 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4094 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4095 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4096 	}
4097 
4098 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4099 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4100 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4101 	}
4102 
4103 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4104 		/* disable any further VFLR event notifications */
4105 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4106 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4107 
4108 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4109 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4110 		} else {
4111 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4112 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4113 		}
4114 	}
4115 
4116 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4117 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4118 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4119 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4120 		val = rd32(hw, I40E_GLGEN_RSTAT);
4121 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4122 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4123 		if (val == I40E_RESET_CORER) {
4124 			pf->corer_count++;
4125 		} else if (val == I40E_RESET_GLOBR) {
4126 			pf->globr_count++;
4127 		} else if (val == I40E_RESET_EMPR) {
4128 			pf->empr_count++;
4129 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4130 		}
4131 	}
4132 
4133 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4134 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4135 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4136 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4137 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4138 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4139 	}
4140 
4141 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4142 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4143 
4144 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4145 			schedule_work(&pf->ptp_extts0_work);
4146 
4147 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4148 			i40e_ptp_tx_hwtstamp(pf);
4149 
4150 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4151 	}
4152 
4153 	/* If a critical error is pending we have no choice but to reset the
4154 	 * device.
4155 	 * Report and mask out any remaining unexpected interrupts.
4156 	 */
4157 	icr0_remaining = icr0 & ena_mask;
4158 	if (icr0_remaining) {
4159 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4160 			 icr0_remaining);
4161 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4162 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4163 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4164 			dev_info(&pf->pdev->dev, "device will be reset\n");
4165 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4166 			i40e_service_event_schedule(pf);
4167 		}
4168 		ena_mask &= ~icr0_remaining;
4169 	}
4170 	ret = IRQ_HANDLED;
4171 
4172 enable_intr:
4173 	/* re-enable interrupt causes */
4174 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4175 	if (!test_bit(__I40E_DOWN, pf->state) ||
4176 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4177 		i40e_service_event_schedule(pf);
4178 		i40e_irq_dynamic_enable_icr0(pf);
4179 	}
4180 
4181 	return ret;
4182 }
4183 
4184 /**
4185  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4186  * @tx_ring:  tx ring to clean
4187  * @budget:   how many cleans we're allowed
4188  *
4189  * Returns true if there's any budget left (e.g. the clean is finished)
4190  **/
4191 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4192 {
4193 	struct i40e_vsi *vsi = tx_ring->vsi;
4194 	u16 i = tx_ring->next_to_clean;
4195 	struct i40e_tx_buffer *tx_buf;
4196 	struct i40e_tx_desc *tx_desc;
4197 
4198 	tx_buf = &tx_ring->tx_bi[i];
4199 	tx_desc = I40E_TX_DESC(tx_ring, i);
4200 	i -= tx_ring->count;
4201 
4202 	do {
4203 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4204 
4205 		/* if next_to_watch is not set then there is no work pending */
4206 		if (!eop_desc)
4207 			break;
4208 
4209 		/* prevent any other reads prior to eop_desc */
4210 		smp_rmb();
4211 
4212 		/* if the descriptor isn't done, no work yet to do */
4213 		if (!(eop_desc->cmd_type_offset_bsz &
4214 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4215 			break;
4216 
4217 		/* clear next_to_watch to prevent false hangs */
4218 		tx_buf->next_to_watch = NULL;
4219 
4220 		tx_desc->buffer_addr = 0;
4221 		tx_desc->cmd_type_offset_bsz = 0;
4222 		/* move past filter desc */
4223 		tx_buf++;
4224 		tx_desc++;
4225 		i++;
4226 		if (unlikely(!i)) {
4227 			i -= tx_ring->count;
4228 			tx_buf = tx_ring->tx_bi;
4229 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4230 		}
4231 		/* unmap skb header data */
4232 		dma_unmap_single(tx_ring->dev,
4233 				 dma_unmap_addr(tx_buf, dma),
4234 				 dma_unmap_len(tx_buf, len),
4235 				 DMA_TO_DEVICE);
4236 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4237 			kfree(tx_buf->raw_buf);
4238 
4239 		tx_buf->raw_buf = NULL;
4240 		tx_buf->tx_flags = 0;
4241 		tx_buf->next_to_watch = NULL;
4242 		dma_unmap_len_set(tx_buf, len, 0);
4243 		tx_desc->buffer_addr = 0;
4244 		tx_desc->cmd_type_offset_bsz = 0;
4245 
4246 		/* move us past the eop_desc for start of next FD desc */
4247 		tx_buf++;
4248 		tx_desc++;
4249 		i++;
4250 		if (unlikely(!i)) {
4251 			i -= tx_ring->count;
4252 			tx_buf = tx_ring->tx_bi;
4253 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4254 		}
4255 
4256 		/* update budget accounting */
4257 		budget--;
4258 	} while (likely(budget));
4259 
4260 	i += tx_ring->count;
4261 	tx_ring->next_to_clean = i;
4262 
4263 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4264 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4265 
4266 	return budget > 0;
4267 }
4268 
4269 /**
4270  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4271  * @irq: interrupt number
4272  * @data: pointer to a q_vector
4273  **/
4274 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4275 {
4276 	struct i40e_q_vector *q_vector = data;
4277 	struct i40e_vsi *vsi;
4278 
4279 	if (!q_vector->tx.ring)
4280 		return IRQ_HANDLED;
4281 
4282 	vsi = q_vector->tx.ring->vsi;
4283 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4284 
4285 	return IRQ_HANDLED;
4286 }
4287 
4288 /**
4289  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4290  * @vsi: the VSI being configured
4291  * @v_idx: vector index
4292  * @qp_idx: queue pair index
4293  **/
4294 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4295 {
4296 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4297 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4298 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4299 
4300 	tx_ring->q_vector = q_vector;
4301 	tx_ring->next = q_vector->tx.ring;
4302 	q_vector->tx.ring = tx_ring;
4303 	q_vector->tx.count++;
4304 
4305 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4306 	if (i40e_enabled_xdp_vsi(vsi)) {
4307 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4308 
4309 		xdp_ring->q_vector = q_vector;
4310 		xdp_ring->next = q_vector->tx.ring;
4311 		q_vector->tx.ring = xdp_ring;
4312 		q_vector->tx.count++;
4313 	}
4314 
4315 	rx_ring->q_vector = q_vector;
4316 	rx_ring->next = q_vector->rx.ring;
4317 	q_vector->rx.ring = rx_ring;
4318 	q_vector->rx.count++;
4319 }
4320 
4321 /**
4322  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4323  * @vsi: the VSI being configured
4324  *
4325  * This function maps descriptor rings to the queue-specific vectors
4326  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4327  * one vector per queue pair, but on a constrained vector budget, we
4328  * group the queue pairs as "efficiently" as possible.
4329  **/
4330 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4331 {
4332 	int qp_remaining = vsi->num_queue_pairs;
4333 	int q_vectors = vsi->num_q_vectors;
4334 	int num_ringpairs;
4335 	int v_start = 0;
4336 	int qp_idx = 0;
4337 
4338 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4339 	 * group them so there are multiple queues per vector.
4340 	 * It is also important to go through all the vectors available to be
4341 	 * sure that if we don't use all the vectors, that the remaining vectors
4342 	 * are cleared. This is especially important when decreasing the
4343 	 * number of queues in use.
4344 	 */
4345 	for (; v_start < q_vectors; v_start++) {
4346 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4347 
4348 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4349 
4350 		q_vector->num_ringpairs = num_ringpairs;
4351 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4352 
4353 		q_vector->rx.count = 0;
4354 		q_vector->tx.count = 0;
4355 		q_vector->rx.ring = NULL;
4356 		q_vector->tx.ring = NULL;
4357 
4358 		while (num_ringpairs--) {
4359 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4360 			qp_idx++;
4361 			qp_remaining--;
4362 		}
4363 	}
4364 }
4365 
4366 /**
4367  * i40e_vsi_request_irq - Request IRQ from the OS
4368  * @vsi: the VSI being configured
4369  * @basename: name for the vector
4370  **/
4371 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4372 {
4373 	struct i40e_pf *pf = vsi->back;
4374 	int err;
4375 
4376 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4377 		err = i40e_vsi_request_irq_msix(vsi, basename);
4378 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4379 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4380 				  pf->int_name, pf);
4381 	else
4382 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4383 				  pf->int_name, pf);
4384 
4385 	if (err)
4386 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4387 
4388 	return err;
4389 }
4390 
4391 #ifdef CONFIG_NET_POLL_CONTROLLER
4392 /**
4393  * i40e_netpoll - A Polling 'interrupt' handler
4394  * @netdev: network interface device structure
4395  *
4396  * This is used by netconsole to send skbs without having to re-enable
4397  * interrupts.  It's not called while the normal interrupt routine is executing.
4398  **/
4399 static void i40e_netpoll(struct net_device *netdev)
4400 {
4401 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4402 	struct i40e_vsi *vsi = np->vsi;
4403 	struct i40e_pf *pf = vsi->back;
4404 	int i;
4405 
4406 	/* if interface is down do nothing */
4407 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4408 		return;
4409 
4410 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4411 		for (i = 0; i < vsi->num_q_vectors; i++)
4412 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4413 	} else {
4414 		i40e_intr(pf->pdev->irq, netdev);
4415 	}
4416 }
4417 #endif
4418 
4419 #define I40E_QTX_ENA_WAIT_COUNT 50
4420 
4421 /**
4422  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4423  * @pf: the PF being configured
4424  * @pf_q: the PF queue
4425  * @enable: enable or disable state of the queue
4426  *
4427  * This routine will wait for the given Tx queue of the PF to reach the
4428  * enabled or disabled state.
4429  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4430  * multiple retries; else will return 0 in case of success.
4431  **/
4432 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4433 {
4434 	int i;
4435 	u32 tx_reg;
4436 
4437 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4438 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4439 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4440 			break;
4441 
4442 		usleep_range(10, 20);
4443 	}
4444 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4445 		return -ETIMEDOUT;
4446 
4447 	return 0;
4448 }
4449 
4450 /**
4451  * i40e_control_tx_q - Start or stop a particular Tx queue
4452  * @pf: the PF structure
4453  * @pf_q: the PF queue to configure
4454  * @enable: start or stop the queue
4455  *
4456  * This function enables or disables a single queue. Note that any delay
4457  * required after the operation is expected to be handled by the caller of
4458  * this function.
4459  **/
4460 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4461 {
4462 	struct i40e_hw *hw = &pf->hw;
4463 	u32 tx_reg;
4464 	int i;
4465 
4466 	/* warn the TX unit of coming changes */
4467 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4468 	if (!enable)
4469 		usleep_range(10, 20);
4470 
4471 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4472 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4473 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4474 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4475 			break;
4476 		usleep_range(1000, 2000);
4477 	}
4478 
4479 	/* Skip if the queue is already in the requested state */
4480 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4481 		return;
4482 
4483 	/* turn on/off the queue */
4484 	if (enable) {
4485 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4486 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4487 	} else {
4488 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4489 	}
4490 
4491 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4492 }
4493 
4494 /**
4495  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4496  * @seid: VSI SEID
4497  * @pf: the PF structure
4498  * @pf_q: the PF queue to configure
4499  * @is_xdp: true if the queue is used for XDP
4500  * @enable: start or stop the queue
4501  **/
4502 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4503 			   bool is_xdp, bool enable)
4504 {
4505 	int ret;
4506 
4507 	i40e_control_tx_q(pf, pf_q, enable);
4508 
4509 	/* wait for the change to finish */
4510 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4511 	if (ret) {
4512 		dev_info(&pf->pdev->dev,
4513 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4514 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4515 			 (enable ? "en" : "dis"));
4516 	}
4517 
4518 	return ret;
4519 }
4520 
4521 /**
4522  * i40e_vsi_enable_tx - Start a VSI's rings
4523  * @vsi: the VSI being configured
4524  **/
4525 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4526 {
4527 	struct i40e_pf *pf = vsi->back;
4528 	int i, pf_q, ret = 0;
4529 
4530 	pf_q = vsi->base_queue;
4531 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4532 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4533 					     pf_q,
4534 					     false /*is xdp*/, true);
4535 		if (ret)
4536 			break;
4537 
4538 		if (!i40e_enabled_xdp_vsi(vsi))
4539 			continue;
4540 
4541 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4542 					     pf_q + vsi->alloc_queue_pairs,
4543 					     true /*is xdp*/, true);
4544 		if (ret)
4545 			break;
4546 	}
4547 	return ret;
4548 }
4549 
4550 /**
4551  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4552  * @pf: the PF being configured
4553  * @pf_q: the PF queue
4554  * @enable: enable or disable state of the queue
4555  *
4556  * This routine will wait for the given Rx queue of the PF to reach the
4557  * enabled or disabled state.
4558  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4559  * multiple retries; else will return 0 in case of success.
4560  **/
4561 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4562 {
4563 	int i;
4564 	u32 rx_reg;
4565 
4566 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4567 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4568 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4569 			break;
4570 
4571 		usleep_range(10, 20);
4572 	}
4573 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4574 		return -ETIMEDOUT;
4575 
4576 	return 0;
4577 }
4578 
4579 /**
4580  * i40e_control_rx_q - Start or stop a particular Rx queue
4581  * @pf: the PF structure
4582  * @pf_q: the PF queue to configure
4583  * @enable: start or stop the queue
4584  *
4585  * This function enables or disables a single queue. Note that
4586  * any delay required after the operation is expected to be
4587  * handled by the caller of this function.
4588  **/
4589 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4590 {
4591 	struct i40e_hw *hw = &pf->hw;
4592 	u32 rx_reg;
4593 	int i;
4594 
4595 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4596 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4597 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4598 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4599 			break;
4600 		usleep_range(1000, 2000);
4601 	}
4602 
4603 	/* Skip if the queue is already in the requested state */
4604 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4605 		return;
4606 
4607 	/* turn on/off the queue */
4608 	if (enable)
4609 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4610 	else
4611 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4612 
4613 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4614 }
4615 
4616 /**
4617  * i40e_control_wait_rx_q
4618  * @pf: the PF structure
4619  * @pf_q: queue being configured
4620  * @enable: start or stop the rings
4621  *
4622  * This function enables or disables a single queue along with waiting
4623  * for the change to finish. The caller of this function should handle
4624  * the delays needed in the case of disabling queues.
4625  **/
4626 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4627 {
4628 	int ret = 0;
4629 
4630 	i40e_control_rx_q(pf, pf_q, enable);
4631 
4632 	/* wait for the change to finish */
4633 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4634 	if (ret)
4635 		return ret;
4636 
4637 	return ret;
4638 }
4639 
4640 /**
4641  * i40e_vsi_enable_rx - Start a VSI's rings
4642  * @vsi: the VSI being configured
4643  **/
4644 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4645 {
4646 	struct i40e_pf *pf = vsi->back;
4647 	int i, pf_q, ret = 0;
4648 
4649 	pf_q = vsi->base_queue;
4650 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4651 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4652 		if (ret) {
4653 			dev_info(&pf->pdev->dev,
4654 				 "VSI seid %d Rx ring %d enable timeout\n",
4655 				 vsi->seid, pf_q);
4656 			break;
4657 		}
4658 	}
4659 
4660 	return ret;
4661 }
4662 
4663 /**
4664  * i40e_vsi_start_rings - Start a VSI's rings
4665  * @vsi: the VSI being configured
4666  **/
4667 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4668 {
4669 	int ret = 0;
4670 
4671 	/* do rx first for enable and last for disable */
4672 	ret = i40e_vsi_enable_rx(vsi);
4673 	if (ret)
4674 		return ret;
4675 	ret = i40e_vsi_enable_tx(vsi);
4676 
4677 	return ret;
4678 }
4679 
4680 #define I40E_DISABLE_TX_GAP_MSEC	50
4681 
4682 /**
4683  * i40e_vsi_stop_rings - Stop a VSI's rings
4684  * @vsi: the VSI being configured
4685  **/
4686 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4687 {
4688 	struct i40e_pf *pf = vsi->back;
4689 	int pf_q, err, q_end;
4690 
4691 	/* When port TX is suspended, don't wait */
4692 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4693 		return i40e_vsi_stop_rings_no_wait(vsi);
4694 
4695 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4696 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4697 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4698 
4699 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4700 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4701 		if (err)
4702 			dev_info(&pf->pdev->dev,
4703 				 "VSI seid %d Rx ring %d disable timeout\n",
4704 				 vsi->seid, pf_q);
4705 	}
4706 
4707 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4708 	pf_q = vsi->base_queue;
4709 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4710 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4711 
4712 	i40e_vsi_wait_queues_disabled(vsi);
4713 }
4714 
4715 /**
4716  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4717  * @vsi: the VSI being shutdown
4718  *
4719  * This function stops all the rings for a VSI but does not delay to verify
4720  * that rings have been disabled. It is expected that the caller is shutting
4721  * down multiple VSIs at once and will delay together for all the VSIs after
4722  * initiating the shutdown. This is particularly useful for shutting down lots
4723  * of VFs together. Otherwise, a large delay can be incurred while configuring
4724  * each VSI in serial.
4725  **/
4726 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4727 {
4728 	struct i40e_pf *pf = vsi->back;
4729 	int i, pf_q;
4730 
4731 	pf_q = vsi->base_queue;
4732 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4733 		i40e_control_tx_q(pf, pf_q, false);
4734 		i40e_control_rx_q(pf, pf_q, false);
4735 	}
4736 }
4737 
4738 /**
4739  * i40e_vsi_free_irq - Free the irq association with the OS
4740  * @vsi: the VSI being configured
4741  **/
4742 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4743 {
4744 	struct i40e_pf *pf = vsi->back;
4745 	struct i40e_hw *hw = &pf->hw;
4746 	int base = vsi->base_vector;
4747 	u32 val, qp;
4748 	int i;
4749 
4750 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4751 		if (!vsi->q_vectors)
4752 			return;
4753 
4754 		if (!vsi->irqs_ready)
4755 			return;
4756 
4757 		vsi->irqs_ready = false;
4758 		for (i = 0; i < vsi->num_q_vectors; i++) {
4759 			int irq_num;
4760 			u16 vector;
4761 
4762 			vector = i + base;
4763 			irq_num = pf->msix_entries[vector].vector;
4764 
4765 			/* free only the irqs that were actually requested */
4766 			if (!vsi->q_vectors[i] ||
4767 			    !vsi->q_vectors[i]->num_ringpairs)
4768 				continue;
4769 
4770 			/* clear the affinity notifier in the IRQ descriptor */
4771 			irq_set_affinity_notifier(irq_num, NULL);
4772 			/* remove our suggested affinity mask for this IRQ */
4773 			irq_update_affinity_hint(irq_num, NULL);
4774 			synchronize_irq(irq_num);
4775 			free_irq(irq_num, vsi->q_vectors[i]);
4776 
4777 			/* Tear down the interrupt queue link list
4778 			 *
4779 			 * We know that they come in pairs and always
4780 			 * the Rx first, then the Tx.  To clear the
4781 			 * link list, stick the EOL value into the
4782 			 * next_q field of the registers.
4783 			 */
4784 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4785 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4786 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4787 			val |= I40E_QUEUE_END_OF_LIST
4788 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4789 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4790 
4791 			while (qp != I40E_QUEUE_END_OF_LIST) {
4792 				u32 next;
4793 
4794 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4795 
4796 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4797 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4798 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4799 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4800 
4801 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4802 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4803 
4804 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4805 
4806 				val = rd32(hw, I40E_QINT_TQCTL(qp));
4807 
4808 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4809 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4810 
4811 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4812 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4813 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4814 					 I40E_QINT_TQCTL_INTEVENT_MASK);
4815 
4816 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4817 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4818 
4819 				wr32(hw, I40E_QINT_TQCTL(qp), val);
4820 				qp = next;
4821 			}
4822 		}
4823 	} else {
4824 		free_irq(pf->pdev->irq, pf);
4825 
4826 		val = rd32(hw, I40E_PFINT_LNKLST0);
4827 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4828 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4829 		val |= I40E_QUEUE_END_OF_LIST
4830 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4831 		wr32(hw, I40E_PFINT_LNKLST0, val);
4832 
4833 		val = rd32(hw, I40E_QINT_RQCTL(qp));
4834 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4835 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4836 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4837 			 I40E_QINT_RQCTL_INTEVENT_MASK);
4838 
4839 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4840 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4841 
4842 		wr32(hw, I40E_QINT_RQCTL(qp), val);
4843 
4844 		val = rd32(hw, I40E_QINT_TQCTL(qp));
4845 
4846 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4847 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4848 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4849 			 I40E_QINT_TQCTL_INTEVENT_MASK);
4850 
4851 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4852 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4853 
4854 		wr32(hw, I40E_QINT_TQCTL(qp), val);
4855 	}
4856 }
4857 
4858 /**
4859  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4860  * @vsi: the VSI being configured
4861  * @v_idx: Index of vector to be freed
4862  *
4863  * This function frees the memory allocated to the q_vector.  In addition if
4864  * NAPI is enabled it will delete any references to the NAPI struct prior
4865  * to freeing the q_vector.
4866  **/
4867 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4868 {
4869 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4870 	struct i40e_ring *ring;
4871 
4872 	if (!q_vector)
4873 		return;
4874 
4875 	/* disassociate q_vector from rings */
4876 	i40e_for_each_ring(ring, q_vector->tx)
4877 		ring->q_vector = NULL;
4878 
4879 	i40e_for_each_ring(ring, q_vector->rx)
4880 		ring->q_vector = NULL;
4881 
4882 	/* only VSI w/ an associated netdev is set up w/ NAPI */
4883 	if (vsi->netdev)
4884 		netif_napi_del(&q_vector->napi);
4885 
4886 	vsi->q_vectors[v_idx] = NULL;
4887 
4888 	kfree_rcu(q_vector, rcu);
4889 }
4890 
4891 /**
4892  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4893  * @vsi: the VSI being un-configured
4894  *
4895  * This frees the memory allocated to the q_vectors and
4896  * deletes references to the NAPI struct.
4897  **/
4898 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4899 {
4900 	int v_idx;
4901 
4902 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4903 		i40e_free_q_vector(vsi, v_idx);
4904 }
4905 
4906 /**
4907  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4908  * @pf: board private structure
4909  **/
4910 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4911 {
4912 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4913 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4914 		pci_disable_msix(pf->pdev);
4915 		kfree(pf->msix_entries);
4916 		pf->msix_entries = NULL;
4917 		kfree(pf->irq_pile);
4918 		pf->irq_pile = NULL;
4919 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4920 		pci_disable_msi(pf->pdev);
4921 	}
4922 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4923 }
4924 
4925 /**
4926  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4927  * @pf: board private structure
4928  *
4929  * We go through and clear interrupt specific resources and reset the structure
4930  * to pre-load conditions
4931  **/
4932 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4933 {
4934 	int i;
4935 
4936 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
4937 		i40e_free_misc_vector(pf);
4938 
4939 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4940 		      I40E_IWARP_IRQ_PILE_ID);
4941 
4942 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
4943 	for (i = 0; i < pf->num_alloc_vsi; i++)
4944 		if (pf->vsi[i])
4945 			i40e_vsi_free_q_vectors(pf->vsi[i]);
4946 	i40e_reset_interrupt_capability(pf);
4947 }
4948 
4949 /**
4950  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4951  * @vsi: the VSI being configured
4952  **/
4953 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4954 {
4955 	int q_idx;
4956 
4957 	if (!vsi->netdev)
4958 		return;
4959 
4960 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4961 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4962 
4963 		if (q_vector->rx.ring || q_vector->tx.ring)
4964 			napi_enable(&q_vector->napi);
4965 	}
4966 }
4967 
4968 /**
4969  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4970  * @vsi: the VSI being configured
4971  **/
4972 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4973 {
4974 	int q_idx;
4975 
4976 	if (!vsi->netdev)
4977 		return;
4978 
4979 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4980 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4981 
4982 		if (q_vector->rx.ring || q_vector->tx.ring)
4983 			napi_disable(&q_vector->napi);
4984 	}
4985 }
4986 
4987 /**
4988  * i40e_vsi_close - Shut down a VSI
4989  * @vsi: the vsi to be quelled
4990  **/
4991 static void i40e_vsi_close(struct i40e_vsi *vsi)
4992 {
4993 	struct i40e_pf *pf = vsi->back;
4994 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
4995 		i40e_down(vsi);
4996 	i40e_vsi_free_irq(vsi);
4997 	i40e_vsi_free_tx_resources(vsi);
4998 	i40e_vsi_free_rx_resources(vsi);
4999 	vsi->current_netdev_flags = 0;
5000 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5001 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5002 		set_bit(__I40E_CLIENT_RESET, pf->state);
5003 }
5004 
5005 /**
5006  * i40e_quiesce_vsi - Pause a given VSI
5007  * @vsi: the VSI being paused
5008  **/
5009 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5010 {
5011 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5012 		return;
5013 
5014 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5015 	if (vsi->netdev && netif_running(vsi->netdev))
5016 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5017 	else
5018 		i40e_vsi_close(vsi);
5019 }
5020 
5021 /**
5022  * i40e_unquiesce_vsi - Resume a given VSI
5023  * @vsi: the VSI being resumed
5024  **/
5025 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5026 {
5027 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5028 		return;
5029 
5030 	if (vsi->netdev && netif_running(vsi->netdev))
5031 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5032 	else
5033 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5034 }
5035 
5036 /**
5037  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5038  * @pf: the PF
5039  **/
5040 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5041 {
5042 	int v;
5043 
5044 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5045 		if (pf->vsi[v])
5046 			i40e_quiesce_vsi(pf->vsi[v]);
5047 	}
5048 }
5049 
5050 /**
5051  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5052  * @pf: the PF
5053  **/
5054 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5055 {
5056 	int v;
5057 
5058 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5059 		if (pf->vsi[v])
5060 			i40e_unquiesce_vsi(pf->vsi[v]);
5061 	}
5062 }
5063 
5064 /**
5065  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5066  * @vsi: the VSI being configured
5067  *
5068  * Wait until all queues on a given VSI have been disabled.
5069  **/
5070 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5071 {
5072 	struct i40e_pf *pf = vsi->back;
5073 	int i, pf_q, ret;
5074 
5075 	pf_q = vsi->base_queue;
5076 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5077 		/* Check and wait for the Tx queue */
5078 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5079 		if (ret) {
5080 			dev_info(&pf->pdev->dev,
5081 				 "VSI seid %d Tx ring %d disable timeout\n",
5082 				 vsi->seid, pf_q);
5083 			return ret;
5084 		}
5085 
5086 		if (!i40e_enabled_xdp_vsi(vsi))
5087 			goto wait_rx;
5088 
5089 		/* Check and wait for the XDP Tx queue */
5090 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5091 				       false);
5092 		if (ret) {
5093 			dev_info(&pf->pdev->dev,
5094 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5095 				 vsi->seid, pf_q);
5096 			return ret;
5097 		}
5098 wait_rx:
5099 		/* Check and wait for the Rx queue */
5100 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5101 		if (ret) {
5102 			dev_info(&pf->pdev->dev,
5103 				 "VSI seid %d Rx ring %d disable timeout\n",
5104 				 vsi->seid, pf_q);
5105 			return ret;
5106 		}
5107 	}
5108 
5109 	return 0;
5110 }
5111 
5112 #ifdef CONFIG_I40E_DCB
5113 /**
5114  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5115  * @pf: the PF
5116  *
5117  * This function waits for the queues to be in disabled state for all the
5118  * VSIs that are managed by this PF.
5119  **/
5120 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5121 {
5122 	int v, ret = 0;
5123 
5124 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5125 		if (pf->vsi[v]) {
5126 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5127 			if (ret)
5128 				break;
5129 		}
5130 	}
5131 
5132 	return ret;
5133 }
5134 
5135 #endif
5136 
5137 /**
5138  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5139  * @pf: pointer to PF
5140  *
5141  * Get TC map for ISCSI PF type that will include iSCSI TC
5142  * and LAN TC.
5143  **/
5144 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5145 {
5146 	struct i40e_dcb_app_priority_table app;
5147 	struct i40e_hw *hw = &pf->hw;
5148 	u8 enabled_tc = 1; /* TC0 is always enabled */
5149 	u8 tc, i;
5150 	/* Get the iSCSI APP TLV */
5151 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5152 
5153 	for (i = 0; i < dcbcfg->numapps; i++) {
5154 		app = dcbcfg->app[i];
5155 		if (app.selector == I40E_APP_SEL_TCPIP &&
5156 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5157 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5158 			enabled_tc |= BIT(tc);
5159 			break;
5160 		}
5161 	}
5162 
5163 	return enabled_tc;
5164 }
5165 
5166 /**
5167  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5168  * @dcbcfg: the corresponding DCBx configuration structure
5169  *
5170  * Return the number of TCs from given DCBx configuration
5171  **/
5172 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5173 {
5174 	int i, tc_unused = 0;
5175 	u8 num_tc = 0;
5176 	u8 ret = 0;
5177 
5178 	/* Scan the ETS Config Priority Table to find
5179 	 * traffic class enabled for a given priority
5180 	 * and create a bitmask of enabled TCs
5181 	 */
5182 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5183 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5184 
5185 	/* Now scan the bitmask to check for
5186 	 * contiguous TCs starting with TC0
5187 	 */
5188 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5189 		if (num_tc & BIT(i)) {
5190 			if (!tc_unused) {
5191 				ret++;
5192 			} else {
5193 				pr_err("Non-contiguous TC - Disabling DCB\n");
5194 				return 1;
5195 			}
5196 		} else {
5197 			tc_unused = 1;
5198 		}
5199 	}
5200 
5201 	/* There is always at least TC0 */
5202 	if (!ret)
5203 		ret = 1;
5204 
5205 	return ret;
5206 }
5207 
5208 /**
5209  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5210  * @dcbcfg: the corresponding DCBx configuration structure
5211  *
5212  * Query the current DCB configuration and return the number of
5213  * traffic classes enabled from the given DCBX config
5214  **/
5215 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5216 {
5217 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5218 	u8 enabled_tc = 1;
5219 	u8 i;
5220 
5221 	for (i = 0; i < num_tc; i++)
5222 		enabled_tc |= BIT(i);
5223 
5224 	return enabled_tc;
5225 }
5226 
5227 /**
5228  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5229  * @pf: PF being queried
5230  *
5231  * Query the current MQPRIO configuration and return the number of
5232  * traffic classes enabled.
5233  **/
5234 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5235 {
5236 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5237 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5238 	u8 enabled_tc = 1, i;
5239 
5240 	for (i = 1; i < num_tc; i++)
5241 		enabled_tc |= BIT(i);
5242 	return enabled_tc;
5243 }
5244 
5245 /**
5246  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5247  * @pf: PF being queried
5248  *
5249  * Return number of traffic classes enabled for the given PF
5250  **/
5251 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5252 {
5253 	struct i40e_hw *hw = &pf->hw;
5254 	u8 i, enabled_tc = 1;
5255 	u8 num_tc = 0;
5256 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5257 
5258 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5259 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5260 
5261 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5262 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5263 		return 1;
5264 
5265 	/* SFP mode will be enabled for all TCs on port */
5266 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5267 		return i40e_dcb_get_num_tc(dcbcfg);
5268 
5269 	/* MFP mode return count of enabled TCs for this PF */
5270 	if (pf->hw.func_caps.iscsi)
5271 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5272 	else
5273 		return 1; /* Only TC0 */
5274 
5275 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5276 		if (enabled_tc & BIT(i))
5277 			num_tc++;
5278 	}
5279 	return num_tc;
5280 }
5281 
5282 /**
5283  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5284  * @pf: PF being queried
5285  *
5286  * Return a bitmap for enabled traffic classes for this PF.
5287  **/
5288 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5289 {
5290 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5291 		return i40e_mqprio_get_enabled_tc(pf);
5292 
5293 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5294 	 * default TC
5295 	 */
5296 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5297 		return I40E_DEFAULT_TRAFFIC_CLASS;
5298 
5299 	/* SFP mode we want PF to be enabled for all TCs */
5300 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5301 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5302 
5303 	/* MFP enabled and iSCSI PF type */
5304 	if (pf->hw.func_caps.iscsi)
5305 		return i40e_get_iscsi_tc_map(pf);
5306 	else
5307 		return I40E_DEFAULT_TRAFFIC_CLASS;
5308 }
5309 
5310 /**
5311  * i40e_vsi_get_bw_info - Query VSI BW Information
5312  * @vsi: the VSI being queried
5313  *
5314  * Returns 0 on success, negative value on failure
5315  **/
5316 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5317 {
5318 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5319 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5320 	struct i40e_pf *pf = vsi->back;
5321 	struct i40e_hw *hw = &pf->hw;
5322 	i40e_status ret;
5323 	u32 tc_bw_max;
5324 	int i;
5325 
5326 	/* Get the VSI level BW configuration */
5327 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5328 	if (ret) {
5329 		dev_info(&pf->pdev->dev,
5330 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5331 			 i40e_stat_str(&pf->hw, ret),
5332 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5333 		return -EINVAL;
5334 	}
5335 
5336 	/* Get the VSI level BW configuration per TC */
5337 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5338 					       NULL);
5339 	if (ret) {
5340 		dev_info(&pf->pdev->dev,
5341 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5342 			 i40e_stat_str(&pf->hw, ret),
5343 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5344 		return -EINVAL;
5345 	}
5346 
5347 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5348 		dev_info(&pf->pdev->dev,
5349 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5350 			 bw_config.tc_valid_bits,
5351 			 bw_ets_config.tc_valid_bits);
5352 		/* Still continuing */
5353 	}
5354 
5355 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5356 	vsi->bw_max_quanta = bw_config.max_bw;
5357 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5358 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5359 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5360 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5361 		vsi->bw_ets_limit_credits[i] =
5362 					le16_to_cpu(bw_ets_config.credits[i]);
5363 		/* 3 bits out of 4 for each TC */
5364 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5365 	}
5366 
5367 	return 0;
5368 }
5369 
5370 /**
5371  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5372  * @vsi: the VSI being configured
5373  * @enabled_tc: TC bitmap
5374  * @bw_share: BW shared credits per TC
5375  *
5376  * Returns 0 on success, negative value on failure
5377  **/
5378 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5379 				       u8 *bw_share)
5380 {
5381 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5382 	struct i40e_pf *pf = vsi->back;
5383 	i40e_status ret;
5384 	int i;
5385 
5386 	/* There is no need to reset BW when mqprio mode is on.  */
5387 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5388 		return 0;
5389 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5390 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5391 		if (ret)
5392 			dev_info(&pf->pdev->dev,
5393 				 "Failed to reset tx rate for vsi->seid %u\n",
5394 				 vsi->seid);
5395 		return ret;
5396 	}
5397 	memset(&bw_data, 0, sizeof(bw_data));
5398 	bw_data.tc_valid_bits = enabled_tc;
5399 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5400 		bw_data.tc_bw_credits[i] = bw_share[i];
5401 
5402 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5403 	if (ret) {
5404 		dev_info(&pf->pdev->dev,
5405 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5406 			 pf->hw.aq.asq_last_status);
5407 		return -EINVAL;
5408 	}
5409 
5410 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5411 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5412 
5413 	return 0;
5414 }
5415 
5416 /**
5417  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5418  * @vsi: the VSI being configured
5419  * @enabled_tc: TC map to be enabled
5420  *
5421  **/
5422 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5423 {
5424 	struct net_device *netdev = vsi->netdev;
5425 	struct i40e_pf *pf = vsi->back;
5426 	struct i40e_hw *hw = &pf->hw;
5427 	u8 netdev_tc = 0;
5428 	int i;
5429 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5430 
5431 	if (!netdev)
5432 		return;
5433 
5434 	if (!enabled_tc) {
5435 		netdev_reset_tc(netdev);
5436 		return;
5437 	}
5438 
5439 	/* Set up actual enabled TCs on the VSI */
5440 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5441 		return;
5442 
5443 	/* set per TC queues for the VSI */
5444 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5445 		/* Only set TC queues for enabled tcs
5446 		 *
5447 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5448 		 * enabled_tc bitmap would be 0x00001001; the driver
5449 		 * will set the numtc for netdev as 2 that will be
5450 		 * referenced by the netdev layer as TC 0 and 1.
5451 		 */
5452 		if (vsi->tc_config.enabled_tc & BIT(i))
5453 			netdev_set_tc_queue(netdev,
5454 					vsi->tc_config.tc_info[i].netdev_tc,
5455 					vsi->tc_config.tc_info[i].qcount,
5456 					vsi->tc_config.tc_info[i].qoffset);
5457 	}
5458 
5459 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5460 		return;
5461 
5462 	/* Assign UP2TC map for the VSI */
5463 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5464 		/* Get the actual TC# for the UP */
5465 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5466 		/* Get the mapped netdev TC# for the UP */
5467 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5468 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5469 	}
5470 }
5471 
5472 /**
5473  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5474  * @vsi: the VSI being configured
5475  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5476  **/
5477 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5478 				      struct i40e_vsi_context *ctxt)
5479 {
5480 	/* copy just the sections touched not the entire info
5481 	 * since not all sections are valid as returned by
5482 	 * update vsi params
5483 	 */
5484 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5485 	memcpy(&vsi->info.queue_mapping,
5486 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5487 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5488 	       sizeof(vsi->info.tc_mapping));
5489 }
5490 
5491 /**
5492  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5493  * @vsi: the VSI being reconfigured
5494  * @vsi_offset: offset from main VF VSI
5495  */
5496 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5497 {
5498 	struct i40e_vsi_context ctxt = {};
5499 	struct i40e_pf *pf;
5500 	struct i40e_hw *hw;
5501 	int ret;
5502 
5503 	if (!vsi)
5504 		return I40E_ERR_PARAM;
5505 	pf = vsi->back;
5506 	hw = &pf->hw;
5507 
5508 	ctxt.seid = vsi->seid;
5509 	ctxt.pf_num = hw->pf_id;
5510 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5511 	ctxt.uplink_seid = vsi->uplink_seid;
5512 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5513 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5514 	ctxt.info = vsi->info;
5515 
5516 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5517 				 false);
5518 	if (vsi->reconfig_rss) {
5519 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5520 				      vsi->num_queue_pairs);
5521 		ret = i40e_vsi_config_rss(vsi);
5522 		if (ret) {
5523 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5524 			return ret;
5525 		}
5526 		vsi->reconfig_rss = false;
5527 	}
5528 
5529 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5530 	if (ret) {
5531 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %s aq_err %s\n",
5532 			 i40e_stat_str(hw, ret),
5533 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5534 		return ret;
5535 	}
5536 	/* update the local VSI info with updated queue map */
5537 	i40e_vsi_update_queue_map(vsi, &ctxt);
5538 	vsi->info.valid_sections = 0;
5539 
5540 	return ret;
5541 }
5542 
5543 /**
5544  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5545  * @vsi: VSI to be configured
5546  * @enabled_tc: TC bitmap
5547  *
5548  * This configures a particular VSI for TCs that are mapped to the
5549  * given TC bitmap. It uses default bandwidth share for TCs across
5550  * VSIs to configure TC for a particular VSI.
5551  *
5552  * NOTE:
5553  * It is expected that the VSI queues have been quisced before calling
5554  * this function.
5555  **/
5556 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5557 {
5558 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5559 	struct i40e_pf *pf = vsi->back;
5560 	struct i40e_hw *hw = &pf->hw;
5561 	struct i40e_vsi_context ctxt;
5562 	int ret = 0;
5563 	int i;
5564 
5565 	/* Check if enabled_tc is same as existing or new TCs */
5566 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5567 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5568 		return ret;
5569 
5570 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5571 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5572 		if (enabled_tc & BIT(i))
5573 			bw_share[i] = 1;
5574 	}
5575 
5576 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5577 	if (ret) {
5578 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5579 
5580 		dev_info(&pf->pdev->dev,
5581 			 "Failed configuring TC map %d for VSI %d\n",
5582 			 enabled_tc, vsi->seid);
5583 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5584 						  &bw_config, NULL);
5585 		if (ret) {
5586 			dev_info(&pf->pdev->dev,
5587 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5588 				 i40e_stat_str(hw, ret),
5589 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5590 			goto out;
5591 		}
5592 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5593 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5594 
5595 			if (!valid_tc)
5596 				valid_tc = bw_config.tc_valid_bits;
5597 			/* Always enable TC0, no matter what */
5598 			valid_tc |= 1;
5599 			dev_info(&pf->pdev->dev,
5600 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5601 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5602 			enabled_tc = valid_tc;
5603 		}
5604 
5605 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5606 		if (ret) {
5607 			dev_err(&pf->pdev->dev,
5608 				"Unable to  configure TC map %d for VSI %d\n",
5609 				enabled_tc, vsi->seid);
5610 			goto out;
5611 		}
5612 	}
5613 
5614 	/* Update Queue Pairs Mapping for currently enabled UPs */
5615 	ctxt.seid = vsi->seid;
5616 	ctxt.pf_num = vsi->back->hw.pf_id;
5617 	ctxt.vf_num = 0;
5618 	ctxt.uplink_seid = vsi->uplink_seid;
5619 	ctxt.info = vsi->info;
5620 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5621 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5622 		if (ret)
5623 			goto out;
5624 	} else {
5625 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5626 	}
5627 
5628 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5629 	 * queues changed.
5630 	 */
5631 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5632 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5633 				      vsi->num_queue_pairs);
5634 		ret = i40e_vsi_config_rss(vsi);
5635 		if (ret) {
5636 			dev_info(&vsi->back->pdev->dev,
5637 				 "Failed to reconfig rss for num_queues\n");
5638 			return ret;
5639 		}
5640 		vsi->reconfig_rss = false;
5641 	}
5642 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5643 		ctxt.info.valid_sections |=
5644 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5645 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5646 	}
5647 
5648 	/* Update the VSI after updating the VSI queue-mapping
5649 	 * information
5650 	 */
5651 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5652 	if (ret) {
5653 		dev_info(&pf->pdev->dev,
5654 			 "Update vsi tc config failed, err %s aq_err %s\n",
5655 			 i40e_stat_str(hw, ret),
5656 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5657 		goto out;
5658 	}
5659 	/* update the local VSI info with updated queue map */
5660 	i40e_vsi_update_queue_map(vsi, &ctxt);
5661 	vsi->info.valid_sections = 0;
5662 
5663 	/* Update current VSI BW information */
5664 	ret = i40e_vsi_get_bw_info(vsi);
5665 	if (ret) {
5666 		dev_info(&pf->pdev->dev,
5667 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5668 			 i40e_stat_str(hw, ret),
5669 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5670 		goto out;
5671 	}
5672 
5673 	/* Update the netdev TC setup */
5674 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5675 out:
5676 	return ret;
5677 }
5678 
5679 /**
5680  * i40e_get_link_speed - Returns link speed for the interface
5681  * @vsi: VSI to be configured
5682  *
5683  **/
5684 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5685 {
5686 	struct i40e_pf *pf = vsi->back;
5687 
5688 	switch (pf->hw.phy.link_info.link_speed) {
5689 	case I40E_LINK_SPEED_40GB:
5690 		return 40000;
5691 	case I40E_LINK_SPEED_25GB:
5692 		return 25000;
5693 	case I40E_LINK_SPEED_20GB:
5694 		return 20000;
5695 	case I40E_LINK_SPEED_10GB:
5696 		return 10000;
5697 	case I40E_LINK_SPEED_1GB:
5698 		return 1000;
5699 	default:
5700 		return -EINVAL;
5701 	}
5702 }
5703 
5704 /**
5705  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5706  * @vsi: VSI to be configured
5707  * @seid: seid of the channel/VSI
5708  * @max_tx_rate: max TX rate to be configured as BW limit
5709  *
5710  * Helper function to set BW limit for a given VSI
5711  **/
5712 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5713 {
5714 	struct i40e_pf *pf = vsi->back;
5715 	u64 credits = 0;
5716 	int speed = 0;
5717 	int ret = 0;
5718 
5719 	speed = i40e_get_link_speed(vsi);
5720 	if (max_tx_rate > speed) {
5721 		dev_err(&pf->pdev->dev,
5722 			"Invalid max tx rate %llu specified for VSI seid %d.",
5723 			max_tx_rate, seid);
5724 		return -EINVAL;
5725 	}
5726 	if (max_tx_rate && max_tx_rate < 50) {
5727 		dev_warn(&pf->pdev->dev,
5728 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5729 		max_tx_rate = 50;
5730 	}
5731 
5732 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5733 	credits = max_tx_rate;
5734 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5735 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5736 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5737 	if (ret)
5738 		dev_err(&pf->pdev->dev,
5739 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5740 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5741 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5742 	return ret;
5743 }
5744 
5745 /**
5746  * i40e_remove_queue_channels - Remove queue channels for the TCs
5747  * @vsi: VSI to be configured
5748  *
5749  * Remove queue channels for the TCs
5750  **/
5751 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5752 {
5753 	enum i40e_admin_queue_err last_aq_status;
5754 	struct i40e_cloud_filter *cfilter;
5755 	struct i40e_channel *ch, *ch_tmp;
5756 	struct i40e_pf *pf = vsi->back;
5757 	struct hlist_node *node;
5758 	int ret, i;
5759 
5760 	/* Reset rss size that was stored when reconfiguring rss for
5761 	 * channel VSIs with non-power-of-2 queue count.
5762 	 */
5763 	vsi->current_rss_size = 0;
5764 
5765 	/* perform cleanup for channels if they exist */
5766 	if (list_empty(&vsi->ch_list))
5767 		return;
5768 
5769 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5770 		struct i40e_vsi *p_vsi;
5771 
5772 		list_del(&ch->list);
5773 		p_vsi = ch->parent_vsi;
5774 		if (!p_vsi || !ch->initialized) {
5775 			kfree(ch);
5776 			continue;
5777 		}
5778 		/* Reset queue contexts */
5779 		for (i = 0; i < ch->num_queue_pairs; i++) {
5780 			struct i40e_ring *tx_ring, *rx_ring;
5781 			u16 pf_q;
5782 
5783 			pf_q = ch->base_queue + i;
5784 			tx_ring = vsi->tx_rings[pf_q];
5785 			tx_ring->ch = NULL;
5786 
5787 			rx_ring = vsi->rx_rings[pf_q];
5788 			rx_ring->ch = NULL;
5789 		}
5790 
5791 		/* Reset BW configured for this VSI via mqprio */
5792 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5793 		if (ret)
5794 			dev_info(&vsi->back->pdev->dev,
5795 				 "Failed to reset tx rate for ch->seid %u\n",
5796 				 ch->seid);
5797 
5798 		/* delete cloud filters associated with this channel */
5799 		hlist_for_each_entry_safe(cfilter, node,
5800 					  &pf->cloud_filter_list, cloud_node) {
5801 			if (cfilter->seid != ch->seid)
5802 				continue;
5803 
5804 			hash_del(&cfilter->cloud_node);
5805 			if (cfilter->dst_port)
5806 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
5807 									cfilter,
5808 									false);
5809 			else
5810 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
5811 								false);
5812 			last_aq_status = pf->hw.aq.asq_last_status;
5813 			if (ret)
5814 				dev_info(&pf->pdev->dev,
5815 					 "Failed to delete cloud filter, err %s aq_err %s\n",
5816 					 i40e_stat_str(&pf->hw, ret),
5817 					 i40e_aq_str(&pf->hw, last_aq_status));
5818 			kfree(cfilter);
5819 		}
5820 
5821 		/* delete VSI from FW */
5822 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5823 					     NULL);
5824 		if (ret)
5825 			dev_err(&vsi->back->pdev->dev,
5826 				"unable to remove channel (%d) for parent VSI(%d)\n",
5827 				ch->seid, p_vsi->seid);
5828 		kfree(ch);
5829 	}
5830 	INIT_LIST_HEAD(&vsi->ch_list);
5831 }
5832 
5833 /**
5834  * i40e_get_max_queues_for_channel
5835  * @vsi: ptr to VSI to which channels are associated with
5836  *
5837  * Helper function which returns max value among the queue counts set on the
5838  * channels/TCs created.
5839  **/
5840 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5841 {
5842 	struct i40e_channel *ch, *ch_tmp;
5843 	int max = 0;
5844 
5845 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5846 		if (!ch->initialized)
5847 			continue;
5848 		if (ch->num_queue_pairs > max)
5849 			max = ch->num_queue_pairs;
5850 	}
5851 
5852 	return max;
5853 }
5854 
5855 /**
5856  * i40e_validate_num_queues - validate num_queues w.r.t channel
5857  * @pf: ptr to PF device
5858  * @num_queues: number of queues
5859  * @vsi: the parent VSI
5860  * @reconfig_rss: indicates should the RSS be reconfigured or not
5861  *
5862  * This function validates number of queues in the context of new channel
5863  * which is being established and determines if RSS should be reconfigured
5864  * or not for parent VSI.
5865  **/
5866 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5867 				    struct i40e_vsi *vsi, bool *reconfig_rss)
5868 {
5869 	int max_ch_queues;
5870 
5871 	if (!reconfig_rss)
5872 		return -EINVAL;
5873 
5874 	*reconfig_rss = false;
5875 	if (vsi->current_rss_size) {
5876 		if (num_queues > vsi->current_rss_size) {
5877 			dev_dbg(&pf->pdev->dev,
5878 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
5879 				num_queues, vsi->current_rss_size);
5880 			return -EINVAL;
5881 		} else if ((num_queues < vsi->current_rss_size) &&
5882 			   (!is_power_of_2(num_queues))) {
5883 			dev_dbg(&pf->pdev->dev,
5884 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5885 				num_queues, vsi->current_rss_size);
5886 			return -EINVAL;
5887 		}
5888 	}
5889 
5890 	if (!is_power_of_2(num_queues)) {
5891 		/* Find the max num_queues configured for channel if channel
5892 		 * exist.
5893 		 * if channel exist, then enforce 'num_queues' to be more than
5894 		 * max ever queues configured for channel.
5895 		 */
5896 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5897 		if (num_queues < max_ch_queues) {
5898 			dev_dbg(&pf->pdev->dev,
5899 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
5900 				num_queues, max_ch_queues);
5901 			return -EINVAL;
5902 		}
5903 		*reconfig_rss = true;
5904 	}
5905 
5906 	return 0;
5907 }
5908 
5909 /**
5910  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5911  * @vsi: the VSI being setup
5912  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5913  *
5914  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5915  **/
5916 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5917 {
5918 	struct i40e_pf *pf = vsi->back;
5919 	u8 seed[I40E_HKEY_ARRAY_SIZE];
5920 	struct i40e_hw *hw = &pf->hw;
5921 	int local_rss_size;
5922 	u8 *lut;
5923 	int ret;
5924 
5925 	if (!vsi->rss_size)
5926 		return -EINVAL;
5927 
5928 	if (rss_size > vsi->rss_size)
5929 		return -EINVAL;
5930 
5931 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
5932 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5933 	if (!lut)
5934 		return -ENOMEM;
5935 
5936 	/* Ignoring user configured lut if there is one */
5937 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5938 
5939 	/* Use user configured hash key if there is one, otherwise
5940 	 * use default.
5941 	 */
5942 	if (vsi->rss_hkey_user)
5943 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5944 	else
5945 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5946 
5947 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5948 	if (ret) {
5949 		dev_info(&pf->pdev->dev,
5950 			 "Cannot set RSS lut, err %s aq_err %s\n",
5951 			 i40e_stat_str(hw, ret),
5952 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5953 		kfree(lut);
5954 		return ret;
5955 	}
5956 	kfree(lut);
5957 
5958 	/* Do the update w.r.t. storing rss_size */
5959 	if (!vsi->orig_rss_size)
5960 		vsi->orig_rss_size = vsi->rss_size;
5961 	vsi->current_rss_size = local_rss_size;
5962 
5963 	return ret;
5964 }
5965 
5966 /**
5967  * i40e_channel_setup_queue_map - Setup a channel queue map
5968  * @pf: ptr to PF device
5969  * @ctxt: VSI context structure
5970  * @ch: ptr to channel structure
5971  *
5972  * Setup queue map for a specific channel
5973  **/
5974 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5975 					 struct i40e_vsi_context *ctxt,
5976 					 struct i40e_channel *ch)
5977 {
5978 	u16 qcount, qmap, sections = 0;
5979 	u8 offset = 0;
5980 	int pow;
5981 
5982 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5983 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5984 
5985 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5986 	ch->num_queue_pairs = qcount;
5987 
5988 	/* find the next higher power-of-2 of num queue pairs */
5989 	pow = ilog2(qcount);
5990 	if (!is_power_of_2(qcount))
5991 		pow++;
5992 
5993 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5994 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
5995 
5996 	/* Setup queue TC[0].qmap for given VSI context */
5997 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
5998 
5999 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6000 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6001 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6002 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6003 }
6004 
6005 /**
6006  * i40e_add_channel - add a channel by adding VSI
6007  * @pf: ptr to PF device
6008  * @uplink_seid: underlying HW switching element (VEB) ID
6009  * @ch: ptr to channel structure
6010  *
6011  * Add a channel (VSI) using add_vsi and queue_map
6012  **/
6013 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6014 			    struct i40e_channel *ch)
6015 {
6016 	struct i40e_hw *hw = &pf->hw;
6017 	struct i40e_vsi_context ctxt;
6018 	u8 enabled_tc = 0x1; /* TC0 enabled */
6019 	int ret;
6020 
6021 	if (ch->type != I40E_VSI_VMDQ2) {
6022 		dev_info(&pf->pdev->dev,
6023 			 "add new vsi failed, ch->type %d\n", ch->type);
6024 		return -EINVAL;
6025 	}
6026 
6027 	memset(&ctxt, 0, sizeof(ctxt));
6028 	ctxt.pf_num = hw->pf_id;
6029 	ctxt.vf_num = 0;
6030 	ctxt.uplink_seid = uplink_seid;
6031 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6032 	if (ch->type == I40E_VSI_VMDQ2)
6033 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6034 
6035 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6036 		ctxt.info.valid_sections |=
6037 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6038 		ctxt.info.switch_id =
6039 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6040 	}
6041 
6042 	/* Set queue map for a given VSI context */
6043 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6044 
6045 	/* Now time to create VSI */
6046 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6047 	if (ret) {
6048 		dev_info(&pf->pdev->dev,
6049 			 "add new vsi failed, err %s aq_err %s\n",
6050 			 i40e_stat_str(&pf->hw, ret),
6051 			 i40e_aq_str(&pf->hw,
6052 				     pf->hw.aq.asq_last_status));
6053 		return -ENOENT;
6054 	}
6055 
6056 	/* Success, update channel, set enabled_tc only if the channel
6057 	 * is not a macvlan
6058 	 */
6059 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6060 	ch->seid = ctxt.seid;
6061 	ch->vsi_number = ctxt.vsi_number;
6062 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6063 
6064 	/* copy just the sections touched not the entire info
6065 	 * since not all sections are valid as returned by
6066 	 * update vsi params
6067 	 */
6068 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6069 	memcpy(&ch->info.queue_mapping,
6070 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6071 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6072 	       sizeof(ctxt.info.tc_mapping));
6073 
6074 	return 0;
6075 }
6076 
6077 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6078 				  u8 *bw_share)
6079 {
6080 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6081 	i40e_status ret;
6082 	int i;
6083 
6084 	memset(&bw_data, 0, sizeof(bw_data));
6085 	bw_data.tc_valid_bits = ch->enabled_tc;
6086 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6087 		bw_data.tc_bw_credits[i] = bw_share[i];
6088 
6089 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6090 				       &bw_data, NULL);
6091 	if (ret) {
6092 		dev_info(&vsi->back->pdev->dev,
6093 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6094 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6095 		return -EINVAL;
6096 	}
6097 
6098 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6099 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6100 
6101 	return 0;
6102 }
6103 
6104 /**
6105  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6106  * @pf: ptr to PF device
6107  * @vsi: the VSI being setup
6108  * @ch: ptr to channel structure
6109  *
6110  * Configure TX rings associated with channel (VSI) since queues are being
6111  * from parent VSI.
6112  **/
6113 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6114 				       struct i40e_vsi *vsi,
6115 				       struct i40e_channel *ch)
6116 {
6117 	i40e_status ret;
6118 	int i;
6119 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6120 
6121 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6122 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6123 		if (ch->enabled_tc & BIT(i))
6124 			bw_share[i] = 1;
6125 	}
6126 
6127 	/* configure BW for new VSI */
6128 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6129 	if (ret) {
6130 		dev_info(&vsi->back->pdev->dev,
6131 			 "Failed configuring TC map %d for channel (seid %u)\n",
6132 			 ch->enabled_tc, ch->seid);
6133 		return ret;
6134 	}
6135 
6136 	for (i = 0; i < ch->num_queue_pairs; i++) {
6137 		struct i40e_ring *tx_ring, *rx_ring;
6138 		u16 pf_q;
6139 
6140 		pf_q = ch->base_queue + i;
6141 
6142 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6143 		 * context
6144 		 */
6145 		tx_ring = vsi->tx_rings[pf_q];
6146 		tx_ring->ch = ch;
6147 
6148 		/* Get the RX ring ptr */
6149 		rx_ring = vsi->rx_rings[pf_q];
6150 		rx_ring->ch = ch;
6151 	}
6152 
6153 	return 0;
6154 }
6155 
6156 /**
6157  * i40e_setup_hw_channel - setup new channel
6158  * @pf: ptr to PF device
6159  * @vsi: the VSI being setup
6160  * @ch: ptr to channel structure
6161  * @uplink_seid: underlying HW switching element (VEB) ID
6162  * @type: type of channel to be created (VMDq2/VF)
6163  *
6164  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6165  * and configures TX rings accordingly
6166  **/
6167 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6168 					struct i40e_vsi *vsi,
6169 					struct i40e_channel *ch,
6170 					u16 uplink_seid, u8 type)
6171 {
6172 	int ret;
6173 
6174 	ch->initialized = false;
6175 	ch->base_queue = vsi->next_base_queue;
6176 	ch->type = type;
6177 
6178 	/* Proceed with creation of channel (VMDq2) VSI */
6179 	ret = i40e_add_channel(pf, uplink_seid, ch);
6180 	if (ret) {
6181 		dev_info(&pf->pdev->dev,
6182 			 "failed to add_channel using uplink_seid %u\n",
6183 			 uplink_seid);
6184 		return ret;
6185 	}
6186 
6187 	/* Mark the successful creation of channel */
6188 	ch->initialized = true;
6189 
6190 	/* Reconfigure TX queues using QTX_CTL register */
6191 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6192 	if (ret) {
6193 		dev_info(&pf->pdev->dev,
6194 			 "failed to configure TX rings for channel %u\n",
6195 			 ch->seid);
6196 		return ret;
6197 	}
6198 
6199 	/* update 'next_base_queue' */
6200 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6201 	dev_dbg(&pf->pdev->dev,
6202 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6203 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6204 		ch->num_queue_pairs,
6205 		vsi->next_base_queue);
6206 	return ret;
6207 }
6208 
6209 /**
6210  * i40e_setup_channel - setup new channel using uplink element
6211  * @pf: ptr to PF device
6212  * @vsi: pointer to the VSI to set up the channel within
6213  * @ch: ptr to channel structure
6214  *
6215  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6216  * and uplink switching element (uplink_seid)
6217  **/
6218 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6219 			       struct i40e_channel *ch)
6220 {
6221 	u8 vsi_type;
6222 	u16 seid;
6223 	int ret;
6224 
6225 	if (vsi->type == I40E_VSI_MAIN) {
6226 		vsi_type = I40E_VSI_VMDQ2;
6227 	} else {
6228 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6229 			vsi->type);
6230 		return false;
6231 	}
6232 
6233 	/* underlying switching element */
6234 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6235 
6236 	/* create channel (VSI), configure TX rings */
6237 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6238 	if (ret) {
6239 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6240 		return false;
6241 	}
6242 
6243 	return ch->initialized ? true : false;
6244 }
6245 
6246 /**
6247  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6248  * @vsi: ptr to VSI which has PF backing
6249  *
6250  * Sets up switch mode correctly if it needs to be changed and perform
6251  * what are allowed modes.
6252  **/
6253 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6254 {
6255 	u8 mode;
6256 	struct i40e_pf *pf = vsi->back;
6257 	struct i40e_hw *hw = &pf->hw;
6258 	int ret;
6259 
6260 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6261 	if (ret)
6262 		return -EINVAL;
6263 
6264 	if (hw->dev_caps.switch_mode) {
6265 		/* if switch mode is set, support mode2 (non-tunneled for
6266 		 * cloud filter) for now
6267 		 */
6268 		u32 switch_mode = hw->dev_caps.switch_mode &
6269 				  I40E_SWITCH_MODE_MASK;
6270 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6271 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6272 				return 0;
6273 			dev_err(&pf->pdev->dev,
6274 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6275 				hw->dev_caps.switch_mode);
6276 			return -EINVAL;
6277 		}
6278 	}
6279 
6280 	/* Set Bit 7 to be valid */
6281 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6282 
6283 	/* Set L4type for TCP support */
6284 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6285 
6286 	/* Set cloud filter mode */
6287 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6288 
6289 	/* Prep mode field for set_switch_config */
6290 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6291 					pf->last_sw_conf_valid_flags,
6292 					mode, NULL);
6293 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6294 		dev_err(&pf->pdev->dev,
6295 			"couldn't set switch config bits, err %s aq_err %s\n",
6296 			i40e_stat_str(hw, ret),
6297 			i40e_aq_str(hw,
6298 				    hw->aq.asq_last_status));
6299 
6300 	return ret;
6301 }
6302 
6303 /**
6304  * i40e_create_queue_channel - function to create channel
6305  * @vsi: VSI to be configured
6306  * @ch: ptr to channel (it contains channel specific params)
6307  *
6308  * This function creates channel (VSI) using num_queues specified by user,
6309  * reconfigs RSS if needed.
6310  **/
6311 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6312 			      struct i40e_channel *ch)
6313 {
6314 	struct i40e_pf *pf = vsi->back;
6315 	bool reconfig_rss;
6316 	int err;
6317 
6318 	if (!ch)
6319 		return -EINVAL;
6320 
6321 	if (!ch->num_queue_pairs) {
6322 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6323 			ch->num_queue_pairs);
6324 		return -EINVAL;
6325 	}
6326 
6327 	/* validate user requested num_queues for channel */
6328 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6329 				       &reconfig_rss);
6330 	if (err) {
6331 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6332 			 ch->num_queue_pairs);
6333 		return -EINVAL;
6334 	}
6335 
6336 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6337 	 * VSI to be added switch to VEB mode.
6338 	 */
6339 
6340 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6341 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6342 
6343 		if (vsi->type == I40E_VSI_MAIN) {
6344 			if (pf->flags & I40E_FLAG_TC_MQPRIO)
6345 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6346 			else
6347 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6348 		}
6349 		/* now onwards for main VSI, number of queues will be value
6350 		 * of TC0's queue count
6351 		 */
6352 	}
6353 
6354 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6355 	 * it should be more than num_queues
6356 	 */
6357 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6358 		dev_dbg(&pf->pdev->dev,
6359 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6360 			vsi->cnt_q_avail, ch->num_queue_pairs);
6361 		return -EINVAL;
6362 	}
6363 
6364 	/* reconfig_rss only if vsi type is MAIN_VSI */
6365 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6366 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6367 		if (err) {
6368 			dev_info(&pf->pdev->dev,
6369 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6370 				 ch->num_queue_pairs);
6371 			return -EINVAL;
6372 		}
6373 	}
6374 
6375 	if (!i40e_setup_channel(pf, vsi, ch)) {
6376 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6377 		return -EINVAL;
6378 	}
6379 
6380 	dev_info(&pf->pdev->dev,
6381 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6382 		 ch->seid, ch->num_queue_pairs);
6383 
6384 	/* configure VSI for BW limit */
6385 	if (ch->max_tx_rate) {
6386 		u64 credits = ch->max_tx_rate;
6387 
6388 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6389 			return -EINVAL;
6390 
6391 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6392 		dev_dbg(&pf->pdev->dev,
6393 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6394 			ch->max_tx_rate,
6395 			credits,
6396 			ch->seid);
6397 	}
6398 
6399 	/* in case of VF, this will be main SRIOV VSI */
6400 	ch->parent_vsi = vsi;
6401 
6402 	/* and update main_vsi's count for queue_available to use */
6403 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6404 
6405 	return 0;
6406 }
6407 
6408 /**
6409  * i40e_configure_queue_channels - Add queue channel for the given TCs
6410  * @vsi: VSI to be configured
6411  *
6412  * Configures queue channel mapping to the given TCs
6413  **/
6414 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6415 {
6416 	struct i40e_channel *ch;
6417 	u64 max_rate = 0;
6418 	int ret = 0, i;
6419 
6420 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6421 	vsi->tc_seid_map[0] = vsi->seid;
6422 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6423 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6424 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6425 			if (!ch) {
6426 				ret = -ENOMEM;
6427 				goto err_free;
6428 			}
6429 
6430 			INIT_LIST_HEAD(&ch->list);
6431 			ch->num_queue_pairs =
6432 				vsi->tc_config.tc_info[i].qcount;
6433 			ch->base_queue =
6434 				vsi->tc_config.tc_info[i].qoffset;
6435 
6436 			/* Bandwidth limit through tc interface is in bytes/s,
6437 			 * change to Mbit/s
6438 			 */
6439 			max_rate = vsi->mqprio_qopt.max_rate[i];
6440 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6441 			ch->max_tx_rate = max_rate;
6442 
6443 			list_add_tail(&ch->list, &vsi->ch_list);
6444 
6445 			ret = i40e_create_queue_channel(vsi, ch);
6446 			if (ret) {
6447 				dev_err(&vsi->back->pdev->dev,
6448 					"Failed creating queue channel with TC%d: queues %d\n",
6449 					i, ch->num_queue_pairs);
6450 				goto err_free;
6451 			}
6452 			vsi->tc_seid_map[i] = ch->seid;
6453 		}
6454 	}
6455 	return ret;
6456 
6457 err_free:
6458 	i40e_remove_queue_channels(vsi);
6459 	return ret;
6460 }
6461 
6462 /**
6463  * i40e_veb_config_tc - Configure TCs for given VEB
6464  * @veb: given VEB
6465  * @enabled_tc: TC bitmap
6466  *
6467  * Configures given TC bitmap for VEB (switching) element
6468  **/
6469 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6470 {
6471 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6472 	struct i40e_pf *pf = veb->pf;
6473 	int ret = 0;
6474 	int i;
6475 
6476 	/* No TCs or already enabled TCs just return */
6477 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6478 		return ret;
6479 
6480 	bw_data.tc_valid_bits = enabled_tc;
6481 	/* bw_data.absolute_credits is not set (relative) */
6482 
6483 	/* Enable ETS TCs with equal BW Share for now */
6484 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6485 		if (enabled_tc & BIT(i))
6486 			bw_data.tc_bw_share_credits[i] = 1;
6487 	}
6488 
6489 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6490 						   &bw_data, NULL);
6491 	if (ret) {
6492 		dev_info(&pf->pdev->dev,
6493 			 "VEB bw config failed, err %s aq_err %s\n",
6494 			 i40e_stat_str(&pf->hw, ret),
6495 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6496 		goto out;
6497 	}
6498 
6499 	/* Update the BW information */
6500 	ret = i40e_veb_get_bw_info(veb);
6501 	if (ret) {
6502 		dev_info(&pf->pdev->dev,
6503 			 "Failed getting veb bw config, err %s aq_err %s\n",
6504 			 i40e_stat_str(&pf->hw, ret),
6505 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6506 	}
6507 
6508 out:
6509 	return ret;
6510 }
6511 
6512 #ifdef CONFIG_I40E_DCB
6513 /**
6514  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6515  * @pf: PF struct
6516  *
6517  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6518  * the caller would've quiesce all the VSIs before calling
6519  * this function
6520  **/
6521 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6522 {
6523 	u8 tc_map = 0;
6524 	int ret;
6525 	u8 v;
6526 
6527 	/* Enable the TCs available on PF to all VEBs */
6528 	tc_map = i40e_pf_get_tc_map(pf);
6529 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6530 		return;
6531 
6532 	for (v = 0; v < I40E_MAX_VEB; v++) {
6533 		if (!pf->veb[v])
6534 			continue;
6535 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6536 		if (ret) {
6537 			dev_info(&pf->pdev->dev,
6538 				 "Failed configuring TC for VEB seid=%d\n",
6539 				 pf->veb[v]->seid);
6540 			/* Will try to configure as many components */
6541 		}
6542 	}
6543 
6544 	/* Update each VSI */
6545 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6546 		if (!pf->vsi[v])
6547 			continue;
6548 
6549 		/* - Enable all TCs for the LAN VSI
6550 		 * - For all others keep them at TC0 for now
6551 		 */
6552 		if (v == pf->lan_vsi)
6553 			tc_map = i40e_pf_get_tc_map(pf);
6554 		else
6555 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6556 
6557 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6558 		if (ret) {
6559 			dev_info(&pf->pdev->dev,
6560 				 "Failed configuring TC for VSI seid=%d\n",
6561 				 pf->vsi[v]->seid);
6562 			/* Will try to configure as many components */
6563 		} else {
6564 			/* Re-configure VSI vectors based on updated TC map */
6565 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6566 			if (pf->vsi[v]->netdev)
6567 				i40e_dcbnl_set_all(pf->vsi[v]);
6568 		}
6569 	}
6570 }
6571 
6572 /**
6573  * i40e_resume_port_tx - Resume port Tx
6574  * @pf: PF struct
6575  *
6576  * Resume a port's Tx and issue a PF reset in case of failure to
6577  * resume.
6578  **/
6579 static int i40e_resume_port_tx(struct i40e_pf *pf)
6580 {
6581 	struct i40e_hw *hw = &pf->hw;
6582 	int ret;
6583 
6584 	ret = i40e_aq_resume_port_tx(hw, NULL);
6585 	if (ret) {
6586 		dev_info(&pf->pdev->dev,
6587 			 "Resume Port Tx failed, err %s aq_err %s\n",
6588 			  i40e_stat_str(&pf->hw, ret),
6589 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6590 		/* Schedule PF reset to recover */
6591 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6592 		i40e_service_event_schedule(pf);
6593 	}
6594 
6595 	return ret;
6596 }
6597 
6598 /**
6599  * i40e_suspend_port_tx - Suspend port Tx
6600  * @pf: PF struct
6601  *
6602  * Suspend a port's Tx and issue a PF reset in case of failure.
6603  **/
6604 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6605 {
6606 	struct i40e_hw *hw = &pf->hw;
6607 	int ret;
6608 
6609 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6610 	if (ret) {
6611 		dev_info(&pf->pdev->dev,
6612 			 "Suspend Port Tx failed, err %s aq_err %s\n",
6613 			 i40e_stat_str(&pf->hw, ret),
6614 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6615 		/* Schedule PF reset to recover */
6616 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6617 		i40e_service_event_schedule(pf);
6618 	}
6619 
6620 	return ret;
6621 }
6622 
6623 /**
6624  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6625  * @pf: PF being configured
6626  * @new_cfg: New DCBX configuration
6627  *
6628  * Program DCB settings into HW and reconfigure VEB/VSIs on
6629  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6630  **/
6631 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6632 				  struct i40e_dcbx_config *new_cfg)
6633 {
6634 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6635 	int ret;
6636 
6637 	/* Check if need reconfiguration */
6638 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6639 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6640 		return 0;
6641 	}
6642 
6643 	/* Config change disable all VSIs */
6644 	i40e_pf_quiesce_all_vsi(pf);
6645 
6646 	/* Copy the new config to the current config */
6647 	*old_cfg = *new_cfg;
6648 	old_cfg->etsrec = old_cfg->etscfg;
6649 	ret = i40e_set_dcb_config(&pf->hw);
6650 	if (ret) {
6651 		dev_info(&pf->pdev->dev,
6652 			 "Set DCB Config failed, err %s aq_err %s\n",
6653 			 i40e_stat_str(&pf->hw, ret),
6654 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6655 		goto out;
6656 	}
6657 
6658 	/* Changes in configuration update VEB/VSI */
6659 	i40e_dcb_reconfigure(pf);
6660 out:
6661 	/* In case of reset do not try to resume anything */
6662 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6663 		/* Re-start the VSIs if disabled */
6664 		ret = i40e_resume_port_tx(pf);
6665 		/* In case of error no point in resuming VSIs */
6666 		if (ret)
6667 			goto err;
6668 		i40e_pf_unquiesce_all_vsi(pf);
6669 	}
6670 err:
6671 	return ret;
6672 }
6673 
6674 /**
6675  * i40e_hw_dcb_config - Program new DCBX settings into HW
6676  * @pf: PF being configured
6677  * @new_cfg: New DCBX configuration
6678  *
6679  * Program DCB settings into HW and reconfigure VEB/VSIs on
6680  * given PF
6681  **/
6682 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6683 {
6684 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6685 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6686 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6687 	struct i40e_dcbx_config *old_cfg;
6688 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6689 	struct i40e_rx_pb_config pb_cfg;
6690 	struct i40e_hw *hw = &pf->hw;
6691 	u8 num_ports = hw->num_ports;
6692 	bool need_reconfig;
6693 	int ret = -EINVAL;
6694 	u8 lltc_map = 0;
6695 	u8 tc_map = 0;
6696 	u8 new_numtc;
6697 	u8 i;
6698 
6699 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6700 	/* Un-pack information to Program ETS HW via shared API
6701 	 * numtc, tcmap
6702 	 * LLTC map
6703 	 * ETS/NON-ETS arbiter mode
6704 	 * max exponent (credit refills)
6705 	 * Total number of ports
6706 	 * PFC priority bit-map
6707 	 * Priority Table
6708 	 * BW % per TC
6709 	 * Arbiter mode between UPs sharing same TC
6710 	 * TSA table (ETS or non-ETS)
6711 	 * EEE enabled or not
6712 	 * MFS TC table
6713 	 */
6714 
6715 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6716 
6717 	memset(&ets_data, 0, sizeof(ets_data));
6718 	for (i = 0; i < new_numtc; i++) {
6719 		tc_map |= BIT(i);
6720 		switch (new_cfg->etscfg.tsatable[i]) {
6721 		case I40E_IEEE_TSA_ETS:
6722 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6723 			ets_data.tc_bw_share_credits[i] =
6724 					new_cfg->etscfg.tcbwtable[i];
6725 			break;
6726 		case I40E_IEEE_TSA_STRICT:
6727 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6728 			lltc_map |= BIT(i);
6729 			ets_data.tc_bw_share_credits[i] =
6730 					I40E_DCB_STRICT_PRIO_CREDITS;
6731 			break;
6732 		default:
6733 			/* Invalid TSA type */
6734 			need_reconfig = false;
6735 			goto out;
6736 		}
6737 	}
6738 
6739 	old_cfg = &hw->local_dcbx_config;
6740 	/* Check if need reconfiguration */
6741 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6742 
6743 	/* If needed, enable/disable frame tagging, disable all VSIs
6744 	 * and suspend port tx
6745 	 */
6746 	if (need_reconfig) {
6747 		/* Enable DCB tagging only when more than one TC */
6748 		if (new_numtc > 1)
6749 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6750 		else
6751 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6752 
6753 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6754 		/* Reconfiguration needed quiesce all VSIs */
6755 		i40e_pf_quiesce_all_vsi(pf);
6756 		ret = i40e_suspend_port_tx(pf);
6757 		if (ret)
6758 			goto err;
6759 	}
6760 
6761 	/* Configure Port ETS Tx Scheduler */
6762 	ets_data.tc_valid_bits = tc_map;
6763 	ets_data.tc_strict_priority_flags = lltc_map;
6764 	ret = i40e_aq_config_switch_comp_ets
6765 		(hw, pf->mac_seid, &ets_data,
6766 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
6767 	if (ret) {
6768 		dev_info(&pf->pdev->dev,
6769 			 "Modify Port ETS failed, err %s aq_err %s\n",
6770 			 i40e_stat_str(&pf->hw, ret),
6771 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6772 		goto out;
6773 	}
6774 
6775 	/* Configure Rx ETS HW */
6776 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
6777 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
6778 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
6779 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
6780 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
6781 				   lltc_map);
6782 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
6783 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
6784 				     prio_type);
6785 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
6786 			       new_cfg->etscfg.prioritytable);
6787 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
6788 
6789 	/* Configure Rx Packet Buffers in HW */
6790 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6791 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
6792 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
6793 	}
6794 
6795 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
6796 					 false, new_cfg->pfc.pfcenable,
6797 					 mfs_tc, &pb_cfg);
6798 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
6799 
6800 	/* Update the local Rx Packet buffer config */
6801 	pf->pb_cfg = pb_cfg;
6802 
6803 	/* Inform the FW about changes to DCB configuration */
6804 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
6805 	if (ret) {
6806 		dev_info(&pf->pdev->dev,
6807 			 "DCB Updated failed, err %s aq_err %s\n",
6808 			 i40e_stat_str(&pf->hw, ret),
6809 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6810 		goto out;
6811 	}
6812 
6813 	/* Update the port DCBx configuration */
6814 	*old_cfg = *new_cfg;
6815 
6816 	/* Changes in configuration update VEB/VSI */
6817 	i40e_dcb_reconfigure(pf);
6818 out:
6819 	/* Re-start the VSIs if disabled */
6820 	if (need_reconfig) {
6821 		ret = i40e_resume_port_tx(pf);
6822 
6823 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
6824 		/* In case of error no point in resuming VSIs */
6825 		if (ret)
6826 			goto err;
6827 
6828 		/* Wait for the PF's queues to be disabled */
6829 		ret = i40e_pf_wait_queues_disabled(pf);
6830 		if (ret) {
6831 			/* Schedule PF reset to recover */
6832 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6833 			i40e_service_event_schedule(pf);
6834 			goto err;
6835 		} else {
6836 			i40e_pf_unquiesce_all_vsi(pf);
6837 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
6838 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
6839 		}
6840 		/* registers are set, lets apply */
6841 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
6842 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
6843 	}
6844 
6845 err:
6846 	return ret;
6847 }
6848 
6849 /**
6850  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
6851  * @pf: PF being queried
6852  *
6853  * Set default DCB configuration in case DCB is to be done in SW.
6854  **/
6855 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
6856 {
6857 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
6858 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6859 	struct i40e_hw *hw = &pf->hw;
6860 	int err;
6861 
6862 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
6863 		/* Update the local cached instance with TC0 ETS */
6864 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
6865 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
6866 		pf->tmp_cfg.etscfg.maxtcs = 0;
6867 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
6868 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
6869 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
6870 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
6871 		/* FW needs one App to configure HW */
6872 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
6873 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
6874 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
6875 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
6876 
6877 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
6878 	}
6879 
6880 	memset(&ets_data, 0, sizeof(ets_data));
6881 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
6882 	ets_data.tc_strict_priority_flags = 0; /* ETS */
6883 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
6884 
6885 	/* Enable ETS on the Physical port */
6886 	err = i40e_aq_config_switch_comp_ets
6887 		(hw, pf->mac_seid, &ets_data,
6888 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
6889 	if (err) {
6890 		dev_info(&pf->pdev->dev,
6891 			 "Enable Port ETS failed, err %s aq_err %s\n",
6892 			 i40e_stat_str(&pf->hw, err),
6893 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6894 		err = -ENOENT;
6895 		goto out;
6896 	}
6897 
6898 	/* Update the local cached instance with TC0 ETS */
6899 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
6900 	dcb_cfg->etscfg.cbs = 0;
6901 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
6902 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
6903 
6904 out:
6905 	return err;
6906 }
6907 
6908 /**
6909  * i40e_init_pf_dcb - Initialize DCB configuration
6910  * @pf: PF being configured
6911  *
6912  * Query the current DCB configuration and cache it
6913  * in the hardware structure
6914  **/
6915 static int i40e_init_pf_dcb(struct i40e_pf *pf)
6916 {
6917 	struct i40e_hw *hw = &pf->hw;
6918 	int err;
6919 
6920 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
6921 	 * Also do not enable DCBx if FW LLDP agent is disabled
6922 	 */
6923 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
6924 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
6925 		err = I40E_NOT_SUPPORTED;
6926 		goto out;
6927 	}
6928 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
6929 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
6930 		err = i40e_dcb_sw_default_config(pf);
6931 		if (err) {
6932 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
6933 			goto out;
6934 		}
6935 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
6936 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
6937 			       DCB_CAP_DCBX_VER_IEEE;
6938 		/* at init capable but disabled */
6939 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
6940 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6941 		goto out;
6942 	}
6943 	err = i40e_init_dcb(hw, true);
6944 	if (!err) {
6945 		/* Device/Function is not DCBX capable */
6946 		if ((!hw->func_caps.dcb) ||
6947 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6948 			dev_info(&pf->pdev->dev,
6949 				 "DCBX offload is not supported or is disabled for this PF.\n");
6950 		} else {
6951 			/* When status is not DISABLED then DCBX in FW */
6952 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6953 				       DCB_CAP_DCBX_VER_IEEE;
6954 
6955 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
6956 			/* Enable DCB tagging only when more than one TC
6957 			 * or explicitly disable if only one TC
6958 			 */
6959 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6960 				pf->flags |= I40E_FLAG_DCB_ENABLED;
6961 			else
6962 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6963 			dev_dbg(&pf->pdev->dev,
6964 				"DCBX offload is supported for this PF.\n");
6965 		}
6966 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
6967 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
6968 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
6969 	} else {
6970 		dev_info(&pf->pdev->dev,
6971 			 "Query for DCB configuration failed, err %s aq_err %s\n",
6972 			 i40e_stat_str(&pf->hw, err),
6973 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6974 	}
6975 
6976 out:
6977 	return err;
6978 }
6979 #endif /* CONFIG_I40E_DCB */
6980 
6981 /**
6982  * i40e_print_link_message - print link up or down
6983  * @vsi: the VSI for which link needs a message
6984  * @isup: true of link is up, false otherwise
6985  */
6986 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
6987 {
6988 	enum i40e_aq_link_speed new_speed;
6989 	struct i40e_pf *pf = vsi->back;
6990 	char *speed = "Unknown";
6991 	char *fc = "Unknown";
6992 	char *fec = "";
6993 	char *req_fec = "";
6994 	char *an = "";
6995 
6996 	if (isup)
6997 		new_speed = pf->hw.phy.link_info.link_speed;
6998 	else
6999 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7000 
7001 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7002 		return;
7003 	vsi->current_isup = isup;
7004 	vsi->current_speed = new_speed;
7005 	if (!isup) {
7006 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7007 		return;
7008 	}
7009 
7010 	/* Warn user if link speed on NPAR enabled partition is not at
7011 	 * least 10GB
7012 	 */
7013 	if (pf->hw.func_caps.npar_enable &&
7014 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7015 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7016 		netdev_warn(vsi->netdev,
7017 			    "The partition detected link speed that is less than 10Gbps\n");
7018 
7019 	switch (pf->hw.phy.link_info.link_speed) {
7020 	case I40E_LINK_SPEED_40GB:
7021 		speed = "40 G";
7022 		break;
7023 	case I40E_LINK_SPEED_20GB:
7024 		speed = "20 G";
7025 		break;
7026 	case I40E_LINK_SPEED_25GB:
7027 		speed = "25 G";
7028 		break;
7029 	case I40E_LINK_SPEED_10GB:
7030 		speed = "10 G";
7031 		break;
7032 	case I40E_LINK_SPEED_5GB:
7033 		speed = "5 G";
7034 		break;
7035 	case I40E_LINK_SPEED_2_5GB:
7036 		speed = "2.5 G";
7037 		break;
7038 	case I40E_LINK_SPEED_1GB:
7039 		speed = "1000 M";
7040 		break;
7041 	case I40E_LINK_SPEED_100MB:
7042 		speed = "100 M";
7043 		break;
7044 	default:
7045 		break;
7046 	}
7047 
7048 	switch (pf->hw.fc.current_mode) {
7049 	case I40E_FC_FULL:
7050 		fc = "RX/TX";
7051 		break;
7052 	case I40E_FC_TX_PAUSE:
7053 		fc = "TX";
7054 		break;
7055 	case I40E_FC_RX_PAUSE:
7056 		fc = "RX";
7057 		break;
7058 	default:
7059 		fc = "None";
7060 		break;
7061 	}
7062 
7063 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7064 		req_fec = "None";
7065 		fec = "None";
7066 		an = "False";
7067 
7068 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7069 			an = "True";
7070 
7071 		if (pf->hw.phy.link_info.fec_info &
7072 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7073 			fec = "CL74 FC-FEC/BASE-R";
7074 		else if (pf->hw.phy.link_info.fec_info &
7075 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7076 			fec = "CL108 RS-FEC";
7077 
7078 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7079 		 * both RS and FC are requested
7080 		 */
7081 		if (vsi->back->hw.phy.link_info.req_fec_info &
7082 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7083 			if (vsi->back->hw.phy.link_info.req_fec_info &
7084 			    I40E_AQ_REQUEST_FEC_RS)
7085 				req_fec = "CL108 RS-FEC";
7086 			else
7087 				req_fec = "CL74 FC-FEC/BASE-R";
7088 		}
7089 		netdev_info(vsi->netdev,
7090 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7091 			    speed, req_fec, fec, an, fc);
7092 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7093 		req_fec = "None";
7094 		fec = "None";
7095 		an = "False";
7096 
7097 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7098 			an = "True";
7099 
7100 		if (pf->hw.phy.link_info.fec_info &
7101 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7102 			fec = "CL74 FC-FEC/BASE-R";
7103 
7104 		if (pf->hw.phy.link_info.req_fec_info &
7105 		    I40E_AQ_REQUEST_FEC_KR)
7106 			req_fec = "CL74 FC-FEC/BASE-R";
7107 
7108 		netdev_info(vsi->netdev,
7109 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7110 			    speed, req_fec, fec, an, fc);
7111 	} else {
7112 		netdev_info(vsi->netdev,
7113 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7114 			    speed, fc);
7115 	}
7116 
7117 }
7118 
7119 /**
7120  * i40e_up_complete - Finish the last steps of bringing up a connection
7121  * @vsi: the VSI being configured
7122  **/
7123 static int i40e_up_complete(struct i40e_vsi *vsi)
7124 {
7125 	struct i40e_pf *pf = vsi->back;
7126 	int err;
7127 
7128 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7129 		i40e_vsi_configure_msix(vsi);
7130 	else
7131 		i40e_configure_msi_and_legacy(vsi);
7132 
7133 	/* start rings */
7134 	err = i40e_vsi_start_rings(vsi);
7135 	if (err)
7136 		return err;
7137 
7138 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7139 	i40e_napi_enable_all(vsi);
7140 	i40e_vsi_enable_irq(vsi);
7141 
7142 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7143 	    (vsi->netdev)) {
7144 		i40e_print_link_message(vsi, true);
7145 		netif_tx_start_all_queues(vsi->netdev);
7146 		netif_carrier_on(vsi->netdev);
7147 	}
7148 
7149 	/* replay FDIR SB filters */
7150 	if (vsi->type == I40E_VSI_FDIR) {
7151 		/* reset fd counters */
7152 		pf->fd_add_err = 0;
7153 		pf->fd_atr_cnt = 0;
7154 		i40e_fdir_filter_restore(vsi);
7155 	}
7156 
7157 	/* On the next run of the service_task, notify any clients of the new
7158 	 * opened netdev
7159 	 */
7160 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7161 	i40e_service_event_schedule(pf);
7162 
7163 	return 0;
7164 }
7165 
7166 /**
7167  * i40e_vsi_reinit_locked - Reset the VSI
7168  * @vsi: the VSI being configured
7169  *
7170  * Rebuild the ring structs after some configuration
7171  * has changed, e.g. MTU size.
7172  **/
7173 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7174 {
7175 	struct i40e_pf *pf = vsi->back;
7176 
7177 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7178 		usleep_range(1000, 2000);
7179 	i40e_down(vsi);
7180 
7181 	i40e_up(vsi);
7182 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7183 }
7184 
7185 /**
7186  * i40e_force_link_state - Force the link status
7187  * @pf: board private structure
7188  * @is_up: whether the link state should be forced up or down
7189  **/
7190 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7191 {
7192 	struct i40e_aq_get_phy_abilities_resp abilities;
7193 	struct i40e_aq_set_phy_config config = {0};
7194 	bool non_zero_phy_type = is_up;
7195 	struct i40e_hw *hw = &pf->hw;
7196 	i40e_status err;
7197 	u64 mask;
7198 	u8 speed;
7199 
7200 	/* Card might've been put in an unstable state by other drivers
7201 	 * and applications, which causes incorrect speed values being
7202 	 * set on startup. In order to clear speed registers, we call
7203 	 * get_phy_capabilities twice, once to get initial state of
7204 	 * available speeds, and once to get current PHY config.
7205 	 */
7206 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7207 					   NULL);
7208 	if (err) {
7209 		dev_err(&pf->pdev->dev,
7210 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7211 			i40e_stat_str(hw, err),
7212 			i40e_aq_str(hw, hw->aq.asq_last_status));
7213 		return err;
7214 	}
7215 	speed = abilities.link_speed;
7216 
7217 	/* Get the current phy config */
7218 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7219 					   NULL);
7220 	if (err) {
7221 		dev_err(&pf->pdev->dev,
7222 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7223 			i40e_stat_str(hw, err),
7224 			i40e_aq_str(hw, hw->aq.asq_last_status));
7225 		return err;
7226 	}
7227 
7228 	/* If link needs to go up, but was not forced to go down,
7229 	 * and its speed values are OK, no need for a flap
7230 	 * if non_zero_phy_type was set, still need to force up
7231 	 */
7232 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7233 		non_zero_phy_type = true;
7234 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7235 		return I40E_SUCCESS;
7236 
7237 	/* To force link we need to set bits for all supported PHY types,
7238 	 * but there are now more than 32, so we need to split the bitmap
7239 	 * across two fields.
7240 	 */
7241 	mask = I40E_PHY_TYPES_BITMASK;
7242 	config.phy_type =
7243 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7244 	config.phy_type_ext =
7245 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7246 	/* Copy the old settings, except of phy_type */
7247 	config.abilities = abilities.abilities;
7248 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7249 		if (is_up)
7250 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7251 		else
7252 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7253 	}
7254 	if (abilities.link_speed != 0)
7255 		config.link_speed = abilities.link_speed;
7256 	else
7257 		config.link_speed = speed;
7258 	config.eee_capability = abilities.eee_capability;
7259 	config.eeer = abilities.eeer_val;
7260 	config.low_power_ctrl = abilities.d3_lpan;
7261 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7262 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7263 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7264 
7265 	if (err) {
7266 		dev_err(&pf->pdev->dev,
7267 			"set phy config ret =  %s last_status =  %s\n",
7268 			i40e_stat_str(&pf->hw, err),
7269 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7270 		return err;
7271 	}
7272 
7273 	/* Update the link info */
7274 	err = i40e_update_link_info(hw);
7275 	if (err) {
7276 		/* Wait a little bit (on 40G cards it sometimes takes a really
7277 		 * long time for link to come back from the atomic reset)
7278 		 * and try once more
7279 		 */
7280 		msleep(1000);
7281 		i40e_update_link_info(hw);
7282 	}
7283 
7284 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7285 
7286 	return I40E_SUCCESS;
7287 }
7288 
7289 /**
7290  * i40e_up - Bring the connection back up after being down
7291  * @vsi: the VSI being configured
7292  **/
7293 int i40e_up(struct i40e_vsi *vsi)
7294 {
7295 	int err;
7296 
7297 	if (vsi->type == I40E_VSI_MAIN &&
7298 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7299 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7300 		i40e_force_link_state(vsi->back, true);
7301 
7302 	err = i40e_vsi_configure(vsi);
7303 	if (!err)
7304 		err = i40e_up_complete(vsi);
7305 
7306 	return err;
7307 }
7308 
7309 /**
7310  * i40e_down - Shutdown the connection processing
7311  * @vsi: the VSI being stopped
7312  **/
7313 void i40e_down(struct i40e_vsi *vsi)
7314 {
7315 	int i;
7316 
7317 	/* It is assumed that the caller of this function
7318 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7319 	 */
7320 	if (vsi->netdev) {
7321 		netif_carrier_off(vsi->netdev);
7322 		netif_tx_disable(vsi->netdev);
7323 	}
7324 	i40e_vsi_disable_irq(vsi);
7325 	i40e_vsi_stop_rings(vsi);
7326 	if (vsi->type == I40E_VSI_MAIN &&
7327 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7328 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7329 		i40e_force_link_state(vsi->back, false);
7330 	i40e_napi_disable_all(vsi);
7331 
7332 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7333 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7334 		if (i40e_enabled_xdp_vsi(vsi)) {
7335 			/* Make sure that in-progress ndo_xdp_xmit and
7336 			 * ndo_xsk_wakeup calls are completed.
7337 			 */
7338 			synchronize_rcu();
7339 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7340 		}
7341 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7342 	}
7343 
7344 }
7345 
7346 /**
7347  * i40e_validate_mqprio_qopt- validate queue mapping info
7348  * @vsi: the VSI being configured
7349  * @mqprio_qopt: queue parametrs
7350  **/
7351 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7352 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7353 {
7354 	u64 sum_max_rate = 0;
7355 	u64 max_rate = 0;
7356 	int i;
7357 
7358 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7359 	    mqprio_qopt->qopt.num_tc < 1 ||
7360 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7361 		return -EINVAL;
7362 	for (i = 0; ; i++) {
7363 		if (!mqprio_qopt->qopt.count[i])
7364 			return -EINVAL;
7365 		if (mqprio_qopt->min_rate[i]) {
7366 			dev_err(&vsi->back->pdev->dev,
7367 				"Invalid min tx rate (greater than 0) specified\n");
7368 			return -EINVAL;
7369 		}
7370 		max_rate = mqprio_qopt->max_rate[i];
7371 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7372 		sum_max_rate += max_rate;
7373 
7374 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7375 			break;
7376 		if (mqprio_qopt->qopt.offset[i + 1] !=
7377 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7378 			return -EINVAL;
7379 	}
7380 	if (vsi->num_queue_pairs <
7381 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7382 		dev_err(&vsi->back->pdev->dev,
7383 			"Failed to create traffic channel, insufficient number of queues.\n");
7384 		return -EINVAL;
7385 	}
7386 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7387 		dev_err(&vsi->back->pdev->dev,
7388 			"Invalid max tx rate specified\n");
7389 		return -EINVAL;
7390 	}
7391 	return 0;
7392 }
7393 
7394 /**
7395  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7396  * @vsi: the VSI being configured
7397  **/
7398 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7399 {
7400 	u16 qcount;
7401 	int i;
7402 
7403 	/* Only TC0 is enabled */
7404 	vsi->tc_config.numtc = 1;
7405 	vsi->tc_config.enabled_tc = 1;
7406 	qcount = min_t(int, vsi->alloc_queue_pairs,
7407 		       i40e_pf_get_max_q_per_tc(vsi->back));
7408 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7409 		/* For the TC that is not enabled set the offset to default
7410 		 * queue and allocate one queue for the given TC.
7411 		 */
7412 		vsi->tc_config.tc_info[i].qoffset = 0;
7413 		if (i == 0)
7414 			vsi->tc_config.tc_info[i].qcount = qcount;
7415 		else
7416 			vsi->tc_config.tc_info[i].qcount = 1;
7417 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7418 	}
7419 }
7420 
7421 /**
7422  * i40e_del_macvlan_filter
7423  * @hw: pointer to the HW structure
7424  * @seid: seid of the channel VSI
7425  * @macaddr: the mac address to apply as a filter
7426  * @aq_err: store the admin Q error
7427  *
7428  * This function deletes a mac filter on the channel VSI which serves as the
7429  * macvlan. Returns 0 on success.
7430  **/
7431 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7432 					   const u8 *macaddr, int *aq_err)
7433 {
7434 	struct i40e_aqc_remove_macvlan_element_data element;
7435 	i40e_status status;
7436 
7437 	memset(&element, 0, sizeof(element));
7438 	ether_addr_copy(element.mac_addr, macaddr);
7439 	element.vlan_tag = 0;
7440 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7441 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7442 	*aq_err = hw->aq.asq_last_status;
7443 
7444 	return status;
7445 }
7446 
7447 /**
7448  * i40e_add_macvlan_filter
7449  * @hw: pointer to the HW structure
7450  * @seid: seid of the channel VSI
7451  * @macaddr: the mac address to apply as a filter
7452  * @aq_err: store the admin Q error
7453  *
7454  * This function adds a mac filter on the channel VSI which serves as the
7455  * macvlan. Returns 0 on success.
7456  **/
7457 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7458 					   const u8 *macaddr, int *aq_err)
7459 {
7460 	struct i40e_aqc_add_macvlan_element_data element;
7461 	i40e_status status;
7462 	u16 cmd_flags = 0;
7463 
7464 	ether_addr_copy(element.mac_addr, macaddr);
7465 	element.vlan_tag = 0;
7466 	element.queue_number = 0;
7467 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7468 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7469 	element.flags = cpu_to_le16(cmd_flags);
7470 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7471 	*aq_err = hw->aq.asq_last_status;
7472 
7473 	return status;
7474 }
7475 
7476 /**
7477  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7478  * @vsi: the VSI we want to access
7479  * @ch: the channel we want to access
7480  */
7481 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7482 {
7483 	struct i40e_ring *tx_ring, *rx_ring;
7484 	u16 pf_q;
7485 	int i;
7486 
7487 	for (i = 0; i < ch->num_queue_pairs; i++) {
7488 		pf_q = ch->base_queue + i;
7489 		tx_ring = vsi->tx_rings[pf_q];
7490 		tx_ring->ch = NULL;
7491 		rx_ring = vsi->rx_rings[pf_q];
7492 		rx_ring->ch = NULL;
7493 	}
7494 }
7495 
7496 /**
7497  * i40e_free_macvlan_channels
7498  * @vsi: the VSI we want to access
7499  *
7500  * This function frees the Qs of the channel VSI from
7501  * the stack and also deletes the channel VSIs which
7502  * serve as macvlans.
7503  */
7504 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7505 {
7506 	struct i40e_channel *ch, *ch_tmp;
7507 	int ret;
7508 
7509 	if (list_empty(&vsi->macvlan_list))
7510 		return;
7511 
7512 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7513 		struct i40e_vsi *parent_vsi;
7514 
7515 		if (i40e_is_channel_macvlan(ch)) {
7516 			i40e_reset_ch_rings(vsi, ch);
7517 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7518 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7519 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7520 			kfree(ch->fwd);
7521 			ch->fwd = NULL;
7522 		}
7523 
7524 		list_del(&ch->list);
7525 		parent_vsi = ch->parent_vsi;
7526 		if (!parent_vsi || !ch->initialized) {
7527 			kfree(ch);
7528 			continue;
7529 		}
7530 
7531 		/* remove the VSI */
7532 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7533 					     NULL);
7534 		if (ret)
7535 			dev_err(&vsi->back->pdev->dev,
7536 				"unable to remove channel (%d) for parent VSI(%d)\n",
7537 				ch->seid, parent_vsi->seid);
7538 		kfree(ch);
7539 	}
7540 	vsi->macvlan_cnt = 0;
7541 }
7542 
7543 /**
7544  * i40e_fwd_ring_up - bring the macvlan device up
7545  * @vsi: the VSI we want to access
7546  * @vdev: macvlan netdevice
7547  * @fwd: the private fwd structure
7548  */
7549 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7550 			    struct i40e_fwd_adapter *fwd)
7551 {
7552 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7553 	int ret = 0, num_tc = 1,  i, aq_err;
7554 	struct i40e_pf *pf = vsi->back;
7555 	struct i40e_hw *hw = &pf->hw;
7556 
7557 	/* Go through the list and find an available channel */
7558 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7559 		if (!i40e_is_channel_macvlan(iter)) {
7560 			iter->fwd = fwd;
7561 			/* record configuration for macvlan interface in vdev */
7562 			for (i = 0; i < num_tc; i++)
7563 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7564 							     i,
7565 							     iter->num_queue_pairs,
7566 							     iter->base_queue);
7567 			for (i = 0; i < iter->num_queue_pairs; i++) {
7568 				struct i40e_ring *tx_ring, *rx_ring;
7569 				u16 pf_q;
7570 
7571 				pf_q = iter->base_queue + i;
7572 
7573 				/* Get to TX ring ptr */
7574 				tx_ring = vsi->tx_rings[pf_q];
7575 				tx_ring->ch = iter;
7576 
7577 				/* Get the RX ring ptr */
7578 				rx_ring = vsi->rx_rings[pf_q];
7579 				rx_ring->ch = iter;
7580 			}
7581 			ch = iter;
7582 			break;
7583 		}
7584 	}
7585 
7586 	if (!ch)
7587 		return -EINVAL;
7588 
7589 	/* Guarantee all rings are updated before we update the
7590 	 * MAC address filter.
7591 	 */
7592 	wmb();
7593 
7594 	/* Add a mac filter */
7595 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7596 	if (ret) {
7597 		/* if we cannot add the MAC rule then disable the offload */
7598 		macvlan_release_l2fw_offload(vdev);
7599 		for (i = 0; i < ch->num_queue_pairs; i++) {
7600 			struct i40e_ring *rx_ring;
7601 			u16 pf_q;
7602 
7603 			pf_q = ch->base_queue + i;
7604 			rx_ring = vsi->rx_rings[pf_q];
7605 			rx_ring->netdev = NULL;
7606 		}
7607 		dev_info(&pf->pdev->dev,
7608 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7609 			  i40e_stat_str(hw, ret),
7610 			  i40e_aq_str(hw, aq_err));
7611 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7612 	}
7613 
7614 	return ret;
7615 }
7616 
7617 /**
7618  * i40e_setup_macvlans - create the channels which will be macvlans
7619  * @vsi: the VSI we want to access
7620  * @macvlan_cnt: no. of macvlans to be setup
7621  * @qcnt: no. of Qs per macvlan
7622  * @vdev: macvlan netdevice
7623  */
7624 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7625 			       struct net_device *vdev)
7626 {
7627 	struct i40e_pf *pf = vsi->back;
7628 	struct i40e_hw *hw = &pf->hw;
7629 	struct i40e_vsi_context ctxt;
7630 	u16 sections, qmap, num_qps;
7631 	struct i40e_channel *ch;
7632 	int i, pow, ret = 0;
7633 	u8 offset = 0;
7634 
7635 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7636 		return -EINVAL;
7637 
7638 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7639 
7640 	/* find the next higher power-of-2 of num queue pairs */
7641 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7642 
7643 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7644 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7645 
7646 	/* Setup context bits for the main VSI */
7647 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7648 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7649 	memset(&ctxt, 0, sizeof(ctxt));
7650 	ctxt.seid = vsi->seid;
7651 	ctxt.pf_num = vsi->back->hw.pf_id;
7652 	ctxt.vf_num = 0;
7653 	ctxt.uplink_seid = vsi->uplink_seid;
7654 	ctxt.info = vsi->info;
7655 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7656 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7657 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7658 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7659 
7660 	/* Reconfigure RSS for main VSI with new max queue count */
7661 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7662 	ret = i40e_vsi_config_rss(vsi);
7663 	if (ret) {
7664 		dev_info(&pf->pdev->dev,
7665 			 "Failed to reconfig RSS for num_queues (%u)\n",
7666 			 vsi->rss_size);
7667 		return ret;
7668 	}
7669 	vsi->reconfig_rss = true;
7670 	dev_dbg(&vsi->back->pdev->dev,
7671 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7672 	vsi->next_base_queue = num_qps;
7673 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7674 
7675 	/* Update the VSI after updating the VSI queue-mapping
7676 	 * information
7677 	 */
7678 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7679 	if (ret) {
7680 		dev_info(&pf->pdev->dev,
7681 			 "Update vsi tc config failed, err %s aq_err %s\n",
7682 			 i40e_stat_str(hw, ret),
7683 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7684 		return ret;
7685 	}
7686 	/* update the local VSI info with updated queue map */
7687 	i40e_vsi_update_queue_map(vsi, &ctxt);
7688 	vsi->info.valid_sections = 0;
7689 
7690 	/* Create channels for macvlans */
7691 	INIT_LIST_HEAD(&vsi->macvlan_list);
7692 	for (i = 0; i < macvlan_cnt; i++) {
7693 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7694 		if (!ch) {
7695 			ret = -ENOMEM;
7696 			goto err_free;
7697 		}
7698 		INIT_LIST_HEAD(&ch->list);
7699 		ch->num_queue_pairs = qcnt;
7700 		if (!i40e_setup_channel(pf, vsi, ch)) {
7701 			ret = -EINVAL;
7702 			kfree(ch);
7703 			goto err_free;
7704 		}
7705 		ch->parent_vsi = vsi;
7706 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7707 		vsi->macvlan_cnt++;
7708 		list_add_tail(&ch->list, &vsi->macvlan_list);
7709 	}
7710 
7711 	return ret;
7712 
7713 err_free:
7714 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7715 	i40e_free_macvlan_channels(vsi);
7716 
7717 	return ret;
7718 }
7719 
7720 /**
7721  * i40e_fwd_add - configure macvlans
7722  * @netdev: net device to configure
7723  * @vdev: macvlan netdevice
7724  **/
7725 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7726 {
7727 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7728 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7729 	struct i40e_vsi *vsi = np->vsi;
7730 	struct i40e_pf *pf = vsi->back;
7731 	struct i40e_fwd_adapter *fwd;
7732 	int avail_macvlan, ret;
7733 
7734 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7735 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7736 		return ERR_PTR(-EINVAL);
7737 	}
7738 	if ((pf->flags & I40E_FLAG_TC_MQPRIO)) {
7739 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7740 		return ERR_PTR(-EINVAL);
7741 	}
7742 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7743 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7744 		return ERR_PTR(-EINVAL);
7745 	}
7746 
7747 	/* The macvlan device has to be a single Q device so that the
7748 	 * tc_to_txq field can be reused to pick the tx queue.
7749 	 */
7750 	if (netif_is_multiqueue(vdev))
7751 		return ERR_PTR(-ERANGE);
7752 
7753 	if (!vsi->macvlan_cnt) {
7754 		/* reserve bit 0 for the pf device */
7755 		set_bit(0, vsi->fwd_bitmask);
7756 
7757 		/* Try to reserve as many queues as possible for macvlans. First
7758 		 * reserve 3/4th of max vectors, then half, then quarter and
7759 		 * calculate Qs per macvlan as you go
7760 		 */
7761 		vectors = pf->num_lan_msix;
7762 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7763 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7764 			q_per_macvlan = 4;
7765 			macvlan_cnt = (vectors - 32) / 4;
7766 		} else if (vectors <= 64 && vectors > 32) {
7767 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7768 			q_per_macvlan = 2;
7769 			macvlan_cnt = (vectors - 16) / 2;
7770 		} else if (vectors <= 32 && vectors > 16) {
7771 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7772 			q_per_macvlan = 1;
7773 			macvlan_cnt = vectors - 16;
7774 		} else if (vectors <= 16 && vectors > 8) {
7775 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7776 			q_per_macvlan = 1;
7777 			macvlan_cnt = vectors - 8;
7778 		} else {
7779 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7780 			q_per_macvlan = 1;
7781 			macvlan_cnt = vectors - 1;
7782 		}
7783 
7784 		if (macvlan_cnt == 0)
7785 			return ERR_PTR(-EBUSY);
7786 
7787 		/* Quiesce VSI queues */
7788 		i40e_quiesce_vsi(vsi);
7789 
7790 		/* sets up the macvlans but does not "enable" them */
7791 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
7792 					  vdev);
7793 		if (ret)
7794 			return ERR_PTR(ret);
7795 
7796 		/* Unquiesce VSI */
7797 		i40e_unquiesce_vsi(vsi);
7798 	}
7799 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
7800 					    vsi->macvlan_cnt);
7801 	if (avail_macvlan >= I40E_MAX_MACVLANS)
7802 		return ERR_PTR(-EBUSY);
7803 
7804 	/* create the fwd struct */
7805 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
7806 	if (!fwd)
7807 		return ERR_PTR(-ENOMEM);
7808 
7809 	set_bit(avail_macvlan, vsi->fwd_bitmask);
7810 	fwd->bit_no = avail_macvlan;
7811 	netdev_set_sb_channel(vdev, avail_macvlan);
7812 	fwd->netdev = vdev;
7813 
7814 	if (!netif_running(netdev))
7815 		return fwd;
7816 
7817 	/* Set fwd ring up */
7818 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
7819 	if (ret) {
7820 		/* unbind the queues and drop the subordinate channel config */
7821 		netdev_unbind_sb_channel(netdev, vdev);
7822 		netdev_set_sb_channel(vdev, 0);
7823 
7824 		kfree(fwd);
7825 		return ERR_PTR(-EINVAL);
7826 	}
7827 
7828 	return fwd;
7829 }
7830 
7831 /**
7832  * i40e_del_all_macvlans - Delete all the mac filters on the channels
7833  * @vsi: the VSI we want to access
7834  */
7835 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
7836 {
7837 	struct i40e_channel *ch, *ch_tmp;
7838 	struct i40e_pf *pf = vsi->back;
7839 	struct i40e_hw *hw = &pf->hw;
7840 	int aq_err, ret = 0;
7841 
7842 	if (list_empty(&vsi->macvlan_list))
7843 		return;
7844 
7845 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7846 		if (i40e_is_channel_macvlan(ch)) {
7847 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7848 						      i40e_channel_mac(ch),
7849 						      &aq_err);
7850 			if (!ret) {
7851 				/* Reset queue contexts */
7852 				i40e_reset_ch_rings(vsi, ch);
7853 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7854 				netdev_unbind_sb_channel(vsi->netdev,
7855 							 ch->fwd->netdev);
7856 				netdev_set_sb_channel(ch->fwd->netdev, 0);
7857 				kfree(ch->fwd);
7858 				ch->fwd = NULL;
7859 			}
7860 		}
7861 	}
7862 }
7863 
7864 /**
7865  * i40e_fwd_del - delete macvlan interfaces
7866  * @netdev: net device to configure
7867  * @vdev: macvlan netdevice
7868  */
7869 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
7870 {
7871 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7872 	struct i40e_fwd_adapter *fwd = vdev;
7873 	struct i40e_channel *ch, *ch_tmp;
7874 	struct i40e_vsi *vsi = np->vsi;
7875 	struct i40e_pf *pf = vsi->back;
7876 	struct i40e_hw *hw = &pf->hw;
7877 	int aq_err, ret = 0;
7878 
7879 	/* Find the channel associated with the macvlan and del mac filter */
7880 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7881 		if (i40e_is_channel_macvlan(ch) &&
7882 		    ether_addr_equal(i40e_channel_mac(ch),
7883 				     fwd->netdev->dev_addr)) {
7884 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7885 						      i40e_channel_mac(ch),
7886 						      &aq_err);
7887 			if (!ret) {
7888 				/* Reset queue contexts */
7889 				i40e_reset_ch_rings(vsi, ch);
7890 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7891 				netdev_unbind_sb_channel(netdev, fwd->netdev);
7892 				netdev_set_sb_channel(fwd->netdev, 0);
7893 				kfree(ch->fwd);
7894 				ch->fwd = NULL;
7895 			} else {
7896 				dev_info(&pf->pdev->dev,
7897 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
7898 					  i40e_stat_str(hw, ret),
7899 					  i40e_aq_str(hw, aq_err));
7900 			}
7901 			break;
7902 		}
7903 	}
7904 }
7905 
7906 /**
7907  * i40e_setup_tc - configure multiple traffic classes
7908  * @netdev: net device to configure
7909  * @type_data: tc offload data
7910  **/
7911 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
7912 {
7913 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
7914 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7915 	struct i40e_vsi *vsi = np->vsi;
7916 	struct i40e_pf *pf = vsi->back;
7917 	u8 enabled_tc = 0, num_tc, hw;
7918 	bool need_reset = false;
7919 	int old_queue_pairs;
7920 	int ret = -EINVAL;
7921 	u16 mode;
7922 	int i;
7923 
7924 	old_queue_pairs = vsi->num_queue_pairs;
7925 	num_tc = mqprio_qopt->qopt.num_tc;
7926 	hw = mqprio_qopt->qopt.hw;
7927 	mode = mqprio_qopt->mode;
7928 	if (!hw) {
7929 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7930 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
7931 		goto config_tc;
7932 	}
7933 
7934 	/* Check if MFP enabled */
7935 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
7936 		netdev_info(netdev,
7937 			    "Configuring TC not supported in MFP mode\n");
7938 		return ret;
7939 	}
7940 	switch (mode) {
7941 	case TC_MQPRIO_MODE_DCB:
7942 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7943 
7944 		/* Check if DCB enabled to continue */
7945 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7946 			netdev_info(netdev,
7947 				    "DCB is not enabled for adapter\n");
7948 			return ret;
7949 		}
7950 
7951 		/* Check whether tc count is within enabled limit */
7952 		if (num_tc > i40e_pf_get_num_tc(pf)) {
7953 			netdev_info(netdev,
7954 				    "TC count greater than enabled on link for adapter\n");
7955 			return ret;
7956 		}
7957 		break;
7958 	case TC_MQPRIO_MODE_CHANNEL:
7959 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
7960 			netdev_info(netdev,
7961 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
7962 			return ret;
7963 		}
7964 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7965 			return ret;
7966 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
7967 		if (ret)
7968 			return ret;
7969 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
7970 		       sizeof(*mqprio_qopt));
7971 		pf->flags |= I40E_FLAG_TC_MQPRIO;
7972 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7973 		break;
7974 	default:
7975 		return -EINVAL;
7976 	}
7977 
7978 config_tc:
7979 	/* Generate TC map for number of tc requested */
7980 	for (i = 0; i < num_tc; i++)
7981 		enabled_tc |= BIT(i);
7982 
7983 	/* Requesting same TC configuration as already enabled */
7984 	if (enabled_tc == vsi->tc_config.enabled_tc &&
7985 	    mode != TC_MQPRIO_MODE_CHANNEL)
7986 		return 0;
7987 
7988 	/* Quiesce VSI queues */
7989 	i40e_quiesce_vsi(vsi);
7990 
7991 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
7992 		i40e_remove_queue_channels(vsi);
7993 
7994 	/* Configure VSI for enabled TCs */
7995 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
7996 	if (ret) {
7997 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
7998 			    vsi->seid);
7999 		need_reset = true;
8000 		goto exit;
8001 	} else if (enabled_tc &&
8002 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8003 		netdev_info(netdev,
8004 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8005 			    vsi->tc_config.tc_info[0].qcount);
8006 		ret = -EINVAL;
8007 		need_reset = true;
8008 		goto exit;
8009 	}
8010 
8011 	dev_info(&vsi->back->pdev->dev,
8012 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8013 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8014 
8015 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
8016 		if (vsi->mqprio_qopt.max_rate[0]) {
8017 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
8018 
8019 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
8020 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8021 			if (!ret) {
8022 				u64 credits = max_tx_rate;
8023 
8024 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8025 				dev_dbg(&vsi->back->pdev->dev,
8026 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8027 					max_tx_rate,
8028 					credits,
8029 					vsi->seid);
8030 			} else {
8031 				need_reset = true;
8032 				goto exit;
8033 			}
8034 		}
8035 		ret = i40e_configure_queue_channels(vsi);
8036 		if (ret) {
8037 			vsi->num_queue_pairs = old_queue_pairs;
8038 			netdev_info(netdev,
8039 				    "Failed configuring queue channels\n");
8040 			need_reset = true;
8041 			goto exit;
8042 		}
8043 	}
8044 
8045 exit:
8046 	/* Reset the configuration data to defaults, only TC0 is enabled */
8047 	if (need_reset) {
8048 		i40e_vsi_set_default_tc_config(vsi);
8049 		need_reset = false;
8050 	}
8051 
8052 	/* Unquiesce VSI */
8053 	i40e_unquiesce_vsi(vsi);
8054 	return ret;
8055 }
8056 
8057 /**
8058  * i40e_set_cld_element - sets cloud filter element data
8059  * @filter: cloud filter rule
8060  * @cld: ptr to cloud filter element data
8061  *
8062  * This is helper function to copy data into cloud filter element
8063  **/
8064 static inline void
8065 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8066 		     struct i40e_aqc_cloud_filters_element_data *cld)
8067 {
8068 	u32 ipa;
8069 	int i;
8070 
8071 	memset(cld, 0, sizeof(*cld));
8072 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8073 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8074 
8075 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8076 		return;
8077 
8078 	if (filter->n_proto == ETH_P_IPV6) {
8079 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8080 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8081 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8082 
8083 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8084 		}
8085 	} else {
8086 		ipa = be32_to_cpu(filter->dst_ipv4);
8087 
8088 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8089 	}
8090 
8091 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8092 
8093 	/* tenant_id is not supported by FW now, once the support is enabled
8094 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8095 	 */
8096 	if (filter->tenant_id)
8097 		return;
8098 }
8099 
8100 /**
8101  * i40e_add_del_cloud_filter - Add/del cloud filter
8102  * @vsi: pointer to VSI
8103  * @filter: cloud filter rule
8104  * @add: if true, add, if false, delete
8105  *
8106  * Add or delete a cloud filter for a specific flow spec.
8107  * Returns 0 if the filter were successfully added.
8108  **/
8109 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8110 			      struct i40e_cloud_filter *filter, bool add)
8111 {
8112 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8113 	struct i40e_pf *pf = vsi->back;
8114 	int ret;
8115 	static const u16 flag_table[128] = {
8116 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8117 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8118 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8119 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8120 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8121 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8122 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8123 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8124 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8125 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8126 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8127 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8128 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8129 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8130 	};
8131 
8132 	if (filter->flags >= ARRAY_SIZE(flag_table))
8133 		return I40E_ERR_CONFIG;
8134 
8135 	memset(&cld_filter, 0, sizeof(cld_filter));
8136 
8137 	/* copy element needed to add cloud filter from filter */
8138 	i40e_set_cld_element(filter, &cld_filter);
8139 
8140 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8141 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8142 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8143 
8144 	if (filter->n_proto == ETH_P_IPV6)
8145 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8146 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8147 	else
8148 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8149 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8150 
8151 	if (add)
8152 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8153 						&cld_filter, 1);
8154 	else
8155 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8156 						&cld_filter, 1);
8157 	if (ret)
8158 		dev_dbg(&pf->pdev->dev,
8159 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8160 			add ? "add" : "delete", filter->dst_port, ret,
8161 			pf->hw.aq.asq_last_status);
8162 	else
8163 		dev_info(&pf->pdev->dev,
8164 			 "%s cloud filter for VSI: %d\n",
8165 			 add ? "Added" : "Deleted", filter->seid);
8166 	return ret;
8167 }
8168 
8169 /**
8170  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8171  * @vsi: pointer to VSI
8172  * @filter: cloud filter rule
8173  * @add: if true, add, if false, delete
8174  *
8175  * Add or delete a cloud filter for a specific flow spec using big buffer.
8176  * Returns 0 if the filter were successfully added.
8177  **/
8178 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8179 				      struct i40e_cloud_filter *filter,
8180 				      bool add)
8181 {
8182 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8183 	struct i40e_pf *pf = vsi->back;
8184 	int ret;
8185 
8186 	/* Both (src/dst) valid mac_addr are not supported */
8187 	if ((is_valid_ether_addr(filter->dst_mac) &&
8188 	     is_valid_ether_addr(filter->src_mac)) ||
8189 	    (is_multicast_ether_addr(filter->dst_mac) &&
8190 	     is_multicast_ether_addr(filter->src_mac)))
8191 		return -EOPNOTSUPP;
8192 
8193 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8194 	 * ports are not supported via big buffer now.
8195 	 */
8196 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8197 		return -EOPNOTSUPP;
8198 
8199 	/* adding filter using src_port/src_ip is not supported at this stage */
8200 	if (filter->src_port ||
8201 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8202 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8203 		return -EOPNOTSUPP;
8204 
8205 	memset(&cld_filter, 0, sizeof(cld_filter));
8206 
8207 	/* copy element needed to add cloud filter from filter */
8208 	i40e_set_cld_element(filter, &cld_filter.element);
8209 
8210 	if (is_valid_ether_addr(filter->dst_mac) ||
8211 	    is_valid_ether_addr(filter->src_mac) ||
8212 	    is_multicast_ether_addr(filter->dst_mac) ||
8213 	    is_multicast_ether_addr(filter->src_mac)) {
8214 		/* MAC + IP : unsupported mode */
8215 		if (filter->dst_ipv4)
8216 			return -EOPNOTSUPP;
8217 
8218 		/* since we validated that L4 port must be valid before
8219 		 * we get here, start with respective "flags" value
8220 		 * and update if vlan is present or not
8221 		 */
8222 		cld_filter.element.flags =
8223 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8224 
8225 		if (filter->vlan_id) {
8226 			cld_filter.element.flags =
8227 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8228 		}
8229 
8230 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8231 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8232 		cld_filter.element.flags =
8233 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8234 		if (filter->n_proto == ETH_P_IPV6)
8235 			cld_filter.element.flags |=
8236 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8237 		else
8238 			cld_filter.element.flags |=
8239 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8240 	} else {
8241 		dev_err(&pf->pdev->dev,
8242 			"either mac or ip has to be valid for cloud filter\n");
8243 		return -EINVAL;
8244 	}
8245 
8246 	/* Now copy L4 port in Byte 6..7 in general fields */
8247 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8248 						be16_to_cpu(filter->dst_port);
8249 
8250 	if (add) {
8251 		/* Validate current device switch mode, change if necessary */
8252 		ret = i40e_validate_and_set_switch_mode(vsi);
8253 		if (ret) {
8254 			dev_err(&pf->pdev->dev,
8255 				"failed to set switch mode, ret %d\n",
8256 				ret);
8257 			return ret;
8258 		}
8259 
8260 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8261 						   &cld_filter, 1);
8262 	} else {
8263 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8264 						   &cld_filter, 1);
8265 	}
8266 
8267 	if (ret)
8268 		dev_dbg(&pf->pdev->dev,
8269 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8270 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8271 	else
8272 		dev_info(&pf->pdev->dev,
8273 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8274 			 add ? "add" : "delete", filter->seid,
8275 			 ntohs(filter->dst_port));
8276 	return ret;
8277 }
8278 
8279 /**
8280  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8281  * @vsi: Pointer to VSI
8282  * @f: Pointer to struct flow_cls_offload
8283  * @filter: Pointer to cloud filter structure
8284  *
8285  **/
8286 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8287 				 struct flow_cls_offload *f,
8288 				 struct i40e_cloud_filter *filter)
8289 {
8290 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8291 	struct flow_dissector *dissector = rule->match.dissector;
8292 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8293 	struct i40e_pf *pf = vsi->back;
8294 	u8 field_flags = 0;
8295 
8296 	if (dissector->used_keys &
8297 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8298 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8299 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8300 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8301 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8302 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8303 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8304 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8305 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8306 			dissector->used_keys);
8307 		return -EOPNOTSUPP;
8308 	}
8309 
8310 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8311 		struct flow_match_enc_keyid match;
8312 
8313 		flow_rule_match_enc_keyid(rule, &match);
8314 		if (match.mask->keyid != 0)
8315 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8316 
8317 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8318 	}
8319 
8320 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8321 		struct flow_match_basic match;
8322 
8323 		flow_rule_match_basic(rule, &match);
8324 		n_proto_key = ntohs(match.key->n_proto);
8325 		n_proto_mask = ntohs(match.mask->n_proto);
8326 
8327 		if (n_proto_key == ETH_P_ALL) {
8328 			n_proto_key = 0;
8329 			n_proto_mask = 0;
8330 		}
8331 		filter->n_proto = n_proto_key & n_proto_mask;
8332 		filter->ip_proto = match.key->ip_proto;
8333 	}
8334 
8335 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8336 		struct flow_match_eth_addrs match;
8337 
8338 		flow_rule_match_eth_addrs(rule, &match);
8339 
8340 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8341 		if (!is_zero_ether_addr(match.mask->dst)) {
8342 			if (is_broadcast_ether_addr(match.mask->dst)) {
8343 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8344 			} else {
8345 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8346 					match.mask->dst);
8347 				return I40E_ERR_CONFIG;
8348 			}
8349 		}
8350 
8351 		if (!is_zero_ether_addr(match.mask->src)) {
8352 			if (is_broadcast_ether_addr(match.mask->src)) {
8353 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8354 			} else {
8355 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8356 					match.mask->src);
8357 				return I40E_ERR_CONFIG;
8358 			}
8359 		}
8360 		ether_addr_copy(filter->dst_mac, match.key->dst);
8361 		ether_addr_copy(filter->src_mac, match.key->src);
8362 	}
8363 
8364 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8365 		struct flow_match_vlan match;
8366 
8367 		flow_rule_match_vlan(rule, &match);
8368 		if (match.mask->vlan_id) {
8369 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8370 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8371 
8372 			} else {
8373 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8374 					match.mask->vlan_id);
8375 				return I40E_ERR_CONFIG;
8376 			}
8377 		}
8378 
8379 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8380 	}
8381 
8382 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8383 		struct flow_match_control match;
8384 
8385 		flow_rule_match_control(rule, &match);
8386 		addr_type = match.key->addr_type;
8387 	}
8388 
8389 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8390 		struct flow_match_ipv4_addrs match;
8391 
8392 		flow_rule_match_ipv4_addrs(rule, &match);
8393 		if (match.mask->dst) {
8394 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8395 				field_flags |= I40E_CLOUD_FIELD_IIP;
8396 			} else {
8397 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8398 					&match.mask->dst);
8399 				return I40E_ERR_CONFIG;
8400 			}
8401 		}
8402 
8403 		if (match.mask->src) {
8404 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8405 				field_flags |= I40E_CLOUD_FIELD_IIP;
8406 			} else {
8407 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8408 					&match.mask->src);
8409 				return I40E_ERR_CONFIG;
8410 			}
8411 		}
8412 
8413 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8414 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8415 			return I40E_ERR_CONFIG;
8416 		}
8417 		filter->dst_ipv4 = match.key->dst;
8418 		filter->src_ipv4 = match.key->src;
8419 	}
8420 
8421 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8422 		struct flow_match_ipv6_addrs match;
8423 
8424 		flow_rule_match_ipv6_addrs(rule, &match);
8425 
8426 		/* src and dest IPV6 address should not be LOOPBACK
8427 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8428 		 */
8429 		if (ipv6_addr_loopback(&match.key->dst) ||
8430 		    ipv6_addr_loopback(&match.key->src)) {
8431 			dev_err(&pf->pdev->dev,
8432 				"Bad ipv6, addr is LOOPBACK\n");
8433 			return I40E_ERR_CONFIG;
8434 		}
8435 		if (!ipv6_addr_any(&match.mask->dst) ||
8436 		    !ipv6_addr_any(&match.mask->src))
8437 			field_flags |= I40E_CLOUD_FIELD_IIP;
8438 
8439 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8440 		       sizeof(filter->src_ipv6));
8441 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8442 		       sizeof(filter->dst_ipv6));
8443 	}
8444 
8445 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8446 		struct flow_match_ports match;
8447 
8448 		flow_rule_match_ports(rule, &match);
8449 		if (match.mask->src) {
8450 			if (match.mask->src == cpu_to_be16(0xffff)) {
8451 				field_flags |= I40E_CLOUD_FIELD_IIP;
8452 			} else {
8453 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8454 					be16_to_cpu(match.mask->src));
8455 				return I40E_ERR_CONFIG;
8456 			}
8457 		}
8458 
8459 		if (match.mask->dst) {
8460 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8461 				field_flags |= I40E_CLOUD_FIELD_IIP;
8462 			} else {
8463 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8464 					be16_to_cpu(match.mask->dst));
8465 				return I40E_ERR_CONFIG;
8466 			}
8467 		}
8468 
8469 		filter->dst_port = match.key->dst;
8470 		filter->src_port = match.key->src;
8471 
8472 		switch (filter->ip_proto) {
8473 		case IPPROTO_TCP:
8474 		case IPPROTO_UDP:
8475 			break;
8476 		default:
8477 			dev_err(&pf->pdev->dev,
8478 				"Only UDP and TCP transport are supported\n");
8479 			return -EINVAL;
8480 		}
8481 	}
8482 	filter->flags = field_flags;
8483 	return 0;
8484 }
8485 
8486 /**
8487  * i40e_handle_tclass: Forward to a traffic class on the device
8488  * @vsi: Pointer to VSI
8489  * @tc: traffic class index on the device
8490  * @filter: Pointer to cloud filter structure
8491  *
8492  **/
8493 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8494 			      struct i40e_cloud_filter *filter)
8495 {
8496 	struct i40e_channel *ch, *ch_tmp;
8497 
8498 	/* direct to a traffic class on the same device */
8499 	if (tc == 0) {
8500 		filter->seid = vsi->seid;
8501 		return 0;
8502 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8503 		if (!filter->dst_port) {
8504 			dev_err(&vsi->back->pdev->dev,
8505 				"Specify destination port to direct to traffic class that is not default\n");
8506 			return -EINVAL;
8507 		}
8508 		if (list_empty(&vsi->ch_list))
8509 			return -EINVAL;
8510 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8511 					 list) {
8512 			if (ch->seid == vsi->tc_seid_map[tc])
8513 				filter->seid = ch->seid;
8514 		}
8515 		return 0;
8516 	}
8517 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8518 	return -EINVAL;
8519 }
8520 
8521 /**
8522  * i40e_configure_clsflower - Configure tc flower filters
8523  * @vsi: Pointer to VSI
8524  * @cls_flower: Pointer to struct flow_cls_offload
8525  *
8526  **/
8527 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8528 				    struct flow_cls_offload *cls_flower)
8529 {
8530 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8531 	struct i40e_cloud_filter *filter = NULL;
8532 	struct i40e_pf *pf = vsi->back;
8533 	int err = 0;
8534 
8535 	if (tc < 0) {
8536 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8537 		return -EOPNOTSUPP;
8538 	}
8539 
8540 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8541 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8542 		return -EBUSY;
8543 
8544 	if (pf->fdir_pf_active_filters ||
8545 	    (!hlist_empty(&pf->fdir_filter_list))) {
8546 		dev_err(&vsi->back->pdev->dev,
8547 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8548 		return -EINVAL;
8549 	}
8550 
8551 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8552 		dev_err(&vsi->back->pdev->dev,
8553 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8554 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8555 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8556 	}
8557 
8558 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8559 	if (!filter)
8560 		return -ENOMEM;
8561 
8562 	filter->cookie = cls_flower->cookie;
8563 
8564 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8565 	if (err < 0)
8566 		goto err;
8567 
8568 	err = i40e_handle_tclass(vsi, tc, filter);
8569 	if (err < 0)
8570 		goto err;
8571 
8572 	/* Add cloud filter */
8573 	if (filter->dst_port)
8574 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8575 	else
8576 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8577 
8578 	if (err) {
8579 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8580 			err);
8581 		goto err;
8582 	}
8583 
8584 	/* add filter to the ordered list */
8585 	INIT_HLIST_NODE(&filter->cloud_node);
8586 
8587 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8588 
8589 	pf->num_cloud_filters++;
8590 
8591 	return err;
8592 err:
8593 	kfree(filter);
8594 	return err;
8595 }
8596 
8597 /**
8598  * i40e_find_cloud_filter - Find the could filter in the list
8599  * @vsi: Pointer to VSI
8600  * @cookie: filter specific cookie
8601  *
8602  **/
8603 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8604 							unsigned long *cookie)
8605 {
8606 	struct i40e_cloud_filter *filter = NULL;
8607 	struct hlist_node *node2;
8608 
8609 	hlist_for_each_entry_safe(filter, node2,
8610 				  &vsi->back->cloud_filter_list, cloud_node)
8611 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8612 			return filter;
8613 	return NULL;
8614 }
8615 
8616 /**
8617  * i40e_delete_clsflower - Remove tc flower filters
8618  * @vsi: Pointer to VSI
8619  * @cls_flower: Pointer to struct flow_cls_offload
8620  *
8621  **/
8622 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8623 				 struct flow_cls_offload *cls_flower)
8624 {
8625 	struct i40e_cloud_filter *filter = NULL;
8626 	struct i40e_pf *pf = vsi->back;
8627 	int err = 0;
8628 
8629 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8630 
8631 	if (!filter)
8632 		return -EINVAL;
8633 
8634 	hash_del(&filter->cloud_node);
8635 
8636 	if (filter->dst_port)
8637 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8638 	else
8639 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8640 
8641 	kfree(filter);
8642 	if (err) {
8643 		dev_err(&pf->pdev->dev,
8644 			"Failed to delete cloud filter, err %s\n",
8645 			i40e_stat_str(&pf->hw, err));
8646 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8647 	}
8648 
8649 	pf->num_cloud_filters--;
8650 	if (!pf->num_cloud_filters)
8651 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8652 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8653 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8654 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8655 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8656 		}
8657 	return 0;
8658 }
8659 
8660 /**
8661  * i40e_setup_tc_cls_flower - flower classifier offloads
8662  * @np: net device to configure
8663  * @cls_flower: offload data
8664  **/
8665 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8666 				    struct flow_cls_offload *cls_flower)
8667 {
8668 	struct i40e_vsi *vsi = np->vsi;
8669 
8670 	switch (cls_flower->command) {
8671 	case FLOW_CLS_REPLACE:
8672 		return i40e_configure_clsflower(vsi, cls_flower);
8673 	case FLOW_CLS_DESTROY:
8674 		return i40e_delete_clsflower(vsi, cls_flower);
8675 	case FLOW_CLS_STATS:
8676 		return -EOPNOTSUPP;
8677 	default:
8678 		return -EOPNOTSUPP;
8679 	}
8680 }
8681 
8682 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8683 				  void *cb_priv)
8684 {
8685 	struct i40e_netdev_priv *np = cb_priv;
8686 
8687 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8688 		return -EOPNOTSUPP;
8689 
8690 	switch (type) {
8691 	case TC_SETUP_CLSFLOWER:
8692 		return i40e_setup_tc_cls_flower(np, type_data);
8693 
8694 	default:
8695 		return -EOPNOTSUPP;
8696 	}
8697 }
8698 
8699 static LIST_HEAD(i40e_block_cb_list);
8700 
8701 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8702 			   void *type_data)
8703 {
8704 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8705 
8706 	switch (type) {
8707 	case TC_SETUP_QDISC_MQPRIO:
8708 		return i40e_setup_tc(netdev, type_data);
8709 	case TC_SETUP_BLOCK:
8710 		return flow_block_cb_setup_simple(type_data,
8711 						  &i40e_block_cb_list,
8712 						  i40e_setup_tc_block_cb,
8713 						  np, np, true);
8714 	default:
8715 		return -EOPNOTSUPP;
8716 	}
8717 }
8718 
8719 /**
8720  * i40e_open - Called when a network interface is made active
8721  * @netdev: network interface device structure
8722  *
8723  * The open entry point is called when a network interface is made
8724  * active by the system (IFF_UP).  At this point all resources needed
8725  * for transmit and receive operations are allocated, the interrupt
8726  * handler is registered with the OS, the netdev watchdog subtask is
8727  * enabled, and the stack is notified that the interface is ready.
8728  *
8729  * Returns 0 on success, negative value on failure
8730  **/
8731 int i40e_open(struct net_device *netdev)
8732 {
8733 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8734 	struct i40e_vsi *vsi = np->vsi;
8735 	struct i40e_pf *pf = vsi->back;
8736 	int err;
8737 
8738 	/* disallow open during test or if eeprom is broken */
8739 	if (test_bit(__I40E_TESTING, pf->state) ||
8740 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8741 		return -EBUSY;
8742 
8743 	netif_carrier_off(netdev);
8744 
8745 	if (i40e_force_link_state(pf, true))
8746 		return -EAGAIN;
8747 
8748 	err = i40e_vsi_open(vsi);
8749 	if (err)
8750 		return err;
8751 
8752 	/* configure global TSO hardware offload settings */
8753 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8754 						       TCP_FLAG_FIN) >> 16);
8755 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8756 						       TCP_FLAG_FIN |
8757 						       TCP_FLAG_CWR) >> 16);
8758 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8759 	udp_tunnel_get_rx_info(netdev);
8760 
8761 	return 0;
8762 }
8763 
8764 /**
8765  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
8766  * @vsi: vsi structure
8767  *
8768  * This updates netdev's number of tx/rx queues
8769  *
8770  * Returns status of setting tx/rx queues
8771  **/
8772 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
8773 {
8774 	int ret;
8775 
8776 	ret = netif_set_real_num_rx_queues(vsi->netdev,
8777 					   vsi->num_queue_pairs);
8778 	if (ret)
8779 		return ret;
8780 
8781 	return netif_set_real_num_tx_queues(vsi->netdev,
8782 					    vsi->num_queue_pairs);
8783 }
8784 
8785 /**
8786  * i40e_vsi_open -
8787  * @vsi: the VSI to open
8788  *
8789  * Finish initialization of the VSI.
8790  *
8791  * Returns 0 on success, negative value on failure
8792  *
8793  * Note: expects to be called while under rtnl_lock()
8794  **/
8795 int i40e_vsi_open(struct i40e_vsi *vsi)
8796 {
8797 	struct i40e_pf *pf = vsi->back;
8798 	char int_name[I40E_INT_NAME_STR_LEN];
8799 	int err;
8800 
8801 	/* allocate descriptors */
8802 	err = i40e_vsi_setup_tx_resources(vsi);
8803 	if (err)
8804 		goto err_setup_tx;
8805 	err = i40e_vsi_setup_rx_resources(vsi);
8806 	if (err)
8807 		goto err_setup_rx;
8808 
8809 	err = i40e_vsi_configure(vsi);
8810 	if (err)
8811 		goto err_setup_rx;
8812 
8813 	if (vsi->netdev) {
8814 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
8815 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
8816 		err = i40e_vsi_request_irq(vsi, int_name);
8817 		if (err)
8818 			goto err_setup_rx;
8819 
8820 		/* Notify the stack of the actual queue counts. */
8821 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
8822 		if (err)
8823 			goto err_set_queues;
8824 
8825 	} else if (vsi->type == I40E_VSI_FDIR) {
8826 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
8827 			 dev_driver_string(&pf->pdev->dev),
8828 			 dev_name(&pf->pdev->dev));
8829 		err = i40e_vsi_request_irq(vsi, int_name);
8830 		if (err)
8831 			goto err_setup_rx;
8832 
8833 	} else {
8834 		err = -EINVAL;
8835 		goto err_setup_rx;
8836 	}
8837 
8838 	err = i40e_up_complete(vsi);
8839 	if (err)
8840 		goto err_up_complete;
8841 
8842 	return 0;
8843 
8844 err_up_complete:
8845 	i40e_down(vsi);
8846 err_set_queues:
8847 	i40e_vsi_free_irq(vsi);
8848 err_setup_rx:
8849 	i40e_vsi_free_rx_resources(vsi);
8850 err_setup_tx:
8851 	i40e_vsi_free_tx_resources(vsi);
8852 	if (vsi == pf->vsi[pf->lan_vsi])
8853 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
8854 
8855 	return err;
8856 }
8857 
8858 /**
8859  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
8860  * @pf: Pointer to PF
8861  *
8862  * This function destroys the hlist where all the Flow Director
8863  * filters were saved.
8864  **/
8865 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
8866 {
8867 	struct i40e_fdir_filter *filter;
8868 	struct i40e_flex_pit *pit_entry, *tmp;
8869 	struct hlist_node *node2;
8870 
8871 	hlist_for_each_entry_safe(filter, node2,
8872 				  &pf->fdir_filter_list, fdir_node) {
8873 		hlist_del(&filter->fdir_node);
8874 		kfree(filter);
8875 	}
8876 
8877 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
8878 		list_del(&pit_entry->list);
8879 		kfree(pit_entry);
8880 	}
8881 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
8882 
8883 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
8884 		list_del(&pit_entry->list);
8885 		kfree(pit_entry);
8886 	}
8887 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
8888 
8889 	pf->fdir_pf_active_filters = 0;
8890 	i40e_reset_fdir_filter_cnt(pf);
8891 
8892 	/* Reprogram the default input set for TCP/IPv4 */
8893 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8894 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8895 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8896 
8897 	/* Reprogram the default input set for TCP/IPv6 */
8898 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
8899 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8900 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8901 
8902 	/* Reprogram the default input set for UDP/IPv4 */
8903 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
8904 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8905 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8906 
8907 	/* Reprogram the default input set for UDP/IPv6 */
8908 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
8909 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8910 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8911 
8912 	/* Reprogram the default input set for SCTP/IPv4 */
8913 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
8914 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8915 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8916 
8917 	/* Reprogram the default input set for SCTP/IPv6 */
8918 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
8919 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8920 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8921 
8922 	/* Reprogram the default input set for Other/IPv4 */
8923 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
8924 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8925 
8926 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
8927 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8928 
8929 	/* Reprogram the default input set for Other/IPv6 */
8930 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
8931 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8932 
8933 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
8934 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8935 }
8936 
8937 /**
8938  * i40e_cloud_filter_exit - Cleans up the cloud filters
8939  * @pf: Pointer to PF
8940  *
8941  * This function destroys the hlist where all the cloud filters
8942  * were saved.
8943  **/
8944 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
8945 {
8946 	struct i40e_cloud_filter *cfilter;
8947 	struct hlist_node *node;
8948 
8949 	hlist_for_each_entry_safe(cfilter, node,
8950 				  &pf->cloud_filter_list, cloud_node) {
8951 		hlist_del(&cfilter->cloud_node);
8952 		kfree(cfilter);
8953 	}
8954 	pf->num_cloud_filters = 0;
8955 
8956 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8957 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8958 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8959 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8960 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8961 	}
8962 }
8963 
8964 /**
8965  * i40e_close - Disables a network interface
8966  * @netdev: network interface device structure
8967  *
8968  * The close entry point is called when an interface is de-activated
8969  * by the OS.  The hardware is still under the driver's control, but
8970  * this netdev interface is disabled.
8971  *
8972  * Returns 0, this is not allowed to fail
8973  **/
8974 int i40e_close(struct net_device *netdev)
8975 {
8976 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8977 	struct i40e_vsi *vsi = np->vsi;
8978 
8979 	i40e_vsi_close(vsi);
8980 
8981 	return 0;
8982 }
8983 
8984 /**
8985  * i40e_do_reset - Start a PF or Core Reset sequence
8986  * @pf: board private structure
8987  * @reset_flags: which reset is requested
8988  * @lock_acquired: indicates whether or not the lock has been acquired
8989  * before this function was called.
8990  *
8991  * The essential difference in resets is that the PF Reset
8992  * doesn't clear the packet buffers, doesn't reset the PE
8993  * firmware, and doesn't bother the other PFs on the chip.
8994  **/
8995 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
8996 {
8997 	u32 val;
8998 
8999 	/* do the biggest reset indicated */
9000 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9001 
9002 		/* Request a Global Reset
9003 		 *
9004 		 * This will start the chip's countdown to the actual full
9005 		 * chip reset event, and a warning interrupt to be sent
9006 		 * to all PFs, including the requestor.  Our handler
9007 		 * for the warning interrupt will deal with the shutdown
9008 		 * and recovery of the switch setup.
9009 		 */
9010 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9011 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9012 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9013 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9014 
9015 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9016 
9017 		/* Request a Core Reset
9018 		 *
9019 		 * Same as Global Reset, except does *not* include the MAC/PHY
9020 		 */
9021 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9022 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9023 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9024 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9025 		i40e_flush(&pf->hw);
9026 
9027 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9028 
9029 		/* Request a PF Reset
9030 		 *
9031 		 * Resets only the PF-specific registers
9032 		 *
9033 		 * This goes directly to the tear-down and rebuild of
9034 		 * the switch, since we need to do all the recovery as
9035 		 * for the Core Reset.
9036 		 */
9037 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9038 		i40e_handle_reset_warning(pf, lock_acquired);
9039 
9040 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9041 		/* Request a PF Reset
9042 		 *
9043 		 * Resets PF and reinitializes PFs VSI.
9044 		 */
9045 		i40e_prep_for_reset(pf);
9046 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9047 		dev_info(&pf->pdev->dev,
9048 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9049 			 "FW LLDP is disabled\n" :
9050 			 "FW LLDP is enabled\n");
9051 
9052 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9053 		int v;
9054 
9055 		/* Find the VSI(s) that requested a re-init */
9056 		dev_info(&pf->pdev->dev,
9057 			 "VSI reinit requested\n");
9058 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9059 			struct i40e_vsi *vsi = pf->vsi[v];
9060 
9061 			if (vsi != NULL &&
9062 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9063 					       vsi->state))
9064 				i40e_vsi_reinit_locked(pf->vsi[v]);
9065 		}
9066 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9067 		int v;
9068 
9069 		/* Find the VSI(s) that needs to be brought down */
9070 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9071 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9072 			struct i40e_vsi *vsi = pf->vsi[v];
9073 
9074 			if (vsi != NULL &&
9075 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9076 					       vsi->state)) {
9077 				set_bit(__I40E_VSI_DOWN, vsi->state);
9078 				i40e_down(vsi);
9079 			}
9080 		}
9081 	} else {
9082 		dev_info(&pf->pdev->dev,
9083 			 "bad reset request 0x%08x\n", reset_flags);
9084 	}
9085 }
9086 
9087 #ifdef CONFIG_I40E_DCB
9088 /**
9089  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9090  * @pf: board private structure
9091  * @old_cfg: current DCB config
9092  * @new_cfg: new DCB config
9093  **/
9094 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9095 			    struct i40e_dcbx_config *old_cfg,
9096 			    struct i40e_dcbx_config *new_cfg)
9097 {
9098 	bool need_reconfig = false;
9099 
9100 	/* Check if ETS configuration has changed */
9101 	if (memcmp(&new_cfg->etscfg,
9102 		   &old_cfg->etscfg,
9103 		   sizeof(new_cfg->etscfg))) {
9104 		/* If Priority Table has changed reconfig is needed */
9105 		if (memcmp(&new_cfg->etscfg.prioritytable,
9106 			   &old_cfg->etscfg.prioritytable,
9107 			   sizeof(new_cfg->etscfg.prioritytable))) {
9108 			need_reconfig = true;
9109 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9110 		}
9111 
9112 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9113 			   &old_cfg->etscfg.tcbwtable,
9114 			   sizeof(new_cfg->etscfg.tcbwtable)))
9115 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9116 
9117 		if (memcmp(&new_cfg->etscfg.tsatable,
9118 			   &old_cfg->etscfg.tsatable,
9119 			   sizeof(new_cfg->etscfg.tsatable)))
9120 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9121 	}
9122 
9123 	/* Check if PFC configuration has changed */
9124 	if (memcmp(&new_cfg->pfc,
9125 		   &old_cfg->pfc,
9126 		   sizeof(new_cfg->pfc))) {
9127 		need_reconfig = true;
9128 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9129 	}
9130 
9131 	/* Check if APP Table has changed */
9132 	if (memcmp(&new_cfg->app,
9133 		   &old_cfg->app,
9134 		   sizeof(new_cfg->app))) {
9135 		need_reconfig = true;
9136 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9137 	}
9138 
9139 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9140 	return need_reconfig;
9141 }
9142 
9143 /**
9144  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9145  * @pf: board private structure
9146  * @e: event info posted on ARQ
9147  **/
9148 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9149 				  struct i40e_arq_event_info *e)
9150 {
9151 	struct i40e_aqc_lldp_get_mib *mib =
9152 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9153 	struct i40e_hw *hw = &pf->hw;
9154 	struct i40e_dcbx_config tmp_dcbx_cfg;
9155 	bool need_reconfig = false;
9156 	int ret = 0;
9157 	u8 type;
9158 
9159 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9160 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9161 	    (hw->phy.link_info.link_speed &
9162 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9163 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9164 		/* let firmware decide if the DCB should be disabled */
9165 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9166 
9167 	/* Not DCB capable or capability disabled */
9168 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9169 		return ret;
9170 
9171 	/* Ignore if event is not for Nearest Bridge */
9172 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9173 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9174 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9175 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9176 		return ret;
9177 
9178 	/* Check MIB Type and return if event for Remote MIB update */
9179 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9180 	dev_dbg(&pf->pdev->dev,
9181 		"LLDP event mib type %s\n", type ? "remote" : "local");
9182 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9183 		/* Update the remote cached instance and return */
9184 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9185 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9186 				&hw->remote_dcbx_config);
9187 		goto exit;
9188 	}
9189 
9190 	/* Store the old configuration */
9191 	tmp_dcbx_cfg = hw->local_dcbx_config;
9192 
9193 	/* Reset the old DCBx configuration data */
9194 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9195 	/* Get updated DCBX data from firmware */
9196 	ret = i40e_get_dcb_config(&pf->hw);
9197 	if (ret) {
9198 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9199 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9200 		    (hw->phy.link_info.link_speed &
9201 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9202 			dev_warn(&pf->pdev->dev,
9203 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9204 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9205 		} else {
9206 			dev_info(&pf->pdev->dev,
9207 				 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
9208 				 i40e_stat_str(&pf->hw, ret),
9209 				 i40e_aq_str(&pf->hw,
9210 					     pf->hw.aq.asq_last_status));
9211 		}
9212 		goto exit;
9213 	}
9214 
9215 	/* No change detected in DCBX configs */
9216 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9217 		    sizeof(tmp_dcbx_cfg))) {
9218 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9219 		goto exit;
9220 	}
9221 
9222 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9223 					       &hw->local_dcbx_config);
9224 
9225 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9226 
9227 	if (!need_reconfig)
9228 		goto exit;
9229 
9230 	/* Enable DCB tagging only when more than one TC */
9231 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9232 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9233 	else
9234 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9235 
9236 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9237 	/* Reconfiguration needed quiesce all VSIs */
9238 	i40e_pf_quiesce_all_vsi(pf);
9239 
9240 	/* Changes in configuration update VEB/VSI */
9241 	i40e_dcb_reconfigure(pf);
9242 
9243 	ret = i40e_resume_port_tx(pf);
9244 
9245 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9246 	/* In case of error no point in resuming VSIs */
9247 	if (ret)
9248 		goto exit;
9249 
9250 	/* Wait for the PF's queues to be disabled */
9251 	ret = i40e_pf_wait_queues_disabled(pf);
9252 	if (ret) {
9253 		/* Schedule PF reset to recover */
9254 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9255 		i40e_service_event_schedule(pf);
9256 	} else {
9257 		i40e_pf_unquiesce_all_vsi(pf);
9258 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9259 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9260 	}
9261 
9262 exit:
9263 	return ret;
9264 }
9265 #endif /* CONFIG_I40E_DCB */
9266 
9267 /**
9268  * i40e_do_reset_safe - Protected reset path for userland calls.
9269  * @pf: board private structure
9270  * @reset_flags: which reset is requested
9271  *
9272  **/
9273 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9274 {
9275 	rtnl_lock();
9276 	i40e_do_reset(pf, reset_flags, true);
9277 	rtnl_unlock();
9278 }
9279 
9280 /**
9281  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9282  * @pf: board private structure
9283  * @e: event info posted on ARQ
9284  *
9285  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9286  * and VF queues
9287  **/
9288 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9289 					   struct i40e_arq_event_info *e)
9290 {
9291 	struct i40e_aqc_lan_overflow *data =
9292 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9293 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9294 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9295 	struct i40e_hw *hw = &pf->hw;
9296 	struct i40e_vf *vf;
9297 	u16 vf_id;
9298 
9299 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9300 		queue, qtx_ctl);
9301 
9302 	/* Queue belongs to VF, find the VF and issue VF reset */
9303 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9304 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9305 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9306 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9307 		vf_id -= hw->func_caps.vf_base_id;
9308 		vf = &pf->vf[vf_id];
9309 		i40e_vc_notify_vf_reset(vf);
9310 		/* Allow VF to process pending reset notification */
9311 		msleep(20);
9312 		i40e_reset_vf(vf, false);
9313 	}
9314 }
9315 
9316 /**
9317  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9318  * @pf: board private structure
9319  **/
9320 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9321 {
9322 	u32 val, fcnt_prog;
9323 
9324 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9325 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9326 	return fcnt_prog;
9327 }
9328 
9329 /**
9330  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9331  * @pf: board private structure
9332  **/
9333 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9334 {
9335 	u32 val, fcnt_prog;
9336 
9337 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9338 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9339 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9340 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9341 	return fcnt_prog;
9342 }
9343 
9344 /**
9345  * i40e_get_global_fd_count - Get total FD filters programmed on device
9346  * @pf: board private structure
9347  **/
9348 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9349 {
9350 	u32 val, fcnt_prog;
9351 
9352 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9353 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9354 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9355 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9356 	return fcnt_prog;
9357 }
9358 
9359 /**
9360  * i40e_reenable_fdir_sb - Restore FDir SB capability
9361  * @pf: board private structure
9362  **/
9363 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9364 {
9365 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9366 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9367 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9368 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9369 }
9370 
9371 /**
9372  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9373  * @pf: board private structure
9374  **/
9375 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9376 {
9377 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9378 		/* ATR uses the same filtering logic as SB rules. It only
9379 		 * functions properly if the input set mask is at the default
9380 		 * settings. It is safe to restore the default input set
9381 		 * because there are no active TCPv4 filter rules.
9382 		 */
9383 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9384 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9385 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9386 
9387 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9388 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9389 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9390 	}
9391 }
9392 
9393 /**
9394  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9395  * @pf: board private structure
9396  * @filter: FDir filter to remove
9397  */
9398 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9399 				       struct i40e_fdir_filter *filter)
9400 {
9401 	/* Update counters */
9402 	pf->fdir_pf_active_filters--;
9403 	pf->fd_inv = 0;
9404 
9405 	switch (filter->flow_type) {
9406 	case TCP_V4_FLOW:
9407 		pf->fd_tcp4_filter_cnt--;
9408 		break;
9409 	case UDP_V4_FLOW:
9410 		pf->fd_udp4_filter_cnt--;
9411 		break;
9412 	case SCTP_V4_FLOW:
9413 		pf->fd_sctp4_filter_cnt--;
9414 		break;
9415 	case TCP_V6_FLOW:
9416 		pf->fd_tcp6_filter_cnt--;
9417 		break;
9418 	case UDP_V6_FLOW:
9419 		pf->fd_udp6_filter_cnt--;
9420 		break;
9421 	case SCTP_V6_FLOW:
9422 		pf->fd_udp6_filter_cnt--;
9423 		break;
9424 	case IP_USER_FLOW:
9425 		switch (filter->ipl4_proto) {
9426 		case IPPROTO_TCP:
9427 			pf->fd_tcp4_filter_cnt--;
9428 			break;
9429 		case IPPROTO_UDP:
9430 			pf->fd_udp4_filter_cnt--;
9431 			break;
9432 		case IPPROTO_SCTP:
9433 			pf->fd_sctp4_filter_cnt--;
9434 			break;
9435 		case IPPROTO_IP:
9436 			pf->fd_ip4_filter_cnt--;
9437 			break;
9438 		}
9439 		break;
9440 	case IPV6_USER_FLOW:
9441 		switch (filter->ipl4_proto) {
9442 		case IPPROTO_TCP:
9443 			pf->fd_tcp6_filter_cnt--;
9444 			break;
9445 		case IPPROTO_UDP:
9446 			pf->fd_udp6_filter_cnt--;
9447 			break;
9448 		case IPPROTO_SCTP:
9449 			pf->fd_sctp6_filter_cnt--;
9450 			break;
9451 		case IPPROTO_IP:
9452 			pf->fd_ip6_filter_cnt--;
9453 			break;
9454 		}
9455 		break;
9456 	}
9457 
9458 	/* Remove the filter from the list and free memory */
9459 	hlist_del(&filter->fdir_node);
9460 	kfree(filter);
9461 }
9462 
9463 /**
9464  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9465  * @pf: board private structure
9466  **/
9467 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9468 {
9469 	struct i40e_fdir_filter *filter;
9470 	u32 fcnt_prog, fcnt_avail;
9471 	struct hlist_node *node;
9472 
9473 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9474 		return;
9475 
9476 	/* Check if we have enough room to re-enable FDir SB capability. */
9477 	fcnt_prog = i40e_get_global_fd_count(pf);
9478 	fcnt_avail = pf->fdir_pf_filter_count;
9479 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9480 	    (pf->fd_add_err == 0) ||
9481 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9482 		i40e_reenable_fdir_sb(pf);
9483 
9484 	/* We should wait for even more space before re-enabling ATR.
9485 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9486 	 * rules active.
9487 	 */
9488 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9489 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9490 		i40e_reenable_fdir_atr(pf);
9491 
9492 	/* if hw had a problem adding a filter, delete it */
9493 	if (pf->fd_inv > 0) {
9494 		hlist_for_each_entry_safe(filter, node,
9495 					  &pf->fdir_filter_list, fdir_node)
9496 			if (filter->fd_id == pf->fd_inv)
9497 				i40e_delete_invalid_filter(pf, filter);
9498 	}
9499 }
9500 
9501 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9502 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9503 /**
9504  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9505  * @pf: board private structure
9506  **/
9507 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9508 {
9509 	unsigned long min_flush_time;
9510 	int flush_wait_retry = 50;
9511 	bool disable_atr = false;
9512 	int fd_room;
9513 	int reg;
9514 
9515 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9516 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9517 		return;
9518 
9519 	/* If the flush is happening too quick and we have mostly SB rules we
9520 	 * should not re-enable ATR for some time.
9521 	 */
9522 	min_flush_time = pf->fd_flush_timestamp +
9523 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9524 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9525 
9526 	if (!(time_after(jiffies, min_flush_time)) &&
9527 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9528 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9529 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9530 		disable_atr = true;
9531 	}
9532 
9533 	pf->fd_flush_timestamp = jiffies;
9534 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9535 	/* flush all filters */
9536 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9537 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9538 	i40e_flush(&pf->hw);
9539 	pf->fd_flush_cnt++;
9540 	pf->fd_add_err = 0;
9541 	do {
9542 		/* Check FD flush status every 5-6msec */
9543 		usleep_range(5000, 6000);
9544 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9545 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9546 			break;
9547 	} while (flush_wait_retry--);
9548 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9549 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9550 	} else {
9551 		/* replay sideband filters */
9552 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9553 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9554 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9555 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9556 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9557 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9558 	}
9559 }
9560 
9561 /**
9562  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9563  * @pf: board private structure
9564  **/
9565 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9566 {
9567 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9568 }
9569 
9570 /**
9571  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9572  * @pf: board private structure
9573  **/
9574 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9575 {
9576 
9577 	/* if interface is down do nothing */
9578 	if (test_bit(__I40E_DOWN, pf->state))
9579 		return;
9580 
9581 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9582 		i40e_fdir_flush_and_replay(pf);
9583 
9584 	i40e_fdir_check_and_reenable(pf);
9585 
9586 }
9587 
9588 /**
9589  * i40e_vsi_link_event - notify VSI of a link event
9590  * @vsi: vsi to be notified
9591  * @link_up: link up or down
9592  **/
9593 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9594 {
9595 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9596 		return;
9597 
9598 	switch (vsi->type) {
9599 	case I40E_VSI_MAIN:
9600 		if (!vsi->netdev || !vsi->netdev_registered)
9601 			break;
9602 
9603 		if (link_up) {
9604 			netif_carrier_on(vsi->netdev);
9605 			netif_tx_wake_all_queues(vsi->netdev);
9606 		} else {
9607 			netif_carrier_off(vsi->netdev);
9608 			netif_tx_stop_all_queues(vsi->netdev);
9609 		}
9610 		break;
9611 
9612 	case I40E_VSI_SRIOV:
9613 	case I40E_VSI_VMDQ2:
9614 	case I40E_VSI_CTRL:
9615 	case I40E_VSI_IWARP:
9616 	case I40E_VSI_MIRROR:
9617 	default:
9618 		/* there is no notification for other VSIs */
9619 		break;
9620 	}
9621 }
9622 
9623 /**
9624  * i40e_veb_link_event - notify elements on the veb of a link event
9625  * @veb: veb to be notified
9626  * @link_up: link up or down
9627  **/
9628 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9629 {
9630 	struct i40e_pf *pf;
9631 	int i;
9632 
9633 	if (!veb || !veb->pf)
9634 		return;
9635 	pf = veb->pf;
9636 
9637 	/* depth first... */
9638 	for (i = 0; i < I40E_MAX_VEB; i++)
9639 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9640 			i40e_veb_link_event(pf->veb[i], link_up);
9641 
9642 	/* ... now the local VSIs */
9643 	for (i = 0; i < pf->num_alloc_vsi; i++)
9644 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9645 			i40e_vsi_link_event(pf->vsi[i], link_up);
9646 }
9647 
9648 /**
9649  * i40e_link_event - Update netif_carrier status
9650  * @pf: board private structure
9651  **/
9652 static void i40e_link_event(struct i40e_pf *pf)
9653 {
9654 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9655 	u8 new_link_speed, old_link_speed;
9656 	i40e_status status;
9657 	bool new_link, old_link;
9658 #ifdef CONFIG_I40E_DCB
9659 	int err;
9660 #endif /* CONFIG_I40E_DCB */
9661 
9662 	/* set this to force the get_link_status call to refresh state */
9663 	pf->hw.phy.get_link_info = true;
9664 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9665 	status = i40e_get_link_status(&pf->hw, &new_link);
9666 
9667 	/* On success, disable temp link polling */
9668 	if (status == I40E_SUCCESS) {
9669 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9670 	} else {
9671 		/* Enable link polling temporarily until i40e_get_link_status
9672 		 * returns I40E_SUCCESS
9673 		 */
9674 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9675 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9676 			status);
9677 		return;
9678 	}
9679 
9680 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9681 	new_link_speed = pf->hw.phy.link_info.link_speed;
9682 
9683 	if (new_link == old_link &&
9684 	    new_link_speed == old_link_speed &&
9685 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9686 	     new_link == netif_carrier_ok(vsi->netdev)))
9687 		return;
9688 
9689 	i40e_print_link_message(vsi, new_link);
9690 
9691 	/* Notify the base of the switch tree connected to
9692 	 * the link.  Floating VEBs are not notified.
9693 	 */
9694 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9695 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9696 	else
9697 		i40e_vsi_link_event(vsi, new_link);
9698 
9699 	if (pf->vf)
9700 		i40e_vc_notify_link_state(pf);
9701 
9702 	if (pf->flags & I40E_FLAG_PTP)
9703 		i40e_ptp_set_increment(pf);
9704 #ifdef CONFIG_I40E_DCB
9705 	if (new_link == old_link)
9706 		return;
9707 	/* Not SW DCB so firmware will take care of default settings */
9708 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9709 		return;
9710 
9711 	/* We cover here only link down, as after link up in case of SW DCB
9712 	 * SW LLDP agent will take care of setting it up
9713 	 */
9714 	if (!new_link) {
9715 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9716 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9717 		err = i40e_dcb_sw_default_config(pf);
9718 		if (err) {
9719 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9720 				       I40E_FLAG_DCB_ENABLED);
9721 		} else {
9722 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9723 				       DCB_CAP_DCBX_VER_IEEE;
9724 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9725 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9726 		}
9727 	}
9728 #endif /* CONFIG_I40E_DCB */
9729 }
9730 
9731 /**
9732  * i40e_watchdog_subtask - periodic checks not using event driven response
9733  * @pf: board private structure
9734  **/
9735 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9736 {
9737 	int i;
9738 
9739 	/* if interface is down do nothing */
9740 	if (test_bit(__I40E_DOWN, pf->state) ||
9741 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9742 		return;
9743 
9744 	/* make sure we don't do these things too often */
9745 	if (time_before(jiffies, (pf->service_timer_previous +
9746 				  pf->service_timer_period)))
9747 		return;
9748 	pf->service_timer_previous = jiffies;
9749 
9750 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9751 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9752 		i40e_link_event(pf);
9753 
9754 	/* Update the stats for active netdevs so the network stack
9755 	 * can look at updated numbers whenever it cares to
9756 	 */
9757 	for (i = 0; i < pf->num_alloc_vsi; i++)
9758 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9759 			i40e_update_stats(pf->vsi[i]);
9760 
9761 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9762 		/* Update the stats for the active switching components */
9763 		for (i = 0; i < I40E_MAX_VEB; i++)
9764 			if (pf->veb[i])
9765 				i40e_update_veb_stats(pf->veb[i]);
9766 	}
9767 
9768 	i40e_ptp_rx_hang(pf);
9769 	i40e_ptp_tx_hang(pf);
9770 }
9771 
9772 /**
9773  * i40e_reset_subtask - Set up for resetting the device and driver
9774  * @pf: board private structure
9775  **/
9776 static void i40e_reset_subtask(struct i40e_pf *pf)
9777 {
9778 	u32 reset_flags = 0;
9779 
9780 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
9781 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
9782 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
9783 	}
9784 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
9785 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
9786 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9787 	}
9788 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
9789 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
9790 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
9791 	}
9792 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
9793 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
9794 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
9795 	}
9796 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
9797 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
9798 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
9799 	}
9800 
9801 	/* If there's a recovery already waiting, it takes
9802 	 * precedence before starting a new reset sequence.
9803 	 */
9804 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
9805 		i40e_prep_for_reset(pf);
9806 		i40e_reset(pf);
9807 		i40e_rebuild(pf, false, false);
9808 	}
9809 
9810 	/* If we're already down or resetting, just bail */
9811 	if (reset_flags &&
9812 	    !test_bit(__I40E_DOWN, pf->state) &&
9813 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
9814 		i40e_do_reset(pf, reset_flags, false);
9815 	}
9816 }
9817 
9818 /**
9819  * i40e_handle_link_event - Handle link event
9820  * @pf: board private structure
9821  * @e: event info posted on ARQ
9822  **/
9823 static void i40e_handle_link_event(struct i40e_pf *pf,
9824 				   struct i40e_arq_event_info *e)
9825 {
9826 	struct i40e_aqc_get_link_status *status =
9827 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
9828 
9829 	/* Do a new status request to re-enable LSE reporting
9830 	 * and load new status information into the hw struct
9831 	 * This completely ignores any state information
9832 	 * in the ARQ event info, instead choosing to always
9833 	 * issue the AQ update link status command.
9834 	 */
9835 	i40e_link_event(pf);
9836 
9837 	/* Check if module meets thermal requirements */
9838 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
9839 		dev_err(&pf->pdev->dev,
9840 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
9841 		dev_err(&pf->pdev->dev,
9842 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9843 	} else {
9844 		/* check for unqualified module, if link is down, suppress
9845 		 * the message if link was forced to be down.
9846 		 */
9847 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
9848 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
9849 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
9850 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
9851 			dev_err(&pf->pdev->dev,
9852 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
9853 			dev_err(&pf->pdev->dev,
9854 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9855 		}
9856 	}
9857 }
9858 
9859 /**
9860  * i40e_clean_adminq_subtask - Clean the AdminQ rings
9861  * @pf: board private structure
9862  **/
9863 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
9864 {
9865 	struct i40e_arq_event_info event;
9866 	struct i40e_hw *hw = &pf->hw;
9867 	u16 pending, i = 0;
9868 	i40e_status ret;
9869 	u16 opcode;
9870 	u32 oldval;
9871 	u32 val;
9872 
9873 	/* Do not run clean AQ when PF reset fails */
9874 	if (test_bit(__I40E_RESET_FAILED, pf->state))
9875 		return;
9876 
9877 	/* check for error indications */
9878 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
9879 	oldval = val;
9880 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
9881 		if (hw->debug_mask & I40E_DEBUG_AQ)
9882 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
9883 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
9884 	}
9885 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
9886 		if (hw->debug_mask & I40E_DEBUG_AQ)
9887 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
9888 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
9889 		pf->arq_overflows++;
9890 	}
9891 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
9892 		if (hw->debug_mask & I40E_DEBUG_AQ)
9893 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
9894 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
9895 	}
9896 	if (oldval != val)
9897 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
9898 
9899 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
9900 	oldval = val;
9901 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
9902 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9903 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
9904 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
9905 	}
9906 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
9907 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9908 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
9909 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
9910 	}
9911 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
9912 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9913 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
9914 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
9915 	}
9916 	if (oldval != val)
9917 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
9918 
9919 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
9920 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
9921 	if (!event.msg_buf)
9922 		return;
9923 
9924 	do {
9925 		ret = i40e_clean_arq_element(hw, &event, &pending);
9926 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
9927 			break;
9928 		else if (ret) {
9929 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
9930 			break;
9931 		}
9932 
9933 		opcode = le16_to_cpu(event.desc.opcode);
9934 		switch (opcode) {
9935 
9936 		case i40e_aqc_opc_get_link_status:
9937 			rtnl_lock();
9938 			i40e_handle_link_event(pf, &event);
9939 			rtnl_unlock();
9940 			break;
9941 		case i40e_aqc_opc_send_msg_to_pf:
9942 			ret = i40e_vc_process_vf_msg(pf,
9943 					le16_to_cpu(event.desc.retval),
9944 					le32_to_cpu(event.desc.cookie_high),
9945 					le32_to_cpu(event.desc.cookie_low),
9946 					event.msg_buf,
9947 					event.msg_len);
9948 			break;
9949 		case i40e_aqc_opc_lldp_update_mib:
9950 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
9951 #ifdef CONFIG_I40E_DCB
9952 			rtnl_lock();
9953 			i40e_handle_lldp_event(pf, &event);
9954 			rtnl_unlock();
9955 #endif /* CONFIG_I40E_DCB */
9956 			break;
9957 		case i40e_aqc_opc_event_lan_overflow:
9958 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
9959 			i40e_handle_lan_overflow_event(pf, &event);
9960 			break;
9961 		case i40e_aqc_opc_send_msg_to_peer:
9962 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
9963 			break;
9964 		case i40e_aqc_opc_nvm_erase:
9965 		case i40e_aqc_opc_nvm_update:
9966 		case i40e_aqc_opc_oem_post_update:
9967 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
9968 				   "ARQ NVM operation 0x%04x completed\n",
9969 				   opcode);
9970 			break;
9971 		default:
9972 			dev_info(&pf->pdev->dev,
9973 				 "ARQ: Unknown event 0x%04x ignored\n",
9974 				 opcode);
9975 			break;
9976 		}
9977 	} while (i++ < pf->adminq_work_limit);
9978 
9979 	if (i < pf->adminq_work_limit)
9980 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
9981 
9982 	/* re-enable Admin queue interrupt cause */
9983 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
9984 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
9985 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
9986 	i40e_flush(hw);
9987 
9988 	kfree(event.msg_buf);
9989 }
9990 
9991 /**
9992  * i40e_verify_eeprom - make sure eeprom is good to use
9993  * @pf: board private structure
9994  **/
9995 static void i40e_verify_eeprom(struct i40e_pf *pf)
9996 {
9997 	int err;
9998 
9999 	err = i40e_diag_eeprom_test(&pf->hw);
10000 	if (err) {
10001 		/* retry in case of garbage read */
10002 		err = i40e_diag_eeprom_test(&pf->hw);
10003 		if (err) {
10004 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10005 				 err);
10006 			set_bit(__I40E_BAD_EEPROM, pf->state);
10007 		}
10008 	}
10009 
10010 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10011 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10012 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10013 	}
10014 }
10015 
10016 /**
10017  * i40e_enable_pf_switch_lb
10018  * @pf: pointer to the PF structure
10019  *
10020  * enable switch loop back or die - no point in a return value
10021  **/
10022 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10023 {
10024 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10025 	struct i40e_vsi_context ctxt;
10026 	int ret;
10027 
10028 	ctxt.seid = pf->main_vsi_seid;
10029 	ctxt.pf_num = pf->hw.pf_id;
10030 	ctxt.vf_num = 0;
10031 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10032 	if (ret) {
10033 		dev_info(&pf->pdev->dev,
10034 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10035 			 i40e_stat_str(&pf->hw, ret),
10036 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10037 		return;
10038 	}
10039 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10040 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10041 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10042 
10043 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10044 	if (ret) {
10045 		dev_info(&pf->pdev->dev,
10046 			 "update vsi switch failed, err %s aq_err %s\n",
10047 			 i40e_stat_str(&pf->hw, ret),
10048 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10049 	}
10050 }
10051 
10052 /**
10053  * i40e_disable_pf_switch_lb
10054  * @pf: pointer to the PF structure
10055  *
10056  * disable switch loop back or die - no point in a return value
10057  **/
10058 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10059 {
10060 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10061 	struct i40e_vsi_context ctxt;
10062 	int ret;
10063 
10064 	ctxt.seid = pf->main_vsi_seid;
10065 	ctxt.pf_num = pf->hw.pf_id;
10066 	ctxt.vf_num = 0;
10067 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10068 	if (ret) {
10069 		dev_info(&pf->pdev->dev,
10070 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10071 			 i40e_stat_str(&pf->hw, ret),
10072 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10073 		return;
10074 	}
10075 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10076 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10077 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10078 
10079 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10080 	if (ret) {
10081 		dev_info(&pf->pdev->dev,
10082 			 "update vsi switch failed, err %s aq_err %s\n",
10083 			 i40e_stat_str(&pf->hw, ret),
10084 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10085 	}
10086 }
10087 
10088 /**
10089  * i40e_config_bridge_mode - Configure the HW bridge mode
10090  * @veb: pointer to the bridge instance
10091  *
10092  * Configure the loop back mode for the LAN VSI that is downlink to the
10093  * specified HW bridge instance. It is expected this function is called
10094  * when a new HW bridge is instantiated.
10095  **/
10096 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10097 {
10098 	struct i40e_pf *pf = veb->pf;
10099 
10100 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10101 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10102 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10103 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10104 		i40e_disable_pf_switch_lb(pf);
10105 	else
10106 		i40e_enable_pf_switch_lb(pf);
10107 }
10108 
10109 /**
10110  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10111  * @veb: pointer to the VEB instance
10112  *
10113  * This is a recursive function that first builds the attached VSIs then
10114  * recurses in to build the next layer of VEB.  We track the connections
10115  * through our own index numbers because the seid's from the HW could
10116  * change across the reset.
10117  **/
10118 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10119 {
10120 	struct i40e_vsi *ctl_vsi = NULL;
10121 	struct i40e_pf *pf = veb->pf;
10122 	int v, veb_idx;
10123 	int ret;
10124 
10125 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10126 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10127 		if (pf->vsi[v] &&
10128 		    pf->vsi[v]->veb_idx == veb->idx &&
10129 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10130 			ctl_vsi = pf->vsi[v];
10131 			break;
10132 		}
10133 	}
10134 	if (!ctl_vsi) {
10135 		dev_info(&pf->pdev->dev,
10136 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10137 		ret = -ENOENT;
10138 		goto end_reconstitute;
10139 	}
10140 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10141 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10142 	ret = i40e_add_vsi(ctl_vsi);
10143 	if (ret) {
10144 		dev_info(&pf->pdev->dev,
10145 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10146 			 veb->idx, ret);
10147 		goto end_reconstitute;
10148 	}
10149 	i40e_vsi_reset_stats(ctl_vsi);
10150 
10151 	/* create the VEB in the switch and move the VSI onto the VEB */
10152 	ret = i40e_add_veb(veb, ctl_vsi);
10153 	if (ret)
10154 		goto end_reconstitute;
10155 
10156 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10157 		veb->bridge_mode = BRIDGE_MODE_VEB;
10158 	else
10159 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10160 	i40e_config_bridge_mode(veb);
10161 
10162 	/* create the remaining VSIs attached to this VEB */
10163 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10164 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10165 			continue;
10166 
10167 		if (pf->vsi[v]->veb_idx == veb->idx) {
10168 			struct i40e_vsi *vsi = pf->vsi[v];
10169 
10170 			vsi->uplink_seid = veb->seid;
10171 			ret = i40e_add_vsi(vsi);
10172 			if (ret) {
10173 				dev_info(&pf->pdev->dev,
10174 					 "rebuild of vsi_idx %d failed: %d\n",
10175 					 v, ret);
10176 				goto end_reconstitute;
10177 			}
10178 			i40e_vsi_reset_stats(vsi);
10179 		}
10180 	}
10181 
10182 	/* create any VEBs attached to this VEB - RECURSION */
10183 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10184 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10185 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10186 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10187 			if (ret)
10188 				break;
10189 		}
10190 	}
10191 
10192 end_reconstitute:
10193 	return ret;
10194 }
10195 
10196 /**
10197  * i40e_get_capabilities - get info about the HW
10198  * @pf: the PF struct
10199  * @list_type: AQ capability to be queried
10200  **/
10201 static int i40e_get_capabilities(struct i40e_pf *pf,
10202 				 enum i40e_admin_queue_opc list_type)
10203 {
10204 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10205 	u16 data_size;
10206 	int buf_len;
10207 	int err;
10208 
10209 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10210 	do {
10211 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10212 		if (!cap_buf)
10213 			return -ENOMEM;
10214 
10215 		/* this loads the data into the hw struct for us */
10216 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10217 						    &data_size, list_type,
10218 						    NULL);
10219 		/* data loaded, buffer no longer needed */
10220 		kfree(cap_buf);
10221 
10222 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10223 			/* retry with a larger buffer */
10224 			buf_len = data_size;
10225 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10226 			dev_info(&pf->pdev->dev,
10227 				 "capability discovery failed, err %s aq_err %s\n",
10228 				 i40e_stat_str(&pf->hw, err),
10229 				 i40e_aq_str(&pf->hw,
10230 					     pf->hw.aq.asq_last_status));
10231 			return -ENODEV;
10232 		}
10233 	} while (err);
10234 
10235 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10236 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10237 			dev_info(&pf->pdev->dev,
10238 				 "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",
10239 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10240 				 pf->hw.func_caps.num_msix_vectors,
10241 				 pf->hw.func_caps.num_msix_vectors_vf,
10242 				 pf->hw.func_caps.fd_filters_guaranteed,
10243 				 pf->hw.func_caps.fd_filters_best_effort,
10244 				 pf->hw.func_caps.num_tx_qp,
10245 				 pf->hw.func_caps.num_vsis);
10246 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10247 			dev_info(&pf->pdev->dev,
10248 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10249 				 pf->hw.dev_caps.switch_mode,
10250 				 pf->hw.dev_caps.valid_functions);
10251 			dev_info(&pf->pdev->dev,
10252 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10253 				 pf->hw.dev_caps.sr_iov_1_1,
10254 				 pf->hw.dev_caps.num_vfs);
10255 			dev_info(&pf->pdev->dev,
10256 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10257 				 pf->hw.dev_caps.num_vsis,
10258 				 pf->hw.dev_caps.num_rx_qp,
10259 				 pf->hw.dev_caps.num_tx_qp);
10260 		}
10261 	}
10262 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10263 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10264 		       + pf->hw.func_caps.num_vfs)
10265 		if (pf->hw.revision_id == 0 &&
10266 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10267 			dev_info(&pf->pdev->dev,
10268 				 "got num_vsis %d, setting num_vsis to %d\n",
10269 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10270 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10271 		}
10272 	}
10273 	return 0;
10274 }
10275 
10276 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10277 
10278 /**
10279  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10280  * @pf: board private structure
10281  **/
10282 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10283 {
10284 	struct i40e_vsi *vsi;
10285 
10286 	/* quick workaround for an NVM issue that leaves a critical register
10287 	 * uninitialized
10288 	 */
10289 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10290 		static const u32 hkey[] = {
10291 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10292 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10293 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10294 			0x95b3a76d};
10295 		int i;
10296 
10297 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10298 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10299 	}
10300 
10301 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10302 		return;
10303 
10304 	/* find existing VSI and see if it needs configuring */
10305 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10306 
10307 	/* create a new VSI if none exists */
10308 	if (!vsi) {
10309 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10310 				     pf->vsi[pf->lan_vsi]->seid, 0);
10311 		if (!vsi) {
10312 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10313 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10314 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10315 			return;
10316 		}
10317 	}
10318 
10319 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10320 }
10321 
10322 /**
10323  * i40e_fdir_teardown - release the Flow Director resources
10324  * @pf: board private structure
10325  **/
10326 static void i40e_fdir_teardown(struct i40e_pf *pf)
10327 {
10328 	struct i40e_vsi *vsi;
10329 
10330 	i40e_fdir_filter_exit(pf);
10331 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10332 	if (vsi)
10333 		i40e_vsi_release(vsi);
10334 }
10335 
10336 /**
10337  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10338  * @vsi: PF main vsi
10339  * @seid: seid of main or channel VSIs
10340  *
10341  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10342  * existed before reset
10343  **/
10344 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10345 {
10346 	struct i40e_cloud_filter *cfilter;
10347 	struct i40e_pf *pf = vsi->back;
10348 	struct hlist_node *node;
10349 	i40e_status ret;
10350 
10351 	/* Add cloud filters back if they exist */
10352 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10353 				  cloud_node) {
10354 		if (cfilter->seid != seid)
10355 			continue;
10356 
10357 		if (cfilter->dst_port)
10358 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10359 								true);
10360 		else
10361 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10362 
10363 		if (ret) {
10364 			dev_dbg(&pf->pdev->dev,
10365 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
10366 				i40e_stat_str(&pf->hw, ret),
10367 				i40e_aq_str(&pf->hw,
10368 					    pf->hw.aq.asq_last_status));
10369 			return ret;
10370 		}
10371 	}
10372 	return 0;
10373 }
10374 
10375 /**
10376  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10377  * @vsi: PF main vsi
10378  *
10379  * Rebuilds channel VSIs if they existed before reset
10380  **/
10381 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10382 {
10383 	struct i40e_channel *ch, *ch_tmp;
10384 	i40e_status ret;
10385 
10386 	if (list_empty(&vsi->ch_list))
10387 		return 0;
10388 
10389 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10390 		if (!ch->initialized)
10391 			break;
10392 		/* Proceed with creation of channel (VMDq2) VSI */
10393 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10394 		if (ret) {
10395 			dev_info(&vsi->back->pdev->dev,
10396 				 "failed to rebuild channels using uplink_seid %u\n",
10397 				 vsi->uplink_seid);
10398 			return ret;
10399 		}
10400 		/* Reconfigure TX queues using QTX_CTL register */
10401 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10402 		if (ret) {
10403 			dev_info(&vsi->back->pdev->dev,
10404 				 "failed to configure TX rings for channel %u\n",
10405 				 ch->seid);
10406 			return ret;
10407 		}
10408 		/* update 'next_base_queue' */
10409 		vsi->next_base_queue = vsi->next_base_queue +
10410 							ch->num_queue_pairs;
10411 		if (ch->max_tx_rate) {
10412 			u64 credits = ch->max_tx_rate;
10413 
10414 			if (i40e_set_bw_limit(vsi, ch->seid,
10415 					      ch->max_tx_rate))
10416 				return -EINVAL;
10417 
10418 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10419 			dev_dbg(&vsi->back->pdev->dev,
10420 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10421 				ch->max_tx_rate,
10422 				credits,
10423 				ch->seid);
10424 		}
10425 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10426 		if (ret) {
10427 			dev_dbg(&vsi->back->pdev->dev,
10428 				"Failed to rebuild cloud filters for channel VSI %u\n",
10429 				ch->seid);
10430 			return ret;
10431 		}
10432 	}
10433 	return 0;
10434 }
10435 
10436 /**
10437  * i40e_prep_for_reset - prep for the core to reset
10438  * @pf: board private structure
10439  *
10440  * Close up the VFs and other things in prep for PF Reset.
10441   **/
10442 static void i40e_prep_for_reset(struct i40e_pf *pf)
10443 {
10444 	struct i40e_hw *hw = &pf->hw;
10445 	i40e_status ret = 0;
10446 	u32 v;
10447 
10448 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10449 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10450 		return;
10451 	if (i40e_check_asq_alive(&pf->hw))
10452 		i40e_vc_notify_reset(pf);
10453 
10454 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10455 
10456 	/* quiesce the VSIs and their queues that are not already DOWN */
10457 	i40e_pf_quiesce_all_vsi(pf);
10458 
10459 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10460 		if (pf->vsi[v])
10461 			pf->vsi[v]->seid = 0;
10462 	}
10463 
10464 	i40e_shutdown_adminq(&pf->hw);
10465 
10466 	/* call shutdown HMC */
10467 	if (hw->hmc.hmc_obj) {
10468 		ret = i40e_shutdown_lan_hmc(hw);
10469 		if (ret)
10470 			dev_warn(&pf->pdev->dev,
10471 				 "shutdown_lan_hmc failed: %d\n", ret);
10472 	}
10473 
10474 	/* Save the current PTP time so that we can restore the time after the
10475 	 * reset completes.
10476 	 */
10477 	i40e_ptp_save_hw_time(pf);
10478 }
10479 
10480 /**
10481  * i40e_send_version - update firmware with driver version
10482  * @pf: PF struct
10483  */
10484 static void i40e_send_version(struct i40e_pf *pf)
10485 {
10486 	struct i40e_driver_version dv;
10487 
10488 	dv.major_version = 0xff;
10489 	dv.minor_version = 0xff;
10490 	dv.build_version = 0xff;
10491 	dv.subbuild_version = 0;
10492 	strlcpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10493 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10494 }
10495 
10496 /**
10497  * i40e_get_oem_version - get OEM specific version information
10498  * @hw: pointer to the hardware structure
10499  **/
10500 static void i40e_get_oem_version(struct i40e_hw *hw)
10501 {
10502 	u16 block_offset = 0xffff;
10503 	u16 block_length = 0;
10504 	u16 capabilities = 0;
10505 	u16 gen_snap = 0;
10506 	u16 release = 0;
10507 
10508 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10509 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10510 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10511 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10512 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10513 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10514 #define I40E_NVM_OEM_LENGTH			3
10515 
10516 	/* Check if pointer to OEM version block is valid. */
10517 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10518 	if (block_offset == 0xffff)
10519 		return;
10520 
10521 	/* Check if OEM version block has correct length. */
10522 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10523 			   &block_length);
10524 	if (block_length < I40E_NVM_OEM_LENGTH)
10525 		return;
10526 
10527 	/* Check if OEM version format is as expected. */
10528 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10529 			   &capabilities);
10530 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10531 		return;
10532 
10533 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10534 			   &gen_snap);
10535 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10536 			   &release);
10537 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10538 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10539 }
10540 
10541 /**
10542  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10543  * @pf: board private structure
10544  **/
10545 static int i40e_reset(struct i40e_pf *pf)
10546 {
10547 	struct i40e_hw *hw = &pf->hw;
10548 	i40e_status ret;
10549 
10550 	ret = i40e_pf_reset(hw);
10551 	if (ret) {
10552 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10553 		set_bit(__I40E_RESET_FAILED, pf->state);
10554 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10555 	} else {
10556 		pf->pfr_count++;
10557 	}
10558 	return ret;
10559 }
10560 
10561 /**
10562  * i40e_rebuild - rebuild using a saved config
10563  * @pf: board private structure
10564  * @reinit: if the Main VSI needs to re-initialized.
10565  * @lock_acquired: indicates whether or not the lock has been acquired
10566  * before this function was called.
10567  **/
10568 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10569 {
10570 	int old_recovery_mode_bit = test_bit(__I40E_RECOVERY_MODE, pf->state);
10571 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10572 	struct i40e_hw *hw = &pf->hw;
10573 	i40e_status ret;
10574 	u32 val;
10575 	int v;
10576 
10577 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10578 	    i40e_check_recovery_mode(pf)) {
10579 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10580 	}
10581 
10582 	if (test_bit(__I40E_DOWN, pf->state) &&
10583 	    !test_bit(__I40E_RECOVERY_MODE, pf->state) &&
10584 	    !old_recovery_mode_bit)
10585 		goto clear_recovery;
10586 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10587 
10588 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10589 	ret = i40e_init_adminq(&pf->hw);
10590 	if (ret) {
10591 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10592 			 i40e_stat_str(&pf->hw, ret),
10593 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10594 		goto clear_recovery;
10595 	}
10596 	i40e_get_oem_version(&pf->hw);
10597 
10598 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10599 		/* The following delay is necessary for firmware update. */
10600 		mdelay(1000);
10601 	}
10602 
10603 	/* re-verify the eeprom if we just had an EMP reset */
10604 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10605 		i40e_verify_eeprom(pf);
10606 
10607 	/* if we are going out of or into recovery mode we have to act
10608 	 * accordingly with regard to resources initialization
10609 	 * and deinitialization
10610 	 */
10611 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) ||
10612 	    old_recovery_mode_bit) {
10613 		if (i40e_get_capabilities(pf,
10614 					  i40e_aqc_opc_list_func_capabilities))
10615 			goto end_unlock;
10616 
10617 		if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10618 			/* we're staying in recovery mode so we'll reinitialize
10619 			 * misc vector here
10620 			 */
10621 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10622 				goto end_unlock;
10623 		} else {
10624 			if (!lock_acquired)
10625 				rtnl_lock();
10626 			/* we're going out of recovery mode so we'll free
10627 			 * the IRQ allocated specifically for recovery mode
10628 			 * and restore the interrupt scheme
10629 			 */
10630 			free_irq(pf->pdev->irq, pf);
10631 			i40e_clear_interrupt_scheme(pf);
10632 			if (i40e_restore_interrupt_scheme(pf))
10633 				goto end_unlock;
10634 		}
10635 
10636 		/* tell the firmware that we're starting */
10637 		i40e_send_version(pf);
10638 
10639 		/* bail out in case recovery mode was detected, as there is
10640 		 * no need for further configuration.
10641 		 */
10642 		goto end_unlock;
10643 	}
10644 
10645 	i40e_clear_pxe_mode(hw);
10646 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10647 	if (ret)
10648 		goto end_core_reset;
10649 
10650 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10651 				hw->func_caps.num_rx_qp, 0, 0);
10652 	if (ret) {
10653 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10654 		goto end_core_reset;
10655 	}
10656 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10657 	if (ret) {
10658 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10659 		goto end_core_reset;
10660 	}
10661 
10662 #ifdef CONFIG_I40E_DCB
10663 	/* Enable FW to write a default DCB config on link-up
10664 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10665 	 * is not supported with new link speed
10666 	 */
10667 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
10668 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10669 	} else {
10670 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10671 		    (hw->phy.link_info.link_speed &
10672 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10673 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10674 			dev_warn(&pf->pdev->dev,
10675 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10676 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10677 		} else {
10678 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10679 			ret = i40e_init_pf_dcb(pf);
10680 			if (ret) {
10681 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10682 					 ret);
10683 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10684 				/* Continue without DCB enabled */
10685 			}
10686 		}
10687 	}
10688 
10689 #endif /* CONFIG_I40E_DCB */
10690 	if (!lock_acquired)
10691 		rtnl_lock();
10692 	ret = i40e_setup_pf_switch(pf, reinit, true);
10693 	if (ret)
10694 		goto end_unlock;
10695 
10696 	/* The driver only wants link up/down and module qualification
10697 	 * reports from firmware.  Note the negative logic.
10698 	 */
10699 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10700 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10701 					 I40E_AQ_EVENT_MEDIA_NA |
10702 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10703 	if (ret)
10704 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10705 			 i40e_stat_str(&pf->hw, ret),
10706 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10707 
10708 	/* Rebuild the VSIs and VEBs that existed before reset.
10709 	 * They are still in our local switch element arrays, so only
10710 	 * need to rebuild the switch model in the HW.
10711 	 *
10712 	 * If there were VEBs but the reconstitution failed, we'll try
10713 	 * to recover minimal use by getting the basic PF VSI working.
10714 	 */
10715 	if (vsi->uplink_seid != pf->mac_seid) {
10716 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10717 		/* find the one VEB connected to the MAC, and find orphans */
10718 		for (v = 0; v < I40E_MAX_VEB; v++) {
10719 			if (!pf->veb[v])
10720 				continue;
10721 
10722 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10723 			    pf->veb[v]->uplink_seid == 0) {
10724 				ret = i40e_reconstitute_veb(pf->veb[v]);
10725 
10726 				if (!ret)
10727 					continue;
10728 
10729 				/* If Main VEB failed, we're in deep doodoo,
10730 				 * so give up rebuilding the switch and set up
10731 				 * for minimal rebuild of PF VSI.
10732 				 * If orphan failed, we'll report the error
10733 				 * but try to keep going.
10734 				 */
10735 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10736 					dev_info(&pf->pdev->dev,
10737 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10738 						 ret);
10739 					vsi->uplink_seid = pf->mac_seid;
10740 					break;
10741 				} else if (pf->veb[v]->uplink_seid == 0) {
10742 					dev_info(&pf->pdev->dev,
10743 						 "rebuild of orphan VEB failed: %d\n",
10744 						 ret);
10745 				}
10746 			}
10747 		}
10748 	}
10749 
10750 	if (vsi->uplink_seid == pf->mac_seid) {
10751 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10752 		/* no VEB, so rebuild only the Main VSI */
10753 		ret = i40e_add_vsi(vsi);
10754 		if (ret) {
10755 			dev_info(&pf->pdev->dev,
10756 				 "rebuild of Main VSI failed: %d\n", ret);
10757 			goto end_unlock;
10758 		}
10759 	}
10760 
10761 	if (vsi->mqprio_qopt.max_rate[0]) {
10762 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
10763 		u64 credits = 0;
10764 
10765 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
10766 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10767 		if (ret)
10768 			goto end_unlock;
10769 
10770 		credits = max_tx_rate;
10771 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10772 		dev_dbg(&vsi->back->pdev->dev,
10773 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10774 			max_tx_rate,
10775 			credits,
10776 			vsi->seid);
10777 	}
10778 
10779 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10780 	if (ret)
10781 		goto end_unlock;
10782 
10783 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
10784 	 * for this main VSI if they exist
10785 	 */
10786 	ret = i40e_rebuild_channels(vsi);
10787 	if (ret)
10788 		goto end_unlock;
10789 
10790 	/* Reconfigure hardware for allowing smaller MSS in the case
10791 	 * of TSO, so that we avoid the MDD being fired and causing
10792 	 * a reset in the case of small MSS+TSO.
10793 	 */
10794 #define I40E_REG_MSS          0x000E64DC
10795 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
10796 #define I40E_64BYTE_MSS       0x400000
10797 	val = rd32(hw, I40E_REG_MSS);
10798 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10799 		val &= ~I40E_REG_MSS_MIN_MASK;
10800 		val |= I40E_64BYTE_MSS;
10801 		wr32(hw, I40E_REG_MSS, val);
10802 	}
10803 
10804 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
10805 		msleep(75);
10806 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10807 		if (ret)
10808 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10809 				 i40e_stat_str(&pf->hw, ret),
10810 				 i40e_aq_str(&pf->hw,
10811 					     pf->hw.aq.asq_last_status));
10812 	}
10813 	/* reinit the misc interrupt */
10814 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10815 		ret = i40e_setup_misc_vector(pf);
10816 
10817 	/* Add a filter to drop all Flow control frames from any VSI from being
10818 	 * transmitted. By doing so we stop a malicious VF from sending out
10819 	 * PAUSE or PFC frames and potentially controlling traffic for other
10820 	 * PF/VF VSIs.
10821 	 * The FW can still send Flow control frames if enabled.
10822 	 */
10823 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
10824 						       pf->main_vsi_seid);
10825 
10826 	/* restart the VSIs that were rebuilt and running before the reset */
10827 	i40e_pf_unquiesce_all_vsi(pf);
10828 
10829 	/* Release the RTNL lock before we start resetting VFs */
10830 	if (!lock_acquired)
10831 		rtnl_unlock();
10832 
10833 	/* Restore promiscuous settings */
10834 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
10835 	if (ret)
10836 		dev_warn(&pf->pdev->dev,
10837 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
10838 			 pf->cur_promisc ? "on" : "off",
10839 			 i40e_stat_str(&pf->hw, ret),
10840 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10841 
10842 	i40e_reset_all_vfs(pf, true);
10843 
10844 	/* tell the firmware that we're starting */
10845 	i40e_send_version(pf);
10846 
10847 	/* We've already released the lock, so don't do it again */
10848 	goto end_core_reset;
10849 
10850 end_unlock:
10851 	if (!lock_acquired)
10852 		rtnl_unlock();
10853 end_core_reset:
10854 	clear_bit(__I40E_RESET_FAILED, pf->state);
10855 clear_recovery:
10856 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10857 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
10858 }
10859 
10860 /**
10861  * i40e_reset_and_rebuild - reset and rebuild using a saved config
10862  * @pf: board private structure
10863  * @reinit: if the Main VSI needs to re-initialized.
10864  * @lock_acquired: indicates whether or not the lock has been acquired
10865  * before this function was called.
10866  **/
10867 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
10868 				   bool lock_acquired)
10869 {
10870 	int ret;
10871 
10872 	if (test_bit(__I40E_IN_REMOVE, pf->state))
10873 		return;
10874 	/* Now we wait for GRST to settle out.
10875 	 * We don't have to delete the VEBs or VSIs from the hw switch
10876 	 * because the reset will make them disappear.
10877 	 */
10878 	ret = i40e_reset(pf);
10879 	if (!ret)
10880 		i40e_rebuild(pf, reinit, lock_acquired);
10881 }
10882 
10883 /**
10884  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
10885  * @pf: board private structure
10886  *
10887  * Close up the VFs and other things in prep for a Core Reset,
10888  * then get ready to rebuild the world.
10889  * @lock_acquired: indicates whether or not the lock has been acquired
10890  * before this function was called.
10891  **/
10892 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
10893 {
10894 	i40e_prep_for_reset(pf);
10895 	i40e_reset_and_rebuild(pf, false, lock_acquired);
10896 }
10897 
10898 /**
10899  * i40e_handle_mdd_event
10900  * @pf: pointer to the PF structure
10901  *
10902  * Called from the MDD irq handler to identify possibly malicious vfs
10903  **/
10904 static void i40e_handle_mdd_event(struct i40e_pf *pf)
10905 {
10906 	struct i40e_hw *hw = &pf->hw;
10907 	bool mdd_detected = false;
10908 	struct i40e_vf *vf;
10909 	u32 reg;
10910 	int i;
10911 
10912 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
10913 		return;
10914 
10915 	/* find what triggered the MDD event */
10916 	reg = rd32(hw, I40E_GL_MDET_TX);
10917 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
10918 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
10919 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
10920 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
10921 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
10922 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
10923 				I40E_GL_MDET_TX_EVENT_SHIFT;
10924 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
10925 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
10926 				pf->hw.func_caps.base_queue;
10927 		if (netif_msg_tx_err(pf))
10928 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
10929 				 event, queue, pf_num, vf_num);
10930 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
10931 		mdd_detected = true;
10932 	}
10933 	reg = rd32(hw, I40E_GL_MDET_RX);
10934 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
10935 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
10936 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
10937 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
10938 				I40E_GL_MDET_RX_EVENT_SHIFT;
10939 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
10940 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
10941 				pf->hw.func_caps.base_queue;
10942 		if (netif_msg_rx_err(pf))
10943 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
10944 				 event, queue, func);
10945 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
10946 		mdd_detected = true;
10947 	}
10948 
10949 	if (mdd_detected) {
10950 		reg = rd32(hw, I40E_PF_MDET_TX);
10951 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
10952 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
10953 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
10954 		}
10955 		reg = rd32(hw, I40E_PF_MDET_RX);
10956 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
10957 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
10958 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
10959 		}
10960 	}
10961 
10962 	/* see if one of the VFs needs its hand slapped */
10963 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
10964 		vf = &(pf->vf[i]);
10965 		reg = rd32(hw, I40E_VP_MDET_TX(i));
10966 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
10967 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
10968 			vf->num_mdd_events++;
10969 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
10970 				 i);
10971 			dev_info(&pf->pdev->dev,
10972 				 "Use PF Control I/F to re-enable the VF\n");
10973 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10974 		}
10975 
10976 		reg = rd32(hw, I40E_VP_MDET_RX(i));
10977 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
10978 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
10979 			vf->num_mdd_events++;
10980 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
10981 				 i);
10982 			dev_info(&pf->pdev->dev,
10983 				 "Use PF Control I/F to re-enable the VF\n");
10984 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10985 		}
10986 	}
10987 
10988 	/* re-enable mdd interrupt cause */
10989 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
10990 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
10991 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
10992 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
10993 	i40e_flush(hw);
10994 }
10995 
10996 /**
10997  * i40e_service_task - Run the driver's async subtasks
10998  * @work: pointer to work_struct containing our data
10999  **/
11000 static void i40e_service_task(struct work_struct *work)
11001 {
11002 	struct i40e_pf *pf = container_of(work,
11003 					  struct i40e_pf,
11004 					  service_task);
11005 	unsigned long start_time = jiffies;
11006 
11007 	/* don't bother with service tasks if a reset is in progress */
11008 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11009 	    test_bit(__I40E_SUSPENDED, pf->state))
11010 		return;
11011 
11012 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11013 		return;
11014 
11015 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11016 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11017 		i40e_sync_filters_subtask(pf);
11018 		i40e_reset_subtask(pf);
11019 		i40e_handle_mdd_event(pf);
11020 		i40e_vc_process_vflr_event(pf);
11021 		i40e_watchdog_subtask(pf);
11022 		i40e_fdir_reinit_subtask(pf);
11023 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11024 			/* Client subtask will reopen next time through. */
11025 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11026 							   true);
11027 		} else {
11028 			i40e_client_subtask(pf);
11029 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11030 					       pf->state))
11031 				i40e_notify_client_of_l2_param_changes(
11032 								pf->vsi[pf->lan_vsi]);
11033 		}
11034 		i40e_sync_filters_subtask(pf);
11035 	} else {
11036 		i40e_reset_subtask(pf);
11037 	}
11038 
11039 	i40e_clean_adminq_subtask(pf);
11040 
11041 	/* flush memory to make sure state is correct before next watchdog */
11042 	smp_mb__before_atomic();
11043 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11044 
11045 	/* If the tasks have taken longer than one timer cycle or there
11046 	 * is more work to be done, reschedule the service task now
11047 	 * rather than wait for the timer to tick again.
11048 	 */
11049 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11050 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11051 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11052 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11053 		i40e_service_event_schedule(pf);
11054 }
11055 
11056 /**
11057  * i40e_service_timer - timer callback
11058  * @t: timer list pointer
11059  **/
11060 static void i40e_service_timer(struct timer_list *t)
11061 {
11062 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11063 
11064 	mod_timer(&pf->service_timer,
11065 		  round_jiffies(jiffies + pf->service_timer_period));
11066 	i40e_service_event_schedule(pf);
11067 }
11068 
11069 /**
11070  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11071  * @vsi: the VSI being configured
11072  **/
11073 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11074 {
11075 	struct i40e_pf *pf = vsi->back;
11076 
11077 	switch (vsi->type) {
11078 	case I40E_VSI_MAIN:
11079 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11080 		if (!vsi->num_tx_desc)
11081 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11082 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11083 		if (!vsi->num_rx_desc)
11084 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11085 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11086 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11087 			vsi->num_q_vectors = pf->num_lan_msix;
11088 		else
11089 			vsi->num_q_vectors = 1;
11090 
11091 		break;
11092 
11093 	case I40E_VSI_FDIR:
11094 		vsi->alloc_queue_pairs = 1;
11095 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11096 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11097 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11098 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11099 		vsi->num_q_vectors = pf->num_fdsb_msix;
11100 		break;
11101 
11102 	case I40E_VSI_VMDQ2:
11103 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11104 		if (!vsi->num_tx_desc)
11105 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11106 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11107 		if (!vsi->num_rx_desc)
11108 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11109 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11110 		vsi->num_q_vectors = pf->num_vmdq_msix;
11111 		break;
11112 
11113 	case I40E_VSI_SRIOV:
11114 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11115 		if (!vsi->num_tx_desc)
11116 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11117 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11118 		if (!vsi->num_rx_desc)
11119 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11120 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11121 		break;
11122 
11123 	default:
11124 		WARN_ON(1);
11125 		return -ENODATA;
11126 	}
11127 
11128 	if (is_kdump_kernel()) {
11129 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11130 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11131 	}
11132 
11133 	return 0;
11134 }
11135 
11136 /**
11137  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11138  * @vsi: VSI pointer
11139  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11140  *
11141  * On error: returns error code (negative)
11142  * On success: returns 0
11143  **/
11144 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11145 {
11146 	struct i40e_ring **next_rings;
11147 	int size;
11148 	int ret = 0;
11149 
11150 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11151 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11152 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11153 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11154 	if (!vsi->tx_rings)
11155 		return -ENOMEM;
11156 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11157 	if (i40e_enabled_xdp_vsi(vsi)) {
11158 		vsi->xdp_rings = next_rings;
11159 		next_rings += vsi->alloc_queue_pairs;
11160 	}
11161 	vsi->rx_rings = next_rings;
11162 
11163 	if (alloc_qvectors) {
11164 		/* allocate memory for q_vector pointers */
11165 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11166 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11167 		if (!vsi->q_vectors) {
11168 			ret = -ENOMEM;
11169 			goto err_vectors;
11170 		}
11171 	}
11172 	return ret;
11173 
11174 err_vectors:
11175 	kfree(vsi->tx_rings);
11176 	return ret;
11177 }
11178 
11179 /**
11180  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11181  * @pf: board private structure
11182  * @type: type of VSI
11183  *
11184  * On error: returns error code (negative)
11185  * On success: returns vsi index in PF (positive)
11186  **/
11187 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11188 {
11189 	int ret = -ENODEV;
11190 	struct i40e_vsi *vsi;
11191 	int vsi_idx;
11192 	int i;
11193 
11194 	/* Need to protect the allocation of the VSIs at the PF level */
11195 	mutex_lock(&pf->switch_mutex);
11196 
11197 	/* VSI list may be fragmented if VSI creation/destruction has
11198 	 * been happening.  We can afford to do a quick scan to look
11199 	 * for any free VSIs in the list.
11200 	 *
11201 	 * find next empty vsi slot, looping back around if necessary
11202 	 */
11203 	i = pf->next_vsi;
11204 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11205 		i++;
11206 	if (i >= pf->num_alloc_vsi) {
11207 		i = 0;
11208 		while (i < pf->next_vsi && pf->vsi[i])
11209 			i++;
11210 	}
11211 
11212 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11213 		vsi_idx = i;             /* Found one! */
11214 	} else {
11215 		ret = -ENODEV;
11216 		goto unlock_pf;  /* out of VSI slots! */
11217 	}
11218 	pf->next_vsi = ++i;
11219 
11220 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11221 	if (!vsi) {
11222 		ret = -ENOMEM;
11223 		goto unlock_pf;
11224 	}
11225 	vsi->type = type;
11226 	vsi->back = pf;
11227 	set_bit(__I40E_VSI_DOWN, vsi->state);
11228 	vsi->flags = 0;
11229 	vsi->idx = vsi_idx;
11230 	vsi->int_rate_limit = 0;
11231 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11232 				pf->rss_table_size : 64;
11233 	vsi->netdev_registered = false;
11234 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11235 	hash_init(vsi->mac_filter_hash);
11236 	vsi->irqs_ready = false;
11237 
11238 	if (type == I40E_VSI_MAIN) {
11239 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11240 		if (!vsi->af_xdp_zc_qps)
11241 			goto err_rings;
11242 	}
11243 
11244 	ret = i40e_set_num_rings_in_vsi(vsi);
11245 	if (ret)
11246 		goto err_rings;
11247 
11248 	ret = i40e_vsi_alloc_arrays(vsi, true);
11249 	if (ret)
11250 		goto err_rings;
11251 
11252 	/* Setup default MSIX irq handler for VSI */
11253 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11254 
11255 	/* Initialize VSI lock */
11256 	spin_lock_init(&vsi->mac_filter_hash_lock);
11257 	pf->vsi[vsi_idx] = vsi;
11258 	ret = vsi_idx;
11259 	goto unlock_pf;
11260 
11261 err_rings:
11262 	bitmap_free(vsi->af_xdp_zc_qps);
11263 	pf->next_vsi = i - 1;
11264 	kfree(vsi);
11265 unlock_pf:
11266 	mutex_unlock(&pf->switch_mutex);
11267 	return ret;
11268 }
11269 
11270 /**
11271  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11272  * @vsi: VSI pointer
11273  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11274  *
11275  * On error: returns error code (negative)
11276  * On success: returns 0
11277  **/
11278 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11279 {
11280 	/* free the ring and vector containers */
11281 	if (free_qvectors) {
11282 		kfree(vsi->q_vectors);
11283 		vsi->q_vectors = NULL;
11284 	}
11285 	kfree(vsi->tx_rings);
11286 	vsi->tx_rings = NULL;
11287 	vsi->rx_rings = NULL;
11288 	vsi->xdp_rings = NULL;
11289 }
11290 
11291 /**
11292  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11293  * and lookup table
11294  * @vsi: Pointer to VSI structure
11295  */
11296 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11297 {
11298 	if (!vsi)
11299 		return;
11300 
11301 	kfree(vsi->rss_hkey_user);
11302 	vsi->rss_hkey_user = NULL;
11303 
11304 	kfree(vsi->rss_lut_user);
11305 	vsi->rss_lut_user = NULL;
11306 }
11307 
11308 /**
11309  * i40e_vsi_clear - Deallocate the VSI provided
11310  * @vsi: the VSI being un-configured
11311  **/
11312 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11313 {
11314 	struct i40e_pf *pf;
11315 
11316 	if (!vsi)
11317 		return 0;
11318 
11319 	if (!vsi->back)
11320 		goto free_vsi;
11321 	pf = vsi->back;
11322 
11323 	mutex_lock(&pf->switch_mutex);
11324 	if (!pf->vsi[vsi->idx]) {
11325 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11326 			vsi->idx, vsi->idx, vsi->type);
11327 		goto unlock_vsi;
11328 	}
11329 
11330 	if (pf->vsi[vsi->idx] != vsi) {
11331 		dev_err(&pf->pdev->dev,
11332 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11333 			pf->vsi[vsi->idx]->idx,
11334 			pf->vsi[vsi->idx]->type,
11335 			vsi->idx, vsi->type);
11336 		goto unlock_vsi;
11337 	}
11338 
11339 	/* updates the PF for this cleared vsi */
11340 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11341 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11342 
11343 	bitmap_free(vsi->af_xdp_zc_qps);
11344 	i40e_vsi_free_arrays(vsi, true);
11345 	i40e_clear_rss_config_user(vsi);
11346 
11347 	pf->vsi[vsi->idx] = NULL;
11348 	if (vsi->idx < pf->next_vsi)
11349 		pf->next_vsi = vsi->idx;
11350 
11351 unlock_vsi:
11352 	mutex_unlock(&pf->switch_mutex);
11353 free_vsi:
11354 	kfree(vsi);
11355 
11356 	return 0;
11357 }
11358 
11359 /**
11360  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11361  * @vsi: the VSI being cleaned
11362  **/
11363 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11364 {
11365 	int i;
11366 
11367 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11368 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11369 			kfree_rcu(vsi->tx_rings[i], rcu);
11370 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11371 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11372 			if (vsi->xdp_rings)
11373 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11374 		}
11375 	}
11376 }
11377 
11378 /**
11379  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11380  * @vsi: the VSI being configured
11381  **/
11382 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11383 {
11384 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11385 	struct i40e_pf *pf = vsi->back;
11386 	struct i40e_ring *ring;
11387 
11388 	/* Set basic values in the rings to be used later during open() */
11389 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11390 		/* allocate space for both Tx and Rx in one shot */
11391 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11392 		if (!ring)
11393 			goto err_out;
11394 
11395 		ring->queue_index = i;
11396 		ring->reg_idx = vsi->base_queue + i;
11397 		ring->ring_active = false;
11398 		ring->vsi = vsi;
11399 		ring->netdev = vsi->netdev;
11400 		ring->dev = &pf->pdev->dev;
11401 		ring->count = vsi->num_tx_desc;
11402 		ring->size = 0;
11403 		ring->dcb_tc = 0;
11404 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11405 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11406 		ring->itr_setting = pf->tx_itr_default;
11407 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11408 
11409 		if (!i40e_enabled_xdp_vsi(vsi))
11410 			goto setup_rx;
11411 
11412 		ring->queue_index = vsi->alloc_queue_pairs + i;
11413 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11414 		ring->ring_active = false;
11415 		ring->vsi = vsi;
11416 		ring->netdev = NULL;
11417 		ring->dev = &pf->pdev->dev;
11418 		ring->count = vsi->num_tx_desc;
11419 		ring->size = 0;
11420 		ring->dcb_tc = 0;
11421 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11422 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11423 		set_ring_xdp(ring);
11424 		ring->itr_setting = pf->tx_itr_default;
11425 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11426 
11427 setup_rx:
11428 		ring->queue_index = i;
11429 		ring->reg_idx = vsi->base_queue + i;
11430 		ring->ring_active = false;
11431 		ring->vsi = vsi;
11432 		ring->netdev = vsi->netdev;
11433 		ring->dev = &pf->pdev->dev;
11434 		ring->count = vsi->num_rx_desc;
11435 		ring->size = 0;
11436 		ring->dcb_tc = 0;
11437 		ring->itr_setting = pf->rx_itr_default;
11438 		WRITE_ONCE(vsi->rx_rings[i], ring);
11439 	}
11440 
11441 	return 0;
11442 
11443 err_out:
11444 	i40e_vsi_clear_rings(vsi);
11445 	return -ENOMEM;
11446 }
11447 
11448 /**
11449  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11450  * @pf: board private structure
11451  * @vectors: the number of MSI-X vectors to request
11452  *
11453  * Returns the number of vectors reserved, or error
11454  **/
11455 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11456 {
11457 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11458 					I40E_MIN_MSIX, vectors);
11459 	if (vectors < 0) {
11460 		dev_info(&pf->pdev->dev,
11461 			 "MSI-X vector reservation failed: %d\n", vectors);
11462 		vectors = 0;
11463 	}
11464 
11465 	return vectors;
11466 }
11467 
11468 /**
11469  * i40e_init_msix - Setup the MSIX capability
11470  * @pf: board private structure
11471  *
11472  * Work with the OS to set up the MSIX vectors needed.
11473  *
11474  * Returns the number of vectors reserved or negative on failure
11475  **/
11476 static int i40e_init_msix(struct i40e_pf *pf)
11477 {
11478 	struct i40e_hw *hw = &pf->hw;
11479 	int cpus, extra_vectors;
11480 	int vectors_left;
11481 	int v_budget, i;
11482 	int v_actual;
11483 	int iwarp_requested = 0;
11484 
11485 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11486 		return -ENODEV;
11487 
11488 	/* The number of vectors we'll request will be comprised of:
11489 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11490 	 *   - The number of LAN queue pairs
11491 	 *	- Queues being used for RSS.
11492 	 *		We don't need as many as max_rss_size vectors.
11493 	 *		use rss_size instead in the calculation since that
11494 	 *		is governed by number of cpus in the system.
11495 	 *	- assumes symmetric Tx/Rx pairing
11496 	 *   - The number of VMDq pairs
11497 	 *   - The CPU count within the NUMA node if iWARP is enabled
11498 	 * Once we count this up, try the request.
11499 	 *
11500 	 * If we can't get what we want, we'll simplify to nearly nothing
11501 	 * and try again.  If that still fails, we punt.
11502 	 */
11503 	vectors_left = hw->func_caps.num_msix_vectors;
11504 	v_budget = 0;
11505 
11506 	/* reserve one vector for miscellaneous handler */
11507 	if (vectors_left) {
11508 		v_budget++;
11509 		vectors_left--;
11510 	}
11511 
11512 	/* reserve some vectors for the main PF traffic queues. Initially we
11513 	 * only reserve at most 50% of the available vectors, in the case that
11514 	 * the number of online CPUs is large. This ensures that we can enable
11515 	 * extra features as well. Once we've enabled the other features, we
11516 	 * will use any remaining vectors to reach as close as we can to the
11517 	 * number of online CPUs.
11518 	 */
11519 	cpus = num_online_cpus();
11520 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11521 	vectors_left -= pf->num_lan_msix;
11522 
11523 	/* reserve one vector for sideband flow director */
11524 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11525 		if (vectors_left) {
11526 			pf->num_fdsb_msix = 1;
11527 			v_budget++;
11528 			vectors_left--;
11529 		} else {
11530 			pf->num_fdsb_msix = 0;
11531 		}
11532 	}
11533 
11534 	/* can we reserve enough for iWARP? */
11535 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11536 		iwarp_requested = pf->num_iwarp_msix;
11537 
11538 		if (!vectors_left)
11539 			pf->num_iwarp_msix = 0;
11540 		else if (vectors_left < pf->num_iwarp_msix)
11541 			pf->num_iwarp_msix = 1;
11542 		v_budget += pf->num_iwarp_msix;
11543 		vectors_left -= pf->num_iwarp_msix;
11544 	}
11545 
11546 	/* any vectors left over go for VMDq support */
11547 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11548 		if (!vectors_left) {
11549 			pf->num_vmdq_msix = 0;
11550 			pf->num_vmdq_qps = 0;
11551 		} else {
11552 			int vmdq_vecs_wanted =
11553 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11554 			int vmdq_vecs =
11555 				min_t(int, vectors_left, vmdq_vecs_wanted);
11556 
11557 			/* if we're short on vectors for what's desired, we limit
11558 			 * the queues per vmdq.  If this is still more than are
11559 			 * available, the user will need to change the number of
11560 			 * queues/vectors used by the PF later with the ethtool
11561 			 * channels command
11562 			 */
11563 			if (vectors_left < vmdq_vecs_wanted) {
11564 				pf->num_vmdq_qps = 1;
11565 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11566 				vmdq_vecs = min_t(int,
11567 						  vectors_left,
11568 						  vmdq_vecs_wanted);
11569 			}
11570 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11571 
11572 			v_budget += vmdq_vecs;
11573 			vectors_left -= vmdq_vecs;
11574 		}
11575 	}
11576 
11577 	/* On systems with a large number of SMP cores, we previously limited
11578 	 * the number of vectors for num_lan_msix to be at most 50% of the
11579 	 * available vectors, to allow for other features. Now, we add back
11580 	 * the remaining vectors. However, we ensure that the total
11581 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11582 	 * calculate the number of vectors we can add without going over the
11583 	 * cap of CPUs. For systems with a small number of CPUs this will be
11584 	 * zero.
11585 	 */
11586 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11587 	pf->num_lan_msix += extra_vectors;
11588 	vectors_left -= extra_vectors;
11589 
11590 	WARN(vectors_left < 0,
11591 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11592 
11593 	v_budget += pf->num_lan_msix;
11594 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11595 				   GFP_KERNEL);
11596 	if (!pf->msix_entries)
11597 		return -ENOMEM;
11598 
11599 	for (i = 0; i < v_budget; i++)
11600 		pf->msix_entries[i].entry = i;
11601 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11602 
11603 	if (v_actual < I40E_MIN_MSIX) {
11604 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11605 		kfree(pf->msix_entries);
11606 		pf->msix_entries = NULL;
11607 		pci_disable_msix(pf->pdev);
11608 		return -ENODEV;
11609 
11610 	} else if (v_actual == I40E_MIN_MSIX) {
11611 		/* Adjust for minimal MSIX use */
11612 		pf->num_vmdq_vsis = 0;
11613 		pf->num_vmdq_qps = 0;
11614 		pf->num_lan_qps = 1;
11615 		pf->num_lan_msix = 1;
11616 
11617 	} else if (v_actual != v_budget) {
11618 		/* If we have limited resources, we will start with no vectors
11619 		 * for the special features and then allocate vectors to some
11620 		 * of these features based on the policy and at the end disable
11621 		 * the features that did not get any vectors.
11622 		 */
11623 		int vec;
11624 
11625 		dev_info(&pf->pdev->dev,
11626 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11627 			 v_actual, v_budget);
11628 		/* reserve the misc vector */
11629 		vec = v_actual - 1;
11630 
11631 		/* Scale vector usage down */
11632 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11633 		pf->num_vmdq_vsis = 1;
11634 		pf->num_vmdq_qps = 1;
11635 
11636 		/* partition out the remaining vectors */
11637 		switch (vec) {
11638 		case 2:
11639 			pf->num_lan_msix = 1;
11640 			break;
11641 		case 3:
11642 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11643 				pf->num_lan_msix = 1;
11644 				pf->num_iwarp_msix = 1;
11645 			} else {
11646 				pf->num_lan_msix = 2;
11647 			}
11648 			break;
11649 		default:
11650 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11651 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11652 						 iwarp_requested);
11653 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11654 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11655 			} else {
11656 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11657 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11658 			}
11659 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11660 				pf->num_fdsb_msix = 1;
11661 				vec--;
11662 			}
11663 			pf->num_lan_msix = min_t(int,
11664 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11665 							      pf->num_lan_msix);
11666 			pf->num_lan_qps = pf->num_lan_msix;
11667 			break;
11668 		}
11669 	}
11670 
11671 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11672 	    (pf->num_fdsb_msix == 0)) {
11673 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11674 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11675 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11676 	}
11677 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11678 	    (pf->num_vmdq_msix == 0)) {
11679 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11680 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11681 	}
11682 
11683 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11684 	    (pf->num_iwarp_msix == 0)) {
11685 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11686 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11687 	}
11688 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11689 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11690 		   pf->num_lan_msix,
11691 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11692 		   pf->num_fdsb_msix,
11693 		   pf->num_iwarp_msix);
11694 
11695 	return v_actual;
11696 }
11697 
11698 /**
11699  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11700  * @vsi: the VSI being configured
11701  * @v_idx: index of the vector in the vsi struct
11702  *
11703  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11704  **/
11705 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11706 {
11707 	struct i40e_q_vector *q_vector;
11708 
11709 	/* allocate q_vector */
11710 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11711 	if (!q_vector)
11712 		return -ENOMEM;
11713 
11714 	q_vector->vsi = vsi;
11715 	q_vector->v_idx = v_idx;
11716 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11717 
11718 	if (vsi->netdev)
11719 		netif_napi_add(vsi->netdev, &q_vector->napi,
11720 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
11721 
11722 	/* tie q_vector and vsi together */
11723 	vsi->q_vectors[v_idx] = q_vector;
11724 
11725 	return 0;
11726 }
11727 
11728 /**
11729  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11730  * @vsi: the VSI being configured
11731  *
11732  * We allocate one q_vector per queue interrupt.  If allocation fails we
11733  * return -ENOMEM.
11734  **/
11735 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11736 {
11737 	struct i40e_pf *pf = vsi->back;
11738 	int err, v_idx, num_q_vectors;
11739 
11740 	/* if not MSIX, give the one vector only to the LAN VSI */
11741 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11742 		num_q_vectors = vsi->num_q_vectors;
11743 	else if (vsi == pf->vsi[pf->lan_vsi])
11744 		num_q_vectors = 1;
11745 	else
11746 		return -EINVAL;
11747 
11748 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11749 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11750 		if (err)
11751 			goto err_out;
11752 	}
11753 
11754 	return 0;
11755 
11756 err_out:
11757 	while (v_idx--)
11758 		i40e_free_q_vector(vsi, v_idx);
11759 
11760 	return err;
11761 }
11762 
11763 /**
11764  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11765  * @pf: board private structure to initialize
11766  **/
11767 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11768 {
11769 	int vectors = 0;
11770 	ssize_t size;
11771 
11772 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11773 		vectors = i40e_init_msix(pf);
11774 		if (vectors < 0) {
11775 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11776 				       I40E_FLAG_IWARP_ENABLED	|
11777 				       I40E_FLAG_RSS_ENABLED	|
11778 				       I40E_FLAG_DCB_CAPABLE	|
11779 				       I40E_FLAG_DCB_ENABLED	|
11780 				       I40E_FLAG_SRIOV_ENABLED	|
11781 				       I40E_FLAG_FD_SB_ENABLED	|
11782 				       I40E_FLAG_FD_ATR_ENABLED	|
11783 				       I40E_FLAG_VMDQ_ENABLED);
11784 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11785 
11786 			/* rework the queue expectations without MSIX */
11787 			i40e_determine_queue_usage(pf);
11788 		}
11789 	}
11790 
11791 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11792 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
11793 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
11794 		vectors = pci_enable_msi(pf->pdev);
11795 		if (vectors < 0) {
11796 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
11797 				 vectors);
11798 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
11799 		}
11800 		vectors = 1;  /* one MSI or Legacy vector */
11801 	}
11802 
11803 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
11804 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
11805 
11806 	/* set up vector assignment tracking */
11807 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
11808 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
11809 	if (!pf->irq_pile)
11810 		return -ENOMEM;
11811 
11812 	pf->irq_pile->num_entries = vectors;
11813 
11814 	/* track first vector for misc interrupts, ignore return */
11815 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
11816 
11817 	return 0;
11818 }
11819 
11820 /**
11821  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
11822  * @pf: private board data structure
11823  *
11824  * Restore the interrupt scheme that was cleared when we suspended the
11825  * device. This should be called during resume to re-allocate the q_vectors
11826  * and reacquire IRQs.
11827  */
11828 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
11829 {
11830 	int err, i;
11831 
11832 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
11833 	 * scheme. We need to re-enabled them here in order to attempt to
11834 	 * re-acquire the MSI or MSI-X vectors
11835 	 */
11836 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
11837 
11838 	err = i40e_init_interrupt_scheme(pf);
11839 	if (err)
11840 		return err;
11841 
11842 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
11843 	 * rings together again.
11844 	 */
11845 	for (i = 0; i < pf->num_alloc_vsi; i++) {
11846 		if (pf->vsi[i]) {
11847 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
11848 			if (err)
11849 				goto err_unwind;
11850 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
11851 		}
11852 	}
11853 
11854 	err = i40e_setup_misc_vector(pf);
11855 	if (err)
11856 		goto err_unwind;
11857 
11858 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
11859 		i40e_client_update_msix_info(pf);
11860 
11861 	return 0;
11862 
11863 err_unwind:
11864 	while (i--) {
11865 		if (pf->vsi[i])
11866 			i40e_vsi_free_q_vectors(pf->vsi[i]);
11867 	}
11868 
11869 	return err;
11870 }
11871 
11872 /**
11873  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
11874  * non queue events in recovery mode
11875  * @pf: board private structure
11876  *
11877  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
11878  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
11879  * This is handled differently than in recovery mode since no Tx/Rx resources
11880  * are being allocated.
11881  **/
11882 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
11883 {
11884 	int err;
11885 
11886 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11887 		err = i40e_setup_misc_vector(pf);
11888 
11889 		if (err) {
11890 			dev_info(&pf->pdev->dev,
11891 				 "MSI-X misc vector request failed, error %d\n",
11892 				 err);
11893 			return err;
11894 		}
11895 	} else {
11896 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
11897 
11898 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
11899 				  pf->int_name, pf);
11900 
11901 		if (err) {
11902 			dev_info(&pf->pdev->dev,
11903 				 "MSI/legacy misc vector request failed, error %d\n",
11904 				 err);
11905 			return err;
11906 		}
11907 		i40e_enable_misc_int_causes(pf);
11908 		i40e_irq_dynamic_enable_icr0(pf);
11909 	}
11910 
11911 	return 0;
11912 }
11913 
11914 /**
11915  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
11916  * @pf: board private structure
11917  *
11918  * This sets up the handler for MSIX 0, which is used to manage the
11919  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
11920  * when in MSI or Legacy interrupt mode.
11921  **/
11922 static int i40e_setup_misc_vector(struct i40e_pf *pf)
11923 {
11924 	struct i40e_hw *hw = &pf->hw;
11925 	int err = 0;
11926 
11927 	/* Only request the IRQ once, the first time through. */
11928 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
11929 		err = request_irq(pf->msix_entries[0].vector,
11930 				  i40e_intr, 0, pf->int_name, pf);
11931 		if (err) {
11932 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
11933 			dev_info(&pf->pdev->dev,
11934 				 "request_irq for %s failed: %d\n",
11935 				 pf->int_name, err);
11936 			return -EFAULT;
11937 		}
11938 	}
11939 
11940 	i40e_enable_misc_int_causes(pf);
11941 
11942 	/* associate no queues to the misc vector */
11943 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
11944 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
11945 
11946 	i40e_flush(hw);
11947 
11948 	i40e_irq_dynamic_enable_icr0(pf);
11949 
11950 	return err;
11951 }
11952 
11953 /**
11954  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
11955  * @vsi: Pointer to vsi structure
11956  * @seed: Buffter to store the hash keys
11957  * @lut: Buffer to store the lookup table entries
11958  * @lut_size: Size of buffer to store the lookup table entries
11959  *
11960  * Return 0 on success, negative on failure
11961  */
11962 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
11963 			   u8 *lut, u16 lut_size)
11964 {
11965 	struct i40e_pf *pf = vsi->back;
11966 	struct i40e_hw *hw = &pf->hw;
11967 	int ret = 0;
11968 
11969 	if (seed) {
11970 		ret = i40e_aq_get_rss_key(hw, vsi->id,
11971 			(struct i40e_aqc_get_set_rss_key_data *)seed);
11972 		if (ret) {
11973 			dev_info(&pf->pdev->dev,
11974 				 "Cannot get RSS key, err %s aq_err %s\n",
11975 				 i40e_stat_str(&pf->hw, ret),
11976 				 i40e_aq_str(&pf->hw,
11977 					     pf->hw.aq.asq_last_status));
11978 			return ret;
11979 		}
11980 	}
11981 
11982 	if (lut) {
11983 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
11984 
11985 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
11986 		if (ret) {
11987 			dev_info(&pf->pdev->dev,
11988 				 "Cannot get RSS lut, err %s aq_err %s\n",
11989 				 i40e_stat_str(&pf->hw, ret),
11990 				 i40e_aq_str(&pf->hw,
11991 					     pf->hw.aq.asq_last_status));
11992 			return ret;
11993 		}
11994 	}
11995 
11996 	return ret;
11997 }
11998 
11999 /**
12000  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12001  * @vsi: Pointer to vsi structure
12002  * @seed: RSS hash seed
12003  * @lut: Lookup table
12004  * @lut_size: Lookup table size
12005  *
12006  * Returns 0 on success, negative on failure
12007  **/
12008 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12009 			       const u8 *lut, u16 lut_size)
12010 {
12011 	struct i40e_pf *pf = vsi->back;
12012 	struct i40e_hw *hw = &pf->hw;
12013 	u16 vf_id = vsi->vf_id;
12014 	u8 i;
12015 
12016 	/* Fill out hash function seed */
12017 	if (seed) {
12018 		u32 *seed_dw = (u32 *)seed;
12019 
12020 		if (vsi->type == I40E_VSI_MAIN) {
12021 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12022 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12023 		} else if (vsi->type == I40E_VSI_SRIOV) {
12024 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12025 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12026 		} else {
12027 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12028 		}
12029 	}
12030 
12031 	if (lut) {
12032 		u32 *lut_dw = (u32 *)lut;
12033 
12034 		if (vsi->type == I40E_VSI_MAIN) {
12035 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12036 				return -EINVAL;
12037 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12038 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12039 		} else if (vsi->type == I40E_VSI_SRIOV) {
12040 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12041 				return -EINVAL;
12042 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12043 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12044 		} else {
12045 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12046 		}
12047 	}
12048 	i40e_flush(hw);
12049 
12050 	return 0;
12051 }
12052 
12053 /**
12054  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12055  * @vsi: Pointer to VSI structure
12056  * @seed: Buffer to store the keys
12057  * @lut: Buffer to store the lookup table entries
12058  * @lut_size: Size of buffer to store the lookup table entries
12059  *
12060  * Returns 0 on success, negative on failure
12061  */
12062 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12063 			    u8 *lut, u16 lut_size)
12064 {
12065 	struct i40e_pf *pf = vsi->back;
12066 	struct i40e_hw *hw = &pf->hw;
12067 	u16 i;
12068 
12069 	if (seed) {
12070 		u32 *seed_dw = (u32 *)seed;
12071 
12072 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12073 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12074 	}
12075 	if (lut) {
12076 		u32 *lut_dw = (u32 *)lut;
12077 
12078 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12079 			return -EINVAL;
12080 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12081 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12082 	}
12083 
12084 	return 0;
12085 }
12086 
12087 /**
12088  * i40e_config_rss - Configure RSS keys and lut
12089  * @vsi: Pointer to VSI structure
12090  * @seed: RSS hash seed
12091  * @lut: Lookup table
12092  * @lut_size: Lookup table size
12093  *
12094  * Returns 0 on success, negative on failure
12095  */
12096 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12097 {
12098 	struct i40e_pf *pf = vsi->back;
12099 
12100 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12101 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12102 	else
12103 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12104 }
12105 
12106 /**
12107  * i40e_get_rss - Get RSS keys and lut
12108  * @vsi: Pointer to VSI structure
12109  * @seed: Buffer to store the keys
12110  * @lut: Buffer to store the lookup table entries
12111  * @lut_size: Size of buffer to store the lookup table entries
12112  *
12113  * Returns 0 on success, negative on failure
12114  */
12115 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12116 {
12117 	struct i40e_pf *pf = vsi->back;
12118 
12119 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12120 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12121 	else
12122 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12123 }
12124 
12125 /**
12126  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12127  * @pf: Pointer to board private structure
12128  * @lut: Lookup table
12129  * @rss_table_size: Lookup table size
12130  * @rss_size: Range of queue number for hashing
12131  */
12132 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12133 		       u16 rss_table_size, u16 rss_size)
12134 {
12135 	u16 i;
12136 
12137 	for (i = 0; i < rss_table_size; i++)
12138 		lut[i] = i % rss_size;
12139 }
12140 
12141 /**
12142  * i40e_pf_config_rss - Prepare for RSS if used
12143  * @pf: board private structure
12144  **/
12145 static int i40e_pf_config_rss(struct i40e_pf *pf)
12146 {
12147 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12148 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12149 	u8 *lut;
12150 	struct i40e_hw *hw = &pf->hw;
12151 	u32 reg_val;
12152 	u64 hena;
12153 	int ret;
12154 
12155 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12156 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12157 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12158 	hena |= i40e_pf_get_default_rss_hena(pf);
12159 
12160 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12161 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12162 
12163 	/* Determine the RSS table size based on the hardware capabilities */
12164 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12165 	reg_val = (pf->rss_table_size == 512) ?
12166 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12167 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12168 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12169 
12170 	/* Determine the RSS size of the VSI */
12171 	if (!vsi->rss_size) {
12172 		u16 qcount;
12173 		/* If the firmware does something weird during VSI init, we
12174 		 * could end up with zero TCs. Check for that to avoid
12175 		 * divide-by-zero. It probably won't pass traffic, but it also
12176 		 * won't panic.
12177 		 */
12178 		qcount = vsi->num_queue_pairs /
12179 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12180 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12181 	}
12182 	if (!vsi->rss_size)
12183 		return -EINVAL;
12184 
12185 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12186 	if (!lut)
12187 		return -ENOMEM;
12188 
12189 	/* Use user configured lut if there is one, otherwise use default */
12190 	if (vsi->rss_lut_user)
12191 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12192 	else
12193 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12194 
12195 	/* Use user configured hash key if there is one, otherwise
12196 	 * use default.
12197 	 */
12198 	if (vsi->rss_hkey_user)
12199 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12200 	else
12201 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12202 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12203 	kfree(lut);
12204 
12205 	return ret;
12206 }
12207 
12208 /**
12209  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12210  * @pf: board private structure
12211  * @queue_count: the requested queue count for rss.
12212  *
12213  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12214  * count which may be different from the requested queue count.
12215  * Note: expects to be called while under rtnl_lock()
12216  **/
12217 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12218 {
12219 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12220 	int new_rss_size;
12221 
12222 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12223 		return 0;
12224 
12225 	queue_count = min_t(int, queue_count, num_online_cpus());
12226 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12227 
12228 	if (queue_count != vsi->num_queue_pairs) {
12229 		u16 qcount;
12230 
12231 		vsi->req_queue_pairs = queue_count;
12232 		i40e_prep_for_reset(pf);
12233 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12234 			return pf->alloc_rss_size;
12235 
12236 		pf->alloc_rss_size = new_rss_size;
12237 
12238 		i40e_reset_and_rebuild(pf, true, true);
12239 
12240 		/* Discard the user configured hash keys and lut, if less
12241 		 * queues are enabled.
12242 		 */
12243 		if (queue_count < vsi->rss_size) {
12244 			i40e_clear_rss_config_user(vsi);
12245 			dev_dbg(&pf->pdev->dev,
12246 				"discard user configured hash keys and lut\n");
12247 		}
12248 
12249 		/* Reset vsi->rss_size, as number of enabled queues changed */
12250 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12251 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12252 
12253 		i40e_pf_config_rss(pf);
12254 	}
12255 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12256 		 vsi->req_queue_pairs, pf->rss_size_max);
12257 	return pf->alloc_rss_size;
12258 }
12259 
12260 /**
12261  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12262  * @pf: board private structure
12263  **/
12264 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
12265 {
12266 	i40e_status status;
12267 	bool min_valid, max_valid;
12268 	u32 max_bw, min_bw;
12269 
12270 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12271 					   &min_valid, &max_valid);
12272 
12273 	if (!status) {
12274 		if (min_valid)
12275 			pf->min_bw = min_bw;
12276 		if (max_valid)
12277 			pf->max_bw = max_bw;
12278 	}
12279 
12280 	return status;
12281 }
12282 
12283 /**
12284  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12285  * @pf: board private structure
12286  **/
12287 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
12288 {
12289 	struct i40e_aqc_configure_partition_bw_data bw_data;
12290 	i40e_status status;
12291 
12292 	memset(&bw_data, 0, sizeof(bw_data));
12293 
12294 	/* Set the valid bit for this PF */
12295 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12296 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12297 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12298 
12299 	/* Set the new bandwidths */
12300 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12301 
12302 	return status;
12303 }
12304 
12305 /**
12306  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12307  * @pf: board private structure
12308  **/
12309 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12310 {
12311 	/* Commit temporary BW setting to permanent NVM image */
12312 	enum i40e_admin_queue_err last_aq_status;
12313 	i40e_status ret;
12314 	u16 nvm_word;
12315 
12316 	if (pf->hw.partition_id != 1) {
12317 		dev_info(&pf->pdev->dev,
12318 			 "Commit BW only works on partition 1! This is partition %d",
12319 			 pf->hw.partition_id);
12320 		ret = I40E_NOT_SUPPORTED;
12321 		goto bw_commit_out;
12322 	}
12323 
12324 	/* Acquire NVM for read access */
12325 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12326 	last_aq_status = pf->hw.aq.asq_last_status;
12327 	if (ret) {
12328 		dev_info(&pf->pdev->dev,
12329 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
12330 			 i40e_stat_str(&pf->hw, ret),
12331 			 i40e_aq_str(&pf->hw, last_aq_status));
12332 		goto bw_commit_out;
12333 	}
12334 
12335 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12336 	ret = i40e_aq_read_nvm(&pf->hw,
12337 			       I40E_SR_NVM_CONTROL_WORD,
12338 			       0x10, sizeof(nvm_word), &nvm_word,
12339 			       false, NULL);
12340 	/* Save off last admin queue command status before releasing
12341 	 * the NVM
12342 	 */
12343 	last_aq_status = pf->hw.aq.asq_last_status;
12344 	i40e_release_nvm(&pf->hw);
12345 	if (ret) {
12346 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
12347 			 i40e_stat_str(&pf->hw, ret),
12348 			 i40e_aq_str(&pf->hw, last_aq_status));
12349 		goto bw_commit_out;
12350 	}
12351 
12352 	/* Wait a bit for NVM release to complete */
12353 	msleep(50);
12354 
12355 	/* Acquire NVM for write access */
12356 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12357 	last_aq_status = pf->hw.aq.asq_last_status;
12358 	if (ret) {
12359 		dev_info(&pf->pdev->dev,
12360 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
12361 			 i40e_stat_str(&pf->hw, ret),
12362 			 i40e_aq_str(&pf->hw, last_aq_status));
12363 		goto bw_commit_out;
12364 	}
12365 	/* Write it back out unchanged to initiate update NVM,
12366 	 * which will force a write of the shadow (alt) RAM to
12367 	 * the NVM - thus storing the bandwidth values permanently.
12368 	 */
12369 	ret = i40e_aq_update_nvm(&pf->hw,
12370 				 I40E_SR_NVM_CONTROL_WORD,
12371 				 0x10, sizeof(nvm_word),
12372 				 &nvm_word, true, 0, NULL);
12373 	/* Save off last admin queue command status before releasing
12374 	 * the NVM
12375 	 */
12376 	last_aq_status = pf->hw.aq.asq_last_status;
12377 	i40e_release_nvm(&pf->hw);
12378 	if (ret)
12379 		dev_info(&pf->pdev->dev,
12380 			 "BW settings NOT SAVED, err %s aq_err %s\n",
12381 			 i40e_stat_str(&pf->hw, ret),
12382 			 i40e_aq_str(&pf->hw, last_aq_status));
12383 bw_commit_out:
12384 
12385 	return ret;
12386 }
12387 
12388 /**
12389  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12390  * if total port shutdown feature is enabled for this PF
12391  * @pf: board private structure
12392  **/
12393 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12394 {
12395 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12396 #define I40E_FEATURES_ENABLE_PTR		0x2A
12397 #define I40E_CURRENT_SETTING_PTR		0x2B
12398 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12399 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12400 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12401 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12402 	i40e_status read_status = I40E_SUCCESS;
12403 	u16 sr_emp_sr_settings_ptr = 0;
12404 	u16 features_enable = 0;
12405 	u16 link_behavior = 0;
12406 	bool ret = false;
12407 
12408 	read_status = i40e_read_nvm_word(&pf->hw,
12409 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12410 					 &sr_emp_sr_settings_ptr);
12411 	if (read_status)
12412 		goto err_nvm;
12413 	read_status = i40e_read_nvm_word(&pf->hw,
12414 					 sr_emp_sr_settings_ptr +
12415 					 I40E_FEATURES_ENABLE_PTR,
12416 					 &features_enable);
12417 	if (read_status)
12418 		goto err_nvm;
12419 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12420 		read_status = i40e_read_nvm_module_data(&pf->hw,
12421 							I40E_SR_EMP_SR_SETTINGS_PTR,
12422 							I40E_CURRENT_SETTING_PTR,
12423 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12424 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12425 							&link_behavior);
12426 		if (read_status)
12427 			goto err_nvm;
12428 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12429 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12430 	}
12431 	return ret;
12432 
12433 err_nvm:
12434 	dev_warn(&pf->pdev->dev,
12435 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
12436 		 i40e_stat_str(&pf->hw, read_status));
12437 	return ret;
12438 }
12439 
12440 /**
12441  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12442  * @pf: board private structure to initialize
12443  *
12444  * i40e_sw_init initializes the Adapter private data structure.
12445  * Fields are initialized based on PCI device information and
12446  * OS network device settings (MTU size).
12447  **/
12448 static int i40e_sw_init(struct i40e_pf *pf)
12449 {
12450 	int err = 0;
12451 	int size;
12452 	u16 pow;
12453 
12454 	/* Set default capability flags */
12455 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12456 		    I40E_FLAG_MSI_ENABLED     |
12457 		    I40E_FLAG_MSIX_ENABLED;
12458 
12459 	/* Set default ITR */
12460 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12461 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12462 
12463 	/* Depending on PF configurations, it is possible that the RSS
12464 	 * maximum might end up larger than the available queues
12465 	 */
12466 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12467 	pf->alloc_rss_size = 1;
12468 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12469 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12470 				 pf->hw.func_caps.num_tx_qp);
12471 
12472 	/* find the next higher power-of-2 of num cpus */
12473 	pow = roundup_pow_of_two(num_online_cpus());
12474 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12475 
12476 	if (pf->hw.func_caps.rss) {
12477 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12478 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12479 					   num_online_cpus());
12480 	}
12481 
12482 	/* MFP mode enabled */
12483 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12484 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12485 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12486 		if (i40e_get_partition_bw_setting(pf)) {
12487 			dev_warn(&pf->pdev->dev,
12488 				 "Could not get partition bw settings\n");
12489 		} else {
12490 			dev_info(&pf->pdev->dev,
12491 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12492 				 pf->min_bw, pf->max_bw);
12493 
12494 			/* nudge the Tx scheduler */
12495 			i40e_set_partition_bw_setting(pf);
12496 		}
12497 	}
12498 
12499 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12500 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12501 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12502 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12503 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12504 		    pf->hw.num_partitions > 1)
12505 			dev_info(&pf->pdev->dev,
12506 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12507 		else
12508 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12509 		pf->fdir_pf_filter_count =
12510 				 pf->hw.func_caps.fd_filters_guaranteed;
12511 		pf->hw.fdir_shared_filter_count =
12512 				 pf->hw.func_caps.fd_filters_best_effort;
12513 	}
12514 
12515 	if (pf->hw.mac.type == I40E_MAC_X722) {
12516 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12517 				    I40E_HW_128_QP_RSS_CAPABLE |
12518 				    I40E_HW_ATR_EVICT_CAPABLE |
12519 				    I40E_HW_WB_ON_ITR_CAPABLE |
12520 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12521 				    I40E_HW_NO_PCI_LINK_CHECK |
12522 				    I40E_HW_USE_SET_LLDP_MIB |
12523 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12524 				    I40E_HW_PTP_L4_CAPABLE |
12525 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12526 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12527 
12528 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12529 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12530 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12531 			dev_warn(&pf->pdev->dev,
12532 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12533 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12534 		}
12535 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12536 		   ((pf->hw.aq.api_maj_ver == 1) &&
12537 		    (pf->hw.aq.api_min_ver > 4))) {
12538 		/* Supported in FW API version higher than 1.4 */
12539 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12540 	}
12541 
12542 	/* Enable HW ATR eviction if possible */
12543 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12544 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12545 
12546 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12547 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12548 	    (pf->hw.aq.fw_maj_ver < 4))) {
12549 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12550 		/* No DCB support  for FW < v4.33 */
12551 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12552 	}
12553 
12554 	/* Disable FW LLDP if FW < v4.3 */
12555 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12556 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12557 	    (pf->hw.aq.fw_maj_ver < 4)))
12558 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12559 
12560 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12561 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12562 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12563 	    (pf->hw.aq.fw_maj_ver >= 5)))
12564 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12565 
12566 	/* Enable PTP L4 if FW > v6.0 */
12567 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12568 	    pf->hw.aq.fw_maj_ver >= 6)
12569 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12570 
12571 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12572 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12573 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12574 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12575 	}
12576 
12577 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12578 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12579 		/* IWARP needs one extra vector for CQP just like MISC.*/
12580 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12581 	}
12582 	/* Stopping FW LLDP engine is supported on XL710 and X722
12583 	 * starting from FW versions determined in i40e_init_adminq.
12584 	 * Stopping the FW LLDP engine is not supported on XL710
12585 	 * if NPAR is functioning so unset this hw flag in this case.
12586 	 */
12587 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12588 	    pf->hw.func_caps.npar_enable &&
12589 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12590 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12591 
12592 #ifdef CONFIG_PCI_IOV
12593 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12594 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12595 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12596 		pf->num_req_vfs = min_t(int,
12597 					pf->hw.func_caps.num_vfs,
12598 					I40E_MAX_VF_COUNT);
12599 	}
12600 #endif /* CONFIG_PCI_IOV */
12601 	pf->eeprom_version = 0xDEAD;
12602 	pf->lan_veb = I40E_NO_VEB;
12603 	pf->lan_vsi = I40E_NO_VSI;
12604 
12605 	/* By default FW has this off for performance reasons */
12606 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12607 
12608 	/* set up queue assignment tracking */
12609 	size = sizeof(struct i40e_lump_tracking)
12610 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12611 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12612 	if (!pf->qp_pile) {
12613 		err = -ENOMEM;
12614 		goto sw_init_done;
12615 	}
12616 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12617 
12618 	pf->tx_timeout_recovery_level = 1;
12619 
12620 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12621 	    i40e_is_total_port_shutdown_enabled(pf)) {
12622 		/* Link down on close must be on when total port shutdown
12623 		 * is enabled for a given port
12624 		 */
12625 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12626 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12627 		dev_info(&pf->pdev->dev,
12628 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12629 	}
12630 	mutex_init(&pf->switch_mutex);
12631 
12632 sw_init_done:
12633 	return err;
12634 }
12635 
12636 /**
12637  * i40e_set_ntuple - set the ntuple feature flag and take action
12638  * @pf: board private structure to initialize
12639  * @features: the feature set that the stack is suggesting
12640  *
12641  * returns a bool to indicate if reset needs to happen
12642  **/
12643 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12644 {
12645 	bool need_reset = false;
12646 
12647 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12648 	 * the state changed, we need to reset.
12649 	 */
12650 	if (features & NETIF_F_NTUPLE) {
12651 		/* Enable filters and mark for reset */
12652 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12653 			need_reset = true;
12654 		/* enable FD_SB only if there is MSI-X vector and no cloud
12655 		 * filters exist
12656 		 */
12657 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12658 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12659 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12660 		}
12661 	} else {
12662 		/* turn off filters, mark for reset and clear SW filter list */
12663 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12664 			need_reset = true;
12665 			i40e_fdir_filter_exit(pf);
12666 		}
12667 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12668 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12669 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12670 
12671 		/* reset fd counters */
12672 		pf->fd_add_err = 0;
12673 		pf->fd_atr_cnt = 0;
12674 		/* if ATR was auto disabled it can be re-enabled. */
12675 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12676 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12677 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12678 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12679 	}
12680 	return need_reset;
12681 }
12682 
12683 /**
12684  * i40e_clear_rss_lut - clear the rx hash lookup table
12685  * @vsi: the VSI being configured
12686  **/
12687 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12688 {
12689 	struct i40e_pf *pf = vsi->back;
12690 	struct i40e_hw *hw = &pf->hw;
12691 	u16 vf_id = vsi->vf_id;
12692 	u8 i;
12693 
12694 	if (vsi->type == I40E_VSI_MAIN) {
12695 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12696 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12697 	} else if (vsi->type == I40E_VSI_SRIOV) {
12698 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12699 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12700 	} else {
12701 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12702 	}
12703 }
12704 
12705 /**
12706  * i40e_set_features - set the netdev feature flags
12707  * @netdev: ptr to the netdev being adjusted
12708  * @features: the feature set that the stack is suggesting
12709  * Note: expects to be called while under rtnl_lock()
12710  **/
12711 static int i40e_set_features(struct net_device *netdev,
12712 			     netdev_features_t features)
12713 {
12714 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12715 	struct i40e_vsi *vsi = np->vsi;
12716 	struct i40e_pf *pf = vsi->back;
12717 	bool need_reset;
12718 
12719 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12720 		i40e_pf_config_rss(pf);
12721 	else if (!(features & NETIF_F_RXHASH) &&
12722 		 netdev->features & NETIF_F_RXHASH)
12723 		i40e_clear_rss_lut(vsi);
12724 
12725 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12726 		i40e_vlan_stripping_enable(vsi);
12727 	else
12728 		i40e_vlan_stripping_disable(vsi);
12729 
12730 	if (!(features & NETIF_F_HW_TC) &&
12731 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12732 		dev_err(&pf->pdev->dev,
12733 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12734 		return -EINVAL;
12735 	}
12736 
12737 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12738 		i40e_del_all_macvlans(vsi);
12739 
12740 	need_reset = i40e_set_ntuple(pf, features);
12741 
12742 	if (need_reset)
12743 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12744 
12745 	return 0;
12746 }
12747 
12748 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12749 				    unsigned int table, unsigned int idx,
12750 				    struct udp_tunnel_info *ti)
12751 {
12752 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12753 	struct i40e_hw *hw = &np->vsi->back->hw;
12754 	u8 type, filter_index;
12755 	i40e_status ret;
12756 
12757 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12758 						   I40E_AQC_TUNNEL_TYPE_NGE;
12759 
12760 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12761 				     NULL);
12762 	if (ret) {
12763 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12764 			    i40e_stat_str(hw, ret),
12765 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12766 		return -EIO;
12767 	}
12768 
12769 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12770 	return 0;
12771 }
12772 
12773 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12774 				      unsigned int table, unsigned int idx,
12775 				      struct udp_tunnel_info *ti)
12776 {
12777 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12778 	struct i40e_hw *hw = &np->vsi->back->hw;
12779 	i40e_status ret;
12780 
12781 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12782 	if (ret) {
12783 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
12784 			    i40e_stat_str(hw, ret),
12785 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12786 		return -EIO;
12787 	}
12788 
12789 	return 0;
12790 }
12791 
12792 static int i40e_get_phys_port_id(struct net_device *netdev,
12793 				 struct netdev_phys_item_id *ppid)
12794 {
12795 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12796 	struct i40e_pf *pf = np->vsi->back;
12797 	struct i40e_hw *hw = &pf->hw;
12798 
12799 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
12800 		return -EOPNOTSUPP;
12801 
12802 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
12803 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
12804 
12805 	return 0;
12806 }
12807 
12808 /**
12809  * i40e_ndo_fdb_add - add an entry to the hardware database
12810  * @ndm: the input from the stack
12811  * @tb: pointer to array of nladdr (unused)
12812  * @dev: the net device pointer
12813  * @addr: the MAC address entry being added
12814  * @vid: VLAN ID
12815  * @flags: instructions from stack about fdb operation
12816  * @extack: netlink extended ack, unused currently
12817  */
12818 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
12819 			    struct net_device *dev,
12820 			    const unsigned char *addr, u16 vid,
12821 			    u16 flags,
12822 			    struct netlink_ext_ack *extack)
12823 {
12824 	struct i40e_netdev_priv *np = netdev_priv(dev);
12825 	struct i40e_pf *pf = np->vsi->back;
12826 	int err = 0;
12827 
12828 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
12829 		return -EOPNOTSUPP;
12830 
12831 	if (vid) {
12832 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
12833 		return -EINVAL;
12834 	}
12835 
12836 	/* Hardware does not support aging addresses so if a
12837 	 * ndm_state is given only allow permanent addresses
12838 	 */
12839 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
12840 		netdev_info(dev, "FDB only supports static addresses\n");
12841 		return -EINVAL;
12842 	}
12843 
12844 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
12845 		err = dev_uc_add_excl(dev, addr);
12846 	else if (is_multicast_ether_addr(addr))
12847 		err = dev_mc_add_excl(dev, addr);
12848 	else
12849 		err = -EINVAL;
12850 
12851 	/* Only return duplicate errors if NLM_F_EXCL is set */
12852 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
12853 		err = 0;
12854 
12855 	return err;
12856 }
12857 
12858 /**
12859  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
12860  * @dev: the netdev being configured
12861  * @nlh: RTNL message
12862  * @flags: bridge flags
12863  * @extack: netlink extended ack
12864  *
12865  * Inserts a new hardware bridge if not already created and
12866  * enables the bridging mode requested (VEB or VEPA). If the
12867  * hardware bridge has already been inserted and the request
12868  * is to change the mode then that requires a PF reset to
12869  * allow rebuild of the components with required hardware
12870  * bridge mode enabled.
12871  *
12872  * Note: expects to be called while under rtnl_lock()
12873  **/
12874 static int i40e_ndo_bridge_setlink(struct net_device *dev,
12875 				   struct nlmsghdr *nlh,
12876 				   u16 flags,
12877 				   struct netlink_ext_ack *extack)
12878 {
12879 	struct i40e_netdev_priv *np = netdev_priv(dev);
12880 	struct i40e_vsi *vsi = np->vsi;
12881 	struct i40e_pf *pf = vsi->back;
12882 	struct i40e_veb *veb = NULL;
12883 	struct nlattr *attr, *br_spec;
12884 	int i, rem;
12885 
12886 	/* Only for PF VSI for now */
12887 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12888 		return -EOPNOTSUPP;
12889 
12890 	/* Find the HW bridge for PF VSI */
12891 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12892 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12893 			veb = pf->veb[i];
12894 	}
12895 
12896 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
12897 
12898 	nla_for_each_nested(attr, br_spec, rem) {
12899 		__u16 mode;
12900 
12901 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
12902 			continue;
12903 
12904 		mode = nla_get_u16(attr);
12905 		if ((mode != BRIDGE_MODE_VEPA) &&
12906 		    (mode != BRIDGE_MODE_VEB))
12907 			return -EINVAL;
12908 
12909 		/* Insert a new HW bridge */
12910 		if (!veb) {
12911 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12912 					     vsi->tc_config.enabled_tc);
12913 			if (veb) {
12914 				veb->bridge_mode = mode;
12915 				i40e_config_bridge_mode(veb);
12916 			} else {
12917 				/* No Bridge HW offload available */
12918 				return -ENOENT;
12919 			}
12920 			break;
12921 		} else if (mode != veb->bridge_mode) {
12922 			/* Existing HW bridge but different mode needs reset */
12923 			veb->bridge_mode = mode;
12924 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
12925 			if (mode == BRIDGE_MODE_VEB)
12926 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
12927 			else
12928 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12929 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12930 			break;
12931 		}
12932 	}
12933 
12934 	return 0;
12935 }
12936 
12937 /**
12938  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
12939  * @skb: skb buff
12940  * @pid: process id
12941  * @seq: RTNL message seq #
12942  * @dev: the netdev being configured
12943  * @filter_mask: unused
12944  * @nlflags: netlink flags passed in
12945  *
12946  * Return the mode in which the hardware bridge is operating in
12947  * i.e VEB or VEPA.
12948  **/
12949 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
12950 				   struct net_device *dev,
12951 				   u32 __always_unused filter_mask,
12952 				   int nlflags)
12953 {
12954 	struct i40e_netdev_priv *np = netdev_priv(dev);
12955 	struct i40e_vsi *vsi = np->vsi;
12956 	struct i40e_pf *pf = vsi->back;
12957 	struct i40e_veb *veb = NULL;
12958 	int i;
12959 
12960 	/* Only for PF VSI for now */
12961 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12962 		return -EOPNOTSUPP;
12963 
12964 	/* Find the HW bridge for the PF VSI */
12965 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12966 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12967 			veb = pf->veb[i];
12968 	}
12969 
12970 	if (!veb)
12971 		return 0;
12972 
12973 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
12974 				       0, 0, nlflags, filter_mask, NULL);
12975 }
12976 
12977 /**
12978  * i40e_features_check - Validate encapsulated packet conforms to limits
12979  * @skb: skb buff
12980  * @dev: This physical port's netdev
12981  * @features: Offload features that the stack believes apply
12982  **/
12983 static netdev_features_t i40e_features_check(struct sk_buff *skb,
12984 					     struct net_device *dev,
12985 					     netdev_features_t features)
12986 {
12987 	size_t len;
12988 
12989 	/* No point in doing any of this if neither checksum nor GSO are
12990 	 * being requested for this frame.  We can rule out both by just
12991 	 * checking for CHECKSUM_PARTIAL
12992 	 */
12993 	if (skb->ip_summed != CHECKSUM_PARTIAL)
12994 		return features;
12995 
12996 	/* We cannot support GSO if the MSS is going to be less than
12997 	 * 64 bytes.  If it is then we need to drop support for GSO.
12998 	 */
12999 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13000 		features &= ~NETIF_F_GSO_MASK;
13001 
13002 	/* MACLEN can support at most 63 words */
13003 	len = skb_network_header(skb) - skb->data;
13004 	if (len & ~(63 * 2))
13005 		goto out_err;
13006 
13007 	/* IPLEN and EIPLEN can support at most 127 dwords */
13008 	len = skb_transport_header(skb) - skb_network_header(skb);
13009 	if (len & ~(127 * 4))
13010 		goto out_err;
13011 
13012 	if (skb->encapsulation) {
13013 		/* L4TUNLEN can support 127 words */
13014 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13015 		if (len & ~(127 * 2))
13016 			goto out_err;
13017 
13018 		/* IPLEN can support at most 127 dwords */
13019 		len = skb_inner_transport_header(skb) -
13020 		      skb_inner_network_header(skb);
13021 		if (len & ~(127 * 4))
13022 			goto out_err;
13023 	}
13024 
13025 	/* No need to validate L4LEN as TCP is the only protocol with a
13026 	 * a flexible value and we support all possible values supported
13027 	 * by TCP, which is at most 15 dwords
13028 	 */
13029 
13030 	return features;
13031 out_err:
13032 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13033 }
13034 
13035 /**
13036  * i40e_xdp_setup - add/remove an XDP program
13037  * @vsi: VSI to changed
13038  * @prog: XDP program
13039  * @extack: netlink extended ack
13040  **/
13041 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13042 			  struct netlink_ext_ack *extack)
13043 {
13044 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
13045 	struct i40e_pf *pf = vsi->back;
13046 	struct bpf_prog *old_prog;
13047 	bool need_reset;
13048 	int i;
13049 
13050 	/* Don't allow frames that span over multiple buffers */
13051 	if (frame_size > vsi->rx_buf_len) {
13052 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
13053 		return -EINVAL;
13054 	}
13055 
13056 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13057 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13058 
13059 	if (need_reset)
13060 		i40e_prep_for_reset(pf);
13061 
13062 	/* VSI shall be deleted in a moment, just return EINVAL */
13063 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13064 		return -EINVAL;
13065 
13066 	old_prog = xchg(&vsi->xdp_prog, prog);
13067 
13068 	if (need_reset) {
13069 		if (!prog)
13070 			/* Wait until ndo_xsk_wakeup completes. */
13071 			synchronize_rcu();
13072 		i40e_reset_and_rebuild(pf, true, true);
13073 	}
13074 
13075 	for (i = 0; i < vsi->num_queue_pairs; i++)
13076 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13077 
13078 	if (old_prog)
13079 		bpf_prog_put(old_prog);
13080 
13081 	/* Kick start the NAPI context if there is an AF_XDP socket open
13082 	 * on that queue id. This so that receiving will start.
13083 	 */
13084 	if (need_reset && prog)
13085 		for (i = 0; i < vsi->num_queue_pairs; i++)
13086 			if (vsi->xdp_rings[i]->xsk_pool)
13087 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13088 						      XDP_WAKEUP_RX);
13089 
13090 	return 0;
13091 }
13092 
13093 /**
13094  * i40e_enter_busy_conf - Enters busy config state
13095  * @vsi: vsi
13096  *
13097  * Returns 0 on success, <0 for failure.
13098  **/
13099 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13100 {
13101 	struct i40e_pf *pf = vsi->back;
13102 	int timeout = 50;
13103 
13104 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13105 		timeout--;
13106 		if (!timeout)
13107 			return -EBUSY;
13108 		usleep_range(1000, 2000);
13109 	}
13110 
13111 	return 0;
13112 }
13113 
13114 /**
13115  * i40e_exit_busy_conf - Exits busy config state
13116  * @vsi: vsi
13117  **/
13118 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13119 {
13120 	struct i40e_pf *pf = vsi->back;
13121 
13122 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13123 }
13124 
13125 /**
13126  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13127  * @vsi: vsi
13128  * @queue_pair: queue pair
13129  **/
13130 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13131 {
13132 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13133 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13134 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13135 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13136 	if (i40e_enabled_xdp_vsi(vsi)) {
13137 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13138 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13139 	}
13140 }
13141 
13142 /**
13143  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13144  * @vsi: vsi
13145  * @queue_pair: queue pair
13146  **/
13147 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13148 {
13149 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13150 	if (i40e_enabled_xdp_vsi(vsi)) {
13151 		/* Make sure that in-progress ndo_xdp_xmit calls are
13152 		 * completed.
13153 		 */
13154 		synchronize_rcu();
13155 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13156 	}
13157 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13158 }
13159 
13160 /**
13161  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13162  * @vsi: vsi
13163  * @queue_pair: queue pair
13164  * @enable: true for enable, false for disable
13165  **/
13166 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13167 					bool enable)
13168 {
13169 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13170 	struct i40e_q_vector *q_vector = rxr->q_vector;
13171 
13172 	if (!vsi->netdev)
13173 		return;
13174 
13175 	/* All rings in a qp belong to the same qvector. */
13176 	if (q_vector->rx.ring || q_vector->tx.ring) {
13177 		if (enable)
13178 			napi_enable(&q_vector->napi);
13179 		else
13180 			napi_disable(&q_vector->napi);
13181 	}
13182 }
13183 
13184 /**
13185  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13186  * @vsi: vsi
13187  * @queue_pair: queue pair
13188  * @enable: true for enable, false for disable
13189  *
13190  * Returns 0 on success, <0 on failure.
13191  **/
13192 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13193 					bool enable)
13194 {
13195 	struct i40e_pf *pf = vsi->back;
13196 	int pf_q, ret = 0;
13197 
13198 	pf_q = vsi->base_queue + queue_pair;
13199 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13200 				     false /*is xdp*/, enable);
13201 	if (ret) {
13202 		dev_info(&pf->pdev->dev,
13203 			 "VSI seid %d Tx ring %d %sable timeout\n",
13204 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13205 		return ret;
13206 	}
13207 
13208 	i40e_control_rx_q(pf, pf_q, enable);
13209 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13210 	if (ret) {
13211 		dev_info(&pf->pdev->dev,
13212 			 "VSI seid %d Rx ring %d %sable timeout\n",
13213 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13214 		return ret;
13215 	}
13216 
13217 	/* Due to HW errata, on Rx disable only, the register can
13218 	 * indicate done before it really is. Needs 50ms to be sure
13219 	 */
13220 	if (!enable)
13221 		mdelay(50);
13222 
13223 	if (!i40e_enabled_xdp_vsi(vsi))
13224 		return ret;
13225 
13226 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13227 				     pf_q + vsi->alloc_queue_pairs,
13228 				     true /*is xdp*/, enable);
13229 	if (ret) {
13230 		dev_info(&pf->pdev->dev,
13231 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13232 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13233 	}
13234 
13235 	return ret;
13236 }
13237 
13238 /**
13239  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13240  * @vsi: vsi
13241  * @queue_pair: queue_pair
13242  **/
13243 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13244 {
13245 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13246 	struct i40e_pf *pf = vsi->back;
13247 	struct i40e_hw *hw = &pf->hw;
13248 
13249 	/* All rings in a qp belong to the same qvector. */
13250 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13251 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13252 	else
13253 		i40e_irq_dynamic_enable_icr0(pf);
13254 
13255 	i40e_flush(hw);
13256 }
13257 
13258 /**
13259  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13260  * @vsi: vsi
13261  * @queue_pair: queue_pair
13262  **/
13263 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13264 {
13265 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13266 	struct i40e_pf *pf = vsi->back;
13267 	struct i40e_hw *hw = &pf->hw;
13268 
13269 	/* For simplicity, instead of removing the qp interrupt causes
13270 	 * from the interrupt linked list, we simply disable the interrupt, and
13271 	 * leave the list intact.
13272 	 *
13273 	 * All rings in a qp belong to the same qvector.
13274 	 */
13275 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13276 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13277 
13278 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13279 		i40e_flush(hw);
13280 		synchronize_irq(pf->msix_entries[intpf].vector);
13281 	} else {
13282 		/* Legacy and MSI mode - this stops all interrupt handling */
13283 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13284 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13285 		i40e_flush(hw);
13286 		synchronize_irq(pf->pdev->irq);
13287 	}
13288 }
13289 
13290 /**
13291  * i40e_queue_pair_disable - Disables a queue pair
13292  * @vsi: vsi
13293  * @queue_pair: queue pair
13294  *
13295  * Returns 0 on success, <0 on failure.
13296  **/
13297 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13298 {
13299 	int err;
13300 
13301 	err = i40e_enter_busy_conf(vsi);
13302 	if (err)
13303 		return err;
13304 
13305 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13306 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13307 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13308 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13309 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13310 
13311 	return err;
13312 }
13313 
13314 /**
13315  * i40e_queue_pair_enable - Enables a queue pair
13316  * @vsi: vsi
13317  * @queue_pair: queue pair
13318  *
13319  * Returns 0 on success, <0 on failure.
13320  **/
13321 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13322 {
13323 	int err;
13324 
13325 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13326 	if (err)
13327 		return err;
13328 
13329 	if (i40e_enabled_xdp_vsi(vsi)) {
13330 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13331 		if (err)
13332 			return err;
13333 	}
13334 
13335 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13336 	if (err)
13337 		return err;
13338 
13339 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13340 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13341 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13342 
13343 	i40e_exit_busy_conf(vsi);
13344 
13345 	return err;
13346 }
13347 
13348 /**
13349  * i40e_xdp - implements ndo_bpf for i40e
13350  * @dev: netdevice
13351  * @xdp: XDP command
13352  **/
13353 static int i40e_xdp(struct net_device *dev,
13354 		    struct netdev_bpf *xdp)
13355 {
13356 	struct i40e_netdev_priv *np = netdev_priv(dev);
13357 	struct i40e_vsi *vsi = np->vsi;
13358 
13359 	if (vsi->type != I40E_VSI_MAIN)
13360 		return -EINVAL;
13361 
13362 	switch (xdp->command) {
13363 	case XDP_SETUP_PROG:
13364 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13365 	case XDP_SETUP_XSK_POOL:
13366 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13367 					   xdp->xsk.queue_id);
13368 	default:
13369 		return -EINVAL;
13370 	}
13371 }
13372 
13373 static const struct net_device_ops i40e_netdev_ops = {
13374 	.ndo_open		= i40e_open,
13375 	.ndo_stop		= i40e_close,
13376 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13377 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13378 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13379 	.ndo_validate_addr	= eth_validate_addr,
13380 	.ndo_set_mac_address	= i40e_set_mac,
13381 	.ndo_change_mtu		= i40e_change_mtu,
13382 	.ndo_eth_ioctl		= i40e_ioctl,
13383 	.ndo_tx_timeout		= i40e_tx_timeout,
13384 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13385 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13386 #ifdef CONFIG_NET_POLL_CONTROLLER
13387 	.ndo_poll_controller	= i40e_netpoll,
13388 #endif
13389 	.ndo_setup_tc		= __i40e_setup_tc,
13390 	.ndo_select_queue	= i40e_lan_select_queue,
13391 	.ndo_set_features	= i40e_set_features,
13392 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13393 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13394 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13395 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13396 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13397 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13398 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13399 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13400 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13401 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13402 	.ndo_features_check	= i40e_features_check,
13403 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13404 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13405 	.ndo_bpf		= i40e_xdp,
13406 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13407 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13408 	.ndo_dfwd_add_station	= i40e_fwd_add,
13409 	.ndo_dfwd_del_station	= i40e_fwd_del,
13410 };
13411 
13412 /**
13413  * i40e_config_netdev - Setup the netdev flags
13414  * @vsi: the VSI being configured
13415  *
13416  * Returns 0 on success, negative value on failure
13417  **/
13418 static int i40e_config_netdev(struct i40e_vsi *vsi)
13419 {
13420 	struct i40e_pf *pf = vsi->back;
13421 	struct i40e_hw *hw = &pf->hw;
13422 	struct i40e_netdev_priv *np;
13423 	struct net_device *netdev;
13424 	u8 broadcast[ETH_ALEN];
13425 	u8 mac_addr[ETH_ALEN];
13426 	int etherdev_size;
13427 	netdev_features_t hw_enc_features;
13428 	netdev_features_t hw_features;
13429 
13430 	etherdev_size = sizeof(struct i40e_netdev_priv);
13431 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13432 	if (!netdev)
13433 		return -ENOMEM;
13434 
13435 	vsi->netdev = netdev;
13436 	np = netdev_priv(netdev);
13437 	np->vsi = vsi;
13438 
13439 	hw_enc_features = NETIF_F_SG			|
13440 			  NETIF_F_IP_CSUM		|
13441 			  NETIF_F_IPV6_CSUM		|
13442 			  NETIF_F_HIGHDMA		|
13443 			  NETIF_F_SOFT_FEATURES		|
13444 			  NETIF_F_TSO			|
13445 			  NETIF_F_TSO_ECN		|
13446 			  NETIF_F_TSO6			|
13447 			  NETIF_F_GSO_GRE		|
13448 			  NETIF_F_GSO_GRE_CSUM		|
13449 			  NETIF_F_GSO_PARTIAL		|
13450 			  NETIF_F_GSO_IPXIP4		|
13451 			  NETIF_F_GSO_IPXIP6		|
13452 			  NETIF_F_GSO_UDP_TUNNEL	|
13453 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13454 			  NETIF_F_GSO_UDP_L4		|
13455 			  NETIF_F_SCTP_CRC		|
13456 			  NETIF_F_RXHASH		|
13457 			  NETIF_F_RXCSUM		|
13458 			  0;
13459 
13460 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13461 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13462 
13463 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13464 
13465 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13466 
13467 	netdev->hw_enc_features |= hw_enc_features;
13468 
13469 	/* record features VLANs can make use of */
13470 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13471 
13472 	/* enable macvlan offloads */
13473 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13474 
13475 	hw_features = hw_enc_features		|
13476 		      NETIF_F_HW_VLAN_CTAG_TX	|
13477 		      NETIF_F_HW_VLAN_CTAG_RX;
13478 
13479 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13480 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13481 
13482 	netdev->hw_features |= hw_features;
13483 
13484 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13485 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13486 
13487 	netdev->features &= ~NETIF_F_HW_TC;
13488 
13489 	if (vsi->type == I40E_VSI_MAIN) {
13490 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13491 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13492 		/* The following steps are necessary for two reasons. First,
13493 		 * some older NVM configurations load a default MAC-VLAN
13494 		 * filter that will accept any tagged packet, and we want to
13495 		 * replace this with a normal filter. Additionally, it is
13496 		 * possible our MAC address was provided by the platform using
13497 		 * Open Firmware or similar.
13498 		 *
13499 		 * Thus, we need to remove the default filter and install one
13500 		 * specific to the MAC address.
13501 		 */
13502 		i40e_rm_default_mac_filter(vsi, mac_addr);
13503 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13504 		i40e_add_mac_filter(vsi, mac_addr);
13505 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13506 	} else {
13507 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13508 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13509 		 * the end, which is 4 bytes long, so force truncation of the
13510 		 * original name by IFNAMSIZ - 4
13511 		 */
13512 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13513 			 IFNAMSIZ - 4,
13514 			 pf->vsi[pf->lan_vsi]->netdev->name);
13515 		eth_random_addr(mac_addr);
13516 
13517 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13518 		i40e_add_mac_filter(vsi, mac_addr);
13519 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13520 	}
13521 
13522 	/* Add the broadcast filter so that we initially will receive
13523 	 * broadcast packets. Note that when a new VLAN is first added the
13524 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13525 	 * specific filters as part of transitioning into "vlan" operation.
13526 	 * When more VLANs are added, the driver will copy each existing MAC
13527 	 * filter and add it for the new VLAN.
13528 	 *
13529 	 * Broadcast filters are handled specially by
13530 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13531 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13532 	 * filter. The subtask will update the correct broadcast promiscuous
13533 	 * bits as VLANs become active or inactive.
13534 	 */
13535 	eth_broadcast_addr(broadcast);
13536 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13537 	i40e_add_mac_filter(vsi, broadcast);
13538 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13539 
13540 	eth_hw_addr_set(netdev, mac_addr);
13541 	ether_addr_copy(netdev->perm_addr, mac_addr);
13542 
13543 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13544 	netdev->neigh_priv_len = sizeof(u32) * 4;
13545 
13546 	netdev->priv_flags |= IFF_UNICAST_FLT;
13547 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13548 	/* Setup netdev TC information */
13549 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13550 
13551 	netdev->netdev_ops = &i40e_netdev_ops;
13552 	netdev->watchdog_timeo = 5 * HZ;
13553 	i40e_set_ethtool_ops(netdev);
13554 
13555 	/* MTU range: 68 - 9706 */
13556 	netdev->min_mtu = ETH_MIN_MTU;
13557 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13558 
13559 	return 0;
13560 }
13561 
13562 /**
13563  * i40e_vsi_delete - Delete a VSI from the switch
13564  * @vsi: the VSI being removed
13565  *
13566  * Returns 0 on success, negative value on failure
13567  **/
13568 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13569 {
13570 	/* remove default VSI is not allowed */
13571 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13572 		return;
13573 
13574 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13575 }
13576 
13577 /**
13578  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13579  * @vsi: the VSI being queried
13580  *
13581  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13582  **/
13583 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13584 {
13585 	struct i40e_veb *veb;
13586 	struct i40e_pf *pf = vsi->back;
13587 
13588 	/* Uplink is not a bridge so default to VEB */
13589 	if (vsi->veb_idx >= I40E_MAX_VEB)
13590 		return 1;
13591 
13592 	veb = pf->veb[vsi->veb_idx];
13593 	if (!veb) {
13594 		dev_info(&pf->pdev->dev,
13595 			 "There is no veb associated with the bridge\n");
13596 		return -ENOENT;
13597 	}
13598 
13599 	/* Uplink is a bridge in VEPA mode */
13600 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13601 		return 0;
13602 	} else {
13603 		/* Uplink is a bridge in VEB mode */
13604 		return 1;
13605 	}
13606 
13607 	/* VEPA is now default bridge, so return 0 */
13608 	return 0;
13609 }
13610 
13611 /**
13612  * i40e_add_vsi - Add a VSI to the switch
13613  * @vsi: the VSI being configured
13614  *
13615  * This initializes a VSI context depending on the VSI type to be added and
13616  * passes it down to the add_vsi aq command.
13617  **/
13618 static int i40e_add_vsi(struct i40e_vsi *vsi)
13619 {
13620 	int ret = -ENODEV;
13621 	struct i40e_pf *pf = vsi->back;
13622 	struct i40e_hw *hw = &pf->hw;
13623 	struct i40e_vsi_context ctxt;
13624 	struct i40e_mac_filter *f;
13625 	struct hlist_node *h;
13626 	int bkt;
13627 
13628 	u8 enabled_tc = 0x1; /* TC0 enabled */
13629 	int f_count = 0;
13630 
13631 	memset(&ctxt, 0, sizeof(ctxt));
13632 	switch (vsi->type) {
13633 	case I40E_VSI_MAIN:
13634 		/* The PF's main VSI is already setup as part of the
13635 		 * device initialization, so we'll not bother with
13636 		 * the add_vsi call, but we will retrieve the current
13637 		 * VSI context.
13638 		 */
13639 		ctxt.seid = pf->main_vsi_seid;
13640 		ctxt.pf_num = pf->hw.pf_id;
13641 		ctxt.vf_num = 0;
13642 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13643 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13644 		if (ret) {
13645 			dev_info(&pf->pdev->dev,
13646 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13647 				 i40e_stat_str(&pf->hw, ret),
13648 				 i40e_aq_str(&pf->hw,
13649 					     pf->hw.aq.asq_last_status));
13650 			return -ENOENT;
13651 		}
13652 		vsi->info = ctxt.info;
13653 		vsi->info.valid_sections = 0;
13654 
13655 		vsi->seid = ctxt.seid;
13656 		vsi->id = ctxt.vsi_number;
13657 
13658 		enabled_tc = i40e_pf_get_tc_map(pf);
13659 
13660 		/* Source pruning is enabled by default, so the flag is
13661 		 * negative logic - if it's set, we need to fiddle with
13662 		 * the VSI to disable source pruning.
13663 		 */
13664 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13665 			memset(&ctxt, 0, sizeof(ctxt));
13666 			ctxt.seid = pf->main_vsi_seid;
13667 			ctxt.pf_num = pf->hw.pf_id;
13668 			ctxt.vf_num = 0;
13669 			ctxt.info.valid_sections |=
13670 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13671 			ctxt.info.switch_id =
13672 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13673 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13674 			if (ret) {
13675 				dev_info(&pf->pdev->dev,
13676 					 "update vsi failed, err %s aq_err %s\n",
13677 					 i40e_stat_str(&pf->hw, ret),
13678 					 i40e_aq_str(&pf->hw,
13679 						     pf->hw.aq.asq_last_status));
13680 				ret = -ENOENT;
13681 				goto err;
13682 			}
13683 		}
13684 
13685 		/* MFP mode setup queue map and update VSI */
13686 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13687 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13688 			memset(&ctxt, 0, sizeof(ctxt));
13689 			ctxt.seid = pf->main_vsi_seid;
13690 			ctxt.pf_num = pf->hw.pf_id;
13691 			ctxt.vf_num = 0;
13692 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
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 			/* update the local VSI info queue map */
13704 			i40e_vsi_update_queue_map(vsi, &ctxt);
13705 			vsi->info.valid_sections = 0;
13706 		} else {
13707 			/* Default/Main VSI is only enabled for TC0
13708 			 * reconfigure it to enable all TCs that are
13709 			 * available on the port in SFP mode.
13710 			 * For MFP case the iSCSI PF would use this
13711 			 * flow to enable LAN+iSCSI TC.
13712 			 */
13713 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13714 			if (ret) {
13715 				/* Single TC condition is not fatal,
13716 				 * message and continue
13717 				 */
13718 				dev_info(&pf->pdev->dev,
13719 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13720 					 enabled_tc,
13721 					 i40e_stat_str(&pf->hw, ret),
13722 					 i40e_aq_str(&pf->hw,
13723 						    pf->hw.aq.asq_last_status));
13724 			}
13725 		}
13726 		break;
13727 
13728 	case I40E_VSI_FDIR:
13729 		ctxt.pf_num = hw->pf_id;
13730 		ctxt.vf_num = 0;
13731 		ctxt.uplink_seid = vsi->uplink_seid;
13732 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13733 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13734 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13735 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13736 			ctxt.info.valid_sections |=
13737 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13738 			ctxt.info.switch_id =
13739 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13740 		}
13741 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13742 		break;
13743 
13744 	case I40E_VSI_VMDQ2:
13745 		ctxt.pf_num = hw->pf_id;
13746 		ctxt.vf_num = 0;
13747 		ctxt.uplink_seid = vsi->uplink_seid;
13748 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13749 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13750 
13751 		/* This VSI is connected to VEB so the switch_id
13752 		 * should be set to zero by default.
13753 		 */
13754 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13755 			ctxt.info.valid_sections |=
13756 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13757 			ctxt.info.switch_id =
13758 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13759 		}
13760 
13761 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13762 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13763 		break;
13764 
13765 	case I40E_VSI_SRIOV:
13766 		ctxt.pf_num = hw->pf_id;
13767 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13768 		ctxt.uplink_seid = vsi->uplink_seid;
13769 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13770 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13771 
13772 		/* This VSI is connected to VEB so the switch_id
13773 		 * should be set to zero by default.
13774 		 */
13775 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13776 			ctxt.info.valid_sections |=
13777 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13778 			ctxt.info.switch_id =
13779 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13780 		}
13781 
13782 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
13783 			ctxt.info.valid_sections |=
13784 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
13785 			ctxt.info.queueing_opt_flags |=
13786 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
13787 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
13788 		}
13789 
13790 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
13791 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
13792 		if (pf->vf[vsi->vf_id].spoofchk) {
13793 			ctxt.info.valid_sections |=
13794 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
13795 			ctxt.info.sec_flags |=
13796 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
13797 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
13798 		}
13799 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13800 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13801 		break;
13802 
13803 	case I40E_VSI_IWARP:
13804 		/* send down message to iWARP */
13805 		break;
13806 
13807 	default:
13808 		return -ENODEV;
13809 	}
13810 
13811 	if (vsi->type != I40E_VSI_MAIN) {
13812 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
13813 		if (ret) {
13814 			dev_info(&vsi->back->pdev->dev,
13815 				 "add vsi failed, err %s aq_err %s\n",
13816 				 i40e_stat_str(&pf->hw, ret),
13817 				 i40e_aq_str(&pf->hw,
13818 					     pf->hw.aq.asq_last_status));
13819 			ret = -ENOENT;
13820 			goto err;
13821 		}
13822 		vsi->info = ctxt.info;
13823 		vsi->info.valid_sections = 0;
13824 		vsi->seid = ctxt.seid;
13825 		vsi->id = ctxt.vsi_number;
13826 	}
13827 
13828 	vsi->active_filters = 0;
13829 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
13830 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13831 	/* If macvlan filters already exist, force them to get loaded */
13832 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
13833 		f->state = I40E_FILTER_NEW;
13834 		f_count++;
13835 	}
13836 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13837 
13838 	if (f_count) {
13839 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
13840 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
13841 	}
13842 
13843 	/* Update VSI BW information */
13844 	ret = i40e_vsi_get_bw_info(vsi);
13845 	if (ret) {
13846 		dev_info(&pf->pdev->dev,
13847 			 "couldn't get vsi bw info, err %s aq_err %s\n",
13848 			 i40e_stat_str(&pf->hw, ret),
13849 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13850 		/* VSI is already added so not tearing that up */
13851 		ret = 0;
13852 	}
13853 
13854 err:
13855 	return ret;
13856 }
13857 
13858 /**
13859  * i40e_vsi_release - Delete a VSI and free its resources
13860  * @vsi: the VSI being removed
13861  *
13862  * Returns 0 on success or < 0 on error
13863  **/
13864 int i40e_vsi_release(struct i40e_vsi *vsi)
13865 {
13866 	struct i40e_mac_filter *f;
13867 	struct hlist_node *h;
13868 	struct i40e_veb *veb = NULL;
13869 	struct i40e_pf *pf;
13870 	u16 uplink_seid;
13871 	int i, n, bkt;
13872 
13873 	pf = vsi->back;
13874 
13875 	/* release of a VEB-owner or last VSI is not allowed */
13876 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
13877 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
13878 			 vsi->seid, vsi->uplink_seid);
13879 		return -ENODEV;
13880 	}
13881 	if (vsi == pf->vsi[pf->lan_vsi] &&
13882 	    !test_bit(__I40E_DOWN, pf->state)) {
13883 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
13884 		return -ENODEV;
13885 	}
13886 	set_bit(__I40E_VSI_RELEASING, vsi->state);
13887 	uplink_seid = vsi->uplink_seid;
13888 	if (vsi->type != I40E_VSI_SRIOV) {
13889 		if (vsi->netdev_registered) {
13890 			vsi->netdev_registered = false;
13891 			if (vsi->netdev) {
13892 				/* results in a call to i40e_close() */
13893 				unregister_netdev(vsi->netdev);
13894 			}
13895 		} else {
13896 			i40e_vsi_close(vsi);
13897 		}
13898 		i40e_vsi_disable_irq(vsi);
13899 	}
13900 
13901 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13902 
13903 	/* clear the sync flag on all filters */
13904 	if (vsi->netdev) {
13905 		__dev_uc_unsync(vsi->netdev, NULL);
13906 		__dev_mc_unsync(vsi->netdev, NULL);
13907 	}
13908 
13909 	/* make sure any remaining filters are marked for deletion */
13910 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
13911 		__i40e_del_filter(vsi, f);
13912 
13913 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13914 
13915 	i40e_sync_vsi_filters(vsi);
13916 
13917 	i40e_vsi_delete(vsi);
13918 	i40e_vsi_free_q_vectors(vsi);
13919 	if (vsi->netdev) {
13920 		free_netdev(vsi->netdev);
13921 		vsi->netdev = NULL;
13922 	}
13923 	i40e_vsi_clear_rings(vsi);
13924 	i40e_vsi_clear(vsi);
13925 
13926 	/* If this was the last thing on the VEB, except for the
13927 	 * controlling VSI, remove the VEB, which puts the controlling
13928 	 * VSI onto the next level down in the switch.
13929 	 *
13930 	 * Well, okay, there's one more exception here: don't remove
13931 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
13932 	 * from up the network stack.
13933 	 */
13934 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
13935 		if (pf->vsi[i] &&
13936 		    pf->vsi[i]->uplink_seid == uplink_seid &&
13937 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13938 			n++;      /* count the VSIs */
13939 		}
13940 	}
13941 	for (i = 0; i < I40E_MAX_VEB; i++) {
13942 		if (!pf->veb[i])
13943 			continue;
13944 		if (pf->veb[i]->uplink_seid == uplink_seid)
13945 			n++;     /* count the VEBs */
13946 		if (pf->veb[i]->seid == uplink_seid)
13947 			veb = pf->veb[i];
13948 	}
13949 	if (n == 0 && veb && veb->uplink_seid != 0)
13950 		i40e_veb_release(veb);
13951 
13952 	return 0;
13953 }
13954 
13955 /**
13956  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
13957  * @vsi: ptr to the VSI
13958  *
13959  * This should only be called after i40e_vsi_mem_alloc() which allocates the
13960  * corresponding SW VSI structure and initializes num_queue_pairs for the
13961  * newly allocated VSI.
13962  *
13963  * Returns 0 on success or negative on failure
13964  **/
13965 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
13966 {
13967 	int ret = -ENOENT;
13968 	struct i40e_pf *pf = vsi->back;
13969 
13970 	if (vsi->q_vectors[0]) {
13971 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
13972 			 vsi->seid);
13973 		return -EEXIST;
13974 	}
13975 
13976 	if (vsi->base_vector) {
13977 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
13978 			 vsi->seid, vsi->base_vector);
13979 		return -EEXIST;
13980 	}
13981 
13982 	ret = i40e_vsi_alloc_q_vectors(vsi);
13983 	if (ret) {
13984 		dev_info(&pf->pdev->dev,
13985 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
13986 			 vsi->num_q_vectors, vsi->seid, ret);
13987 		vsi->num_q_vectors = 0;
13988 		goto vector_setup_out;
13989 	}
13990 
13991 	/* In Legacy mode, we do not have to get any other vector since we
13992 	 * piggyback on the misc/ICR0 for queue interrupts.
13993 	*/
13994 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
13995 		return ret;
13996 	if (vsi->num_q_vectors)
13997 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
13998 						 vsi->num_q_vectors, vsi->idx);
13999 	if (vsi->base_vector < 0) {
14000 		dev_info(&pf->pdev->dev,
14001 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14002 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14003 		i40e_vsi_free_q_vectors(vsi);
14004 		ret = -ENOENT;
14005 		goto vector_setup_out;
14006 	}
14007 
14008 vector_setup_out:
14009 	return ret;
14010 }
14011 
14012 /**
14013  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14014  * @vsi: pointer to the vsi.
14015  *
14016  * This re-allocates a vsi's queue resources.
14017  *
14018  * Returns pointer to the successfully allocated and configured VSI sw struct
14019  * on success, otherwise returns NULL on failure.
14020  **/
14021 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14022 {
14023 	u16 alloc_queue_pairs;
14024 	struct i40e_pf *pf;
14025 	u8 enabled_tc;
14026 	int ret;
14027 
14028 	if (!vsi)
14029 		return NULL;
14030 
14031 	pf = vsi->back;
14032 
14033 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14034 	i40e_vsi_clear_rings(vsi);
14035 
14036 	i40e_vsi_free_arrays(vsi, false);
14037 	i40e_set_num_rings_in_vsi(vsi);
14038 	ret = i40e_vsi_alloc_arrays(vsi, false);
14039 	if (ret)
14040 		goto err_vsi;
14041 
14042 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14043 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14044 
14045 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14046 	if (ret < 0) {
14047 		dev_info(&pf->pdev->dev,
14048 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14049 			 alloc_queue_pairs, vsi->seid, ret);
14050 		goto err_vsi;
14051 	}
14052 	vsi->base_queue = ret;
14053 
14054 	/* Update the FW view of the VSI. Force a reset of TC and queue
14055 	 * layout configurations.
14056 	 */
14057 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14058 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14059 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14060 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14061 	if (vsi->type == I40E_VSI_MAIN)
14062 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14063 
14064 	/* assign it some queues */
14065 	ret = i40e_alloc_rings(vsi);
14066 	if (ret)
14067 		goto err_rings;
14068 
14069 	/* map all of the rings to the q_vectors */
14070 	i40e_vsi_map_rings_to_vectors(vsi);
14071 	return vsi;
14072 
14073 err_rings:
14074 	i40e_vsi_free_q_vectors(vsi);
14075 	if (vsi->netdev_registered) {
14076 		vsi->netdev_registered = false;
14077 		unregister_netdev(vsi->netdev);
14078 		free_netdev(vsi->netdev);
14079 		vsi->netdev = NULL;
14080 	}
14081 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14082 err_vsi:
14083 	i40e_vsi_clear(vsi);
14084 	return NULL;
14085 }
14086 
14087 /**
14088  * i40e_vsi_setup - Set up a VSI by a given type
14089  * @pf: board private structure
14090  * @type: VSI type
14091  * @uplink_seid: the switch element to link to
14092  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14093  *
14094  * This allocates the sw VSI structure and its queue resources, then add a VSI
14095  * to the identified VEB.
14096  *
14097  * Returns pointer to the successfully allocated and configure VSI sw struct on
14098  * success, otherwise returns NULL on failure.
14099  **/
14100 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14101 				u16 uplink_seid, u32 param1)
14102 {
14103 	struct i40e_vsi *vsi = NULL;
14104 	struct i40e_veb *veb = NULL;
14105 	u16 alloc_queue_pairs;
14106 	int ret, i;
14107 	int v_idx;
14108 
14109 	/* The requested uplink_seid must be either
14110 	 *     - the PF's port seid
14111 	 *              no VEB is needed because this is the PF
14112 	 *              or this is a Flow Director special case VSI
14113 	 *     - seid of an existing VEB
14114 	 *     - seid of a VSI that owns an existing VEB
14115 	 *     - seid of a VSI that doesn't own a VEB
14116 	 *              a new VEB is created and the VSI becomes the owner
14117 	 *     - seid of the PF VSI, which is what creates the first VEB
14118 	 *              this is a special case of the previous
14119 	 *
14120 	 * Find which uplink_seid we were given and create a new VEB if needed
14121 	 */
14122 	for (i = 0; i < I40E_MAX_VEB; i++) {
14123 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14124 			veb = pf->veb[i];
14125 			break;
14126 		}
14127 	}
14128 
14129 	if (!veb && uplink_seid != pf->mac_seid) {
14130 
14131 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14132 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14133 				vsi = pf->vsi[i];
14134 				break;
14135 			}
14136 		}
14137 		if (!vsi) {
14138 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14139 				 uplink_seid);
14140 			return NULL;
14141 		}
14142 
14143 		if (vsi->uplink_seid == pf->mac_seid)
14144 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14145 					     vsi->tc_config.enabled_tc);
14146 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14147 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14148 					     vsi->tc_config.enabled_tc);
14149 		if (veb) {
14150 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14151 				dev_info(&vsi->back->pdev->dev,
14152 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14153 				return NULL;
14154 			}
14155 			/* We come up by default in VEPA mode if SRIOV is not
14156 			 * already enabled, in which case we can't force VEPA
14157 			 * mode.
14158 			 */
14159 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14160 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14161 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14162 			}
14163 			i40e_config_bridge_mode(veb);
14164 		}
14165 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14166 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14167 				veb = pf->veb[i];
14168 		}
14169 		if (!veb) {
14170 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14171 			return NULL;
14172 		}
14173 
14174 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14175 		uplink_seid = veb->seid;
14176 	}
14177 
14178 	/* get vsi sw struct */
14179 	v_idx = i40e_vsi_mem_alloc(pf, type);
14180 	if (v_idx < 0)
14181 		goto err_alloc;
14182 	vsi = pf->vsi[v_idx];
14183 	if (!vsi)
14184 		goto err_alloc;
14185 	vsi->type = type;
14186 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14187 
14188 	if (type == I40E_VSI_MAIN)
14189 		pf->lan_vsi = v_idx;
14190 	else if (type == I40E_VSI_SRIOV)
14191 		vsi->vf_id = param1;
14192 	/* assign it some queues */
14193 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14194 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14195 
14196 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14197 	if (ret < 0) {
14198 		dev_info(&pf->pdev->dev,
14199 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14200 			 alloc_queue_pairs, vsi->seid, ret);
14201 		goto err_vsi;
14202 	}
14203 	vsi->base_queue = ret;
14204 
14205 	/* get a VSI from the hardware */
14206 	vsi->uplink_seid = uplink_seid;
14207 	ret = i40e_add_vsi(vsi);
14208 	if (ret)
14209 		goto err_vsi;
14210 
14211 	switch (vsi->type) {
14212 	/* setup the netdev if needed */
14213 	case I40E_VSI_MAIN:
14214 	case I40E_VSI_VMDQ2:
14215 		ret = i40e_config_netdev(vsi);
14216 		if (ret)
14217 			goto err_netdev;
14218 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14219 		if (ret)
14220 			goto err_netdev;
14221 		ret = register_netdev(vsi->netdev);
14222 		if (ret)
14223 			goto err_netdev;
14224 		vsi->netdev_registered = true;
14225 		netif_carrier_off(vsi->netdev);
14226 #ifdef CONFIG_I40E_DCB
14227 		/* Setup DCB netlink interface */
14228 		i40e_dcbnl_setup(vsi);
14229 #endif /* CONFIG_I40E_DCB */
14230 		fallthrough;
14231 	case I40E_VSI_FDIR:
14232 		/* set up vectors and rings if needed */
14233 		ret = i40e_vsi_setup_vectors(vsi);
14234 		if (ret)
14235 			goto err_msix;
14236 
14237 		ret = i40e_alloc_rings(vsi);
14238 		if (ret)
14239 			goto err_rings;
14240 
14241 		/* map all of the rings to the q_vectors */
14242 		i40e_vsi_map_rings_to_vectors(vsi);
14243 
14244 		i40e_vsi_reset_stats(vsi);
14245 		break;
14246 	default:
14247 		/* no netdev or rings for the other VSI types */
14248 		break;
14249 	}
14250 
14251 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14252 	    (vsi->type == I40E_VSI_VMDQ2)) {
14253 		ret = i40e_vsi_config_rss(vsi);
14254 	}
14255 	return vsi;
14256 
14257 err_rings:
14258 	i40e_vsi_free_q_vectors(vsi);
14259 err_msix:
14260 	if (vsi->netdev_registered) {
14261 		vsi->netdev_registered = false;
14262 		unregister_netdev(vsi->netdev);
14263 		free_netdev(vsi->netdev);
14264 		vsi->netdev = NULL;
14265 	}
14266 err_netdev:
14267 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14268 err_vsi:
14269 	i40e_vsi_clear(vsi);
14270 err_alloc:
14271 	return NULL;
14272 }
14273 
14274 /**
14275  * i40e_veb_get_bw_info - Query VEB BW information
14276  * @veb: the veb to query
14277  *
14278  * Query the Tx scheduler BW configuration data for given VEB
14279  **/
14280 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14281 {
14282 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14283 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14284 	struct i40e_pf *pf = veb->pf;
14285 	struct i40e_hw *hw = &pf->hw;
14286 	u32 tc_bw_max;
14287 	int ret = 0;
14288 	int i;
14289 
14290 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14291 						  &bw_data, NULL);
14292 	if (ret) {
14293 		dev_info(&pf->pdev->dev,
14294 			 "query veb bw config failed, err %s aq_err %s\n",
14295 			 i40e_stat_str(&pf->hw, ret),
14296 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14297 		goto out;
14298 	}
14299 
14300 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14301 						   &ets_data, NULL);
14302 	if (ret) {
14303 		dev_info(&pf->pdev->dev,
14304 			 "query veb bw ets config failed, err %s aq_err %s\n",
14305 			 i40e_stat_str(&pf->hw, ret),
14306 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14307 		goto out;
14308 	}
14309 
14310 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14311 	veb->bw_max_quanta = ets_data.tc_bw_max;
14312 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14313 	veb->enabled_tc = ets_data.tc_valid_bits;
14314 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14315 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14316 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14317 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14318 		veb->bw_tc_limit_credits[i] =
14319 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14320 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14321 	}
14322 
14323 out:
14324 	return ret;
14325 }
14326 
14327 /**
14328  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14329  * @pf: board private structure
14330  *
14331  * On error: returns error code (negative)
14332  * On success: returns vsi index in PF (positive)
14333  **/
14334 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14335 {
14336 	int ret = -ENOENT;
14337 	struct i40e_veb *veb;
14338 	int i;
14339 
14340 	/* Need to protect the allocation of switch elements at the PF level */
14341 	mutex_lock(&pf->switch_mutex);
14342 
14343 	/* VEB list may be fragmented if VEB creation/destruction has
14344 	 * been happening.  We can afford to do a quick scan to look
14345 	 * for any free slots in the list.
14346 	 *
14347 	 * find next empty veb slot, looping back around if necessary
14348 	 */
14349 	i = 0;
14350 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14351 		i++;
14352 	if (i >= I40E_MAX_VEB) {
14353 		ret = -ENOMEM;
14354 		goto err_alloc_veb;  /* out of VEB slots! */
14355 	}
14356 
14357 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14358 	if (!veb) {
14359 		ret = -ENOMEM;
14360 		goto err_alloc_veb;
14361 	}
14362 	veb->pf = pf;
14363 	veb->idx = i;
14364 	veb->enabled_tc = 1;
14365 
14366 	pf->veb[i] = veb;
14367 	ret = i;
14368 err_alloc_veb:
14369 	mutex_unlock(&pf->switch_mutex);
14370 	return ret;
14371 }
14372 
14373 /**
14374  * i40e_switch_branch_release - Delete a branch of the switch tree
14375  * @branch: where to start deleting
14376  *
14377  * This uses recursion to find the tips of the branch to be
14378  * removed, deleting until we get back to and can delete this VEB.
14379  **/
14380 static void i40e_switch_branch_release(struct i40e_veb *branch)
14381 {
14382 	struct i40e_pf *pf = branch->pf;
14383 	u16 branch_seid = branch->seid;
14384 	u16 veb_idx = branch->idx;
14385 	int i;
14386 
14387 	/* release any VEBs on this VEB - RECURSION */
14388 	for (i = 0; i < I40E_MAX_VEB; i++) {
14389 		if (!pf->veb[i])
14390 			continue;
14391 		if (pf->veb[i]->uplink_seid == branch->seid)
14392 			i40e_switch_branch_release(pf->veb[i]);
14393 	}
14394 
14395 	/* Release the VSIs on this VEB, but not the owner VSI.
14396 	 *
14397 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14398 	 *       the VEB itself, so don't use (*branch) after this loop.
14399 	 */
14400 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14401 		if (!pf->vsi[i])
14402 			continue;
14403 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14404 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14405 			i40e_vsi_release(pf->vsi[i]);
14406 		}
14407 	}
14408 
14409 	/* There's one corner case where the VEB might not have been
14410 	 * removed, so double check it here and remove it if needed.
14411 	 * This case happens if the veb was created from the debugfs
14412 	 * commands and no VSIs were added to it.
14413 	 */
14414 	if (pf->veb[veb_idx])
14415 		i40e_veb_release(pf->veb[veb_idx]);
14416 }
14417 
14418 /**
14419  * i40e_veb_clear - remove veb struct
14420  * @veb: the veb to remove
14421  **/
14422 static void i40e_veb_clear(struct i40e_veb *veb)
14423 {
14424 	if (!veb)
14425 		return;
14426 
14427 	if (veb->pf) {
14428 		struct i40e_pf *pf = veb->pf;
14429 
14430 		mutex_lock(&pf->switch_mutex);
14431 		if (pf->veb[veb->idx] == veb)
14432 			pf->veb[veb->idx] = NULL;
14433 		mutex_unlock(&pf->switch_mutex);
14434 	}
14435 
14436 	kfree(veb);
14437 }
14438 
14439 /**
14440  * i40e_veb_release - Delete a VEB and free its resources
14441  * @veb: the VEB being removed
14442  **/
14443 void i40e_veb_release(struct i40e_veb *veb)
14444 {
14445 	struct i40e_vsi *vsi = NULL;
14446 	struct i40e_pf *pf;
14447 	int i, n = 0;
14448 
14449 	pf = veb->pf;
14450 
14451 	/* find the remaining VSI and check for extras */
14452 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14453 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14454 			n++;
14455 			vsi = pf->vsi[i];
14456 		}
14457 	}
14458 	if (n != 1) {
14459 		dev_info(&pf->pdev->dev,
14460 			 "can't remove VEB %d with %d VSIs left\n",
14461 			 veb->seid, n);
14462 		return;
14463 	}
14464 
14465 	/* move the remaining VSI to uplink veb */
14466 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14467 	if (veb->uplink_seid) {
14468 		vsi->uplink_seid = veb->uplink_seid;
14469 		if (veb->uplink_seid == pf->mac_seid)
14470 			vsi->veb_idx = I40E_NO_VEB;
14471 		else
14472 			vsi->veb_idx = veb->veb_idx;
14473 	} else {
14474 		/* floating VEB */
14475 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14476 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14477 	}
14478 
14479 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14480 	i40e_veb_clear(veb);
14481 }
14482 
14483 /**
14484  * i40e_add_veb - create the VEB in the switch
14485  * @veb: the VEB to be instantiated
14486  * @vsi: the controlling VSI
14487  **/
14488 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14489 {
14490 	struct i40e_pf *pf = veb->pf;
14491 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14492 	int ret;
14493 
14494 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14495 			      veb->enabled_tc, false,
14496 			      &veb->seid, enable_stats, NULL);
14497 
14498 	/* get a VEB from the hardware */
14499 	if (ret) {
14500 		dev_info(&pf->pdev->dev,
14501 			 "couldn't add VEB, err %s aq_err %s\n",
14502 			 i40e_stat_str(&pf->hw, ret),
14503 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14504 		return -EPERM;
14505 	}
14506 
14507 	/* get statistics counter */
14508 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14509 					 &veb->stats_idx, NULL, NULL, NULL);
14510 	if (ret) {
14511 		dev_info(&pf->pdev->dev,
14512 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
14513 			 i40e_stat_str(&pf->hw, ret),
14514 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14515 		return -EPERM;
14516 	}
14517 	ret = i40e_veb_get_bw_info(veb);
14518 	if (ret) {
14519 		dev_info(&pf->pdev->dev,
14520 			 "couldn't get VEB bw info, err %s aq_err %s\n",
14521 			 i40e_stat_str(&pf->hw, ret),
14522 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14523 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14524 		return -ENOENT;
14525 	}
14526 
14527 	vsi->uplink_seid = veb->seid;
14528 	vsi->veb_idx = veb->idx;
14529 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14530 
14531 	return 0;
14532 }
14533 
14534 /**
14535  * i40e_veb_setup - Set up a VEB
14536  * @pf: board private structure
14537  * @flags: VEB setup flags
14538  * @uplink_seid: the switch element to link to
14539  * @vsi_seid: the initial VSI seid
14540  * @enabled_tc: Enabled TC bit-map
14541  *
14542  * This allocates the sw VEB structure and links it into the switch
14543  * It is possible and legal for this to be a duplicate of an already
14544  * existing VEB.  It is also possible for both uplink and vsi seids
14545  * to be zero, in order to create a floating VEB.
14546  *
14547  * Returns pointer to the successfully allocated VEB sw struct on
14548  * success, otherwise returns NULL on failure.
14549  **/
14550 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14551 				u16 uplink_seid, u16 vsi_seid,
14552 				u8 enabled_tc)
14553 {
14554 	struct i40e_veb *veb, *uplink_veb = NULL;
14555 	int vsi_idx, veb_idx;
14556 	int ret;
14557 
14558 	/* if one seid is 0, the other must be 0 to create a floating relay */
14559 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14560 	    (uplink_seid + vsi_seid != 0)) {
14561 		dev_info(&pf->pdev->dev,
14562 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14563 			 uplink_seid, vsi_seid);
14564 		return NULL;
14565 	}
14566 
14567 	/* make sure there is such a vsi and uplink */
14568 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14569 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14570 			break;
14571 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14572 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14573 			 vsi_seid);
14574 		return NULL;
14575 	}
14576 
14577 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14578 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14579 			if (pf->veb[veb_idx] &&
14580 			    pf->veb[veb_idx]->seid == uplink_seid) {
14581 				uplink_veb = pf->veb[veb_idx];
14582 				break;
14583 			}
14584 		}
14585 		if (!uplink_veb) {
14586 			dev_info(&pf->pdev->dev,
14587 				 "uplink seid %d not found\n", uplink_seid);
14588 			return NULL;
14589 		}
14590 	}
14591 
14592 	/* get veb sw struct */
14593 	veb_idx = i40e_veb_mem_alloc(pf);
14594 	if (veb_idx < 0)
14595 		goto err_alloc;
14596 	veb = pf->veb[veb_idx];
14597 	veb->flags = flags;
14598 	veb->uplink_seid = uplink_seid;
14599 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14600 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14601 
14602 	/* create the VEB in the switch */
14603 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14604 	if (ret)
14605 		goto err_veb;
14606 	if (vsi_idx == pf->lan_vsi)
14607 		pf->lan_veb = veb->idx;
14608 
14609 	return veb;
14610 
14611 err_veb:
14612 	i40e_veb_clear(veb);
14613 err_alloc:
14614 	return NULL;
14615 }
14616 
14617 /**
14618  * i40e_setup_pf_switch_element - set PF vars based on switch type
14619  * @pf: board private structure
14620  * @ele: element we are building info from
14621  * @num_reported: total number of elements
14622  * @printconfig: should we print the contents
14623  *
14624  * helper function to assist in extracting a few useful SEID values.
14625  **/
14626 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14627 				struct i40e_aqc_switch_config_element_resp *ele,
14628 				u16 num_reported, bool printconfig)
14629 {
14630 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14631 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14632 	u8 element_type = ele->element_type;
14633 	u16 seid = le16_to_cpu(ele->seid);
14634 
14635 	if (printconfig)
14636 		dev_info(&pf->pdev->dev,
14637 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14638 			 element_type, seid, uplink_seid, downlink_seid);
14639 
14640 	switch (element_type) {
14641 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14642 		pf->mac_seid = seid;
14643 		break;
14644 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14645 		/* Main VEB? */
14646 		if (uplink_seid != pf->mac_seid)
14647 			break;
14648 		if (pf->lan_veb >= I40E_MAX_VEB) {
14649 			int v;
14650 
14651 			/* find existing or else empty VEB */
14652 			for (v = 0; v < I40E_MAX_VEB; v++) {
14653 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14654 					pf->lan_veb = v;
14655 					break;
14656 				}
14657 			}
14658 			if (pf->lan_veb >= I40E_MAX_VEB) {
14659 				v = i40e_veb_mem_alloc(pf);
14660 				if (v < 0)
14661 					break;
14662 				pf->lan_veb = v;
14663 			}
14664 		}
14665 		if (pf->lan_veb >= I40E_MAX_VEB)
14666 			break;
14667 
14668 		pf->veb[pf->lan_veb]->seid = seid;
14669 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14670 		pf->veb[pf->lan_veb]->pf = pf;
14671 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14672 		break;
14673 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14674 		if (num_reported != 1)
14675 			break;
14676 		/* This is immediately after a reset so we can assume this is
14677 		 * the PF's VSI
14678 		 */
14679 		pf->mac_seid = uplink_seid;
14680 		pf->pf_seid = downlink_seid;
14681 		pf->main_vsi_seid = seid;
14682 		if (printconfig)
14683 			dev_info(&pf->pdev->dev,
14684 				 "pf_seid=%d main_vsi_seid=%d\n",
14685 				 pf->pf_seid, pf->main_vsi_seid);
14686 		break;
14687 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14688 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14689 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14690 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14691 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14692 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14693 		/* ignore these for now */
14694 		break;
14695 	default:
14696 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14697 			 element_type, seid);
14698 		break;
14699 	}
14700 }
14701 
14702 /**
14703  * i40e_fetch_switch_configuration - Get switch config from firmware
14704  * @pf: board private structure
14705  * @printconfig: should we print the contents
14706  *
14707  * Get the current switch configuration from the device and
14708  * extract a few useful SEID values.
14709  **/
14710 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14711 {
14712 	struct i40e_aqc_get_switch_config_resp *sw_config;
14713 	u16 next_seid = 0;
14714 	int ret = 0;
14715 	u8 *aq_buf;
14716 	int i;
14717 
14718 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14719 	if (!aq_buf)
14720 		return -ENOMEM;
14721 
14722 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14723 	do {
14724 		u16 num_reported, num_total;
14725 
14726 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14727 						I40E_AQ_LARGE_BUF,
14728 						&next_seid, NULL);
14729 		if (ret) {
14730 			dev_info(&pf->pdev->dev,
14731 				 "get switch config failed err %s aq_err %s\n",
14732 				 i40e_stat_str(&pf->hw, ret),
14733 				 i40e_aq_str(&pf->hw,
14734 					     pf->hw.aq.asq_last_status));
14735 			kfree(aq_buf);
14736 			return -ENOENT;
14737 		}
14738 
14739 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14740 		num_total = le16_to_cpu(sw_config->header.num_total);
14741 
14742 		if (printconfig)
14743 			dev_info(&pf->pdev->dev,
14744 				 "header: %d reported %d total\n",
14745 				 num_reported, num_total);
14746 
14747 		for (i = 0; i < num_reported; i++) {
14748 			struct i40e_aqc_switch_config_element_resp *ele =
14749 				&sw_config->element[i];
14750 
14751 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14752 						     printconfig);
14753 		}
14754 	} while (next_seid != 0);
14755 
14756 	kfree(aq_buf);
14757 	return ret;
14758 }
14759 
14760 /**
14761  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14762  * @pf: board private structure
14763  * @reinit: if the Main VSI needs to re-initialized.
14764  * @lock_acquired: indicates whether or not the lock has been acquired
14765  *
14766  * Returns 0 on success, negative value on failure
14767  **/
14768 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14769 {
14770 	u16 flags = 0;
14771 	int ret;
14772 
14773 	/* find out what's out there already */
14774 	ret = i40e_fetch_switch_configuration(pf, false);
14775 	if (ret) {
14776 		dev_info(&pf->pdev->dev,
14777 			 "couldn't fetch switch config, err %s aq_err %s\n",
14778 			 i40e_stat_str(&pf->hw, ret),
14779 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14780 		return ret;
14781 	}
14782 	i40e_pf_reset_stats(pf);
14783 
14784 	/* set the switch config bit for the whole device to
14785 	 * support limited promisc or true promisc
14786 	 * when user requests promisc. The default is limited
14787 	 * promisc.
14788 	*/
14789 
14790 	if ((pf->hw.pf_id == 0) &&
14791 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
14792 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14793 		pf->last_sw_conf_flags = flags;
14794 	}
14795 
14796 	if (pf->hw.pf_id == 0) {
14797 		u16 valid_flags;
14798 
14799 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14800 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
14801 						NULL);
14802 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
14803 			dev_info(&pf->pdev->dev,
14804 				 "couldn't set switch config bits, err %s aq_err %s\n",
14805 				 i40e_stat_str(&pf->hw, ret),
14806 				 i40e_aq_str(&pf->hw,
14807 					     pf->hw.aq.asq_last_status));
14808 			/* not a fatal problem, just keep going */
14809 		}
14810 		pf->last_sw_conf_valid_flags = valid_flags;
14811 	}
14812 
14813 	/* first time setup */
14814 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
14815 		struct i40e_vsi *vsi = NULL;
14816 		u16 uplink_seid;
14817 
14818 		/* Set up the PF VSI associated with the PF's main VSI
14819 		 * that is already in the HW switch
14820 		 */
14821 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
14822 			uplink_seid = pf->veb[pf->lan_veb]->seid;
14823 		else
14824 			uplink_seid = pf->mac_seid;
14825 		if (pf->lan_vsi == I40E_NO_VSI)
14826 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
14827 		else if (reinit)
14828 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
14829 		if (!vsi) {
14830 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
14831 			i40e_cloud_filter_exit(pf);
14832 			i40e_fdir_teardown(pf);
14833 			return -EAGAIN;
14834 		}
14835 	} else {
14836 		/* force a reset of TC and queue layout configurations */
14837 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14838 
14839 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14840 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14841 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14842 	}
14843 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
14844 
14845 	i40e_fdir_sb_setup(pf);
14846 
14847 	/* Setup static PF queue filter control settings */
14848 	ret = i40e_setup_pf_filter_control(pf);
14849 	if (ret) {
14850 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
14851 			 ret);
14852 		/* Failure here should not stop continuing other steps */
14853 	}
14854 
14855 	/* enable RSS in the HW, even for only one queue, as the stack can use
14856 	 * the hash
14857 	 */
14858 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
14859 		i40e_pf_config_rss(pf);
14860 
14861 	/* fill in link information and enable LSE reporting */
14862 	i40e_link_event(pf);
14863 
14864 	/* Initialize user-specific link properties */
14865 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
14866 				  I40E_AQ_AN_COMPLETED) ? true : false);
14867 
14868 	i40e_ptp_init(pf);
14869 
14870 	if (!lock_acquired)
14871 		rtnl_lock();
14872 
14873 	/* repopulate tunnel port filters */
14874 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
14875 
14876 	if (!lock_acquired)
14877 		rtnl_unlock();
14878 
14879 	return ret;
14880 }
14881 
14882 /**
14883  * i40e_determine_queue_usage - Work out queue distribution
14884  * @pf: board private structure
14885  **/
14886 static void i40e_determine_queue_usage(struct i40e_pf *pf)
14887 {
14888 	int queues_left;
14889 	int q_max;
14890 
14891 	pf->num_lan_qps = 0;
14892 
14893 	/* Find the max queues to be put into basic use.  We'll always be
14894 	 * using TC0, whether or not DCB is running, and TC0 will get the
14895 	 * big RSS set.
14896 	 */
14897 	queues_left = pf->hw.func_caps.num_tx_qp;
14898 
14899 	if ((queues_left == 1) ||
14900 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
14901 		/* one qp for PF, no queues for anything else */
14902 		queues_left = 0;
14903 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14904 
14905 		/* make sure all the fancies are disabled */
14906 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14907 			       I40E_FLAG_IWARP_ENABLED	|
14908 			       I40E_FLAG_FD_SB_ENABLED	|
14909 			       I40E_FLAG_FD_ATR_ENABLED	|
14910 			       I40E_FLAG_DCB_CAPABLE	|
14911 			       I40E_FLAG_DCB_ENABLED	|
14912 			       I40E_FLAG_SRIOV_ENABLED	|
14913 			       I40E_FLAG_VMDQ_ENABLED);
14914 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14915 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
14916 				  I40E_FLAG_FD_SB_ENABLED |
14917 				  I40E_FLAG_FD_ATR_ENABLED |
14918 				  I40E_FLAG_DCB_CAPABLE))) {
14919 		/* one qp for PF */
14920 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14921 		queues_left -= pf->num_lan_qps;
14922 
14923 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14924 			       I40E_FLAG_IWARP_ENABLED	|
14925 			       I40E_FLAG_FD_SB_ENABLED	|
14926 			       I40E_FLAG_FD_ATR_ENABLED	|
14927 			       I40E_FLAG_DCB_ENABLED	|
14928 			       I40E_FLAG_VMDQ_ENABLED);
14929 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14930 	} else {
14931 		/* Not enough queues for all TCs */
14932 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
14933 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
14934 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
14935 					I40E_FLAG_DCB_ENABLED);
14936 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
14937 		}
14938 
14939 		/* limit lan qps to the smaller of qps, cpus or msix */
14940 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
14941 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
14942 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
14943 		pf->num_lan_qps = q_max;
14944 
14945 		queues_left -= pf->num_lan_qps;
14946 	}
14947 
14948 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14949 		if (queues_left > 1) {
14950 			queues_left -= 1; /* save 1 queue for FD */
14951 		} else {
14952 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
14953 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14954 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
14955 		}
14956 	}
14957 
14958 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
14959 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
14960 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
14961 					(queues_left / pf->num_vf_qps));
14962 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
14963 	}
14964 
14965 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
14966 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
14967 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
14968 					  (queues_left / pf->num_vmdq_qps));
14969 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
14970 	}
14971 
14972 	pf->queues_left = queues_left;
14973 	dev_dbg(&pf->pdev->dev,
14974 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
14975 		pf->hw.func_caps.num_tx_qp,
14976 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
14977 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
14978 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
14979 		queues_left);
14980 }
14981 
14982 /**
14983  * i40e_setup_pf_filter_control - Setup PF static filter control
14984  * @pf: PF to be setup
14985  *
14986  * i40e_setup_pf_filter_control sets up a PF's initial filter control
14987  * settings. If PE/FCoE are enabled then it will also set the per PF
14988  * based filter sizes required for them. It also enables Flow director,
14989  * ethertype and macvlan type filter settings for the pf.
14990  *
14991  * Returns 0 on success, negative on failure
14992  **/
14993 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
14994 {
14995 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
14996 
14997 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
14998 
14999 	/* Flow Director is enabled */
15000 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
15001 		settings->enable_fdir = true;
15002 
15003 	/* Ethtype and MACVLAN filters enabled for PF */
15004 	settings->enable_ethtype = true;
15005 	settings->enable_macvlan = true;
15006 
15007 	if (i40e_set_filter_control(&pf->hw, settings))
15008 		return -ENOENT;
15009 
15010 	return 0;
15011 }
15012 
15013 #define INFO_STRING_LEN 255
15014 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15015 static void i40e_print_features(struct i40e_pf *pf)
15016 {
15017 	struct i40e_hw *hw = &pf->hw;
15018 	char *buf;
15019 	int i;
15020 
15021 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15022 	if (!buf)
15023 		return;
15024 
15025 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15026 #ifdef CONFIG_PCI_IOV
15027 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15028 #endif
15029 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15030 		      pf->hw.func_caps.num_vsis,
15031 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15032 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15033 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15034 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15035 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15036 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15037 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15038 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15039 	}
15040 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15041 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15042 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15043 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15044 	if (pf->flags & I40E_FLAG_PTP)
15045 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15046 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15047 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15048 	else
15049 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15050 
15051 	dev_info(&pf->pdev->dev, "%s\n", buf);
15052 	kfree(buf);
15053 	WARN_ON(i > INFO_STRING_LEN);
15054 }
15055 
15056 /**
15057  * i40e_get_platform_mac_addr - get platform-specific MAC address
15058  * @pdev: PCI device information struct
15059  * @pf: board private structure
15060  *
15061  * Look up the MAC address for the device. First we'll try
15062  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15063  * specific fallback. Otherwise, we'll default to the stored value in
15064  * firmware.
15065  **/
15066 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15067 {
15068 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15069 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15070 }
15071 
15072 /**
15073  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15074  * @fec_cfg: FEC option to set in flags
15075  * @flags: ptr to flags in which we set FEC option
15076  **/
15077 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15078 {
15079 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15080 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15081 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15082 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15083 		*flags |= I40E_FLAG_RS_FEC;
15084 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15085 	}
15086 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15087 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15088 		*flags |= I40E_FLAG_BASE_R_FEC;
15089 		*flags &= ~I40E_FLAG_RS_FEC;
15090 	}
15091 	if (fec_cfg == 0)
15092 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15093 }
15094 
15095 /**
15096  * i40e_check_recovery_mode - check if we are running transition firmware
15097  * @pf: board private structure
15098  *
15099  * Check registers indicating the firmware runs in recovery mode. Sets the
15100  * appropriate driver state.
15101  *
15102  * Returns true if the recovery mode was detected, false otherwise
15103  **/
15104 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15105 {
15106 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15107 
15108 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15109 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15110 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15111 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15112 
15113 		return true;
15114 	}
15115 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15116 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15117 
15118 	return false;
15119 }
15120 
15121 /**
15122  * i40e_pf_loop_reset - perform reset in a loop.
15123  * @pf: board private structure
15124  *
15125  * This function is useful when a NIC is about to enter recovery mode.
15126  * When a NIC's internal data structures are corrupted the NIC's
15127  * firmware is going to enter recovery mode.
15128  * Right after a POR it takes about 7 minutes for firmware to enter
15129  * recovery mode. Until that time a NIC is in some kind of intermediate
15130  * state. After that time period the NIC almost surely enters
15131  * recovery mode. The only way for a driver to detect intermediate
15132  * state is to issue a series of pf-resets and check a return value.
15133  * If a PF reset returns success then the firmware could be in recovery
15134  * mode so the caller of this code needs to check for recovery mode
15135  * if this function returns success. There is a little chance that
15136  * firmware will hang in intermediate state forever.
15137  * Since waiting 7 minutes is quite a lot of time this function waits
15138  * 10 seconds and then gives up by returning an error.
15139  *
15140  * Return 0 on success, negative on failure.
15141  **/
15142 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
15143 {
15144 	/* wait max 10 seconds for PF reset to succeed */
15145 	const unsigned long time_end = jiffies + 10 * HZ;
15146 
15147 	struct i40e_hw *hw = &pf->hw;
15148 	i40e_status ret;
15149 
15150 	ret = i40e_pf_reset(hw);
15151 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15152 		usleep_range(10000, 20000);
15153 		ret = i40e_pf_reset(hw);
15154 	}
15155 
15156 	if (ret == I40E_SUCCESS)
15157 		pf->pfr_count++;
15158 	else
15159 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15160 
15161 	return ret;
15162 }
15163 
15164 /**
15165  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15166  * @pf: board private structure
15167  *
15168  * Check FW registers to determine if FW issued unexpected EMP Reset.
15169  * Every time when unexpected EMP Reset occurs the FW increments
15170  * a counter of unexpected EMP Resets. When the counter reaches 10
15171  * the FW should enter the Recovery mode
15172  *
15173  * Returns true if FW issued unexpected EMP Reset
15174  **/
15175 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15176 {
15177 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15178 			   I40E_GL_FWSTS_FWS1B_MASK;
15179 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15180 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15181 }
15182 
15183 /**
15184  * i40e_handle_resets - handle EMP resets and PF resets
15185  * @pf: board private structure
15186  *
15187  * Handle both EMP resets and PF resets and conclude whether there are
15188  * any issues regarding these resets. If there are any issues then
15189  * generate log entry.
15190  *
15191  * Return 0 if NIC is healthy or negative value when there are issues
15192  * with resets
15193  **/
15194 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
15195 {
15196 	const i40e_status pfr = i40e_pf_loop_reset(pf);
15197 	const bool is_empr = i40e_check_fw_empr(pf);
15198 
15199 	if (is_empr || pfr != I40E_SUCCESS)
15200 		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");
15201 
15202 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15203 }
15204 
15205 /**
15206  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15207  * @pf: board private structure
15208  * @hw: ptr to the hardware info
15209  *
15210  * This function does a minimal setup of all subsystems needed for running
15211  * recovery mode.
15212  *
15213  * Returns 0 on success, negative on failure
15214  **/
15215 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15216 {
15217 	struct i40e_vsi *vsi;
15218 	int err;
15219 	int v_idx;
15220 
15221 	pci_save_state(pf->pdev);
15222 
15223 	/* set up periodic task facility */
15224 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15225 	pf->service_timer_period = HZ;
15226 
15227 	INIT_WORK(&pf->service_task, i40e_service_task);
15228 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15229 
15230 	err = i40e_init_interrupt_scheme(pf);
15231 	if (err)
15232 		goto err_switch_setup;
15233 
15234 	/* The number of VSIs reported by the FW is the minimum guaranteed
15235 	 * to us; HW supports far more and we share the remaining pool with
15236 	 * the other PFs. We allocate space for more than the guarantee with
15237 	 * the understanding that we might not get them all later.
15238 	 */
15239 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15240 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15241 	else
15242 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15243 
15244 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15245 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15246 			  GFP_KERNEL);
15247 	if (!pf->vsi) {
15248 		err = -ENOMEM;
15249 		goto err_switch_setup;
15250 	}
15251 
15252 	/* We allocate one VSI which is needed as absolute minimum
15253 	 * in order to register the netdev
15254 	 */
15255 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15256 	if (v_idx < 0) {
15257 		err = v_idx;
15258 		goto err_switch_setup;
15259 	}
15260 	pf->lan_vsi = v_idx;
15261 	vsi = pf->vsi[v_idx];
15262 	if (!vsi) {
15263 		err = -EFAULT;
15264 		goto err_switch_setup;
15265 	}
15266 	vsi->alloc_queue_pairs = 1;
15267 	err = i40e_config_netdev(vsi);
15268 	if (err)
15269 		goto err_switch_setup;
15270 	err = register_netdev(vsi->netdev);
15271 	if (err)
15272 		goto err_switch_setup;
15273 	vsi->netdev_registered = true;
15274 	i40e_dbg_pf_init(pf);
15275 
15276 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15277 	if (err)
15278 		goto err_switch_setup;
15279 
15280 	/* tell the firmware that we're starting */
15281 	i40e_send_version(pf);
15282 
15283 	/* since everything's happy, start the service_task timer */
15284 	mod_timer(&pf->service_timer,
15285 		  round_jiffies(jiffies + pf->service_timer_period));
15286 
15287 	return 0;
15288 
15289 err_switch_setup:
15290 	i40e_reset_interrupt_capability(pf);
15291 	del_timer_sync(&pf->service_timer);
15292 	i40e_shutdown_adminq(hw);
15293 	iounmap(hw->hw_addr);
15294 	pci_disable_pcie_error_reporting(pf->pdev);
15295 	pci_release_mem_regions(pf->pdev);
15296 	pci_disable_device(pf->pdev);
15297 	kfree(pf);
15298 
15299 	return err;
15300 }
15301 
15302 /**
15303  * i40e_set_subsystem_device_id - set subsystem device id
15304  * @hw: pointer to the hardware info
15305  *
15306  * Set PCI subsystem device id either from a pci_dev structure or
15307  * a specific FW register.
15308  **/
15309 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15310 {
15311 	struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15312 
15313 	hw->subsystem_device_id = pdev->subsystem_device ?
15314 		pdev->subsystem_device :
15315 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15316 }
15317 
15318 /**
15319  * i40e_probe - Device initialization routine
15320  * @pdev: PCI device information struct
15321  * @ent: entry in i40e_pci_tbl
15322  *
15323  * i40e_probe initializes a PF identified by a pci_dev structure.
15324  * The OS initialization, configuring of the PF private structure,
15325  * and a hardware reset occur.
15326  *
15327  * Returns 0 on success, negative on failure
15328  **/
15329 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15330 {
15331 	struct i40e_aq_get_phy_abilities_resp abilities;
15332 #ifdef CONFIG_I40E_DCB
15333 	enum i40e_get_fw_lldp_status_resp lldp_status;
15334 	i40e_status status;
15335 #endif /* CONFIG_I40E_DCB */
15336 	struct i40e_pf *pf;
15337 	struct i40e_hw *hw;
15338 	static u16 pfs_found;
15339 	u16 wol_nvm_bits;
15340 	u16 link_status;
15341 	int err;
15342 	u32 val;
15343 	u32 i;
15344 
15345 	err = pci_enable_device_mem(pdev);
15346 	if (err)
15347 		return err;
15348 
15349 	/* set up for high or low dma */
15350 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15351 	if (err) {
15352 		dev_err(&pdev->dev,
15353 			"DMA configuration failed: 0x%x\n", err);
15354 		goto err_dma;
15355 	}
15356 
15357 	/* set up pci connections */
15358 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15359 	if (err) {
15360 		dev_info(&pdev->dev,
15361 			 "pci_request_selected_regions failed %d\n", err);
15362 		goto err_pci_reg;
15363 	}
15364 
15365 	pci_enable_pcie_error_reporting(pdev);
15366 	pci_set_master(pdev);
15367 
15368 	/* Now that we have a PCI connection, we need to do the
15369 	 * low level device setup.  This is primarily setting up
15370 	 * the Admin Queue structures and then querying for the
15371 	 * device's current profile information.
15372 	 */
15373 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15374 	if (!pf) {
15375 		err = -ENOMEM;
15376 		goto err_pf_alloc;
15377 	}
15378 	pf->next_vsi = 0;
15379 	pf->pdev = pdev;
15380 	set_bit(__I40E_DOWN, pf->state);
15381 
15382 	hw = &pf->hw;
15383 	hw->back = pf;
15384 
15385 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15386 				I40E_MAX_CSR_SPACE);
15387 	/* We believe that the highest register to read is
15388 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15389 	 * is not less than that before mapping to prevent a
15390 	 * kernel panic.
15391 	 */
15392 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15393 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15394 			pf->ioremap_len);
15395 		err = -ENOMEM;
15396 		goto err_ioremap;
15397 	}
15398 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15399 	if (!hw->hw_addr) {
15400 		err = -EIO;
15401 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15402 			 (unsigned int)pci_resource_start(pdev, 0),
15403 			 pf->ioremap_len, err);
15404 		goto err_ioremap;
15405 	}
15406 	hw->vendor_id = pdev->vendor;
15407 	hw->device_id = pdev->device;
15408 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15409 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15410 	i40e_set_subsystem_device_id(hw);
15411 	hw->bus.device = PCI_SLOT(pdev->devfn);
15412 	hw->bus.func = PCI_FUNC(pdev->devfn);
15413 	hw->bus.bus_id = pdev->bus->number;
15414 	pf->instance = pfs_found;
15415 
15416 	/* Select something other than the 802.1ad ethertype for the
15417 	 * switch to use internally and drop on ingress.
15418 	 */
15419 	hw->switch_tag = 0xffff;
15420 	hw->first_tag = ETH_P_8021AD;
15421 	hw->second_tag = ETH_P_8021Q;
15422 
15423 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15424 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15425 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15426 
15427 	/* set up the locks for the AQ, do this only once in probe
15428 	 * and destroy them only once in remove
15429 	 */
15430 	mutex_init(&hw->aq.asq_mutex);
15431 	mutex_init(&hw->aq.arq_mutex);
15432 
15433 	pf->msg_enable = netif_msg_init(debug,
15434 					NETIF_MSG_DRV |
15435 					NETIF_MSG_PROBE |
15436 					NETIF_MSG_LINK);
15437 	if (debug < -1)
15438 		pf->hw.debug_mask = debug;
15439 
15440 	/* do a special CORER for clearing PXE mode once at init */
15441 	if (hw->revision_id == 0 &&
15442 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15443 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15444 		i40e_flush(hw);
15445 		msleep(200);
15446 		pf->corer_count++;
15447 
15448 		i40e_clear_pxe_mode(hw);
15449 	}
15450 
15451 	/* Reset here to make sure all is clean and to define PF 'n' */
15452 	i40e_clear_hw(hw);
15453 
15454 	err = i40e_set_mac_type(hw);
15455 	if (err) {
15456 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15457 			 err);
15458 		goto err_pf_reset;
15459 	}
15460 
15461 	err = i40e_handle_resets(pf);
15462 	if (err)
15463 		goto err_pf_reset;
15464 
15465 	i40e_check_recovery_mode(pf);
15466 
15467 	if (is_kdump_kernel()) {
15468 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15469 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15470 	} else {
15471 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15472 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15473 	}
15474 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15475 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15476 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15477 
15478 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15479 		 "%s-%s:misc",
15480 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15481 
15482 	err = i40e_init_shared_code(hw);
15483 	if (err) {
15484 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15485 			 err);
15486 		goto err_pf_reset;
15487 	}
15488 
15489 	/* set up a default setting for link flow control */
15490 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15491 
15492 	err = i40e_init_adminq(hw);
15493 	if (err) {
15494 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15495 			dev_info(&pdev->dev,
15496 				 "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",
15497 				 hw->aq.api_maj_ver,
15498 				 hw->aq.api_min_ver,
15499 				 I40E_FW_API_VERSION_MAJOR,
15500 				 I40E_FW_MINOR_VERSION(hw));
15501 		else
15502 			dev_info(&pdev->dev,
15503 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15504 
15505 		goto err_pf_reset;
15506 	}
15507 	i40e_get_oem_version(hw);
15508 
15509 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15510 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15511 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15512 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15513 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15514 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15515 
15516 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15517 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15518 		dev_dbg(&pdev->dev,
15519 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15520 			 hw->aq.api_maj_ver,
15521 			 hw->aq.api_min_ver,
15522 			 I40E_FW_API_VERSION_MAJOR,
15523 			 I40E_FW_MINOR_VERSION(hw));
15524 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15525 		dev_info(&pdev->dev,
15526 			 "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",
15527 			 hw->aq.api_maj_ver,
15528 			 hw->aq.api_min_ver,
15529 			 I40E_FW_API_VERSION_MAJOR,
15530 			 I40E_FW_MINOR_VERSION(hw));
15531 
15532 	i40e_verify_eeprom(pf);
15533 
15534 	/* Rev 0 hardware was never productized */
15535 	if (hw->revision_id < 1)
15536 		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");
15537 
15538 	i40e_clear_pxe_mode(hw);
15539 
15540 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15541 	if (err)
15542 		goto err_adminq_setup;
15543 
15544 	err = i40e_sw_init(pf);
15545 	if (err) {
15546 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15547 		goto err_sw_init;
15548 	}
15549 
15550 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15551 		return i40e_init_recovery_mode(pf, hw);
15552 
15553 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15554 				hw->func_caps.num_rx_qp, 0, 0);
15555 	if (err) {
15556 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15557 		goto err_init_lan_hmc;
15558 	}
15559 
15560 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15561 	if (err) {
15562 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15563 		err = -ENOENT;
15564 		goto err_configure_lan_hmc;
15565 	}
15566 
15567 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15568 	 * Ignore error return codes because if it was already disabled via
15569 	 * hardware settings this will fail
15570 	 */
15571 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15572 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15573 		i40e_aq_stop_lldp(hw, true, false, NULL);
15574 	}
15575 
15576 	/* allow a platform config to override the HW addr */
15577 	i40e_get_platform_mac_addr(pdev, pf);
15578 
15579 	if (!is_valid_ether_addr(hw->mac.addr)) {
15580 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15581 		err = -EIO;
15582 		goto err_mac_addr;
15583 	}
15584 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15585 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15586 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15587 	if (is_valid_ether_addr(hw->mac.port_addr))
15588 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15589 
15590 	i40e_ptp_alloc_pins(pf);
15591 	pci_set_drvdata(pdev, pf);
15592 	pci_save_state(pdev);
15593 
15594 #ifdef CONFIG_I40E_DCB
15595 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15596 	(!status &&
15597 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15598 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15599 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15600 	dev_info(&pdev->dev,
15601 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15602 			"FW LLDP is disabled\n" :
15603 			"FW LLDP is enabled\n");
15604 
15605 	/* Enable FW to write default DCB config on link-up */
15606 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15607 
15608 	err = i40e_init_pf_dcb(pf);
15609 	if (err) {
15610 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15611 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15612 		/* Continue without DCB enabled */
15613 	}
15614 #endif /* CONFIG_I40E_DCB */
15615 
15616 	/* set up periodic task facility */
15617 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15618 	pf->service_timer_period = HZ;
15619 
15620 	INIT_WORK(&pf->service_task, i40e_service_task);
15621 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15622 
15623 	/* NVM bit on means WoL disabled for the port */
15624 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15625 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15626 		pf->wol_en = false;
15627 	else
15628 		pf->wol_en = true;
15629 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15630 
15631 	/* set up the main switch operations */
15632 	i40e_determine_queue_usage(pf);
15633 	err = i40e_init_interrupt_scheme(pf);
15634 	if (err)
15635 		goto err_switch_setup;
15636 
15637 	/* Reduce Tx and Rx pairs for kdump
15638 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15639 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15640 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15641 	 */
15642 	if (is_kdump_kernel())
15643 		pf->num_lan_msix = 1;
15644 
15645 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15646 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15647 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15648 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15649 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15650 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15651 						    UDP_TUNNEL_TYPE_GENEVE;
15652 
15653 	/* The number of VSIs reported by the FW is the minimum guaranteed
15654 	 * to us; HW supports far more and we share the remaining pool with
15655 	 * the other PFs. We allocate space for more than the guarantee with
15656 	 * the understanding that we might not get them all later.
15657 	 */
15658 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15659 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15660 	else
15661 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15662 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15663 		dev_warn(&pf->pdev->dev,
15664 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15665 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15666 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15667 	}
15668 
15669 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15670 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15671 			  GFP_KERNEL);
15672 	if (!pf->vsi) {
15673 		err = -ENOMEM;
15674 		goto err_switch_setup;
15675 	}
15676 
15677 #ifdef CONFIG_PCI_IOV
15678 	/* prep for VF support */
15679 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15680 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15681 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15682 		if (pci_num_vf(pdev))
15683 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15684 	}
15685 #endif
15686 	err = i40e_setup_pf_switch(pf, false, false);
15687 	if (err) {
15688 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15689 		goto err_vsis;
15690 	}
15691 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15692 
15693 	/* if FDIR VSI was set up, start it now */
15694 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15695 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15696 			i40e_vsi_open(pf->vsi[i]);
15697 			break;
15698 		}
15699 	}
15700 
15701 	/* The driver only wants link up/down and module qualification
15702 	 * reports from firmware.  Note the negative logic.
15703 	 */
15704 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15705 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15706 					 I40E_AQ_EVENT_MEDIA_NA |
15707 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15708 	if (err)
15709 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15710 			 i40e_stat_str(&pf->hw, err),
15711 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15712 
15713 	/* Reconfigure hardware for allowing smaller MSS in the case
15714 	 * of TSO, so that we avoid the MDD being fired and causing
15715 	 * a reset in the case of small MSS+TSO.
15716 	 */
15717 	val = rd32(hw, I40E_REG_MSS);
15718 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15719 		val &= ~I40E_REG_MSS_MIN_MASK;
15720 		val |= I40E_64BYTE_MSS;
15721 		wr32(hw, I40E_REG_MSS, val);
15722 	}
15723 
15724 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15725 		msleep(75);
15726 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15727 		if (err)
15728 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15729 				 i40e_stat_str(&pf->hw, err),
15730 				 i40e_aq_str(&pf->hw,
15731 					     pf->hw.aq.asq_last_status));
15732 	}
15733 	/* The main driver is (mostly) up and happy. We need to set this state
15734 	 * before setting up the misc vector or we get a race and the vector
15735 	 * ends up disabled forever.
15736 	 */
15737 	clear_bit(__I40E_DOWN, pf->state);
15738 
15739 	/* In case of MSIX we are going to setup the misc vector right here
15740 	 * to handle admin queue events etc. In case of legacy and MSI
15741 	 * the misc functionality and queue processing is combined in
15742 	 * the same vector and that gets setup at open.
15743 	 */
15744 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15745 		err = i40e_setup_misc_vector(pf);
15746 		if (err) {
15747 			dev_info(&pdev->dev,
15748 				 "setup of misc vector failed: %d\n", err);
15749 			i40e_cloud_filter_exit(pf);
15750 			i40e_fdir_teardown(pf);
15751 			goto err_vsis;
15752 		}
15753 	}
15754 
15755 #ifdef CONFIG_PCI_IOV
15756 	/* prep for VF support */
15757 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15758 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15759 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15760 		/* disable link interrupts for VFs */
15761 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15762 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15763 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15764 		i40e_flush(hw);
15765 
15766 		if (pci_num_vf(pdev)) {
15767 			dev_info(&pdev->dev,
15768 				 "Active VFs found, allocating resources.\n");
15769 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15770 			if (err)
15771 				dev_info(&pdev->dev,
15772 					 "Error %d allocating resources for existing VFs\n",
15773 					 err);
15774 		}
15775 	}
15776 #endif /* CONFIG_PCI_IOV */
15777 
15778 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15779 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
15780 						      pf->num_iwarp_msix,
15781 						      I40E_IWARP_IRQ_PILE_ID);
15782 		if (pf->iwarp_base_vector < 0) {
15783 			dev_info(&pdev->dev,
15784 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
15785 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
15786 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
15787 		}
15788 	}
15789 
15790 	i40e_dbg_pf_init(pf);
15791 
15792 	/* tell the firmware that we're starting */
15793 	i40e_send_version(pf);
15794 
15795 	/* since everything's happy, start the service_task timer */
15796 	mod_timer(&pf->service_timer,
15797 		  round_jiffies(jiffies + pf->service_timer_period));
15798 
15799 	/* add this PF to client device list and launch a client service task */
15800 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15801 		err = i40e_lan_add_device(pf);
15802 		if (err)
15803 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
15804 				 err);
15805 	}
15806 
15807 #define PCI_SPEED_SIZE 8
15808 #define PCI_WIDTH_SIZE 8
15809 	/* Devices on the IOSF bus do not have this information
15810 	 * and will report PCI Gen 1 x 1 by default so don't bother
15811 	 * checking them.
15812 	 */
15813 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
15814 		char speed[PCI_SPEED_SIZE] = "Unknown";
15815 		char width[PCI_WIDTH_SIZE] = "Unknown";
15816 
15817 		/* Get the negotiated link width and speed from PCI config
15818 		 * space
15819 		 */
15820 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
15821 					  &link_status);
15822 
15823 		i40e_set_pci_config_data(hw, link_status);
15824 
15825 		switch (hw->bus.speed) {
15826 		case i40e_bus_speed_8000:
15827 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
15828 		case i40e_bus_speed_5000:
15829 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
15830 		case i40e_bus_speed_2500:
15831 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
15832 		default:
15833 			break;
15834 		}
15835 		switch (hw->bus.width) {
15836 		case i40e_bus_width_pcie_x8:
15837 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
15838 		case i40e_bus_width_pcie_x4:
15839 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
15840 		case i40e_bus_width_pcie_x2:
15841 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
15842 		case i40e_bus_width_pcie_x1:
15843 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
15844 		default:
15845 			break;
15846 		}
15847 
15848 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
15849 			 speed, width);
15850 
15851 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
15852 		    hw->bus.speed < i40e_bus_speed_8000) {
15853 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
15854 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
15855 		}
15856 	}
15857 
15858 	/* get the requested speeds from the fw */
15859 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
15860 	if (err)
15861 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
15862 			i40e_stat_str(&pf->hw, err),
15863 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15864 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
15865 
15866 	/* set the FEC config due to the board capabilities */
15867 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
15868 
15869 	/* get the supported phy types from the fw */
15870 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
15871 	if (err)
15872 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
15873 			i40e_stat_str(&pf->hw, err),
15874 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15875 
15876 	/* make sure the MFS hasn't been set lower than the default */
15877 #define MAX_FRAME_SIZE_DEFAULT 0x2600
15878 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
15879 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
15880 	if (val < MAX_FRAME_SIZE_DEFAULT)
15881 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
15882 			 i, val);
15883 
15884 	/* Add a filter to drop all Flow control frames from any VSI from being
15885 	 * transmitted. By doing so we stop a malicious VF from sending out
15886 	 * PAUSE or PFC frames and potentially controlling traffic for other
15887 	 * PF/VF VSIs.
15888 	 * The FW can still send Flow control frames if enabled.
15889 	 */
15890 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
15891 						       pf->main_vsi_seid);
15892 
15893 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
15894 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
15895 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
15896 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
15897 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
15898 	/* print a string summarizing features */
15899 	i40e_print_features(pf);
15900 
15901 	return 0;
15902 
15903 	/* Unwind what we've done if something failed in the setup */
15904 err_vsis:
15905 	set_bit(__I40E_DOWN, pf->state);
15906 	i40e_clear_interrupt_scheme(pf);
15907 	kfree(pf->vsi);
15908 err_switch_setup:
15909 	i40e_reset_interrupt_capability(pf);
15910 	del_timer_sync(&pf->service_timer);
15911 err_mac_addr:
15912 err_configure_lan_hmc:
15913 	(void)i40e_shutdown_lan_hmc(hw);
15914 err_init_lan_hmc:
15915 	kfree(pf->qp_pile);
15916 err_sw_init:
15917 err_adminq_setup:
15918 err_pf_reset:
15919 	iounmap(hw->hw_addr);
15920 err_ioremap:
15921 	kfree(pf);
15922 err_pf_alloc:
15923 	pci_disable_pcie_error_reporting(pdev);
15924 	pci_release_mem_regions(pdev);
15925 err_pci_reg:
15926 err_dma:
15927 	pci_disable_device(pdev);
15928 	return err;
15929 }
15930 
15931 /**
15932  * i40e_remove - Device removal routine
15933  * @pdev: PCI device information struct
15934  *
15935  * i40e_remove is called by the PCI subsystem to alert the driver
15936  * that is should release a PCI device.  This could be caused by a
15937  * Hot-Plug event, or because the driver is going to be removed from
15938  * memory.
15939  **/
15940 static void i40e_remove(struct pci_dev *pdev)
15941 {
15942 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15943 	struct i40e_hw *hw = &pf->hw;
15944 	i40e_status ret_code;
15945 	int i;
15946 
15947 	i40e_dbg_pf_exit(pf);
15948 
15949 	i40e_ptp_stop(pf);
15950 
15951 	/* Disable RSS in hw */
15952 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
15953 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
15954 
15955 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
15956 	 * flags, once they are set, i40e_rebuild should not be called as
15957 	 * i40e_prep_for_reset always returns early.
15958 	 */
15959 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
15960 		usleep_range(1000, 2000);
15961 	set_bit(__I40E_IN_REMOVE, pf->state);
15962 
15963 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
15964 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
15965 		i40e_free_vfs(pf);
15966 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
15967 	}
15968 	/* no more scheduling of any task */
15969 	set_bit(__I40E_SUSPENDED, pf->state);
15970 	set_bit(__I40E_DOWN, pf->state);
15971 	if (pf->service_timer.function)
15972 		del_timer_sync(&pf->service_timer);
15973 	if (pf->service_task.func)
15974 		cancel_work_sync(&pf->service_task);
15975 
15976 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
15977 		struct i40e_vsi *vsi = pf->vsi[0];
15978 
15979 		/* We know that we have allocated only one vsi for this PF,
15980 		 * it was just for registering netdevice, so the interface
15981 		 * could be visible in the 'ifconfig' output
15982 		 */
15983 		unregister_netdev(vsi->netdev);
15984 		free_netdev(vsi->netdev);
15985 
15986 		goto unmap;
15987 	}
15988 
15989 	/* Client close must be called explicitly here because the timer
15990 	 * has been stopped.
15991 	 */
15992 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15993 
15994 	i40e_fdir_teardown(pf);
15995 
15996 	/* If there is a switch structure or any orphans, remove them.
15997 	 * This will leave only the PF's VSI remaining.
15998 	 */
15999 	for (i = 0; i < I40E_MAX_VEB; i++) {
16000 		if (!pf->veb[i])
16001 			continue;
16002 
16003 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16004 		    pf->veb[i]->uplink_seid == 0)
16005 			i40e_switch_branch_release(pf->veb[i]);
16006 	}
16007 
16008 	/* Now we can shutdown the PF's VSI, just before we kill
16009 	 * adminq and hmc.
16010 	 */
16011 	if (pf->vsi[pf->lan_vsi])
16012 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
16013 
16014 	i40e_cloud_filter_exit(pf);
16015 
16016 	/* remove attached clients */
16017 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16018 		ret_code = i40e_lan_del_device(pf);
16019 		if (ret_code)
16020 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16021 				 ret_code);
16022 	}
16023 
16024 	/* shutdown and destroy the HMC */
16025 	if (hw->hmc.hmc_obj) {
16026 		ret_code = i40e_shutdown_lan_hmc(hw);
16027 		if (ret_code)
16028 			dev_warn(&pdev->dev,
16029 				 "Failed to destroy the HMC resources: %d\n",
16030 				 ret_code);
16031 	}
16032 
16033 unmap:
16034 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16035 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16036 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16037 		free_irq(pf->pdev->irq, pf);
16038 
16039 	/* shutdown the adminq */
16040 	i40e_shutdown_adminq(hw);
16041 
16042 	/* destroy the locks only once, here */
16043 	mutex_destroy(&hw->aq.arq_mutex);
16044 	mutex_destroy(&hw->aq.asq_mutex);
16045 
16046 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16047 	rtnl_lock();
16048 	i40e_clear_interrupt_scheme(pf);
16049 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16050 		if (pf->vsi[i]) {
16051 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16052 				i40e_vsi_clear_rings(pf->vsi[i]);
16053 			i40e_vsi_clear(pf->vsi[i]);
16054 			pf->vsi[i] = NULL;
16055 		}
16056 	}
16057 	rtnl_unlock();
16058 
16059 	for (i = 0; i < I40E_MAX_VEB; i++) {
16060 		kfree(pf->veb[i]);
16061 		pf->veb[i] = NULL;
16062 	}
16063 
16064 	kfree(pf->qp_pile);
16065 	kfree(pf->vsi);
16066 
16067 	iounmap(hw->hw_addr);
16068 	kfree(pf);
16069 	pci_release_mem_regions(pdev);
16070 
16071 	pci_disable_pcie_error_reporting(pdev);
16072 	pci_disable_device(pdev);
16073 }
16074 
16075 /**
16076  * i40e_pci_error_detected - warning that something funky happened in PCI land
16077  * @pdev: PCI device information struct
16078  * @error: the type of PCI error
16079  *
16080  * Called to warn that something happened and the error handling steps
16081  * are in progress.  Allows the driver to quiesce things, be ready for
16082  * remediation.
16083  **/
16084 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16085 						pci_channel_state_t error)
16086 {
16087 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16088 
16089 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16090 
16091 	if (!pf) {
16092 		dev_info(&pdev->dev,
16093 			 "Cannot recover - error happened during device probe\n");
16094 		return PCI_ERS_RESULT_DISCONNECT;
16095 	}
16096 
16097 	/* shutdown all operations */
16098 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16099 		i40e_prep_for_reset(pf);
16100 
16101 	/* Request a slot reset */
16102 	return PCI_ERS_RESULT_NEED_RESET;
16103 }
16104 
16105 /**
16106  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16107  * @pdev: PCI device information struct
16108  *
16109  * Called to find if the driver can work with the device now that
16110  * the pci slot has been reset.  If a basic connection seems good
16111  * (registers are readable and have sane content) then return a
16112  * happy little PCI_ERS_RESULT_xxx.
16113  **/
16114 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16115 {
16116 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16117 	pci_ers_result_t result;
16118 	u32 reg;
16119 
16120 	dev_dbg(&pdev->dev, "%s\n", __func__);
16121 	if (pci_enable_device_mem(pdev)) {
16122 		dev_info(&pdev->dev,
16123 			 "Cannot re-enable PCI device after reset.\n");
16124 		result = PCI_ERS_RESULT_DISCONNECT;
16125 	} else {
16126 		pci_set_master(pdev);
16127 		pci_restore_state(pdev);
16128 		pci_save_state(pdev);
16129 		pci_wake_from_d3(pdev, false);
16130 
16131 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16132 		if (reg == 0)
16133 			result = PCI_ERS_RESULT_RECOVERED;
16134 		else
16135 			result = PCI_ERS_RESULT_DISCONNECT;
16136 	}
16137 
16138 	return result;
16139 }
16140 
16141 /**
16142  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16143  * @pdev: PCI device information struct
16144  */
16145 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16146 {
16147 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16148 
16149 	i40e_prep_for_reset(pf);
16150 }
16151 
16152 /**
16153  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16154  * @pdev: PCI device information struct
16155  */
16156 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16157 {
16158 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16159 
16160 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16161 		return;
16162 
16163 	i40e_reset_and_rebuild(pf, false, false);
16164 }
16165 
16166 /**
16167  * i40e_pci_error_resume - restart operations after PCI error recovery
16168  * @pdev: PCI device information struct
16169  *
16170  * Called to allow the driver to bring things back up after PCI error
16171  * and/or reset recovery has finished.
16172  **/
16173 static void i40e_pci_error_resume(struct pci_dev *pdev)
16174 {
16175 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16176 
16177 	dev_dbg(&pdev->dev, "%s\n", __func__);
16178 	if (test_bit(__I40E_SUSPENDED, pf->state))
16179 		return;
16180 
16181 	i40e_handle_reset_warning(pf, false);
16182 }
16183 
16184 /**
16185  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16186  * using the mac_address_write admin q function
16187  * @pf: pointer to i40e_pf struct
16188  **/
16189 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16190 {
16191 	struct i40e_hw *hw = &pf->hw;
16192 	i40e_status ret;
16193 	u8 mac_addr[6];
16194 	u16 flags = 0;
16195 
16196 	/* Get current MAC address in case it's an LAA */
16197 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16198 		ether_addr_copy(mac_addr,
16199 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16200 	} else {
16201 		dev_err(&pf->pdev->dev,
16202 			"Failed to retrieve MAC address; using default\n");
16203 		ether_addr_copy(mac_addr, hw->mac.addr);
16204 	}
16205 
16206 	/* The FW expects the mac address write cmd to first be called with
16207 	 * one of these flags before calling it again with the multicast
16208 	 * enable flags.
16209 	 */
16210 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16211 
16212 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16213 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16214 
16215 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16216 	if (ret) {
16217 		dev_err(&pf->pdev->dev,
16218 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16219 		return;
16220 	}
16221 
16222 	flags = I40E_AQC_MC_MAG_EN
16223 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16224 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16225 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16226 	if (ret)
16227 		dev_err(&pf->pdev->dev,
16228 			"Failed to enable Multicast Magic Packet wake up\n");
16229 }
16230 
16231 /**
16232  * i40e_shutdown - PCI callback for shutting down
16233  * @pdev: PCI device information struct
16234  **/
16235 static void i40e_shutdown(struct pci_dev *pdev)
16236 {
16237 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16238 	struct i40e_hw *hw = &pf->hw;
16239 
16240 	set_bit(__I40E_SUSPENDED, pf->state);
16241 	set_bit(__I40E_DOWN, pf->state);
16242 
16243 	del_timer_sync(&pf->service_timer);
16244 	cancel_work_sync(&pf->service_task);
16245 	i40e_cloud_filter_exit(pf);
16246 	i40e_fdir_teardown(pf);
16247 
16248 	/* Client close must be called explicitly here because the timer
16249 	 * has been stopped.
16250 	 */
16251 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16252 
16253 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16254 		i40e_enable_mc_magic_wake(pf);
16255 
16256 	i40e_prep_for_reset(pf);
16257 
16258 	wr32(hw, I40E_PFPM_APM,
16259 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16260 	wr32(hw, I40E_PFPM_WUFC,
16261 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16262 
16263 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16264 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16265 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16266 		free_irq(pf->pdev->irq, pf);
16267 
16268 	/* Since we're going to destroy queues during the
16269 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16270 	 * whole section
16271 	 */
16272 	rtnl_lock();
16273 	i40e_clear_interrupt_scheme(pf);
16274 	rtnl_unlock();
16275 
16276 	if (system_state == SYSTEM_POWER_OFF) {
16277 		pci_wake_from_d3(pdev, pf->wol_en);
16278 		pci_set_power_state(pdev, PCI_D3hot);
16279 	}
16280 }
16281 
16282 /**
16283  * i40e_suspend - PM callback for moving to D3
16284  * @dev: generic device information structure
16285  **/
16286 static int __maybe_unused i40e_suspend(struct device *dev)
16287 {
16288 	struct i40e_pf *pf = dev_get_drvdata(dev);
16289 	struct i40e_hw *hw = &pf->hw;
16290 
16291 	/* If we're already suspended, then there is nothing to do */
16292 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16293 		return 0;
16294 
16295 	set_bit(__I40E_DOWN, pf->state);
16296 
16297 	/* Ensure service task will not be running */
16298 	del_timer_sync(&pf->service_timer);
16299 	cancel_work_sync(&pf->service_task);
16300 
16301 	/* Client close must be called explicitly here because the timer
16302 	 * has been stopped.
16303 	 */
16304 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16305 
16306 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16307 		i40e_enable_mc_magic_wake(pf);
16308 
16309 	/* Since we're going to destroy queues during the
16310 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16311 	 * whole section
16312 	 */
16313 	rtnl_lock();
16314 
16315 	i40e_prep_for_reset(pf);
16316 
16317 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16318 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16319 
16320 	/* Clear the interrupt scheme and release our IRQs so that the system
16321 	 * can safely hibernate even when there are a large number of CPUs.
16322 	 * Otherwise hibernation might fail when mapping all the vectors back
16323 	 * to CPU0.
16324 	 */
16325 	i40e_clear_interrupt_scheme(pf);
16326 
16327 	rtnl_unlock();
16328 
16329 	return 0;
16330 }
16331 
16332 /**
16333  * i40e_resume - PM callback for waking up from D3
16334  * @dev: generic device information structure
16335  **/
16336 static int __maybe_unused i40e_resume(struct device *dev)
16337 {
16338 	struct i40e_pf *pf = dev_get_drvdata(dev);
16339 	int err;
16340 
16341 	/* If we're not suspended, then there is nothing to do */
16342 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16343 		return 0;
16344 
16345 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16346 	 * since we're going to be restoring queues
16347 	 */
16348 	rtnl_lock();
16349 
16350 	/* We cleared the interrupt scheme when we suspended, so we need to
16351 	 * restore it now to resume device functionality.
16352 	 */
16353 	err = i40e_restore_interrupt_scheme(pf);
16354 	if (err) {
16355 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16356 			err);
16357 	}
16358 
16359 	clear_bit(__I40E_DOWN, pf->state);
16360 	i40e_reset_and_rebuild(pf, false, true);
16361 
16362 	rtnl_unlock();
16363 
16364 	/* Clear suspended state last after everything is recovered */
16365 	clear_bit(__I40E_SUSPENDED, pf->state);
16366 
16367 	/* Restart the service task */
16368 	mod_timer(&pf->service_timer,
16369 		  round_jiffies(jiffies + pf->service_timer_period));
16370 
16371 	return 0;
16372 }
16373 
16374 static const struct pci_error_handlers i40e_err_handler = {
16375 	.error_detected = i40e_pci_error_detected,
16376 	.slot_reset = i40e_pci_error_slot_reset,
16377 	.reset_prepare = i40e_pci_error_reset_prepare,
16378 	.reset_done = i40e_pci_error_reset_done,
16379 	.resume = i40e_pci_error_resume,
16380 };
16381 
16382 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16383 
16384 static struct pci_driver i40e_driver = {
16385 	.name     = i40e_driver_name,
16386 	.id_table = i40e_pci_tbl,
16387 	.probe    = i40e_probe,
16388 	.remove   = i40e_remove,
16389 	.driver   = {
16390 		.pm = &i40e_pm_ops,
16391 	},
16392 	.shutdown = i40e_shutdown,
16393 	.err_handler = &i40e_err_handler,
16394 	.sriov_configure = i40e_pci_sriov_configure,
16395 };
16396 
16397 /**
16398  * i40e_init_module - Driver registration routine
16399  *
16400  * i40e_init_module is the first routine called when the driver is
16401  * loaded. All it does is register with the PCI subsystem.
16402  **/
16403 static int __init i40e_init_module(void)
16404 {
16405 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16406 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16407 
16408 	/* There is no need to throttle the number of active tasks because
16409 	 * each device limits its own task using a state bit for scheduling
16410 	 * the service task, and the device tasks do not interfere with each
16411 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16412 	 * since we need to be able to guarantee forward progress even under
16413 	 * memory pressure.
16414 	 */
16415 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16416 	if (!i40e_wq) {
16417 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16418 		return -ENOMEM;
16419 	}
16420 
16421 	i40e_dbg_init();
16422 	return pci_register_driver(&i40e_driver);
16423 }
16424 module_init(i40e_init_module);
16425 
16426 /**
16427  * i40e_exit_module - Driver exit cleanup routine
16428  *
16429  * i40e_exit_module is called just before the driver is removed
16430  * from memory.
16431  **/
16432 static void __exit i40e_exit_module(void)
16433 {
16434 	pci_unregister_driver(&i40e_driver);
16435 	destroy_workqueue(i40e_wq);
16436 	ida_destroy(&i40e_client_ida);
16437 	i40e_dbg_exit();
16438 }
16439 module_exit(i40e_exit_module);
16440