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 	struct i40e_pf *pf = vsi->back;
777 	struct rtnl_link_stats64 *ons;
778 	struct rtnl_link_stats64 *ns;   /* netdev stats */
779 	struct i40e_eth_stats *oes;
780 	struct i40e_eth_stats *es;     /* device's eth stats */
781 	u64 tx_restart, tx_busy;
782 	struct i40e_ring *p;
783 	u64 rx_page, rx_buf;
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 	rcu_read_lock();
810 	for (q = 0; q < vsi->num_queue_pairs; q++) {
811 		/* locate Tx ring */
812 		p = READ_ONCE(vsi->tx_rings[q]);
813 		if (!p)
814 			continue;
815 
816 		do {
817 			start = u64_stats_fetch_begin_irq(&p->syncp);
818 			packets = p->stats.packets;
819 			bytes = p->stats.bytes;
820 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
821 		tx_b += bytes;
822 		tx_p += packets;
823 		tx_restart += p->tx_stats.restart_queue;
824 		tx_busy += p->tx_stats.tx_busy;
825 		tx_linearize += p->tx_stats.tx_linearize;
826 		tx_force_wb += p->tx_stats.tx_force_wb;
827 
828 		/* locate Rx ring */
829 		p = READ_ONCE(vsi->rx_rings[q]);
830 		if (!p)
831 			continue;
832 
833 		do {
834 			start = u64_stats_fetch_begin_irq(&p->syncp);
835 			packets = p->stats.packets;
836 			bytes = p->stats.bytes;
837 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
838 		rx_b += bytes;
839 		rx_p += packets;
840 		rx_buf += p->rx_stats.alloc_buff_failed;
841 		rx_page += p->rx_stats.alloc_page_failed;
842 
843 		if (i40e_enabled_xdp_vsi(vsi)) {
844 			/* locate XDP ring */
845 			p = READ_ONCE(vsi->xdp_rings[q]);
846 			if (!p)
847 				continue;
848 
849 			do {
850 				start = u64_stats_fetch_begin_irq(&p->syncp);
851 				packets = p->stats.packets;
852 				bytes = p->stats.bytes;
853 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
854 			tx_b += bytes;
855 			tx_p += packets;
856 			tx_restart += p->tx_stats.restart_queue;
857 			tx_busy += p->tx_stats.tx_busy;
858 			tx_linearize += p->tx_stats.tx_linearize;
859 			tx_force_wb += p->tx_stats.tx_force_wb;
860 		}
861 	}
862 	rcu_read_unlock();
863 	vsi->tx_restart = tx_restart;
864 	vsi->tx_busy = tx_busy;
865 	vsi->tx_linearize = tx_linearize;
866 	vsi->tx_force_wb = tx_force_wb;
867 	vsi->rx_page_failed = rx_page;
868 	vsi->rx_buf_failed = rx_buf;
869 
870 	ns->rx_packets = rx_p;
871 	ns->rx_bytes = rx_b;
872 	ns->tx_packets = tx_p;
873 	ns->tx_bytes = tx_b;
874 
875 	/* update netdev stats from eth stats */
876 	i40e_update_eth_stats(vsi);
877 	ons->tx_errors = oes->tx_errors;
878 	ns->tx_errors = es->tx_errors;
879 	ons->multicast = oes->rx_multicast;
880 	ns->multicast = es->rx_multicast;
881 	ons->rx_dropped = oes->rx_discards;
882 	ns->rx_dropped = es->rx_discards;
883 	ons->tx_dropped = oes->tx_discards;
884 	ns->tx_dropped = es->tx_discards;
885 
886 	/* pull in a couple PF stats if this is the main vsi */
887 	if (vsi == pf->vsi[pf->lan_vsi]) {
888 		ns->rx_crc_errors = pf->stats.crc_errors;
889 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
890 		ns->rx_length_errors = pf->stats.rx_length_errors;
891 	}
892 }
893 
894 /**
895  * i40e_update_pf_stats - Update the PF statistics counters.
896  * @pf: the PF to be updated
897  **/
898 static void i40e_update_pf_stats(struct i40e_pf *pf)
899 {
900 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
901 	struct i40e_hw_port_stats *nsd = &pf->stats;
902 	struct i40e_hw *hw = &pf->hw;
903 	u32 val;
904 	int i;
905 
906 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
907 			   I40E_GLPRT_GORCL(hw->port),
908 			   pf->stat_offsets_loaded,
909 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
910 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
911 			   I40E_GLPRT_GOTCL(hw->port),
912 			   pf->stat_offsets_loaded,
913 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
914 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
915 			   pf->stat_offsets_loaded,
916 			   &osd->eth.rx_discards,
917 			   &nsd->eth.rx_discards);
918 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
919 			   I40E_GLPRT_UPRCL(hw->port),
920 			   pf->stat_offsets_loaded,
921 			   &osd->eth.rx_unicast,
922 			   &nsd->eth.rx_unicast);
923 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
924 			   I40E_GLPRT_MPRCL(hw->port),
925 			   pf->stat_offsets_loaded,
926 			   &osd->eth.rx_multicast,
927 			   &nsd->eth.rx_multicast);
928 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
929 			   I40E_GLPRT_BPRCL(hw->port),
930 			   pf->stat_offsets_loaded,
931 			   &osd->eth.rx_broadcast,
932 			   &nsd->eth.rx_broadcast);
933 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
934 			   I40E_GLPRT_UPTCL(hw->port),
935 			   pf->stat_offsets_loaded,
936 			   &osd->eth.tx_unicast,
937 			   &nsd->eth.tx_unicast);
938 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
939 			   I40E_GLPRT_MPTCL(hw->port),
940 			   pf->stat_offsets_loaded,
941 			   &osd->eth.tx_multicast,
942 			   &nsd->eth.tx_multicast);
943 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
944 			   I40E_GLPRT_BPTCL(hw->port),
945 			   pf->stat_offsets_loaded,
946 			   &osd->eth.tx_broadcast,
947 			   &nsd->eth.tx_broadcast);
948 
949 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
950 			   pf->stat_offsets_loaded,
951 			   &osd->tx_dropped_link_down,
952 			   &nsd->tx_dropped_link_down);
953 
954 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
955 			   pf->stat_offsets_loaded,
956 			   &osd->crc_errors, &nsd->crc_errors);
957 
958 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
959 			   pf->stat_offsets_loaded,
960 			   &osd->illegal_bytes, &nsd->illegal_bytes);
961 
962 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
963 			   pf->stat_offsets_loaded,
964 			   &osd->mac_local_faults,
965 			   &nsd->mac_local_faults);
966 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
967 			   pf->stat_offsets_loaded,
968 			   &osd->mac_remote_faults,
969 			   &nsd->mac_remote_faults);
970 
971 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
972 			   pf->stat_offsets_loaded,
973 			   &osd->rx_length_errors,
974 			   &nsd->rx_length_errors);
975 
976 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
977 			   pf->stat_offsets_loaded,
978 			   &osd->link_xon_rx, &nsd->link_xon_rx);
979 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
980 			   pf->stat_offsets_loaded,
981 			   &osd->link_xon_tx, &nsd->link_xon_tx);
982 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
983 			   pf->stat_offsets_loaded,
984 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
985 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
986 			   pf->stat_offsets_loaded,
987 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
988 
989 	for (i = 0; i < 8; i++) {
990 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
991 				   pf->stat_offsets_loaded,
992 				   &osd->priority_xoff_rx[i],
993 				   &nsd->priority_xoff_rx[i]);
994 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
995 				   pf->stat_offsets_loaded,
996 				   &osd->priority_xon_rx[i],
997 				   &nsd->priority_xon_rx[i]);
998 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
999 				   pf->stat_offsets_loaded,
1000 				   &osd->priority_xon_tx[i],
1001 				   &nsd->priority_xon_tx[i]);
1002 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1003 				   pf->stat_offsets_loaded,
1004 				   &osd->priority_xoff_tx[i],
1005 				   &nsd->priority_xoff_tx[i]);
1006 		i40e_stat_update32(hw,
1007 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1008 				   pf->stat_offsets_loaded,
1009 				   &osd->priority_xon_2_xoff[i],
1010 				   &nsd->priority_xon_2_xoff[i]);
1011 	}
1012 
1013 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1014 			   I40E_GLPRT_PRC64L(hw->port),
1015 			   pf->stat_offsets_loaded,
1016 			   &osd->rx_size_64, &nsd->rx_size_64);
1017 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1018 			   I40E_GLPRT_PRC127L(hw->port),
1019 			   pf->stat_offsets_loaded,
1020 			   &osd->rx_size_127, &nsd->rx_size_127);
1021 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1022 			   I40E_GLPRT_PRC255L(hw->port),
1023 			   pf->stat_offsets_loaded,
1024 			   &osd->rx_size_255, &nsd->rx_size_255);
1025 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1026 			   I40E_GLPRT_PRC511L(hw->port),
1027 			   pf->stat_offsets_loaded,
1028 			   &osd->rx_size_511, &nsd->rx_size_511);
1029 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1030 			   I40E_GLPRT_PRC1023L(hw->port),
1031 			   pf->stat_offsets_loaded,
1032 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1033 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1034 			   I40E_GLPRT_PRC1522L(hw->port),
1035 			   pf->stat_offsets_loaded,
1036 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1037 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1038 			   I40E_GLPRT_PRC9522L(hw->port),
1039 			   pf->stat_offsets_loaded,
1040 			   &osd->rx_size_big, &nsd->rx_size_big);
1041 
1042 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1043 			   I40E_GLPRT_PTC64L(hw->port),
1044 			   pf->stat_offsets_loaded,
1045 			   &osd->tx_size_64, &nsd->tx_size_64);
1046 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1047 			   I40E_GLPRT_PTC127L(hw->port),
1048 			   pf->stat_offsets_loaded,
1049 			   &osd->tx_size_127, &nsd->tx_size_127);
1050 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1051 			   I40E_GLPRT_PTC255L(hw->port),
1052 			   pf->stat_offsets_loaded,
1053 			   &osd->tx_size_255, &nsd->tx_size_255);
1054 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1055 			   I40E_GLPRT_PTC511L(hw->port),
1056 			   pf->stat_offsets_loaded,
1057 			   &osd->tx_size_511, &nsd->tx_size_511);
1058 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1059 			   I40E_GLPRT_PTC1023L(hw->port),
1060 			   pf->stat_offsets_loaded,
1061 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1062 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1063 			   I40E_GLPRT_PTC1522L(hw->port),
1064 			   pf->stat_offsets_loaded,
1065 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1066 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1067 			   I40E_GLPRT_PTC9522L(hw->port),
1068 			   pf->stat_offsets_loaded,
1069 			   &osd->tx_size_big, &nsd->tx_size_big);
1070 
1071 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1072 			   pf->stat_offsets_loaded,
1073 			   &osd->rx_undersize, &nsd->rx_undersize);
1074 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1075 			   pf->stat_offsets_loaded,
1076 			   &osd->rx_fragments, &nsd->rx_fragments);
1077 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1078 			   pf->stat_offsets_loaded,
1079 			   &osd->rx_oversize, &nsd->rx_oversize);
1080 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1081 			   pf->stat_offsets_loaded,
1082 			   &osd->rx_jabber, &nsd->rx_jabber);
1083 
1084 	/* FDIR stats */
1085 	i40e_stat_update_and_clear32(hw,
1086 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1087 			&nsd->fd_atr_match);
1088 	i40e_stat_update_and_clear32(hw,
1089 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1090 			&nsd->fd_sb_match);
1091 	i40e_stat_update_and_clear32(hw,
1092 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1093 			&nsd->fd_atr_tunnel_match);
1094 
1095 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1096 	nsd->tx_lpi_status =
1097 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1098 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1099 	nsd->rx_lpi_status =
1100 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1101 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1102 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1103 			   pf->stat_offsets_loaded,
1104 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1105 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1106 			   pf->stat_offsets_loaded,
1107 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1108 
1109 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1110 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1111 		nsd->fd_sb_status = true;
1112 	else
1113 		nsd->fd_sb_status = false;
1114 
1115 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1116 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1117 		nsd->fd_atr_status = true;
1118 	else
1119 		nsd->fd_atr_status = false;
1120 
1121 	pf->stat_offsets_loaded = true;
1122 }
1123 
1124 /**
1125  * i40e_update_stats - Update the various statistics counters.
1126  * @vsi: the VSI to be updated
1127  *
1128  * Update the various stats for this VSI and its related entities.
1129  **/
1130 void i40e_update_stats(struct i40e_vsi *vsi)
1131 {
1132 	struct i40e_pf *pf = vsi->back;
1133 
1134 	if (vsi == pf->vsi[pf->lan_vsi])
1135 		i40e_update_pf_stats(pf);
1136 
1137 	i40e_update_vsi_stats(vsi);
1138 }
1139 
1140 /**
1141  * i40e_count_filters - counts VSI mac filters
1142  * @vsi: the VSI to be searched
1143  *
1144  * Returns count of mac filters
1145  **/
1146 int i40e_count_filters(struct i40e_vsi *vsi)
1147 {
1148 	struct i40e_mac_filter *f;
1149 	struct hlist_node *h;
1150 	int bkt;
1151 	int cnt = 0;
1152 
1153 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1154 		++cnt;
1155 
1156 	return cnt;
1157 }
1158 
1159 /**
1160  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1161  * @vsi: the VSI to be searched
1162  * @macaddr: the MAC address
1163  * @vlan: the vlan
1164  *
1165  * Returns ptr to the filter object or NULL
1166  **/
1167 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1168 						const u8 *macaddr, s16 vlan)
1169 {
1170 	struct i40e_mac_filter *f;
1171 	u64 key;
1172 
1173 	if (!vsi || !macaddr)
1174 		return NULL;
1175 
1176 	key = i40e_addr_to_hkey(macaddr);
1177 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1178 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1179 		    (vlan == f->vlan))
1180 			return f;
1181 	}
1182 	return NULL;
1183 }
1184 
1185 /**
1186  * i40e_find_mac - Find a mac addr in the macvlan filters list
1187  * @vsi: the VSI to be searched
1188  * @macaddr: the MAC address we are searching for
1189  *
1190  * Returns the first filter with the provided MAC address or NULL if
1191  * MAC address was not found
1192  **/
1193 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1194 {
1195 	struct i40e_mac_filter *f;
1196 	u64 key;
1197 
1198 	if (!vsi || !macaddr)
1199 		return NULL;
1200 
1201 	key = i40e_addr_to_hkey(macaddr);
1202 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1203 		if ((ether_addr_equal(macaddr, f->macaddr)))
1204 			return f;
1205 	}
1206 	return NULL;
1207 }
1208 
1209 /**
1210  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1211  * @vsi: the VSI to be searched
1212  *
1213  * Returns true if VSI is in vlan mode or false otherwise
1214  **/
1215 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1216 {
1217 	/* If we have a PVID, always operate in VLAN mode */
1218 	if (vsi->info.pvid)
1219 		return true;
1220 
1221 	/* We need to operate in VLAN mode whenever we have any filters with
1222 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1223 	 * time, incurring search cost repeatedly. However, we can notice two
1224 	 * things:
1225 	 *
1226 	 * 1) the only place where we can gain a VLAN filter is in
1227 	 *    i40e_add_filter.
1228 	 *
1229 	 * 2) the only place where filters are actually removed is in
1230 	 *    i40e_sync_filters_subtask.
1231 	 *
1232 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1233 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1234 	 * we have to perform the full search after deleting filters in
1235 	 * i40e_sync_filters_subtask, but we already have to search
1236 	 * filters here and can perform the check at the same time. This
1237 	 * results in avoiding embedding a loop for VLAN mode inside another
1238 	 * loop over all the filters, and should maintain correctness as noted
1239 	 * above.
1240 	 */
1241 	return vsi->has_vlan_filter;
1242 }
1243 
1244 /**
1245  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1246  * @vsi: the VSI to configure
1247  * @tmp_add_list: list of filters ready to be added
1248  * @tmp_del_list: list of filters ready to be deleted
1249  * @vlan_filters: the number of active VLAN filters
1250  *
1251  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1252  * behave as expected. If we have any active VLAN filters remaining or about
1253  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1254  * so that they only match against untagged traffic. If we no longer have any
1255  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1256  * so that they match against both tagged and untagged traffic. In this way,
1257  * we ensure that we correctly receive the desired traffic. This ensures that
1258  * when we have an active VLAN we will receive only untagged traffic and
1259  * traffic matching active VLANs. If we have no active VLANs then we will
1260  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1261  *
1262  * Finally, in a similar fashion, this function also corrects filters when
1263  * there is an active PVID assigned to this VSI.
1264  *
1265  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1266  *
1267  * This function is only expected to be called from within
1268  * i40e_sync_vsi_filters.
1269  *
1270  * NOTE: This function expects to be called while under the
1271  * mac_filter_hash_lock
1272  */
1273 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1274 					 struct hlist_head *tmp_add_list,
1275 					 struct hlist_head *tmp_del_list,
1276 					 int vlan_filters)
1277 {
1278 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1279 	struct i40e_mac_filter *f, *add_head;
1280 	struct i40e_new_mac_filter *new;
1281 	struct hlist_node *h;
1282 	int bkt, new_vlan;
1283 
1284 	/* To determine if a particular filter needs to be replaced we
1285 	 * have the three following conditions:
1286 	 *
1287 	 * a) if we have a PVID assigned, then all filters which are
1288 	 *    not marked as VLAN=PVID must be replaced with filters that
1289 	 *    are.
1290 	 * b) otherwise, if we have any active VLANS, all filters
1291 	 *    which are marked as VLAN=-1 must be replaced with
1292 	 *    filters marked as VLAN=0
1293 	 * c) finally, if we do not have any active VLANS, all filters
1294 	 *    which are marked as VLAN=0 must be replaced with filters
1295 	 *    marked as VLAN=-1
1296 	 */
1297 
1298 	/* Update the filters about to be added in place */
1299 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1300 		if (pvid && new->f->vlan != pvid)
1301 			new->f->vlan = pvid;
1302 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1303 			new->f->vlan = 0;
1304 		else if (!vlan_filters && new->f->vlan == 0)
1305 			new->f->vlan = I40E_VLAN_ANY;
1306 	}
1307 
1308 	/* Update the remaining active filters */
1309 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1310 		/* Combine the checks for whether a filter needs to be changed
1311 		 * and then determine the new VLAN inside the if block, in
1312 		 * order to avoid duplicating code for adding the new filter
1313 		 * then deleting the old filter.
1314 		 */
1315 		if ((pvid && f->vlan != pvid) ||
1316 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1317 		    (!vlan_filters && f->vlan == 0)) {
1318 			/* Determine the new vlan we will be adding */
1319 			if (pvid)
1320 				new_vlan = pvid;
1321 			else if (vlan_filters)
1322 				new_vlan = 0;
1323 			else
1324 				new_vlan = I40E_VLAN_ANY;
1325 
1326 			/* Create the new filter */
1327 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1328 			if (!add_head)
1329 				return -ENOMEM;
1330 
1331 			/* Create a temporary i40e_new_mac_filter */
1332 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1333 			if (!new)
1334 				return -ENOMEM;
1335 
1336 			new->f = add_head;
1337 			new->state = add_head->state;
1338 
1339 			/* Add the new filter to the tmp list */
1340 			hlist_add_head(&new->hlist, tmp_add_list);
1341 
1342 			/* Put the original filter into the delete list */
1343 			f->state = I40E_FILTER_REMOVE;
1344 			hash_del(&f->hlist);
1345 			hlist_add_head(&f->hlist, tmp_del_list);
1346 		}
1347 	}
1348 
1349 	vsi->has_vlan_filter = !!vlan_filters;
1350 
1351 	return 0;
1352 }
1353 
1354 /**
1355  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1356  * @vsi: the PF Main VSI - inappropriate for any other VSI
1357  * @macaddr: the MAC address
1358  *
1359  * Remove whatever filter the firmware set up so the driver can manage
1360  * its own filtering intelligently.
1361  **/
1362 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1363 {
1364 	struct i40e_aqc_remove_macvlan_element_data element;
1365 	struct i40e_pf *pf = vsi->back;
1366 
1367 	/* Only appropriate for the PF main VSI */
1368 	if (vsi->type != I40E_VSI_MAIN)
1369 		return;
1370 
1371 	memset(&element, 0, sizeof(element));
1372 	ether_addr_copy(element.mac_addr, macaddr);
1373 	element.vlan_tag = 0;
1374 	/* Ignore error returns, some firmware does it this way... */
1375 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1376 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1377 
1378 	memset(&element, 0, sizeof(element));
1379 	ether_addr_copy(element.mac_addr, macaddr);
1380 	element.vlan_tag = 0;
1381 	/* ...and some firmware does it this way. */
1382 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1383 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1384 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1385 }
1386 
1387 /**
1388  * i40e_add_filter - Add a mac/vlan filter to the VSI
1389  * @vsi: the VSI to be searched
1390  * @macaddr: the MAC address
1391  * @vlan: the vlan
1392  *
1393  * Returns ptr to the filter object or NULL when no memory available.
1394  *
1395  * NOTE: This function is expected to be called with mac_filter_hash_lock
1396  * being held.
1397  **/
1398 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1399 					const u8 *macaddr, s16 vlan)
1400 {
1401 	struct i40e_mac_filter *f;
1402 	u64 key;
1403 
1404 	if (!vsi || !macaddr)
1405 		return NULL;
1406 
1407 	f = i40e_find_filter(vsi, macaddr, vlan);
1408 	if (!f) {
1409 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1410 		if (!f)
1411 			return NULL;
1412 
1413 		/* Update the boolean indicating if we need to function in
1414 		 * VLAN mode.
1415 		 */
1416 		if (vlan >= 0)
1417 			vsi->has_vlan_filter = true;
1418 
1419 		ether_addr_copy(f->macaddr, macaddr);
1420 		f->vlan = vlan;
1421 		f->state = I40E_FILTER_NEW;
1422 		INIT_HLIST_NODE(&f->hlist);
1423 
1424 		key = i40e_addr_to_hkey(macaddr);
1425 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1426 
1427 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1428 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1429 	}
1430 
1431 	/* If we're asked to add a filter that has been marked for removal, it
1432 	 * is safe to simply restore it to active state. __i40e_del_filter
1433 	 * will have simply deleted any filters which were previously marked
1434 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1435 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1436 	 * task, just restore this filter to the ACTIVE state so that the
1437 	 * sync task leaves it in place
1438 	 */
1439 	if (f->state == I40E_FILTER_REMOVE)
1440 		f->state = I40E_FILTER_ACTIVE;
1441 
1442 	return f;
1443 }
1444 
1445 /**
1446  * __i40e_del_filter - Remove a specific filter from the VSI
1447  * @vsi: VSI to remove from
1448  * @f: the filter to remove from the list
1449  *
1450  * This function should be called instead of i40e_del_filter only if you know
1451  * the exact filter you will remove already, such as via i40e_find_filter or
1452  * i40e_find_mac.
1453  *
1454  * NOTE: This function is expected to be called with mac_filter_hash_lock
1455  * being held.
1456  * ANOTHER NOTE: This function MUST be called from within the context of
1457  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1458  * instead of list_for_each_entry().
1459  **/
1460 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1461 {
1462 	if (!f)
1463 		return;
1464 
1465 	/* If the filter was never added to firmware then we can just delete it
1466 	 * directly and we don't want to set the status to remove or else an
1467 	 * admin queue command will unnecessarily fire.
1468 	 */
1469 	if ((f->state == I40E_FILTER_FAILED) ||
1470 	    (f->state == I40E_FILTER_NEW)) {
1471 		hash_del(&f->hlist);
1472 		kfree(f);
1473 	} else {
1474 		f->state = I40E_FILTER_REMOVE;
1475 	}
1476 
1477 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1478 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1479 }
1480 
1481 /**
1482  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1483  * @vsi: the VSI to be searched
1484  * @macaddr: the MAC address
1485  * @vlan: the VLAN
1486  *
1487  * NOTE: This function is expected to be called with mac_filter_hash_lock
1488  * being held.
1489  * ANOTHER NOTE: This function MUST be called from within the context of
1490  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1491  * instead of list_for_each_entry().
1492  **/
1493 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1494 {
1495 	struct i40e_mac_filter *f;
1496 
1497 	if (!vsi || !macaddr)
1498 		return;
1499 
1500 	f = i40e_find_filter(vsi, macaddr, vlan);
1501 	__i40e_del_filter(vsi, f);
1502 }
1503 
1504 /**
1505  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1506  * @vsi: the VSI to be searched
1507  * @macaddr: the mac address to be filtered
1508  *
1509  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1510  * go through all the macvlan filters and add a macvlan filter for each
1511  * unique vlan that already exists. If a PVID has been assigned, instead only
1512  * add the macaddr to that VLAN.
1513  *
1514  * Returns last filter added on success, else NULL
1515  **/
1516 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1517 					    const u8 *macaddr)
1518 {
1519 	struct i40e_mac_filter *f, *add = NULL;
1520 	struct hlist_node *h;
1521 	int bkt;
1522 
1523 	if (vsi->info.pvid)
1524 		return i40e_add_filter(vsi, macaddr,
1525 				       le16_to_cpu(vsi->info.pvid));
1526 
1527 	if (!i40e_is_vsi_in_vlan(vsi))
1528 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1529 
1530 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1531 		if (f->state == I40E_FILTER_REMOVE)
1532 			continue;
1533 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1534 		if (!add)
1535 			return NULL;
1536 	}
1537 
1538 	return add;
1539 }
1540 
1541 /**
1542  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1543  * @vsi: the VSI to be searched
1544  * @macaddr: the mac address to be removed
1545  *
1546  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1547  * associated with.
1548  *
1549  * Returns 0 for success, or error
1550  **/
1551 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1552 {
1553 	struct i40e_mac_filter *f;
1554 	struct hlist_node *h;
1555 	bool found = false;
1556 	int bkt;
1557 
1558 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1559 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1560 		if (ether_addr_equal(macaddr, f->macaddr)) {
1561 			__i40e_del_filter(vsi, f);
1562 			found = true;
1563 		}
1564 	}
1565 
1566 	if (found)
1567 		return 0;
1568 	else
1569 		return -ENOENT;
1570 }
1571 
1572 /**
1573  * i40e_set_mac - NDO callback to set mac address
1574  * @netdev: network interface device structure
1575  * @p: pointer to an address structure
1576  *
1577  * Returns 0 on success, negative on failure
1578  **/
1579 static int i40e_set_mac(struct net_device *netdev, void *p)
1580 {
1581 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1582 	struct i40e_vsi *vsi = np->vsi;
1583 	struct i40e_pf *pf = vsi->back;
1584 	struct i40e_hw *hw = &pf->hw;
1585 	struct sockaddr *addr = p;
1586 
1587 	if (!is_valid_ether_addr(addr->sa_data))
1588 		return -EADDRNOTAVAIL;
1589 
1590 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1591 		netdev_info(netdev, "already using mac address %pM\n",
1592 			    addr->sa_data);
1593 		return 0;
1594 	}
1595 
1596 	if (test_bit(__I40E_DOWN, pf->state) ||
1597 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1598 		return -EADDRNOTAVAIL;
1599 
1600 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1601 		netdev_info(netdev, "returning to hw mac address %pM\n",
1602 			    hw->mac.addr);
1603 	else
1604 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1605 
1606 	/* Copy the address first, so that we avoid a possible race with
1607 	 * .set_rx_mode().
1608 	 * - Remove old address from MAC filter
1609 	 * - Copy new address
1610 	 * - Add new address to MAC filter
1611 	 */
1612 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1613 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1614 	eth_hw_addr_set(netdev, addr->sa_data);
1615 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1616 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1617 
1618 	if (vsi->type == I40E_VSI_MAIN) {
1619 		i40e_status ret;
1620 
1621 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1622 						addr->sa_data, NULL);
1623 		if (ret)
1624 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1625 				    i40e_stat_str(hw, ret),
1626 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1627 	}
1628 
1629 	/* schedule our worker thread which will take care of
1630 	 * applying the new filter changes
1631 	 */
1632 	i40e_service_event_schedule(pf);
1633 	return 0;
1634 }
1635 
1636 /**
1637  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1638  * @vsi: vsi structure
1639  * @seed: RSS hash seed
1640  * @lut: pointer to lookup table of lut_size
1641  * @lut_size: size of the lookup table
1642  **/
1643 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1644 			      u8 *lut, u16 lut_size)
1645 {
1646 	struct i40e_pf *pf = vsi->back;
1647 	struct i40e_hw *hw = &pf->hw;
1648 	int ret = 0;
1649 
1650 	if (seed) {
1651 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1652 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1653 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1654 		if (ret) {
1655 			dev_info(&pf->pdev->dev,
1656 				 "Cannot set RSS key, err %s aq_err %s\n",
1657 				 i40e_stat_str(hw, ret),
1658 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1659 			return ret;
1660 		}
1661 	}
1662 	if (lut) {
1663 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1664 
1665 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1666 		if (ret) {
1667 			dev_info(&pf->pdev->dev,
1668 				 "Cannot set RSS lut, 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 	return ret;
1675 }
1676 
1677 /**
1678  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1679  * @vsi: VSI structure
1680  **/
1681 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1682 {
1683 	struct i40e_pf *pf = vsi->back;
1684 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1685 	u8 *lut;
1686 	int ret;
1687 
1688 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1689 		return 0;
1690 	if (!vsi->rss_size)
1691 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1692 				      vsi->num_queue_pairs);
1693 	if (!vsi->rss_size)
1694 		return -EINVAL;
1695 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1696 	if (!lut)
1697 		return -ENOMEM;
1698 
1699 	/* Use the user configured hash keys and lookup table if there is one,
1700 	 * otherwise use default
1701 	 */
1702 	if (vsi->rss_lut_user)
1703 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1704 	else
1705 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1706 	if (vsi->rss_hkey_user)
1707 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1708 	else
1709 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1710 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1711 	kfree(lut);
1712 	return ret;
1713 }
1714 
1715 /**
1716  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1717  * @vsi: the VSI being configured,
1718  * @ctxt: VSI context structure
1719  * @enabled_tc: number of traffic classes to enable
1720  *
1721  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1722  **/
1723 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1724 					   struct i40e_vsi_context *ctxt,
1725 					   u8 enabled_tc)
1726 {
1727 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1728 	int i, override_q, pow, num_qps, ret;
1729 	u8 netdev_tc = 0, offset = 0;
1730 
1731 	if (vsi->type != I40E_VSI_MAIN)
1732 		return -EINVAL;
1733 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1734 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1735 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1736 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1737 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1738 
1739 	/* find the next higher power-of-2 of num queue pairs */
1740 	pow = ilog2(num_qps);
1741 	if (!is_power_of_2(num_qps))
1742 		pow++;
1743 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1744 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1745 
1746 	/* Setup queue offset/count for all TCs for given VSI */
1747 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1748 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1749 		/* See if the given TC is enabled for the given VSI */
1750 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1751 			offset = vsi->mqprio_qopt.qopt.offset[i];
1752 			qcount = vsi->mqprio_qopt.qopt.count[i];
1753 			if (qcount > max_qcount)
1754 				max_qcount = qcount;
1755 			vsi->tc_config.tc_info[i].qoffset = offset;
1756 			vsi->tc_config.tc_info[i].qcount = qcount;
1757 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1758 		} else {
1759 			/* TC is not enabled so set the offset to
1760 			 * default queue and allocate one queue
1761 			 * for the given TC.
1762 			 */
1763 			vsi->tc_config.tc_info[i].qoffset = 0;
1764 			vsi->tc_config.tc_info[i].qcount = 1;
1765 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1766 		}
1767 	}
1768 
1769 	/* Set actual Tx/Rx queue pairs */
1770 	vsi->num_queue_pairs = offset + qcount;
1771 
1772 	/* Setup queue TC[0].qmap for given VSI context */
1773 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1774 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1775 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1776 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1777 
1778 	/* Reconfigure RSS for main VSI with max queue count */
1779 	vsi->rss_size = max_qcount;
1780 	ret = i40e_vsi_config_rss(vsi);
1781 	if (ret) {
1782 		dev_info(&vsi->back->pdev->dev,
1783 			 "Failed to reconfig rss for num_queues (%u)\n",
1784 			 max_qcount);
1785 		return ret;
1786 	}
1787 	vsi->reconfig_rss = true;
1788 	dev_dbg(&vsi->back->pdev->dev,
1789 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1790 
1791 	/* Find queue count available for channel VSIs and starting offset
1792 	 * for channel VSIs
1793 	 */
1794 	override_q = vsi->mqprio_qopt.qopt.count[0];
1795 	if (override_q && override_q < vsi->num_queue_pairs) {
1796 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1797 		vsi->next_base_queue = override_q;
1798 	}
1799 	return 0;
1800 }
1801 
1802 /**
1803  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1804  * @vsi: the VSI being setup
1805  * @ctxt: VSI context structure
1806  * @enabled_tc: Enabled TCs bitmap
1807  * @is_add: True if called before Add VSI
1808  *
1809  * Setup VSI queue mapping for enabled traffic classes.
1810  **/
1811 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1812 				     struct i40e_vsi_context *ctxt,
1813 				     u8 enabled_tc,
1814 				     bool is_add)
1815 {
1816 	struct i40e_pf *pf = vsi->back;
1817 	u16 num_tc_qps = 0;
1818 	u16 sections = 0;
1819 	u8 netdev_tc = 0;
1820 	u16 numtc = 1;
1821 	u16 qcount;
1822 	u8 offset;
1823 	u16 qmap;
1824 	int i;
1825 
1826 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1827 	offset = 0;
1828 	/* zero out queue mapping, it will get updated on the end of the function */
1829 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
1830 
1831 	if (vsi->type == I40E_VSI_MAIN) {
1832 		/* This code helps add more queue to the VSI if we have
1833 		 * more cores than RSS can support, the higher cores will
1834 		 * be served by ATR or other filters. Furthermore, the
1835 		 * non-zero req_queue_pairs says that user requested a new
1836 		 * queue count via ethtool's set_channels, so use this
1837 		 * value for queues distribution across traffic classes
1838 		 */
1839 		if (vsi->req_queue_pairs > 0)
1840 			vsi->num_queue_pairs = vsi->req_queue_pairs;
1841 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1842 			vsi->num_queue_pairs = pf->num_lan_msix;
1843 	}
1844 
1845 	/* Number of queues per enabled TC */
1846 	if (vsi->type == I40E_VSI_MAIN ||
1847 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
1848 		num_tc_qps = vsi->num_queue_pairs;
1849 	else
1850 		num_tc_qps = vsi->alloc_queue_pairs;
1851 
1852 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1853 		/* Find numtc from enabled TC bitmap */
1854 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1855 			if (enabled_tc & BIT(i)) /* TC is enabled */
1856 				numtc++;
1857 		}
1858 		if (!numtc) {
1859 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1860 			numtc = 1;
1861 		}
1862 		num_tc_qps = num_tc_qps / numtc;
1863 		num_tc_qps = min_t(int, num_tc_qps,
1864 				   i40e_pf_get_max_q_per_tc(pf));
1865 	}
1866 
1867 	vsi->tc_config.numtc = numtc;
1868 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1869 
1870 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
1871 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1872 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
1873 
1874 	/* Setup queue offset/count for all TCs for given VSI */
1875 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1876 		/* See if the given TC is enabled for the given VSI */
1877 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1878 			/* TC is enabled */
1879 			int pow, num_qps;
1880 
1881 			switch (vsi->type) {
1882 			case I40E_VSI_MAIN:
1883 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
1884 				    I40E_FLAG_FD_ATR_ENABLED)) ||
1885 				    vsi->tc_config.enabled_tc != 1) {
1886 					qcount = min_t(int, pf->alloc_rss_size,
1887 						       num_tc_qps);
1888 					break;
1889 				}
1890 				fallthrough;
1891 			case I40E_VSI_FDIR:
1892 			case I40E_VSI_SRIOV:
1893 			case I40E_VSI_VMDQ2:
1894 			default:
1895 				qcount = num_tc_qps;
1896 				WARN_ON(i != 0);
1897 				break;
1898 			}
1899 			vsi->tc_config.tc_info[i].qoffset = offset;
1900 			vsi->tc_config.tc_info[i].qcount = qcount;
1901 
1902 			/* find the next higher power-of-2 of num queue pairs */
1903 			num_qps = qcount;
1904 			pow = 0;
1905 			while (num_qps && (BIT_ULL(pow) < qcount)) {
1906 				pow++;
1907 				num_qps >>= 1;
1908 			}
1909 
1910 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1911 			qmap =
1912 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1913 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1914 
1915 			offset += qcount;
1916 		} else {
1917 			/* TC is not enabled so set the offset to
1918 			 * default queue and allocate one queue
1919 			 * for the given TC.
1920 			 */
1921 			vsi->tc_config.tc_info[i].qoffset = 0;
1922 			vsi->tc_config.tc_info[i].qcount = 1;
1923 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1924 
1925 			qmap = 0;
1926 		}
1927 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1928 	}
1929 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
1930 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
1931 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
1932 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
1933 		vsi->num_queue_pairs = offset;
1934 
1935 	/* Scheduler section valid can only be set for ADD VSI */
1936 	if (is_add) {
1937 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1938 
1939 		ctxt->info.up_enable_bits = enabled_tc;
1940 	}
1941 	if (vsi->type == I40E_VSI_SRIOV) {
1942 		ctxt->info.mapping_flags |=
1943 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1944 		for (i = 0; i < vsi->num_queue_pairs; i++)
1945 			ctxt->info.queue_mapping[i] =
1946 					       cpu_to_le16(vsi->base_queue + i);
1947 	} else {
1948 		ctxt->info.mapping_flags |=
1949 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1950 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1951 	}
1952 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1953 }
1954 
1955 /**
1956  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1957  * @netdev: the netdevice
1958  * @addr: address to add
1959  *
1960  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1961  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1962  */
1963 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1964 {
1965 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1966 	struct i40e_vsi *vsi = np->vsi;
1967 
1968 	if (i40e_add_mac_filter(vsi, addr))
1969 		return 0;
1970 	else
1971 		return -ENOMEM;
1972 }
1973 
1974 /**
1975  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1976  * @netdev: the netdevice
1977  * @addr: address to add
1978  *
1979  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1980  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1981  */
1982 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1983 {
1984 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1985 	struct i40e_vsi *vsi = np->vsi;
1986 
1987 	/* Under some circumstances, we might receive a request to delete
1988 	 * our own device address from our uc list. Because we store the
1989 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
1990 	 * such requests and not delete our device address from this list.
1991 	 */
1992 	if (ether_addr_equal(addr, netdev->dev_addr))
1993 		return 0;
1994 
1995 	i40e_del_mac_filter(vsi, addr);
1996 
1997 	return 0;
1998 }
1999 
2000 /**
2001  * i40e_set_rx_mode - NDO callback to set the netdev filters
2002  * @netdev: network interface device structure
2003  **/
2004 static void i40e_set_rx_mode(struct net_device *netdev)
2005 {
2006 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2007 	struct i40e_vsi *vsi = np->vsi;
2008 
2009 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2010 
2011 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2012 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2013 
2014 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2015 
2016 	/* check for other flag changes */
2017 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2018 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2019 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2020 	}
2021 }
2022 
2023 /**
2024  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2025  * @vsi: Pointer to VSI struct
2026  * @from: Pointer to list which contains MAC filter entries - changes to
2027  *        those entries needs to be undone.
2028  *
2029  * MAC filter entries from this list were slated for deletion.
2030  **/
2031 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2032 					 struct hlist_head *from)
2033 {
2034 	struct i40e_mac_filter *f;
2035 	struct hlist_node *h;
2036 
2037 	hlist_for_each_entry_safe(f, h, from, hlist) {
2038 		u64 key = i40e_addr_to_hkey(f->macaddr);
2039 
2040 		/* Move the element back into MAC filter list*/
2041 		hlist_del(&f->hlist);
2042 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2043 	}
2044 }
2045 
2046 /**
2047  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2048  * @vsi: Pointer to vsi struct
2049  * @from: Pointer to list which contains MAC filter entries - changes to
2050  *        those entries needs to be undone.
2051  *
2052  * MAC filter entries from this list were slated for addition.
2053  **/
2054 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2055 					 struct hlist_head *from)
2056 {
2057 	struct i40e_new_mac_filter *new;
2058 	struct hlist_node *h;
2059 
2060 	hlist_for_each_entry_safe(new, h, from, hlist) {
2061 		/* We can simply free the wrapper structure */
2062 		hlist_del(&new->hlist);
2063 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2064 		kfree(new);
2065 	}
2066 }
2067 
2068 /**
2069  * i40e_next_filter - Get the next non-broadcast filter from a list
2070  * @next: pointer to filter in list
2071  *
2072  * Returns the next non-broadcast filter in the list. Required so that we
2073  * ignore broadcast filters within the list, since these are not handled via
2074  * the normal firmware update path.
2075  */
2076 static
2077 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2078 {
2079 	hlist_for_each_entry_continue(next, hlist) {
2080 		if (!is_broadcast_ether_addr(next->f->macaddr))
2081 			return next;
2082 	}
2083 
2084 	return NULL;
2085 }
2086 
2087 /**
2088  * i40e_update_filter_state - Update filter state based on return data
2089  * from firmware
2090  * @count: Number of filters added
2091  * @add_list: return data from fw
2092  * @add_head: pointer to first filter in current batch
2093  *
2094  * MAC filter entries from list were slated to be added to device. Returns
2095  * number of successful filters. Note that 0 does NOT mean success!
2096  **/
2097 static int
2098 i40e_update_filter_state(int count,
2099 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2100 			 struct i40e_new_mac_filter *add_head)
2101 {
2102 	int retval = 0;
2103 	int i;
2104 
2105 	for (i = 0; i < count; i++) {
2106 		/* Always check status of each filter. We don't need to check
2107 		 * the firmware return status because we pre-set the filter
2108 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2109 		 * request to the adminq. Thus, if it no longer matches then
2110 		 * we know the filter is active.
2111 		 */
2112 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2113 			add_head->state = I40E_FILTER_FAILED;
2114 		} else {
2115 			add_head->state = I40E_FILTER_ACTIVE;
2116 			retval++;
2117 		}
2118 
2119 		add_head = i40e_next_filter(add_head);
2120 		if (!add_head)
2121 			break;
2122 	}
2123 
2124 	return retval;
2125 }
2126 
2127 /**
2128  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2129  * @vsi: ptr to the VSI
2130  * @vsi_name: name to display in messages
2131  * @list: the list of filters to send to firmware
2132  * @num_del: the number of filters to delete
2133  * @retval: Set to -EIO on failure to delete
2134  *
2135  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2136  * *retval instead of a return value so that success does not force ret_val to
2137  * be set to 0. This ensures that a sequence of calls to this function
2138  * preserve the previous value of *retval on successful delete.
2139  */
2140 static
2141 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2142 			  struct i40e_aqc_remove_macvlan_element_data *list,
2143 			  int num_del, int *retval)
2144 {
2145 	struct i40e_hw *hw = &vsi->back->hw;
2146 	i40e_status aq_ret;
2147 	int aq_err;
2148 
2149 	aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
2150 	aq_err = hw->aq.asq_last_status;
2151 
2152 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2153 	if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
2154 		*retval = -EIO;
2155 		dev_info(&vsi->back->pdev->dev,
2156 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2157 			 vsi_name, i40e_stat_str(hw, aq_ret),
2158 			 i40e_aq_str(hw, aq_err));
2159 	}
2160 }
2161 
2162 /**
2163  * i40e_aqc_add_filters - Request firmware to add a set of filters
2164  * @vsi: ptr to the VSI
2165  * @vsi_name: name to display in messages
2166  * @list: the list of filters to send to firmware
2167  * @add_head: Position in the add hlist
2168  * @num_add: the number of filters to add
2169  *
2170  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2171  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2172  * space for more filters.
2173  */
2174 static
2175 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2176 			  struct i40e_aqc_add_macvlan_element_data *list,
2177 			  struct i40e_new_mac_filter *add_head,
2178 			  int num_add)
2179 {
2180 	struct i40e_hw *hw = &vsi->back->hw;
2181 	int aq_err, fcnt;
2182 
2183 	i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
2184 	aq_err = hw->aq.asq_last_status;
2185 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2186 
2187 	if (fcnt != num_add) {
2188 		if (vsi->type == I40E_VSI_MAIN) {
2189 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2190 			dev_warn(&vsi->back->pdev->dev,
2191 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2192 				 i40e_aq_str(hw, aq_err), vsi_name);
2193 		} else if (vsi->type == I40E_VSI_SRIOV ||
2194 			   vsi->type == I40E_VSI_VMDQ1 ||
2195 			   vsi->type == I40E_VSI_VMDQ2) {
2196 			dev_warn(&vsi->back->pdev->dev,
2197 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2198 				 i40e_aq_str(hw, aq_err), vsi_name, vsi_name);
2199 		} else {
2200 			dev_warn(&vsi->back->pdev->dev,
2201 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2202 				 i40e_aq_str(hw, aq_err), vsi_name, vsi->type);
2203 		}
2204 	}
2205 }
2206 
2207 /**
2208  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2209  * @vsi: pointer to the VSI
2210  * @vsi_name: the VSI name
2211  * @f: filter data
2212  *
2213  * This function sets or clears the promiscuous broadcast flags for VLAN
2214  * filters in order to properly receive broadcast frames. Assumes that only
2215  * broadcast filters are passed.
2216  *
2217  * Returns status indicating success or failure;
2218  **/
2219 static i40e_status
2220 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2221 			  struct i40e_mac_filter *f)
2222 {
2223 	bool enable = f->state == I40E_FILTER_NEW;
2224 	struct i40e_hw *hw = &vsi->back->hw;
2225 	i40e_status aq_ret;
2226 
2227 	if (f->vlan == I40E_VLAN_ANY) {
2228 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2229 						   vsi->seid,
2230 						   enable,
2231 						   NULL);
2232 	} else {
2233 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2234 							    vsi->seid,
2235 							    enable,
2236 							    f->vlan,
2237 							    NULL);
2238 	}
2239 
2240 	if (aq_ret) {
2241 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2242 		dev_warn(&vsi->back->pdev->dev,
2243 			 "Error %s, forcing overflow promiscuous on %s\n",
2244 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2245 			 vsi_name);
2246 	}
2247 
2248 	return aq_ret;
2249 }
2250 
2251 /**
2252  * i40e_set_promiscuous - set promiscuous mode
2253  * @pf: board private structure
2254  * @promisc: promisc on or off
2255  *
2256  * There are different ways of setting promiscuous mode on a PF depending on
2257  * what state/environment we're in.  This identifies and sets it appropriately.
2258  * Returns 0 on success.
2259  **/
2260 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2261 {
2262 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2263 	struct i40e_hw *hw = &pf->hw;
2264 	i40e_status aq_ret;
2265 
2266 	if (vsi->type == I40E_VSI_MAIN &&
2267 	    pf->lan_veb != I40E_NO_VEB &&
2268 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2269 		/* set defport ON for Main VSI instead of true promisc
2270 		 * this way we will get all unicast/multicast and VLAN
2271 		 * promisc behavior but will not get VF or VMDq traffic
2272 		 * replicated on the Main VSI.
2273 		 */
2274 		if (promisc)
2275 			aq_ret = i40e_aq_set_default_vsi(hw,
2276 							 vsi->seid,
2277 							 NULL);
2278 		else
2279 			aq_ret = i40e_aq_clear_default_vsi(hw,
2280 							   vsi->seid,
2281 							   NULL);
2282 		if (aq_ret) {
2283 			dev_info(&pf->pdev->dev,
2284 				 "Set default VSI failed, err %s, aq_err %s\n",
2285 				 i40e_stat_str(hw, aq_ret),
2286 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2287 		}
2288 	} else {
2289 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2290 						  hw,
2291 						  vsi->seid,
2292 						  promisc, NULL,
2293 						  true);
2294 		if (aq_ret) {
2295 			dev_info(&pf->pdev->dev,
2296 				 "set unicast promisc failed, err %s, aq_err %s\n",
2297 				 i40e_stat_str(hw, aq_ret),
2298 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2299 		}
2300 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2301 						  hw,
2302 						  vsi->seid,
2303 						  promisc, NULL);
2304 		if (aq_ret) {
2305 			dev_info(&pf->pdev->dev,
2306 				 "set multicast promisc failed, err %s, aq_err %s\n",
2307 				 i40e_stat_str(hw, aq_ret),
2308 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2309 		}
2310 	}
2311 
2312 	if (!aq_ret)
2313 		pf->cur_promisc = promisc;
2314 
2315 	return aq_ret;
2316 }
2317 
2318 /**
2319  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2320  * @vsi: ptr to the VSI
2321  *
2322  * Push any outstanding VSI filter changes through the AdminQ.
2323  *
2324  * Returns 0 or error value
2325  **/
2326 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2327 {
2328 	struct hlist_head tmp_add_list, tmp_del_list;
2329 	struct i40e_mac_filter *f;
2330 	struct i40e_new_mac_filter *new, *add_head = NULL;
2331 	struct i40e_hw *hw = &vsi->back->hw;
2332 	bool old_overflow, new_overflow;
2333 	unsigned int failed_filters = 0;
2334 	unsigned int vlan_filters = 0;
2335 	char vsi_name[16] = "PF";
2336 	int filter_list_len = 0;
2337 	i40e_status aq_ret = 0;
2338 	u32 changed_flags = 0;
2339 	struct hlist_node *h;
2340 	struct i40e_pf *pf;
2341 	int num_add = 0;
2342 	int num_del = 0;
2343 	int retval = 0;
2344 	u16 cmd_flags;
2345 	int list_size;
2346 	int bkt;
2347 
2348 	/* empty array typed pointers, kcalloc later */
2349 	struct i40e_aqc_add_macvlan_element_data *add_list;
2350 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2351 
2352 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2353 		usleep_range(1000, 2000);
2354 	pf = vsi->back;
2355 
2356 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2357 
2358 	if (vsi->netdev) {
2359 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2360 		vsi->current_netdev_flags = vsi->netdev->flags;
2361 	}
2362 
2363 	INIT_HLIST_HEAD(&tmp_add_list);
2364 	INIT_HLIST_HEAD(&tmp_del_list);
2365 
2366 	if (vsi->type == I40E_VSI_SRIOV)
2367 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2368 	else if (vsi->type != I40E_VSI_MAIN)
2369 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2370 
2371 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2372 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2373 
2374 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2375 		/* Create a list of filters to delete. */
2376 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2377 			if (f->state == I40E_FILTER_REMOVE) {
2378 				/* Move the element into temporary del_list */
2379 				hash_del(&f->hlist);
2380 				hlist_add_head(&f->hlist, &tmp_del_list);
2381 
2382 				/* Avoid counting removed filters */
2383 				continue;
2384 			}
2385 			if (f->state == I40E_FILTER_NEW) {
2386 				/* Create a temporary i40e_new_mac_filter */
2387 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2388 				if (!new)
2389 					goto err_no_memory_locked;
2390 
2391 				/* Store pointer to the real filter */
2392 				new->f = f;
2393 				new->state = f->state;
2394 
2395 				/* Add it to the hash list */
2396 				hlist_add_head(&new->hlist, &tmp_add_list);
2397 			}
2398 
2399 			/* Count the number of active (current and new) VLAN
2400 			 * filters we have now. Does not count filters which
2401 			 * are marked for deletion.
2402 			 */
2403 			if (f->vlan > 0)
2404 				vlan_filters++;
2405 		}
2406 
2407 		retval = i40e_correct_mac_vlan_filters(vsi,
2408 						       &tmp_add_list,
2409 						       &tmp_del_list,
2410 						       vlan_filters);
2411 
2412 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2413 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2414 
2415 		if (retval)
2416 			goto err_no_memory_locked;
2417 
2418 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2419 	}
2420 
2421 	/* Now process 'del_list' outside the lock */
2422 	if (!hlist_empty(&tmp_del_list)) {
2423 		filter_list_len = hw->aq.asq_buf_size /
2424 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2425 		list_size = filter_list_len *
2426 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2427 		del_list = kzalloc(list_size, GFP_ATOMIC);
2428 		if (!del_list)
2429 			goto err_no_memory;
2430 
2431 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2432 			cmd_flags = 0;
2433 
2434 			/* handle broadcast filters by updating the broadcast
2435 			 * promiscuous flag and release filter list.
2436 			 */
2437 			if (is_broadcast_ether_addr(f->macaddr)) {
2438 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2439 
2440 				hlist_del(&f->hlist);
2441 				kfree(f);
2442 				continue;
2443 			}
2444 
2445 			/* add to delete list */
2446 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2447 			if (f->vlan == I40E_VLAN_ANY) {
2448 				del_list[num_del].vlan_tag = 0;
2449 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2450 			} else {
2451 				del_list[num_del].vlan_tag =
2452 					cpu_to_le16((u16)(f->vlan));
2453 			}
2454 
2455 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2456 			del_list[num_del].flags = cmd_flags;
2457 			num_del++;
2458 
2459 			/* flush a full buffer */
2460 			if (num_del == filter_list_len) {
2461 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2462 						     num_del, &retval);
2463 				memset(del_list, 0, list_size);
2464 				num_del = 0;
2465 			}
2466 			/* Release memory for MAC filter entries which were
2467 			 * synced up with HW.
2468 			 */
2469 			hlist_del(&f->hlist);
2470 			kfree(f);
2471 		}
2472 
2473 		if (num_del) {
2474 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2475 					     num_del, &retval);
2476 		}
2477 
2478 		kfree(del_list);
2479 		del_list = NULL;
2480 	}
2481 
2482 	if (!hlist_empty(&tmp_add_list)) {
2483 		/* Do all the adds now. */
2484 		filter_list_len = hw->aq.asq_buf_size /
2485 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2486 		list_size = filter_list_len *
2487 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2488 		add_list = kzalloc(list_size, GFP_ATOMIC);
2489 		if (!add_list)
2490 			goto err_no_memory;
2491 
2492 		num_add = 0;
2493 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2494 			/* handle broadcast filters by updating the broadcast
2495 			 * promiscuous flag instead of adding a MAC filter.
2496 			 */
2497 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2498 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2499 							      new->f))
2500 					new->state = I40E_FILTER_FAILED;
2501 				else
2502 					new->state = I40E_FILTER_ACTIVE;
2503 				continue;
2504 			}
2505 
2506 			/* add to add array */
2507 			if (num_add == 0)
2508 				add_head = new;
2509 			cmd_flags = 0;
2510 			ether_addr_copy(add_list[num_add].mac_addr,
2511 					new->f->macaddr);
2512 			if (new->f->vlan == I40E_VLAN_ANY) {
2513 				add_list[num_add].vlan_tag = 0;
2514 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2515 			} else {
2516 				add_list[num_add].vlan_tag =
2517 					cpu_to_le16((u16)(new->f->vlan));
2518 			}
2519 			add_list[num_add].queue_number = 0;
2520 			/* set invalid match method for later detection */
2521 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2522 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2523 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2524 			num_add++;
2525 
2526 			/* flush a full buffer */
2527 			if (num_add == filter_list_len) {
2528 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2529 						     add_head, num_add);
2530 				memset(add_list, 0, list_size);
2531 				num_add = 0;
2532 			}
2533 		}
2534 		if (num_add) {
2535 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2536 					     num_add);
2537 		}
2538 		/* Now move all of the filters from the temp add list back to
2539 		 * the VSI's list.
2540 		 */
2541 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2542 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2543 			/* Only update the state if we're still NEW */
2544 			if (new->f->state == I40E_FILTER_NEW)
2545 				new->f->state = new->state;
2546 			hlist_del(&new->hlist);
2547 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2548 			kfree(new);
2549 		}
2550 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2551 		kfree(add_list);
2552 		add_list = NULL;
2553 	}
2554 
2555 	/* Determine the number of active and failed filters. */
2556 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2557 	vsi->active_filters = 0;
2558 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2559 		if (f->state == I40E_FILTER_ACTIVE)
2560 			vsi->active_filters++;
2561 		else if (f->state == I40E_FILTER_FAILED)
2562 			failed_filters++;
2563 	}
2564 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2565 
2566 	/* Check if we are able to exit overflow promiscuous mode. We can
2567 	 * safely exit if we didn't just enter, we no longer have any failed
2568 	 * filters, and we have reduced filters below the threshold value.
2569 	 */
2570 	if (old_overflow && !failed_filters &&
2571 	    vsi->active_filters < vsi->promisc_threshold) {
2572 		dev_info(&pf->pdev->dev,
2573 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2574 			 vsi_name);
2575 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2576 		vsi->promisc_threshold = 0;
2577 	}
2578 
2579 	/* if the VF is not trusted do not do promisc */
2580 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2581 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2582 		goto out;
2583 	}
2584 
2585 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2586 
2587 	/* If we are entering overflow promiscuous, we need to calculate a new
2588 	 * threshold for when we are safe to exit
2589 	 */
2590 	if (!old_overflow && new_overflow)
2591 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2592 
2593 	/* check for changes in promiscuous modes */
2594 	if (changed_flags & IFF_ALLMULTI) {
2595 		bool cur_multipromisc;
2596 
2597 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2598 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2599 							       vsi->seid,
2600 							       cur_multipromisc,
2601 							       NULL);
2602 		if (aq_ret) {
2603 			retval = i40e_aq_rc_to_posix(aq_ret,
2604 						     hw->aq.asq_last_status);
2605 			dev_info(&pf->pdev->dev,
2606 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2607 				 vsi_name,
2608 				 i40e_stat_str(hw, aq_ret),
2609 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2610 		} else {
2611 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2612 				 cur_multipromisc ? "entering" : "leaving");
2613 		}
2614 	}
2615 
2616 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2617 		bool cur_promisc;
2618 
2619 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2620 			       new_overflow);
2621 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2622 		if (aq_ret) {
2623 			retval = i40e_aq_rc_to_posix(aq_ret,
2624 						     hw->aq.asq_last_status);
2625 			dev_info(&pf->pdev->dev,
2626 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2627 				 cur_promisc ? "on" : "off",
2628 				 vsi_name,
2629 				 i40e_stat_str(hw, aq_ret),
2630 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2631 		}
2632 	}
2633 out:
2634 	/* if something went wrong then set the changed flag so we try again */
2635 	if (retval)
2636 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2637 
2638 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2639 	return retval;
2640 
2641 err_no_memory:
2642 	/* Restore elements on the temporary add and delete lists */
2643 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2644 err_no_memory_locked:
2645 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2646 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2647 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2648 
2649 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2650 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2651 	return -ENOMEM;
2652 }
2653 
2654 /**
2655  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2656  * @pf: board private structure
2657  **/
2658 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2659 {
2660 	int v;
2661 
2662 	if (!pf)
2663 		return;
2664 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2665 		return;
2666 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2667 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2668 		return;
2669 	}
2670 
2671 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2672 		if (pf->vsi[v] &&
2673 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2674 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2675 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2676 
2677 			if (ret) {
2678 				/* come back and try again later */
2679 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2680 					pf->state);
2681 				break;
2682 			}
2683 		}
2684 	}
2685 }
2686 
2687 /**
2688  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2689  * @vsi: the vsi
2690  **/
2691 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2692 {
2693 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2694 		return I40E_RXBUFFER_2048;
2695 	else
2696 		return I40E_RXBUFFER_3072;
2697 }
2698 
2699 /**
2700  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2701  * @netdev: network interface device structure
2702  * @new_mtu: new value for maximum frame size
2703  *
2704  * Returns 0 on success, negative on failure
2705  **/
2706 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2707 {
2708 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2709 	struct i40e_vsi *vsi = np->vsi;
2710 	struct i40e_pf *pf = vsi->back;
2711 
2712 	if (i40e_enabled_xdp_vsi(vsi)) {
2713 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2714 
2715 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2716 			return -EINVAL;
2717 	}
2718 
2719 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2720 		   netdev->mtu, new_mtu);
2721 	netdev->mtu = new_mtu;
2722 	if (netif_running(netdev))
2723 		i40e_vsi_reinit_locked(vsi);
2724 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2725 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2726 	return 0;
2727 }
2728 
2729 /**
2730  * i40e_ioctl - Access the hwtstamp interface
2731  * @netdev: network interface device structure
2732  * @ifr: interface request data
2733  * @cmd: ioctl command
2734  **/
2735 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2736 {
2737 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2738 	struct i40e_pf *pf = np->vsi->back;
2739 
2740 	switch (cmd) {
2741 	case SIOCGHWTSTAMP:
2742 		return i40e_ptp_get_ts_config(pf, ifr);
2743 	case SIOCSHWTSTAMP:
2744 		return i40e_ptp_set_ts_config(pf, ifr);
2745 	default:
2746 		return -EOPNOTSUPP;
2747 	}
2748 }
2749 
2750 /**
2751  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2752  * @vsi: the vsi being adjusted
2753  **/
2754 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2755 {
2756 	struct i40e_vsi_context ctxt;
2757 	i40e_status ret;
2758 
2759 	/* Don't modify stripping options if a port VLAN is active */
2760 	if (vsi->info.pvid)
2761 		return;
2762 
2763 	if ((vsi->info.valid_sections &
2764 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2765 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2766 		return;  /* already enabled */
2767 
2768 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2769 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2770 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2771 
2772 	ctxt.seid = vsi->seid;
2773 	ctxt.info = vsi->info;
2774 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2775 	if (ret) {
2776 		dev_info(&vsi->back->pdev->dev,
2777 			 "update vlan stripping failed, err %s aq_err %s\n",
2778 			 i40e_stat_str(&vsi->back->hw, ret),
2779 			 i40e_aq_str(&vsi->back->hw,
2780 				     vsi->back->hw.aq.asq_last_status));
2781 	}
2782 }
2783 
2784 /**
2785  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2786  * @vsi: the vsi being adjusted
2787  **/
2788 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2789 {
2790 	struct i40e_vsi_context ctxt;
2791 	i40e_status ret;
2792 
2793 	/* Don't modify stripping options if a port VLAN is active */
2794 	if (vsi->info.pvid)
2795 		return;
2796 
2797 	if ((vsi->info.valid_sections &
2798 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2799 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2800 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
2801 		return;  /* already disabled */
2802 
2803 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2804 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2805 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2806 
2807 	ctxt.seid = vsi->seid;
2808 	ctxt.info = vsi->info;
2809 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2810 	if (ret) {
2811 		dev_info(&vsi->back->pdev->dev,
2812 			 "update vlan stripping failed, err %s aq_err %s\n",
2813 			 i40e_stat_str(&vsi->back->hw, ret),
2814 			 i40e_aq_str(&vsi->back->hw,
2815 				     vsi->back->hw.aq.asq_last_status));
2816 	}
2817 }
2818 
2819 /**
2820  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2821  * @vsi: the vsi being configured
2822  * @vid: vlan id to be added (0 = untagged only , -1 = any)
2823  *
2824  * This is a helper function for adding a new MAC/VLAN filter with the
2825  * specified VLAN for each existing MAC address already in the hash table.
2826  * This function does *not* perform any accounting to update filters based on
2827  * VLAN mode.
2828  *
2829  * NOTE: this function expects to be called while under the
2830  * mac_filter_hash_lock
2831  **/
2832 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2833 {
2834 	struct i40e_mac_filter *f, *add_f;
2835 	struct hlist_node *h;
2836 	int bkt;
2837 
2838 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2839 		if (f->state == I40E_FILTER_REMOVE)
2840 			continue;
2841 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
2842 		if (!add_f) {
2843 			dev_info(&vsi->back->pdev->dev,
2844 				 "Could not add vlan filter %d for %pM\n",
2845 				 vid, f->macaddr);
2846 			return -ENOMEM;
2847 		}
2848 	}
2849 
2850 	return 0;
2851 }
2852 
2853 /**
2854  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2855  * @vsi: the VSI being configured
2856  * @vid: VLAN id to be added
2857  **/
2858 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
2859 {
2860 	int err;
2861 
2862 	if (vsi->info.pvid)
2863 		return -EINVAL;
2864 
2865 	/* The network stack will attempt to add VID=0, with the intention to
2866 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
2867 	 * these packets by default when configured to receive untagged
2868 	 * packets, so we don't need to add a filter for this case.
2869 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
2870 	 * receive *only* tagged traffic and stops receiving untagged traffic.
2871 	 * Thus, we do not want to actually add a filter for VID=0
2872 	 */
2873 	if (!vid)
2874 		return 0;
2875 
2876 	/* Locked once because all functions invoked below iterates list*/
2877 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2878 	err = i40e_add_vlan_all_mac(vsi, vid);
2879 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2880 	if (err)
2881 		return err;
2882 
2883 	/* schedule our worker thread which will take care of
2884 	 * applying the new filter changes
2885 	 */
2886 	i40e_service_event_schedule(vsi->back);
2887 	return 0;
2888 }
2889 
2890 /**
2891  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
2892  * @vsi: the vsi being configured
2893  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2894  *
2895  * This function should be used to remove all VLAN filters which match the
2896  * given VID. It does not schedule the service event and does not take the
2897  * mac_filter_hash_lock so it may be combined with other operations under
2898  * a single invocation of the mac_filter_hash_lock.
2899  *
2900  * NOTE: this function expects to be called while under the
2901  * mac_filter_hash_lock
2902  */
2903 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2904 {
2905 	struct i40e_mac_filter *f;
2906 	struct hlist_node *h;
2907 	int bkt;
2908 
2909 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2910 		if (f->vlan == vid)
2911 			__i40e_del_filter(vsi, f);
2912 	}
2913 }
2914 
2915 /**
2916  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2917  * @vsi: the VSI being configured
2918  * @vid: VLAN id to be removed
2919  **/
2920 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
2921 {
2922 	if (!vid || vsi->info.pvid)
2923 		return;
2924 
2925 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2926 	i40e_rm_vlan_all_mac(vsi, vid);
2927 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2928 
2929 	/* schedule our worker thread which will take care of
2930 	 * applying the new filter changes
2931 	 */
2932 	i40e_service_event_schedule(vsi->back);
2933 }
2934 
2935 /**
2936  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2937  * @netdev: network interface to be adjusted
2938  * @proto: unused protocol value
2939  * @vid: vlan id to be added
2940  *
2941  * net_device_ops implementation for adding vlan ids
2942  **/
2943 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2944 				__always_unused __be16 proto, u16 vid)
2945 {
2946 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2947 	struct i40e_vsi *vsi = np->vsi;
2948 	int ret = 0;
2949 
2950 	if (vid >= VLAN_N_VID)
2951 		return -EINVAL;
2952 
2953 	ret = i40e_vsi_add_vlan(vsi, vid);
2954 	if (!ret)
2955 		set_bit(vid, vsi->active_vlans);
2956 
2957 	return ret;
2958 }
2959 
2960 /**
2961  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
2962  * @netdev: network interface to be adjusted
2963  * @proto: unused protocol value
2964  * @vid: vlan id to be added
2965  **/
2966 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
2967 				    __always_unused __be16 proto, u16 vid)
2968 {
2969 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2970 	struct i40e_vsi *vsi = np->vsi;
2971 
2972 	if (vid >= VLAN_N_VID)
2973 		return;
2974 	set_bit(vid, vsi->active_vlans);
2975 }
2976 
2977 /**
2978  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2979  * @netdev: network interface to be adjusted
2980  * @proto: unused protocol value
2981  * @vid: vlan id to be removed
2982  *
2983  * net_device_ops implementation for removing vlan ids
2984  **/
2985 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2986 				 __always_unused __be16 proto, u16 vid)
2987 {
2988 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2989 	struct i40e_vsi *vsi = np->vsi;
2990 
2991 	/* return code is ignored as there is nothing a user
2992 	 * can do about failure to remove and a log message was
2993 	 * already printed from the other function
2994 	 */
2995 	i40e_vsi_kill_vlan(vsi, vid);
2996 
2997 	clear_bit(vid, vsi->active_vlans);
2998 
2999 	return 0;
3000 }
3001 
3002 /**
3003  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3004  * @vsi: the vsi being brought back up
3005  **/
3006 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3007 {
3008 	u16 vid;
3009 
3010 	if (!vsi->netdev)
3011 		return;
3012 
3013 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3014 		i40e_vlan_stripping_enable(vsi);
3015 	else
3016 		i40e_vlan_stripping_disable(vsi);
3017 
3018 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3019 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3020 					vid);
3021 }
3022 
3023 /**
3024  * i40e_vsi_add_pvid - Add pvid for the VSI
3025  * @vsi: the vsi being adjusted
3026  * @vid: the vlan id to set as a PVID
3027  **/
3028 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3029 {
3030 	struct i40e_vsi_context ctxt;
3031 	i40e_status ret;
3032 
3033 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3034 	vsi->info.pvid = cpu_to_le16(vid);
3035 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3036 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3037 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3038 
3039 	ctxt.seid = vsi->seid;
3040 	ctxt.info = vsi->info;
3041 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3042 	if (ret) {
3043 		dev_info(&vsi->back->pdev->dev,
3044 			 "add pvid failed, err %s aq_err %s\n",
3045 			 i40e_stat_str(&vsi->back->hw, ret),
3046 			 i40e_aq_str(&vsi->back->hw,
3047 				     vsi->back->hw.aq.asq_last_status));
3048 		return -ENOENT;
3049 	}
3050 
3051 	return 0;
3052 }
3053 
3054 /**
3055  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3056  * @vsi: the vsi being adjusted
3057  *
3058  * Just use the vlan_rx_register() service to put it back to normal
3059  **/
3060 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3061 {
3062 	vsi->info.pvid = 0;
3063 
3064 	i40e_vlan_stripping_disable(vsi);
3065 }
3066 
3067 /**
3068  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3069  * @vsi: ptr to the VSI
3070  *
3071  * If this function returns with an error, then it's possible one or
3072  * more of the rings is populated (while the rest are not).  It is the
3073  * callers duty to clean those orphaned rings.
3074  *
3075  * Return 0 on success, negative on failure
3076  **/
3077 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3078 {
3079 	int i, err = 0;
3080 
3081 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3082 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3083 
3084 	if (!i40e_enabled_xdp_vsi(vsi))
3085 		return err;
3086 
3087 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3088 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3089 
3090 	return err;
3091 }
3092 
3093 /**
3094  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3095  * @vsi: ptr to the VSI
3096  *
3097  * Free VSI's transmit software resources
3098  **/
3099 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3100 {
3101 	int i;
3102 
3103 	if (vsi->tx_rings) {
3104 		for (i = 0; i < vsi->num_queue_pairs; i++)
3105 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3106 				i40e_free_tx_resources(vsi->tx_rings[i]);
3107 	}
3108 
3109 	if (vsi->xdp_rings) {
3110 		for (i = 0; i < vsi->num_queue_pairs; i++)
3111 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3112 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3113 	}
3114 }
3115 
3116 /**
3117  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3118  * @vsi: ptr to the VSI
3119  *
3120  * If this function returns with an error, then it's possible one or
3121  * more of the rings is populated (while the rest are not).  It is the
3122  * callers duty to clean those orphaned rings.
3123  *
3124  * Return 0 on success, negative on failure
3125  **/
3126 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3127 {
3128 	int i, err = 0;
3129 
3130 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3131 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3132 	return err;
3133 }
3134 
3135 /**
3136  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3137  * @vsi: ptr to the VSI
3138  *
3139  * Free all receive software resources
3140  **/
3141 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3142 {
3143 	int i;
3144 
3145 	if (!vsi->rx_rings)
3146 		return;
3147 
3148 	for (i = 0; i < vsi->num_queue_pairs; i++)
3149 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3150 			i40e_free_rx_resources(vsi->rx_rings[i]);
3151 }
3152 
3153 /**
3154  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3155  * @ring: The Tx ring to configure
3156  *
3157  * This enables/disables XPS for a given Tx descriptor ring
3158  * based on the TCs enabled for the VSI that ring belongs to.
3159  **/
3160 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3161 {
3162 	int cpu;
3163 
3164 	if (!ring->q_vector || !ring->netdev || ring->ch)
3165 		return;
3166 
3167 	/* We only initialize XPS once, so as not to overwrite user settings */
3168 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3169 		return;
3170 
3171 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3172 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3173 			    ring->queue_index);
3174 }
3175 
3176 /**
3177  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3178  * @ring: The Tx or Rx ring
3179  *
3180  * Returns the AF_XDP buffer pool or NULL.
3181  **/
3182 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3183 {
3184 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3185 	int qid = ring->queue_index;
3186 
3187 	if (ring_is_xdp(ring))
3188 		qid -= ring->vsi->alloc_queue_pairs;
3189 
3190 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3191 		return NULL;
3192 
3193 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3194 }
3195 
3196 /**
3197  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3198  * @ring: The Tx ring to configure
3199  *
3200  * Configure the Tx descriptor ring in the HMC context.
3201  **/
3202 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3203 {
3204 	struct i40e_vsi *vsi = ring->vsi;
3205 	u16 pf_q = vsi->base_queue + ring->queue_index;
3206 	struct i40e_hw *hw = &vsi->back->hw;
3207 	struct i40e_hmc_obj_txq tx_ctx;
3208 	i40e_status err = 0;
3209 	u32 qtx_ctl = 0;
3210 
3211 	if (ring_is_xdp(ring))
3212 		ring->xsk_pool = i40e_xsk_pool(ring);
3213 
3214 	/* some ATR related tx ring init */
3215 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3216 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3217 		ring->atr_count = 0;
3218 	} else {
3219 		ring->atr_sample_rate = 0;
3220 	}
3221 
3222 	/* configure XPS */
3223 	i40e_config_xps_tx_ring(ring);
3224 
3225 	/* clear the context structure first */
3226 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3227 
3228 	tx_ctx.new_context = 1;
3229 	tx_ctx.base = (ring->dma / 128);
3230 	tx_ctx.qlen = ring->count;
3231 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3232 					       I40E_FLAG_FD_ATR_ENABLED));
3233 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3234 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3235 	if (vsi->type != I40E_VSI_FDIR)
3236 		tx_ctx.head_wb_ena = 1;
3237 	tx_ctx.head_wb_addr = ring->dma +
3238 			      (ring->count * sizeof(struct i40e_tx_desc));
3239 
3240 	/* As part of VSI creation/update, FW allocates certain
3241 	 * Tx arbitration queue sets for each TC enabled for
3242 	 * the VSI. The FW returns the handles to these queue
3243 	 * sets as part of the response buffer to Add VSI,
3244 	 * Update VSI, etc. AQ commands. It is expected that
3245 	 * these queue set handles be associated with the Tx
3246 	 * queues by the driver as part of the TX queue context
3247 	 * initialization. This has to be done regardless of
3248 	 * DCB as by default everything is mapped to TC0.
3249 	 */
3250 
3251 	if (ring->ch)
3252 		tx_ctx.rdylist =
3253 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3254 
3255 	else
3256 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3257 
3258 	tx_ctx.rdylist_act = 0;
3259 
3260 	/* clear the context in the HMC */
3261 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3262 	if (err) {
3263 		dev_info(&vsi->back->pdev->dev,
3264 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3265 			 ring->queue_index, pf_q, err);
3266 		return -ENOMEM;
3267 	}
3268 
3269 	/* set the context in the HMC */
3270 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3271 	if (err) {
3272 		dev_info(&vsi->back->pdev->dev,
3273 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3274 			 ring->queue_index, pf_q, err);
3275 		return -ENOMEM;
3276 	}
3277 
3278 	/* Now associate this queue with this PCI function */
3279 	if (ring->ch) {
3280 		if (ring->ch->type == I40E_VSI_VMDQ2)
3281 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3282 		else
3283 			return -EINVAL;
3284 
3285 		qtx_ctl |= (ring->ch->vsi_number <<
3286 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3287 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3288 	} else {
3289 		if (vsi->type == I40E_VSI_VMDQ2) {
3290 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3291 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3292 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3293 		} else {
3294 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3295 		}
3296 	}
3297 
3298 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3299 		    I40E_QTX_CTL_PF_INDX_MASK);
3300 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3301 	i40e_flush(hw);
3302 
3303 	/* cache tail off for easier writes later */
3304 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3305 
3306 	return 0;
3307 }
3308 
3309 /**
3310  * i40e_rx_offset - Return expected offset into page to access data
3311  * @rx_ring: Ring we are requesting offset of
3312  *
3313  * Returns the offset value for ring into the data buffer.
3314  */
3315 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3316 {
3317 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3318 }
3319 
3320 /**
3321  * i40e_configure_rx_ring - Configure a receive ring context
3322  * @ring: The Rx ring to configure
3323  *
3324  * Configure the Rx descriptor ring in the HMC context.
3325  **/
3326 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3327 {
3328 	struct i40e_vsi *vsi = ring->vsi;
3329 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3330 	u16 pf_q = vsi->base_queue + ring->queue_index;
3331 	struct i40e_hw *hw = &vsi->back->hw;
3332 	struct i40e_hmc_obj_rxq rx_ctx;
3333 	i40e_status err = 0;
3334 	bool ok;
3335 	int ret;
3336 
3337 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3338 
3339 	/* clear the context structure first */
3340 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3341 
3342 	if (ring->vsi->type == I40E_VSI_MAIN)
3343 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3344 
3345 	kfree(ring->rx_bi);
3346 	ring->xsk_pool = i40e_xsk_pool(ring);
3347 	if (ring->xsk_pool) {
3348 		ret = i40e_alloc_rx_bi_zc(ring);
3349 		if (ret)
3350 			return ret;
3351 		ring->rx_buf_len =
3352 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3353 		/* For AF_XDP ZC, we disallow packets to span on
3354 		 * multiple buffers, thus letting us skip that
3355 		 * handling in the fast-path.
3356 		 */
3357 		chain_len = 1;
3358 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3359 						 MEM_TYPE_XSK_BUFF_POOL,
3360 						 NULL);
3361 		if (ret)
3362 			return ret;
3363 		dev_info(&vsi->back->pdev->dev,
3364 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3365 			 ring->queue_index);
3366 
3367 	} else {
3368 		ret = i40e_alloc_rx_bi(ring);
3369 		if (ret)
3370 			return ret;
3371 		ring->rx_buf_len = vsi->rx_buf_len;
3372 		if (ring->vsi->type == I40E_VSI_MAIN) {
3373 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3374 							 MEM_TYPE_PAGE_SHARED,
3375 							 NULL);
3376 			if (ret)
3377 				return ret;
3378 		}
3379 	}
3380 
3381 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3382 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3383 
3384 	rx_ctx.base = (ring->dma / 128);
3385 	rx_ctx.qlen = ring->count;
3386 
3387 	/* use 16 byte descriptors */
3388 	rx_ctx.dsize = 0;
3389 
3390 	/* descriptor type is always zero
3391 	 * rx_ctx.dtype = 0;
3392 	 */
3393 	rx_ctx.hsplit_0 = 0;
3394 
3395 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3396 	if (hw->revision_id == 0)
3397 		rx_ctx.lrxqthresh = 0;
3398 	else
3399 		rx_ctx.lrxqthresh = 1;
3400 	rx_ctx.crcstrip = 1;
3401 	rx_ctx.l2tsel = 1;
3402 	/* this controls whether VLAN is stripped from inner headers */
3403 	rx_ctx.showiv = 0;
3404 	/* set the prefena field to 1 because the manual says to */
3405 	rx_ctx.prefena = 1;
3406 
3407 	/* clear the context in the HMC */
3408 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3409 	if (err) {
3410 		dev_info(&vsi->back->pdev->dev,
3411 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3412 			 ring->queue_index, pf_q, err);
3413 		return -ENOMEM;
3414 	}
3415 
3416 	/* set the context in the HMC */
3417 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3418 	if (err) {
3419 		dev_info(&vsi->back->pdev->dev,
3420 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3421 			 ring->queue_index, pf_q, err);
3422 		return -ENOMEM;
3423 	}
3424 
3425 	/* configure Rx buffer alignment */
3426 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3427 		clear_ring_build_skb_enabled(ring);
3428 	else
3429 		set_ring_build_skb_enabled(ring);
3430 
3431 	ring->rx_offset = i40e_rx_offset(ring);
3432 
3433 	/* cache tail for quicker writes, and clear the reg before use */
3434 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3435 	writel(0, ring->tail);
3436 
3437 	if (ring->xsk_pool) {
3438 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3439 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3440 	} else {
3441 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3442 	}
3443 	if (!ok) {
3444 		/* Log this in case the user has forgotten to give the kernel
3445 		 * any buffers, even later in the application.
3446 		 */
3447 		dev_info(&vsi->back->pdev->dev,
3448 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3449 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3450 			 ring->queue_index, pf_q);
3451 	}
3452 
3453 	return 0;
3454 }
3455 
3456 /**
3457  * i40e_vsi_configure_tx - Configure the VSI for Tx
3458  * @vsi: VSI structure describing this set of rings and resources
3459  *
3460  * Configure the Tx VSI for operation.
3461  **/
3462 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3463 {
3464 	int err = 0;
3465 	u16 i;
3466 
3467 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3468 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3469 
3470 	if (err || !i40e_enabled_xdp_vsi(vsi))
3471 		return err;
3472 
3473 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3474 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3475 
3476 	return err;
3477 }
3478 
3479 /**
3480  * i40e_vsi_configure_rx - Configure the VSI for Rx
3481  * @vsi: the VSI being configured
3482  *
3483  * Configure the Rx VSI for operation.
3484  **/
3485 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3486 {
3487 	int err = 0;
3488 	u16 i;
3489 
3490 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3491 		vsi->max_frame = I40E_MAX_RXBUFFER;
3492 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3493 #if (PAGE_SIZE < 8192)
3494 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3495 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3496 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3497 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3498 #endif
3499 	} else {
3500 		vsi->max_frame = I40E_MAX_RXBUFFER;
3501 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3502 						       I40E_RXBUFFER_2048;
3503 	}
3504 
3505 	/* set up individual rings */
3506 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3507 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3508 
3509 	return err;
3510 }
3511 
3512 /**
3513  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3514  * @vsi: ptr to the VSI
3515  **/
3516 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3517 {
3518 	struct i40e_ring *tx_ring, *rx_ring;
3519 	u16 qoffset, qcount;
3520 	int i, n;
3521 
3522 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3523 		/* Reset the TC information */
3524 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3525 			rx_ring = vsi->rx_rings[i];
3526 			tx_ring = vsi->tx_rings[i];
3527 			rx_ring->dcb_tc = 0;
3528 			tx_ring->dcb_tc = 0;
3529 		}
3530 		return;
3531 	}
3532 
3533 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3534 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3535 			continue;
3536 
3537 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3538 		qcount = vsi->tc_config.tc_info[n].qcount;
3539 		for (i = qoffset; i < (qoffset + qcount); i++) {
3540 			rx_ring = vsi->rx_rings[i];
3541 			tx_ring = vsi->tx_rings[i];
3542 			rx_ring->dcb_tc = n;
3543 			tx_ring->dcb_tc = n;
3544 		}
3545 	}
3546 }
3547 
3548 /**
3549  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3550  * @vsi: ptr to the VSI
3551  **/
3552 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3553 {
3554 	if (vsi->netdev)
3555 		i40e_set_rx_mode(vsi->netdev);
3556 }
3557 
3558 /**
3559  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3560  * @pf: Pointer to the targeted PF
3561  *
3562  * Set all flow director counters to 0.
3563  */
3564 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3565 {
3566 	pf->fd_tcp4_filter_cnt = 0;
3567 	pf->fd_udp4_filter_cnt = 0;
3568 	pf->fd_sctp4_filter_cnt = 0;
3569 	pf->fd_ip4_filter_cnt = 0;
3570 	pf->fd_tcp6_filter_cnt = 0;
3571 	pf->fd_udp6_filter_cnt = 0;
3572 	pf->fd_sctp6_filter_cnt = 0;
3573 	pf->fd_ip6_filter_cnt = 0;
3574 }
3575 
3576 /**
3577  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3578  * @vsi: Pointer to the targeted VSI
3579  *
3580  * This function replays the hlist on the hw where all the SB Flow Director
3581  * filters were saved.
3582  **/
3583 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3584 {
3585 	struct i40e_fdir_filter *filter;
3586 	struct i40e_pf *pf = vsi->back;
3587 	struct hlist_node *node;
3588 
3589 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3590 		return;
3591 
3592 	/* Reset FDir counters as we're replaying all existing filters */
3593 	i40e_reset_fdir_filter_cnt(pf);
3594 
3595 	hlist_for_each_entry_safe(filter, node,
3596 				  &pf->fdir_filter_list, fdir_node) {
3597 		i40e_add_del_fdir(vsi, filter, true);
3598 	}
3599 }
3600 
3601 /**
3602  * i40e_vsi_configure - Set up the VSI for action
3603  * @vsi: the VSI being configured
3604  **/
3605 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3606 {
3607 	int err;
3608 
3609 	i40e_set_vsi_rx_mode(vsi);
3610 	i40e_restore_vlan(vsi);
3611 	i40e_vsi_config_dcb_rings(vsi);
3612 	err = i40e_vsi_configure_tx(vsi);
3613 	if (!err)
3614 		err = i40e_vsi_configure_rx(vsi);
3615 
3616 	return err;
3617 }
3618 
3619 /**
3620  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3621  * @vsi: the VSI being configured
3622  **/
3623 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3624 {
3625 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3626 	struct i40e_pf *pf = vsi->back;
3627 	struct i40e_hw *hw = &pf->hw;
3628 	u16 vector;
3629 	int i, q;
3630 	u32 qp;
3631 
3632 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3633 	 * and PFINT_LNKLSTn registers, e.g.:
3634 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3635 	 */
3636 	qp = vsi->base_queue;
3637 	vector = vsi->base_vector;
3638 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3639 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3640 
3641 		q_vector->rx.next_update = jiffies + 1;
3642 		q_vector->rx.target_itr =
3643 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3644 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3645 		     q_vector->rx.target_itr >> 1);
3646 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3647 
3648 		q_vector->tx.next_update = jiffies + 1;
3649 		q_vector->tx.target_itr =
3650 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3651 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3652 		     q_vector->tx.target_itr >> 1);
3653 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3654 
3655 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3656 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3657 
3658 		/* Linked list for the queuepairs assigned to this vector */
3659 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3660 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3661 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3662 			u32 val;
3663 
3664 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3665 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3666 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3667 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3668 			      (I40E_QUEUE_TYPE_TX <<
3669 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3670 
3671 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3672 
3673 			if (has_xdp) {
3674 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3675 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3676 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3677 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3678 				      (I40E_QUEUE_TYPE_TX <<
3679 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3680 
3681 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3682 			}
3683 
3684 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3685 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3686 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3687 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3688 			      (I40E_QUEUE_TYPE_RX <<
3689 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3690 
3691 			/* Terminate the linked list */
3692 			if (q == (q_vector->num_ringpairs - 1))
3693 				val |= (I40E_QUEUE_END_OF_LIST <<
3694 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3695 
3696 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3697 			qp++;
3698 		}
3699 	}
3700 
3701 	i40e_flush(hw);
3702 }
3703 
3704 /**
3705  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3706  * @pf: pointer to private device data structure
3707  **/
3708 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3709 {
3710 	struct i40e_hw *hw = &pf->hw;
3711 	u32 val;
3712 
3713 	/* clear things first */
3714 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3715 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3716 
3717 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3718 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3719 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3720 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3721 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3722 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3723 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3724 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3725 
3726 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3727 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3728 
3729 	if (pf->flags & I40E_FLAG_PTP)
3730 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3731 
3732 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3733 
3734 	/* SW_ITR_IDX = 0, but don't change INTENA */
3735 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3736 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3737 
3738 	/* OTHER_ITR_IDX = 0 */
3739 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3740 }
3741 
3742 /**
3743  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3744  * @vsi: the VSI being configured
3745  **/
3746 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3747 {
3748 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3749 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3750 	struct i40e_pf *pf = vsi->back;
3751 	struct i40e_hw *hw = &pf->hw;
3752 	u32 val;
3753 
3754 	/* set the ITR configuration */
3755 	q_vector->rx.next_update = jiffies + 1;
3756 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3757 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3758 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3759 	q_vector->tx.next_update = jiffies + 1;
3760 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3761 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3762 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3763 
3764 	i40e_enable_misc_int_causes(pf);
3765 
3766 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3767 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3768 
3769 	/* Associate the queue pair to the vector and enable the queue int */
3770 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3771 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3772 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3773 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3774 
3775 	wr32(hw, I40E_QINT_RQCTL(0), val);
3776 
3777 	if (i40e_enabled_xdp_vsi(vsi)) {
3778 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
3779 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3780 		      (I40E_QUEUE_TYPE_TX
3781 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3782 
3783 		wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3784 	}
3785 
3786 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
3787 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3788 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3789 
3790 	wr32(hw, I40E_QINT_TQCTL(0), val);
3791 	i40e_flush(hw);
3792 }
3793 
3794 /**
3795  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3796  * @pf: board private structure
3797  **/
3798 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3799 {
3800 	struct i40e_hw *hw = &pf->hw;
3801 
3802 	wr32(hw, I40E_PFINT_DYN_CTL0,
3803 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3804 	i40e_flush(hw);
3805 }
3806 
3807 /**
3808  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3809  * @pf: board private structure
3810  **/
3811 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3812 {
3813 	struct i40e_hw *hw = &pf->hw;
3814 	u32 val;
3815 
3816 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
3817 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3818 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3819 
3820 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
3821 	i40e_flush(hw);
3822 }
3823 
3824 /**
3825  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3826  * @irq: interrupt number
3827  * @data: pointer to a q_vector
3828  **/
3829 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3830 {
3831 	struct i40e_q_vector *q_vector = data;
3832 
3833 	if (!q_vector->tx.ring && !q_vector->rx.ring)
3834 		return IRQ_HANDLED;
3835 
3836 	napi_schedule_irqoff(&q_vector->napi);
3837 
3838 	return IRQ_HANDLED;
3839 }
3840 
3841 /**
3842  * i40e_irq_affinity_notify - Callback for affinity changes
3843  * @notify: context as to what irq was changed
3844  * @mask: the new affinity mask
3845  *
3846  * This is a callback function used by the irq_set_affinity_notifier function
3847  * so that we may register to receive changes to the irq affinity masks.
3848  **/
3849 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3850 				     const cpumask_t *mask)
3851 {
3852 	struct i40e_q_vector *q_vector =
3853 		container_of(notify, struct i40e_q_vector, affinity_notify);
3854 
3855 	cpumask_copy(&q_vector->affinity_mask, mask);
3856 }
3857 
3858 /**
3859  * i40e_irq_affinity_release - Callback for affinity notifier release
3860  * @ref: internal core kernel usage
3861  *
3862  * This is a callback function used by the irq_set_affinity_notifier function
3863  * to inform the current notification subscriber that they will no longer
3864  * receive notifications.
3865  **/
3866 static void i40e_irq_affinity_release(struct kref *ref) {}
3867 
3868 /**
3869  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3870  * @vsi: the VSI being configured
3871  * @basename: name for the vector
3872  *
3873  * Allocates MSI-X vectors and requests interrupts from the kernel.
3874  **/
3875 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3876 {
3877 	int q_vectors = vsi->num_q_vectors;
3878 	struct i40e_pf *pf = vsi->back;
3879 	int base = vsi->base_vector;
3880 	int rx_int_idx = 0;
3881 	int tx_int_idx = 0;
3882 	int vector, err;
3883 	int irq_num;
3884 	int cpu;
3885 
3886 	for (vector = 0; vector < q_vectors; vector++) {
3887 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
3888 
3889 		irq_num = pf->msix_entries[base + vector].vector;
3890 
3891 		if (q_vector->tx.ring && q_vector->rx.ring) {
3892 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3893 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3894 			tx_int_idx++;
3895 		} else if (q_vector->rx.ring) {
3896 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3897 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
3898 		} else if (q_vector->tx.ring) {
3899 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3900 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
3901 		} else {
3902 			/* skip this unused q_vector */
3903 			continue;
3904 		}
3905 		err = request_irq(irq_num,
3906 				  vsi->irq_handler,
3907 				  0,
3908 				  q_vector->name,
3909 				  q_vector);
3910 		if (err) {
3911 			dev_info(&pf->pdev->dev,
3912 				 "MSIX request_irq failed, error: %d\n", err);
3913 			goto free_queue_irqs;
3914 		}
3915 
3916 		/* register for affinity change notifications */
3917 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3918 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
3919 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
3920 		/* Spread affinity hints out across online CPUs.
3921 		 *
3922 		 * get_cpu_mask returns a static constant mask with
3923 		 * a permanent lifetime so it's ok to pass to
3924 		 * irq_update_affinity_hint without making a copy.
3925 		 */
3926 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
3927 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
3928 	}
3929 
3930 	vsi->irqs_ready = true;
3931 	return 0;
3932 
3933 free_queue_irqs:
3934 	while (vector) {
3935 		vector--;
3936 		irq_num = pf->msix_entries[base + vector].vector;
3937 		irq_set_affinity_notifier(irq_num, NULL);
3938 		irq_update_affinity_hint(irq_num, NULL);
3939 		free_irq(irq_num, &vsi->q_vectors[vector]);
3940 	}
3941 	return err;
3942 }
3943 
3944 /**
3945  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3946  * @vsi: the VSI being un-configured
3947  **/
3948 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3949 {
3950 	struct i40e_pf *pf = vsi->back;
3951 	struct i40e_hw *hw = &pf->hw;
3952 	int base = vsi->base_vector;
3953 	int i;
3954 
3955 	/* disable interrupt causation from each queue */
3956 	for (i = 0; i < vsi->num_queue_pairs; i++) {
3957 		u32 val;
3958 
3959 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3960 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3961 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3962 
3963 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3964 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3965 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3966 
3967 		if (!i40e_enabled_xdp_vsi(vsi))
3968 			continue;
3969 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
3970 	}
3971 
3972 	/* disable each interrupt */
3973 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3974 		for (i = vsi->base_vector;
3975 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
3976 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3977 
3978 		i40e_flush(hw);
3979 		for (i = 0; i < vsi->num_q_vectors; i++)
3980 			synchronize_irq(pf->msix_entries[i + base].vector);
3981 	} else {
3982 		/* Legacy and MSI mode - this stops all interrupt handling */
3983 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3984 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3985 		i40e_flush(hw);
3986 		synchronize_irq(pf->pdev->irq);
3987 	}
3988 }
3989 
3990 /**
3991  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3992  * @vsi: the VSI being configured
3993  **/
3994 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3995 {
3996 	struct i40e_pf *pf = vsi->back;
3997 	int i;
3998 
3999 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4000 		for (i = 0; i < vsi->num_q_vectors; i++)
4001 			i40e_irq_dynamic_enable(vsi, i);
4002 	} else {
4003 		i40e_irq_dynamic_enable_icr0(pf);
4004 	}
4005 
4006 	i40e_flush(&pf->hw);
4007 	return 0;
4008 }
4009 
4010 /**
4011  * i40e_free_misc_vector - Free the vector that handles non-queue events
4012  * @pf: board private structure
4013  **/
4014 static void i40e_free_misc_vector(struct i40e_pf *pf)
4015 {
4016 	/* Disable ICR 0 */
4017 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4018 	i40e_flush(&pf->hw);
4019 
4020 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4021 		synchronize_irq(pf->msix_entries[0].vector);
4022 		free_irq(pf->msix_entries[0].vector, pf);
4023 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4024 	}
4025 }
4026 
4027 /**
4028  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4029  * @irq: interrupt number
4030  * @data: pointer to a q_vector
4031  *
4032  * This is the handler used for all MSI/Legacy interrupts, and deals
4033  * with both queue and non-queue interrupts.  This is also used in
4034  * MSIX mode to handle the non-queue interrupts.
4035  **/
4036 static irqreturn_t i40e_intr(int irq, void *data)
4037 {
4038 	struct i40e_pf *pf = (struct i40e_pf *)data;
4039 	struct i40e_hw *hw = &pf->hw;
4040 	irqreturn_t ret = IRQ_NONE;
4041 	u32 icr0, icr0_remaining;
4042 	u32 val, ena_mask;
4043 
4044 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4045 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4046 
4047 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4048 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4049 		goto enable_intr;
4050 
4051 	/* if interrupt but no bits showing, must be SWINT */
4052 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4053 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4054 		pf->sw_int_count++;
4055 
4056 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4057 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4058 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4059 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4060 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4061 	}
4062 
4063 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4064 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4065 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4066 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4067 
4068 		/* We do not have a way to disarm Queue causes while leaving
4069 		 * interrupt enabled for all other causes, ideally
4070 		 * interrupt should be disabled while we are in NAPI but
4071 		 * this is not a performance path and napi_schedule()
4072 		 * can deal with rescheduling.
4073 		 */
4074 		if (!test_bit(__I40E_DOWN, pf->state))
4075 			napi_schedule_irqoff(&q_vector->napi);
4076 	}
4077 
4078 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4079 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4080 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4081 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4082 	}
4083 
4084 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4085 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4086 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4087 	}
4088 
4089 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4090 		/* disable any further VFLR event notifications */
4091 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4092 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4093 
4094 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4095 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4096 		} else {
4097 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4098 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4099 		}
4100 	}
4101 
4102 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4103 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4104 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4105 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4106 		val = rd32(hw, I40E_GLGEN_RSTAT);
4107 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4108 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4109 		if (val == I40E_RESET_CORER) {
4110 			pf->corer_count++;
4111 		} else if (val == I40E_RESET_GLOBR) {
4112 			pf->globr_count++;
4113 		} else if (val == I40E_RESET_EMPR) {
4114 			pf->empr_count++;
4115 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4116 		}
4117 	}
4118 
4119 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4120 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4121 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4122 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4123 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4124 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4125 	}
4126 
4127 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4128 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4129 
4130 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4131 			schedule_work(&pf->ptp_extts0_work);
4132 
4133 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4134 			i40e_ptp_tx_hwtstamp(pf);
4135 
4136 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4137 	}
4138 
4139 	/* If a critical error is pending we have no choice but to reset the
4140 	 * device.
4141 	 * Report and mask out any remaining unexpected interrupts.
4142 	 */
4143 	icr0_remaining = icr0 & ena_mask;
4144 	if (icr0_remaining) {
4145 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4146 			 icr0_remaining);
4147 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4148 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4149 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4150 			dev_info(&pf->pdev->dev, "device will be reset\n");
4151 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4152 			i40e_service_event_schedule(pf);
4153 		}
4154 		ena_mask &= ~icr0_remaining;
4155 	}
4156 	ret = IRQ_HANDLED;
4157 
4158 enable_intr:
4159 	/* re-enable interrupt causes */
4160 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4161 	if (!test_bit(__I40E_DOWN, pf->state) ||
4162 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4163 		i40e_service_event_schedule(pf);
4164 		i40e_irq_dynamic_enable_icr0(pf);
4165 	}
4166 
4167 	return ret;
4168 }
4169 
4170 /**
4171  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4172  * @tx_ring:  tx ring to clean
4173  * @budget:   how many cleans we're allowed
4174  *
4175  * Returns true if there's any budget left (e.g. the clean is finished)
4176  **/
4177 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4178 {
4179 	struct i40e_vsi *vsi = tx_ring->vsi;
4180 	u16 i = tx_ring->next_to_clean;
4181 	struct i40e_tx_buffer *tx_buf;
4182 	struct i40e_tx_desc *tx_desc;
4183 
4184 	tx_buf = &tx_ring->tx_bi[i];
4185 	tx_desc = I40E_TX_DESC(tx_ring, i);
4186 	i -= tx_ring->count;
4187 
4188 	do {
4189 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4190 
4191 		/* if next_to_watch is not set then there is no work pending */
4192 		if (!eop_desc)
4193 			break;
4194 
4195 		/* prevent any other reads prior to eop_desc */
4196 		smp_rmb();
4197 
4198 		/* if the descriptor isn't done, no work yet to do */
4199 		if (!(eop_desc->cmd_type_offset_bsz &
4200 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4201 			break;
4202 
4203 		/* clear next_to_watch to prevent false hangs */
4204 		tx_buf->next_to_watch = NULL;
4205 
4206 		tx_desc->buffer_addr = 0;
4207 		tx_desc->cmd_type_offset_bsz = 0;
4208 		/* move past filter desc */
4209 		tx_buf++;
4210 		tx_desc++;
4211 		i++;
4212 		if (unlikely(!i)) {
4213 			i -= tx_ring->count;
4214 			tx_buf = tx_ring->tx_bi;
4215 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4216 		}
4217 		/* unmap skb header data */
4218 		dma_unmap_single(tx_ring->dev,
4219 				 dma_unmap_addr(tx_buf, dma),
4220 				 dma_unmap_len(tx_buf, len),
4221 				 DMA_TO_DEVICE);
4222 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4223 			kfree(tx_buf->raw_buf);
4224 
4225 		tx_buf->raw_buf = NULL;
4226 		tx_buf->tx_flags = 0;
4227 		tx_buf->next_to_watch = NULL;
4228 		dma_unmap_len_set(tx_buf, len, 0);
4229 		tx_desc->buffer_addr = 0;
4230 		tx_desc->cmd_type_offset_bsz = 0;
4231 
4232 		/* move us past the eop_desc for start of next FD desc */
4233 		tx_buf++;
4234 		tx_desc++;
4235 		i++;
4236 		if (unlikely(!i)) {
4237 			i -= tx_ring->count;
4238 			tx_buf = tx_ring->tx_bi;
4239 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4240 		}
4241 
4242 		/* update budget accounting */
4243 		budget--;
4244 	} while (likely(budget));
4245 
4246 	i += tx_ring->count;
4247 	tx_ring->next_to_clean = i;
4248 
4249 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4250 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4251 
4252 	return budget > 0;
4253 }
4254 
4255 /**
4256  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4257  * @irq: interrupt number
4258  * @data: pointer to a q_vector
4259  **/
4260 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4261 {
4262 	struct i40e_q_vector *q_vector = data;
4263 	struct i40e_vsi *vsi;
4264 
4265 	if (!q_vector->tx.ring)
4266 		return IRQ_HANDLED;
4267 
4268 	vsi = q_vector->tx.ring->vsi;
4269 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4270 
4271 	return IRQ_HANDLED;
4272 }
4273 
4274 /**
4275  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4276  * @vsi: the VSI being configured
4277  * @v_idx: vector index
4278  * @qp_idx: queue pair index
4279  **/
4280 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4281 {
4282 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4283 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4284 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4285 
4286 	tx_ring->q_vector = q_vector;
4287 	tx_ring->next = q_vector->tx.ring;
4288 	q_vector->tx.ring = tx_ring;
4289 	q_vector->tx.count++;
4290 
4291 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4292 	if (i40e_enabled_xdp_vsi(vsi)) {
4293 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4294 
4295 		xdp_ring->q_vector = q_vector;
4296 		xdp_ring->next = q_vector->tx.ring;
4297 		q_vector->tx.ring = xdp_ring;
4298 		q_vector->tx.count++;
4299 	}
4300 
4301 	rx_ring->q_vector = q_vector;
4302 	rx_ring->next = q_vector->rx.ring;
4303 	q_vector->rx.ring = rx_ring;
4304 	q_vector->rx.count++;
4305 }
4306 
4307 /**
4308  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4309  * @vsi: the VSI being configured
4310  *
4311  * This function maps descriptor rings to the queue-specific vectors
4312  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4313  * one vector per queue pair, but on a constrained vector budget, we
4314  * group the queue pairs as "efficiently" as possible.
4315  **/
4316 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4317 {
4318 	int qp_remaining = vsi->num_queue_pairs;
4319 	int q_vectors = vsi->num_q_vectors;
4320 	int num_ringpairs;
4321 	int v_start = 0;
4322 	int qp_idx = 0;
4323 
4324 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4325 	 * group them so there are multiple queues per vector.
4326 	 * It is also important to go through all the vectors available to be
4327 	 * sure that if we don't use all the vectors, that the remaining vectors
4328 	 * are cleared. This is especially important when decreasing the
4329 	 * number of queues in use.
4330 	 */
4331 	for (; v_start < q_vectors; v_start++) {
4332 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4333 
4334 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4335 
4336 		q_vector->num_ringpairs = num_ringpairs;
4337 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4338 
4339 		q_vector->rx.count = 0;
4340 		q_vector->tx.count = 0;
4341 		q_vector->rx.ring = NULL;
4342 		q_vector->tx.ring = NULL;
4343 
4344 		while (num_ringpairs--) {
4345 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4346 			qp_idx++;
4347 			qp_remaining--;
4348 		}
4349 	}
4350 }
4351 
4352 /**
4353  * i40e_vsi_request_irq - Request IRQ from the OS
4354  * @vsi: the VSI being configured
4355  * @basename: name for the vector
4356  **/
4357 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4358 {
4359 	struct i40e_pf *pf = vsi->back;
4360 	int err;
4361 
4362 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4363 		err = i40e_vsi_request_irq_msix(vsi, basename);
4364 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4365 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4366 				  pf->int_name, pf);
4367 	else
4368 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4369 				  pf->int_name, pf);
4370 
4371 	if (err)
4372 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4373 
4374 	return err;
4375 }
4376 
4377 #ifdef CONFIG_NET_POLL_CONTROLLER
4378 /**
4379  * i40e_netpoll - A Polling 'interrupt' handler
4380  * @netdev: network interface device structure
4381  *
4382  * This is used by netconsole to send skbs without having to re-enable
4383  * interrupts.  It's not called while the normal interrupt routine is executing.
4384  **/
4385 static void i40e_netpoll(struct net_device *netdev)
4386 {
4387 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4388 	struct i40e_vsi *vsi = np->vsi;
4389 	struct i40e_pf *pf = vsi->back;
4390 	int i;
4391 
4392 	/* if interface is down do nothing */
4393 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4394 		return;
4395 
4396 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4397 		for (i = 0; i < vsi->num_q_vectors; i++)
4398 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4399 	} else {
4400 		i40e_intr(pf->pdev->irq, netdev);
4401 	}
4402 }
4403 #endif
4404 
4405 #define I40E_QTX_ENA_WAIT_COUNT 50
4406 
4407 /**
4408  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4409  * @pf: the PF being configured
4410  * @pf_q: the PF queue
4411  * @enable: enable or disable state of the queue
4412  *
4413  * This routine will wait for the given Tx queue of the PF to reach the
4414  * enabled or disabled state.
4415  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4416  * multiple retries; else will return 0 in case of success.
4417  **/
4418 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4419 {
4420 	int i;
4421 	u32 tx_reg;
4422 
4423 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4424 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4425 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4426 			break;
4427 
4428 		usleep_range(10, 20);
4429 	}
4430 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4431 		return -ETIMEDOUT;
4432 
4433 	return 0;
4434 }
4435 
4436 /**
4437  * i40e_control_tx_q - Start or stop a particular Tx queue
4438  * @pf: the PF structure
4439  * @pf_q: the PF queue to configure
4440  * @enable: start or stop the queue
4441  *
4442  * This function enables or disables a single queue. Note that any delay
4443  * required after the operation is expected to be handled by the caller of
4444  * this function.
4445  **/
4446 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4447 {
4448 	struct i40e_hw *hw = &pf->hw;
4449 	u32 tx_reg;
4450 	int i;
4451 
4452 	/* warn the TX unit of coming changes */
4453 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4454 	if (!enable)
4455 		usleep_range(10, 20);
4456 
4457 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4458 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4459 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4460 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4461 			break;
4462 		usleep_range(1000, 2000);
4463 	}
4464 
4465 	/* Skip if the queue is already in the requested state */
4466 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4467 		return;
4468 
4469 	/* turn on/off the queue */
4470 	if (enable) {
4471 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4472 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4473 	} else {
4474 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4475 	}
4476 
4477 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4478 }
4479 
4480 /**
4481  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4482  * @seid: VSI SEID
4483  * @pf: the PF structure
4484  * @pf_q: the PF queue to configure
4485  * @is_xdp: true if the queue is used for XDP
4486  * @enable: start or stop the queue
4487  **/
4488 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4489 			   bool is_xdp, bool enable)
4490 {
4491 	int ret;
4492 
4493 	i40e_control_tx_q(pf, pf_q, enable);
4494 
4495 	/* wait for the change to finish */
4496 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4497 	if (ret) {
4498 		dev_info(&pf->pdev->dev,
4499 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4500 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4501 			 (enable ? "en" : "dis"));
4502 	}
4503 
4504 	return ret;
4505 }
4506 
4507 /**
4508  * i40e_vsi_enable_tx - Start a VSI's rings
4509  * @vsi: the VSI being configured
4510  **/
4511 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4512 {
4513 	struct i40e_pf *pf = vsi->back;
4514 	int i, pf_q, ret = 0;
4515 
4516 	pf_q = vsi->base_queue;
4517 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4518 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4519 					     pf_q,
4520 					     false /*is xdp*/, true);
4521 		if (ret)
4522 			break;
4523 
4524 		if (!i40e_enabled_xdp_vsi(vsi))
4525 			continue;
4526 
4527 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4528 					     pf_q + vsi->alloc_queue_pairs,
4529 					     true /*is xdp*/, true);
4530 		if (ret)
4531 			break;
4532 	}
4533 	return ret;
4534 }
4535 
4536 /**
4537  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4538  * @pf: the PF being configured
4539  * @pf_q: the PF queue
4540  * @enable: enable or disable state of the queue
4541  *
4542  * This routine will wait for the given Rx queue of the PF to reach the
4543  * enabled or disabled state.
4544  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4545  * multiple retries; else will return 0 in case of success.
4546  **/
4547 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4548 {
4549 	int i;
4550 	u32 rx_reg;
4551 
4552 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4553 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4554 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4555 			break;
4556 
4557 		usleep_range(10, 20);
4558 	}
4559 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4560 		return -ETIMEDOUT;
4561 
4562 	return 0;
4563 }
4564 
4565 /**
4566  * i40e_control_rx_q - Start or stop a particular Rx queue
4567  * @pf: the PF structure
4568  * @pf_q: the PF queue to configure
4569  * @enable: start or stop the queue
4570  *
4571  * This function enables or disables a single queue. Note that
4572  * any delay required after the operation is expected to be
4573  * handled by the caller of this function.
4574  **/
4575 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4576 {
4577 	struct i40e_hw *hw = &pf->hw;
4578 	u32 rx_reg;
4579 	int i;
4580 
4581 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4582 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4583 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4584 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4585 			break;
4586 		usleep_range(1000, 2000);
4587 	}
4588 
4589 	/* Skip if the queue is already in the requested state */
4590 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4591 		return;
4592 
4593 	/* turn on/off the queue */
4594 	if (enable)
4595 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4596 	else
4597 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4598 
4599 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4600 }
4601 
4602 /**
4603  * i40e_control_wait_rx_q
4604  * @pf: the PF structure
4605  * @pf_q: queue being configured
4606  * @enable: start or stop the rings
4607  *
4608  * This function enables or disables a single queue along with waiting
4609  * for the change to finish. The caller of this function should handle
4610  * the delays needed in the case of disabling queues.
4611  **/
4612 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4613 {
4614 	int ret = 0;
4615 
4616 	i40e_control_rx_q(pf, pf_q, enable);
4617 
4618 	/* wait for the change to finish */
4619 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4620 	if (ret)
4621 		return ret;
4622 
4623 	return ret;
4624 }
4625 
4626 /**
4627  * i40e_vsi_enable_rx - Start a VSI's rings
4628  * @vsi: the VSI being configured
4629  **/
4630 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4631 {
4632 	struct i40e_pf *pf = vsi->back;
4633 	int i, pf_q, ret = 0;
4634 
4635 	pf_q = vsi->base_queue;
4636 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4637 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4638 		if (ret) {
4639 			dev_info(&pf->pdev->dev,
4640 				 "VSI seid %d Rx ring %d enable timeout\n",
4641 				 vsi->seid, pf_q);
4642 			break;
4643 		}
4644 	}
4645 
4646 	return ret;
4647 }
4648 
4649 /**
4650  * i40e_vsi_start_rings - Start a VSI's rings
4651  * @vsi: the VSI being configured
4652  **/
4653 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4654 {
4655 	int ret = 0;
4656 
4657 	/* do rx first for enable and last for disable */
4658 	ret = i40e_vsi_enable_rx(vsi);
4659 	if (ret)
4660 		return ret;
4661 	ret = i40e_vsi_enable_tx(vsi);
4662 
4663 	return ret;
4664 }
4665 
4666 #define I40E_DISABLE_TX_GAP_MSEC	50
4667 
4668 /**
4669  * i40e_vsi_stop_rings - Stop a VSI's rings
4670  * @vsi: the VSI being configured
4671  **/
4672 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4673 {
4674 	struct i40e_pf *pf = vsi->back;
4675 	int pf_q, err, q_end;
4676 
4677 	/* When port TX is suspended, don't wait */
4678 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4679 		return i40e_vsi_stop_rings_no_wait(vsi);
4680 
4681 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4682 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4683 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4684 
4685 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4686 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4687 		if (err)
4688 			dev_info(&pf->pdev->dev,
4689 				 "VSI seid %d Rx ring %d disable timeout\n",
4690 				 vsi->seid, pf_q);
4691 	}
4692 
4693 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4694 	pf_q = vsi->base_queue;
4695 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4696 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4697 
4698 	i40e_vsi_wait_queues_disabled(vsi);
4699 }
4700 
4701 /**
4702  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4703  * @vsi: the VSI being shutdown
4704  *
4705  * This function stops all the rings for a VSI but does not delay to verify
4706  * that rings have been disabled. It is expected that the caller is shutting
4707  * down multiple VSIs at once and will delay together for all the VSIs after
4708  * initiating the shutdown. This is particularly useful for shutting down lots
4709  * of VFs together. Otherwise, a large delay can be incurred while configuring
4710  * each VSI in serial.
4711  **/
4712 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4713 {
4714 	struct i40e_pf *pf = vsi->back;
4715 	int i, pf_q;
4716 
4717 	pf_q = vsi->base_queue;
4718 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4719 		i40e_control_tx_q(pf, pf_q, false);
4720 		i40e_control_rx_q(pf, pf_q, false);
4721 	}
4722 }
4723 
4724 /**
4725  * i40e_vsi_free_irq - Free the irq association with the OS
4726  * @vsi: the VSI being configured
4727  **/
4728 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4729 {
4730 	struct i40e_pf *pf = vsi->back;
4731 	struct i40e_hw *hw = &pf->hw;
4732 	int base = vsi->base_vector;
4733 	u32 val, qp;
4734 	int i;
4735 
4736 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4737 		if (!vsi->q_vectors)
4738 			return;
4739 
4740 		if (!vsi->irqs_ready)
4741 			return;
4742 
4743 		vsi->irqs_ready = false;
4744 		for (i = 0; i < vsi->num_q_vectors; i++) {
4745 			int irq_num;
4746 			u16 vector;
4747 
4748 			vector = i + base;
4749 			irq_num = pf->msix_entries[vector].vector;
4750 
4751 			/* free only the irqs that were actually requested */
4752 			if (!vsi->q_vectors[i] ||
4753 			    !vsi->q_vectors[i]->num_ringpairs)
4754 				continue;
4755 
4756 			/* clear the affinity notifier in the IRQ descriptor */
4757 			irq_set_affinity_notifier(irq_num, NULL);
4758 			/* remove our suggested affinity mask for this IRQ */
4759 			irq_update_affinity_hint(irq_num, NULL);
4760 			synchronize_irq(irq_num);
4761 			free_irq(irq_num, vsi->q_vectors[i]);
4762 
4763 			/* Tear down the interrupt queue link list
4764 			 *
4765 			 * We know that they come in pairs and always
4766 			 * the Rx first, then the Tx.  To clear the
4767 			 * link list, stick the EOL value into the
4768 			 * next_q field of the registers.
4769 			 */
4770 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4771 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4772 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4773 			val |= I40E_QUEUE_END_OF_LIST
4774 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4775 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4776 
4777 			while (qp != I40E_QUEUE_END_OF_LIST) {
4778 				u32 next;
4779 
4780 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4781 
4782 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4783 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4784 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4785 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4786 
4787 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4788 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4789 
4790 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4791 
4792 				val = rd32(hw, I40E_QINT_TQCTL(qp));
4793 
4794 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4795 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4796 
4797 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4798 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4799 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4800 					 I40E_QINT_TQCTL_INTEVENT_MASK);
4801 
4802 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4803 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4804 
4805 				wr32(hw, I40E_QINT_TQCTL(qp), val);
4806 				qp = next;
4807 			}
4808 		}
4809 	} else {
4810 		free_irq(pf->pdev->irq, pf);
4811 
4812 		val = rd32(hw, I40E_PFINT_LNKLST0);
4813 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4814 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4815 		val |= I40E_QUEUE_END_OF_LIST
4816 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4817 		wr32(hw, I40E_PFINT_LNKLST0, val);
4818 
4819 		val = rd32(hw, I40E_QINT_RQCTL(qp));
4820 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4821 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4822 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4823 			 I40E_QINT_RQCTL_INTEVENT_MASK);
4824 
4825 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4826 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4827 
4828 		wr32(hw, I40E_QINT_RQCTL(qp), val);
4829 
4830 		val = rd32(hw, I40E_QINT_TQCTL(qp));
4831 
4832 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4833 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4834 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4835 			 I40E_QINT_TQCTL_INTEVENT_MASK);
4836 
4837 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4838 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4839 
4840 		wr32(hw, I40E_QINT_TQCTL(qp), val);
4841 	}
4842 }
4843 
4844 /**
4845  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4846  * @vsi: the VSI being configured
4847  * @v_idx: Index of vector to be freed
4848  *
4849  * This function frees the memory allocated to the q_vector.  In addition if
4850  * NAPI is enabled it will delete any references to the NAPI struct prior
4851  * to freeing the q_vector.
4852  **/
4853 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4854 {
4855 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4856 	struct i40e_ring *ring;
4857 
4858 	if (!q_vector)
4859 		return;
4860 
4861 	/* disassociate q_vector from rings */
4862 	i40e_for_each_ring(ring, q_vector->tx)
4863 		ring->q_vector = NULL;
4864 
4865 	i40e_for_each_ring(ring, q_vector->rx)
4866 		ring->q_vector = NULL;
4867 
4868 	/* only VSI w/ an associated netdev is set up w/ NAPI */
4869 	if (vsi->netdev)
4870 		netif_napi_del(&q_vector->napi);
4871 
4872 	vsi->q_vectors[v_idx] = NULL;
4873 
4874 	kfree_rcu(q_vector, rcu);
4875 }
4876 
4877 /**
4878  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4879  * @vsi: the VSI being un-configured
4880  *
4881  * This frees the memory allocated to the q_vectors and
4882  * deletes references to the NAPI struct.
4883  **/
4884 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4885 {
4886 	int v_idx;
4887 
4888 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4889 		i40e_free_q_vector(vsi, v_idx);
4890 }
4891 
4892 /**
4893  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4894  * @pf: board private structure
4895  **/
4896 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4897 {
4898 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4899 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4900 		pci_disable_msix(pf->pdev);
4901 		kfree(pf->msix_entries);
4902 		pf->msix_entries = NULL;
4903 		kfree(pf->irq_pile);
4904 		pf->irq_pile = NULL;
4905 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4906 		pci_disable_msi(pf->pdev);
4907 	}
4908 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4909 }
4910 
4911 /**
4912  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4913  * @pf: board private structure
4914  *
4915  * We go through and clear interrupt specific resources and reset the structure
4916  * to pre-load conditions
4917  **/
4918 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4919 {
4920 	int i;
4921 
4922 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
4923 		i40e_free_misc_vector(pf);
4924 
4925 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4926 		      I40E_IWARP_IRQ_PILE_ID);
4927 
4928 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
4929 	for (i = 0; i < pf->num_alloc_vsi; i++)
4930 		if (pf->vsi[i])
4931 			i40e_vsi_free_q_vectors(pf->vsi[i]);
4932 	i40e_reset_interrupt_capability(pf);
4933 }
4934 
4935 /**
4936  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4937  * @vsi: the VSI being configured
4938  **/
4939 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4940 {
4941 	int q_idx;
4942 
4943 	if (!vsi->netdev)
4944 		return;
4945 
4946 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4947 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4948 
4949 		if (q_vector->rx.ring || q_vector->tx.ring)
4950 			napi_enable(&q_vector->napi);
4951 	}
4952 }
4953 
4954 /**
4955  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4956  * @vsi: the VSI being configured
4957  **/
4958 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4959 {
4960 	int q_idx;
4961 
4962 	if (!vsi->netdev)
4963 		return;
4964 
4965 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4966 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4967 
4968 		if (q_vector->rx.ring || q_vector->tx.ring)
4969 			napi_disable(&q_vector->napi);
4970 	}
4971 }
4972 
4973 /**
4974  * i40e_vsi_close - Shut down a VSI
4975  * @vsi: the vsi to be quelled
4976  **/
4977 static void i40e_vsi_close(struct i40e_vsi *vsi)
4978 {
4979 	struct i40e_pf *pf = vsi->back;
4980 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
4981 		i40e_down(vsi);
4982 	i40e_vsi_free_irq(vsi);
4983 	i40e_vsi_free_tx_resources(vsi);
4984 	i40e_vsi_free_rx_resources(vsi);
4985 	vsi->current_netdev_flags = 0;
4986 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
4987 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4988 		set_bit(__I40E_CLIENT_RESET, pf->state);
4989 }
4990 
4991 /**
4992  * i40e_quiesce_vsi - Pause a given VSI
4993  * @vsi: the VSI being paused
4994  **/
4995 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4996 {
4997 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4998 		return;
4999 
5000 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5001 	if (vsi->netdev && netif_running(vsi->netdev))
5002 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5003 	else
5004 		i40e_vsi_close(vsi);
5005 }
5006 
5007 /**
5008  * i40e_unquiesce_vsi - Resume a given VSI
5009  * @vsi: the VSI being resumed
5010  **/
5011 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5012 {
5013 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5014 		return;
5015 
5016 	if (vsi->netdev && netif_running(vsi->netdev))
5017 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5018 	else
5019 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5020 }
5021 
5022 /**
5023  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5024  * @pf: the PF
5025  **/
5026 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5027 {
5028 	int v;
5029 
5030 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5031 		if (pf->vsi[v])
5032 			i40e_quiesce_vsi(pf->vsi[v]);
5033 	}
5034 }
5035 
5036 /**
5037  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5038  * @pf: the PF
5039  **/
5040 static void i40e_pf_unquiesce_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_unquiesce_vsi(pf->vsi[v]);
5047 	}
5048 }
5049 
5050 /**
5051  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5052  * @vsi: the VSI being configured
5053  *
5054  * Wait until all queues on a given VSI have been disabled.
5055  **/
5056 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5057 {
5058 	struct i40e_pf *pf = vsi->back;
5059 	int i, pf_q, ret;
5060 
5061 	pf_q = vsi->base_queue;
5062 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5063 		/* Check and wait for the Tx queue */
5064 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5065 		if (ret) {
5066 			dev_info(&pf->pdev->dev,
5067 				 "VSI seid %d Tx ring %d disable timeout\n",
5068 				 vsi->seid, pf_q);
5069 			return ret;
5070 		}
5071 
5072 		if (!i40e_enabled_xdp_vsi(vsi))
5073 			goto wait_rx;
5074 
5075 		/* Check and wait for the XDP Tx queue */
5076 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5077 				       false);
5078 		if (ret) {
5079 			dev_info(&pf->pdev->dev,
5080 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5081 				 vsi->seid, pf_q);
5082 			return ret;
5083 		}
5084 wait_rx:
5085 		/* Check and wait for the Rx queue */
5086 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5087 		if (ret) {
5088 			dev_info(&pf->pdev->dev,
5089 				 "VSI seid %d Rx ring %d disable timeout\n",
5090 				 vsi->seid, pf_q);
5091 			return ret;
5092 		}
5093 	}
5094 
5095 	return 0;
5096 }
5097 
5098 #ifdef CONFIG_I40E_DCB
5099 /**
5100  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5101  * @pf: the PF
5102  *
5103  * This function waits for the queues to be in disabled state for all the
5104  * VSIs that are managed by this PF.
5105  **/
5106 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5107 {
5108 	int v, ret = 0;
5109 
5110 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5111 		if (pf->vsi[v]) {
5112 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5113 			if (ret)
5114 				break;
5115 		}
5116 	}
5117 
5118 	return ret;
5119 }
5120 
5121 #endif
5122 
5123 /**
5124  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5125  * @pf: pointer to PF
5126  *
5127  * Get TC map for ISCSI PF type that will include iSCSI TC
5128  * and LAN TC.
5129  **/
5130 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5131 {
5132 	struct i40e_dcb_app_priority_table app;
5133 	struct i40e_hw *hw = &pf->hw;
5134 	u8 enabled_tc = 1; /* TC0 is always enabled */
5135 	u8 tc, i;
5136 	/* Get the iSCSI APP TLV */
5137 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5138 
5139 	for (i = 0; i < dcbcfg->numapps; i++) {
5140 		app = dcbcfg->app[i];
5141 		if (app.selector == I40E_APP_SEL_TCPIP &&
5142 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5143 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5144 			enabled_tc |= BIT(tc);
5145 			break;
5146 		}
5147 	}
5148 
5149 	return enabled_tc;
5150 }
5151 
5152 /**
5153  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5154  * @dcbcfg: the corresponding DCBx configuration structure
5155  *
5156  * Return the number of TCs from given DCBx configuration
5157  **/
5158 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5159 {
5160 	int i, tc_unused = 0;
5161 	u8 num_tc = 0;
5162 	u8 ret = 0;
5163 
5164 	/* Scan the ETS Config Priority Table to find
5165 	 * traffic class enabled for a given priority
5166 	 * and create a bitmask of enabled TCs
5167 	 */
5168 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5169 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5170 
5171 	/* Now scan the bitmask to check for
5172 	 * contiguous TCs starting with TC0
5173 	 */
5174 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5175 		if (num_tc & BIT(i)) {
5176 			if (!tc_unused) {
5177 				ret++;
5178 			} else {
5179 				pr_err("Non-contiguous TC - Disabling DCB\n");
5180 				return 1;
5181 			}
5182 		} else {
5183 			tc_unused = 1;
5184 		}
5185 	}
5186 
5187 	/* There is always at least TC0 */
5188 	if (!ret)
5189 		ret = 1;
5190 
5191 	return ret;
5192 }
5193 
5194 /**
5195  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5196  * @dcbcfg: the corresponding DCBx configuration structure
5197  *
5198  * Query the current DCB configuration and return the number of
5199  * traffic classes enabled from the given DCBX config
5200  **/
5201 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5202 {
5203 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5204 	u8 enabled_tc = 1;
5205 	u8 i;
5206 
5207 	for (i = 0; i < num_tc; i++)
5208 		enabled_tc |= BIT(i);
5209 
5210 	return enabled_tc;
5211 }
5212 
5213 /**
5214  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5215  * @pf: PF being queried
5216  *
5217  * Query the current MQPRIO configuration and return the number of
5218  * traffic classes enabled.
5219  **/
5220 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5221 {
5222 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5223 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5224 	u8 enabled_tc = 1, i;
5225 
5226 	for (i = 1; i < num_tc; i++)
5227 		enabled_tc |= BIT(i);
5228 	return enabled_tc;
5229 }
5230 
5231 /**
5232  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5233  * @pf: PF being queried
5234  *
5235  * Return number of traffic classes enabled for the given PF
5236  **/
5237 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5238 {
5239 	struct i40e_hw *hw = &pf->hw;
5240 	u8 i, enabled_tc = 1;
5241 	u8 num_tc = 0;
5242 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5243 
5244 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5245 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5246 
5247 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5248 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5249 		return 1;
5250 
5251 	/* SFP mode will be enabled for all TCs on port */
5252 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5253 		return i40e_dcb_get_num_tc(dcbcfg);
5254 
5255 	/* MFP mode return count of enabled TCs for this PF */
5256 	if (pf->hw.func_caps.iscsi)
5257 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5258 	else
5259 		return 1; /* Only TC0 */
5260 
5261 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5262 		if (enabled_tc & BIT(i))
5263 			num_tc++;
5264 	}
5265 	return num_tc;
5266 }
5267 
5268 /**
5269  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5270  * @pf: PF being queried
5271  *
5272  * Return a bitmap for enabled traffic classes for this PF.
5273  **/
5274 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5275 {
5276 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5277 		return i40e_mqprio_get_enabled_tc(pf);
5278 
5279 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5280 	 * default TC
5281 	 */
5282 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5283 		return I40E_DEFAULT_TRAFFIC_CLASS;
5284 
5285 	/* SFP mode we want PF to be enabled for all TCs */
5286 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5287 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5288 
5289 	/* MFP enabled and iSCSI PF type */
5290 	if (pf->hw.func_caps.iscsi)
5291 		return i40e_get_iscsi_tc_map(pf);
5292 	else
5293 		return I40E_DEFAULT_TRAFFIC_CLASS;
5294 }
5295 
5296 /**
5297  * i40e_vsi_get_bw_info - Query VSI BW Information
5298  * @vsi: the VSI being queried
5299  *
5300  * Returns 0 on success, negative value on failure
5301  **/
5302 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5303 {
5304 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5305 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5306 	struct i40e_pf *pf = vsi->back;
5307 	struct i40e_hw *hw = &pf->hw;
5308 	i40e_status ret;
5309 	u32 tc_bw_max;
5310 	int i;
5311 
5312 	/* Get the VSI level BW configuration */
5313 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5314 	if (ret) {
5315 		dev_info(&pf->pdev->dev,
5316 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5317 			 i40e_stat_str(&pf->hw, ret),
5318 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5319 		return -EINVAL;
5320 	}
5321 
5322 	/* Get the VSI level BW configuration per TC */
5323 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5324 					       NULL);
5325 	if (ret) {
5326 		dev_info(&pf->pdev->dev,
5327 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5328 			 i40e_stat_str(&pf->hw, ret),
5329 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5330 		return -EINVAL;
5331 	}
5332 
5333 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5334 		dev_info(&pf->pdev->dev,
5335 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5336 			 bw_config.tc_valid_bits,
5337 			 bw_ets_config.tc_valid_bits);
5338 		/* Still continuing */
5339 	}
5340 
5341 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5342 	vsi->bw_max_quanta = bw_config.max_bw;
5343 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5344 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5345 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5346 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5347 		vsi->bw_ets_limit_credits[i] =
5348 					le16_to_cpu(bw_ets_config.credits[i]);
5349 		/* 3 bits out of 4 for each TC */
5350 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5351 	}
5352 
5353 	return 0;
5354 }
5355 
5356 /**
5357  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5358  * @vsi: the VSI being configured
5359  * @enabled_tc: TC bitmap
5360  * @bw_share: BW shared credits per TC
5361  *
5362  * Returns 0 on success, negative value on failure
5363  **/
5364 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5365 				       u8 *bw_share)
5366 {
5367 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5368 	struct i40e_pf *pf = vsi->back;
5369 	i40e_status ret;
5370 	int i;
5371 
5372 	/* There is no need to reset BW when mqprio mode is on.  */
5373 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5374 		return 0;
5375 
5376 	if (!vsi->mqprio_qopt.qopt.hw) {
5377 		if (pf->flags & I40E_FLAG_DCB_ENABLED)
5378 			goto skip_reset;
5379 
5380 		if (IS_ENABLED(CONFIG_I40E_DCB) &&
5381 		    i40e_dcb_hw_get_num_tc(&pf->hw) == 1)
5382 			goto skip_reset;
5383 
5384 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5385 		if (ret)
5386 			dev_info(&pf->pdev->dev,
5387 				 "Failed to reset tx rate for vsi->seid %u\n",
5388 				 vsi->seid);
5389 		return ret;
5390 	}
5391 
5392 skip_reset:
5393 	memset(&bw_data, 0, sizeof(bw_data));
5394 	bw_data.tc_valid_bits = enabled_tc;
5395 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5396 		bw_data.tc_bw_credits[i] = bw_share[i];
5397 
5398 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5399 	if (ret) {
5400 		dev_info(&pf->pdev->dev,
5401 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5402 			 pf->hw.aq.asq_last_status);
5403 		return -EINVAL;
5404 	}
5405 
5406 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5407 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5408 
5409 	return 0;
5410 }
5411 
5412 /**
5413  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5414  * @vsi: the VSI being configured
5415  * @enabled_tc: TC map to be enabled
5416  *
5417  **/
5418 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5419 {
5420 	struct net_device *netdev = vsi->netdev;
5421 	struct i40e_pf *pf = vsi->back;
5422 	struct i40e_hw *hw = &pf->hw;
5423 	u8 netdev_tc = 0;
5424 	int i;
5425 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5426 
5427 	if (!netdev)
5428 		return;
5429 
5430 	if (!enabled_tc) {
5431 		netdev_reset_tc(netdev);
5432 		return;
5433 	}
5434 
5435 	/* Set up actual enabled TCs on the VSI */
5436 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5437 		return;
5438 
5439 	/* set per TC queues for the VSI */
5440 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5441 		/* Only set TC queues for enabled tcs
5442 		 *
5443 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5444 		 * enabled_tc bitmap would be 0x00001001; the driver
5445 		 * will set the numtc for netdev as 2 that will be
5446 		 * referenced by the netdev layer as TC 0 and 1.
5447 		 */
5448 		if (vsi->tc_config.enabled_tc & BIT(i))
5449 			netdev_set_tc_queue(netdev,
5450 					vsi->tc_config.tc_info[i].netdev_tc,
5451 					vsi->tc_config.tc_info[i].qcount,
5452 					vsi->tc_config.tc_info[i].qoffset);
5453 	}
5454 
5455 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5456 		return;
5457 
5458 	/* Assign UP2TC map for the VSI */
5459 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5460 		/* Get the actual TC# for the UP */
5461 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5462 		/* Get the mapped netdev TC# for the UP */
5463 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5464 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5465 	}
5466 }
5467 
5468 /**
5469  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5470  * @vsi: the VSI being configured
5471  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5472  **/
5473 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5474 				      struct i40e_vsi_context *ctxt)
5475 {
5476 	/* copy just the sections touched not the entire info
5477 	 * since not all sections are valid as returned by
5478 	 * update vsi params
5479 	 */
5480 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5481 	memcpy(&vsi->info.queue_mapping,
5482 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5483 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5484 	       sizeof(vsi->info.tc_mapping));
5485 }
5486 
5487 /**
5488  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5489  * @vsi: the VSI being reconfigured
5490  * @vsi_offset: offset from main VF VSI
5491  */
5492 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5493 {
5494 	struct i40e_vsi_context ctxt = {};
5495 	struct i40e_pf *pf;
5496 	struct i40e_hw *hw;
5497 	int ret;
5498 
5499 	if (!vsi)
5500 		return I40E_ERR_PARAM;
5501 	pf = vsi->back;
5502 	hw = &pf->hw;
5503 
5504 	ctxt.seid = vsi->seid;
5505 	ctxt.pf_num = hw->pf_id;
5506 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5507 	ctxt.uplink_seid = vsi->uplink_seid;
5508 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5509 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5510 	ctxt.info = vsi->info;
5511 
5512 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5513 				 false);
5514 	if (vsi->reconfig_rss) {
5515 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5516 				      vsi->num_queue_pairs);
5517 		ret = i40e_vsi_config_rss(vsi);
5518 		if (ret) {
5519 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5520 			return ret;
5521 		}
5522 		vsi->reconfig_rss = false;
5523 	}
5524 
5525 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5526 	if (ret) {
5527 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %s aq_err %s\n",
5528 			 i40e_stat_str(hw, ret),
5529 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5530 		return ret;
5531 	}
5532 	/* update the local VSI info with updated queue map */
5533 	i40e_vsi_update_queue_map(vsi, &ctxt);
5534 	vsi->info.valid_sections = 0;
5535 
5536 	return ret;
5537 }
5538 
5539 /**
5540  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5541  * @vsi: VSI to be configured
5542  * @enabled_tc: TC bitmap
5543  *
5544  * This configures a particular VSI for TCs that are mapped to the
5545  * given TC bitmap. It uses default bandwidth share for TCs across
5546  * VSIs to configure TC for a particular VSI.
5547  *
5548  * NOTE:
5549  * It is expected that the VSI queues have been quisced before calling
5550  * this function.
5551  **/
5552 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5553 {
5554 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5555 	struct i40e_pf *pf = vsi->back;
5556 	struct i40e_hw *hw = &pf->hw;
5557 	struct i40e_vsi_context ctxt;
5558 	int ret = 0;
5559 	int i;
5560 
5561 	/* Check if enabled_tc is same as existing or new TCs */
5562 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5563 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5564 		return ret;
5565 
5566 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5567 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5568 		if (enabled_tc & BIT(i))
5569 			bw_share[i] = 1;
5570 	}
5571 
5572 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5573 	if (ret) {
5574 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5575 
5576 		dev_info(&pf->pdev->dev,
5577 			 "Failed configuring TC map %d for VSI %d\n",
5578 			 enabled_tc, vsi->seid);
5579 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5580 						  &bw_config, NULL);
5581 		if (ret) {
5582 			dev_info(&pf->pdev->dev,
5583 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5584 				 i40e_stat_str(hw, ret),
5585 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5586 			goto out;
5587 		}
5588 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5589 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5590 
5591 			if (!valid_tc)
5592 				valid_tc = bw_config.tc_valid_bits;
5593 			/* Always enable TC0, no matter what */
5594 			valid_tc |= 1;
5595 			dev_info(&pf->pdev->dev,
5596 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5597 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5598 			enabled_tc = valid_tc;
5599 		}
5600 
5601 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5602 		if (ret) {
5603 			dev_err(&pf->pdev->dev,
5604 				"Unable to  configure TC map %d for VSI %d\n",
5605 				enabled_tc, vsi->seid);
5606 			goto out;
5607 		}
5608 	}
5609 
5610 	/* Update Queue Pairs Mapping for currently enabled UPs */
5611 	ctxt.seid = vsi->seid;
5612 	ctxt.pf_num = vsi->back->hw.pf_id;
5613 	ctxt.vf_num = 0;
5614 	ctxt.uplink_seid = vsi->uplink_seid;
5615 	ctxt.info = vsi->info;
5616 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5617 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5618 		if (ret)
5619 			goto out;
5620 	} else {
5621 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5622 	}
5623 
5624 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5625 	 * queues changed.
5626 	 */
5627 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5628 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5629 				      vsi->num_queue_pairs);
5630 		ret = i40e_vsi_config_rss(vsi);
5631 		if (ret) {
5632 			dev_info(&vsi->back->pdev->dev,
5633 				 "Failed to reconfig rss for num_queues\n");
5634 			return ret;
5635 		}
5636 		vsi->reconfig_rss = false;
5637 	}
5638 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5639 		ctxt.info.valid_sections |=
5640 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5641 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5642 	}
5643 
5644 	/* Update the VSI after updating the VSI queue-mapping
5645 	 * information
5646 	 */
5647 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5648 	if (ret) {
5649 		dev_info(&pf->pdev->dev,
5650 			 "Update vsi tc config failed, err %s aq_err %s\n",
5651 			 i40e_stat_str(hw, ret),
5652 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5653 		goto out;
5654 	}
5655 	/* update the local VSI info with updated queue map */
5656 	i40e_vsi_update_queue_map(vsi, &ctxt);
5657 	vsi->info.valid_sections = 0;
5658 
5659 	/* Update current VSI BW information */
5660 	ret = i40e_vsi_get_bw_info(vsi);
5661 	if (ret) {
5662 		dev_info(&pf->pdev->dev,
5663 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5664 			 i40e_stat_str(hw, ret),
5665 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5666 		goto out;
5667 	}
5668 
5669 	/* Update the netdev TC setup */
5670 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5671 out:
5672 	return ret;
5673 }
5674 
5675 /**
5676  * i40e_get_link_speed - Returns link speed for the interface
5677  * @vsi: VSI to be configured
5678  *
5679  **/
5680 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5681 {
5682 	struct i40e_pf *pf = vsi->back;
5683 
5684 	switch (pf->hw.phy.link_info.link_speed) {
5685 	case I40E_LINK_SPEED_40GB:
5686 		return 40000;
5687 	case I40E_LINK_SPEED_25GB:
5688 		return 25000;
5689 	case I40E_LINK_SPEED_20GB:
5690 		return 20000;
5691 	case I40E_LINK_SPEED_10GB:
5692 		return 10000;
5693 	case I40E_LINK_SPEED_1GB:
5694 		return 1000;
5695 	default:
5696 		return -EINVAL;
5697 	}
5698 }
5699 
5700 /**
5701  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5702  * @vsi: VSI to be configured
5703  * @seid: seid of the channel/VSI
5704  * @max_tx_rate: max TX rate to be configured as BW limit
5705  *
5706  * Helper function to set BW limit for a given VSI
5707  **/
5708 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5709 {
5710 	struct i40e_pf *pf = vsi->back;
5711 	u64 credits = 0;
5712 	int speed = 0;
5713 	int ret = 0;
5714 
5715 	speed = i40e_get_link_speed(vsi);
5716 	if (max_tx_rate > speed) {
5717 		dev_err(&pf->pdev->dev,
5718 			"Invalid max tx rate %llu specified for VSI seid %d.",
5719 			max_tx_rate, seid);
5720 		return -EINVAL;
5721 	}
5722 	if (max_tx_rate && max_tx_rate < 50) {
5723 		dev_warn(&pf->pdev->dev,
5724 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5725 		max_tx_rate = 50;
5726 	}
5727 
5728 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5729 	credits = max_tx_rate;
5730 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5731 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5732 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5733 	if (ret)
5734 		dev_err(&pf->pdev->dev,
5735 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5736 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5737 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5738 	return ret;
5739 }
5740 
5741 /**
5742  * i40e_remove_queue_channels - Remove queue channels for the TCs
5743  * @vsi: VSI to be configured
5744  *
5745  * Remove queue channels for the TCs
5746  **/
5747 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5748 {
5749 	enum i40e_admin_queue_err last_aq_status;
5750 	struct i40e_cloud_filter *cfilter;
5751 	struct i40e_channel *ch, *ch_tmp;
5752 	struct i40e_pf *pf = vsi->back;
5753 	struct hlist_node *node;
5754 	int ret, i;
5755 
5756 	/* Reset rss size that was stored when reconfiguring rss for
5757 	 * channel VSIs with non-power-of-2 queue count.
5758 	 */
5759 	vsi->current_rss_size = 0;
5760 
5761 	/* perform cleanup for channels if they exist */
5762 	if (list_empty(&vsi->ch_list))
5763 		return;
5764 
5765 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5766 		struct i40e_vsi *p_vsi;
5767 
5768 		list_del(&ch->list);
5769 		p_vsi = ch->parent_vsi;
5770 		if (!p_vsi || !ch->initialized) {
5771 			kfree(ch);
5772 			continue;
5773 		}
5774 		/* Reset queue contexts */
5775 		for (i = 0; i < ch->num_queue_pairs; i++) {
5776 			struct i40e_ring *tx_ring, *rx_ring;
5777 			u16 pf_q;
5778 
5779 			pf_q = ch->base_queue + i;
5780 			tx_ring = vsi->tx_rings[pf_q];
5781 			tx_ring->ch = NULL;
5782 
5783 			rx_ring = vsi->rx_rings[pf_q];
5784 			rx_ring->ch = NULL;
5785 		}
5786 
5787 		/* Reset BW configured for this VSI via mqprio */
5788 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5789 		if (ret)
5790 			dev_info(&vsi->back->pdev->dev,
5791 				 "Failed to reset tx rate for ch->seid %u\n",
5792 				 ch->seid);
5793 
5794 		/* delete cloud filters associated with this channel */
5795 		hlist_for_each_entry_safe(cfilter, node,
5796 					  &pf->cloud_filter_list, cloud_node) {
5797 			if (cfilter->seid != ch->seid)
5798 				continue;
5799 
5800 			hash_del(&cfilter->cloud_node);
5801 			if (cfilter->dst_port)
5802 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
5803 									cfilter,
5804 									false);
5805 			else
5806 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
5807 								false);
5808 			last_aq_status = pf->hw.aq.asq_last_status;
5809 			if (ret)
5810 				dev_info(&pf->pdev->dev,
5811 					 "Failed to delete cloud filter, err %s aq_err %s\n",
5812 					 i40e_stat_str(&pf->hw, ret),
5813 					 i40e_aq_str(&pf->hw, last_aq_status));
5814 			kfree(cfilter);
5815 		}
5816 
5817 		/* delete VSI from FW */
5818 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5819 					     NULL);
5820 		if (ret)
5821 			dev_err(&vsi->back->pdev->dev,
5822 				"unable to remove channel (%d) for parent VSI(%d)\n",
5823 				ch->seid, p_vsi->seid);
5824 		kfree(ch);
5825 	}
5826 	INIT_LIST_HEAD(&vsi->ch_list);
5827 }
5828 
5829 /**
5830  * i40e_get_max_queues_for_channel
5831  * @vsi: ptr to VSI to which channels are associated with
5832  *
5833  * Helper function which returns max value among the queue counts set on the
5834  * channels/TCs created.
5835  **/
5836 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5837 {
5838 	struct i40e_channel *ch, *ch_tmp;
5839 	int max = 0;
5840 
5841 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5842 		if (!ch->initialized)
5843 			continue;
5844 		if (ch->num_queue_pairs > max)
5845 			max = ch->num_queue_pairs;
5846 	}
5847 
5848 	return max;
5849 }
5850 
5851 /**
5852  * i40e_validate_num_queues - validate num_queues w.r.t channel
5853  * @pf: ptr to PF device
5854  * @num_queues: number of queues
5855  * @vsi: the parent VSI
5856  * @reconfig_rss: indicates should the RSS be reconfigured or not
5857  *
5858  * This function validates number of queues in the context of new channel
5859  * which is being established and determines if RSS should be reconfigured
5860  * or not for parent VSI.
5861  **/
5862 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5863 				    struct i40e_vsi *vsi, bool *reconfig_rss)
5864 {
5865 	int max_ch_queues;
5866 
5867 	if (!reconfig_rss)
5868 		return -EINVAL;
5869 
5870 	*reconfig_rss = false;
5871 	if (vsi->current_rss_size) {
5872 		if (num_queues > vsi->current_rss_size) {
5873 			dev_dbg(&pf->pdev->dev,
5874 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
5875 				num_queues, vsi->current_rss_size);
5876 			return -EINVAL;
5877 		} else if ((num_queues < vsi->current_rss_size) &&
5878 			   (!is_power_of_2(num_queues))) {
5879 			dev_dbg(&pf->pdev->dev,
5880 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5881 				num_queues, vsi->current_rss_size);
5882 			return -EINVAL;
5883 		}
5884 	}
5885 
5886 	if (!is_power_of_2(num_queues)) {
5887 		/* Find the max num_queues configured for channel if channel
5888 		 * exist.
5889 		 * if channel exist, then enforce 'num_queues' to be more than
5890 		 * max ever queues configured for channel.
5891 		 */
5892 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5893 		if (num_queues < max_ch_queues) {
5894 			dev_dbg(&pf->pdev->dev,
5895 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
5896 				num_queues, max_ch_queues);
5897 			return -EINVAL;
5898 		}
5899 		*reconfig_rss = true;
5900 	}
5901 
5902 	return 0;
5903 }
5904 
5905 /**
5906  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5907  * @vsi: the VSI being setup
5908  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5909  *
5910  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5911  **/
5912 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5913 {
5914 	struct i40e_pf *pf = vsi->back;
5915 	u8 seed[I40E_HKEY_ARRAY_SIZE];
5916 	struct i40e_hw *hw = &pf->hw;
5917 	int local_rss_size;
5918 	u8 *lut;
5919 	int ret;
5920 
5921 	if (!vsi->rss_size)
5922 		return -EINVAL;
5923 
5924 	if (rss_size > vsi->rss_size)
5925 		return -EINVAL;
5926 
5927 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
5928 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5929 	if (!lut)
5930 		return -ENOMEM;
5931 
5932 	/* Ignoring user configured lut if there is one */
5933 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5934 
5935 	/* Use user configured hash key if there is one, otherwise
5936 	 * use default.
5937 	 */
5938 	if (vsi->rss_hkey_user)
5939 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5940 	else
5941 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5942 
5943 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5944 	if (ret) {
5945 		dev_info(&pf->pdev->dev,
5946 			 "Cannot set RSS lut, err %s aq_err %s\n",
5947 			 i40e_stat_str(hw, ret),
5948 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5949 		kfree(lut);
5950 		return ret;
5951 	}
5952 	kfree(lut);
5953 
5954 	/* Do the update w.r.t. storing rss_size */
5955 	if (!vsi->orig_rss_size)
5956 		vsi->orig_rss_size = vsi->rss_size;
5957 	vsi->current_rss_size = local_rss_size;
5958 
5959 	return ret;
5960 }
5961 
5962 /**
5963  * i40e_channel_setup_queue_map - Setup a channel queue map
5964  * @pf: ptr to PF device
5965  * @ctxt: VSI context structure
5966  * @ch: ptr to channel structure
5967  *
5968  * Setup queue map for a specific channel
5969  **/
5970 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5971 					 struct i40e_vsi_context *ctxt,
5972 					 struct i40e_channel *ch)
5973 {
5974 	u16 qcount, qmap, sections = 0;
5975 	u8 offset = 0;
5976 	int pow;
5977 
5978 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5979 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5980 
5981 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5982 	ch->num_queue_pairs = qcount;
5983 
5984 	/* find the next higher power-of-2 of num queue pairs */
5985 	pow = ilog2(qcount);
5986 	if (!is_power_of_2(qcount))
5987 		pow++;
5988 
5989 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5990 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
5991 
5992 	/* Setup queue TC[0].qmap for given VSI context */
5993 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
5994 
5995 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
5996 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
5997 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
5998 	ctxt->info.valid_sections |= cpu_to_le16(sections);
5999 }
6000 
6001 /**
6002  * i40e_add_channel - add a channel by adding VSI
6003  * @pf: ptr to PF device
6004  * @uplink_seid: underlying HW switching element (VEB) ID
6005  * @ch: ptr to channel structure
6006  *
6007  * Add a channel (VSI) using add_vsi and queue_map
6008  **/
6009 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6010 			    struct i40e_channel *ch)
6011 {
6012 	struct i40e_hw *hw = &pf->hw;
6013 	struct i40e_vsi_context ctxt;
6014 	u8 enabled_tc = 0x1; /* TC0 enabled */
6015 	int ret;
6016 
6017 	if (ch->type != I40E_VSI_VMDQ2) {
6018 		dev_info(&pf->pdev->dev,
6019 			 "add new vsi failed, ch->type %d\n", ch->type);
6020 		return -EINVAL;
6021 	}
6022 
6023 	memset(&ctxt, 0, sizeof(ctxt));
6024 	ctxt.pf_num = hw->pf_id;
6025 	ctxt.vf_num = 0;
6026 	ctxt.uplink_seid = uplink_seid;
6027 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6028 	if (ch->type == I40E_VSI_VMDQ2)
6029 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6030 
6031 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6032 		ctxt.info.valid_sections |=
6033 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6034 		ctxt.info.switch_id =
6035 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6036 	}
6037 
6038 	/* Set queue map for a given VSI context */
6039 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6040 
6041 	/* Now time to create VSI */
6042 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6043 	if (ret) {
6044 		dev_info(&pf->pdev->dev,
6045 			 "add new vsi failed, err %s aq_err %s\n",
6046 			 i40e_stat_str(&pf->hw, ret),
6047 			 i40e_aq_str(&pf->hw,
6048 				     pf->hw.aq.asq_last_status));
6049 		return -ENOENT;
6050 	}
6051 
6052 	/* Success, update channel, set enabled_tc only if the channel
6053 	 * is not a macvlan
6054 	 */
6055 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6056 	ch->seid = ctxt.seid;
6057 	ch->vsi_number = ctxt.vsi_number;
6058 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6059 
6060 	/* copy just the sections touched not the entire info
6061 	 * since not all sections are valid as returned by
6062 	 * update vsi params
6063 	 */
6064 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6065 	memcpy(&ch->info.queue_mapping,
6066 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6067 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6068 	       sizeof(ctxt.info.tc_mapping));
6069 
6070 	return 0;
6071 }
6072 
6073 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6074 				  u8 *bw_share)
6075 {
6076 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6077 	i40e_status ret;
6078 	int i;
6079 
6080 	memset(&bw_data, 0, sizeof(bw_data));
6081 	bw_data.tc_valid_bits = ch->enabled_tc;
6082 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6083 		bw_data.tc_bw_credits[i] = bw_share[i];
6084 
6085 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6086 				       &bw_data, NULL);
6087 	if (ret) {
6088 		dev_info(&vsi->back->pdev->dev,
6089 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6090 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6091 		return -EINVAL;
6092 	}
6093 
6094 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6095 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6096 
6097 	return 0;
6098 }
6099 
6100 /**
6101  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6102  * @pf: ptr to PF device
6103  * @vsi: the VSI being setup
6104  * @ch: ptr to channel structure
6105  *
6106  * Configure TX rings associated with channel (VSI) since queues are being
6107  * from parent VSI.
6108  **/
6109 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6110 				       struct i40e_vsi *vsi,
6111 				       struct i40e_channel *ch)
6112 {
6113 	i40e_status ret;
6114 	int i;
6115 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6116 
6117 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6118 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6119 		if (ch->enabled_tc & BIT(i))
6120 			bw_share[i] = 1;
6121 	}
6122 
6123 	/* configure BW for new VSI */
6124 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6125 	if (ret) {
6126 		dev_info(&vsi->back->pdev->dev,
6127 			 "Failed configuring TC map %d for channel (seid %u)\n",
6128 			 ch->enabled_tc, ch->seid);
6129 		return ret;
6130 	}
6131 
6132 	for (i = 0; i < ch->num_queue_pairs; i++) {
6133 		struct i40e_ring *tx_ring, *rx_ring;
6134 		u16 pf_q;
6135 
6136 		pf_q = ch->base_queue + i;
6137 
6138 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6139 		 * context
6140 		 */
6141 		tx_ring = vsi->tx_rings[pf_q];
6142 		tx_ring->ch = ch;
6143 
6144 		/* Get the RX ring ptr */
6145 		rx_ring = vsi->rx_rings[pf_q];
6146 		rx_ring->ch = ch;
6147 	}
6148 
6149 	return 0;
6150 }
6151 
6152 /**
6153  * i40e_setup_hw_channel - setup new channel
6154  * @pf: ptr to PF device
6155  * @vsi: the VSI being setup
6156  * @ch: ptr to channel structure
6157  * @uplink_seid: underlying HW switching element (VEB) ID
6158  * @type: type of channel to be created (VMDq2/VF)
6159  *
6160  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6161  * and configures TX rings accordingly
6162  **/
6163 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6164 					struct i40e_vsi *vsi,
6165 					struct i40e_channel *ch,
6166 					u16 uplink_seid, u8 type)
6167 {
6168 	int ret;
6169 
6170 	ch->initialized = false;
6171 	ch->base_queue = vsi->next_base_queue;
6172 	ch->type = type;
6173 
6174 	/* Proceed with creation of channel (VMDq2) VSI */
6175 	ret = i40e_add_channel(pf, uplink_seid, ch);
6176 	if (ret) {
6177 		dev_info(&pf->pdev->dev,
6178 			 "failed to add_channel using uplink_seid %u\n",
6179 			 uplink_seid);
6180 		return ret;
6181 	}
6182 
6183 	/* Mark the successful creation of channel */
6184 	ch->initialized = true;
6185 
6186 	/* Reconfigure TX queues using QTX_CTL register */
6187 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6188 	if (ret) {
6189 		dev_info(&pf->pdev->dev,
6190 			 "failed to configure TX rings for channel %u\n",
6191 			 ch->seid);
6192 		return ret;
6193 	}
6194 
6195 	/* update 'next_base_queue' */
6196 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6197 	dev_dbg(&pf->pdev->dev,
6198 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6199 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6200 		ch->num_queue_pairs,
6201 		vsi->next_base_queue);
6202 	return ret;
6203 }
6204 
6205 /**
6206  * i40e_setup_channel - setup new channel using uplink element
6207  * @pf: ptr to PF device
6208  * @vsi: pointer to the VSI to set up the channel within
6209  * @ch: ptr to channel structure
6210  *
6211  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6212  * and uplink switching element (uplink_seid)
6213  **/
6214 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6215 			       struct i40e_channel *ch)
6216 {
6217 	u8 vsi_type;
6218 	u16 seid;
6219 	int ret;
6220 
6221 	if (vsi->type == I40E_VSI_MAIN) {
6222 		vsi_type = I40E_VSI_VMDQ2;
6223 	} else {
6224 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6225 			vsi->type);
6226 		return false;
6227 	}
6228 
6229 	/* underlying switching element */
6230 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6231 
6232 	/* create channel (VSI), configure TX rings */
6233 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6234 	if (ret) {
6235 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6236 		return false;
6237 	}
6238 
6239 	return ch->initialized ? true : false;
6240 }
6241 
6242 /**
6243  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6244  * @vsi: ptr to VSI which has PF backing
6245  *
6246  * Sets up switch mode correctly if it needs to be changed and perform
6247  * what are allowed modes.
6248  **/
6249 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6250 {
6251 	u8 mode;
6252 	struct i40e_pf *pf = vsi->back;
6253 	struct i40e_hw *hw = &pf->hw;
6254 	int ret;
6255 
6256 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6257 	if (ret)
6258 		return -EINVAL;
6259 
6260 	if (hw->dev_caps.switch_mode) {
6261 		/* if switch mode is set, support mode2 (non-tunneled for
6262 		 * cloud filter) for now
6263 		 */
6264 		u32 switch_mode = hw->dev_caps.switch_mode &
6265 				  I40E_SWITCH_MODE_MASK;
6266 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6267 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6268 				return 0;
6269 			dev_err(&pf->pdev->dev,
6270 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6271 				hw->dev_caps.switch_mode);
6272 			return -EINVAL;
6273 		}
6274 	}
6275 
6276 	/* Set Bit 7 to be valid */
6277 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6278 
6279 	/* Set L4type for TCP support */
6280 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6281 
6282 	/* Set cloud filter mode */
6283 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6284 
6285 	/* Prep mode field for set_switch_config */
6286 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6287 					pf->last_sw_conf_valid_flags,
6288 					mode, NULL);
6289 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6290 		dev_err(&pf->pdev->dev,
6291 			"couldn't set switch config bits, err %s aq_err %s\n",
6292 			i40e_stat_str(hw, ret),
6293 			i40e_aq_str(hw,
6294 				    hw->aq.asq_last_status));
6295 
6296 	return ret;
6297 }
6298 
6299 /**
6300  * i40e_create_queue_channel - function to create channel
6301  * @vsi: VSI to be configured
6302  * @ch: ptr to channel (it contains channel specific params)
6303  *
6304  * This function creates channel (VSI) using num_queues specified by user,
6305  * reconfigs RSS if needed.
6306  **/
6307 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6308 			      struct i40e_channel *ch)
6309 {
6310 	struct i40e_pf *pf = vsi->back;
6311 	bool reconfig_rss;
6312 	int err;
6313 
6314 	if (!ch)
6315 		return -EINVAL;
6316 
6317 	if (!ch->num_queue_pairs) {
6318 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6319 			ch->num_queue_pairs);
6320 		return -EINVAL;
6321 	}
6322 
6323 	/* validate user requested num_queues for channel */
6324 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6325 				       &reconfig_rss);
6326 	if (err) {
6327 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6328 			 ch->num_queue_pairs);
6329 		return -EINVAL;
6330 	}
6331 
6332 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6333 	 * VSI to be added switch to VEB mode.
6334 	 */
6335 
6336 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6337 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6338 
6339 		if (vsi->type == I40E_VSI_MAIN) {
6340 			if (pf->flags & I40E_FLAG_TC_MQPRIO)
6341 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6342 			else
6343 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6344 		}
6345 		/* now onwards for main VSI, number of queues will be value
6346 		 * of TC0's queue count
6347 		 */
6348 	}
6349 
6350 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6351 	 * it should be more than num_queues
6352 	 */
6353 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6354 		dev_dbg(&pf->pdev->dev,
6355 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6356 			vsi->cnt_q_avail, ch->num_queue_pairs);
6357 		return -EINVAL;
6358 	}
6359 
6360 	/* reconfig_rss only if vsi type is MAIN_VSI */
6361 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6362 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6363 		if (err) {
6364 			dev_info(&pf->pdev->dev,
6365 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6366 				 ch->num_queue_pairs);
6367 			return -EINVAL;
6368 		}
6369 	}
6370 
6371 	if (!i40e_setup_channel(pf, vsi, ch)) {
6372 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6373 		return -EINVAL;
6374 	}
6375 
6376 	dev_info(&pf->pdev->dev,
6377 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6378 		 ch->seid, ch->num_queue_pairs);
6379 
6380 	/* configure VSI for BW limit */
6381 	if (ch->max_tx_rate) {
6382 		u64 credits = ch->max_tx_rate;
6383 
6384 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6385 			return -EINVAL;
6386 
6387 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6388 		dev_dbg(&pf->pdev->dev,
6389 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6390 			ch->max_tx_rate,
6391 			credits,
6392 			ch->seid);
6393 	}
6394 
6395 	/* in case of VF, this will be main SRIOV VSI */
6396 	ch->parent_vsi = vsi;
6397 
6398 	/* and update main_vsi's count for queue_available to use */
6399 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6400 
6401 	return 0;
6402 }
6403 
6404 /**
6405  * i40e_configure_queue_channels - Add queue channel for the given TCs
6406  * @vsi: VSI to be configured
6407  *
6408  * Configures queue channel mapping to the given TCs
6409  **/
6410 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6411 {
6412 	struct i40e_channel *ch;
6413 	u64 max_rate = 0;
6414 	int ret = 0, i;
6415 
6416 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6417 	vsi->tc_seid_map[0] = vsi->seid;
6418 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6419 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6420 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6421 			if (!ch) {
6422 				ret = -ENOMEM;
6423 				goto err_free;
6424 			}
6425 
6426 			INIT_LIST_HEAD(&ch->list);
6427 			ch->num_queue_pairs =
6428 				vsi->tc_config.tc_info[i].qcount;
6429 			ch->base_queue =
6430 				vsi->tc_config.tc_info[i].qoffset;
6431 
6432 			/* Bandwidth limit through tc interface is in bytes/s,
6433 			 * change to Mbit/s
6434 			 */
6435 			max_rate = vsi->mqprio_qopt.max_rate[i];
6436 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6437 			ch->max_tx_rate = max_rate;
6438 
6439 			list_add_tail(&ch->list, &vsi->ch_list);
6440 
6441 			ret = i40e_create_queue_channel(vsi, ch);
6442 			if (ret) {
6443 				dev_err(&vsi->back->pdev->dev,
6444 					"Failed creating queue channel with TC%d: queues %d\n",
6445 					i, ch->num_queue_pairs);
6446 				goto err_free;
6447 			}
6448 			vsi->tc_seid_map[i] = ch->seid;
6449 		}
6450 	}
6451 	return ret;
6452 
6453 err_free:
6454 	i40e_remove_queue_channels(vsi);
6455 	return ret;
6456 }
6457 
6458 /**
6459  * i40e_veb_config_tc - Configure TCs for given VEB
6460  * @veb: given VEB
6461  * @enabled_tc: TC bitmap
6462  *
6463  * Configures given TC bitmap for VEB (switching) element
6464  **/
6465 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6466 {
6467 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6468 	struct i40e_pf *pf = veb->pf;
6469 	int ret = 0;
6470 	int i;
6471 
6472 	/* No TCs or already enabled TCs just return */
6473 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6474 		return ret;
6475 
6476 	bw_data.tc_valid_bits = enabled_tc;
6477 	/* bw_data.absolute_credits is not set (relative) */
6478 
6479 	/* Enable ETS TCs with equal BW Share for now */
6480 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6481 		if (enabled_tc & BIT(i))
6482 			bw_data.tc_bw_share_credits[i] = 1;
6483 	}
6484 
6485 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6486 						   &bw_data, NULL);
6487 	if (ret) {
6488 		dev_info(&pf->pdev->dev,
6489 			 "VEB bw config failed, err %s aq_err %s\n",
6490 			 i40e_stat_str(&pf->hw, ret),
6491 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6492 		goto out;
6493 	}
6494 
6495 	/* Update the BW information */
6496 	ret = i40e_veb_get_bw_info(veb);
6497 	if (ret) {
6498 		dev_info(&pf->pdev->dev,
6499 			 "Failed getting veb bw config, err %s aq_err %s\n",
6500 			 i40e_stat_str(&pf->hw, ret),
6501 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6502 	}
6503 
6504 out:
6505 	return ret;
6506 }
6507 
6508 #ifdef CONFIG_I40E_DCB
6509 /**
6510  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6511  * @pf: PF struct
6512  *
6513  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6514  * the caller would've quiesce all the VSIs before calling
6515  * this function
6516  **/
6517 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6518 {
6519 	u8 tc_map = 0;
6520 	int ret;
6521 	u8 v;
6522 
6523 	/* Enable the TCs available on PF to all VEBs */
6524 	tc_map = i40e_pf_get_tc_map(pf);
6525 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6526 		return;
6527 
6528 	for (v = 0; v < I40E_MAX_VEB; v++) {
6529 		if (!pf->veb[v])
6530 			continue;
6531 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6532 		if (ret) {
6533 			dev_info(&pf->pdev->dev,
6534 				 "Failed configuring TC for VEB seid=%d\n",
6535 				 pf->veb[v]->seid);
6536 			/* Will try to configure as many components */
6537 		}
6538 	}
6539 
6540 	/* Update each VSI */
6541 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6542 		if (!pf->vsi[v])
6543 			continue;
6544 
6545 		/* - Enable all TCs for the LAN VSI
6546 		 * - For all others keep them at TC0 for now
6547 		 */
6548 		if (v == pf->lan_vsi)
6549 			tc_map = i40e_pf_get_tc_map(pf);
6550 		else
6551 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6552 
6553 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6554 		if (ret) {
6555 			dev_info(&pf->pdev->dev,
6556 				 "Failed configuring TC for VSI seid=%d\n",
6557 				 pf->vsi[v]->seid);
6558 			/* Will try to configure as many components */
6559 		} else {
6560 			/* Re-configure VSI vectors based on updated TC map */
6561 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6562 			if (pf->vsi[v]->netdev)
6563 				i40e_dcbnl_set_all(pf->vsi[v]);
6564 		}
6565 	}
6566 }
6567 
6568 /**
6569  * i40e_resume_port_tx - Resume port Tx
6570  * @pf: PF struct
6571  *
6572  * Resume a port's Tx and issue a PF reset in case of failure to
6573  * resume.
6574  **/
6575 static int i40e_resume_port_tx(struct i40e_pf *pf)
6576 {
6577 	struct i40e_hw *hw = &pf->hw;
6578 	int ret;
6579 
6580 	ret = i40e_aq_resume_port_tx(hw, NULL);
6581 	if (ret) {
6582 		dev_info(&pf->pdev->dev,
6583 			 "Resume Port Tx failed, err %s aq_err %s\n",
6584 			  i40e_stat_str(&pf->hw, ret),
6585 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6586 		/* Schedule PF reset to recover */
6587 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6588 		i40e_service_event_schedule(pf);
6589 	}
6590 
6591 	return ret;
6592 }
6593 
6594 /**
6595  * i40e_suspend_port_tx - Suspend port Tx
6596  * @pf: PF struct
6597  *
6598  * Suspend a port's Tx and issue a PF reset in case of failure.
6599  **/
6600 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6601 {
6602 	struct i40e_hw *hw = &pf->hw;
6603 	int ret;
6604 
6605 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6606 	if (ret) {
6607 		dev_info(&pf->pdev->dev,
6608 			 "Suspend Port Tx failed, err %s aq_err %s\n",
6609 			 i40e_stat_str(&pf->hw, ret),
6610 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6611 		/* Schedule PF reset to recover */
6612 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6613 		i40e_service_event_schedule(pf);
6614 	}
6615 
6616 	return ret;
6617 }
6618 
6619 /**
6620  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6621  * @pf: PF being configured
6622  * @new_cfg: New DCBX configuration
6623  *
6624  * Program DCB settings into HW and reconfigure VEB/VSIs on
6625  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6626  **/
6627 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6628 				  struct i40e_dcbx_config *new_cfg)
6629 {
6630 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6631 	int ret;
6632 
6633 	/* Check if need reconfiguration */
6634 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6635 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6636 		return 0;
6637 	}
6638 
6639 	/* Config change disable all VSIs */
6640 	i40e_pf_quiesce_all_vsi(pf);
6641 
6642 	/* Copy the new config to the current config */
6643 	*old_cfg = *new_cfg;
6644 	old_cfg->etsrec = old_cfg->etscfg;
6645 	ret = i40e_set_dcb_config(&pf->hw);
6646 	if (ret) {
6647 		dev_info(&pf->pdev->dev,
6648 			 "Set DCB Config failed, err %s aq_err %s\n",
6649 			 i40e_stat_str(&pf->hw, ret),
6650 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6651 		goto out;
6652 	}
6653 
6654 	/* Changes in configuration update VEB/VSI */
6655 	i40e_dcb_reconfigure(pf);
6656 out:
6657 	/* In case of reset do not try to resume anything */
6658 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6659 		/* Re-start the VSIs if disabled */
6660 		ret = i40e_resume_port_tx(pf);
6661 		/* In case of error no point in resuming VSIs */
6662 		if (ret)
6663 			goto err;
6664 		i40e_pf_unquiesce_all_vsi(pf);
6665 	}
6666 err:
6667 	return ret;
6668 }
6669 
6670 /**
6671  * i40e_hw_dcb_config - Program new DCBX settings into HW
6672  * @pf: PF being configured
6673  * @new_cfg: New DCBX configuration
6674  *
6675  * Program DCB settings into HW and reconfigure VEB/VSIs on
6676  * given PF
6677  **/
6678 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6679 {
6680 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6681 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6682 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6683 	struct i40e_dcbx_config *old_cfg;
6684 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6685 	struct i40e_rx_pb_config pb_cfg;
6686 	struct i40e_hw *hw = &pf->hw;
6687 	u8 num_ports = hw->num_ports;
6688 	bool need_reconfig;
6689 	int ret = -EINVAL;
6690 	u8 lltc_map = 0;
6691 	u8 tc_map = 0;
6692 	u8 new_numtc;
6693 	u8 i;
6694 
6695 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6696 	/* Un-pack information to Program ETS HW via shared API
6697 	 * numtc, tcmap
6698 	 * LLTC map
6699 	 * ETS/NON-ETS arbiter mode
6700 	 * max exponent (credit refills)
6701 	 * Total number of ports
6702 	 * PFC priority bit-map
6703 	 * Priority Table
6704 	 * BW % per TC
6705 	 * Arbiter mode between UPs sharing same TC
6706 	 * TSA table (ETS or non-ETS)
6707 	 * EEE enabled or not
6708 	 * MFS TC table
6709 	 */
6710 
6711 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6712 
6713 	memset(&ets_data, 0, sizeof(ets_data));
6714 	for (i = 0; i < new_numtc; i++) {
6715 		tc_map |= BIT(i);
6716 		switch (new_cfg->etscfg.tsatable[i]) {
6717 		case I40E_IEEE_TSA_ETS:
6718 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6719 			ets_data.tc_bw_share_credits[i] =
6720 					new_cfg->etscfg.tcbwtable[i];
6721 			break;
6722 		case I40E_IEEE_TSA_STRICT:
6723 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6724 			lltc_map |= BIT(i);
6725 			ets_data.tc_bw_share_credits[i] =
6726 					I40E_DCB_STRICT_PRIO_CREDITS;
6727 			break;
6728 		default:
6729 			/* Invalid TSA type */
6730 			need_reconfig = false;
6731 			goto out;
6732 		}
6733 	}
6734 
6735 	old_cfg = &hw->local_dcbx_config;
6736 	/* Check if need reconfiguration */
6737 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6738 
6739 	/* If needed, enable/disable frame tagging, disable all VSIs
6740 	 * and suspend port tx
6741 	 */
6742 	if (need_reconfig) {
6743 		/* Enable DCB tagging only when more than one TC */
6744 		if (new_numtc > 1)
6745 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6746 		else
6747 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6748 
6749 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6750 		/* Reconfiguration needed quiesce all VSIs */
6751 		i40e_pf_quiesce_all_vsi(pf);
6752 		ret = i40e_suspend_port_tx(pf);
6753 		if (ret)
6754 			goto err;
6755 	}
6756 
6757 	/* Configure Port ETS Tx Scheduler */
6758 	ets_data.tc_valid_bits = tc_map;
6759 	ets_data.tc_strict_priority_flags = lltc_map;
6760 	ret = i40e_aq_config_switch_comp_ets
6761 		(hw, pf->mac_seid, &ets_data,
6762 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
6763 	if (ret) {
6764 		dev_info(&pf->pdev->dev,
6765 			 "Modify Port ETS failed, err %s aq_err %s\n",
6766 			 i40e_stat_str(&pf->hw, ret),
6767 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6768 		goto out;
6769 	}
6770 
6771 	/* Configure Rx ETS HW */
6772 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
6773 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
6774 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
6775 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
6776 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
6777 				   lltc_map);
6778 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
6779 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
6780 				     prio_type);
6781 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
6782 			       new_cfg->etscfg.prioritytable);
6783 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
6784 
6785 	/* Configure Rx Packet Buffers in HW */
6786 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6787 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
6788 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
6789 	}
6790 
6791 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
6792 					 false, new_cfg->pfc.pfcenable,
6793 					 mfs_tc, &pb_cfg);
6794 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
6795 
6796 	/* Update the local Rx Packet buffer config */
6797 	pf->pb_cfg = pb_cfg;
6798 
6799 	/* Inform the FW about changes to DCB configuration */
6800 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
6801 	if (ret) {
6802 		dev_info(&pf->pdev->dev,
6803 			 "DCB Updated failed, err %s aq_err %s\n",
6804 			 i40e_stat_str(&pf->hw, ret),
6805 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6806 		goto out;
6807 	}
6808 
6809 	/* Update the port DCBx configuration */
6810 	*old_cfg = *new_cfg;
6811 
6812 	/* Changes in configuration update VEB/VSI */
6813 	i40e_dcb_reconfigure(pf);
6814 out:
6815 	/* Re-start the VSIs if disabled */
6816 	if (need_reconfig) {
6817 		ret = i40e_resume_port_tx(pf);
6818 
6819 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
6820 		/* In case of error no point in resuming VSIs */
6821 		if (ret)
6822 			goto err;
6823 
6824 		/* Wait for the PF's queues to be disabled */
6825 		ret = i40e_pf_wait_queues_disabled(pf);
6826 		if (ret) {
6827 			/* Schedule PF reset to recover */
6828 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6829 			i40e_service_event_schedule(pf);
6830 			goto err;
6831 		} else {
6832 			i40e_pf_unquiesce_all_vsi(pf);
6833 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
6834 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
6835 		}
6836 		/* registers are set, lets apply */
6837 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
6838 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
6839 	}
6840 
6841 err:
6842 	return ret;
6843 }
6844 
6845 /**
6846  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
6847  * @pf: PF being queried
6848  *
6849  * Set default DCB configuration in case DCB is to be done in SW.
6850  **/
6851 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
6852 {
6853 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
6854 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6855 	struct i40e_hw *hw = &pf->hw;
6856 	int err;
6857 
6858 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
6859 		/* Update the local cached instance with TC0 ETS */
6860 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
6861 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
6862 		pf->tmp_cfg.etscfg.maxtcs = 0;
6863 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
6864 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
6865 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
6866 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
6867 		/* FW needs one App to configure HW */
6868 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
6869 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
6870 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
6871 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
6872 
6873 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
6874 	}
6875 
6876 	memset(&ets_data, 0, sizeof(ets_data));
6877 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
6878 	ets_data.tc_strict_priority_flags = 0; /* ETS */
6879 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
6880 
6881 	/* Enable ETS on the Physical port */
6882 	err = i40e_aq_config_switch_comp_ets
6883 		(hw, pf->mac_seid, &ets_data,
6884 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
6885 	if (err) {
6886 		dev_info(&pf->pdev->dev,
6887 			 "Enable Port ETS failed, err %s aq_err %s\n",
6888 			 i40e_stat_str(&pf->hw, err),
6889 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6890 		err = -ENOENT;
6891 		goto out;
6892 	}
6893 
6894 	/* Update the local cached instance with TC0 ETS */
6895 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
6896 	dcb_cfg->etscfg.cbs = 0;
6897 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
6898 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
6899 
6900 out:
6901 	return err;
6902 }
6903 
6904 /**
6905  * i40e_init_pf_dcb - Initialize DCB configuration
6906  * @pf: PF being configured
6907  *
6908  * Query the current DCB configuration and cache it
6909  * in the hardware structure
6910  **/
6911 static int i40e_init_pf_dcb(struct i40e_pf *pf)
6912 {
6913 	struct i40e_hw *hw = &pf->hw;
6914 	int err;
6915 
6916 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
6917 	 * Also do not enable DCBx if FW LLDP agent is disabled
6918 	 */
6919 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
6920 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
6921 		err = I40E_NOT_SUPPORTED;
6922 		goto out;
6923 	}
6924 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
6925 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
6926 		err = i40e_dcb_sw_default_config(pf);
6927 		if (err) {
6928 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
6929 			goto out;
6930 		}
6931 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
6932 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
6933 			       DCB_CAP_DCBX_VER_IEEE;
6934 		/* at init capable but disabled */
6935 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
6936 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6937 		goto out;
6938 	}
6939 	err = i40e_init_dcb(hw, true);
6940 	if (!err) {
6941 		/* Device/Function is not DCBX capable */
6942 		if ((!hw->func_caps.dcb) ||
6943 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6944 			dev_info(&pf->pdev->dev,
6945 				 "DCBX offload is not supported or is disabled for this PF.\n");
6946 		} else {
6947 			/* When status is not DISABLED then DCBX in FW */
6948 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6949 				       DCB_CAP_DCBX_VER_IEEE;
6950 
6951 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
6952 			/* Enable DCB tagging only when more than one TC
6953 			 * or explicitly disable if only one TC
6954 			 */
6955 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6956 				pf->flags |= I40E_FLAG_DCB_ENABLED;
6957 			else
6958 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6959 			dev_dbg(&pf->pdev->dev,
6960 				"DCBX offload is supported for this PF.\n");
6961 		}
6962 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
6963 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
6964 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
6965 	} else {
6966 		dev_info(&pf->pdev->dev,
6967 			 "Query for DCB configuration failed, err %s aq_err %s\n",
6968 			 i40e_stat_str(&pf->hw, err),
6969 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6970 	}
6971 
6972 out:
6973 	return err;
6974 }
6975 #endif /* CONFIG_I40E_DCB */
6976 
6977 /**
6978  * i40e_print_link_message - print link up or down
6979  * @vsi: the VSI for which link needs a message
6980  * @isup: true of link is up, false otherwise
6981  */
6982 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
6983 {
6984 	enum i40e_aq_link_speed new_speed;
6985 	struct i40e_pf *pf = vsi->back;
6986 	char *speed = "Unknown";
6987 	char *fc = "Unknown";
6988 	char *fec = "";
6989 	char *req_fec = "";
6990 	char *an = "";
6991 
6992 	if (isup)
6993 		new_speed = pf->hw.phy.link_info.link_speed;
6994 	else
6995 		new_speed = I40E_LINK_SPEED_UNKNOWN;
6996 
6997 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
6998 		return;
6999 	vsi->current_isup = isup;
7000 	vsi->current_speed = new_speed;
7001 	if (!isup) {
7002 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7003 		return;
7004 	}
7005 
7006 	/* Warn user if link speed on NPAR enabled partition is not at
7007 	 * least 10GB
7008 	 */
7009 	if (pf->hw.func_caps.npar_enable &&
7010 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7011 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7012 		netdev_warn(vsi->netdev,
7013 			    "The partition detected link speed that is less than 10Gbps\n");
7014 
7015 	switch (pf->hw.phy.link_info.link_speed) {
7016 	case I40E_LINK_SPEED_40GB:
7017 		speed = "40 G";
7018 		break;
7019 	case I40E_LINK_SPEED_20GB:
7020 		speed = "20 G";
7021 		break;
7022 	case I40E_LINK_SPEED_25GB:
7023 		speed = "25 G";
7024 		break;
7025 	case I40E_LINK_SPEED_10GB:
7026 		speed = "10 G";
7027 		break;
7028 	case I40E_LINK_SPEED_5GB:
7029 		speed = "5 G";
7030 		break;
7031 	case I40E_LINK_SPEED_2_5GB:
7032 		speed = "2.5 G";
7033 		break;
7034 	case I40E_LINK_SPEED_1GB:
7035 		speed = "1000 M";
7036 		break;
7037 	case I40E_LINK_SPEED_100MB:
7038 		speed = "100 M";
7039 		break;
7040 	default:
7041 		break;
7042 	}
7043 
7044 	switch (pf->hw.fc.current_mode) {
7045 	case I40E_FC_FULL:
7046 		fc = "RX/TX";
7047 		break;
7048 	case I40E_FC_TX_PAUSE:
7049 		fc = "TX";
7050 		break;
7051 	case I40E_FC_RX_PAUSE:
7052 		fc = "RX";
7053 		break;
7054 	default:
7055 		fc = "None";
7056 		break;
7057 	}
7058 
7059 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7060 		req_fec = "None";
7061 		fec = "None";
7062 		an = "False";
7063 
7064 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7065 			an = "True";
7066 
7067 		if (pf->hw.phy.link_info.fec_info &
7068 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7069 			fec = "CL74 FC-FEC/BASE-R";
7070 		else if (pf->hw.phy.link_info.fec_info &
7071 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7072 			fec = "CL108 RS-FEC";
7073 
7074 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7075 		 * both RS and FC are requested
7076 		 */
7077 		if (vsi->back->hw.phy.link_info.req_fec_info &
7078 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7079 			if (vsi->back->hw.phy.link_info.req_fec_info &
7080 			    I40E_AQ_REQUEST_FEC_RS)
7081 				req_fec = "CL108 RS-FEC";
7082 			else
7083 				req_fec = "CL74 FC-FEC/BASE-R";
7084 		}
7085 		netdev_info(vsi->netdev,
7086 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7087 			    speed, req_fec, fec, an, fc);
7088 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7089 		req_fec = "None";
7090 		fec = "None";
7091 		an = "False";
7092 
7093 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7094 			an = "True";
7095 
7096 		if (pf->hw.phy.link_info.fec_info &
7097 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7098 			fec = "CL74 FC-FEC/BASE-R";
7099 
7100 		if (pf->hw.phy.link_info.req_fec_info &
7101 		    I40E_AQ_REQUEST_FEC_KR)
7102 			req_fec = "CL74 FC-FEC/BASE-R";
7103 
7104 		netdev_info(vsi->netdev,
7105 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7106 			    speed, req_fec, fec, an, fc);
7107 	} else {
7108 		netdev_info(vsi->netdev,
7109 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7110 			    speed, fc);
7111 	}
7112 
7113 }
7114 
7115 /**
7116  * i40e_up_complete - Finish the last steps of bringing up a connection
7117  * @vsi: the VSI being configured
7118  **/
7119 static int i40e_up_complete(struct i40e_vsi *vsi)
7120 {
7121 	struct i40e_pf *pf = vsi->back;
7122 	int err;
7123 
7124 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7125 		i40e_vsi_configure_msix(vsi);
7126 	else
7127 		i40e_configure_msi_and_legacy(vsi);
7128 
7129 	/* start rings */
7130 	err = i40e_vsi_start_rings(vsi);
7131 	if (err)
7132 		return err;
7133 
7134 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7135 	i40e_napi_enable_all(vsi);
7136 	i40e_vsi_enable_irq(vsi);
7137 
7138 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7139 	    (vsi->netdev)) {
7140 		i40e_print_link_message(vsi, true);
7141 		netif_tx_start_all_queues(vsi->netdev);
7142 		netif_carrier_on(vsi->netdev);
7143 	}
7144 
7145 	/* replay FDIR SB filters */
7146 	if (vsi->type == I40E_VSI_FDIR) {
7147 		/* reset fd counters */
7148 		pf->fd_add_err = 0;
7149 		pf->fd_atr_cnt = 0;
7150 		i40e_fdir_filter_restore(vsi);
7151 	}
7152 
7153 	/* On the next run of the service_task, notify any clients of the new
7154 	 * opened netdev
7155 	 */
7156 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7157 	i40e_service_event_schedule(pf);
7158 
7159 	return 0;
7160 }
7161 
7162 /**
7163  * i40e_vsi_reinit_locked - Reset the VSI
7164  * @vsi: the VSI being configured
7165  *
7166  * Rebuild the ring structs after some configuration
7167  * has changed, e.g. MTU size.
7168  **/
7169 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7170 {
7171 	struct i40e_pf *pf = vsi->back;
7172 
7173 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7174 		usleep_range(1000, 2000);
7175 	i40e_down(vsi);
7176 
7177 	i40e_up(vsi);
7178 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7179 }
7180 
7181 /**
7182  * i40e_force_link_state - Force the link status
7183  * @pf: board private structure
7184  * @is_up: whether the link state should be forced up or down
7185  **/
7186 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7187 {
7188 	struct i40e_aq_get_phy_abilities_resp abilities;
7189 	struct i40e_aq_set_phy_config config = {0};
7190 	bool non_zero_phy_type = is_up;
7191 	struct i40e_hw *hw = &pf->hw;
7192 	i40e_status err;
7193 	u64 mask;
7194 	u8 speed;
7195 
7196 	/* Card might've been put in an unstable state by other drivers
7197 	 * and applications, which causes incorrect speed values being
7198 	 * set on startup. In order to clear speed registers, we call
7199 	 * get_phy_capabilities twice, once to get initial state of
7200 	 * available speeds, and once to get current PHY config.
7201 	 */
7202 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7203 					   NULL);
7204 	if (err) {
7205 		dev_err(&pf->pdev->dev,
7206 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7207 			i40e_stat_str(hw, err),
7208 			i40e_aq_str(hw, hw->aq.asq_last_status));
7209 		return err;
7210 	}
7211 	speed = abilities.link_speed;
7212 
7213 	/* Get the current phy config */
7214 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7215 					   NULL);
7216 	if (err) {
7217 		dev_err(&pf->pdev->dev,
7218 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7219 			i40e_stat_str(hw, err),
7220 			i40e_aq_str(hw, hw->aq.asq_last_status));
7221 		return err;
7222 	}
7223 
7224 	/* If link needs to go up, but was not forced to go down,
7225 	 * and its speed values are OK, no need for a flap
7226 	 * if non_zero_phy_type was set, still need to force up
7227 	 */
7228 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7229 		non_zero_phy_type = true;
7230 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7231 		return I40E_SUCCESS;
7232 
7233 	/* To force link we need to set bits for all supported PHY types,
7234 	 * but there are now more than 32, so we need to split the bitmap
7235 	 * across two fields.
7236 	 */
7237 	mask = I40E_PHY_TYPES_BITMASK;
7238 	config.phy_type =
7239 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7240 	config.phy_type_ext =
7241 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7242 	/* Copy the old settings, except of phy_type */
7243 	config.abilities = abilities.abilities;
7244 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7245 		if (is_up)
7246 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7247 		else
7248 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7249 	}
7250 	if (abilities.link_speed != 0)
7251 		config.link_speed = abilities.link_speed;
7252 	else
7253 		config.link_speed = speed;
7254 	config.eee_capability = abilities.eee_capability;
7255 	config.eeer = abilities.eeer_val;
7256 	config.low_power_ctrl = abilities.d3_lpan;
7257 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7258 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7259 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7260 
7261 	if (err) {
7262 		dev_err(&pf->pdev->dev,
7263 			"set phy config ret =  %s last_status =  %s\n",
7264 			i40e_stat_str(&pf->hw, err),
7265 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7266 		return err;
7267 	}
7268 
7269 	/* Update the link info */
7270 	err = i40e_update_link_info(hw);
7271 	if (err) {
7272 		/* Wait a little bit (on 40G cards it sometimes takes a really
7273 		 * long time for link to come back from the atomic reset)
7274 		 * and try once more
7275 		 */
7276 		msleep(1000);
7277 		i40e_update_link_info(hw);
7278 	}
7279 
7280 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7281 
7282 	return I40E_SUCCESS;
7283 }
7284 
7285 /**
7286  * i40e_up - Bring the connection back up after being down
7287  * @vsi: the VSI being configured
7288  **/
7289 int i40e_up(struct i40e_vsi *vsi)
7290 {
7291 	int err;
7292 
7293 	if (vsi->type == I40E_VSI_MAIN &&
7294 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7295 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7296 		i40e_force_link_state(vsi->back, true);
7297 
7298 	err = i40e_vsi_configure(vsi);
7299 	if (!err)
7300 		err = i40e_up_complete(vsi);
7301 
7302 	return err;
7303 }
7304 
7305 /**
7306  * i40e_down - Shutdown the connection processing
7307  * @vsi: the VSI being stopped
7308  **/
7309 void i40e_down(struct i40e_vsi *vsi)
7310 {
7311 	int i;
7312 
7313 	/* It is assumed that the caller of this function
7314 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7315 	 */
7316 	if (vsi->netdev) {
7317 		netif_carrier_off(vsi->netdev);
7318 		netif_tx_disable(vsi->netdev);
7319 	}
7320 	i40e_vsi_disable_irq(vsi);
7321 	i40e_vsi_stop_rings(vsi);
7322 	if (vsi->type == I40E_VSI_MAIN &&
7323 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7324 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7325 		i40e_force_link_state(vsi->back, false);
7326 	i40e_napi_disable_all(vsi);
7327 
7328 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7329 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7330 		if (i40e_enabled_xdp_vsi(vsi)) {
7331 			/* Make sure that in-progress ndo_xdp_xmit and
7332 			 * ndo_xsk_wakeup calls are completed.
7333 			 */
7334 			synchronize_rcu();
7335 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7336 		}
7337 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7338 	}
7339 
7340 }
7341 
7342 /**
7343  * i40e_validate_mqprio_qopt- validate queue mapping info
7344  * @vsi: the VSI being configured
7345  * @mqprio_qopt: queue parametrs
7346  **/
7347 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7348 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7349 {
7350 	u64 sum_max_rate = 0;
7351 	u64 max_rate = 0;
7352 	int i;
7353 
7354 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7355 	    mqprio_qopt->qopt.num_tc < 1 ||
7356 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7357 		return -EINVAL;
7358 	for (i = 0; ; i++) {
7359 		if (!mqprio_qopt->qopt.count[i])
7360 			return -EINVAL;
7361 		if (mqprio_qopt->min_rate[i]) {
7362 			dev_err(&vsi->back->pdev->dev,
7363 				"Invalid min tx rate (greater than 0) specified\n");
7364 			return -EINVAL;
7365 		}
7366 		max_rate = mqprio_qopt->max_rate[i];
7367 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7368 		sum_max_rate += max_rate;
7369 
7370 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7371 			break;
7372 		if (mqprio_qopt->qopt.offset[i + 1] !=
7373 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7374 			return -EINVAL;
7375 	}
7376 	if (vsi->num_queue_pairs <
7377 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7378 		dev_err(&vsi->back->pdev->dev,
7379 			"Failed to create traffic channel, insufficient number of queues.\n");
7380 		return -EINVAL;
7381 	}
7382 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7383 		dev_err(&vsi->back->pdev->dev,
7384 			"Invalid max tx rate specified\n");
7385 		return -EINVAL;
7386 	}
7387 	return 0;
7388 }
7389 
7390 /**
7391  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7392  * @vsi: the VSI being configured
7393  **/
7394 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7395 {
7396 	u16 qcount;
7397 	int i;
7398 
7399 	/* Only TC0 is enabled */
7400 	vsi->tc_config.numtc = 1;
7401 	vsi->tc_config.enabled_tc = 1;
7402 	qcount = min_t(int, vsi->alloc_queue_pairs,
7403 		       i40e_pf_get_max_q_per_tc(vsi->back));
7404 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7405 		/* For the TC that is not enabled set the offset to default
7406 		 * queue and allocate one queue for the given TC.
7407 		 */
7408 		vsi->tc_config.tc_info[i].qoffset = 0;
7409 		if (i == 0)
7410 			vsi->tc_config.tc_info[i].qcount = qcount;
7411 		else
7412 			vsi->tc_config.tc_info[i].qcount = 1;
7413 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7414 	}
7415 }
7416 
7417 /**
7418  * i40e_del_macvlan_filter
7419  * @hw: pointer to the HW structure
7420  * @seid: seid of the channel VSI
7421  * @macaddr: the mac address to apply as a filter
7422  * @aq_err: store the admin Q error
7423  *
7424  * This function deletes a mac filter on the channel VSI which serves as the
7425  * macvlan. Returns 0 on success.
7426  **/
7427 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7428 					   const u8 *macaddr, int *aq_err)
7429 {
7430 	struct i40e_aqc_remove_macvlan_element_data element;
7431 	i40e_status status;
7432 
7433 	memset(&element, 0, sizeof(element));
7434 	ether_addr_copy(element.mac_addr, macaddr);
7435 	element.vlan_tag = 0;
7436 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7437 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7438 	*aq_err = hw->aq.asq_last_status;
7439 
7440 	return status;
7441 }
7442 
7443 /**
7444  * i40e_add_macvlan_filter
7445  * @hw: pointer to the HW structure
7446  * @seid: seid of the channel VSI
7447  * @macaddr: the mac address to apply as a filter
7448  * @aq_err: store the admin Q error
7449  *
7450  * This function adds a mac filter on the channel VSI which serves as the
7451  * macvlan. Returns 0 on success.
7452  **/
7453 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7454 					   const u8 *macaddr, int *aq_err)
7455 {
7456 	struct i40e_aqc_add_macvlan_element_data element;
7457 	i40e_status status;
7458 	u16 cmd_flags = 0;
7459 
7460 	ether_addr_copy(element.mac_addr, macaddr);
7461 	element.vlan_tag = 0;
7462 	element.queue_number = 0;
7463 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7464 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7465 	element.flags = cpu_to_le16(cmd_flags);
7466 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7467 	*aq_err = hw->aq.asq_last_status;
7468 
7469 	return status;
7470 }
7471 
7472 /**
7473  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7474  * @vsi: the VSI we want to access
7475  * @ch: the channel we want to access
7476  */
7477 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7478 {
7479 	struct i40e_ring *tx_ring, *rx_ring;
7480 	u16 pf_q;
7481 	int i;
7482 
7483 	for (i = 0; i < ch->num_queue_pairs; i++) {
7484 		pf_q = ch->base_queue + i;
7485 		tx_ring = vsi->tx_rings[pf_q];
7486 		tx_ring->ch = NULL;
7487 		rx_ring = vsi->rx_rings[pf_q];
7488 		rx_ring->ch = NULL;
7489 	}
7490 }
7491 
7492 /**
7493  * i40e_free_macvlan_channels
7494  * @vsi: the VSI we want to access
7495  *
7496  * This function frees the Qs of the channel VSI from
7497  * the stack and also deletes the channel VSIs which
7498  * serve as macvlans.
7499  */
7500 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7501 {
7502 	struct i40e_channel *ch, *ch_tmp;
7503 	int ret;
7504 
7505 	if (list_empty(&vsi->macvlan_list))
7506 		return;
7507 
7508 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7509 		struct i40e_vsi *parent_vsi;
7510 
7511 		if (i40e_is_channel_macvlan(ch)) {
7512 			i40e_reset_ch_rings(vsi, ch);
7513 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7514 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7515 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7516 			kfree(ch->fwd);
7517 			ch->fwd = NULL;
7518 		}
7519 
7520 		list_del(&ch->list);
7521 		parent_vsi = ch->parent_vsi;
7522 		if (!parent_vsi || !ch->initialized) {
7523 			kfree(ch);
7524 			continue;
7525 		}
7526 
7527 		/* remove the VSI */
7528 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7529 					     NULL);
7530 		if (ret)
7531 			dev_err(&vsi->back->pdev->dev,
7532 				"unable to remove channel (%d) for parent VSI(%d)\n",
7533 				ch->seid, parent_vsi->seid);
7534 		kfree(ch);
7535 	}
7536 	vsi->macvlan_cnt = 0;
7537 }
7538 
7539 /**
7540  * i40e_fwd_ring_up - bring the macvlan device up
7541  * @vsi: the VSI we want to access
7542  * @vdev: macvlan netdevice
7543  * @fwd: the private fwd structure
7544  */
7545 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7546 			    struct i40e_fwd_adapter *fwd)
7547 {
7548 	int ret = 0, num_tc = 1,  i, aq_err;
7549 	struct i40e_channel *ch, *ch_tmp;
7550 	struct i40e_pf *pf = vsi->back;
7551 	struct i40e_hw *hw = &pf->hw;
7552 
7553 	if (list_empty(&vsi->macvlan_list))
7554 		return -EINVAL;
7555 
7556 	/* Go through the list and find an available channel */
7557 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7558 		if (!i40e_is_channel_macvlan(ch)) {
7559 			ch->fwd = fwd;
7560 			/* record configuration for macvlan interface in vdev */
7561 			for (i = 0; i < num_tc; i++)
7562 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7563 							     i,
7564 							     ch->num_queue_pairs,
7565 							     ch->base_queue);
7566 			for (i = 0; i < ch->num_queue_pairs; i++) {
7567 				struct i40e_ring *tx_ring, *rx_ring;
7568 				u16 pf_q;
7569 
7570 				pf_q = ch->base_queue + i;
7571 
7572 				/* Get to TX ring ptr */
7573 				tx_ring = vsi->tx_rings[pf_q];
7574 				tx_ring->ch = ch;
7575 
7576 				/* Get the RX ring ptr */
7577 				rx_ring = vsi->rx_rings[pf_q];
7578 				rx_ring->ch = ch;
7579 			}
7580 			break;
7581 		}
7582 	}
7583 
7584 	/* Guarantee all rings are updated before we update the
7585 	 * MAC address filter.
7586 	 */
7587 	wmb();
7588 
7589 	/* Add a mac filter */
7590 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7591 	if (ret) {
7592 		/* if we cannot add the MAC rule then disable the offload */
7593 		macvlan_release_l2fw_offload(vdev);
7594 		for (i = 0; i < ch->num_queue_pairs; i++) {
7595 			struct i40e_ring *rx_ring;
7596 			u16 pf_q;
7597 
7598 			pf_q = ch->base_queue + i;
7599 			rx_ring = vsi->rx_rings[pf_q];
7600 			rx_ring->netdev = NULL;
7601 		}
7602 		dev_info(&pf->pdev->dev,
7603 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7604 			  i40e_stat_str(hw, ret),
7605 			  i40e_aq_str(hw, aq_err));
7606 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7607 	}
7608 
7609 	return ret;
7610 }
7611 
7612 /**
7613  * i40e_setup_macvlans - create the channels which will be macvlans
7614  * @vsi: the VSI we want to access
7615  * @macvlan_cnt: no. of macvlans to be setup
7616  * @qcnt: no. of Qs per macvlan
7617  * @vdev: macvlan netdevice
7618  */
7619 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7620 			       struct net_device *vdev)
7621 {
7622 	struct i40e_pf *pf = vsi->back;
7623 	struct i40e_hw *hw = &pf->hw;
7624 	struct i40e_vsi_context ctxt;
7625 	u16 sections, qmap, num_qps;
7626 	struct i40e_channel *ch;
7627 	int i, pow, ret = 0;
7628 	u8 offset = 0;
7629 
7630 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7631 		return -EINVAL;
7632 
7633 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7634 
7635 	/* find the next higher power-of-2 of num queue pairs */
7636 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7637 
7638 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7639 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7640 
7641 	/* Setup context bits for the main VSI */
7642 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7643 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7644 	memset(&ctxt, 0, sizeof(ctxt));
7645 	ctxt.seid = vsi->seid;
7646 	ctxt.pf_num = vsi->back->hw.pf_id;
7647 	ctxt.vf_num = 0;
7648 	ctxt.uplink_seid = vsi->uplink_seid;
7649 	ctxt.info = vsi->info;
7650 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7651 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7652 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7653 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7654 
7655 	/* Reconfigure RSS for main VSI with new max queue count */
7656 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7657 	ret = i40e_vsi_config_rss(vsi);
7658 	if (ret) {
7659 		dev_info(&pf->pdev->dev,
7660 			 "Failed to reconfig RSS for num_queues (%u)\n",
7661 			 vsi->rss_size);
7662 		return ret;
7663 	}
7664 	vsi->reconfig_rss = true;
7665 	dev_dbg(&vsi->back->pdev->dev,
7666 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7667 	vsi->next_base_queue = num_qps;
7668 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7669 
7670 	/* Update the VSI after updating the VSI queue-mapping
7671 	 * information
7672 	 */
7673 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7674 	if (ret) {
7675 		dev_info(&pf->pdev->dev,
7676 			 "Update vsi tc config failed, err %s aq_err %s\n",
7677 			 i40e_stat_str(hw, ret),
7678 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7679 		return ret;
7680 	}
7681 	/* update the local VSI info with updated queue map */
7682 	i40e_vsi_update_queue_map(vsi, &ctxt);
7683 	vsi->info.valid_sections = 0;
7684 
7685 	/* Create channels for macvlans */
7686 	INIT_LIST_HEAD(&vsi->macvlan_list);
7687 	for (i = 0; i < macvlan_cnt; i++) {
7688 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7689 		if (!ch) {
7690 			ret = -ENOMEM;
7691 			goto err_free;
7692 		}
7693 		INIT_LIST_HEAD(&ch->list);
7694 		ch->num_queue_pairs = qcnt;
7695 		if (!i40e_setup_channel(pf, vsi, ch)) {
7696 			ret = -EINVAL;
7697 			kfree(ch);
7698 			goto err_free;
7699 		}
7700 		ch->parent_vsi = vsi;
7701 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7702 		vsi->macvlan_cnt++;
7703 		list_add_tail(&ch->list, &vsi->macvlan_list);
7704 	}
7705 
7706 	return ret;
7707 
7708 err_free:
7709 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7710 	i40e_free_macvlan_channels(vsi);
7711 
7712 	return ret;
7713 }
7714 
7715 /**
7716  * i40e_fwd_add - configure macvlans
7717  * @netdev: net device to configure
7718  * @vdev: macvlan netdevice
7719  **/
7720 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7721 {
7722 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7723 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7724 	struct i40e_vsi *vsi = np->vsi;
7725 	struct i40e_pf *pf = vsi->back;
7726 	struct i40e_fwd_adapter *fwd;
7727 	int avail_macvlan, ret;
7728 
7729 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7730 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7731 		return ERR_PTR(-EINVAL);
7732 	}
7733 	if ((pf->flags & I40E_FLAG_TC_MQPRIO)) {
7734 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7735 		return ERR_PTR(-EINVAL);
7736 	}
7737 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7738 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7739 		return ERR_PTR(-EINVAL);
7740 	}
7741 
7742 	/* The macvlan device has to be a single Q device so that the
7743 	 * tc_to_txq field can be reused to pick the tx queue.
7744 	 */
7745 	if (netif_is_multiqueue(vdev))
7746 		return ERR_PTR(-ERANGE);
7747 
7748 	if (!vsi->macvlan_cnt) {
7749 		/* reserve bit 0 for the pf device */
7750 		set_bit(0, vsi->fwd_bitmask);
7751 
7752 		/* Try to reserve as many queues as possible for macvlans. First
7753 		 * reserve 3/4th of max vectors, then half, then quarter and
7754 		 * calculate Qs per macvlan as you go
7755 		 */
7756 		vectors = pf->num_lan_msix;
7757 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7758 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7759 			q_per_macvlan = 4;
7760 			macvlan_cnt = (vectors - 32) / 4;
7761 		} else if (vectors <= 64 && vectors > 32) {
7762 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7763 			q_per_macvlan = 2;
7764 			macvlan_cnt = (vectors - 16) / 2;
7765 		} else if (vectors <= 32 && vectors > 16) {
7766 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7767 			q_per_macvlan = 1;
7768 			macvlan_cnt = vectors - 16;
7769 		} else if (vectors <= 16 && vectors > 8) {
7770 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7771 			q_per_macvlan = 1;
7772 			macvlan_cnt = vectors - 8;
7773 		} else {
7774 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7775 			q_per_macvlan = 1;
7776 			macvlan_cnt = vectors - 1;
7777 		}
7778 
7779 		if (macvlan_cnt == 0)
7780 			return ERR_PTR(-EBUSY);
7781 
7782 		/* Quiesce VSI queues */
7783 		i40e_quiesce_vsi(vsi);
7784 
7785 		/* sets up the macvlans but does not "enable" them */
7786 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
7787 					  vdev);
7788 		if (ret)
7789 			return ERR_PTR(ret);
7790 
7791 		/* Unquiesce VSI */
7792 		i40e_unquiesce_vsi(vsi);
7793 	}
7794 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
7795 					    vsi->macvlan_cnt);
7796 	if (avail_macvlan >= I40E_MAX_MACVLANS)
7797 		return ERR_PTR(-EBUSY);
7798 
7799 	/* create the fwd struct */
7800 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
7801 	if (!fwd)
7802 		return ERR_PTR(-ENOMEM);
7803 
7804 	set_bit(avail_macvlan, vsi->fwd_bitmask);
7805 	fwd->bit_no = avail_macvlan;
7806 	netdev_set_sb_channel(vdev, avail_macvlan);
7807 	fwd->netdev = vdev;
7808 
7809 	if (!netif_running(netdev))
7810 		return fwd;
7811 
7812 	/* Set fwd ring up */
7813 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
7814 	if (ret) {
7815 		/* unbind the queues and drop the subordinate channel config */
7816 		netdev_unbind_sb_channel(netdev, vdev);
7817 		netdev_set_sb_channel(vdev, 0);
7818 
7819 		kfree(fwd);
7820 		return ERR_PTR(-EINVAL);
7821 	}
7822 
7823 	return fwd;
7824 }
7825 
7826 /**
7827  * i40e_del_all_macvlans - Delete all the mac filters on the channels
7828  * @vsi: the VSI we want to access
7829  */
7830 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
7831 {
7832 	struct i40e_channel *ch, *ch_tmp;
7833 	struct i40e_pf *pf = vsi->back;
7834 	struct i40e_hw *hw = &pf->hw;
7835 	int aq_err, ret = 0;
7836 
7837 	if (list_empty(&vsi->macvlan_list))
7838 		return;
7839 
7840 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7841 		if (i40e_is_channel_macvlan(ch)) {
7842 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7843 						      i40e_channel_mac(ch),
7844 						      &aq_err);
7845 			if (!ret) {
7846 				/* Reset queue contexts */
7847 				i40e_reset_ch_rings(vsi, ch);
7848 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7849 				netdev_unbind_sb_channel(vsi->netdev,
7850 							 ch->fwd->netdev);
7851 				netdev_set_sb_channel(ch->fwd->netdev, 0);
7852 				kfree(ch->fwd);
7853 				ch->fwd = NULL;
7854 			}
7855 		}
7856 	}
7857 }
7858 
7859 /**
7860  * i40e_fwd_del - delete macvlan interfaces
7861  * @netdev: net device to configure
7862  * @vdev: macvlan netdevice
7863  */
7864 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
7865 {
7866 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7867 	struct i40e_fwd_adapter *fwd = vdev;
7868 	struct i40e_channel *ch, *ch_tmp;
7869 	struct i40e_vsi *vsi = np->vsi;
7870 	struct i40e_pf *pf = vsi->back;
7871 	struct i40e_hw *hw = &pf->hw;
7872 	int aq_err, ret = 0;
7873 
7874 	/* Find the channel associated with the macvlan and del mac filter */
7875 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7876 		if (i40e_is_channel_macvlan(ch) &&
7877 		    ether_addr_equal(i40e_channel_mac(ch),
7878 				     fwd->netdev->dev_addr)) {
7879 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7880 						      i40e_channel_mac(ch),
7881 						      &aq_err);
7882 			if (!ret) {
7883 				/* Reset queue contexts */
7884 				i40e_reset_ch_rings(vsi, ch);
7885 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7886 				netdev_unbind_sb_channel(netdev, fwd->netdev);
7887 				netdev_set_sb_channel(fwd->netdev, 0);
7888 				kfree(ch->fwd);
7889 				ch->fwd = NULL;
7890 			} else {
7891 				dev_info(&pf->pdev->dev,
7892 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
7893 					  i40e_stat_str(hw, ret),
7894 					  i40e_aq_str(hw, aq_err));
7895 			}
7896 			break;
7897 		}
7898 	}
7899 }
7900 
7901 /**
7902  * i40e_setup_tc - configure multiple traffic classes
7903  * @netdev: net device to configure
7904  * @type_data: tc offload data
7905  **/
7906 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
7907 {
7908 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
7909 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7910 	struct i40e_vsi *vsi = np->vsi;
7911 	struct i40e_pf *pf = vsi->back;
7912 	u8 enabled_tc = 0, num_tc, hw;
7913 	bool need_reset = false;
7914 	int old_queue_pairs;
7915 	int ret = -EINVAL;
7916 	u16 mode;
7917 	int i;
7918 
7919 	old_queue_pairs = vsi->num_queue_pairs;
7920 	num_tc = mqprio_qopt->qopt.num_tc;
7921 	hw = mqprio_qopt->qopt.hw;
7922 	mode = mqprio_qopt->mode;
7923 	if (!hw) {
7924 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7925 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
7926 		goto config_tc;
7927 	}
7928 
7929 	/* Check if MFP enabled */
7930 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
7931 		netdev_info(netdev,
7932 			    "Configuring TC not supported in MFP mode\n");
7933 		return ret;
7934 	}
7935 	switch (mode) {
7936 	case TC_MQPRIO_MODE_DCB:
7937 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7938 
7939 		/* Check if DCB enabled to continue */
7940 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7941 			netdev_info(netdev,
7942 				    "DCB is not enabled for adapter\n");
7943 			return ret;
7944 		}
7945 
7946 		/* Check whether tc count is within enabled limit */
7947 		if (num_tc > i40e_pf_get_num_tc(pf)) {
7948 			netdev_info(netdev,
7949 				    "TC count greater than enabled on link for adapter\n");
7950 			return ret;
7951 		}
7952 		break;
7953 	case TC_MQPRIO_MODE_CHANNEL:
7954 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
7955 			netdev_info(netdev,
7956 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
7957 			return ret;
7958 		}
7959 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7960 			return ret;
7961 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
7962 		if (ret)
7963 			return ret;
7964 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
7965 		       sizeof(*mqprio_qopt));
7966 		pf->flags |= I40E_FLAG_TC_MQPRIO;
7967 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7968 		break;
7969 	default:
7970 		return -EINVAL;
7971 	}
7972 
7973 config_tc:
7974 	/* Generate TC map for number of tc requested */
7975 	for (i = 0; i < num_tc; i++)
7976 		enabled_tc |= BIT(i);
7977 
7978 	/* Requesting same TC configuration as already enabled */
7979 	if (enabled_tc == vsi->tc_config.enabled_tc &&
7980 	    mode != TC_MQPRIO_MODE_CHANNEL)
7981 		return 0;
7982 
7983 	/* Quiesce VSI queues */
7984 	i40e_quiesce_vsi(vsi);
7985 
7986 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
7987 		i40e_remove_queue_channels(vsi);
7988 
7989 	/* Configure VSI for enabled TCs */
7990 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
7991 	if (ret) {
7992 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
7993 			    vsi->seid);
7994 		need_reset = true;
7995 		goto exit;
7996 	} else if (enabled_tc &&
7997 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
7998 		netdev_info(netdev,
7999 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8000 			    vsi->tc_config.tc_info[0].qcount);
8001 		ret = -EINVAL;
8002 		need_reset = true;
8003 		goto exit;
8004 	}
8005 
8006 	dev_info(&vsi->back->pdev->dev,
8007 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8008 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8009 
8010 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
8011 		if (vsi->mqprio_qopt.max_rate[0]) {
8012 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
8013 
8014 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
8015 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8016 			if (!ret) {
8017 				u64 credits = max_tx_rate;
8018 
8019 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8020 				dev_dbg(&vsi->back->pdev->dev,
8021 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8022 					max_tx_rate,
8023 					credits,
8024 					vsi->seid);
8025 			} else {
8026 				need_reset = true;
8027 				goto exit;
8028 			}
8029 		}
8030 		ret = i40e_configure_queue_channels(vsi);
8031 		if (ret) {
8032 			vsi->num_queue_pairs = old_queue_pairs;
8033 			netdev_info(netdev,
8034 				    "Failed configuring queue channels\n");
8035 			need_reset = true;
8036 			goto exit;
8037 		}
8038 	}
8039 
8040 exit:
8041 	/* Reset the configuration data to defaults, only TC0 is enabled */
8042 	if (need_reset) {
8043 		i40e_vsi_set_default_tc_config(vsi);
8044 		need_reset = false;
8045 	}
8046 
8047 	/* Unquiesce VSI */
8048 	i40e_unquiesce_vsi(vsi);
8049 	return ret;
8050 }
8051 
8052 /**
8053  * i40e_set_cld_element - sets cloud filter element data
8054  * @filter: cloud filter rule
8055  * @cld: ptr to cloud filter element data
8056  *
8057  * This is helper function to copy data into cloud filter element
8058  **/
8059 static inline void
8060 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8061 		     struct i40e_aqc_cloud_filters_element_data *cld)
8062 {
8063 	u32 ipa;
8064 	int i;
8065 
8066 	memset(cld, 0, sizeof(*cld));
8067 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8068 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8069 
8070 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8071 		return;
8072 
8073 	if (filter->n_proto == ETH_P_IPV6) {
8074 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8075 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8076 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8077 
8078 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8079 		}
8080 	} else {
8081 		ipa = be32_to_cpu(filter->dst_ipv4);
8082 
8083 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8084 	}
8085 
8086 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8087 
8088 	/* tenant_id is not supported by FW now, once the support is enabled
8089 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8090 	 */
8091 	if (filter->tenant_id)
8092 		return;
8093 }
8094 
8095 /**
8096  * i40e_add_del_cloud_filter - Add/del cloud filter
8097  * @vsi: pointer to VSI
8098  * @filter: cloud filter rule
8099  * @add: if true, add, if false, delete
8100  *
8101  * Add or delete a cloud filter for a specific flow spec.
8102  * Returns 0 if the filter were successfully added.
8103  **/
8104 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8105 			      struct i40e_cloud_filter *filter, bool add)
8106 {
8107 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8108 	struct i40e_pf *pf = vsi->back;
8109 	int ret;
8110 	static const u16 flag_table[128] = {
8111 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8112 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8113 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8114 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8115 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8116 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8117 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8118 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8119 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8120 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8121 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8122 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8123 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8124 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8125 	};
8126 
8127 	if (filter->flags >= ARRAY_SIZE(flag_table))
8128 		return I40E_ERR_CONFIG;
8129 
8130 	memset(&cld_filter, 0, sizeof(cld_filter));
8131 
8132 	/* copy element needed to add cloud filter from filter */
8133 	i40e_set_cld_element(filter, &cld_filter);
8134 
8135 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8136 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8137 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8138 
8139 	if (filter->n_proto == ETH_P_IPV6)
8140 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8141 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8142 	else
8143 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8144 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8145 
8146 	if (add)
8147 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8148 						&cld_filter, 1);
8149 	else
8150 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8151 						&cld_filter, 1);
8152 	if (ret)
8153 		dev_dbg(&pf->pdev->dev,
8154 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8155 			add ? "add" : "delete", filter->dst_port, ret,
8156 			pf->hw.aq.asq_last_status);
8157 	else
8158 		dev_info(&pf->pdev->dev,
8159 			 "%s cloud filter for VSI: %d\n",
8160 			 add ? "Added" : "Deleted", filter->seid);
8161 	return ret;
8162 }
8163 
8164 /**
8165  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8166  * @vsi: pointer to VSI
8167  * @filter: cloud filter rule
8168  * @add: if true, add, if false, delete
8169  *
8170  * Add or delete a cloud filter for a specific flow spec using big buffer.
8171  * Returns 0 if the filter were successfully added.
8172  **/
8173 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8174 				      struct i40e_cloud_filter *filter,
8175 				      bool add)
8176 {
8177 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8178 	struct i40e_pf *pf = vsi->back;
8179 	int ret;
8180 
8181 	/* Both (src/dst) valid mac_addr are not supported */
8182 	if ((is_valid_ether_addr(filter->dst_mac) &&
8183 	     is_valid_ether_addr(filter->src_mac)) ||
8184 	    (is_multicast_ether_addr(filter->dst_mac) &&
8185 	     is_multicast_ether_addr(filter->src_mac)))
8186 		return -EOPNOTSUPP;
8187 
8188 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8189 	 * ports are not supported via big buffer now.
8190 	 */
8191 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8192 		return -EOPNOTSUPP;
8193 
8194 	/* adding filter using src_port/src_ip is not supported at this stage */
8195 	if (filter->src_port ||
8196 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8197 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8198 		return -EOPNOTSUPP;
8199 
8200 	memset(&cld_filter, 0, sizeof(cld_filter));
8201 
8202 	/* copy element needed to add cloud filter from filter */
8203 	i40e_set_cld_element(filter, &cld_filter.element);
8204 
8205 	if (is_valid_ether_addr(filter->dst_mac) ||
8206 	    is_valid_ether_addr(filter->src_mac) ||
8207 	    is_multicast_ether_addr(filter->dst_mac) ||
8208 	    is_multicast_ether_addr(filter->src_mac)) {
8209 		/* MAC + IP : unsupported mode */
8210 		if (filter->dst_ipv4)
8211 			return -EOPNOTSUPP;
8212 
8213 		/* since we validated that L4 port must be valid before
8214 		 * we get here, start with respective "flags" value
8215 		 * and update if vlan is present or not
8216 		 */
8217 		cld_filter.element.flags =
8218 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8219 
8220 		if (filter->vlan_id) {
8221 			cld_filter.element.flags =
8222 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8223 		}
8224 
8225 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8226 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8227 		cld_filter.element.flags =
8228 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8229 		if (filter->n_proto == ETH_P_IPV6)
8230 			cld_filter.element.flags |=
8231 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8232 		else
8233 			cld_filter.element.flags |=
8234 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8235 	} else {
8236 		dev_err(&pf->pdev->dev,
8237 			"either mac or ip has to be valid for cloud filter\n");
8238 		return -EINVAL;
8239 	}
8240 
8241 	/* Now copy L4 port in Byte 6..7 in general fields */
8242 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8243 						be16_to_cpu(filter->dst_port);
8244 
8245 	if (add) {
8246 		/* Validate current device switch mode, change if necessary */
8247 		ret = i40e_validate_and_set_switch_mode(vsi);
8248 		if (ret) {
8249 			dev_err(&pf->pdev->dev,
8250 				"failed to set switch mode, ret %d\n",
8251 				ret);
8252 			return ret;
8253 		}
8254 
8255 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8256 						   &cld_filter, 1);
8257 	} else {
8258 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8259 						   &cld_filter, 1);
8260 	}
8261 
8262 	if (ret)
8263 		dev_dbg(&pf->pdev->dev,
8264 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8265 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8266 	else
8267 		dev_info(&pf->pdev->dev,
8268 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8269 			 add ? "add" : "delete", filter->seid,
8270 			 ntohs(filter->dst_port));
8271 	return ret;
8272 }
8273 
8274 /**
8275  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8276  * @vsi: Pointer to VSI
8277  * @f: Pointer to struct flow_cls_offload
8278  * @filter: Pointer to cloud filter structure
8279  *
8280  **/
8281 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8282 				 struct flow_cls_offload *f,
8283 				 struct i40e_cloud_filter *filter)
8284 {
8285 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8286 	struct flow_dissector *dissector = rule->match.dissector;
8287 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8288 	struct i40e_pf *pf = vsi->back;
8289 	u8 field_flags = 0;
8290 
8291 	if (dissector->used_keys &
8292 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8293 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8294 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8295 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8296 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8297 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8298 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8299 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8300 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8301 			dissector->used_keys);
8302 		return -EOPNOTSUPP;
8303 	}
8304 
8305 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8306 		struct flow_match_enc_keyid match;
8307 
8308 		flow_rule_match_enc_keyid(rule, &match);
8309 		if (match.mask->keyid != 0)
8310 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8311 
8312 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8313 	}
8314 
8315 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8316 		struct flow_match_basic match;
8317 
8318 		flow_rule_match_basic(rule, &match);
8319 		n_proto_key = ntohs(match.key->n_proto);
8320 		n_proto_mask = ntohs(match.mask->n_proto);
8321 
8322 		if (n_proto_key == ETH_P_ALL) {
8323 			n_proto_key = 0;
8324 			n_proto_mask = 0;
8325 		}
8326 		filter->n_proto = n_proto_key & n_proto_mask;
8327 		filter->ip_proto = match.key->ip_proto;
8328 	}
8329 
8330 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8331 		struct flow_match_eth_addrs match;
8332 
8333 		flow_rule_match_eth_addrs(rule, &match);
8334 
8335 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8336 		if (!is_zero_ether_addr(match.mask->dst)) {
8337 			if (is_broadcast_ether_addr(match.mask->dst)) {
8338 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8339 			} else {
8340 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8341 					match.mask->dst);
8342 				return I40E_ERR_CONFIG;
8343 			}
8344 		}
8345 
8346 		if (!is_zero_ether_addr(match.mask->src)) {
8347 			if (is_broadcast_ether_addr(match.mask->src)) {
8348 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8349 			} else {
8350 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8351 					match.mask->src);
8352 				return I40E_ERR_CONFIG;
8353 			}
8354 		}
8355 		ether_addr_copy(filter->dst_mac, match.key->dst);
8356 		ether_addr_copy(filter->src_mac, match.key->src);
8357 	}
8358 
8359 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8360 		struct flow_match_vlan match;
8361 
8362 		flow_rule_match_vlan(rule, &match);
8363 		if (match.mask->vlan_id) {
8364 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8365 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8366 
8367 			} else {
8368 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8369 					match.mask->vlan_id);
8370 				return I40E_ERR_CONFIG;
8371 			}
8372 		}
8373 
8374 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8375 	}
8376 
8377 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8378 		struct flow_match_control match;
8379 
8380 		flow_rule_match_control(rule, &match);
8381 		addr_type = match.key->addr_type;
8382 	}
8383 
8384 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8385 		struct flow_match_ipv4_addrs match;
8386 
8387 		flow_rule_match_ipv4_addrs(rule, &match);
8388 		if (match.mask->dst) {
8389 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8390 				field_flags |= I40E_CLOUD_FIELD_IIP;
8391 			} else {
8392 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8393 					&match.mask->dst);
8394 				return I40E_ERR_CONFIG;
8395 			}
8396 		}
8397 
8398 		if (match.mask->src) {
8399 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8400 				field_flags |= I40E_CLOUD_FIELD_IIP;
8401 			} else {
8402 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8403 					&match.mask->src);
8404 				return I40E_ERR_CONFIG;
8405 			}
8406 		}
8407 
8408 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8409 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8410 			return I40E_ERR_CONFIG;
8411 		}
8412 		filter->dst_ipv4 = match.key->dst;
8413 		filter->src_ipv4 = match.key->src;
8414 	}
8415 
8416 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8417 		struct flow_match_ipv6_addrs match;
8418 
8419 		flow_rule_match_ipv6_addrs(rule, &match);
8420 
8421 		/* src and dest IPV6 address should not be LOOPBACK
8422 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8423 		 */
8424 		if (ipv6_addr_loopback(&match.key->dst) ||
8425 		    ipv6_addr_loopback(&match.key->src)) {
8426 			dev_err(&pf->pdev->dev,
8427 				"Bad ipv6, addr is LOOPBACK\n");
8428 			return I40E_ERR_CONFIG;
8429 		}
8430 		if (!ipv6_addr_any(&match.mask->dst) ||
8431 		    !ipv6_addr_any(&match.mask->src))
8432 			field_flags |= I40E_CLOUD_FIELD_IIP;
8433 
8434 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8435 		       sizeof(filter->src_ipv6));
8436 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8437 		       sizeof(filter->dst_ipv6));
8438 	}
8439 
8440 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8441 		struct flow_match_ports match;
8442 
8443 		flow_rule_match_ports(rule, &match);
8444 		if (match.mask->src) {
8445 			if (match.mask->src == cpu_to_be16(0xffff)) {
8446 				field_flags |= I40E_CLOUD_FIELD_IIP;
8447 			} else {
8448 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8449 					be16_to_cpu(match.mask->src));
8450 				return I40E_ERR_CONFIG;
8451 			}
8452 		}
8453 
8454 		if (match.mask->dst) {
8455 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8456 				field_flags |= I40E_CLOUD_FIELD_IIP;
8457 			} else {
8458 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8459 					be16_to_cpu(match.mask->dst));
8460 				return I40E_ERR_CONFIG;
8461 			}
8462 		}
8463 
8464 		filter->dst_port = match.key->dst;
8465 		filter->src_port = match.key->src;
8466 
8467 		switch (filter->ip_proto) {
8468 		case IPPROTO_TCP:
8469 		case IPPROTO_UDP:
8470 			break;
8471 		default:
8472 			dev_err(&pf->pdev->dev,
8473 				"Only UDP and TCP transport are supported\n");
8474 			return -EINVAL;
8475 		}
8476 	}
8477 	filter->flags = field_flags;
8478 	return 0;
8479 }
8480 
8481 /**
8482  * i40e_handle_tclass: Forward to a traffic class on the device
8483  * @vsi: Pointer to VSI
8484  * @tc: traffic class index on the device
8485  * @filter: Pointer to cloud filter structure
8486  *
8487  **/
8488 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8489 			      struct i40e_cloud_filter *filter)
8490 {
8491 	struct i40e_channel *ch, *ch_tmp;
8492 
8493 	/* direct to a traffic class on the same device */
8494 	if (tc == 0) {
8495 		filter->seid = vsi->seid;
8496 		return 0;
8497 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8498 		if (!filter->dst_port) {
8499 			dev_err(&vsi->back->pdev->dev,
8500 				"Specify destination port to direct to traffic class that is not default\n");
8501 			return -EINVAL;
8502 		}
8503 		if (list_empty(&vsi->ch_list))
8504 			return -EINVAL;
8505 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8506 					 list) {
8507 			if (ch->seid == vsi->tc_seid_map[tc])
8508 				filter->seid = ch->seid;
8509 		}
8510 		return 0;
8511 	}
8512 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8513 	return -EINVAL;
8514 }
8515 
8516 /**
8517  * i40e_configure_clsflower - Configure tc flower filters
8518  * @vsi: Pointer to VSI
8519  * @cls_flower: Pointer to struct flow_cls_offload
8520  *
8521  **/
8522 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8523 				    struct flow_cls_offload *cls_flower)
8524 {
8525 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8526 	struct i40e_cloud_filter *filter = NULL;
8527 	struct i40e_pf *pf = vsi->back;
8528 	int err = 0;
8529 
8530 	if (tc < 0) {
8531 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8532 		return -EOPNOTSUPP;
8533 	}
8534 
8535 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8536 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8537 		return -EBUSY;
8538 
8539 	if (pf->fdir_pf_active_filters ||
8540 	    (!hlist_empty(&pf->fdir_filter_list))) {
8541 		dev_err(&vsi->back->pdev->dev,
8542 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8543 		return -EINVAL;
8544 	}
8545 
8546 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8547 		dev_err(&vsi->back->pdev->dev,
8548 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8549 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8550 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8551 	}
8552 
8553 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8554 	if (!filter)
8555 		return -ENOMEM;
8556 
8557 	filter->cookie = cls_flower->cookie;
8558 
8559 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8560 	if (err < 0)
8561 		goto err;
8562 
8563 	err = i40e_handle_tclass(vsi, tc, filter);
8564 	if (err < 0)
8565 		goto err;
8566 
8567 	/* Add cloud filter */
8568 	if (filter->dst_port)
8569 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8570 	else
8571 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8572 
8573 	if (err) {
8574 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8575 			err);
8576 		goto err;
8577 	}
8578 
8579 	/* add filter to the ordered list */
8580 	INIT_HLIST_NODE(&filter->cloud_node);
8581 
8582 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8583 
8584 	pf->num_cloud_filters++;
8585 
8586 	return err;
8587 err:
8588 	kfree(filter);
8589 	return err;
8590 }
8591 
8592 /**
8593  * i40e_find_cloud_filter - Find the could filter in the list
8594  * @vsi: Pointer to VSI
8595  * @cookie: filter specific cookie
8596  *
8597  **/
8598 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8599 							unsigned long *cookie)
8600 {
8601 	struct i40e_cloud_filter *filter = NULL;
8602 	struct hlist_node *node2;
8603 
8604 	hlist_for_each_entry_safe(filter, node2,
8605 				  &vsi->back->cloud_filter_list, cloud_node)
8606 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8607 			return filter;
8608 	return NULL;
8609 }
8610 
8611 /**
8612  * i40e_delete_clsflower - Remove tc flower filters
8613  * @vsi: Pointer to VSI
8614  * @cls_flower: Pointer to struct flow_cls_offload
8615  *
8616  **/
8617 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8618 				 struct flow_cls_offload *cls_flower)
8619 {
8620 	struct i40e_cloud_filter *filter = NULL;
8621 	struct i40e_pf *pf = vsi->back;
8622 	int err = 0;
8623 
8624 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8625 
8626 	if (!filter)
8627 		return -EINVAL;
8628 
8629 	hash_del(&filter->cloud_node);
8630 
8631 	if (filter->dst_port)
8632 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8633 	else
8634 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8635 
8636 	kfree(filter);
8637 	if (err) {
8638 		dev_err(&pf->pdev->dev,
8639 			"Failed to delete cloud filter, err %s\n",
8640 			i40e_stat_str(&pf->hw, err));
8641 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8642 	}
8643 
8644 	pf->num_cloud_filters--;
8645 	if (!pf->num_cloud_filters)
8646 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8647 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8648 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8649 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8650 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8651 		}
8652 	return 0;
8653 }
8654 
8655 /**
8656  * i40e_setup_tc_cls_flower - flower classifier offloads
8657  * @np: net device to configure
8658  * @cls_flower: offload data
8659  **/
8660 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8661 				    struct flow_cls_offload *cls_flower)
8662 {
8663 	struct i40e_vsi *vsi = np->vsi;
8664 
8665 	switch (cls_flower->command) {
8666 	case FLOW_CLS_REPLACE:
8667 		return i40e_configure_clsflower(vsi, cls_flower);
8668 	case FLOW_CLS_DESTROY:
8669 		return i40e_delete_clsflower(vsi, cls_flower);
8670 	case FLOW_CLS_STATS:
8671 		return -EOPNOTSUPP;
8672 	default:
8673 		return -EOPNOTSUPP;
8674 	}
8675 }
8676 
8677 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8678 				  void *cb_priv)
8679 {
8680 	struct i40e_netdev_priv *np = cb_priv;
8681 
8682 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8683 		return -EOPNOTSUPP;
8684 
8685 	switch (type) {
8686 	case TC_SETUP_CLSFLOWER:
8687 		return i40e_setup_tc_cls_flower(np, type_data);
8688 
8689 	default:
8690 		return -EOPNOTSUPP;
8691 	}
8692 }
8693 
8694 static LIST_HEAD(i40e_block_cb_list);
8695 
8696 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8697 			   void *type_data)
8698 {
8699 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8700 
8701 	switch (type) {
8702 	case TC_SETUP_QDISC_MQPRIO:
8703 		return i40e_setup_tc(netdev, type_data);
8704 	case TC_SETUP_BLOCK:
8705 		return flow_block_cb_setup_simple(type_data,
8706 						  &i40e_block_cb_list,
8707 						  i40e_setup_tc_block_cb,
8708 						  np, np, true);
8709 	default:
8710 		return -EOPNOTSUPP;
8711 	}
8712 }
8713 
8714 /**
8715  * i40e_open - Called when a network interface is made active
8716  * @netdev: network interface device structure
8717  *
8718  * The open entry point is called when a network interface is made
8719  * active by the system (IFF_UP).  At this point all resources needed
8720  * for transmit and receive operations are allocated, the interrupt
8721  * handler is registered with the OS, the netdev watchdog subtask is
8722  * enabled, and the stack is notified that the interface is ready.
8723  *
8724  * Returns 0 on success, negative value on failure
8725  **/
8726 int i40e_open(struct net_device *netdev)
8727 {
8728 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8729 	struct i40e_vsi *vsi = np->vsi;
8730 	struct i40e_pf *pf = vsi->back;
8731 	int err;
8732 
8733 	/* disallow open during test or if eeprom is broken */
8734 	if (test_bit(__I40E_TESTING, pf->state) ||
8735 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8736 		return -EBUSY;
8737 
8738 	netif_carrier_off(netdev);
8739 
8740 	if (i40e_force_link_state(pf, true))
8741 		return -EAGAIN;
8742 
8743 	err = i40e_vsi_open(vsi);
8744 	if (err)
8745 		return err;
8746 
8747 	/* configure global TSO hardware offload settings */
8748 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8749 						       TCP_FLAG_FIN) >> 16);
8750 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8751 						       TCP_FLAG_FIN |
8752 						       TCP_FLAG_CWR) >> 16);
8753 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8754 	udp_tunnel_get_rx_info(netdev);
8755 
8756 	return 0;
8757 }
8758 
8759 /**
8760  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
8761  * @vsi: vsi structure
8762  *
8763  * This updates netdev's number of tx/rx queues
8764  *
8765  * Returns status of setting tx/rx queues
8766  **/
8767 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
8768 {
8769 	int ret;
8770 
8771 	ret = netif_set_real_num_rx_queues(vsi->netdev,
8772 					   vsi->num_queue_pairs);
8773 	if (ret)
8774 		return ret;
8775 
8776 	return netif_set_real_num_tx_queues(vsi->netdev,
8777 					    vsi->num_queue_pairs);
8778 }
8779 
8780 /**
8781  * i40e_vsi_open -
8782  * @vsi: the VSI to open
8783  *
8784  * Finish initialization of the VSI.
8785  *
8786  * Returns 0 on success, negative value on failure
8787  *
8788  * Note: expects to be called while under rtnl_lock()
8789  **/
8790 int i40e_vsi_open(struct i40e_vsi *vsi)
8791 {
8792 	struct i40e_pf *pf = vsi->back;
8793 	char int_name[I40E_INT_NAME_STR_LEN];
8794 	int err;
8795 
8796 	/* allocate descriptors */
8797 	err = i40e_vsi_setup_tx_resources(vsi);
8798 	if (err)
8799 		goto err_setup_tx;
8800 	err = i40e_vsi_setup_rx_resources(vsi);
8801 	if (err)
8802 		goto err_setup_rx;
8803 
8804 	err = i40e_vsi_configure(vsi);
8805 	if (err)
8806 		goto err_setup_rx;
8807 
8808 	if (vsi->netdev) {
8809 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
8810 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
8811 		err = i40e_vsi_request_irq(vsi, int_name);
8812 		if (err)
8813 			goto err_setup_rx;
8814 
8815 		/* Notify the stack of the actual queue counts. */
8816 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
8817 		if (err)
8818 			goto err_set_queues;
8819 
8820 	} else if (vsi->type == I40E_VSI_FDIR) {
8821 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
8822 			 dev_driver_string(&pf->pdev->dev),
8823 			 dev_name(&pf->pdev->dev));
8824 		err = i40e_vsi_request_irq(vsi, int_name);
8825 		if (err)
8826 			goto err_setup_rx;
8827 
8828 	} else {
8829 		err = -EINVAL;
8830 		goto err_setup_rx;
8831 	}
8832 
8833 	err = i40e_up_complete(vsi);
8834 	if (err)
8835 		goto err_up_complete;
8836 
8837 	return 0;
8838 
8839 err_up_complete:
8840 	i40e_down(vsi);
8841 err_set_queues:
8842 	i40e_vsi_free_irq(vsi);
8843 err_setup_rx:
8844 	i40e_vsi_free_rx_resources(vsi);
8845 err_setup_tx:
8846 	i40e_vsi_free_tx_resources(vsi);
8847 	if (vsi == pf->vsi[pf->lan_vsi])
8848 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
8849 
8850 	return err;
8851 }
8852 
8853 /**
8854  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
8855  * @pf: Pointer to PF
8856  *
8857  * This function destroys the hlist where all the Flow Director
8858  * filters were saved.
8859  **/
8860 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
8861 {
8862 	struct i40e_fdir_filter *filter;
8863 	struct i40e_flex_pit *pit_entry, *tmp;
8864 	struct hlist_node *node2;
8865 
8866 	hlist_for_each_entry_safe(filter, node2,
8867 				  &pf->fdir_filter_list, fdir_node) {
8868 		hlist_del(&filter->fdir_node);
8869 		kfree(filter);
8870 	}
8871 
8872 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
8873 		list_del(&pit_entry->list);
8874 		kfree(pit_entry);
8875 	}
8876 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
8877 
8878 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
8879 		list_del(&pit_entry->list);
8880 		kfree(pit_entry);
8881 	}
8882 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
8883 
8884 	pf->fdir_pf_active_filters = 0;
8885 	i40e_reset_fdir_filter_cnt(pf);
8886 
8887 	/* Reprogram the default input set for TCP/IPv4 */
8888 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8889 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8890 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8891 
8892 	/* Reprogram the default input set for TCP/IPv6 */
8893 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
8894 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8895 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8896 
8897 	/* Reprogram the default input set for UDP/IPv4 */
8898 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
8899 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8900 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8901 
8902 	/* Reprogram the default input set for UDP/IPv6 */
8903 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
8904 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8905 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8906 
8907 	/* Reprogram the default input set for SCTP/IPv4 */
8908 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
8909 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8910 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8911 
8912 	/* Reprogram the default input set for SCTP/IPv6 */
8913 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
8914 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8915 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8916 
8917 	/* Reprogram the default input set for Other/IPv4 */
8918 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
8919 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8920 
8921 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
8922 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8923 
8924 	/* Reprogram the default input set for Other/IPv6 */
8925 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
8926 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8927 
8928 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
8929 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8930 }
8931 
8932 /**
8933  * i40e_cloud_filter_exit - Cleans up the cloud filters
8934  * @pf: Pointer to PF
8935  *
8936  * This function destroys the hlist where all the cloud filters
8937  * were saved.
8938  **/
8939 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
8940 {
8941 	struct i40e_cloud_filter *cfilter;
8942 	struct hlist_node *node;
8943 
8944 	hlist_for_each_entry_safe(cfilter, node,
8945 				  &pf->cloud_filter_list, cloud_node) {
8946 		hlist_del(&cfilter->cloud_node);
8947 		kfree(cfilter);
8948 	}
8949 	pf->num_cloud_filters = 0;
8950 
8951 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8952 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8953 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8954 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8955 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8956 	}
8957 }
8958 
8959 /**
8960  * i40e_close - Disables a network interface
8961  * @netdev: network interface device structure
8962  *
8963  * The close entry point is called when an interface is de-activated
8964  * by the OS.  The hardware is still under the driver's control, but
8965  * this netdev interface is disabled.
8966  *
8967  * Returns 0, this is not allowed to fail
8968  **/
8969 int i40e_close(struct net_device *netdev)
8970 {
8971 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8972 	struct i40e_vsi *vsi = np->vsi;
8973 
8974 	i40e_vsi_close(vsi);
8975 
8976 	return 0;
8977 }
8978 
8979 /**
8980  * i40e_do_reset - Start a PF or Core Reset sequence
8981  * @pf: board private structure
8982  * @reset_flags: which reset is requested
8983  * @lock_acquired: indicates whether or not the lock has been acquired
8984  * before this function was called.
8985  *
8986  * The essential difference in resets is that the PF Reset
8987  * doesn't clear the packet buffers, doesn't reset the PE
8988  * firmware, and doesn't bother the other PFs on the chip.
8989  **/
8990 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
8991 {
8992 	u32 val;
8993 
8994 	/* do the biggest reset indicated */
8995 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
8996 
8997 		/* Request a Global Reset
8998 		 *
8999 		 * This will start the chip's countdown to the actual full
9000 		 * chip reset event, and a warning interrupt to be sent
9001 		 * to all PFs, including the requestor.  Our handler
9002 		 * for the warning interrupt will deal with the shutdown
9003 		 * and recovery of the switch setup.
9004 		 */
9005 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9006 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9007 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9008 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9009 
9010 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9011 
9012 		/* Request a Core Reset
9013 		 *
9014 		 * Same as Global Reset, except does *not* include the MAC/PHY
9015 		 */
9016 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9017 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9018 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9019 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9020 		i40e_flush(&pf->hw);
9021 
9022 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9023 
9024 		/* Request a PF Reset
9025 		 *
9026 		 * Resets only the PF-specific registers
9027 		 *
9028 		 * This goes directly to the tear-down and rebuild of
9029 		 * the switch, since we need to do all the recovery as
9030 		 * for the Core Reset.
9031 		 */
9032 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9033 		i40e_handle_reset_warning(pf, lock_acquired);
9034 
9035 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9036 		/* Request a PF Reset
9037 		 *
9038 		 * Resets PF and reinitializes PFs VSI.
9039 		 */
9040 		i40e_prep_for_reset(pf);
9041 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9042 		dev_info(&pf->pdev->dev,
9043 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9044 			 "FW LLDP is disabled\n" :
9045 			 "FW LLDP is enabled\n");
9046 
9047 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9048 		int v;
9049 
9050 		/* Find the VSI(s) that requested a re-init */
9051 		dev_info(&pf->pdev->dev,
9052 			 "VSI reinit requested\n");
9053 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9054 			struct i40e_vsi *vsi = pf->vsi[v];
9055 
9056 			if (vsi != NULL &&
9057 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9058 					       vsi->state))
9059 				i40e_vsi_reinit_locked(pf->vsi[v]);
9060 		}
9061 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9062 		int v;
9063 
9064 		/* Find the VSI(s) that needs to be brought down */
9065 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9066 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9067 			struct i40e_vsi *vsi = pf->vsi[v];
9068 
9069 			if (vsi != NULL &&
9070 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9071 					       vsi->state)) {
9072 				set_bit(__I40E_VSI_DOWN, vsi->state);
9073 				i40e_down(vsi);
9074 			}
9075 		}
9076 	} else {
9077 		dev_info(&pf->pdev->dev,
9078 			 "bad reset request 0x%08x\n", reset_flags);
9079 	}
9080 }
9081 
9082 #ifdef CONFIG_I40E_DCB
9083 /**
9084  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9085  * @pf: board private structure
9086  * @old_cfg: current DCB config
9087  * @new_cfg: new DCB config
9088  **/
9089 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9090 			    struct i40e_dcbx_config *old_cfg,
9091 			    struct i40e_dcbx_config *new_cfg)
9092 {
9093 	bool need_reconfig = false;
9094 
9095 	/* Check if ETS configuration has changed */
9096 	if (memcmp(&new_cfg->etscfg,
9097 		   &old_cfg->etscfg,
9098 		   sizeof(new_cfg->etscfg))) {
9099 		/* If Priority Table has changed reconfig is needed */
9100 		if (memcmp(&new_cfg->etscfg.prioritytable,
9101 			   &old_cfg->etscfg.prioritytable,
9102 			   sizeof(new_cfg->etscfg.prioritytable))) {
9103 			need_reconfig = true;
9104 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9105 		}
9106 
9107 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9108 			   &old_cfg->etscfg.tcbwtable,
9109 			   sizeof(new_cfg->etscfg.tcbwtable)))
9110 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9111 
9112 		if (memcmp(&new_cfg->etscfg.tsatable,
9113 			   &old_cfg->etscfg.tsatable,
9114 			   sizeof(new_cfg->etscfg.tsatable)))
9115 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9116 	}
9117 
9118 	/* Check if PFC configuration has changed */
9119 	if (memcmp(&new_cfg->pfc,
9120 		   &old_cfg->pfc,
9121 		   sizeof(new_cfg->pfc))) {
9122 		need_reconfig = true;
9123 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9124 	}
9125 
9126 	/* Check if APP Table has changed */
9127 	if (memcmp(&new_cfg->app,
9128 		   &old_cfg->app,
9129 		   sizeof(new_cfg->app))) {
9130 		need_reconfig = true;
9131 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9132 	}
9133 
9134 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9135 	return need_reconfig;
9136 }
9137 
9138 /**
9139  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9140  * @pf: board private structure
9141  * @e: event info posted on ARQ
9142  **/
9143 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9144 				  struct i40e_arq_event_info *e)
9145 {
9146 	struct i40e_aqc_lldp_get_mib *mib =
9147 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9148 	struct i40e_hw *hw = &pf->hw;
9149 	struct i40e_dcbx_config tmp_dcbx_cfg;
9150 	bool need_reconfig = false;
9151 	int ret = 0;
9152 	u8 type;
9153 
9154 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9155 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9156 	    (hw->phy.link_info.link_speed &
9157 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9158 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9159 		/* let firmware decide if the DCB should be disabled */
9160 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9161 
9162 	/* Not DCB capable or capability disabled */
9163 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9164 		return ret;
9165 
9166 	/* Ignore if event is not for Nearest Bridge */
9167 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9168 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9169 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9170 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9171 		return ret;
9172 
9173 	/* Check MIB Type and return if event for Remote MIB update */
9174 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9175 	dev_dbg(&pf->pdev->dev,
9176 		"LLDP event mib type %s\n", type ? "remote" : "local");
9177 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9178 		/* Update the remote cached instance and return */
9179 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9180 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9181 				&hw->remote_dcbx_config);
9182 		goto exit;
9183 	}
9184 
9185 	/* Store the old configuration */
9186 	tmp_dcbx_cfg = hw->local_dcbx_config;
9187 
9188 	/* Reset the old DCBx configuration data */
9189 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9190 	/* Get updated DCBX data from firmware */
9191 	ret = i40e_get_dcb_config(&pf->hw);
9192 	if (ret) {
9193 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9194 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9195 		    (hw->phy.link_info.link_speed &
9196 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9197 			dev_warn(&pf->pdev->dev,
9198 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9199 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9200 		} else {
9201 			dev_info(&pf->pdev->dev,
9202 				 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
9203 				 i40e_stat_str(&pf->hw, ret),
9204 				 i40e_aq_str(&pf->hw,
9205 					     pf->hw.aq.asq_last_status));
9206 		}
9207 		goto exit;
9208 	}
9209 
9210 	/* No change detected in DCBX configs */
9211 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9212 		    sizeof(tmp_dcbx_cfg))) {
9213 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9214 		goto exit;
9215 	}
9216 
9217 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9218 					       &hw->local_dcbx_config);
9219 
9220 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9221 
9222 	if (!need_reconfig)
9223 		goto exit;
9224 
9225 	/* Enable DCB tagging only when more than one TC */
9226 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9227 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9228 	else
9229 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9230 
9231 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9232 	/* Reconfiguration needed quiesce all VSIs */
9233 	i40e_pf_quiesce_all_vsi(pf);
9234 
9235 	/* Changes in configuration update VEB/VSI */
9236 	i40e_dcb_reconfigure(pf);
9237 
9238 	ret = i40e_resume_port_tx(pf);
9239 
9240 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9241 	/* In case of error no point in resuming VSIs */
9242 	if (ret)
9243 		goto exit;
9244 
9245 	/* Wait for the PF's queues to be disabled */
9246 	ret = i40e_pf_wait_queues_disabled(pf);
9247 	if (ret) {
9248 		/* Schedule PF reset to recover */
9249 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9250 		i40e_service_event_schedule(pf);
9251 	} else {
9252 		i40e_pf_unquiesce_all_vsi(pf);
9253 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9254 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9255 	}
9256 
9257 exit:
9258 	return ret;
9259 }
9260 #endif /* CONFIG_I40E_DCB */
9261 
9262 /**
9263  * i40e_do_reset_safe - Protected reset path for userland calls.
9264  * @pf: board private structure
9265  * @reset_flags: which reset is requested
9266  *
9267  **/
9268 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9269 {
9270 	rtnl_lock();
9271 	i40e_do_reset(pf, reset_flags, true);
9272 	rtnl_unlock();
9273 }
9274 
9275 /**
9276  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9277  * @pf: board private structure
9278  * @e: event info posted on ARQ
9279  *
9280  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9281  * and VF queues
9282  **/
9283 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9284 					   struct i40e_arq_event_info *e)
9285 {
9286 	struct i40e_aqc_lan_overflow *data =
9287 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9288 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9289 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9290 	struct i40e_hw *hw = &pf->hw;
9291 	struct i40e_vf *vf;
9292 	u16 vf_id;
9293 
9294 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9295 		queue, qtx_ctl);
9296 
9297 	/* Queue belongs to VF, find the VF and issue VF reset */
9298 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9299 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9300 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9301 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9302 		vf_id -= hw->func_caps.vf_base_id;
9303 		vf = &pf->vf[vf_id];
9304 		i40e_vc_notify_vf_reset(vf);
9305 		/* Allow VF to process pending reset notification */
9306 		msleep(20);
9307 		i40e_reset_vf(vf, false);
9308 	}
9309 }
9310 
9311 /**
9312  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9313  * @pf: board private structure
9314  **/
9315 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9316 {
9317 	u32 val, fcnt_prog;
9318 
9319 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9320 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9321 	return fcnt_prog;
9322 }
9323 
9324 /**
9325  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9326  * @pf: board private structure
9327  **/
9328 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9329 {
9330 	u32 val, fcnt_prog;
9331 
9332 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9333 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9334 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9335 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9336 	return fcnt_prog;
9337 }
9338 
9339 /**
9340  * i40e_get_global_fd_count - Get total FD filters programmed on device
9341  * @pf: board private structure
9342  **/
9343 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9344 {
9345 	u32 val, fcnt_prog;
9346 
9347 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9348 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9349 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9350 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9351 	return fcnt_prog;
9352 }
9353 
9354 /**
9355  * i40e_reenable_fdir_sb - Restore FDir SB capability
9356  * @pf: board private structure
9357  **/
9358 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9359 {
9360 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9361 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9362 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9363 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9364 }
9365 
9366 /**
9367  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9368  * @pf: board private structure
9369  **/
9370 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9371 {
9372 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9373 		/* ATR uses the same filtering logic as SB rules. It only
9374 		 * functions properly if the input set mask is at the default
9375 		 * settings. It is safe to restore the default input set
9376 		 * because there are no active TCPv4 filter rules.
9377 		 */
9378 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9379 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9380 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9381 
9382 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9383 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9384 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9385 	}
9386 }
9387 
9388 /**
9389  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9390  * @pf: board private structure
9391  * @filter: FDir filter to remove
9392  */
9393 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9394 				       struct i40e_fdir_filter *filter)
9395 {
9396 	/* Update counters */
9397 	pf->fdir_pf_active_filters--;
9398 	pf->fd_inv = 0;
9399 
9400 	switch (filter->flow_type) {
9401 	case TCP_V4_FLOW:
9402 		pf->fd_tcp4_filter_cnt--;
9403 		break;
9404 	case UDP_V4_FLOW:
9405 		pf->fd_udp4_filter_cnt--;
9406 		break;
9407 	case SCTP_V4_FLOW:
9408 		pf->fd_sctp4_filter_cnt--;
9409 		break;
9410 	case TCP_V6_FLOW:
9411 		pf->fd_tcp6_filter_cnt--;
9412 		break;
9413 	case UDP_V6_FLOW:
9414 		pf->fd_udp6_filter_cnt--;
9415 		break;
9416 	case SCTP_V6_FLOW:
9417 		pf->fd_udp6_filter_cnt--;
9418 		break;
9419 	case IP_USER_FLOW:
9420 		switch (filter->ipl4_proto) {
9421 		case IPPROTO_TCP:
9422 			pf->fd_tcp4_filter_cnt--;
9423 			break;
9424 		case IPPROTO_UDP:
9425 			pf->fd_udp4_filter_cnt--;
9426 			break;
9427 		case IPPROTO_SCTP:
9428 			pf->fd_sctp4_filter_cnt--;
9429 			break;
9430 		case IPPROTO_IP:
9431 			pf->fd_ip4_filter_cnt--;
9432 			break;
9433 		}
9434 		break;
9435 	case IPV6_USER_FLOW:
9436 		switch (filter->ipl4_proto) {
9437 		case IPPROTO_TCP:
9438 			pf->fd_tcp6_filter_cnt--;
9439 			break;
9440 		case IPPROTO_UDP:
9441 			pf->fd_udp6_filter_cnt--;
9442 			break;
9443 		case IPPROTO_SCTP:
9444 			pf->fd_sctp6_filter_cnt--;
9445 			break;
9446 		case IPPROTO_IP:
9447 			pf->fd_ip6_filter_cnt--;
9448 			break;
9449 		}
9450 		break;
9451 	}
9452 
9453 	/* Remove the filter from the list and free memory */
9454 	hlist_del(&filter->fdir_node);
9455 	kfree(filter);
9456 }
9457 
9458 /**
9459  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9460  * @pf: board private structure
9461  **/
9462 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9463 {
9464 	struct i40e_fdir_filter *filter;
9465 	u32 fcnt_prog, fcnt_avail;
9466 	struct hlist_node *node;
9467 
9468 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9469 		return;
9470 
9471 	/* Check if we have enough room to re-enable FDir SB capability. */
9472 	fcnt_prog = i40e_get_global_fd_count(pf);
9473 	fcnt_avail = pf->fdir_pf_filter_count;
9474 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9475 	    (pf->fd_add_err == 0) ||
9476 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9477 		i40e_reenable_fdir_sb(pf);
9478 
9479 	/* We should wait for even more space before re-enabling ATR.
9480 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9481 	 * rules active.
9482 	 */
9483 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9484 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9485 		i40e_reenable_fdir_atr(pf);
9486 
9487 	/* if hw had a problem adding a filter, delete it */
9488 	if (pf->fd_inv > 0) {
9489 		hlist_for_each_entry_safe(filter, node,
9490 					  &pf->fdir_filter_list, fdir_node)
9491 			if (filter->fd_id == pf->fd_inv)
9492 				i40e_delete_invalid_filter(pf, filter);
9493 	}
9494 }
9495 
9496 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9497 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9498 /**
9499  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9500  * @pf: board private structure
9501  **/
9502 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9503 {
9504 	unsigned long min_flush_time;
9505 	int flush_wait_retry = 50;
9506 	bool disable_atr = false;
9507 	int fd_room;
9508 	int reg;
9509 
9510 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9511 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9512 		return;
9513 
9514 	/* If the flush is happening too quick and we have mostly SB rules we
9515 	 * should not re-enable ATR for some time.
9516 	 */
9517 	min_flush_time = pf->fd_flush_timestamp +
9518 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9519 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9520 
9521 	if (!(time_after(jiffies, min_flush_time)) &&
9522 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9523 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9524 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9525 		disable_atr = true;
9526 	}
9527 
9528 	pf->fd_flush_timestamp = jiffies;
9529 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9530 	/* flush all filters */
9531 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9532 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9533 	i40e_flush(&pf->hw);
9534 	pf->fd_flush_cnt++;
9535 	pf->fd_add_err = 0;
9536 	do {
9537 		/* Check FD flush status every 5-6msec */
9538 		usleep_range(5000, 6000);
9539 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9540 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9541 			break;
9542 	} while (flush_wait_retry--);
9543 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9544 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9545 	} else {
9546 		/* replay sideband filters */
9547 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9548 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9549 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9550 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9551 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9552 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9553 	}
9554 }
9555 
9556 /**
9557  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9558  * @pf: board private structure
9559  **/
9560 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9561 {
9562 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9563 }
9564 
9565 /**
9566  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9567  * @pf: board private structure
9568  **/
9569 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9570 {
9571 
9572 	/* if interface is down do nothing */
9573 	if (test_bit(__I40E_DOWN, pf->state))
9574 		return;
9575 
9576 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9577 		i40e_fdir_flush_and_replay(pf);
9578 
9579 	i40e_fdir_check_and_reenable(pf);
9580 
9581 }
9582 
9583 /**
9584  * i40e_vsi_link_event - notify VSI of a link event
9585  * @vsi: vsi to be notified
9586  * @link_up: link up or down
9587  **/
9588 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9589 {
9590 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9591 		return;
9592 
9593 	switch (vsi->type) {
9594 	case I40E_VSI_MAIN:
9595 		if (!vsi->netdev || !vsi->netdev_registered)
9596 			break;
9597 
9598 		if (link_up) {
9599 			netif_carrier_on(vsi->netdev);
9600 			netif_tx_wake_all_queues(vsi->netdev);
9601 		} else {
9602 			netif_carrier_off(vsi->netdev);
9603 			netif_tx_stop_all_queues(vsi->netdev);
9604 		}
9605 		break;
9606 
9607 	case I40E_VSI_SRIOV:
9608 	case I40E_VSI_VMDQ2:
9609 	case I40E_VSI_CTRL:
9610 	case I40E_VSI_IWARP:
9611 	case I40E_VSI_MIRROR:
9612 	default:
9613 		/* there is no notification for other VSIs */
9614 		break;
9615 	}
9616 }
9617 
9618 /**
9619  * i40e_veb_link_event - notify elements on the veb of a link event
9620  * @veb: veb to be notified
9621  * @link_up: link up or down
9622  **/
9623 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9624 {
9625 	struct i40e_pf *pf;
9626 	int i;
9627 
9628 	if (!veb || !veb->pf)
9629 		return;
9630 	pf = veb->pf;
9631 
9632 	/* depth first... */
9633 	for (i = 0; i < I40E_MAX_VEB; i++)
9634 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9635 			i40e_veb_link_event(pf->veb[i], link_up);
9636 
9637 	/* ... now the local VSIs */
9638 	for (i = 0; i < pf->num_alloc_vsi; i++)
9639 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9640 			i40e_vsi_link_event(pf->vsi[i], link_up);
9641 }
9642 
9643 /**
9644  * i40e_link_event - Update netif_carrier status
9645  * @pf: board private structure
9646  **/
9647 static void i40e_link_event(struct i40e_pf *pf)
9648 {
9649 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9650 	u8 new_link_speed, old_link_speed;
9651 	i40e_status status;
9652 	bool new_link, old_link;
9653 #ifdef CONFIG_I40E_DCB
9654 	int err;
9655 #endif /* CONFIG_I40E_DCB */
9656 
9657 	/* set this to force the get_link_status call to refresh state */
9658 	pf->hw.phy.get_link_info = true;
9659 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9660 	status = i40e_get_link_status(&pf->hw, &new_link);
9661 
9662 	/* On success, disable temp link polling */
9663 	if (status == I40E_SUCCESS) {
9664 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9665 	} else {
9666 		/* Enable link polling temporarily until i40e_get_link_status
9667 		 * returns I40E_SUCCESS
9668 		 */
9669 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9670 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9671 			status);
9672 		return;
9673 	}
9674 
9675 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9676 	new_link_speed = pf->hw.phy.link_info.link_speed;
9677 
9678 	if (new_link == old_link &&
9679 	    new_link_speed == old_link_speed &&
9680 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9681 	     new_link == netif_carrier_ok(vsi->netdev)))
9682 		return;
9683 
9684 	i40e_print_link_message(vsi, new_link);
9685 
9686 	/* Notify the base of the switch tree connected to
9687 	 * the link.  Floating VEBs are not notified.
9688 	 */
9689 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9690 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9691 	else
9692 		i40e_vsi_link_event(vsi, new_link);
9693 
9694 	if (pf->vf)
9695 		i40e_vc_notify_link_state(pf);
9696 
9697 	if (pf->flags & I40E_FLAG_PTP)
9698 		i40e_ptp_set_increment(pf);
9699 #ifdef CONFIG_I40E_DCB
9700 	if (new_link == old_link)
9701 		return;
9702 	/* Not SW DCB so firmware will take care of default settings */
9703 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9704 		return;
9705 
9706 	/* We cover here only link down, as after link up in case of SW DCB
9707 	 * SW LLDP agent will take care of setting it up
9708 	 */
9709 	if (!new_link) {
9710 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9711 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9712 		err = i40e_dcb_sw_default_config(pf);
9713 		if (err) {
9714 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9715 				       I40E_FLAG_DCB_ENABLED);
9716 		} else {
9717 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9718 				       DCB_CAP_DCBX_VER_IEEE;
9719 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9720 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9721 		}
9722 	}
9723 #endif /* CONFIG_I40E_DCB */
9724 }
9725 
9726 /**
9727  * i40e_watchdog_subtask - periodic checks not using event driven response
9728  * @pf: board private structure
9729  **/
9730 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9731 {
9732 	int i;
9733 
9734 	/* if interface is down do nothing */
9735 	if (test_bit(__I40E_DOWN, pf->state) ||
9736 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9737 		return;
9738 
9739 	/* make sure we don't do these things too often */
9740 	if (time_before(jiffies, (pf->service_timer_previous +
9741 				  pf->service_timer_period)))
9742 		return;
9743 	pf->service_timer_previous = jiffies;
9744 
9745 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9746 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9747 		i40e_link_event(pf);
9748 
9749 	/* Update the stats for active netdevs so the network stack
9750 	 * can look at updated numbers whenever it cares to
9751 	 */
9752 	for (i = 0; i < pf->num_alloc_vsi; i++)
9753 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9754 			i40e_update_stats(pf->vsi[i]);
9755 
9756 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9757 		/* Update the stats for the active switching components */
9758 		for (i = 0; i < I40E_MAX_VEB; i++)
9759 			if (pf->veb[i])
9760 				i40e_update_veb_stats(pf->veb[i]);
9761 	}
9762 
9763 	i40e_ptp_rx_hang(pf);
9764 	i40e_ptp_tx_hang(pf);
9765 }
9766 
9767 /**
9768  * i40e_reset_subtask - Set up for resetting the device and driver
9769  * @pf: board private structure
9770  **/
9771 static void i40e_reset_subtask(struct i40e_pf *pf)
9772 {
9773 	u32 reset_flags = 0;
9774 
9775 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
9776 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
9777 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
9778 	}
9779 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
9780 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
9781 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9782 	}
9783 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
9784 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
9785 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
9786 	}
9787 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
9788 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
9789 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
9790 	}
9791 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
9792 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
9793 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
9794 	}
9795 
9796 	/* If there's a recovery already waiting, it takes
9797 	 * precedence before starting a new reset sequence.
9798 	 */
9799 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
9800 		i40e_prep_for_reset(pf);
9801 		i40e_reset(pf);
9802 		i40e_rebuild(pf, false, false);
9803 	}
9804 
9805 	/* If we're already down or resetting, just bail */
9806 	if (reset_flags &&
9807 	    !test_bit(__I40E_DOWN, pf->state) &&
9808 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
9809 		i40e_do_reset(pf, reset_flags, false);
9810 	}
9811 }
9812 
9813 /**
9814  * i40e_handle_link_event - Handle link event
9815  * @pf: board private structure
9816  * @e: event info posted on ARQ
9817  **/
9818 static void i40e_handle_link_event(struct i40e_pf *pf,
9819 				   struct i40e_arq_event_info *e)
9820 {
9821 	struct i40e_aqc_get_link_status *status =
9822 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
9823 
9824 	/* Do a new status request to re-enable LSE reporting
9825 	 * and load new status information into the hw struct
9826 	 * This completely ignores any state information
9827 	 * in the ARQ event info, instead choosing to always
9828 	 * issue the AQ update link status command.
9829 	 */
9830 	i40e_link_event(pf);
9831 
9832 	/* Check if module meets thermal requirements */
9833 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
9834 		dev_err(&pf->pdev->dev,
9835 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
9836 		dev_err(&pf->pdev->dev,
9837 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9838 	} else {
9839 		/* check for unqualified module, if link is down, suppress
9840 		 * the message if link was forced to be down.
9841 		 */
9842 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
9843 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
9844 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
9845 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
9846 			dev_err(&pf->pdev->dev,
9847 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
9848 			dev_err(&pf->pdev->dev,
9849 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9850 		}
9851 	}
9852 }
9853 
9854 /**
9855  * i40e_clean_adminq_subtask - Clean the AdminQ rings
9856  * @pf: board private structure
9857  **/
9858 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
9859 {
9860 	struct i40e_arq_event_info event;
9861 	struct i40e_hw *hw = &pf->hw;
9862 	u16 pending, i = 0;
9863 	i40e_status ret;
9864 	u16 opcode;
9865 	u32 oldval;
9866 	u32 val;
9867 
9868 	/* Do not run clean AQ when PF reset fails */
9869 	if (test_bit(__I40E_RESET_FAILED, pf->state))
9870 		return;
9871 
9872 	/* check for error indications */
9873 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
9874 	oldval = val;
9875 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
9876 		if (hw->debug_mask & I40E_DEBUG_AQ)
9877 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
9878 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
9879 	}
9880 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
9881 		if (hw->debug_mask & I40E_DEBUG_AQ)
9882 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
9883 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
9884 		pf->arq_overflows++;
9885 	}
9886 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
9887 		if (hw->debug_mask & I40E_DEBUG_AQ)
9888 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
9889 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
9890 	}
9891 	if (oldval != val)
9892 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
9893 
9894 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
9895 	oldval = val;
9896 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
9897 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9898 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
9899 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
9900 	}
9901 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
9902 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9903 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
9904 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
9905 	}
9906 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
9907 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9908 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
9909 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
9910 	}
9911 	if (oldval != val)
9912 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
9913 
9914 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
9915 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
9916 	if (!event.msg_buf)
9917 		return;
9918 
9919 	do {
9920 		ret = i40e_clean_arq_element(hw, &event, &pending);
9921 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
9922 			break;
9923 		else if (ret) {
9924 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
9925 			break;
9926 		}
9927 
9928 		opcode = le16_to_cpu(event.desc.opcode);
9929 		switch (opcode) {
9930 
9931 		case i40e_aqc_opc_get_link_status:
9932 			rtnl_lock();
9933 			i40e_handle_link_event(pf, &event);
9934 			rtnl_unlock();
9935 			break;
9936 		case i40e_aqc_opc_send_msg_to_pf:
9937 			ret = i40e_vc_process_vf_msg(pf,
9938 					le16_to_cpu(event.desc.retval),
9939 					le32_to_cpu(event.desc.cookie_high),
9940 					le32_to_cpu(event.desc.cookie_low),
9941 					event.msg_buf,
9942 					event.msg_len);
9943 			break;
9944 		case i40e_aqc_opc_lldp_update_mib:
9945 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
9946 #ifdef CONFIG_I40E_DCB
9947 			rtnl_lock();
9948 			i40e_handle_lldp_event(pf, &event);
9949 			rtnl_unlock();
9950 #endif /* CONFIG_I40E_DCB */
9951 			break;
9952 		case i40e_aqc_opc_event_lan_overflow:
9953 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
9954 			i40e_handle_lan_overflow_event(pf, &event);
9955 			break;
9956 		case i40e_aqc_opc_send_msg_to_peer:
9957 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
9958 			break;
9959 		case i40e_aqc_opc_nvm_erase:
9960 		case i40e_aqc_opc_nvm_update:
9961 		case i40e_aqc_opc_oem_post_update:
9962 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
9963 				   "ARQ NVM operation 0x%04x completed\n",
9964 				   opcode);
9965 			break;
9966 		default:
9967 			dev_info(&pf->pdev->dev,
9968 				 "ARQ: Unknown event 0x%04x ignored\n",
9969 				 opcode);
9970 			break;
9971 		}
9972 	} while (i++ < pf->adminq_work_limit);
9973 
9974 	if (i < pf->adminq_work_limit)
9975 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
9976 
9977 	/* re-enable Admin queue interrupt cause */
9978 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
9979 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
9980 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
9981 	i40e_flush(hw);
9982 
9983 	kfree(event.msg_buf);
9984 }
9985 
9986 /**
9987  * i40e_verify_eeprom - make sure eeprom is good to use
9988  * @pf: board private structure
9989  **/
9990 static void i40e_verify_eeprom(struct i40e_pf *pf)
9991 {
9992 	int err;
9993 
9994 	err = i40e_diag_eeprom_test(&pf->hw);
9995 	if (err) {
9996 		/* retry in case of garbage read */
9997 		err = i40e_diag_eeprom_test(&pf->hw);
9998 		if (err) {
9999 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10000 				 err);
10001 			set_bit(__I40E_BAD_EEPROM, pf->state);
10002 		}
10003 	}
10004 
10005 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10006 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10007 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10008 	}
10009 }
10010 
10011 /**
10012  * i40e_enable_pf_switch_lb
10013  * @pf: pointer to the PF structure
10014  *
10015  * enable switch loop back or die - no point in a return value
10016  **/
10017 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10018 {
10019 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10020 	struct i40e_vsi_context ctxt;
10021 	int ret;
10022 
10023 	ctxt.seid = pf->main_vsi_seid;
10024 	ctxt.pf_num = pf->hw.pf_id;
10025 	ctxt.vf_num = 0;
10026 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10027 	if (ret) {
10028 		dev_info(&pf->pdev->dev,
10029 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10030 			 i40e_stat_str(&pf->hw, ret),
10031 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10032 		return;
10033 	}
10034 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10035 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10036 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10037 
10038 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10039 	if (ret) {
10040 		dev_info(&pf->pdev->dev,
10041 			 "update vsi switch failed, err %s aq_err %s\n",
10042 			 i40e_stat_str(&pf->hw, ret),
10043 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10044 	}
10045 }
10046 
10047 /**
10048  * i40e_disable_pf_switch_lb
10049  * @pf: pointer to the PF structure
10050  *
10051  * disable switch loop back or die - no point in a return value
10052  **/
10053 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10054 {
10055 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10056 	struct i40e_vsi_context ctxt;
10057 	int ret;
10058 
10059 	ctxt.seid = pf->main_vsi_seid;
10060 	ctxt.pf_num = pf->hw.pf_id;
10061 	ctxt.vf_num = 0;
10062 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10063 	if (ret) {
10064 		dev_info(&pf->pdev->dev,
10065 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10066 			 i40e_stat_str(&pf->hw, ret),
10067 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10068 		return;
10069 	}
10070 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10071 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10072 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10073 
10074 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10075 	if (ret) {
10076 		dev_info(&pf->pdev->dev,
10077 			 "update vsi switch failed, err %s aq_err %s\n",
10078 			 i40e_stat_str(&pf->hw, ret),
10079 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10080 	}
10081 }
10082 
10083 /**
10084  * i40e_config_bridge_mode - Configure the HW bridge mode
10085  * @veb: pointer to the bridge instance
10086  *
10087  * Configure the loop back mode for the LAN VSI that is downlink to the
10088  * specified HW bridge instance. It is expected this function is called
10089  * when a new HW bridge is instantiated.
10090  **/
10091 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10092 {
10093 	struct i40e_pf *pf = veb->pf;
10094 
10095 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10096 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10097 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10098 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10099 		i40e_disable_pf_switch_lb(pf);
10100 	else
10101 		i40e_enable_pf_switch_lb(pf);
10102 }
10103 
10104 /**
10105  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10106  * @veb: pointer to the VEB instance
10107  *
10108  * This is a recursive function that first builds the attached VSIs then
10109  * recurses in to build the next layer of VEB.  We track the connections
10110  * through our own index numbers because the seid's from the HW could
10111  * change across the reset.
10112  **/
10113 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10114 {
10115 	struct i40e_vsi *ctl_vsi = NULL;
10116 	struct i40e_pf *pf = veb->pf;
10117 	int v, veb_idx;
10118 	int ret;
10119 
10120 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10121 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10122 		if (pf->vsi[v] &&
10123 		    pf->vsi[v]->veb_idx == veb->idx &&
10124 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10125 			ctl_vsi = pf->vsi[v];
10126 			break;
10127 		}
10128 	}
10129 	if (!ctl_vsi) {
10130 		dev_info(&pf->pdev->dev,
10131 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10132 		ret = -ENOENT;
10133 		goto end_reconstitute;
10134 	}
10135 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10136 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10137 	ret = i40e_add_vsi(ctl_vsi);
10138 	if (ret) {
10139 		dev_info(&pf->pdev->dev,
10140 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10141 			 veb->idx, ret);
10142 		goto end_reconstitute;
10143 	}
10144 	i40e_vsi_reset_stats(ctl_vsi);
10145 
10146 	/* create the VEB in the switch and move the VSI onto the VEB */
10147 	ret = i40e_add_veb(veb, ctl_vsi);
10148 	if (ret)
10149 		goto end_reconstitute;
10150 
10151 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10152 		veb->bridge_mode = BRIDGE_MODE_VEB;
10153 	else
10154 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10155 	i40e_config_bridge_mode(veb);
10156 
10157 	/* create the remaining VSIs attached to this VEB */
10158 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10159 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10160 			continue;
10161 
10162 		if (pf->vsi[v]->veb_idx == veb->idx) {
10163 			struct i40e_vsi *vsi = pf->vsi[v];
10164 
10165 			vsi->uplink_seid = veb->seid;
10166 			ret = i40e_add_vsi(vsi);
10167 			if (ret) {
10168 				dev_info(&pf->pdev->dev,
10169 					 "rebuild of vsi_idx %d failed: %d\n",
10170 					 v, ret);
10171 				goto end_reconstitute;
10172 			}
10173 			i40e_vsi_reset_stats(vsi);
10174 		}
10175 	}
10176 
10177 	/* create any VEBs attached to this VEB - RECURSION */
10178 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10179 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10180 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10181 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10182 			if (ret)
10183 				break;
10184 		}
10185 	}
10186 
10187 end_reconstitute:
10188 	return ret;
10189 }
10190 
10191 /**
10192  * i40e_get_capabilities - get info about the HW
10193  * @pf: the PF struct
10194  * @list_type: AQ capability to be queried
10195  **/
10196 static int i40e_get_capabilities(struct i40e_pf *pf,
10197 				 enum i40e_admin_queue_opc list_type)
10198 {
10199 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10200 	u16 data_size;
10201 	int buf_len;
10202 	int err;
10203 
10204 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10205 	do {
10206 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10207 		if (!cap_buf)
10208 			return -ENOMEM;
10209 
10210 		/* this loads the data into the hw struct for us */
10211 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10212 						    &data_size, list_type,
10213 						    NULL);
10214 		/* data loaded, buffer no longer needed */
10215 		kfree(cap_buf);
10216 
10217 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10218 			/* retry with a larger buffer */
10219 			buf_len = data_size;
10220 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10221 			dev_info(&pf->pdev->dev,
10222 				 "capability discovery failed, err %s aq_err %s\n",
10223 				 i40e_stat_str(&pf->hw, err),
10224 				 i40e_aq_str(&pf->hw,
10225 					     pf->hw.aq.asq_last_status));
10226 			return -ENODEV;
10227 		}
10228 	} while (err);
10229 
10230 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10231 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10232 			dev_info(&pf->pdev->dev,
10233 				 "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",
10234 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10235 				 pf->hw.func_caps.num_msix_vectors,
10236 				 pf->hw.func_caps.num_msix_vectors_vf,
10237 				 pf->hw.func_caps.fd_filters_guaranteed,
10238 				 pf->hw.func_caps.fd_filters_best_effort,
10239 				 pf->hw.func_caps.num_tx_qp,
10240 				 pf->hw.func_caps.num_vsis);
10241 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10242 			dev_info(&pf->pdev->dev,
10243 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10244 				 pf->hw.dev_caps.switch_mode,
10245 				 pf->hw.dev_caps.valid_functions);
10246 			dev_info(&pf->pdev->dev,
10247 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10248 				 pf->hw.dev_caps.sr_iov_1_1,
10249 				 pf->hw.dev_caps.num_vfs);
10250 			dev_info(&pf->pdev->dev,
10251 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10252 				 pf->hw.dev_caps.num_vsis,
10253 				 pf->hw.dev_caps.num_rx_qp,
10254 				 pf->hw.dev_caps.num_tx_qp);
10255 		}
10256 	}
10257 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10258 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10259 		       + pf->hw.func_caps.num_vfs)
10260 		if (pf->hw.revision_id == 0 &&
10261 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10262 			dev_info(&pf->pdev->dev,
10263 				 "got num_vsis %d, setting num_vsis to %d\n",
10264 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10265 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10266 		}
10267 	}
10268 	return 0;
10269 }
10270 
10271 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10272 
10273 /**
10274  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10275  * @pf: board private structure
10276  **/
10277 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10278 {
10279 	struct i40e_vsi *vsi;
10280 
10281 	/* quick workaround for an NVM issue that leaves a critical register
10282 	 * uninitialized
10283 	 */
10284 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10285 		static const u32 hkey[] = {
10286 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10287 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10288 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10289 			0x95b3a76d};
10290 		int i;
10291 
10292 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10293 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10294 	}
10295 
10296 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10297 		return;
10298 
10299 	/* find existing VSI and see if it needs configuring */
10300 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10301 
10302 	/* create a new VSI if none exists */
10303 	if (!vsi) {
10304 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10305 				     pf->vsi[pf->lan_vsi]->seid, 0);
10306 		if (!vsi) {
10307 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10308 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10309 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10310 			return;
10311 		}
10312 	}
10313 
10314 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10315 }
10316 
10317 /**
10318  * i40e_fdir_teardown - release the Flow Director resources
10319  * @pf: board private structure
10320  **/
10321 static void i40e_fdir_teardown(struct i40e_pf *pf)
10322 {
10323 	struct i40e_vsi *vsi;
10324 
10325 	i40e_fdir_filter_exit(pf);
10326 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10327 	if (vsi)
10328 		i40e_vsi_release(vsi);
10329 }
10330 
10331 /**
10332  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10333  * @vsi: PF main vsi
10334  * @seid: seid of main or channel VSIs
10335  *
10336  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10337  * existed before reset
10338  **/
10339 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10340 {
10341 	struct i40e_cloud_filter *cfilter;
10342 	struct i40e_pf *pf = vsi->back;
10343 	struct hlist_node *node;
10344 	i40e_status ret;
10345 
10346 	/* Add cloud filters back if they exist */
10347 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10348 				  cloud_node) {
10349 		if (cfilter->seid != seid)
10350 			continue;
10351 
10352 		if (cfilter->dst_port)
10353 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10354 								true);
10355 		else
10356 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10357 
10358 		if (ret) {
10359 			dev_dbg(&pf->pdev->dev,
10360 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
10361 				i40e_stat_str(&pf->hw, ret),
10362 				i40e_aq_str(&pf->hw,
10363 					    pf->hw.aq.asq_last_status));
10364 			return ret;
10365 		}
10366 	}
10367 	return 0;
10368 }
10369 
10370 /**
10371  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10372  * @vsi: PF main vsi
10373  *
10374  * Rebuilds channel VSIs if they existed before reset
10375  **/
10376 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10377 {
10378 	struct i40e_channel *ch, *ch_tmp;
10379 	i40e_status ret;
10380 
10381 	if (list_empty(&vsi->ch_list))
10382 		return 0;
10383 
10384 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10385 		if (!ch->initialized)
10386 			break;
10387 		/* Proceed with creation of channel (VMDq2) VSI */
10388 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10389 		if (ret) {
10390 			dev_info(&vsi->back->pdev->dev,
10391 				 "failed to rebuild channels using uplink_seid %u\n",
10392 				 vsi->uplink_seid);
10393 			return ret;
10394 		}
10395 		/* Reconfigure TX queues using QTX_CTL register */
10396 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10397 		if (ret) {
10398 			dev_info(&vsi->back->pdev->dev,
10399 				 "failed to configure TX rings for channel %u\n",
10400 				 ch->seid);
10401 			return ret;
10402 		}
10403 		/* update 'next_base_queue' */
10404 		vsi->next_base_queue = vsi->next_base_queue +
10405 							ch->num_queue_pairs;
10406 		if (ch->max_tx_rate) {
10407 			u64 credits = ch->max_tx_rate;
10408 
10409 			if (i40e_set_bw_limit(vsi, ch->seid,
10410 					      ch->max_tx_rate))
10411 				return -EINVAL;
10412 
10413 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10414 			dev_dbg(&vsi->back->pdev->dev,
10415 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10416 				ch->max_tx_rate,
10417 				credits,
10418 				ch->seid);
10419 		}
10420 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10421 		if (ret) {
10422 			dev_dbg(&vsi->back->pdev->dev,
10423 				"Failed to rebuild cloud filters for channel VSI %u\n",
10424 				ch->seid);
10425 			return ret;
10426 		}
10427 	}
10428 	return 0;
10429 }
10430 
10431 /**
10432  * i40e_prep_for_reset - prep for the core to reset
10433  * @pf: board private structure
10434  *
10435  * Close up the VFs and other things in prep for PF Reset.
10436   **/
10437 static void i40e_prep_for_reset(struct i40e_pf *pf)
10438 {
10439 	struct i40e_hw *hw = &pf->hw;
10440 	i40e_status ret = 0;
10441 	u32 v;
10442 
10443 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10444 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10445 		return;
10446 	if (i40e_check_asq_alive(&pf->hw))
10447 		i40e_vc_notify_reset(pf);
10448 
10449 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10450 
10451 	/* quiesce the VSIs and their queues that are not already DOWN */
10452 	i40e_pf_quiesce_all_vsi(pf);
10453 
10454 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10455 		if (pf->vsi[v])
10456 			pf->vsi[v]->seid = 0;
10457 	}
10458 
10459 	i40e_shutdown_adminq(&pf->hw);
10460 
10461 	/* call shutdown HMC */
10462 	if (hw->hmc.hmc_obj) {
10463 		ret = i40e_shutdown_lan_hmc(hw);
10464 		if (ret)
10465 			dev_warn(&pf->pdev->dev,
10466 				 "shutdown_lan_hmc failed: %d\n", ret);
10467 	}
10468 
10469 	/* Save the current PTP time so that we can restore the time after the
10470 	 * reset completes.
10471 	 */
10472 	i40e_ptp_save_hw_time(pf);
10473 }
10474 
10475 /**
10476  * i40e_send_version - update firmware with driver version
10477  * @pf: PF struct
10478  */
10479 static void i40e_send_version(struct i40e_pf *pf)
10480 {
10481 	struct i40e_driver_version dv;
10482 
10483 	dv.major_version = 0xff;
10484 	dv.minor_version = 0xff;
10485 	dv.build_version = 0xff;
10486 	dv.subbuild_version = 0;
10487 	strlcpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10488 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10489 }
10490 
10491 /**
10492  * i40e_get_oem_version - get OEM specific version information
10493  * @hw: pointer to the hardware structure
10494  **/
10495 static void i40e_get_oem_version(struct i40e_hw *hw)
10496 {
10497 	u16 block_offset = 0xffff;
10498 	u16 block_length = 0;
10499 	u16 capabilities = 0;
10500 	u16 gen_snap = 0;
10501 	u16 release = 0;
10502 
10503 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10504 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10505 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10506 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10507 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10508 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10509 #define I40E_NVM_OEM_LENGTH			3
10510 
10511 	/* Check if pointer to OEM version block is valid. */
10512 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10513 	if (block_offset == 0xffff)
10514 		return;
10515 
10516 	/* Check if OEM version block has correct length. */
10517 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10518 			   &block_length);
10519 	if (block_length < I40E_NVM_OEM_LENGTH)
10520 		return;
10521 
10522 	/* Check if OEM version format is as expected. */
10523 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10524 			   &capabilities);
10525 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10526 		return;
10527 
10528 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10529 			   &gen_snap);
10530 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10531 			   &release);
10532 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10533 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10534 }
10535 
10536 /**
10537  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10538  * @pf: board private structure
10539  **/
10540 static int i40e_reset(struct i40e_pf *pf)
10541 {
10542 	struct i40e_hw *hw = &pf->hw;
10543 	i40e_status ret;
10544 
10545 	ret = i40e_pf_reset(hw);
10546 	if (ret) {
10547 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10548 		set_bit(__I40E_RESET_FAILED, pf->state);
10549 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10550 	} else {
10551 		pf->pfr_count++;
10552 	}
10553 	return ret;
10554 }
10555 
10556 /**
10557  * i40e_rebuild - rebuild using a saved config
10558  * @pf: board private structure
10559  * @reinit: if the Main VSI needs to re-initialized.
10560  * @lock_acquired: indicates whether or not the lock has been acquired
10561  * before this function was called.
10562  **/
10563 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10564 {
10565 	int old_recovery_mode_bit = test_bit(__I40E_RECOVERY_MODE, pf->state);
10566 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10567 	struct i40e_hw *hw = &pf->hw;
10568 	i40e_status ret;
10569 	u32 val;
10570 	int v;
10571 
10572 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10573 	    i40e_check_recovery_mode(pf)) {
10574 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10575 	}
10576 
10577 	if (test_bit(__I40E_DOWN, pf->state) &&
10578 	    !test_bit(__I40E_RECOVERY_MODE, pf->state) &&
10579 	    !old_recovery_mode_bit)
10580 		goto clear_recovery;
10581 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10582 
10583 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10584 	ret = i40e_init_adminq(&pf->hw);
10585 	if (ret) {
10586 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10587 			 i40e_stat_str(&pf->hw, ret),
10588 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10589 		goto clear_recovery;
10590 	}
10591 	i40e_get_oem_version(&pf->hw);
10592 
10593 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10594 		/* The following delay is necessary for firmware update. */
10595 		mdelay(1000);
10596 	}
10597 
10598 	/* re-verify the eeprom if we just had an EMP reset */
10599 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10600 		i40e_verify_eeprom(pf);
10601 
10602 	/* if we are going out of or into recovery mode we have to act
10603 	 * accordingly with regard to resources initialization
10604 	 * and deinitialization
10605 	 */
10606 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) ||
10607 	    old_recovery_mode_bit) {
10608 		if (i40e_get_capabilities(pf,
10609 					  i40e_aqc_opc_list_func_capabilities))
10610 			goto end_unlock;
10611 
10612 		if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10613 			/* we're staying in recovery mode so we'll reinitialize
10614 			 * misc vector here
10615 			 */
10616 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10617 				goto end_unlock;
10618 		} else {
10619 			if (!lock_acquired)
10620 				rtnl_lock();
10621 			/* we're going out of recovery mode so we'll free
10622 			 * the IRQ allocated specifically for recovery mode
10623 			 * and restore the interrupt scheme
10624 			 */
10625 			free_irq(pf->pdev->irq, pf);
10626 			i40e_clear_interrupt_scheme(pf);
10627 			if (i40e_restore_interrupt_scheme(pf))
10628 				goto end_unlock;
10629 		}
10630 
10631 		/* tell the firmware that we're starting */
10632 		i40e_send_version(pf);
10633 
10634 		/* bail out in case recovery mode was detected, as there is
10635 		 * no need for further configuration.
10636 		 */
10637 		goto end_unlock;
10638 	}
10639 
10640 	i40e_clear_pxe_mode(hw);
10641 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10642 	if (ret)
10643 		goto end_core_reset;
10644 
10645 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10646 				hw->func_caps.num_rx_qp, 0, 0);
10647 	if (ret) {
10648 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10649 		goto end_core_reset;
10650 	}
10651 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10652 	if (ret) {
10653 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10654 		goto end_core_reset;
10655 	}
10656 
10657 #ifdef CONFIG_I40E_DCB
10658 	/* Enable FW to write a default DCB config on link-up
10659 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10660 	 * is not supported with new link speed
10661 	 */
10662 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
10663 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10664 	} else {
10665 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10666 		    (hw->phy.link_info.link_speed &
10667 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10668 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10669 			dev_warn(&pf->pdev->dev,
10670 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10671 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10672 		} else {
10673 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10674 			ret = i40e_init_pf_dcb(pf);
10675 			if (ret) {
10676 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10677 					 ret);
10678 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10679 				/* Continue without DCB enabled */
10680 			}
10681 		}
10682 	}
10683 
10684 #endif /* CONFIG_I40E_DCB */
10685 	if (!lock_acquired)
10686 		rtnl_lock();
10687 	ret = i40e_setup_pf_switch(pf, reinit, true);
10688 	if (ret)
10689 		goto end_unlock;
10690 
10691 	/* The driver only wants link up/down and module qualification
10692 	 * reports from firmware.  Note the negative logic.
10693 	 */
10694 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10695 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10696 					 I40E_AQ_EVENT_MEDIA_NA |
10697 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10698 	if (ret)
10699 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10700 			 i40e_stat_str(&pf->hw, ret),
10701 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10702 
10703 	/* Rebuild the VSIs and VEBs that existed before reset.
10704 	 * They are still in our local switch element arrays, so only
10705 	 * need to rebuild the switch model in the HW.
10706 	 *
10707 	 * If there were VEBs but the reconstitution failed, we'll try
10708 	 * to recover minimal use by getting the basic PF VSI working.
10709 	 */
10710 	if (vsi->uplink_seid != pf->mac_seid) {
10711 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10712 		/* find the one VEB connected to the MAC, and find orphans */
10713 		for (v = 0; v < I40E_MAX_VEB; v++) {
10714 			if (!pf->veb[v])
10715 				continue;
10716 
10717 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10718 			    pf->veb[v]->uplink_seid == 0) {
10719 				ret = i40e_reconstitute_veb(pf->veb[v]);
10720 
10721 				if (!ret)
10722 					continue;
10723 
10724 				/* If Main VEB failed, we're in deep doodoo,
10725 				 * so give up rebuilding the switch and set up
10726 				 * for minimal rebuild of PF VSI.
10727 				 * If orphan failed, we'll report the error
10728 				 * but try to keep going.
10729 				 */
10730 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10731 					dev_info(&pf->pdev->dev,
10732 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10733 						 ret);
10734 					vsi->uplink_seid = pf->mac_seid;
10735 					break;
10736 				} else if (pf->veb[v]->uplink_seid == 0) {
10737 					dev_info(&pf->pdev->dev,
10738 						 "rebuild of orphan VEB failed: %d\n",
10739 						 ret);
10740 				}
10741 			}
10742 		}
10743 	}
10744 
10745 	if (vsi->uplink_seid == pf->mac_seid) {
10746 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10747 		/* no VEB, so rebuild only the Main VSI */
10748 		ret = i40e_add_vsi(vsi);
10749 		if (ret) {
10750 			dev_info(&pf->pdev->dev,
10751 				 "rebuild of Main VSI failed: %d\n", ret);
10752 			goto end_unlock;
10753 		}
10754 	}
10755 
10756 	if (vsi->mqprio_qopt.max_rate[0]) {
10757 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
10758 		u64 credits = 0;
10759 
10760 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
10761 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10762 		if (ret)
10763 			goto end_unlock;
10764 
10765 		credits = max_tx_rate;
10766 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10767 		dev_dbg(&vsi->back->pdev->dev,
10768 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10769 			max_tx_rate,
10770 			credits,
10771 			vsi->seid);
10772 	}
10773 
10774 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10775 	if (ret)
10776 		goto end_unlock;
10777 
10778 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
10779 	 * for this main VSI if they exist
10780 	 */
10781 	ret = i40e_rebuild_channels(vsi);
10782 	if (ret)
10783 		goto end_unlock;
10784 
10785 	/* Reconfigure hardware for allowing smaller MSS in the case
10786 	 * of TSO, so that we avoid the MDD being fired and causing
10787 	 * a reset in the case of small MSS+TSO.
10788 	 */
10789 #define I40E_REG_MSS          0x000E64DC
10790 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
10791 #define I40E_64BYTE_MSS       0x400000
10792 	val = rd32(hw, I40E_REG_MSS);
10793 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10794 		val &= ~I40E_REG_MSS_MIN_MASK;
10795 		val |= I40E_64BYTE_MSS;
10796 		wr32(hw, I40E_REG_MSS, val);
10797 	}
10798 
10799 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
10800 		msleep(75);
10801 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10802 		if (ret)
10803 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10804 				 i40e_stat_str(&pf->hw, ret),
10805 				 i40e_aq_str(&pf->hw,
10806 					     pf->hw.aq.asq_last_status));
10807 	}
10808 	/* reinit the misc interrupt */
10809 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10810 		ret = i40e_setup_misc_vector(pf);
10811 
10812 	/* Add a filter to drop all Flow control frames from any VSI from being
10813 	 * transmitted. By doing so we stop a malicious VF from sending out
10814 	 * PAUSE or PFC frames and potentially controlling traffic for other
10815 	 * PF/VF VSIs.
10816 	 * The FW can still send Flow control frames if enabled.
10817 	 */
10818 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
10819 						       pf->main_vsi_seid);
10820 
10821 	/* restart the VSIs that were rebuilt and running before the reset */
10822 	i40e_pf_unquiesce_all_vsi(pf);
10823 
10824 	/* Release the RTNL lock before we start resetting VFs */
10825 	if (!lock_acquired)
10826 		rtnl_unlock();
10827 
10828 	/* Restore promiscuous settings */
10829 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
10830 	if (ret)
10831 		dev_warn(&pf->pdev->dev,
10832 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
10833 			 pf->cur_promisc ? "on" : "off",
10834 			 i40e_stat_str(&pf->hw, ret),
10835 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10836 
10837 	i40e_reset_all_vfs(pf, true);
10838 
10839 	/* tell the firmware that we're starting */
10840 	i40e_send_version(pf);
10841 
10842 	/* We've already released the lock, so don't do it again */
10843 	goto end_core_reset;
10844 
10845 end_unlock:
10846 	if (!lock_acquired)
10847 		rtnl_unlock();
10848 end_core_reset:
10849 	clear_bit(__I40E_RESET_FAILED, pf->state);
10850 clear_recovery:
10851 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10852 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
10853 }
10854 
10855 /**
10856  * i40e_reset_and_rebuild - reset and rebuild using a saved config
10857  * @pf: board private structure
10858  * @reinit: if the Main VSI needs to re-initialized.
10859  * @lock_acquired: indicates whether or not the lock has been acquired
10860  * before this function was called.
10861  **/
10862 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
10863 				   bool lock_acquired)
10864 {
10865 	int ret;
10866 
10867 	if (test_bit(__I40E_IN_REMOVE, pf->state))
10868 		return;
10869 	/* Now we wait for GRST to settle out.
10870 	 * We don't have to delete the VEBs or VSIs from the hw switch
10871 	 * because the reset will make them disappear.
10872 	 */
10873 	ret = i40e_reset(pf);
10874 	if (!ret)
10875 		i40e_rebuild(pf, reinit, lock_acquired);
10876 }
10877 
10878 /**
10879  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
10880  * @pf: board private structure
10881  *
10882  * Close up the VFs and other things in prep for a Core Reset,
10883  * then get ready to rebuild the world.
10884  * @lock_acquired: indicates whether or not the lock has been acquired
10885  * before this function was called.
10886  **/
10887 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
10888 {
10889 	i40e_prep_for_reset(pf);
10890 	i40e_reset_and_rebuild(pf, false, lock_acquired);
10891 }
10892 
10893 /**
10894  * i40e_handle_mdd_event
10895  * @pf: pointer to the PF structure
10896  *
10897  * Called from the MDD irq handler to identify possibly malicious vfs
10898  **/
10899 static void i40e_handle_mdd_event(struct i40e_pf *pf)
10900 {
10901 	struct i40e_hw *hw = &pf->hw;
10902 	bool mdd_detected = false;
10903 	struct i40e_vf *vf;
10904 	u32 reg;
10905 	int i;
10906 
10907 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
10908 		return;
10909 
10910 	/* find what triggered the MDD event */
10911 	reg = rd32(hw, I40E_GL_MDET_TX);
10912 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
10913 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
10914 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
10915 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
10916 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
10917 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
10918 				I40E_GL_MDET_TX_EVENT_SHIFT;
10919 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
10920 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
10921 				pf->hw.func_caps.base_queue;
10922 		if (netif_msg_tx_err(pf))
10923 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
10924 				 event, queue, pf_num, vf_num);
10925 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
10926 		mdd_detected = true;
10927 	}
10928 	reg = rd32(hw, I40E_GL_MDET_RX);
10929 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
10930 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
10931 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
10932 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
10933 				I40E_GL_MDET_RX_EVENT_SHIFT;
10934 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
10935 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
10936 				pf->hw.func_caps.base_queue;
10937 		if (netif_msg_rx_err(pf))
10938 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
10939 				 event, queue, func);
10940 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
10941 		mdd_detected = true;
10942 	}
10943 
10944 	if (mdd_detected) {
10945 		reg = rd32(hw, I40E_PF_MDET_TX);
10946 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
10947 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
10948 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
10949 		}
10950 		reg = rd32(hw, I40E_PF_MDET_RX);
10951 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
10952 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
10953 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
10954 		}
10955 	}
10956 
10957 	/* see if one of the VFs needs its hand slapped */
10958 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
10959 		vf = &(pf->vf[i]);
10960 		reg = rd32(hw, I40E_VP_MDET_TX(i));
10961 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
10962 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
10963 			vf->num_mdd_events++;
10964 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
10965 				 i);
10966 			dev_info(&pf->pdev->dev,
10967 				 "Use PF Control I/F to re-enable the VF\n");
10968 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10969 		}
10970 
10971 		reg = rd32(hw, I40E_VP_MDET_RX(i));
10972 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
10973 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
10974 			vf->num_mdd_events++;
10975 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
10976 				 i);
10977 			dev_info(&pf->pdev->dev,
10978 				 "Use PF Control I/F to re-enable the VF\n");
10979 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10980 		}
10981 	}
10982 
10983 	/* re-enable mdd interrupt cause */
10984 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
10985 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
10986 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
10987 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
10988 	i40e_flush(hw);
10989 }
10990 
10991 /**
10992  * i40e_service_task - Run the driver's async subtasks
10993  * @work: pointer to work_struct containing our data
10994  **/
10995 static void i40e_service_task(struct work_struct *work)
10996 {
10997 	struct i40e_pf *pf = container_of(work,
10998 					  struct i40e_pf,
10999 					  service_task);
11000 	unsigned long start_time = jiffies;
11001 
11002 	/* don't bother with service tasks if a reset is in progress */
11003 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11004 	    test_bit(__I40E_SUSPENDED, pf->state))
11005 		return;
11006 
11007 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11008 		return;
11009 
11010 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11011 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11012 		i40e_sync_filters_subtask(pf);
11013 		i40e_reset_subtask(pf);
11014 		i40e_handle_mdd_event(pf);
11015 		i40e_vc_process_vflr_event(pf);
11016 		i40e_watchdog_subtask(pf);
11017 		i40e_fdir_reinit_subtask(pf);
11018 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11019 			/* Client subtask will reopen next time through. */
11020 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11021 							   true);
11022 		} else {
11023 			i40e_client_subtask(pf);
11024 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11025 					       pf->state))
11026 				i40e_notify_client_of_l2_param_changes(
11027 								pf->vsi[pf->lan_vsi]);
11028 		}
11029 		i40e_sync_filters_subtask(pf);
11030 	} else {
11031 		i40e_reset_subtask(pf);
11032 	}
11033 
11034 	i40e_clean_adminq_subtask(pf);
11035 
11036 	/* flush memory to make sure state is correct before next watchdog */
11037 	smp_mb__before_atomic();
11038 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11039 
11040 	/* If the tasks have taken longer than one timer cycle or there
11041 	 * is more work to be done, reschedule the service task now
11042 	 * rather than wait for the timer to tick again.
11043 	 */
11044 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11045 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11046 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11047 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11048 		i40e_service_event_schedule(pf);
11049 }
11050 
11051 /**
11052  * i40e_service_timer - timer callback
11053  * @t: timer list pointer
11054  **/
11055 static void i40e_service_timer(struct timer_list *t)
11056 {
11057 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11058 
11059 	mod_timer(&pf->service_timer,
11060 		  round_jiffies(jiffies + pf->service_timer_period));
11061 	i40e_service_event_schedule(pf);
11062 }
11063 
11064 /**
11065  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11066  * @vsi: the VSI being configured
11067  **/
11068 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11069 {
11070 	struct i40e_pf *pf = vsi->back;
11071 
11072 	switch (vsi->type) {
11073 	case I40E_VSI_MAIN:
11074 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11075 		if (!vsi->num_tx_desc)
11076 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11077 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11078 		if (!vsi->num_rx_desc)
11079 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11080 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11081 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11082 			vsi->num_q_vectors = pf->num_lan_msix;
11083 		else
11084 			vsi->num_q_vectors = 1;
11085 
11086 		break;
11087 
11088 	case I40E_VSI_FDIR:
11089 		vsi->alloc_queue_pairs = 1;
11090 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11091 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11092 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11093 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11094 		vsi->num_q_vectors = pf->num_fdsb_msix;
11095 		break;
11096 
11097 	case I40E_VSI_VMDQ2:
11098 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11099 		if (!vsi->num_tx_desc)
11100 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11101 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11102 		if (!vsi->num_rx_desc)
11103 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11104 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11105 		vsi->num_q_vectors = pf->num_vmdq_msix;
11106 		break;
11107 
11108 	case I40E_VSI_SRIOV:
11109 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11110 		if (!vsi->num_tx_desc)
11111 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11112 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11113 		if (!vsi->num_rx_desc)
11114 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11115 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11116 		break;
11117 
11118 	default:
11119 		WARN_ON(1);
11120 		return -ENODATA;
11121 	}
11122 
11123 	if (is_kdump_kernel()) {
11124 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11125 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11126 	}
11127 
11128 	return 0;
11129 }
11130 
11131 /**
11132  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11133  * @vsi: VSI pointer
11134  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11135  *
11136  * On error: returns error code (negative)
11137  * On success: returns 0
11138  **/
11139 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11140 {
11141 	struct i40e_ring **next_rings;
11142 	int size;
11143 	int ret = 0;
11144 
11145 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11146 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11147 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11148 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11149 	if (!vsi->tx_rings)
11150 		return -ENOMEM;
11151 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11152 	if (i40e_enabled_xdp_vsi(vsi)) {
11153 		vsi->xdp_rings = next_rings;
11154 		next_rings += vsi->alloc_queue_pairs;
11155 	}
11156 	vsi->rx_rings = next_rings;
11157 
11158 	if (alloc_qvectors) {
11159 		/* allocate memory for q_vector pointers */
11160 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11161 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11162 		if (!vsi->q_vectors) {
11163 			ret = -ENOMEM;
11164 			goto err_vectors;
11165 		}
11166 	}
11167 	return ret;
11168 
11169 err_vectors:
11170 	kfree(vsi->tx_rings);
11171 	return ret;
11172 }
11173 
11174 /**
11175  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11176  * @pf: board private structure
11177  * @type: type of VSI
11178  *
11179  * On error: returns error code (negative)
11180  * On success: returns vsi index in PF (positive)
11181  **/
11182 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11183 {
11184 	int ret = -ENODEV;
11185 	struct i40e_vsi *vsi;
11186 	int vsi_idx;
11187 	int i;
11188 
11189 	/* Need to protect the allocation of the VSIs at the PF level */
11190 	mutex_lock(&pf->switch_mutex);
11191 
11192 	/* VSI list may be fragmented if VSI creation/destruction has
11193 	 * been happening.  We can afford to do a quick scan to look
11194 	 * for any free VSIs in the list.
11195 	 *
11196 	 * find next empty vsi slot, looping back around if necessary
11197 	 */
11198 	i = pf->next_vsi;
11199 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11200 		i++;
11201 	if (i >= pf->num_alloc_vsi) {
11202 		i = 0;
11203 		while (i < pf->next_vsi && pf->vsi[i])
11204 			i++;
11205 	}
11206 
11207 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11208 		vsi_idx = i;             /* Found one! */
11209 	} else {
11210 		ret = -ENODEV;
11211 		goto unlock_pf;  /* out of VSI slots! */
11212 	}
11213 	pf->next_vsi = ++i;
11214 
11215 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11216 	if (!vsi) {
11217 		ret = -ENOMEM;
11218 		goto unlock_pf;
11219 	}
11220 	vsi->type = type;
11221 	vsi->back = pf;
11222 	set_bit(__I40E_VSI_DOWN, vsi->state);
11223 	vsi->flags = 0;
11224 	vsi->idx = vsi_idx;
11225 	vsi->int_rate_limit = 0;
11226 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11227 				pf->rss_table_size : 64;
11228 	vsi->netdev_registered = false;
11229 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11230 	hash_init(vsi->mac_filter_hash);
11231 	vsi->irqs_ready = false;
11232 
11233 	if (type == I40E_VSI_MAIN) {
11234 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11235 		if (!vsi->af_xdp_zc_qps)
11236 			goto err_rings;
11237 	}
11238 
11239 	ret = i40e_set_num_rings_in_vsi(vsi);
11240 	if (ret)
11241 		goto err_rings;
11242 
11243 	ret = i40e_vsi_alloc_arrays(vsi, true);
11244 	if (ret)
11245 		goto err_rings;
11246 
11247 	/* Setup default MSIX irq handler for VSI */
11248 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11249 
11250 	/* Initialize VSI lock */
11251 	spin_lock_init(&vsi->mac_filter_hash_lock);
11252 	pf->vsi[vsi_idx] = vsi;
11253 	ret = vsi_idx;
11254 	goto unlock_pf;
11255 
11256 err_rings:
11257 	bitmap_free(vsi->af_xdp_zc_qps);
11258 	pf->next_vsi = i - 1;
11259 	kfree(vsi);
11260 unlock_pf:
11261 	mutex_unlock(&pf->switch_mutex);
11262 	return ret;
11263 }
11264 
11265 /**
11266  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11267  * @vsi: VSI pointer
11268  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11269  *
11270  * On error: returns error code (negative)
11271  * On success: returns 0
11272  **/
11273 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11274 {
11275 	/* free the ring and vector containers */
11276 	if (free_qvectors) {
11277 		kfree(vsi->q_vectors);
11278 		vsi->q_vectors = NULL;
11279 	}
11280 	kfree(vsi->tx_rings);
11281 	vsi->tx_rings = NULL;
11282 	vsi->rx_rings = NULL;
11283 	vsi->xdp_rings = NULL;
11284 }
11285 
11286 /**
11287  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11288  * and lookup table
11289  * @vsi: Pointer to VSI structure
11290  */
11291 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11292 {
11293 	if (!vsi)
11294 		return;
11295 
11296 	kfree(vsi->rss_hkey_user);
11297 	vsi->rss_hkey_user = NULL;
11298 
11299 	kfree(vsi->rss_lut_user);
11300 	vsi->rss_lut_user = NULL;
11301 }
11302 
11303 /**
11304  * i40e_vsi_clear - Deallocate the VSI provided
11305  * @vsi: the VSI being un-configured
11306  **/
11307 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11308 {
11309 	struct i40e_pf *pf;
11310 
11311 	if (!vsi)
11312 		return 0;
11313 
11314 	if (!vsi->back)
11315 		goto free_vsi;
11316 	pf = vsi->back;
11317 
11318 	mutex_lock(&pf->switch_mutex);
11319 	if (!pf->vsi[vsi->idx]) {
11320 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11321 			vsi->idx, vsi->idx, vsi->type);
11322 		goto unlock_vsi;
11323 	}
11324 
11325 	if (pf->vsi[vsi->idx] != vsi) {
11326 		dev_err(&pf->pdev->dev,
11327 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11328 			pf->vsi[vsi->idx]->idx,
11329 			pf->vsi[vsi->idx]->type,
11330 			vsi->idx, vsi->type);
11331 		goto unlock_vsi;
11332 	}
11333 
11334 	/* updates the PF for this cleared vsi */
11335 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11336 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11337 
11338 	bitmap_free(vsi->af_xdp_zc_qps);
11339 	i40e_vsi_free_arrays(vsi, true);
11340 	i40e_clear_rss_config_user(vsi);
11341 
11342 	pf->vsi[vsi->idx] = NULL;
11343 	if (vsi->idx < pf->next_vsi)
11344 		pf->next_vsi = vsi->idx;
11345 
11346 unlock_vsi:
11347 	mutex_unlock(&pf->switch_mutex);
11348 free_vsi:
11349 	kfree(vsi);
11350 
11351 	return 0;
11352 }
11353 
11354 /**
11355  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11356  * @vsi: the VSI being cleaned
11357  **/
11358 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11359 {
11360 	int i;
11361 
11362 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11363 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11364 			kfree_rcu(vsi->tx_rings[i], rcu);
11365 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11366 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11367 			if (vsi->xdp_rings)
11368 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11369 		}
11370 	}
11371 }
11372 
11373 /**
11374  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11375  * @vsi: the VSI being configured
11376  **/
11377 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11378 {
11379 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11380 	struct i40e_pf *pf = vsi->back;
11381 	struct i40e_ring *ring;
11382 
11383 	/* Set basic values in the rings to be used later during open() */
11384 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11385 		/* allocate space for both Tx and Rx in one shot */
11386 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11387 		if (!ring)
11388 			goto err_out;
11389 
11390 		ring->queue_index = i;
11391 		ring->reg_idx = vsi->base_queue + i;
11392 		ring->ring_active = false;
11393 		ring->vsi = vsi;
11394 		ring->netdev = vsi->netdev;
11395 		ring->dev = &pf->pdev->dev;
11396 		ring->count = vsi->num_tx_desc;
11397 		ring->size = 0;
11398 		ring->dcb_tc = 0;
11399 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11400 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11401 		ring->itr_setting = pf->tx_itr_default;
11402 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11403 
11404 		if (!i40e_enabled_xdp_vsi(vsi))
11405 			goto setup_rx;
11406 
11407 		ring->queue_index = vsi->alloc_queue_pairs + i;
11408 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11409 		ring->ring_active = false;
11410 		ring->vsi = vsi;
11411 		ring->netdev = NULL;
11412 		ring->dev = &pf->pdev->dev;
11413 		ring->count = vsi->num_tx_desc;
11414 		ring->size = 0;
11415 		ring->dcb_tc = 0;
11416 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11417 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11418 		set_ring_xdp(ring);
11419 		ring->itr_setting = pf->tx_itr_default;
11420 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11421 
11422 setup_rx:
11423 		ring->queue_index = i;
11424 		ring->reg_idx = vsi->base_queue + i;
11425 		ring->ring_active = false;
11426 		ring->vsi = vsi;
11427 		ring->netdev = vsi->netdev;
11428 		ring->dev = &pf->pdev->dev;
11429 		ring->count = vsi->num_rx_desc;
11430 		ring->size = 0;
11431 		ring->dcb_tc = 0;
11432 		ring->itr_setting = pf->rx_itr_default;
11433 		WRITE_ONCE(vsi->rx_rings[i], ring);
11434 	}
11435 
11436 	return 0;
11437 
11438 err_out:
11439 	i40e_vsi_clear_rings(vsi);
11440 	return -ENOMEM;
11441 }
11442 
11443 /**
11444  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11445  * @pf: board private structure
11446  * @vectors: the number of MSI-X vectors to request
11447  *
11448  * Returns the number of vectors reserved, or error
11449  **/
11450 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11451 {
11452 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11453 					I40E_MIN_MSIX, vectors);
11454 	if (vectors < 0) {
11455 		dev_info(&pf->pdev->dev,
11456 			 "MSI-X vector reservation failed: %d\n", vectors);
11457 		vectors = 0;
11458 	}
11459 
11460 	return vectors;
11461 }
11462 
11463 /**
11464  * i40e_init_msix - Setup the MSIX capability
11465  * @pf: board private structure
11466  *
11467  * Work with the OS to set up the MSIX vectors needed.
11468  *
11469  * Returns the number of vectors reserved or negative on failure
11470  **/
11471 static int i40e_init_msix(struct i40e_pf *pf)
11472 {
11473 	struct i40e_hw *hw = &pf->hw;
11474 	int cpus, extra_vectors;
11475 	int vectors_left;
11476 	int v_budget, i;
11477 	int v_actual;
11478 	int iwarp_requested = 0;
11479 
11480 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11481 		return -ENODEV;
11482 
11483 	/* The number of vectors we'll request will be comprised of:
11484 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11485 	 *   - The number of LAN queue pairs
11486 	 *	- Queues being used for RSS.
11487 	 *		We don't need as many as max_rss_size vectors.
11488 	 *		use rss_size instead in the calculation since that
11489 	 *		is governed by number of cpus in the system.
11490 	 *	- assumes symmetric Tx/Rx pairing
11491 	 *   - The number of VMDq pairs
11492 	 *   - The CPU count within the NUMA node if iWARP is enabled
11493 	 * Once we count this up, try the request.
11494 	 *
11495 	 * If we can't get what we want, we'll simplify to nearly nothing
11496 	 * and try again.  If that still fails, we punt.
11497 	 */
11498 	vectors_left = hw->func_caps.num_msix_vectors;
11499 	v_budget = 0;
11500 
11501 	/* reserve one vector for miscellaneous handler */
11502 	if (vectors_left) {
11503 		v_budget++;
11504 		vectors_left--;
11505 	}
11506 
11507 	/* reserve some vectors for the main PF traffic queues. Initially we
11508 	 * only reserve at most 50% of the available vectors, in the case that
11509 	 * the number of online CPUs is large. This ensures that we can enable
11510 	 * extra features as well. Once we've enabled the other features, we
11511 	 * will use any remaining vectors to reach as close as we can to the
11512 	 * number of online CPUs.
11513 	 */
11514 	cpus = num_online_cpus();
11515 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11516 	vectors_left -= pf->num_lan_msix;
11517 
11518 	/* reserve one vector for sideband flow director */
11519 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11520 		if (vectors_left) {
11521 			pf->num_fdsb_msix = 1;
11522 			v_budget++;
11523 			vectors_left--;
11524 		} else {
11525 			pf->num_fdsb_msix = 0;
11526 		}
11527 	}
11528 
11529 	/* can we reserve enough for iWARP? */
11530 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11531 		iwarp_requested = pf->num_iwarp_msix;
11532 
11533 		if (!vectors_left)
11534 			pf->num_iwarp_msix = 0;
11535 		else if (vectors_left < pf->num_iwarp_msix)
11536 			pf->num_iwarp_msix = 1;
11537 		v_budget += pf->num_iwarp_msix;
11538 		vectors_left -= pf->num_iwarp_msix;
11539 	}
11540 
11541 	/* any vectors left over go for VMDq support */
11542 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11543 		if (!vectors_left) {
11544 			pf->num_vmdq_msix = 0;
11545 			pf->num_vmdq_qps = 0;
11546 		} else {
11547 			int vmdq_vecs_wanted =
11548 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11549 			int vmdq_vecs =
11550 				min_t(int, vectors_left, vmdq_vecs_wanted);
11551 
11552 			/* if we're short on vectors for what's desired, we limit
11553 			 * the queues per vmdq.  If this is still more than are
11554 			 * available, the user will need to change the number of
11555 			 * queues/vectors used by the PF later with the ethtool
11556 			 * channels command
11557 			 */
11558 			if (vectors_left < vmdq_vecs_wanted) {
11559 				pf->num_vmdq_qps = 1;
11560 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11561 				vmdq_vecs = min_t(int,
11562 						  vectors_left,
11563 						  vmdq_vecs_wanted);
11564 			}
11565 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11566 
11567 			v_budget += vmdq_vecs;
11568 			vectors_left -= vmdq_vecs;
11569 		}
11570 	}
11571 
11572 	/* On systems with a large number of SMP cores, we previously limited
11573 	 * the number of vectors for num_lan_msix to be at most 50% of the
11574 	 * available vectors, to allow for other features. Now, we add back
11575 	 * the remaining vectors. However, we ensure that the total
11576 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11577 	 * calculate the number of vectors we can add without going over the
11578 	 * cap of CPUs. For systems with a small number of CPUs this will be
11579 	 * zero.
11580 	 */
11581 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11582 	pf->num_lan_msix += extra_vectors;
11583 	vectors_left -= extra_vectors;
11584 
11585 	WARN(vectors_left < 0,
11586 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11587 
11588 	v_budget += pf->num_lan_msix;
11589 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11590 				   GFP_KERNEL);
11591 	if (!pf->msix_entries)
11592 		return -ENOMEM;
11593 
11594 	for (i = 0; i < v_budget; i++)
11595 		pf->msix_entries[i].entry = i;
11596 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11597 
11598 	if (v_actual < I40E_MIN_MSIX) {
11599 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11600 		kfree(pf->msix_entries);
11601 		pf->msix_entries = NULL;
11602 		pci_disable_msix(pf->pdev);
11603 		return -ENODEV;
11604 
11605 	} else if (v_actual == I40E_MIN_MSIX) {
11606 		/* Adjust for minimal MSIX use */
11607 		pf->num_vmdq_vsis = 0;
11608 		pf->num_vmdq_qps = 0;
11609 		pf->num_lan_qps = 1;
11610 		pf->num_lan_msix = 1;
11611 
11612 	} else if (v_actual != v_budget) {
11613 		/* If we have limited resources, we will start with no vectors
11614 		 * for the special features and then allocate vectors to some
11615 		 * of these features based on the policy and at the end disable
11616 		 * the features that did not get any vectors.
11617 		 */
11618 		int vec;
11619 
11620 		dev_info(&pf->pdev->dev,
11621 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11622 			 v_actual, v_budget);
11623 		/* reserve the misc vector */
11624 		vec = v_actual - 1;
11625 
11626 		/* Scale vector usage down */
11627 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11628 		pf->num_vmdq_vsis = 1;
11629 		pf->num_vmdq_qps = 1;
11630 
11631 		/* partition out the remaining vectors */
11632 		switch (vec) {
11633 		case 2:
11634 			pf->num_lan_msix = 1;
11635 			break;
11636 		case 3:
11637 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11638 				pf->num_lan_msix = 1;
11639 				pf->num_iwarp_msix = 1;
11640 			} else {
11641 				pf->num_lan_msix = 2;
11642 			}
11643 			break;
11644 		default:
11645 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11646 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11647 						 iwarp_requested);
11648 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11649 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11650 			} else {
11651 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11652 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11653 			}
11654 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11655 				pf->num_fdsb_msix = 1;
11656 				vec--;
11657 			}
11658 			pf->num_lan_msix = min_t(int,
11659 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11660 							      pf->num_lan_msix);
11661 			pf->num_lan_qps = pf->num_lan_msix;
11662 			break;
11663 		}
11664 	}
11665 
11666 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11667 	    (pf->num_fdsb_msix == 0)) {
11668 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11669 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11670 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11671 	}
11672 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11673 	    (pf->num_vmdq_msix == 0)) {
11674 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11675 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11676 	}
11677 
11678 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11679 	    (pf->num_iwarp_msix == 0)) {
11680 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11681 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11682 	}
11683 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11684 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11685 		   pf->num_lan_msix,
11686 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11687 		   pf->num_fdsb_msix,
11688 		   pf->num_iwarp_msix);
11689 
11690 	return v_actual;
11691 }
11692 
11693 /**
11694  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11695  * @vsi: the VSI being configured
11696  * @v_idx: index of the vector in the vsi struct
11697  *
11698  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11699  **/
11700 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11701 {
11702 	struct i40e_q_vector *q_vector;
11703 
11704 	/* allocate q_vector */
11705 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11706 	if (!q_vector)
11707 		return -ENOMEM;
11708 
11709 	q_vector->vsi = vsi;
11710 	q_vector->v_idx = v_idx;
11711 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11712 
11713 	if (vsi->netdev)
11714 		netif_napi_add(vsi->netdev, &q_vector->napi,
11715 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
11716 
11717 	/* tie q_vector and vsi together */
11718 	vsi->q_vectors[v_idx] = q_vector;
11719 
11720 	return 0;
11721 }
11722 
11723 /**
11724  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11725  * @vsi: the VSI being configured
11726  *
11727  * We allocate one q_vector per queue interrupt.  If allocation fails we
11728  * return -ENOMEM.
11729  **/
11730 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11731 {
11732 	struct i40e_pf *pf = vsi->back;
11733 	int err, v_idx, num_q_vectors;
11734 
11735 	/* if not MSIX, give the one vector only to the LAN VSI */
11736 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11737 		num_q_vectors = vsi->num_q_vectors;
11738 	else if (vsi == pf->vsi[pf->lan_vsi])
11739 		num_q_vectors = 1;
11740 	else
11741 		return -EINVAL;
11742 
11743 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11744 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11745 		if (err)
11746 			goto err_out;
11747 	}
11748 
11749 	return 0;
11750 
11751 err_out:
11752 	while (v_idx--)
11753 		i40e_free_q_vector(vsi, v_idx);
11754 
11755 	return err;
11756 }
11757 
11758 /**
11759  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11760  * @pf: board private structure to initialize
11761  **/
11762 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11763 {
11764 	int vectors = 0;
11765 	ssize_t size;
11766 
11767 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11768 		vectors = i40e_init_msix(pf);
11769 		if (vectors < 0) {
11770 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11771 				       I40E_FLAG_IWARP_ENABLED	|
11772 				       I40E_FLAG_RSS_ENABLED	|
11773 				       I40E_FLAG_DCB_CAPABLE	|
11774 				       I40E_FLAG_DCB_ENABLED	|
11775 				       I40E_FLAG_SRIOV_ENABLED	|
11776 				       I40E_FLAG_FD_SB_ENABLED	|
11777 				       I40E_FLAG_FD_ATR_ENABLED	|
11778 				       I40E_FLAG_VMDQ_ENABLED);
11779 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11780 
11781 			/* rework the queue expectations without MSIX */
11782 			i40e_determine_queue_usage(pf);
11783 		}
11784 	}
11785 
11786 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11787 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
11788 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
11789 		vectors = pci_enable_msi(pf->pdev);
11790 		if (vectors < 0) {
11791 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
11792 				 vectors);
11793 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
11794 		}
11795 		vectors = 1;  /* one MSI or Legacy vector */
11796 	}
11797 
11798 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
11799 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
11800 
11801 	/* set up vector assignment tracking */
11802 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
11803 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
11804 	if (!pf->irq_pile)
11805 		return -ENOMEM;
11806 
11807 	pf->irq_pile->num_entries = vectors;
11808 
11809 	/* track first vector for misc interrupts, ignore return */
11810 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
11811 
11812 	return 0;
11813 }
11814 
11815 /**
11816  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
11817  * @pf: private board data structure
11818  *
11819  * Restore the interrupt scheme that was cleared when we suspended the
11820  * device. This should be called during resume to re-allocate the q_vectors
11821  * and reacquire IRQs.
11822  */
11823 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
11824 {
11825 	int err, i;
11826 
11827 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
11828 	 * scheme. We need to re-enabled them here in order to attempt to
11829 	 * re-acquire the MSI or MSI-X vectors
11830 	 */
11831 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
11832 
11833 	err = i40e_init_interrupt_scheme(pf);
11834 	if (err)
11835 		return err;
11836 
11837 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
11838 	 * rings together again.
11839 	 */
11840 	for (i = 0; i < pf->num_alloc_vsi; i++) {
11841 		if (pf->vsi[i]) {
11842 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
11843 			if (err)
11844 				goto err_unwind;
11845 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
11846 		}
11847 	}
11848 
11849 	err = i40e_setup_misc_vector(pf);
11850 	if (err)
11851 		goto err_unwind;
11852 
11853 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
11854 		i40e_client_update_msix_info(pf);
11855 
11856 	return 0;
11857 
11858 err_unwind:
11859 	while (i--) {
11860 		if (pf->vsi[i])
11861 			i40e_vsi_free_q_vectors(pf->vsi[i]);
11862 	}
11863 
11864 	return err;
11865 }
11866 
11867 /**
11868  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
11869  * non queue events in recovery mode
11870  * @pf: board private structure
11871  *
11872  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
11873  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
11874  * This is handled differently than in recovery mode since no Tx/Rx resources
11875  * are being allocated.
11876  **/
11877 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
11878 {
11879 	int err;
11880 
11881 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11882 		err = i40e_setup_misc_vector(pf);
11883 
11884 		if (err) {
11885 			dev_info(&pf->pdev->dev,
11886 				 "MSI-X misc vector request failed, error %d\n",
11887 				 err);
11888 			return err;
11889 		}
11890 	} else {
11891 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
11892 
11893 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
11894 				  pf->int_name, pf);
11895 
11896 		if (err) {
11897 			dev_info(&pf->pdev->dev,
11898 				 "MSI/legacy misc vector request failed, error %d\n",
11899 				 err);
11900 			return err;
11901 		}
11902 		i40e_enable_misc_int_causes(pf);
11903 		i40e_irq_dynamic_enable_icr0(pf);
11904 	}
11905 
11906 	return 0;
11907 }
11908 
11909 /**
11910  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
11911  * @pf: board private structure
11912  *
11913  * This sets up the handler for MSIX 0, which is used to manage the
11914  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
11915  * when in MSI or Legacy interrupt mode.
11916  **/
11917 static int i40e_setup_misc_vector(struct i40e_pf *pf)
11918 {
11919 	struct i40e_hw *hw = &pf->hw;
11920 	int err = 0;
11921 
11922 	/* Only request the IRQ once, the first time through. */
11923 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
11924 		err = request_irq(pf->msix_entries[0].vector,
11925 				  i40e_intr, 0, pf->int_name, pf);
11926 		if (err) {
11927 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
11928 			dev_info(&pf->pdev->dev,
11929 				 "request_irq for %s failed: %d\n",
11930 				 pf->int_name, err);
11931 			return -EFAULT;
11932 		}
11933 	}
11934 
11935 	i40e_enable_misc_int_causes(pf);
11936 
11937 	/* associate no queues to the misc vector */
11938 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
11939 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
11940 
11941 	i40e_flush(hw);
11942 
11943 	i40e_irq_dynamic_enable_icr0(pf);
11944 
11945 	return err;
11946 }
11947 
11948 /**
11949  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
11950  * @vsi: Pointer to vsi structure
11951  * @seed: Buffter to store the hash keys
11952  * @lut: Buffer to store the lookup table entries
11953  * @lut_size: Size of buffer to store the lookup table entries
11954  *
11955  * Return 0 on success, negative on failure
11956  */
11957 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
11958 			   u8 *lut, u16 lut_size)
11959 {
11960 	struct i40e_pf *pf = vsi->back;
11961 	struct i40e_hw *hw = &pf->hw;
11962 	int ret = 0;
11963 
11964 	if (seed) {
11965 		ret = i40e_aq_get_rss_key(hw, vsi->id,
11966 			(struct i40e_aqc_get_set_rss_key_data *)seed);
11967 		if (ret) {
11968 			dev_info(&pf->pdev->dev,
11969 				 "Cannot get RSS key, err %s aq_err %s\n",
11970 				 i40e_stat_str(&pf->hw, ret),
11971 				 i40e_aq_str(&pf->hw,
11972 					     pf->hw.aq.asq_last_status));
11973 			return ret;
11974 		}
11975 	}
11976 
11977 	if (lut) {
11978 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
11979 
11980 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
11981 		if (ret) {
11982 			dev_info(&pf->pdev->dev,
11983 				 "Cannot get RSS lut, err %s aq_err %s\n",
11984 				 i40e_stat_str(&pf->hw, ret),
11985 				 i40e_aq_str(&pf->hw,
11986 					     pf->hw.aq.asq_last_status));
11987 			return ret;
11988 		}
11989 	}
11990 
11991 	return ret;
11992 }
11993 
11994 /**
11995  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
11996  * @vsi: Pointer to vsi structure
11997  * @seed: RSS hash seed
11998  * @lut: Lookup table
11999  * @lut_size: Lookup table size
12000  *
12001  * Returns 0 on success, negative on failure
12002  **/
12003 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12004 			       const u8 *lut, u16 lut_size)
12005 {
12006 	struct i40e_pf *pf = vsi->back;
12007 	struct i40e_hw *hw = &pf->hw;
12008 	u16 vf_id = vsi->vf_id;
12009 	u8 i;
12010 
12011 	/* Fill out hash function seed */
12012 	if (seed) {
12013 		u32 *seed_dw = (u32 *)seed;
12014 
12015 		if (vsi->type == I40E_VSI_MAIN) {
12016 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12017 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12018 		} else if (vsi->type == I40E_VSI_SRIOV) {
12019 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12020 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12021 		} else {
12022 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12023 		}
12024 	}
12025 
12026 	if (lut) {
12027 		u32 *lut_dw = (u32 *)lut;
12028 
12029 		if (vsi->type == I40E_VSI_MAIN) {
12030 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12031 				return -EINVAL;
12032 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12033 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12034 		} else if (vsi->type == I40E_VSI_SRIOV) {
12035 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12036 				return -EINVAL;
12037 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12038 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12039 		} else {
12040 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12041 		}
12042 	}
12043 	i40e_flush(hw);
12044 
12045 	return 0;
12046 }
12047 
12048 /**
12049  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12050  * @vsi: Pointer to VSI structure
12051  * @seed: Buffer to store the keys
12052  * @lut: Buffer to store the lookup table entries
12053  * @lut_size: Size of buffer to store the lookup table entries
12054  *
12055  * Returns 0 on success, negative on failure
12056  */
12057 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12058 			    u8 *lut, u16 lut_size)
12059 {
12060 	struct i40e_pf *pf = vsi->back;
12061 	struct i40e_hw *hw = &pf->hw;
12062 	u16 i;
12063 
12064 	if (seed) {
12065 		u32 *seed_dw = (u32 *)seed;
12066 
12067 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12068 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12069 	}
12070 	if (lut) {
12071 		u32 *lut_dw = (u32 *)lut;
12072 
12073 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12074 			return -EINVAL;
12075 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12076 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12077 	}
12078 
12079 	return 0;
12080 }
12081 
12082 /**
12083  * i40e_config_rss - Configure RSS keys and lut
12084  * @vsi: Pointer to VSI structure
12085  * @seed: RSS hash seed
12086  * @lut: Lookup table
12087  * @lut_size: Lookup table size
12088  *
12089  * Returns 0 on success, negative on failure
12090  */
12091 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12092 {
12093 	struct i40e_pf *pf = vsi->back;
12094 
12095 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12096 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12097 	else
12098 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12099 }
12100 
12101 /**
12102  * i40e_get_rss - Get RSS keys and lut
12103  * @vsi: Pointer to VSI structure
12104  * @seed: Buffer to store the keys
12105  * @lut: Buffer to store the lookup table entries
12106  * @lut_size: Size of buffer to store the lookup table entries
12107  *
12108  * Returns 0 on success, negative on failure
12109  */
12110 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12111 {
12112 	struct i40e_pf *pf = vsi->back;
12113 
12114 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12115 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12116 	else
12117 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12118 }
12119 
12120 /**
12121  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12122  * @pf: Pointer to board private structure
12123  * @lut: Lookup table
12124  * @rss_table_size: Lookup table size
12125  * @rss_size: Range of queue number for hashing
12126  */
12127 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12128 		       u16 rss_table_size, u16 rss_size)
12129 {
12130 	u16 i;
12131 
12132 	for (i = 0; i < rss_table_size; i++)
12133 		lut[i] = i % rss_size;
12134 }
12135 
12136 /**
12137  * i40e_pf_config_rss - Prepare for RSS if used
12138  * @pf: board private structure
12139  **/
12140 static int i40e_pf_config_rss(struct i40e_pf *pf)
12141 {
12142 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12143 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12144 	u8 *lut;
12145 	struct i40e_hw *hw = &pf->hw;
12146 	u32 reg_val;
12147 	u64 hena;
12148 	int ret;
12149 
12150 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12151 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12152 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12153 	hena |= i40e_pf_get_default_rss_hena(pf);
12154 
12155 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12156 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12157 
12158 	/* Determine the RSS table size based on the hardware capabilities */
12159 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12160 	reg_val = (pf->rss_table_size == 512) ?
12161 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12162 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12163 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12164 
12165 	/* Determine the RSS size of the VSI */
12166 	if (!vsi->rss_size) {
12167 		u16 qcount;
12168 		/* If the firmware does something weird during VSI init, we
12169 		 * could end up with zero TCs. Check for that to avoid
12170 		 * divide-by-zero. It probably won't pass traffic, but it also
12171 		 * won't panic.
12172 		 */
12173 		qcount = vsi->num_queue_pairs /
12174 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12175 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12176 	}
12177 	if (!vsi->rss_size)
12178 		return -EINVAL;
12179 
12180 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12181 	if (!lut)
12182 		return -ENOMEM;
12183 
12184 	/* Use user configured lut if there is one, otherwise use default */
12185 	if (vsi->rss_lut_user)
12186 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12187 	else
12188 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12189 
12190 	/* Use user configured hash key if there is one, otherwise
12191 	 * use default.
12192 	 */
12193 	if (vsi->rss_hkey_user)
12194 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12195 	else
12196 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12197 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12198 	kfree(lut);
12199 
12200 	return ret;
12201 }
12202 
12203 /**
12204  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12205  * @pf: board private structure
12206  * @queue_count: the requested queue count for rss.
12207  *
12208  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12209  * count which may be different from the requested queue count.
12210  * Note: expects to be called while under rtnl_lock()
12211  **/
12212 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12213 {
12214 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12215 	int new_rss_size;
12216 
12217 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12218 		return 0;
12219 
12220 	queue_count = min_t(int, queue_count, num_online_cpus());
12221 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12222 
12223 	if (queue_count != vsi->num_queue_pairs) {
12224 		u16 qcount;
12225 
12226 		vsi->req_queue_pairs = queue_count;
12227 		i40e_prep_for_reset(pf);
12228 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12229 			return pf->alloc_rss_size;
12230 
12231 		pf->alloc_rss_size = new_rss_size;
12232 
12233 		i40e_reset_and_rebuild(pf, true, true);
12234 
12235 		/* Discard the user configured hash keys and lut, if less
12236 		 * queues are enabled.
12237 		 */
12238 		if (queue_count < vsi->rss_size) {
12239 			i40e_clear_rss_config_user(vsi);
12240 			dev_dbg(&pf->pdev->dev,
12241 				"discard user configured hash keys and lut\n");
12242 		}
12243 
12244 		/* Reset vsi->rss_size, as number of enabled queues changed */
12245 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12246 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12247 
12248 		i40e_pf_config_rss(pf);
12249 	}
12250 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12251 		 vsi->req_queue_pairs, pf->rss_size_max);
12252 	return pf->alloc_rss_size;
12253 }
12254 
12255 /**
12256  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12257  * @pf: board private structure
12258  **/
12259 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
12260 {
12261 	i40e_status status;
12262 	bool min_valid, max_valid;
12263 	u32 max_bw, min_bw;
12264 
12265 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12266 					   &min_valid, &max_valid);
12267 
12268 	if (!status) {
12269 		if (min_valid)
12270 			pf->min_bw = min_bw;
12271 		if (max_valid)
12272 			pf->max_bw = max_bw;
12273 	}
12274 
12275 	return status;
12276 }
12277 
12278 /**
12279  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12280  * @pf: board private structure
12281  **/
12282 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
12283 {
12284 	struct i40e_aqc_configure_partition_bw_data bw_data;
12285 	i40e_status status;
12286 
12287 	memset(&bw_data, 0, sizeof(bw_data));
12288 
12289 	/* Set the valid bit for this PF */
12290 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12291 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12292 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12293 
12294 	/* Set the new bandwidths */
12295 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12296 
12297 	return status;
12298 }
12299 
12300 /**
12301  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12302  * @pf: board private structure
12303  **/
12304 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12305 {
12306 	/* Commit temporary BW setting to permanent NVM image */
12307 	enum i40e_admin_queue_err last_aq_status;
12308 	i40e_status ret;
12309 	u16 nvm_word;
12310 
12311 	if (pf->hw.partition_id != 1) {
12312 		dev_info(&pf->pdev->dev,
12313 			 "Commit BW only works on partition 1! This is partition %d",
12314 			 pf->hw.partition_id);
12315 		ret = I40E_NOT_SUPPORTED;
12316 		goto bw_commit_out;
12317 	}
12318 
12319 	/* Acquire NVM for read access */
12320 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12321 	last_aq_status = pf->hw.aq.asq_last_status;
12322 	if (ret) {
12323 		dev_info(&pf->pdev->dev,
12324 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
12325 			 i40e_stat_str(&pf->hw, ret),
12326 			 i40e_aq_str(&pf->hw, last_aq_status));
12327 		goto bw_commit_out;
12328 	}
12329 
12330 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12331 	ret = i40e_aq_read_nvm(&pf->hw,
12332 			       I40E_SR_NVM_CONTROL_WORD,
12333 			       0x10, sizeof(nvm_word), &nvm_word,
12334 			       false, NULL);
12335 	/* Save off last admin queue command status before releasing
12336 	 * the NVM
12337 	 */
12338 	last_aq_status = pf->hw.aq.asq_last_status;
12339 	i40e_release_nvm(&pf->hw);
12340 	if (ret) {
12341 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
12342 			 i40e_stat_str(&pf->hw, ret),
12343 			 i40e_aq_str(&pf->hw, last_aq_status));
12344 		goto bw_commit_out;
12345 	}
12346 
12347 	/* Wait a bit for NVM release to complete */
12348 	msleep(50);
12349 
12350 	/* Acquire NVM for write access */
12351 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12352 	last_aq_status = pf->hw.aq.asq_last_status;
12353 	if (ret) {
12354 		dev_info(&pf->pdev->dev,
12355 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
12356 			 i40e_stat_str(&pf->hw, ret),
12357 			 i40e_aq_str(&pf->hw, last_aq_status));
12358 		goto bw_commit_out;
12359 	}
12360 	/* Write it back out unchanged to initiate update NVM,
12361 	 * which will force a write of the shadow (alt) RAM to
12362 	 * the NVM - thus storing the bandwidth values permanently.
12363 	 */
12364 	ret = i40e_aq_update_nvm(&pf->hw,
12365 				 I40E_SR_NVM_CONTROL_WORD,
12366 				 0x10, sizeof(nvm_word),
12367 				 &nvm_word, true, 0, NULL);
12368 	/* Save off last admin queue command status before releasing
12369 	 * the NVM
12370 	 */
12371 	last_aq_status = pf->hw.aq.asq_last_status;
12372 	i40e_release_nvm(&pf->hw);
12373 	if (ret)
12374 		dev_info(&pf->pdev->dev,
12375 			 "BW settings NOT SAVED, err %s aq_err %s\n",
12376 			 i40e_stat_str(&pf->hw, ret),
12377 			 i40e_aq_str(&pf->hw, last_aq_status));
12378 bw_commit_out:
12379 
12380 	return ret;
12381 }
12382 
12383 /**
12384  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12385  * if total port shutdown feature is enabled for this PF
12386  * @pf: board private structure
12387  **/
12388 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12389 {
12390 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12391 #define I40E_FEATURES_ENABLE_PTR		0x2A
12392 #define I40E_CURRENT_SETTING_PTR		0x2B
12393 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12394 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12395 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12396 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12397 	i40e_status read_status = I40E_SUCCESS;
12398 	u16 sr_emp_sr_settings_ptr = 0;
12399 	u16 features_enable = 0;
12400 	u16 link_behavior = 0;
12401 	bool ret = false;
12402 
12403 	read_status = i40e_read_nvm_word(&pf->hw,
12404 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12405 					 &sr_emp_sr_settings_ptr);
12406 	if (read_status)
12407 		goto err_nvm;
12408 	read_status = i40e_read_nvm_word(&pf->hw,
12409 					 sr_emp_sr_settings_ptr +
12410 					 I40E_FEATURES_ENABLE_PTR,
12411 					 &features_enable);
12412 	if (read_status)
12413 		goto err_nvm;
12414 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12415 		read_status = i40e_read_nvm_module_data(&pf->hw,
12416 							I40E_SR_EMP_SR_SETTINGS_PTR,
12417 							I40E_CURRENT_SETTING_PTR,
12418 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12419 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12420 							&link_behavior);
12421 		if (read_status)
12422 			goto err_nvm;
12423 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12424 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12425 	}
12426 	return ret;
12427 
12428 err_nvm:
12429 	dev_warn(&pf->pdev->dev,
12430 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
12431 		 i40e_stat_str(&pf->hw, read_status));
12432 	return ret;
12433 }
12434 
12435 /**
12436  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12437  * @pf: board private structure to initialize
12438  *
12439  * i40e_sw_init initializes the Adapter private data structure.
12440  * Fields are initialized based on PCI device information and
12441  * OS network device settings (MTU size).
12442  **/
12443 static int i40e_sw_init(struct i40e_pf *pf)
12444 {
12445 	int err = 0;
12446 	int size;
12447 	u16 pow;
12448 
12449 	/* Set default capability flags */
12450 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12451 		    I40E_FLAG_MSI_ENABLED     |
12452 		    I40E_FLAG_MSIX_ENABLED;
12453 
12454 	/* Set default ITR */
12455 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12456 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12457 
12458 	/* Depending on PF configurations, it is possible that the RSS
12459 	 * maximum might end up larger than the available queues
12460 	 */
12461 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12462 	pf->alloc_rss_size = 1;
12463 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12464 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12465 				 pf->hw.func_caps.num_tx_qp);
12466 
12467 	/* find the next higher power-of-2 of num cpus */
12468 	pow = roundup_pow_of_two(num_online_cpus());
12469 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12470 
12471 	if (pf->hw.func_caps.rss) {
12472 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12473 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12474 					   num_online_cpus());
12475 	}
12476 
12477 	/* MFP mode enabled */
12478 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12479 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12480 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12481 		if (i40e_get_partition_bw_setting(pf)) {
12482 			dev_warn(&pf->pdev->dev,
12483 				 "Could not get partition bw settings\n");
12484 		} else {
12485 			dev_info(&pf->pdev->dev,
12486 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12487 				 pf->min_bw, pf->max_bw);
12488 
12489 			/* nudge the Tx scheduler */
12490 			i40e_set_partition_bw_setting(pf);
12491 		}
12492 	}
12493 
12494 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12495 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12496 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12497 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12498 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12499 		    pf->hw.num_partitions > 1)
12500 			dev_info(&pf->pdev->dev,
12501 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12502 		else
12503 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12504 		pf->fdir_pf_filter_count =
12505 				 pf->hw.func_caps.fd_filters_guaranteed;
12506 		pf->hw.fdir_shared_filter_count =
12507 				 pf->hw.func_caps.fd_filters_best_effort;
12508 	}
12509 
12510 	if (pf->hw.mac.type == I40E_MAC_X722) {
12511 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12512 				    I40E_HW_128_QP_RSS_CAPABLE |
12513 				    I40E_HW_ATR_EVICT_CAPABLE |
12514 				    I40E_HW_WB_ON_ITR_CAPABLE |
12515 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12516 				    I40E_HW_NO_PCI_LINK_CHECK |
12517 				    I40E_HW_USE_SET_LLDP_MIB |
12518 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12519 				    I40E_HW_PTP_L4_CAPABLE |
12520 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12521 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12522 
12523 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12524 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12525 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12526 			dev_warn(&pf->pdev->dev,
12527 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12528 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12529 		}
12530 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12531 		   ((pf->hw.aq.api_maj_ver == 1) &&
12532 		    (pf->hw.aq.api_min_ver > 4))) {
12533 		/* Supported in FW API version higher than 1.4 */
12534 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12535 	}
12536 
12537 	/* Enable HW ATR eviction if possible */
12538 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12539 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12540 
12541 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12542 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12543 	    (pf->hw.aq.fw_maj_ver < 4))) {
12544 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12545 		/* No DCB support  for FW < v4.33 */
12546 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12547 	}
12548 
12549 	/* Disable FW LLDP if FW < v4.3 */
12550 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12551 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12552 	    (pf->hw.aq.fw_maj_ver < 4)))
12553 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12554 
12555 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12556 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12557 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12558 	    (pf->hw.aq.fw_maj_ver >= 5)))
12559 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12560 
12561 	/* Enable PTP L4 if FW > v6.0 */
12562 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12563 	    pf->hw.aq.fw_maj_ver >= 6)
12564 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12565 
12566 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12567 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12568 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12569 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12570 	}
12571 
12572 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12573 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12574 		/* IWARP needs one extra vector for CQP just like MISC.*/
12575 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12576 	}
12577 	/* Stopping FW LLDP engine is supported on XL710 and X722
12578 	 * starting from FW versions determined in i40e_init_adminq.
12579 	 * Stopping the FW LLDP engine is not supported on XL710
12580 	 * if NPAR is functioning so unset this hw flag in this case.
12581 	 */
12582 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12583 	    pf->hw.func_caps.npar_enable &&
12584 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12585 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12586 
12587 #ifdef CONFIG_PCI_IOV
12588 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12589 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12590 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12591 		pf->num_req_vfs = min_t(int,
12592 					pf->hw.func_caps.num_vfs,
12593 					I40E_MAX_VF_COUNT);
12594 	}
12595 #endif /* CONFIG_PCI_IOV */
12596 	pf->eeprom_version = 0xDEAD;
12597 	pf->lan_veb = I40E_NO_VEB;
12598 	pf->lan_vsi = I40E_NO_VSI;
12599 
12600 	/* By default FW has this off for performance reasons */
12601 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12602 
12603 	/* set up queue assignment tracking */
12604 	size = sizeof(struct i40e_lump_tracking)
12605 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12606 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12607 	if (!pf->qp_pile) {
12608 		err = -ENOMEM;
12609 		goto sw_init_done;
12610 	}
12611 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12612 
12613 	pf->tx_timeout_recovery_level = 1;
12614 
12615 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12616 	    i40e_is_total_port_shutdown_enabled(pf)) {
12617 		/* Link down on close must be on when total port shutdown
12618 		 * is enabled for a given port
12619 		 */
12620 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12621 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12622 		dev_info(&pf->pdev->dev,
12623 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12624 	}
12625 	mutex_init(&pf->switch_mutex);
12626 
12627 sw_init_done:
12628 	return err;
12629 }
12630 
12631 /**
12632  * i40e_set_ntuple - set the ntuple feature flag and take action
12633  * @pf: board private structure to initialize
12634  * @features: the feature set that the stack is suggesting
12635  *
12636  * returns a bool to indicate if reset needs to happen
12637  **/
12638 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12639 {
12640 	bool need_reset = false;
12641 
12642 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12643 	 * the state changed, we need to reset.
12644 	 */
12645 	if (features & NETIF_F_NTUPLE) {
12646 		/* Enable filters and mark for reset */
12647 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12648 			need_reset = true;
12649 		/* enable FD_SB only if there is MSI-X vector and no cloud
12650 		 * filters exist
12651 		 */
12652 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12653 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12654 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12655 		}
12656 	} else {
12657 		/* turn off filters, mark for reset and clear SW filter list */
12658 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12659 			need_reset = true;
12660 			i40e_fdir_filter_exit(pf);
12661 		}
12662 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12663 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12664 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12665 
12666 		/* reset fd counters */
12667 		pf->fd_add_err = 0;
12668 		pf->fd_atr_cnt = 0;
12669 		/* if ATR was auto disabled it can be re-enabled. */
12670 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12671 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12672 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12673 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12674 	}
12675 	return need_reset;
12676 }
12677 
12678 /**
12679  * i40e_clear_rss_lut - clear the rx hash lookup table
12680  * @vsi: the VSI being configured
12681  **/
12682 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12683 {
12684 	struct i40e_pf *pf = vsi->back;
12685 	struct i40e_hw *hw = &pf->hw;
12686 	u16 vf_id = vsi->vf_id;
12687 	u8 i;
12688 
12689 	if (vsi->type == I40E_VSI_MAIN) {
12690 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12691 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12692 	} else if (vsi->type == I40E_VSI_SRIOV) {
12693 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12694 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12695 	} else {
12696 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12697 	}
12698 }
12699 
12700 /**
12701  * i40e_set_features - set the netdev feature flags
12702  * @netdev: ptr to the netdev being adjusted
12703  * @features: the feature set that the stack is suggesting
12704  * Note: expects to be called while under rtnl_lock()
12705  **/
12706 static int i40e_set_features(struct net_device *netdev,
12707 			     netdev_features_t features)
12708 {
12709 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12710 	struct i40e_vsi *vsi = np->vsi;
12711 	struct i40e_pf *pf = vsi->back;
12712 	bool need_reset;
12713 
12714 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12715 		i40e_pf_config_rss(pf);
12716 	else if (!(features & NETIF_F_RXHASH) &&
12717 		 netdev->features & NETIF_F_RXHASH)
12718 		i40e_clear_rss_lut(vsi);
12719 
12720 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12721 		i40e_vlan_stripping_enable(vsi);
12722 	else
12723 		i40e_vlan_stripping_disable(vsi);
12724 
12725 	if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12726 		dev_err(&pf->pdev->dev,
12727 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12728 		return -EINVAL;
12729 	}
12730 
12731 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12732 		i40e_del_all_macvlans(vsi);
12733 
12734 	need_reset = i40e_set_ntuple(pf, features);
12735 
12736 	if (need_reset)
12737 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12738 
12739 	return 0;
12740 }
12741 
12742 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12743 				    unsigned int table, unsigned int idx,
12744 				    struct udp_tunnel_info *ti)
12745 {
12746 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12747 	struct i40e_hw *hw = &np->vsi->back->hw;
12748 	u8 type, filter_index;
12749 	i40e_status ret;
12750 
12751 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12752 						   I40E_AQC_TUNNEL_TYPE_NGE;
12753 
12754 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12755 				     NULL);
12756 	if (ret) {
12757 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12758 			    i40e_stat_str(hw, ret),
12759 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12760 		return -EIO;
12761 	}
12762 
12763 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12764 	return 0;
12765 }
12766 
12767 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12768 				      unsigned int table, unsigned int idx,
12769 				      struct udp_tunnel_info *ti)
12770 {
12771 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12772 	struct i40e_hw *hw = &np->vsi->back->hw;
12773 	i40e_status ret;
12774 
12775 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12776 	if (ret) {
12777 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
12778 			    i40e_stat_str(hw, ret),
12779 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12780 		return -EIO;
12781 	}
12782 
12783 	return 0;
12784 }
12785 
12786 static int i40e_get_phys_port_id(struct net_device *netdev,
12787 				 struct netdev_phys_item_id *ppid)
12788 {
12789 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12790 	struct i40e_pf *pf = np->vsi->back;
12791 	struct i40e_hw *hw = &pf->hw;
12792 
12793 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
12794 		return -EOPNOTSUPP;
12795 
12796 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
12797 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
12798 
12799 	return 0;
12800 }
12801 
12802 /**
12803  * i40e_ndo_fdb_add - add an entry to the hardware database
12804  * @ndm: the input from the stack
12805  * @tb: pointer to array of nladdr (unused)
12806  * @dev: the net device pointer
12807  * @addr: the MAC address entry being added
12808  * @vid: VLAN ID
12809  * @flags: instructions from stack about fdb operation
12810  * @extack: netlink extended ack, unused currently
12811  */
12812 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
12813 			    struct net_device *dev,
12814 			    const unsigned char *addr, u16 vid,
12815 			    u16 flags,
12816 			    struct netlink_ext_ack *extack)
12817 {
12818 	struct i40e_netdev_priv *np = netdev_priv(dev);
12819 	struct i40e_pf *pf = np->vsi->back;
12820 	int err = 0;
12821 
12822 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
12823 		return -EOPNOTSUPP;
12824 
12825 	if (vid) {
12826 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
12827 		return -EINVAL;
12828 	}
12829 
12830 	/* Hardware does not support aging addresses so if a
12831 	 * ndm_state is given only allow permanent addresses
12832 	 */
12833 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
12834 		netdev_info(dev, "FDB only supports static addresses\n");
12835 		return -EINVAL;
12836 	}
12837 
12838 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
12839 		err = dev_uc_add_excl(dev, addr);
12840 	else if (is_multicast_ether_addr(addr))
12841 		err = dev_mc_add_excl(dev, addr);
12842 	else
12843 		err = -EINVAL;
12844 
12845 	/* Only return duplicate errors if NLM_F_EXCL is set */
12846 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
12847 		err = 0;
12848 
12849 	return err;
12850 }
12851 
12852 /**
12853  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
12854  * @dev: the netdev being configured
12855  * @nlh: RTNL message
12856  * @flags: bridge flags
12857  * @extack: netlink extended ack
12858  *
12859  * Inserts a new hardware bridge if not already created and
12860  * enables the bridging mode requested (VEB or VEPA). If the
12861  * hardware bridge has already been inserted and the request
12862  * is to change the mode then that requires a PF reset to
12863  * allow rebuild of the components with required hardware
12864  * bridge mode enabled.
12865  *
12866  * Note: expects to be called while under rtnl_lock()
12867  **/
12868 static int i40e_ndo_bridge_setlink(struct net_device *dev,
12869 				   struct nlmsghdr *nlh,
12870 				   u16 flags,
12871 				   struct netlink_ext_ack *extack)
12872 {
12873 	struct i40e_netdev_priv *np = netdev_priv(dev);
12874 	struct i40e_vsi *vsi = np->vsi;
12875 	struct i40e_pf *pf = vsi->back;
12876 	struct i40e_veb *veb = NULL;
12877 	struct nlattr *attr, *br_spec;
12878 	int i, rem;
12879 
12880 	/* Only for PF VSI for now */
12881 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12882 		return -EOPNOTSUPP;
12883 
12884 	/* Find the HW bridge for PF VSI */
12885 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12886 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12887 			veb = pf->veb[i];
12888 	}
12889 
12890 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
12891 
12892 	nla_for_each_nested(attr, br_spec, rem) {
12893 		__u16 mode;
12894 
12895 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
12896 			continue;
12897 
12898 		mode = nla_get_u16(attr);
12899 		if ((mode != BRIDGE_MODE_VEPA) &&
12900 		    (mode != BRIDGE_MODE_VEB))
12901 			return -EINVAL;
12902 
12903 		/* Insert a new HW bridge */
12904 		if (!veb) {
12905 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12906 					     vsi->tc_config.enabled_tc);
12907 			if (veb) {
12908 				veb->bridge_mode = mode;
12909 				i40e_config_bridge_mode(veb);
12910 			} else {
12911 				/* No Bridge HW offload available */
12912 				return -ENOENT;
12913 			}
12914 			break;
12915 		} else if (mode != veb->bridge_mode) {
12916 			/* Existing HW bridge but different mode needs reset */
12917 			veb->bridge_mode = mode;
12918 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
12919 			if (mode == BRIDGE_MODE_VEB)
12920 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
12921 			else
12922 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12923 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12924 			break;
12925 		}
12926 	}
12927 
12928 	return 0;
12929 }
12930 
12931 /**
12932  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
12933  * @skb: skb buff
12934  * @pid: process id
12935  * @seq: RTNL message seq #
12936  * @dev: the netdev being configured
12937  * @filter_mask: unused
12938  * @nlflags: netlink flags passed in
12939  *
12940  * Return the mode in which the hardware bridge is operating in
12941  * i.e VEB or VEPA.
12942  **/
12943 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
12944 				   struct net_device *dev,
12945 				   u32 __always_unused filter_mask,
12946 				   int nlflags)
12947 {
12948 	struct i40e_netdev_priv *np = netdev_priv(dev);
12949 	struct i40e_vsi *vsi = np->vsi;
12950 	struct i40e_pf *pf = vsi->back;
12951 	struct i40e_veb *veb = NULL;
12952 	int i;
12953 
12954 	/* Only for PF VSI for now */
12955 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12956 		return -EOPNOTSUPP;
12957 
12958 	/* Find the HW bridge for the PF VSI */
12959 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12960 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12961 			veb = pf->veb[i];
12962 	}
12963 
12964 	if (!veb)
12965 		return 0;
12966 
12967 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
12968 				       0, 0, nlflags, filter_mask, NULL);
12969 }
12970 
12971 /**
12972  * i40e_features_check - Validate encapsulated packet conforms to limits
12973  * @skb: skb buff
12974  * @dev: This physical port's netdev
12975  * @features: Offload features that the stack believes apply
12976  **/
12977 static netdev_features_t i40e_features_check(struct sk_buff *skb,
12978 					     struct net_device *dev,
12979 					     netdev_features_t features)
12980 {
12981 	size_t len;
12982 
12983 	/* No point in doing any of this if neither checksum nor GSO are
12984 	 * being requested for this frame.  We can rule out both by just
12985 	 * checking for CHECKSUM_PARTIAL
12986 	 */
12987 	if (skb->ip_summed != CHECKSUM_PARTIAL)
12988 		return features;
12989 
12990 	/* We cannot support GSO if the MSS is going to be less than
12991 	 * 64 bytes.  If it is then we need to drop support for GSO.
12992 	 */
12993 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
12994 		features &= ~NETIF_F_GSO_MASK;
12995 
12996 	/* MACLEN can support at most 63 words */
12997 	len = skb_network_header(skb) - skb->data;
12998 	if (len & ~(63 * 2))
12999 		goto out_err;
13000 
13001 	/* IPLEN and EIPLEN can support at most 127 dwords */
13002 	len = skb_transport_header(skb) - skb_network_header(skb);
13003 	if (len & ~(127 * 4))
13004 		goto out_err;
13005 
13006 	if (skb->encapsulation) {
13007 		/* L4TUNLEN can support 127 words */
13008 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13009 		if (len & ~(127 * 2))
13010 			goto out_err;
13011 
13012 		/* IPLEN can support at most 127 dwords */
13013 		len = skb_inner_transport_header(skb) -
13014 		      skb_inner_network_header(skb);
13015 		if (len & ~(127 * 4))
13016 			goto out_err;
13017 	}
13018 
13019 	/* No need to validate L4LEN as TCP is the only protocol with a
13020 	 * a flexible value and we support all possible values supported
13021 	 * by TCP, which is at most 15 dwords
13022 	 */
13023 
13024 	return features;
13025 out_err:
13026 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13027 }
13028 
13029 /**
13030  * i40e_xdp_setup - add/remove an XDP program
13031  * @vsi: VSI to changed
13032  * @prog: XDP program
13033  * @extack: netlink extended ack
13034  **/
13035 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13036 			  struct netlink_ext_ack *extack)
13037 {
13038 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
13039 	struct i40e_pf *pf = vsi->back;
13040 	struct bpf_prog *old_prog;
13041 	bool need_reset;
13042 	int i;
13043 
13044 	/* Don't allow frames that span over multiple buffers */
13045 	if (frame_size > vsi->rx_buf_len) {
13046 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
13047 		return -EINVAL;
13048 	}
13049 
13050 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13051 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13052 
13053 	if (need_reset)
13054 		i40e_prep_for_reset(pf);
13055 
13056 	/* VSI shall be deleted in a moment, just return EINVAL */
13057 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13058 		return -EINVAL;
13059 
13060 	old_prog = xchg(&vsi->xdp_prog, prog);
13061 
13062 	if (need_reset) {
13063 		if (!prog)
13064 			/* Wait until ndo_xsk_wakeup completes. */
13065 			synchronize_rcu();
13066 		i40e_reset_and_rebuild(pf, true, true);
13067 	}
13068 
13069 	for (i = 0; i < vsi->num_queue_pairs; i++)
13070 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13071 
13072 	if (old_prog)
13073 		bpf_prog_put(old_prog);
13074 
13075 	/* Kick start the NAPI context if there is an AF_XDP socket open
13076 	 * on that queue id. This so that receiving will start.
13077 	 */
13078 	if (need_reset && prog)
13079 		for (i = 0; i < vsi->num_queue_pairs; i++)
13080 			if (vsi->xdp_rings[i]->xsk_pool)
13081 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13082 						      XDP_WAKEUP_RX);
13083 
13084 	return 0;
13085 }
13086 
13087 /**
13088  * i40e_enter_busy_conf - Enters busy config state
13089  * @vsi: vsi
13090  *
13091  * Returns 0 on success, <0 for failure.
13092  **/
13093 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13094 {
13095 	struct i40e_pf *pf = vsi->back;
13096 	int timeout = 50;
13097 
13098 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13099 		timeout--;
13100 		if (!timeout)
13101 			return -EBUSY;
13102 		usleep_range(1000, 2000);
13103 	}
13104 
13105 	return 0;
13106 }
13107 
13108 /**
13109  * i40e_exit_busy_conf - Exits busy config state
13110  * @vsi: vsi
13111  **/
13112 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13113 {
13114 	struct i40e_pf *pf = vsi->back;
13115 
13116 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13117 }
13118 
13119 /**
13120  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13121  * @vsi: vsi
13122  * @queue_pair: queue pair
13123  **/
13124 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13125 {
13126 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13127 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13128 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13129 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13130 	if (i40e_enabled_xdp_vsi(vsi)) {
13131 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13132 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13133 	}
13134 }
13135 
13136 /**
13137  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13138  * @vsi: vsi
13139  * @queue_pair: queue pair
13140  **/
13141 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13142 {
13143 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13144 	if (i40e_enabled_xdp_vsi(vsi)) {
13145 		/* Make sure that in-progress ndo_xdp_xmit calls are
13146 		 * completed.
13147 		 */
13148 		synchronize_rcu();
13149 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13150 	}
13151 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13152 }
13153 
13154 /**
13155  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13156  * @vsi: vsi
13157  * @queue_pair: queue pair
13158  * @enable: true for enable, false for disable
13159  **/
13160 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13161 					bool enable)
13162 {
13163 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13164 	struct i40e_q_vector *q_vector = rxr->q_vector;
13165 
13166 	if (!vsi->netdev)
13167 		return;
13168 
13169 	/* All rings in a qp belong to the same qvector. */
13170 	if (q_vector->rx.ring || q_vector->tx.ring) {
13171 		if (enable)
13172 			napi_enable(&q_vector->napi);
13173 		else
13174 			napi_disable(&q_vector->napi);
13175 	}
13176 }
13177 
13178 /**
13179  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13180  * @vsi: vsi
13181  * @queue_pair: queue pair
13182  * @enable: true for enable, false for disable
13183  *
13184  * Returns 0 on success, <0 on failure.
13185  **/
13186 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13187 					bool enable)
13188 {
13189 	struct i40e_pf *pf = vsi->back;
13190 	int pf_q, ret = 0;
13191 
13192 	pf_q = vsi->base_queue + queue_pair;
13193 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13194 				     false /*is xdp*/, enable);
13195 	if (ret) {
13196 		dev_info(&pf->pdev->dev,
13197 			 "VSI seid %d Tx ring %d %sable timeout\n",
13198 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13199 		return ret;
13200 	}
13201 
13202 	i40e_control_rx_q(pf, pf_q, enable);
13203 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13204 	if (ret) {
13205 		dev_info(&pf->pdev->dev,
13206 			 "VSI seid %d Rx ring %d %sable timeout\n",
13207 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13208 		return ret;
13209 	}
13210 
13211 	/* Due to HW errata, on Rx disable only, the register can
13212 	 * indicate done before it really is. Needs 50ms to be sure
13213 	 */
13214 	if (!enable)
13215 		mdelay(50);
13216 
13217 	if (!i40e_enabled_xdp_vsi(vsi))
13218 		return ret;
13219 
13220 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13221 				     pf_q + vsi->alloc_queue_pairs,
13222 				     true /*is xdp*/, enable);
13223 	if (ret) {
13224 		dev_info(&pf->pdev->dev,
13225 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13226 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13227 	}
13228 
13229 	return ret;
13230 }
13231 
13232 /**
13233  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13234  * @vsi: vsi
13235  * @queue_pair: queue_pair
13236  **/
13237 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13238 {
13239 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13240 	struct i40e_pf *pf = vsi->back;
13241 	struct i40e_hw *hw = &pf->hw;
13242 
13243 	/* All rings in a qp belong to the same qvector. */
13244 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13245 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13246 	else
13247 		i40e_irq_dynamic_enable_icr0(pf);
13248 
13249 	i40e_flush(hw);
13250 }
13251 
13252 /**
13253  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13254  * @vsi: vsi
13255  * @queue_pair: queue_pair
13256  **/
13257 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13258 {
13259 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13260 	struct i40e_pf *pf = vsi->back;
13261 	struct i40e_hw *hw = &pf->hw;
13262 
13263 	/* For simplicity, instead of removing the qp interrupt causes
13264 	 * from the interrupt linked list, we simply disable the interrupt, and
13265 	 * leave the list intact.
13266 	 *
13267 	 * All rings in a qp belong to the same qvector.
13268 	 */
13269 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13270 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13271 
13272 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13273 		i40e_flush(hw);
13274 		synchronize_irq(pf->msix_entries[intpf].vector);
13275 	} else {
13276 		/* Legacy and MSI mode - this stops all interrupt handling */
13277 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13278 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13279 		i40e_flush(hw);
13280 		synchronize_irq(pf->pdev->irq);
13281 	}
13282 }
13283 
13284 /**
13285  * i40e_queue_pair_disable - Disables a queue pair
13286  * @vsi: vsi
13287  * @queue_pair: queue pair
13288  *
13289  * Returns 0 on success, <0 on failure.
13290  **/
13291 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13292 {
13293 	int err;
13294 
13295 	err = i40e_enter_busy_conf(vsi);
13296 	if (err)
13297 		return err;
13298 
13299 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13300 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13301 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13302 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13303 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13304 
13305 	return err;
13306 }
13307 
13308 /**
13309  * i40e_queue_pair_enable - Enables a queue pair
13310  * @vsi: vsi
13311  * @queue_pair: queue pair
13312  *
13313  * Returns 0 on success, <0 on failure.
13314  **/
13315 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13316 {
13317 	int err;
13318 
13319 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13320 	if (err)
13321 		return err;
13322 
13323 	if (i40e_enabled_xdp_vsi(vsi)) {
13324 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13325 		if (err)
13326 			return err;
13327 	}
13328 
13329 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13330 	if (err)
13331 		return err;
13332 
13333 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13334 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13335 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13336 
13337 	i40e_exit_busy_conf(vsi);
13338 
13339 	return err;
13340 }
13341 
13342 /**
13343  * i40e_xdp - implements ndo_bpf for i40e
13344  * @dev: netdevice
13345  * @xdp: XDP command
13346  **/
13347 static int i40e_xdp(struct net_device *dev,
13348 		    struct netdev_bpf *xdp)
13349 {
13350 	struct i40e_netdev_priv *np = netdev_priv(dev);
13351 	struct i40e_vsi *vsi = np->vsi;
13352 
13353 	if (vsi->type != I40E_VSI_MAIN)
13354 		return -EINVAL;
13355 
13356 	switch (xdp->command) {
13357 	case XDP_SETUP_PROG:
13358 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13359 	case XDP_SETUP_XSK_POOL:
13360 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13361 					   xdp->xsk.queue_id);
13362 	default:
13363 		return -EINVAL;
13364 	}
13365 }
13366 
13367 static const struct net_device_ops i40e_netdev_ops = {
13368 	.ndo_open		= i40e_open,
13369 	.ndo_stop		= i40e_close,
13370 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13371 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13372 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13373 	.ndo_validate_addr	= eth_validate_addr,
13374 	.ndo_set_mac_address	= i40e_set_mac,
13375 	.ndo_change_mtu		= i40e_change_mtu,
13376 	.ndo_eth_ioctl		= i40e_ioctl,
13377 	.ndo_tx_timeout		= i40e_tx_timeout,
13378 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13379 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13380 #ifdef CONFIG_NET_POLL_CONTROLLER
13381 	.ndo_poll_controller	= i40e_netpoll,
13382 #endif
13383 	.ndo_setup_tc		= __i40e_setup_tc,
13384 	.ndo_select_queue	= i40e_lan_select_queue,
13385 	.ndo_set_features	= i40e_set_features,
13386 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13387 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13388 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13389 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13390 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13391 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13392 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13393 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13394 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13395 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13396 	.ndo_features_check	= i40e_features_check,
13397 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13398 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13399 	.ndo_bpf		= i40e_xdp,
13400 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13401 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13402 	.ndo_dfwd_add_station	= i40e_fwd_add,
13403 	.ndo_dfwd_del_station	= i40e_fwd_del,
13404 };
13405 
13406 /**
13407  * i40e_config_netdev - Setup the netdev flags
13408  * @vsi: the VSI being configured
13409  *
13410  * Returns 0 on success, negative value on failure
13411  **/
13412 static int i40e_config_netdev(struct i40e_vsi *vsi)
13413 {
13414 	struct i40e_pf *pf = vsi->back;
13415 	struct i40e_hw *hw = &pf->hw;
13416 	struct i40e_netdev_priv *np;
13417 	struct net_device *netdev;
13418 	u8 broadcast[ETH_ALEN];
13419 	u8 mac_addr[ETH_ALEN];
13420 	int etherdev_size;
13421 	netdev_features_t hw_enc_features;
13422 	netdev_features_t hw_features;
13423 
13424 	etherdev_size = sizeof(struct i40e_netdev_priv);
13425 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13426 	if (!netdev)
13427 		return -ENOMEM;
13428 
13429 	vsi->netdev = netdev;
13430 	np = netdev_priv(netdev);
13431 	np->vsi = vsi;
13432 
13433 	hw_enc_features = NETIF_F_SG			|
13434 			  NETIF_F_IP_CSUM		|
13435 			  NETIF_F_IPV6_CSUM		|
13436 			  NETIF_F_HIGHDMA		|
13437 			  NETIF_F_SOFT_FEATURES		|
13438 			  NETIF_F_TSO			|
13439 			  NETIF_F_TSO_ECN		|
13440 			  NETIF_F_TSO6			|
13441 			  NETIF_F_GSO_GRE		|
13442 			  NETIF_F_GSO_GRE_CSUM		|
13443 			  NETIF_F_GSO_PARTIAL		|
13444 			  NETIF_F_GSO_IPXIP4		|
13445 			  NETIF_F_GSO_IPXIP6		|
13446 			  NETIF_F_GSO_UDP_TUNNEL	|
13447 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13448 			  NETIF_F_GSO_UDP_L4		|
13449 			  NETIF_F_SCTP_CRC		|
13450 			  NETIF_F_RXHASH		|
13451 			  NETIF_F_RXCSUM		|
13452 			  0;
13453 
13454 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13455 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13456 
13457 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13458 
13459 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13460 
13461 	netdev->hw_enc_features |= hw_enc_features;
13462 
13463 	/* record features VLANs can make use of */
13464 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13465 
13466 	/* enable macvlan offloads */
13467 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13468 
13469 	hw_features = hw_enc_features		|
13470 		      NETIF_F_HW_VLAN_CTAG_TX	|
13471 		      NETIF_F_HW_VLAN_CTAG_RX;
13472 
13473 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13474 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13475 
13476 	netdev->hw_features |= hw_features;
13477 
13478 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13479 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13480 
13481 	if (vsi->type == I40E_VSI_MAIN) {
13482 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13483 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13484 		/* The following steps are necessary for two reasons. First,
13485 		 * some older NVM configurations load a default MAC-VLAN
13486 		 * filter that will accept any tagged packet, and we want to
13487 		 * replace this with a normal filter. Additionally, it is
13488 		 * possible our MAC address was provided by the platform using
13489 		 * Open Firmware or similar.
13490 		 *
13491 		 * Thus, we need to remove the default filter and install one
13492 		 * specific to the MAC address.
13493 		 */
13494 		i40e_rm_default_mac_filter(vsi, mac_addr);
13495 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13496 		i40e_add_mac_filter(vsi, mac_addr);
13497 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13498 	} else {
13499 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13500 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13501 		 * the end, which is 4 bytes long, so force truncation of the
13502 		 * original name by IFNAMSIZ - 4
13503 		 */
13504 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13505 			 IFNAMSIZ - 4,
13506 			 pf->vsi[pf->lan_vsi]->netdev->name);
13507 		eth_random_addr(mac_addr);
13508 
13509 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13510 		i40e_add_mac_filter(vsi, mac_addr);
13511 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13512 	}
13513 
13514 	/* Add the broadcast filter so that we initially will receive
13515 	 * broadcast packets. Note that when a new VLAN is first added the
13516 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13517 	 * specific filters as part of transitioning into "vlan" operation.
13518 	 * When more VLANs are added, the driver will copy each existing MAC
13519 	 * filter and add it for the new VLAN.
13520 	 *
13521 	 * Broadcast filters are handled specially by
13522 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13523 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13524 	 * filter. The subtask will update the correct broadcast promiscuous
13525 	 * bits as VLANs become active or inactive.
13526 	 */
13527 	eth_broadcast_addr(broadcast);
13528 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13529 	i40e_add_mac_filter(vsi, broadcast);
13530 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13531 
13532 	eth_hw_addr_set(netdev, mac_addr);
13533 	ether_addr_copy(netdev->perm_addr, mac_addr);
13534 
13535 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13536 	netdev->neigh_priv_len = sizeof(u32) * 4;
13537 
13538 	netdev->priv_flags |= IFF_UNICAST_FLT;
13539 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13540 	/* Setup netdev TC information */
13541 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13542 
13543 	netdev->netdev_ops = &i40e_netdev_ops;
13544 	netdev->watchdog_timeo = 5 * HZ;
13545 	i40e_set_ethtool_ops(netdev);
13546 
13547 	/* MTU range: 68 - 9706 */
13548 	netdev->min_mtu = ETH_MIN_MTU;
13549 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13550 
13551 	return 0;
13552 }
13553 
13554 /**
13555  * i40e_vsi_delete - Delete a VSI from the switch
13556  * @vsi: the VSI being removed
13557  *
13558  * Returns 0 on success, negative value on failure
13559  **/
13560 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13561 {
13562 	/* remove default VSI is not allowed */
13563 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13564 		return;
13565 
13566 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13567 }
13568 
13569 /**
13570  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13571  * @vsi: the VSI being queried
13572  *
13573  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13574  **/
13575 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13576 {
13577 	struct i40e_veb *veb;
13578 	struct i40e_pf *pf = vsi->back;
13579 
13580 	/* Uplink is not a bridge so default to VEB */
13581 	if (vsi->veb_idx >= I40E_MAX_VEB)
13582 		return 1;
13583 
13584 	veb = pf->veb[vsi->veb_idx];
13585 	if (!veb) {
13586 		dev_info(&pf->pdev->dev,
13587 			 "There is no veb associated with the bridge\n");
13588 		return -ENOENT;
13589 	}
13590 
13591 	/* Uplink is a bridge in VEPA mode */
13592 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13593 		return 0;
13594 	} else {
13595 		/* Uplink is a bridge in VEB mode */
13596 		return 1;
13597 	}
13598 
13599 	/* VEPA is now default bridge, so return 0 */
13600 	return 0;
13601 }
13602 
13603 /**
13604  * i40e_add_vsi - Add a VSI to the switch
13605  * @vsi: the VSI being configured
13606  *
13607  * This initializes a VSI context depending on the VSI type to be added and
13608  * passes it down to the add_vsi aq command.
13609  **/
13610 static int i40e_add_vsi(struct i40e_vsi *vsi)
13611 {
13612 	int ret = -ENODEV;
13613 	struct i40e_pf *pf = vsi->back;
13614 	struct i40e_hw *hw = &pf->hw;
13615 	struct i40e_vsi_context ctxt;
13616 	struct i40e_mac_filter *f;
13617 	struct hlist_node *h;
13618 	int bkt;
13619 
13620 	u8 enabled_tc = 0x1; /* TC0 enabled */
13621 	int f_count = 0;
13622 
13623 	memset(&ctxt, 0, sizeof(ctxt));
13624 	switch (vsi->type) {
13625 	case I40E_VSI_MAIN:
13626 		/* The PF's main VSI is already setup as part of the
13627 		 * device initialization, so we'll not bother with
13628 		 * the add_vsi call, but we will retrieve the current
13629 		 * VSI context.
13630 		 */
13631 		ctxt.seid = pf->main_vsi_seid;
13632 		ctxt.pf_num = pf->hw.pf_id;
13633 		ctxt.vf_num = 0;
13634 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13635 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13636 		if (ret) {
13637 			dev_info(&pf->pdev->dev,
13638 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13639 				 i40e_stat_str(&pf->hw, ret),
13640 				 i40e_aq_str(&pf->hw,
13641 					     pf->hw.aq.asq_last_status));
13642 			return -ENOENT;
13643 		}
13644 		vsi->info = ctxt.info;
13645 		vsi->info.valid_sections = 0;
13646 
13647 		vsi->seid = ctxt.seid;
13648 		vsi->id = ctxt.vsi_number;
13649 
13650 		enabled_tc = i40e_pf_get_tc_map(pf);
13651 
13652 		/* Source pruning is enabled by default, so the flag is
13653 		 * negative logic - if it's set, we need to fiddle with
13654 		 * the VSI to disable source pruning.
13655 		 */
13656 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13657 			memset(&ctxt, 0, sizeof(ctxt));
13658 			ctxt.seid = pf->main_vsi_seid;
13659 			ctxt.pf_num = pf->hw.pf_id;
13660 			ctxt.vf_num = 0;
13661 			ctxt.info.valid_sections |=
13662 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13663 			ctxt.info.switch_id =
13664 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13665 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13666 			if (ret) {
13667 				dev_info(&pf->pdev->dev,
13668 					 "update vsi failed, err %s aq_err %s\n",
13669 					 i40e_stat_str(&pf->hw, ret),
13670 					 i40e_aq_str(&pf->hw,
13671 						     pf->hw.aq.asq_last_status));
13672 				ret = -ENOENT;
13673 				goto err;
13674 			}
13675 		}
13676 
13677 		/* MFP mode setup queue map and update VSI */
13678 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13679 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13680 			memset(&ctxt, 0, sizeof(ctxt));
13681 			ctxt.seid = pf->main_vsi_seid;
13682 			ctxt.pf_num = pf->hw.pf_id;
13683 			ctxt.vf_num = 0;
13684 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13685 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13686 			if (ret) {
13687 				dev_info(&pf->pdev->dev,
13688 					 "update vsi failed, err %s aq_err %s\n",
13689 					 i40e_stat_str(&pf->hw, ret),
13690 					 i40e_aq_str(&pf->hw,
13691 						    pf->hw.aq.asq_last_status));
13692 				ret = -ENOENT;
13693 				goto err;
13694 			}
13695 			/* update the local VSI info queue map */
13696 			i40e_vsi_update_queue_map(vsi, &ctxt);
13697 			vsi->info.valid_sections = 0;
13698 		} else {
13699 			/* Default/Main VSI is only enabled for TC0
13700 			 * reconfigure it to enable all TCs that are
13701 			 * available on the port in SFP mode.
13702 			 * For MFP case the iSCSI PF would use this
13703 			 * flow to enable LAN+iSCSI TC.
13704 			 */
13705 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13706 			if (ret) {
13707 				/* Single TC condition is not fatal,
13708 				 * message and continue
13709 				 */
13710 				dev_info(&pf->pdev->dev,
13711 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13712 					 enabled_tc,
13713 					 i40e_stat_str(&pf->hw, ret),
13714 					 i40e_aq_str(&pf->hw,
13715 						    pf->hw.aq.asq_last_status));
13716 			}
13717 		}
13718 		break;
13719 
13720 	case I40E_VSI_FDIR:
13721 		ctxt.pf_num = hw->pf_id;
13722 		ctxt.vf_num = 0;
13723 		ctxt.uplink_seid = vsi->uplink_seid;
13724 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13725 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13726 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13727 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13728 			ctxt.info.valid_sections |=
13729 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13730 			ctxt.info.switch_id =
13731 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13732 		}
13733 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13734 		break;
13735 
13736 	case I40E_VSI_VMDQ2:
13737 		ctxt.pf_num = hw->pf_id;
13738 		ctxt.vf_num = 0;
13739 		ctxt.uplink_seid = vsi->uplink_seid;
13740 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13741 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13742 
13743 		/* This VSI is connected to VEB so the switch_id
13744 		 * should be set to zero by default.
13745 		 */
13746 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13747 			ctxt.info.valid_sections |=
13748 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13749 			ctxt.info.switch_id =
13750 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13751 		}
13752 
13753 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13754 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13755 		break;
13756 
13757 	case I40E_VSI_SRIOV:
13758 		ctxt.pf_num = hw->pf_id;
13759 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13760 		ctxt.uplink_seid = vsi->uplink_seid;
13761 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13762 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13763 
13764 		/* This VSI is connected to VEB so the switch_id
13765 		 * should be set to zero by default.
13766 		 */
13767 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13768 			ctxt.info.valid_sections |=
13769 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13770 			ctxt.info.switch_id =
13771 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13772 		}
13773 
13774 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
13775 			ctxt.info.valid_sections |=
13776 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
13777 			ctxt.info.queueing_opt_flags |=
13778 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
13779 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
13780 		}
13781 
13782 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
13783 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
13784 		if (pf->vf[vsi->vf_id].spoofchk) {
13785 			ctxt.info.valid_sections |=
13786 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
13787 			ctxt.info.sec_flags |=
13788 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
13789 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
13790 		}
13791 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13792 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13793 		break;
13794 
13795 	case I40E_VSI_IWARP:
13796 		/* send down message to iWARP */
13797 		break;
13798 
13799 	default:
13800 		return -ENODEV;
13801 	}
13802 
13803 	if (vsi->type != I40E_VSI_MAIN) {
13804 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
13805 		if (ret) {
13806 			dev_info(&vsi->back->pdev->dev,
13807 				 "add vsi failed, err %s aq_err %s\n",
13808 				 i40e_stat_str(&pf->hw, ret),
13809 				 i40e_aq_str(&pf->hw,
13810 					     pf->hw.aq.asq_last_status));
13811 			ret = -ENOENT;
13812 			goto err;
13813 		}
13814 		vsi->info = ctxt.info;
13815 		vsi->info.valid_sections = 0;
13816 		vsi->seid = ctxt.seid;
13817 		vsi->id = ctxt.vsi_number;
13818 	}
13819 
13820 	vsi->active_filters = 0;
13821 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
13822 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13823 	/* If macvlan filters already exist, force them to get loaded */
13824 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
13825 		f->state = I40E_FILTER_NEW;
13826 		f_count++;
13827 	}
13828 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13829 
13830 	if (f_count) {
13831 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
13832 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
13833 	}
13834 
13835 	/* Update VSI BW information */
13836 	ret = i40e_vsi_get_bw_info(vsi);
13837 	if (ret) {
13838 		dev_info(&pf->pdev->dev,
13839 			 "couldn't get vsi bw info, err %s aq_err %s\n",
13840 			 i40e_stat_str(&pf->hw, ret),
13841 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13842 		/* VSI is already added so not tearing that up */
13843 		ret = 0;
13844 	}
13845 
13846 err:
13847 	return ret;
13848 }
13849 
13850 /**
13851  * i40e_vsi_release - Delete a VSI and free its resources
13852  * @vsi: the VSI being removed
13853  *
13854  * Returns 0 on success or < 0 on error
13855  **/
13856 int i40e_vsi_release(struct i40e_vsi *vsi)
13857 {
13858 	struct i40e_mac_filter *f;
13859 	struct hlist_node *h;
13860 	struct i40e_veb *veb = NULL;
13861 	struct i40e_pf *pf;
13862 	u16 uplink_seid;
13863 	int i, n, bkt;
13864 
13865 	pf = vsi->back;
13866 
13867 	/* release of a VEB-owner or last VSI is not allowed */
13868 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
13869 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
13870 			 vsi->seid, vsi->uplink_seid);
13871 		return -ENODEV;
13872 	}
13873 	if (vsi == pf->vsi[pf->lan_vsi] &&
13874 	    !test_bit(__I40E_DOWN, pf->state)) {
13875 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
13876 		return -ENODEV;
13877 	}
13878 	set_bit(__I40E_VSI_RELEASING, vsi->state);
13879 	uplink_seid = vsi->uplink_seid;
13880 	if (vsi->type != I40E_VSI_SRIOV) {
13881 		if (vsi->netdev_registered) {
13882 			vsi->netdev_registered = false;
13883 			if (vsi->netdev) {
13884 				/* results in a call to i40e_close() */
13885 				unregister_netdev(vsi->netdev);
13886 			}
13887 		} else {
13888 			i40e_vsi_close(vsi);
13889 		}
13890 		i40e_vsi_disable_irq(vsi);
13891 	}
13892 
13893 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13894 
13895 	/* clear the sync flag on all filters */
13896 	if (vsi->netdev) {
13897 		__dev_uc_unsync(vsi->netdev, NULL);
13898 		__dev_mc_unsync(vsi->netdev, NULL);
13899 	}
13900 
13901 	/* make sure any remaining filters are marked for deletion */
13902 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
13903 		__i40e_del_filter(vsi, f);
13904 
13905 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13906 
13907 	i40e_sync_vsi_filters(vsi);
13908 
13909 	i40e_vsi_delete(vsi);
13910 	i40e_vsi_free_q_vectors(vsi);
13911 	if (vsi->netdev) {
13912 		free_netdev(vsi->netdev);
13913 		vsi->netdev = NULL;
13914 	}
13915 	i40e_vsi_clear_rings(vsi);
13916 	i40e_vsi_clear(vsi);
13917 
13918 	/* If this was the last thing on the VEB, except for the
13919 	 * controlling VSI, remove the VEB, which puts the controlling
13920 	 * VSI onto the next level down in the switch.
13921 	 *
13922 	 * Well, okay, there's one more exception here: don't remove
13923 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
13924 	 * from up the network stack.
13925 	 */
13926 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
13927 		if (pf->vsi[i] &&
13928 		    pf->vsi[i]->uplink_seid == uplink_seid &&
13929 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13930 			n++;      /* count the VSIs */
13931 		}
13932 	}
13933 	for (i = 0; i < I40E_MAX_VEB; i++) {
13934 		if (!pf->veb[i])
13935 			continue;
13936 		if (pf->veb[i]->uplink_seid == uplink_seid)
13937 			n++;     /* count the VEBs */
13938 		if (pf->veb[i]->seid == uplink_seid)
13939 			veb = pf->veb[i];
13940 	}
13941 	if (n == 0 && veb && veb->uplink_seid != 0)
13942 		i40e_veb_release(veb);
13943 
13944 	return 0;
13945 }
13946 
13947 /**
13948  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
13949  * @vsi: ptr to the VSI
13950  *
13951  * This should only be called after i40e_vsi_mem_alloc() which allocates the
13952  * corresponding SW VSI structure and initializes num_queue_pairs for the
13953  * newly allocated VSI.
13954  *
13955  * Returns 0 on success or negative on failure
13956  **/
13957 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
13958 {
13959 	int ret = -ENOENT;
13960 	struct i40e_pf *pf = vsi->back;
13961 
13962 	if (vsi->q_vectors[0]) {
13963 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
13964 			 vsi->seid);
13965 		return -EEXIST;
13966 	}
13967 
13968 	if (vsi->base_vector) {
13969 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
13970 			 vsi->seid, vsi->base_vector);
13971 		return -EEXIST;
13972 	}
13973 
13974 	ret = i40e_vsi_alloc_q_vectors(vsi);
13975 	if (ret) {
13976 		dev_info(&pf->pdev->dev,
13977 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
13978 			 vsi->num_q_vectors, vsi->seid, ret);
13979 		vsi->num_q_vectors = 0;
13980 		goto vector_setup_out;
13981 	}
13982 
13983 	/* In Legacy mode, we do not have to get any other vector since we
13984 	 * piggyback on the misc/ICR0 for queue interrupts.
13985 	*/
13986 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
13987 		return ret;
13988 	if (vsi->num_q_vectors)
13989 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
13990 						 vsi->num_q_vectors, vsi->idx);
13991 	if (vsi->base_vector < 0) {
13992 		dev_info(&pf->pdev->dev,
13993 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
13994 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
13995 		i40e_vsi_free_q_vectors(vsi);
13996 		ret = -ENOENT;
13997 		goto vector_setup_out;
13998 	}
13999 
14000 vector_setup_out:
14001 	return ret;
14002 }
14003 
14004 /**
14005  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14006  * @vsi: pointer to the vsi.
14007  *
14008  * This re-allocates a vsi's queue resources.
14009  *
14010  * Returns pointer to the successfully allocated and configured VSI sw struct
14011  * on success, otherwise returns NULL on failure.
14012  **/
14013 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14014 {
14015 	u16 alloc_queue_pairs;
14016 	struct i40e_pf *pf;
14017 	u8 enabled_tc;
14018 	int ret;
14019 
14020 	if (!vsi)
14021 		return NULL;
14022 
14023 	pf = vsi->back;
14024 
14025 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14026 	i40e_vsi_clear_rings(vsi);
14027 
14028 	i40e_vsi_free_arrays(vsi, false);
14029 	i40e_set_num_rings_in_vsi(vsi);
14030 	ret = i40e_vsi_alloc_arrays(vsi, false);
14031 	if (ret)
14032 		goto err_vsi;
14033 
14034 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14035 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14036 
14037 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14038 	if (ret < 0) {
14039 		dev_info(&pf->pdev->dev,
14040 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14041 			 alloc_queue_pairs, vsi->seid, ret);
14042 		goto err_vsi;
14043 	}
14044 	vsi->base_queue = ret;
14045 
14046 	/* Update the FW view of the VSI. Force a reset of TC and queue
14047 	 * layout configurations.
14048 	 */
14049 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14050 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14051 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14052 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14053 	if (vsi->type == I40E_VSI_MAIN)
14054 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14055 
14056 	/* assign it some queues */
14057 	ret = i40e_alloc_rings(vsi);
14058 	if (ret)
14059 		goto err_rings;
14060 
14061 	/* map all of the rings to the q_vectors */
14062 	i40e_vsi_map_rings_to_vectors(vsi);
14063 	return vsi;
14064 
14065 err_rings:
14066 	i40e_vsi_free_q_vectors(vsi);
14067 	if (vsi->netdev_registered) {
14068 		vsi->netdev_registered = false;
14069 		unregister_netdev(vsi->netdev);
14070 		free_netdev(vsi->netdev);
14071 		vsi->netdev = NULL;
14072 	}
14073 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14074 err_vsi:
14075 	i40e_vsi_clear(vsi);
14076 	return NULL;
14077 }
14078 
14079 /**
14080  * i40e_vsi_setup - Set up a VSI by a given type
14081  * @pf: board private structure
14082  * @type: VSI type
14083  * @uplink_seid: the switch element to link to
14084  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14085  *
14086  * This allocates the sw VSI structure and its queue resources, then add a VSI
14087  * to the identified VEB.
14088  *
14089  * Returns pointer to the successfully allocated and configure VSI sw struct on
14090  * success, otherwise returns NULL on failure.
14091  **/
14092 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14093 				u16 uplink_seid, u32 param1)
14094 {
14095 	struct i40e_vsi *vsi = NULL;
14096 	struct i40e_veb *veb = NULL;
14097 	u16 alloc_queue_pairs;
14098 	int ret, i;
14099 	int v_idx;
14100 
14101 	/* The requested uplink_seid must be either
14102 	 *     - the PF's port seid
14103 	 *              no VEB is needed because this is the PF
14104 	 *              or this is a Flow Director special case VSI
14105 	 *     - seid of an existing VEB
14106 	 *     - seid of a VSI that owns an existing VEB
14107 	 *     - seid of a VSI that doesn't own a VEB
14108 	 *              a new VEB is created and the VSI becomes the owner
14109 	 *     - seid of the PF VSI, which is what creates the first VEB
14110 	 *              this is a special case of the previous
14111 	 *
14112 	 * Find which uplink_seid we were given and create a new VEB if needed
14113 	 */
14114 	for (i = 0; i < I40E_MAX_VEB; i++) {
14115 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14116 			veb = pf->veb[i];
14117 			break;
14118 		}
14119 	}
14120 
14121 	if (!veb && uplink_seid != pf->mac_seid) {
14122 
14123 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14124 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14125 				vsi = pf->vsi[i];
14126 				break;
14127 			}
14128 		}
14129 		if (!vsi) {
14130 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14131 				 uplink_seid);
14132 			return NULL;
14133 		}
14134 
14135 		if (vsi->uplink_seid == pf->mac_seid)
14136 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14137 					     vsi->tc_config.enabled_tc);
14138 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14139 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14140 					     vsi->tc_config.enabled_tc);
14141 		if (veb) {
14142 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14143 				dev_info(&vsi->back->pdev->dev,
14144 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14145 				return NULL;
14146 			}
14147 			/* We come up by default in VEPA mode if SRIOV is not
14148 			 * already enabled, in which case we can't force VEPA
14149 			 * mode.
14150 			 */
14151 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14152 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14153 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14154 			}
14155 			i40e_config_bridge_mode(veb);
14156 		}
14157 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14158 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14159 				veb = pf->veb[i];
14160 		}
14161 		if (!veb) {
14162 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14163 			return NULL;
14164 		}
14165 
14166 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14167 		uplink_seid = veb->seid;
14168 	}
14169 
14170 	/* get vsi sw struct */
14171 	v_idx = i40e_vsi_mem_alloc(pf, type);
14172 	if (v_idx < 0)
14173 		goto err_alloc;
14174 	vsi = pf->vsi[v_idx];
14175 	if (!vsi)
14176 		goto err_alloc;
14177 	vsi->type = type;
14178 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14179 
14180 	if (type == I40E_VSI_MAIN)
14181 		pf->lan_vsi = v_idx;
14182 	else if (type == I40E_VSI_SRIOV)
14183 		vsi->vf_id = param1;
14184 	/* assign it some queues */
14185 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14186 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14187 
14188 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14189 	if (ret < 0) {
14190 		dev_info(&pf->pdev->dev,
14191 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14192 			 alloc_queue_pairs, vsi->seid, ret);
14193 		goto err_vsi;
14194 	}
14195 	vsi->base_queue = ret;
14196 
14197 	/* get a VSI from the hardware */
14198 	vsi->uplink_seid = uplink_seid;
14199 	ret = i40e_add_vsi(vsi);
14200 	if (ret)
14201 		goto err_vsi;
14202 
14203 	switch (vsi->type) {
14204 	/* setup the netdev if needed */
14205 	case I40E_VSI_MAIN:
14206 	case I40E_VSI_VMDQ2:
14207 		ret = i40e_config_netdev(vsi);
14208 		if (ret)
14209 			goto err_netdev;
14210 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14211 		if (ret)
14212 			goto err_netdev;
14213 		ret = register_netdev(vsi->netdev);
14214 		if (ret)
14215 			goto err_netdev;
14216 		vsi->netdev_registered = true;
14217 		netif_carrier_off(vsi->netdev);
14218 #ifdef CONFIG_I40E_DCB
14219 		/* Setup DCB netlink interface */
14220 		i40e_dcbnl_setup(vsi);
14221 #endif /* CONFIG_I40E_DCB */
14222 		fallthrough;
14223 	case I40E_VSI_FDIR:
14224 		/* set up vectors and rings if needed */
14225 		ret = i40e_vsi_setup_vectors(vsi);
14226 		if (ret)
14227 			goto err_msix;
14228 
14229 		ret = i40e_alloc_rings(vsi);
14230 		if (ret)
14231 			goto err_rings;
14232 
14233 		/* map all of the rings to the q_vectors */
14234 		i40e_vsi_map_rings_to_vectors(vsi);
14235 
14236 		i40e_vsi_reset_stats(vsi);
14237 		break;
14238 	default:
14239 		/* no netdev or rings for the other VSI types */
14240 		break;
14241 	}
14242 
14243 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14244 	    (vsi->type == I40E_VSI_VMDQ2)) {
14245 		ret = i40e_vsi_config_rss(vsi);
14246 	}
14247 	return vsi;
14248 
14249 err_rings:
14250 	i40e_vsi_free_q_vectors(vsi);
14251 err_msix:
14252 	if (vsi->netdev_registered) {
14253 		vsi->netdev_registered = false;
14254 		unregister_netdev(vsi->netdev);
14255 		free_netdev(vsi->netdev);
14256 		vsi->netdev = NULL;
14257 	}
14258 err_netdev:
14259 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14260 err_vsi:
14261 	i40e_vsi_clear(vsi);
14262 err_alloc:
14263 	return NULL;
14264 }
14265 
14266 /**
14267  * i40e_veb_get_bw_info - Query VEB BW information
14268  * @veb: the veb to query
14269  *
14270  * Query the Tx scheduler BW configuration data for given VEB
14271  **/
14272 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14273 {
14274 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14275 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14276 	struct i40e_pf *pf = veb->pf;
14277 	struct i40e_hw *hw = &pf->hw;
14278 	u32 tc_bw_max;
14279 	int ret = 0;
14280 	int i;
14281 
14282 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14283 						  &bw_data, NULL);
14284 	if (ret) {
14285 		dev_info(&pf->pdev->dev,
14286 			 "query veb bw config failed, err %s aq_err %s\n",
14287 			 i40e_stat_str(&pf->hw, ret),
14288 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14289 		goto out;
14290 	}
14291 
14292 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14293 						   &ets_data, NULL);
14294 	if (ret) {
14295 		dev_info(&pf->pdev->dev,
14296 			 "query veb bw ets config failed, err %s aq_err %s\n",
14297 			 i40e_stat_str(&pf->hw, ret),
14298 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14299 		goto out;
14300 	}
14301 
14302 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14303 	veb->bw_max_quanta = ets_data.tc_bw_max;
14304 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14305 	veb->enabled_tc = ets_data.tc_valid_bits;
14306 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14307 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14308 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14309 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14310 		veb->bw_tc_limit_credits[i] =
14311 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14312 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14313 	}
14314 
14315 out:
14316 	return ret;
14317 }
14318 
14319 /**
14320  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14321  * @pf: board private structure
14322  *
14323  * On error: returns error code (negative)
14324  * On success: returns vsi index in PF (positive)
14325  **/
14326 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14327 {
14328 	int ret = -ENOENT;
14329 	struct i40e_veb *veb;
14330 	int i;
14331 
14332 	/* Need to protect the allocation of switch elements at the PF level */
14333 	mutex_lock(&pf->switch_mutex);
14334 
14335 	/* VEB list may be fragmented if VEB creation/destruction has
14336 	 * been happening.  We can afford to do a quick scan to look
14337 	 * for any free slots in the list.
14338 	 *
14339 	 * find next empty veb slot, looping back around if necessary
14340 	 */
14341 	i = 0;
14342 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14343 		i++;
14344 	if (i >= I40E_MAX_VEB) {
14345 		ret = -ENOMEM;
14346 		goto err_alloc_veb;  /* out of VEB slots! */
14347 	}
14348 
14349 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14350 	if (!veb) {
14351 		ret = -ENOMEM;
14352 		goto err_alloc_veb;
14353 	}
14354 	veb->pf = pf;
14355 	veb->idx = i;
14356 	veb->enabled_tc = 1;
14357 
14358 	pf->veb[i] = veb;
14359 	ret = i;
14360 err_alloc_veb:
14361 	mutex_unlock(&pf->switch_mutex);
14362 	return ret;
14363 }
14364 
14365 /**
14366  * i40e_switch_branch_release - Delete a branch of the switch tree
14367  * @branch: where to start deleting
14368  *
14369  * This uses recursion to find the tips of the branch to be
14370  * removed, deleting until we get back to and can delete this VEB.
14371  **/
14372 static void i40e_switch_branch_release(struct i40e_veb *branch)
14373 {
14374 	struct i40e_pf *pf = branch->pf;
14375 	u16 branch_seid = branch->seid;
14376 	u16 veb_idx = branch->idx;
14377 	int i;
14378 
14379 	/* release any VEBs on this VEB - RECURSION */
14380 	for (i = 0; i < I40E_MAX_VEB; i++) {
14381 		if (!pf->veb[i])
14382 			continue;
14383 		if (pf->veb[i]->uplink_seid == branch->seid)
14384 			i40e_switch_branch_release(pf->veb[i]);
14385 	}
14386 
14387 	/* Release the VSIs on this VEB, but not the owner VSI.
14388 	 *
14389 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14390 	 *       the VEB itself, so don't use (*branch) after this loop.
14391 	 */
14392 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14393 		if (!pf->vsi[i])
14394 			continue;
14395 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14396 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14397 			i40e_vsi_release(pf->vsi[i]);
14398 		}
14399 	}
14400 
14401 	/* There's one corner case where the VEB might not have been
14402 	 * removed, so double check it here and remove it if needed.
14403 	 * This case happens if the veb was created from the debugfs
14404 	 * commands and no VSIs were added to it.
14405 	 */
14406 	if (pf->veb[veb_idx])
14407 		i40e_veb_release(pf->veb[veb_idx]);
14408 }
14409 
14410 /**
14411  * i40e_veb_clear - remove veb struct
14412  * @veb: the veb to remove
14413  **/
14414 static void i40e_veb_clear(struct i40e_veb *veb)
14415 {
14416 	if (!veb)
14417 		return;
14418 
14419 	if (veb->pf) {
14420 		struct i40e_pf *pf = veb->pf;
14421 
14422 		mutex_lock(&pf->switch_mutex);
14423 		if (pf->veb[veb->idx] == veb)
14424 			pf->veb[veb->idx] = NULL;
14425 		mutex_unlock(&pf->switch_mutex);
14426 	}
14427 
14428 	kfree(veb);
14429 }
14430 
14431 /**
14432  * i40e_veb_release - Delete a VEB and free its resources
14433  * @veb: the VEB being removed
14434  **/
14435 void i40e_veb_release(struct i40e_veb *veb)
14436 {
14437 	struct i40e_vsi *vsi = NULL;
14438 	struct i40e_pf *pf;
14439 	int i, n = 0;
14440 
14441 	pf = veb->pf;
14442 
14443 	/* find the remaining VSI and check for extras */
14444 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14445 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14446 			n++;
14447 			vsi = pf->vsi[i];
14448 		}
14449 	}
14450 	if (n != 1) {
14451 		dev_info(&pf->pdev->dev,
14452 			 "can't remove VEB %d with %d VSIs left\n",
14453 			 veb->seid, n);
14454 		return;
14455 	}
14456 
14457 	/* move the remaining VSI to uplink veb */
14458 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14459 	if (veb->uplink_seid) {
14460 		vsi->uplink_seid = veb->uplink_seid;
14461 		if (veb->uplink_seid == pf->mac_seid)
14462 			vsi->veb_idx = I40E_NO_VEB;
14463 		else
14464 			vsi->veb_idx = veb->veb_idx;
14465 	} else {
14466 		/* floating VEB */
14467 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14468 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14469 	}
14470 
14471 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14472 	i40e_veb_clear(veb);
14473 }
14474 
14475 /**
14476  * i40e_add_veb - create the VEB in the switch
14477  * @veb: the VEB to be instantiated
14478  * @vsi: the controlling VSI
14479  **/
14480 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14481 {
14482 	struct i40e_pf *pf = veb->pf;
14483 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14484 	int ret;
14485 
14486 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14487 			      veb->enabled_tc, false,
14488 			      &veb->seid, enable_stats, NULL);
14489 
14490 	/* get a VEB from the hardware */
14491 	if (ret) {
14492 		dev_info(&pf->pdev->dev,
14493 			 "couldn't add VEB, err %s aq_err %s\n",
14494 			 i40e_stat_str(&pf->hw, ret),
14495 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14496 		return -EPERM;
14497 	}
14498 
14499 	/* get statistics counter */
14500 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14501 					 &veb->stats_idx, NULL, NULL, NULL);
14502 	if (ret) {
14503 		dev_info(&pf->pdev->dev,
14504 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
14505 			 i40e_stat_str(&pf->hw, ret),
14506 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14507 		return -EPERM;
14508 	}
14509 	ret = i40e_veb_get_bw_info(veb);
14510 	if (ret) {
14511 		dev_info(&pf->pdev->dev,
14512 			 "couldn't get VEB bw info, 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 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14516 		return -ENOENT;
14517 	}
14518 
14519 	vsi->uplink_seid = veb->seid;
14520 	vsi->veb_idx = veb->idx;
14521 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14522 
14523 	return 0;
14524 }
14525 
14526 /**
14527  * i40e_veb_setup - Set up a VEB
14528  * @pf: board private structure
14529  * @flags: VEB setup flags
14530  * @uplink_seid: the switch element to link to
14531  * @vsi_seid: the initial VSI seid
14532  * @enabled_tc: Enabled TC bit-map
14533  *
14534  * This allocates the sw VEB structure and links it into the switch
14535  * It is possible and legal for this to be a duplicate of an already
14536  * existing VEB.  It is also possible for both uplink and vsi seids
14537  * to be zero, in order to create a floating VEB.
14538  *
14539  * Returns pointer to the successfully allocated VEB sw struct on
14540  * success, otherwise returns NULL on failure.
14541  **/
14542 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14543 				u16 uplink_seid, u16 vsi_seid,
14544 				u8 enabled_tc)
14545 {
14546 	struct i40e_veb *veb, *uplink_veb = NULL;
14547 	int vsi_idx, veb_idx;
14548 	int ret;
14549 
14550 	/* if one seid is 0, the other must be 0 to create a floating relay */
14551 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14552 	    (uplink_seid + vsi_seid != 0)) {
14553 		dev_info(&pf->pdev->dev,
14554 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14555 			 uplink_seid, vsi_seid);
14556 		return NULL;
14557 	}
14558 
14559 	/* make sure there is such a vsi and uplink */
14560 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14561 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14562 			break;
14563 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14564 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14565 			 vsi_seid);
14566 		return NULL;
14567 	}
14568 
14569 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14570 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14571 			if (pf->veb[veb_idx] &&
14572 			    pf->veb[veb_idx]->seid == uplink_seid) {
14573 				uplink_veb = pf->veb[veb_idx];
14574 				break;
14575 			}
14576 		}
14577 		if (!uplink_veb) {
14578 			dev_info(&pf->pdev->dev,
14579 				 "uplink seid %d not found\n", uplink_seid);
14580 			return NULL;
14581 		}
14582 	}
14583 
14584 	/* get veb sw struct */
14585 	veb_idx = i40e_veb_mem_alloc(pf);
14586 	if (veb_idx < 0)
14587 		goto err_alloc;
14588 	veb = pf->veb[veb_idx];
14589 	veb->flags = flags;
14590 	veb->uplink_seid = uplink_seid;
14591 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14592 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14593 
14594 	/* create the VEB in the switch */
14595 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14596 	if (ret)
14597 		goto err_veb;
14598 	if (vsi_idx == pf->lan_vsi)
14599 		pf->lan_veb = veb->idx;
14600 
14601 	return veb;
14602 
14603 err_veb:
14604 	i40e_veb_clear(veb);
14605 err_alloc:
14606 	return NULL;
14607 }
14608 
14609 /**
14610  * i40e_setup_pf_switch_element - set PF vars based on switch type
14611  * @pf: board private structure
14612  * @ele: element we are building info from
14613  * @num_reported: total number of elements
14614  * @printconfig: should we print the contents
14615  *
14616  * helper function to assist in extracting a few useful SEID values.
14617  **/
14618 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14619 				struct i40e_aqc_switch_config_element_resp *ele,
14620 				u16 num_reported, bool printconfig)
14621 {
14622 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14623 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14624 	u8 element_type = ele->element_type;
14625 	u16 seid = le16_to_cpu(ele->seid);
14626 
14627 	if (printconfig)
14628 		dev_info(&pf->pdev->dev,
14629 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14630 			 element_type, seid, uplink_seid, downlink_seid);
14631 
14632 	switch (element_type) {
14633 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14634 		pf->mac_seid = seid;
14635 		break;
14636 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14637 		/* Main VEB? */
14638 		if (uplink_seid != pf->mac_seid)
14639 			break;
14640 		if (pf->lan_veb >= I40E_MAX_VEB) {
14641 			int v;
14642 
14643 			/* find existing or else empty VEB */
14644 			for (v = 0; v < I40E_MAX_VEB; v++) {
14645 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14646 					pf->lan_veb = v;
14647 					break;
14648 				}
14649 			}
14650 			if (pf->lan_veb >= I40E_MAX_VEB) {
14651 				v = i40e_veb_mem_alloc(pf);
14652 				if (v < 0)
14653 					break;
14654 				pf->lan_veb = v;
14655 			}
14656 		}
14657 		if (pf->lan_veb >= I40E_MAX_VEB)
14658 			break;
14659 
14660 		pf->veb[pf->lan_veb]->seid = seid;
14661 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14662 		pf->veb[pf->lan_veb]->pf = pf;
14663 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14664 		break;
14665 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14666 		if (num_reported != 1)
14667 			break;
14668 		/* This is immediately after a reset so we can assume this is
14669 		 * the PF's VSI
14670 		 */
14671 		pf->mac_seid = uplink_seid;
14672 		pf->pf_seid = downlink_seid;
14673 		pf->main_vsi_seid = seid;
14674 		if (printconfig)
14675 			dev_info(&pf->pdev->dev,
14676 				 "pf_seid=%d main_vsi_seid=%d\n",
14677 				 pf->pf_seid, pf->main_vsi_seid);
14678 		break;
14679 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14680 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14681 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14682 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14683 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14684 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14685 		/* ignore these for now */
14686 		break;
14687 	default:
14688 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14689 			 element_type, seid);
14690 		break;
14691 	}
14692 }
14693 
14694 /**
14695  * i40e_fetch_switch_configuration - Get switch config from firmware
14696  * @pf: board private structure
14697  * @printconfig: should we print the contents
14698  *
14699  * Get the current switch configuration from the device and
14700  * extract a few useful SEID values.
14701  **/
14702 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14703 {
14704 	struct i40e_aqc_get_switch_config_resp *sw_config;
14705 	u16 next_seid = 0;
14706 	int ret = 0;
14707 	u8 *aq_buf;
14708 	int i;
14709 
14710 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14711 	if (!aq_buf)
14712 		return -ENOMEM;
14713 
14714 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14715 	do {
14716 		u16 num_reported, num_total;
14717 
14718 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14719 						I40E_AQ_LARGE_BUF,
14720 						&next_seid, NULL);
14721 		if (ret) {
14722 			dev_info(&pf->pdev->dev,
14723 				 "get switch config failed err %s aq_err %s\n",
14724 				 i40e_stat_str(&pf->hw, ret),
14725 				 i40e_aq_str(&pf->hw,
14726 					     pf->hw.aq.asq_last_status));
14727 			kfree(aq_buf);
14728 			return -ENOENT;
14729 		}
14730 
14731 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14732 		num_total = le16_to_cpu(sw_config->header.num_total);
14733 
14734 		if (printconfig)
14735 			dev_info(&pf->pdev->dev,
14736 				 "header: %d reported %d total\n",
14737 				 num_reported, num_total);
14738 
14739 		for (i = 0; i < num_reported; i++) {
14740 			struct i40e_aqc_switch_config_element_resp *ele =
14741 				&sw_config->element[i];
14742 
14743 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14744 						     printconfig);
14745 		}
14746 	} while (next_seid != 0);
14747 
14748 	kfree(aq_buf);
14749 	return ret;
14750 }
14751 
14752 /**
14753  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14754  * @pf: board private structure
14755  * @reinit: if the Main VSI needs to re-initialized.
14756  * @lock_acquired: indicates whether or not the lock has been acquired
14757  *
14758  * Returns 0 on success, negative value on failure
14759  **/
14760 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14761 {
14762 	u16 flags = 0;
14763 	int ret;
14764 
14765 	/* find out what's out there already */
14766 	ret = i40e_fetch_switch_configuration(pf, false);
14767 	if (ret) {
14768 		dev_info(&pf->pdev->dev,
14769 			 "couldn't fetch switch config, err %s aq_err %s\n",
14770 			 i40e_stat_str(&pf->hw, ret),
14771 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14772 		return ret;
14773 	}
14774 	i40e_pf_reset_stats(pf);
14775 
14776 	/* set the switch config bit for the whole device to
14777 	 * support limited promisc or true promisc
14778 	 * when user requests promisc. The default is limited
14779 	 * promisc.
14780 	*/
14781 
14782 	if ((pf->hw.pf_id == 0) &&
14783 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
14784 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14785 		pf->last_sw_conf_flags = flags;
14786 	}
14787 
14788 	if (pf->hw.pf_id == 0) {
14789 		u16 valid_flags;
14790 
14791 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14792 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
14793 						NULL);
14794 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
14795 			dev_info(&pf->pdev->dev,
14796 				 "couldn't set switch config bits, err %s aq_err %s\n",
14797 				 i40e_stat_str(&pf->hw, ret),
14798 				 i40e_aq_str(&pf->hw,
14799 					     pf->hw.aq.asq_last_status));
14800 			/* not a fatal problem, just keep going */
14801 		}
14802 		pf->last_sw_conf_valid_flags = valid_flags;
14803 	}
14804 
14805 	/* first time setup */
14806 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
14807 		struct i40e_vsi *vsi = NULL;
14808 		u16 uplink_seid;
14809 
14810 		/* Set up the PF VSI associated with the PF's main VSI
14811 		 * that is already in the HW switch
14812 		 */
14813 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
14814 			uplink_seid = pf->veb[pf->lan_veb]->seid;
14815 		else
14816 			uplink_seid = pf->mac_seid;
14817 		if (pf->lan_vsi == I40E_NO_VSI)
14818 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
14819 		else if (reinit)
14820 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
14821 		if (!vsi) {
14822 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
14823 			i40e_cloud_filter_exit(pf);
14824 			i40e_fdir_teardown(pf);
14825 			return -EAGAIN;
14826 		}
14827 	} else {
14828 		/* force a reset of TC and queue layout configurations */
14829 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14830 
14831 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14832 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14833 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14834 	}
14835 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
14836 
14837 	i40e_fdir_sb_setup(pf);
14838 
14839 	/* Setup static PF queue filter control settings */
14840 	ret = i40e_setup_pf_filter_control(pf);
14841 	if (ret) {
14842 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
14843 			 ret);
14844 		/* Failure here should not stop continuing other steps */
14845 	}
14846 
14847 	/* enable RSS in the HW, even for only one queue, as the stack can use
14848 	 * the hash
14849 	 */
14850 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
14851 		i40e_pf_config_rss(pf);
14852 
14853 	/* fill in link information and enable LSE reporting */
14854 	i40e_link_event(pf);
14855 
14856 	/* Initialize user-specific link properties */
14857 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
14858 				  I40E_AQ_AN_COMPLETED) ? true : false);
14859 
14860 	i40e_ptp_init(pf);
14861 
14862 	if (!lock_acquired)
14863 		rtnl_lock();
14864 
14865 	/* repopulate tunnel port filters */
14866 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
14867 
14868 	if (!lock_acquired)
14869 		rtnl_unlock();
14870 
14871 	return ret;
14872 }
14873 
14874 /**
14875  * i40e_determine_queue_usage - Work out queue distribution
14876  * @pf: board private structure
14877  **/
14878 static void i40e_determine_queue_usage(struct i40e_pf *pf)
14879 {
14880 	int queues_left;
14881 	int q_max;
14882 
14883 	pf->num_lan_qps = 0;
14884 
14885 	/* Find the max queues to be put into basic use.  We'll always be
14886 	 * using TC0, whether or not DCB is running, and TC0 will get the
14887 	 * big RSS set.
14888 	 */
14889 	queues_left = pf->hw.func_caps.num_tx_qp;
14890 
14891 	if ((queues_left == 1) ||
14892 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
14893 		/* one qp for PF, no queues for anything else */
14894 		queues_left = 0;
14895 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14896 
14897 		/* make sure all the fancies are disabled */
14898 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14899 			       I40E_FLAG_IWARP_ENABLED	|
14900 			       I40E_FLAG_FD_SB_ENABLED	|
14901 			       I40E_FLAG_FD_ATR_ENABLED	|
14902 			       I40E_FLAG_DCB_CAPABLE	|
14903 			       I40E_FLAG_DCB_ENABLED	|
14904 			       I40E_FLAG_SRIOV_ENABLED	|
14905 			       I40E_FLAG_VMDQ_ENABLED);
14906 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14907 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
14908 				  I40E_FLAG_FD_SB_ENABLED |
14909 				  I40E_FLAG_FD_ATR_ENABLED |
14910 				  I40E_FLAG_DCB_CAPABLE))) {
14911 		/* one qp for PF */
14912 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14913 		queues_left -= pf->num_lan_qps;
14914 
14915 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14916 			       I40E_FLAG_IWARP_ENABLED	|
14917 			       I40E_FLAG_FD_SB_ENABLED	|
14918 			       I40E_FLAG_FD_ATR_ENABLED	|
14919 			       I40E_FLAG_DCB_ENABLED	|
14920 			       I40E_FLAG_VMDQ_ENABLED);
14921 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14922 	} else {
14923 		/* Not enough queues for all TCs */
14924 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
14925 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
14926 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
14927 					I40E_FLAG_DCB_ENABLED);
14928 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
14929 		}
14930 
14931 		/* limit lan qps to the smaller of qps, cpus or msix */
14932 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
14933 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
14934 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
14935 		pf->num_lan_qps = q_max;
14936 
14937 		queues_left -= pf->num_lan_qps;
14938 	}
14939 
14940 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14941 		if (queues_left > 1) {
14942 			queues_left -= 1; /* save 1 queue for FD */
14943 		} else {
14944 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
14945 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14946 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
14947 		}
14948 	}
14949 
14950 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
14951 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
14952 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
14953 					(queues_left / pf->num_vf_qps));
14954 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
14955 	}
14956 
14957 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
14958 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
14959 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
14960 					  (queues_left / pf->num_vmdq_qps));
14961 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
14962 	}
14963 
14964 	pf->queues_left = queues_left;
14965 	dev_dbg(&pf->pdev->dev,
14966 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
14967 		pf->hw.func_caps.num_tx_qp,
14968 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
14969 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
14970 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
14971 		queues_left);
14972 }
14973 
14974 /**
14975  * i40e_setup_pf_filter_control - Setup PF static filter control
14976  * @pf: PF to be setup
14977  *
14978  * i40e_setup_pf_filter_control sets up a PF's initial filter control
14979  * settings. If PE/FCoE are enabled then it will also set the per PF
14980  * based filter sizes required for them. It also enables Flow director,
14981  * ethertype and macvlan type filter settings for the pf.
14982  *
14983  * Returns 0 on success, negative on failure
14984  **/
14985 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
14986 {
14987 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
14988 
14989 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
14990 
14991 	/* Flow Director is enabled */
14992 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
14993 		settings->enable_fdir = true;
14994 
14995 	/* Ethtype and MACVLAN filters enabled for PF */
14996 	settings->enable_ethtype = true;
14997 	settings->enable_macvlan = true;
14998 
14999 	if (i40e_set_filter_control(&pf->hw, settings))
15000 		return -ENOENT;
15001 
15002 	return 0;
15003 }
15004 
15005 #define INFO_STRING_LEN 255
15006 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15007 static void i40e_print_features(struct i40e_pf *pf)
15008 {
15009 	struct i40e_hw *hw = &pf->hw;
15010 	char *buf;
15011 	int i;
15012 
15013 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15014 	if (!buf)
15015 		return;
15016 
15017 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15018 #ifdef CONFIG_PCI_IOV
15019 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15020 #endif
15021 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15022 		      pf->hw.func_caps.num_vsis,
15023 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15024 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15025 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15026 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15027 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15028 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15029 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15030 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15031 	}
15032 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15033 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15034 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15035 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15036 	if (pf->flags & I40E_FLAG_PTP)
15037 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15038 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15039 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15040 	else
15041 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15042 
15043 	dev_info(&pf->pdev->dev, "%s\n", buf);
15044 	kfree(buf);
15045 	WARN_ON(i > INFO_STRING_LEN);
15046 }
15047 
15048 /**
15049  * i40e_get_platform_mac_addr - get platform-specific MAC address
15050  * @pdev: PCI device information struct
15051  * @pf: board private structure
15052  *
15053  * Look up the MAC address for the device. First we'll try
15054  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15055  * specific fallback. Otherwise, we'll default to the stored value in
15056  * firmware.
15057  **/
15058 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15059 {
15060 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15061 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15062 }
15063 
15064 /**
15065  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15066  * @fec_cfg: FEC option to set in flags
15067  * @flags: ptr to flags in which we set FEC option
15068  **/
15069 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15070 {
15071 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15072 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15073 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15074 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15075 		*flags |= I40E_FLAG_RS_FEC;
15076 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15077 	}
15078 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15079 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15080 		*flags |= I40E_FLAG_BASE_R_FEC;
15081 		*flags &= ~I40E_FLAG_RS_FEC;
15082 	}
15083 	if (fec_cfg == 0)
15084 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15085 }
15086 
15087 /**
15088  * i40e_check_recovery_mode - check if we are running transition firmware
15089  * @pf: board private structure
15090  *
15091  * Check registers indicating the firmware runs in recovery mode. Sets the
15092  * appropriate driver state.
15093  *
15094  * Returns true if the recovery mode was detected, false otherwise
15095  **/
15096 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15097 {
15098 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15099 
15100 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15101 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15102 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15103 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15104 
15105 		return true;
15106 	}
15107 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15108 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15109 
15110 	return false;
15111 }
15112 
15113 /**
15114  * i40e_pf_loop_reset - perform reset in a loop.
15115  * @pf: board private structure
15116  *
15117  * This function is useful when a NIC is about to enter recovery mode.
15118  * When a NIC's internal data structures are corrupted the NIC's
15119  * firmware is going to enter recovery mode.
15120  * Right after a POR it takes about 7 minutes for firmware to enter
15121  * recovery mode. Until that time a NIC is in some kind of intermediate
15122  * state. After that time period the NIC almost surely enters
15123  * recovery mode. The only way for a driver to detect intermediate
15124  * state is to issue a series of pf-resets and check a return value.
15125  * If a PF reset returns success then the firmware could be in recovery
15126  * mode so the caller of this code needs to check for recovery mode
15127  * if this function returns success. There is a little chance that
15128  * firmware will hang in intermediate state forever.
15129  * Since waiting 7 minutes is quite a lot of time this function waits
15130  * 10 seconds and then gives up by returning an error.
15131  *
15132  * Return 0 on success, negative on failure.
15133  **/
15134 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
15135 {
15136 	/* wait max 10 seconds for PF reset to succeed */
15137 	const unsigned long time_end = jiffies + 10 * HZ;
15138 
15139 	struct i40e_hw *hw = &pf->hw;
15140 	i40e_status ret;
15141 
15142 	ret = i40e_pf_reset(hw);
15143 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15144 		usleep_range(10000, 20000);
15145 		ret = i40e_pf_reset(hw);
15146 	}
15147 
15148 	if (ret == I40E_SUCCESS)
15149 		pf->pfr_count++;
15150 	else
15151 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15152 
15153 	return ret;
15154 }
15155 
15156 /**
15157  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15158  * @pf: board private structure
15159  *
15160  * Check FW registers to determine if FW issued unexpected EMP Reset.
15161  * Every time when unexpected EMP Reset occurs the FW increments
15162  * a counter of unexpected EMP Resets. When the counter reaches 10
15163  * the FW should enter the Recovery mode
15164  *
15165  * Returns true if FW issued unexpected EMP Reset
15166  **/
15167 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15168 {
15169 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15170 			   I40E_GL_FWSTS_FWS1B_MASK;
15171 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15172 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15173 }
15174 
15175 /**
15176  * i40e_handle_resets - handle EMP resets and PF resets
15177  * @pf: board private structure
15178  *
15179  * Handle both EMP resets and PF resets and conclude whether there are
15180  * any issues regarding these resets. If there are any issues then
15181  * generate log entry.
15182  *
15183  * Return 0 if NIC is healthy or negative value when there are issues
15184  * with resets
15185  **/
15186 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
15187 {
15188 	const i40e_status pfr = i40e_pf_loop_reset(pf);
15189 	const bool is_empr = i40e_check_fw_empr(pf);
15190 
15191 	if (is_empr || pfr != I40E_SUCCESS)
15192 		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");
15193 
15194 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15195 }
15196 
15197 /**
15198  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15199  * @pf: board private structure
15200  * @hw: ptr to the hardware info
15201  *
15202  * This function does a minimal setup of all subsystems needed for running
15203  * recovery mode.
15204  *
15205  * Returns 0 on success, negative on failure
15206  **/
15207 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15208 {
15209 	struct i40e_vsi *vsi;
15210 	int err;
15211 	int v_idx;
15212 
15213 	pci_save_state(pf->pdev);
15214 
15215 	/* set up periodic task facility */
15216 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15217 	pf->service_timer_period = HZ;
15218 
15219 	INIT_WORK(&pf->service_task, i40e_service_task);
15220 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15221 
15222 	err = i40e_init_interrupt_scheme(pf);
15223 	if (err)
15224 		goto err_switch_setup;
15225 
15226 	/* The number of VSIs reported by the FW is the minimum guaranteed
15227 	 * to us; HW supports far more and we share the remaining pool with
15228 	 * the other PFs. We allocate space for more than the guarantee with
15229 	 * the understanding that we might not get them all later.
15230 	 */
15231 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15232 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15233 	else
15234 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15235 
15236 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15237 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15238 			  GFP_KERNEL);
15239 	if (!pf->vsi) {
15240 		err = -ENOMEM;
15241 		goto err_switch_setup;
15242 	}
15243 
15244 	/* We allocate one VSI which is needed as absolute minimum
15245 	 * in order to register the netdev
15246 	 */
15247 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15248 	if (v_idx < 0) {
15249 		err = v_idx;
15250 		goto err_switch_setup;
15251 	}
15252 	pf->lan_vsi = v_idx;
15253 	vsi = pf->vsi[v_idx];
15254 	if (!vsi) {
15255 		err = -EFAULT;
15256 		goto err_switch_setup;
15257 	}
15258 	vsi->alloc_queue_pairs = 1;
15259 	err = i40e_config_netdev(vsi);
15260 	if (err)
15261 		goto err_switch_setup;
15262 	err = register_netdev(vsi->netdev);
15263 	if (err)
15264 		goto err_switch_setup;
15265 	vsi->netdev_registered = true;
15266 	i40e_dbg_pf_init(pf);
15267 
15268 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15269 	if (err)
15270 		goto err_switch_setup;
15271 
15272 	/* tell the firmware that we're starting */
15273 	i40e_send_version(pf);
15274 
15275 	/* since everything's happy, start the service_task timer */
15276 	mod_timer(&pf->service_timer,
15277 		  round_jiffies(jiffies + pf->service_timer_period));
15278 
15279 	return 0;
15280 
15281 err_switch_setup:
15282 	i40e_reset_interrupt_capability(pf);
15283 	del_timer_sync(&pf->service_timer);
15284 	i40e_shutdown_adminq(hw);
15285 	iounmap(hw->hw_addr);
15286 	pci_disable_pcie_error_reporting(pf->pdev);
15287 	pci_release_mem_regions(pf->pdev);
15288 	pci_disable_device(pf->pdev);
15289 	kfree(pf);
15290 
15291 	return err;
15292 }
15293 
15294 /**
15295  * i40e_set_subsystem_device_id - set subsystem device id
15296  * @hw: pointer to the hardware info
15297  *
15298  * Set PCI subsystem device id either from a pci_dev structure or
15299  * a specific FW register.
15300  **/
15301 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15302 {
15303 	struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15304 
15305 	hw->subsystem_device_id = pdev->subsystem_device ?
15306 		pdev->subsystem_device :
15307 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15308 }
15309 
15310 /**
15311  * i40e_probe - Device initialization routine
15312  * @pdev: PCI device information struct
15313  * @ent: entry in i40e_pci_tbl
15314  *
15315  * i40e_probe initializes a PF identified by a pci_dev structure.
15316  * The OS initialization, configuring of the PF private structure,
15317  * and a hardware reset occur.
15318  *
15319  * Returns 0 on success, negative on failure
15320  **/
15321 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15322 {
15323 	struct i40e_aq_get_phy_abilities_resp abilities;
15324 #ifdef CONFIG_I40E_DCB
15325 	enum i40e_get_fw_lldp_status_resp lldp_status;
15326 	i40e_status status;
15327 #endif /* CONFIG_I40E_DCB */
15328 	struct i40e_pf *pf;
15329 	struct i40e_hw *hw;
15330 	static u16 pfs_found;
15331 	u16 wol_nvm_bits;
15332 	u16 link_status;
15333 	int err;
15334 	u32 val;
15335 	u32 i;
15336 
15337 	err = pci_enable_device_mem(pdev);
15338 	if (err)
15339 		return err;
15340 
15341 	/* set up for high or low dma */
15342 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15343 	if (err) {
15344 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
15345 		if (err) {
15346 			dev_err(&pdev->dev,
15347 				"DMA configuration failed: 0x%x\n", err);
15348 			goto err_dma;
15349 		}
15350 	}
15351 
15352 	/* set up pci connections */
15353 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15354 	if (err) {
15355 		dev_info(&pdev->dev,
15356 			 "pci_request_selected_regions failed %d\n", err);
15357 		goto err_pci_reg;
15358 	}
15359 
15360 	pci_enable_pcie_error_reporting(pdev);
15361 	pci_set_master(pdev);
15362 
15363 	/* Now that we have a PCI connection, we need to do the
15364 	 * low level device setup.  This is primarily setting up
15365 	 * the Admin Queue structures and then querying for the
15366 	 * device's current profile information.
15367 	 */
15368 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15369 	if (!pf) {
15370 		err = -ENOMEM;
15371 		goto err_pf_alloc;
15372 	}
15373 	pf->next_vsi = 0;
15374 	pf->pdev = pdev;
15375 	set_bit(__I40E_DOWN, pf->state);
15376 
15377 	hw = &pf->hw;
15378 	hw->back = pf;
15379 
15380 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15381 				I40E_MAX_CSR_SPACE);
15382 	/* We believe that the highest register to read is
15383 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15384 	 * is not less than that before mapping to prevent a
15385 	 * kernel panic.
15386 	 */
15387 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15388 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15389 			pf->ioremap_len);
15390 		err = -ENOMEM;
15391 		goto err_ioremap;
15392 	}
15393 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15394 	if (!hw->hw_addr) {
15395 		err = -EIO;
15396 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15397 			 (unsigned int)pci_resource_start(pdev, 0),
15398 			 pf->ioremap_len, err);
15399 		goto err_ioremap;
15400 	}
15401 	hw->vendor_id = pdev->vendor;
15402 	hw->device_id = pdev->device;
15403 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15404 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15405 	i40e_set_subsystem_device_id(hw);
15406 	hw->bus.device = PCI_SLOT(pdev->devfn);
15407 	hw->bus.func = PCI_FUNC(pdev->devfn);
15408 	hw->bus.bus_id = pdev->bus->number;
15409 	pf->instance = pfs_found;
15410 
15411 	/* Select something other than the 802.1ad ethertype for the
15412 	 * switch to use internally and drop on ingress.
15413 	 */
15414 	hw->switch_tag = 0xffff;
15415 	hw->first_tag = ETH_P_8021AD;
15416 	hw->second_tag = ETH_P_8021Q;
15417 
15418 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15419 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15420 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15421 
15422 	/* set up the locks for the AQ, do this only once in probe
15423 	 * and destroy them only once in remove
15424 	 */
15425 	mutex_init(&hw->aq.asq_mutex);
15426 	mutex_init(&hw->aq.arq_mutex);
15427 
15428 	pf->msg_enable = netif_msg_init(debug,
15429 					NETIF_MSG_DRV |
15430 					NETIF_MSG_PROBE |
15431 					NETIF_MSG_LINK);
15432 	if (debug < -1)
15433 		pf->hw.debug_mask = debug;
15434 
15435 	/* do a special CORER for clearing PXE mode once at init */
15436 	if (hw->revision_id == 0 &&
15437 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15438 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15439 		i40e_flush(hw);
15440 		msleep(200);
15441 		pf->corer_count++;
15442 
15443 		i40e_clear_pxe_mode(hw);
15444 	}
15445 
15446 	/* Reset here to make sure all is clean and to define PF 'n' */
15447 	i40e_clear_hw(hw);
15448 
15449 	err = i40e_set_mac_type(hw);
15450 	if (err) {
15451 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15452 			 err);
15453 		goto err_pf_reset;
15454 	}
15455 
15456 	err = i40e_handle_resets(pf);
15457 	if (err)
15458 		goto err_pf_reset;
15459 
15460 	i40e_check_recovery_mode(pf);
15461 
15462 	if (is_kdump_kernel()) {
15463 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15464 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15465 	} else {
15466 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15467 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15468 	}
15469 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15470 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15471 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15472 
15473 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15474 		 "%s-%s:misc",
15475 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15476 
15477 	err = i40e_init_shared_code(hw);
15478 	if (err) {
15479 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15480 			 err);
15481 		goto err_pf_reset;
15482 	}
15483 
15484 	/* set up a default setting for link flow control */
15485 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15486 
15487 	err = i40e_init_adminq(hw);
15488 	if (err) {
15489 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15490 			dev_info(&pdev->dev,
15491 				 "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",
15492 				 hw->aq.api_maj_ver,
15493 				 hw->aq.api_min_ver,
15494 				 I40E_FW_API_VERSION_MAJOR,
15495 				 I40E_FW_MINOR_VERSION(hw));
15496 		else
15497 			dev_info(&pdev->dev,
15498 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15499 
15500 		goto err_pf_reset;
15501 	}
15502 	i40e_get_oem_version(hw);
15503 
15504 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15505 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15506 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15507 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15508 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15509 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15510 
15511 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15512 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15513 		dev_dbg(&pdev->dev,
15514 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15515 			 hw->aq.api_maj_ver,
15516 			 hw->aq.api_min_ver,
15517 			 I40E_FW_API_VERSION_MAJOR,
15518 			 I40E_FW_MINOR_VERSION(hw));
15519 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15520 		dev_info(&pdev->dev,
15521 			 "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",
15522 			 hw->aq.api_maj_ver,
15523 			 hw->aq.api_min_ver,
15524 			 I40E_FW_API_VERSION_MAJOR,
15525 			 I40E_FW_MINOR_VERSION(hw));
15526 
15527 	i40e_verify_eeprom(pf);
15528 
15529 	/* Rev 0 hardware was never productized */
15530 	if (hw->revision_id < 1)
15531 		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");
15532 
15533 	i40e_clear_pxe_mode(hw);
15534 
15535 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15536 	if (err)
15537 		goto err_adminq_setup;
15538 
15539 	err = i40e_sw_init(pf);
15540 	if (err) {
15541 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15542 		goto err_sw_init;
15543 	}
15544 
15545 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15546 		return i40e_init_recovery_mode(pf, hw);
15547 
15548 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15549 				hw->func_caps.num_rx_qp, 0, 0);
15550 	if (err) {
15551 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15552 		goto err_init_lan_hmc;
15553 	}
15554 
15555 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15556 	if (err) {
15557 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15558 		err = -ENOENT;
15559 		goto err_configure_lan_hmc;
15560 	}
15561 
15562 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15563 	 * Ignore error return codes because if it was already disabled via
15564 	 * hardware settings this will fail
15565 	 */
15566 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15567 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15568 		i40e_aq_stop_lldp(hw, true, false, NULL);
15569 	}
15570 
15571 	/* allow a platform config to override the HW addr */
15572 	i40e_get_platform_mac_addr(pdev, pf);
15573 
15574 	if (!is_valid_ether_addr(hw->mac.addr)) {
15575 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15576 		err = -EIO;
15577 		goto err_mac_addr;
15578 	}
15579 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15580 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15581 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15582 	if (is_valid_ether_addr(hw->mac.port_addr))
15583 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15584 
15585 	i40e_ptp_alloc_pins(pf);
15586 	pci_set_drvdata(pdev, pf);
15587 	pci_save_state(pdev);
15588 
15589 #ifdef CONFIG_I40E_DCB
15590 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15591 	(!status &&
15592 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15593 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15594 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15595 	dev_info(&pdev->dev,
15596 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15597 			"FW LLDP is disabled\n" :
15598 			"FW LLDP is enabled\n");
15599 
15600 	/* Enable FW to write default DCB config on link-up */
15601 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15602 
15603 	err = i40e_init_pf_dcb(pf);
15604 	if (err) {
15605 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15606 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15607 		/* Continue without DCB enabled */
15608 	}
15609 #endif /* CONFIG_I40E_DCB */
15610 
15611 	/* set up periodic task facility */
15612 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15613 	pf->service_timer_period = HZ;
15614 
15615 	INIT_WORK(&pf->service_task, i40e_service_task);
15616 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15617 
15618 	/* NVM bit on means WoL disabled for the port */
15619 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15620 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15621 		pf->wol_en = false;
15622 	else
15623 		pf->wol_en = true;
15624 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15625 
15626 	/* set up the main switch operations */
15627 	i40e_determine_queue_usage(pf);
15628 	err = i40e_init_interrupt_scheme(pf);
15629 	if (err)
15630 		goto err_switch_setup;
15631 
15632 	/* Reduce Tx and Rx pairs for kdump
15633 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15634 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15635 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15636 	 */
15637 	if (is_kdump_kernel())
15638 		pf->num_lan_msix = 1;
15639 
15640 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15641 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15642 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15643 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15644 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15645 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15646 						    UDP_TUNNEL_TYPE_GENEVE;
15647 
15648 	/* The number of VSIs reported by the FW is the minimum guaranteed
15649 	 * to us; HW supports far more and we share the remaining pool with
15650 	 * the other PFs. We allocate space for more than the guarantee with
15651 	 * the understanding that we might not get them all later.
15652 	 */
15653 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15654 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15655 	else
15656 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15657 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15658 		dev_warn(&pf->pdev->dev,
15659 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15660 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15661 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15662 	}
15663 
15664 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15665 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15666 			  GFP_KERNEL);
15667 	if (!pf->vsi) {
15668 		err = -ENOMEM;
15669 		goto err_switch_setup;
15670 	}
15671 
15672 #ifdef CONFIG_PCI_IOV
15673 	/* prep for VF support */
15674 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15675 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15676 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15677 		if (pci_num_vf(pdev))
15678 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15679 	}
15680 #endif
15681 	err = i40e_setup_pf_switch(pf, false, false);
15682 	if (err) {
15683 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15684 		goto err_vsis;
15685 	}
15686 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15687 
15688 	/* if FDIR VSI was set up, start it now */
15689 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15690 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15691 			i40e_vsi_open(pf->vsi[i]);
15692 			break;
15693 		}
15694 	}
15695 
15696 	/* The driver only wants link up/down and module qualification
15697 	 * reports from firmware.  Note the negative logic.
15698 	 */
15699 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15700 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15701 					 I40E_AQ_EVENT_MEDIA_NA |
15702 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15703 	if (err)
15704 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15705 			 i40e_stat_str(&pf->hw, err),
15706 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15707 
15708 	/* Reconfigure hardware for allowing smaller MSS in the case
15709 	 * of TSO, so that we avoid the MDD being fired and causing
15710 	 * a reset in the case of small MSS+TSO.
15711 	 */
15712 	val = rd32(hw, I40E_REG_MSS);
15713 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15714 		val &= ~I40E_REG_MSS_MIN_MASK;
15715 		val |= I40E_64BYTE_MSS;
15716 		wr32(hw, I40E_REG_MSS, val);
15717 	}
15718 
15719 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15720 		msleep(75);
15721 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15722 		if (err)
15723 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15724 				 i40e_stat_str(&pf->hw, err),
15725 				 i40e_aq_str(&pf->hw,
15726 					     pf->hw.aq.asq_last_status));
15727 	}
15728 	/* The main driver is (mostly) up and happy. We need to set this state
15729 	 * before setting up the misc vector or we get a race and the vector
15730 	 * ends up disabled forever.
15731 	 */
15732 	clear_bit(__I40E_DOWN, pf->state);
15733 
15734 	/* In case of MSIX we are going to setup the misc vector right here
15735 	 * to handle admin queue events etc. In case of legacy and MSI
15736 	 * the misc functionality and queue processing is combined in
15737 	 * the same vector and that gets setup at open.
15738 	 */
15739 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15740 		err = i40e_setup_misc_vector(pf);
15741 		if (err) {
15742 			dev_info(&pdev->dev,
15743 				 "setup of misc vector failed: %d\n", err);
15744 			i40e_cloud_filter_exit(pf);
15745 			i40e_fdir_teardown(pf);
15746 			goto err_vsis;
15747 		}
15748 	}
15749 
15750 #ifdef CONFIG_PCI_IOV
15751 	/* prep for VF support */
15752 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15753 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15754 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15755 		/* disable link interrupts for VFs */
15756 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15757 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15758 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15759 		i40e_flush(hw);
15760 
15761 		if (pci_num_vf(pdev)) {
15762 			dev_info(&pdev->dev,
15763 				 "Active VFs found, allocating resources.\n");
15764 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15765 			if (err)
15766 				dev_info(&pdev->dev,
15767 					 "Error %d allocating resources for existing VFs\n",
15768 					 err);
15769 		}
15770 	}
15771 #endif /* CONFIG_PCI_IOV */
15772 
15773 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15774 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
15775 						      pf->num_iwarp_msix,
15776 						      I40E_IWARP_IRQ_PILE_ID);
15777 		if (pf->iwarp_base_vector < 0) {
15778 			dev_info(&pdev->dev,
15779 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
15780 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
15781 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
15782 		}
15783 	}
15784 
15785 	i40e_dbg_pf_init(pf);
15786 
15787 	/* tell the firmware that we're starting */
15788 	i40e_send_version(pf);
15789 
15790 	/* since everything's happy, start the service_task timer */
15791 	mod_timer(&pf->service_timer,
15792 		  round_jiffies(jiffies + pf->service_timer_period));
15793 
15794 	/* add this PF to client device list and launch a client service task */
15795 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15796 		err = i40e_lan_add_device(pf);
15797 		if (err)
15798 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
15799 				 err);
15800 	}
15801 
15802 #define PCI_SPEED_SIZE 8
15803 #define PCI_WIDTH_SIZE 8
15804 	/* Devices on the IOSF bus do not have this information
15805 	 * and will report PCI Gen 1 x 1 by default so don't bother
15806 	 * checking them.
15807 	 */
15808 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
15809 		char speed[PCI_SPEED_SIZE] = "Unknown";
15810 		char width[PCI_WIDTH_SIZE] = "Unknown";
15811 
15812 		/* Get the negotiated link width and speed from PCI config
15813 		 * space
15814 		 */
15815 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
15816 					  &link_status);
15817 
15818 		i40e_set_pci_config_data(hw, link_status);
15819 
15820 		switch (hw->bus.speed) {
15821 		case i40e_bus_speed_8000:
15822 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
15823 		case i40e_bus_speed_5000:
15824 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
15825 		case i40e_bus_speed_2500:
15826 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
15827 		default:
15828 			break;
15829 		}
15830 		switch (hw->bus.width) {
15831 		case i40e_bus_width_pcie_x8:
15832 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
15833 		case i40e_bus_width_pcie_x4:
15834 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
15835 		case i40e_bus_width_pcie_x2:
15836 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
15837 		case i40e_bus_width_pcie_x1:
15838 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
15839 		default:
15840 			break;
15841 		}
15842 
15843 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
15844 			 speed, width);
15845 
15846 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
15847 		    hw->bus.speed < i40e_bus_speed_8000) {
15848 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
15849 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
15850 		}
15851 	}
15852 
15853 	/* get the requested speeds from the fw */
15854 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
15855 	if (err)
15856 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
15857 			i40e_stat_str(&pf->hw, err),
15858 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15859 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
15860 
15861 	/* set the FEC config due to the board capabilities */
15862 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
15863 
15864 	/* get the supported phy types from the fw */
15865 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
15866 	if (err)
15867 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
15868 			i40e_stat_str(&pf->hw, err),
15869 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15870 
15871 	/* make sure the MFS hasn't been set lower than the default */
15872 #define MAX_FRAME_SIZE_DEFAULT 0x2600
15873 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
15874 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
15875 	if (val < MAX_FRAME_SIZE_DEFAULT)
15876 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
15877 			 i, val);
15878 
15879 	/* Add a filter to drop all Flow control frames from any VSI from being
15880 	 * transmitted. By doing so we stop a malicious VF from sending out
15881 	 * PAUSE or PFC frames and potentially controlling traffic for other
15882 	 * PF/VF VSIs.
15883 	 * The FW can still send Flow control frames if enabled.
15884 	 */
15885 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
15886 						       pf->main_vsi_seid);
15887 
15888 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
15889 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
15890 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
15891 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
15892 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
15893 	/* print a string summarizing features */
15894 	i40e_print_features(pf);
15895 
15896 	return 0;
15897 
15898 	/* Unwind what we've done if something failed in the setup */
15899 err_vsis:
15900 	set_bit(__I40E_DOWN, pf->state);
15901 	i40e_clear_interrupt_scheme(pf);
15902 	kfree(pf->vsi);
15903 err_switch_setup:
15904 	i40e_reset_interrupt_capability(pf);
15905 	del_timer_sync(&pf->service_timer);
15906 err_mac_addr:
15907 err_configure_lan_hmc:
15908 	(void)i40e_shutdown_lan_hmc(hw);
15909 err_init_lan_hmc:
15910 	kfree(pf->qp_pile);
15911 err_sw_init:
15912 err_adminq_setup:
15913 err_pf_reset:
15914 	iounmap(hw->hw_addr);
15915 err_ioremap:
15916 	kfree(pf);
15917 err_pf_alloc:
15918 	pci_disable_pcie_error_reporting(pdev);
15919 	pci_release_mem_regions(pdev);
15920 err_pci_reg:
15921 err_dma:
15922 	pci_disable_device(pdev);
15923 	return err;
15924 }
15925 
15926 /**
15927  * i40e_remove - Device removal routine
15928  * @pdev: PCI device information struct
15929  *
15930  * i40e_remove is called by the PCI subsystem to alert the driver
15931  * that is should release a PCI device.  This could be caused by a
15932  * Hot-Plug event, or because the driver is going to be removed from
15933  * memory.
15934  **/
15935 static void i40e_remove(struct pci_dev *pdev)
15936 {
15937 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15938 	struct i40e_hw *hw = &pf->hw;
15939 	i40e_status ret_code;
15940 	int i;
15941 
15942 	i40e_dbg_pf_exit(pf);
15943 
15944 	i40e_ptp_stop(pf);
15945 
15946 	/* Disable RSS in hw */
15947 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
15948 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
15949 
15950 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
15951 	 * flags, once they are set, i40e_rebuild should not be called as
15952 	 * i40e_prep_for_reset always returns early.
15953 	 */
15954 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
15955 		usleep_range(1000, 2000);
15956 	set_bit(__I40E_IN_REMOVE, pf->state);
15957 
15958 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
15959 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
15960 		i40e_free_vfs(pf);
15961 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
15962 	}
15963 	/* no more scheduling of any task */
15964 	set_bit(__I40E_SUSPENDED, pf->state);
15965 	set_bit(__I40E_DOWN, pf->state);
15966 	if (pf->service_timer.function)
15967 		del_timer_sync(&pf->service_timer);
15968 	if (pf->service_task.func)
15969 		cancel_work_sync(&pf->service_task);
15970 
15971 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
15972 		struct i40e_vsi *vsi = pf->vsi[0];
15973 
15974 		/* We know that we have allocated only one vsi for this PF,
15975 		 * it was just for registering netdevice, so the interface
15976 		 * could be visible in the 'ifconfig' output
15977 		 */
15978 		unregister_netdev(vsi->netdev);
15979 		free_netdev(vsi->netdev);
15980 
15981 		goto unmap;
15982 	}
15983 
15984 	/* Client close must be called explicitly here because the timer
15985 	 * has been stopped.
15986 	 */
15987 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15988 
15989 	i40e_fdir_teardown(pf);
15990 
15991 	/* If there is a switch structure or any orphans, remove them.
15992 	 * This will leave only the PF's VSI remaining.
15993 	 */
15994 	for (i = 0; i < I40E_MAX_VEB; i++) {
15995 		if (!pf->veb[i])
15996 			continue;
15997 
15998 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
15999 		    pf->veb[i]->uplink_seid == 0)
16000 			i40e_switch_branch_release(pf->veb[i]);
16001 	}
16002 
16003 	/* Now we can shutdown the PF's VSI, just before we kill
16004 	 * adminq and hmc.
16005 	 */
16006 	if (pf->vsi[pf->lan_vsi])
16007 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
16008 
16009 	i40e_cloud_filter_exit(pf);
16010 
16011 	/* remove attached clients */
16012 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16013 		ret_code = i40e_lan_del_device(pf);
16014 		if (ret_code)
16015 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16016 				 ret_code);
16017 	}
16018 
16019 	/* shutdown and destroy the HMC */
16020 	if (hw->hmc.hmc_obj) {
16021 		ret_code = i40e_shutdown_lan_hmc(hw);
16022 		if (ret_code)
16023 			dev_warn(&pdev->dev,
16024 				 "Failed to destroy the HMC resources: %d\n",
16025 				 ret_code);
16026 	}
16027 
16028 unmap:
16029 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16030 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16031 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16032 		free_irq(pf->pdev->irq, pf);
16033 
16034 	/* shutdown the adminq */
16035 	i40e_shutdown_adminq(hw);
16036 
16037 	/* destroy the locks only once, here */
16038 	mutex_destroy(&hw->aq.arq_mutex);
16039 	mutex_destroy(&hw->aq.asq_mutex);
16040 
16041 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16042 	rtnl_lock();
16043 	i40e_clear_interrupt_scheme(pf);
16044 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16045 		if (pf->vsi[i]) {
16046 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16047 				i40e_vsi_clear_rings(pf->vsi[i]);
16048 			i40e_vsi_clear(pf->vsi[i]);
16049 			pf->vsi[i] = NULL;
16050 		}
16051 	}
16052 	rtnl_unlock();
16053 
16054 	for (i = 0; i < I40E_MAX_VEB; i++) {
16055 		kfree(pf->veb[i]);
16056 		pf->veb[i] = NULL;
16057 	}
16058 
16059 	kfree(pf->qp_pile);
16060 	kfree(pf->vsi);
16061 
16062 	iounmap(hw->hw_addr);
16063 	kfree(pf);
16064 	pci_release_mem_regions(pdev);
16065 
16066 	pci_disable_pcie_error_reporting(pdev);
16067 	pci_disable_device(pdev);
16068 }
16069 
16070 /**
16071  * i40e_pci_error_detected - warning that something funky happened in PCI land
16072  * @pdev: PCI device information struct
16073  * @error: the type of PCI error
16074  *
16075  * Called to warn that something happened and the error handling steps
16076  * are in progress.  Allows the driver to quiesce things, be ready for
16077  * remediation.
16078  **/
16079 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16080 						pci_channel_state_t error)
16081 {
16082 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16083 
16084 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16085 
16086 	if (!pf) {
16087 		dev_info(&pdev->dev,
16088 			 "Cannot recover - error happened during device probe\n");
16089 		return PCI_ERS_RESULT_DISCONNECT;
16090 	}
16091 
16092 	/* shutdown all operations */
16093 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16094 		i40e_prep_for_reset(pf);
16095 
16096 	/* Request a slot reset */
16097 	return PCI_ERS_RESULT_NEED_RESET;
16098 }
16099 
16100 /**
16101  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16102  * @pdev: PCI device information struct
16103  *
16104  * Called to find if the driver can work with the device now that
16105  * the pci slot has been reset.  If a basic connection seems good
16106  * (registers are readable and have sane content) then return a
16107  * happy little PCI_ERS_RESULT_xxx.
16108  **/
16109 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16110 {
16111 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16112 	pci_ers_result_t result;
16113 	u32 reg;
16114 
16115 	dev_dbg(&pdev->dev, "%s\n", __func__);
16116 	if (pci_enable_device_mem(pdev)) {
16117 		dev_info(&pdev->dev,
16118 			 "Cannot re-enable PCI device after reset.\n");
16119 		result = PCI_ERS_RESULT_DISCONNECT;
16120 	} else {
16121 		pci_set_master(pdev);
16122 		pci_restore_state(pdev);
16123 		pci_save_state(pdev);
16124 		pci_wake_from_d3(pdev, false);
16125 
16126 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16127 		if (reg == 0)
16128 			result = PCI_ERS_RESULT_RECOVERED;
16129 		else
16130 			result = PCI_ERS_RESULT_DISCONNECT;
16131 	}
16132 
16133 	return result;
16134 }
16135 
16136 /**
16137  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16138  * @pdev: PCI device information struct
16139  */
16140 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16141 {
16142 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16143 
16144 	i40e_prep_for_reset(pf);
16145 }
16146 
16147 /**
16148  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16149  * @pdev: PCI device information struct
16150  */
16151 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16152 {
16153 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16154 
16155 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16156 		return;
16157 
16158 	i40e_reset_and_rebuild(pf, false, false);
16159 }
16160 
16161 /**
16162  * i40e_pci_error_resume - restart operations after PCI error recovery
16163  * @pdev: PCI device information struct
16164  *
16165  * Called to allow the driver to bring things back up after PCI error
16166  * and/or reset recovery has finished.
16167  **/
16168 static void i40e_pci_error_resume(struct pci_dev *pdev)
16169 {
16170 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16171 
16172 	dev_dbg(&pdev->dev, "%s\n", __func__);
16173 	if (test_bit(__I40E_SUSPENDED, pf->state))
16174 		return;
16175 
16176 	i40e_handle_reset_warning(pf, false);
16177 }
16178 
16179 /**
16180  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16181  * using the mac_address_write admin q function
16182  * @pf: pointer to i40e_pf struct
16183  **/
16184 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16185 {
16186 	struct i40e_hw *hw = &pf->hw;
16187 	i40e_status ret;
16188 	u8 mac_addr[6];
16189 	u16 flags = 0;
16190 
16191 	/* Get current MAC address in case it's an LAA */
16192 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16193 		ether_addr_copy(mac_addr,
16194 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16195 	} else {
16196 		dev_err(&pf->pdev->dev,
16197 			"Failed to retrieve MAC address; using default\n");
16198 		ether_addr_copy(mac_addr, hw->mac.addr);
16199 	}
16200 
16201 	/* The FW expects the mac address write cmd to first be called with
16202 	 * one of these flags before calling it again with the multicast
16203 	 * enable flags.
16204 	 */
16205 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16206 
16207 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16208 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16209 
16210 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16211 	if (ret) {
16212 		dev_err(&pf->pdev->dev,
16213 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16214 		return;
16215 	}
16216 
16217 	flags = I40E_AQC_MC_MAG_EN
16218 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16219 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16220 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16221 	if (ret)
16222 		dev_err(&pf->pdev->dev,
16223 			"Failed to enable Multicast Magic Packet wake up\n");
16224 }
16225 
16226 /**
16227  * i40e_shutdown - PCI callback for shutting down
16228  * @pdev: PCI device information struct
16229  **/
16230 static void i40e_shutdown(struct pci_dev *pdev)
16231 {
16232 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16233 	struct i40e_hw *hw = &pf->hw;
16234 
16235 	set_bit(__I40E_SUSPENDED, pf->state);
16236 	set_bit(__I40E_DOWN, pf->state);
16237 
16238 	del_timer_sync(&pf->service_timer);
16239 	cancel_work_sync(&pf->service_task);
16240 	i40e_cloud_filter_exit(pf);
16241 	i40e_fdir_teardown(pf);
16242 
16243 	/* Client close must be called explicitly here because the timer
16244 	 * has been stopped.
16245 	 */
16246 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16247 
16248 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16249 		i40e_enable_mc_magic_wake(pf);
16250 
16251 	i40e_prep_for_reset(pf);
16252 
16253 	wr32(hw, I40E_PFPM_APM,
16254 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16255 	wr32(hw, I40E_PFPM_WUFC,
16256 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16257 
16258 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16259 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16260 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16261 		free_irq(pf->pdev->irq, pf);
16262 
16263 	/* Since we're going to destroy queues during the
16264 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16265 	 * whole section
16266 	 */
16267 	rtnl_lock();
16268 	i40e_clear_interrupt_scheme(pf);
16269 	rtnl_unlock();
16270 
16271 	if (system_state == SYSTEM_POWER_OFF) {
16272 		pci_wake_from_d3(pdev, pf->wol_en);
16273 		pci_set_power_state(pdev, PCI_D3hot);
16274 	}
16275 }
16276 
16277 /**
16278  * i40e_suspend - PM callback for moving to D3
16279  * @dev: generic device information structure
16280  **/
16281 static int __maybe_unused i40e_suspend(struct device *dev)
16282 {
16283 	struct i40e_pf *pf = dev_get_drvdata(dev);
16284 	struct i40e_hw *hw = &pf->hw;
16285 
16286 	/* If we're already suspended, then there is nothing to do */
16287 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16288 		return 0;
16289 
16290 	set_bit(__I40E_DOWN, pf->state);
16291 
16292 	/* Ensure service task will not be running */
16293 	del_timer_sync(&pf->service_timer);
16294 	cancel_work_sync(&pf->service_task);
16295 
16296 	/* Client close must be called explicitly here because the timer
16297 	 * has been stopped.
16298 	 */
16299 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16300 
16301 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16302 		i40e_enable_mc_magic_wake(pf);
16303 
16304 	/* Since we're going to destroy queues during the
16305 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16306 	 * whole section
16307 	 */
16308 	rtnl_lock();
16309 
16310 	i40e_prep_for_reset(pf);
16311 
16312 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16313 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16314 
16315 	/* Clear the interrupt scheme and release our IRQs so that the system
16316 	 * can safely hibernate even when there are a large number of CPUs.
16317 	 * Otherwise hibernation might fail when mapping all the vectors back
16318 	 * to CPU0.
16319 	 */
16320 	i40e_clear_interrupt_scheme(pf);
16321 
16322 	rtnl_unlock();
16323 
16324 	return 0;
16325 }
16326 
16327 /**
16328  * i40e_resume - PM callback for waking up from D3
16329  * @dev: generic device information structure
16330  **/
16331 static int __maybe_unused i40e_resume(struct device *dev)
16332 {
16333 	struct i40e_pf *pf = dev_get_drvdata(dev);
16334 	int err;
16335 
16336 	/* If we're not suspended, then there is nothing to do */
16337 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16338 		return 0;
16339 
16340 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16341 	 * since we're going to be restoring queues
16342 	 */
16343 	rtnl_lock();
16344 
16345 	/* We cleared the interrupt scheme when we suspended, so we need to
16346 	 * restore it now to resume device functionality.
16347 	 */
16348 	err = i40e_restore_interrupt_scheme(pf);
16349 	if (err) {
16350 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16351 			err);
16352 	}
16353 
16354 	clear_bit(__I40E_DOWN, pf->state);
16355 	i40e_reset_and_rebuild(pf, false, true);
16356 
16357 	rtnl_unlock();
16358 
16359 	/* Clear suspended state last after everything is recovered */
16360 	clear_bit(__I40E_SUSPENDED, pf->state);
16361 
16362 	/* Restart the service task */
16363 	mod_timer(&pf->service_timer,
16364 		  round_jiffies(jiffies + pf->service_timer_period));
16365 
16366 	return 0;
16367 }
16368 
16369 static const struct pci_error_handlers i40e_err_handler = {
16370 	.error_detected = i40e_pci_error_detected,
16371 	.slot_reset = i40e_pci_error_slot_reset,
16372 	.reset_prepare = i40e_pci_error_reset_prepare,
16373 	.reset_done = i40e_pci_error_reset_done,
16374 	.resume = i40e_pci_error_resume,
16375 };
16376 
16377 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16378 
16379 static struct pci_driver i40e_driver = {
16380 	.name     = i40e_driver_name,
16381 	.id_table = i40e_pci_tbl,
16382 	.probe    = i40e_probe,
16383 	.remove   = i40e_remove,
16384 	.driver   = {
16385 		.pm = &i40e_pm_ops,
16386 	},
16387 	.shutdown = i40e_shutdown,
16388 	.err_handler = &i40e_err_handler,
16389 	.sriov_configure = i40e_pci_sriov_configure,
16390 };
16391 
16392 /**
16393  * i40e_init_module - Driver registration routine
16394  *
16395  * i40e_init_module is the first routine called when the driver is
16396  * loaded. All it does is register with the PCI subsystem.
16397  **/
16398 static int __init i40e_init_module(void)
16399 {
16400 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16401 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16402 
16403 	/* There is no need to throttle the number of active tasks because
16404 	 * each device limits its own task using a state bit for scheduling
16405 	 * the service task, and the device tasks do not interfere with each
16406 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16407 	 * since we need to be able to guarantee forward progress even under
16408 	 * memory pressure.
16409 	 */
16410 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16411 	if (!i40e_wq) {
16412 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16413 		return -ENOMEM;
16414 	}
16415 
16416 	i40e_dbg_init();
16417 	return pci_register_driver(&i40e_driver);
16418 }
16419 module_init(i40e_init_module);
16420 
16421 /**
16422  * i40e_exit_module - Driver exit cleanup routine
16423  *
16424  * i40e_exit_module is called just before the driver is removed
16425  * from memory.
16426  **/
16427 static void __exit i40e_exit_module(void)
16428 {
16429 	pci_unregister_driver(&i40e_driver);
16430 	destroy_workqueue(i40e_wq);
16431 	ida_destroy(&i40e_client_ida);
16432 	i40e_dbg_exit();
16433 }
16434 module_exit(i40e_exit_module);
16435